File Index
Files and directories
Sign in
FILE

debugfs.c

File preview with syntax highlighting.

linux-6.19.14/drivers/thunderbolt/debugfs.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Debugfs interface
   4 *
   5 * Copyright (C) 2020, Intel Corporation
   6 * Authors: Gil Fine <gil.fine@intel.com>
   7 *	    Mika Westerberg <mika.westerberg@linux.intel.com>
   8 */
   9
  10#include <linux/array_size.h>
  11#include <linux/bitfield.h>
  12#include <linux/debugfs.h>
  13#include <linux/delay.h>
  14#include <linux/pm_runtime.h>
  15#include <linux/string_choices.h>
  16#include <linux/uaccess.h>
  17
  18#include "tb.h"
  19#include "sb_regs.h"
  20
  21#define PORT_CAP_V1_PCIE_LEN	1
  22#define PORT_CAP_V2_PCIE_LEN	2
  23#define PORT_CAP_POWER_LEN	2
  24#define PORT_CAP_LANE_LEN	3
  25#define PORT_CAP_USB3_LEN	5
  26#define PORT_CAP_DP_V1_LEN	9
  27#define PORT_CAP_DP_V2_LEN	14
  28#define PORT_CAP_TMU_V1_LEN	8
  29#define PORT_CAP_TMU_V2_LEN	10
  30#define PORT_CAP_BASIC_LEN	9
  31#define PORT_CAP_USB4_LEN	20
  32
  33#define SWITCH_CAP_TMU_LEN	26
  34#define SWITCH_CAP_BASIC_LEN	27
  35
  36#define PATH_LEN		2
  37
  38#define COUNTER_SET_LEN		3
  39
  40/*
  41 * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms
  42 * probed to give good results.
  43 */
  44#define MIN_DWELL_TIME		100 /* ms */
  45#define MAX_DWELL_TIME		500 /* ms */
  46#define DWELL_SAMPLE_INTERVAL	10
  47
  48enum usb4_margin_cap_voltage_indp {
  49	USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN,
  50	USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL,
  51	USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH,
  52	USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN,
  53	USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH,
  54	USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN,
  55};
  56
  57enum usb4_margin_cap_time_indp {
  58	USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN,
  59	USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR,
  60	USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH,
  61	USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN,
  62	USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH,
  63	USB4_MARGIN_CAP_TIME_INDP_UNKNOWN,
  64};
  65
  66/* Sideband registers and their sizes as defined in the USB4 spec */
  67struct sb_reg {
  68	unsigned int reg;
  69	unsigned int size;
  70};
  71
  72#define SB_MAX_SIZE		64
  73
  74/* Sideband registers for router */
  75static const struct sb_reg port_sb_regs[] = {
  76	{ USB4_SB_VENDOR_ID, 4 },
  77	{ USB4_SB_PRODUCT_ID, 4 },
  78	{ USB4_SB_DEBUG_CONF, 4 },
  79	{ USB4_SB_DEBUG, 54 },
  80	{ USB4_SB_LRD_TUNING, 4 },
  81	{ USB4_SB_OPCODE, 4 },
  82	{ USB4_SB_METADATA, 4 },
  83	{ USB4_SB_LINK_CONF, 3 },
  84	{ USB4_SB_GEN23_TXFFE, 4 },
  85	{ USB4_SB_GEN4_TXFFE, 4 },
  86	{ USB4_SB_VERSION, 4 },
  87	{ USB4_SB_DATA, 64 },
  88};
  89
  90/* Sideband registers for retimer */
  91static const struct sb_reg retimer_sb_regs[] = {
  92	{ USB4_SB_VENDOR_ID, 4 },
  93	{ USB4_SB_PRODUCT_ID, 4 },
  94	{ USB4_SB_FW_VERSION, 4 },
  95	{ USB4_SB_LRD_TUNING, 4 },
  96	{ USB4_SB_OPCODE, 4 },
  97	{ USB4_SB_METADATA, 4 },
  98	{ USB4_SB_GEN23_TXFFE, 4 },
  99	{ USB4_SB_GEN4_TXFFE, 4 },
 100	{ USB4_SB_VERSION, 4 },
 101	{ USB4_SB_DATA, 64 },
 102};
 103
 104#define DEBUGFS_ATTR(__space, __write)					\
 105static int __space ## _open(struct inode *inode, struct file *file)	\
 106{									\
 107	return single_open(file, __space ## _show, inode->i_private);	\
 108}									\
 109									\
 110static const struct file_operations __space ## _fops = {		\
 111	.owner = THIS_MODULE,						\
 112	.open = __space ## _open,					\
 113	.release = single_release,					\
 114	.read  = seq_read,						\
 115	.write = __write,						\
 116	.llseek = seq_lseek,						\
 117}
 118
 119#define DEBUGFS_ATTR_RO(__space)					\
 120	DEBUGFS_ATTR(__space, NULL)
 121
 122#define DEBUGFS_ATTR_RW(__space)					\
 123	DEBUGFS_ATTR(__space, __space ## _write)
 124
 125static struct dentry *tb_debugfs_root;
 126
 127static void *validate_and_copy_from_user(const void __user *user_buf,
 128					 size_t *count)
 129{
 130	size_t nbytes;
 131	void *buf;
 132
 133	if (!*count)
 134		return ERR_PTR(-EINVAL);
 135
 136	if (!access_ok(user_buf, *count))
 137		return ERR_PTR(-EFAULT);
 138
 139	buf = (void *)get_zeroed_page(GFP_KERNEL);
 140	if (!buf)
 141		return ERR_PTR(-ENOMEM);
 142
 143	nbytes = min_t(size_t, *count, PAGE_SIZE);
 144	if (copy_from_user(buf, user_buf, nbytes)) {
 145		free_page((unsigned long)buf);
 146		return ERR_PTR(-EFAULT);
 147	}
 148
 149	*count = nbytes;
 150	return buf;
 151}
 152
 153static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
 154		       int long_fmt_len)
 155{
 156	char *token;
 157	u32 v[5];
 158	int ret;
 159
 160	token = strsep(line, "\n");
 161	if (!token)
 162		return false;
 163
 164	/*
 165	 * For Adapter/Router configuration space:
 166	 * Short format is: offset value\n
 167	 *		    v[0]   v[1]
 168	 * Long format as produced from the read side:
 169	 * offset relative_offset cap_id vs_cap_id value\n
 170	 * v[0]   v[1]            v[2]   v[3]      v[4]
 171	 *
 172	 * For Path configuration space:
 173	 * Short format is: offset value\n
 174	 *		    v[0]   v[1]
 175	 * Long format as produced from the read side:
 176	 * offset relative_offset in_hop_id value\n
 177	 * v[0]   v[1]            v[2]      v[3]
 178	 *
 179	 * For Counter configuration space:
 180	 * Short format is: offset\n
 181	 *		    v[0]
 182	 * Long format as produced from the read side:
 183	 * offset relative_offset counter_id value\n
 184	 * v[0]   v[1]            v[2]       v[3]
 185	 */
 186	ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
 187	/* In case of Counters, clear counter, "val" content is NA */
 188	if (ret == short_fmt_len) {
 189		*offs = v[0];
 190		*val = v[short_fmt_len - 1];
 191		return true;
 192	} else if (ret == long_fmt_len) {
 193		*offs = v[0];
 194		*val = v[long_fmt_len - 1];
 195		return true;
 196	}
 197
 198	return false;
 199}
 200
 201#if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
 202/*
 203 * Path registers need to be written in double word pairs and they both must be
 204 * read before written. This writes one double word in path config space
 205 * following the spec flow.
 206 */
 207static int path_write_one(struct tb_port *port, u32 val, u32 offset)
 208{
 209	u32 index = offset % PATH_LEN;
 210	u32 offs = offset - index;
 211	u32 data[PATH_LEN];
 212	int ret;
 213
 214	ret = tb_port_read(port, data, TB_CFG_HOPS, offs, PATH_LEN);
 215	if (ret)
 216		return ret;
 217	data[index] = val;
 218	return tb_port_write(port, data, TB_CFG_HOPS, offs, PATH_LEN);
 219}
 220
 221static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
 222			  enum tb_cfg_space space, const char __user *user_buf,
 223			  size_t count, loff_t *ppos)
 224{
 225	int long_fmt_len, ret = 0;
 226	struct tb *tb = sw->tb;
 227	char *line, *buf;
 228	u32 val, offset;
 229
 230	buf = validate_and_copy_from_user(user_buf, &count);
 231	if (IS_ERR(buf))
 232		return PTR_ERR(buf);
 233
 234	pm_runtime_get_sync(&sw->dev);
 235
 236	if (mutex_lock_interruptible(&tb->lock)) {
 237		ret = -ERESTARTSYS;
 238		goto out;
 239	}
 240
 241	/* User did hardware changes behind the driver's back */
 242	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
 243
 244	if (space == TB_CFG_HOPS)
 245		long_fmt_len = 4;
 246	else
 247		long_fmt_len = 5;
 248
 249	line = buf;
 250	while (parse_line(&line, &offset, &val, 2, long_fmt_len)) {
 251		if (port) {
 252			if (space == TB_CFG_HOPS)
 253				ret = path_write_one(port, val, offset);
 254			else
 255				ret = tb_port_write(port, &val, space, offset, 1);
 256		} else {
 257			ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
 258		}
 259		if (ret)
 260			break;
 261	}
 262
 263	mutex_unlock(&tb->lock);
 264
 265out:
 266	pm_runtime_mark_last_busy(&sw->dev);
 267	pm_runtime_put_autosuspend(&sw->dev);
 268	free_page((unsigned long)buf);
 269
 270	return ret < 0 ? ret : count;
 271}
 272
 273static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
 274			       size_t count, loff_t *ppos)
 275{
 276	struct seq_file *s = file->private_data;
 277	struct tb_port *port = s->private;
 278
 279	return regs_write(port->sw, port, TB_CFG_PORT, user_buf, count, ppos);
 280}
 281
 282static ssize_t path_write(struct file *file, const char __user *user_buf,
 283			  size_t count, loff_t *ppos)
 284{
 285	struct seq_file *s = file->private_data;
 286	struct tb_port *port = s->private;
 287
 288	return regs_write(port->sw, port, TB_CFG_HOPS, user_buf, count, ppos);
 289}
 290
 291static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
 292				 size_t count, loff_t *ppos)
 293{
 294	struct seq_file *s = file->private_data;
 295	struct tb_switch *sw = s->private;
 296
 297	return regs_write(sw, NULL, TB_CFG_SWITCH, user_buf, count, ppos);
 298}
 299
 300static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size,
 301			  size_t *bytes_read)
 302{
 303	char *field, *token;
 304	int i;
 305
 306	token = strsep(line, "\n");
 307	if (!token)
 308		return false;
 309
 310	/* Parse the register first */
 311	field = strsep(&token, " ");
 312	if (!field)
 313		return false;
 314	if (kstrtou8(field, 0, reg))
 315		return false;
 316
 317	/* Then the values for the register, up to data_size */
 318	for (i = 0; i < data_size; i++) {
 319		field = strsep(&token, " ");
 320		if (!field)
 321			break;
 322		if (kstrtou8(field, 0, &data[i]))
 323			return false;
 324	}
 325
 326	*bytes_read = i;
 327	return true;
 328}
 329
 330static ssize_t sb_regs_write(struct tb_port *port, const struct sb_reg *sb_regs,
 331			     size_t size, enum usb4_sb_target target, u8 index,
 332			     char *buf, size_t count, loff_t *ppos)
 333{
 334	u8 reg, data[SB_MAX_SIZE];
 335	size_t bytes_read;
 336	char *line = buf;
 337
 338	/* User did hardware changes behind the driver's back */
 339	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
 340
 341	/*
 342	 * For sideband registers we accept:
 343	 * reg b0 b1 b2...\n
 344	 *
 345	 * Here "reg" is the byte offset of the sideband register and "b0"..
 346	 * are the byte values. There can be less byte values than the register
 347	 * size. The leftovers will not be overwritten.
 348	 */
 349	while (parse_sb_line(&line, &reg, data, ARRAY_SIZE(data), &bytes_read)) {
 350		const struct sb_reg *sb_reg;
 351		int ret;
 352
 353		/* At least one byte must be passed */
 354		if (bytes_read < 1)
 355			return -EINVAL;
 356
 357		/* Find the register */
 358		sb_reg = NULL;
 359		for (int i = 0; i < size; i++) {
 360			if (sb_regs[i].reg == reg) {
 361				sb_reg = &sb_regs[i];
 362				break;
 363			}
 364		}
 365
 366		if (!sb_reg)
 367			return -EINVAL;
 368
 369		if (bytes_read > sb_regs->size)
 370			return -E2BIG;
 371
 372		ret = usb4_port_sb_write(port, target, index, sb_reg->reg, data,
 373					 bytes_read);
 374		if (ret)
 375			return ret;
 376	}
 377
 378	return 0;
 379}
 380
 381static ssize_t port_sb_regs_write(struct file *file, const char __user *user_buf,
 382				  size_t count, loff_t *ppos)
 383{
 384	struct seq_file *s = file->private_data;
 385	struct tb_port *port = s->private;
 386	struct tb_switch *sw = port->sw;
 387	struct tb *tb = sw->tb;
 388	char *buf;
 389	int ret;
 390
 391	buf = validate_and_copy_from_user(user_buf, &count);
 392	if (IS_ERR(buf))
 393		return PTR_ERR(buf);
 394
 395	pm_runtime_get_sync(&sw->dev);
 396
 397	if (mutex_lock_interruptible(&tb->lock)) {
 398		ret = -ERESTARTSYS;
 399		goto out;
 400	}
 401
 402	ret = sb_regs_write(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
 403			    USB4_SB_TARGET_ROUTER, 0, buf, count, ppos);
 404
 405	mutex_unlock(&tb->lock);
 406out:
 407	pm_runtime_mark_last_busy(&sw->dev);
 408	pm_runtime_put_autosuspend(&sw->dev);
 409	free_page((unsigned long)buf);
 410
 411	return ret < 0 ? ret : count;
 412}
 413
 414static ssize_t retimer_sb_regs_write(struct file *file,
 415				     const char __user *user_buf,
 416				     size_t count, loff_t *ppos)
 417{
 418	struct seq_file *s = file->private_data;
 419	struct tb_retimer *rt = s->private;
 420	struct tb *tb = rt->tb;
 421	char *buf;
 422	int ret;
 423
 424	buf = validate_and_copy_from_user(user_buf, &count);
 425	if (IS_ERR(buf))
 426		return PTR_ERR(buf);
 427
 428	pm_runtime_get_sync(&rt->dev);
 429
 430	if (mutex_lock_interruptible(&tb->lock)) {
 431		ret = -ERESTARTSYS;
 432		goto out;
 433	}
 434
 435	ret = sb_regs_write(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
 436			    USB4_SB_TARGET_RETIMER, rt->index, buf, count, ppos);
 437
 438	mutex_unlock(&tb->lock);
 439out:
 440	pm_runtime_mark_last_busy(&rt->dev);
 441	pm_runtime_put_autosuspend(&rt->dev);
 442	free_page((unsigned long)buf);
 443
 444	return ret < 0 ? ret : count;
 445}
 446#define DEBUGFS_MODE		0600
 447#else
 448#define port_regs_write		NULL
 449#define path_write		NULL
 450#define switch_regs_write	NULL
 451#define port_sb_regs_write	NULL
 452#define retimer_sb_regs_write	NULL
 453#define DEBUGFS_MODE		0400
 454#endif
 455
 456#if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
 457/**
 458 * struct tb_margining - Lane margining support
 459 * @port: USB4 port through which the margining operations are run
 460 * @target: Sideband target
 461 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
 462 * @dev: Pointer to the device that is the target (USB4 port or retimer)
 463 * @gen: Link generation
 464 * @asym_rx: %true% if @port supports asymmetric link with 3 Rx
 465 * @caps: Port lane margining capabilities
 466 * @results: Last lane margining results
 467 * @lanes: %0, %1 or %7 (all)
 468 * @min_ber_level: Minimum supported BER level contour value
 469 * @max_ber_level: Maximum supported BER level contour value
 470 * @ber_level: Current BER level contour value
 471 * @voltage_steps: Number of mandatory voltage steps
 472 * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
 473 * @voltage_steps_optional_range: Number of voltage steps for optional range
 474 * @max_voltage_offset_optional_range: Maximum voltage offset for the optional
 475 *					range (in mV).
 476 * @time_steps: Number of time margin steps
 477 * @max_time_offset: Maximum time margin offset (in mUI)
 478 * @voltage_time_offset: Offset for voltage / time for software margining
 479 * @dwell_time: Dwell time for software margining (in ms)
 480 * @error_counter: Error counter operation for software margining
 481 * @optional_voltage_offset_range: Enable optional extended voltage range
 482 * @software: %true if software margining is used instead of hardware
 483 * @time: %true if time margining is used instead of voltage
 484 * @right_high: %false if left/low margin test is performed, %true if
 485 *		right/high
 486 * @upper_eye: %false if the lower PAM3 eye is used, %true if the upper
 487 *	       eye is used
 488 */
 489struct tb_margining {
 490	struct tb_port *port;
 491	enum usb4_sb_target target;
 492	u8 index;
 493	struct device *dev;
 494	unsigned int gen;
 495	bool asym_rx;
 496	u32 caps[3];
 497	u32 results[3];
 498	enum usb4_margining_lane lanes;
 499	unsigned int min_ber_level;
 500	unsigned int max_ber_level;
 501	unsigned int ber_level;
 502	unsigned int voltage_steps;
 503	unsigned int max_voltage_offset;
 504	unsigned int voltage_steps_optional_range;
 505	unsigned int max_voltage_offset_optional_range;
 506	unsigned int time_steps;
 507	unsigned int max_time_offset;
 508	unsigned int voltage_time_offset;
 509	unsigned int dwell_time;
 510	enum usb4_margin_sw_error_counter error_counter;
 511	bool optional_voltage_offset_range;
 512	bool software;
 513	bool time;
 514	bool right_high;
 515	bool upper_eye;
 516};
 517
 518static int margining_modify_error_counter(struct tb_margining *margining,
 519	u32 lanes, enum usb4_margin_sw_error_counter error_counter)
 520{
 521	struct usb4_port_margining_params params = { 0 };
 522	struct tb_port *port = margining->port;
 523	u32 result;
 524
 525	if (error_counter != USB4_MARGIN_SW_ERROR_COUNTER_CLEAR &&
 526	    error_counter != USB4_MARGIN_SW_ERROR_COUNTER_STOP)
 527		return -EOPNOTSUPP;
 528
 529	params.error_counter = error_counter;
 530	params.lanes = lanes;
 531
 532	return usb4_port_sw_margin(port, margining->target, margining->index,
 533				   &params, &result);
 534}
 535
 536static bool supports_software(const struct tb_margining *margining)
 537{
 538	if (margining->gen < 4)
 539		return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
 540	return margining->caps[2] & USB4_MARGIN_CAP_2_MODES_SW;
 541}
 542
 543static bool supports_hardware(const struct tb_margining *margining)
 544{
 545	if (margining->gen < 4)
 546		return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
 547	return margining->caps[2] & USB4_MARGIN_CAP_2_MODES_HW;
 548}
 549
 550static bool all_lanes(const struct tb_margining *margining)
 551{
 552	return margining->caps[0] & USB4_MARGIN_CAP_0_ALL_LANES;
 553}
 554
 555static enum usb4_margin_cap_voltage_indp
 556independent_voltage_margins(const struct tb_margining *margining)
 557{
 558	if (margining->gen < 4) {
 559		switch (FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK, margining->caps[0])) {
 560		case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
 561			return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN;
 562		case USB4_MARGIN_CAP_0_VOLTAGE_HL:
 563			return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL;
 564		case USB4_MARGIN_CAP_1_TIME_BOTH:
 565			return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH;
 566		}
 567	} else {
 568		switch (FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_INDP_MASK, margining->caps[2])) {
 569		case USB4_MARGIN_CAP_2_VOLTAGE_MIN:
 570			return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN;
 571		case USB4_MARGIN_CAP_2_VOLTAGE_BOTH:
 572			return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH;
 573		}
 574	}
 575	return USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN;
 576}
 577
 578static bool supports_time(const struct tb_margining *margining)
 579{
 580	if (margining->gen < 4)
 581		return margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
 582	return margining->caps[2] & USB4_MARGIN_CAP_2_TIME;
 583}
 584
 585/* Only applicable if supports_time() returns true */
 586static enum usb4_margin_cap_time_indp
 587independent_time_margins(const struct tb_margining *margining)
 588{
 589	if (margining->gen < 4) {
 590		switch (FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK, margining->caps[1])) {
 591		case USB4_MARGIN_CAP_1_TIME_MIN:
 592			return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN;
 593		case USB4_MARGIN_CAP_1_TIME_LR:
 594			return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR;
 595		case USB4_MARGIN_CAP_1_TIME_BOTH:
 596			return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH;
 597		}
 598	} else {
 599		switch (FIELD_GET(USB4_MARGIN_CAP_2_TIME_INDP_MASK, margining->caps[2])) {
 600		case USB4_MARGIN_CAP_2_TIME_MIN:
 601			return USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN;
 602		case USB4_MARGIN_CAP_2_TIME_BOTH:
 603			return USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH;
 604		}
 605	}
 606	return USB4_MARGIN_CAP_TIME_INDP_UNKNOWN;
 607}
 608
 609static bool
 610supports_optional_voltage_offset_range(const struct tb_margining *margining)
 611{
 612	return margining->caps[0] & USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT;
 613}
 614
 615static ssize_t
 616margining_ber_level_write(struct file *file, const char __user *user_buf,
 617			   size_t count, loff_t *ppos)
 618{
 619	struct seq_file *s = file->private_data;
 620	struct tb_margining *margining = s->private;
 621	struct tb *tb = margining->port->sw->tb;
 622	unsigned int val;
 623	int ret = 0;
 624	char *buf;
 625
 626	if (mutex_lock_interruptible(&tb->lock))
 627		return -ERESTARTSYS;
 628
 629	if (margining->software) {
 630		ret = -EINVAL;
 631		goto out_unlock;
 632	}
 633
 634	buf = validate_and_copy_from_user(user_buf, &count);
 635	if (IS_ERR(buf)) {
 636		ret = PTR_ERR(buf);
 637		goto out_unlock;
 638	}
 639
 640	buf[count - 1] = '\0';
 641
 642	ret = kstrtouint(buf, 10, &val);
 643	if (ret)
 644		goto out_free;
 645
 646	if (val < margining->min_ber_level ||
 647	    val > margining->max_ber_level) {
 648		ret = -EINVAL;
 649		goto out_free;
 650	}
 651
 652	margining->ber_level = val;
 653
 654out_free:
 655	free_page((unsigned long)buf);
 656out_unlock:
 657	mutex_unlock(&tb->lock);
 658
 659	return ret < 0 ? ret : count;
 660}
 661
 662static void ber_level_show(struct seq_file *s, unsigned int val)
 663{
 664	if (val % 2)
 665		seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
 666	else
 667		seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
 668}
 669
 670static int margining_ber_level_show(struct seq_file *s, void *not_used)
 671{
 672	const struct tb_margining *margining = s->private;
 673
 674	if (margining->software)
 675		return -EINVAL;
 676	ber_level_show(s, margining->ber_level);
 677	return 0;
 678}
 679DEBUGFS_ATTR_RW(margining_ber_level);
 680
 681static int margining_caps_show(struct seq_file *s, void *not_used)
 682{
 683	struct tb_margining *margining = s->private;
 684	struct tb *tb = margining->port->sw->tb;
 685	int ret = 0;
 686
 687	if (mutex_lock_interruptible(&tb->lock))
 688		return -ERESTARTSYS;
 689
 690	/* Dump the raw caps first */
 691	for (int i = 0; i < ARRAY_SIZE(margining->caps); i++)
 692		seq_printf(s, "0x%08x\n", margining->caps[i]);
 693
 694	seq_printf(s, "# software margining: %s\n",
 695		   str_yes_no(supports_software(margining)));
 696	if (supports_hardware(margining)) {
 697		seq_puts(s, "# hardware margining: yes\n");
 698		seq_puts(s, "# minimum BER level contour: ");
 699		ber_level_show(s, margining->min_ber_level);
 700		seq_puts(s, "# maximum BER level contour: ");
 701		ber_level_show(s, margining->max_ber_level);
 702	} else {
 703		seq_puts(s, "# hardware margining: no\n");
 704	}
 705
 706	seq_printf(s, "# all lanes simultaneously: %s\n",
 707		  str_yes_no(all_lanes(margining)));
 708	seq_printf(s, "# voltage margin steps: %u\n",
 709		   margining->voltage_steps);
 710	seq_printf(s, "# maximum voltage offset: %u mV\n",
 711		   margining->max_voltage_offset);
 712	seq_printf(s, "# optional voltage offset range support: %s\n",
 713		   str_yes_no(supports_optional_voltage_offset_range(margining)));
 714	if (supports_optional_voltage_offset_range(margining)) {
 715		seq_printf(s, "# voltage margin steps, optional range: %u\n",
 716			   margining->voltage_steps_optional_range);
 717		seq_printf(s, "# maximum voltage offset, optional range: %u mV\n",
 718			   margining->max_voltage_offset_optional_range);
 719	}
 720
 721	switch (independent_voltage_margins(margining)) {
 722	case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN:
 723		seq_puts(s, "# returns minimum between high and low voltage margins\n");
 724		break;
 725	case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL:
 726		seq_puts(s, "# returns high or low voltage margin\n");
 727		break;
 728	case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH:
 729		seq_puts(s, "# returns both high and low margins\n");
 730		break;
 731	case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN:
 732		seq_puts(s, "# returns minimum between high and low voltage margins in both lower and upper eye\n");
 733		break;
 734	case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH:
 735		seq_puts(s, "# returns both high and low margins of both upper and lower eye\n");
 736		break;
 737	case USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN:
 738		tb_port_warn(margining->port,
 739			     "failed to parse independent voltage margining capabilities\n");
 740		ret = -EIO;
 741		goto out;
 742	}
 743
 744	if (supports_time(margining)) {
 745		seq_puts(s, "# time margining: yes\n");
 746		seq_printf(s, "# time margining is destructive: %s\n",
 747			   str_yes_no(margining->caps[1] & USB4_MARGIN_CAP_1_TIME_DESTR));
 748
 749		switch (independent_time_margins(margining)) {
 750		case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN:
 751			seq_puts(s, "# returns minimum between left and right time margins\n");
 752			break;
 753		case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR:
 754			seq_puts(s, "# returns left or right margin\n");
 755			break;
 756		case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH:
 757			seq_puts(s, "# returns both left and right margins\n");
 758			break;
 759		case USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN:
 760			seq_puts(s, "# returns minimum between left and right time margins in both lower and upper eye\n");
 761			break;
 762		case USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH:
 763			seq_puts(s, "# returns both left and right margins of both upper and lower eye\n");
 764			break;
 765		case USB4_MARGIN_CAP_TIME_INDP_UNKNOWN:
 766			tb_port_warn(margining->port,
 767				     "failed to parse independent time margining capabilities\n");
 768			ret = -EIO;
 769			goto out;
 770		}
 771
 772		seq_printf(s, "# time margin steps: %u\n",
 773			   margining->time_steps);
 774		seq_printf(s, "# maximum time offset: %u mUI\n",
 775			   margining->max_time_offset);
 776	} else {
 777		seq_puts(s, "# time margining: no\n");
 778	}
 779
 780out:
 781	mutex_unlock(&tb->lock);
 782	return ret;
 783}
 784DEBUGFS_ATTR_RO(margining_caps);
 785
 786static const struct {
 787	enum usb4_margining_lane lane;
 788	const char *name;
 789} lane_names[] = {
 790	{
 791		.lane = USB4_MARGINING_LANE_RX0,
 792		.name = "0",
 793	},
 794	{
 795		.lane = USB4_MARGINING_LANE_RX1,
 796		.name = "1",
 797	},
 798	{
 799		.lane = USB4_MARGINING_LANE_RX2,
 800		.name = "2",
 801	},
 802	{
 803		.lane = USB4_MARGINING_LANE_ALL,
 804		.name = "all",
 805	},
 806};
 807
 808static ssize_t
 809margining_lanes_write(struct file *file, const char __user *user_buf,
 810		      size_t count, loff_t *ppos)
 811{
 812	struct seq_file *s = file->private_data;
 813	struct tb_margining *margining = s->private;
 814	struct tb_port *port = margining->port;
 815	struct tb *tb = port->sw->tb;
 816	int lane = -1;
 817	char *buf;
 818
 819	buf = validate_and_copy_from_user(user_buf, &count);
 820	if (IS_ERR(buf))
 821		return PTR_ERR(buf);
 822
 823	buf[count - 1] = '\0';
 824
 825	for (int i = 0; i < ARRAY_SIZE(lane_names); i++) {
 826		if (!strcmp(buf, lane_names[i].name)) {
 827			lane = lane_names[i].lane;
 828			break;
 829		}
 830	}
 831
 832	free_page((unsigned long)buf);
 833
 834	if (lane == -1)
 835		return -EINVAL;
 836
 837	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
 838		if (lane == USB4_MARGINING_LANE_ALL && !all_lanes(margining))
 839			return -EINVAL;
 840		/*
 841		 * Enabling on RX2 requires that it is supported by the
 842		 * USB4 port.
 843		 */
 844		if (lane == USB4_MARGINING_LANE_RX2 && !margining->asym_rx)
 845			return -EINVAL;
 846
 847		margining->lanes = lane;
 848	}
 849
 850	return count;
 851}
 852
 853static int margining_lanes_show(struct seq_file *s, void *not_used)
 854{
 855	struct tb_margining *margining = s->private;
 856	struct tb_port *port = margining->port;
 857	struct tb *tb = port->sw->tb;
 858
 859	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
 860		for (int i = 0; i < ARRAY_SIZE(lane_names); i++) {
 861			if (lane_names[i].lane == USB4_MARGINING_LANE_ALL &&
 862			    !all_lanes(margining))
 863				continue;
 864			if (lane_names[i].lane == USB4_MARGINING_LANE_RX2 &&
 865			    !margining->asym_rx)
 866				continue;
 867
 868			if (i != 0)
 869				seq_putc(s, ' ');
 870
 871			if (lane_names[i].lane == margining->lanes)
 872				seq_printf(s, "[%s]", lane_names[i].name);
 873			else
 874				seq_printf(s, "%s", lane_names[i].name);
 875		}
 876		seq_puts(s, "\n");
 877	}
 878
 879	return 0;
 880}
 881DEBUGFS_ATTR_RW(margining_lanes);
 882
 883static ssize_t
 884margining_voltage_time_offset_write(struct file *file,
 885				    const char __user *user_buf,
 886				    size_t count, loff_t *ppos)
 887{
 888	struct seq_file *s = file->private_data;
 889	struct tb_margining *margining = s->private;
 890	struct tb *tb = margining->port->sw->tb;
 891	unsigned int max_margin;
 892	unsigned int val;
 893	int ret;
 894
 895	ret = kstrtouint_from_user(user_buf, count, 10, &val);
 896	if (ret)
 897		return ret;
 898
 899	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
 900		if (!margining->software)
 901			return -EOPNOTSUPP;
 902
 903		if (margining->time)
 904			max_margin = margining->time_steps;
 905		else
 906			if (margining->optional_voltage_offset_range)
 907				max_margin = margining->voltage_steps_optional_range;
 908			else
 909				max_margin = margining->voltage_steps;
 910
 911		margining->voltage_time_offset = clamp(val, 0, max_margin);
 912	}
 913
 914	return count;
 915}
 916
 917static int margining_voltage_time_offset_show(struct seq_file *s,
 918					      void *not_used)
 919{
 920	const struct tb_margining *margining = s->private;
 921	struct tb *tb = margining->port->sw->tb;
 922
 923	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
 924		if (!margining->software)
 925			return -EOPNOTSUPP;
 926
 927		seq_printf(s, "%d\n", margining->voltage_time_offset);
 928	}
 929
 930	return 0;
 931}
 932DEBUGFS_ATTR_RW(margining_voltage_time_offset);
 933
 934static ssize_t
 935margining_error_counter_write(struct file *file, const char __user *user_buf,
 936			      size_t count, loff_t *ppos)
 937{
 938	enum usb4_margin_sw_error_counter error_counter;
 939	struct seq_file *s = file->private_data;
 940	struct tb_margining *margining = s->private;
 941	struct tb *tb = margining->port->sw->tb;
 942	char *buf;
 943
 944	buf = validate_and_copy_from_user(user_buf, &count);
 945	if (IS_ERR(buf))
 946		return PTR_ERR(buf);
 947
 948	buf[count - 1] = '\0';
 949
 950	if (!strcmp(buf, "nop"))
 951		error_counter = USB4_MARGIN_SW_ERROR_COUNTER_NOP;
 952	else if (!strcmp(buf, "clear"))
 953		error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR;
 954	else if (!strcmp(buf, "start"))
 955		error_counter = USB4_MARGIN_SW_ERROR_COUNTER_START;
 956	else if (!strcmp(buf, "stop"))
 957		error_counter = USB4_MARGIN_SW_ERROR_COUNTER_STOP;
 958	else
 959		return -EINVAL;
 960
 961	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
 962		if (!margining->software)
 963			return -EOPNOTSUPP;
 964
 965		margining->error_counter = error_counter;
 966	}
 967
 968	return count;
 969}
 970
 971static int margining_error_counter_show(struct seq_file *s, void *not_used)
 972{
 973	const struct tb_margining *margining = s->private;
 974	struct tb *tb = margining->port->sw->tb;
 975
 976	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
 977		if (!margining->software)
 978			return -EOPNOTSUPP;
 979
 980		switch (margining->error_counter) {
 981		case USB4_MARGIN_SW_ERROR_COUNTER_NOP:
 982			seq_puts(s, "[nop] clear start stop\n");
 983			break;
 984		case USB4_MARGIN_SW_ERROR_COUNTER_CLEAR:
 985			seq_puts(s, "nop [clear] start stop\n");
 986			break;
 987		case USB4_MARGIN_SW_ERROR_COUNTER_START:
 988			seq_puts(s, "nop clear [start] stop\n");
 989			break;
 990		case USB4_MARGIN_SW_ERROR_COUNTER_STOP:
 991			seq_puts(s, "nop clear start [stop]\n");
 992			break;
 993		}
 994	}
 995
 996	return 0;
 997}
 998DEBUGFS_ATTR_RW(margining_error_counter);
 999
