xref: /openbmc/linux/drivers/thunderbolt/debugfs.c (revision 0d7ddfc8)
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)					\
37 static int __space ## _open(struct inode *inode, struct file *file)	\
38 {									\
39 	return single_open(file, __space ## _show, inode->i_private);	\
40 }									\
41 									\
42 static 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 
57 static struct dentry *tb_debugfs_root;
58 
59 static 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 
85 static 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)
127 static 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 
162 out:
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 
170 static 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 
179 static 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  */
212 struct 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 
228 static bool supports_software(const struct usb4_port *usb4)
229 {
230 	return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
231 }
232 
233 static bool supports_hardware(const struct usb4_port *usb4)
234 {
235 	return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
236 }
237 
238 static bool both_lanes(const struct usb4_port *usb4)
239 {
240 	return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
241 }
242 
243 static 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 
249 static 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 */
255 static 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 
261 static ssize_t
262 margining_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 
301 out_free:
302 	free_page((unsigned long)buf);
303 out_unlock:
304 	mutex_unlock(&tb->lock);
305 
306 	return ret < 0 ? ret : count;
307 }
308 
309 static 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 
317 static 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 }
327 DEBUGFS_ATTR_RW(margining_ber_level);
328 
329 static 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 }
404 DEBUGFS_ATTR_RO(margining_caps);
405 
406 static ssize_t
407 margining_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 
444 out_free:
445 	free_page((unsigned long)buf);
446 	return ret < 0 ? ret : count;
447 }
448 
449 static 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 }
477 DEBUGFS_ATTR_RW(margining_lanes);
478 
479 static 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 
517 out_free:
518 	free_page((unsigned long)buf);
519 	return ret ? ret : count;
520 }
521 
522 static 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 }
551 DEBUGFS_ATTR_RW(margining_mode);
552 
553 static 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 
616 out_clx:
617 	if (down_sw)
618 		tb_switch_clx_enable(down_sw, clx);
619 out_unlock:
620 	mutex_unlock(&tb->lock);
621 out_rpm_put:
622 	pm_runtime_mark_last_busy(&sw->dev);
623 	pm_runtime_put_autosuspend(&sw->dev);
624 
625 	return ret;
626 }
627 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
628 			 "%llu\n");
629 
630 static 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 
650 static 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 
663 static 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 
676 static 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 }
741 DEBUGFS_ATTR_RW(margining_results);
742 
743 static 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 
774 out_free:
775 	free_page((unsigned long)buf);
776 	return ret ? ret : count;
777 }
778 
779 static 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 }
800 DEBUGFS_ATTR_RW(margining_test);
801 
802 static 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 
842 out_free:
843 	free_page((unsigned long)buf);
844 	return ret ? ret : count;
845 }
846 
847 static 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 }
871 DEBUGFS_ATTR_RW(margining_margin);
872 
873 static 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) == USB4_MARGIN_CAP_0_VOLTAGE_HL ||
947 	    (supports_time(usb4) &&
948 	     independent_time_margins(usb4) == USB4_MARGIN_CAP_1_TIME_LR))
949 		debugfs_create_file("margin", 0600, dir, port, &margining_margin_fops);
950 }
951 
952 static void margining_port_remove(struct tb_port *port)
953 {
954 	struct dentry *parent;
955 	char dir_name[10];
956 
957 	if (!port->usb4)
958 		return;
959 
960 	snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
961 	parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
962 	if (parent)
963 		debugfs_lookup_and_remove("margining", parent);
964 
965 	kfree(port->usb4->margining);
966 	port->usb4->margining = NULL;
967 }
968 
969 static void margining_switch_init(struct tb_switch *sw)
970 {
971 	struct tb_port *upstream, *downstream;
972 	struct tb_switch *parent_sw;
973 	u64 route = tb_route(sw);
974 
975 	if (!route)
976 		return;
977 
978 	upstream = tb_upstream_port(sw);
979 	parent_sw = tb_switch_parent(sw);
980 	downstream = tb_port_at(route, parent_sw);
981 
982 	margining_port_init(downstream);
983 	margining_port_init(upstream);
984 }
985 
986 static void margining_switch_remove(struct tb_switch *sw)
987 {
988 	struct tb_port *upstream, *downstream;
989 	struct tb_switch *parent_sw;
990 	u64 route = tb_route(sw);
991 
992 	if (!route)
993 		return;
994 
995 	upstream = tb_upstream_port(sw);
996 	parent_sw = tb_switch_parent(sw);
997 	downstream = tb_port_at(route, parent_sw);
998 
999 	margining_port_remove(upstream);
1000 	margining_port_remove(downstream);
1001 }
1002 
1003 static void margining_xdomain_init(struct tb_xdomain *xd)
1004 {
1005 	struct tb_switch *parent_sw;
1006 	struct tb_port *downstream;
1007 
1008 	parent_sw = tb_xdomain_parent(xd);
1009 	downstream = tb_port_at(xd->route, parent_sw);
1010 
1011 	margining_port_init(downstream);
1012 }
1013 
1014 static void margining_xdomain_remove(struct tb_xdomain *xd)
1015 {
1016 	struct tb_switch *parent_sw;
1017 	struct tb_port *downstream;
1018 
1019 	parent_sw = tb_xdomain_parent(xd);
1020 	downstream = tb_port_at(xd->route, parent_sw);
1021 	margining_port_remove(downstream);
1022 }
1023 #else
1024 static inline void margining_switch_init(struct tb_switch *sw) { }
1025 static inline void margining_switch_remove(struct tb_switch *sw) { }
1026 static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1027 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1028 #endif
1029 
1030 static int port_clear_all_counters(struct tb_port *port)
1031 {
1032 	u32 *buf;
1033 	int ret;
1034 
1035 	buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1036 		      GFP_KERNEL);
1037 	if (!buf)
1038 		return -ENOMEM;
1039 
1040 	ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1041 			    COUNTER_SET_LEN * port->config.max_counters);
1042 	kfree(buf);
1043 
1044 	return ret;
1045 }
1046 
1047 static ssize_t counters_write(struct file *file, const char __user *user_buf,
1048 			      size_t count, loff_t *ppos)
1049 {
1050 	struct seq_file *s = file->private_data;
1051 	struct tb_port *port = s->private;
1052 	struct tb_switch *sw = port->sw;
1053 	struct tb *tb = port->sw->tb;
1054 	char *buf;
1055 	int ret;
1056 
1057 	buf = validate_and_copy_from_user(user_buf, &count);
1058 	if (IS_ERR(buf))
1059 		return PTR_ERR(buf);
1060 
1061 	pm_runtime_get_sync(&sw->dev);
1062 
1063 	if (mutex_lock_interruptible(&tb->lock)) {
1064 		ret = -ERESTARTSYS;
1065 		goto out;
1066 	}
1067 
1068 	/* If written delimiter only, clear all counters in one shot */
1069 	if (buf[0] == '\n') {
1070 		ret = port_clear_all_counters(port);
1071 	} else  {
1072 		char *line = buf;
1073 		u32 val, offset;
1074 
1075 		ret = -EINVAL;
1076 		while (parse_line(&line, &offset, &val, 1, 4)) {
1077 			ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1078 					    offset, 1);
1079 			if (ret)
1080 				break;
1081 		}
1082 	}
1083 
1084 	mutex_unlock(&tb->lock);
1085 
1086 out:
1087 	pm_runtime_mark_last_busy(&sw->dev);
1088 	pm_runtime_put_autosuspend(&sw->dev);
1089 	free_page((unsigned long)buf);
1090 
1091 	return ret < 0 ? ret : count;
1092 }
1093 
1094 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1095 			   struct tb_port *port, unsigned int cap,
1096 			   unsigned int offset, u8 cap_id, u8 vsec_id,
1097 			   int dwords)
1098 {
1099 	int i, ret;
1100 	u32 data;
1101 
1102 	for (i = 0; i < dwords; i++) {
1103 		if (port)
1104 			ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1105 		else
1106 			ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1107 		if (ret) {
1108 			seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1109 			continue;
1110 		}
1111 
1112 		seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1113 			   offset + i, cap_id, vsec_id, data);
1114 	}
1115 }
1116 
1117 static void cap_show(struct seq_file *s, struct tb_switch *sw,
1118 		     struct tb_port *port, unsigned int cap, u8 cap_id,
1119 		     u8 vsec_id, int length)
1120 {
1121 	int ret, offset = 0;
1122 
1123 	while (length > 0) {
1124 		int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1125 		u32 data[TB_MAX_CONFIG_RW_LENGTH];
1126 
1127 		if (port)
1128 			ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1129 					   dwords);
1130 		else
1131 			ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1132 		if (ret) {
1133 			cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1134 			return;
1135 		}
1136 
1137 		for (i = 0; i < dwords; i++) {
1138 			seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1139 				   cap + offset + i, offset + i,
1140 				   cap_id, vsec_id, data[i]);
1141 		}
1142 
1143 		length -= dwords;
1144 		offset += dwords;
1145 	}
1146 }
1147 
1148 static void port_cap_show(struct tb_port *port, struct seq_file *s,
1149 			  unsigned int cap)
1150 {
1151 	struct tb_cap_any header;
1152 	u8 vsec_id = 0;
1153 	size_t length;
1154 	int ret;
1155 
1156 	ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1157 	if (ret) {
1158 		seq_printf(s, "0x%04x <capability read failed>\n", cap);
1159 		return;
1160 	}
1161 
1162 	switch (header.basic.cap) {
1163 	case TB_PORT_CAP_PHY:
1164 		length = PORT_CAP_LANE_LEN;
1165 		break;
1166 
1167 	case TB_PORT_CAP_TIME1:
1168 		if (usb4_switch_version(port->sw) < 2)
1169 			length = PORT_CAP_TMU_V1_LEN;
1170 		else
1171 			length = PORT_CAP_TMU_V2_LEN;
1172 		break;
1173 
1174 	case TB_PORT_CAP_POWER:
1175 		length = PORT_CAP_POWER_LEN;
1176 		break;
1177 
1178 	case TB_PORT_CAP_ADAP:
1179 		if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
1180 			if (usb4_switch_version(port->sw) < 2)
1181 				length = PORT_CAP_V1_PCIE_LEN;
1182 			else
1183 				length = PORT_CAP_V2_PCIE_LEN;
1184 		} else if (tb_port_is_dpin(port)) {
1185 			if (usb4_switch_version(port->sw) < 2)
1186 				length = PORT_CAP_DP_V1_LEN;
1187 			else
1188 				length = PORT_CAP_DP_V2_LEN;
1189 		} else if (tb_port_is_dpout(port)) {
1190 			length = PORT_CAP_DP_V1_LEN;
1191 		} else if (tb_port_is_usb3_down(port) ||
1192 			   tb_port_is_usb3_up(port)) {
1193 			length = PORT_CAP_USB3_LEN;
1194 		} else {
1195 			seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1196 				   cap, header.basic.cap);
1197 			return;
1198 		}
1199 		break;
1200 
1201 	case TB_PORT_CAP_VSE:
1202 		if (!header.extended_short.length) {
1203 			ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
1204 					   cap + 1, 1);
1205 			if (ret) {
1206 				seq_printf(s, "0x%04x <capability read failed>\n",
1207 					   cap + 1);
1208 				return;
1209 			}
1210 			length = header.extended_long.length;
1211 			vsec_id = header.extended_short.vsec_id;
1212 		} else {
1213 			length = header.extended_short.length;
1214 			vsec_id = header.extended_short.vsec_id;
1215 		}
1216 		break;
1217 
1218 	case TB_PORT_CAP_USB4:
1219 		length = PORT_CAP_USB4_LEN;
1220 		break;
1221 
1222 	default:
1223 		seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1224 			   cap, header.basic.cap);
1225 		return;
1226 	}
1227 
1228 	cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
1229 }
1230 
1231 static void port_caps_show(struct tb_port *port, struct seq_file *s)
1232 {
1233 	int cap;
1234 
1235 	cap = tb_port_next_cap(port, 0);
1236 	while (cap > 0) {
1237 		port_cap_show(port, s, cap);
1238 		cap = tb_port_next_cap(port, cap);
1239 	}
1240 }
1241 
1242 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
1243 {
1244 	u32 data[PORT_CAP_BASIC_LEN];
1245 	int ret, i;
1246 
1247 	ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
1248 	if (ret)
1249 		return ret;
1250 
1251 	for (i = 0; i < ARRAY_SIZE(data); i++)
1252 		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1253 
1254 	return 0;
1255 }
1256 
1257 static int port_regs_show(struct seq_file *s, void *not_used)
1258 {
1259 	struct tb_port *port = s->private;
1260 	struct tb_switch *sw = port->sw;
1261 	struct tb *tb = sw->tb;
1262 	int ret;
1263 
1264 	pm_runtime_get_sync(&sw->dev);
1265 
1266 	if (mutex_lock_interruptible(&tb->lock)) {
1267 		ret = -ERESTARTSYS;
1268 		goto out_rpm_put;
1269 	}
1270 
1271 	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1272 
1273 	ret = port_basic_regs_show(port, s);
1274 	if (ret)
1275 		goto out_unlock;
1276 
1277 	port_caps_show(port, s);
1278 
1279 out_unlock:
1280 	mutex_unlock(&tb->lock);
1281 out_rpm_put:
1282 	pm_runtime_mark_last_busy(&sw->dev);
1283 	pm_runtime_put_autosuspend(&sw->dev);
1284 
1285 	return ret;
1286 }
1287 DEBUGFS_ATTR_RW(port_regs);
1288 
1289 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
1290 			    unsigned int cap)
1291 {
1292 	struct tb_cap_any header;
1293 	int ret, length;
1294 	u8 vsec_id = 0;
1295 
1296 	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
1297 	if (ret) {
1298 		seq_printf(s, "0x%04x <capability read failed>\n", cap);
1299 		return;
1300 	}
1301 
1302 	if (header.basic.cap == TB_SWITCH_CAP_VSE) {
1303 		if (!header.extended_short.length) {
1304 			ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
1305 					 cap + 1, 1);
1306 			if (ret) {
1307 				seq_printf(s, "0x%04x <capability read failed>\n",
1308 					   cap + 1);
1309 				return;
1310 			}
1311 			length = header.extended_long.length;
1312 		} else {
1313 			length = header.extended_short.length;
1314 		}
1315 		vsec_id = header.extended_short.vsec_id;
1316 	} else {
1317 		if (header.basic.cap == TB_SWITCH_CAP_TMU) {
1318 			length = SWITCH_CAP_TMU_LEN;
1319 		} else  {
1320 			seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
1321 				   cap, header.basic.cap);
1322 			return;
1323 		}
1324 	}
1325 
1326 	cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
1327 }
1328 
1329 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
1330 {
1331 	int cap;
1332 
1333 	cap = tb_switch_next_cap(sw, 0);
1334 	while (cap > 0) {
1335 		switch_cap_show(sw, s, cap);
1336 		cap = tb_switch_next_cap(sw, cap);
1337 	}
1338 }
1339 
1340 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
1341 {
1342 	u32 data[SWITCH_CAP_BASIC_LEN];
1343 	size_t dwords;
1344 	int ret, i;
1345 
1346 	/* Only USB4 has the additional registers */
1347 	if (tb_switch_is_usb4(sw))
1348 		dwords = ARRAY_SIZE(data);
1349 	else
1350 		dwords = 7;
1351 
1352 	ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
1353 	if (ret)
1354 		return ret;
1355 
1356 	for (i = 0; i < dwords; i++)
1357 		seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1358 
1359 	return 0;
1360 }
1361 
1362 static int switch_regs_show(struct seq_file *s, void *not_used)
1363 {
1364 	struct tb_switch *sw = s->private;
1365 	struct tb *tb = sw->tb;
1366 	int ret;
1367 
1368 	pm_runtime_get_sync(&sw->dev);
1369 
1370 	if (mutex_lock_interruptible(&tb->lock)) {
1371 		ret = -ERESTARTSYS;
1372 		goto out_rpm_put;
1373 	}
1374 
1375 	seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1376 
1377 	ret = switch_basic_regs_show(sw, s);
1378 	if (ret)
1379 		goto out_unlock;
1380 
1381 	switch_caps_show(sw, s);
1382 
1383 out_unlock:
1384 	mutex_unlock(&tb->lock);
1385 out_rpm_put:
1386 	pm_runtime_mark_last_busy(&sw->dev);
1387 	pm_runtime_put_autosuspend(&sw->dev);
1388 
1389 	return ret;
1390 }
1391 DEBUGFS_ATTR_RW(switch_regs);
1392 
1393 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
1394 {
1395 	u32 data[PATH_LEN];
1396 	int ret, i;
1397 
1398 	ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
1399 			   ARRAY_SIZE(data));
1400 	if (ret) {
1401 		seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
1402 		return ret;
1403 	}
1404 
1405 	for (i = 0; i < ARRAY_SIZE(data); i++) {
1406 		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1407 			   hopid * PATH_LEN + i, i, hopid, data[i]);
1408 	}
1409 
1410 	return 0;
1411 }
1412 
1413 static int path_show(struct seq_file *s, void *not_used)
1414 {
1415 	struct tb_port *port = s->private;
1416 	struct tb_switch *sw = port->sw;
1417 	struct tb *tb = sw->tb;
1418 	int start, i, ret = 0;
1419 
1420 	pm_runtime_get_sync(&sw->dev);
1421 
1422 	if (mutex_lock_interruptible(&tb->lock)) {
1423 		ret = -ERESTARTSYS;
1424 		goto out_rpm_put;
1425 	}
1426 
1427 	seq_puts(s, "# offset relative_offset in_hop_id value\n");
1428 
1429 	/* NHI and lane adapters have entry for path 0 */
1430 	if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
1431 		ret = path_show_one(port, s, 0);
1432 		if (ret)
1433 			goto out_unlock;
1434 	}
1435 
1436 	start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
1437 
1438 	for (i = start; i <= port->config.max_in_hop_id; i++) {
1439 		ret = path_show_one(port, s, i);
1440 		if (ret)
1441 			break;
1442 	}
1443 
1444 out_unlock:
1445 	mutex_unlock(&tb->lock);
1446 out_rpm_put:
1447 	pm_runtime_mark_last_busy(&sw->dev);
1448 	pm_runtime_put_autosuspend(&sw->dev);
1449 
1450 	return ret;
1451 }
1452 DEBUGFS_ATTR_RO(path);
1453 
1454 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
1455 				 int counter)
1456 {
1457 	u32 data[COUNTER_SET_LEN];
1458 	int ret, i;
1459 
1460 	ret = tb_port_read(port, data, TB_CFG_COUNTERS,
1461 			   counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
1462 	if (ret) {
1463 		seq_printf(s, "0x%04x <not accessible>\n",
1464 			   counter * COUNTER_SET_LEN);
1465 		return ret;
1466 	}
1467 
1468 	for (i = 0; i < ARRAY_SIZE(data); i++) {
1469 		seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1470 			   counter * COUNTER_SET_LEN + i, i, counter, data[i]);
1471 	}
1472 
1473 	return 0;
1474 }
1475 
1476 static int counters_show(struct seq_file *s, void *not_used)
1477 {
1478 	struct tb_port *port = s->private;
1479 	struct tb_switch *sw = port->sw;
1480 	struct tb *tb = sw->tb;
1481 	int i, ret = 0;
1482 
1483 	pm_runtime_get_sync(&sw->dev);
1484 
1485 	if (mutex_lock_interruptible(&tb->lock)) {
1486 		ret = -ERESTARTSYS;
1487 		goto out;
1488 	}
1489 
1490 	seq_puts(s, "# offset relative_offset counter_id value\n");
1491 
1492 	for (i = 0; i < port->config.max_counters; i++) {
1493 		ret = counter_set_regs_show(port, s, i);
1494 		if (ret)
1495 			break;
1496 	}
1497 
1498 	mutex_unlock(&tb->lock);
1499 
1500 out:
1501 	pm_runtime_mark_last_busy(&sw->dev);
1502 	pm_runtime_put_autosuspend(&sw->dev);
1503 
1504 	return ret;
1505 }
1506 DEBUGFS_ATTR_RW(counters);
1507 
1508 /**
1509  * tb_switch_debugfs_init() - Add debugfs entries for router
1510  * @sw: Pointer to the router
1511  *
1512  * Adds debugfs directories and files for given router.
1513  */
1514 void tb_switch_debugfs_init(struct tb_switch *sw)
1515 {
1516 	struct dentry *debugfs_dir;
1517 	struct tb_port *port;
1518 
1519 	debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
1520 	sw->debugfs_dir = debugfs_dir;
1521 	debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
1522 			    &switch_regs_fops);
1523 
1524 	tb_switch_for_each_port(sw, port) {
1525 		struct dentry *debugfs_dir;
1526 		char dir_name[10];
1527 
1528 		if (port->disabled)
1529 			continue;
1530 		if (port->config.type == TB_TYPE_INACTIVE)
1531 			continue;
1532 
1533 		snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1534 		debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
1535 		debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
1536 				    port, &port_regs_fops);
1537 		debugfs_create_file("path", 0400, debugfs_dir, port,
1538 				    &path_fops);
1539 		if (port->config.counters_support)
1540 			debugfs_create_file("counters", 0600, debugfs_dir, port,
1541 					    &counters_fops);
1542 	}
1543 
1544 	margining_switch_init(sw);
1545 }
1546 
1547 /**
1548  * tb_switch_debugfs_remove() - Remove all router debugfs entries
1549  * @sw: Pointer to the router
1550  *
1551  * Removes all previously added debugfs entries under this router.
1552  */
1553 void tb_switch_debugfs_remove(struct tb_switch *sw)
1554 {
1555 	margining_switch_remove(sw);
1556 	debugfs_remove_recursive(sw->debugfs_dir);
1557 }
1558 
1559 void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
1560 {
1561 	margining_xdomain_init(xd);
1562 }
1563 
1564 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
1565 {
1566 	margining_xdomain_remove(xd);
1567 }
1568 
1569 /**
1570  * tb_service_debugfs_init() - Add debugfs directory for service
1571  * @svc: Thunderbolt service pointer
1572  *
1573  * Adds debugfs directory for service.
1574  */
1575 void tb_service_debugfs_init(struct tb_service *svc)
1576 {
1577 	svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
1578 					      tb_debugfs_root);
1579 }
1580 
1581 /**
1582  * tb_service_debugfs_remove() - Remove service debugfs directory
1583  * @svc: Thunderbolt service pointer
1584  *
1585  * Removes the previously created debugfs directory for @svc.
1586  */
1587 void tb_service_debugfs_remove(struct tb_service *svc)
1588 {
1589 	debugfs_remove_recursive(svc->debugfs_dir);
1590 	svc->debugfs_dir = NULL;
1591 }
1592 
1593 void tb_debugfs_init(void)
1594 {
1595 	tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
1596 }
1597 
1598 void tb_debugfs_exit(void)
1599 {
1600 	debugfs_remove_recursive(tb_debugfs_root);
1601 }
1602