1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2020, Intel Corporation
10 #include <linux/bitfield.h>
11 #include <linux/debugfs.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/uaccess.h>
18 #define PORT_CAP_V1_PCIE_LEN 1
19 #define PORT_CAP_V2_PCIE_LEN 2
20 #define PORT_CAP_POWER_LEN 2
21 #define PORT_CAP_LANE_LEN 3
22 #define PORT_CAP_USB3_LEN 5
23 #define PORT_CAP_DP_V1_LEN 9
24 #define PORT_CAP_DP_V2_LEN 14
25 #define PORT_CAP_TMU_V1_LEN 8
26 #define PORT_CAP_TMU_V2_LEN 10
27 #define PORT_CAP_BASIC_LEN 9
28 #define PORT_CAP_USB4_LEN 20
30 #define SWITCH_CAP_TMU_LEN 26
31 #define SWITCH_CAP_BASIC_LEN 27
35 #define COUNTER_SET_LEN 3
37 /* Sideband registers and their sizes as defined in the USB4 spec */
43 #define SB_MAX_SIZE 64
45 /* Sideband registers for router */
46 static const struct sb_reg port_sb_regs[] = {
47 { USB4_SB_VENDOR_ID, 4 },
48 { USB4_SB_PRODUCT_ID, 4 },
49 { USB4_SB_DEBUG_CONF, 4 },
50 { USB4_SB_DEBUG, 54 },
51 { USB4_SB_LRD_TUNING, 4 },
52 { USB4_SB_OPCODE, 4 },
53 { USB4_SB_METADATA, 4 },
54 { USB4_SB_LINK_CONF, 3 },
55 { USB4_SB_GEN23_TXFFE, 4 },
56 { USB4_SB_GEN4_TXFFE, 4 },
57 { USB4_SB_VERSION, 4 },
61 /* Sideband registers for retimer */
62 static const struct sb_reg retimer_sb_regs[] = {
63 { USB4_SB_VENDOR_ID, 4 },
64 { USB4_SB_PRODUCT_ID, 4 },
65 { USB4_SB_FW_VERSION, 4 },
66 { USB4_SB_LRD_TUNING, 4 },
67 { USB4_SB_OPCODE, 4 },
68 { USB4_SB_METADATA, 4 },
69 { USB4_SB_GEN23_TXFFE, 4 },
70 { USB4_SB_GEN4_TXFFE, 4 },
71 { USB4_SB_VERSION, 4 },
75 #define DEBUGFS_ATTR(__space, __write) \
76 static int __space ## _open(struct inode *inode, struct file *file) \
78 return single_open(file, __space ## _show, inode->i_private); \
81 static const struct file_operations __space ## _fops = { \
82 .owner = THIS_MODULE, \
83 .open = __space ## _open, \
84 .release = single_release, \
87 .llseek = seq_lseek, \
90 #define DEBUGFS_ATTR_RO(__space) \
91 DEBUGFS_ATTR(__space, NULL)
93 #define DEBUGFS_ATTR_RW(__space) \
94 DEBUGFS_ATTR(__space, __space ## _write)
96 static struct dentry *tb_debugfs_root;
98 static void *validate_and_copy_from_user(const void __user *user_buf,
105 return ERR_PTR(-EINVAL);
107 if (!access_ok(user_buf, *count))
108 return ERR_PTR(-EFAULT);
110 buf = (void *)get_zeroed_page(GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
114 nbytes = min_t(size_t, *count, PAGE_SIZE);
115 if (copy_from_user(buf, user_buf, nbytes)) {
116 free_page((unsigned long)buf);
117 return ERR_PTR(-EFAULT);
124 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
131 token = strsep(line, "\n");
136 * For Adapter/Router configuration space:
137 * Short format is: offset value\n
139 * Long format as produced from the read side:
140 * offset relative_offset cap_id vs_cap_id value\n
141 * v[0] v[1] v[2] v[3] v[4]
143 * For Counter configuration space:
144 * Short format is: offset\n
146 * Long format as produced from the read side:
147 * offset relative_offset counter_id value\n
148 * v[0] v[1] v[2] v[3]
150 ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
151 /* In case of Counters, clear counter, "val" content is NA */
152 if (ret == short_fmt_len) {
154 *val = v[short_fmt_len - 1];
156 } else if (ret == long_fmt_len) {
158 *val = v[long_fmt_len - 1];
165 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
166 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
167 const char __user *user_buf, size_t count,
170 struct tb *tb = sw->tb;
175 buf = validate_and_copy_from_user(user_buf, &count);
179 pm_runtime_get_sync(&sw->dev);
181 if (mutex_lock_interruptible(&tb->lock)) {
186 /* User did hardware changes behind the driver's back */
187 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
190 while (parse_line(&line, &offset, &val, 2, 5)) {
192 ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
194 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
199 mutex_unlock(&tb->lock);
202 pm_runtime_mark_last_busy(&sw->dev);
203 pm_runtime_put_autosuspend(&sw->dev);
204 free_page((unsigned long)buf);
206 return ret < 0 ? ret : count;
209 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
210 size_t count, loff_t *ppos)
212 struct seq_file *s = file->private_data;
213 struct tb_port *port = s->private;
215 return regs_write(port->sw, port, user_buf, count, ppos);
218 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
219 size_t count, loff_t *ppos)
221 struct seq_file *s = file->private_data;
222 struct tb_switch *sw = s->private;
224 return regs_write(sw, NULL, user_buf, count, ppos);
227 static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size,
233 token = strsep(line, "\n");
237 /* Parse the register first */
238 field = strsep(&token, " ");
241 if (kstrtou8(field, 0, reg))
244 /* Then the values for the register, up to data_size */
245 for (i = 0; i < data_size; i++) {
246 field = strsep(&token, " ");
249 if (kstrtou8(field, 0, &data[i]))
257 static ssize_t sb_regs_write(struct tb_port *port, const struct sb_reg *sb_regs,
258 size_t size, enum usb4_sb_target target, u8 index,
259 char *buf, size_t count, loff_t *ppos)
261 u8 reg, data[SB_MAX_SIZE];
265 /* User did hardware changes behind the driver's back */
266 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
269 * For sideband registers we accept:
272 * Here "reg" is the byte offset of the sideband register and "b0"..
273 * are the byte values. There can be less byte values than the register
274 * size. The leftovers will not be overwritten.
276 while (parse_sb_line(&line, ®, data, ARRAY_SIZE(data), &bytes_read)) {
277 const struct sb_reg *sb_reg;
280 /* At least one byte must be passed */
284 /* Find the register */
286 for (int i = 0; i < size; i++) {
287 if (sb_regs[i].reg == reg) {
288 sb_reg = &sb_regs[i];
296 if (bytes_read > sb_regs->size)
299 ret = usb4_port_sb_write(port, target, index, sb_reg->reg, data,
308 static ssize_t port_sb_regs_write(struct file *file, const char __user *user_buf,
309 size_t count, loff_t *ppos)
311 struct seq_file *s = file->private_data;
312 struct tb_port *port = s->private;
313 struct tb_switch *sw = port->sw;
314 struct tb *tb = sw->tb;
318 buf = validate_and_copy_from_user(user_buf, &count);
322 pm_runtime_get_sync(&sw->dev);
324 if (mutex_lock_interruptible(&tb->lock)) {
329 ret = sb_regs_write(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
330 USB4_SB_TARGET_ROUTER, 0, buf, count, ppos);
332 mutex_unlock(&tb->lock);
334 pm_runtime_mark_last_busy(&sw->dev);
335 pm_runtime_put_autosuspend(&sw->dev);
337 return ret < 0 ? ret : count;
340 static ssize_t retimer_sb_regs_write(struct file *file,
341 const char __user *user_buf,
342 size_t count, loff_t *ppos)
344 struct seq_file *s = file->private_data;
345 struct tb_retimer *rt = s->private;
346 struct tb *tb = rt->tb;
350 buf = validate_and_copy_from_user(user_buf, &count);
354 pm_runtime_get_sync(&rt->dev);
356 if (mutex_lock_interruptible(&tb->lock)) {
361 ret = sb_regs_write(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
362 USB4_SB_TARGET_RETIMER, rt->index, buf, count, ppos);
364 mutex_unlock(&tb->lock);
366 pm_runtime_mark_last_busy(&rt->dev);
367 pm_runtime_put_autosuspend(&rt->dev);
369 return ret < 0 ? ret : count;
371 #define DEBUGFS_MODE 0600
373 #define port_regs_write NULL
374 #define switch_regs_write NULL
375 #define port_sb_regs_write NULL
376 #define retimer_sb_regs_write NULL
377 #define DEBUGFS_MODE 0400
380 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
382 * struct tb_margining - Lane margining support
383 * @port: USB4 port through which the margining operations are run
384 * @target: Sideband target
385 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
386 * @dev: Pointer to the device that is the target (USB4 port or retimer)
387 * @caps: Port lane margining capabilities
388 * @results: Last lane margining results
389 * @lanes: %0, %1 or %7 (all)
390 * @min_ber_level: Minimum supported BER level contour value
391 * @max_ber_level: Maximum supported BER level contour value
392 * @ber_level: Current BER level contour value
393 * @voltage_steps: Number of mandatory voltage steps
394 * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
395 * @time_steps: Number of time margin steps
396 * @max_time_offset: Maximum time margin offset (in mUI)
397 * @software: %true if software margining is used instead of hardware
398 * @time: %true if time margining is used instead of voltage
399 * @right_high: %false if left/low margin test is performed, %true if
402 struct tb_margining {
403 struct tb_port *port;
404 enum usb4_sb_target target;
410 unsigned int min_ber_level;
411 unsigned int max_ber_level;
412 unsigned int ber_level;
413 unsigned int voltage_steps;
414 unsigned int max_voltage_offset;
415 unsigned int time_steps;
416 unsigned int max_time_offset;
422 static bool supports_software(const struct tb_margining *margining)
424 return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
427 static bool supports_hardware(const struct tb_margining *margining)
429 return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
432 static bool both_lanes(const struct tb_margining *margining)
434 return margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
438 independent_voltage_margins(const struct tb_margining *margining)
440 return FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK, margining->caps[0]);
443 static bool supports_time(const struct tb_margining *margining)
445 return margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
448 /* Only applicable if supports_time() returns true */
450 independent_time_margins(const struct tb_margining *margining)
452 return FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK, margining->caps[1]);
456 margining_ber_level_write(struct file *file, const char __user *user_buf,
457 size_t count, loff_t *ppos)
459 struct seq_file *s = file->private_data;
460 struct tb_margining *margining = s->private;
461 struct tb *tb = margining->port->sw->tb;
466 if (mutex_lock_interruptible(&tb->lock))
469 if (margining->software) {
474 buf = validate_and_copy_from_user(user_buf, &count);
480 buf[count - 1] = '\0';
482 ret = kstrtouint(buf, 10, &val);
486 if (val < margining->min_ber_level ||
487 val > margining->max_ber_level) {
492 margining->ber_level = val;
495 free_page((unsigned long)buf);
497 mutex_unlock(&tb->lock);
499 return ret < 0 ? ret : count;
502 static void ber_level_show(struct seq_file *s, unsigned int val)
505 seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
507 seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
510 static int margining_ber_level_show(struct seq_file *s, void *not_used)
512 const struct tb_margining *margining = s->private;
514 if (margining->software)
516 ber_level_show(s, margining->ber_level);
519 DEBUGFS_ATTR_RW(margining_ber_level);
521 static int margining_caps_show(struct seq_file *s, void *not_used)
523 struct tb_margining *margining = s->private;
524 struct tb *tb = margining->port->sw->tb;
527 if (mutex_lock_interruptible(&tb->lock))
530 /* Dump the raw caps first */
531 cap0 = margining->caps[0];
532 seq_printf(s, "0x%08x\n", cap0);
533 cap1 = margining->caps[1];
534 seq_printf(s, "0x%08x\n", cap1);
536 seq_printf(s, "# software margining: %s\n",
537 supports_software(margining) ? "yes" : "no");
538 if (supports_hardware(margining)) {
539 seq_puts(s, "# hardware margining: yes\n");
540 seq_puts(s, "# minimum BER level contour: ");
541 ber_level_show(s, margining->min_ber_level);
542 seq_puts(s, "# maximum BER level contour: ");
543 ber_level_show(s, margining->max_ber_level);
545 seq_puts(s, "# hardware margining: no\n");
548 seq_printf(s, "# both lanes simultaneously: %s\n",
549 both_lanes(margining) ? "yes" : "no");
550 seq_printf(s, "# voltage margin steps: %u\n",
551 margining->voltage_steps);
552 seq_printf(s, "# maximum voltage offset: %u mV\n",
553 margining->max_voltage_offset);
555 switch (independent_voltage_margins(margining)) {
556 case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
557 seq_puts(s, "# returns minimum between high and low voltage margins\n");
559 case USB4_MARGIN_CAP_0_VOLTAGE_HL:
560 seq_puts(s, "# returns high or low voltage margin\n");
562 case USB4_MARGIN_CAP_0_VOLTAGE_BOTH:
563 seq_puts(s, "# returns both high and low margins\n");
567 if (supports_time(margining)) {
568 seq_puts(s, "# time margining: yes\n");
569 seq_printf(s, "# time margining is destructive: %s\n",
570 cap1 & USB4_MARGIN_CAP_1_TIME_DESTR ? "yes" : "no");
572 switch (independent_time_margins(margining)) {
573 case USB4_MARGIN_CAP_1_TIME_MIN:
574 seq_puts(s, "# returns minimum between left and right time margins\n");
576 case USB4_MARGIN_CAP_1_TIME_LR:
577 seq_puts(s, "# returns left or right margin\n");
579 case USB4_MARGIN_CAP_1_TIME_BOTH:
580 seq_puts(s, "# returns both left and right margins\n");
584 seq_printf(s, "# time margin steps: %u\n",
585 margining->time_steps);
586 seq_printf(s, "# maximum time offset: %u mUI\n",
587 margining->max_time_offset);
589 seq_puts(s, "# time margining: no\n");
592 mutex_unlock(&tb->lock);
595 DEBUGFS_ATTR_RO(margining_caps);
598 margining_lanes_write(struct file *file, const char __user *user_buf,
599 size_t count, loff_t *ppos)
601 struct seq_file *s = file->private_data;
602 struct tb_margining *margining = s->private;
603 struct tb *tb = margining->port->sw->tb;
607 buf = validate_and_copy_from_user(user_buf, &count);
611 buf[count - 1] = '\0';
613 if (mutex_lock_interruptible(&tb->lock)) {
618 if (!strcmp(buf, "0")) {
619 margining->lanes = 0;
620 } else if (!strcmp(buf, "1")) {
621 margining->lanes = 1;
622 } else if (!strcmp(buf, "all")) {
623 /* Needs to be supported */
624 if (both_lanes(margining))
625 margining->lanes = 7;
632 mutex_unlock(&tb->lock);
635 free_page((unsigned long)buf);
636 return ret < 0 ? ret : count;
639 static int margining_lanes_show(struct seq_file *s, void *not_used)
641 struct tb_margining *margining = s->private;
642 struct tb *tb = margining->port->sw->tb;
645 if (mutex_lock_interruptible(&tb->lock))
648 lanes = margining->lanes;
649 if (both_lanes(margining)) {
651 seq_puts(s, "[0] 1 all\n");
653 seq_puts(s, "0 [1] all\n");
655 seq_puts(s, "0 1 [all]\n");
658 seq_puts(s, "[0] 1\n");
660 seq_puts(s, "0 [1]\n");
663 mutex_unlock(&tb->lock);
666 DEBUGFS_ATTR_RW(margining_lanes);
668 static ssize_t margining_mode_write(struct file *file,
669 const char __user *user_buf,
670 size_t count, loff_t *ppos)
672 struct seq_file *s = file->private_data;
673 struct tb_margining *margining = s->private;
674 struct tb *tb = margining->port->sw->tb;
678 buf = validate_and_copy_from_user(user_buf, &count);
682 buf[count - 1] = '\0';
684 if (mutex_lock_interruptible(&tb->lock)) {
689 if (!strcmp(buf, "software")) {
690 if (supports_software(margining))
691 margining->software = true;
694 } else if (!strcmp(buf, "hardware")) {
695 if (supports_hardware(margining))
696 margining->software = false;
703 mutex_unlock(&tb->lock);
706 free_page((unsigned long)buf);
707 return ret ? ret : count;
710 static int margining_mode_show(struct seq_file *s, void *not_used)
712 struct tb_margining *margining = s->private;
713 struct tb *tb = margining->port->sw->tb;
714 const char *space = "";
716 if (mutex_lock_interruptible(&tb->lock))
719 if (supports_software(margining)) {
720 if (margining->software)
721 seq_puts(s, "[software]");
723 seq_puts(s, "software");
726 if (supports_hardware(margining)) {
727 if (margining->software)
728 seq_printf(s, "%shardware", space);
730 seq_printf(s, "%s[hardware]", space);
733 mutex_unlock(&tb->lock);
738 DEBUGFS_ATTR_RW(margining_mode);
740 static int margining_run_write(void *data, u64 val)
742 struct tb_margining *margining = data;
743 struct tb_port *port = margining->port;
744 struct device *dev = margining->dev;
745 struct tb_switch *sw = port->sw;
746 struct tb_switch *down_sw;
747 struct tb *tb = sw->tb;
753 pm_runtime_get_sync(dev);
755 if (mutex_lock_interruptible(&tb->lock)) {
760 if (tb_is_upstream_port(port))
762 else if (port->remote)
763 down_sw = port->remote->sw;
769 * CL states may interfere with lane margining so
770 * disable them temporarily now.
772 ret = tb_switch_clx_disable(down_sw);
774 tb_sw_warn(down_sw, "failed to disable CL states\n");
780 if (margining->software) {
782 "running software %s lane margining for %s lanes %u\n",
783 margining->time ? "time" : "voltage", dev_name(dev),
785 ret = usb4_port_sw_margin(port, margining->target, margining->index,
786 margining->lanes, margining->time,
787 margining->right_high,
788 USB4_MARGIN_SW_COUNTER_CLEAR);
792 ret = usb4_port_sw_margin_errors(port, margining->target,
794 &margining->results[0]);
797 "running hardware %s lane margining for %s lanes %u\n",
798 margining->time ? "time" : "voltage", dev_name(dev),
800 /* Clear the results */
801 margining->results[0] = 0;
802 margining->results[1] = 0;
803 ret = usb4_port_hw_margin(port, margining->target, margining->index,
804 margining->lanes, margining->ber_level,
805 margining->time, margining->right_high,
811 tb_switch_clx_enable(down_sw, clx);
813 mutex_unlock(&tb->lock);
815 pm_runtime_mark_last_busy(dev);
816 pm_runtime_put_autosuspend(dev);
820 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
823 static ssize_t margining_results_write(struct file *file,
824 const char __user *user_buf,
825 size_t count, loff_t *ppos)
827 struct seq_file *s = file->private_data;
828 struct tb_margining *margining = s->private;
829 struct tb *tb = margining->port->sw->tb;
831 if (mutex_lock_interruptible(&tb->lock))
834 /* Just clear the results */
835 margining->results[0] = 0;
836 margining->results[1] = 0;
838 mutex_unlock(&tb->lock);
842 static void voltage_margin_show(struct seq_file *s,
843 const struct tb_margining *margining, u8 val)
845 unsigned int tmp, voltage;
847 tmp = FIELD_GET(USB4_MARGIN_HW_RES_1_MARGIN_MASK, val);
848 voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
849 seq_printf(s, "%u mV (%u)", voltage, tmp);
850 if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
851 seq_puts(s, " exceeds maximum");
855 static void time_margin_show(struct seq_file *s,
856 const struct tb_margining *margining, u8 val)
858 unsigned int tmp, interval;
860 tmp = FIELD_GET(USB4_MARGIN_HW_RES_1_MARGIN_MASK, val);
861 interval = tmp * margining->max_time_offset / margining->time_steps;
862 seq_printf(s, "%u mUI (%u)", interval, tmp);
863 if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
864 seq_puts(s, " exceeds maximum");
868 static int margining_results_show(struct seq_file *s, void *not_used)
870 struct tb_margining *margining = s->private;
871 struct tb *tb = margining->port->sw->tb;
873 if (mutex_lock_interruptible(&tb->lock))
876 /* Dump the raw results first */
877 seq_printf(s, "0x%08x\n", margining->results[0]);
878 /* Only the hardware margining has two result dwords */
879 if (!margining->software) {
882 seq_printf(s, "0x%08x\n", margining->results[1]);
884 if (margining->time) {
885 if (!margining->lanes || margining->lanes == 7) {
886 val = margining->results[1];
887 seq_puts(s, "# lane 0 right time margin: ");
888 time_margin_show(s, margining, val);
889 val = margining->results[1] >>
890 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
891 seq_puts(s, "# lane 0 left time margin: ");
892 time_margin_show(s, margining, val);
894 if (margining->lanes == 1 || margining->lanes == 7) {
895 val = margining->results[1] >>
896 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
897 seq_puts(s, "# lane 1 right time margin: ");
898 time_margin_show(s, margining, val);
899 val = margining->results[1] >>
900 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
901 seq_puts(s, "# lane 1 left time margin: ");
902 time_margin_show(s, margining, val);
905 if (!margining->lanes || margining->lanes == 7) {
906 val = margining->results[1];
907 seq_puts(s, "# lane 0 high voltage margin: ");
908 voltage_margin_show(s, margining, val);
909 val = margining->results[1] >>
910 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
911 seq_puts(s, "# lane 0 low voltage margin: ");
912 voltage_margin_show(s, margining, val);
914 if (margining->lanes == 1 || margining->lanes == 7) {
915 val = margining->results[1] >>
916 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
917 seq_puts(s, "# lane 1 high voltage margin: ");
918 voltage_margin_show(s, margining, val);
919 val = margining->results[1] >>
920 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
921 seq_puts(s, "# lane 1 low voltage margin: ");
922 voltage_margin_show(s, margining, val);
927 mutex_unlock(&tb->lock);
930 DEBUGFS_ATTR_RW(margining_results);
932 static ssize_t margining_test_write(struct file *file,
933 const char __user *user_buf,
934 size_t count, loff_t *ppos)
936 struct seq_file *s = file->private_data;
937 struct tb_margining *margining = s->private;
938 struct tb *tb = margining->port->sw->tb;
942 buf = validate_and_copy_from_user(user_buf, &count);
946 buf[count - 1] = '\0';
948 if (mutex_lock_interruptible(&tb->lock)) {
953 if (!strcmp(buf, "time") && supports_time(margining))
954 margining->time = true;
955 else if (!strcmp(buf, "voltage"))
956 margining->time = false;
960 mutex_unlock(&tb->lock);
963 free_page((unsigned long)buf);
964 return ret ? ret : count;
967 static int margining_test_show(struct seq_file *s, void *not_used)
969 struct tb_margining *margining = s->private;
970 struct tb *tb = margining->port->sw->tb;
972 if (mutex_lock_interruptible(&tb->lock))
975 if (supports_time(margining)) {
977 seq_puts(s, "voltage [time]\n");
979 seq_puts(s, "[voltage] time\n");
981 seq_puts(s, "[voltage]\n");
984 mutex_unlock(&tb->lock);
987 DEBUGFS_ATTR_RW(margining_test);
989 static ssize_t margining_margin_write(struct file *file,
990 const char __user *user_buf,
991 size_t count, loff_t *ppos)
993 struct seq_file *s = file->private_data;
994 struct tb_margining *margining = s->private;
995 struct tb *tb = margining->port->sw->tb;
999 buf = validate_and_copy_from_user(user_buf, &count);
1001 return PTR_ERR(buf);
1003 buf[count - 1] = '\0';
1005 if (mutex_lock_interruptible(&tb->lock)) {
1010 if (margining->time) {
1011 if (!strcmp(buf, "left"))
1012 margining->right_high = false;
1013 else if (!strcmp(buf, "right"))
1014 margining->right_high = true;
1018 if (!strcmp(buf, "low"))
1019 margining->right_high = false;
1020 else if (!strcmp(buf, "high"))
1021 margining->right_high = true;
1026 mutex_unlock(&tb->lock);
1029 free_page((unsigned long)buf);
1030 return ret ? ret : count;
1033 static int margining_margin_show(struct seq_file *s, void *not_used)
1035 struct tb_margining *margining = s->private;
1036 struct tb *tb = margining->port->sw->tb;
1038 if (mutex_lock_interruptible(&tb->lock))
1039 return -ERESTARTSYS;
1041 if (margining->time) {
1042 if (margining->right_high)
1043 seq_puts(s, "left [right]\n");
1045 seq_puts(s, "[left] right\n");
1047 if (margining->right_high)
1048 seq_puts(s, "low [high]\n");
1050 seq_puts(s, "[low] high\n");
1053 mutex_unlock(&tb->lock);
1056 DEBUGFS_ATTR_RW(margining_margin);
1058 static struct tb_margining *margining_alloc(struct tb_port *port,
1060 enum usb4_sb_target target,
1061 u8 index, struct dentry *parent)
1063 struct tb_margining *margining;
1068 margining = kzalloc(sizeof(*margining), GFP_KERNEL);
1072 margining->port = port;
1073 margining->target = target;
1074 margining->index = index;
1075 margining->dev = dev;
1077 ret = usb4_port_margining_caps(port, target, index, margining->caps);
1083 /* Set the initial mode */
1084 if (supports_software(margining))
1085 margining->software = true;
1087 val = FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK, margining->caps[0]);
1088 margining->voltage_steps = val;
1089 val = FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK, margining->caps[0]);
1090 margining->max_voltage_offset = 74 + val * 2;
1092 if (supports_time(margining)) {
1093 val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK, margining->caps[1]);
1094 margining->time_steps = val;
1095 val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK, margining->caps[1]);
1097 * Store it as mUI (milli Unit Interval) because we want
1098 * to keep it as integer.
1100 margining->max_time_offset = 200 + 10 * val;
1103 dir = debugfs_create_dir("margining", parent);
1104 if (supports_hardware(margining)) {
1105 val = FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK, margining->caps[1]);
1106 margining->min_ber_level = val;
1107 val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK, margining->caps[1]);
1108 margining->max_ber_level = val;
1110 /* Set the default to minimum */
1111 margining->ber_level = margining->min_ber_level;
1113 debugfs_create_file("ber_level_contour", 0400, dir, margining,
1114 &margining_ber_level_fops);
1116 debugfs_create_file("caps", 0400, dir, margining, &margining_caps_fops);
1117 debugfs_create_file("lanes", 0600, dir, margining, &margining_lanes_fops);
1118 debugfs_create_file("mode", 0600, dir, margining, &margining_mode_fops);
1119 debugfs_create_file("run", 0600, dir, margining, &margining_run_fops);
1120 debugfs_create_file("results", 0600, dir, margining,
1121 &margining_results_fops);
1122 debugfs_create_file("test", 0600, dir, margining, &margining_test_fops);
1123 if (independent_voltage_margins(margining) == USB4_MARGIN_CAP_0_VOLTAGE_HL ||
1124 (supports_time(margining) &&
1125 independent_time_margins(margining) == USB4_MARGIN_CAP_1_TIME_LR))
1126 debugfs_create_file("margin", 0600, dir, margining,
1127 &margining_margin_fops);
1131 static void margining_port_init(struct tb_port *port)
1133 struct dentry *parent;
1139 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1140 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1141 port->usb4->margining = margining_alloc(port, &port->usb4->dev,
1142 USB4_SB_TARGET_ROUTER, 0,
1146 static void margining_port_remove(struct tb_port *port)
1148 struct dentry *parent;
1154 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1155 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
1157 debugfs_lookup_and_remove("margining", parent);
1159 kfree(port->usb4->margining);
1160 port->usb4->margining = NULL;
1163 static void margining_switch_init(struct tb_switch *sw)
1165 struct tb_port *upstream, *downstream;
1166 struct tb_switch *parent_sw;
1167 u64 route = tb_route(sw);
1172 upstream = tb_upstream_port(sw);
1173 parent_sw = tb_switch_parent(sw);
1174 downstream = tb_port_at(route, parent_sw);
1176 margining_port_init(downstream);
1177 margining_port_init(upstream);
1180 static void margining_switch_remove(struct tb_switch *sw)
1182 struct tb_port *upstream, *downstream;
1183 struct tb_switch *parent_sw;
1184 u64 route = tb_route(sw);
1189 upstream = tb_upstream_port(sw);
1190 parent_sw = tb_switch_parent(sw);
1191 downstream = tb_port_at(route, parent_sw);
1193 margining_port_remove(upstream);
1194 margining_port_remove(downstream);
1197 static void margining_xdomain_init(struct tb_xdomain *xd)
1199 struct tb_switch *parent_sw;
1200 struct tb_port *downstream;
1202 parent_sw = tb_xdomain_parent(xd);
1203 downstream = tb_port_at(xd->route, parent_sw);
1205 margining_port_init(downstream);
1208 static void margining_xdomain_remove(struct tb_xdomain *xd)
1210 struct tb_switch *parent_sw;
1211 struct tb_port *downstream;
1213 parent_sw = tb_xdomain_parent(xd);
1214 downstream = tb_port_at(xd->route, parent_sw);
1215 margining_port_remove(downstream);
1218 static void margining_retimer_init(struct tb_retimer *rt, struct dentry *debugfs_dir)
1220 rt->margining = margining_alloc(rt->port, &rt->dev,
1221 USB4_SB_TARGET_RETIMER, rt->index,
1225 static void margining_retimer_remove(struct tb_retimer *rt)
1227 kfree(rt->margining);
1228 rt->margining = NULL;
1231 static inline void margining_switch_init(struct tb_switch *sw) { }
1232 static inline void margining_switch_remove(struct tb_switch *sw) { }
1233 static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1234 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1235 static inline void margining_retimer_init(struct tb_retimer *rt,
1236 struct dentry *debugfs_dir) { }
1237 static inline void margining_retimer_remove(struct tb_retimer *rt) { }
1240 static int port_clear_all_counters(struct tb_port *port)
1245 buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1250 ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1251 COUNTER_SET_LEN * port->config.max_counters);
1257 static ssize_t counters_write(struct file *file, const char __user *user_buf,
1258 size_t count, loff_t *ppos)
1260 struct seq_file *s = file->private_data;
1261 struct tb_port *port = s->private;
1262 struct tb_switch *sw = port->sw;
1263 struct tb *tb = port->sw->tb;
1267 buf = validate_and_copy_from_user(user_buf, &count);
1269 return PTR_ERR(buf);
1271 pm_runtime_get_sync(&sw->dev);
1273 if (mutex_lock_interruptible(&tb->lock)) {
1278 /* If written delimiter only, clear all counters in one shot */
1279 if (buf[0] == '\n') {
1280 ret = port_clear_all_counters(port);
1286 while (parse_line(&line, &offset, &val, 1, 4)) {
1287 ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1294 mutex_unlock(&tb->lock);
1297 pm_runtime_mark_last_busy(&sw->dev);
1298 pm_runtime_put_autosuspend(&sw->dev);
1299 free_page((unsigned long)buf);
1301 return ret < 0 ? ret : count;
1304 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1305 struct tb_port *port, unsigned int cap,
1306 unsigned int offset, u8 cap_id, u8 vsec_id,
1312 for (i = 0; i < dwords; i++) {
1314 ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1316 ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1318 seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1322 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1323 offset + i, cap_id, vsec_id, data);
1327 static void cap_show(struct seq_file *s, struct tb_switch *sw,
1328 struct tb_port *port, unsigned int cap, u8 cap_id,
1329 u8 vsec_id, int length)
1331 int ret, offset = 0;
1333 while (length > 0) {
1334 int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1335 u32 data[TB_MAX_CONFIG_RW_LENGTH];
1338 ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1341 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1343 cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1347 for (i = 0; i < dwords; i++) {
1348 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1349 cap + offset + i, offset + i,
1350 cap_id, vsec_id, data[i]);
1358 static void port_cap_show(struct tb_port *port, struct seq_file *s,
1361 struct tb_cap_any header;
1366 ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1368 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1372 switch (header.basic.cap) {
1373 case TB_PORT_CAP_PHY:
1374 length = PORT_CAP_LANE_LEN;
1377 case TB_PORT_CAP_TIME1:
1378 if (usb4_switch_version(port->sw) < 2)
1379 length = PORT_CAP_TMU_V1_LEN;
1381 length = PORT_CAP_TMU_V2_LEN;
1384 case TB_PORT_CAP_POWER:
1385 length = PORT_CAP_POWER_LEN;
1388 case TB_PORT_CAP_ADAP:
1389 if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
1390 if (usb4_switch_version(port->sw) < 2)
1391 length = PORT_CAP_V1_PCIE_LEN;
1393 length = PORT_CAP_V2_PCIE_LEN;
1394 } else if (tb_port_is_dpin(port)) {
1395 if (usb4_switch_version(port->sw) < 2)
1396 length = PORT_CAP_DP_V1_LEN;
1398 length = PORT_CAP_DP_V2_LEN;
1399 } else if (tb_port_is_dpout(port)) {
1400 length = PORT_CAP_DP_V1_LEN;
1401 } else if (tb_port_is_usb3_down(port) ||
1402 tb_port_is_usb3_up(port)) {
1403 length = PORT_CAP_USB3_LEN;
1405 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1406 cap, header.basic.cap);
1411 case TB_PORT_CAP_VSE:
1412 if (!header.extended_short.length) {
1413 ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
1416 seq_printf(s, "0x%04x <capability read failed>\n",
1420 length = header.extended_long.length;
1421 vsec_id = header.extended_short.vsec_id;
1423 length = header.extended_short.length;
1424 vsec_id = header.extended_short.vsec_id;
1428 case TB_PORT_CAP_USB4:
1429 length = PORT_CAP_USB4_LEN;
1433 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1434 cap, header.basic.cap);
1438 cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
1441 static void port_caps_show(struct tb_port *port, struct seq_file *s)
1445 cap = tb_port_next_cap(port, 0);
1447 port_cap_show(port, s, cap);
1448 cap = tb_port_next_cap(port, cap);
1452 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
1454 u32 data[PORT_CAP_BASIC_LEN];
1457 ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
1461 for (i = 0; i < ARRAY_SIZE(data); i++)
1462 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1467 static int port_regs_show(struct seq_file *s, void *not_used)
1469 struct tb_port *port = s->private;
1470 struct tb_switch *sw = port->sw;
1471 struct tb *tb = sw->tb;
1474 pm_runtime_get_sync(&sw->dev);
1476 if (mutex_lock_interruptible(&tb->lock)) {
1481 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1483 ret = port_basic_regs_show(port, s);
1487 port_caps_show(port, s);
1490 mutex_unlock(&tb->lock);
1492 pm_runtime_mark_last_busy(&sw->dev);
1493 pm_runtime_put_autosuspend(&sw->dev);
1497 DEBUGFS_ATTR_RW(port_regs);
1499 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
1502 struct tb_cap_any header;
1506 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
1508 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1512 if (header.basic.cap == TB_SWITCH_CAP_VSE) {
1513 if (!header.extended_short.length) {
1514 ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
1517 seq_printf(s, "0x%04x <capability read failed>\n",
1521 length = header.extended_long.length;
1523 length = header.extended_short.length;
1525 vsec_id = header.extended_short.vsec_id;
1527 if (header.basic.cap == TB_SWITCH_CAP_TMU) {
1528 length = SWITCH_CAP_TMU_LEN;
1530 seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
1531 cap, header.basic.cap);
1536 cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
1539 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
1543 cap = tb_switch_next_cap(sw, 0);
1545 switch_cap_show(sw, s, cap);
1546 cap = tb_switch_next_cap(sw, cap);
1550 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
1552 u32 data[SWITCH_CAP_BASIC_LEN];
1556 /* Only USB4 has the additional registers */
1557 if (tb_switch_is_usb4(sw))
1558 dwords = ARRAY_SIZE(data);
1562 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
1566 for (i = 0; i < dwords; i++)
1567 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1572 static int switch_regs_show(struct seq_file *s, void *not_used)
1574 struct tb_switch *sw = s->private;
1575 struct tb *tb = sw->tb;
1578 pm_runtime_get_sync(&sw->dev);
1580 if (mutex_lock_interruptible(&tb->lock)) {
1585 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1587 ret = switch_basic_regs_show(sw, s);
1591 switch_caps_show(sw, s);
1594 mutex_unlock(&tb->lock);
1596 pm_runtime_mark_last_busy(&sw->dev);
1597 pm_runtime_put_autosuspend(&sw->dev);
1601 DEBUGFS_ATTR_RW(switch_regs);
1603 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
1608 ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
1611 seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
1615 for (i = 0; i < ARRAY_SIZE(data); i++) {
1616 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1617 hopid * PATH_LEN + i, i, hopid, data[i]);
1623 static int path_show(struct seq_file *s, void *not_used)
1625 struct tb_port *port = s->private;
1626 struct tb_switch *sw = port->sw;
1627 struct tb *tb = sw->tb;
1628 int start, i, ret = 0;
1630 pm_runtime_get_sync(&sw->dev);
1632 if (mutex_lock_interruptible(&tb->lock)) {
1637 seq_puts(s, "# offset relative_offset in_hop_id value\n");
1639 /* NHI and lane adapters have entry for path 0 */
1640 if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
1641 ret = path_show_one(port, s, 0);
1646 start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
1648 for (i = start; i <= port->config.max_in_hop_id; i++) {
1649 ret = path_show_one(port, s, i);
1655 mutex_unlock(&tb->lock);
1657 pm_runtime_mark_last_busy(&sw->dev);
1658 pm_runtime_put_autosuspend(&sw->dev);
1662 DEBUGFS_ATTR_RO(path);
1664 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
1667 u32 data[COUNTER_SET_LEN];
1670 ret = tb_port_read(port, data, TB_CFG_COUNTERS,
1671 counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
1673 seq_printf(s, "0x%04x <not accessible>\n",
1674 counter * COUNTER_SET_LEN);
1678 for (i = 0; i < ARRAY_SIZE(data); i++) {
1679 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1680 counter * COUNTER_SET_LEN + i, i, counter, data[i]);
1686 static int counters_show(struct seq_file *s, void *not_used)
1688 struct tb_port *port = s->private;
1689 struct tb_switch *sw = port->sw;
1690 struct tb *tb = sw->tb;
1693 pm_runtime_get_sync(&sw->dev);
1695 if (mutex_lock_interruptible(&tb->lock)) {
1700 seq_puts(s, "# offset relative_offset counter_id value\n");
1702 for (i = 0; i < port->config.max_counters; i++) {
1703 ret = counter_set_regs_show(port, s, i);
1708 mutex_unlock(&tb->lock);
1711 pm_runtime_mark_last_busy(&sw->dev);
1712 pm_runtime_put_autosuspend(&sw->dev);
1716 DEBUGFS_ATTR_RW(counters);
1718 static int sb_regs_show(struct tb_port *port, const struct sb_reg *sb_regs,
1719 size_t size, enum usb4_sb_target target, u8 index,
1724 seq_puts(s, "# register value\n");
1726 for (i = 0; i < size; i++) {
1727 const struct sb_reg *regs = &sb_regs[i];
1731 memset(data, 0, sizeof(data));
1732 ret = usb4_port_sb_read(port, target, index, regs->reg, data,
1737 seq_printf(s, "0x%02x", regs->reg);
1738 for (j = 0; j < regs->size; j++)
1739 seq_printf(s, " 0x%02x", data[j]);
1746 static int port_sb_regs_show(struct seq_file *s, void *not_used)
1748 struct tb_port *port = s->private;
1749 struct tb_switch *sw = port->sw;
1750 struct tb *tb = sw->tb;
1753 pm_runtime_get_sync(&sw->dev);
1755 if (mutex_lock_interruptible(&tb->lock)) {
1760 ret = sb_regs_show(port, port_sb_regs, ARRAY_SIZE(port_sb_regs),
1761 USB4_SB_TARGET_ROUTER, 0, s);
1763 mutex_unlock(&tb->lock);
1765 pm_runtime_mark_last_busy(&sw->dev);
1766 pm_runtime_put_autosuspend(&sw->dev);
1770 DEBUGFS_ATTR_RW(port_sb_regs);
1773 * tb_switch_debugfs_init() - Add debugfs entries for router
1774 * @sw: Pointer to the router
1776 * Adds debugfs directories and files for given router.
1778 void tb_switch_debugfs_init(struct tb_switch *sw)
1780 struct dentry *debugfs_dir;
1781 struct tb_port *port;
1783 debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
1784 sw->debugfs_dir = debugfs_dir;
1785 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
1788 tb_switch_for_each_port(sw, port) {
1789 struct dentry *debugfs_dir;
1794 if (port->config.type == TB_TYPE_INACTIVE)
1797 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1798 debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
1799 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
1800 port, &port_regs_fops);
1801 debugfs_create_file("path", 0400, debugfs_dir, port,
1803 if (port->config.counters_support)
1804 debugfs_create_file("counters", 0600, debugfs_dir, port,
1807 debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir,
1808 port, &port_sb_regs_fops);
1811 margining_switch_init(sw);
1815 * tb_switch_debugfs_remove() - Remove all router debugfs entries
1816 * @sw: Pointer to the router
1818 * Removes all previously added debugfs entries under this router.
1820 void tb_switch_debugfs_remove(struct tb_switch *sw)
1822 margining_switch_remove(sw);
1823 debugfs_remove_recursive(sw->debugfs_dir);
1826 void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
1828 margining_xdomain_init(xd);
1831 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
1833 margining_xdomain_remove(xd);
1837 * tb_service_debugfs_init() - Add debugfs directory for service
1838 * @svc: Thunderbolt service pointer
1840 * Adds debugfs directory for service.
1842 void tb_service_debugfs_init(struct tb_service *svc)
1844 svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
1849 * tb_service_debugfs_remove() - Remove service debugfs directory
1850 * @svc: Thunderbolt service pointer
1852 * Removes the previously created debugfs directory for @svc.
1854 void tb_service_debugfs_remove(struct tb_service *svc)
1856 debugfs_remove_recursive(svc->debugfs_dir);
1857 svc->debugfs_dir = NULL;
1860 static int retimer_sb_regs_show(struct seq_file *s, void *not_used)
1862 struct tb_retimer *rt = s->private;
1863 struct tb *tb = rt->tb;
1866 pm_runtime_get_sync(&rt->dev);
1868 if (mutex_lock_interruptible(&tb->lock)) {
1873 ret = sb_regs_show(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs),
1874 USB4_SB_TARGET_RETIMER, rt->index, s);
1876 mutex_unlock(&tb->lock);
1878 pm_runtime_mark_last_busy(&rt->dev);
1879 pm_runtime_put_autosuspend(&rt->dev);
1883 DEBUGFS_ATTR_RW(retimer_sb_regs);
1886 * tb_retimer_debugfs_init() - Add debugfs directory for retimer
1887 * @rt: Pointer to retimer structure
1889 * Adds and populates retimer debugfs directory.
1891 void tb_retimer_debugfs_init(struct tb_retimer *rt)
1893 struct dentry *debugfs_dir;
1895 debugfs_dir = debugfs_create_dir(dev_name(&rt->dev), tb_debugfs_root);
1896 debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, rt,
1897 &retimer_sb_regs_fops);
1898 margining_retimer_init(rt, debugfs_dir);
1902 * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
1903 * @rt: Pointer to retimer structure
1905 * Removes the retimer debugfs directory along with its contents.
1907 void tb_retimer_debugfs_remove(struct tb_retimer *rt)
1909 debugfs_lookup_and_remove(dev_name(&rt->dev), tb_debugfs_root);
1910 margining_retimer_remove(rt);
1913 void tb_debugfs_init(void)
1915 tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
1918 void tb_debugfs_exit(void)
1920 debugfs_remove_recursive(tb_debugfs_root);