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/array_size.h>
11 #include <linux/bitfield.h>
12 #include <linux/debugfs.h>
13 #include <linux/delay.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/uaccess.h>
20 #define PORT_CAP_V1_PCIE_LEN 1
21 #define PORT_CAP_V2_PCIE_LEN 2
22 #define PORT_CAP_POWER_LEN 2
23 #define PORT_CAP_LANE_LEN 3
24 #define PORT_CAP_USB3_LEN 5
25 #define PORT_CAP_DP_V1_LEN 9
26 #define PORT_CAP_DP_V2_LEN 14
27 #define PORT_CAP_TMU_V1_LEN 8
28 #define PORT_CAP_TMU_V2_LEN 10
29 #define PORT_CAP_BASIC_LEN 9
30 #define PORT_CAP_USB4_LEN 20
32 #define SWITCH_CAP_TMU_LEN 26
33 #define SWITCH_CAP_BASIC_LEN 27
37 #define COUNTER_SET_LEN 3
40 * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms
41 * probed to give good results.
43 #define MIN_DWELL_TIME 100 /* ms */
44 #define MAX_DWELL_TIME 500 /* ms */
45 #define DWELL_SAMPLE_INTERVAL 10
47 enum usb4_margin_cap_voltage_indp
{
48 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN
,
49 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL
,
50 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH
,
51 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN
,
52 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH
,
53 USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN
,
56 enum usb4_margin_cap_time_indp
{
57 USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN
,
58 USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR
,
59 USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH
,
60 USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN
,
61 USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH
,
62 USB4_MARGIN_CAP_TIME_INDP_UNKNOWN
,
65 /* Sideband registers and their sizes as defined in the USB4 spec */
71 #define SB_MAX_SIZE 64
73 /* Sideband registers for router */
74 static const struct sb_reg port_sb_regs
[] = {
75 { USB4_SB_VENDOR_ID
, 4 },
76 { USB4_SB_PRODUCT_ID
, 4 },
77 { USB4_SB_DEBUG_CONF
, 4 },
78 { USB4_SB_DEBUG
, 54 },
79 { USB4_SB_LRD_TUNING
, 4 },
80 { USB4_SB_OPCODE
, 4 },
81 { USB4_SB_METADATA
, 4 },
82 { USB4_SB_LINK_CONF
, 3 },
83 { USB4_SB_GEN23_TXFFE
, 4 },
84 { USB4_SB_GEN4_TXFFE
, 4 },
85 { USB4_SB_VERSION
, 4 },
89 /* Sideband registers for retimer */
90 static const struct sb_reg retimer_sb_regs
[] = {
91 { USB4_SB_VENDOR_ID
, 4 },
92 { USB4_SB_PRODUCT_ID
, 4 },
93 { USB4_SB_FW_VERSION
, 4 },
94 { USB4_SB_LRD_TUNING
, 4 },
95 { USB4_SB_OPCODE
, 4 },
96 { USB4_SB_METADATA
, 4 },
97 { USB4_SB_GEN23_TXFFE
, 4 },
98 { USB4_SB_GEN4_TXFFE
, 4 },
99 { USB4_SB_VERSION
, 4 },
100 { USB4_SB_DATA
, 64 },
103 #define DEBUGFS_ATTR(__space, __write) \
104 static int __space ## _open(struct inode *inode, struct file *file) \
106 return single_open(file, __space ## _show, inode->i_private); \
109 static const struct file_operations __space ## _fops = { \
110 .owner = THIS_MODULE, \
111 .open = __space ## _open, \
112 .release = single_release, \
115 .llseek = seq_lseek, \
118 #define DEBUGFS_ATTR_RO(__space) \
119 DEBUGFS_ATTR(__space, NULL)
121 #define DEBUGFS_ATTR_RW(__space) \
122 DEBUGFS_ATTR(__space, __space ## _write)
124 static struct dentry
*tb_debugfs_root
;
126 static void *validate_and_copy_from_user(const void __user
*user_buf
,
133 return ERR_PTR(-EINVAL
);
135 if (!access_ok(user_buf
, *count
))
136 return ERR_PTR(-EFAULT
);
138 buf
= (void *)get_zeroed_page(GFP_KERNEL
);
140 return ERR_PTR(-ENOMEM
);
142 nbytes
= min_t(size_t, *count
, PAGE_SIZE
);
143 if (copy_from_user(buf
, user_buf
, nbytes
)) {
144 free_page((unsigned long)buf
);
145 return ERR_PTR(-EFAULT
);
152 static bool parse_line(char **line
, u32
*offs
, u32
*val
, int short_fmt_len
,
159 token
= strsep(line
, "\n");
164 * For Adapter/Router configuration space:
165 * Short format is: offset value\n
167 * Long format as produced from the read side:
168 * offset relative_offset cap_id vs_cap_id value\n
169 * v[0] v[1] v[2] v[3] v[4]
171 * For Counter configuration space:
172 * Short format is: offset\n
174 * Long format as produced from the read side:
175 * offset relative_offset counter_id value\n
176 * v[0] v[1] v[2] v[3]
178 ret
= sscanf(token
, "%i %i %i %i %i", &v
[0], &v
[1], &v
[2], &v
[3], &v
[4]);
179 /* In case of Counters, clear counter, "val" content is NA */
180 if (ret
== short_fmt_len
) {
182 *val
= v
[short_fmt_len
- 1];
184 } else if (ret
== long_fmt_len
) {
186 *val
= v
[long_fmt_len
- 1];
193 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE)
194 static ssize_t
regs_write(struct tb_switch
*sw
, struct tb_port
*port
,
195 const char __user
*user_buf
, size_t count
,
198 struct tb
*tb
= sw
->tb
;
203 buf
= validate_and_copy_from_user(user_buf
, &count
);
207 pm_runtime_get_sync(&sw
->dev
);
209 if (mutex_lock_interruptible(&tb
->lock
)) {
214 /* User did hardware changes behind the driver's back */
215 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
218 while (parse_line(&line
, &offset
, &val
, 2, 5)) {
220 ret
= tb_port_write(port
, &val
, TB_CFG_PORT
, offset
, 1);
222 ret
= tb_sw_write(sw
, &val
, TB_CFG_SWITCH
, offset
, 1);
227 mutex_unlock(&tb
->lock
);
230 pm_runtime_mark_last_busy(&sw
->dev
);
231 pm_runtime_put_autosuspend(&sw
->dev
);
232 free_page((unsigned long)buf
);
234 return ret
< 0 ? ret
: count
;
237 static ssize_t
port_regs_write(struct file
*file
, const char __user
*user_buf
,
238 size_t count
, loff_t
*ppos
)
240 struct seq_file
*s
= file
->private_data
;
241 struct tb_port
*port
= s
->private;
243 return regs_write(port
->sw
, port
, user_buf
, count
, ppos
);
246 static ssize_t
switch_regs_write(struct file
*file
, const char __user
*user_buf
,
247 size_t count
, loff_t
*ppos
)
249 struct seq_file
*s
= file
->private_data
;
250 struct tb_switch
*sw
= s
->private;
252 return regs_write(sw
, NULL
, user_buf
, count
, ppos
);
255 static bool parse_sb_line(char **line
, u8
*reg
, u8
*data
, size_t data_size
,
261 token
= strsep(line
, "\n");
265 /* Parse the register first */
266 field
= strsep(&token
, " ");
269 if (kstrtou8(field
, 0, reg
))
272 /* Then the values for the register, up to data_size */
273 for (i
= 0; i
< data_size
; i
++) {
274 field
= strsep(&token
, " ");
277 if (kstrtou8(field
, 0, &data
[i
]))
285 static ssize_t
sb_regs_write(struct tb_port
*port
, const struct sb_reg
*sb_regs
,
286 size_t size
, enum usb4_sb_target target
, u8 index
,
287 char *buf
, size_t count
, loff_t
*ppos
)
289 u8 reg
, data
[SB_MAX_SIZE
];
293 /* User did hardware changes behind the driver's back */
294 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
297 * For sideband registers we accept:
300 * Here "reg" is the byte offset of the sideband register and "b0"..
301 * are the byte values. There can be less byte values than the register
302 * size. The leftovers will not be overwritten.
304 while (parse_sb_line(&line
, ®
, data
, ARRAY_SIZE(data
), &bytes_read
)) {
305 const struct sb_reg
*sb_reg
;
308 /* At least one byte must be passed */
312 /* Find the register */
314 for (int i
= 0; i
< size
; i
++) {
315 if (sb_regs
[i
].reg
== reg
) {
316 sb_reg
= &sb_regs
[i
];
324 if (bytes_read
> sb_regs
->size
)
327 ret
= usb4_port_sb_write(port
, target
, index
, sb_reg
->reg
, data
,
336 static ssize_t
port_sb_regs_write(struct file
*file
, const char __user
*user_buf
,
337 size_t count
, loff_t
*ppos
)
339 struct seq_file
*s
= file
->private_data
;
340 struct tb_port
*port
= s
->private;
341 struct tb_switch
*sw
= port
->sw
;
342 struct tb
*tb
= sw
->tb
;
346 buf
= validate_and_copy_from_user(user_buf
, &count
);
350 pm_runtime_get_sync(&sw
->dev
);
352 if (mutex_lock_interruptible(&tb
->lock
)) {
357 ret
= sb_regs_write(port
, port_sb_regs
, ARRAY_SIZE(port_sb_regs
),
358 USB4_SB_TARGET_ROUTER
, 0, buf
, count
, ppos
);
360 mutex_unlock(&tb
->lock
);
362 pm_runtime_mark_last_busy(&sw
->dev
);
363 pm_runtime_put_autosuspend(&sw
->dev
);
364 free_page((unsigned long)buf
);
366 return ret
< 0 ? ret
: count
;
369 static ssize_t
retimer_sb_regs_write(struct file
*file
,
370 const char __user
*user_buf
,
371 size_t count
, loff_t
*ppos
)
373 struct seq_file
*s
= file
->private_data
;
374 struct tb_retimer
*rt
= s
->private;
375 struct tb
*tb
= rt
->tb
;
379 buf
= validate_and_copy_from_user(user_buf
, &count
);
383 pm_runtime_get_sync(&rt
->dev
);
385 if (mutex_lock_interruptible(&tb
->lock
)) {
390 ret
= sb_regs_write(rt
->port
, retimer_sb_regs
, ARRAY_SIZE(retimer_sb_regs
),
391 USB4_SB_TARGET_RETIMER
, rt
->index
, buf
, count
, ppos
);
393 mutex_unlock(&tb
->lock
);
395 pm_runtime_mark_last_busy(&rt
->dev
);
396 pm_runtime_put_autosuspend(&rt
->dev
);
397 free_page((unsigned long)buf
);
399 return ret
< 0 ? ret
: count
;
401 #define DEBUGFS_MODE 0600
403 #define port_regs_write NULL
404 #define switch_regs_write NULL
405 #define port_sb_regs_write NULL
406 #define retimer_sb_regs_write NULL
407 #define DEBUGFS_MODE 0400
410 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING)
412 * struct tb_margining - Lane margining support
413 * @port: USB4 port through which the margining operations are run
414 * @target: Sideband target
415 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER
416 * @dev: Pointer to the device that is the target (USB4 port or retimer)
417 * @gen: Link generation
418 * @asym_rx: %true% if @port supports asymmetric link with 3 Rx
419 * @caps: Port lane margining capabilities
420 * @results: Last lane margining results
421 * @lanes: %0, %1 or %7 (all)
422 * @min_ber_level: Minimum supported BER level contour value
423 * @max_ber_level: Maximum supported BER level contour value
424 * @ber_level: Current BER level contour value
425 * @voltage_steps: Number of mandatory voltage steps
426 * @max_voltage_offset: Maximum mandatory voltage offset (in mV)
427 * @voltage_steps_optional_range: Number of voltage steps for optional range
428 * @max_voltage_offset_optional_range: Maximum voltage offset for the optional
430 * @time_steps: Number of time margin steps
431 * @max_time_offset: Maximum time margin offset (in mUI)
432 * @voltage_time_offset: Offset for voltage / time for software margining
433 * @dwell_time: Dwell time for software margining (in ms)
434 * @error_counter: Error counter operation for software margining
435 * @optional_voltage_offset_range: Enable optional extended voltage range
436 * @software: %true if software margining is used instead of hardware
437 * @time: %true if time margining is used instead of voltage
438 * @right_high: %false if left/low margin test is performed, %true if
440 * @upper_eye: %false if the lower PAM3 eye is used, %true if the upper
443 struct tb_margining
{
444 struct tb_port
*port
;
445 enum usb4_sb_target target
;
452 enum usb4_margining_lane lanes
;
453 unsigned int min_ber_level
;
454 unsigned int max_ber_level
;
455 unsigned int ber_level
;
456 unsigned int voltage_steps
;
457 unsigned int max_voltage_offset
;
458 unsigned int voltage_steps_optional_range
;
459 unsigned int max_voltage_offset_optional_range
;
460 unsigned int time_steps
;
461 unsigned int max_time_offset
;
462 unsigned int voltage_time_offset
;
463 unsigned int dwell_time
;
464 enum usb4_margin_sw_error_counter error_counter
;
465 bool optional_voltage_offset_range
;
472 static int margining_modify_error_counter(struct tb_margining
*margining
,
473 u32 lanes
, enum usb4_margin_sw_error_counter error_counter
)
475 struct usb4_port_margining_params params
= { 0 };
476 struct tb_port
*port
= margining
->port
;
479 if (error_counter
!= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
&&
480 error_counter
!= USB4_MARGIN_SW_ERROR_COUNTER_STOP
)
483 params
.error_counter
= error_counter
;
484 params
.lanes
= lanes
;
486 return usb4_port_sw_margin(port
, margining
->target
, margining
->index
,
490 static bool supports_software(const struct tb_margining
*margining
)
492 if (margining
->gen
< 4)
493 return margining
->caps
[0] & USB4_MARGIN_CAP_0_MODES_SW
;
494 return margining
->caps
[2] & USB4_MARGIN_CAP_2_MODES_SW
;
497 static bool supports_hardware(const struct tb_margining
*margining
)
499 if (margining
->gen
< 4)
500 return margining
->caps
[0] & USB4_MARGIN_CAP_0_MODES_HW
;
501 return margining
->caps
[2] & USB4_MARGIN_CAP_2_MODES_HW
;
504 static bool all_lanes(const struct tb_margining
*margining
)
506 return margining
->caps
[0] & USB4_MARGIN_CAP_0_ALL_LANES
;
509 static enum usb4_margin_cap_voltage_indp
510 independent_voltage_margins(const struct tb_margining
*margining
)
512 if (margining
->gen
< 4) {
513 switch (FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK
, margining
->caps
[0])) {
514 case USB4_MARGIN_CAP_0_VOLTAGE_MIN
:
515 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN
;
516 case USB4_MARGIN_CAP_0_VOLTAGE_HL
:
517 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL
;
518 case USB4_MARGIN_CAP_1_TIME_BOTH
:
519 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH
;
522 switch (FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_INDP_MASK
, margining
->caps
[2])) {
523 case USB4_MARGIN_CAP_2_VOLTAGE_MIN
:
524 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN
;
525 case USB4_MARGIN_CAP_2_VOLTAGE_BOTH
:
526 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH
;
529 return USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN
;
532 static bool supports_time(const struct tb_margining
*margining
)
534 if (margining
->gen
< 4)
535 return margining
->caps
[0] & USB4_MARGIN_CAP_0_TIME
;
536 return margining
->caps
[2] & USB4_MARGIN_CAP_2_TIME
;
539 /* Only applicable if supports_time() returns true */
540 static enum usb4_margin_cap_time_indp
541 independent_time_margins(const struct tb_margining
*margining
)
543 if (margining
->gen
< 4) {
544 switch (FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK
, margining
->caps
[1])) {
545 case USB4_MARGIN_CAP_1_TIME_MIN
:
546 return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN
;
547 case USB4_MARGIN_CAP_1_TIME_LR
:
548 return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR
;
549 case USB4_MARGIN_CAP_1_TIME_BOTH
:
550 return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH
;
553 switch (FIELD_GET(USB4_MARGIN_CAP_2_TIME_INDP_MASK
, margining
->caps
[2])) {
554 case USB4_MARGIN_CAP_2_TIME_MIN
:
555 return USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN
;
556 case USB4_MARGIN_CAP_2_TIME_BOTH
:
557 return USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH
;
560 return USB4_MARGIN_CAP_TIME_INDP_UNKNOWN
;
564 supports_optional_voltage_offset_range(const struct tb_margining
*margining
)
566 return margining
->caps
[0] & USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT
;
570 margining_ber_level_write(struct file
*file
, const char __user
*user_buf
,
571 size_t count
, loff_t
*ppos
)
573 struct seq_file
*s
= file
->private_data
;
574 struct tb_margining
*margining
= s
->private;
575 struct tb
*tb
= margining
->port
->sw
->tb
;
580 if (mutex_lock_interruptible(&tb
->lock
))
583 if (margining
->software
) {
588 buf
= validate_and_copy_from_user(user_buf
, &count
);
594 buf
[count
- 1] = '\0';
596 ret
= kstrtouint(buf
, 10, &val
);
600 if (val
< margining
->min_ber_level
||
601 val
> margining
->max_ber_level
) {
606 margining
->ber_level
= val
;
609 free_page((unsigned long)buf
);
611 mutex_unlock(&tb
->lock
);
613 return ret
< 0 ? ret
: count
;
616 static void ber_level_show(struct seq_file
*s
, unsigned int val
)
619 seq_printf(s
, "3 * 1e%d (%u)\n", -12 + (val
+ 1) / 2, val
);
621 seq_printf(s
, "1e%d (%u)\n", -12 + val
/ 2, val
);
624 static int margining_ber_level_show(struct seq_file
*s
, void *not_used
)
626 const struct tb_margining
*margining
= s
->private;
628 if (margining
->software
)
630 ber_level_show(s
, margining
->ber_level
);
633 DEBUGFS_ATTR_RW(margining_ber_level
);
635 static int margining_caps_show(struct seq_file
*s
, void *not_used
)
637 struct tb_margining
*margining
= s
->private;
638 struct tb
*tb
= margining
->port
->sw
->tb
;
641 if (mutex_lock_interruptible(&tb
->lock
))
644 /* Dump the raw caps first */
645 for (int i
= 0; i
< ARRAY_SIZE(margining
->caps
); i
++)
646 seq_printf(s
, "0x%08x\n", margining
->caps
[i
]);
648 seq_printf(s
, "# software margining: %s\n",
649 supports_software(margining
) ? "yes" : "no");
650 if (supports_hardware(margining
)) {
651 seq_puts(s
, "# hardware margining: yes\n");
652 seq_puts(s
, "# minimum BER level contour: ");
653 ber_level_show(s
, margining
->min_ber_level
);
654 seq_puts(s
, "# maximum BER level contour: ");
655 ber_level_show(s
, margining
->max_ber_level
);
657 seq_puts(s
, "# hardware margining: no\n");
660 seq_printf(s
, "# all lanes simultaneously: %s\n",
661 str_yes_no(all_lanes(margining
)));
662 seq_printf(s
, "# voltage margin steps: %u\n",
663 margining
->voltage_steps
);
664 seq_printf(s
, "# maximum voltage offset: %u mV\n",
665 margining
->max_voltage_offset
);
666 seq_printf(s
, "# optional voltage offset range support: %s\n",
667 str_yes_no(supports_optional_voltage_offset_range(margining
)));
668 if (supports_optional_voltage_offset_range(margining
)) {
669 seq_printf(s
, "# voltage margin steps, optional range: %u\n",
670 margining
->voltage_steps_optional_range
);
671 seq_printf(s
, "# maximum voltage offset, optional range: %u mV\n",
672 margining
->max_voltage_offset_optional_range
);
675 switch (independent_voltage_margins(margining
)) {
676 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN
:
677 seq_puts(s
, "# returns minimum between high and low voltage margins\n");
679 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL
:
680 seq_puts(s
, "# returns high or low voltage margin\n");
682 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH
:
683 seq_puts(s
, "# returns both high and low margins\n");
685 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN
:
686 seq_puts(s
, "# returns minimum between high and low voltage margins in both lower and upper eye\n");
688 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH
:
689 seq_puts(s
, "# returns both high and low margins of both upper and lower eye\n");
691 case USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN
:
692 tb_port_warn(margining
->port
,
693 "failed to parse independent voltage margining capabilities\n");
698 if (supports_time(margining
)) {
699 seq_puts(s
, "# time margining: yes\n");
700 seq_printf(s
, "# time margining is destructive: %s\n",
701 str_yes_no(margining
->caps
[1] & USB4_MARGIN_CAP_1_TIME_DESTR
));
703 switch (independent_time_margins(margining
)) {
704 case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN
:
705 seq_puts(s
, "# returns minimum between left and right time margins\n");
707 case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR
:
708 seq_puts(s
, "# returns left or right margin\n");
710 case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH
:
711 seq_puts(s
, "# returns both left and right margins\n");
713 case USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN
:
714 seq_puts(s
, "# returns minimum between left and right time margins in both lower and upper eye\n");
716 case USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH
:
717 seq_puts(s
, "# returns both left and right margins of both upper and lower eye\n");
719 case USB4_MARGIN_CAP_TIME_INDP_UNKNOWN
:
720 tb_port_warn(margining
->port
,
721 "failed to parse independent time margining capabilities\n");
726 seq_printf(s
, "# time margin steps: %u\n",
727 margining
->time_steps
);
728 seq_printf(s
, "# maximum time offset: %u mUI\n",
729 margining
->max_time_offset
);
731 seq_puts(s
, "# time margining: no\n");
735 mutex_unlock(&tb
->lock
);
738 DEBUGFS_ATTR_RO(margining_caps
);
740 static const struct {
741 enum usb4_margining_lane lane
;
745 .lane
= USB4_MARGINING_LANE_RX0
,
749 .lane
= USB4_MARGINING_LANE_RX1
,
753 .lane
= USB4_MARGINING_LANE_RX2
,
757 .lane
= USB4_MARGINING_LANE_ALL
,
763 margining_lanes_write(struct file
*file
, const char __user
*user_buf
,
764 size_t count
, loff_t
*ppos
)
766 struct seq_file
*s
= file
->private_data
;
767 struct tb_margining
*margining
= s
->private;
768 struct tb_port
*port
= margining
->port
;
769 struct tb
*tb
= port
->sw
->tb
;
773 buf
= validate_and_copy_from_user(user_buf
, &count
);
777 buf
[count
- 1] = '\0';
779 for (int i
= 0; i
< ARRAY_SIZE(lane_names
); i
++) {
780 if (!strcmp(buf
, lane_names
[i
].name
)) {
781 lane
= lane_names
[i
].lane
;
786 free_page((unsigned long)buf
);
791 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
792 if (lane
== USB4_MARGINING_LANE_ALL
&& !all_lanes(margining
))
795 * Enabling on RX2 requires that it is supported by the
798 if (lane
== USB4_MARGINING_LANE_RX2
&& !margining
->asym_rx
)
801 margining
->lanes
= lane
;
807 static int margining_lanes_show(struct seq_file
*s
, void *not_used
)
809 struct tb_margining
*margining
= s
->private;
810 struct tb_port
*port
= margining
->port
;
811 struct tb
*tb
= port
->sw
->tb
;
813 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
814 for (int i
= 0; i
< ARRAY_SIZE(lane_names
); i
++) {
815 if (lane_names
[i
].lane
== USB4_MARGINING_LANE_ALL
&&
816 !all_lanes(margining
))
818 if (lane_names
[i
].lane
== USB4_MARGINING_LANE_RX2
&&
825 if (lane_names
[i
].lane
== margining
->lanes
)
826 seq_printf(s
, "[%s]", lane_names
[i
].name
);
828 seq_printf(s
, "%s", lane_names
[i
].name
);
835 DEBUGFS_ATTR_RW(margining_lanes
);
838 margining_voltage_time_offset_write(struct file
*file
,
839 const char __user
*user_buf
,
840 size_t count
, loff_t
*ppos
)
842 struct seq_file
*s
= file
->private_data
;
843 struct tb_margining
*margining
= s
->private;
844 struct tb
*tb
= margining
->port
->sw
->tb
;
845 unsigned int max_margin
;
849 ret
= kstrtouint_from_user(user_buf
, count
, 10, &val
);
853 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
854 if (!margining
->software
)
858 max_margin
= margining
->time_steps
;
860 if (margining
->optional_voltage_offset_range
)
861 max_margin
= margining
->voltage_steps_optional_range
;
863 max_margin
= margining
->voltage_steps
;
865 margining
->voltage_time_offset
= clamp(val
, 0, max_margin
);
871 static int margining_voltage_time_offset_show(struct seq_file
*s
,
874 const struct tb_margining
*margining
= s
->private;
875 struct tb
*tb
= margining
->port
->sw
->tb
;
877 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
878 if (!margining
->software
)
881 seq_printf(s
, "%d\n", margining
->voltage_time_offset
);
886 DEBUGFS_ATTR_RW(margining_voltage_time_offset
);
889 margining_error_counter_write(struct file
*file
, const char __user
*user_buf
,
890 size_t count
, loff_t
*ppos
)
892 enum usb4_margin_sw_error_counter error_counter
;
893 struct seq_file
*s
= file
->private_data
;
894 struct tb_margining
*margining
= s
->private;
895 struct tb
*tb
= margining
->port
->sw
->tb
;
898 buf
= validate_and_copy_from_user(user_buf
, &count
);
902 buf
[count
- 1] = '\0';
904 if (!strcmp(buf
, "nop"))
905 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_NOP
;
906 else if (!strcmp(buf
, "clear"))
907 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
;
908 else if (!strcmp(buf
, "start"))
909 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_START
;
910 else if (!strcmp(buf
, "stop"))
911 error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_STOP
;
915 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
916 if (!margining
->software
)
919 margining
->error_counter
= error_counter
;
925 static int margining_error_counter_show(struct seq_file
*s
, void *not_used
)
927 const struct tb_margining
*margining
= s
->private;
928 struct tb
*tb
= margining
->port
->sw
->tb
;
930 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
931 if (!margining
->software
)
934 switch (margining
->error_counter
) {
935 case USB4_MARGIN_SW_ERROR_COUNTER_NOP
:
936 seq_puts(s
, "[nop] clear start stop\n");
938 case USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
:
939 seq_puts(s
, "nop [clear] start stop\n");
941 case USB4_MARGIN_SW_ERROR_COUNTER_START
:
942 seq_puts(s
, "nop clear [start] stop\n");
944 case USB4_MARGIN_SW_ERROR_COUNTER_STOP
:
945 seq_puts(s
, "nop clear start [stop]\n");
952 DEBUGFS_ATTR_RW(margining_error_counter
);
955 margining_dwell_time_write(struct file
*file
, const char __user
*user_buf
,
956 size_t count
, loff_t
*ppos
)
958 struct seq_file
*s
= file
->private_data
;
959 struct tb_margining
*margining
= s
->private;
960 struct tb
*tb
= margining
->port
->sw
->tb
;
964 ret
= kstrtouint_from_user(user_buf
, count
, 10, &val
);
968 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
969 if (!margining
->software
)
972 margining
->dwell_time
= clamp(val
, MIN_DWELL_TIME
, MAX_DWELL_TIME
);
978 static int margining_dwell_time_show(struct seq_file
*s
, void *not_used
)
980 struct tb_margining
*margining
= s
->private;
981 struct tb
*tb
= margining
->port
->sw
->tb
;
983 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
984 if (!margining
->software
)
987 seq_printf(s
, "%d\n", margining
->dwell_time
);
992 DEBUGFS_ATTR_RW(margining_dwell_time
);
995 margining_optional_voltage_offset_write(struct file
*file
, const char __user
*user_buf
,
996 size_t count
, loff_t
*ppos
)
998 struct seq_file
*s
= file
->private_data
;
999 struct tb_margining
*margining
= s
->private;
1000 struct tb
*tb
= margining
->port
->sw
->tb
;
1004 ret
= kstrtobool_from_user(user_buf
, count
, &val
);
1008 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
1009 margining
->optional_voltage_offset_range
= val
;
1015 static int margining_optional_voltage_offset_show(struct seq_file
*s
,
1018 struct tb_margining
*margining
= s
->private;
1019 struct tb
*tb
= margining
->port
->sw
->tb
;
1021 scoped_cond_guard(mutex_intr
, return -ERESTARTSYS
, &tb
->lock
) {
1022 seq_printf(s
, "%u\n", margining
->optional_voltage_offset_range
);
1027 DEBUGFS_ATTR_RW(margining_optional_voltage_offset
);
1029 static ssize_t
margining_mode_write(struct file
*file
,
1030 const char __user
*user_buf
,
1031 size_t count
, loff_t
*ppos
)
1033 struct seq_file
*s
= file
->private_data
;
1034 struct tb_margining
*margining
= s
->private;
1035 struct tb
*tb
= margining
->port
->sw
->tb
;
1039 buf
= validate_and_copy_from_user(user_buf
, &count
);
1041 return PTR_ERR(buf
);
1043 buf
[count
- 1] = '\0';
1045 if (mutex_lock_interruptible(&tb
->lock
)) {
1050 if (!strcmp(buf
, "software")) {
1051 if (supports_software(margining
))
1052 margining
->software
= true;
1055 } else if (!strcmp(buf
, "hardware")) {
1056 if (supports_hardware(margining
))
1057 margining
->software
= false;
1064 mutex_unlock(&tb
->lock
);
1067 free_page((unsigned long)buf
);
1068 return ret
? ret
: count
;
1071 static int margining_mode_show(struct seq_file
*s
, void *not_used
)
1073 struct tb_margining
*margining
= s
->private;
1074 struct tb
*tb
= margining
->port
->sw
->tb
;
1075 const char *space
= "";
1077 if (mutex_lock_interruptible(&tb
->lock
))
1078 return -ERESTARTSYS
;
1080 if (supports_software(margining
)) {
1081 if (margining
->software
)
1082 seq_puts(s
, "[software]");
1084 seq_puts(s
, "software");
1087 if (supports_hardware(margining
)) {
1088 if (margining
->software
)
1089 seq_printf(s
, "%shardware", space
);
1091 seq_printf(s
, "%s[hardware]", space
);
1094 mutex_unlock(&tb
->lock
);
1099 DEBUGFS_ATTR_RW(margining_mode
);
1101 static int margining_run_sw(struct tb_margining
*margining
,
1102 struct usb4_port_margining_params
*params
)
1104 u32 nsamples
= margining
->dwell_time
/ DWELL_SAMPLE_INTERVAL
;
1107 ret
= usb4_port_sw_margin(margining
->port
, margining
->target
, margining
->index
,
1108 params
, margining
->results
);
1112 for (i
= 0; i
<= nsamples
; i
++) {
1115 ret
= usb4_port_sw_margin_errors(margining
->port
, margining
->target
,
1116 margining
->index
, &margining
->results
[1]);
1120 if (margining
->lanes
== USB4_MARGINING_LANE_RX0
)
1121 errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK
,
1122 margining
->results
[1]);
1123 else if (margining
->lanes
== USB4_MARGINING_LANE_RX1
)
1124 errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK
,
1125 margining
->results
[1]);
1126 else if (margining
->lanes
== USB4_MARGINING_LANE_RX2
)
1127 errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK
,
1128 margining
->results
[1]);
1129 else if (margining
->lanes
== USB4_MARGINING_LANE_ALL
)
1130 errors
= margining
->results
[1];
1132 /* Any errors stop the test */
1136 fsleep(DWELL_SAMPLE_INTERVAL
* USEC_PER_MSEC
);
1141 * Stop the counters but don't clear them to allow the
1142 * different error counter configurations.
1144 margining_modify_error_counter(margining
, margining
->lanes
,
1145 USB4_MARGIN_SW_ERROR_COUNTER_STOP
);
1149 static int validate_margining(struct tb_margining
*margining
)
1152 * For running on RX2 the link must be asymmetric with 3
1153 * receivers. Because this is can change dynamically, check it
1154 * here before we start the margining and report back error if
1155 * expectations are not met.
1157 if (margining
->lanes
== USB4_MARGINING_LANE_RX2
) {
1160 ret
= tb_port_get_link_width(margining
->port
);
1163 if (ret
!= TB_LINK_WIDTH_ASYM_RX
) {
1164 tb_port_warn(margining
->port
, "link is %s expected %s",
1166 tb_width_name(TB_LINK_WIDTH_ASYM_RX
));
1174 static int margining_run_write(void *data
, u64 val
)
1176 struct tb_margining
*margining
= data
;
1177 struct tb_port
*port
= margining
->port
;
1178 struct device
*dev
= margining
->dev
;
1179 struct tb_switch
*sw
= port
->sw
;
1180 struct tb_switch
*down_sw
;
1181 struct tb
*tb
= sw
->tb
;
1187 pm_runtime_get_sync(dev
);
1189 if (mutex_lock_interruptible(&tb
->lock
)) {
1194 ret
= validate_margining(margining
);
1198 if (tb_is_upstream_port(port
))
1200 else if (port
->remote
)
1201 down_sw
= port
->remote
->sw
;
1207 * CL states may interfere with lane margining so
1208 * disable them temporarily now.
1210 ret
= tb_switch_clx_disable(down_sw
);
1212 tb_sw_warn(down_sw
, "failed to disable CL states\n");
1218 /* Clear the results */
1219 memset(margining
->results
, 0, sizeof(margining
->results
));
1221 if (margining
->software
) {
1222 struct usb4_port_margining_params params
= {
1223 .error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
,
1224 .lanes
= margining
->lanes
,
1225 .time
= margining
->time
,
1226 .voltage_time_offset
= margining
->voltage_time_offset
,
1227 .right_high
= margining
->right_high
,
1228 .upper_eye
= margining
->upper_eye
,
1229 .optional_voltage_offset_range
= margining
->optional_voltage_offset_range
,
1233 "running software %s lane margining for %s lanes %u\n",
1234 margining
->time
? "time" : "voltage", dev_name(dev
),
1237 ret
= margining_run_sw(margining
, ¶ms
);
1239 struct usb4_port_margining_params params
= {
1240 .ber_level
= margining
->ber_level
,
1241 .lanes
= margining
->lanes
,
1242 .time
= margining
->time
,
1243 .right_high
= margining
->right_high
,
1244 .upper_eye
= margining
->upper_eye
,
1245 .optional_voltage_offset_range
= margining
->optional_voltage_offset_range
,
1249 "running hardware %s lane margining for %s lanes %u\n",
1250 margining
->time
? "time" : "voltage", dev_name(dev
),
1253 ret
= usb4_port_hw_margin(port
, margining
->target
, margining
->index
, ¶ms
,
1254 margining
->results
, ARRAY_SIZE(margining
->results
));
1258 tb_switch_clx_enable(down_sw
, clx
);
1260 mutex_unlock(&tb
->lock
);
1262 pm_runtime_mark_last_busy(dev
);
1263 pm_runtime_put_autosuspend(dev
);
1267 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops
, NULL
, margining_run_write
,
1270 static ssize_t
margining_results_write(struct file
*file
,
1271 const char __user
*user_buf
,
1272 size_t count
, loff_t
*ppos
)
1274 struct seq_file
*s
= file
->private_data
;
1275 struct tb_margining
*margining
= s
->private;
1276 struct tb
*tb
= margining
->port
->sw
->tb
;
1278 if (mutex_lock_interruptible(&tb
->lock
))
1279 return -ERESTARTSYS
;
1281 /* Just clear the results */
1282 memset(margining
->results
, 0, sizeof(margining
->results
));
1284 if (margining
->software
) {
1285 /* Clear the error counters */
1286 margining_modify_error_counter(margining
,
1287 USB4_MARGINING_LANE_ALL
,
1288 USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
);
1291 mutex_unlock(&tb
->lock
);
1295 static void voltage_margin_show(struct seq_file
*s
,
1296 const struct tb_margining
*margining
, u8 val
)
1298 unsigned int tmp
, voltage
;
1300 tmp
= FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK
, val
);
1301 voltage
= tmp
* margining
->max_voltage_offset
/ margining
->voltage_steps
;
1302 seq_printf(s
, "%u mV (%u)", voltage
, tmp
);
1303 if (val
& USB4_MARGIN_HW_RES_EXCEEDS
)
1304 seq_puts(s
, " exceeds maximum");
1306 if (margining
->optional_voltage_offset_range
)
1307 seq_puts(s
, " optional voltage offset range enabled\n");
1310 static void time_margin_show(struct seq_file
*s
,
1311 const struct tb_margining
*margining
, u8 val
)
1313 unsigned int tmp
, interval
;
1315 tmp
= FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK
, val
);
1316 interval
= tmp
* margining
->max_time_offset
/ margining
->time_steps
;
1317 seq_printf(s
, "%u mUI (%u)", interval
, tmp
);
1318 if (val
& USB4_MARGIN_HW_RES_EXCEEDS
)
1319 seq_puts(s
, " exceeds maximum");
1323 static u8
margining_hw_result_val(const u32
*results
,
1324 enum usb4_margining_lane lane
,
1329 if (lane
== USB4_MARGINING_LANE_RX0
)
1331 else if (lane
== USB4_MARGINING_LANE_RX1
)
1332 val
= results
[1] >> USB4_MARGIN_HW_RES_LANE_SHIFT
;
1333 else if (lane
== USB4_MARGINING_LANE_RX2
)
1338 return right_high
? val
: val
>> USB4_MARGIN_HW_RES_LL_SHIFT
;
1341 static void margining_hw_result_format(struct seq_file
*s
,
1342 const struct tb_margining
*margining
,
1343 enum usb4_margining_lane lane
)
1347 if (margining
->time
) {
1348 val
= margining_hw_result_val(margining
->results
, lane
, true);
1349 seq_printf(s
, "# lane %u right time margin: ", lane
);
1350 time_margin_show(s
, margining
, val
);
1351 val
= margining_hw_result_val(margining
->results
, lane
, false);
1352 seq_printf(s
, "# lane %u left time margin: ", lane
);
1353 time_margin_show(s
, margining
, val
);
1355 val
= margining_hw_result_val(margining
->results
, lane
, true);
1356 seq_printf(s
, "# lane %u high voltage margin: ", lane
);
1357 voltage_margin_show(s
, margining
, val
);
1358 val
= margining_hw_result_val(margining
->results
, lane
, false);
1359 seq_printf(s
, "# lane %u low voltage margin: ", lane
);
1360 voltage_margin_show(s
, margining
, val
);
1364 static int margining_results_show(struct seq_file
*s
, void *not_used
)
1366 struct tb_margining
*margining
= s
->private;
1367 struct tb
*tb
= margining
->port
->sw
->tb
;
1369 if (mutex_lock_interruptible(&tb
->lock
))
1370 return -ERESTARTSYS
;
1372 /* Dump the raw results first */
1373 seq_printf(s
, "0x%08x\n", margining
->results
[0]);
1374 /* Only the hardware margining has two result dwords */
1375 if (!margining
->software
) {
1376 for (int i
= 1; i
< ARRAY_SIZE(margining
->results
); i
++)
1377 seq_printf(s
, "0x%08x\n", margining
->results
[i
]);
1379 if (margining
->lanes
== USB4_MARGINING_LANE_ALL
) {
1380 margining_hw_result_format(s
, margining
,
1381 USB4_MARGINING_LANE_RX0
);
1382 margining_hw_result_format(s
, margining
,
1383 USB4_MARGINING_LANE_RX1
);
1384 if (margining
->asym_rx
)
1385 margining_hw_result_format(s
, margining
,
1386 USB4_MARGINING_LANE_RX2
);
1388 margining_hw_result_format(s
, margining
,
1392 u32 lane_errors
, result
;
1394 seq_printf(s
, "0x%08x\n", margining
->results
[1]);
1396 result
= FIELD_GET(USB4_MARGIN_SW_LANES_MASK
, margining
->results
[0]);
1397 if (result
== USB4_MARGINING_LANE_RX0
||
1398 result
== USB4_MARGINING_LANE_ALL
) {
1399 lane_errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK
,
1400 margining
->results
[1]);
1401 seq_printf(s
, "# lane 0 errors: %u\n", lane_errors
);
1403 if (result
== USB4_MARGINING_LANE_RX1
||
1404 result
== USB4_MARGINING_LANE_ALL
) {
1405 lane_errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK
,
1406 margining
->results
[1]);
1407 seq_printf(s
, "# lane 1 errors: %u\n", lane_errors
);
1409 if (margining
->asym_rx
&&
1410 (result
== USB4_MARGINING_LANE_RX2
||
1411 result
== USB4_MARGINING_LANE_ALL
)) {
1412 lane_errors
= FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK
,
1413 margining
->results
[1]);
1414 seq_printf(s
, "# lane 2 errors: %u\n", lane_errors
);
1418 mutex_unlock(&tb
->lock
);
1421 DEBUGFS_ATTR_RW(margining_results
);
1423 static ssize_t
margining_test_write(struct file
*file
,
1424 const char __user
*user_buf
,
1425 size_t count
, loff_t
*ppos
)
1427 struct seq_file
*s
= file
->private_data
;
1428 struct tb_margining
*margining
= s
->private;
1429 struct tb
*tb
= margining
->port
->sw
->tb
;
1433 buf
= validate_and_copy_from_user(user_buf
, &count
);
1435 return PTR_ERR(buf
);
1437 buf
[count
- 1] = '\0';
1439 if (mutex_lock_interruptible(&tb
->lock
)) {
1444 if (!strcmp(buf
, "time") && supports_time(margining
))
1445 margining
->time
= true;
1446 else if (!strcmp(buf
, "voltage"))
1447 margining
->time
= false;
1451 mutex_unlock(&tb
->lock
);
1454 free_page((unsigned long)buf
);
1455 return ret
? ret
: count
;
1458 static int margining_test_show(struct seq_file
*s
, void *not_used
)
1460 struct tb_margining
*margining
= s
->private;
1461 struct tb
*tb
= margining
->port
->sw
->tb
;
1463 if (mutex_lock_interruptible(&tb
->lock
))
1464 return -ERESTARTSYS
;
1466 if (supports_time(margining
)) {
1467 if (margining
->time
)
1468 seq_puts(s
, "voltage [time]\n");
1470 seq_puts(s
, "[voltage] time\n");
1472 seq_puts(s
, "[voltage]\n");
1475 mutex_unlock(&tb
->lock
);
1478 DEBUGFS_ATTR_RW(margining_test
);
1480 static ssize_t
margining_margin_write(struct file
*file
,
1481 const char __user
*user_buf
,
1482 size_t count
, loff_t
*ppos
)
1484 struct seq_file
*s
= file
->private_data
;
1485 struct tb_margining
*margining
= s
->private;
1486 struct tb
*tb
= margining
->port
->sw
->tb
;
1490 buf
= validate_and_copy_from_user(user_buf
, &count
);
1492 return PTR_ERR(buf
);
1494 buf
[count
- 1] = '\0';
1496 if (mutex_lock_interruptible(&tb
->lock
)) {
1501 if (margining
->time
) {
1502 if (!strcmp(buf
, "left"))
1503 margining
->right_high
= false;
1504 else if (!strcmp(buf
, "right"))
1505 margining
->right_high
= true;
1509 if (!strcmp(buf
, "low"))
1510 margining
->right_high
= false;
1511 else if (!strcmp(buf
, "high"))
1512 margining
->right_high
= true;
1517 mutex_unlock(&tb
->lock
);
1520 free_page((unsigned long)buf
);
1521 return ret
? ret
: count
;
1524 static int margining_margin_show(struct seq_file
*s
, void *not_used
)
1526 struct tb_margining
*margining
= s
->private;
1527 struct tb
*tb
= margining
->port
->sw
->tb
;
1529 if (mutex_lock_interruptible(&tb
->lock
))
1530 return -ERESTARTSYS
;
1532 if (margining
->time
) {
1533 if (margining
->right_high
)
1534 seq_puts(s
, "left [right]\n");
1536 seq_puts(s
, "[left] right\n");
1538 if (margining
->right_high
)
1539 seq_puts(s
, "low [high]\n");
1541 seq_puts(s
, "[low] high\n");
1544 mutex_unlock(&tb
->lock
);
1547 DEBUGFS_ATTR_RW(margining_margin
);
1549 static ssize_t
margining_eye_write(struct file
*file
,
1550 const char __user
*user_buf
,
1551 size_t count
, loff_t
*ppos
)
1553 struct seq_file
*s
= file
->private_data
;
1554 struct tb_port
*port
= s
->private;
1555 struct usb4_port
*usb4
= port
->usb4
;
1556 struct tb
*tb
= port
->sw
->tb
;
1560 buf
= validate_and_copy_from_user(user_buf
, &count
);
1562 return PTR_ERR(buf
);
1564 buf
[count
- 1] = '\0';
1566 scoped_cond_guard(mutex_intr
, ret
= -ERESTARTSYS
, &tb
->lock
) {
1567 if (!strcmp(buf
, "lower"))
1568 usb4
->margining
->upper_eye
= false;
1569 else if (!strcmp(buf
, "upper"))
1570 usb4
->margining
->upper_eye
= true;
1575 free_page((unsigned long)buf
);
1576 return ret
? ret
: count
;
1579 static int margining_eye_show(struct seq_file
*s
, void *not_used
)
1581 struct tb_port
*port
= s
->private;
1582 struct usb4_port
*usb4
= port
->usb4
;
1583 struct tb
*tb
= port
->sw
->tb
;
1585 scoped_guard(mutex_intr
, &tb
->lock
) {
1586 if (usb4
->margining
->upper_eye
)
1587 seq_puts(s
, "lower [upper]\n");
1589 seq_puts(s
, "[lower] upper\n");
1594 return -ERESTARTSYS
;
1596 DEBUGFS_ATTR_RW(margining_eye
);
1598 static struct tb_margining
*margining_alloc(struct tb_port
*port
,
1600 enum usb4_sb_target target
,
1601 u8 index
, struct dentry
*parent
)
1603 struct tb_margining
*margining
;
1608 ret
= tb_port_get_link_generation(port
);
1610 tb_port_warn(port
, "failed to read link generation\n");
1614 margining
= kzalloc(sizeof(*margining
), GFP_KERNEL
);
1618 margining
->port
= port
;
1619 margining
->target
= target
;
1620 margining
->index
= index
;
1621 margining
->dev
= dev
;
1622 margining
->gen
= ret
;
1623 margining
->asym_rx
= tb_port_width_supported(port
, TB_LINK_WIDTH_ASYM_RX
);
1625 ret
= usb4_port_margining_caps(port
, target
, index
, margining
->caps
,
1626 ARRAY_SIZE(margining
->caps
));
1632 /* Set the initial mode */
1633 if (supports_software(margining
))
1634 margining
->software
= true;
1636 if (margining
->gen
< 4) {
1637 val
= FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK
, margining
->caps
[0]);
1638 margining
->voltage_steps
= val
;
1639 val
= FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK
, margining
->caps
[0]);
1640 margining
->max_voltage_offset
= 74 + val
* 2;
1642 val
= FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_STEPS_MASK
, margining
->caps
[2]);
1643 margining
->voltage_steps
= val
;
1644 val
= FIELD_GET(USB4_MARGIN_CAP_2_MAX_VOLTAGE_OFFSET_MASK
, margining
->caps
[2]);
1645 margining
->max_voltage_offset
= 74 + val
* 2;
1648 if (supports_optional_voltage_offset_range(margining
)) {
1649 val
= FIELD_GET(USB4_MARGIN_CAP_0_VOLT_STEPS_OPT_MASK
,
1650 margining
->caps
[0]);
1651 margining
->voltage_steps_optional_range
= val
;
1652 val
= FIELD_GET(USB4_MARGIN_CAP_1_MAX_VOLT_OFS_OPT_MASK
,
1653 margining
->caps
[1]);
1654 margining
->max_voltage_offset_optional_range
= 74 + val
* 2;
1657 if (supports_time(margining
)) {
1658 val
= FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK
, margining
->caps
[1]);
1659 margining
->time_steps
= val
;
1660 val
= FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK
, margining
->caps
[1]);
1662 * Store it as mUI (milli Unit Interval) because we want
1663 * to keep it as integer.
1665 margining
->max_time_offset
= 200 + 10 * val
;
1668 dir
= debugfs_create_dir("margining", parent
);
1669 if (supports_hardware(margining
)) {
1670 val
= FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK
, margining
->caps
[1]);
1671 margining
->min_ber_level
= val
;
1672 val
= FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK
, margining
->caps
[1]);
1673 margining
->max_ber_level
= val
;
1675 /* Set the default to minimum */
1676 margining
->ber_level
= margining
->min_ber_level
;
1678 debugfs_create_file("ber_level_contour", 0400, dir
, margining
,
1679 &margining_ber_level_fops
);
1681 debugfs_create_file("caps", 0400, dir
, margining
, &margining_caps_fops
);
1682 debugfs_create_file("lanes", 0600, dir
, margining
, &margining_lanes_fops
);
1683 debugfs_create_file("mode", 0600, dir
, margining
, &margining_mode_fops
);
1684 debugfs_create_file("run", 0600, dir
, margining
, &margining_run_fops
);
1685 debugfs_create_file("results", 0600, dir
, margining
,
1686 &margining_results_fops
);
1687 debugfs_create_file("test", 0600, dir
, margining
, &margining_test_fops
);
1688 if (independent_voltage_margins(margining
) == USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL
||
1689 (supports_time(margining
) &&
1690 independent_time_margins(margining
) == USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR
))
1691 debugfs_create_file("margin", 0600, dir
, margining
, &margining_margin_fops
);
1693 margining
->error_counter
= USB4_MARGIN_SW_ERROR_COUNTER_CLEAR
;
1694 margining
->dwell_time
= MIN_DWELL_TIME
;
1696 if (supports_optional_voltage_offset_range(margining
))
1697 debugfs_create_file("optional_voltage_offset", DEBUGFS_MODE
, dir
, margining
,
1698 &margining_optional_voltage_offset_fops
);
1700 if (supports_software(margining
)) {
1701 debugfs_create_file("voltage_time_offset", DEBUGFS_MODE
, dir
, margining
,
1702 &margining_voltage_time_offset_fops
);
1703 debugfs_create_file("error_counter", DEBUGFS_MODE
, dir
, margining
,
1704 &margining_error_counter_fops
);
1705 debugfs_create_file("dwell_time", DEBUGFS_MODE
, dir
, margining
,
1706 &margining_dwell_time_fops
);
1709 if (margining
->gen
>= 4)
1710 debugfs_create_file("eye", 0600, dir
, port
, &margining_eye_fops
);
1715 static void margining_port_init(struct tb_port
*port
)
1717 struct dentry
*parent
;
1723 snprintf(dir_name
, sizeof(dir_name
), "port%d", port
->port
);
1724 parent
= debugfs_lookup(dir_name
, port
->sw
->debugfs_dir
);
1725 port
->usb4
->margining
= margining_alloc(port
, &port
->usb4
->dev
,
1726 USB4_SB_TARGET_ROUTER
, 0,
1730 static void margining_port_remove(struct tb_port
*port
)
1732 struct dentry
*parent
;
1738 snprintf(dir_name
, sizeof(dir_name
), "port%d", port
->port
);
1739 parent
= debugfs_lookup(dir_name
, port
->sw
->debugfs_dir
);
1741 debugfs_lookup_and_remove("margining", parent
);
1743 kfree(port
->usb4
->margining
);
1744 port
->usb4
->margining
= NULL
;
1747 static void margining_switch_init(struct tb_switch
*sw
)
1749 struct tb_port
*upstream
, *downstream
;
1750 struct tb_switch
*parent_sw
;
1751 u64 route
= tb_route(sw
);
1756 upstream
= tb_upstream_port(sw
);
1757 parent_sw
= tb_switch_parent(sw
);
1758 downstream
= tb_port_at(route
, parent_sw
);
1760 margining_port_init(downstream
);
1761 margining_port_init(upstream
);
1764 static void margining_switch_remove(struct tb_switch
*sw
)
1766 struct tb_port
*upstream
, *downstream
;
1767 struct tb_switch
*parent_sw
;
1768 u64 route
= tb_route(sw
);
1773 upstream
= tb_upstream_port(sw
);
1774 parent_sw
= tb_switch_parent(sw
);
1775 downstream
= tb_port_at(route
, parent_sw
);
1777 margining_port_remove(upstream
);
1778 margining_port_remove(downstream
);
1781 static void margining_xdomain_init(struct tb_xdomain
*xd
)
1783 struct tb_switch
*parent_sw
;
1784 struct tb_port
*downstream
;
1786 parent_sw
= tb_xdomain_parent(xd
);
1787 downstream
= tb_port_at(xd
->route
, parent_sw
);
1789 margining_port_init(downstream
);
1792 static void margining_xdomain_remove(struct tb_xdomain
*xd
)
1794 struct tb_switch
*parent_sw
;
1795 struct tb_port
*downstream
;
1797 parent_sw
= tb_xdomain_parent(xd
);
1798 downstream
= tb_port_at(xd
->route
, parent_sw
);
1799 margining_port_remove(downstream
);
1802 static void margining_retimer_init(struct tb_retimer
*rt
, struct dentry
*debugfs_dir
)
1804 rt
->margining
= margining_alloc(rt
->port
, &rt
->dev
,
1805 USB4_SB_TARGET_RETIMER
, rt
->index
,
1809 static void margining_retimer_remove(struct tb_retimer
*rt
)
1811 kfree(rt
->margining
);
1812 rt
->margining
= NULL
;
1815 static inline void margining_switch_init(struct tb_switch
*sw
) { }
1816 static inline void margining_switch_remove(struct tb_switch
*sw
) { }
1817 static inline void margining_xdomain_init(struct tb_xdomain
*xd
) { }
1818 static inline void margining_xdomain_remove(struct tb_xdomain
*xd
) { }
1819 static inline void margining_retimer_init(struct tb_retimer
*rt
,
1820 struct dentry
*debugfs_dir
) { }
1821 static inline void margining_retimer_remove(struct tb_retimer
*rt
) { }
1824 static int port_clear_all_counters(struct tb_port
*port
)
1829 buf
= kcalloc(COUNTER_SET_LEN
* port
->config
.max_counters
, sizeof(u32
),
1834 ret
= tb_port_write(port
, buf
, TB_CFG_COUNTERS
, 0,
1835 COUNTER_SET_LEN
* port
->config
.max_counters
);
1841 static ssize_t
counters_write(struct file
*file
, const char __user
*user_buf
,
1842 size_t count
, loff_t
*ppos
)
1844 struct seq_file
*s
= file
->private_data
;
1845 struct tb_port
*port
= s
->private;
1846 struct tb_switch
*sw
= port
->sw
;
1847 struct tb
*tb
= port
->sw
->tb
;
1851 buf
= validate_and_copy_from_user(user_buf
, &count
);
1853 return PTR_ERR(buf
);
1855 pm_runtime_get_sync(&sw
->dev
);
1857 if (mutex_lock_interruptible(&tb
->lock
)) {
1862 /* If written delimiter only, clear all counters in one shot */
1863 if (buf
[0] == '\n') {
1864 ret
= port_clear_all_counters(port
);
1870 while (parse_line(&line
, &offset
, &val
, 1, 4)) {
1871 ret
= tb_port_write(port
, &val
, TB_CFG_COUNTERS
,
1878 mutex_unlock(&tb
->lock
);
1881 pm_runtime_mark_last_busy(&sw
->dev
);
1882 pm_runtime_put_autosuspend(&sw
->dev
);
1883 free_page((unsigned long)buf
);
1885 return ret
< 0 ? ret
: count
;
1888 static void cap_show_by_dw(struct seq_file
*s
, struct tb_switch
*sw
,
1889 struct tb_port
*port
, unsigned int cap
,
1890 unsigned int offset
, u8 cap_id
, u8 vsec_id
,
1896 for (i
= 0; i
< dwords
; i
++) {
1898 ret
= tb_port_read(port
, &data
, TB_CFG_PORT
, cap
+ offset
+ i
, 1);
1900 ret
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, cap
+ offset
+ i
, 1);
1902 seq_printf(s
, "0x%04x <not accessible>\n", cap
+ offset
+ i
);
1906 seq_printf(s
, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap
+ offset
+ i
,
1907 offset
+ i
, cap_id
, vsec_id
, data
);
1911 static void cap_show(struct seq_file
*s
, struct tb_switch
*sw
,
1912 struct tb_port
*port
, unsigned int cap
, u8 cap_id
,
1913 u8 vsec_id
, int length
)
1915 int ret
, offset
= 0;
1917 while (length
> 0) {
1918 int i
, dwords
= min(length
, TB_MAX_CONFIG_RW_LENGTH
);
1919 u32 data
[TB_MAX_CONFIG_RW_LENGTH
];
1922 ret
= tb_port_read(port
, data
, TB_CFG_PORT
, cap
+ offset
,
1925 ret
= tb_sw_read(sw
, data
, TB_CFG_SWITCH
, cap
+ offset
, dwords
);
1927 cap_show_by_dw(s
, sw
, port
, cap
, offset
, cap_id
, vsec_id
, length
);
1931 for (i
= 0; i
< dwords
; i
++) {
1932 seq_printf(s
, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n",
1933 cap
+ offset
+ i
, offset
+ i
,
1934 cap_id
, vsec_id
, data
[i
]);
1942 static void port_cap_show(struct tb_port
*port
, struct seq_file
*s
,
1945 struct tb_cap_any header
;
1950 ret
= tb_port_read(port
, &header
, TB_CFG_PORT
, cap
, 1);
1952 seq_printf(s
, "0x%04x <capability read failed>\n", cap
);
1956 switch (header
.basic
.cap
) {
1957 case TB_PORT_CAP_PHY
:
1958 length
= PORT_CAP_LANE_LEN
;
1961 case TB_PORT_CAP_TIME1
:
1962 if (usb4_switch_version(port
->sw
) < 2)
1963 length
= PORT_CAP_TMU_V1_LEN
;
1965 length
= PORT_CAP_TMU_V2_LEN
;
1968 case TB_PORT_CAP_POWER
:
1969 length
= PORT_CAP_POWER_LEN
;
1972 case TB_PORT_CAP_ADAP
:
1973 if (tb_port_is_pcie_down(port
) || tb_port_is_pcie_up(port
)) {
1974 if (usb4_switch_version(port
->sw
) < 2)
1975 length
= PORT_CAP_V1_PCIE_LEN
;
1977 length
= PORT_CAP_V2_PCIE_LEN
;
1978 } else if (tb_port_is_dpin(port
)) {
1979 if (usb4_switch_version(port
->sw
) < 2)
1980 length
= PORT_CAP_DP_V1_LEN
;
1982 length
= PORT_CAP_DP_V2_LEN
;
1983 } else if (tb_port_is_dpout(port
)) {
1984 length
= PORT_CAP_DP_V1_LEN
;
1985 } else if (tb_port_is_usb3_down(port
) ||
1986 tb_port_is_usb3_up(port
)) {
1987 length
= PORT_CAP_USB3_LEN
;
1989 seq_printf(s
, "0x%04x <unsupported capability 0x%02x>\n",
1990 cap
, header
.basic
.cap
);
1995 case TB_PORT_CAP_VSE
:
1996 if (!header
.extended_short
.length
) {
1997 ret
= tb_port_read(port
, (u32
*)&header
+ 1, TB_CFG_PORT
,
2000 seq_printf(s
, "0x%04x <capability read failed>\n",
2004 length
= header
.extended_long
.length
;
2005 vsec_id
= header
.extended_short
.vsec_id
;
2007 length
= header
.extended_short
.length
;
2008 vsec_id
= header
.extended_short
.vsec_id
;
2012 case TB_PORT_CAP_USB4
:
2013 length
= PORT_CAP_USB4_LEN
;
2017 seq_printf(s
, "0x%04x <unsupported capability 0x%02x>\n",
2018 cap
, header
.basic
.cap
);
2022 cap_show(s
, NULL
, port
, cap
, header
.basic
.cap
, vsec_id
, length
);
2025 static void port_caps_show(struct tb_port
*port
, struct seq_file
*s
)
2029 cap
= tb_port_next_cap(port
, 0);
2031 port_cap_show(port
, s
, cap
);
2032 cap
= tb_port_next_cap(port
, cap
);
2036 static int port_basic_regs_show(struct tb_port
*port
, struct seq_file
*s
)
2038 u32 data
[PORT_CAP_BASIC_LEN
];
2041 ret
= tb_port_read(port
, data
, TB_CFG_PORT
, 0, ARRAY_SIZE(data
));
2045 for (i
= 0; i
< ARRAY_SIZE(data
); i
++)
2046 seq_printf(s
, "0x%04x %4d 0x00 0x00 0x%08x\n", i
, i
, data
[i
]);
2051 static int port_regs_show(struct seq_file
*s
, void *not_used
)
2053 struct tb_port
*port
= s
->private;
2054 struct tb_switch
*sw
= port
->sw
;
2055 struct tb
*tb
= sw
->tb
;
2058 pm_runtime_get_sync(&sw
->dev
);
2060 if (mutex_lock_interruptible(&tb
->lock
)) {
2065 seq_puts(s
, "# offset relative_offset cap_id vs_cap_id value\n");
2067 ret
= port_basic_regs_show(port
, s
);
2071 port_caps_show(port
, s
);
2074 mutex_unlock(&tb
->lock
);
2076 pm_runtime_mark_last_busy(&sw
->dev
);
2077 pm_runtime_put_autosuspend(&sw
->dev
);
2081 DEBUGFS_ATTR_RW(port_regs
);
2083 static void switch_cap_show(struct tb_switch
*sw
, struct seq_file
*s
,
2086 struct tb_cap_any header
;
2090 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, cap
, 1);
2092 seq_printf(s
, "0x%04x <capability read failed>\n", cap
);
2096 if (header
.basic
.cap
== TB_SWITCH_CAP_VSE
) {
2097 if (!header
.extended_short
.length
) {
2098 ret
= tb_sw_read(sw
, (u32
*)&header
+ 1, TB_CFG_SWITCH
,
2101 seq_printf(s
, "0x%04x <capability read failed>\n",
2105 length
= header
.extended_long
.length
;
2107 length
= header
.extended_short
.length
;
2109 vsec_id
= header
.extended_short
.vsec_id
;
2111 if (header
.basic
.cap
== TB_SWITCH_CAP_TMU
) {
2112 length
= SWITCH_CAP_TMU_LEN
;
2114 seq_printf(s
, "0x%04x <unknown capability 0x%02x>\n",
2115 cap
, header
.basic
.cap
);
2120 cap_show(s
, sw
, NULL
, cap
, header
.basic
.cap
, vsec_id
, length
);
2123 static void switch_caps_show(struct tb_switch
*sw
, struct seq_file
*s
)
2127 cap
= tb_switch_next_cap(sw
, 0);
2129 switch_cap_show(sw
, s
, cap
);
2130 cap
= tb_switch_next_cap(sw
, cap
);
2134 static int switch_basic_regs_show(struct tb_switch
*sw
, struct seq_file
*s
)
2136 u32 data
[SWITCH_CAP_BASIC_LEN
];
2140 /* Only USB4 has the additional registers */
2141 if (tb_switch_is_usb4(sw
))
2142 dwords
= ARRAY_SIZE(data
);
2146 ret
= tb_sw_read(sw
, data
, TB_CFG_SWITCH
, 0, dwords
);
2150 for (i
= 0; i
< dwords
; i
++)
2151 seq_printf(s
, "0x%04x %4d 0x00 0x00 0x%08x\n", i
, i
, data
[i
]);
2156 static int switch_regs_show(struct seq_file
*s
, void *not_used
)
2158 struct tb_switch
*sw
= s
->private;
2159 struct tb
*tb
= sw
->tb
;
2162 pm_runtime_get_sync(&sw
->dev
);
2164 if (mutex_lock_interruptible(&tb
->lock
)) {
2169 seq_puts(s
, "# offset relative_offset cap_id vs_cap_id value\n");
2171 ret
= switch_basic_regs_show(sw
, s
);
2175 switch_caps_show(sw
, s
);
2178 mutex_unlock(&tb
->lock
);
2180 pm_runtime_mark_last_busy(&sw
->dev
);
2181 pm_runtime_put_autosuspend(&sw
->dev
);
2185 DEBUGFS_ATTR_RW(switch_regs
);
2187 static int path_show_one(struct tb_port
*port
, struct seq_file
*s
, int hopid
)
2192 ret
= tb_port_read(port
, data
, TB_CFG_HOPS
, hopid
* PATH_LEN
,
2195 seq_printf(s
, "0x%04x <not accessible>\n", hopid
* PATH_LEN
);
2199 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
2200 seq_printf(s
, "0x%04x %4d 0x%02x 0x%08x\n",
2201 hopid
* PATH_LEN
+ i
, i
, hopid
, data
[i
]);
2207 static int path_show(struct seq_file
*s
, void *not_used
)
2209 struct tb_port
*port
= s
->private;
2210 struct tb_switch
*sw
= port
->sw
;
2211 struct tb
*tb
= sw
->tb
;
2212 int start
, i
, ret
= 0;
2214 pm_runtime_get_sync(&sw
->dev
);
2216 if (mutex_lock_interruptible(&tb
->lock
)) {
2221 seq_puts(s
, "# offset relative_offset in_hop_id value\n");
2223 /* NHI and lane adapters have entry for path 0 */
2224 if (tb_port_is_null(port
) || tb_port_is_nhi(port
)) {
2225 ret
= path_show_one(port
, s
, 0);
2230 start
= tb_port_is_nhi(port
) ? 1 : TB_PATH_MIN_HOPID
;
2232 for (i
= start
; i
<= port
->config
.max_in_hop_id
; i
++) {
2233 ret
= path_show_one(port
, s
, i
);
2239 mutex_unlock(&tb
->lock
);
2241 pm_runtime_mark_last_busy(&sw
->dev
);
2242 pm_runtime_put_autosuspend(&sw
->dev
);
2246 DEBUGFS_ATTR_RO(path
);
2248 static int counter_set_regs_show(struct tb_port
*port
, struct seq_file
*s
,
2251 u32 data
[COUNTER_SET_LEN
];
2254 ret
= tb_port_read(port
, data
, TB_CFG_COUNTERS
,
2255 counter
* COUNTER_SET_LEN
, ARRAY_SIZE(data
));
2257 seq_printf(s
, "0x%04x <not accessible>\n",
2258 counter
* COUNTER_SET_LEN
);
2262 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
2263 seq_printf(s
, "0x%04x %4d 0x%02x 0x%08x\n",
2264 counter
* COUNTER_SET_LEN
+ i
, i
, counter
, data
[i
]);
2270 static int counters_show(struct seq_file
*s
, void *not_used
)
2272 struct tb_port
*port
= s
->private;
2273 struct tb_switch
*sw
= port
->sw
;
2274 struct tb
*tb
= sw
->tb
;
2277 pm_runtime_get_sync(&sw
->dev
);
2279 if (mutex_lock_interruptible(&tb
->lock
)) {
2284 seq_puts(s
, "# offset relative_offset counter_id value\n");
2286 for (i
= 0; i
< port
->config
.max_counters
; i
++) {
2287 ret
= counter_set_regs_show(port
, s
, i
);
2292 mutex_unlock(&tb
->lock
);
2295 pm_runtime_mark_last_busy(&sw
->dev
);
2296 pm_runtime_put_autosuspend(&sw
->dev
);
2300 DEBUGFS_ATTR_RW(counters
);
2302 static int sb_regs_show(struct tb_port
*port
, const struct sb_reg
*sb_regs
,
2303 size_t size
, enum usb4_sb_target target
, u8 index
,
2308 seq_puts(s
, "# register value\n");
2310 for (i
= 0; i
< size
; i
++) {
2311 const struct sb_reg
*regs
= &sb_regs
[i
];
2315 memset(data
, 0, sizeof(data
));
2316 ret
= usb4_port_sb_read(port
, target
, index
, regs
->reg
, data
,
2321 seq_printf(s
, "0x%02x", regs
->reg
);
2322 for (j
= 0; j
< regs
->size
; j
++)
2323 seq_printf(s
, " 0x%02x", data
[j
]);
2330 static int port_sb_regs_show(struct seq_file
*s
, void *not_used
)
2332 struct tb_port
*port
= s
->private;
2333 struct tb_switch
*sw
= port
->sw
;
2334 struct tb
*tb
= sw
->tb
;
2337 pm_runtime_get_sync(&sw
->dev
);
2339 if (mutex_lock_interruptible(&tb
->lock
)) {
2344 ret
= sb_regs_show(port
, port_sb_regs
, ARRAY_SIZE(port_sb_regs
),
2345 USB4_SB_TARGET_ROUTER
, 0, s
);
2347 mutex_unlock(&tb
->lock
);
2349 pm_runtime_mark_last_busy(&sw
->dev
);
2350 pm_runtime_put_autosuspend(&sw
->dev
);
2354 DEBUGFS_ATTR_RW(port_sb_regs
);
2357 * tb_switch_debugfs_init() - Add debugfs entries for router
2358 * @sw: Pointer to the router
2360 * Adds debugfs directories and files for given router.
2362 void tb_switch_debugfs_init(struct tb_switch
*sw
)
2364 struct dentry
*debugfs_dir
;
2365 struct tb_port
*port
;
2367 debugfs_dir
= debugfs_create_dir(dev_name(&sw
->dev
), tb_debugfs_root
);
2368 sw
->debugfs_dir
= debugfs_dir
;
2369 debugfs_create_file("regs", DEBUGFS_MODE
, debugfs_dir
, sw
,
2372 tb_switch_for_each_port(sw
, port
) {
2373 struct dentry
*debugfs_dir
;
2378 if (port
->config
.type
== TB_TYPE_INACTIVE
)
2381 snprintf(dir_name
, sizeof(dir_name
), "port%d", port
->port
);
2382 debugfs_dir
= debugfs_create_dir(dir_name
, sw
->debugfs_dir
);
2383 debugfs_create_file("regs", DEBUGFS_MODE
, debugfs_dir
,
2384 port
, &port_regs_fops
);
2385 debugfs_create_file("path", 0400, debugfs_dir
, port
,
2387 if (port
->config
.counters_support
)
2388 debugfs_create_file("counters", 0600, debugfs_dir
, port
,
2391 debugfs_create_file("sb_regs", DEBUGFS_MODE
, debugfs_dir
,
2392 port
, &port_sb_regs_fops
);
2395 margining_switch_init(sw
);
2399 * tb_switch_debugfs_remove() - Remove all router debugfs entries
2400 * @sw: Pointer to the router
2402 * Removes all previously added debugfs entries under this router.
2404 void tb_switch_debugfs_remove(struct tb_switch
*sw
)
2406 margining_switch_remove(sw
);
2407 debugfs_remove_recursive(sw
->debugfs_dir
);
2410 void tb_xdomain_debugfs_init(struct tb_xdomain
*xd
)
2412 margining_xdomain_init(xd
);
2415 void tb_xdomain_debugfs_remove(struct tb_xdomain
*xd
)
2417 margining_xdomain_remove(xd
);
2421 * tb_service_debugfs_init() - Add debugfs directory for service
2422 * @svc: Thunderbolt service pointer
2424 * Adds debugfs directory for service.
2426 void tb_service_debugfs_init(struct tb_service
*svc
)
2428 svc
->debugfs_dir
= debugfs_create_dir(dev_name(&svc
->dev
),
2433 * tb_service_debugfs_remove() - Remove service debugfs directory
2434 * @svc: Thunderbolt service pointer
2436 * Removes the previously created debugfs directory for @svc.
2438 void tb_service_debugfs_remove(struct tb_service
*svc
)
2440 debugfs_remove_recursive(svc
->debugfs_dir
);
2441 svc
->debugfs_dir
= NULL
;
2444 static int retimer_sb_regs_show(struct seq_file
*s
, void *not_used
)
2446 struct tb_retimer
*rt
= s
->private;
2447 struct tb
*tb
= rt
->tb
;
2450 pm_runtime_get_sync(&rt
->dev
);
2452 if (mutex_lock_interruptible(&tb
->lock
)) {
2457 ret
= sb_regs_show(rt
->port
, retimer_sb_regs
, ARRAY_SIZE(retimer_sb_regs
),
2458 USB4_SB_TARGET_RETIMER
, rt
->index
, s
);
2460 mutex_unlock(&tb
->lock
);
2462 pm_runtime_mark_last_busy(&rt
->dev
);
2463 pm_runtime_put_autosuspend(&rt
->dev
);
2467 DEBUGFS_ATTR_RW(retimer_sb_regs
);
2470 * tb_retimer_debugfs_init() - Add debugfs directory for retimer
2471 * @rt: Pointer to retimer structure
2473 * Adds and populates retimer debugfs directory.
2475 void tb_retimer_debugfs_init(struct tb_retimer
*rt
)
2477 struct dentry
*debugfs_dir
;
2479 debugfs_dir
= debugfs_create_dir(dev_name(&rt
->dev
), tb_debugfs_root
);
2480 debugfs_create_file("sb_regs", DEBUGFS_MODE
, debugfs_dir
, rt
,
2481 &retimer_sb_regs_fops
);
2482 margining_retimer_init(rt
, debugfs_dir
);
2486 * tb_retimer_debugfs_remove() - Remove retimer debugfs directory
2487 * @rt: Pointer to retimer structure
2489 * Removes the retimer debugfs directory along with its contents.
2491 void tb_retimer_debugfs_remove(struct tb_retimer
*rt
)
2493 debugfs_lookup_and_remove(dev_name(&rt
->dev
), tb_debugfs_root
);
2494 margining_retimer_remove(rt
);
2497 void tb_debugfs_init(void)
2499 tb_debugfs_root
= debugfs_create_dir("thunderbolt", NULL
);
2502 void tb_debugfs_exit(void)
2504 debugfs_remove_recursive(tb_debugfs_root
);