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