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