thunderbolt: Add support for extended error counters
Subsystems (2)
cover thunderbolt: Add support for extended error counters
Hi all,
USB4 v2 introduced extended error counters for lane margining. Compared to the USB4 v1 these are 16-bit (vs. 4-bit in v1). This series makes them available for debugfs software lane margining.
Anurag (3):
thunderbolt: Make software margining error counter configurable
thunderbolt: Fix sideband register access in usb4_port_sw_margin_errors()
thunderbolt: Extract per-lane extended error counters in software margining
Rene Sapiens (2):
thunderbolt: Define number of dwords for hardware margining results
thunderbolt: Add extended error counter support for software lane margining
drivers/thunderbolt/debugfs.c | 126 +++++++++++++++++++++++++++------- drivers/thunderbolt/sb_regs.h | 2 + drivers/thunderbolt/tb.h | 2 +- drivers/thunderbolt/usb4.c | 10 ++- 4 files changed, 113 insertions(+), 27 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/5 thunderbolt: Make software margining error counter configurable
From: Anurag <[email protected]>
The debugfs software margining run path currently initializes the request with USB4_MARGIN_SW_ERROR_COUNTER_CLEAR, which implicitly clears/enables counters every time software margining is started.
On some host-router/firmware implementations that enforce PHY interface for USB4 (PIPE) margining control, CLEAR and START are distinct operations.
- CLEAR resets error counters without starting margining
- START begins margin testing and error counting
Make the error counter operation configurable through debugfs error_counter control instead of hardcoding CLEAR. Additionally, only execute the software margining sampling loop when the selected operation is START; for CLEAR or STOP operations, execute the counter operation and return immediately.
Example to CLEAR error counter:
cd /sys/kernel/debug/thunderbolt/<ROUTER_ID>/<PORT>/margining echo clear > error_counter
Example to START error counter:
cd /sys/kernel/debug/thunderbolt/<ROUTER_ID>/<PORT>/margining echo start > error_counter
After configuring error counter run software margining for changes to take effect:
echo 1 > run
This aligns the debugfs behavior with the USB4 v2/PIPE expectation and lets users explicitly choose between CLEAR/START/STOP.
--- drivers/thunderbolt/debugfs.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/thunderbolt/debugfs.c +12 -8
@@ -1156,18 +1156,23 @@ static int margining_run_sw(struct tb_margining *margining,u32 nsamples = margining->dwell_time /DWELL_SAMPLE_INTERVAL ;int ret, i; +if (params->error_counter !=USB4_MARGIN_SW_ERROR_COUNTER_START ) + goto out_stop_or_clear; + ret =usb4_port_sw_margin (margining->port, margining->target, margining->index, params, margining->results);if (ret) - goto out_stop; +return ret;for (i =0 ; i <= nsamples; i++) {u32 errors =0 ; ret =usb4_port_sw_margin_errors (margining->port, margining->target, margining->index, &margining->results[1 ]); -if (ret) +if (ret) { +tb_port_warn (margining->port,"failed to read margining error counters\n" );break ; + }if (margining->lanes ==USB4_MARGINING_LANE_RX0 ) errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK,
@@ -1188,14 +1193,13 @@ static int margining_run_sw(struct tb_margining *margining,fsleep (DWELL_SAMPLE_INTERVAL *USEC_PER_MSEC ); } -out_stop: +out_stop_or_clear:/* -* Stop the counters but don't clear them to allow the +* Stop the counters or clear them as per the * different error counter configurations. */ -margining_modify_error_counter (margining, margining->lanes, -USB4_MARGIN_SW_ERROR_COUNTER_STOP ); -return ret; +return margining_modify_error_counter (margining, margining->lanes, + params->error_counter); }static int validate_margining (struct tb_margining *margining)
@@ -1272,7 +1276,7 @@ static int margining_run_write(void *data, u64 val)if (margining->software) {struct usb4_port_margining_params params = { - .error_counter =USB4_MARGIN_SW_ERROR_COUNTER_CLEAR , + .error_counter = margining->error_counter, .lanes = margining->lanes, .time = margining->time, .voltage_time_offset = margining->voltage_time_offset,
Signature · 1 line
2.50.1
PATCH 2/5 thunderbolt: Define number of dwords for hardware margining results
In reply to Mika WesterbergFrom: Rene Sapiens <[email protected]>
Use a defined size for hardware margining results in preparation for the software margining extended error counters which requires a larger buffer size.
--- drivers/thunderbolt/debugfs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/thunderbolt/debugfs.c +4 -2
@@ -37,6 +37,8 @@#define COUNTER_SET_LEN 3 +#define HW_MARGINING_DWORDS 3 +/* * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms * probed to give good results.
@@ -1307,7 +1309,7 @@ static int margining_run_write(void *data, u64 val) margining->lanes); ret =usb4_port_hw_margin (port, margining->target, margining->index, ¶ms, - margining->results,ARRAY_SIZE (margining->results)); + margining->results,HW_MARGINING_DWORDS ); }if (down_sw)
@@ -1429,7 +1431,7 @@ static int margining_results_show(struct seq_file *s, void *not_used)seq_printf (s,"0x%08x\n" , margining->results[0 ]);/* Only the hardware margining has two result dwords */ if (!margining->software) { -for (int i =1 ; i <ARRAY_SIZE (margining->results); i++) +for (int i =1 ; i <HW_MARGINING_DWORDS ; i++)seq_printf (s,"0x%08x\n" , margining->results[i]);if (margining->lanes ==USB4_MARGINING_LANE_ALL ) {
Signature · 1 line
2.50.1
PATCH 3/5 thunderbolt: Add extended error counter support for software lane margining
In reply to Mika WesterbergFrom: Rene Sapiens <[email protected]>
Add support for software margining extended error counters. Increase the results buffer size to accommodate the new dwords used for extended error counters for RX0, RX1 and RX2.
--- drivers/thunderbolt/debugfs.c | 34 ++++++++++++++++++++++++++++++++-- drivers/thunderbolt/sb_regs.h | 2 ++ drivers/thunderbolt/tb.h | 2 +- drivers/thunderbolt/usb4.c | 5 +++-- 4 files changed, 38 insertions(+), 5 deletions(-)
drivers/thunderbolt/debugfs.c +32 -2
@@ -38,6 +38,7 @@#define COUNTER_SET_LEN 3#define HW_MARGINING_DWORDS 3 +#define SW_MARGINING_DWORDS 4/* * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms
@@ -496,7 +497,7 @@ struct tb_margining {unsigned int gen;bool asym_rx;u32 caps[3 ]; -u32 results[3 ]; +u32 results[5 ];enum usb4_margining_lane lanes;unsigned int min_ber_level;unsigned int max_ber_level;
@@ -614,6 +615,11 @@ supports_optional_voltage_offset_range(const struct tb_margining *margining)return margining->caps[0 ] &USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT ; } +static bool supports_extended_error_counter (const struct tb_margining *margining) +{ +return margining->caps[0 ] &USB4_MARGIN_CAP_0_EXT_ERR_COUNTER ; +} +static ssize_t margining_ber_level_write (struct file *file,const char __user *user_buf,size_t count,loff_t *ppos)
@@ -703,6 +709,8 @@ static int margining_caps_show(struct seq_file *s, void *not_used)ber_level_show (s, margining->max_ber_level); }else {seq_puts (s,"# hardware margining: no\n" ); +seq_printf (s,"# extended error counter: %s\n" , +str_yes_no (supports_extended_error_counter (margining))); } seq_printf(s, "# all lanes simultaneously: %s\n",
@@ -1157,10 +1165,16 @@ static int margining_run_sw(struct tb_margining *margining, {u32 nsamples = margining->dwell_time /DWELL_SAMPLE_INTERVAL ;int ret, i; +u32 dwords;if (params->error_counter !=USB4_MARGIN_SW_ERROR_COUNTER_START ) goto out_stop_or_clear; +if (supports_extended_error_counter (margining)) + dwords =SW_MARGINING_DWORDS ; +else + dwords =1 ; + ret =usb4_port_sw_margin (margining->port, margining->target, margining->index, params, margining->results);if (ret)
@@ -1170,7 +1184,8 @@ static int margining_run_sw(struct tb_margining *margining,u32 errors =0 ; ret =usb4_port_sw_margin_errors (margining->port, margining->target, - margining->index, &margining->results[1 ]); + margining->index, &margining->results[1 ], + dwords);if (ret) {tb_port_warn (margining->port,"failed to read margining error counters\n" );break ;
@@ -1450,18 +1465,30 @@ static int margining_results_show(struct seq_file *s, void *not_used)u32 lane_errors, result;seq_printf (s,"0x%08x\n" , margining->results[1 ]); +if (supports_extended_error_counter (margining)) { +seq_printf (s,"0x%08x\n" , margining->results[2 ]); +seq_printf (s,"0x%08x\n" , margining->results[3 ]); +seq_printf (s,"0x%08x\n" , margining->results[4 ]); + } + result =FIELD_GET (USB4_MARGIN_SW_LANES_MASK , margining->results[0 ]);if (result ==USB4_MARGINING_LANE_RX0 || result ==USB4_MARGINING_LANE_ALL ) { lane_errors =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK , margining->results[1 ]); +if (supports_extended_error_counter (margining)) + lane_errors |=FIELD_PREP (USB4_MARGIN_SW_EXT_ERR_COUNTER_MASK , + margining->results[2 ]);seq_printf (s,"# lane 0 errors: %u\n" , lane_errors); }if (result ==USB4_MARGINING_LANE_RX1 || result ==USB4_MARGINING_LANE_ALL ) { lane_errors =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK , margining->results[1 ]); +if (supports_extended_error_counter (margining)) + lane_errors |=FIELD_PREP (USB4_MARGIN_SW_EXT_ERR_COUNTER_MASK , + margining->results[3 ]);seq_printf (s,"# lane 1 errors: %u\n" , lane_errors); } if (margining->asym_rx &&
@@ -1469,6 +1496,9 @@ static int margining_results_show(struct seq_file *s, void *not_used) result == USB4_MARGINING_LANE_ALL)) { lane_errors =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK , margining->results[1 ]); +if (supports_extended_error_counter (margining)) + lane_errors |=FIELD_PREP (USB4_MARGIN_SW_EXT_ERR_COUNTER_MASK , + margining->results[4 ]);seq_printf (s,"# lane 2 errors: %u\n" , lane_errors); } }
drivers/thunderbolt/sb_regs.h +2 -0
@@ -59,6 +59,7 @@ enum usb4_sb_opcode {#define USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK GENMASK(18, 13)#define USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT BIT(19)#define USB4_MARGIN_CAP_0_VOLT_STEPS_OPT_MASK GENMASK(26, 20) +#define USB4_MARGIN_CAP_0_EXT_ERR_COUNTER BIT(28)#define USB4_MARGIN_CAP_1_MAX_VOLT_OFS_OPT_MASK GENMASK(7, 0)#define USB4_MARGIN_CAP_1_TIME_DESTR BIT(8)#define USB4_MARGIN_CAP_1_TIME_INDP_MASK GENMASK(10, 9)
@@ -108,5 +109,6 @@ enum usb4_sb_opcode {#define USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK GENMASK(3, 0)#define USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK GENMASK(7, 4)#define USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK GENMASK(11, 8) +#define USB4_MARGIN_SW_EXT_ERR_COUNTER_MASK GENMASK(15, 4)#endif
drivers/thunderbolt/tb.h +1 -1
@@ -1439,7 +1439,7 @@ int usb4_port_sw_margin(struct tb_port *port, enum usb4_sb_target target, u8 index, const struct usb4_port_margining_params *params, u32 *results);int usb4_port_sw_margin_errors (struct tb_port *port,enum usb4_sb_target target, -u8 index,u32 *errors); +u8 index,u32 *errors,size_t dwords);int usb4_port_retimer_set_inbound_sbtx (struct tb_port *port,u8 index);int usb4_port_retimer_unset_inbound_sbtx (struct tb_port *port,u8 index);
drivers/thunderbolt/usb4.c +3 -2
@@ -1826,13 +1826,14 @@ int usb4_port_sw_margin(struct tb_port *port, enum usb4_sb_target target,* @target: Sideband target * @index: Retimer index if target is %USB4_SB_TARGET_RETIMER * @errors: Error metadata is copied here. +* @dwords: Number of dwords to read error counter values * * This reads back the software margining error counters from the port. * * Return: %0 on success, negative errno otherwise. */ int usb4_port_sw_margin_errors (struct tb_port *port,enum usb4_sb_target target, -u8 index,u32 *errors) +u8 index,u32 *errors,size_t dwords) {int ret;
@@ -1842,7 +1843,7 @@ int usb4_port_sw_margin_errors(struct tb_port *port, enum usb4_sb_target target,return ret;return usb4_port_sb_read (port, target, index,USB4_SB_METADATA , errors, -sizeof (*errors)); +sizeof (*errors) * dwords); } static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
Signature · 1 line
2.50.1
PATCH 4/5 thunderbolt: Fix sideband register access in usb4_port_sw_margin_errors()
In reply to Mika WesterbergFrom: Anurag <[email protected]>
The READ_SW_MARGIN_ERR (RDSW) completion payload is returned in the USB4 sideband data window, but the location differs between standard and extended error counters.
Standard error counters are returned in USB4_SB_METADATA and fit within a single dword. Extended error counters require 4 dwords and are returned in USB4_SB_DATA.
Add conditional logic to select the appropriate register based on the dwords parameter that corresponds with error counter type.
--- drivers/thunderbolt/usb4.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/thunderbolt/usb4.c +4 -1
@@ -1836,13 +1836,16 @@ int usb4_port_sw_margin_errors(struct tb_port *port, enum usb4_sb_target target, u8 index, u32 *errors, size_t dwords) { int ret; +u8 reg; ret =usb4_port_sb_op (port, target, index,USB4_SB_OPCODE_READ_SW_MARGIN_ERR ,150 );if (ret)return ret; -return usb4_port_sb_read (port, target, index,USB4_SB_METADATA , errors, + reg = (dwords >1 ) ?USB4_SB_DATA :USB4_SB_METADATA ; + +return usb4_port_sb_read (port, target, index, reg, errors,sizeof (*errors) * dwords); }
Signature · 1 line
2.50.1
PATCH 5/5 thunderbolt: Extract per-lane extended error counters in software margining
In reply to Mika WesterbergFrom: Anurag <[email protected]>
USB4 defines two error counter formats for software margining:
- Standard counters (USB4 v1): 4-bit per-lane values in DW0.
- Extended counters (USB4 v2): 16-bit per-lane values split across DW0 (lower 4 bits) and DW1-DW3 (upper 12 bits).
The existing debugfs logic only reads DW0 when checking for errors, which works correctly for standard error counters but only captures the lower 4 bits, missing the 12 MSBs if extended error counter is supported.
Read and combine the full counter values from all result dwords before evaluating whether errors occurred on any lane.
When running software margining with "all lanes", determine which Rx lanes are actually active and only check those:
- ASYM_RX: RX0, RX1, RX2
- ASYM_TX: RX0
- Symmetric: RX0, RX1
Refactor the error counter extraction into a helper function to improve readability.
--- drivers/thunderbolt/debugfs.c | 68 ++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 12 deletions(-)
drivers/thunderbolt/debugfs.c +56 -12
@@ -1160,9 +1160,34 @@ static int margining_mode_show(struct seq_file *s, void *not_used) }DEBUGFS_ATTR_RW (margining_mode); +static u32 margining_get_lane_error (const u32 *results,unsigned int lane,bool extended) +{ +u32 error_count; + +switch (lane) { +case USB4_MARGINING_LANE_RX0 : + error_count =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK , results[0 ]); +break ; +case USB4_MARGINING_LANE_RX1 : + error_count =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK , results[0 ]); +break ; +case USB4_MARGINING_LANE_RX2 : + error_count =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK , results[0 ]); +break ; +default : +return 0 ; + } + +if (extended) + error_count |=FIELD_PREP (USB4_MARGIN_SW_EXT_ERR_COUNTER_MASK , results[1 + lane]); + +return error_count; +} +static int margining_run_sw (struct tb_margining *margining,struct usb4_port_margining_params *params) { +bool extended_err =supports_extended_error_counter (margining);u32 nsamples = margining->dwell_time /DWELL_SAMPLE_INTERVAL ;int ret, i;u32 dwords;
@@ -1170,7 +1195,7 @@ static int margining_run_sw(struct tb_margining *margining,if (params->error_counter !=USB4_MARGIN_SW_ERROR_COUNTER_START ) goto out_stop_or_clear; -if (supports_extended_error_counter (margining)) +if (extended_err) dwords =SW_MARGINING_DWORDS ;else dwords =1 ;
@@ -1191,17 +1216,35 @@ static int margining_run_sw(struct tb_margining *margining,break ; } -if (margining->lanes ==USB4_MARGINING_LANE_RX0 ) - errors =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK , - margining->results[1 ]); -else if (margining->lanes ==USB4_MARGINING_LANE_RX1 ) - errors =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK , - margining->results[1 ]); -else if (margining->lanes ==USB4_MARGINING_LANE_RX2 ) - errors =FIELD_GET (USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK , - margining->results[1 ]); -else if (margining->lanes ==USB4_MARGINING_LANE_ALL ) - errors = margining->results[1 ]; +if (margining->lanes ==USB4_MARGINING_LANE_ALL ) { +if (extended_err) { +/* Extended counters with all lanes: check each active lane */ +int width =tb_port_get_link_width (margining->port); +unsigned int lane, max_lane =2 ; + +if (width ==TB_LINK_WIDTH_ASYM_TX ) { + max_lane =1 ; + }else if ((width ==TB_LINK_WIDTH_ASYM_RX ) && margining->asym_rx) { + max_lane =3 ; + }else if (width <0 ) { +tb_port_warn (margining->port,"failed to read link width\n" ); +break ; + } + +for (lane =0 ; lane < max_lane; lane++) { + errors =margining_get_lane_error (&margining->results[1 ], + lane, extended_err); +if (errors) +break ; + } + }else { +/* Standard counters: all lane errors fit in results[1]*/ + errors = margining->results[1 ]; + } + }else { + errors =margining_get_lane_error (&margining->results[1 ], + margining->lanes, extended_err); + }/* Any errors stop the test */ if (errors)
@@ -1209,6 +1252,7 @@ static int margining_run_sw(struct tb_margining *margining,fsleep (DWELL_SAMPLE_INTERVAL *USEC_PER_MSEC ); } + params->error_counter =USB4_MARGIN_SW_ERROR_COUNTER_STOP ; out_stop_or_clear:/*
Signature · 1 line
2.50.1
Mika Westerberg
AppliedQuoted 6 lines · Mika Westerberg
Hi all,
USB4 v2 introduced extended error counters for lane margining. Compared to the USB4 v1 these are 16-bit (vs. 4-bit in v1). This series makes them available for debugfs software lane margining.
Quoted 8 lines
Anurag (3):
thunderbolt: Make software margining error counter configurable
thunderbolt: Fix sideband register access inusb4_port_sw_margin_errors()
thunderbolt: Extract per-lane extended error counters in software marginingRene Sapiens (2):
thunderbolt: Define number of dwords for hardware margining results
thunderbolt: Add extended error counter support for software lane margining
Applied to thunderbolt.git/next.