1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2020, Intel Corporation
6 * Authors: Gil Fine <gil.fine@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/debugfs.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/uaccess.h>
17 #define PORT_CAP_PCIE_LEN 1
18 #define PORT_CAP_POWER_LEN 2
19 #define PORT_CAP_LANE_LEN 3
20 #define PORT_CAP_USB3_LEN 5
21 #define PORT_CAP_DP_LEN 8
22 #define PORT_CAP_TMU_LEN 8
23 #define PORT_CAP_BASIC_LEN 9
24 #define PORT_CAP_USB4_LEN 20
26 #define SWITCH_CAP_TMU_LEN 26
27 #define SWITCH_CAP_BASIC_LEN 27
31 #define COUNTER_SET_LEN 3
33 #define DEBUGFS_ATTR(__space, __write) \
34 static int __space ## _open(struct inode *inode, struct file *file) \
36 return single_open(file, __space ## _show, inode->i_private); \
39 static const struct file_operations __space ## _fops = { \
40 .owner = THIS_MODULE, \
41 .open = __space ## _open, \
42 .release = single_release, \
45 .llseek = seq_lseek, \
48 #define DEBUGFS_ATTR_RO(__space) \
49 DEBUGFS_ATTR(__space, NULL)
51 #define DEBUGFS_ATTR_RW(__space) \
52 DEBUGFS_ATTR(__space, __space ## _write)
54 static struct dentry *tb_debugfs_root;
56 static void *validate_and_copy_from_user(const void __user *user_buf,
63 return ERR_PTR(-EINVAL);
65 if (!access_ok(user_buf, *count))
66 return ERR_PTR(-EFAULT);
68 buf = (void *)get_zeroed_page(GFP_KERNEL);
70 return ERR_PTR(-ENOMEM);
72 nbytes = min_t(size_t, *count, PAGE_SIZE);
73 if (copy_from_user(buf, user_buf, nbytes)) {
74 free_page((unsigned long)buf);
75 return ERR_PTR(-EFAULT);
82 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len,
89 token = strsep(line, "\n");
94 * For Adapter/Router configuration space:
95 * Short format is: offset value\n
97 * Long format as produced from the read side:
98 * offset relative_offset cap_id vs_cap_id value\n
99 * v[0] v[1] v[2] v[3] v[4]
101 * For Counter configuration space:
102 * Short format is: offset\n
104 * Long format as produced from the read side:
105 * offset relative_offset counter_id value\n
106 * v[0] v[1] v[2] v[3]
108 ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]);
109 /* In case of Counters, clear counter, "val" content is NA */
110 if (ret == short_fmt_len) {
112 *val = v[short_fmt_len - 1];
114 } else if (ret == long_fmt_len) {
116 *val = v[long_fmt_len - 1];
123 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
124 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port,
125 const char __user *user_buf, size_t count,
128 struct tb *tb = sw->tb;
133 buf = validate_and_copy_from_user(user_buf, &count);
137 pm_runtime_get_sync(&sw->dev);
139 if (mutex_lock_interruptible(&tb->lock)) {
144 /* User did hardware changes behind the driver's back */
145 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
148 while (parse_line(&line, &offset, &val, 2, 5)) {
150 ret = tb_port_write(port, &val, TB_CFG_PORT, offset, 1);
152 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1);
157 mutex_unlock(&tb->lock);
160 pm_runtime_mark_last_busy(&sw->dev);
161 pm_runtime_put_autosuspend(&sw->dev);
162 free_page((unsigned long)buf);
164 return ret < 0 ? ret : count;
167 static ssize_t port_regs_write(struct file *file, const char __user *user_buf,
168 size_t count, loff_t *ppos)
170 struct seq_file *s = file->private_data;
171 struct tb_port *port = s->private;
173 return regs_write(port->sw, port, user_buf, count, ppos);
176 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf,
177 size_t count, loff_t *ppos)
179 struct seq_file *s = file->private_data;
180 struct tb_switch *sw = s->private;
182 return regs_write(sw, NULL, user_buf, count, ppos);
184 #define DEBUGFS_MODE 0600
186 #define port_regs_write NULL
187 #define switch_regs_write NULL
188 #define DEBUGFS_MODE 0400
191 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
193 * struct tb_margining - Lane margining support
194 * @caps: Port lane margining capabilities
195 * @results: Last lane margining results
196 * @lanes: %0, %1 or %7 (all)
197 * @min_ber_level: Minimum supported BER level contour value
198 * @max_ber_level: Maximum supported BER level contour value
199 * @ber_level: Current BER level contour value
200 * @voltage_steps: Number of mandatory voltage steps
201 * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
202 * @time_steps: Number of time margin steps
203 * @max_time_offset: Maximum time margin offset (in mUI)
204 * @software: %true if software margining is used instead of hardware
205 * @time: %true if time margining is used instead of voltage
206 * @right_high: %false if left/low margin test is performed, %true if
209 struct tb_margining {
213 unsigned int min_ber_level;
214 unsigned int max_ber_level;
215 unsigned int ber_level;
216 unsigned int voltage_steps;
217 unsigned int max_voltage_offset;
218 unsigned int time_steps;
219 unsigned int max_time_offset;
225 static bool supports_software(const struct usb4_port *usb4)
227 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW;
230 static bool supports_hardware(const struct usb4_port *usb4)
232 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW;
235 static bool both_lanes(const struct usb4_port *usb4)
237 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_2_LANES;
240 static unsigned int independent_voltage_margins(const struct usb4_port *usb4)
242 return (usb4->margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK) >>
243 USB4_MARGIN_CAP_0_VOLTAGE_INDP_SHIFT;
246 static bool supports_time(const struct usb4_port *usb4)
248 return usb4->margining->caps[0] & USB4_MARGIN_CAP_0_TIME;
251 /* Only applicable if supports_time() returns true */
252 static unsigned int independent_time_margins(const struct usb4_port *usb4)
254 return (usb4->margining->caps[1] & USB4_MARGIN_CAP_1_TIME_INDP_MASK) >>
255 USB4_MARGIN_CAP_1_TIME_INDP_SHIFT;
259 margining_ber_level_write(struct file *file, const char __user *user_buf,
260 size_t count, loff_t *ppos)
262 struct seq_file *s = file->private_data;
263 struct tb_port *port = s->private;
264 struct usb4_port *usb4 = port->usb4;
265 struct tb *tb = port->sw->tb;
270 if (mutex_lock_interruptible(&tb->lock))
273 if (usb4->margining->software) {
278 buf = validate_and_copy_from_user(user_buf, &count);
284 buf[count - 1] = '\0';
286 ret = kstrtouint(buf, 10, &val);
290 if (val < usb4->margining->min_ber_level ||
291 val > usb4->margining->max_ber_level) {
296 usb4->margining->ber_level = val;
299 free_page((unsigned long)buf);
301 mutex_unlock(&tb->lock);
303 return ret < 0 ? ret : count;
306 static void ber_level_show(struct seq_file *s, unsigned int val)
309 seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val);
311 seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val);
314 static int margining_ber_level_show(struct seq_file *s, void *not_used)
316 struct tb_port *port = s->private;
317 struct usb4_port *usb4 = port->usb4;
319 if (usb4->margining->software)
321 ber_level_show(s, usb4->margining->ber_level);
324 DEBUGFS_ATTR_RW(margining_ber_level);
326 static int margining_caps_show(struct seq_file *s, void *not_used)
328 struct tb_port *port = s->private;
329 struct usb4_port *usb4 = port->usb4;
330 struct tb *tb = port->sw->tb;
333 if (mutex_lock_interruptible(&tb->lock))
336 /* Dump the raw caps first */
337 cap0 = usb4->margining->caps[0];
338 seq_printf(s, "0x%08x\n", cap0);
339 cap1 = usb4->margining->caps[1];
340 seq_printf(s, "0x%08x\n", cap1);
342 seq_printf(s, "# software margining: %s\n",
343 supports_software(usb4) ? "yes" : "no");
344 if (supports_hardware(usb4)) {
345 seq_puts(s, "# hardware margining: yes\n");
346 seq_puts(s, "# minimum BER level contour: ");
347 ber_level_show(s, usb4->margining->min_ber_level);
348 seq_puts(s, "# maximum BER level contour: ");
349 ber_level_show(s, usb4->margining->max_ber_level);
351 seq_puts(s, "# hardware margining: no\n");
354 seq_printf(s, "# both lanes simultaneously: %s\n",
355 both_lanes(usb4) ? "yes" : "no");
356 seq_printf(s, "# voltage margin steps: %u\n",
357 usb4->margining->voltage_steps);
358 seq_printf(s, "# maximum voltage offset: %u mV\n",
359 usb4->margining->max_voltage_offset);
361 switch (independent_voltage_margins(usb4)) {
362 case USB4_MARGIN_CAP_0_VOLTAGE_MIN:
363 seq_puts(s, "# returns minimum between high and low voltage margins\n");
365 case USB4_MARGIN_CAP_0_VOLTAGE_HL:
366 seq_puts(s, "# returns high or low voltage margin\n");
368 case USB4_MARGIN_CAP_0_VOLTAGE_BOTH:
369 seq_puts(s, "# returns both high and low margins\n");
373 if (supports_time(usb4)) {
374 seq_puts(s, "# time margining: yes\n");
375 seq_printf(s, "# time margining is destructive: %s\n",
376 cap1 & USB4_MARGIN_CAP_1_TIME_DESTR ? "yes" : "no");
378 switch (independent_time_margins(usb4)) {
379 case USB4_MARGIN_CAP_1_TIME_MIN:
380 seq_puts(s, "# returns minimum between left and right time margins\n");
382 case USB4_MARGIN_CAP_1_TIME_LR:
383 seq_puts(s, "# returns left or right margin\n");
385 case USB4_MARGIN_CAP_1_TIME_BOTH:
386 seq_puts(s, "# returns both left and right margins\n");
390 seq_printf(s, "# time margin steps: %u\n",
391 usb4->margining->time_steps);
392 seq_printf(s, "# maximum time offset: %u mUI\n",
393 usb4->margining->max_time_offset);
395 seq_puts(s, "# time margining: no\n");
398 mutex_unlock(&tb->lock);
401 DEBUGFS_ATTR_RO(margining_caps);
404 margining_lanes_write(struct file *file, const char __user *user_buf,
405 size_t count, loff_t *ppos)
407 struct seq_file *s = file->private_data;
408 struct tb_port *port = s->private;
409 struct usb4_port *usb4 = port->usb4;
410 struct tb *tb = port->sw->tb;
414 buf = validate_and_copy_from_user(user_buf, &count);
418 buf[count - 1] = '\0';
420 if (mutex_lock_interruptible(&tb->lock)) {
425 if (!strcmp(buf, "0")) {
426 usb4->margining->lanes = 0;
427 } else if (!strcmp(buf, "1")) {
428 usb4->margining->lanes = 1;
429 } else if (!strcmp(buf, "all")) {
430 /* Needs to be supported */
431 if (both_lanes(usb4))
432 usb4->margining->lanes = 7;
439 mutex_unlock(&tb->lock);
442 free_page((unsigned long)buf);
443 return ret < 0 ? ret : count;
446 static int margining_lanes_show(struct seq_file *s, void *not_used)
448 struct tb_port *port = s->private;
449 struct usb4_port *usb4 = port->usb4;
450 struct tb *tb = port->sw->tb;
453 if (mutex_lock_interruptible(&tb->lock))
456 lanes = usb4->margining->lanes;
457 if (both_lanes(usb4)) {
459 seq_puts(s, "[0] 1 all\n");
461 seq_puts(s, "0 [1] all\n");
463 seq_puts(s, "0 1 [all]\n");
466 seq_puts(s, "[0] 1\n");
468 seq_puts(s, "0 [1]\n");
471 mutex_unlock(&tb->lock);
474 DEBUGFS_ATTR_RW(margining_lanes);
476 static ssize_t margining_mode_write(struct file *file,
477 const char __user *user_buf,
478 size_t count, loff_t *ppos)
480 struct seq_file *s = file->private_data;
481 struct tb_port *port = s->private;
482 struct usb4_port *usb4 = port->usb4;
483 struct tb *tb = port->sw->tb;
487 buf = validate_and_copy_from_user(user_buf, &count);
491 buf[count - 1] = '\0';
493 if (mutex_lock_interruptible(&tb->lock)) {
498 if (!strcmp(buf, "software")) {
499 if (supports_software(usb4))
500 usb4->margining->software = true;
503 } else if (!strcmp(buf, "hardware")) {
504 if (supports_hardware(usb4))
505 usb4->margining->software = false;
512 mutex_unlock(&tb->lock);
515 free_page((unsigned long)buf);
516 return ret ? ret : count;
519 static int margining_mode_show(struct seq_file *s, void *not_used)
521 const struct tb_port *port = s->private;
522 const struct usb4_port *usb4 = port->usb4;
523 struct tb *tb = port->sw->tb;
524 const char *space = "";
526 if (mutex_lock_interruptible(&tb->lock))
529 if (supports_software(usb4)) {
530 if (usb4->margining->software)
531 seq_puts(s, "[software]");
533 seq_puts(s, "software");
536 if (supports_hardware(usb4)) {
537 if (usb4->margining->software)
538 seq_printf(s, "%shardware", space);
540 seq_printf(s, "%s[hardware]", space);
543 mutex_unlock(&tb->lock);
548 DEBUGFS_ATTR_RW(margining_mode);
550 static int margining_run_write(void *data, u64 val)
552 struct tb_port *port = data;
553 struct usb4_port *usb4 = port->usb4;
554 struct tb_switch *sw = port->sw;
555 struct tb_margining *margining;
556 struct tb *tb = sw->tb;
562 pm_runtime_get_sync(&sw->dev);
564 if (mutex_lock_interruptible(&tb->lock)) {
570 * CL states may interfere with lane margining so inform the user know
573 if (tb_port_is_clx_enabled(port, TB_CL1 | TB_CL2)) {
575 "CL states are enabled, Disable them with clx=0 and re-connect\n");
580 margining = usb4->margining;
582 if (margining->software) {
583 tb_port_dbg(port, "running software %s lane margining for lanes %u\n",
584 margining->time ? "time" : "voltage", margining->lanes);
585 ret = usb4_port_sw_margin(port, margining->lanes, margining->time,
586 margining->right_high,
587 USB4_MARGIN_SW_COUNTER_CLEAR);
591 ret = usb4_port_sw_margin_errors(port, &margining->results[0]);
593 tb_port_dbg(port, "running hardware %s lane margining for lanes %u\n",
594 margining->time ? "time" : "voltage", margining->lanes);
595 /* Clear the results */
596 margining->results[0] = 0;
597 margining->results[1] = 0;
598 ret = usb4_port_hw_margin(port, margining->lanes,
599 margining->ber_level, margining->time,
600 margining->right_high, margining->results);
604 mutex_unlock(&tb->lock);
606 pm_runtime_mark_last_busy(&sw->dev);
607 pm_runtime_put_autosuspend(&sw->dev);
611 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write,
614 static ssize_t margining_results_write(struct file *file,
615 const char __user *user_buf,
616 size_t count, loff_t *ppos)
618 struct seq_file *s = file->private_data;
619 struct tb_port *port = s->private;
620 struct usb4_port *usb4 = port->usb4;
621 struct tb *tb = port->sw->tb;
623 if (mutex_lock_interruptible(&tb->lock))
626 /* Just clear the results */
627 usb4->margining->results[0] = 0;
628 usb4->margining->results[1] = 0;
630 mutex_unlock(&tb->lock);
634 static void voltage_margin_show(struct seq_file *s,
635 const struct tb_margining *margining, u8 val)
637 unsigned int tmp, voltage;
639 tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
640 voltage = tmp * margining->max_voltage_offset / margining->voltage_steps;
641 seq_printf(s, "%u mV (%u)", voltage, tmp);
642 if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
643 seq_puts(s, " exceeds maximum");
647 static void time_margin_show(struct seq_file *s,
648 const struct tb_margining *margining, u8 val)
650 unsigned int tmp, interval;
652 tmp = val & USB4_MARGIN_HW_RES_1_MARGIN_MASK;
653 interval = tmp * margining->max_time_offset / margining->time_steps;
654 seq_printf(s, "%u mUI (%u)", interval, tmp);
655 if (val & USB4_MARGIN_HW_RES_1_EXCEEDS)
656 seq_puts(s, " exceeds maximum");
660 static int margining_results_show(struct seq_file *s, void *not_used)
662 struct tb_port *port = s->private;
663 struct usb4_port *usb4 = port->usb4;
664 struct tb_margining *margining;
665 struct tb *tb = port->sw->tb;
667 if (mutex_lock_interruptible(&tb->lock))
670 margining = usb4->margining;
671 /* Dump the raw results first */
672 seq_printf(s, "0x%08x\n", margining->results[0]);
673 /* Only the hardware margining has two result dwords */
674 if (!margining->software) {
677 seq_printf(s, "0x%08x\n", margining->results[1]);
679 if (margining->time) {
680 if (!margining->lanes || margining->lanes == 7) {
681 val = margining->results[1];
682 seq_puts(s, "# lane 0 right time margin: ");
683 time_margin_show(s, margining, val);
684 val = margining->results[1] >>
685 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
686 seq_puts(s, "# lane 0 left time margin: ");
687 time_margin_show(s, margining, val);
689 if (margining->lanes == 1 || margining->lanes == 7) {
690 val = margining->results[1] >>
691 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
692 seq_puts(s, "# lane 1 right time margin: ");
693 time_margin_show(s, margining, val);
694 val = margining->results[1] >>
695 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
696 seq_puts(s, "# lane 1 left time margin: ");
697 time_margin_show(s, margining, val);
700 if (!margining->lanes || margining->lanes == 7) {
701 val = margining->results[1];
702 seq_puts(s, "# lane 0 high voltage margin: ");
703 voltage_margin_show(s, margining, val);
704 val = margining->results[1] >>
705 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT;
706 seq_puts(s, "# lane 0 low voltage margin: ");
707 voltage_margin_show(s, margining, val);
709 if (margining->lanes == 1 || margining->lanes == 7) {
710 val = margining->results[1] >>
711 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT;
712 seq_puts(s, "# lane 1 high voltage margin: ");
713 voltage_margin_show(s, margining, val);
714 val = margining->results[1] >>
715 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT;
716 seq_puts(s, "# lane 1 low voltage margin: ");
717 voltage_margin_show(s, margining, val);
722 mutex_unlock(&tb->lock);
725 DEBUGFS_ATTR_RW(margining_results);
727 static ssize_t margining_test_write(struct file *file,
728 const char __user *user_buf,
729 size_t count, loff_t *ppos)
731 struct seq_file *s = file->private_data;
732 struct tb_port *port = s->private;
733 struct usb4_port *usb4 = port->usb4;
734 struct tb *tb = port->sw->tb;
738 buf = validate_and_copy_from_user(user_buf, &count);
742 buf[count - 1] = '\0';
744 if (mutex_lock_interruptible(&tb->lock)) {
749 if (!strcmp(buf, "time") && supports_time(usb4))
750 usb4->margining->time = true;
751 else if (!strcmp(buf, "voltage"))
752 usb4->margining->time = false;
756 mutex_unlock(&tb->lock);
759 free_page((unsigned long)buf);
760 return ret ? ret : count;
763 static int margining_test_show(struct seq_file *s, void *not_used)
765 struct tb_port *port = s->private;
766 struct usb4_port *usb4 = port->usb4;
767 struct tb *tb = port->sw->tb;
769 if (mutex_lock_interruptible(&tb->lock))
772 if (supports_time(usb4)) {
773 if (usb4->margining->time)
774 seq_puts(s, "voltage [time]\n");
776 seq_puts(s, "[voltage] time\n");
778 seq_puts(s, "[voltage]\n");
781 mutex_unlock(&tb->lock);
784 DEBUGFS_ATTR_RW(margining_test);
786 static ssize_t margining_margin_write(struct file *file,
787 const char __user *user_buf,
788 size_t count, loff_t *ppos)
790 struct seq_file *s = file->private_data;
791 struct tb_port *port = s->private;
792 struct usb4_port *usb4 = port->usb4;
793 struct tb *tb = port->sw->tb;
797 buf = validate_and_copy_from_user(user_buf, &count);
801 buf[count - 1] = '\0';
803 if (mutex_lock_interruptible(&tb->lock)) {
808 if (usb4->margining->time) {
809 if (!strcmp(buf, "left"))
810 usb4->margining->right_high = false;
811 else if (!strcmp(buf, "right"))
812 usb4->margining->right_high = true;
816 if (!strcmp(buf, "low"))
817 usb4->margining->right_high = false;
818 else if (!strcmp(buf, "high"))
819 usb4->margining->right_high = true;
824 mutex_unlock(&tb->lock);
827 free_page((unsigned long)buf);
828 return ret ? ret : count;
831 static int margining_margin_show(struct seq_file *s, void *not_used)
833 struct tb_port *port = s->private;
834 struct usb4_port *usb4 = port->usb4;
835 struct tb *tb = port->sw->tb;
837 if (mutex_lock_interruptible(&tb->lock))
840 if (usb4->margining->time) {
841 if (usb4->margining->right_high)
842 seq_puts(s, "left [right]\n");
844 seq_puts(s, "[left] right\n");
846 if (usb4->margining->right_high)
847 seq_puts(s, "low [high]\n");
849 seq_puts(s, "[low] high\n");
852 mutex_unlock(&tb->lock);
855 DEBUGFS_ATTR_RW(margining_margin);
857 static void margining_port_init(struct tb_port *port)
859 struct tb_margining *margining;
860 struct dentry *dir, *parent;
861 struct usb4_port *usb4;
870 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
871 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
873 margining = kzalloc(sizeof(*margining), GFP_KERNEL);
877 ret = usb4_port_margining_caps(port, margining->caps);
883 usb4->margining = margining;
885 /* Set the initial mode */
886 if (supports_software(usb4))
887 margining->software = true;
889 val = (margining->caps[0] & USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK) >>
890 USB4_MARGIN_CAP_0_VOLTAGE_STEPS_SHIFT;
891 margining->voltage_steps = val;
892 val = (margining->caps[0] & USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK) >>
893 USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_SHIFT;
894 margining->max_voltage_offset = 74 + val * 2;
896 if (supports_time(usb4)) {
897 val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_STEPS_MASK) >>
898 USB4_MARGIN_CAP_1_TIME_STEPS_SHIFT;
899 margining->time_steps = val;
900 val = (margining->caps[1] & USB4_MARGIN_CAP_1_TIME_OFFSET_MASK) >>
901 USB4_MARGIN_CAP_1_TIME_OFFSET_SHIFT;
903 * Store it as mUI (milli Unit Interval) because we want
904 * to keep it as integer.
906 margining->max_time_offset = 200 + 10 * val;
909 dir = debugfs_create_dir("margining", parent);
910 if (supports_hardware(usb4)) {
911 val = (margining->caps[1] & USB4_MARGIN_CAP_1_MIN_BER_MASK) >>
912 USB4_MARGIN_CAP_1_MIN_BER_SHIFT;
913 margining->min_ber_level = val;
914 val = (margining->caps[1] & USB4_MARGIN_CAP_1_MAX_BER_MASK) >>
915 USB4_MARGIN_CAP_1_MAX_BER_SHIFT;
916 margining->max_ber_level = val;
918 /* Set the default to minimum */
919 margining->ber_level = margining->min_ber_level;
921 debugfs_create_file("ber_level_contour", 0400, dir, port,
922 &margining_ber_level_fops);
924 debugfs_create_file("caps", 0400, dir, port, &margining_caps_fops);
925 debugfs_create_file("lanes", 0600, dir, port, &margining_lanes_fops);
926 debugfs_create_file("mode", 0600, dir, port, &margining_mode_fops);
927 debugfs_create_file("run", 0600, dir, port, &margining_run_fops);
928 debugfs_create_file("results", 0600, dir, port, &margining_results_fops);
929 debugfs_create_file("test", 0600, dir, port, &margining_test_fops);
930 if (independent_voltage_margins(usb4) ||
931 (supports_time(usb4) && independent_time_margins(usb4)))
932 debugfs_create_file("margin", 0600, dir, port, &margining_margin_fops);
935 static void margining_port_remove(struct tb_port *port)
937 struct dentry *parent;
943 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
944 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir);
945 debugfs_remove_recursive(debugfs_lookup("margining", parent));
947 kfree(port->usb4->margining);
948 port->usb4->margining = NULL;
951 static void margining_switch_init(struct tb_switch *sw)
953 struct tb_port *upstream, *downstream;
954 struct tb_switch *parent_sw;
955 u64 route = tb_route(sw);
960 upstream = tb_upstream_port(sw);
961 parent_sw = tb_switch_parent(sw);
962 downstream = tb_port_at(route, parent_sw);
964 margining_port_init(downstream);
965 margining_port_init(upstream);
968 static void margining_switch_remove(struct tb_switch *sw)
970 struct tb_switch *parent_sw;
971 struct tb_port *downstream;
972 u64 route = tb_route(sw);
978 * Upstream is removed with the router itself but we need to
979 * remove the downstream port margining directory.
981 parent_sw = tb_switch_parent(sw);
982 downstream = tb_port_at(route, parent_sw);
983 margining_port_remove(downstream);
986 static void margining_xdomain_init(struct tb_xdomain *xd)
988 struct tb_switch *parent_sw;
989 struct tb_port *downstream;
991 parent_sw = tb_xdomain_parent(xd);
992 downstream = tb_port_at(xd->route, parent_sw);
994 margining_port_init(downstream);
997 static void margining_xdomain_remove(struct tb_xdomain *xd)
999 struct tb_switch *parent_sw;
1000 struct tb_port *downstream;
1002 parent_sw = tb_xdomain_parent(xd);
1003 downstream = tb_port_at(xd->route, parent_sw);
1004 margining_port_remove(downstream);
1007 static inline void margining_switch_init(struct tb_switch *sw) { }
1008 static inline void margining_switch_remove(struct tb_switch *sw) { }
1009 static inline void margining_xdomain_init(struct tb_xdomain *xd) { }
1010 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { }
1013 static int port_clear_all_counters(struct tb_port *port)
1018 buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32),
1023 ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0,
1024 COUNTER_SET_LEN * port->config.max_counters);
1030 static ssize_t counters_write(struct file *file, const char __user *user_buf,
1031 size_t count, loff_t *ppos)
1033 struct seq_file *s = file->private_data;
1034 struct tb_port *port = s->private;
1035 struct tb_switch *sw = port->sw;
1036 struct tb *tb = port->sw->tb;
1040 buf = validate_and_copy_from_user(user_buf, &count);
1042 return PTR_ERR(buf);
1044 pm_runtime_get_sync(&sw->dev);
1046 if (mutex_lock_interruptible(&tb->lock)) {
1051 /* If written delimiter only, clear all counters in one shot */
1052 if (buf[0] == '\n') {
1053 ret = port_clear_all_counters(port);
1059 while (parse_line(&line, &offset, &val, 1, 4)) {
1060 ret = tb_port_write(port, &val, TB_CFG_COUNTERS,
1067 mutex_unlock(&tb->lock);
1070 pm_runtime_mark_last_busy(&sw->dev);
1071 pm_runtime_put_autosuspend(&sw->dev);
1072 free_page((unsigned long)buf);
1074 return ret < 0 ? ret : count;
1077 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw,
1078 struct tb_port *port, unsigned int cap,
1079 unsigned int offset, u8 cap_id, u8 vsec_id,
1085 for (i = 0; i < dwords; i++) {
1087 ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1);
1089 ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1);
1091 seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i);
1095 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i,
1096 offset + i, cap_id, vsec_id, data);
1100 static void cap_show(struct seq_file *s, struct tb_switch *sw,
1101 struct tb_port *port, unsigned int cap, u8 cap_id,
1102 u8 vsec_id, int length)
1104 int ret, offset = 0;
1106 while (length > 0) {
1107 int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH);
1108 u32 data[TB_MAX_CONFIG_RW_LENGTH];
1111 ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset,
1114 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords);
1116 cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length);
1120 for (i = 0; i < dwords; i++) {
1121 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1122 cap + offset + i, offset + i,
1123 cap_id, vsec_id, data[i]);
1131 static void port_cap_show(struct tb_port *port, struct seq_file *s,
1134 struct tb_cap_any header;
1139 ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1);
1141 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1145 switch (header.basic.cap) {
1146 case TB_PORT_CAP_PHY:
1147 length = PORT_CAP_LANE_LEN;
1150 case TB_PORT_CAP_TIME1:
1151 length = PORT_CAP_TMU_LEN;
1154 case TB_PORT_CAP_POWER:
1155 length = PORT_CAP_POWER_LEN;
1158 case TB_PORT_CAP_ADAP:
1159 if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) {
1160 length = PORT_CAP_PCIE_LEN;
1161 } else if (tb_port_is_dpin(port) || tb_port_is_dpout(port)) {
1162 length = PORT_CAP_DP_LEN;
1163 } else if (tb_port_is_usb3_down(port) ||
1164 tb_port_is_usb3_up(port)) {
1165 length = PORT_CAP_USB3_LEN;
1167 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1168 cap, header.basic.cap);
1173 case TB_PORT_CAP_VSE:
1174 if (!header.extended_short.length) {
1175 ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT,
1178 seq_printf(s, "0x%04x <capability read failed>\n",
1182 length = header.extended_long.length;
1183 vsec_id = header.extended_short.vsec_id;
1185 length = header.extended_short.length;
1186 vsec_id = header.extended_short.vsec_id;
1190 case TB_PORT_CAP_USB4:
1191 length = PORT_CAP_USB4_LEN;
1195 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n",
1196 cap, header.basic.cap);
1200 cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length);
1203 static void port_caps_show(struct tb_port *port, struct seq_file *s)
1207 cap = tb_port_next_cap(port, 0);
1209 port_cap_show(port, s, cap);
1210 cap = tb_port_next_cap(port, cap);
1214 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s)
1216 u32 data[PORT_CAP_BASIC_LEN];
1219 ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data));
1223 for (i = 0; i < ARRAY_SIZE(data); i++)
1224 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1229 static int port_regs_show(struct seq_file *s, void *not_used)
1231 struct tb_port *port = s->private;
1232 struct tb_switch *sw = port->sw;
1233 struct tb *tb = sw->tb;
1236 pm_runtime_get_sync(&sw->dev);
1238 if (mutex_lock_interruptible(&tb->lock)) {
1243 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1245 ret = port_basic_regs_show(port, s);
1249 port_caps_show(port, s);
1252 mutex_unlock(&tb->lock);
1254 pm_runtime_mark_last_busy(&sw->dev);
1255 pm_runtime_put_autosuspend(&sw->dev);
1259 DEBUGFS_ATTR_RW(port_regs);
1261 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s,
1264 struct tb_cap_any header;
1268 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1);
1270 seq_printf(s, "0x%04x <capability read failed>\n", cap);
1274 if (header.basic.cap == TB_SWITCH_CAP_VSE) {
1275 if (!header.extended_short.length) {
1276 ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH,
1279 seq_printf(s, "0x%04x <capability read failed>\n",
1283 length = header.extended_long.length;
1285 length = header.extended_short.length;
1287 vsec_id = header.extended_short.vsec_id;
1289 if (header.basic.cap == TB_SWITCH_CAP_TMU) {
1290 length = SWITCH_CAP_TMU_LEN;
1292 seq_printf(s, "0x%04x <unknown capability 0x%02x>\n",
1293 cap, header.basic.cap);
1298 cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length);
1301 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s)
1305 cap = tb_switch_next_cap(sw, 0);
1307 switch_cap_show(sw, s, cap);
1308 cap = tb_switch_next_cap(sw, cap);
1312 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s)
1314 u32 data[SWITCH_CAP_BASIC_LEN];
1318 /* Only USB4 has the additional registers */
1319 if (tb_switch_is_usb4(sw))
1320 dwords = ARRAY_SIZE(data);
1324 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords);
1328 for (i = 0; i < dwords; i++)
1329 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]);
1334 static int switch_regs_show(struct seq_file *s, void *not_used)
1336 struct tb_switch *sw = s->private;
1337 struct tb *tb = sw->tb;
1340 pm_runtime_get_sync(&sw->dev);
1342 if (mutex_lock_interruptible(&tb->lock)) {
1347 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n");
1349 ret = switch_basic_regs_show(sw, s);
1353 switch_caps_show(sw, s);
1356 mutex_unlock(&tb->lock);
1358 pm_runtime_mark_last_busy(&sw->dev);
1359 pm_runtime_put_autosuspend(&sw->dev);
1363 DEBUGFS_ATTR_RW(switch_regs);
1365 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid)
1370 ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN,
1373 seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN);
1377 for (i = 0; i < ARRAY_SIZE(data); i++) {
1378 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1379 hopid * PATH_LEN + i, i, hopid, data[i]);
1385 static int path_show(struct seq_file *s, void *not_used)
1387 struct tb_port *port = s->private;
1388 struct tb_switch *sw = port->sw;
1389 struct tb *tb = sw->tb;
1390 int start, i, ret = 0;
1392 pm_runtime_get_sync(&sw->dev);
1394 if (mutex_lock_interruptible(&tb->lock)) {
1399 seq_puts(s, "# offset relative_offset in_hop_id value\n");
1401 /* NHI and lane adapters have entry for path 0 */
1402 if (tb_port_is_null(port) || tb_port_is_nhi(port)) {
1403 ret = path_show_one(port, s, 0);
1408 start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID;
1410 for (i = start; i <= port->config.max_in_hop_id; i++) {
1411 ret = path_show_one(port, s, i);
1417 mutex_unlock(&tb->lock);
1419 pm_runtime_mark_last_busy(&sw->dev);
1420 pm_runtime_put_autosuspend(&sw->dev);
1424 DEBUGFS_ATTR_RO(path);
1426 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s,
1429 u32 data[COUNTER_SET_LEN];
1432 ret = tb_port_read(port, data, TB_CFG_COUNTERS,
1433 counter * COUNTER_SET_LEN, ARRAY_SIZE(data));
1435 seq_printf(s, "0x%04x <not accessible>\n",
1436 counter * COUNTER_SET_LEN);
1440 for (i = 0; i < ARRAY_SIZE(data); i++) {
1441 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n",
1442 counter * COUNTER_SET_LEN + i, i, counter, data[i]);
1448 static int counters_show(struct seq_file *s, void *not_used)
1450 struct tb_port *port = s->private;
1451 struct tb_switch *sw = port->sw;
1452 struct tb *tb = sw->tb;
1455 pm_runtime_get_sync(&sw->dev);
1457 if (mutex_lock_interruptible(&tb->lock)) {
1462 seq_puts(s, "# offset relative_offset counter_id value\n");
1464 for (i = 0; i < port->config.max_counters; i++) {
1465 ret = counter_set_regs_show(port, s, i);
1470 mutex_unlock(&tb->lock);
1473 pm_runtime_mark_last_busy(&sw->dev);
1474 pm_runtime_put_autosuspend(&sw->dev);
1478 DEBUGFS_ATTR_RW(counters);
1481 * tb_switch_debugfs_init() - Add debugfs entries for router
1482 * @sw: Pointer to the router
1484 * Adds debugfs directories and files for given router.
1486 void tb_switch_debugfs_init(struct tb_switch *sw)
1488 struct dentry *debugfs_dir;
1489 struct tb_port *port;
1491 debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root);
1492 sw->debugfs_dir = debugfs_dir;
1493 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw,
1496 tb_switch_for_each_port(sw, port) {
1497 struct dentry *debugfs_dir;
1502 if (port->config.type == TB_TYPE_INACTIVE)
1505 snprintf(dir_name, sizeof(dir_name), "port%d", port->port);
1506 debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir);
1507 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir,
1508 port, &port_regs_fops);
1509 debugfs_create_file("path", 0400, debugfs_dir, port,
1511 if (port->config.counters_support)
1512 debugfs_create_file("counters", 0600, debugfs_dir, port,
1516 margining_switch_init(sw);
1520 * tb_switch_debugfs_remove() - Remove all router debugfs entries
1521 * @sw: Pointer to the router
1523 * Removes all previously added debugfs entries under this router.
1525 void tb_switch_debugfs_remove(struct tb_switch *sw)
1527 margining_switch_remove(sw);
1528 debugfs_remove_recursive(sw->debugfs_dir);
1531 void tb_xdomain_debugfs_init(struct tb_xdomain *xd)
1533 margining_xdomain_init(xd);
1536 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd)
1538 margining_xdomain_remove(xd);
1542 * tb_service_debugfs_init() - Add debugfs directory for service
1543 * @svc: Thunderbolt service pointer
1545 * Adds debugfs directory for service.
1547 void tb_service_debugfs_init(struct tb_service *svc)
1549 svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev),
1554 * tb_service_debugfs_remove() - Remove service debugfs directory
1555 * @svc: Thunderbolt service pointer
1557 * Removes the previously created debugfs directory for @svc.
1559 void tb_service_debugfs_remove(struct tb_service *svc)
1561 debugfs_remove_recursive(svc->debugfs_dir);
1562 svc->debugfs_dir = NULL;
1565 void tb_debugfs_init(void)
1567 tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL);
1570 void tb_debugfs_exit(void)
1572 debugfs_remove_recursive(tb_debugfs_root);