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/bitfield.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/uaccess.h>
19 #define PORT_CAP_V1_PCIE_LEN 1
20 #define PORT_CAP_V2_PCIE_LEN 2
21 #define PORT_CAP_POWER_LEN 2
22 #define PORT_CAP_LANE_LEN 3
23 #define PORT_CAP_USB3_LEN 5
24 #define PORT_CAP_DP_V1_LEN 9
25 #define PORT_CAP_DP_V2_LEN 14
26 #define PORT_CAP_TMU_V1_LEN 8
27 #define PORT_CAP_TMU_V2_LEN 10
28 #define PORT_CAP_BASIC_LEN 9
29 #define PORT_CAP_USB4_LEN 20
31 #define SWITCH_CAP_TMU_LEN 26
32 #define SWITCH_CAP_BASIC_LEN 27
36 #define COUNTER_SET_LEN 3
39 * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms
40 * probed to give good results.
42 #define MIN_DWELL_TIME 100 /* ms */
43 #define MAX_DWELL_TIME 500 /* ms */
44 #define DWELL_SAMPLE_INTERVAL 10
46 /* Sideband registers and their sizes as defined in the USB4 spec */
52 #define SB_MAX_SIZE 64
54 /* Sideband registers for router */
55 static const struct sb_reg port_sb_regs
[] = {
56 { USB4_SB_VENDOR_ID
, 4 },
57 { USB4_SB_PRODUCT_ID
, 4 },
58 { USB4_SB_DEBUG_CONF
, 4 },
59 { USB4_SB_DEBUG
, 54 },
60 { USB4_SB_LRD_TUNING
, 4 },
61 { USB4_SB_OPCODE
, 4 },
62 { USB4_SB_METADATA
, 4 },
63 { USB4_SB_LINK_CONF
, 3 },
64 { USB4_SB_GEN23_TXFFE
, 4 },
65 { USB4_SB_GEN4_TXFFE
, 4 },
66 { USB4_SB_VERSION
, 4 },
70 /* Sideband registers for retimer */
71 static const struct sb_reg retimer_sb_regs
[] = {
72 { USB4_SB_VENDOR_ID
, 4 },
73 { USB4_SB_PRODUCT_ID
, 4 },
74 { USB4_SB_FW_VERSION
, 4 },
75 { USB4_SB_LRD_TUNING
, 4 },
76 { USB4_SB_OPCODE
, 4 },
77 { USB4_SB_METADATA
, 4 },
78 { USB4_SB_GEN23_TXFFE
, 4 },
79 { USB4_SB_GEN4_TXFFE
, 4 },
80 { USB4_SB_VERSION
, 4 },
84 #define DEBUGFS_ATTR(__space, __write) \
85 static int __space ## _open(struct inode *inode, struct file *file) \
87 return single_open(file, __space ## _show, inode->i_private); \
90 static const struct file_operations __space ## _fops = { \
91 .owner = THIS_MODULE, \
92 .open = __space ## _open, \
93 .release = single_release, \
96 .llseek = seq_lseek, \
99 #define DEBUGFS_ATTR_RO(__space) \
100 DEBUGFS_ATTR(__space, NULL)
102 #define DEBUGFS_ATTR_RW(__space) \
103 DEBUGFS_ATTR(__space, __space ## _write)
105 static struct dentry
*tb_debugfs_root
;
107 static void *validate_and_copy_from_user(const void __user
*user_buf
,
114 return ERR_PTR(-EINVAL
);
116 if (!access_ok(user_buf
, *count
))
117 return ERR_PTR(-EFAULT
);
119 buf
= (void *)get_zeroed_page(GFP_KERNEL
);
121 return ERR_PTR(-ENOMEM
);
123 nbytes
= min_t(size_t, *count
, PAGE_SIZE
);
124 if (copy_from_user(buf
, user_buf
, nbytes
)) {
125 free_page((unsigned long)buf
);
126 return ERR_PTR(-EFAULT
);
133 static bool parse_line(char **line
, u32
*offs
, u32
*val
, int short_fmt_len
,
140 token
= strsep(line
, "\n");
145 * For Adapter/Router configuration space:
146 * Short format is: offset value\n
148 * Long format as produced from the read side:
149 * offset relative_offset cap_id vs_cap_id value\n
150 * v[0] v[1] v[2] v[3] v[4]
152 * For Counter configuration space:
153 * Short format is: offset\n
155 * Long format as produced from the read side:
156 * offset relative_offset counter_id value\n
157 * v[0] v[1] v[2] v[3]
159 ret
= sscanf(token
, "%i %i %i %i %i", &v
[0], &v
[1], &v
[2], &v
[3], &v
[4]);
160 /* In case of Counters, clear counter, "val" content is NA */
161 if (ret
== short_fmt_len
) {
163 *val
= v
[short_fmt_len
- 1];
165 } else if (ret
== long_fmt_len
) {
167 *val
= v
[long_fmt_len
- 1];
174 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
175 static ssize_t
regs_write(struct tb_switch
*sw
, struct tb_port
*port
,
176 const char __user
*user_buf
, size_t count
,
179 struct tb
*tb
= sw
->tb
;
184 buf
= validate_and_copy_from_user(user_buf
, &count
);
188 pm_runtime_get_sync(&sw
->dev
);
190 if (mutex_lock_interruptible(&tb
->lock
)) {
195 /* User did hardware changes behind the driver's back */
196 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
199 while (parse_line(&line
, &offset
, &val
, 2, 5)) {
201 ret
= tb_port_write(port
, &val
, TB_CFG_PORT
, offset
, 1);
203 ret
= tb_sw_write(sw
, &val
, TB_CFG_SWITCH
, offset
, 1);
208 mutex_unlock(&tb
->lock
);
211 pm_runtime_mark_last_busy(&sw
->dev
);
212 pm_runtime_put_autosuspend(&sw
->dev
);
213 free_page((unsigned long)buf
);
215 return ret
< 0 ? ret
: count
;
218 static ssize_t
port_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_port
*port
= s
->private;
224 return regs_write(port
->sw
, port
, user_buf
, count
, ppos
);
227 static ssize_t
switch_regs_write(struct file
*file
, const char __user
*user_buf
,
228 size_t count
, loff_t
*ppos
)
230 struct seq_file
*s
= file
->private_data
;
231 struct tb_switch
*sw
= s
->private;
233 return regs_write(sw
, NULL
, user_buf
, count
, ppos
);
236 static bool parse_sb_line(char **line
, u8
*reg
, u8
*data
, size_t data_size
,
242 token
= strsep(line
, "\n");
246 /* Parse the register first */
247 field
= strsep(&token
, " ");
250 if (kstrtou8(field
, 0, reg
))
253 /* Then the values for the register, up to data_size */
254 for (i
= 0; i
< data_size
; i
++) {
255 field
= strsep(&token
, " ");
258 if (kstrtou8(field
, 0, &data
[i
]))
266 static ssize_t
sb_regs_write(struct tb_port
*port
, const struct sb_reg
*sb_regs
,
267 size_t size
, enum usb4_sb_target target
, u8 index
,
268 char *buf
, size_t count
, loff_t
*ppos
)
270 u8 reg
, data
[SB_MAX_SIZE
];
274 /* User did hardware changes behind the driver's back */
275 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
278 * For sideband registers we accept:
281 * Here "reg" is the byte offset of the sideband register and "b0"..
282 * are the byte values. There can be less byte values than the register
283 * size. The leftovers will not be overwritten.
285 while (parse_sb_line(&line
, ®
, data
, ARRAY_SIZE(data
), &bytes_read
)) {
286 const struct sb_reg
*sb_reg
;
289 /* At least one byte must be passed */
293 /* Find the register */
295 for (int i
= 0; i
< size
; i
++) {
296 if (sb_regs
[i
].reg
== reg
) {
297 sb_reg
= &sb_regs
[i
];
305 if (bytes_read
> sb_regs
->size
)
308 ret
= usb4_port_sb_write(port
, target
, index
, sb_reg
->reg
, data
,
317 static ssize_t
port_sb_regs_write(struct file
*file
, const char __user
*user_buf
,
318 size_t count
, loff_t
*ppos
)
320 struct seq_file
*s
= file
->private_data
;
321 struct tb_port
*port
= s
->private;
322 struct tb_switch
*sw
= port
->sw
;
323 struct tb
*tb
= sw
->tb
;
327 buf
= validate_and_copy_from_user(user_buf
, &count
);
331 pm_runtime_get_sync(&sw
->dev
);
333 if (mutex_lock_interruptible(&tb
->lock
)) {
338 ret
= sb_regs_write(port
, port_sb_regs
, ARRAY_SIZE(port_sb_regs
),
339 USB4_SB_TARGET_ROUTER
, 0, buf
, count
, ppos
);
341 mutex_unlock(&tb
->lock
);
343 pm_runtime_mark_last_busy(&sw
->dev
);
344 pm_runtime_put_autosuspend(&sw
->dev
);
345 free_page((unsigned long)buf
);
347 return ret
< 0 ? ret
: count
;
350 static ssize_t
retimer_sb_regs_write(struct file
*file
,
351 const char __user
*user_buf
,
352 size_t count
, loff_t
*ppos
)
354 struct seq_file
*s
= file
->private_data
;
355 struct tb_retimer
*rt
= s
->private;
356 struct tb
*tb
= rt
->tb
;
360 buf
= validate_and_copy_from_user(user_buf
, &count
);
364 pm_runtime_get_sync(&rt
->dev
);
366 if (mutex_lock_interruptible(&tb
->lock
)) {
371 ret
= sb_regs_write(rt
->port
, retimer_sb_regs
, ARRAY_SIZE(retimer_sb_regs
),
372 USB4_SB_TARGET_RETIMER
, rt
->index
, buf
, count
, ppos
);
374 mutex_unlock(&tb
->lock
);
376 pm_runtime_mark_last_busy(&rt
->dev
);
377 pm_runtime_put_autosuspend(&rt
->dev
);
378 free_page((unsigned long)buf
);
380 return ret
< 0 ? ret
: count
;
382 #define DEBUGFS_MODE 0600
384 #define port_regs_write NULL
385 #define switch_regs_write NULL
386 #define port_sb_regs_write NULL
387 #define retimer_sb_regs_write NULL
388 #define DEBUGFS_MODE 0400
391 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
393 * struct tb_margining - Lane margining support
394 * @port: USB4 port through which the margining operations are run
395 * @target: Sideband target
396 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
397 * @dev: Pointer to the device that is the target (USB4 port or retimer)
398 * @caps: Port lane margining capabilities
399 * @results: Last lane margining results
400 * @lanes: %0, %1 or %7 (all)
401 * @min_ber_level: Minimum supported BER level contour value
402 * @max_ber_level: Maximum supported BER level contour value
403 * @ber_level: Current BER level contour value
404 * @voltage_steps: Number of mandatory voltage steps
405 * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
406 * @voltage_steps_optional_range: Number of voltage steps for optional range
407 * @max_voltage_offset_optional_range: Maximum voltage offset for the optional
409 * @time_steps: Number of time margin steps
410 * @max_time_offset: Maximum time margin offset (in mUI)
411 * @voltage_time_offset: Offset for voltage / time for software margining
412 * @dwell_time: Dwell time for software margining (in ms)
413 * @error_counter: Error counter operation for software margining
414 * @optional_voltage_offset_range: Enable optional extended voltage range
415 * @software: %true if software margining is used instead of hardware
416 * @time: %true if time margining is used instead of voltage
417 * @right_high: %false if left/low margin test is performed, %true if
420 struct tb_margining
{
421 struct tb_port
*port
;
422 enum usb4_sb_target target
;
428 unsigned int min_ber_level
;
429 unsigned int max_ber_level
;
430 unsigned int ber_level
;
431 unsigned int voltage_steps
;
432 unsigned int max_voltage_offset
;
433 unsigned int voltage_steps_optional_range
;
434 unsigned int max_voltage_offset_optional_range
;
435 unsigned int time_steps
;
436 unsigned int max_time_offset
;
437 unsigned int voltage_time_offset
;
438 unsigned int dwell_time
;
439 enum usb4_margin_sw_error_counter error_counter
;
440 bool optional_voltage_offset_range
;
446 static int margining_modify_error_counter(struct tb_margining
*margining
,
447 u32 lanes
, enum usb4_margin_sw_error_counter error_counter
)
449 struct usb4_port_margining_params params
= { 0 };
450 struct tb_port
*port
= margining
->port
;
453 if (error_counter
!= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
&&
454 error_counter
!= USB4_MARGIN_SW_ERROR_COUNTER_STOP
)
457 params
.error_counter
= error_counter
;
458 params
.lanes
= lanes
;
460 return usb4_port_sw_margin(port
, margining
->target
, margining
->index
,
464 static bool supports_software(const struct tb_margining
*margining
)
466 return margining
->caps
[0] & USB4_MARGIN_CAP_0_MODES_SW
;
469 static bool supports_hardware(const struct tb_margining
*margining
)
471 return margining
->caps
[0] & USB4_MARGIN_CAP_0_MODES_HW
;
474 static bool both_lanes(const struct tb_margining
*margining
)
476 return margining
->caps
[0] & USB4_MARGIN_CAP_0_2_LANES
;
480 independent_voltage_margins(const struct tb_margining
*margining
)
482 return FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK
, margining
->caps
[0]);
485 static bool supports_time(const struct tb_margining
*margining
)
487 return margining
->caps
[0] & USB4_MARGIN_CAP_0_TIME
;
490 /* Only applicable if supports_time() returns true */
492 independent_time_margins(const struct tb_margining
*margining
)
494 return FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK
, margining
->caps
[1]);
498 supports_optional_voltage_offset_range(const struct tb_margining
*margining
)
500 return margining
->caps
[0] & USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT
;
504 margining_ber_level_write(struct file
*file
, const char __user
*user_buf
,
505 size_t count
, loff_t
*ppos
)
507 struct seq_file
*s
= file
->private_data
;
508 struct tb_margining
*margining
= s
->private;
509 struct tb
*tb
= margining
->port
->sw
->tb
;
514 if (mutex_lock_interruptible(&tb
->lock
))
517 if (margining
->software
) {
522 buf
= validate_and_copy_from_user(user_buf
, &count
);
528 buf
[count
- 1] = '\0';
530 ret
= kstrtouint(buf
, 10, &val
);
534 if (val
< margining
->min_ber_level
||
535 val
> margining
->max_ber_level
) {
540 margining
->ber_level
= val
;
543 free_page((unsigned long)buf
);
545 mutex_unlock(&tb
->lock
);
547 return ret
< 0 ? ret
: count
;
550 static void ber_level_show(struct seq_file
*s
, unsigned int val
)
553 seq_printf(s
, "3 * 1e%d (%u)\n", -12 + (val
+ 1) / 2, val
);
555 seq_printf(s
, "1e%d (%u)\n", -12 + val
/ 2, val
);
558 static int margining_ber_level_show(struct seq_file
*s
, void *not_used
)
560 const struct tb_margining
*margining
= s
->private;
562 if (margining
->software
)
564 ber_level_show(s
, margining
->ber_level
);
567 DEBUGFS_ATTR_RW(margining_ber_level
);
569 static int margining_caps_show(struct seq_file
*s
, void *not_used
)
571 struct tb_margining
*margining
= s
->private;
572 struct tb
*tb
= margining
->port
->sw
->tb
;
575 if (mutex_lock_interruptible(&tb
->lock
))
578 /* Dump the raw caps first */
579 cap0
= margining
->caps
[0];
580 seq_printf(s
, "0x%08x\n", cap0
);
581 cap1
= margining
->caps
[1];
582 seq_printf(s
, "0x%08x\n", cap1
);
584 seq_printf(s
, "# software margining: %s\n",
585 supports_software(margining
) ? "yes" : "no");
586 if (supports_hardware(margining
)) {
587 seq_puts(s
, "# hardware margining: yes\n");
588 seq_puts(s
, "# minimum BER level contour: ");
589 ber_level_show(s
, margining
->min_ber_level
);
590 seq_puts(s
, "# maximum BER level contour: ");
591 ber_level_show(s
, margining
->max_ber_level
);
593 seq_puts(s
, "# hardware margining: no\n");
596 seq_printf(s
, "# both lanes simultaneously: %s\n",
597 both_lanes(margining
) ? "yes" : "no");
598 seq_printf(s
, "# voltage margin steps: %u\n",
599 margining
->voltage_steps
);
600 seq_printf(s
, "# maximum voltage offset: %u mV\n",
601 margining
->max_voltage_offset
);
602 seq_printf(s
, "# optional voltage offset range support: %s\n",
603 str_yes_no(supports_optional_voltage_offset_range(margining
)));
604 if (supports_optional_voltage_offset_range(margining
)) {
605 seq_printf(s
, "# voltage margin steps, optional range: %u\n",
606 margining
->voltage_steps_optional_range
);
607 seq_printf(s
, "# maximum voltage offset, optional range: %u mV\n",
608 margining
->max_voltage_offset_optional_range
);
611 switch (independent_voltage_margins(margining
)) {
612 case USB4_MARGIN_CAP_0_VOLTAGE_MIN
:
613 seq_puts(s
, "# returns minimum between high and low voltage margins\n");
615 case USB4_MARGIN_CAP_0_VOLTAGE_HL
:
616 seq_puts(s
, "# returns high or low voltage margin\n");
618 case USB4_MARGIN_CAP_0_VOLTAGE_BOTH
:
619 seq_puts(s
, "# returns both high and low margins\n");
623 if (supports_time(margining
)) {
624 seq_puts(s
, "# time margining: yes\n");
625 seq_printf(s
, "# time margining is destructive: %s\n",
626 cap1
& USB4_MARGIN_CAP_1_TIME_DESTR
? "yes" : "no");
628 switch (independent_time_margins(margining
)) {
629 case USB4_MARGIN_CAP_1_TIME_MIN
:
630 seq_puts(s
, "# returns minimum between left and right time margins\n");
632 case USB4_MARGIN_CAP_1_TIME_LR
:
633 seq_puts(s
, "# returns left or right margin\n");
635 case USB4_MARGIN_CAP_1_TIME_BOTH
:
636 seq_puts(s
, "# returns both left and right margins\n");
640 seq_printf(s
, "# time margin steps: %u\n",
641 margining
->time_steps
);
642 seq_printf(s
, "# maximum time offset: %u mUI\n",
643 margining
->max_time_offset
);
645 seq_puts(s
, "# time margining: no\n");
648 mutex_unlock(&tb
->lock
);
651 DEBUGFS_ATTR_RO(margining_caps
);
654 margining_lanes_write(struct file
*file
, const char __user
*user_buf
,
655 size_t count
, loff_t
*ppos
)
657 struct seq_file
*s
= file
->private_data
;
658 struct tb_margining
*margining
= s
->private;
659 struct tb
*tb
= margining
->port
->sw
->tb
;
663 buf
= validate_and_copy_from_user(user_buf
, &count
);
667 buf
[count
- 1] = '\0';
669 if (mutex_lock_interruptible(&tb
->lock
)) {
674 if (!strcmp(buf
, "0")) {
675 margining
->lanes
= 0;
676 } else if (!strcmp(buf
, "1")) {
677 margining
->lanes
= 1;
678 } else if (!strcmp(buf
, "all")) {
679 /* Needs to be supported */
680 if (both_lanes(margining
))
681 margining
->lanes
= 7;
688 mutex_unlock(&tb
->lock
);
691 free_page((unsigned long)buf
);
692 return ret
< 0 ? ret
: count
;
695 static int margining_lanes_show(struct seq_file
*s
, void *not_used
)
697 struct tb_margining
*margining
= s
->private;
698 struct tb
*tb
= margining
->port
->sw
->tb
;
701 if (mutex_lock_interruptible(&tb
->lock
))
704 lanes
= margining
->lanes
;
705 if (both_lanes(margining
)) {
707 seq_puts(s
, "[0] 1 all\n");
709 seq_puts(s
, "0 [1] all\n");
711 seq_puts(s
, "0 1 [all]\n");
714 seq_puts(s
, "[0] 1\n");
716 seq_puts(s
, "0 [1]\n");
719 mutex_unlock(&tb
->lock
);
722 DEBUGFS_ATTR_RW(margining_lanes
);
725 margining_voltage_time_offset_write(struct file
*file
,
726 const char __user
*user_buf
,
727 size_t count
, loff_t
*ppos
)
729 struct seq_file
*s
= file
->private_data
;
730 struct tb_margining
*margining
= s
->private;
731 struct tb
*tb
= margining
->port
->sw
->tb
;
732 unsigned int max_margin
;
736 ret
= kstrtouint_from_user(user_buf
, count
, 10, &val
);
740 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
741 if (!margining
->software
)
745 max_margin
= margining
->time_steps
;
747 if (margining
->optional_voltage_offset_range
)
748 max_margin
= margining
->voltage_steps_optional_range
;
750 max_margin
= margining
->voltage_steps
;
752 margining
->voltage_time_offset
= clamp(val
, 0, max_margin
);
758 static int margining_voltage_time_offset_show(struct seq_file
*s
,
761 const struct tb_margining
*margining
= s
->private;
762 struct tb
*tb
= margining
->port
->sw
->tb
;
764 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
765 if (!margining
->software
)
768 seq_printf(s
, "%d\n", margining
->voltage_time_offset
);
773 DEBUGFS_ATTR_RW(margining_voltage_time_offset
);
776 margining_error_counter_write(struct file
*file
, const char __user
*user_buf
,
777 size_t count
, loff_t
*ppos
)
779 enum usb4_margin_sw_error_counter error_counter
;
780 struct seq_file
*s
= file
->private_data
;
781 struct tb_margining
*margining
= s
->private;
782 struct tb
*tb
= margining
->port
->sw
->tb
;
785 buf
= validate_and_copy_from_user(user_buf
, &count
);
789 buf
[count
- 1] = '\0';
791 if (!strcmp(buf
, "nop"))
792 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_NOP
;
793 else if (!strcmp(buf
, "clear"))
794 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
;
795 else if (!strcmp(buf
, "start"))
796 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_START
;
797 else if (!strcmp(buf
, "stop"))
798 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_STOP
;
802 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
803 if (!margining
->software
)
806 margining
->error_counter
= error_counter
;
812 static int margining_error_counter_show(struct seq_file
*s
, void *not_used
)
814 const struct tb_margining
*margining
= s
->private;
815 struct tb
*tb
= margining
->port
->sw
->tb
;
817 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
818 if (!margining
->software
)
821 switch (margining
->error_counter
) {
822 case USB4_MARGIN_SW_ERROR_COUNTER_NOP
:
823 seq_puts(s
, "[nop] clear start stop\n");
825 case USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
:
826 seq_puts(s
, "nop [clear] start stop\n");
828 case USB4_MARGIN_SW_ERROR_COUNTER_START
:
829 seq_puts(s
, "nop clear [start] stop\n");
831 case USB4_MARGIN_SW_ERROR_COUNTER_STOP
:
832 seq_puts(s
, "nop clear start [stop]\n");
839 DEBUGFS_ATTR_RW(margining_error_counter
);
842 margining_dwell_time_write(struct file
*file
, const char __user
*user_buf
,
843 size_t count
, loff_t
*ppos
)
845 struct seq_file
*s
= file
->private_data
;
846 struct tb_margining
*margining
= s
->private;
847 struct tb
*tb
= margining
->port
->sw
->tb
;
851 ret
= kstrtouint_from_user(user_buf
, count
, 10, &val
);
855 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
856 if (!margining
->software
)
859 margining
->dwell_time
= clamp(val
, MIN_DWELL_TIME
, MAX_DWELL_TIME
);
865 static int margining_dwell_time_show(struct seq_file
*s
, void *not_used
)
867 struct tb_margining
*margining
= s
->private;
868 struct tb
*tb
= margining
->port
->sw
->tb
;
870 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
871 if (!margining
->software
)
874 seq_printf(s
, "%d\n", margining
->dwell_time
);
879 DEBUGFS_ATTR_RW(margining_dwell_time
);
882 margining_optional_voltage_offset_write(struct file
*file
, const char __user
*user_buf
,
883 size_t count
, loff_t
*ppos
)
885 struct seq_file
*s
= file
->private_data
;
886 struct tb_margining
*margining
= s
->private;
887 struct tb
*tb
= margining
->port
->sw
->tb
;
891 ret
= kstrtobool_from_user(user_buf
, count
, &val
);
895 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
896 margining
->optional_voltage_offset_range
= val
;
902 static int margining_optional_voltage_offset_show(struct seq_file
*s
,
905 struct tb_margining
*margining
= s
->private;
906 struct tb
*tb
= margining
->port
->sw
->tb
;
908 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
909 seq_printf(s
, "%u\n", margining
->optional_voltage_offset_range
);
914 DEBUGFS_ATTR_RW(margining_optional_voltage_offset
);
916 static ssize_t
margining_mode_write(struct file
*file
,
917 const char __user
*user_buf
,
918 size_t count
, loff_t
*ppos
)
920 struct seq_file
*s
= file
->private_data
;
921 struct tb_margining
*margining
= s
->private;
922 struct tb
*tb
= margining
->port
->sw
->tb
;
926 buf
= validate_and_copy_from_user(user_buf
, &count
);
930 buf
[count
- 1] = '\0';
932 if (mutex_lock_interruptible(&tb
->lock
)) {
937 if (!strcmp(buf
, "software")) {
938 if (supports_software(margining
))
939 margining
->software
= true;
942 } else if (!strcmp(buf
, "hardware")) {
943 if (supports_hardware(margining
))
944 margining
->software
= false;
951 mutex_unlock(&tb
->lock
);
954 free_page((unsigned long)buf
);
955 return ret
? ret
: count
;
958 static int margining_mode_show(struct seq_file
*s
, void *not_used
)
960 struct tb_margining
*margining
= s
->private;
961 struct tb
*tb
= margining
->port
->sw
->tb
;
962 const char *space
= "";
964 if (mutex_lock_interruptible(&tb
->lock
))
967 if (supports_software(margining
)) {
968 if (margining
->software
)
969 seq_puts(s
, "[software]");
971 seq_puts(s
, "software");
974 if (supports_hardware(margining
)) {
975 if (margining
->software
)
976 seq_printf(s
, "%shardware", space
);
978 seq_printf(s
, "%s[hardware]", space
);
981 mutex_unlock(&tb
->lock
);
986 DEBUGFS_ATTR_RW(margining_mode
);
988 static int margining_run_sw(struct tb_margining
*margining
,
989 struct usb4_port_margining_params
*params
)
991 u32 nsamples
= margining
->dwell_time
/ DWELL_SAMPLE_INTERVAL
;
994 ret
= usb4_port_sw_margin(margining
->port
, margining
->target
, margining
->index
,
995 params
, margining
->results
);
999 for (i
= 0; i
<= nsamples
; i
++) {
1002 ret
= usb4_port_sw_margin_errors(margining
->port
, margining
->target
,
1003 margining
->index
, &margining
->results
[1]);
1007 if (margining
->lanes
== USB4_MARGIN_SW_LANE_0
)
1008 errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK
,
1009 margining
->results
[1]);
1010 else if (margining
->lanes
== USB4_MARGIN_SW_LANE_1
)
1011 errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK
,
1012 margining
->results
[1]);
1013 else if (margining
->lanes
== USB4_MARGIN_SW_ALL_LANES
)
1014 errors
= margining
->results
[1];
1016 /* Any errors stop the test */
1020 fsleep(DWELL_SAMPLE_INTERVAL
* USEC_PER_MSEC
);
1025 * Stop the counters but don't clear them to allow the
1026 * different error counter configurations.
1028 margining_modify_error_counter(margining
, margining
->lanes
,
1029 USB4_MARGIN_SW_ERROR_COUNTER_STOP
);
1033 static int margining_run_write(void *data
, u64 val
)
1035 struct tb_margining
*margining
= data
;
1036 struct tb_port
*port
= margining
->port
;
1037 struct device
*dev
= margining
->dev
;
1038 struct tb_switch
*sw
= port
->sw
;
1039 struct tb_switch
*down_sw
;
1040 struct tb
*tb
= sw
->tb
;
1046 pm_runtime_get_sync(dev
);
1048 if (mutex_lock_interruptible(&tb
->lock
)) {
1053 if (tb_is_upstream_port(port
))
1055 else if (port
->remote
)
1056 down_sw
= port
->remote
->sw
;
1062 * CL states may interfere with lane margining so
1063 * disable them temporarily now.
1065 ret
= tb_switch_clx_disable(down_sw
);
1067 tb_sw_warn(down_sw
, "failed to disable CL states\n");
1073 /* Clear the results */
1074 memset(margining
->results
, 0, sizeof(margining
->results
));
1076 if (margining
->software
) {
1077 struct usb4_port_margining_params params
= {
1078 .error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
,
1079 .lanes
= margining
->lanes
,
1080 .time
= margining
->time
,
1081 .voltage_time_offset
= margining
->voltage_time_offset
,
1082 .right_high
= margining
->right_high
,
1083 .optional_voltage_offset_range
= margining
->optional_voltage_offset_range
,
1087 "running software %s lane margining for %s lanes %u\n",
1088 margining
->time
? "time" : "voltage", dev_name(dev
),
1091 ret
= margining_run_sw(margining
, ¶ms
);
1093 struct usb4_port_margining_params params
= {
1094 .ber_level
= margining
->ber_level
,
1095 .lanes
= margining
->lanes
,
1096 .time
= margining
->time
,
1097 .right_high
= margining
->right_high
,
1098 .optional_voltage_offset_range
= margining
->optional_voltage_offset_range
,
1102 "running hardware %s lane margining for %s lanes %u\n",
1103 margining
->time
? "time" : "voltage", dev_name(dev
),
1106 ret
= usb4_port_hw_margin(port
, margining
->target
, margining
->index
, ¶ms
,
1107 margining
->results
);
1111 tb_switch_clx_enable(down_sw
, clx
);
1113 mutex_unlock(&tb
->lock
);
1115 pm_runtime_mark_last_busy(dev
);
1116 pm_runtime_put_autosuspend(dev
);
1120 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops
, NULL
, margining_run_write
,
1123 static ssize_t
margining_results_write(struct file
*file
,
1124 const char __user
*user_buf
,
1125 size_t count
, loff_t
*ppos
)
1127 struct seq_file
*s
= file
->private_data
;
1128 struct tb_margining
*margining
= s
->private;
1129 struct tb
*tb
= margining
->port
->sw
->tb
;
1131 if (mutex_lock_interruptible(&tb
->lock
))
1132 return -ERESTARTSYS
;
1134 /* Just clear the results */
1135 margining
->results
[0] = 0;
1136 margining
->results
[1] = 0;
1138 if (margining
->software
) {
1139 /* Clear the error counters */
1140 margining_modify_error_counter(margining
,
1141 USB4_MARGIN_SW_ALL_LANES
,
1142 USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
);
1145 mutex_unlock(&tb
->lock
);
1149 static void voltage_margin_show(struct seq_file
*s
,
1150 const struct tb_margining
*margining
, u8 val
)
1152 unsigned int tmp
, voltage
;
1154 tmp
= FIELD_GET(USB4_MARGIN_HW_RES_1_MARGIN_MASK
, val
);
1155 voltage
= tmp
* margining
->max_voltage_offset
/ margining
->voltage_steps
;
1156 seq_printf(s
, "%u mV (%u)", voltage
, tmp
);
1157 if (val
& USB4_MARGIN_HW_RES_1_EXCEEDS
)
1158 seq_puts(s
, " exceeds maximum");
1160 if (margining
->optional_voltage_offset_range
)
1161 seq_puts(s
, " optional voltage offset range enabled\n");
1164 static void time_margin_show(struct seq_file
*s
,
1165 const struct tb_margining
*margining
, u8 val
)
1167 unsigned int tmp
, interval
;
1169 tmp
= FIELD_GET(USB4_MARGIN_HW_RES_1_MARGIN_MASK
, val
);
1170 interval
= tmp
* margining
->max_time_offset
/ margining
->time_steps
;
1171 seq_printf(s
, "%u mUI (%u)", interval
, tmp
);
1172 if (val
& USB4_MARGIN_HW_RES_1_EXCEEDS
)
1173 seq_puts(s
, " exceeds maximum");
1177 static int margining_results_show(struct seq_file
*s
, void *not_used
)
1179 struct tb_margining
*margining
= s
->private;
1180 struct tb
*tb
= margining
->port
->sw
->tb
;
1182 if (mutex_lock_interruptible(&tb
->lock
))
1183 return -ERESTARTSYS
;
1185 /* Dump the raw results first */
1186 seq_printf(s
, "0x%08x\n", margining
->results
[0]);
1187 /* Only the hardware margining has two result dwords */
1188 if (!margining
->software
) {
1191 seq_printf(s
, "0x%08x\n", margining
->results
[1]);
1193 if (margining
->time
) {
1194 if (!margining
->lanes
|| margining
->lanes
== 7) {
1195 val
= margining
->results
[1];
1196 seq_puts(s
, "# lane 0 right time margin: ");
1197 time_margin_show(s
, margining
, val
);
1198 val
= margining
->results
[1] >>
1199 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT
;
1200 seq_puts(s
, "# lane 0 left time margin: ");
1201 time_margin_show(s
, margining
, val
);
1203 if (margining
->lanes
== 1 || margining
->lanes
== 7) {
1204 val
= margining
->results
[1] >>
1205 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT
;
1206 seq_puts(s
, "# lane 1 right time margin: ");
1207 time_margin_show(s
, margining
, val
);
1208 val
= margining
->results
[1] >>
1209 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT
;
1210 seq_puts(s
, "# lane 1 left time margin: ");
1211 time_margin_show(s
, margining
, val
);
1214 if (!margining
->lanes
|| margining
->lanes
== 7) {
1215 val
= margining
->results
[1];
1216 seq_puts(s
, "# lane 0 high voltage margin: ");
1217 voltage_margin_show(s
, margining
, val
);
1218 val
= margining
->results
[1] >>
1219 USB4_MARGIN_HW_RES_1_L0_LL_MARGIN_SHIFT
;
1220 seq_puts(s
, "# lane 0 low voltage margin: ");
1221 voltage_margin_show(s
, margining
, val
);
1223 if (margining
->lanes
== 1 || margining
->lanes
== 7) {
1224 val
= margining
->results
[1] >>
1225 USB4_MARGIN_HW_RES_1_L1_RH_MARGIN_SHIFT
;
1226 seq_puts(s
, "# lane 1 high voltage margin: ");
1227 voltage_margin_show(s
, margining
, val
);
1228 val
= margining
->results
[1] >>
1229 USB4_MARGIN_HW_RES_1_L1_LL_MARGIN_SHIFT
;
1230 seq_puts(s
, "# lane 1 low voltage margin: ");
1231 voltage_margin_show(s
, margining
, val
);
1235 u32 lane_errors
, result
;
1237 seq_printf(s
, "0x%08x\n", margining
->results
[1]);
1238 result
= FIELD_GET(USB4_MARGIN_SW_LANES_MASK
, margining
->results
[0]);
1240 if (result
== USB4_MARGIN_SW_LANE_0
||
1241 result
== USB4_MARGIN_SW_ALL_LANES
) {
1242 lane_errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK
,
1243 margining
->results
[1]);
1244 seq_printf(s
, "# lane 0 errors: %u\n", lane_errors
);
1246 if (result
== USB4_MARGIN_SW_LANE_1
||
1247 result
== USB4_MARGIN_SW_ALL_LANES
) {
1248 lane_errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK
,
1249 margining
->results
[1]);
1250 seq_printf(s
, "# lane 1 errors: %u\n", lane_errors
);
1254 mutex_unlock(&tb
->lock
);
1257 DEBUGFS_ATTR_RW(margining_results
);
1259 static ssize_t
margining_test_write(struct file
*file
,
1260 const char __user
*user_buf
,
1261 size_t count
, loff_t
*ppos
)
1263 struct seq_file
*s
= file
->private_data
;
1264 struct tb_margining
*margining
= s
->private;
1265 struct tb
*tb
= margining
->port
->sw
->tb
;
1269 buf
= validate_and_copy_from_user(user_buf
, &count
);
1271 return PTR_ERR(buf
);
1273 buf
[count
- 1] = '\0';
1275 if (mutex_lock_interruptible(&tb
->lock
)) {
1280 if (!strcmp(buf
, "time") && supports_time(margining
))
1281 margining
->time
= true;
1282 else if (!strcmp(buf
, "voltage"))
1283 margining
->time
= false;
1287 mutex_unlock(&tb
->lock
);
1290 free_page((unsigned long)buf
);
1291 return ret
? ret
: count
;
1294 static int margining_test_show(struct seq_file
*s
, void *not_used
)
1296 struct tb_margining
*margining
= s
->private;
1297 struct tb
*tb
= margining
->port
->sw
->tb
;
1299 if (mutex_lock_interruptible(&tb
->lock
))
1300 return -ERESTARTSYS
;
1302 if (supports_time(margining
)) {
1303 if (margining
->time
)
1304 seq_puts(s
, "voltage [time]\n");
1306 seq_puts(s
, "[voltage] time\n");
1308 seq_puts(s
, "[voltage]\n");
1311 mutex_unlock(&tb
->lock
);
1314 DEBUGFS_ATTR_RW(margining_test
);
1316 static ssize_t
margining_margin_write(struct file
*file
,
1317 const char __user
*user_buf
,
1318 size_t count
, loff_t
*ppos
)
1320 struct seq_file
*s
= file
->private_data
;
1321 struct tb_margining
*margining
= s
->private;
1322 struct tb
*tb
= margining
->port
->sw
->tb
;
1326 buf
= validate_and_copy_from_user(user_buf
, &count
);
1328 return PTR_ERR(buf
);
1330 buf
[count
- 1] = '\0';
1332 if (mutex_lock_interruptible(&tb
->lock
)) {
1337 if (margining
->time
) {
1338 if (!strcmp(buf
, "left"))
1339 margining
->right_high
= false;
1340 else if (!strcmp(buf
, "right"))
1341 margining
->right_high
= true;
1345 if (!strcmp(buf
, "low"))
1346 margining
->right_high
= false;
1347 else if (!strcmp(buf
, "high"))
1348 margining
->right_high
= true;
1353 mutex_unlock(&tb
->lock
);
1356 free_page((unsigned long)buf
);
1357 return ret
? ret
: count
;
1360 static int margining_margin_show(struct seq_file
*s
, void *not_used
)
1362 struct tb_margining
*margining
= s
->private;
1363 struct tb
*tb
= margining
->port
->sw
->tb
;
1365 if (mutex_lock_interruptible(&tb
->lock
))
1366 return -ERESTARTSYS
;
1368 if (margining
->time
) {
1369 if (margining
->right_high
)
1370 seq_puts(s
, "left [right]\n");
1372 seq_puts(s
, "[left] right\n");
1374 if (margining
->right_high
)
1375 seq_puts(s
, "low [high]\n");
1377 seq_puts(s
, "[low] high\n");
1380 mutex_unlock(&tb
->lock
);
1383 DEBUGFS_ATTR_RW(margining_margin
);
1385 static struct tb_margining
*margining_alloc(struct tb_port
*port
,
1387 enum usb4_sb_target target
,
1388 u8 index
, struct dentry
*parent
)
1390 struct tb_margining
*margining
;
1395 margining
= kzalloc(sizeof(*margining
), GFP_KERNEL
);
1399 margining
->port
= port
;
1400 margining
->target
= target
;
1401 margining
->index
= index
;
1402 margining
->dev
= dev
;
1404 ret
= usb4_port_margining_caps(port
, target
, index
, margining
->caps
);
1410 /* Set the initial mode */
1411 if (supports_software(margining
))
1412 margining
->software
= true;
1414 val
= FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK
, margining
->caps
[0]);
1415 margining
->voltage_steps
= val
;
1416 val
= FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK
, margining
->caps
[0]);
1417 margining
->max_voltage_offset
= 74 + val
* 2;
1419 if (supports_optional_voltage_offset_range(margining
)) {
1420 val
= FIELD_GET(USB4_MARGIN_CAP_0_VOLT_STEPS_OPT_MASK
,
1421 margining
->caps
[0]);
1422 margining
->voltage_steps_optional_range
= val
;
1423 val
= FIELD_GET(USB4_MARGIN_CAP_1_MAX_VOLT_OFS_OPT_MASK
,
1424 margining
->caps
[1]);
1425 margining
->max_voltage_offset_optional_range
= 74 + val
* 2;
1428 if (supports_time(margining
)) {
1429 val
= FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK
, margining
->caps
[1]);
1430 margining
->time_steps
= val
;
1431 val
= FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK
, margining
->caps
[1]);
1433 * Store it as mUI (milli Unit Interval) because we want
1434 * to keep it as integer.
1436 margining
->max_time_offset
= 200 + 10 * val
;
1439 dir
= debugfs_create_dir("margining", parent
);
1440 if (supports_hardware(margining
)) {
1441 val
= FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK
, margining
->caps
[1]);
1442 margining
->min_ber_level
= val
;
1443 val
= FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK
, margining
->caps
[1]);
1444 margining
->max_ber_level
= val
;
1446 /* Set the default to minimum */
1447 margining
->ber_level
= margining
->min_ber_level
;
1449 debugfs_create_file("ber_level_contour", 0400, dir
, margining
,
1450 &margining_ber_level_fops
);
1452 debugfs_create_file("caps", 0400, dir
, margining
, &margining_caps_fops
);
1453 debugfs_create_file("lanes", 0600, dir
, margining
, &margining_lanes_fops
);
1454 debugfs_create_file("mode", 0600, dir
, margining
, &margining_mode_fops
);
1455 debugfs_create_file("run", 0600, dir
, margining
, &margining_run_fops
);
1456 debugfs_create_file("results", 0600, dir
, margining
,
1457 &margining_results_fops
);
1458 debugfs_create_file("test", 0600, dir
, margining
, &margining_test_fops
);
1459 if (independent_voltage_margins(margining
) == USB4_MARGIN_CAP_0_VOLTAGE_HL
||
1460 (supports_time(margining
) &&
1461 independent_time_margins(margining
) == USB4_MARGIN_CAP_1_TIME_LR
))
1462 debugfs_create_file("margin", 0600, dir
, margining
,
1463 &margining_margin_fops
);
1465 margining
->error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
;
1466 margining
->dwell_time
= MIN_DWELL_TIME
;
1468 if (supports_optional_voltage_offset_range(margining
))
1469 debugfs_create_file("optional_voltage_offset", DEBUGFS_MODE
, dir
, margining
,
1470 &margining_optional_voltage_offset_fops
);
1472 if (supports_software(margining
)) {
1473 debugfs_create_file("voltage_time_offset", DEBUGFS_MODE
, dir
, margining
,
1474 &margining_voltage_time_offset_fops
);
1475 debugfs_create_file("error_counter", DEBUGFS_MODE
, dir
, margining
,
1476 &margining_error_counter_fops
);
1477 debugfs_create_file("dwell_time", DEBUGFS_MODE
, dir
, margining
,
1478 &margining_dwell_time_fops
);
1483 static void margining_port_init(struct tb_port
*port
)
1485 struct dentry
*parent
;
1491 snprintf(dir_name
, sizeof(dir_name
), "port%d", port
->port
);
1492 parent
= debugfs_lookup(dir_name
, port
->sw
->debugfs_dir
);
1493 port
->usb4
->margining
= margining_alloc(port
, &port
->usb4
->dev
,
1494 USB4_SB_TARGET_ROUTER
, 0,
1498 static void margining_port_remove(struct tb_port
*port
)
1500 struct dentry
*parent
;
1506 snprintf(dir_name
, sizeof(dir_name
), "port%d", port
->port
);
1507 parent
= debugfs_lookup(dir_name
, port
->sw
->debugfs_dir
);
1509 debugfs_lookup_and_remove("margining", parent
);
1511 kfree(port
->usb4
->margining
);
1512 port
->usb4
->margining
= NULL
;
1515 static void margining_switch_init(struct tb_switch
*sw
)
1517 struct tb_port
*upstream
, *downstream
;
1518 struct tb_switch
*parent_sw
;
1519 u64 route
= tb_route(sw
);
1524 upstream
= tb_upstream_port(sw
);
1525 parent_sw
= tb_switch_parent(sw
);
1526 downstream
= tb_port_at(route
, parent_sw
);
1528 margining_port_init(downstream
);
1529 margining_port_init(upstream
);
1532 static void margining_switch_remove(struct tb_switch
*sw
)
1534 struct tb_port
*upstream
, *downstream
;
1535 struct tb_switch
*parent_sw
;
1536 u64 route
= tb_route(sw
);
1541 upstream
= tb_upstream_port(sw
);
1542 parent_sw
= tb_switch_parent(sw
);
1543 downstream
= tb_port_at(route
, parent_sw
);
1545 margining_port_remove(upstream
);
1546 margining_port_remove(downstream
);
1549 static void margining_xdomain_init(struct tb_xdomain
*xd
)
1551 struct tb_switch
*parent_sw
;
1552 struct tb_port
*downstream
;
1554 parent_sw
= tb_xdomain_parent(xd
);
1555 downstream
= tb_port_at(xd
->route
, parent_sw
);
1557 margining_port_init(downstream
);
1560 static void margining_xdomain_remove(struct tb_xdomain
*xd
)
1562 struct tb_switch
*parent_sw
;
1563 struct tb_port
*downstream
;
1565 parent_sw
= tb_xdomain_parent(xd
);
1566 downstream
= tb_port_at(xd
->route
, parent_sw
);
1567 margining_port_remove(downstream
);
1570 static void margining_retimer_init(struct tb_retimer
*rt
, struct dentry
*debugfs_dir
)
1572 rt
->margining
= margining_alloc(rt
->port
, &rt
->dev
,
1573 USB4_SB_TARGET_RETIMER
, rt
->index
,
1577 static void margining_retimer_remove(struct tb_retimer
*rt
)
1579 kfree(rt
->margining
);
1580 rt
->margining
= NULL
;
1583 static inline void margining_switch_init(struct tb_switch
*sw
) { }
1584 static inline void margining_switch_remove(struct tb_switch
*sw
) { }
1585 static inline void margining_xdomain_init(struct tb_xdomain
*xd
) { }
1586 static inline void margining_xdomain_remove(struct tb_xdomain
*xd
) { }
1587 static inline void margining_retimer_init(struct tb_retimer
*rt
,
1588 struct dentry
*debugfs_dir
) { }
1589 static inline void margining_retimer_remove(struct tb_retimer
*rt
) { }
1592 static int port_clear_all_counters(struct tb_port
*port
)
1597 buf
= kcalloc(COUNTER_SET_LEN
* port
->config
.max_counters
, sizeof(u32
),
1602 ret
= tb_port_write(port
, buf
, TB_CFG_COUNTERS
, 0,
1603 COUNTER_SET_LEN
* port
->config
.max_counters
);
1609 static ssize_t
counters_write(struct file
*file
, const char __user
*user_buf
,
1610 size_t count
, loff_t
*ppos
)
1612 struct seq_file
*s
= file
->private_data
;
1613 struct tb_port
*port
= s
->private;
1614 struct tb_switch
*sw
= port
->sw
;
1615 struct tb
*tb
= port
->sw
->tb
;
1619 buf
= validate_and_copy_from_user(user_buf
, &count
);
1621 return PTR_ERR(buf
);
1623 pm_runtime_get_sync(&sw
->dev
);
1625 if (mutex_lock_interruptible(&tb
->lock
)) {
1630 /* If written delimiter only, clear all counters in one shot */
1631 if (buf
[0] == '\n') {
1632 ret
= port_clear_all_counters(port
);
1638 while (parse_line(&line
, &offset
, &val
, 1, 4)) {
1639 ret
= tb_port_write(port
, &val
, TB_CFG_COUNTERS
,
1646 mutex_unlock(&tb
->lock
);
1649 pm_runtime_mark_last_busy(&sw
->dev
);
1650 pm_runtime_put_autosuspend(&sw
->dev
);
1651 free_page((unsigned long)buf
);
1653 return ret
< 0 ? ret
: count
;
1656 static void cap_show_by_dw(struct seq_file
*s
, struct tb_switch
*sw
,
1657 struct tb_port
*port
, unsigned int cap
,
1658 unsigned int offset
, u8 cap_id
, u8 vsec_id
,
1664 for (i
= 0; i
< dwords
; i
++) {
1666 ret
= tb_port_read(port
, &data
, TB_CFG_PORT
, cap
+ offset
+ i
, 1);
1668 ret
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, cap
+ offset
+ i
, 1);
1670 seq_printf(s
, "0x%04x <not accessible>\n", cap
+ offset
+ i
);
1674 seq_printf(s
, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap
+ offset
+ i
,
1675 offset
+ i
, cap_id
, vsec_id
, data
);
1679 static void cap_show(struct seq_file
*s
, struct tb_switch
*sw
,
1680 struct tb_port
*port
, unsigned int cap
, u8 cap_id
,
1681 u8 vsec_id
, int length
)
1683 int ret
, offset
= 0;
1685 while (length
> 0) {
1686 int i
, dwords
= min(length
, TB_MAX_CONFIG_RW_LENGTH
);
1687 u32 data
[TB_MAX_CONFIG_RW_LENGTH
];
1690 ret
= tb_port_read(port
, data
, TB_CFG_PORT
, cap
+ offset
,
1693 ret
= tb_sw_read(sw
, data
, TB_CFG_SWITCH
, cap
+ offset
, dwords
);
1695 cap_show_by_dw(s
, sw
, port
, cap
, offset
, cap_id
, vsec_id
, length
);
1699 for (i
= 0; i
< dwords
; i
++) {
1700 seq_printf(s
, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1701 cap
+ offset
+ i
, offset
+ i
,
1702 cap_id
, vsec_id
, data
[i
]);
1710 static void port_cap_show(struct tb_port
*port
, struct seq_file
*s
,
1713 struct tb_cap_any header
;
1718 ret
= tb_port_read(port
, &header
, TB_CFG_PORT
, cap
, 1);
1720 seq_printf(s
, "0x%04x <capability read failed>\n", cap
);
1724 switch (header
.basic
.cap
) {
1725 case TB_PORT_CAP_PHY
:
1726 length
= PORT_CAP_LANE_LEN
;
1729 case TB_PORT_CAP_TIME1
:
1730 if (usb4_switch_version(port
->sw
) < 2)
1731 length
= PORT_CAP_TMU_V1_LEN
;
1733 length
= PORT_CAP_TMU_V2_LEN
;
1736 case TB_PORT_CAP_POWER
:
1737 length
= PORT_CAP_POWER_LEN
;
1740 case TB_PORT_CAP_ADAP
:
1741 if (tb_port_is_pcie_down(port
) || tb_port_is_pcie_up(port
)) {
1742 if (usb4_switch_version(port
->sw
) < 2)
1743 length
= PORT_CAP_V1_PCIE_LEN
;
1745 length
= PORT_CAP_V2_PCIE_LEN
;
1746 } else if (tb_port_is_dpin(port
)) {
1747 if (usb4_switch_version(port
->sw
) < 2)
1748 length
= PORT_CAP_DP_V1_LEN
;
1750 length
= PORT_CAP_DP_V2_LEN
;
1751 } else if (tb_port_is_dpout(port
)) {
1752 length
= PORT_CAP_DP_V1_LEN
;
1753 } else if (tb_port_is_usb3_down(port
) ||
1754 tb_port_is_usb3_up(port
)) {
1755 length
= PORT_CAP_USB3_LEN
;
1757 seq_printf(s
, "0x%04x <unsupported capability 0x%02x>\n",
1758 cap
, header
.basic
.cap
);
1763 case TB_PORT_CAP_VSE
:
1764 if (!header
.extended_short
.length
) {
1765 ret
= tb_port_read(port
, (u32
*)&header
+ 1, TB_CFG_PORT
,
1768 seq_printf(s
, "0x%04x <capability read failed>\n",
1772 length
= header
.extended_long
.length
;
1773 vsec_id
= header
.extended_short
.vsec_id
;
1775 length
= header
.extended_short
.length
;
1776 vsec_id
= header
.extended_short
.vsec_id
;
1780 case TB_PORT_CAP_USB4
:
1781 length
= PORT_CAP_USB4_LEN
;
1785 seq_printf(s
, "0x%04x <unsupported capability 0x%02x>\n",
1786 cap
, header
.basic
.cap
);
1790 cap_show(s
, NULL
, port
, cap
, header
.basic
.cap
, vsec_id
, length
);
1793 static void port_caps_show(struct tb_port
*port
, struct seq_file
*s
)
1797 cap
= tb_port_next_cap(port
, 0);
1799 port_cap_show(port
, s
, cap
);
1800 cap
= tb_port_next_cap(port
, cap
);
1804 static int port_basic_regs_show(struct tb_port
*port
, struct seq_file
*s
)
1806 u32 data
[PORT_CAP_BASIC_LEN
];
1809 ret
= tb_port_read(port
, data
, TB_CFG_PORT
, 0, ARRAY_SIZE(data
));
1813 for (i
= 0; i
< ARRAY_SIZE(data
); i
++)
1814 seq_printf(s
, "0x%04x %4d 0x00 0x00 0x%08x\n", i
, i
, data
[i
]);
1819 static int port_regs_show(struct seq_file
*s
, void *not_used
)
1821 struct tb_port
*port
= s
->private;
1822 struct tb_switch
*sw
= port
->sw
;
1823 struct tb
*tb
= sw
->tb
;
1826 pm_runtime_get_sync(&sw
->dev
);
1828 if (mutex_lock_interruptible(&tb
->lock
)) {
1833 seq_puts(s
, "# offset relative_offset cap_id vs_cap_id value\n");
1835 ret
= port_basic_regs_show(port
, s
);
1839 port_caps_show(port
, s
);
1842 mutex_unlock(&tb
->lock
);
1844 pm_runtime_mark_last_busy(&sw
->dev
);
1845 pm_runtime_put_autosuspend(&sw
->dev
);
1849 DEBUGFS_ATTR_RW(port_regs
);
1851 static void switch_cap_show(struct tb_switch
*sw
, struct seq_file
*s
,
1854 struct tb_cap_any header
;
1858 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, cap
, 1);
1860 seq_printf(s
, "0x%04x <capability read failed>\n", cap
);
1864 if (header
.basic
.cap
== TB_SWITCH_CAP_VSE
) {
1865 if (!header
.extended_short
.length
) {
1866 ret
= tb_sw_read(sw
, (u32
*)&header
+ 1, TB_CFG_SWITCH
,
1869 seq_printf(s
, "0x%04x <capability read failed>\n",
1873 length
= header
.extended_long
.length
;
1875 length
= header
.extended_short
.length
;
1877 vsec_id
= header
.extended_short
.vsec_id
;
1879 if (header
.basic
.cap
== TB_SWITCH_CAP_TMU
) {
1880 length
= SWITCH_CAP_TMU_LEN
;
1882 seq_printf(s
, "0x%04x <unknown capability 0x%02x>\n",
1883 cap
, header
.basic
.cap
);
1888 cap_show(s
, sw
, NULL
, cap
, header
.basic
.cap
, vsec_id
, length
);
1891 static void switch_caps_show(struct tb_switch
*sw
, struct seq_file
*s
)
1895 cap
= tb_switch_next_cap(sw
, 0);
1897 switch_cap_show(sw
, s
, cap
);
1898 cap
= tb_switch_next_cap(sw
, cap
);
1902 static int switch_basic_regs_show(struct tb_switch
*sw
, struct seq_file
*s
)
1904 u32 data
[SWITCH_CAP_BASIC_LEN
];
1908 /* Only USB4 has the additional registers */
1909 if (tb_switch_is_usb4(sw
))
1910 dwords
= ARRAY_SIZE(data
);
1914 ret
= tb_sw_read(sw
, data
, TB_CFG_SWITCH
, 0, dwords
);
1918 for (i
= 0; i
< dwords
; i
++)
1919 seq_printf(s
, "0x%04x %4d 0x00 0x00 0x%08x\n", i
, i
, data
[i
]);
1924 static int switch_regs_show(struct seq_file
*s
, void *not_used
)
1926 struct tb_switch
*sw
= s
->private;
1927 struct tb
*tb
= sw
->tb
;
1930 pm_runtime_get_sync(&sw
->dev
);
1932 if (mutex_lock_interruptible(&tb
->lock
)) {
1937 seq_puts(s
, "# offset relative_offset cap_id vs_cap_id value\n");
1939 ret
= switch_basic_regs_show(sw
, s
);
1943 switch_caps_show(sw
, s
);
1946 mutex_unlock(&tb
->lock
);
1948 pm_runtime_mark_last_busy(&sw
->dev
);
1949 pm_runtime_put_autosuspend(&sw
->dev
);
1953 DEBUGFS_ATTR_RW(switch_regs
);
1955 static int path_show_one(struct tb_port
*port
, struct seq_file
*s
, int hopid
)
1960 ret
= tb_port_read(port
, data
, TB_CFG_HOPS
, hopid
* PATH_LEN
,
1963 seq_printf(s
, "0x%04x <not accessible>\n", hopid
* PATH_LEN
);
1967 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
1968 seq_printf(s
, "0x%04x %4d 0x%02x 0x%08x\n",
1969 hopid
* PATH_LEN
+ i
, i
, hopid
, data
[i
]);
1975 static int path_show(struct seq_file
*s
, void *not_used
)
1977 struct tb_port
*port
= s
->private;
1978 struct tb_switch
*sw
= port
->sw
;
1979 struct tb
*tb
= sw
->tb
;
1980 int start
, i
, ret
= 0;
1982 pm_runtime_get_sync(&sw
->dev
);
1984 if (mutex_lock_interruptible(&tb
->lock
)) {
1989 seq_puts(s
, "# offset relative_offset in_hop_id value\n");
1991 /* NHI and lane adapters have entry for path 0 */
1992 if (tb_port_is_null(port
) || tb_port_is_nhi(port
)) {
1993 ret
= path_show_one(port
, s
, 0);
1998 start
= tb_port_is_nhi(port
) ? 1 : TB_PATH_MIN_HOPID
;
2000 for (i
= start
; i
<= port
->config
.max_in_hop_id
; i
++) {
2001 ret
= path_show_one(port
, s
, i
);
2007 mutex_unlock(&tb
->lock
);
2009 pm_runtime_mark_last_busy(&sw
->dev
);
2010 pm_runtime_put_autosuspend(&sw
->dev
);
2014 DEBUGFS_ATTR_RO(path
);
2016 static int counter_set_regs_show(struct tb_port
*port
, struct seq_file
*s
,
2019 u32 data
[COUNTER_SET_LEN
];
2022 ret
= tb_port_read(port
, data
, TB_CFG_COUNTERS
,
2023 counter
* COUNTER_SET_LEN
, ARRAY_SIZE(data
));
2025 seq_printf(s
, "0x%04x <not accessible>\n",
2026 counter
* COUNTER_SET_LEN
);
2030 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
2031 seq_printf(s
, "0x%04x %4d 0x%02x 0x%08x\n",
2032 counter
* COUNTER_SET_LEN
+ i
, i
, counter
, data
[i
]);
2038 static int counters_show(struct seq_file
*s
, void *not_used
)
2040 struct tb_port
*port
= s
->private;
2041 struct tb_switch
*sw
= port
->sw
;
2042 struct tb
*tb
= sw
->tb
;
2045 pm_runtime_get_sync(&sw
->dev
);
2047 if (mutex_lock_interruptible(&tb
->lock
)) {
2052 seq_puts(s
, "# offset relative_offset counter_id value\n");
2054 for (i
= 0; i
< port
->config
.max_counters
; i
++) {
2055 ret
= counter_set_regs_show(port
, s
, i
);
2060 mutex_unlock(&tb
->lock
);
2063 pm_runtime_mark_last_busy(&sw
->dev
);
2064 pm_runtime_put_autosuspend(&sw
->dev
);
2068 DEBUGFS_ATTR_RW(counters
);
2070 static int sb_regs_show(struct tb_port
*port
, const struct sb_reg
*sb_regs
,
2071 size_t size
, enum usb4_sb_target target
, u8 index
,
2076 seq_puts(s
, "# register value\n");
2078 for (i
= 0; i
< size
; i
++) {
2079 const struct sb_reg
*regs
= &sb_regs
[i
];
2083 memset(data
, 0, sizeof(data
));
2084 ret
= usb4_port_sb_read(port
, target
, index
, regs
->reg
, data
,
2089 seq_printf(s
, "0x%02x", regs
->reg
);
2090 for (j
= 0; j
< regs
->size
; j
++)
2091 seq_printf(s
, " 0x%02x", data
[j
]);
2098 static int port_sb_regs_show(struct seq_file
*s
, void *not_used
)
2100 struct tb_port
*port
= s
->private;
2101 struct tb_switch
*sw
= port
->sw
;
2102 struct tb
*tb
= sw
->tb
;
2105 pm_runtime_get_sync(&sw
->dev
);
2107 if (mutex_lock_interruptible(&tb
->lock
)) {
2112 ret
= sb_regs_show(port
, port_sb_regs
, ARRAY_SIZE(port_sb_regs
),
2113 USB4_SB_TARGET_ROUTER
, 0, s
);
2115 mutex_unlock(&tb
->lock
);
2117 pm_runtime_mark_last_busy(&sw
->dev
);
2118 pm_runtime_put_autosuspend(&sw
->dev
);
2122 DEBUGFS_ATTR_RW(port_sb_regs
);
2125 * tb_switch_debugfs_init() - Add debugfs entries for router
2126 * @sw: Pointer to the router
2128 * Adds debugfs directories and files for given router.
2130 void tb_switch_debugfs_init(struct tb_switch
*sw
)
2132 struct dentry
*debugfs_dir
;
2133 struct tb_port
*port
;
2135 debugfs_dir
= debugfs_create_dir(dev_name(&sw
->dev
), tb_debugfs_root
);
2136 sw
->debugfs_dir
= debugfs_dir
;
2137 debugfs_create_file("regs", DEBUGFS_MODE
, debugfs_dir
, sw
,
2140 tb_switch_for_each_port(sw
, port
) {
2141 struct dentry
*debugfs_dir
;
2146 if (port
->config
.type
== TB_TYPE_INACTIVE
)
2149 snprintf(dir_name
, sizeof(dir_name
), "port%d", port
->port
);
2150 debugfs_dir
= debugfs_create_dir(dir_name
, sw
->debugfs_dir
);
2151 debugfs_create_file("regs", DEBUGFS_MODE
, debugfs_dir
,
2152 port
, &port_regs_fops
);
2153 debugfs_create_file("path", 0400, debugfs_dir
, port
,
2155 if (port
->config
.counters_support
)
2156 debugfs_create_file("counters", 0600, debugfs_dir
, port
,
2159 debugfs_create_file("sb_regs", DEBUGFS_MODE
, debugfs_dir
,
2160 port
, &port_sb_regs_fops
);
2163 margining_switch_init(sw
);
2167 * tb_switch_debugfs_remove() - Remove all router debugfs entries
2168 * @sw: Pointer to the router
2170 * Removes all previously added debugfs entries under this router.
2172 void tb_switch_debugfs_remove(struct tb_switch
*sw
)
2174 margining_switch_remove(sw
);
2175 debugfs_remove_recursive(sw
->debugfs_dir
);
2178 void tb_xdomain_debugfs_init(struct tb_xdomain
*xd
)
2180 margining_xdomain_init(xd
);
2183 void tb_xdomain_debugfs_remove(struct tb_xdomain
*xd
)
2185 margining_xdomain_remove(xd
);
2189 * tb_service_debugfs_init() - Add debugfs directory for service
2190 * @svc: Thunderbolt service pointer
2192 * Adds debugfs directory for service.
2194 void tb_service_debugfs_init(struct tb_service
*svc
)
2196 svc
->debugfs_dir
= debugfs_create_dir(dev_name(&svc
->dev
),
2201 * tb_service_debugfs_remove() - Remove service debugfs directory
2202 * @svc: Thunderbolt service pointer
2204 * Removes the previously created debugfs directory for @svc.
2206 void tb_service_debugfs_remove(struct tb_service
*svc
)
2208 debugfs_remove_recursive(svc
->debugfs_dir
);
2209 svc
->debugfs_dir
= NULL
;
2212 static int retimer_sb_regs_show(struct seq_file
*s
, void *not_used
)
2214 struct tb_retimer
*rt
= s
->private;
2215 struct tb
*tb
= rt
->tb
;
2218 pm_runtime_get_sync(&rt
->dev
);
2220 if (mutex_lock_interruptible(&tb
->lock
)) {
2225 ret
= sb_regs_show(rt
->port
, retimer_sb_regs
, ARRAY_SIZE(retimer_sb_regs
),
2226 USB4_SB_TARGET_RETIMER
, rt
->index
, s
);
2228 mutex_unlock(&tb
->lock
);
2230 pm_runtime_mark_last_busy(&rt
->dev
);
2231 pm_runtime_put_autosuspend(&rt
->dev
);
2235 DEBUGFS_ATTR_RW(retimer_sb_regs
);
2238 * tb_retimer_debugfs_init() - Add debugfs directory for retimer
2239 * @rt: Pointer to retimer structure
2241 * Adds and populates retimer debugfs directory.
2243 void tb_retimer_debugfs_init(struct tb_retimer
*rt
)
2245 struct dentry
*debugfs_dir
;
2247 debugfs_dir
= debugfs_create_dir(dev_name(&rt
->dev
), tb_debugfs_root
);
2248 debugfs_create_file("sb_regs", DEBUGFS_MODE
, debugfs_dir
, rt
,
2249 &retimer_sb_regs_fops
);
2250 margining_retimer_init(rt
, debugfs_dir
);
2254 * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
2255 * @rt: Pointer to retimer structure
2257 * Removes the retimer debugfs directory along with its contents.
2259 void tb_retimer_debugfs_remove(struct tb_retimer
*rt
)
2261 debugfs_lookup_and_remove(dev_name(&rt
->dev
), tb_debugfs_root
);
2262 margining_retimer_remove(rt
);
2265 void tb_debugfs_init(void)
2267 tb_debugfs_root
= debugfs_create_dir("thunderbolt", NULL
);
2270 void tb_debugfs_exit(void)
2272 debugfs_remove_recursive(tb_debugfs_root
);