1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Thunderbolt driver - switch/port utility functions 4 * 5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 6 * Copyright (C) 2018, Intel Corporation 7 */ 8 9 #include <linux/delay.h> 10 #include <linux/hex.h> 11 #include <linux/idr.h> 12 #include <linux/module.h> 13 #include <linux/nvmem-provider.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/sched/signal.h> 16 #include <linux/sizes.h> 17 #include <linux/slab.h> 18 #include <linux/string_helpers.h> 19 20 #include "tb.h" 21 22 /* How long a hop is given to drain when a path is deactivated */ 23 #define TB_PORT_PENDING_TIMEOUT 500 /* ms */ 24 25 /* Switch NVM support */ 26 27 struct nvm_auth_status { 28 struct list_head list; 29 uuid_t uuid; 30 u32 status; 31 }; 32 33 /* 34 * Hold NVM authentication failure status per switch This information 35 * needs to stay around even when the switch gets power cycled so we 36 * keep it separately. 37 */ 38 static LIST_HEAD(nvm_auth_status_cache); 39 static DEFINE_MUTEX(nvm_auth_status_lock); 40 41 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) 42 { 43 struct nvm_auth_status *st; 44 45 list_for_each_entry(st, &nvm_auth_status_cache, list) { 46 if (uuid_equal(&st->uuid, sw->uuid)) 47 return st; 48 } 49 50 return NULL; 51 } 52 53 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) 54 { 55 struct nvm_auth_status *st; 56 57 mutex_lock(&nvm_auth_status_lock); 58 st = __nvm_get_auth_status(sw); 59 mutex_unlock(&nvm_auth_status_lock); 60 61 *status = st ? st->status : 0; 62 } 63 64 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) 65 { 66 struct nvm_auth_status *st; 67 68 if (WARN_ON(!sw->uuid)) 69 return; 70 71 mutex_lock(&nvm_auth_status_lock); 72 st = __nvm_get_auth_status(sw); 73 74 if (!st) { 75 st = kzalloc_obj(*st); 76 if (!st) 77 goto unlock; 78 79 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); 80 INIT_LIST_HEAD(&st->list); 81 list_add_tail(&st->list, &nvm_auth_status_cache); 82 } 83 84 st->status = status; 85 unlock: 86 mutex_unlock(&nvm_auth_status_lock); 87 } 88 89 static void nvm_clear_auth_status(const struct tb_switch *sw) 90 { 91 struct nvm_auth_status *st; 92 93 mutex_lock(&nvm_auth_status_lock); 94 st = __nvm_get_auth_status(sw); 95 if (st) { 96 list_del(&st->list); 97 kfree(st); 98 } 99 mutex_unlock(&nvm_auth_status_lock); 100 } 101 102 static int nvm_validate_and_write(struct tb_switch *sw) 103 { 104 unsigned int image_size; 105 const u8 *buf; 106 int ret; 107 108 ret = tb_nvm_validate(sw->nvm); 109 if (ret) 110 return ret; 111 112 ret = tb_nvm_write_headers(sw->nvm); 113 if (ret) 114 return ret; 115 116 buf = sw->nvm->buf_data_start; 117 image_size = sw->nvm->buf_data_size; 118 119 if (tb_switch_is_usb4(sw)) 120 ret = usb4_switch_nvm_write(sw, 0, buf, image_size); 121 else 122 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size); 123 if (ret) 124 return ret; 125 126 sw->nvm->flushed = true; 127 return 0; 128 } 129 130 static int nvm_authenticate_host_dma_port(struct tb_switch *sw) 131 { 132 int ret = 0; 133 134 /* 135 * Root switch NVM upgrade requires that we disconnect the 136 * existing paths first (in case it is not in safe mode 137 * already). 138 */ 139 if (!sw->safe_mode) { 140 u32 status; 141 142 ret = tb_domain_disconnect_all_paths(sw->tb); 143 if (ret) 144 return ret; 145 /* 146 * The host controller goes away pretty soon after this if 147 * everything goes well so getting timeout is expected. 148 */ 149 ret = dma_port_flash_update_auth(sw->dma_port); 150 if (!ret || ret == -ETIMEDOUT) 151 return 0; 152 153 /* 154 * Any error from update auth operation requires power 155 * cycling of the host router. 156 */ 157 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); 158 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) 159 nvm_set_auth_status(sw, status); 160 } 161 162 /* 163 * From safe mode we can get out by just power cycling the 164 * switch. 165 */ 166 dma_port_power_cycle(sw->dma_port); 167 return ret; 168 } 169 170 static int nvm_authenticate_device_dma_port(struct tb_switch *sw) 171 { 172 int ret, retries = 10; 173 174 ret = dma_port_flash_update_auth(sw->dma_port); 175 switch (ret) { 176 case 0: 177 case -ETIMEDOUT: 178 case -EACCES: 179 case -EINVAL: 180 /* Power cycle is required */ 181 break; 182 default: 183 return ret; 184 } 185 186 /* 187 * Poll here for the authentication status. It takes some time 188 * for the device to respond (we get timeout for a while). Once 189 * we get response the device needs to be power cycled in order 190 * to the new NVM to be taken into use. 191 */ 192 do { 193 u32 status; 194 195 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); 196 if (ret < 0 && ret != -ETIMEDOUT) 197 return ret; 198 if (ret > 0) { 199 if (status) { 200 tb_sw_warn(sw, "failed to authenticate NVM\n"); 201 nvm_set_auth_status(sw, status); 202 } 203 204 tb_sw_info(sw, "power cycling the switch now\n"); 205 dma_port_power_cycle(sw->dma_port); 206 return 0; 207 } 208 209 msleep(500); 210 } while (--retries); 211 212 return -ETIMEDOUT; 213 } 214 215 static inline bool nvm_readable(struct tb_switch *sw) 216 { 217 if (tb_switch_is_usb4(sw)) { 218 /* 219 * USB4 devices must support NVM operations but it is 220 * optional for hosts. Therefore we query the NVM sector 221 * size here and if it is supported assume NVM 222 * operations are implemented. 223 */ 224 return usb4_switch_nvm_sector_size(sw) > 0; 225 } 226 227 /* Thunderbolt 2 and 3 devices support NVM through DMA port */ 228 return !!sw->dma_port; 229 } 230 231 static inline bool nvm_upgradeable(struct tb_switch *sw) 232 { 233 if (sw->no_nvm_upgrade) 234 return false; 235 return nvm_readable(sw); 236 } 237 238 static int nvm_authenticate(struct tb_switch *sw, bool auth_only) 239 { 240 struct tb_nhi *nhi = sw->tb->nhi; 241 int ret; 242 243 if (tb_switch_is_usb4(sw)) { 244 if (auth_only) { 245 ret = usb4_switch_nvm_set_offset(sw, 0); 246 if (ret) 247 return ret; 248 } 249 sw->nvm->authenticating = true; 250 return usb4_switch_nvm_authenticate(sw); 251 } 252 if (auth_only) 253 return -EOPNOTSUPP; 254 255 sw->nvm->authenticating = true; 256 if (!tb_route(sw)) { 257 if (nhi->ops->pre_nvm_auth) 258 nhi->ops->pre_nvm_auth(nhi); 259 ret = nvm_authenticate_host_dma_port(sw); 260 } else { 261 ret = nvm_authenticate_device_dma_port(sw); 262 } 263 264 return ret; 265 } 266 267 /** 268 * tb_switch_nvm_read() - Read router NVM 269 * @sw: Router whose NVM to read 270 * @address: Start address on the NVM 271 * @buf: Buffer where the read data is copied 272 * @size: Size of the buffer in bytes 273 * 274 * Reads from router NVM and returns the requested data in @buf. Locking 275 * is up to the caller. 276 * 277 * Return: %0 on success, negative errno otherwise. 278 */ 279 int tb_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf, 280 size_t size) 281 { 282 if (tb_switch_is_usb4(sw)) 283 return usb4_switch_nvm_read(sw, address, buf, size); 284 return dma_port_flash_read(sw->dma_port, address, buf, size); 285 } 286 287 static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes) 288 { 289 struct tb_nvm *nvm = priv; 290 struct tb_switch *sw = tb_to_switch(nvm->dev); 291 int ret; 292 293 pm_runtime_get_sync(&sw->dev); 294 295 if (!mutex_trylock(&sw->tb->lock)) { 296 ret = restart_syscall(); 297 goto out; 298 } 299 300 ret = tb_switch_nvm_read(sw, offset, val, bytes); 301 mutex_unlock(&sw->tb->lock); 302 303 out: 304 pm_runtime_mark_last_busy(&sw->dev); 305 pm_runtime_put_autosuspend(&sw->dev); 306 307 return ret; 308 } 309 310 static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes) 311 { 312 struct tb_nvm *nvm = priv; 313 struct tb_switch *sw = tb_to_switch(nvm->dev); 314 int ret; 315 316 if (!mutex_trylock(&sw->tb->lock)) 317 return restart_syscall(); 318 319 /* 320 * Since writing the NVM image might require some special steps, 321 * for example when CSS headers are written, we cache the image 322 * locally here and handle the special cases when the user asks 323 * us to authenticate the image. 324 */ 325 ret = tb_nvm_write_buf(nvm, offset, val, bytes); 326 mutex_unlock(&sw->tb->lock); 327 328 return ret; 329 } 330 331 static int tb_switch_nvm_init(struct tb_switch *sw) 332 { 333 struct tb_nvm *nvm; 334 int ret; 335 336 if (!nvm_readable(sw)) 337 return 0; 338 339 nvm = tb_nvm_alloc(&sw->dev); 340 if (IS_ERR(nvm)) { 341 ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm); 342 goto err_nvm; 343 } 344 345 ret = tb_nvm_read_version(nvm); 346 if (ret) 347 goto err_nvm; 348 349 sw->nvm = nvm; 350 return 0; 351 352 err_nvm: 353 tb_sw_dbg(sw, "NVM upgrade disabled\n"); 354 sw->no_nvm_upgrade = true; 355 if (!IS_ERR(nvm)) 356 tb_nvm_free(nvm); 357 358 return ret; 359 } 360 361 static int tb_switch_nvm_add(struct tb_switch *sw) 362 { 363 struct tb_nvm *nvm = sw->nvm; 364 int ret; 365 366 if (!nvm) 367 return 0; 368 369 /* 370 * If the switch is in safe-mode the only accessible portion of 371 * the NVM is the non-active one where userspace is expected to 372 * write new functional NVM. 373 */ 374 if (!sw->safe_mode) { 375 ret = tb_nvm_add_active(nvm, nvm_read); 376 if (ret) 377 goto err_nvm; 378 tb_sw_dbg(sw, "NVM version %x.%x\n", nvm->major, nvm->minor); 379 } 380 381 if (!sw->no_nvm_upgrade) { 382 ret = tb_nvm_add_non_active(nvm, nvm_write); 383 if (ret) 384 goto err_nvm; 385 } 386 387 return 0; 388 389 err_nvm: 390 tb_sw_dbg(sw, "NVM upgrade disabled\n"); 391 sw->no_nvm_upgrade = true; 392 tb_nvm_free(nvm); 393 394 return ret; 395 } 396 397 static void tb_switch_nvm_remove(struct tb_switch *sw) 398 { 399 struct tb_nvm *nvm; 400 401 nvm = sw->nvm; 402 sw->nvm = NULL; 403 404 if (!nvm) 405 return; 406 407 /* Remove authentication status in case the switch is unplugged */ 408 if (!nvm->authenticating) 409 nvm_clear_auth_status(sw); 410 411 tb_nvm_free(nvm); 412 } 413 414 /* port utility functions */ 415 416 static const char *tb_port_type(const struct tb_regs_port_header *port) 417 { 418 switch (port->type >> 16) { 419 case 0: 420 switch ((u8) port->type) { 421 case 0: 422 return "Inactive"; 423 case 1: 424 return "Port"; 425 case 2: 426 return "NHI"; 427 default: 428 return "unknown"; 429 } 430 case 0x2: 431 return "Ethernet"; 432 case 0x8: 433 return "SATA"; 434 case 0xe: 435 return "DP/HDMI"; 436 case 0x10: 437 return "PCIe"; 438 case 0x20: 439 return "USB"; 440 default: 441 return "unknown"; 442 } 443 } 444 445 static void tb_dump_port(struct tb *tb, const struct tb_port *port) 446 { 447 const struct tb_regs_port_header *regs = &port->config; 448 449 tb_dbg(tb, 450 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n", 451 regs->port_number, regs->vendor_id, regs->device_id, 452 regs->revision, regs->thunderbolt_version, tb_port_type(regs), 453 regs->type); 454 tb_dbg(tb, " Max hop id (in/out): %d/%d\n", 455 regs->max_in_hop_id, regs->max_out_hop_id); 456 tb_dbg(tb, " Max counters: %d\n", regs->max_counters); 457 tb_dbg(tb, " NFC Credits: %#x\n", regs->nfc_credits); 458 tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits, 459 port->ctl_credits); 460 } 461 462 /** 463 * tb_port_state() - get connectedness state of a port 464 * @port: the port to check 465 * 466 * The port must have a TB_CAP_PHY (i.e. it should be a real port). 467 * 468 * Return: &enum tb_port_state or negative error code on failure. 469 */ 470 int tb_port_state(struct tb_port *port) 471 { 472 struct tb_cap_phy phy; 473 int res; 474 if (port->cap_phy == 0) { 475 tb_port_WARN(port, "does not have a PHY\n"); 476 return -EINVAL; 477 } 478 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); 479 if (res) 480 return res; 481 return phy.state; 482 } 483 484 /** 485 * tb_wait_for_port() - wait for a port to become ready 486 * @port: Port to wait 487 * @wait_if_unplugged: Wait also when port is unplugged 488 * 489 * Wait up to 1 second for a port to reach state TB_PORT_UP. If 490 * wait_if_unplugged is set then we also wait if the port is in state 491 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after 492 * switch resume). Otherwise we only wait if a device is registered but the link 493 * has not yet been established. 494 * 495 * Return: 496 * * %0 - If the port is not connected or failed to reach 497 * state %TB_PORT_UP within one second. 498 * * %1 - If the port is connected and in state %TB_PORT_UP. 499 * * Negative errno - An error occurred. 500 */ 501 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged) 502 { 503 int retries = 10; 504 int state; 505 if (!port->cap_phy) { 506 tb_port_WARN(port, "does not have PHY\n"); 507 return -EINVAL; 508 } 509 if (tb_is_upstream_port(port)) { 510 tb_port_WARN(port, "is the upstream port\n"); 511 return -EINVAL; 512 } 513 514 while (retries--) { 515 state = tb_port_state(port); 516 switch (state) { 517 case TB_PORT_DISABLED: 518 tb_port_dbg(port, "is disabled (state: 0)\n"); 519 return 0; 520 521 case TB_PORT_UNPLUGGED: 522 if (wait_if_unplugged) { 523 /* used during resume */ 524 tb_port_dbg(port, 525 "is unplugged (state: 7), retrying...\n"); 526 msleep(100); 527 break; 528 } 529 tb_port_dbg(port, "is unplugged (state: 7)\n"); 530 return 0; 531 532 case TB_PORT_UP: 533 case TB_PORT_TX_CL0S: 534 case TB_PORT_RX_CL0S: 535 case TB_PORT_CL1: 536 case TB_PORT_CL2: 537 tb_port_dbg(port, "is connected, link is up (state: %d)\n", state); 538 return 1; 539 540 default: 541 if (state < 0) 542 return state; 543 544 /* 545 * After plug-in the state is TB_PORT_CONNECTING. Give it some 546 * time. 547 */ 548 tb_port_dbg(port, 549 "is connected, link is not up (state: %d), retrying...\n", 550 state); 551 msleep(100); 552 } 553 554 } 555 tb_port_warn(port, 556 "failed to reach state TB_PORT_UP. Ignoring port...\n"); 557 return 0; 558 } 559 560 /** 561 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port 562 * @port: Port to add/remove NFC credits 563 * @credits: Credits to add/remove 564 * 565 * Change the number of NFC credits allocated to @port by @credits. To remove 566 * NFC credits pass a negative amount of credits. 567 * 568 * Return: %0 on success, negative errno otherwise. 569 */ 570 int tb_port_add_nfc_credits(struct tb_port *port, int credits) 571 { 572 u32 nfc_credits; 573 574 if (credits == 0 || port->sw->is_unplugged) 575 return 0; 576 577 /* 578 * USB4 restricts programming NFC buffers to lane adapters only 579 * so skip other ports. 580 */ 581 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port)) 582 return 0; 583 584 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK; 585 if (credits < 0) 586 credits = max_t(int, -nfc_credits, credits); 587 588 nfc_credits += credits; 589 590 tb_port_dbg(port, "adding %d NFC credits to %lu", credits, 591 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK); 592 593 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK; 594 port->config.nfc_credits |= nfc_credits; 595 596 return tb_port_write(port, &port->config.nfc_credits, 597 TB_CFG_PORT, ADP_CS_4, 1); 598 } 599 600 /** 601 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER 602 * @port: Port whose counters to clear 603 * @counter: Counter index to clear 604 * 605 * Return: %0 on success, negative errno otherwise. 606 */ 607 int tb_port_clear_counter(struct tb_port *port, int counter) 608 { 609 u32 zero[3] = { 0, 0, 0 }; 610 tb_port_dbg(port, "clearing counter %d\n", counter); 611 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3); 612 } 613 614 /** 615 * tb_port_unlock() - Unlock downstream port 616 * @port: Port to unlock 617 * 618 * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the 619 * downstream router accessible for CM. 620 * 621 * Return: %0 on success, negative errno otherwise. 622 */ 623 int tb_port_unlock(struct tb_port *port) 624 { 625 if (tb_switch_is_icm(port->sw)) 626 return 0; 627 if (!tb_port_is_null(port)) 628 return -EINVAL; 629 if (tb_switch_is_usb4(port->sw)) 630 return usb4_port_unlock(port); 631 return 0; 632 } 633 634 static int __tb_port_enable(struct tb_port *port, bool enable) 635 { 636 int ret; 637 u32 phy; 638 639 if (!tb_port_is_null(port)) 640 return -EINVAL; 641 642 ret = tb_port_read(port, &phy, TB_CFG_PORT, 643 port->cap_phy + LANE_ADP_CS_1, 1); 644 if (ret) 645 return ret; 646 647 if (enable) 648 phy &= ~LANE_ADP_CS_1_LD; 649 else 650 phy |= LANE_ADP_CS_1_LD; 651 652 653 ret = tb_port_write(port, &phy, TB_CFG_PORT, 654 port->cap_phy + LANE_ADP_CS_1, 1); 655 if (ret) 656 return ret; 657 658 tb_port_dbg(port, "lane %s\n", str_enabled_disabled(enable)); 659 return 0; 660 } 661 662 /** 663 * tb_port_enable() - Enable lane adapter 664 * @port: Port to enable (can be %NULL) 665 * 666 * This is used for lane 0 and 1 adapters to enable it. 667 * 668 * Return: %0 on success, negative errno otherwise. 669 */ 670 int tb_port_enable(struct tb_port *port) 671 { 672 return __tb_port_enable(port, true); 673 } 674 675 /** 676 * tb_port_disable() - Disable lane adapter 677 * @port: Port to disable (can be %NULL) 678 * 679 * This is used for lane 0 and 1 adapters to disable it. 680 * 681 * Return: %0 on success, negative errno otherwise. 682 */ 683 int tb_port_disable(struct tb_port *port) 684 { 685 return __tb_port_enable(port, false); 686 } 687 688 /** 689 * tb_port_reset() - Reset the port 690 * @port: Port to reset 691 * 692 * Resets @port. For USB4 ports this issues a USB4 port reset and for 693 * legacy ports the link controller port is reset. 694 * 695 * Return: %0 on success, negative errno otherwise. 696 */ 697 int tb_port_reset(struct tb_port *port) 698 { 699 if (tb_switch_is_usb4(port->sw)) 700 return port->cap_usb4 ? usb4_port_reset(port) : 0; 701 return tb_lc_reset_port(port); 702 } 703 704 /* 705 * tb_init_port() - initialize a port 706 * 707 * This is a helper method for tb_switch_alloc. Does not check or initialize 708 * any downstream switches. 709 * 710 * Return: %0 on success, negative errno otherwise. 711 */ 712 static int tb_init_port(struct tb_port *port) 713 { 714 int res; 715 int cap; 716 717 INIT_LIST_HEAD(&port->list); 718 port->pp_timeout_msec = TB_PORT_PENDING_TIMEOUT; 719 720 /* Control adapter does not have configuration space */ 721 if (!port->port) 722 return 0; 723 724 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); 725 if (res) { 726 if (res == -ENODEV) { 727 tb_dbg(port->sw->tb, " Port %d: not implemented\n", 728 port->port); 729 port->disabled = true; 730 return 0; 731 } 732 return res; 733 } 734 735 /* Port 0 is the switch itself and has no PHY. */ 736 if (port->config.type == TB_TYPE_PORT) { 737 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY); 738 739 if (cap > 0) 740 port->cap_phy = cap; 741 else 742 tb_port_WARN(port, "non switch port without a PHY\n"); 743 744 cap = tb_port_find_cap(port, TB_PORT_CAP_USB4); 745 if (cap > 0) 746 port->cap_usb4 = cap; 747 748 /* 749 * USB4 port buffers allocated for the control path 750 * can be read from the path config space. Legacy 751 * devices use hard-coded value. 752 */ 753 if (port->cap_usb4) { 754 struct tb_regs_hop hop; 755 756 if (!tb_port_read(port, &hop, TB_CFG_HOPS, 0, 2)) 757 port->ctl_credits = hop.initial_credits; 758 } 759 if (!port->ctl_credits) 760 port->ctl_credits = 2; 761 762 } else { 763 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP); 764 if (cap > 0) 765 port->cap_adap = cap; 766 } 767 768 port->total_credits = 769 (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >> 770 ADP_CS_4_TOTAL_BUFFERS_SHIFT; 771 772 tb_dump_port(port->sw->tb, port); 773 return 0; 774 } 775 776 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid, 777 int max_hopid) 778 { 779 int port_max_hopid; 780 struct ida *ida; 781 int ret; 782 783 if (in) { 784 port_max_hopid = port->config.max_in_hop_id; 785 ida = &port->in_hopids; 786 } else { 787 port_max_hopid = port->config.max_out_hop_id; 788 ida = &port->out_hopids; 789 } 790 791 /* 792 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are 793 * reserved. 794 */ 795 if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID) 796 min_hopid = TB_PATH_MIN_HOPID; 797 798 if (max_hopid < 0 || max_hopid > port_max_hopid) 799 max_hopid = port_max_hopid; 800 801 ret = ida_alloc_range(ida, min_hopid, max_hopid, GFP_KERNEL); 802 if (ret >= 0) 803 tb_switch_get(port->sw); 804 805 return ret; 806 } 807 808 /** 809 * tb_port_alloc_in_hopid() - Allocate input HopID from port 810 * @port: Port to allocate HopID for 811 * @min_hopid: Minimum acceptable input HopID 812 * @max_hopid: Maximum acceptable input HopID 813 * 814 * Return: HopID between @min_hopid and @max_hopid or negative errno in 815 * case of error. 816 */ 817 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid) 818 { 819 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid); 820 } 821 822 /** 823 * tb_port_alloc_out_hopid() - Allocate output HopID from port 824 * @port: Port to allocate HopID for 825 * @min_hopid: Minimum acceptable output HopID 826 * @max_hopid: Maximum acceptable output HopID 827 * 828 * Return: HopID between @min_hopid and @max_hopid or negative errno in 829 * case of error. 830 */ 831 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid) 832 { 833 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid); 834 } 835 836 /** 837 * tb_port_release_in_hopid() - Release allocated input HopID from port 838 * @port: Port whose HopID to release 839 * @hopid: HopID to release 840 */ 841 void tb_port_release_in_hopid(struct tb_port *port, int hopid) 842 { 843 ida_free(&port->in_hopids, hopid); 844 tb_switch_put(port->sw); 845 } 846 847 /** 848 * tb_port_release_out_hopid() - Release allocated output HopID from port 849 * @port: Port whose HopID to release 850 * @hopid: HopID to release 851 */ 852 void tb_port_release_out_hopid(struct tb_port *port, int hopid) 853 { 854 ida_free(&port->out_hopids, hopid); 855 tb_switch_put(port->sw); 856 } 857 858 static inline bool tb_switch_is_reachable(const struct tb_switch *parent, 859 const struct tb_switch *sw) 860 { 861 u64 mask = (1ULL << parent->config.depth * 8) - 1; 862 return (tb_route(parent) & mask) == (tb_route(sw) & mask); 863 } 864 865 /** 866 * tb_next_port_on_path() - Return next port for given port on a path 867 * @start: Start port of the walk 868 * @end: End port of the walk 869 * @prev: Previous port (%NULL if this is the first) 870 * 871 * This function can be used to walk from one port to another if they 872 * are connected through zero or more switches. If the @prev is dual 873 * link port, the function follows that link and returns another end on 874 * that same link. 875 * 876 * Domain tb->lock must be held when this function is called. 877 * 878 * Return: Pointer to &struct tb_port, %NULL if the @end port has been reached. 879 */ 880 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end, 881 struct tb_port *prev) 882 { 883 struct tb_port *next; 884 885 if (!prev) 886 return start; 887 888 if (prev->sw == end->sw) { 889 if (prev == end) 890 return NULL; 891 return end; 892 } 893 894 if (tb_switch_is_reachable(prev->sw, end->sw)) { 895 next = tb_port_at(tb_route(end->sw), prev->sw); 896 /* Walk down the topology if next == prev */ 897 if (prev->remote && 898 (next == prev || next->dual_link_port == prev)) 899 next = prev->remote; 900 } else { 901 if (tb_is_upstream_port(prev)) { 902 next = prev->remote; 903 } else { 904 next = tb_upstream_port(prev->sw); 905 /* 906 * Keep the same link if prev and next are both 907 * dual link ports. 908 */ 909 if (next->dual_link_port && 910 next->link_nr != prev->link_nr) { 911 next = next->dual_link_port; 912 } 913 } 914 } 915 916 return next != prev ? next : NULL; 917 } 918 919 /** 920 * tb_port_get_link_speed() - Get current link speed 921 * @port: Port to check (USB4 or CIO) 922 * 923 * Return: Link speed in Gb/s or negative errno in case of failure. 924 */ 925 int tb_port_get_link_speed(struct tb_port *port) 926 { 927 u32 val, speed; 928 int ret; 929 930 if (!port->cap_phy) 931 return -EINVAL; 932 933 ret = tb_port_read(port, &val, TB_CFG_PORT, 934 port->cap_phy + LANE_ADP_CS_1, 1); 935 if (ret) 936 return ret; 937 938 speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >> 939 LANE_ADP_CS_1_CURRENT_SPEED_SHIFT; 940 941 switch (speed) { 942 case LANE_ADP_CS_1_CURRENT_SPEED_GEN4: 943 return 40; 944 case LANE_ADP_CS_1_CURRENT_SPEED_GEN3: 945 return 20; 946 default: 947 return 10; 948 } 949 } 950 951 /** 952 * tb_port_get_link_generation() - Returns link generation 953 * @port: Lane adapter 954 * 955 * Return: Link generation as a number or negative errno in case of 956 * failure. 957 * 958 * Does not distinguish between Thunderbolt 1 and Thunderbolt 2 959 * links so for those always returns %2. 960 */ 961 int tb_port_get_link_generation(struct tb_port *port) 962 { 963 int ret; 964 965 ret = tb_port_get_link_speed(port); 966 if (ret < 0) 967 return ret; 968 969 switch (ret) { 970 case 40: 971 return 4; 972 case 20: 973 return 3; 974 default: 975 return 2; 976 } 977 } 978 979 /** 980 * tb_port_get_link_width() - Get current link width 981 * @port: Port to check (USB4 or CIO) 982 * 983 * Return: Link width encoded in &enum tb_link_width or 984 * negative errno in case of failure. 985 */ 986 int tb_port_get_link_width(struct tb_port *port) 987 { 988 u32 val; 989 int ret; 990 991 if (!port->cap_phy) 992 return -EINVAL; 993 994 ret = tb_port_read(port, &val, TB_CFG_PORT, 995 port->cap_phy + LANE_ADP_CS_1, 1); 996 if (ret) 997 return ret; 998 999 /* Matches the values in enum tb_link_width */ 1000 return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >> 1001 LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT; 1002 } 1003 1004 /** 1005 * tb_port_width_supported() - Is the given link width supported 1006 * @port: Port to check 1007 * @width: Widths to check (bitmask) 1008 * 1009 * Can be called to any lane adapter. Checks if given @width is 1010 * supported by the hardware. 1011 * 1012 * Return: %true if link width is supported, %false otherwise. 1013 */ 1014 bool tb_port_width_supported(struct tb_port *port, unsigned int width) 1015 { 1016 u32 phy, widths; 1017 int ret; 1018 1019 if (!port->cap_phy) 1020 return false; 1021 1022 if (width & (TB_LINK_WIDTH_ASYM_TX | TB_LINK_WIDTH_ASYM_RX)) { 1023 if (tb_port_get_link_generation(port) < 4 || 1024 !usb4_port_asym_supported(port)) 1025 return false; 1026 } 1027 1028 ret = tb_port_read(port, &phy, TB_CFG_PORT, 1029 port->cap_phy + LANE_ADP_CS_0, 1); 1030 if (ret) 1031 return false; 1032 1033 /* 1034 * The field encoding is the same as &enum tb_link_width (which is 1035 * passed to @width). 1036 */ 1037 widths = FIELD_GET(LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK, phy); 1038 return widths & width; 1039 } 1040 1041 /** 1042 * tb_port_set_link_width() - Set target link width of the lane adapter 1043 * @port: Lane adapter 1044 * @width: Target link width 1045 * 1046 * Sets the target link width of the lane adapter to @width. Does not 1047 * enable/disable lane bonding. For that call tb_port_set_lane_bonding(). 1048 * 1049 * Return: %0 on success, negative errno otherwise. 1050 */ 1051 int tb_port_set_link_width(struct tb_port *port, enum tb_link_width width) 1052 { 1053 u32 val; 1054 int ret; 1055 1056 if (!port->cap_phy) 1057 return -EINVAL; 1058 1059 ret = tb_port_read(port, &val, TB_CFG_PORT, 1060 port->cap_phy + LANE_ADP_CS_1, 1); 1061 if (ret) 1062 return ret; 1063 1064 val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK; 1065 switch (width) { 1066 case TB_LINK_WIDTH_SINGLE: 1067 /* Gen 4 link cannot be single */ 1068 if (tb_port_get_link_generation(port) >= 4) 1069 return -EOPNOTSUPP; 1070 val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE << 1071 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT; 1072 break; 1073 1074 case TB_LINK_WIDTH_DUAL: 1075 if (tb_port_get_link_generation(port) >= 4) 1076 return usb4_port_asym_set_link_width(port, width); 1077 val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL << 1078 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT; 1079 break; 1080 1081 case TB_LINK_WIDTH_ASYM_TX: 1082 case TB_LINK_WIDTH_ASYM_RX: 1083 return usb4_port_asym_set_link_width(port, width); 1084 1085 default: 1086 return -EINVAL; 1087 } 1088 1089 return tb_port_write(port, &val, TB_CFG_PORT, 1090 port->cap_phy + LANE_ADP_CS_1, 1); 1091 } 1092 1093 /** 1094 * tb_port_set_lane_bonding() - Enable/disable lane bonding 1095 * @port: Lane adapter 1096 * @bonding: enable/disable bonding 1097 * 1098 * Enables or disables lane bonding. This should be called after target 1099 * link width has been set (tb_port_set_link_width()). Note in most 1100 * cases one should use tb_port_lane_bonding_enable() instead to enable 1101 * lane bonding. 1102 * 1103 * Return: %0 on success, negative errno otherwise. 1104 */ 1105 static int tb_port_set_lane_bonding(struct tb_port *port, bool bonding) 1106 { 1107 u32 val; 1108 int ret; 1109 1110 if (!port->cap_phy) 1111 return -EINVAL; 1112 1113 ret = tb_port_read(port, &val, TB_CFG_PORT, 1114 port->cap_phy + LANE_ADP_CS_1, 1); 1115 if (ret) 1116 return ret; 1117 1118 if (bonding) 1119 val |= LANE_ADP_CS_1_LB; 1120 else 1121 val &= ~LANE_ADP_CS_1_LB; 1122 1123 return tb_port_write(port, &val, TB_CFG_PORT, 1124 port->cap_phy + LANE_ADP_CS_1, 1); 1125 } 1126 1127 /** 1128 * tb_port_lane_bonding_enable() - Enable bonding on port 1129 * @port: port to enable 1130 * 1131 * Enable bonding by setting the link width of the port and the other 1132 * port in case of dual link port. Does not wait for the link to 1133 * actually reach the bonded state so caller needs to call 1134 * tb_port_wait_for_link_width() before enabling any paths through the 1135 * link to make sure the link is in expected state. 1136 * 1137 * Return: %0 on success, negative errno otherwise. 1138 */ 1139 int tb_port_lane_bonding_enable(struct tb_port *port) 1140 { 1141 enum tb_link_width width; 1142 int ret; 1143 1144 /* 1145 * Enable lane bonding for both links if not already enabled by 1146 * for example the boot firmware. 1147 */ 1148 width = tb_port_get_link_width(port); 1149 if (width == TB_LINK_WIDTH_SINGLE) { 1150 ret = tb_port_set_link_width(port, TB_LINK_WIDTH_DUAL); 1151 if (ret) 1152 goto err_lane0; 1153 } 1154 1155 width = tb_port_get_link_width(port->dual_link_port); 1156 if (width == TB_LINK_WIDTH_SINGLE) { 1157 ret = tb_port_set_link_width(port->dual_link_port, 1158 TB_LINK_WIDTH_DUAL); 1159 if (ret) 1160 goto err_lane1; 1161 } 1162 1163 /* 1164 * Only set bonding if the link was not already bonded. This 1165 * avoids the lane adapter to re-enter bonding state. 1166 */ 1167 if (width == TB_LINK_WIDTH_SINGLE && !tb_is_upstream_port(port)) { 1168 ret = tb_port_set_lane_bonding(port, true); 1169 if (ret) 1170 goto err_lane1; 1171 } 1172 1173 /* 1174 * When lane 0 bonding is set it will affect lane 1 too so 1175 * update both. 1176 */ 1177 port->bonded = true; 1178 port->dual_link_port->bonded = true; 1179 1180 return 0; 1181 1182 err_lane1: 1183 tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE); 1184 err_lane0: 1185 tb_port_set_link_width(port, TB_LINK_WIDTH_SINGLE); 1186 1187 return ret; 1188 } 1189 1190 /** 1191 * tb_port_lane_bonding_disable() - Disable bonding on port 1192 * @port: port to disable 1193 * 1194 * Disable bonding by setting the link width of the port and the 1195 * other port in case of dual link port. 1196 */ 1197 void tb_port_lane_bonding_disable(struct tb_port *port) 1198 { 1199 tb_port_set_lane_bonding(port, false); 1200 tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE); 1201 tb_port_set_link_width(port, TB_LINK_WIDTH_SINGLE); 1202 port->dual_link_port->bonded = false; 1203 port->bonded = false; 1204 } 1205 1206 /** 1207 * tb_port_wait_for_link_width() - Wait until link reaches specific width 1208 * @port: Port to wait for 1209 * @width: Expected link width (bitmask) 1210 * @timeout_msec: Timeout in ms how long to wait 1211 * 1212 * Should be used after both ends of the link have been bonded (or 1213 * bonding has been disabled) to wait until the link actually reaches 1214 * the expected state. 1215 * 1216 * Can be passed a mask of expected widths. 1217 * 1218 * Return: 1219 * * %0 - If link reaches any of the specified widths. 1220 * * %-ETIMEDOUT - If link does not reach specified width. 1221 * * Negative errno - Another error occurred. 1222 */ 1223 int tb_port_wait_for_link_width(struct tb_port *port, unsigned int width, 1224 int timeout_msec) 1225 { 1226 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec); 1227 int ret; 1228 1229 /* Gen 4 link does not support single lane */ 1230 if ((width & TB_LINK_WIDTH_SINGLE) && 1231 tb_port_get_link_generation(port) >= 4) 1232 return -EOPNOTSUPP; 1233 1234 do { 1235 ret = tb_port_get_link_width(port); 1236 if (ret < 0) { 1237 /* 1238 * Sometimes we get port locked error when 1239 * polling the lanes so we can ignore it and 1240 * retry. 1241 */ 1242 if (ret != -EACCES) 1243 return ret; 1244 } else if (ret & width) { 1245 return 0; 1246 } 1247 1248 usleep_range(1000, 2000); 1249 } while (ktime_before(ktime_get(), timeout)); 1250 1251 return -ETIMEDOUT; 1252 } 1253 1254 static int tb_port_do_update_credits(struct tb_port *port) 1255 { 1256 u32 nfc_credits; 1257 int ret; 1258 1259 ret = tb_port_read(port, &nfc_credits, TB_CFG_PORT, ADP_CS_4, 1); 1260 if (ret) 1261 return ret; 1262 1263 if (nfc_credits != port->config.nfc_credits) { 1264 u32 total; 1265 1266 total = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >> 1267 ADP_CS_4_TOTAL_BUFFERS_SHIFT; 1268 1269 tb_port_dbg(port, "total credits changed %u -> %u\n", 1270 port->total_credits, total); 1271 1272 port->config.nfc_credits = nfc_credits; 1273 port->total_credits = total; 1274 } 1275 1276 return 0; 1277 } 1278 1279 /** 1280 * tb_port_update_credits() - Re-read port total credits 1281 * @port: Port to update 1282 * 1283 * After the link is bonded (or bonding was disabled) the port total 1284 * credits may change, so this function needs to be called to re-read 1285 * the credits. Updates also the second lane adapter. 1286 * 1287 * Return: %0 on success, negative errno otherwise. 1288 */ 1289 int tb_port_update_credits(struct tb_port *port) 1290 { 1291 int ret; 1292 1293 ret = tb_port_do_update_credits(port); 1294 if (ret) 1295 return ret; 1296 1297 if (!port->dual_link_port) 1298 return 0; 1299 return tb_port_do_update_credits(port->dual_link_port); 1300 } 1301 1302 static int tb_port_start_lane_initialization(struct tb_port *port) 1303 { 1304 int ret; 1305 1306 if (tb_switch_is_usb4(port->sw)) 1307 return 0; 1308 1309 ret = tb_lc_start_lane_initialization(port); 1310 return ret == -EINVAL ? 0 : ret; 1311 } 1312 1313 /* 1314 * Returns true if the port had something (router, XDomain) connected 1315 * before suspend. 1316 */ 1317 static bool tb_port_resume(struct tb_port *port) 1318 { 1319 bool has_remote = tb_port_has_remote(port); 1320 1321 if (port->usb4) { 1322 usb4_port_device_resume(port->usb4); 1323 } else if (!has_remote) { 1324 /* 1325 * For disconnected downstream lane adapters start lane 1326 * initialization now so we detect future connects. 1327 * 1328 * For XDomain start the lane initialzation now so the 1329 * link gets re-established. 1330 * 1331 * This is only needed for non-USB4 ports. 1332 */ 1333 if (!tb_is_upstream_port(port) || port->xdomain) 1334 tb_port_start_lane_initialization(port); 1335 } 1336 1337 return has_remote || port->xdomain; 1338 } 1339 1340 /** 1341 * tb_port_is_enabled() - Is the adapter port enabled 1342 * @port: Port to check 1343 * 1344 * Return: %true if port is enabled, %false otherwise. 1345 */ 1346 bool tb_port_is_enabled(struct tb_port *port) 1347 { 1348 switch (port->config.type) { 1349 case TB_TYPE_PCIE_UP: 1350 case TB_TYPE_PCIE_DOWN: 1351 return tb_pci_port_is_enabled(port); 1352 1353 case TB_TYPE_DP_HDMI_IN: 1354 case TB_TYPE_DP_HDMI_OUT: 1355 return tb_dp_port_is_enabled(port); 1356 1357 case TB_TYPE_USB3_UP: 1358 case TB_TYPE_USB3_DOWN: 1359 return tb_usb3_port_is_enabled(port); 1360 1361 default: 1362 return false; 1363 } 1364 } 1365 1366 /** 1367 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled 1368 * @port: USB3 adapter port to check 1369 * 1370 * Return: %true if port is enabled, %false otherwise. 1371 */ 1372 bool tb_usb3_port_is_enabled(struct tb_port *port) 1373 { 1374 u32 data; 1375 1376 if (tb_port_read(port, &data, TB_CFG_PORT, 1377 port->cap_adap + ADP_USB3_CS_0, 1)) 1378 return false; 1379 1380 return !!(data & ADP_USB3_CS_0_PE); 1381 } 1382 1383 /** 1384 * tb_usb3_port_enable() - Enable USB3 adapter port 1385 * @port: USB3 adapter port to enable 1386 * @enable: Enable/disable the USB3 adapter 1387 * 1388 * Return: %0 on success, negative errno otherwise. 1389 */ 1390 int tb_usb3_port_enable(struct tb_port *port, bool enable) 1391 { 1392 u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V) 1393 : ADP_USB3_CS_0_V; 1394 1395 if (!port->cap_adap) 1396 return -ENXIO; 1397 return tb_port_write(port, &word, TB_CFG_PORT, 1398 port->cap_adap + ADP_USB3_CS_0, 1); 1399 } 1400 1401 /** 1402 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled 1403 * @port: PCIe port to check 1404 * 1405 * Return: %true if port is enabled, %false otherwise. 1406 */ 1407 bool tb_pci_port_is_enabled(struct tb_port *port) 1408 { 1409 u32 data; 1410 1411 if (tb_port_read(port, &data, TB_CFG_PORT, 1412 port->cap_adap + ADP_PCIE_CS_0, 1)) 1413 return false; 1414 1415 return !!(data & ADP_PCIE_CS_0_PE); 1416 } 1417 1418 /** 1419 * tb_pci_port_enable() - Enable PCIe adapter port 1420 * @port: PCIe port to enable 1421 * @enable: Enable/disable the PCIe adapter 1422 * 1423 * Return: %0 on success, negative errno otherwise. 1424 */ 1425 int tb_pci_port_enable(struct tb_port *port, bool enable) 1426 { 1427 u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0; 1428 if (!port->cap_adap) 1429 return -ENXIO; 1430 return tb_port_write(port, &word, TB_CFG_PORT, 1431 port->cap_adap + ADP_PCIE_CS_0, 1); 1432 } 1433 1434 /** 1435 * tb_dp_port_hpd_is_active() - Is HPD already active 1436 * @port: DP out port to check 1437 * 1438 * Checks if the DP OUT adapter port has HPD bit already set. 1439 * 1440 * Return: %1 if HPD is active, %0 otherwise. 1441 */ 1442 int tb_dp_port_hpd_is_active(struct tb_port *port) 1443 { 1444 u32 data; 1445 int ret; 1446 1447 ret = tb_port_read(port, &data, TB_CFG_PORT, 1448 port->cap_adap + ADP_DP_CS_2, 1); 1449 if (ret) 1450 return ret; 1451 1452 return !!(data & ADP_DP_CS_2_HPD); 1453 } 1454 1455 /** 1456 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port 1457 * @port: Port to clear HPD 1458 * 1459 * If the DP IN port has HPD set, this function can be used to clear it. 1460 * 1461 * Return: %0 on success, negative errno otherwise. 1462 */ 1463 int tb_dp_port_hpd_clear(struct tb_port *port) 1464 { 1465 u32 data; 1466 int ret; 1467 1468 ret = tb_port_read(port, &data, TB_CFG_PORT, 1469 port->cap_adap + ADP_DP_CS_3, 1); 1470 if (ret) 1471 return ret; 1472 1473 data |= ADP_DP_CS_3_HPDC; 1474 return tb_port_write(port, &data, TB_CFG_PORT, 1475 port->cap_adap + ADP_DP_CS_3, 1); 1476 } 1477 1478 /** 1479 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port 1480 * @port: DP IN/OUT port to set hops 1481 * @video: Video Hop ID 1482 * @aux_tx: AUX TX Hop ID 1483 * @aux_rx: AUX RX Hop ID 1484 * 1485 * Programs specified Hop IDs for DP IN/OUT port. Can be called for USB4 1486 * router DP adapters too but does not program the values as the fields 1487 * are read-only. 1488 * 1489 * Return: %0 on success, negative errno otherwise. 1490 */ 1491 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video, 1492 unsigned int aux_tx, unsigned int aux_rx) 1493 { 1494 u32 data[2]; 1495 int ret; 1496 1497 if (tb_switch_is_usb4(port->sw)) 1498 return 0; 1499 1500 ret = tb_port_read(port, data, TB_CFG_PORT, 1501 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); 1502 if (ret) 1503 return ret; 1504 1505 data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK; 1506 data[1] &= ~ADP_DP_CS_1_AUX_TX_HOPID_MASK; 1507 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK; 1508 1509 data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) & 1510 ADP_DP_CS_0_VIDEO_HOPID_MASK; 1511 data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK; 1512 data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) & 1513 ADP_DP_CS_1_AUX_RX_HOPID_MASK; 1514 1515 return tb_port_write(port, data, TB_CFG_PORT, 1516 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); 1517 } 1518 1519 /** 1520 * tb_dp_port_is_enabled() - Is DP adapter port enabled 1521 * @port: DP adapter port to check 1522 * 1523 * Return: %true if DP port is enabled, %false otherwise. 1524 */ 1525 bool tb_dp_port_is_enabled(struct tb_port *port) 1526 { 1527 u32 data[2]; 1528 1529 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0, 1530 ARRAY_SIZE(data))) 1531 return false; 1532 1533 return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE)); 1534 } 1535 1536 /** 1537 * tb_dp_port_enable() - Enables/disables DP paths of a port 1538 * @port: DP IN/OUT port 1539 * @enable: Enable/disable DP path 1540 * 1541 * Once Hop IDs are programmed DP paths can be enabled or disabled by 1542 * calling this function. 1543 * 1544 * Return: %0 on success, negative errno otherwise. 1545 */ 1546 int tb_dp_port_enable(struct tb_port *port, bool enable) 1547 { 1548 u32 data[2]; 1549 int ret; 1550 1551 ret = tb_port_read(port, data, TB_CFG_PORT, 1552 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); 1553 if (ret) 1554 return ret; 1555 1556 if (enable) 1557 data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE; 1558 else 1559 data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE); 1560 1561 return tb_port_write(port, data, TB_CFG_PORT, 1562 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); 1563 } 1564 1565 /* switch utility functions */ 1566 1567 static const char *tb_switch_generation_name(const struct tb_switch *sw) 1568 { 1569 switch (sw->generation) { 1570 case 1: 1571 return "Thunderbolt 1"; 1572 case 2: 1573 return "Thunderbolt 2"; 1574 case 3: 1575 return "Thunderbolt 3"; 1576 case 4: 1577 return "USB4"; 1578 default: 1579 return "Unknown"; 1580 } 1581 } 1582 1583 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw) 1584 { 1585 const struct tb_regs_switch_header *regs = &sw->config; 1586 1587 tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n", 1588 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, 1589 regs->revision, regs->thunderbolt_version); 1590 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number); 1591 tb_dbg(tb, " Config:\n"); 1592 tb_dbg(tb, 1593 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", 1594 regs->upstream_port_number, regs->depth, 1595 (((u64) regs->route_hi) << 32) | regs->route_lo, 1596 regs->enabled, regs->plug_events_delay); 1597 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n", 1598 regs->__unknown1, regs->__unknown4); 1599 } 1600 1601 static int tb_switch_reset_host(struct tb_switch *sw) 1602 { 1603 if (sw->generation > 1) { 1604 struct tb_port *port; 1605 1606 tb_switch_for_each_port(sw, port) { 1607 int i, ret; 1608 1609 /* 1610 * For lane adapters we issue downstream port 1611 * reset and clear up path config spaces. 1612 * 1613 * For protocol adapters we disable the path and 1614 * clear path config space one by one (from 8 to 1615 * Max Input HopID of the adapter). 1616 */ 1617 if (tb_port_is_null(port) && !tb_is_upstream_port(port)) { 1618 ret = tb_port_reset(port); 1619 if (ret) 1620 return ret; 1621 /* 1622 * USB4 Lane 1 adapters do not have accessible 1623 * path config space. 1624 */ 1625 if (tb_switch_is_usb4(sw) && !port->usb4) 1626 continue; 1627 } else if (tb_port_is_usb3_down(port) || 1628 tb_port_is_usb3_up(port)) { 1629 tb_usb3_port_enable(port, false); 1630 } else if (tb_port_is_dpin(port) || 1631 tb_port_is_dpout(port)) { 1632 tb_dp_port_enable(port, false); 1633 } else if (tb_port_is_pcie_down(port) || 1634 tb_port_is_pcie_up(port)) { 1635 tb_pci_port_enable(port, false); 1636 } else { 1637 continue; 1638 } 1639 1640 /* Cleanup path config space of protocol adapter */ 1641 for (i = TB_PATH_MIN_HOPID; 1642 i <= port->config.max_in_hop_id; i++) { 1643 ret = tb_path_deactivate_hop(port, i); 1644 if (ret) 1645 return ret; 1646 } 1647 } 1648 } else { 1649 struct tb_cfg_result res; 1650 1651 /* Thunderbolt 1 uses the "reset" config space packet */ 1652 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, 1653 TB_CFG_SWITCH, 2, 2); 1654 if (res.err) 1655 return res.err; 1656 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw)); 1657 if (res.err > 0) 1658 return -EIO; 1659 else if (res.err < 0) 1660 return res.err; 1661 } 1662 1663 return 0; 1664 } 1665 1666 static int tb_switch_reset_device(struct tb_switch *sw) 1667 { 1668 return tb_port_reset(tb_switch_downstream_port(sw)); 1669 } 1670 1671 static bool tb_switch_enumerated(struct tb_switch *sw) 1672 { 1673 u32 val; 1674 int ret; 1675 1676 /* 1677 * Read directly from the hardware because we use this also 1678 * during system sleep where sw->config.enabled is already set 1679 * by us. 1680 */ 1681 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_3, 1); 1682 if (ret) 1683 return false; 1684 1685 return !!(val & ROUTER_CS_3_V); 1686 } 1687 1688 /** 1689 * tb_switch_reset() - Perform reset to the router 1690 * @sw: Router to reset 1691 * 1692 * Issues reset to the router @sw. Can be used for any router. For host 1693 * routers, resets all the downstream ports and cleans up path config 1694 * spaces accordingly. For device routers issues downstream port reset 1695 * through the parent router, so as side effect there will be unplug 1696 * soon after this is finished. 1697 * 1698 * If the router is not enumerated does nothing. 1699 * 1700 * Return: %0 on success, negative errno otherwise. 1701 */ 1702 int tb_switch_reset(struct tb_switch *sw) 1703 { 1704 int ret; 1705 1706 /* 1707 * We cannot access the port config spaces unless the router is 1708 * already enumerated. If the router is not enumerated it is 1709 * equal to being reset so we can skip that here. 1710 */ 1711 if (!tb_switch_enumerated(sw)) 1712 return 0; 1713 1714 tb_sw_dbg(sw, "resetting\n"); 1715 1716 if (tb_route(sw)) 1717 ret = tb_switch_reset_device(sw); 1718 else 1719 ret = tb_switch_reset_host(sw); 1720 1721 if (ret) 1722 tb_sw_warn(sw, "failed to reset\n"); 1723 1724 return ret; 1725 } 1726 1727 /** 1728 * tb_switch_wait_for_bit() - Wait for specified value of bits in offset 1729 * @sw: Router to read the offset value from 1730 * @offset: Offset in the router config space to read from 1731 * @bit: Bit mask in the offset to wait for 1732 * @value: Value of the bits to wait for 1733 * @timeout_msec: Timeout in ms how long to wait 1734 * 1735 * Wait till the specified bits in specified offset reach specified value. 1736 * 1737 * Return: 1738 * * %0 - On success. 1739 * * %-ETIMEDOUT - If the @value was not reached within 1740 * the given timeout. 1741 * * Negative errno - In case of failure. 1742 */ 1743 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit, 1744 u32 value, int timeout_msec) 1745 { 1746 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec); 1747 1748 do { 1749 u32 val; 1750 int ret; 1751 1752 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1); 1753 if (ret) 1754 return ret; 1755 1756 if ((val & bit) == value) 1757 return 0; 1758 1759 usleep_range(50, 100); 1760 } while (ktime_before(ktime_get(), timeout)); 1761 1762 return -ETIMEDOUT; 1763 } 1764 1765 /* 1766 * tb_plug_events_active() - enable/disable plug events on a switch 1767 * 1768 * Return: %0 on success, negative errno otherwise. 1769 */ 1770 static int tb_plug_events_active(struct tb_switch *sw, bool active) 1771 { 1772 u32 data; 1773 int res; 1774 1775 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw)) 1776 return 0; 1777 1778 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); 1779 if (res) 1780 return res; 1781 1782 if (active) { 1783 data = data & 0xFFFFFF83; 1784 switch (sw->config.device_id) { 1785 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: 1786 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: 1787 case PCI_DEVICE_ID_INTEL_PORT_RIDGE: 1788 break; 1789 default: 1790 /* 1791 * Skip Alpine Ridge, it needs to have vendor 1792 * specific USB hotplug event enabled for the 1793 * internal xHCI to work. 1794 */ 1795 if (!tb_switch_is_alpine_ridge(sw)) 1796 data |= TB_PLUG_EVENTS_USB_DISABLE; 1797 } 1798 } else { 1799 data = data | 0x7c; 1800 } 1801 return tb_sw_write(sw, &data, TB_CFG_SWITCH, 1802 sw->cap_plug_events + 1, 1); 1803 } 1804 1805 static ssize_t authorized_show(struct device *dev, 1806 struct device_attribute *attr, 1807 char *buf) 1808 { 1809 struct tb_switch *sw = tb_to_switch(dev); 1810 1811 return sysfs_emit(buf, "%u\n", sw->authorized); 1812 } 1813 1814 static int disapprove_switch(struct device *dev, void *not_used) 1815 { 1816 char *envp[] = { "AUTHORIZED=0", NULL }; 1817 struct tb_switch *sw; 1818 1819 sw = tb_to_switch(dev); 1820 if (sw && sw->authorized) { 1821 int ret; 1822 1823 /* First children */ 1824 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch); 1825 if (ret) 1826 return ret; 1827 1828 ret = tb_domain_disapprove_switch(sw->tb, sw); 1829 if (ret) 1830 return ret; 1831 1832 sw->authorized = 0; 1833 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); 1834 } 1835 1836 return 0; 1837 } 1838 1839 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) 1840 { 1841 char envp_string[13]; 1842 int ret = -EINVAL; 1843 char *envp[] = { envp_string, NULL }; 1844 1845 if (!mutex_trylock(&sw->tb->lock)) 1846 return restart_syscall(); 1847 1848 if (!!sw->authorized == !!val) 1849 goto unlock; 1850 1851 switch (val) { 1852 /* Disapprove switch */ 1853 case 0: 1854 if (tb_route(sw)) { 1855 ret = disapprove_switch(&sw->dev, NULL); 1856 goto unlock; 1857 } 1858 break; 1859 1860 /* Approve switch */ 1861 case 1: 1862 if (sw->key) 1863 ret = tb_domain_approve_switch_key(sw->tb, sw); 1864 else 1865 ret = tb_domain_approve_switch(sw->tb, sw); 1866 break; 1867 1868 /* Challenge switch */ 1869 case 2: 1870 if (sw->key) 1871 ret = tb_domain_challenge_switch_key(sw->tb, sw); 1872 break; 1873 1874 default: 1875 break; 1876 } 1877 1878 if (!ret) { 1879 sw->authorized = val; 1880 /* 1881 * Notify status change to the userspace, informing the new 1882 * value of /sys/bus/thunderbolt/devices/.../authorized. 1883 */ 1884 sprintf(envp_string, "AUTHORIZED=%u", sw->authorized); 1885 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); 1886 } 1887 1888 unlock: 1889 mutex_unlock(&sw->tb->lock); 1890 return ret; 1891 } 1892 1893 static ssize_t authorized_store(struct device *dev, 1894 struct device_attribute *attr, 1895 const char *buf, size_t count) 1896 { 1897 struct tb_switch *sw = tb_to_switch(dev); 1898 unsigned int val; 1899 ssize_t ret; 1900 1901 ret = kstrtouint(buf, 0, &val); 1902 if (ret) 1903 return ret; 1904 if (val > 2) 1905 return -EINVAL; 1906 1907 pm_runtime_get_sync(&sw->dev); 1908 ret = tb_switch_set_authorized(sw, val); 1909 pm_runtime_mark_last_busy(&sw->dev); 1910 pm_runtime_put_autosuspend(&sw->dev); 1911 1912 return ret ? ret : count; 1913 } 1914 static DEVICE_ATTR_RW(authorized); 1915 1916 static ssize_t boot_show(struct device *dev, struct device_attribute *attr, 1917 char *buf) 1918 { 1919 struct tb_switch *sw = tb_to_switch(dev); 1920 1921 return sysfs_emit(buf, "%u\n", sw->boot); 1922 } 1923 static DEVICE_ATTR_RO(boot); 1924 1925 static ssize_t device_show(struct device *dev, struct device_attribute *attr, 1926 char *buf) 1927 { 1928 struct tb_switch *sw = tb_to_switch(dev); 1929 1930 return sysfs_emit(buf, "%#x\n", sw->device); 1931 } 1932 static DEVICE_ATTR_RO(device); 1933 1934 static ssize_t 1935 device_name_show(struct device *dev, struct device_attribute *attr, char *buf) 1936 { 1937 struct tb_switch *sw = tb_to_switch(dev); 1938 1939 return sysfs_emit(buf, "%s\n", sw->device_name ?: ""); 1940 } 1941 static DEVICE_ATTR_RO(device_name); 1942 1943 static ssize_t 1944 generation_show(struct device *dev, struct device_attribute *attr, char *buf) 1945 { 1946 struct tb_switch *sw = tb_to_switch(dev); 1947 1948 return sysfs_emit(buf, "%u\n", sw->generation); 1949 } 1950 static DEVICE_ATTR_RO(generation); 1951 1952 static ssize_t key_show(struct device *dev, struct device_attribute *attr, 1953 char *buf) 1954 { 1955 struct tb_switch *sw = tb_to_switch(dev); 1956 ssize_t ret; 1957 1958 if (!mutex_trylock(&sw->tb->lock)) 1959 return restart_syscall(); 1960 1961 if (sw->key) 1962 ret = sysfs_emit(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); 1963 else 1964 ret = sysfs_emit(buf, "\n"); 1965 1966 mutex_unlock(&sw->tb->lock); 1967 return ret; 1968 } 1969 1970 static ssize_t key_store(struct device *dev, struct device_attribute *attr, 1971 const char *buf, size_t count) 1972 { 1973 struct tb_switch *sw = tb_to_switch(dev); 1974 u8 key[TB_SWITCH_KEY_SIZE]; 1975 ssize_t ret = count; 1976 bool clear = false; 1977 1978 if (!strcmp(buf, "\n")) 1979 clear = true; 1980 else if (hex2bin(key, buf, sizeof(key))) 1981 return -EINVAL; 1982 1983 if (!mutex_trylock(&sw->tb->lock)) 1984 return restart_syscall(); 1985 1986 if (sw->authorized) { 1987 ret = -EBUSY; 1988 } else { 1989 kfree(sw->key); 1990 if (clear) { 1991 sw->key = NULL; 1992 } else { 1993 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); 1994 if (!sw->key) 1995 ret = -ENOMEM; 1996 } 1997 } 1998 1999 mutex_unlock(&sw->tb->lock); 2000 return ret; 2001 } 2002 static DEVICE_ATTR(key, 0600, key_show, key_store); 2003 2004 static ssize_t speed_show(struct device *dev, struct device_attribute *attr, 2005 char *buf) 2006 { 2007 struct tb_switch *sw = tb_to_switch(dev); 2008 2009 return sysfs_emit(buf, "%u.0 Gb/s\n", sw->link_speed); 2010 } 2011 2012 /* 2013 * Currently all lanes must run at the same speed but we expose here 2014 * both directions to allow possible asymmetric links in the future. 2015 */ 2016 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL); 2017 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL); 2018 2019 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr, 2020 char *buf) 2021 { 2022 struct tb_switch *sw = tb_to_switch(dev); 2023 unsigned int width; 2024 2025 switch (sw->link_width) { 2026 case TB_LINK_WIDTH_SINGLE: 2027 case TB_LINK_WIDTH_ASYM_TX: 2028 width = 1; 2029 break; 2030 case TB_LINK_WIDTH_DUAL: 2031 width = 2; 2032 break; 2033 case TB_LINK_WIDTH_ASYM_RX: 2034 width = 3; 2035 break; 2036 default: 2037 WARN_ON_ONCE(1); 2038 return -EINVAL; 2039 } 2040 2041 return sysfs_emit(buf, "%u\n", width); 2042 } 2043 static DEVICE_ATTR(rx_lanes, 0444, rx_lanes_show, NULL); 2044 2045 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr, 2046 char *buf) 2047 { 2048 struct tb_switch *sw = tb_to_switch(dev); 2049 unsigned int width; 2050 2051 switch (sw->link_width) { 2052 case TB_LINK_WIDTH_SINGLE: 2053 case TB_LINK_WIDTH_ASYM_RX: 2054 width = 1; 2055 break; 2056 case TB_LINK_WIDTH_DUAL: 2057 width = 2; 2058 break; 2059 case TB_LINK_WIDTH_ASYM_TX: 2060 width = 3; 2061 break; 2062 default: 2063 WARN_ON_ONCE(1); 2064 return -EINVAL; 2065 } 2066 2067 return sysfs_emit(buf, "%u\n", width); 2068 } 2069 static DEVICE_ATTR(tx_lanes, 0444, tx_lanes_show, NULL); 2070 2071 static ssize_t nvm_authenticate_show(struct device *dev, 2072 struct device_attribute *attr, char *buf) 2073 { 2074 struct tb_switch *sw = tb_to_switch(dev); 2075 u32 status; 2076 2077 nvm_get_auth_status(sw, &status); 2078 return sysfs_emit(buf, "%#x\n", status); 2079 } 2080 2081 static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf, 2082 bool disconnect) 2083 { 2084 struct tb_switch *sw = tb_to_switch(dev); 2085 int val, ret; 2086 2087 pm_runtime_get_sync(&sw->dev); 2088 2089 if (!mutex_trylock(&sw->tb->lock)) { 2090 ret = restart_syscall(); 2091 goto exit_rpm; 2092 } 2093 2094 if (sw->no_nvm_upgrade) { 2095 ret = -EOPNOTSUPP; 2096 goto exit_unlock; 2097 } 2098 2099 /* If NVMem devices are not yet added */ 2100 if (!sw->nvm) { 2101 ret = -EAGAIN; 2102 goto exit_unlock; 2103 } 2104 2105 ret = kstrtoint(buf, 10, &val); 2106 if (ret) 2107 goto exit_unlock; 2108 2109 /* Always clear the authentication status */ 2110 nvm_clear_auth_status(sw); 2111 2112 if (val > 0) { 2113 if (val == AUTHENTICATE_ONLY) { 2114 if (disconnect) 2115 ret = -EINVAL; 2116 else 2117 ret = nvm_authenticate(sw, true); 2118 } else { 2119 if (!sw->nvm->flushed) { 2120 if (!sw->nvm->buf) { 2121 ret = -EINVAL; 2122 goto exit_unlock; 2123 } 2124 2125 ret = nvm_validate_and_write(sw); 2126 if (ret || val == WRITE_ONLY) 2127 goto exit_unlock; 2128 } 2129 if (val == WRITE_AND_AUTHENTICATE) { 2130 if (disconnect) 2131 ret = tb_lc_force_power(sw); 2132 else 2133 ret = nvm_authenticate(sw, false); 2134 } 2135 } 2136 } 2137 2138 exit_unlock: 2139 mutex_unlock(&sw->tb->lock); 2140 exit_rpm: 2141 pm_runtime_mark_last_busy(&sw->dev); 2142 pm_runtime_put_autosuspend(&sw->dev); 2143 2144 return ret; 2145 } 2146 2147 static ssize_t nvm_authenticate_store(struct device *dev, 2148 struct device_attribute *attr, const char *buf, size_t count) 2149 { 2150 int ret = nvm_authenticate_sysfs(dev, buf, false); 2151 if (ret) 2152 return ret; 2153 return count; 2154 } 2155 static DEVICE_ATTR_RW(nvm_authenticate); 2156 2157 static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev, 2158 struct device_attribute *attr, char *buf) 2159 { 2160 return nvm_authenticate_show(dev, attr, buf); 2161 } 2162 2163 static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev, 2164 struct device_attribute *attr, const char *buf, size_t count) 2165 { 2166 int ret; 2167 2168 ret = nvm_authenticate_sysfs(dev, buf, true); 2169 return ret ? ret : count; 2170 } 2171 static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect); 2172 2173 static ssize_t nvm_version_show(struct device *dev, 2174 struct device_attribute *attr, char *buf) 2175 { 2176 struct tb_switch *sw = tb_to_switch(dev); 2177 int ret; 2178 2179 if (!mutex_trylock(&sw->tb->lock)) 2180 return restart_syscall(); 2181 2182 if (sw->safe_mode) 2183 ret = -ENODATA; 2184 else if (!sw->nvm) 2185 ret = -EAGAIN; 2186 else 2187 ret = sysfs_emit(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); 2188 2189 mutex_unlock(&sw->tb->lock); 2190 2191 return ret; 2192 } 2193 static DEVICE_ATTR_RO(nvm_version); 2194 2195 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, 2196 char *buf) 2197 { 2198 struct tb_switch *sw = tb_to_switch(dev); 2199 2200 return sysfs_emit(buf, "%#x\n", sw->vendor); 2201 } 2202 static DEVICE_ATTR_RO(vendor); 2203 2204 static ssize_t 2205 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) 2206 { 2207 struct tb_switch *sw = tb_to_switch(dev); 2208 2209 return sysfs_emit(buf, "%s\n", sw->vendor_name ?: ""); 2210 } 2211 static DEVICE_ATTR_RO(vendor_name); 2212 2213 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, 2214 char *buf) 2215 { 2216 struct tb_switch *sw = tb_to_switch(dev); 2217 2218 return sysfs_emit(buf, "%pUb\n", sw->uuid); 2219 } 2220 static DEVICE_ATTR_RO(unique_id); 2221 2222 static struct attribute *switch_attrs[] = { 2223 &dev_attr_authorized.attr, 2224 &dev_attr_boot.attr, 2225 &dev_attr_device.attr, 2226 &dev_attr_device_name.attr, 2227 &dev_attr_generation.attr, 2228 &dev_attr_key.attr, 2229 &dev_attr_nvm_authenticate.attr, 2230 &dev_attr_nvm_authenticate_on_disconnect.attr, 2231 &dev_attr_nvm_version.attr, 2232 &dev_attr_rx_speed.attr, 2233 &dev_attr_rx_lanes.attr, 2234 &dev_attr_tx_speed.attr, 2235 &dev_attr_tx_lanes.attr, 2236 &dev_attr_vendor.attr, 2237 &dev_attr_vendor_name.attr, 2238 &dev_attr_unique_id.attr, 2239 NULL, 2240 }; 2241 2242 static umode_t switch_attr_is_visible(struct kobject *kobj, 2243 struct attribute *attr, int n) 2244 { 2245 struct device *dev = kobj_to_dev(kobj); 2246 struct tb_switch *sw = tb_to_switch(dev); 2247 2248 if (attr == &dev_attr_authorized.attr) { 2249 if (sw->tb->security_level == TB_SECURITY_NOPCIE || 2250 sw->tb->security_level == TB_SECURITY_DPONLY) 2251 return 0; 2252 } else if (attr == &dev_attr_device.attr) { 2253 if (!sw->device) 2254 return 0; 2255 } else if (attr == &dev_attr_device_name.attr) { 2256 if (!sw->device_name) 2257 return 0; 2258 } else if (attr == &dev_attr_vendor.attr) { 2259 if (!sw->vendor) 2260 return 0; 2261 } else if (attr == &dev_attr_vendor_name.attr) { 2262 if (!sw->vendor_name) 2263 return 0; 2264 } else if (attr == &dev_attr_key.attr) { 2265 if (tb_route(sw) && 2266 sw->tb->security_level == TB_SECURITY_SECURE && 2267 sw->security_level == TB_SECURITY_SECURE) 2268 return attr->mode; 2269 return 0; 2270 } else if (attr == &dev_attr_rx_speed.attr || 2271 attr == &dev_attr_rx_lanes.attr || 2272 attr == &dev_attr_tx_speed.attr || 2273 attr == &dev_attr_tx_lanes.attr) { 2274 if (tb_route(sw)) 2275 return attr->mode; 2276 return 0; 2277 } else if (attr == &dev_attr_nvm_authenticate.attr) { 2278 if (nvm_upgradeable(sw)) 2279 return attr->mode; 2280 return 0; 2281 } else if (attr == &dev_attr_nvm_version.attr) { 2282 if (nvm_readable(sw)) 2283 return attr->mode; 2284 return 0; 2285 } else if (attr == &dev_attr_boot.attr) { 2286 if (tb_route(sw)) 2287 return attr->mode; 2288 return 0; 2289 } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) { 2290 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) 2291 return attr->mode; 2292 return 0; 2293 } 2294 2295 return sw->safe_mode ? 0 : attr->mode; 2296 } 2297 2298 static const struct attribute_group switch_group = { 2299 .is_visible = switch_attr_is_visible, 2300 .attrs = switch_attrs, 2301 }; 2302 2303 static const struct attribute_group *switch_groups[] = { 2304 &switch_group, 2305 NULL, 2306 }; 2307 2308 static void tb_switch_release(struct device *dev) 2309 { 2310 struct tb_switch *sw = tb_to_switch(dev); 2311 struct tb_port *port; 2312 2313 dma_port_free(sw->dma_port); 2314 2315 tb_switch_for_each_port(sw, port) { 2316 ida_destroy(&port->in_hopids); 2317 ida_destroy(&port->out_hopids); 2318 } 2319 2320 kfree(sw->uuid); 2321 kfree(sw->device_name); 2322 kfree(sw->vendor_name); 2323 kfree(sw->ports); 2324 kfree(sw->drom); 2325 kfree(sw->key); 2326 kfree(sw); 2327 } 2328 2329 static int tb_switch_uevent(const struct device *dev, struct kobj_uevent_env *env) 2330 { 2331 const struct tb_switch *sw = tb_to_switch(dev); 2332 const char *type; 2333 2334 if (tb_switch_is_usb4(sw)) { 2335 if (add_uevent_var(env, "USB4_VERSION=%u.0", 2336 usb4_switch_version(sw))) 2337 return -ENOMEM; 2338 } 2339 2340 if (!tb_route(sw)) { 2341 type = "host"; 2342 } else { 2343 const struct tb_port *port; 2344 bool hub = false; 2345 2346 /* Device is hub if it has any downstream ports */ 2347 tb_switch_for_each_port(sw, port) { 2348 if (!port->disabled && !tb_is_upstream_port(port) && 2349 tb_port_is_null(port)) { 2350 hub = true; 2351 break; 2352 } 2353 } 2354 2355 type = hub ? "hub" : "device"; 2356 } 2357 2358 if (add_uevent_var(env, "USB4_TYPE=%s", type)) 2359 return -ENOMEM; 2360 return 0; 2361 } 2362 2363 /* 2364 * Currently only need to provide the callbacks. Everything else is handled 2365 * in the connection manager. 2366 */ 2367 static int __maybe_unused tb_switch_runtime_suspend(struct device *dev) 2368 { 2369 struct tb_switch *sw = tb_to_switch(dev); 2370 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; 2371 2372 if (cm_ops->runtime_suspend_switch) 2373 return cm_ops->runtime_suspend_switch(sw); 2374 2375 return 0; 2376 } 2377 2378 static int __maybe_unused tb_switch_runtime_resume(struct device *dev) 2379 { 2380 struct tb_switch *sw = tb_to_switch(dev); 2381 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; 2382 2383 if (cm_ops->runtime_resume_switch) 2384 return cm_ops->runtime_resume_switch(sw); 2385 return 0; 2386 } 2387 2388 static const struct dev_pm_ops tb_switch_pm_ops = { 2389 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume, 2390 NULL) 2391 }; 2392 2393 const struct device_type tb_switch_type = { 2394 .name = "thunderbolt_device", 2395 .release = tb_switch_release, 2396 .uevent = tb_switch_uevent, 2397 .pm = &tb_switch_pm_ops, 2398 }; 2399 2400 static int tb_switch_get_generation(struct tb_switch *sw) 2401 { 2402 if (tb_switch_is_usb4(sw)) 2403 return 4; 2404 2405 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) { 2406 switch (sw->config.device_id) { 2407 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: 2408 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: 2409 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK: 2410 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C: 2411 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C: 2412 case PCI_DEVICE_ID_INTEL_PORT_RIDGE: 2413 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE: 2414 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE: 2415 return 1; 2416 2417 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE: 2418 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE: 2419 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE: 2420 return 2; 2421 2422 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: 2423 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: 2424 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: 2425 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: 2426 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: 2427 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE: 2428 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE: 2429 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE: 2430 case PCI_DEVICE_ID_INTEL_ICL_NHI0: 2431 case PCI_DEVICE_ID_INTEL_ICL_NHI1: 2432 return 3; 2433 } 2434 } 2435 2436 /* 2437 * For unknown switches assume generation to be 1 to be on the 2438 * safe side. 2439 */ 2440 tb_sw_warn(sw, "unsupported switch device id %#x\n", 2441 sw->config.device_id); 2442 return 1; 2443 } 2444 2445 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth) 2446 { 2447 int max_depth; 2448 2449 if (tb_switch_is_usb4(sw) || 2450 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) 2451 max_depth = USB4_SWITCH_MAX_DEPTH; 2452 else 2453 max_depth = TB_SWITCH_MAX_DEPTH; 2454 2455 return depth > max_depth; 2456 } 2457 2458 /** 2459 * tb_switch_alloc() - allocate a switch 2460 * @tb: Pointer to the owning domain 2461 * @parent: Parent device for this switch 2462 * @route: Route string for this switch 2463 * 2464 * Allocates and initializes a switch. Will not upload configuration to 2465 * the switch. For that you need to call tb_switch_configure() 2466 * separately. The returned switch should be released by calling 2467 * tb_switch_put(). 2468 * 2469 * Return: Pointer to &struct tb_switch or ERR_PTR() in case of failure. 2470 */ 2471 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent, 2472 u64 route) 2473 { 2474 struct tb_switch *sw; 2475 int upstream_port; 2476 int i, ret, depth; 2477 2478 /* Unlock the downstream port so we can access the switch below */ 2479 if (route) { 2480 struct tb_switch *parent_sw = tb_to_switch(parent); 2481 struct tb_port *down; 2482 2483 down = tb_port_at(route, parent_sw); 2484 tb_port_unlock(down); 2485 } 2486 2487 depth = tb_route_length(route); 2488 2489 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); 2490 if (upstream_port < 0) 2491 return ERR_PTR(upstream_port); 2492 2493 sw = kzalloc_obj(*sw); 2494 if (!sw) 2495 return ERR_PTR(-ENOMEM); 2496 2497 sw->tb = tb; 2498 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); 2499 if (ret) 2500 goto err_free_sw_ports; 2501 2502 sw->generation = tb_switch_get_generation(sw); 2503 2504 tb_dbg(tb, "current switch config:\n"); 2505 tb_dump_switch(tb, sw); 2506 2507 /* configure switch */ 2508 sw->config.upstream_port_number = upstream_port; 2509 sw->config.depth = depth; 2510 sw->config.route_hi = upper_32_bits(route); 2511 sw->config.route_lo = lower_32_bits(route); 2512 sw->config.enabled = 0; 2513 2514 /* Make sure we do not exceed maximum topology limit */ 2515 if (tb_switch_exceeds_max_depth(sw, depth)) { 2516 ret = -EADDRNOTAVAIL; 2517 goto err_free_sw_ports; 2518 } 2519 2520 /* initialize ports */ 2521 sw->ports = kzalloc_objs(*sw->ports, sw->config.max_port_number + 1); 2522 if (!sw->ports) { 2523 ret = -ENOMEM; 2524 goto err_free_sw_ports; 2525 } 2526 2527 for (i = 0; i <= sw->config.max_port_number; i++) { 2528 /* minimum setup for tb_find_cap and tb_drom_read to work */ 2529 sw->ports[i].sw = sw; 2530 sw->ports[i].port = i; 2531 2532 /* Control port does not need HopID allocation */ 2533 if (i) { 2534 ida_init(&sw->ports[i].in_hopids); 2535 ida_init(&sw->ports[i].out_hopids); 2536 } 2537 } 2538 2539 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); 2540 if (ret > 0) 2541 sw->cap_plug_events = ret; 2542 2543 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_TIME2); 2544 if (ret > 0) 2545 sw->cap_vsec_tmu = ret; 2546 2547 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); 2548 if (ret > 0) 2549 sw->cap_lc = ret; 2550 2551 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_CP_LP); 2552 if (ret > 0) 2553 sw->cap_lp = ret; 2554 2555 /* Root switch is always authorized */ 2556 if (!route) 2557 sw->authorized = true; 2558 2559 device_initialize(&sw->dev); 2560 sw->dev.parent = parent; 2561 sw->dev.bus = &tb_bus_type; 2562 sw->dev.type = &tb_switch_type; 2563 sw->dev.groups = switch_groups; 2564 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); 2565 2566 return sw; 2567 2568 err_free_sw_ports: 2569 kfree(sw->ports); 2570 kfree(sw); 2571 2572 return ERR_PTR(ret); 2573 } 2574 2575 /** 2576 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode 2577 * @tb: Pointer to the owning domain 2578 * @parent: Parent device for this switch 2579 * @route: Route string for this switch 2580 * 2581 * This creates a switch in safe mode. This means the switch pretty much 2582 * lacks all capabilities except DMA configuration port before it is 2583 * flashed with a valid NVM firmware. 2584 * 2585 * The returned switch must be released by calling tb_switch_put(). 2586 * 2587 * Return: Pointer to &struct tb_switch or ERR_PTR() in case of failure. 2588 */ 2589 struct tb_switch * 2590 tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route) 2591 { 2592 struct tb_switch *sw; 2593 2594 sw = kzalloc_obj(*sw); 2595 if (!sw) 2596 return ERR_PTR(-ENOMEM); 2597 2598 sw->tb = tb; 2599 sw->config.depth = tb_route_length(route); 2600 sw->config.route_hi = upper_32_bits(route); 2601 sw->config.route_lo = lower_32_bits(route); 2602 sw->safe_mode = true; 2603 2604 device_initialize(&sw->dev); 2605 sw->dev.parent = parent; 2606 sw->dev.bus = &tb_bus_type; 2607 sw->dev.type = &tb_switch_type; 2608 sw->dev.groups = switch_groups; 2609 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); 2610 2611 return sw; 2612 } 2613 2614 /** 2615 * tb_switch_configure() - Uploads configuration to the switch 2616 * @sw: Switch to configure 2617 * 2618 * Call this function before the switch is added to the system. It will 2619 * upload configuration to the switch and makes it available for the 2620 * connection manager to use. Can be called to the switch again after 2621 * resume from low power states to re-initialize it. 2622 * 2623 * Return: %0 on success, negative errno otherwise. 2624 */ 2625 int tb_switch_configure(struct tb_switch *sw) 2626 { 2627 struct tb *tb = sw->tb; 2628 u64 route; 2629 int ret; 2630 2631 route = tb_route(sw); 2632 2633 tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n", 2634 sw->config.enabled ? "restoring" : "initializing", route, 2635 tb_route_length(route), sw->config.upstream_port_number); 2636 2637 sw->config.enabled = 1; 2638 2639 /* Set Notification Timeout to 255 ms for all routers */ 2640 sw->config.plug_events_delay = 0xff; 2641 if (tb_switch_is_usb4(sw)) { 2642 /* 2643 * For USB4 devices, we need to program the CM version 2644 * accordingly so that it knows to expose all the 2645 * additional capabilities. Program it according to USB4 2646 * version to avoid changing existing (v1) routers behaviour. 2647 */ 2648 if (usb4_switch_version(sw) < 2) 2649 sw->config.cmuv = ROUTER_CS_4_CMUV_V1; 2650 else 2651 sw->config.cmuv = ROUTER_CS_4_CMUV_V2; 2652 2653 /* Enumerate the switch */ 2654 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, 2655 ROUTER_CS_1, 4); 2656 if (ret) 2657 return ret; 2658 2659 ret = usb4_switch_setup(sw); 2660 } else { 2661 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) 2662 tb_sw_warn(sw, "unknown switch vendor id %#x\n", 2663 sw->config.vendor_id); 2664 2665 if (!sw->cap_plug_events) { 2666 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); 2667 return -ENODEV; 2668 } 2669 2670 /* Enumerate the switch */ 2671 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, 2672 ROUTER_CS_1, 4); 2673 } 2674 if (ret) 2675 return ret; 2676 2677 return tb_plug_events_active(sw, true); 2678 } 2679 2680 /** 2681 * tb_switch_configuration_valid() - Set the tunneling configuration to be valid 2682 * @sw: Router to configure 2683 * 2684 * Needs to be called before any tunnels can be setup through the 2685 * router. Can be called to any router. 2686 * 2687 * Return: %0 on success, negative errno otherwise. 2688 */ 2689 int tb_switch_configuration_valid(struct tb_switch *sw) 2690 { 2691 if (tb_switch_is_usb4(sw)) 2692 return usb4_switch_configuration_valid(sw); 2693 return 0; 2694 } 2695 2696 static int tb_switch_set_uuid(struct tb_switch *sw) 2697 { 2698 bool uid = false; 2699 u32 uuid[4]; 2700 int ret; 2701 2702 if (sw->uuid) 2703 return 0; 2704 2705 if (tb_switch_is_usb4(sw)) { 2706 ret = usb4_switch_read_uid(sw, &sw->uid); 2707 if (ret) 2708 return ret; 2709 uid = true; 2710 } else { 2711 /* 2712 * The newer controllers include fused UUID as part of 2713 * link controller specific registers 2714 */ 2715 ret = tb_lc_read_uuid(sw, uuid); 2716 if (ret) { 2717 if (ret != -EINVAL) 2718 return ret; 2719 uid = true; 2720 } 2721 } 2722 2723 if (uid) { 2724 /* 2725 * ICM generates UUID based on UID and fills the upper 2726 * two words with ones. This is not strictly following 2727 * UUID format but we want to be compatible with it so 2728 * we do the same here. 2729 */ 2730 uuid[0] = sw->uid & 0xffffffff; 2731 uuid[1] = (sw->uid >> 32) & 0xffffffff; 2732 uuid[2] = 0xffffffff; 2733 uuid[3] = 0xffffffff; 2734 } 2735 2736 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); 2737 if (!sw->uuid) 2738 return -ENOMEM; 2739 return 0; 2740 } 2741 2742 static int tb_switch_add_dma_port(struct tb_switch *sw) 2743 { 2744 struct tb_nhi *nhi = sw->tb->nhi; 2745 u32 status; 2746 int ret; 2747 2748 switch (sw->generation) { 2749 case 2: 2750 /* Only root switch can be upgraded */ 2751 if (tb_route(sw)) 2752 return 0; 2753 2754 fallthrough; 2755 case 3: 2756 case 4: 2757 ret = tb_switch_set_uuid(sw); 2758 if (ret) 2759 return ret; 2760 break; 2761 2762 default: 2763 /* 2764 * DMA port is the only thing available when the switch 2765 * is in safe mode. 2766 */ 2767 if (!sw->safe_mode) 2768 return 0; 2769 break; 2770 } 2771 2772 if (sw->no_nvm_upgrade) 2773 return 0; 2774 2775 if (tb_switch_is_usb4(sw)) { 2776 ret = usb4_switch_nvm_authenticate_status(sw, &status); 2777 if (ret) 2778 return ret; 2779 2780 if (status) { 2781 tb_sw_info(sw, "switch flash authentication failed\n"); 2782 nvm_set_auth_status(sw, status); 2783 } 2784 2785 return 0; 2786 } 2787 2788 /* Root switch DMA port requires running firmware */ 2789 if (!tb_route(sw) && !tb_switch_is_icm(sw)) 2790 return 0; 2791 2792 sw->dma_port = dma_port_alloc(sw); 2793 if (!sw->dma_port) 2794 return 0; 2795 2796 /* 2797 * If there is status already set then authentication failed 2798 * when the dma_port_flash_update_auth() returned. Power cycling 2799 * is not needed (it was done already) so only thing we do here 2800 * is to unblock runtime PM of the root port. 2801 */ 2802 nvm_get_auth_status(sw, &status); 2803 if (status) { 2804 if (!tb_route(sw)) { 2805 if (nhi->ops->post_nvm_auth) 2806 nhi->ops->post_nvm_auth(nhi); 2807 } 2808 return 0; 2809 } 2810 2811 /* 2812 * Check status of the previous flash authentication. If there 2813 * is one we need to power cycle the switch in any case to make 2814 * it functional again. 2815 */ 2816 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); 2817 if (ret <= 0) 2818 return ret; 2819 2820 /* Now we can allow root port to suspend again */ 2821 if (!tb_route(sw)) { 2822 if (nhi->ops->post_nvm_auth) 2823 nhi->ops->post_nvm_auth(nhi); 2824 } 2825 2826 if (status) { 2827 tb_sw_info(sw, "switch flash authentication failed\n"); 2828 nvm_set_auth_status(sw, status); 2829 } 2830 2831 tb_sw_info(sw, "power cycling the switch now\n"); 2832 dma_port_power_cycle(sw->dma_port); 2833 2834 /* 2835 * We return error here which causes the switch adding failure. 2836 * It should appear back after power cycle is complete. 2837 */ 2838 return -ESHUTDOWN; 2839 } 2840 2841 static void tb_switch_default_link_ports(struct tb_switch *sw) 2842 { 2843 int i; 2844 2845 for (i = 1; i <= sw->config.max_port_number; i++) { 2846 struct tb_port *port = &sw->ports[i]; 2847 struct tb_port *subordinate; 2848 2849 if (!tb_port_is_null(port)) 2850 continue; 2851 2852 /* Check for the subordinate port */ 2853 if (i == sw->config.max_port_number || 2854 !tb_port_is_null(&sw->ports[i + 1])) 2855 continue; 2856 2857 /* Link them if not already done so (by DROM) */ 2858 subordinate = &sw->ports[i + 1]; 2859 if (!port->dual_link_port && !subordinate->dual_link_port) { 2860 port->link_nr = 0; 2861 port->dual_link_port = subordinate; 2862 subordinate->link_nr = 1; 2863 subordinate->dual_link_port = port; 2864 2865 tb_sw_dbg(sw, "linked ports %d <-> %d\n", 2866 port->port, subordinate->port); 2867 } 2868 } 2869 } 2870 2871 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw) 2872 { 2873 const struct tb_port *up = tb_upstream_port(sw); 2874 2875 if (!up->dual_link_port || !up->dual_link_port->remote) 2876 return false; 2877 2878 if (tb_switch_is_usb4(sw)) 2879 return usb4_switch_lane_bonding_possible(sw); 2880 return tb_lc_lane_bonding_possible(sw); 2881 } 2882 2883 static int tb_switch_update_link_attributes(struct tb_switch *sw) 2884 { 2885 struct tb_port *up; 2886 bool change = false; 2887 int ret; 2888 2889 if (!tb_route(sw) || tb_switch_is_icm(sw)) 2890 return 0; 2891 2892 up = tb_upstream_port(sw); 2893 2894 ret = tb_port_get_link_speed(up); 2895 if (ret < 0) 2896 return ret; 2897 if (sw->link_speed != ret) 2898 change = true; 2899 sw->link_speed = ret; 2900 2901 ret = tb_port_get_link_width(up); 2902 if (ret < 0) 2903 return ret; 2904 if (sw->link_width != ret) 2905 change = true; 2906 sw->link_width = ret; 2907 2908 /* Notify userspace that there is possible link attribute change */ 2909 if (device_is_registered(&sw->dev) && change) 2910 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); 2911 2912 return 0; 2913 } 2914 2915 /* Must be called after tb_switch_update_link_attributes() */ 2916 static void tb_switch_link_init(struct tb_switch *sw) 2917 { 2918 struct tb_port *up, *down; 2919 bool bonded; 2920 2921 if (!tb_route(sw) || tb_switch_is_icm(sw)) 2922 return; 2923 2924 tb_sw_dbg(sw, "current link speed %u.0 Gb/s\n", sw->link_speed); 2925 tb_sw_dbg(sw, "current link width %s\n", tb_width_name(sw->link_width)); 2926 2927 bonded = sw->link_width >= TB_LINK_WIDTH_DUAL; 2928 2929 /* 2930 * Gen 4 links come up as bonded so update the port structures 2931 * accordingly. 2932 */ 2933 up = tb_upstream_port(sw); 2934 down = tb_switch_downstream_port(sw); 2935 2936 up->bonded = bonded; 2937 if (up->dual_link_port) 2938 up->dual_link_port->bonded = bonded; 2939 tb_port_update_credits(up); 2940 2941 down->bonded = bonded; 2942 if (down->dual_link_port) 2943 down->dual_link_port->bonded = bonded; 2944 tb_port_update_credits(down); 2945 2946 if (tb_port_get_link_generation(up) < 4) 2947 return; 2948 2949 /* 2950 * Set the Gen 4 preferred link width. This is what the router 2951 * prefers when the link is brought up. If the router does not 2952 * support asymmetric link configuration, this also will be set 2953 * to TB_LINK_WIDTH_DUAL. 2954 */ 2955 sw->preferred_link_width = sw->link_width; 2956 tb_sw_dbg(sw, "preferred link width %s\n", 2957 tb_width_name(sw->preferred_link_width)); 2958 } 2959 2960 /** 2961 * tb_switch_lane_bonding_enable() - Enable lane bonding 2962 * @sw: Switch to enable lane bonding 2963 * 2964 * Connection manager can call this function to enable lane bonding of a 2965 * switch. If conditions are correct and both switches support the feature, 2966 * lanes are bonded. It is safe to call this to any switch. 2967 * 2968 * Return: %0 on success, negative errno otherwise. 2969 */ 2970 static int tb_switch_lane_bonding_enable(struct tb_switch *sw) 2971 { 2972 struct tb_port *up, *down; 2973 unsigned int width; 2974 int ret; 2975 2976 if (!tb_switch_lane_bonding_possible(sw)) 2977 return -EOPNOTSUPP; 2978 2979 up = tb_upstream_port(sw); 2980 down = tb_switch_downstream_port(sw); 2981 2982 if (!tb_port_width_supported(up, TB_LINK_WIDTH_DUAL) || 2983 !tb_port_width_supported(down, TB_LINK_WIDTH_DUAL)) 2984 return -EOPNOTSUPP; 2985 2986 /* 2987 * Both lanes need to be in CL0. Here we assume lane 0 already be in 2988 * CL0 and check just for lane 1. 2989 */ 2990 if (tb_wait_for_port(down->dual_link_port, false) <= 0) 2991 return -ENOTCONN; 2992 2993 ret = tb_port_lane_bonding_enable(up); 2994 if (ret) { 2995 tb_port_warn(up, "failed to enable lane bonding\n"); 2996 return ret; 2997 } 2998 2999 ret = tb_port_lane_bonding_enable(down); 3000 if (ret) { 3001 tb_port_warn(down, "failed to enable lane bonding\n"); 3002 tb_port_lane_bonding_disable(up); 3003 return ret; 3004 } 3005 3006 /* Any of the widths are all bonded */ 3007 width = TB_LINK_WIDTH_DUAL | TB_LINK_WIDTH_ASYM_TX | 3008 TB_LINK_WIDTH_ASYM_RX; 3009 3010 return tb_port_wait_for_link_width(down, width, 100); 3011 } 3012 3013 /** 3014 * tb_switch_lane_bonding_disable() - Disable lane bonding 3015 * @sw: Switch whose lane bonding to disable 3016 * 3017 * Disables lane bonding between @sw and parent. This can be called even 3018 * if lanes were not bonded originally. 3019 * 3020 * Return: %0 on success, negative errno otherwise. 3021 */ 3022 static int tb_switch_lane_bonding_disable(struct tb_switch *sw) 3023 { 3024 struct tb_port *up, *down; 3025 int ret; 3026 3027 up = tb_upstream_port(sw); 3028 if (!up->bonded) 3029 return 0; 3030 3031 /* 3032 * If the link is Gen 4 there is no way to switch the link to 3033 * two single lane links so avoid that here. Also don't bother 3034 * if the link is not up anymore (sw is unplugged). 3035 */ 3036 ret = tb_port_get_link_generation(up); 3037 if (ret < 0) 3038 return ret; 3039 if (ret >= 4) 3040 return -EOPNOTSUPP; 3041 3042 down = tb_switch_downstream_port(sw); 3043 tb_port_lane_bonding_disable(up); 3044 tb_port_lane_bonding_disable(down); 3045 3046 /* 3047 * It is fine if we get other errors as the router might have 3048 * been unplugged. 3049 */ 3050 return tb_port_wait_for_link_width(down, TB_LINK_WIDTH_SINGLE, 100); 3051 } 3052 3053 /* Note updating sw->link_width done in tb_switch_update_link_attributes() */ 3054 static int tb_switch_asym_enable(struct tb_switch *sw, enum tb_link_width width) 3055 { 3056 struct tb_port *up, *down, *port; 3057 enum tb_link_width down_width; 3058 int ret; 3059 3060 up = tb_upstream_port(sw); 3061 down = tb_switch_downstream_port(sw); 3062 3063 if (width == TB_LINK_WIDTH_ASYM_TX) { 3064 down_width = TB_LINK_WIDTH_ASYM_RX; 3065 port = down; 3066 } else { 3067 down_width = TB_LINK_WIDTH_ASYM_TX; 3068 port = up; 3069 } 3070 3071 ret = tb_port_set_link_width(up, width); 3072 if (ret) 3073 return ret; 3074 3075 ret = tb_port_set_link_width(down, down_width); 3076 if (ret) 3077 return ret; 3078 3079 /* 3080 * Initiate the change in the router that one of its TX lanes is 3081 * changing to RX but do so only if there is an actual change. 3082 */ 3083 if (sw->link_width != width) { 3084 ret = usb4_port_asym_start(port); 3085 if (ret) 3086 return ret; 3087 3088 ret = tb_port_wait_for_link_width(up, width, 100); 3089 if (ret) 3090 return ret; 3091 } 3092 3093 return 0; 3094 } 3095 3096 /* Note updating sw->link_width done in tb_switch_update_link_attributes() */ 3097 static int tb_switch_asym_disable(struct tb_switch *sw) 3098 { 3099 struct tb_port *up, *down; 3100 int ret; 3101 3102 up = tb_upstream_port(sw); 3103 down = tb_switch_downstream_port(sw); 3104 3105 ret = tb_port_set_link_width(up, TB_LINK_WIDTH_DUAL); 3106 if (ret) 3107 return ret; 3108 3109 ret = tb_port_set_link_width(down, TB_LINK_WIDTH_DUAL); 3110 if (ret) 3111 return ret; 3112 3113 /* 3114 * Initiate the change in the router that has three TX lanes and 3115 * is changing one of its TX lanes to RX but only if there is a 3116 * change in the link width. 3117 */ 3118 if (sw->link_width > TB_LINK_WIDTH_DUAL) { 3119 if (sw->link_width == TB_LINK_WIDTH_ASYM_TX) 3120 ret = usb4_port_asym_start(up); 3121 else 3122 ret = usb4_port_asym_start(down); 3123 if (ret) 3124 return ret; 3125 3126 ret = tb_port_wait_for_link_width(up, TB_LINK_WIDTH_DUAL, 100); 3127 if (ret) 3128 return ret; 3129 } 3130 3131 return 0; 3132 } 3133 3134 /** 3135 * tb_switch_set_link_width() - Configure router link width 3136 * @sw: Router to configure 3137 * @width: The new link width 3138 * 3139 * Set device router link width to @width from router upstream port 3140 * perspective. Supports also asymmetric links if the routers both side 3141 * of the link supports it. 3142 * 3143 * Does nothing for host router. 3144 * 3145 * Return: %0 on success, negative errno otherwise. 3146 */ 3147 int tb_switch_set_link_width(struct tb_switch *sw, enum tb_link_width width) 3148 { 3149 struct tb_port *up, *down; 3150 int ret = 0; 3151 3152 if (!tb_route(sw)) 3153 return 0; 3154 3155 up = tb_upstream_port(sw); 3156 down = tb_switch_downstream_port(sw); 3157 3158 switch (width) { 3159 case TB_LINK_WIDTH_SINGLE: 3160 ret = tb_switch_lane_bonding_disable(sw); 3161 break; 3162 3163 case TB_LINK_WIDTH_DUAL: 3164 if (sw->link_width == TB_LINK_WIDTH_ASYM_TX || 3165 sw->link_width == TB_LINK_WIDTH_ASYM_RX) { 3166 ret = tb_switch_asym_disable(sw); 3167 if (ret) 3168 break; 3169 } 3170 ret = tb_switch_lane_bonding_enable(sw); 3171 break; 3172 3173 case TB_LINK_WIDTH_ASYM_TX: 3174 case TB_LINK_WIDTH_ASYM_RX: 3175 ret = tb_switch_asym_enable(sw, width); 3176 break; 3177 } 3178 3179 switch (ret) { 3180 case 0: 3181 break; 3182 3183 case -ETIMEDOUT: 3184 tb_sw_warn(sw, "timeout changing link width\n"); 3185 return ret; 3186 3187 case -ENOTCONN: 3188 case -EOPNOTSUPP: 3189 case -ENODEV: 3190 return ret; 3191 3192 default: 3193 tb_sw_dbg(sw, "failed to change link width: %d\n", ret); 3194 return ret; 3195 } 3196 3197 tb_port_update_credits(down); 3198 tb_port_update_credits(up); 3199 3200 tb_switch_update_link_attributes(sw); 3201 3202 tb_sw_dbg(sw, "link width set to %s\n", tb_width_name(width)); 3203 return ret; 3204 } 3205 3206 /** 3207 * tb_switch_configure_link() - Set link configured 3208 * @sw: Switch whose link is configured 3209 * 3210 * Sets the link upstream from @sw configured (from both ends) so that 3211 * it will not be disconnected when the domain exits sleep. Can be 3212 * called for any switch. 3213 * 3214 * It is recommended that this is called after lane bonding is enabled. 3215 * 3216 * Return: %0 on success and negative errno otherwise. 3217 */ 3218 int tb_switch_configure_link(struct tb_switch *sw) 3219 { 3220 struct tb_port *up, *down; 3221 int ret; 3222 3223 if (!tb_route(sw) || tb_switch_is_icm(sw)) 3224 return 0; 3225 3226 up = tb_upstream_port(sw); 3227 if (tb_switch_is_usb4(up->sw)) 3228 ret = usb4_port_configure(up); 3229 else 3230 ret = tb_lc_configure_port(up); 3231 if (ret) 3232 return ret; 3233 3234 down = up->remote; 3235 if (tb_switch_is_usb4(down->sw)) 3236 return usb4_port_configure(down); 3237 return tb_lc_configure_port(down); 3238 } 3239 3240 /** 3241 * tb_switch_unconfigure_link() - Unconfigure link 3242 * @sw: Switch whose link is unconfigured 3243 * 3244 * Sets the link unconfigured so the @sw will be disconnected if the 3245 * domain exits sleep. 3246 */ 3247 void tb_switch_unconfigure_link(struct tb_switch *sw) 3248 { 3249 struct tb_port *up, *down; 3250 3251 if (!tb_route(sw) || tb_switch_is_icm(sw)) 3252 return; 3253 3254 /* 3255 * Unconfigure downstream port so that wake-on-connect can be 3256 * configured after router unplug. No need to unconfigure upstream port 3257 * since its router is unplugged. 3258 */ 3259 up = tb_upstream_port(sw); 3260 down = up->remote; 3261 if (tb_switch_is_usb4(down->sw)) 3262 usb4_port_unconfigure(down); 3263 else 3264 tb_lc_unconfigure_port(down); 3265 3266 if (sw->is_unplugged) 3267 return; 3268 3269 up = tb_upstream_port(sw); 3270 if (tb_switch_is_usb4(up->sw)) 3271 usb4_port_unconfigure(up); 3272 else 3273 tb_lc_unconfigure_port(up); 3274 } 3275 3276 static void tb_switch_credits_init(struct tb_switch *sw) 3277 { 3278 if (tb_switch_is_icm(sw)) 3279 return; 3280 if (!tb_switch_is_usb4(sw)) 3281 return; 3282 if (usb4_switch_credits_init(sw)) 3283 tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n"); 3284 } 3285 3286 static int tb_switch_port_hotplug_enable(struct tb_switch *sw) 3287 { 3288 struct tb_port *port; 3289 3290 if (tb_switch_is_icm(sw)) 3291 return 0; 3292 3293 tb_switch_for_each_port(sw, port) { 3294 int res; 3295 3296 if (!port->cap_usb4) 3297 continue; 3298 3299 res = usb4_port_hotplug_enable(port); 3300 if (res) 3301 return res; 3302 } 3303 return 0; 3304 } 3305 3306 /** 3307 * tb_switch_add() - Add a switch to the domain 3308 * @sw: Switch to add 3309 * 3310 * This is the last step in adding switch to the domain. It will read 3311 * identification information from DROM and initializes ports so that 3312 * they can be used to connect other switches. The switch will be 3313 * exposed to the userspace when this function successfully returns. To 3314 * remove and release the switch, call tb_switch_remove(). 3315 * 3316 * Return: %0 on success, negative errno otherwise. 3317 */ 3318 int tb_switch_add(struct tb_switch *sw) 3319 { 3320 int i, ret; 3321 3322 /* 3323 * Initialize DMA control port now before we read DROM. Recent 3324 * host controllers have more complete DROM on NVM that includes 3325 * vendor and model identification strings which we then expose 3326 * to the userspace. NVM can be accessed through DMA 3327 * configuration based mailbox. 3328 */ 3329 ret = tb_switch_add_dma_port(sw); 3330 if (ret) { 3331 dev_err(&sw->dev, "failed to add DMA port\n"); 3332 return ret; 3333 } 3334 3335 ret = tb_switch_nvm_init(sw); 3336 if (ret) 3337 return ret; 3338 3339 if (!sw->safe_mode) { 3340 tb_switch_credits_init(sw); 3341 3342 /* read drom */ 3343 ret = tb_drom_read(sw); 3344 if (ret) 3345 dev_warn(&sw->dev, "reading DROM failed: %d\n", ret); 3346 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); 3347 3348 ret = tb_switch_set_uuid(sw); 3349 if (ret) { 3350 dev_err(&sw->dev, "failed to set UUID\n"); 3351 return ret; 3352 } 3353 3354 for (i = 0; i <= sw->config.max_port_number; i++) { 3355 if (sw->ports[i].disabled) { 3356 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); 3357 continue; 3358 } 3359 ret = tb_init_port(&sw->ports[i]); 3360 if (ret) { 3361 dev_err(&sw->dev, "failed to initialize port %d\n", i); 3362 return ret; 3363 } 3364 } 3365 3366 tb_check_quirks(sw); 3367 3368 tb_switch_default_link_ports(sw); 3369 3370 ret = tb_switch_update_link_attributes(sw); 3371 if (ret) 3372 return ret; 3373 3374 tb_switch_link_init(sw); 3375 3376 ret = tb_switch_clx_init(sw); 3377 if (ret) 3378 return ret; 3379 3380 ret = tb_switch_tmu_init(sw); 3381 if (ret) 3382 return ret; 3383 } 3384 3385 ret = tb_switch_port_hotplug_enable(sw); 3386 if (ret) 3387 return ret; 3388 3389 ret = device_add(&sw->dev); 3390 if (ret) { 3391 dev_err(&sw->dev, "failed to add device: %d\n", ret); 3392 return ret; 3393 } 3394 3395 if (tb_route(sw)) { 3396 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", 3397 sw->vendor, sw->device); 3398 if (sw->vendor_name && sw->device_name) 3399 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, 3400 sw->device_name); 3401 } 3402 3403 ret = usb4_switch_add_ports(sw); 3404 if (ret) { 3405 dev_err(&sw->dev, "failed to add USB4 ports\n"); 3406 goto err_del; 3407 } 3408 3409 ret = tb_switch_nvm_add(sw); 3410 if (ret) { 3411 dev_err(&sw->dev, "failed to add NVM devices\n"); 3412 goto err_ports; 3413 } 3414 3415 /* 3416 * Thunderbolt routers do not generate wakeups themselves but 3417 * they forward wakeups from tunneled protocols, so enable it 3418 * here. 3419 */ 3420 device_init_wakeup(&sw->dev, true); 3421 3422 pm_runtime_set_active(&sw->dev); 3423 if (sw->rpm) { 3424 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); 3425 pm_runtime_use_autosuspend(&sw->dev); 3426 pm_runtime_mark_last_busy(&sw->dev); 3427 pm_runtime_enable(&sw->dev); 3428 pm_request_autosuspend(&sw->dev); 3429 } 3430 3431 tb_switch_debugfs_init(sw); 3432 return 0; 3433 3434 err_ports: 3435 usb4_switch_remove_ports(sw); 3436 err_del: 3437 device_del(&sw->dev); 3438 3439 return ret; 3440 } 3441 3442 /** 3443 * tb_switch_remove() - Remove and release a switch 3444 * @sw: Switch to remove 3445 * 3446 * This will remove the switch from the domain and release it after last 3447 * reference count drops to zero. If there are switches connected below 3448 * this switch, they will be removed as well. 3449 */ 3450 void tb_switch_remove(struct tb_switch *sw) 3451 { 3452 struct tb_port *port; 3453 3454 tb_switch_debugfs_remove(sw); 3455 3456 if (sw->rpm) { 3457 pm_runtime_get_sync(&sw->dev); 3458 pm_runtime_disable(&sw->dev); 3459 } 3460 3461 /* port 0 is the switch itself and never has a remote */ 3462 tb_switch_for_each_port(sw, port) { 3463 if (tb_port_has_remote(port)) { 3464 tb_switch_remove(port->remote->sw); 3465 port->remote = NULL; 3466 } else if (port->xdomain) { 3467 port->xdomain->is_unplugged = true; 3468 tb_xdomain_remove(port->xdomain); 3469 port->xdomain = NULL; 3470 } 3471 3472 /* Remove any downstream retimers */ 3473 tb_retimer_remove_all(port); 3474 } 3475 3476 if (!sw->is_unplugged) 3477 tb_plug_events_active(sw, false); 3478 3479 tb_switch_nvm_remove(sw); 3480 usb4_switch_remove_ports(sw); 3481 3482 if (tb_route(sw)) 3483 dev_info(&sw->dev, "device disconnected\n"); 3484 device_unregister(&sw->dev); 3485 } 3486 3487 /** 3488 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches 3489 * @sw: Router to mark unplugged 3490 */ 3491 void tb_sw_set_unplugged(struct tb_switch *sw) 3492 { 3493 struct tb_port *port; 3494 3495 if (sw == sw->tb->root_switch) { 3496 tb_sw_WARN(sw, "cannot unplug root switch\n"); 3497 return; 3498 } 3499 if (sw->is_unplugged) { 3500 tb_sw_WARN(sw, "is_unplugged already set\n"); 3501 return; 3502 } 3503 sw->is_unplugged = true; 3504 tb_switch_for_each_port(sw, port) { 3505 if (tb_port_has_remote(port)) 3506 tb_sw_set_unplugged(port->remote->sw); 3507 else if (port->xdomain) 3508 port->xdomain->is_unplugged = true; 3509 } 3510 } 3511 3512 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags, bool runtime) 3513 { 3514 if (flags) 3515 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); 3516 else 3517 tb_sw_dbg(sw, "disabling wakeup\n"); 3518 3519 if (tb_switch_is_usb4(sw)) 3520 return usb4_switch_set_wake(sw, flags, runtime); 3521 return tb_lc_set_wake(sw, flags); 3522 } 3523 3524 static void tb_switch_check_wakes(struct tb_switch *sw) 3525 { 3526 if (device_may_wakeup(&sw->dev)) { 3527 if (tb_switch_is_usb4(sw)) 3528 usb4_switch_check_wakes(sw); 3529 } 3530 } 3531 3532 /** 3533 * tb_switch_resume() - Resume a switch after sleep 3534 * @sw: Switch to resume 3535 * @runtime: Is this resume from runtime suspend or system sleep 3536 * 3537 * Resumes and re-enumerates router (and all its children), if still plugged 3538 * after suspend. Don't enumerate device router whose UID was changed during 3539 * suspend. If this is resume from system sleep, notifies PM core about the 3540 * wakes occurred during suspend. Disables all wakes, except USB4 wake of 3541 * upstream port for USB4 routers that shall be always enabled. 3542 * 3543 * Return: %0 on success, negative errno otherwise. 3544 */ 3545 int tb_switch_resume(struct tb_switch *sw, bool runtime) 3546 { 3547 struct tb_port *port; 3548 int err; 3549 3550 tb_sw_dbg(sw, "resuming switch\n"); 3551 3552 /* 3553 * Check for UID of the connected switches except for root 3554 * switch which we assume cannot be removed. 3555 */ 3556 if (tb_route(sw)) { 3557 u64 uid; 3558 3559 /* 3560 * Check first that we can still read the switch config 3561 * space. It may be that there is now another domain 3562 * connected. 3563 */ 3564 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); 3565 if (err < 0) { 3566 tb_sw_info(sw, "switch not present anymore\n"); 3567 return err; 3568 } 3569 3570 /* We don't have any way to confirm this was the same device */ 3571 if (!sw->uid) 3572 return -ENODEV; 3573 3574 if (tb_switch_is_usb4(sw)) 3575 err = usb4_switch_read_uid(sw, &uid); 3576 else 3577 err = tb_drom_read_uid_only(sw, &uid); 3578 if (err) { 3579 tb_sw_warn(sw, "uid read failed\n"); 3580 return err; 3581 } 3582 if (sw->uid != uid) { 3583 tb_sw_info(sw, 3584 "changed while suspended (uid %#llx -> %#llx)\n", 3585 sw->uid, uid); 3586 return -ENODEV; 3587 } 3588 } 3589 3590 err = tb_switch_configure(sw); 3591 if (err) 3592 return err; 3593 3594 if (!runtime) 3595 tb_switch_check_wakes(sw); 3596 3597 /* Disable wakes */ 3598 tb_switch_set_wake(sw, 0, true); 3599 3600 err = tb_switch_tmu_init(sw); 3601 if (err) 3602 return err; 3603 3604 /* check for surviving downstream switches */ 3605 tb_switch_for_each_port(sw, port) { 3606 if (!tb_port_is_null(port)) 3607 continue; 3608 3609 if (!tb_port_resume(port)) 3610 continue; 3611 3612 if (tb_wait_for_port(port, true) <= 0) { 3613 tb_port_warn(port, 3614 "lost during suspend, disconnecting\n"); 3615 if (tb_port_has_remote(port)) 3616 tb_sw_set_unplugged(port->remote->sw); 3617 else if (port->xdomain) 3618 port->xdomain->is_unplugged = true; 3619 } else { 3620 /* 3621 * Always unlock the port so the downstream 3622 * switch/domain is accessible. 3623 */ 3624 if (tb_port_unlock(port)) 3625 tb_port_warn(port, "failed to unlock port\n"); 3626 if (port->remote && 3627 tb_switch_resume(port->remote->sw, runtime)) { 3628 tb_port_warn(port, 3629 "lost during suspend, disconnecting\n"); 3630 tb_sw_set_unplugged(port->remote->sw); 3631 } else if (port->xdomain) { 3632 /* 3633 * If the user replaced the XDomain with 3634 * another router, this will succeed in 3635 * which case we must remove the XDomain 3636 * before adding the new router. 3637 */ 3638 err = tb_cfg_get_upstream_port(sw->tb->ctl, 3639 port->xdomain->route); 3640 if (err > 0) { 3641 tb_port_warn(port, 3642 "XDomain was disconnected\n"); 3643 port->xdomain->is_unplugged = true; 3644 } 3645 } 3646 } 3647 } 3648 return 0; 3649 } 3650 3651 /** 3652 * tb_switch_suspend() - Put a switch to sleep 3653 * @sw: Switch to suspend 3654 * @runtime: Is this runtime suspend or system sleep 3655 * 3656 * Suspends router and all its children. Enables wakes according to 3657 * value of @runtime and then sets sleep bit for the router. If @sw is 3658 * host router the domain is ready to go to sleep once this function 3659 * returns. 3660 */ 3661 void tb_switch_suspend(struct tb_switch *sw, bool runtime) 3662 { 3663 unsigned int flags = 0; 3664 struct tb_port *port; 3665 int err; 3666 3667 tb_sw_dbg(sw, "suspending switch\n"); 3668 3669 /* 3670 * Actually only needed for Titan Ridge but for simplicity can be 3671 * done for USB4 device too as CLx is re-enabled at resume. 3672 */ 3673 tb_switch_clx_disable(sw); 3674 3675 err = tb_plug_events_active(sw, false); 3676 if (err) 3677 return; 3678 3679 tb_switch_for_each_port(sw, port) { 3680 if (tb_port_has_remote(port)) 3681 tb_switch_suspend(port->remote->sw, runtime); 3682 } 3683 3684 if (runtime) { 3685 /* Trigger wake when something is plugged in/out */ 3686 flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT; 3687 flags |= TB_WAKE_ON_USB4; 3688 flags |= TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE | TB_WAKE_ON_DP; 3689 } else if (device_may_wakeup(&sw->dev)) { 3690 flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT; 3691 flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE; 3692 } 3693 3694 tb_switch_set_wake(sw, flags, runtime); 3695 3696 if (tb_switch_is_usb4(sw)) 3697 usb4_switch_set_sleep(sw); 3698 else 3699 tb_lc_set_sleep(sw); 3700 } 3701 3702 /** 3703 * tb_switch_query_dp_resource() - Query availability of DP resource 3704 * @sw: Switch whose DP resource is queried 3705 * @in: DP IN port 3706 * 3707 * Queries availability of DP resource for DP tunneling using switch 3708 * specific means. 3709 * 3710 * Return: %true if resource is available, %false otherwise. 3711 */ 3712 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) 3713 { 3714 if (tb_switch_is_usb4(sw)) 3715 return usb4_switch_query_dp_resource(sw, in); 3716 return tb_lc_dp_sink_query(sw, in); 3717 } 3718 3719 /** 3720 * tb_switch_alloc_dp_resource() - Allocate available DP resource 3721 * @sw: Switch whose DP resource is allocated 3722 * @in: DP IN port 3723 * 3724 * Allocates DP resource for DP tunneling. The resource must be 3725 * available for this to succeed (see tb_switch_query_dp_resource()). 3726 * 3727 * Return: %0 on success, negative errno otherwise. 3728 */ 3729 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) 3730 { 3731 int ret; 3732 3733 if (tb_switch_is_usb4(sw)) 3734 ret = usb4_switch_alloc_dp_resource(sw, in); 3735 else 3736 ret = tb_lc_dp_sink_alloc(sw, in); 3737 3738 if (ret) 3739 tb_sw_warn(sw, "failed to allocate DP resource for port %d\n", 3740 in->port); 3741 else 3742 tb_sw_dbg(sw, "allocated DP resource for port %d\n", in->port); 3743 3744 return ret; 3745 } 3746 3747 /** 3748 * tb_switch_dealloc_dp_resource() - De-allocate DP resource 3749 * @sw: Switch whose DP resource is de-allocated 3750 * @in: DP IN port 3751 * 3752 * De-allocates DP resource that was previously allocated for DP 3753 * tunneling. 3754 */ 3755 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) 3756 { 3757 int ret; 3758 3759 if (tb_switch_is_usb4(sw)) 3760 ret = usb4_switch_dealloc_dp_resource(sw, in); 3761 else 3762 ret = tb_lc_dp_sink_dealloc(sw, in); 3763 3764 if (ret) 3765 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", 3766 in->port); 3767 else 3768 tb_sw_dbg(sw, "released DP resource for port %d\n", in->port); 3769 } 3770 3771 struct tb_sw_lookup { 3772 struct tb *tb; 3773 u8 link; 3774 u8 depth; 3775 const uuid_t *uuid; 3776 u64 route; 3777 }; 3778 3779 static int tb_switch_match(struct device *dev, const void *data) 3780 { 3781 struct tb_switch *sw = tb_to_switch(dev); 3782 const struct tb_sw_lookup *lookup = data; 3783 3784 if (!sw) 3785 return 0; 3786 if (sw->tb != lookup->tb) 3787 return 0; 3788 3789 if (lookup->uuid) 3790 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); 3791 3792 if (lookup->route) { 3793 return sw->config.route_lo == lower_32_bits(lookup->route) && 3794 sw->config.route_hi == upper_32_bits(lookup->route); 3795 } 3796 3797 /* Root switch is matched only by depth */ 3798 if (!lookup->depth) 3799 return !sw->depth; 3800 3801 return sw->link == lookup->link && sw->depth == lookup->depth; 3802 } 3803 3804 /** 3805 * tb_switch_find_by_link_depth() - Find switch by link and depth 3806 * @tb: Domain the switch belongs 3807 * @link: Link number the switch is connected 3808 * @depth: Depth of the switch in link 3809 * 3810 * Returned switch has reference count increased so the caller needs to 3811 * call tb_switch_put() when done with the switch. 3812 * 3813 * Return: Pointer to &struct tb_switch, %NULL if not found. 3814 */ 3815 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth) 3816 { 3817 struct tb_sw_lookup lookup; 3818 struct device *dev; 3819 3820 memset(&lookup, 0, sizeof(lookup)); 3821 lookup.tb = tb; 3822 lookup.link = link; 3823 lookup.depth = depth; 3824 3825 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); 3826 if (dev) 3827 return tb_to_switch(dev); 3828 3829 return NULL; 3830 } 3831 3832 /** 3833 * tb_switch_find_by_uuid() - Find switch by UUID 3834 * @tb: Domain the switch belongs 3835 * @uuid: UUID to look for 3836 * 3837 * Returned switch has reference count increased so the caller needs to 3838 * call tb_switch_put() when done with the switch. 3839 * 3840 * Return: Pointer to &struct tb_switch, %NULL if not found. 3841 */ 3842 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid) 3843 { 3844 struct tb_sw_lookup lookup; 3845 struct device *dev; 3846 3847 memset(&lookup, 0, sizeof(lookup)); 3848 lookup.tb = tb; 3849 lookup.uuid = uuid; 3850 3851 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); 3852 if (dev) 3853 return tb_to_switch(dev); 3854 3855 return NULL; 3856 } 3857 3858 /** 3859 * tb_switch_find_by_route() - Find switch by route string 3860 * @tb: Domain the switch belongs 3861 * @route: Route string to look for 3862 * 3863 * Returned switch has reference count increased so the caller needs to 3864 * call tb_switch_put() when done with the switch. 3865 * 3866 * Return: Pointer to &struct tb_switch, %NULL if not found. 3867 */ 3868 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route) 3869 { 3870 struct tb_sw_lookup lookup; 3871 struct device *dev; 3872 3873 if (!route) 3874 return tb_switch_get(tb->root_switch); 3875 3876 memset(&lookup, 0, sizeof(lookup)); 3877 lookup.tb = tb; 3878 lookup.route = route; 3879 3880 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); 3881 if (dev) 3882 return tb_to_switch(dev); 3883 3884 return NULL; 3885 } 3886 3887 /** 3888 * tb_switch_find_port() - return the first port of @type on @sw or NULL 3889 * @sw: Switch to find the port from 3890 * @type: Port type to look for 3891 * 3892 * Return: Pointer to &struct tb_port, %NULL if not found. 3893 */ 3894 struct tb_port *tb_switch_find_port(struct tb_switch *sw, 3895 enum tb_port_type type) 3896 { 3897 struct tb_port *port; 3898 3899 tb_switch_for_each_port(sw, port) { 3900 if (port->config.type == type) 3901 return port; 3902 } 3903 3904 return NULL; 3905 } 3906 3907 /* 3908 * Can be used for read/write a specified PCIe bridge for any Thunderbolt 3 3909 * device. For now used only for Titan Ridge. 3910 */ 3911 static int tb_switch_pcie_bridge_write(struct tb_switch *sw, unsigned int bridge, 3912 unsigned int pcie_offset, u32 value) 3913 { 3914 u32 offset, command, val; 3915 int ret; 3916 3917 if (sw->generation != 3) 3918 return -EOPNOTSUPP; 3919 3920 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_WR_DATA; 3921 ret = tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1); 3922 if (ret) 3923 return ret; 3924 3925 command = pcie_offset & TB_PLUG_EVENTS_PCIE_CMD_DW_OFFSET_MASK; 3926 command |= BIT(bridge + TB_PLUG_EVENTS_PCIE_CMD_BR_SHIFT); 3927 command |= TB_PLUG_EVENTS_PCIE_CMD_RD_WR_MASK; 3928 command |= TB_PLUG_EVENTS_PCIE_CMD_COMMAND_VAL 3929 << TB_PLUG_EVENTS_PCIE_CMD_COMMAND_SHIFT; 3930 command |= TB_PLUG_EVENTS_PCIE_CMD_REQ_ACK_MASK; 3931 3932 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_CMD; 3933 3934 ret = tb_sw_write(sw, &command, TB_CFG_SWITCH, offset, 1); 3935 if (ret) 3936 return ret; 3937 3938 ret = tb_switch_wait_for_bit(sw, offset, 3939 TB_PLUG_EVENTS_PCIE_CMD_REQ_ACK_MASK, 0, 100); 3940 if (ret) 3941 return ret; 3942 3943 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1); 3944 if (ret) 3945 return ret; 3946 3947 if (val & TB_PLUG_EVENTS_PCIE_CMD_TIMEOUT_MASK) 3948 return -ETIMEDOUT; 3949 3950 return 0; 3951 } 3952 3953 /** 3954 * tb_switch_pcie_l1_enable() - Enable PCIe link to enter L1 state 3955 * @sw: Router to enable PCIe L1 3956 * 3957 * For Titan Ridge switch to enter CLx state, its PCIe bridges shall enable 3958 * entry to PCIe L1 state. Shall be called after the upstream PCIe tunnel 3959 * was configured. Due to Intel platforms limitation, shall be called only 3960 * for first hop switch. 3961 * 3962 * Return: %0 on success, negative errno otherwise. 3963 */ 3964 int tb_switch_pcie_l1_enable(struct tb_switch *sw) 3965 { 3966 struct tb_switch *parent = tb_switch_parent(sw); 3967 int ret; 3968 3969 if (!tb_route(sw)) 3970 return 0; 3971 3972 if (!tb_switch_is_titan_ridge(sw)) 3973 return 0; 3974 3975 /* Enable PCIe L1 enable only for first hop router (depth = 1) */ 3976 if (tb_route(parent)) 3977 return 0; 3978 3979 /* Write to downstream PCIe bridge #5 aka Dn4 */ 3980 ret = tb_switch_pcie_bridge_write(sw, 5, 0x143, 0x0c7806b1); 3981 if (ret) 3982 return ret; 3983 3984 /* Write to Upstream PCIe bridge #0 aka Up0 */ 3985 return tb_switch_pcie_bridge_write(sw, 0, 0x143, 0x0c5806b1); 3986 } 3987 3988 /** 3989 * tb_switch_xhci_connect() - Connect internal xHCI 3990 * @sw: Router whose xHCI to connect 3991 * 3992 * Can be called to any router. For Alpine Ridge and Titan Ridge 3993 * performs special flows that bring the xHCI functional for any device 3994 * connected to the type-C port. Call only after PCIe tunnel has been 3995 * established. The function only does the connect if not done already 3996 * so can be called several times for the same router. 3997 * 3998 * Return: %0 on success, negative errno otherwise. 3999 */ 4000 int tb_switch_xhci_connect(struct tb_switch *sw) 4001 { 4002 struct tb_port *port1, *port3; 4003 int ret; 4004 4005 if (sw->generation != 3) 4006 return 0; 4007 4008 port1 = &sw->ports[1]; 4009 port3 = &sw->ports[3]; 4010 4011 if (tb_switch_is_alpine_ridge(sw)) { 4012 bool usb_port1, usb_port3, xhci_port1, xhci_port3; 4013 4014 usb_port1 = tb_lc_is_usb_plugged(port1); 4015 usb_port3 = tb_lc_is_usb_plugged(port3); 4016 xhci_port1 = tb_lc_is_xhci_connected(port1); 4017 xhci_port3 = tb_lc_is_xhci_connected(port3); 4018 4019 /* Figure out correct USB port to connect */ 4020 if (usb_port1 && !xhci_port1) { 4021 ret = tb_lc_xhci_connect(port1); 4022 if (ret) 4023 return ret; 4024 } 4025 if (usb_port3 && !xhci_port3) 4026 return tb_lc_xhci_connect(port3); 4027 } else if (tb_switch_is_titan_ridge(sw)) { 4028 ret = tb_lc_xhci_connect(port1); 4029 if (ret) 4030 return ret; 4031 return tb_lc_xhci_connect(port3); 4032 } 4033 4034 return 0; 4035 } 4036 4037 /** 4038 * tb_switch_xhci_disconnect() - Disconnect internal xHCI 4039 * @sw: Router whose xHCI to disconnect 4040 * 4041 * The opposite of tb_switch_xhci_connect(). Disconnects xHCI on both 4042 * ports. 4043 */ 4044 void tb_switch_xhci_disconnect(struct tb_switch *sw) 4045 { 4046 if (sw->generation == 3) { 4047 struct tb_port *port1 = &sw->ports[1]; 4048 struct tb_port *port3 = &sw->ports[3]; 4049 4050 tb_lc_xhci_disconnect(port1); 4051 tb_port_dbg(port1, "disconnected xHCI\n"); 4052 tb_lc_xhci_disconnect(port3); 4053 tb_port_dbg(port3, "disconnected xHCI\n"); 4054 } 4055 } 4056