Linux Audio

Check our new training course

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