1000static ssize_t
1001margining_dwell_time_write(struct file *file, const char __user *user_buf,
1002			   size_t count, loff_t *ppos)
1003{
1004	struct seq_file *s = file->private_data;
1005	struct tb_margining *margining = s->private;
1006	struct tb *tb = margining->port->sw->tb;
1007	unsigned int val;
1008	int ret;
1009
1010	ret = kstrtouint_from_user(user_buf, count, 10, &val);
1011	if (ret)
1012		return ret;
1013
1014	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
1015		if (!margining->software)
1016			return -EOPNOTSUPP;
1017
1018		margining->dwell_time = clamp(val, MIN_DWELL_TIME, MAX_DWELL_TIME);
1019	}
1020
1021	return count;
1022}
1023
1024static int margining_dwell_time_show(struct seq_file *s, void *not_used)
1025{
1026	struct tb_margining *margining = s->private;
1027	struct tb *tb = margining->port->sw->tb;
1028
1029	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
1030		if (!margining->software)
1031			return -EOPNOTSUPP;
1032
1033		seq_printf(s, "%d\n", margining->dwell_time);
1034	}
1035
1036	return 0;
1037}
1038DEBUGFS_ATTR_RW(margining_dwell_time);
1039
1040static ssize_t
1041margining_optional_voltage_offset_write(struct file *file, const char __user *user_buf,
1042					size_t count, loff_t *ppos)
1043{
1044	struct seq_file *s = file->private_data;
1045	struct tb_margining *margining = s->private;
1046	struct tb *tb = margining->port->sw->tb;
1047	bool val;
1048	int ret;
1049
1050	ret = kstrtobool_from_user(user_buf, count, &val);
1051	if (ret)
1052		return ret;
1053
1054	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
1055		margining->optional_voltage_offset_range = val;
1056	}
1057
1058	return count;
1059}
1060
1061static int margining_optional_voltage_offset_show(struct seq_file *s,
1062						  void *not_used)
1063{
1064	struct tb_margining *margining = s->private;
1065	struct tb *tb = margining->port->sw->tb;
1066
1067	scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) {
1068		seq_printf(s, "%u\n", margining->optional_voltage_offset_range);
1069	}
1070
1071	return 0;
1072}
1073DEBUGFS_ATTR_RW(margining_optional_voltage_offset);
1074
1075static ssize_t margining_mode_write(struct file *file,
1076				   const char __user *user_buf,
1077				   size_t count, loff_t *ppos)
1078{
1079	struct seq_file *s = file->private_data;
1080	struct tb_margining *margining = s->private;
1081	struct tb *tb = margining->port->sw->tb;
1082	int ret = 0;
1083	char *buf;
1084
1085	buf = validate_and_copy_from_user(user_buf, &count);
1086	if (IS_ERR(buf))
1087		return PTR_ERR(buf);
1088
1089	buf[count - 1] = '\0';
1090
1091	if (mutex_lock_interruptible(&tb->lock)) {
1092		ret = -ERESTARTSYS;
1093		goto out_free;
1094	}
1095
1096	if (!strcmp(buf, "software")) {
1097		if (supports_software(margining))
1098			margining->software = true;
1099		else
1100			ret = -EINVAL;
1101	} else if (!strcmp(buf, "hardware")) {
1102		if (supports_hardware(margining))
1103			margining->software = false;
1104		else
1105			ret = -EINVAL;
1106	} else {
1107		ret = -EINVAL;
1108	}
1109
1110	mutex_unlock(&tb->lock);
1111
1112out_free:
1113	free_page((unsigned long)buf);
1114	return ret ? ret : count;
1115}
1116
1117static int margining_mode_show(struct seq_file *s, void *not_used)
1118{
1119	struct tb_margining *margining = s->private;
1120	struct tb *tb = margining->port->sw->tb;
1121	const char *space = "";
1122
1123	if (mutex_lock_interruptible(&tb->lock))
1124		return -ERESTARTSYS;
1125
1126	if (supports_software(margining)) {
1127		if (margining->software)
1128			seq_puts(s, "[software]");
1129		else
1130			seq_puts(s, "software");
1131		space = " ";
1132	}
1133	if (supports_hardware(margining)) {
1134		if (margining->software)
1135			seq_printf(s, "%shardware", space);
1136		else
1137			seq_printf(s, "%s[hardware]", space);
1138	}
1139
1140	mutex_unlock(&tb->lock);
1141
1142	seq_puts(s, "\n");
1143	return 0;
1144}
1145DEBUGFS_ATTR_RW(margining_mode);
1146
1147static int margining_run_sw(struct tb_margining *margining,
1148			    struct usb4_port_margining_params *params)
1149{
1150	u32 nsamples = margining->dwell_time / DWELL_SAMPLE_INTERVAL;
1151	int ret, i;
1152
1153	ret = usb4_port_sw_margin(margining->port, margining->target, margining->index,
1154				  params, margining->results);
1155	if (ret)
1156		goto out_stop;
1157
1158	for (i = 0; i <= nsamples; i++) {
1159		u32 errors = 0;
1160
1161		ret = usb4_port_sw_margin_errors(margining->port, margining->target,
1162						 margining->index, &margining->results[1]);
1163		if (ret)
1164			break;
1165
1166		if (margining->lanes == USB4_MARGINING_LANE_RX0)
1167			errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK,
1168					   margining->results[1]);
1169		else if (margining->lanes == USB4_MARGINING_LANE_RX1)
1170			errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK,
1171					   margining->results[1]);
1172		else if (margining->lanes == USB4_MARGINING_LANE_RX2)
1173			errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK,
1174					   margining->results[1]);
1175		else if (margining->lanes == USB4_MARGINING_LANE_ALL)
1176			errors = margining->results[1];
1177
1178		/* Any errors stop the test */
1179		if (errors)
1180			break;
1181
1182		fsleep(DWELL_SAMPLE_INTERVAL * USEC_PER_MSEC);
1183	}
1184
1185out_stop:
1186	/*
1187	 * Stop the counters but don't clear them to allow the
1188	 * different error counter configurations.
1189	 */
1190	margining_modify_error_counter(margining, margining->lanes,
1191				       USB4_MARGIN_SW_ERROR_COUNTER_STOP);
1192	return ret;
1193}
1194
1195static int validate_margining(struct tb_margining *margining)
1196{
1197	/*
1198	 * For running on RX2 the link must be asymmetric with 3
1199	 * receivers. Because this can change dynamically, check it
1200	 * here before we start the margining and report back error if
1201	 * expectations are not met.
1202	 */
1203	if (margining->lanes == USB4_MARGINING_LANE_RX2) {
1204		int ret;
1205
1206		ret = tb_port_get_link_width(margining->port);
1207		if (ret < 0)
1208			return ret;
1209		if (ret != TB_LINK_WIDTH_ASYM_RX) {
1210			tb_port_warn(margining->port, "link is %s expected %s",
1211				     tb_width_name(ret),
1212				     tb_width_name(TB_LINK_WIDTH_ASYM_RX));
1213			return -EINVAL;
1214		}
1215	}
1216
1217	return 0;
1218}
1219
1220static int margining_run_write(void *data, u64 val)
1221{
1222	struct tb_margining *margining = data;
1223	struct tb_port *port = margining->port;
1224	struct device *dev = margining->dev;
1225	struct tb_switch *sw = port->sw;
1226	struct tb_switch *down_sw;
1227	struct tb *tb = sw->tb;
1228	int ret, clx;
1229
1230	if (val != 1)
1231		return -EINVAL;
1232
1233	pm_runtime_get_sync(dev);
1234
1235	if (mutex_lock_interruptible(&tb->lock)) {
1236		ret = -ERESTARTSYS;
1237		goto out_rpm_put;
1238	}
1239
1240	ret = validate_margining(margining);
1241	if (ret)
1242		goto out_unlock;
1243
1244	if (tb_is_upstream_port(port))
1245		down_sw = sw;
1246	else if (port->remote)
1247		down_sw = port->remote->sw;
1248	else
1249		down_sw = NULL;
1250
1251	if (down_sw) {
1252		/*
1253		 * CL states may interfere with lane margining so
1254		 * disable them temporarily now.
1255		 */
1256		ret = tb_switch_clx_disable(down_sw);
1257		if (ret < 0) {
1258			tb_sw_warn(down_sw, "failed to disable CL states\n");
1259			goto out_unlock;
1260		}
1261		clx = ret;
1262	}
1263
1264	/* Clear the results */
1265	memset(margining->results, 0, sizeof(margining->results));
1266
1267	if (margining->software) {
1268		struct usb4_port_margining_params params = {
1269			.error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR,
1270			.lanes = margining->lanes,
1271			.time = margining->time,
1272			.voltage_time_offset = margining->voltage_time_offset,
1273			.right_high = margining->right_high,
1274			.upper_eye = margining->upper_eye,
1275			.optional_voltage_offset_range = margining->optional_voltage_offset_range,
1276		};
1277
1278		tb_port_dbg(port,
1279			    "running software %s lane margining for %s lanes %u\n",
1280			    margining->time ? "time" : "voltage", dev_name(dev),
1281			    margining->lanes);
1282
1283		ret = margining_run_sw(margining, &params);
1284	} else {
1285		struct usb4_port_margining_params params = {
1286			.ber_level = margining->ber_level,
1287			.lanes = margining->lanes,
1288			.time = margining->time,
1289			.right_high = margining->right_high,
1290			.upper_eye = margining->upper_eye,
1291			.optional_voltage_offset_range = margining->optional_voltage_offset_range,
1292		};
1293
1294		tb_port_dbg(port,
1295			    "running hardware %s lane margining for %s lanes %u\n",
1296			    margining->time ? "time" : "voltage", dev_name(dev),
1297			    margining->lanes);
1298
1299		ret = usb4_port_hw_margin(port, margining->target, margining->index, &params,
1300					  margining->results, ARRAY_SIZE(margining->results));
1301	}
1302
1303	if (down_sw)
1304		tb_switch_clx_enable(down_sw, clx);
1305out_unlock:
1306	mutex_unlock(&tb->lock);
1307out_rpm_put:
1308	pm_runtime_mark_last_busy(dev);
1309	pm_runtime_put_autosuspend(dev);
1310
1311	return ret;
1312}
1313DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
1314			 "%llu\n");
1315
1316static ssize_t margining_results_write(struct file *file,
1317				       const char __user *user_buf,
1318				       size_t count, loff_t *ppos)
1319{
1320	struct seq_file *s = file->private_data;
1321	struct tb_margining *margining = s->private;
1322	struct tb *tb = margining->port->sw->tb;
1323
1324	if (mutex_lock_interruptible(&tb->lock))
1325		return -ERESTARTSYS;
1326
1327	/* Just clear the results */
1328	memset(margining->results, 0, sizeof(margining->results));
1329
1330	if (margining->software) {
1331		/* Clear the error counters */
1332		margining_modify_error_counter(margining,
1333					       USB4_MARGINING_LANE_ALL,
1334					       USB4_MARGIN_SW_ERROR_COUNTER_CLEAR);
1335	}
1336
1337	mutex_unlock(&tb->lock);
1338	return count;
1339}
1340
1341static void voltage_margin_show(struct seq_file *s,
1342				const struct tb_margining *margining, u8 val)
1343{
1344	unsigned int tmp, voltage;
1345
1346	tmp = FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK, val);
1347	voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
1348	seq_printf(s, "%u mV (%u)", voltage, tmp);
1349	if (val & USB4_MARGIN_HW_RES_EXCEEDS)
1350		seq_puts(s, " exceeds maximum");
1351	seq_puts(s, "\n");
1352	if (margining->optional_voltage_offset_range)
1353		seq_puts(s, " optional voltage offset range enabled\n");
1354}
1355
1356static void time_margin_show(struct seq_file *s,
1357			     const struct tb_margining *margining, u8 val)
1358{
1359	unsigned int tmp, interval;
1360
1361	tmp = FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK, val);
1362	interval = tmp * margining->max_time_offset / margining->time_steps;
1363	seq_printf(s, "%u mUI (%u)", interval, tmp);
1364	if (val & USB4_MARGIN_HW_RES_EXCEEDS)
1365		seq_puts(s, " exceeds maximum");
1366	seq_puts(s, "\n");
1367}
1368
1369static u8 margining_hw_result_val(const u32 *results,
1370				  enum usb4_margining_lane lane,
1371				  bool right_high)
1372{
1373	u32 val;
1374
1375	if (lane == USB4_MARGINING_LANE_RX0)
1376		val = results[1];
1377	else if (lane == USB4_MARGINING_LANE_RX1)
1378		val = results[1] >> USB4_MARGIN_HW_RES_LANE_SHIFT;
1379	else if (lane == USB4_MARGINING_LANE_RX2)
1380		val = results[2];
1381	else
1382		val = 0;
1383
1384	return right_high ? val : val >> USB4_MARGIN_HW_RES_LL_SHIFT;
1385}
1386
1387static void margining_hw_result_format(struct seq_file *s,
1388				       const struct tb_margining *margining,
1389				       enum usb4_margining_lane lane)
1390{
1391	u8 val;
1392
1393	if (margining->time) {
1394		val = margining_hw_result_val(margining->results, lane, true);
1395		seq_printf(s, "# lane %u right time margin: ", lane);
1396		time_margin_show(s, margining, val);
1397		val = margining_hw_result_val(margining->results, lane, false);
1398		seq_printf(s, "# lane %u left time margin: ", lane);
1399		time_margin_show(s, margining, val);
1400	} else {
1401		val = margining_hw_result_val(margining->results, lane, true);
1402		seq_printf(s, "# lane %u high voltage margin: ", lane);
1403		voltage_margin_show(s, margining, val);
1404		val = margining_hw_result_val(margining->results, lane, false);
1405		seq_printf(s, "# lane %u low voltage margin: ", lane);
1406		voltage_margin_show(s, margining, val);
1407	}
1408}
1409
1410static int margining_results_show(struct seq_file *s, void *not_used)
1411{
1412	struct tb_margining *margining = s->private;
1413	struct tb *tb = margining->port->sw->tb;
1414
1415	if (mutex_lock_interruptible(&tb->lock))
1416		return -ERESTARTSYS;
1417
1418	/* Dump the raw results first */
1419	seq_printf(s, "0x%08x\n", margining->results[0]);
1420	/* Only the hardware margining has two result dwords */
1421	if (!margining->software) {
1422		for (int i = 1; i < ARRAY_SIZE(margining->results); i++)
1423			seq_printf(s, "0x%08x\n", margining->results[i]);
1424
1425		if (margining->lanes == USB4_MARGINING_LANE_ALL) {
1426			margining_hw_result_format(s, margining,
1427						   USB4_MARGINING_LANE_RX0);
1428			margining_hw_result_format(s, margining,
1429						   USB4_MARGINING_LANE_RX1);
1430			if (margining->asym_rx)
1431				margining_hw_result_format(s, margining,
1432						USB4_MARGINING_LANE_RX2);
1433		} else {
1434			margining_hw_result_format(s, margining,
1435						   margining->lanes);
1436		}
1437	} else {
1438		u32 lane_errors, result;
1439
1440		seq_printf(s, "0x%08x\n", margining->results[1]);
1441
1442		result = FIELD_GET(USB4_MARGIN_SW_LANES_MASK, margining->results[0]);
1443		if (result == USB4_MARGINING_LANE_RX0 ||
1444		    result == USB4_MARGINING_LANE_ALL) {
1445			lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK,
1446						margining->results[1]);
1447			seq_printf(s, "# lane 0 errors: %u\n", lane_errors);
1448		}
1449		if (result == USB4_MARGINING_LANE_RX1 ||
1450		    result == USB4_MARGINING_LANE_ALL) {
1451			lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK,
1452						margining->results[1]);
1453			seq_printf(s, "# lane 1 errors: %u\n", lane_errors);
1454		}
1455		if (margining->asym_rx &&
1456		    (result == USB4_MARGINING_LANE_RX2 ||
1457		     result == USB4_MARGINING_LANE_ALL)) {
1458			lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK,
1459						margining->results[1]);
1460			seq_printf(s, "# lane 2 errors: %u\n", lane_errors);
1461		}
1462	}
1463
1464	mutex_unlock(&tb->lock);
1465	return 0;
1466}
1467DEBUGFS_ATTR_RW(margining_results);
1468
1469static ssize_t margining_test_write(struct file *file,
1470				    const char __user *user_buf,
1471				    size_t count, loff_t *ppos)
1472{
1473	struct seq_file *s = file->private_data;
1474	struct tb_margining *margining = s->private;
1475	struct tb *tb = margining->port->sw->tb;
1476	int ret = 0;
1477	char *buf;
1478
1479	buf = validate_and_copy_from_user(user_buf, &count);
1480	if (IS_ERR(buf))
1481		return PTR_ERR(buf);
1482
1483	buf[count - 1] = '\0';
1484
1485	if (mutex_lock_interruptible(&tb->lock)) {
1486		ret = -ERESTARTSYS;
1487		goto out_free;
1488	}
1489
1490	if (!strcmp(buf, "time") && supports_time(margining))
1491		margining->time = true;
1492	else if (!strcmp(buf, "voltage"))
1493		margining->time = false;
1494	else
1495		ret = -EINVAL;
1496
1497	mutex_unlock(&tb->lock);
1498
1499out_free:
1500	free_page((unsigned long)buf);
1501	return ret ? ret : count;
1502}
1503
1504static int margining_test_show(struct seq_file *s, void *not_used)
1505{
1506	struct tb_margining *margining = s->private;
1507	struct tb *tb = margining->port->sw->tb;
1508
1509	if (mutex_lock_interruptible(&tb->lock))
1510		return -ERESTARTSYS;
1511
1512	if (supports_time(margining)) {
1513		if (margining->time)
1514			seq_puts(s, "voltage [time]\n");
1515		else
1516			seq_puts(s, "[voltage] time\n");
1517	} else {
1518		seq_puts(s, "[voltage]\n");
1519	}
1520
1521	mutex_unlock(&tb->lock);
1522	return 0;
1523}
1524DEBUGFS_ATTR_RW(margining_test);
1525
1526static ssize_t margining_margin_write(struct file *file,
1527				    const char __user *user_buf,
1528				    size_t count, loff_t *ppos)
1529{
1530	struct seq_file *s = file->private_data;
1531	struct tb_margining *margining = s->private;
1532	struct tb *tb = margining->port->sw->tb;
1533	int ret = 0;
1534	char *buf;
1535
1536	buf = validate_and_copy_from_user(user_buf, &count);
1537	if (IS_ERR(buf))
1538		return PTR_ERR(buf);
1539
1540	buf[count - 1] = '\0';
1541
1542	if (mutex_lock_interruptible(&tb->lock)) {
1543		ret = -ERESTARTSYS;
1544		goto out_free;
1545	}
1546
1547	if (margining->time) {
1548		if (!strcmp(buf, "left"))
1549			margining->right_high = false;
1550		else if (!strcmp(buf, "right"))
1551			margining->right_high = true;
1552		else
1553			ret = -EINVAL;
1554	} else {
1555		if (!strcmp(buf, "low"))
1556			margining->right_high = false;
1557		else if (!strcmp(buf, "high"))
1558			margining->right_high = true;
1559		else
1560			ret = -EINVAL;
1561	}
1562
1563	mutex_unlock(&tb->lock);
1564
1565out_free:
1566	free_page((unsigned long)buf);
1567	return ret ? ret : count;
1568}
1569
1570static int margining_margin_show(struct seq_file *s, void *not_used)
1571{
1572	struct tb_margining *margining = s->private;
1573	struct tb *tb = margining->port->sw->tb;
1574
1575	if (mutex_lock_interruptible(&tb->lock))
1576		return -ERESTARTSYS;
1577
1578	if (margining->time) {
1579		if (margining->right_high)
1580			seq_puts(s, "left [right]\n");
1581		else
1582			seq_puts(s, "[left] right\n");
1583	} else {
1584		if (margining->right_high)
1585			seq_puts(s, "low [high]\n");
1586		else
1587			seq_puts(s, "[low] high\n");
1588	}
1589
1590	mutex_unlock(&tb->lock);
1591	return 0;
1592}
1593DEBUGFS_ATTR_RW(margining_margin);
1594
1595static ssize_t margining_eye_write(struct file *file,
1596				   const char __user *user_buf,
1597				   size_t count, loff_t *ppos)
1598{
1599	struct seq_file *s = file->private_data;
1600	struct tb_port *port = s->private;
1601	struct usb4_port *usb4 = port->usb4;
1602	struct tb *tb = port->sw->tb;
1603	int ret = 0;
1604	char *buf;
1605
1606	buf = validate_and_copy_from_user(user_buf, &count);
1607	if (IS_ERR(buf))
1608		return PTR_ERR(buf);
1609
1610	buf[count - 1] = '\0';
1611
1612	scoped_cond_guard(mutex_intr, ret = -ERESTARTSYS, &tb->lock) {
1613		if (!strcmp(buf, "lower"))
1614			usb4->margining->upper_eye = false;
1615		else if (!strcmp(buf, "upper"))
1616			usb4->margining->upper_eye = true;
1617		else
1618			ret = -EINVAL;
1619	}
1620
1621	free_page((unsigned long)buf);
1622	return ret ? ret : count;
1623}
1624
1625static int margining_eye_show(struct seq_file *s, void *not_used)
1626{
1627	struct tb_port *port = s->private;
1628	struct usb4_port *usb4 = port->usb4;
1629	struct tb *tb = port->sw->tb;
1630
1631	scoped_guard(mutex_intr, &tb->lock) {
1632		if (usb4->margining->upper_eye)
1633			seq_puts(s, "lower [upper]\n");
1634		else
1635			seq_puts(s, "[lower] upper\n");
1636
1637		return 0;
1638	}
1639
1640	return -ERESTARTSYS;
1641}
1642DEBUGFS_ATTR_RW(margining_eye);
1643
1644static struct tb_margining *margining_alloc(struct tb_port *port,
1645					    struct device *dev,
1646					    enum usb4_sb_target target,
1647					    u8 index, struct dentry *parent)
1648{
1649	struct tb_margining *margining;
1650	struct dentry *dir;
1651	unsigned int val;
1652	int ret;
1653
1654	ret = tb_port_get_link_generation(port);
1655	if (ret < 0) {
1656		tb_port_warn(port, "failed to read link generation\n");
1657		return NULL;
1658	}
1659
1660	margining = kzalloc(sizeof(*margining), GFP_KERNEL);
1661	if (!margining)
1662		return NULL;
1663
1664	margining->port = port;
1665	margining->target = target;
1666	margining->index = index;
1667	margining->dev = dev;
1668	margining->gen = ret;
1669	margining->asym_rx = tb_port_width_supported(port, TB_LINK_WIDTH_ASYM_RX);
1670
1671	ret = usb4_port_margining_caps(port, target, index, margining->caps,
1672				       ARRAY_SIZE(margining->caps));
1673	if (ret) {
1674		kfree(margining);
1675		return NULL;
1676	}
1677
1678	/* Set the initial mode */
1679	if (supports_software(margining))
1680		margining->software = true;
1681
1682	if (margining->gen < 4) {
1683		val = FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK, margining->caps[0]);
1684		margining->voltage_steps = val;
1685		val = FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK, margining->caps[0]);
1686		margining->max_voltage_offset = 74 + val * 2;
1687	} else {
1688		val = FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_STEPS_MASK, margining->caps[2]);
1689		margining->voltage_steps = val;
1690		val = FIELD_GET(USB4_MARGIN_CAP_2_MAX_VOLTAGE_OFFSET_MASK, margining->caps[2]);
1691		margining->max_voltage_offset = 74 + val * 2;
1692	}
1693
1694	if (supports_optional_voltage_offset_range(margining)) {
1695		val = FIELD_GET(USB4_MARGIN_CAP_0_VOLT_STEPS_OPT_MASK,
1696				margining->caps[0]);
1697		margining->voltage_steps_optional_range = val;
1698		val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_VOLT_OFS_OPT_MASK,
1699				margining->caps[1]);
1700		margining->max_voltage_offset_optional_range = 74 + val * 2;
1701	}
1702
1703	if (supports_time(margining)) {
1704		val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK, margining->caps[1]);
1705		margining->time_steps = val;
1706		val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK, margining->caps[1]);
1707		/*
1708		 * Store it as mUI (milli Unit Interval) because we want
1709		 * to keep it as integer.
1710		 */
1711		margining->max_time_offset = 200 + 10 * val;
1712	}
1713
1714	dir = debugfs_create_dir("margining", parent);
1715	if (supports_hardware(margining)) {
1716		val = FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK, margining->caps[1]);
1717		margining->min_ber_level = val;
1718		val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK, margining->caps[1]);
1719		margining->max_ber_level = val;
1720
1721		/* Set the default to minimum */
1722		margining->ber_level = margining->min_ber_level;
1723
1724		debugfs_create_file("ber_level_contour", 0400, dir, margining,
1725				    &margining_ber_level_fops);
1726	}
1727	debugfs_create_file("caps", 0400, dir, margining, &margining_caps_fops);
1728	debugfs_create_file("lanes", 0600, dir, margining, &margining_lanes_fops);
1729	debugfs_create_file("mode", 0600, dir, margining, &margining_mode_fops);
1730	debugfs_create_file("run", 0600, dir, margining, &margining_run_fops);
1731	debugfs_create_file("results", 0600, dir, margining,
1732			    &margining_results_fops);
1733	debugfs_create_file("test", 0600, dir, margining, &margining_test_fops);
1734	if (independent_voltage_margins(margining) == USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL ||
1735	    (supports_time(margining) &&
1736	     independent_time_margins(margining) == USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR))
1737		debugfs_create_file("margin", 0600, dir, margining, &margining_margin_fops);
1738
1739	margining->error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR;
1740	margining->dwell_time = MIN_DWELL_TIME;
1741
1742	if (supports_optional_voltage_offset_range(margining))
1743		debugfs_create_file("optional_voltage_offset", DEBUGFS_MODE, dir, margining,
1744				    &margining_optional_voltage_offset_fops);
1745
1746	if (supports_software(margining)) {
1747		debugfs_create_file("voltage_time_offset", DEBUGFS_MODE, dir, margining,
1748				    &margining_voltage_time_offset_fops);
1749		debugfs_create_file("error_counter", DEBUGFS_MODE, dir, margining,
1750				    &margining_error_counter_fops);
1751		debugfs_create_file("dwell_time", DEBUGFS_MODE, dir, margining,
1752				    &margining_dwell_time_fops);
1753	}
1754
1755	if (margining->gen >= 4)
1756		debugfs_create_file("eye", 0600, dir, port, &margining_eye_fops);
1757
1758	return margining;
1759}
1760
1761static void margining_port_init(struct tb_port *port)
1762{
1763	struct dentry *parent;
1764	char dir_name[10];
1765
1766	if (!port->usb4)
1767		return;
1768
1769	snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1770	parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1771	port->usb4->margining = margining_alloc(port, &port->usb4->dev,
1772						USB4_SB_TARGET_ROUTER, 0,
1773						parent);
1774}
1775
1776static void margining_port_remove(struct tb_port *port)
1777{
1778	struct dentry *parent;
1779	char dir_name[10];
1780
1781	if (!port->usb4)
1782		return;
1783
1784	snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1785	parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1786	if (parent)
1787		debugfs_lookup_and_remove("margining", parent);
1788
1789	kfree(port->usb4->margining);
1790	port->usb4->margining = NULL;
1791}
1792
1793static void margining_switch_init(struct tb_switch *sw)
1794{
1795	struct tb_port *upstream, *downstream;
1796	struct tb_switch *parent_sw;
1797	u64 route = tb_route(sw);
1798
1799	if (!route)
1800		return;
1801
1802	upstream = tb_upstream_port(sw);
1803	parent_sw = tb_switch_parent(sw);
1804	downstream = tb_port_at(route, parent_sw);
1805
1806	margining_port_init(downstream);
1807	margining_port_init(upstream);
1808}
1809
1810static void margining_switch_remove(struct tb_switch *sw)
1811{
1812	struct tb_port *upstream, *downstream;
1813	struct tb_switch *parent_sw;
1814	u64 route = tb_route(sw);
1815
1816	if (!route)
1817		return;
1818
1819	upstream = tb_upstream_port(sw);
1820	parent_sw = tb_switch_parent(sw);
1821	downstream = tb_port_at(route, parent_sw);
1822
1823	margining_port_remove(upstream);
1824	margining_port_remove(downstream);
1825}
1826
1827static void margining_xdomain_init(struct tb_xdomain *xd)
1828{
1829	struct tb_switch *parent_sw;
1830	struct tb_port *downstream;
1831
1832	parent_sw = tb_xdomain_parent(xd);
1833	downstream = tb_port_at(xd->route, parent_sw);
1834
1835	margining_port_init(downstream);
1836}
1837
1838static void margining_xdomain_remove(struct tb_xdomain *xd)
1839{
1840	struct tb_switch *parent_sw;
1841	struct tb_port *downstream;
1842
1843	parent_sw = tb_xdomain_parent(xd);
1844	downstream = tb_port_at(xd->route, parent_sw);
1845	margining_port_remove(downstream);
1846}
1847
1848static void margining_retimer_init(struct tb_retimer *rt, struct dentry *debugfs_dir)
1849{
1850	rt->margining = margining_alloc(rt->port, &rt->dev,
1851					USB4_SB_TARGET_RETIMER, rt->index,
1852					debugfs_dir);
1853}
1854
1855static void margining_retimer_remove(struct tb_retimer *rt)
1856{
1857	kfree(rt->margining);
1858	rt->margining = NULL;
1859}
1860#else
1861static inline void margining_switch_init(struct tb_switch *sw) { }
1862static inline void margining_switch_remove(struct tb_switch *sw) { }
1863static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1864static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1865static inline void margining_retimer_init(struct tb_retimer *rt,
1866					  struct dentry *debugfs_dir) { }
1867static inline void margining_retimer_remove(struct tb_retimer *rt) { }
1868#endif
1869
1870static int port_clear_all_counters(struct tb_port *port)
1871{
1872	u32 *buf;
1873	int ret;
1874
1875	buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1876		      GFP_KERNEL);
1877	if (!buf)
1878		return -ENOMEM;
1879
1880	ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1881			    COUNTER_SET_LEN * port->config.max_counters);
1882	kfree(buf);
1883
1884	return ret;
1885}
1886
1887static ssize_t counters_write(struct file *file, const char __user *user_buf,
1888			      size_t count, loff_t *ppos)
1889{
1890	struct seq_file *s = file->private_data;
1891	struct tb_port *port = s->private;
1892	struct tb_switch *sw = port->sw;
1893	struct tb *tb = port->sw->tb;
1894	char *buf;
1895	int ret;
1896
1897	buf = validate_and_copy_from_user(user_buf, &count);
1898	if (IS_ERR(buf))
1899		return PTR_ERR(buf);
1900
1901	pm_runtime_get_sync(&sw->dev);
1902
1903	if (mutex_lock_interruptible(&tb->lock)) {
1904		ret = -ERESTARTSYS;
1905		goto out;
1906	}
1907
1908	/* If written delimiter only, clear all counters in one shot */
1909	if (buf[0] == '\n') {
1910		ret = port_clear_all_counters(port);
1911	} else  {
1912		char *line = buf;
1913		u32 val, offset;
1914
1915		ret = -EINVAL;
1916		while (parse_line(&line, &offset, &val, 1, 4)) {
1917			ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1918					    offset, 1);
1919			if (ret)
1920				break;
1921		}
1922	}
1923
1924	mutex_unlock(&tb->lock);
1925
1926out:
1927	pm_runtime_mark_last_busy(&sw->dev);
1928	pm_runtime_put_autosuspend(&sw->dev);
1929	free_page((unsigned long)buf);
1930
1931	return ret < 0 ? ret : count;
1932}
1933
1934static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1935			   struct tb_port *port, unsigned int cap,
1936			   unsigned int offset, u8 cap_id, u8 vsec_id,
1937			   int dwords)
1938{
1939	int i, ret;
1940	u32 data;
1941
1942	for (i = 0; i < dwords; i++) {
1943		if (port)
1944			ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1945		else
1946			ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1947		if (ret) {
1948			seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1949			continue;
1950		}
1951
1952		seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1953			   offset + i, cap_id, vsec_id, data);
1954	}
1955}
1956
1957static void cap_show(struct seq_file *s, struct tb_switch *sw,
1958		     struct tb_port *port, unsigned int cap, u8 cap_id,
1959		     u8 vsec_id, int length)
1960{
1961	int ret, offset = 0;
1962
1963	while (length > 0) {
1964		int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1965		u32 data[TB_MAX_CONFIG_RW_LENGTH];
1966
1967		if (port)
1968			ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1969					   dwords);
1970		else
1971			ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1972		if (ret) {
1973			cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1974			return;
1975		}
1976
1977		for (i = 0; i < dwords; i++) {
1978			seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1979				   cap + offset + i, offset + i,
1980				   cap_id, vsec_id, data[i]);
1981		}
1982
1983		length -= dwords;
1984		offset += dwords;
1985	}
1986}
1987
1988static void port_cap_show(struct tb_port *port, struct seq_file *s,
1989			  unsigned int cap)
1990{
1991	struct tb_cap_any header;
1992	u8 vsec_id = 0;
1993	size_t length;
1994	int ret;
1995
1996	ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1997	if (ret) {
1998		seq_printf(s, "0x%04x <capability read failed>\n", cap);
1999		return;
2000	}
2001
2002	switch (header.basic.cap) {
2003	case TB_PORT_CAP_PHY:
2004		length = PORT_CAP_LANE_LEN;
2005		break;
2006
2007	case TB_PORT_CAP_TIME1:
2008		if (usb4_switch_version(port->sw) < 2)
2009			length = PORT_CAP_TMU_V1_LEN;
2010		else
2011			length = PORT_CAP_TMU_V2_LEN;
2012		break;
2013
2014	case TB_PORT_CAP_POWER:
2015		length = PORT_CAP_POWER_LEN;
2016		break;
2017
2018	case TB_PORT_CAP_ADAP:
2019		if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
2020			if (usb4_switch_version(port->sw) < 2)
2021				length = PORT_CAP_V1_PCIE_LEN;
2022			else
2023				length = PORT_CAP_V2_PCIE_LEN;
2024		} else if (tb_port_is_dpin(port)) {
2025			if (usb4_switch_version(port->sw) < 2)
2026				length = PORT_CAP_DP_V1_LEN;
2027			else
2028				length = PORT_CAP_DP_V2_LEN;
2029		} else if (tb_port_is_dpout(port)) {
2030			length = PORT_CAP_DP_V1_LEN;
2031		} else if (tb_port_is_usb3_down(port) ||
2032			   tb_port_is_usb3_up(port)) {
2033			length = PORT_CAP_USB3_LEN;
2034		} else {
2035			seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
2036				   cap, header.basic.cap);
2037			return;
2038		}
2039		break;
2040
2041	case TB_PORT_CAP_VSE:
2042		if (!header.extended_short.length) {
2043			ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
2044					   cap + 1, 1);
2045			if (ret) {
2046				seq_printf(s, "0x%04x <capability read failed>\n",
2047					   cap + 1);
2048				return;
2049			}
2050			length = header.extended_long.length;
2051			vsec_id = header.extended_short.vsec_id;
2052		} else {
2053			length = header.extended_short.length;
2054			vsec_id = header.extended_short.vsec_id;
2055		}
2056		break;
2057
2058	case TB_PORT_CAP_USB4:
2059		length = PORT_CAP_USB4_LEN;
2060		break;
2061
2062	default:
2063		seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
2064			   cap, header.basic.cap);
2065		return;
2066	}
2067
2068	cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
2069}
2070
2071static void port_caps_show(struct tb_port *port, struct seq_file *s)
2072{
2073	int cap;
2074
2075	cap = tb_port_next_cap(port, 0);
2076	while (cap > 0) {
2077		port_cap_show(port, s, cap);
2078		cap = tb_port_next_cap(port, cap);
2079	}
2080}
2081
2082static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
2083{
2084	u32 data[PORT_CAP_BASIC_LEN];
2085	int ret, i;
2086
2087	ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
2088	if (ret)
2089		return ret;
2090
2091	for (i = 0; i < ARRAY_SIZE(data); i++)
2092		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
2093
2094	return 0;
2095}
2096
2097static int port_regs_show(struct seq_file *s, void *not_used)
2098{
2099	struct tb_port *port = s->private;
2100	struct tb_switch *sw = port->sw;
2101	struct tb *tb = sw->tb;
2102	int ret;
2103
2104	pm_runtime_get_sync(&sw->dev);
2105
2106	if (mutex_lock_interruptible(&tb->lock)) {
2107		ret = -ERESTARTSYS;
2108		goto out_rpm_put;
2109	}
2110
2111	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
2112
2113	ret = port_basic_regs_show(port, s);
2114	if (ret)
2115		goto out_unlock;
2116
2117	port_caps_show(port, s);
2118
2119out_unlock:
2120	mutex_unlock(&tb->lock);
2121out_rpm_put:
2122	pm_runtime_mark_last_busy(&sw->dev);
2123	pm_runtime_put_autosuspend(&sw->dev);
2124
2125	return ret;
2126}
2127DEBUGFS_ATTR_RW(port_regs);
2128
2129static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
2130			    unsigned int cap)
2131{
2132	struct tb_cap_any header;
2133	int ret, length;
2134	u8 vsec_id = 0;
2135
2136	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
2137	if (ret) {
2138		seq_printf(s, "0x%04x <capability read failed>\n", cap);
2139		return;
2140	}
2141
2142	if (header.basic.cap == TB_SWITCH_CAP_VSE) {
2143		if (!header.extended_short.length) {
2144			ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
2145					 cap + 1, 1);
2146			if (ret) {
2147				seq_printf(s, "0x%04x <capability read failed>\n",
2148					   cap + 1);
2149				return;
2150			}
2151			length = header.extended_long.length;
2152		} else {
2153			length = header.extended_short.length;
2154		}
2155		vsec_id = header.extended_short.vsec_id;
2156	} else {
2157		if (header.basic.cap == TB_SWITCH_CAP_TMU) {
2158			length = SWITCH_CAP_TMU_LEN;
2159		} else  {
2160			seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
2161				   cap, header.basic.cap);
2162			return;
2163		}
2164	}
2165
2166	cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
2167}
2168
2169static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
2170{
2171	int cap;
2172
2173	cap = tb_switch_next_cap(sw, 0);
2174	while (cap > 0) {
2175		switch_cap_show(sw, s, cap);
2176		cap = tb_switch_next_cap(sw, cap);
2177	}
2178}
2179
2180static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
2181{
2182	u32 data[SWITCH_CAP_BASIC_LEN];
2183	size_t dwords;
2184	int ret, i;
2185
2186	/* Only USB4 has the additional registers */
2187	if (tb_switch_is_usb4(sw))
2188		dwords = ARRAY_SIZE(data);
2189	else
2190		dwords = 5;
2191
2192	ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
2193	if (ret)
2194		return ret;
2195
2196	for (i = 0; i < dwords; i++)
2197		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
2198
2199	return 0;
2200}
2201
2202static int switch_regs_show(struct seq_file *s, void *not_used)
2203{
2204	struct tb_switch *sw = s->private;
2205	struct tb *tb = sw->tb;
2206	int ret;
2207
2208	pm_runtime_get_sync(&sw->dev);
2209
2210	if (mutex_lock_interruptible(&tb->lock)) {
2211		ret = -ERESTARTSYS;
2212		goto out_rpm_put;
2213	}
2214
2215	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
2216
2217	ret = switch_basic_regs_show(sw, s);
2218	if (ret)
2219		goto out_unlock;
2220
2221	switch_caps_show(sw, s);
2222
2223out_unlock:
2224	mutex_unlock(&tb->lock);
2225out_rpm_put:
2226	pm_runtime_mark_last_busy(&sw->dev);
2227	pm_runtime_put_autosuspend(&sw->dev);
2228
2229	return ret;
2230}
2231DEBUGFS_ATTR_RW(switch_regs);
2232
2233static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
2234{
2235	u32 data[PATH_LEN];
2236	int ret, i;
2237
2238	ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
2239			   ARRAY_SIZE(data));
2240	if (ret) {
2241		seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
2242		return ret;
2243	}
2244
2245	for (i = 0; i < ARRAY_SIZE(data); i++) {
2246		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
2247			   hopid * PATH_LEN + i, i, hopid, data[i]);
2248	}
2249
2250	return 0;
2251}
2252
2253static int path_show(struct seq_file *s, void *not_used)
2254{
2255	struct tb_port *port = s->private;
2256	struct tb_switch *sw = port->sw;
2257	struct tb *tb = sw->tb;
2258	int start, i, ret = 0;
2259
2260	pm_runtime_get_sync(&sw->dev);
2261
2262	if (mutex_lock_interruptible(&tb->lock)) {
2263		ret = -ERESTARTSYS;
2264		goto out_rpm_put;
2265	}
2266
2267	seq_puts(s, "# offset relative_offset in_hop_id value\n");
2268
2269	/* NHI and lane adapters have entry for path 0 */
2270	if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
2271		ret = path_show_one(port, s, 0);
2272		if (ret)
2273			goto out_unlock;
2274	}
2275
2276	start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
2277
2278	for (i = start; i <= port->config.max_in_hop_id; i++) {
2279		ret = path_show_one(port, s, i);
2280		if (ret)
2281			break;
2282	}
2283
2284out_unlock:
2285	mutex_unlock(&tb->lock);
2286out_rpm_put:
2287	pm_runtime_mark_last_busy(&sw->dev);
2288	pm_runtime_put_autosuspend(&sw->dev);
2289
2290	return ret;
2291}
2292DEBUGFS_ATTR_RW(path);
2293
2294static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
2295				 int counter)
2296{
2297	u32 data[COUNTER_SET_LEN];
2298	int ret, i;
2299
2300	ret = tb_port_read(port, data, TB_CFG_COUNTERS,
2301			   counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
2302	if (ret) {
2303		seq_printf(s, "0x%04x <not accessible>\n",
2304			   counter * COUNTER_SET_LEN);
2305		return ret;
2306	}
2307
2308	for (i = 0; i < ARRAY_SIZE(data); i++) {
2309		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
2310			   counter * COUNTER_SET_LEN + i, i, counter, data[i]);
2311	}
2312
2313	return 0;
2314}
2315
2316static int counters_show(struct seq_file *s, void *not_used)
2317{
2318	struct tb_port *port = s->private;
2319	struct tb_switch *sw = port->sw;
2320	struct tb *tb = sw->tb;
2321	int i, ret = 0;
2322
2323	pm_runtime_get_sync(&sw->dev);
2324
2325	if (mutex_lock_interruptible(&tb->lock)) {
2326		ret = -ERESTARTSYS;
2327		goto out;
2328	}
2329
2330	seq_puts(s, "# offset relative_offset counter_id value\n");
2331
2332	for (i = 0; i < port->config.max_counters; i++) {
2333		ret = counter_set_regs_show(port, s, i);
2334		if (ret)
2335			break;
2336	}
2337
2338	mutex_unlock(&tb->lock);
2339
2340out:
2341	pm_runtime_mark_last_busy(&sw->dev);
2342	pm_runtime_put_autosuspend(&sw->dev);
2343
2344	return ret;
2345}
2346DEBUGFS_ATTR_RW(counters);
2347
2348static int sb_regs_show(struct tb_port *port, const struct sb_reg *sb_regs,
2349			size_t size, enum usb4_sb_target target, u8 index,
2350			struct seq_file *s)
2351{
2352	int ret, i;
2353
2354	seq_puts(s, "# register value\n");
2355
2356	for (i = 0; i < size; i++) {
2357		const struct sb_reg *regs = &sb_regs[i];
2358		u8 data[64];
2359		int j;
2360
2361		memset(data, 0, sizeof(data));
2362		ret = usb4_port_sb_read(port, target, index, regs->reg, data,
2363					regs->size);
2364		if (ret)
2365			return ret;
2366
2367		seq_printf(s, "0x%02x", regs->reg);
2368		for (j = 0; j < regs->size; j++)
2369			seq_printf(s, " 0x%02x", data[j]);
2370		seq_puts(s, "\n");
2371	}
2372
2373	return 0;
2374}
2375
2376static int port_sb_regs_show(struct seq_file *s, void *not_used)
2377{
2378	struct tb_port *port = s->private;
2379	struct tb_switch *sw = port->sw;
2380	struct tb *tb = sw->tb;
2381	int ret;
2382
2383	pm_runtime_get_sync(&sw->dev);
2384
2385	if (mutex_lock_interruptible(&tb->lock)) {
2386		ret = -ERESTARTSYS;
2387		goto out_rpm_put;
2388	}
2389
2390	ret = sb_regs_show(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
2391			   USB4_SB_TARGET_ROUTER, 0, s);
2392
2393	mutex_unlock(&tb->lock);
2394out_rpm_put:
2395	pm_runtime_mark_last_busy(&sw->dev);
2396	pm_runtime_put_autosuspend(&sw->dev);
2397
2398	return ret;
2399}
2400DEBUGFS_ATTR_RW(port_sb_regs);
2401
2402/**
2403 * tb_switch_debugfs_init() - Add debugfs entries for router
2404 * @sw: Pointer to the router
2405 *
2406 * Adds debugfs directories and files for given router.
2407 */
2408void tb_switch_debugfs_init(struct tb_switch *sw)
2409{
2410	struct dentry *debugfs_dir;
2411	struct tb_port *port;
2412
2413	debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
2414	sw->debugfs_dir = debugfs_dir;
2415	debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
2416			    &switch_regs_fops);
2417	if (sw->drom)
2418		debugfs_create_blob("drom", 0400, debugfs_dir, &sw->drom_blob);
2419
2420	tb_switch_for_each_port(sw, port) {
2421		struct dentry *debugfs_dir;
2422		char dir_name[10];
2423
2424		if (port->disabled)
2425			continue;
2426		if (port->config.type == TB_TYPE_INACTIVE)
2427			continue;
2428
2429		snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
2430		debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
2431		debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
2432				    port, &port_regs_fops);
2433		debugfs_create_file("path", 0400, debugfs_dir, port,
2434				    &path_fops);
2435		if (port->config.counters_support)
2436			debugfs_create_file("counters", 0600, debugfs_dir, port,
2437					    &counters_fops);
2438		if (port->usb4)
2439			debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir,
2440					    port, &port_sb_regs_fops);
2441	}
2442
2443	margining_switch_init(sw);
2444}
2445
2446/**
2447 * tb_switch_debugfs_remove() - Remove all router debugfs entries
2448 * @sw: Pointer to the router
2449 *
2450 * Removes all previously added debugfs entries under this router.
2451 */
2452void tb_switch_debugfs_remove(struct tb_switch *sw)
2453{
2454	margining_switch_remove(sw);
2455	debugfs_remove_recursive(sw->debugfs_dir);
2456}
2457
2458void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
2459{
2460	margining_xdomain_init(xd);
2461}
2462
2463void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
2464{
2465	margining_xdomain_remove(xd);
2466}
2467
2468/**
2469 * tb_service_debugfs_init() - Add debugfs directory for service
2470 * @svc: Thunderbolt service pointer
2471 *
2472 * Adds debugfs directory for service.
2473 */
2474void tb_service_debugfs_init(struct tb_service *svc)
2475{
2476	svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
2477					      tb_debugfs_root);
2478}
2479
2480/**
2481 * tb_service_debugfs_remove() - Remove service debugfs directory
2482 * @svc: Thunderbolt service pointer
2483 *
2484 * Removes the previously created debugfs directory for @svc.
2485 */
2486void tb_service_debugfs_remove(struct tb_service *svc)
2487{
2488	debugfs_remove_recursive(svc->debugfs_dir);
2489	svc->debugfs_dir = NULL;
2490}
2491
2492static int retimer_sb_regs_show(struct seq_file *s, void *not_used)
2493{
2494	struct tb_retimer *rt = s->private;
2495	struct tb *tb = rt->tb;
2496	int ret;
2497
2498	pm_runtime_get_sync(&rt->dev);
2499
2500	if (mutex_lock_interruptible(&tb->lock)) {
2501		ret = -ERESTARTSYS;
2502		goto out_rpm_put;
2503	}
2504
2505	ret = sb_regs_show(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
2506			   USB4_SB_TARGET_RETIMER, rt->index, s);
2507
2508	mutex_unlock(&tb->lock);
2509out_rpm_put:
2510	pm_runtime_mark_last_busy(&rt->dev);
2511	pm_runtime_put_autosuspend(&rt->dev);
2512
2513	return ret;
2514}
2515DEBUGFS_ATTR_RW(retimer_sb_regs);
2516
2517/**
2518 * tb_retimer_debugfs_init() - Add debugfs directory for retimer
2519 * @rt: Pointer to retimer structure
2520 *
2521 * Adds and populates retimer debugfs directory.
2522 */
2523void tb_retimer_debugfs_init(struct tb_retimer *rt)
2524{
2525	struct dentry *debugfs_dir;
2526
2527	debugfs_dir = debugfs_create_dir(dev_name(&rt->dev), tb_debugfs_root);
2528	debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, rt,
2529			    &retimer_sb_regs_fops);
2530	margining_retimer_init(rt, debugfs_dir);
2531}
2532
2533/**
2534 * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
2535 * @rt: Pointer to retimer structure
2536 *
2537 * Removes the retimer debugfs directory along with its contents.
2538 */
2539void tb_retimer_debugfs_remove(struct tb_retimer *rt)
2540{
2541	debugfs_lookup_and_remove(dev_name(&rt->dev), tb_debugfs_root);
2542	margining_retimer_remove(rt);
2543}
2544
2545void tb_debugfs_init(void)
2546{
2547	tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
2548}
2549
2550void tb_debugfs_exit(void)
2551{
2552	debugfs_remove_recursive(tb_debugfs_root);
2553}
~/ Read-only file preview.