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