xref: /linux/drivers/thunderbolt/debugfs.c (revision 0890fc36c70c8d5e80dc128c3a9d7a9122646869)
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 /* Sideband registers and their sizes as defined in the USB4 spec */
37 struct sb_reg {
38 	unsigned int reg;
39 	unsigned int size;
40 };
41 
42 #define SB_MAX_SIZE		64
43 
44 /* Sideband registers for router */
45 static const struct sb_reg port_sb_regs[] = {
46 	{ USB4_SB_VENDOR_ID, 4 },
47 	{ USB4_SB_PRODUCT_ID, 4 },
48 	{ USB4_SB_DEBUG_CONF, 4 },
49 	{ USB4_SB_DEBUG, 54 },
50 	{ USB4_SB_LRD_TUNING, 4 },
51 	{ USB4_SB_OPCODE, 4 },
52 	{ USB4_SB_METADATA, 4 },
53 	{ USB4_SB_LINK_CONF, 3 },
54 	{ USB4_SB_GEN23_TXFFE, 4 },
55 	{ USB4_SB_GEN4_TXFFE, 4 },
56 	{ USB4_SB_VERSION, 4 },
57 	{ USB4_SB_DATA, 64 },
58 };
59 
60 /* Sideband registers for retimer */
61 static const struct sb_reg retimer_sb_regs[] = {
62 	{ USB4_SB_VENDOR_ID, 4 },
63 	{ USB4_SB_PRODUCT_ID, 4 },
64 	{ USB4_SB_FW_VERSION, 4 },
65 	{ USB4_SB_LRD_TUNING, 4 },
66 	{ USB4_SB_OPCODE, 4 },
67 	{ USB4_SB_METADATA, 4 },
68 	{ USB4_SB_GEN23_TXFFE, 4 },
69 	{ USB4_SB_GEN4_TXFFE, 4 },
70 	{ USB4_SB_VERSION, 4 },
71 	{ USB4_SB_DATA, 64 },
72 };
73 
74 #define DEBUGFS_ATTR(__space, __write)					\
75 static int __space ## _open(struct inode *inode, struct file *file)	\
76 {									\
77 	return single_open(file, __space ## _show, inode->i_private);	\
78 }									\
79 									\
80 static const struct file_operations __space ## _fops = {		\
81 	.owner = THIS_MODULE,						\
82 	.open = __space ## _open,					\
83 	.release = single_release,					\
84 	.read  = seq_read,						\
85 	.write = __write,						\
86 	.llseek = seq_lseek,						\
87 }
88 
89 #define DEBUGFS_ATTR_RO(__space)					\
90 	DEBUGFS_ATTR(__space, NULL)
91 
92 #define DEBUGFS_ATTR_RW(__space)					\
93 	DEBUGFS_ATTR(__space, __space ## _write)
94 
95 static struct dentry *tb_debugfs_root;
96 
97 static void *validate_and_copy_from_user(const void __user *user_buf,
98 					 size_t *count)
99 {
100 	size_t nbytes;
101 	void *buf;
102 
103 	if (!*count)
104 		return ERR_PTR(-EINVAL);
105 
106 	if (!access_ok(user_buf, *count))
107 		return ERR_PTR(-EFAULT);
108 
109 	buf = (void *)get_zeroed_page(GFP_KERNEL);
110 	if (!buf)
111 		return ERR_PTR(-ENOMEM);
112 
113 	nbytes = min_t(size_t, *count, PAGE_SIZE);
114 	if (copy_from_user(buf, user_buf, nbytes)) {
115 		free_page((unsigned long)buf);
116 		return ERR_PTR(-EFAULT);
117 	}
118 
119 	*count = nbytes;
120 	return buf;
121 }
122 
123 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
124 		       int long_fmt_len)
125 {
126 	char *token;
127 	u32 v[5];
128 	int ret;
129 
130 	token = strsep(line, "\n");
131 	if (!token)
132 		return false;
133 
134 	/*
135 	 * For Adapter/Router configuration space:
136 	 * Short format is: offset value\n
137 	 *		    v[0]   v[1]
138 	 * Long format as produced from the read side:
139 	 * offset relative_offset cap_id vs_cap_id value\n
140 	 * v[0]   v[1]            v[2]   v[3]      v[4]
141 	 *
142 	 * For Counter configuration space:
143 	 * Short format is: offset\n
144 	 *		    v[0]
145 	 * Long format as produced from the read side:
146 	 * offset relative_offset counter_id value\n
147 	 * v[0]   v[1]            v[2]       v[3]
148 	 */
149 	ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
150 	/* In case of Counters, clear counter, "val" content is NA */
151 	if (ret == short_fmt_len) {
152 		*offs = v[0];
153 		*val = v[short_fmt_len - 1];
154 		return true;
155 	} else if (ret == long_fmt_len) {
156 		*offs = v[0];
157 		*val = v[long_fmt_len - 1];
158 		return true;
159 	}
160 
161 	return false;
162 }
163 
164 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
165 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
166 			  const char __user *user_buf, size_t count,
167 			  loff_t *ppos)
168 {
169 	struct tb *tb = sw->tb;
170 	char *line, *buf;
171 	u32 val, offset;
172 	int ret = 0;
173 
174 	buf = validate_and_copy_from_user(user_buf, &count);
175 	if (IS_ERR(buf))
176 		return PTR_ERR(buf);
177 
178 	pm_runtime_get_sync(&sw->dev);
179 
180 	if (mutex_lock_interruptible(&tb->lock)) {
181 		ret = -ERESTARTSYS;
182 		goto out;
183 	}
184 
185 	/* User did hardware changes behind the driver's back */
186 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
187 
188 	line = buf;
189 	while (parse_line(&line, &offset, &val, 2, 5)) {
190 		if (port)
191 			ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
192 		else
193 			ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
194 		if (ret)
195 			break;
196 	}
197 
198 	mutex_unlock(&tb->lock);
199 
200 out:
201 	pm_runtime_mark_last_busy(&sw->dev);
202 	pm_runtime_put_autosuspend(&sw->dev);
203 	free_page((unsigned long)buf);
204 
205 	return ret < 0 ? ret : count;
206 }
207 
208 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
209 			       size_t count, loff_t *ppos)
210 {
211 	struct seq_file *s = file->private_data;
212 	struct tb_port *port = s->private;
213 
214 	return regs_write(port->sw, port, user_buf, count, ppos);
215 }
216 
217 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
218 				 size_t count, loff_t *ppos)
219 {
220 	struct seq_file *s = file->private_data;
221 	struct tb_switch *sw = s->private;
222 
223 	return regs_write(sw, NULL, user_buf, count, ppos);
224 }
225 
226 static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size,
227 			  size_t *bytes_read)
228 {
229 	char *field, *token;
230 	int i;
231 
232 	token = strsep(line, "\n");
233 	if (!token)
234 		return false;
235 
236 	/* Parse the register first */
237 	field = strsep(&token, " ");
238 	if (!field)
239 		return false;
240 	if (kstrtou8(field, 0, reg))
241 		return false;
242 
243 	/* Then the values for the register, up to data_size */
244 	for (i = 0; i < data_size; i++) {
245 		field = strsep(&token, " ");
246 		if (!field)
247 			break;
248 		if (kstrtou8(field, 0, &data[i]))
249 			return false;
250 	}
251 
252 	*bytes_read = i;
253 	return true;
254 }
255 
256 static ssize_t sb_regs_write(struct tb_port *port, const struct sb_reg *sb_regs,
257 			     size_t size, enum usb4_sb_target target, u8 index,
258 			     char *buf, size_t count, loff_t *ppos)
259 {
260 	u8 reg, data[SB_MAX_SIZE];
261 	size_t bytes_read;
262 	char *line = buf;
263 
264 	/* User did hardware changes behind the driver's back */
265 	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
266 
267 	/*
268 	 * For sideband registers we accept:
269 	 * reg b0 b1 b2...\n
270 	 *
271 	 * Here "reg" is the byte offset of the sideband register and "b0"..
272 	 * are the byte values. There can be less byte values than the register
273 	 * size. The leftovers will not be overwritten.
274 	 */
275 	while (parse_sb_line(&line, &reg, data, ARRAY_SIZE(data), &bytes_read)) {
276 		const struct sb_reg *sb_reg;
277 		int ret;
278 
279 		/* At least one byte must be passed */
280 		if (bytes_read < 1)
281 			return -EINVAL;
282 
283 		/* Find the register */
284 		sb_reg = NULL;
285 		for (int i = 0; i < size; i++) {
286 			if (sb_regs[i].reg == reg) {
287 				sb_reg = &sb_regs[i];
288 				break;
289 			}
290 		}
291 
292 		if (!sb_reg)
293 			return -EINVAL;
294 
295 		if (bytes_read > sb_regs->size)
296 			return -E2BIG;
297 
298 		ret = usb4_port_sb_write(port, target, index, sb_reg->reg, data,
299 					 bytes_read);
300 		if (ret)
301 			return ret;
302 	}
303 
304 	return 0;
305 }
306 
307 static ssize_t port_sb_regs_write(struct file *file, const char __user *user_buf,
308 				  size_t count, loff_t *ppos)
309 {
310 	struct seq_file *s = file->private_data;
311 	struct tb_port *port = s->private;
312 	struct tb_switch *sw = port->sw;
313 	struct tb *tb = sw->tb;
314 	char *buf;
315 	int ret;
316 
317 	buf = validate_and_copy_from_user(user_buf, &count);
318 	if (IS_ERR(buf))
319 		return PTR_ERR(buf);
320 
321 	pm_runtime_get_sync(&sw->dev);
322 
323 	if (mutex_lock_interruptible(&tb->lock)) {
324 		ret = -ERESTARTSYS;
325 		goto out_rpm_put;
326 	}
327 
328 	ret = sb_regs_write(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
329 			    USB4_SB_TARGET_ROUTER, 0, buf, count, ppos);
330 
331 	mutex_unlock(&tb->lock);
332 out_rpm_put:
333 	pm_runtime_mark_last_busy(&sw->dev);
334 	pm_runtime_put_autosuspend(&sw->dev);
335 
336 	return ret < 0 ? ret : count;
337 }
338 
339 static ssize_t retimer_sb_regs_write(struct file *file,
340 				     const char __user *user_buf,
341 				     size_t count, loff_t *ppos)
342 {
343 	struct seq_file *s = file->private_data;
344 	struct tb_retimer *rt = s->private;
345 	struct tb *tb = rt->tb;
346 	char *buf;
347 	int ret;
348 
349 	buf = validate_and_copy_from_user(user_buf, &count);
350 	if (IS_ERR(buf))
351 		return PTR_ERR(buf);
352 
353 	pm_runtime_get_sync(&rt->dev);
354 
355 	if (mutex_lock_interruptible(&tb->lock)) {
356 		ret = -ERESTARTSYS;
357 		goto out_rpm_put;
358 	}
359 
360 	ret = sb_regs_write(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
361 			    USB4_SB_TARGET_RETIMER, rt->index, buf, count, ppos);
362 
363 	mutex_unlock(&tb->lock);
364 out_rpm_put:
365 	pm_runtime_mark_last_busy(&rt->dev);
366 	pm_runtime_put_autosuspend(&rt->dev);
367 
368 	return ret < 0 ? ret : count;
369 }
370 #define DEBUGFS_MODE		0600
371 #else
372 #define port_regs_write		NULL
373 #define switch_regs_write	NULL
374 #define port_sb_regs_write	NULL
375 #define retimer_sb_regs_write	NULL
376 #define DEBUGFS_MODE		0400
377 #endif
378 
379 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
380 /**
381  * struct tb_margining - Lane margining support
382  * @port: USB4 port through which the margining operations are run
383  * @caps: Port lane margining capabilities
384  * @results: Last lane margining results
385  * @lanes: %0, %1 or %7 (all)
386  * @min_ber_level: Minimum supported BER level contour value
387  * @max_ber_level: Maximum supported BER level contour value
388  * @ber_level: Current BER level contour value
389  * @voltage_steps: Number of mandatory voltage steps
390  * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
391  * @time_steps: Number of time margin steps
392  * @max_time_offset: Maximum time margin offset (in mUI)
393  * @software: %true if software margining is used instead of hardware
394  * @time: %true if time margining is used instead of voltage
395  * @right_high: %false if left/low margin test is performed, %true if
396  *		right/high
397  */
398 struct tb_margining {
399 	struct tb_port *port;
400 	u32 caps[2];
401 	u32 results[2];
402 	unsigned int lanes;
403 	unsigned int min_ber_level;
404 	unsigned int max_ber_level;
405 	unsigned int ber_level;
406 	unsigned int voltage_steps;
407 	unsigned int max_voltage_offset;
408 	unsigned int time_steps;
409 	unsigned int max_time_offset;
410 	bool software;
411 	bool time;
412 	bool right_high;
413 };
414 
415 static bool supports_software(const struct tb_margining *margining)
416 {
417 	return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
418 }
419 
420 static bool supports_hardware(const struct tb_margining *margining)
421 {
422 	return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
423 }
424 
425 static bool both_lanes(const struct tb_margining *margining)
426 {
427 	return margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
428 }
429 
430 static unsigned int
431 independent_voltage_margins(const struct tb_margining *margining)
432 {
433 	return (margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK) >>
434 		USB4_MARGIN_CAP_0_VOLTAGE_INDP_SHIFT;
435 }
436 
437 static bool supports_time(const struct tb_margining *margining)
438 {
439 	return margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
440 }
441 
442 /* Only applicable if supports_time() returns true */
443 static unsigned int
444 independent_time_margins(const struct tb_margining *margining)
445 {
446 	return (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_INDP_MASK) >>
447 		USB4_MARGIN_CAP_1_TIME_INDP_SHIFT;
448 }
449 
450 static ssize_t
451 margining_ber_level_write(struct file *file, const char __user *user_buf,
452 			   size_t count, loff_t *ppos)
453 {
454 	struct seq_file *s = file->private_data;
455 	struct tb_margining *margining = s->private;
456 	struct tb *tb = margining->port->sw->tb;
457 	unsigned int val;
458 	int ret = 0;
459 	char *buf;
460 
461 	if (mutex_lock_interruptible(&tb->lock))
462 		return -ERESTARTSYS;
463 
464 	if (margining->software) {
465 		ret = -EINVAL;
466 		goto out_unlock;
467 	}
468 
469 	buf = validate_and_copy_from_user(user_buf, &count);
470 	if (IS_ERR(buf)) {
471 		ret = PTR_ERR(buf);
472 		goto out_unlock;
473 	}
474 
475 	buf[count - 1] = '\0';
476 
477 	ret = kstrtouint(buf, 10, &val);
478 	if (ret)
479 		goto out_free;
480 
481 	if (val < margining->min_ber_level ||
482 	    val > margining->max_ber_level) {
483 		ret = -EINVAL;
484 		goto out_free;
485 	}
486 
487 	margining->ber_level = val;
488 
489 out_free:
490 	free_page((unsigned long)buf);
491 out_unlock:
492 	mutex_unlock(&tb->lock);
493 
494 	return ret < 0 ? ret : count;
495 }
496 
497 static void ber_level_show(struct seq_file *s, unsigned int val)
498 {
499 	if (val % 2)
500 		seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
501 	else
502 		seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
503 }
504 
505 static int margining_ber_level_show(struct seq_file *s, void *not_used)
506 {
507 	const struct tb_margining *margining = s->private;
508 
509 	if (margining->software)
510 		return -EINVAL;
511 	ber_level_show(s, margining->ber_level);
512 	return 0;
513 }
514 DEBUGFS_ATTR_RW(margining_ber_level);
515 
516 static int margining_caps_show(struct seq_file *s, void *not_used)
517 {
518 	struct tb_margining *margining = s->private;
519 	struct tb *tb = margining->port->sw->tb;
520 	u32 cap0, cap1;
521 
522 	if (mutex_lock_interruptible(&tb->lock))
523 		return -ERESTARTSYS;
524 
525 	/* Dump the raw caps first */
526 	cap0 = margining->caps[0];
527 	seq_printf(s, "0x%08x\n", cap0);
528 	cap1 = margining->caps[1];
529 	seq_printf(s, "0x%08x\n", cap1);
530 
531 	seq_printf(s, "# software margining: %s\n",
532 		   supports_software(margining) ? "yes" : "no");
533 	if (supports_hardware(margining)) {
534 		seq_puts(s, "# hardware margining: yes\n");
535 		seq_puts(s, "# minimum BER level contour: ");
536 		ber_level_show(s, margining->min_ber_level);
537 		seq_puts(s, "# maximum BER level contour: ");
538 		ber_level_show(s, margining->max_ber_level);
539 	} else {
540 		seq_puts(s, "# hardware margining: no\n");
541 	}
542 
543 	seq_printf(s, "# both lanes simultaneously: %s\n",
544 		  both_lanes(margining) ? "yes" : "no");
545 	seq_printf(s, "# voltage margin steps: %u\n",
546 		   margining->voltage_steps);
547 	seq_printf(s, "# maximum voltage offset: %u mV\n",
548 		   margining->max_voltage_offset);
549 
550 	switch (independent_voltage_margins(margining)) {
551 	case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
552 		seq_puts(s, "# returns minimum between high and low voltage margins\n");
553 		break;
554 	case USB4_MARGIN_CAP_0_VOLTAGE_HL:
555 		seq_puts(s, "# returns high or low voltage margin\n");
556 		break;
557 	case USB4_MARGIN_CAP_0_VOLTAGE_BOTH:
558 		seq_puts(s, "# returns both high and low margins\n");
559 		break;
560 	}
561 
562 	if (supports_time(margining)) {
563 		seq_puts(s, "# time margining: yes\n");
564 		seq_printf(s, "# time margining is destructive: %s\n",
565 			   cap1 & USB4_MARGIN_CAP_1_TIME_DESTR ? "yes" : "no");
566 
567 		switch (independent_time_margins(margining)) {
568 		case USB4_MARGIN_CAP_1_TIME_MIN:
569 			seq_puts(s, "# returns minimum between left and right time margins\n");
570 			break;
571 		case USB4_MARGIN_CAP_1_TIME_LR:
572 			seq_puts(s, "# returns left or right margin\n");
573 			break;
574 		case USB4_MARGIN_CAP_1_TIME_BOTH:
575 			seq_puts(s, "# returns both left and right margins\n");
576 			break;
577 		}
578 
579 		seq_printf(s, "# time margin steps: %u\n",
580 			   margining->time_steps);
581 		seq_printf(s, "# maximum time offset: %u mUI\n",
582 			   margining->max_time_offset);
583 	} else {
584 		seq_puts(s, "# time margining: no\n");
585 	}
586 
587 	mutex_unlock(&tb->lock);
588 	return 0;
589 }
590 DEBUGFS_ATTR_RO(margining_caps);
591 
592 static ssize_t
593 margining_lanes_write(struct file *file, const char __user *user_buf,
594 		      size_t count, loff_t *ppos)
595 {
596 	struct seq_file *s = file->private_data;
597 	struct tb_margining *margining = s->private;
598 	struct tb *tb = margining->port->sw->tb;
599 	int ret = 0;
600 	char *buf;
601 
602 	buf = validate_and_copy_from_user(user_buf, &count);
603 	if (IS_ERR(buf))
604 		return PTR_ERR(buf);
605 
606 	buf[count - 1] = '\0';
607 
608 	if (mutex_lock_interruptible(&tb->lock)) {
609 		ret = -ERESTARTSYS;
610 		goto out_free;
611 	}
612 
613 	if (!strcmp(buf, "0")) {
614 		margining->lanes = 0;
615 	} else if (!strcmp(buf, "1")) {
616 		margining->lanes = 1;
617 	} else if (!strcmp(buf, "all")) {
618 		/* Needs to be supported */
619 		if (both_lanes(margining))
620 			margining->lanes = 7;
621 		else
622 			ret = -EINVAL;
623 	} else {
624 		ret = -EINVAL;
625 	}
626 
627 	mutex_unlock(&tb->lock);
628 
629 out_free:
630 	free_page((unsigned long)buf);
631 	return ret < 0 ? ret : count;
632 }
633 
634 static int margining_lanes_show(struct seq_file *s, void *not_used)
635 {
636 	struct tb_margining *margining = s->private;
637 	struct tb *tb = margining->port->sw->tb;
638 	unsigned int lanes;
639 
640 	if (mutex_lock_interruptible(&tb->lock))
641 		return -ERESTARTSYS;
642 
643 	lanes = margining->lanes;
644 	if (both_lanes(margining)) {
645 		if (!lanes)
646 			seq_puts(s, "[0] 1 all\n");
647 		else if (lanes == 1)
648 			seq_puts(s, "0 [1] all\n");
649 		else
650 			seq_puts(s, "0 1 [all]\n");
651 	} else {
652 		if (!lanes)
653 			seq_puts(s, "[0] 1\n");
654 		else
655 			seq_puts(s, "0 [1]\n");
656 	}
657 
658 	mutex_unlock(&tb->lock);
659 	return 0;
660 }
661 DEBUGFS_ATTR_RW(margining_lanes);
662 
663 static ssize_t margining_mode_write(struct file *file,
664 				   const char __user *user_buf,
665 				   size_t count, loff_t *ppos)
666 {
667 	struct seq_file *s = file->private_data;
668 	struct tb_margining *margining = s->private;
669 	struct tb *tb = margining->port->sw->tb;
670 	int ret = 0;
671 	char *buf;
672 
673 	buf = validate_and_copy_from_user(user_buf, &count);
674 	if (IS_ERR(buf))
675 		return PTR_ERR(buf);
676 
677 	buf[count - 1] = '\0';
678 
679 	if (mutex_lock_interruptible(&tb->lock)) {
680 		ret = -ERESTARTSYS;
681 		goto out_free;
682 	}
683 
684 	if (!strcmp(buf, "software")) {
685 		if (supports_software(margining))
686 			margining->software = true;
687 		else
688 			ret = -EINVAL;
689 	} else if (!strcmp(buf, "hardware")) {
690 		if (supports_hardware(margining))
691 			margining->software = false;
692 		else
693 			ret = -EINVAL;
694 	} else {
695 		ret = -EINVAL;
696 	}
697 
698 	mutex_unlock(&tb->lock);
699 
700 out_free:
701 	free_page((unsigned long)buf);
702 	return ret ? ret : count;
703 }
704 
705 static int margining_mode_show(struct seq_file *s, void *not_used)
706 {
707 	struct tb_margining *margining = s->private;
708 	struct tb *tb = margining->port->sw->tb;
709 	const char *space = "";
710 
711 	if (mutex_lock_interruptible(&tb->lock))
712 		return -ERESTARTSYS;
713 
714 	if (supports_software(margining)) {
715 		if (margining->software)
716 			seq_puts(s, "[software]");
717 		else
718 			seq_puts(s, "software");
719 		space = " ";
720 	}
721 	if (supports_hardware(margining)) {
722 		if (margining->software)
723 			seq_printf(s, "%shardware", space);
724 		else
725 			seq_printf(s, "%s[hardware]", space);
726 	}
727 
728 	mutex_unlock(&tb->lock);
729 
730 	seq_puts(s, "\n");
731 	return 0;
732 }
733 DEBUGFS_ATTR_RW(margining_mode);
734 
735 static int margining_run_write(void *data, u64 val)
736 {
737 	struct tb_margining *margining = data;
738 	struct tb_port *port = margining->port;
739 	struct tb_switch *sw = port->sw;
740 	struct tb_switch *down_sw;
741 	struct tb *tb = sw->tb;
742 	int ret, clx;
743 
744 	if (val != 1)
745 		return -EINVAL;
746 
747 	pm_runtime_get_sync(&sw->dev);
748 
749 	if (mutex_lock_interruptible(&tb->lock)) {
750 		ret = -ERESTARTSYS;
751 		goto out_rpm_put;
752 	}
753 
754 	if (tb_is_upstream_port(port))
755 		down_sw = sw;
756 	else if (port->remote)
757 		down_sw = port->remote->sw;
758 	else
759 		down_sw = NULL;
760 
761 	if (down_sw) {
762 		/*
763 		 * CL states may interfere with lane margining so
764 		 * disable them temporarily now.
765 		 */
766 		ret = tb_switch_clx_disable(down_sw);
767 		if (ret < 0) {
768 			tb_sw_warn(down_sw, "failed to disable CL states\n");
769 			goto out_unlock;
770 		}
771 		clx = ret;
772 	}
773 
774 	if (margining->software) {
775 		tb_port_dbg(port, "running software %s lane margining for lanes %u\n",
776 			    margining->time ? "time" : "voltage", margining->lanes);
777 		ret = usb4_port_sw_margin(port, USB4_SB_TARGET_ROUTER, 0,
778 					  margining->lanes, margining->time,
779 					  margining->right_high,
780 					  USB4_MARGIN_SW_COUNTER_CLEAR);
781 		if (ret)
782 			goto out_clx;
783 
784 		ret = usb4_port_sw_margin_errors(port, USB4_SB_TARGET_ROUTER, 0,
785 						 &margining->results[0]);
786 	} else {
787 		tb_port_dbg(port, "running hardware %s lane margining for lanes %u\n",
788 			    margining->time ? "time" : "voltage", margining->lanes);
789 		/* Clear the results */
790 		margining->results[0] = 0;
791 		margining->results[1] = 0;
792 		ret = usb4_port_hw_margin(port, USB4_SB_TARGET_ROUTER, 0,
793 					  margining->lanes, margining->ber_level,
794 					  margining->time, margining->right_high,
795 					  margining->results);
796 	}
797 
798 out_clx:
799 	if (down_sw)
800 		tb_switch_clx_enable(down_sw, clx);
801 out_unlock:
802 	mutex_unlock(&tb->lock);
803 out_rpm_put:
804 	pm_runtime_mark_last_busy(&sw->dev);
805 	pm_runtime_put_autosuspend(&sw->dev);
806 
807 	return ret;
808 }
809 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
810 			 "%llu\n");
811 
812 static ssize_t margining_results_write(struct file *file,
813 				       const char __user *user_buf,
814 				       size_t count, loff_t *ppos)
815 {
816 	struct seq_file *s = file->private_data;
817 	struct tb_margining *margining = s->private;
818 	struct tb *tb = margining->port->sw->tb;
819 
820 	if (mutex_lock_interruptible(&tb->lock))
821 		return -ERESTARTSYS;
822 
823 	/* Just clear the results */
824 	margining->results[0] = 0;
825 	margining->results[1] = 0;
826 
827 	mutex_unlock(&tb->lock);
828 	return count;
829 }
830 
831 static void voltage_margin_show(struct seq_file *s,
832 				const struct tb_margining *margining, u8 val)
833 {
834 	unsigned int tmp, voltage;
835 
836 	tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
837 	voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
838 	seq_printf(s, "%u mV (%u)", voltage, tmp);
839 	if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
840 		seq_puts(s, " exceeds maximum");
841 	seq_puts(s, "\n");
842 }
843 
844 static void time_margin_show(struct seq_file *s,
845 			     const struct tb_margining *margining, u8 val)
846 {
847 	unsigned int tmp, interval;
848 
849 	tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
850 	interval = tmp * margining->max_time_offset / margining->time_steps;
851 	seq_printf(s, "%u mUI (%u)", interval, tmp);
852 	if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
853 		seq_puts(s, " exceeds maximum");
854 	seq_puts(s, "\n");
855 }
856 
857 static int margining_results_show(struct seq_file *s, void *not_used)
858 {
859 	struct tb_margining *margining = s->private;
860 	struct tb *tb = margining->port->sw->tb;
861 
862 	if (mutex_lock_interruptible(&tb->lock))
863 		return -ERESTARTSYS;
864 
865 	/* Dump the raw results first */
866 	seq_printf(s, "0x%08x\n", margining->results[0]);
867 	/* Only the hardware margining has two result dwords */
868 	if (!margining->software) {
869 		unsigned int val;
870 
871 		seq_printf(s, "0x%08x\n", margining->results[1]);
872 
873 		if (margining->time) {
874 			if (!margining->lanes || margining->lanes == 7) {
875 				val = margining->results[1];
876 				seq_puts(s, "# lane 0 right time margin: ");
877 				time_margin_show(s, margining, val);
878 				val = margining->results[1] >>
879 					USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
880 				seq_puts(s, "# lane 0 left time margin: ");
881 				time_margin_show(s, margining, val);
882 			}
883 			if (margining->lanes == 1 || margining->lanes == 7) {
884 				val = margining->results[1] >>
885 					USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
886 				seq_puts(s, "# lane 1 right time margin: ");
887 				time_margin_show(s, margining, val);
888 				val = margining->results[1] >>
889 					USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
890 				seq_puts(s, "# lane 1 left time margin: ");
891 				time_margin_show(s, margining, val);
892 			}
893 		} else {
894 			if (!margining->lanes || margining->lanes == 7) {
895 				val = margining->results[1];
896 				seq_puts(s, "# lane 0 high voltage margin: ");
897 				voltage_margin_show(s, margining, val);
898 				val = margining->results[1] >>
899 					USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
900 				seq_puts(s, "# lane 0 low voltage margin: ");
901 				voltage_margin_show(s, margining, val);
902 			}
903 			if (margining->lanes == 1 || margining->lanes == 7) {
904 				val = margining->results[1] >>
905 					USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
906 				seq_puts(s, "# lane 1 high voltage margin: ");
907 				voltage_margin_show(s, margining, val);
908 				val = margining->results[1] >>
909 					USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
910 				seq_puts(s, "# lane 1 low voltage margin: ");
911 				voltage_margin_show(s, margining, val);
912 			}
913 		}
914 	}
915 
916 	mutex_unlock(&tb->lock);
917 	return 0;
918 }
919 DEBUGFS_ATTR_RW(margining_results);
920 
921 static ssize_t margining_test_write(struct file *file,
922 				    const char __user *user_buf,
923 				    size_t count, loff_t *ppos)
924 {
925 	struct seq_file *s = file->private_data;
926 	struct tb_margining *margining = s->private;
927 	struct tb *tb = margining->port->sw->tb;
928 	int ret = 0;
929 	char *buf;
930 
931 	buf = validate_and_copy_from_user(user_buf, &count);
932 	if (IS_ERR(buf))
933 		return PTR_ERR(buf);
934 
935 	buf[count - 1] = '\0';
936 
937 	if (mutex_lock_interruptible(&tb->lock)) {
938 		ret = -ERESTARTSYS;
939 		goto out_free;
940 	}
941 
942 	if (!strcmp(buf, "time") && supports_time(margining))
943 		margining->time = true;
944 	else if (!strcmp(buf, "voltage"))
945 		margining->time = false;
946 	else
947 		ret = -EINVAL;
948 
949 	mutex_unlock(&tb->lock);
950 
951 out_free:
952 	free_page((unsigned long)buf);
953 	return ret ? ret : count;
954 }
955 
956 static int margining_test_show(struct seq_file *s, void *not_used)
957 {
958 	struct tb_margining *margining = s->private;
959 	struct tb *tb = margining->port->sw->tb;
960 
961 	if (mutex_lock_interruptible(&tb->lock))
962 		return -ERESTARTSYS;
963 
964 	if (supports_time(margining)) {
965 		if (margining->time)
966 			seq_puts(s, "voltage [time]\n");
967 		else
968 			seq_puts(s, "[voltage] time\n");
969 	} else {
970 		seq_puts(s, "[voltage]\n");
971 	}
972 
973 	mutex_unlock(&tb->lock);
974 	return 0;
975 }
976 DEBUGFS_ATTR_RW(margining_test);
977 
978 static ssize_t margining_margin_write(struct file *file,
979 				    const char __user *user_buf,
980 				    size_t count, loff_t *ppos)
981 {
982 	struct seq_file *s = file->private_data;
983 	struct tb_margining *margining = s->private;
984 	struct tb *tb = margining->port->sw->tb;
985 	int ret = 0;
986 	char *buf;
987 
988 	buf = validate_and_copy_from_user(user_buf, &count);
989 	if (IS_ERR(buf))
990 		return PTR_ERR(buf);
991 
992 	buf[count - 1] = '\0';
993 
994 	if (mutex_lock_interruptible(&tb->lock)) {
995 		ret = -ERESTARTSYS;
996 		goto out_free;
997 	}
998 
999 	if (margining->time) {
1000 		if (!strcmp(buf, "left"))
1001 			margining->right_high = false;
1002 		else if (!strcmp(buf, "right"))
1003 			margining->right_high = true;
1004 		else
1005 			ret = -EINVAL;
1006 	} else {
1007 		if (!strcmp(buf, "low"))
1008 			margining->right_high = false;
1009 		else if (!strcmp(buf, "high"))
1010 			margining->right_high = true;
1011 		else
1012 			ret = -EINVAL;
1013 	}
1014 
1015 	mutex_unlock(&tb->lock);
1016 
1017 out_free:
1018 	free_page((unsigned long)buf);
1019 	return ret ? ret : count;
1020 }
1021 
1022 static int margining_margin_show(struct seq_file *s, void *not_used)
1023 {
1024 	struct tb_margining *margining = s->private;
1025 	struct tb *tb = margining->port->sw->tb;
1026 
1027 	if (mutex_lock_interruptible(&tb->lock))
1028 		return -ERESTARTSYS;
1029 
1030 	if (margining->time) {
1031 		if (margining->right_high)
1032 			seq_puts(s, "left [right]\n");
1033 		else
1034 			seq_puts(s, "[left] right\n");
1035 	} else {
1036 		if (margining->right_high)
1037 			seq_puts(s, "low [high]\n");
1038 		else
1039 			seq_puts(s, "[low] high\n");
1040 	}
1041 
1042 	mutex_unlock(&tb->lock);
1043 	return 0;
1044 }
1045 DEBUGFS_ATTR_RW(margining_margin);
1046 
1047 static void margining_port_init(struct tb_port *port)
1048 {
1049 	struct tb_margining *margining;
1050 	struct dentry *dir, *parent;
1051 	struct usb4_port *usb4;
1052 	char dir_name[10];
1053 	unsigned int val;
1054 	int ret;
1055 
1056 	usb4 = port->usb4;
1057 	if (!usb4)
1058 		return;
1059 
1060 	snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1061 	parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1062 
1063 	margining = kzalloc(sizeof(*margining), GFP_KERNEL);
1064 	if (!margining)
1065 		return;
1066 
1067 	margining->port = port;
1068 
1069 	ret = usb4_port_margining_caps(port, USB4_SB_TARGET_ROUTER, 0,
1070 				       margining->caps);
1071 	if (ret) {
1072 		kfree(margining);
1073 		return;
1074 	}
1075 
1076 	/* Set the initial mode */
1077 	if (supports_software(margining))
1078 		margining->software = true;
1079 
1080 	val = (margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK) >>
1081 		USB4_MARGIN_CAP_0_VOLTAGE_STEPS_SHIFT;
1082 	margining->voltage_steps = val;
1083 	val = (margining->caps[0] & USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK) >>
1084 		USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_SHIFT;
1085 	margining->max_voltage_offset = 74 + val * 2;
1086 
1087 	if (supports_time(margining)) {
1088 		val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_STEPS_MASK) >>
1089 			USB4_MARGIN_CAP_1_TIME_STEPS_SHIFT;
1090 		margining->time_steps = val;
1091 		val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_OFFSET_MASK) >>
1092 			USB4_MARGIN_CAP_1_TIME_OFFSET_SHIFT;
1093 		/*
1094 		 * Store it as mUI (milli Unit Interval) because we want
1095 		 * to keep it as integer.
1096 		 */
1097 		margining->max_time_offset = 200 + 10 * val;
1098 	}
1099 
1100 	dir = debugfs_create_dir("margining", parent);
1101 	if (supports_hardware(margining)) {
1102 		val = (margining->caps[1] & USB4_MARGIN_CAP_1_MIN_BER_MASK) >>
1103 			USB4_MARGIN_CAP_1_MIN_BER_SHIFT;
1104 		margining->min_ber_level = val;
1105 		val = (margining->caps[1] & USB4_MARGIN_CAP_1_MAX_BER_MASK) >>
1106 			USB4_MARGIN_CAP_1_MAX_BER_SHIFT;
1107 		margining->max_ber_level = val;
1108 
1109 		/* Set the default to minimum */
1110 		margining->ber_level = margining->min_ber_level;
1111 
1112 		debugfs_create_file("ber_level_contour", 0400, dir, margining,
1113 				    &margining_ber_level_fops);
1114 	}
1115 	debugfs_create_file("caps", 0400, dir, margining, &margining_caps_fops);
1116 	debugfs_create_file("lanes", 0600, dir, margining, &margining_lanes_fops);
1117 	debugfs_create_file("mode", 0600, dir, margining, &margining_mode_fops);
1118 	debugfs_create_file("run", 0600, dir, margining, &margining_run_fops);
1119 	debugfs_create_file("results", 0600, dir, margining,
1120 			    &margining_results_fops);
1121 	debugfs_create_file("test", 0600, dir, margining, &margining_test_fops);
1122 	if (independent_voltage_margins(margining) == USB4_MARGIN_CAP_0_VOLTAGE_HL ||
1123 	    (supports_time(margining) &&
1124 	     independent_time_margins(margining) == USB4_MARGIN_CAP_1_TIME_LR))
1125 		debugfs_create_file("margin", 0600, dir, margining,
1126 				    &margining_margin_fops);
1127 
1128 	usb4->margining = margining;
1129 }
1130 
1131 static void margining_port_remove(struct tb_port *port)
1132 {
1133 	struct dentry *parent;
1134 	char dir_name[10];
1135 
1136 	if (!port->usb4)
1137 		return;
1138 
1139 	snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1140 	parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1141 	if (parent)
1142 		debugfs_lookup_and_remove("margining", parent);
1143 
1144 	kfree(port->usb4->margining);
1145 	port->usb4->margining = NULL;
1146 }
1147 
1148 static void margining_switch_init(struct tb_switch *sw)
1149 {
1150 	struct tb_port *upstream, *downstream;
1151 	struct tb_switch *parent_sw;
1152 	u64 route = tb_route(sw);
1153 
1154 	if (!route)
1155 		return;
1156 
1157 	upstream = tb_upstream_port(sw);
1158 	parent_sw = tb_switch_parent(sw);
1159 	downstream = tb_port_at(route, parent_sw);
1160 
1161 	margining_port_init(downstream);
1162 	margining_port_init(upstream);
1163 }
1164 
1165 static void margining_switch_remove(struct tb_switch *sw)
1166 {
1167 	struct tb_port *upstream, *downstream;
1168 	struct tb_switch *parent_sw;
1169 	u64 route = tb_route(sw);
1170 
1171 	if (!route)
1172 		return;
1173 
1174 	upstream = tb_upstream_port(sw);
1175 	parent_sw = tb_switch_parent(sw);
1176 	downstream = tb_port_at(route, parent_sw);
1177 
1178 	margining_port_remove(upstream);
1179 	margining_port_remove(downstream);
1180 }
1181 
1182 static void margining_xdomain_init(struct tb_xdomain *xd)
1183 {
1184 	struct tb_switch *parent_sw;
1185 	struct tb_port *downstream;
1186 
1187 	parent_sw = tb_xdomain_parent(xd);
1188 	downstream = tb_port_at(xd->route, parent_sw);
1189 
1190 	margining_port_init(downstream);
1191 }
1192 
1193 static void margining_xdomain_remove(struct tb_xdomain *xd)
1194 {
1195 	struct tb_switch *parent_sw;
1196 	struct tb_port *downstream;
1197 
1198 	parent_sw = tb_xdomain_parent(xd);
1199 	downstream = tb_port_at(xd->route, parent_sw);
1200 	margining_port_remove(downstream);
1201 }
1202 #else
1203 static inline void margining_switch_init(struct tb_switch *sw) { }
1204 static inline void margining_switch_remove(struct tb_switch *sw) { }
1205 static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1206 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1207 #endif
1208 
1209 static int port_clear_all_counters(struct tb_port *port)
1210 {
1211 	u32 *buf;
1212 	int ret;
1213 
1214 	buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1215 		      GFP_KERNEL);
1216 	if (!buf)
1217 		return -ENOMEM;
1218 
1219 	ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1220 			    COUNTER_SET_LEN * port->config.max_counters);
1221 	kfree(buf);
1222 
1223 	return ret;
1224 }
1225 
1226 static ssize_t counters_write(struct file *file, const char __user *user_buf,
1227 			      size_t count, loff_t *ppos)
1228 {
1229 	struct seq_file *s = file->private_data;
1230 	struct tb_port *port = s->private;
1231 	struct tb_switch *sw = port->sw;
1232 	struct tb *tb = port->sw->tb;
1233 	char *buf;
1234 	int ret;
1235 
1236 	buf = validate_and_copy_from_user(user_buf, &count);
1237 	if (IS_ERR(buf))
1238 		return PTR_ERR(buf);
1239 
1240 	pm_runtime_get_sync(&sw->dev);
1241 
1242 	if (mutex_lock_interruptible(&tb->lock)) {
1243 		ret = -ERESTARTSYS;
1244 		goto out;
1245 	}
1246 
1247 	/* If written delimiter only, clear all counters in one shot */
1248 	if (buf[0] == '\n') {
1249 		ret = port_clear_all_counters(port);
1250 	} else  {
1251 		char *line = buf;
1252 		u32 val, offset;
1253 
1254 		ret = -EINVAL;
1255 		while (parse_line(&line, &offset, &val, 1, 4)) {
1256 			ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1257 					    offset, 1);
1258 			if (ret)
1259 				break;
1260 		}
1261 	}
1262 
1263 	mutex_unlock(&tb->lock);
1264 
1265 out:
1266 	pm_runtime_mark_last_busy(&sw->dev);
1267 	pm_runtime_put_autosuspend(&sw->dev);
1268 	free_page((unsigned long)buf);
1269 
1270 	return ret < 0 ? ret : count;
1271 }
1272 
1273 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1274 			   struct tb_port *port, unsigned int cap,
1275 			   unsigned int offset, u8 cap_id, u8 vsec_id,
1276 			   int dwords)
1277 {
1278 	int i, ret;
1279 	u32 data;
1280 
1281 	for (i = 0; i < dwords; i++) {
1282 		if (port)
1283 			ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1284 		else
1285 			ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1286 		if (ret) {
1287 			seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1288 			continue;
1289 		}
1290 
1291 		seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1292 			   offset + i, cap_id, vsec_id, data);
1293 	}
1294 }
1295 
1296 static void cap_show(struct seq_file *s, struct tb_switch *sw,
1297 		     struct tb_port *port, unsigned int cap, u8 cap_id,
1298 		     u8 vsec_id, int length)
1299 {
1300 	int ret, offset = 0;
1301 
1302 	while (length > 0) {
1303 		int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1304 		u32 data[TB_MAX_CONFIG_RW_LENGTH];
1305 
1306 		if (port)
1307 			ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1308 					   dwords);
1309 		else
1310 			ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1311 		if (ret) {
1312 			cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1313 			return;
1314 		}
1315 
1316 		for (i = 0; i < dwords; i++) {
1317 			seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1318 				   cap + offset + i, offset + i,
1319 				   cap_id, vsec_id, data[i]);
1320 		}
1321 
1322 		length -= dwords;
1323 		offset += dwords;
1324 	}
1325 }
1326 
1327 static void port_cap_show(struct tb_port *port, struct seq_file *s,
1328 			  unsigned int cap)
1329 {
1330 	struct tb_cap_any header;
1331 	u8 vsec_id = 0;
1332 	size_t length;
1333 	int ret;
1334 
1335 	ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1336 	if (ret) {
1337 		seq_printf(s, "0x%04x <capability read failed>\n", cap);
1338 		return;
1339 	}
1340 
1341 	switch (header.basic.cap) {
1342 	case TB_PORT_CAP_PHY:
1343 		length = PORT_CAP_LANE_LEN;
1344 		break;
1345 
1346 	case TB_PORT_CAP_TIME1:
1347 		if (usb4_switch_version(port->sw) < 2)
1348 			length = PORT_CAP_TMU_V1_LEN;
1349 		else
1350 			length = PORT_CAP_TMU_V2_LEN;
1351 		break;
1352 
1353 	case TB_PORT_CAP_POWER:
1354 		length = PORT_CAP_POWER_LEN;
1355 		break;
1356 
1357 	case TB_PORT_CAP_ADAP:
1358 		if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
1359 			if (usb4_switch_version(port->sw) < 2)
1360 				length = PORT_CAP_V1_PCIE_LEN;
1361 			else
1362 				length = PORT_CAP_V2_PCIE_LEN;
1363 		} else if (tb_port_is_dpin(port)) {
1364 			if (usb4_switch_version(port->sw) < 2)
1365 				length = PORT_CAP_DP_V1_LEN;
1366 			else
1367 				length = PORT_CAP_DP_V2_LEN;
1368 		} else if (tb_port_is_dpout(port)) {
1369 			length = PORT_CAP_DP_V1_LEN;
1370 		} else if (tb_port_is_usb3_down(port) ||
1371 			   tb_port_is_usb3_up(port)) {
1372 			length = PORT_CAP_USB3_LEN;
1373 		} else {
1374 			seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1375 				   cap, header.basic.cap);
1376 			return;
1377 		}
1378 		break;
1379 
1380 	case TB_PORT_CAP_VSE:
1381 		if (!header.extended_short.length) {
1382 			ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
1383 					   cap + 1, 1);
1384 			if (ret) {
1385 				seq_printf(s, "0x%04x <capability read failed>\n",
1386 					   cap + 1);
1387 				return;
1388 			}
1389 			length = header.extended_long.length;
1390 			vsec_id = header.extended_short.vsec_id;
1391 		} else {
1392 			length = header.extended_short.length;
1393 			vsec_id = header.extended_short.vsec_id;
1394 		}
1395 		break;
1396 
1397 	case TB_PORT_CAP_USB4:
1398 		length = PORT_CAP_USB4_LEN;
1399 		break;
1400 
1401 	default:
1402 		seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1403 			   cap, header.basic.cap);
1404 		return;
1405 	}
1406 
1407 	cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
1408 }
1409 
1410 static void port_caps_show(struct tb_port *port, struct seq_file *s)
1411 {
1412 	int cap;
1413 
1414 	cap = tb_port_next_cap(port, 0);
1415 	while (cap > 0) {
1416 		port_cap_show(port, s, cap);
1417 		cap = tb_port_next_cap(port, cap);
1418 	}
1419 }
1420 
1421 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
1422 {
1423 	u32 data[PORT_CAP_BASIC_LEN];
1424 	int ret, i;
1425 
1426 	ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
1427 	if (ret)
1428 		return ret;
1429 
1430 	for (i = 0; i < ARRAY_SIZE(data); i++)
1431 		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1432 
1433 	return 0;
1434 }
1435 
1436 static int port_regs_show(struct seq_file *s, void *not_used)
1437 {
1438 	struct tb_port *port = s->private;
1439 	struct tb_switch *sw = port->sw;
1440 	struct tb *tb = sw->tb;
1441 	int ret;
1442 
1443 	pm_runtime_get_sync(&sw->dev);
1444 
1445 	if (mutex_lock_interruptible(&tb->lock)) {
1446 		ret = -ERESTARTSYS;
1447 		goto out_rpm_put;
1448 	}
1449 
1450 	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1451 
1452 	ret = port_basic_regs_show(port, s);
1453 	if (ret)
1454 		goto out_unlock;
1455 
1456 	port_caps_show(port, s);
1457 
1458 out_unlock:
1459 	mutex_unlock(&tb->lock);
1460 out_rpm_put:
1461 	pm_runtime_mark_last_busy(&sw->dev);
1462 	pm_runtime_put_autosuspend(&sw->dev);
1463 
1464 	return ret;
1465 }
1466 DEBUGFS_ATTR_RW(port_regs);
1467 
1468 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
1469 			    unsigned int cap)
1470 {
1471 	struct tb_cap_any header;
1472 	int ret, length;
1473 	u8 vsec_id = 0;
1474 
1475 	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
1476 	if (ret) {
1477 		seq_printf(s, "0x%04x <capability read failed>\n", cap);
1478 		return;
1479 	}
1480 
1481 	if (header.basic.cap == TB_SWITCH_CAP_VSE) {
1482 		if (!header.extended_short.length) {
1483 			ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
1484 					 cap + 1, 1);
1485 			if (ret) {
1486 				seq_printf(s, "0x%04x <capability read failed>\n",
1487 					   cap + 1);
1488 				return;
1489 			}
1490 			length = header.extended_long.length;
1491 		} else {
1492 			length = header.extended_short.length;
1493 		}
1494 		vsec_id = header.extended_short.vsec_id;
1495 	} else {
1496 		if (header.basic.cap == TB_SWITCH_CAP_TMU) {
1497 			length = SWITCH_CAP_TMU_LEN;
1498 		} else  {
1499 			seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
1500 				   cap, header.basic.cap);
1501 			return;
1502 		}
1503 	}
1504 
1505 	cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
1506 }
1507 
1508 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
1509 {
1510 	int cap;
1511 
1512 	cap = tb_switch_next_cap(sw, 0);
1513 	while (cap > 0) {
1514 		switch_cap_show(sw, s, cap);
1515 		cap = tb_switch_next_cap(sw, cap);
1516 	}
1517 }
1518 
1519 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
1520 {
1521 	u32 data[SWITCH_CAP_BASIC_LEN];
1522 	size_t dwords;
1523 	int ret, i;
1524 
1525 	/* Only USB4 has the additional registers */
1526 	if (tb_switch_is_usb4(sw))
1527 		dwords = ARRAY_SIZE(data);
1528 	else
1529 		dwords = 5;
1530 
1531 	ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
1532 	if (ret)
1533 		return ret;
1534 
1535 	for (i = 0; i < dwords; i++)
1536 		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1537 
1538 	return 0;
1539 }
1540 
1541 static int switch_regs_show(struct seq_file *s, void *not_used)
1542 {
1543 	struct tb_switch *sw = s->private;
1544 	struct tb *tb = sw->tb;
1545 	int ret;
1546 
1547 	pm_runtime_get_sync(&sw->dev);
1548 
1549 	if (mutex_lock_interruptible(&tb->lock)) {
1550 		ret = -ERESTARTSYS;
1551 		goto out_rpm_put;
1552 	}
1553 
1554 	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1555 
1556 	ret = switch_basic_regs_show(sw, s);
1557 	if (ret)
1558 		goto out_unlock;
1559 
1560 	switch_caps_show(sw, s);
1561 
1562 out_unlock:
1563 	mutex_unlock(&tb->lock);
1564 out_rpm_put:
1565 	pm_runtime_mark_last_busy(&sw->dev);
1566 	pm_runtime_put_autosuspend(&sw->dev);
1567 
1568 	return ret;
1569 }
1570 DEBUGFS_ATTR_RW(switch_regs);
1571 
1572 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
1573 {
1574 	u32 data[PATH_LEN];
1575 	int ret, i;
1576 
1577 	ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
1578 			   ARRAY_SIZE(data));
1579 	if (ret) {
1580 		seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
1581 		return ret;
1582 	}
1583 
1584 	for (i = 0; i < ARRAY_SIZE(data); i++) {
1585 		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1586 			   hopid * PATH_LEN + i, i, hopid, data[i]);
1587 	}
1588 
1589 	return 0;
1590 }
1591 
1592 static int path_show(struct seq_file *s, void *not_used)
1593 {
1594 	struct tb_port *port = s->private;
1595 	struct tb_switch *sw = port->sw;
1596 	struct tb *tb = sw->tb;
1597 	int start, i, ret = 0;
1598 
1599 	pm_runtime_get_sync(&sw->dev);
1600 
1601 	if (mutex_lock_interruptible(&tb->lock)) {
1602 		ret = -ERESTARTSYS;
1603 		goto out_rpm_put;
1604 	}
1605 
1606 	seq_puts(s, "# offset relative_offset in_hop_id value\n");
1607 
1608 	/* NHI and lane adapters have entry for path 0 */
1609 	if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
1610 		ret = path_show_one(port, s, 0);
1611 		if (ret)
1612 			goto out_unlock;
1613 	}
1614 
1615 	start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
1616 
1617 	for (i = start; i <= port->config.max_in_hop_id; i++) {
1618 		ret = path_show_one(port, s, i);
1619 		if (ret)
1620 			break;
1621 	}
1622 
1623 out_unlock:
1624 	mutex_unlock(&tb->lock);
1625 out_rpm_put:
1626 	pm_runtime_mark_last_busy(&sw->dev);
1627 	pm_runtime_put_autosuspend(&sw->dev);
1628 
1629 	return ret;
1630 }
1631 DEBUGFS_ATTR_RO(path);
1632 
1633 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
1634 				 int counter)
1635 {
1636 	u32 data[COUNTER_SET_LEN];
1637 	int ret, i;
1638 
1639 	ret = tb_port_read(port, data, TB_CFG_COUNTERS,
1640 			   counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
1641 	if (ret) {
1642 		seq_printf(s, "0x%04x <not accessible>\n",
1643 			   counter * COUNTER_SET_LEN);
1644 		return ret;
1645 	}
1646 
1647 	for (i = 0; i < ARRAY_SIZE(data); i++) {
1648 		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1649 			   counter * COUNTER_SET_LEN + i, i, counter, data[i]);
1650 	}
1651 
1652 	return 0;
1653 }
1654 
1655 static int counters_show(struct seq_file *s, void *not_used)
1656 {
1657 	struct tb_port *port = s->private;
1658 	struct tb_switch *sw = port->sw;
1659 	struct tb *tb = sw->tb;
1660 	int i, ret = 0;
1661 
1662 	pm_runtime_get_sync(&sw->dev);
1663 
1664 	if (mutex_lock_interruptible(&tb->lock)) {
1665 		ret = -ERESTARTSYS;
1666 		goto out;
1667 	}
1668 
1669 	seq_puts(s, "# offset relative_offset counter_id value\n");
1670 
1671 	for (i = 0; i < port->config.max_counters; i++) {
1672 		ret = counter_set_regs_show(port, s, i);
1673 		if (ret)
1674 			break;
1675 	}
1676 
1677 	mutex_unlock(&tb->lock);
1678 
1679 out:
1680 	pm_runtime_mark_last_busy(&sw->dev);
1681 	pm_runtime_put_autosuspend(&sw->dev);
1682 
1683 	return ret;
1684 }
1685 DEBUGFS_ATTR_RW(counters);
1686 
1687 static int sb_regs_show(struct tb_port *port, const struct sb_reg *sb_regs,
1688 			size_t size, enum usb4_sb_target target, u8 index,
1689 			struct seq_file *s)
1690 {
1691 	int ret, i;
1692 
1693 	seq_puts(s, "# register value\n");
1694 
1695 	for (i = 0; i < size; i++) {
1696 		const struct sb_reg *regs = &sb_regs[i];
1697 		u8 data[64];
1698 		int j;
1699 
1700 		memset(data, 0, sizeof(data));
1701 		ret = usb4_port_sb_read(port, target, index, regs->reg, data,
1702 					regs->size);
1703 		if (ret)
1704 			return ret;
1705 
1706 		seq_printf(s, "0x%02x", regs->reg);
1707 		for (j = 0; j < regs->size; j++)
1708 			seq_printf(s, " 0x%02x", data[j]);
1709 		seq_puts(s, "\n");
1710 	}
1711 
1712 	return 0;
1713 }
1714 
1715 static int port_sb_regs_show(struct seq_file *s, void *not_used)
1716 {
1717 	struct tb_port *port = s->private;
1718 	struct tb_switch *sw = port->sw;
1719 	struct tb *tb = sw->tb;
1720 	int ret;
1721 
1722 	pm_runtime_get_sync(&sw->dev);
1723 
1724 	if (mutex_lock_interruptible(&tb->lock)) {
1725 		ret = -ERESTARTSYS;
1726 		goto out_rpm_put;
1727 	}
1728 
1729 	ret = sb_regs_show(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
1730 			   USB4_SB_TARGET_ROUTER, 0, s);
1731 
1732 	mutex_unlock(&tb->lock);
1733 out_rpm_put:
1734 	pm_runtime_mark_last_busy(&sw->dev);
1735 	pm_runtime_put_autosuspend(&sw->dev);
1736 
1737 	return ret;
1738 }
1739 DEBUGFS_ATTR_RW(port_sb_regs);
1740 
1741 /**
1742  * tb_switch_debugfs_init() - Add debugfs entries for router
1743  * @sw: Pointer to the router
1744  *
1745  * Adds debugfs directories and files for given router.
1746  */
1747 void tb_switch_debugfs_init(struct tb_switch *sw)
1748 {
1749 	struct dentry *debugfs_dir;
1750 	struct tb_port *port;
1751 
1752 	debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
1753 	sw->debugfs_dir = debugfs_dir;
1754 	debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
1755 			    &switch_regs_fops);
1756 
1757 	tb_switch_for_each_port(sw, port) {
1758 		struct dentry *debugfs_dir;
1759 		char dir_name[10];
1760 
1761 		if (port->disabled)
1762 			continue;
1763 		if (port->config.type == TB_TYPE_INACTIVE)
1764 			continue;
1765 
1766 		snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1767 		debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
1768 		debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
1769 				    port, &port_regs_fops);
1770 		debugfs_create_file("path", 0400, debugfs_dir, port,
1771 				    &path_fops);
1772 		if (port->config.counters_support)
1773 			debugfs_create_file("counters", 0600, debugfs_dir, port,
1774 					    &counters_fops);
1775 		if (port->usb4)
1776 			debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir,
1777 					    port, &port_sb_regs_fops);
1778 	}
1779 
1780 	margining_switch_init(sw);
1781 }
1782 
1783 /**
1784  * tb_switch_debugfs_remove() - Remove all router debugfs entries
1785  * @sw: Pointer to the router
1786  *
1787  * Removes all previously added debugfs entries under this router.
1788  */
1789 void tb_switch_debugfs_remove(struct tb_switch *sw)
1790 {
1791 	margining_switch_remove(sw);
1792 	debugfs_remove_recursive(sw->debugfs_dir);
1793 }
1794 
1795 void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
1796 {
1797 	margining_xdomain_init(xd);
1798 }
1799 
1800 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
1801 {
1802 	margining_xdomain_remove(xd);
1803 }
1804 
1805 /**
1806  * tb_service_debugfs_init() - Add debugfs directory for service
1807  * @svc: Thunderbolt service pointer
1808  *
1809  * Adds debugfs directory for service.
1810  */
1811 void tb_service_debugfs_init(struct tb_service *svc)
1812 {
1813 	svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
1814 					      tb_debugfs_root);
1815 }
1816 
1817 /**
1818  * tb_service_debugfs_remove() - Remove service debugfs directory
1819  * @svc: Thunderbolt service pointer
1820  *
1821  * Removes the previously created debugfs directory for @svc.
1822  */
1823 void tb_service_debugfs_remove(struct tb_service *svc)
1824 {
1825 	debugfs_remove_recursive(svc->debugfs_dir);
1826 	svc->debugfs_dir = NULL;
1827 }
1828 
1829 static int retimer_sb_regs_show(struct seq_file *s, void *not_used)
1830 {
1831 	struct tb_retimer *rt = s->private;
1832 	struct tb *tb = rt->tb;
1833 	int ret;
1834 
1835 	pm_runtime_get_sync(&rt->dev);
1836 
1837 	if (mutex_lock_interruptible(&tb->lock)) {
1838 		ret = -ERESTARTSYS;
1839 		goto out_rpm_put;
1840 	}
1841 
1842 	ret = sb_regs_show(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
1843 			   USB4_SB_TARGET_RETIMER, rt->index, s);
1844 
1845 	mutex_unlock(&tb->lock);
1846 out_rpm_put:
1847 	pm_runtime_mark_last_busy(&rt->dev);
1848 	pm_runtime_put_autosuspend(&rt->dev);
1849 
1850 	return ret;
1851 }
1852 DEBUGFS_ATTR_RW(retimer_sb_regs);
1853 
1854 /**
1855  * tb_retimer_debugfs_init() - Add debugfs directory for retimer
1856  * @rt: Pointer to retimer structure
1857  *
1858  * Adds and populates retimer debugfs directory.
1859  */
1860 void tb_retimer_debugfs_init(struct tb_retimer *rt)
1861 {
1862 	struct dentry *debugfs_dir;
1863 
1864 	debugfs_dir = debugfs_create_dir(dev_name(&rt->dev), tb_debugfs_root);
1865 	debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, rt,
1866 			    &retimer_sb_regs_fops);
1867 }
1868 
1869 /**
1870  * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
1871  * @rt: Pointer to retimer structure
1872  *
1873  * Removes the retimer debugfs directory along with its contents.
1874  */
1875 void tb_retimer_debugfs_remove(struct tb_retimer *rt)
1876 {
1877 	debugfs_lookup_and_remove(dev_name(&rt->dev), tb_debugfs_root);
1878 }
1879 
1880 void tb_debugfs_init(void)
1881 {
1882 	tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
1883 }
1884 
1885 void tb_debugfs_exit(void)
1886 {
1887 	debugfs_remove_recursive(tb_debugfs_root);
1888 }
1889