1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Debugfs interface 4 * 5 * Copyright (C) 2020, Intel Corporation 6 * Authors: Gil Fine <gil.fine@intel.com> 7 * Mika Westerberg <mika.westerberg@linux.intel.com> 8 */ 9 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/string_choices.h> 16 #include <linux/uaccess.h> 17 18 #include "tb.h" 19 #include "sb_regs.h" 20 21 #define PORT_CAP_V1_PCIE_LEN 1 22 #define PORT_CAP_V2_PCIE_LEN 2 23 #define PORT_CAP_POWER_LEN 2 24 #define PORT_CAP_LANE_LEN 3 25 #define PORT_CAP_USB3_LEN 5 26 #define PORT_CAP_DP_V1_LEN 9 27 #define PORT_CAP_DP_V2_LEN 14 28 #define PORT_CAP_TMU_V1_LEN 8 29 #define PORT_CAP_TMU_V2_LEN 10 30 #define PORT_CAP_BASIC_LEN 9 31 #define PORT_CAP_USB4_LEN 20 32 33 #define SWITCH_CAP_TMU_LEN 26 34 #define SWITCH_CAP_BASIC_LEN 27 35 36 #define PATH_LEN 2 37 38 #define COUNTER_SET_LEN 3 39 40 /* 41 * USB4 spec doesn't specify dwell range, the range of 100 ms to 500 ms 42 * probed to give good results. 43 */ 44 #define MIN_DWELL_TIME 100 /* ms */ 45 #define MAX_DWELL_TIME 500 /* ms */ 46 #define DWELL_SAMPLE_INTERVAL 10 47 48 enum usb4_margin_cap_voltage_indp { 49 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN, 50 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL, 51 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH, 52 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN, 53 USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH, 54 USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN, 55 }; 56 57 enum usb4_margin_cap_time_indp { 58 USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN, 59 USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR, 60 USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH, 61 USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN, 62 USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH, 63 USB4_MARGIN_CAP_TIME_INDP_UNKNOWN, 64 }; 65 66 /* Sideband registers and their sizes as defined in the USB4 spec */ 67 struct sb_reg { 68 unsigned int reg; 69 unsigned int size; 70 }; 71 72 #define SB_MAX_SIZE 64 73 74 /* Sideband registers for router */ 75 static const struct sb_reg port_sb_regs[] = { 76 { USB4_SB_VENDOR_ID, 4 }, 77 { USB4_SB_PRODUCT_ID, 4 }, 78 { USB4_SB_DEBUG_CONF, 4 }, 79 { USB4_SB_DEBUG, 54 }, 80 { USB4_SB_LRD_TUNING, 4 }, 81 { USB4_SB_OPCODE, 4 }, 82 { USB4_SB_METADATA, 4 }, 83 { USB4_SB_LINK_CONF, 3 }, 84 { USB4_SB_GEN23_TXFFE, 4 }, 85 { USB4_SB_GEN4_TXFFE, 4 }, 86 { USB4_SB_VERSION, 4 }, 87 { USB4_SB_DATA, 64 }, 88 }; 89 90 /* Sideband registers for retimer */ 91 static const struct sb_reg retimer_sb_regs[] = { 92 { USB4_SB_VENDOR_ID, 4 }, 93 { USB4_SB_PRODUCT_ID, 4 }, 94 { USB4_SB_FW_VERSION, 4 }, 95 { USB4_SB_LRD_TUNING, 4 }, 96 { USB4_SB_OPCODE, 4 }, 97 { USB4_SB_METADATA, 4 }, 98 { USB4_SB_GEN23_TXFFE, 4 }, 99 { USB4_SB_GEN4_TXFFE, 4 }, 100 { USB4_SB_VERSION, 4 }, 101 { USB4_SB_DATA, 64 }, 102 }; 103 104 #define DEBUGFS_ATTR(__space, __write) \ 105 static int __space ## _open(struct inode *inode, struct file *file) \ 106 { \ 107 return single_open(file, __space ## _show, inode->i_private); \ 108 } \ 109 \ 110 static const struct file_operations __space ## _fops = { \ 111 .owner = THIS_MODULE, \ 112 .open = __space ## _open, \ 113 .release = single_release, \ 114 .read = seq_read, \ 115 .write = __write, \ 116 .llseek = seq_lseek, \ 117 } 118 119 #define DEBUGFS_ATTR_RO(__space) \ 120 DEBUGFS_ATTR(__space, NULL) 121 122 #define DEBUGFS_ATTR_RW(__space) \ 123 DEBUGFS_ATTR(__space, __space ## _write) 124 125 static struct dentry *tb_debugfs_root; 126 127 static void *validate_and_copy_from_user(const void __user *user_buf, 128 size_t *count) 129 { 130 size_t nbytes; 131 void *buf; 132 133 if (!*count) 134 return ERR_PTR(-EINVAL); 135 136 if (!access_ok(user_buf, *count)) 137 return ERR_PTR(-EFAULT); 138 139 buf = (void *)get_zeroed_page(GFP_KERNEL); 140 if (!buf) 141 return ERR_PTR(-ENOMEM); 142 143 nbytes = min_t(size_t, *count, PAGE_SIZE); 144 if (copy_from_user(buf, user_buf, nbytes)) { 145 free_page((unsigned long)buf); 146 return ERR_PTR(-EFAULT); 147 } 148 149 *count = nbytes; 150 return buf; 151 } 152 153 static bool parse_line(char **line, u32 *offs, u32 *val, int short_fmt_len, 154 int long_fmt_len) 155 { 156 char *token; 157 u32 v[5]; 158 int ret; 159 160 token = strsep(line, "\n"); 161 if (!token) 162 return false; 163 164 /* 165 * For Adapter/Router configuration space: 166 * Short format is: offset value\n 167 * v[0] v[1] 168 * Long format as produced from the read side: 169 * offset relative_offset cap_id vs_cap_id value\n 170 * v[0] v[1] v[2] v[3] v[4] 171 * 172 * For Path configuration space: 173 * Short format is: offset value\n 174 * v[0] v[1] 175 * Long format as produced from the read side: 176 * offset relative_offset in_hop_id value\n 177 * v[0] v[1] v[2] v[3] 178 * 179 * For Counter configuration space: 180 * Short format is: offset\n 181 * v[0] 182 * Long format as produced from the read side: 183 * offset relative_offset counter_id value\n 184 * v[0] v[1] v[2] v[3] 185 */ 186 ret = sscanf(token, "%i %i %i %i %i", &v[0], &v[1], &v[2], &v[3], &v[4]); 187 /* In case of Counters, clear counter, "val" content is NA */ 188 if (ret == short_fmt_len) { 189 *offs = v[0]; 190 *val = v[short_fmt_len - 1]; 191 return true; 192 } else if (ret == long_fmt_len) { 193 *offs = v[0]; 194 *val = v[long_fmt_len - 1]; 195 return true; 196 } 197 198 return false; 199 } 200 201 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_WRITE) 202 /* 203 * Path registers need to be written in double word pairs and they both must be 204 * read before written. This writes one double word in path config space 205 * following the spec flow. 206 */ 207 static int path_write_one(struct tb_port *port, u32 val, u32 offset) 208 { 209 u32 index = offset % PATH_LEN; 210 u32 offs = offset - index; 211 u32 data[PATH_LEN]; 212 int ret; 213 214 ret = tb_port_read(port, data, TB_CFG_HOPS, offs, PATH_LEN); 215 if (ret) 216 return ret; 217 data[index] = val; 218 return tb_port_write(port, data, TB_CFG_HOPS, offs, PATH_LEN); 219 } 220 221 static ssize_t regs_write(struct tb_switch *sw, struct tb_port *port, 222 enum tb_cfg_space space, const char __user *user_buf, 223 size_t count, loff_t *ppos) 224 { 225 int long_fmt_len, ret = 0; 226 struct tb *tb = sw->tb; 227 char *line, *buf; 228 u32 val, offset; 229 230 buf = validate_and_copy_from_user(user_buf, &count); 231 if (IS_ERR(buf)) 232 return PTR_ERR(buf); 233 234 pm_runtime_get_sync(&sw->dev); 235 236 if (mutex_lock_interruptible(&tb->lock)) { 237 ret = -ERESTARTSYS; 238 goto out; 239 } 240 241 /* User did hardware changes behind the driver's back */ 242 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 243 244 if (space == TB_CFG_HOPS) 245 long_fmt_len = 4; 246 else 247 long_fmt_len = 5; 248 249 line = buf; 250 while (parse_line(&line, &offset, &val, 2, long_fmt_len)) { 251 if (port) { 252 if (space == TB_CFG_HOPS) 253 ret = path_write_one(port, val, offset); 254 else 255 ret = tb_port_write(port, &val, space, offset, 1); 256 } else { 257 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, offset, 1); 258 } 259 if (ret) 260 break; 261 } 262 263 mutex_unlock(&tb->lock); 264 265 out: 266 pm_runtime_mark_last_busy(&sw->dev); 267 pm_runtime_put_autosuspend(&sw->dev); 268 free_page((unsigned long)buf); 269 270 return ret < 0 ? ret : count; 271 } 272 273 static ssize_t port_regs_write(struct file *file, const char __user *user_buf, 274 size_t count, loff_t *ppos) 275 { 276 struct seq_file *s = file->private_data; 277 struct tb_port *port = s->private; 278 279 return regs_write(port->sw, port, TB_CFG_PORT, user_buf, count, ppos); 280 } 281 282 static ssize_t path_write(struct file *file, const char __user *user_buf, 283 size_t count, loff_t *ppos) 284 { 285 struct seq_file *s = file->private_data; 286 struct tb_port *port = s->private; 287 288 return regs_write(port->sw, port, TB_CFG_HOPS, user_buf, count, ppos); 289 } 290 291 static ssize_t switch_regs_write(struct file *file, const char __user *user_buf, 292 size_t count, loff_t *ppos) 293 { 294 struct seq_file *s = file->private_data; 295 struct tb_switch *sw = s->private; 296 297 return regs_write(sw, NULL, TB_CFG_SWITCH, user_buf, count, ppos); 298 } 299 300 static bool parse_sb_line(char **line, u8 *reg, u8 *data, size_t data_size, 301 size_t *bytes_read) 302 { 303 char *field, *token; 304 int i; 305 306 token = strsep(line, "\n"); 307 if (!token) 308 return false; 309 310 /* Parse the register first */ 311 field = strsep(&token, " "); 312 if (!field) 313 return false; 314 if (kstrtou8(field, 0, reg)) 315 return false; 316 317 /* Then the values for the register, up to data_size */ 318 for (i = 0; i < data_size; i++) { 319 field = strsep(&token, " "); 320 if (!field) 321 break; 322 if (kstrtou8(field, 0, &data[i])) 323 return false; 324 } 325 326 *bytes_read = i; 327 return true; 328 } 329 330 static ssize_t sb_regs_write(struct tb_port *port, const struct sb_reg *sb_regs, 331 size_t size, enum usb4_sb_target target, u8 index, 332 char *buf, size_t count, loff_t *ppos) 333 { 334 u8 reg, data[SB_MAX_SIZE]; 335 size_t bytes_read; 336 char *line = buf; 337 338 /* User did hardware changes behind the driver's back */ 339 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 340 341 /* 342 * For sideband registers we accept: 343 * reg b0 b1 b2...\n 344 * 345 * Here "reg" is the byte offset of the sideband register and "b0".. 346 * are the byte values. There can be less byte values than the register 347 * size. The leftovers will not be overwritten. 348 */ 349 while (parse_sb_line(&line, ®, data, ARRAY_SIZE(data), &bytes_read)) { 350 const struct sb_reg *sb_reg; 351 int ret; 352 353 /* At least one byte must be passed */ 354 if (bytes_read < 1) 355 return -EINVAL; 356 357 /* Find the register */ 358 sb_reg = NULL; 359 for (int i = 0; i < size; i++) { 360 if (sb_regs[i].reg == reg) { 361 sb_reg = &sb_regs[i]; 362 break; 363 } 364 } 365 366 if (!sb_reg) 367 return -EINVAL; 368 369 if (bytes_read > sb_reg->size) 370 return -E2BIG; 371 372 ret = usb4_port_sb_write(port, target, index, sb_reg->reg, data, 373 bytes_read); 374 if (ret) 375 return ret; 376 } 377 378 return 0; 379 } 380 381 static ssize_t port_sb_regs_write(struct file *file, const char __user *user_buf, 382 size_t count, loff_t *ppos) 383 { 384 struct seq_file *s = file->private_data; 385 struct tb_port *port = s->private; 386 struct tb_switch *sw = port->sw; 387 struct tb *tb = sw->tb; 388 char *buf; 389 int ret; 390 391 buf = validate_and_copy_from_user(user_buf, &count); 392 if (IS_ERR(buf)) 393 return PTR_ERR(buf); 394 395 pm_runtime_get_sync(&sw->dev); 396 397 if (mutex_lock_interruptible(&tb->lock)) { 398 ret = -ERESTARTSYS; 399 goto out; 400 } 401 402 ret = sb_regs_write(port, port_sb_regs, ARRAY_SIZE(port_sb_regs), 403 USB4_SB_TARGET_ROUTER, 0, buf, count, ppos); 404 405 mutex_unlock(&tb->lock); 406 out: 407 pm_runtime_mark_last_busy(&sw->dev); 408 pm_runtime_put_autosuspend(&sw->dev); 409 free_page((unsigned long)buf); 410 411 return ret < 0 ? ret : count; 412 } 413 414 static ssize_t retimer_sb_regs_write(struct file *file, 415 const char __user *user_buf, 416 size_t count, loff_t *ppos) 417 { 418 struct seq_file *s = file->private_data; 419 struct tb_retimer *rt = s->private; 420 struct tb *tb = rt->tb; 421 char *buf; 422 int ret; 423 424 buf = validate_and_copy_from_user(user_buf, &count); 425 if (IS_ERR(buf)) 426 return PTR_ERR(buf); 427 428 pm_runtime_get_sync(&rt->dev); 429 430 if (mutex_lock_interruptible(&tb->lock)) { 431 ret = -ERESTARTSYS; 432 goto out; 433 } 434 435 ret = sb_regs_write(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs), 436 USB4_SB_TARGET_RETIMER, rt->index, buf, count, ppos); 437 438 mutex_unlock(&tb->lock); 439 out: 440 pm_runtime_mark_last_busy(&rt->dev); 441 pm_runtime_put_autosuspend(&rt->dev); 442 free_page((unsigned long)buf); 443 444 return ret < 0 ? ret : count; 445 } 446 #define DEBUGFS_MODE 0600 447 #else 448 #define port_regs_write NULL 449 #define path_write NULL 450 #define switch_regs_write NULL 451 #define port_sb_regs_write NULL 452 #define retimer_sb_regs_write NULL 453 #define DEBUGFS_MODE 0400 454 #endif 455 456 #if IS_ENABLED(CONFIG_USB4_DEBUGFS_MARGINING) 457 /** 458 * struct tb_margining - Lane margining support 459 * @port: USB4 port through which the margining operations are run 460 * @target: Sideband target 461 * @index: Retimer index if taget is %USB4_SB_TARGET_RETIMER 462 * @dev: Pointer to the device that is the target (USB4 port or retimer) 463 * @gen: Link generation 464 * @asym_rx: %true% if @port supports asymmetric link with 3 Rx 465 * @caps: Port lane margining capabilities 466 * @results: Last lane margining results 467 * @lanes: %0, %1 or %7 (all) 468 * @min_ber_level: Minimum supported BER level contour value 469 * @max_ber_level: Maximum supported BER level contour value 470 * @ber_level: Current BER level contour value 471 * @voltage_steps: Number of mandatory voltage steps 472 * @max_voltage_offset: Maximum mandatory voltage offset (in mV) 473 * @voltage_steps_optional_range: Number of voltage steps for optional range 474 * @max_voltage_offset_optional_range: Maximum voltage offset for the optional 475 * range (in mV). 476 * @time_steps: Number of time margin steps 477 * @max_time_offset: Maximum time margin offset (in mUI) 478 * @voltage_time_offset: Offset for voltage / time for software margining 479 * @dwell_time: Dwell time for software margining (in ms) 480 * @error_counter: Error counter operation for software margining 481 * @optional_voltage_offset_range: Enable optional extended voltage range 482 * @software: %true if software margining is used instead of hardware 483 * @time: %true if time margining is used instead of voltage 484 * @right_high: %false if left/low margin test is performed, %true if 485 * right/high 486 * @upper_eye: %false if the lower PAM3 eye is used, %true if the upper 487 * eye is used 488 */ 489 struct tb_margining { 490 struct tb_port *port; 491 enum usb4_sb_target target; 492 u8 index; 493 struct device *dev; 494 unsigned int gen; 495 bool asym_rx; 496 u32 caps[3]; 497 u32 results[3]; 498 enum usb4_margining_lane lanes; 499 unsigned int min_ber_level; 500 unsigned int max_ber_level; 501 unsigned int ber_level; 502 unsigned int voltage_steps; 503 unsigned int max_voltage_offset; 504 unsigned int voltage_steps_optional_range; 505 unsigned int max_voltage_offset_optional_range; 506 unsigned int time_steps; 507 unsigned int max_time_offset; 508 unsigned int voltage_time_offset; 509 unsigned int dwell_time; 510 enum usb4_margin_sw_error_counter error_counter; 511 bool optional_voltage_offset_range; 512 bool software; 513 bool time; 514 bool right_high; 515 bool upper_eye; 516 }; 517 518 static int margining_modify_error_counter(struct tb_margining *margining, 519 u32 lanes, enum usb4_margin_sw_error_counter error_counter) 520 { 521 struct usb4_port_margining_params params = { 0 }; 522 struct tb_port *port = margining->port; 523 u32 result; 524 525 if (error_counter != USB4_MARGIN_SW_ERROR_COUNTER_CLEAR && 526 error_counter != USB4_MARGIN_SW_ERROR_COUNTER_STOP) 527 return -EOPNOTSUPP; 528 529 params.error_counter = error_counter; 530 params.lanes = lanes; 531 532 return usb4_port_sw_margin(port, margining->target, margining->index, 533 ¶ms, &result); 534 } 535 536 static bool supports_software(const struct tb_margining *margining) 537 { 538 if (margining->gen < 4) 539 return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_SW; 540 return margining->caps[2] & USB4_MARGIN_CAP_2_MODES_SW; 541 } 542 543 static bool supports_hardware(const struct tb_margining *margining) 544 { 545 if (margining->gen < 4) 546 return margining->caps[0] & USB4_MARGIN_CAP_0_MODES_HW; 547 return margining->caps[2] & USB4_MARGIN_CAP_2_MODES_HW; 548 } 549 550 static bool all_lanes(const struct tb_margining *margining) 551 { 552 return margining->caps[0] & USB4_MARGIN_CAP_0_ALL_LANES; 553 } 554 555 static enum usb4_margin_cap_voltage_indp 556 independent_voltage_margins(const struct tb_margining *margining) 557 { 558 if (margining->gen < 4) { 559 switch (FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_INDP_MASK, margining->caps[0])) { 560 case USB4_MARGIN_CAP_0_VOLTAGE_MIN: 561 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN; 562 case USB4_MARGIN_CAP_0_VOLTAGE_HL: 563 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL; 564 case USB4_MARGIN_CAP_1_TIME_BOTH: 565 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH; 566 } 567 } else { 568 switch (FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_INDP_MASK, margining->caps[2])) { 569 case USB4_MARGIN_CAP_2_VOLTAGE_MIN: 570 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN; 571 case USB4_MARGIN_CAP_2_VOLTAGE_BOTH: 572 return USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH; 573 } 574 } 575 return USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN; 576 } 577 578 static bool supports_time(const struct tb_margining *margining) 579 { 580 if (margining->gen < 4) 581 return margining->caps[0] & USB4_MARGIN_CAP_0_TIME; 582 return margining->caps[2] & USB4_MARGIN_CAP_2_TIME; 583 } 584 585 /* Only applicable if supports_time() returns true */ 586 static enum usb4_margin_cap_time_indp 587 independent_time_margins(const struct tb_margining *margining) 588 { 589 if (margining->gen < 4) { 590 switch (FIELD_GET(USB4_MARGIN_CAP_1_TIME_INDP_MASK, margining->caps[1])) { 591 case USB4_MARGIN_CAP_1_TIME_MIN: 592 return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN; 593 case USB4_MARGIN_CAP_1_TIME_LR: 594 return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR; 595 case USB4_MARGIN_CAP_1_TIME_BOTH: 596 return USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH; 597 } 598 } else { 599 switch (FIELD_GET(USB4_MARGIN_CAP_2_TIME_INDP_MASK, margining->caps[2])) { 600 case USB4_MARGIN_CAP_2_TIME_MIN: 601 return USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN; 602 case USB4_MARGIN_CAP_2_TIME_BOTH: 603 return USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH; 604 } 605 } 606 return USB4_MARGIN_CAP_TIME_INDP_UNKNOWN; 607 } 608 609 static bool 610 supports_optional_voltage_offset_range(const struct tb_margining *margining) 611 { 612 return margining->caps[0] & USB4_MARGIN_CAP_0_OPT_VOLTAGE_SUPPORT; 613 } 614 615 static ssize_t 616 margining_ber_level_write(struct file *file, const char __user *user_buf, 617 size_t count, loff_t *ppos) 618 { 619 struct seq_file *s = file->private_data; 620 struct tb_margining *margining = s->private; 621 struct tb *tb = margining->port->sw->tb; 622 unsigned int val; 623 int ret = 0; 624 char *buf; 625 626 if (mutex_lock_interruptible(&tb->lock)) 627 return -ERESTARTSYS; 628 629 if (margining->software) { 630 ret = -EINVAL; 631 goto out_unlock; 632 } 633 634 buf = validate_and_copy_from_user(user_buf, &count); 635 if (IS_ERR(buf)) { 636 ret = PTR_ERR(buf); 637 goto out_unlock; 638 } 639 640 buf[count - 1] = '\0'; 641 642 ret = kstrtouint(buf, 10, &val); 643 if (ret) 644 goto out_free; 645 646 if (val < margining->min_ber_level || 647 val > margining->max_ber_level) { 648 ret = -EINVAL; 649 goto out_free; 650 } 651 652 margining->ber_level = val; 653 654 out_free: 655 free_page((unsigned long)buf); 656 out_unlock: 657 mutex_unlock(&tb->lock); 658 659 return ret < 0 ? ret : count; 660 } 661 662 static void ber_level_show(struct seq_file *s, unsigned int val) 663 { 664 if (val % 2) 665 seq_printf(s, "3 * 1e%d (%u)\n", -12 + (val + 1) / 2, val); 666 else 667 seq_printf(s, "1e%d (%u)\n", -12 + val / 2, val); 668 } 669 670 static int margining_ber_level_show(struct seq_file *s, void *not_used) 671 { 672 const struct tb_margining *margining = s->private; 673 674 if (margining->software) 675 return -EINVAL; 676 ber_level_show(s, margining->ber_level); 677 return 0; 678 } 679 DEBUGFS_ATTR_RW(margining_ber_level); 680 681 static int margining_caps_show(struct seq_file *s, void *not_used) 682 { 683 struct tb_margining *margining = s->private; 684 struct tb *tb = margining->port->sw->tb; 685 int ret = 0; 686 687 if (mutex_lock_interruptible(&tb->lock)) 688 return -ERESTARTSYS; 689 690 /* Dump the raw caps first */ 691 for (int i = 0; i < ARRAY_SIZE(margining->caps); i++) 692 seq_printf(s, "0x%08x\n", margining->caps[i]); 693 694 seq_printf(s, "# software margining: %s\n", 695 str_yes_no(supports_software(margining))); 696 if (supports_hardware(margining)) { 697 seq_puts(s, "# hardware margining: yes\n"); 698 seq_puts(s, "# minimum BER level contour: "); 699 ber_level_show(s, margining->min_ber_level); 700 seq_puts(s, "# maximum BER level contour: "); 701 ber_level_show(s, margining->max_ber_level); 702 } else { 703 seq_puts(s, "# hardware margining: no\n"); 704 } 705 706 seq_printf(s, "# all lanes simultaneously: %s\n", 707 str_yes_no(all_lanes(margining))); 708 seq_printf(s, "# voltage margin steps: %u\n", 709 margining->voltage_steps); 710 seq_printf(s, "# maximum voltage offset: %u mV\n", 711 margining->max_voltage_offset); 712 seq_printf(s, "# optional voltage offset range support: %s\n", 713 str_yes_no(supports_optional_voltage_offset_range(margining))); 714 if (supports_optional_voltage_offset_range(margining)) { 715 seq_printf(s, "# voltage margin steps, optional range: %u\n", 716 margining->voltage_steps_optional_range); 717 seq_printf(s, "# maximum voltage offset, optional range: %u mV\n", 718 margining->max_voltage_offset_optional_range); 719 } 720 721 switch (independent_voltage_margins(margining)) { 722 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_MIN: 723 seq_puts(s, "# returns minimum between high and low voltage margins\n"); 724 break; 725 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL: 726 seq_puts(s, "# returns high or low voltage margin\n"); 727 break; 728 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_BOTH: 729 seq_puts(s, "# returns both high and low margins\n"); 730 break; 731 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_MIN: 732 seq_puts(s, "# returns minimum between high and low voltage margins in both lower and upper eye\n"); 733 break; 734 case USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_4_BOTH: 735 seq_puts(s, "# returns both high and low margins of both upper and lower eye\n"); 736 break; 737 case USB4_MARGIN_CAP_VOLTAGE_INDP_UNKNOWN: 738 tb_port_warn(margining->port, 739 "failed to parse independent voltage margining capabilities\n"); 740 ret = -EIO; 741 goto out; 742 } 743 744 if (supports_time(margining)) { 745 seq_puts(s, "# time margining: yes\n"); 746 seq_printf(s, "# time margining is destructive: %s\n", 747 str_yes_no(margining->caps[1] & USB4_MARGIN_CAP_1_TIME_DESTR)); 748 749 switch (independent_time_margins(margining)) { 750 case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_MIN: 751 seq_puts(s, "# returns minimum between left and right time margins\n"); 752 break; 753 case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR: 754 seq_puts(s, "# returns left or right margin\n"); 755 break; 756 case USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_BOTH: 757 seq_puts(s, "# returns both left and right margins\n"); 758 break; 759 case USB4_MARGIN_CAP_TIME_INDP_GEN_4_MIN: 760 seq_puts(s, "# returns minimum between left and right time margins in both lower and upper eye\n"); 761 break; 762 case USB4_MARGIN_CAP_TIME_INDP_GEN_4_BOTH: 763 seq_puts(s, "# returns both left and right margins of both upper and lower eye\n"); 764 break; 765 case USB4_MARGIN_CAP_TIME_INDP_UNKNOWN: 766 tb_port_warn(margining->port, 767 "failed to parse independent time margining capabilities\n"); 768 ret = -EIO; 769 goto out; 770 } 771 772 seq_printf(s, "# time margin steps: %u\n", 773 margining->time_steps); 774 seq_printf(s, "# maximum time offset: %u mUI\n", 775 margining->max_time_offset); 776 } else { 777 seq_puts(s, "# time margining: no\n"); 778 } 779 780 out: 781 mutex_unlock(&tb->lock); 782 return ret; 783 } 784 DEBUGFS_ATTR_RO(margining_caps); 785 786 static const struct { 787 enum usb4_margining_lane lane; 788 const char *name; 789 } lane_names[] = { 790 { 791 .lane = USB4_MARGINING_LANE_RX0, 792 .name = "0", 793 }, 794 { 795 .lane = USB4_MARGINING_LANE_RX1, 796 .name = "1", 797 }, 798 { 799 .lane = USB4_MARGINING_LANE_RX2, 800 .name = "2", 801 }, 802 { 803 .lane = USB4_MARGINING_LANE_ALL, 804 .name = "all", 805 }, 806 }; 807 808 static ssize_t 809 margining_lanes_write(struct file *file, const char __user *user_buf, 810 size_t count, loff_t *ppos) 811 { 812 struct seq_file *s = file->private_data; 813 struct tb_margining *margining = s->private; 814 struct tb_port *port = margining->port; 815 struct tb *tb = port->sw->tb; 816 int lane = -1; 817 char *buf; 818 819 buf = validate_and_copy_from_user(user_buf, &count); 820 if (IS_ERR(buf)) 821 return PTR_ERR(buf); 822 823 buf[count - 1] = '\0'; 824 825 for (int i = 0; i < ARRAY_SIZE(lane_names); i++) { 826 if (!strcmp(buf, lane_names[i].name)) { 827 lane = lane_names[i].lane; 828 break; 829 } 830 } 831 832 free_page((unsigned long)buf); 833 834 if (lane == -1) 835 return -EINVAL; 836 837 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 838 if (lane == USB4_MARGINING_LANE_ALL && !all_lanes(margining)) 839 return -EINVAL; 840 /* 841 * Enabling on RX2 requires that it is supported by the 842 * USB4 port. 843 */ 844 if (lane == USB4_MARGINING_LANE_RX2 && !margining->asym_rx) 845 return -EINVAL; 846 847 margining->lanes = lane; 848 } 849 850 return count; 851 } 852 853 static int margining_lanes_show(struct seq_file *s, void *not_used) 854 { 855 struct tb_margining *margining = s->private; 856 struct tb_port *port = margining->port; 857 struct tb *tb = port->sw->tb; 858 859 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 860 for (int i = 0; i < ARRAY_SIZE(lane_names); i++) { 861 if (lane_names[i].lane == USB4_MARGINING_LANE_ALL && 862 !all_lanes(margining)) 863 continue; 864 if (lane_names[i].lane == USB4_MARGINING_LANE_RX2 && 865 !margining->asym_rx) 866 continue; 867 868 if (i != 0) 869 seq_putc(s, ' '); 870 871 if (lane_names[i].lane == margining->lanes) 872 seq_printf(s, "[%s]", lane_names[i].name); 873 else 874 seq_printf(s, "%s", lane_names[i].name); 875 } 876 seq_puts(s, "\n"); 877 } 878 879 return 0; 880 } 881 DEBUGFS_ATTR_RW(margining_lanes); 882 883 static ssize_t 884 margining_voltage_time_offset_write(struct file *file, 885 const char __user *user_buf, 886 size_t count, loff_t *ppos) 887 { 888 struct seq_file *s = file->private_data; 889 struct tb_margining *margining = s->private; 890 struct tb *tb = margining->port->sw->tb; 891 unsigned int max_margin; 892 unsigned int val; 893 int ret; 894 895 ret = kstrtouint_from_user(user_buf, count, 10, &val); 896 if (ret) 897 return ret; 898 899 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 900 if (!margining->software) 901 return -EOPNOTSUPP; 902 903 if (margining->time) 904 max_margin = margining->time_steps; 905 else 906 if (margining->optional_voltage_offset_range) 907 max_margin = margining->voltage_steps_optional_range; 908 else 909 max_margin = margining->voltage_steps; 910 911 margining->voltage_time_offset = clamp(val, 0, max_margin); 912 } 913 914 return count; 915 } 916 917 static int margining_voltage_time_offset_show(struct seq_file *s, 918 void *not_used) 919 { 920 const struct tb_margining *margining = s->private; 921 struct tb *tb = margining->port->sw->tb; 922 923 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 924 if (!margining->software) 925 return -EOPNOTSUPP; 926 927 seq_printf(s, "%d\n", margining->voltage_time_offset); 928 } 929 930 return 0; 931 } 932 DEBUGFS_ATTR_RW(margining_voltage_time_offset); 933 934 static ssize_t 935 margining_error_counter_write(struct file *file, const char __user *user_buf, 936 size_t count, loff_t *ppos) 937 { 938 enum usb4_margin_sw_error_counter error_counter; 939 struct seq_file *s = file->private_data; 940 struct tb_margining *margining = s->private; 941 struct tb *tb = margining->port->sw->tb; 942 char *buf; 943 944 buf = validate_and_copy_from_user(user_buf, &count); 945 if (IS_ERR(buf)) 946 return PTR_ERR(buf); 947 948 buf[count - 1] = '\0'; 949 950 if (!strcmp(buf, "nop")) 951 error_counter = USB4_MARGIN_SW_ERROR_COUNTER_NOP; 952 else if (!strcmp(buf, "clear")) 953 error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR; 954 else if (!strcmp(buf, "start")) 955 error_counter = USB4_MARGIN_SW_ERROR_COUNTER_START; 956 else if (!strcmp(buf, "stop")) 957 error_counter = USB4_MARGIN_SW_ERROR_COUNTER_STOP; 958 else 959 goto err_free; 960 961 free_page((unsigned long)buf); 962 963 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 964 if (!margining->software) 965 return -EOPNOTSUPP; 966 967 margining->error_counter = error_counter; 968 } 969 970 return count; 971 972 err_free: 973 free_page((unsigned long)buf); 974 return -EINVAL; 975 } 976 977 static int margining_error_counter_show(struct seq_file *s, void *not_used) 978 { 979 const struct tb_margining *margining = s->private; 980 struct tb *tb = margining->port->sw->tb; 981 982 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 983 if (!margining->software) 984 return -EOPNOTSUPP; 985 986 switch (margining->error_counter) { 987 case USB4_MARGIN_SW_ERROR_COUNTER_NOP: 988 seq_puts(s, "[nop] clear start stop\n"); 989 break; 990 case USB4_MARGIN_SW_ERROR_COUNTER_CLEAR: 991 seq_puts(s, "nop [clear] start stop\n"); 992 break; 993 case USB4_MARGIN_SW_ERROR_COUNTER_START: 994 seq_puts(s, "nop clear [start] stop\n"); 995 break; 996 case USB4_MARGIN_SW_ERROR_COUNTER_STOP: 997 seq_puts(s, "nop clear start [stop]\n"); 998 break; 999 } 1000 } 1001 1002 return 0; 1003 } 1004 DEBUGFS_ATTR_RW(margining_error_counter); 1005 1006 static ssize_t 1007 margining_dwell_time_write(struct file *file, const char __user *user_buf, 1008 size_t count, loff_t *ppos) 1009 { 1010 struct seq_file *s = file->private_data; 1011 struct tb_margining *margining = s->private; 1012 struct tb *tb = margining->port->sw->tb; 1013 unsigned int val; 1014 int ret; 1015 1016 ret = kstrtouint_from_user(user_buf, count, 10, &val); 1017 if (ret) 1018 return ret; 1019 1020 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 1021 if (!margining->software) 1022 return -EOPNOTSUPP; 1023 1024 margining->dwell_time = clamp(val, MIN_DWELL_TIME, MAX_DWELL_TIME); 1025 } 1026 1027 return count; 1028 } 1029 1030 static int margining_dwell_time_show(struct seq_file *s, void *not_used) 1031 { 1032 struct tb_margining *margining = s->private; 1033 struct tb *tb = margining->port->sw->tb; 1034 1035 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 1036 if (!margining->software) 1037 return -EOPNOTSUPP; 1038 1039 seq_printf(s, "%d\n", margining->dwell_time); 1040 } 1041 1042 return 0; 1043 } 1044 DEBUGFS_ATTR_RW(margining_dwell_time); 1045 1046 static ssize_t 1047 margining_optional_voltage_offset_write(struct file *file, const char __user *user_buf, 1048 size_t count, loff_t *ppos) 1049 { 1050 struct seq_file *s = file->private_data; 1051 struct tb_margining *margining = s->private; 1052 struct tb *tb = margining->port->sw->tb; 1053 bool val; 1054 int ret; 1055 1056 ret = kstrtobool_from_user(user_buf, count, &val); 1057 if (ret) 1058 return ret; 1059 1060 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 1061 margining->optional_voltage_offset_range = val; 1062 } 1063 1064 return count; 1065 } 1066 1067 static int margining_optional_voltage_offset_show(struct seq_file *s, 1068 void *not_used) 1069 { 1070 struct tb_margining *margining = s->private; 1071 struct tb *tb = margining->port->sw->tb; 1072 1073 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &tb->lock) { 1074 seq_printf(s, "%u\n", margining->optional_voltage_offset_range); 1075 } 1076 1077 return 0; 1078 } 1079 DEBUGFS_ATTR_RW(margining_optional_voltage_offset); 1080 1081 static ssize_t margining_mode_write(struct file *file, 1082 const char __user *user_buf, 1083 size_t count, loff_t *ppos) 1084 { 1085 struct seq_file *s = file->private_data; 1086 struct tb_margining *margining = s->private; 1087 struct tb *tb = margining->port->sw->tb; 1088 int ret = 0; 1089 char *buf; 1090 1091 buf = validate_and_copy_from_user(user_buf, &count); 1092 if (IS_ERR(buf)) 1093 return PTR_ERR(buf); 1094 1095 buf[count - 1] = '\0'; 1096 1097 if (mutex_lock_interruptible(&tb->lock)) { 1098 ret = -ERESTARTSYS; 1099 goto out_free; 1100 } 1101 1102 if (!strcmp(buf, "software")) { 1103 if (supports_software(margining)) 1104 margining->software = true; 1105 else 1106 ret = -EINVAL; 1107 } else if (!strcmp(buf, "hardware")) { 1108 if (supports_hardware(margining)) 1109 margining->software = false; 1110 else 1111 ret = -EINVAL; 1112 } else { 1113 ret = -EINVAL; 1114 } 1115 1116 mutex_unlock(&tb->lock); 1117 1118 out_free: 1119 free_page((unsigned long)buf); 1120 return ret ? ret : count; 1121 } 1122 1123 static int margining_mode_show(struct seq_file *s, void *not_used) 1124 { 1125 struct tb_margining *margining = s->private; 1126 struct tb *tb = margining->port->sw->tb; 1127 const char *space = ""; 1128 1129 if (mutex_lock_interruptible(&tb->lock)) 1130 return -ERESTARTSYS; 1131 1132 if (supports_software(margining)) { 1133 if (margining->software) 1134 seq_puts(s, "[software]"); 1135 else 1136 seq_puts(s, "software"); 1137 space = " "; 1138 } 1139 if (supports_hardware(margining)) { 1140 if (margining->software) 1141 seq_printf(s, "%shardware", space); 1142 else 1143 seq_printf(s, "%s[hardware]", space); 1144 } 1145 1146 mutex_unlock(&tb->lock); 1147 1148 seq_puts(s, "\n"); 1149 return 0; 1150 } 1151 DEBUGFS_ATTR_RW(margining_mode); 1152 1153 static int margining_run_sw(struct tb_margining *margining, 1154 struct usb4_port_margining_params *params) 1155 { 1156 u32 nsamples = margining->dwell_time / DWELL_SAMPLE_INTERVAL; 1157 int ret, i; 1158 1159 ret = usb4_port_sw_margin(margining->port, margining->target, margining->index, 1160 params, margining->results); 1161 if (ret) 1162 goto out_stop; 1163 1164 for (i = 0; i <= nsamples; i++) { 1165 u32 errors = 0; 1166 1167 ret = usb4_port_sw_margin_errors(margining->port, margining->target, 1168 margining->index, &margining->results[1]); 1169 if (ret) 1170 break; 1171 1172 if (margining->lanes == USB4_MARGINING_LANE_RX0) 1173 errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK, 1174 margining->results[1]); 1175 else if (margining->lanes == USB4_MARGINING_LANE_RX1) 1176 errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK, 1177 margining->results[1]); 1178 else if (margining->lanes == USB4_MARGINING_LANE_RX2) 1179 errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK, 1180 margining->results[1]); 1181 else if (margining->lanes == USB4_MARGINING_LANE_ALL) 1182 errors = margining->results[1]; 1183 1184 /* Any errors stop the test */ 1185 if (errors) 1186 break; 1187 1188 fsleep(DWELL_SAMPLE_INTERVAL * USEC_PER_MSEC); 1189 } 1190 1191 out_stop: 1192 /* 1193 * Stop the counters but don't clear them to allow the 1194 * different error counter configurations. 1195 */ 1196 margining_modify_error_counter(margining, margining->lanes, 1197 USB4_MARGIN_SW_ERROR_COUNTER_STOP); 1198 return ret; 1199 } 1200 1201 static int validate_margining(struct tb_margining *margining) 1202 { 1203 /* 1204 * For running on RX2 the link must be asymmetric with 3 1205 * receivers. Because this can change dynamically, check it 1206 * here before we start the margining and report back error if 1207 * expectations are not met. 1208 */ 1209 if (margining->lanes == USB4_MARGINING_LANE_RX2) { 1210 int ret; 1211 1212 ret = tb_port_get_link_width(margining->port); 1213 if (ret < 0) 1214 return ret; 1215 if (ret != TB_LINK_WIDTH_ASYM_RX) { 1216 tb_port_warn(margining->port, "link is %s expected %s", 1217 tb_width_name(ret), 1218 tb_width_name(TB_LINK_WIDTH_ASYM_RX)); 1219 return -EINVAL; 1220 } 1221 } 1222 1223 return 0; 1224 } 1225 1226 static int margining_run_write(void *data, u64 val) 1227 { 1228 struct tb_margining *margining = data; 1229 struct tb_port *port = margining->port; 1230 struct device *dev = margining->dev; 1231 struct tb_switch *sw = port->sw; 1232 struct tb_switch *down_sw; 1233 struct tb *tb = sw->tb; 1234 int ret, clx; 1235 1236 if (val != 1) 1237 return -EINVAL; 1238 1239 pm_runtime_get_sync(dev); 1240 1241 if (mutex_lock_interruptible(&tb->lock)) { 1242 ret = -ERESTARTSYS; 1243 goto out_rpm_put; 1244 } 1245 1246 ret = validate_margining(margining); 1247 if (ret) 1248 goto out_unlock; 1249 1250 if (tb_is_upstream_port(port)) 1251 down_sw = sw; 1252 else if (port->remote) 1253 down_sw = port->remote->sw; 1254 else 1255 down_sw = NULL; 1256 1257 if (down_sw) { 1258 /* 1259 * CL states may interfere with lane margining so 1260 * disable them temporarily now. 1261 */ 1262 ret = tb_switch_clx_disable(down_sw); 1263 if (ret < 0) { 1264 tb_sw_warn(down_sw, "failed to disable CL states\n"); 1265 goto out_unlock; 1266 } 1267 clx = ret; 1268 } 1269 1270 /* Clear the results */ 1271 memset(margining->results, 0, sizeof(margining->results)); 1272 1273 if (margining->software) { 1274 struct usb4_port_margining_params params = { 1275 .error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR, 1276 .lanes = margining->lanes, 1277 .time = margining->time, 1278 .voltage_time_offset = margining->voltage_time_offset, 1279 .right_high = margining->right_high, 1280 .upper_eye = margining->upper_eye, 1281 .optional_voltage_offset_range = margining->optional_voltage_offset_range, 1282 }; 1283 1284 tb_port_dbg(port, 1285 "running software %s lane margining for %s lanes %u\n", 1286 margining->time ? "time" : "voltage", dev_name(dev), 1287 margining->lanes); 1288 1289 ret = margining_run_sw(margining, ¶ms); 1290 } else { 1291 struct usb4_port_margining_params params = { 1292 .ber_level = margining->ber_level, 1293 .lanes = margining->lanes, 1294 .time = margining->time, 1295 .right_high = margining->right_high, 1296 .upper_eye = margining->upper_eye, 1297 .optional_voltage_offset_range = margining->optional_voltage_offset_range, 1298 }; 1299 1300 tb_port_dbg(port, 1301 "running hardware %s lane margining for %s lanes %u\n", 1302 margining->time ? "time" : "voltage", dev_name(dev), 1303 margining->lanes); 1304 1305 ret = usb4_port_hw_margin(port, margining->target, margining->index, ¶ms, 1306 margining->results, ARRAY_SIZE(margining->results)); 1307 } 1308 1309 if (down_sw) 1310 tb_switch_clx_enable(down_sw, clx); 1311 out_unlock: 1312 mutex_unlock(&tb->lock); 1313 out_rpm_put: 1314 pm_runtime_mark_last_busy(dev); 1315 pm_runtime_put_autosuspend(dev); 1316 1317 return ret; 1318 } 1319 DEFINE_DEBUGFS_ATTRIBUTE(margining_run_fops, NULL, margining_run_write, 1320 "%llu\n"); 1321 1322 static ssize_t margining_results_write(struct file *file, 1323 const char __user *user_buf, 1324 size_t count, loff_t *ppos) 1325 { 1326 struct seq_file *s = file->private_data; 1327 struct tb_margining *margining = s->private; 1328 struct tb *tb = margining->port->sw->tb; 1329 1330 if (mutex_lock_interruptible(&tb->lock)) 1331 return -ERESTARTSYS; 1332 1333 /* Just clear the results */ 1334 memset(margining->results, 0, sizeof(margining->results)); 1335 1336 if (margining->software) { 1337 /* Clear the error counters */ 1338 margining_modify_error_counter(margining, 1339 USB4_MARGINING_LANE_ALL, 1340 USB4_MARGIN_SW_ERROR_COUNTER_CLEAR); 1341 } 1342 1343 mutex_unlock(&tb->lock); 1344 return count; 1345 } 1346 1347 static void voltage_margin_show(struct seq_file *s, 1348 const struct tb_margining *margining, u8 val) 1349 { 1350 unsigned int tmp, voltage; 1351 1352 tmp = FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK, val); 1353 voltage = tmp * margining->max_voltage_offset / margining->voltage_steps; 1354 seq_printf(s, "%u mV (%u)", voltage, tmp); 1355 if (val & USB4_MARGIN_HW_RES_EXCEEDS) 1356 seq_puts(s, " exceeds maximum"); 1357 seq_puts(s, "\n"); 1358 if (margining->optional_voltage_offset_range) 1359 seq_puts(s, " optional voltage offset range enabled\n"); 1360 } 1361 1362 static void time_margin_show(struct seq_file *s, 1363 const struct tb_margining *margining, u8 val) 1364 { 1365 unsigned int tmp, interval; 1366 1367 tmp = FIELD_GET(USB4_MARGIN_HW_RES_MARGIN_MASK, val); 1368 interval = tmp * margining->max_time_offset / margining->time_steps; 1369 seq_printf(s, "%u mUI (%u)", interval, tmp); 1370 if (val & USB4_MARGIN_HW_RES_EXCEEDS) 1371 seq_puts(s, " exceeds maximum"); 1372 seq_puts(s, "\n"); 1373 } 1374 1375 static u8 margining_hw_result_val(const u32 *results, 1376 enum usb4_margining_lane lane, 1377 bool right_high) 1378 { 1379 u32 val; 1380 1381 if (lane == USB4_MARGINING_LANE_RX0) 1382 val = results[1]; 1383 else if (lane == USB4_MARGINING_LANE_RX1) 1384 val = results[1] >> USB4_MARGIN_HW_RES_LANE_SHIFT; 1385 else if (lane == USB4_MARGINING_LANE_RX2) 1386 val = results[2]; 1387 else 1388 val = 0; 1389 1390 return right_high ? val : val >> USB4_MARGIN_HW_RES_LL_SHIFT; 1391 } 1392 1393 static void margining_hw_result_format(struct seq_file *s, 1394 const struct tb_margining *margining, 1395 enum usb4_margining_lane lane) 1396 { 1397 u8 val; 1398 1399 if (margining->time) { 1400 val = margining_hw_result_val(margining->results, lane, true); 1401 seq_printf(s, "# lane %u right time margin: ", lane); 1402 time_margin_show(s, margining, val); 1403 val = margining_hw_result_val(margining->results, lane, false); 1404 seq_printf(s, "# lane %u left time margin: ", lane); 1405 time_margin_show(s, margining, val); 1406 } else { 1407 val = margining_hw_result_val(margining->results, lane, true); 1408 seq_printf(s, "# lane %u high voltage margin: ", lane); 1409 voltage_margin_show(s, margining, val); 1410 val = margining_hw_result_val(margining->results, lane, false); 1411 seq_printf(s, "# lane %u low voltage margin: ", lane); 1412 voltage_margin_show(s, margining, val); 1413 } 1414 } 1415 1416 static int margining_results_show(struct seq_file *s, void *not_used) 1417 { 1418 struct tb_margining *margining = s->private; 1419 struct tb *tb = margining->port->sw->tb; 1420 1421 if (mutex_lock_interruptible(&tb->lock)) 1422 return -ERESTARTSYS; 1423 1424 /* Dump the raw results first */ 1425 seq_printf(s, "0x%08x\n", margining->results[0]); 1426 /* Only the hardware margining has two result dwords */ 1427 if (!margining->software) { 1428 for (int i = 1; i < ARRAY_SIZE(margining->results); i++) 1429 seq_printf(s, "0x%08x\n", margining->results[i]); 1430 1431 if (margining->lanes == USB4_MARGINING_LANE_ALL) { 1432 margining_hw_result_format(s, margining, 1433 USB4_MARGINING_LANE_RX0); 1434 margining_hw_result_format(s, margining, 1435 USB4_MARGINING_LANE_RX1); 1436 if (margining->asym_rx) 1437 margining_hw_result_format(s, margining, 1438 USB4_MARGINING_LANE_RX2); 1439 } else { 1440 margining_hw_result_format(s, margining, 1441 margining->lanes); 1442 } 1443 } else { 1444 u32 lane_errors, result; 1445 1446 seq_printf(s, "0x%08x\n", margining->results[1]); 1447 1448 result = FIELD_GET(USB4_MARGIN_SW_LANES_MASK, margining->results[0]); 1449 if (result == USB4_MARGINING_LANE_RX0 || 1450 result == USB4_MARGINING_LANE_ALL) { 1451 lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_0_MASK, 1452 margining->results[1]); 1453 seq_printf(s, "# lane 0 errors: %u\n", lane_errors); 1454 } 1455 if (result == USB4_MARGINING_LANE_RX1 || 1456 result == USB4_MARGINING_LANE_ALL) { 1457 lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_1_MASK, 1458 margining->results[1]); 1459 seq_printf(s, "# lane 1 errors: %u\n", lane_errors); 1460 } 1461 if (margining->asym_rx && 1462 (result == USB4_MARGINING_LANE_RX2 || 1463 result == USB4_MARGINING_LANE_ALL)) { 1464 lane_errors = FIELD_GET(USB4_MARGIN_SW_ERR_COUNTER_LANE_2_MASK, 1465 margining->results[1]); 1466 seq_printf(s, "# lane 2 errors: %u\n", lane_errors); 1467 } 1468 } 1469 1470 mutex_unlock(&tb->lock); 1471 return 0; 1472 } 1473 DEBUGFS_ATTR_RW(margining_results); 1474 1475 static ssize_t margining_test_write(struct file *file, 1476 const char __user *user_buf, 1477 size_t count, loff_t *ppos) 1478 { 1479 struct seq_file *s = file->private_data; 1480 struct tb_margining *margining = s->private; 1481 struct tb *tb = margining->port->sw->tb; 1482 int ret = 0; 1483 char *buf; 1484 1485 buf = validate_and_copy_from_user(user_buf, &count); 1486 if (IS_ERR(buf)) 1487 return PTR_ERR(buf); 1488 1489 buf[count - 1] = '\0'; 1490 1491 if (mutex_lock_interruptible(&tb->lock)) { 1492 ret = -ERESTARTSYS; 1493 goto out_free; 1494 } 1495 1496 if (!strcmp(buf, "time") && supports_time(margining)) 1497 margining->time = true; 1498 else if (!strcmp(buf, "voltage")) 1499 margining->time = false; 1500 else 1501 ret = -EINVAL; 1502 1503 mutex_unlock(&tb->lock); 1504 1505 out_free: 1506 free_page((unsigned long)buf); 1507 return ret ? ret : count; 1508 } 1509 1510 static int margining_test_show(struct seq_file *s, void *not_used) 1511 { 1512 struct tb_margining *margining = s->private; 1513 struct tb *tb = margining->port->sw->tb; 1514 1515 if (mutex_lock_interruptible(&tb->lock)) 1516 return -ERESTARTSYS; 1517 1518 if (supports_time(margining)) { 1519 if (margining->time) 1520 seq_puts(s, "voltage [time]\n"); 1521 else 1522 seq_puts(s, "[voltage] time\n"); 1523 } else { 1524 seq_puts(s, "[voltage]\n"); 1525 } 1526 1527 mutex_unlock(&tb->lock); 1528 return 0; 1529 } 1530 DEBUGFS_ATTR_RW(margining_test); 1531 1532 static ssize_t margining_margin_write(struct file *file, 1533 const char __user *user_buf, 1534 size_t count, loff_t *ppos) 1535 { 1536 struct seq_file *s = file->private_data; 1537 struct tb_margining *margining = s->private; 1538 struct tb *tb = margining->port->sw->tb; 1539 int ret = 0; 1540 char *buf; 1541 1542 buf = validate_and_copy_from_user(user_buf, &count); 1543 if (IS_ERR(buf)) 1544 return PTR_ERR(buf); 1545 1546 buf[count - 1] = '\0'; 1547 1548 if (mutex_lock_interruptible(&tb->lock)) { 1549 ret = -ERESTARTSYS; 1550 goto out_free; 1551 } 1552 1553 if (margining->time) { 1554 if (!strcmp(buf, "left")) 1555 margining->right_high = false; 1556 else if (!strcmp(buf, "right")) 1557 margining->right_high = true; 1558 else 1559 ret = -EINVAL; 1560 } else { 1561 if (!strcmp(buf, "low")) 1562 margining->right_high = false; 1563 else if (!strcmp(buf, "high")) 1564 margining->right_high = true; 1565 else 1566 ret = -EINVAL; 1567 } 1568 1569 mutex_unlock(&tb->lock); 1570 1571 out_free: 1572 free_page((unsigned long)buf); 1573 return ret ? ret : count; 1574 } 1575 1576 static int margining_margin_show(struct seq_file *s, void *not_used) 1577 { 1578 struct tb_margining *margining = s->private; 1579 struct tb *tb = margining->port->sw->tb; 1580 1581 if (mutex_lock_interruptible(&tb->lock)) 1582 return -ERESTARTSYS; 1583 1584 if (margining->time) { 1585 if (margining->right_high) 1586 seq_puts(s, "left [right]\n"); 1587 else 1588 seq_puts(s, "[left] right\n"); 1589 } else { 1590 if (margining->right_high) 1591 seq_puts(s, "low [high]\n"); 1592 else 1593 seq_puts(s, "[low] high\n"); 1594 } 1595 1596 mutex_unlock(&tb->lock); 1597 return 0; 1598 } 1599 DEBUGFS_ATTR_RW(margining_margin); 1600 1601 static ssize_t margining_eye_write(struct file *file, 1602 const char __user *user_buf, 1603 size_t count, loff_t *ppos) 1604 { 1605 struct seq_file *s = file->private_data; 1606 struct tb_port *port = s->private; 1607 struct usb4_port *usb4 = port->usb4; 1608 struct tb *tb = port->sw->tb; 1609 int ret = 0; 1610 char *buf; 1611 1612 buf = validate_and_copy_from_user(user_buf, &count); 1613 if (IS_ERR(buf)) 1614 return PTR_ERR(buf); 1615 1616 buf[count - 1] = '\0'; 1617 1618 scoped_cond_guard(mutex_intr, ret = -ERESTARTSYS, &tb->lock) { 1619 if (!strcmp(buf, "lower")) 1620 usb4->margining->upper_eye = false; 1621 else if (!strcmp(buf, "upper")) 1622 usb4->margining->upper_eye = true; 1623 else 1624 ret = -EINVAL; 1625 } 1626 1627 free_page((unsigned long)buf); 1628 return ret ? ret : count; 1629 } 1630 1631 static int margining_eye_show(struct seq_file *s, void *not_used) 1632 { 1633 struct tb_port *port = s->private; 1634 struct usb4_port *usb4 = port->usb4; 1635 struct tb *tb = port->sw->tb; 1636 1637 scoped_guard(mutex_intr, &tb->lock) { 1638 if (usb4->margining->upper_eye) 1639 seq_puts(s, "lower [upper]\n"); 1640 else 1641 seq_puts(s, "[lower] upper\n"); 1642 1643 return 0; 1644 } 1645 1646 return -ERESTARTSYS; 1647 } 1648 DEBUGFS_ATTR_RW(margining_eye); 1649 1650 static struct tb_margining *margining_alloc(struct tb_port *port, 1651 struct device *dev, 1652 enum usb4_sb_target target, 1653 u8 index, struct dentry *parent) 1654 { 1655 struct tb_margining *margining; 1656 struct dentry *dir; 1657 unsigned int val; 1658 int ret; 1659 1660 ret = tb_port_get_link_generation(port); 1661 if (ret < 0) { 1662 tb_port_warn(port, "failed to read link generation\n"); 1663 return NULL; 1664 } 1665 1666 margining = kzalloc_obj(*margining); 1667 if (!margining) 1668 return NULL; 1669 1670 margining->port = port; 1671 margining->target = target; 1672 margining->index = index; 1673 margining->dev = dev; 1674 margining->gen = ret; 1675 margining->asym_rx = tb_port_width_supported(port, TB_LINK_WIDTH_ASYM_RX); 1676 1677 ret = usb4_port_margining_caps(port, target, index, margining->caps, 1678 ARRAY_SIZE(margining->caps)); 1679 if (ret) { 1680 kfree(margining); 1681 return NULL; 1682 } 1683 1684 /* Set the initial mode */ 1685 if (supports_software(margining)) 1686 margining->software = true; 1687 1688 if (margining->gen < 4) { 1689 val = FIELD_GET(USB4_MARGIN_CAP_0_VOLTAGE_STEPS_MASK, margining->caps[0]); 1690 margining->voltage_steps = val; 1691 val = FIELD_GET(USB4_MARGIN_CAP_0_MAX_VOLTAGE_OFFSET_MASK, margining->caps[0]); 1692 margining->max_voltage_offset = 74 + val * 2; 1693 } else { 1694 val = FIELD_GET(USB4_MARGIN_CAP_2_VOLTAGE_STEPS_MASK, margining->caps[2]); 1695 margining->voltage_steps = val; 1696 val = FIELD_GET(USB4_MARGIN_CAP_2_MAX_VOLTAGE_OFFSET_MASK, margining->caps[2]); 1697 margining->max_voltage_offset = 74 + val * 2; 1698 } 1699 1700 if (supports_optional_voltage_offset_range(margining)) { 1701 val = FIELD_GET(USB4_MARGIN_CAP_0_VOLT_STEPS_OPT_MASK, 1702 margining->caps[0]); 1703 margining->voltage_steps_optional_range = val; 1704 val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_VOLT_OFS_OPT_MASK, 1705 margining->caps[1]); 1706 margining->max_voltage_offset_optional_range = 74 + val * 2; 1707 } 1708 1709 if (supports_time(margining)) { 1710 val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_STEPS_MASK, margining->caps[1]); 1711 margining->time_steps = val; 1712 val = FIELD_GET(USB4_MARGIN_CAP_1_TIME_OFFSET_MASK, margining->caps[1]); 1713 /* 1714 * Store it as mUI (milli Unit Interval) because we want 1715 * to keep it as integer. 1716 */ 1717 margining->max_time_offset = 200 + 10 * val; 1718 } 1719 1720 dir = debugfs_create_dir("margining", parent); 1721 if (supports_hardware(margining)) { 1722 val = FIELD_GET(USB4_MARGIN_CAP_1_MIN_BER_MASK, margining->caps[1]); 1723 margining->min_ber_level = val; 1724 val = FIELD_GET(USB4_MARGIN_CAP_1_MAX_BER_MASK, margining->caps[1]); 1725 margining->max_ber_level = val; 1726 1727 /* Set the default to minimum */ 1728 margining->ber_level = margining->min_ber_level; 1729 1730 debugfs_create_file("ber_level_contour", 0400, dir, margining, 1731 &margining_ber_level_fops); 1732 } 1733 debugfs_create_file("caps", 0400, dir, margining, &margining_caps_fops); 1734 debugfs_create_file("lanes", 0600, dir, margining, &margining_lanes_fops); 1735 debugfs_create_file("mode", 0600, dir, margining, &margining_mode_fops); 1736 debugfs_create_file("run", 0600, dir, margining, &margining_run_fops); 1737 debugfs_create_file("results", 0600, dir, margining, 1738 &margining_results_fops); 1739 debugfs_create_file("test", 0600, dir, margining, &margining_test_fops); 1740 if (independent_voltage_margins(margining) == USB4_MARGIN_CAP_VOLTAGE_INDP_GEN_2_3_HL || 1741 (supports_time(margining) && 1742 independent_time_margins(margining) == USB4_MARGIN_CAP_TIME_INDP_GEN_2_3_LR)) 1743 debugfs_create_file("margin", 0600, dir, margining, &margining_margin_fops); 1744 1745 margining->error_counter = USB4_MARGIN_SW_ERROR_COUNTER_CLEAR; 1746 margining->dwell_time = MIN_DWELL_TIME; 1747 1748 if (supports_optional_voltage_offset_range(margining)) 1749 debugfs_create_file("optional_voltage_offset", DEBUGFS_MODE, dir, margining, 1750 &margining_optional_voltage_offset_fops); 1751 1752 if (supports_software(margining)) { 1753 debugfs_create_file("voltage_time_offset", DEBUGFS_MODE, dir, margining, 1754 &margining_voltage_time_offset_fops); 1755 debugfs_create_file("error_counter", DEBUGFS_MODE, dir, margining, 1756 &margining_error_counter_fops); 1757 debugfs_create_file("dwell_time", DEBUGFS_MODE, dir, margining, 1758 &margining_dwell_time_fops); 1759 } 1760 1761 if (margining->gen >= 4) 1762 debugfs_create_file("eye", 0600, dir, port, &margining_eye_fops); 1763 1764 return margining; 1765 } 1766 1767 static void margining_port_init(struct tb_port *port) 1768 { 1769 struct dentry *parent; 1770 char dir_name[10]; 1771 1772 if (!port->usb4) 1773 return; 1774 1775 snprintf(dir_name, sizeof(dir_name), "port%d", port->port); 1776 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir); 1777 port->usb4->margining = margining_alloc(port, &port->usb4->dev, 1778 USB4_SB_TARGET_ROUTER, 0, 1779 parent); 1780 } 1781 1782 static void margining_port_remove(struct tb_port *port) 1783 { 1784 struct dentry *parent; 1785 char dir_name[10]; 1786 1787 if (!port->usb4) 1788 return; 1789 if (!port->usb4->margining) 1790 return; 1791 1792 snprintf(dir_name, sizeof(dir_name), "port%d", port->port); 1793 parent = debugfs_lookup(dir_name, port->sw->debugfs_dir); 1794 if (parent) 1795 debugfs_lookup_and_remove("margining", parent); 1796 1797 kfree(port->usb4->margining); 1798 port->usb4->margining = NULL; 1799 } 1800 1801 static void margining_switch_init(struct tb_switch *sw) 1802 { 1803 struct tb_port *upstream, *downstream; 1804 struct tb_switch *parent_sw; 1805 u64 route = tb_route(sw); 1806 1807 if (!route) 1808 return; 1809 1810 upstream = tb_upstream_port(sw); 1811 parent_sw = tb_switch_parent(sw); 1812 downstream = tb_port_at(route, parent_sw); 1813 1814 margining_port_init(downstream); 1815 margining_port_init(upstream); 1816 } 1817 1818 static void margining_switch_remove(struct tb_switch *sw) 1819 { 1820 struct tb_port *upstream, *downstream; 1821 struct tb_switch *parent_sw; 1822 u64 route = tb_route(sw); 1823 1824 if (!route) 1825 return; 1826 1827 upstream = tb_upstream_port(sw); 1828 parent_sw = tb_switch_parent(sw); 1829 downstream = tb_port_at(route, parent_sw); 1830 1831 margining_port_remove(upstream); 1832 margining_port_remove(downstream); 1833 } 1834 1835 static void margining_xdomain_init(struct tb_xdomain *xd) 1836 { 1837 struct tb_switch *parent_sw; 1838 struct tb_port *downstream; 1839 1840 parent_sw = tb_xdomain_parent(xd); 1841 downstream = tb_port_at(xd->route, parent_sw); 1842 1843 margining_port_init(downstream); 1844 } 1845 1846 static void margining_xdomain_remove(struct tb_xdomain *xd) 1847 { 1848 struct tb_switch *parent_sw; 1849 struct tb_port *downstream; 1850 1851 parent_sw = tb_xdomain_parent(xd); 1852 downstream = tb_port_at(xd->route, parent_sw); 1853 margining_port_remove(downstream); 1854 } 1855 1856 static void margining_retimer_init(struct tb_retimer *rt, struct dentry *debugfs_dir) 1857 { 1858 rt->margining = margining_alloc(rt->port, &rt->dev, 1859 USB4_SB_TARGET_RETIMER, rt->index, 1860 debugfs_dir); 1861 } 1862 1863 static void margining_retimer_remove(struct tb_retimer *rt) 1864 { 1865 kfree(rt->margining); 1866 rt->margining = NULL; 1867 } 1868 #else 1869 static inline void margining_switch_init(struct tb_switch *sw) { } 1870 static inline void margining_switch_remove(struct tb_switch *sw) { } 1871 static inline void margining_xdomain_init(struct tb_xdomain *xd) { } 1872 static inline void margining_xdomain_remove(struct tb_xdomain *xd) { } 1873 static inline void margining_retimer_init(struct tb_retimer *rt, 1874 struct dentry *debugfs_dir) { } 1875 static inline void margining_retimer_remove(struct tb_retimer *rt) { } 1876 #endif 1877 1878 static int port_clear_all_counters(struct tb_port *port) 1879 { 1880 u32 *buf; 1881 int ret; 1882 1883 buf = kcalloc(COUNTER_SET_LEN * port->config.max_counters, sizeof(u32), 1884 GFP_KERNEL); 1885 if (!buf) 1886 return -ENOMEM; 1887 1888 ret = tb_port_write(port, buf, TB_CFG_COUNTERS, 0, 1889 COUNTER_SET_LEN * port->config.max_counters); 1890 kfree(buf); 1891 1892 return ret; 1893 } 1894 1895 static ssize_t counters_write(struct file *file, const char __user *user_buf, 1896 size_t count, loff_t *ppos) 1897 { 1898 struct seq_file *s = file->private_data; 1899 struct tb_port *port = s->private; 1900 struct tb_switch *sw = port->sw; 1901 struct tb *tb = port->sw->tb; 1902 char *buf; 1903 int ret; 1904 1905 buf = validate_and_copy_from_user(user_buf, &count); 1906 if (IS_ERR(buf)) 1907 return PTR_ERR(buf); 1908 1909 pm_runtime_get_sync(&sw->dev); 1910 1911 if (mutex_lock_interruptible(&tb->lock)) { 1912 ret = -ERESTARTSYS; 1913 goto out; 1914 } 1915 1916 /* If written delimiter only, clear all counters in one shot */ 1917 if (buf[0] == '\n') { 1918 ret = port_clear_all_counters(port); 1919 } else { 1920 char *line = buf; 1921 u32 val, offset; 1922 1923 ret = -EINVAL; 1924 while (parse_line(&line, &offset, &val, 1, 4)) { 1925 ret = tb_port_write(port, &val, TB_CFG_COUNTERS, 1926 offset, 1); 1927 if (ret) 1928 break; 1929 } 1930 } 1931 1932 mutex_unlock(&tb->lock); 1933 1934 out: 1935 pm_runtime_mark_last_busy(&sw->dev); 1936 pm_runtime_put_autosuspend(&sw->dev); 1937 free_page((unsigned long)buf); 1938 1939 return ret < 0 ? ret : count; 1940 } 1941 1942 static void cap_show_by_dw(struct seq_file *s, struct tb_switch *sw, 1943 struct tb_port *port, unsigned int cap, 1944 unsigned int offset, u8 cap_id, u8 vsec_id, 1945 int dwords) 1946 { 1947 int i, ret; 1948 u32 data; 1949 1950 for (i = 0; i < dwords; i++) { 1951 if (port) 1952 ret = tb_port_read(port, &data, TB_CFG_PORT, cap + offset + i, 1); 1953 else 1954 ret = tb_sw_read(sw, &data, TB_CFG_SWITCH, cap + offset + i, 1); 1955 if (ret) { 1956 seq_printf(s, "0x%04x <not accessible>\n", cap + offset + i); 1957 continue; 1958 } 1959 1960 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", cap + offset + i, 1961 offset + i, cap_id, vsec_id, data); 1962 } 1963 } 1964 1965 static void cap_show(struct seq_file *s, struct tb_switch *sw, 1966 struct tb_port *port, unsigned int cap, u8 cap_id, 1967 u8 vsec_id, int length) 1968 { 1969 int ret, offset = 0; 1970 1971 while (length > 0) { 1972 int i, dwords = min(length, TB_MAX_CONFIG_RW_LENGTH); 1973 u32 data[TB_MAX_CONFIG_RW_LENGTH]; 1974 1975 if (port) 1976 ret = tb_port_read(port, data, TB_CFG_PORT, cap + offset, 1977 dwords); 1978 else 1979 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, cap + offset, dwords); 1980 if (ret) { 1981 cap_show_by_dw(s, sw, port, cap, offset, cap_id, vsec_id, length); 1982 return; 1983 } 1984 1985 for (i = 0; i < dwords; i++) { 1986 seq_printf(s, "0x%04x %4d 0x%02x 0x%02x 0x%08x\n", 1987 cap + offset + i, offset + i, 1988 cap_id, vsec_id, data[i]); 1989 } 1990 1991 length -= dwords; 1992 offset += dwords; 1993 } 1994 } 1995 1996 static void port_cap_show(struct tb_port *port, struct seq_file *s, 1997 unsigned int cap) 1998 { 1999 struct tb_cap_any header; 2000 u8 vsec_id = 0; 2001 size_t length; 2002 int ret; 2003 2004 ret = tb_port_read(port, &header, TB_CFG_PORT, cap, 1); 2005 if (ret) { 2006 seq_printf(s, "0x%04x <capability read failed>\n", cap); 2007 return; 2008 } 2009 2010 switch (header.basic.cap) { 2011 case TB_PORT_CAP_PHY: 2012 length = PORT_CAP_LANE_LEN; 2013 break; 2014 2015 case TB_PORT_CAP_TIME1: 2016 if (usb4_switch_version(port->sw) < 2) 2017 length = PORT_CAP_TMU_V1_LEN; 2018 else 2019 length = PORT_CAP_TMU_V2_LEN; 2020 break; 2021 2022 case TB_PORT_CAP_POWER: 2023 length = PORT_CAP_POWER_LEN; 2024 break; 2025 2026 case TB_PORT_CAP_ADAP: 2027 if (tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) { 2028 if (usb4_switch_version(port->sw) < 2) 2029 length = PORT_CAP_V1_PCIE_LEN; 2030 else 2031 length = PORT_CAP_V2_PCIE_LEN; 2032 } else if (tb_port_is_dpin(port)) { 2033 if (usb4_switch_version(port->sw) < 2) 2034 length = PORT_CAP_DP_V1_LEN; 2035 else 2036 length = PORT_CAP_DP_V2_LEN; 2037 } else if (tb_port_is_dpout(port)) { 2038 length = PORT_CAP_DP_V1_LEN; 2039 } else if (tb_port_is_usb3_down(port) || 2040 tb_port_is_usb3_up(port)) { 2041 length = PORT_CAP_USB3_LEN; 2042 } else { 2043 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n", 2044 cap, header.basic.cap); 2045 return; 2046 } 2047 break; 2048 2049 case TB_PORT_CAP_VSE: 2050 if (!header.extended_short.length) { 2051 ret = tb_port_read(port, (u32 *)&header + 1, TB_CFG_PORT, 2052 cap + 1, 1); 2053 if (ret) { 2054 seq_printf(s, "0x%04x <capability read failed>\n", 2055 cap + 1); 2056 return; 2057 } 2058 length = header.extended_long.length; 2059 vsec_id = header.extended_short.vsec_id; 2060 } else { 2061 length = header.extended_short.length; 2062 vsec_id = header.extended_short.vsec_id; 2063 } 2064 break; 2065 2066 case TB_PORT_CAP_USB4: 2067 length = PORT_CAP_USB4_LEN; 2068 break; 2069 2070 default: 2071 seq_printf(s, "0x%04x <unsupported capability 0x%02x>\n", 2072 cap, header.basic.cap); 2073 return; 2074 } 2075 2076 cap_show(s, NULL, port, cap, header.basic.cap, vsec_id, length); 2077 } 2078 2079 static void port_caps_show(struct tb_port *port, struct seq_file *s) 2080 { 2081 int cap; 2082 2083 cap = tb_port_next_cap(port, 0); 2084 while (cap > 0) { 2085 port_cap_show(port, s, cap); 2086 cap = tb_port_next_cap(port, cap); 2087 } 2088 } 2089 2090 static int port_basic_regs_show(struct tb_port *port, struct seq_file *s) 2091 { 2092 u32 data[PORT_CAP_BASIC_LEN]; 2093 int ret, i; 2094 2095 ret = tb_port_read(port, data, TB_CFG_PORT, 0, ARRAY_SIZE(data)); 2096 if (ret) 2097 return ret; 2098 2099 for (i = 0; i < ARRAY_SIZE(data); i++) 2100 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]); 2101 2102 return 0; 2103 } 2104 2105 static int port_regs_show(struct seq_file *s, void *not_used) 2106 { 2107 struct tb_port *port = s->private; 2108 struct tb_switch *sw = port->sw; 2109 struct tb *tb = sw->tb; 2110 int ret; 2111 2112 pm_runtime_get_sync(&sw->dev); 2113 2114 if (mutex_lock_interruptible(&tb->lock)) { 2115 ret = -ERESTARTSYS; 2116 goto out_rpm_put; 2117 } 2118 2119 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n"); 2120 2121 ret = port_basic_regs_show(port, s); 2122 if (ret) 2123 goto out_unlock; 2124 2125 port_caps_show(port, s); 2126 2127 out_unlock: 2128 mutex_unlock(&tb->lock); 2129 out_rpm_put: 2130 pm_runtime_mark_last_busy(&sw->dev); 2131 pm_runtime_put_autosuspend(&sw->dev); 2132 2133 return ret; 2134 } 2135 DEBUGFS_ATTR_RW(port_regs); 2136 2137 static void switch_cap_show(struct tb_switch *sw, struct seq_file *s, 2138 unsigned int cap) 2139 { 2140 struct tb_cap_any header; 2141 int ret, length; 2142 u8 vsec_id = 0; 2143 2144 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, cap, 1); 2145 if (ret) { 2146 seq_printf(s, "0x%04x <capability read failed>\n", cap); 2147 return; 2148 } 2149 2150 if (header.basic.cap == TB_SWITCH_CAP_VSE) { 2151 if (!header.extended_short.length) { 2152 ret = tb_sw_read(sw, (u32 *)&header + 1, TB_CFG_SWITCH, 2153 cap + 1, 1); 2154 if (ret) { 2155 seq_printf(s, "0x%04x <capability read failed>\n", 2156 cap + 1); 2157 return; 2158 } 2159 length = header.extended_long.length; 2160 } else { 2161 length = header.extended_short.length; 2162 } 2163 vsec_id = header.extended_short.vsec_id; 2164 } else { 2165 if (header.basic.cap == TB_SWITCH_CAP_TMU) { 2166 length = SWITCH_CAP_TMU_LEN; 2167 } else { 2168 seq_printf(s, "0x%04x <unknown capability 0x%02x>\n", 2169 cap, header.basic.cap); 2170 return; 2171 } 2172 } 2173 2174 cap_show(s, sw, NULL, cap, header.basic.cap, vsec_id, length); 2175 } 2176 2177 static void switch_caps_show(struct tb_switch *sw, struct seq_file *s) 2178 { 2179 int cap; 2180 2181 cap = tb_switch_next_cap(sw, 0); 2182 while (cap > 0) { 2183 switch_cap_show(sw, s, cap); 2184 cap = tb_switch_next_cap(sw, cap); 2185 } 2186 } 2187 2188 static int switch_basic_regs_show(struct tb_switch *sw, struct seq_file *s) 2189 { 2190 u32 data[SWITCH_CAP_BASIC_LEN]; 2191 size_t dwords; 2192 int ret, i; 2193 2194 /* Only USB4 has the additional registers */ 2195 if (tb_switch_is_usb4(sw)) 2196 dwords = ARRAY_SIZE(data); 2197 else 2198 dwords = 5; 2199 2200 ret = tb_sw_read(sw, data, TB_CFG_SWITCH, 0, dwords); 2201 if (ret) 2202 return ret; 2203 2204 for (i = 0; i < dwords; i++) 2205 seq_printf(s, "0x%04x %4d 0x00 0x00 0x%08x\n", i, i, data[i]); 2206 2207 return 0; 2208 } 2209 2210 static int switch_regs_show(struct seq_file *s, void *not_used) 2211 { 2212 struct tb_switch *sw = s->private; 2213 struct tb *tb = sw->tb; 2214 int ret; 2215 2216 pm_runtime_get_sync(&sw->dev); 2217 2218 if (mutex_lock_interruptible(&tb->lock)) { 2219 ret = -ERESTARTSYS; 2220 goto out_rpm_put; 2221 } 2222 2223 seq_puts(s, "# offset relative_offset cap_id vs_cap_id value\n"); 2224 2225 ret = switch_basic_regs_show(sw, s); 2226 if (ret) 2227 goto out_unlock; 2228 2229 switch_caps_show(sw, s); 2230 2231 out_unlock: 2232 mutex_unlock(&tb->lock); 2233 out_rpm_put: 2234 pm_runtime_mark_last_busy(&sw->dev); 2235 pm_runtime_put_autosuspend(&sw->dev); 2236 2237 return ret; 2238 } 2239 DEBUGFS_ATTR_RW(switch_regs); 2240 2241 static int path_show_one(struct tb_port *port, struct seq_file *s, int hopid) 2242 { 2243 u32 data[PATH_LEN]; 2244 int ret, i; 2245 2246 ret = tb_port_read(port, data, TB_CFG_HOPS, hopid * PATH_LEN, 2247 ARRAY_SIZE(data)); 2248 if (ret) { 2249 seq_printf(s, "0x%04x <not accessible>\n", hopid * PATH_LEN); 2250 return ret; 2251 } 2252 2253 for (i = 0; i < ARRAY_SIZE(data); i++) { 2254 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n", 2255 hopid * PATH_LEN + i, i, hopid, data[i]); 2256 } 2257 2258 return 0; 2259 } 2260 2261 static int path_show(struct seq_file *s, void *not_used) 2262 { 2263 struct tb_port *port = s->private; 2264 struct tb_switch *sw = port->sw; 2265 struct tb *tb = sw->tb; 2266 int start, i, ret = 0; 2267 2268 pm_runtime_get_sync(&sw->dev); 2269 2270 if (mutex_lock_interruptible(&tb->lock)) { 2271 ret = -ERESTARTSYS; 2272 goto out_rpm_put; 2273 } 2274 2275 seq_puts(s, "# offset relative_offset in_hop_id value\n"); 2276 2277 /* NHI and lane adapters have entry for path 0 */ 2278 if (tb_port_is_null(port) || tb_port_is_nhi(port)) { 2279 ret = path_show_one(port, s, 0); 2280 if (ret) 2281 goto out_unlock; 2282 } 2283 2284 start = tb_port_is_nhi(port) ? 1 : TB_PATH_MIN_HOPID; 2285 2286 for (i = start; i <= port->config.max_in_hop_id; i++) { 2287 ret = path_show_one(port, s, i); 2288 if (ret) 2289 break; 2290 } 2291 2292 out_unlock: 2293 mutex_unlock(&tb->lock); 2294 out_rpm_put: 2295 pm_runtime_mark_last_busy(&sw->dev); 2296 pm_runtime_put_autosuspend(&sw->dev); 2297 2298 return ret; 2299 } 2300 DEBUGFS_ATTR_RW(path); 2301 2302 static int counter_set_regs_show(struct tb_port *port, struct seq_file *s, 2303 int counter) 2304 { 2305 u32 data[COUNTER_SET_LEN]; 2306 int ret, i; 2307 2308 ret = tb_port_read(port, data, TB_CFG_COUNTERS, 2309 counter * COUNTER_SET_LEN, ARRAY_SIZE(data)); 2310 if (ret) { 2311 seq_printf(s, "0x%04x <not accessible>\n", 2312 counter * COUNTER_SET_LEN); 2313 return ret; 2314 } 2315 2316 for (i = 0; i < ARRAY_SIZE(data); i++) { 2317 seq_printf(s, "0x%04x %4d 0x%02x 0x%08x\n", 2318 counter * COUNTER_SET_LEN + i, i, counter, data[i]); 2319 } 2320 2321 return 0; 2322 } 2323 2324 static int counters_show(struct seq_file *s, void *not_used) 2325 { 2326 struct tb_port *port = s->private; 2327 struct tb_switch *sw = port->sw; 2328 struct tb *tb = sw->tb; 2329 int i, ret = 0; 2330 2331 pm_runtime_get_sync(&sw->dev); 2332 2333 if (mutex_lock_interruptible(&tb->lock)) { 2334 ret = -ERESTARTSYS; 2335 goto out; 2336 } 2337 2338 seq_puts(s, "# offset relative_offset counter_id value\n"); 2339 2340 for (i = 0; i < port->config.max_counters; i++) { 2341 ret = counter_set_regs_show(port, s, i); 2342 if (ret) 2343 break; 2344 } 2345 2346 mutex_unlock(&tb->lock); 2347 2348 out: 2349 pm_runtime_mark_last_busy(&sw->dev); 2350 pm_runtime_put_autosuspend(&sw->dev); 2351 2352 return ret; 2353 } 2354 DEBUGFS_ATTR_RW(counters); 2355 2356 static int sb_regs_show(struct tb_port *port, const struct sb_reg *sb_regs, 2357 size_t size, enum usb4_sb_target target, u8 index, 2358 struct seq_file *s) 2359 { 2360 int ret, i; 2361 2362 seq_puts(s, "# register value\n"); 2363 2364 for (i = 0; i < size; i++) { 2365 const struct sb_reg *regs = &sb_regs[i]; 2366 u8 data[64]; 2367 int j; 2368 2369 memset(data, 0, sizeof(data)); 2370 ret = usb4_port_sb_read(port, target, index, regs->reg, data, 2371 regs->size); 2372 if (ret) { 2373 seq_printf(s, "0x%02x <not accessible>\n", regs->reg); 2374 continue; 2375 } 2376 2377 seq_printf(s, "0x%02x", regs->reg); 2378 for (j = 0; j < regs->size; j++) 2379 seq_printf(s, " 0x%02x", data[j]); 2380 seq_puts(s, "\n"); 2381 } 2382 2383 return 0; 2384 } 2385 2386 static int port_sb_regs_show(struct seq_file *s, void *not_used) 2387 { 2388 struct tb_port *port = s->private; 2389 struct tb_switch *sw = port->sw; 2390 struct tb *tb = sw->tb; 2391 int ret; 2392 2393 pm_runtime_get_sync(&sw->dev); 2394 2395 if (mutex_lock_interruptible(&tb->lock)) { 2396 ret = -ERESTARTSYS; 2397 goto out_rpm_put; 2398 } 2399 2400 ret = sb_regs_show(port, port_sb_regs, ARRAY_SIZE(port_sb_regs), 2401 USB4_SB_TARGET_ROUTER, 0, s); 2402 2403 mutex_unlock(&tb->lock); 2404 out_rpm_put: 2405 pm_runtime_mark_last_busy(&sw->dev); 2406 pm_runtime_put_autosuspend(&sw->dev); 2407 2408 return ret; 2409 } 2410 DEBUGFS_ATTR_RW(port_sb_regs); 2411 2412 /** 2413 * tb_switch_debugfs_init() - Add debugfs entries for router 2414 * @sw: Pointer to the router 2415 * 2416 * Adds debugfs directories and files for given router. 2417 */ 2418 void tb_switch_debugfs_init(struct tb_switch *sw) 2419 { 2420 struct dentry *debugfs_dir; 2421 struct tb_port *port; 2422 2423 debugfs_dir = debugfs_create_dir(dev_name(&sw->dev), tb_debugfs_root); 2424 sw->debugfs_dir = debugfs_dir; 2425 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, sw, 2426 &switch_regs_fops); 2427 if (sw->drom) 2428 debugfs_create_blob("drom", 0400, debugfs_dir, &sw->drom_blob); 2429 2430 tb_switch_for_each_port(sw, port) { 2431 struct dentry *debugfs_dir; 2432 char dir_name[10]; 2433 2434 if (port->disabled) 2435 continue; 2436 if (port->config.type == TB_TYPE_INACTIVE) 2437 continue; 2438 2439 snprintf(dir_name, sizeof(dir_name), "port%d", port->port); 2440 debugfs_dir = debugfs_create_dir(dir_name, sw->debugfs_dir); 2441 debugfs_create_file("regs", DEBUGFS_MODE, debugfs_dir, 2442 port, &port_regs_fops); 2443 debugfs_create_file("path", 0400, debugfs_dir, port, 2444 &path_fops); 2445 if (port->config.counters_support) 2446 debugfs_create_file("counters", 0600, debugfs_dir, port, 2447 &counters_fops); 2448 if (port->usb4) 2449 debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, 2450 port, &port_sb_regs_fops); 2451 } 2452 2453 margining_switch_init(sw); 2454 } 2455 2456 /** 2457 * tb_switch_debugfs_remove() - Remove all router debugfs entries 2458 * @sw: Pointer to the router 2459 * 2460 * Removes all previously added debugfs entries under this router. 2461 */ 2462 void tb_switch_debugfs_remove(struct tb_switch *sw) 2463 { 2464 margining_switch_remove(sw); 2465 debugfs_remove_recursive(sw->debugfs_dir); 2466 } 2467 2468 void tb_xdomain_debugfs_init(struct tb_xdomain *xd) 2469 { 2470 margining_xdomain_init(xd); 2471 } 2472 2473 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd) 2474 { 2475 margining_xdomain_remove(xd); 2476 } 2477 2478 /** 2479 * tb_service_debugfs_init() - Add debugfs directory for service 2480 * @svc: Thunderbolt service pointer 2481 * 2482 * Adds debugfs directory for service. 2483 */ 2484 void tb_service_debugfs_init(struct tb_service *svc) 2485 { 2486 svc->debugfs_dir = debugfs_create_dir(dev_name(&svc->dev), 2487 tb_debugfs_root); 2488 } 2489 2490 /** 2491 * tb_service_debugfs_remove() - Remove service debugfs directory 2492 * @svc: Thunderbolt service pointer 2493 * 2494 * Removes the previously created debugfs directory for @svc. 2495 */ 2496 void tb_service_debugfs_remove(struct tb_service *svc) 2497 { 2498 debugfs_remove_recursive(svc->debugfs_dir); 2499 svc->debugfs_dir = NULL; 2500 } 2501 2502 static int retimer_sb_regs_show(struct seq_file *s, void *not_used) 2503 { 2504 struct tb_retimer *rt = s->private; 2505 struct tb *tb = rt->tb; 2506 int ret; 2507 2508 pm_runtime_get_sync(&rt->dev); 2509 2510 if (mutex_lock_interruptible(&tb->lock)) { 2511 ret = -ERESTARTSYS; 2512 goto out_rpm_put; 2513 } 2514 2515 ret = sb_regs_show(rt->port, retimer_sb_regs, ARRAY_SIZE(retimer_sb_regs), 2516 USB4_SB_TARGET_RETIMER, rt->index, s); 2517 2518 mutex_unlock(&tb->lock); 2519 out_rpm_put: 2520 pm_runtime_mark_last_busy(&rt->dev); 2521 pm_runtime_put_autosuspend(&rt->dev); 2522 2523 return ret; 2524 } 2525 DEBUGFS_ATTR_RW(retimer_sb_regs); 2526 2527 /** 2528 * tb_retimer_debugfs_init() - Add debugfs directory for retimer 2529 * @rt: Pointer to retimer structure 2530 * 2531 * Adds and populates retimer debugfs directory. 2532 */ 2533 void tb_retimer_debugfs_init(struct tb_retimer *rt) 2534 { 2535 struct dentry *debugfs_dir; 2536 2537 debugfs_dir = debugfs_create_dir(dev_name(&rt->dev), tb_debugfs_root); 2538 debugfs_create_file("sb_regs", DEBUGFS_MODE, debugfs_dir, rt, 2539 &retimer_sb_regs_fops); 2540 margining_retimer_init(rt, debugfs_dir); 2541 } 2542 2543 /** 2544 * tb_retimer_debugfs_remove() - Remove retimer debugfs directory 2545 * @rt: Pointer to retimer structure 2546 * 2547 * Removes the retimer debugfs directory along with its contents. 2548 */ 2549 void tb_retimer_debugfs_remove(struct tb_retimer *rt) 2550 { 2551 debugfs_lookup_and_remove(dev_name(&rt->dev), tb_debugfs_root); 2552 margining_retimer_remove(rt); 2553 } 2554 2555 void tb_debugfs_init(void) 2556 { 2557 tb_debugfs_root = debugfs_create_dir("thunderbolt", NULL); 2558 } 2559 2560 void tb_debugfs_exit(void) 2561 { 2562 debugfs_remove_recursive(tb_debugfs_root); 2563 } 2564