1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Internal Thunderbolt Connection Manager. This is a firmware running on 4 * the Thunderbolt host controller performing most of the low-level 5 * handling. 6 * 7 * Copyright (C) 2017, Intel Corporation 8 * Authors: Michael Jamet <michael.jamet@intel.com> 9 * Mika Westerberg <mika.westerberg@linux.intel.com> 10 */ 11 12 #include <linux/delay.h> 13 #include <linux/mutex.h> 14 #include <linux/moduleparam.h> 15 #include <linux/pci.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/platform_data/x86/apple.h> 18 #include <linux/sizes.h> 19 #include <linux/slab.h> 20 #include <linux/workqueue.h> 21 22 #include "ctl.h" 23 #include "nhi_regs.h" 24 #include "tb.h" 25 26 #define PCIE2CIO_CMD 0x30 27 #define PCIE2CIO_CMD_TIMEOUT BIT(31) 28 #define PCIE2CIO_CMD_START BIT(30) 29 #define PCIE2CIO_CMD_WRITE BIT(21) 30 #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19) 31 #define PCIE2CIO_CMD_CS_SHIFT 19 32 #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13) 33 #define PCIE2CIO_CMD_PORT_SHIFT 13 34 35 #define PCIE2CIO_WRDATA 0x34 36 #define PCIE2CIO_RDDATA 0x38 37 38 #define PHY_PORT_CS1 0x37 39 #define PHY_PORT_CS1_LINK_DISABLE BIT(14) 40 #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26) 41 #define PHY_PORT_CS1_LINK_STATE_SHIFT 26 42 43 #define ICM_TIMEOUT 5000 /* ms */ 44 #define ICM_APPROVE_TIMEOUT 10000 /* ms */ 45 #define ICM_MAX_LINK 4 46 47 static bool start_icm; 48 module_param(start_icm, bool, 0444); 49 MODULE_PARM_DESC(start_icm, "start ICM firmware if it is not running (default: false)"); 50 51 /** 52 * struct icm - Internal connection manager private data 53 * @request_lock: Makes sure only one message is send to ICM at time 54 * @rescan_work: Work used to rescan the surviving switches after resume 55 * @upstream_port: Pointer to the PCIe upstream port this host 56 * controller is connected. This is only set for systems 57 * where ICM needs to be started manually 58 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides 59 * (only set when @upstream_port is not %NULL) 60 * @safe_mode: ICM is in safe mode 61 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported) 62 * @rpm: Does the controller support runtime PM (RTD3) 63 * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller 64 * @veto: Is RTD3 veto in effect 65 * @is_supported: Checks if we can support ICM on this controller 66 * @cio_reset: Trigger CIO reset 67 * @get_mode: Read and return the ICM firmware mode (optional) 68 * @get_route: Find a route string for given switch 69 * @save_devices: Ask ICM to save devices to ACL when suspending (optional) 70 * @driver_ready: Send driver ready message to ICM 71 * @set_uuid: Set UUID for the root switch (optional) 72 * @device_connected: Handle device connected ICM message 73 * @device_disconnected: Handle device disconnected ICM message 74 * @xdomain_connected - Handle XDomain connected ICM message 75 * @xdomain_disconnected - Handle XDomain disconnected ICM message 76 * @rtd3_veto: Handle RTD3 veto notification ICM message 77 */ 78 struct icm { 79 struct mutex request_lock; 80 struct delayed_work rescan_work; 81 struct pci_dev *upstream_port; 82 size_t max_boot_acl; 83 int vnd_cap; 84 bool safe_mode; 85 bool rpm; 86 bool can_upgrade_nvm; 87 bool veto; 88 bool (*is_supported)(struct tb *tb); 89 int (*cio_reset)(struct tb *tb); 90 int (*get_mode)(struct tb *tb); 91 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route); 92 void (*save_devices)(struct tb *tb); 93 int (*driver_ready)(struct tb *tb, 94 enum tb_security_level *security_level, 95 size_t *nboot_acl, bool *rpm); 96 void (*set_uuid)(struct tb *tb); 97 void (*device_connected)(struct tb *tb, 98 const struct icm_pkg_header *hdr); 99 void (*device_disconnected)(struct tb *tb, 100 const struct icm_pkg_header *hdr); 101 void (*xdomain_connected)(struct tb *tb, 102 const struct icm_pkg_header *hdr); 103 void (*xdomain_disconnected)(struct tb *tb, 104 const struct icm_pkg_header *hdr); 105 void (*rtd3_veto)(struct tb *tb, const struct icm_pkg_header *hdr); 106 }; 107 108 struct icm_notification { 109 struct work_struct work; 110 struct icm_pkg_header *pkg; 111 struct tb *tb; 112 }; 113 114 struct ep_name_entry { 115 u8 len; 116 u8 type; 117 u8 data[]; 118 }; 119 120 #define EP_NAME_INTEL_VSS 0x10 121 122 /* Intel Vendor specific structure */ 123 struct intel_vss { 124 u16 vendor; 125 u16 model; 126 u8 mc; 127 u8 flags; 128 u16 pci_devid; 129 u32 nvm_version; 130 }; 131 132 #define INTEL_VSS_FLAGS_RTD3 BIT(0) 133 134 static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size) 135 { 136 const void *end = ep_name + size; 137 138 while (ep_name < end) { 139 const struct ep_name_entry *ep = ep_name; 140 141 if (!ep->len) 142 break; 143 if (ep_name + ep->len > end) 144 break; 145 146 if (ep->type == EP_NAME_INTEL_VSS) 147 return (const struct intel_vss *)ep->data; 148 149 ep_name += ep->len; 150 } 151 152 return NULL; 153 } 154 155 static bool intel_vss_is_rtd3(const void *ep_name, size_t size) 156 { 157 const struct intel_vss *vss; 158 159 vss = parse_intel_vss(ep_name, size); 160 if (vss) 161 return !!(vss->flags & INTEL_VSS_FLAGS_RTD3); 162 163 return false; 164 } 165 166 static inline struct tb *icm_to_tb(struct icm *icm) 167 { 168 return ((void *)icm - sizeof(struct tb)); 169 } 170 171 static inline u8 phy_port_from_route(u64 route, u8 depth) 172 { 173 u8 link; 174 175 link = depth ? route >> ((depth - 1) * 8) : route; 176 return tb_phy_port_from_link(link); 177 } 178 179 static inline u8 dual_link_from_link(u8 link) 180 { 181 return link ? ((link - 1) ^ 0x01) + 1 : 0; 182 } 183 184 static inline u64 get_route(u32 route_hi, u32 route_lo) 185 { 186 return (u64)route_hi << 32 | route_lo; 187 } 188 189 static inline u64 get_parent_route(u64 route) 190 { 191 int depth = tb_route_length(route); 192 return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0; 193 } 194 195 static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec) 196 { 197 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec); 198 u32 cmd; 199 200 do { 201 pci_read_config_dword(icm->upstream_port, 202 icm->vnd_cap + PCIE2CIO_CMD, &cmd); 203 if (!(cmd & PCIE2CIO_CMD_START)) { 204 if (cmd & PCIE2CIO_CMD_TIMEOUT) 205 break; 206 return 0; 207 } 208 209 msleep(50); 210 } while (time_before(jiffies, end)); 211 212 return -ETIMEDOUT; 213 } 214 215 static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs, 216 unsigned int port, unsigned int index, u32 *data) 217 { 218 struct pci_dev *pdev = icm->upstream_port; 219 int ret, vnd_cap = icm->vnd_cap; 220 u32 cmd; 221 222 cmd = index; 223 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 224 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 225 cmd |= PCIE2CIO_CMD_START; 226 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 227 228 ret = pci2cio_wait_completion(icm, 5000); 229 if (ret) 230 return ret; 231 232 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data); 233 return 0; 234 } 235 236 static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs, 237 unsigned int port, unsigned int index, u32 data) 238 { 239 struct pci_dev *pdev = icm->upstream_port; 240 int vnd_cap = icm->vnd_cap; 241 u32 cmd; 242 243 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data); 244 245 cmd = index; 246 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 247 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 248 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START; 249 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 250 251 return pci2cio_wait_completion(icm, 5000); 252 } 253 254 static bool icm_match(const struct tb_cfg_request *req, 255 const struct ctl_pkg *pkg) 256 { 257 const struct icm_pkg_header *res_hdr = pkg->buffer; 258 const struct icm_pkg_header *req_hdr = req->request; 259 260 if (pkg->frame.eof != req->response_type) 261 return false; 262 if (res_hdr->code != req_hdr->code) 263 return false; 264 265 return true; 266 } 267 268 static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg) 269 { 270 const struct icm_pkg_header *hdr = pkg->buffer; 271 272 if (hdr->packet_id < req->npackets) { 273 size_t offset = hdr->packet_id * req->response_size; 274 275 memcpy(req->response + offset, pkg->buffer, req->response_size); 276 } 277 278 return hdr->packet_id == hdr->total_packets - 1; 279 } 280 281 static int icm_request(struct tb *tb, const void *request, size_t request_size, 282 void *response, size_t response_size, size_t npackets, 283 unsigned int timeout_msec) 284 { 285 struct icm *icm = tb_priv(tb); 286 int retries = 3; 287 288 do { 289 struct tb_cfg_request *req; 290 struct tb_cfg_result res; 291 292 req = tb_cfg_request_alloc(); 293 if (!req) 294 return -ENOMEM; 295 296 req->match = icm_match; 297 req->copy = icm_copy; 298 req->request = request; 299 req->request_size = request_size; 300 req->request_type = TB_CFG_PKG_ICM_CMD; 301 req->response = response; 302 req->npackets = npackets; 303 req->response_size = response_size; 304 req->response_type = TB_CFG_PKG_ICM_RESP; 305 306 mutex_lock(&icm->request_lock); 307 res = tb_cfg_request_sync(tb->ctl, req, timeout_msec); 308 mutex_unlock(&icm->request_lock); 309 310 tb_cfg_request_put(req); 311 312 if (res.err != -ETIMEDOUT) 313 return res.err == 1 ? -EIO : res.err; 314 315 usleep_range(20, 50); 316 } while (retries--); 317 318 return -ETIMEDOUT; 319 } 320 321 /* 322 * If rescan is queued to run (we are resuming), postpone it to give the 323 * firmware some more time to send device connected notifications for next 324 * devices in the chain. 325 */ 326 static void icm_postpone_rescan(struct tb *tb) 327 { 328 struct icm *icm = tb_priv(tb); 329 330 if (delayed_work_pending(&icm->rescan_work)) 331 mod_delayed_work(tb->wq, &icm->rescan_work, 332 msecs_to_jiffies(500)); 333 } 334 335 static void icm_veto_begin(struct tb *tb) 336 { 337 struct icm *icm = tb_priv(tb); 338 339 if (!icm->veto) { 340 icm->veto = true; 341 /* Keep the domain powered while veto is in effect */ 342 pm_runtime_get(&tb->dev); 343 } 344 } 345 346 static void icm_veto_end(struct tb *tb) 347 { 348 struct icm *icm = tb_priv(tb); 349 350 if (icm->veto) { 351 icm->veto = false; 352 /* Allow the domain suspend now */ 353 pm_runtime_mark_last_busy(&tb->dev); 354 pm_runtime_put_autosuspend(&tb->dev); 355 } 356 } 357 358 static bool icm_firmware_running(const struct tb_nhi *nhi) 359 { 360 u32 val; 361 362 val = ioread32(nhi->iobase + REG_FW_STS); 363 return !!(val & REG_FW_STS_ICM_EN); 364 } 365 366 static bool icm_fr_is_supported(struct tb *tb) 367 { 368 return !x86_apple_machine; 369 } 370 371 static inline int icm_fr_get_switch_index(u32 port) 372 { 373 int index; 374 375 if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT) 376 return 0; 377 378 index = port >> ICM_PORT_INDEX_SHIFT; 379 return index != 0xff ? index : 0; 380 } 381 382 static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route) 383 { 384 struct icm_fr_pkg_get_topology_response *switches, *sw; 385 struct icm_fr_pkg_get_topology request = { 386 .hdr = { .code = ICM_GET_TOPOLOGY }, 387 }; 388 size_t npackets = ICM_GET_TOPOLOGY_PACKETS; 389 int ret, index; 390 u8 i; 391 392 switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL); 393 if (!switches) 394 return -ENOMEM; 395 396 ret = icm_request(tb, &request, sizeof(request), switches, 397 sizeof(*switches), npackets, ICM_TIMEOUT); 398 if (ret) 399 goto err_free; 400 401 sw = &switches[0]; 402 index = icm_fr_get_switch_index(sw->ports[link]); 403 if (!index) { 404 ret = -ENODEV; 405 goto err_free; 406 } 407 408 sw = &switches[index]; 409 for (i = 1; i < depth; i++) { 410 unsigned int j; 411 412 if (!(sw->first_data & ICM_SWITCH_USED)) { 413 ret = -ENODEV; 414 goto err_free; 415 } 416 417 for (j = 0; j < ARRAY_SIZE(sw->ports); j++) { 418 index = icm_fr_get_switch_index(sw->ports[j]); 419 if (index > sw->switch_index) { 420 sw = &switches[index]; 421 break; 422 } 423 } 424 } 425 426 *route = get_route(sw->route_hi, sw->route_lo); 427 428 err_free: 429 kfree(switches); 430 return ret; 431 } 432 433 static void icm_fr_save_devices(struct tb *tb) 434 { 435 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0); 436 } 437 438 static int 439 icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level, 440 size_t *nboot_acl, bool *rpm) 441 { 442 struct icm_fr_pkg_driver_ready_response reply; 443 struct icm_pkg_driver_ready request = { 444 .hdr.code = ICM_DRIVER_READY, 445 }; 446 int ret; 447 448 memset(&reply, 0, sizeof(reply)); 449 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 450 1, ICM_TIMEOUT); 451 if (ret) 452 return ret; 453 454 if (security_level) 455 *security_level = reply.security_level & ICM_FR_SLEVEL_MASK; 456 457 return 0; 458 } 459 460 static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw) 461 { 462 struct icm_fr_pkg_approve_device request; 463 struct icm_fr_pkg_approve_device reply; 464 int ret; 465 466 memset(&request, 0, sizeof(request)); 467 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 468 request.hdr.code = ICM_APPROVE_DEVICE; 469 request.connection_id = sw->connection_id; 470 request.connection_key = sw->connection_key; 471 472 memset(&reply, 0, sizeof(reply)); 473 /* Use larger timeout as establishing tunnels can take some time */ 474 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 475 1, ICM_APPROVE_TIMEOUT); 476 if (ret) 477 return ret; 478 479 if (reply.hdr.flags & ICM_FLAGS_ERROR) { 480 tb_warn(tb, "PCIe tunnel creation failed\n"); 481 return -EIO; 482 } 483 484 return 0; 485 } 486 487 static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw) 488 { 489 struct icm_fr_pkg_add_device_key request; 490 struct icm_fr_pkg_add_device_key_response reply; 491 int ret; 492 493 memset(&request, 0, sizeof(request)); 494 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 495 request.hdr.code = ICM_ADD_DEVICE_KEY; 496 request.connection_id = sw->connection_id; 497 request.connection_key = sw->connection_key; 498 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE); 499 500 memset(&reply, 0, sizeof(reply)); 501 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 502 1, ICM_TIMEOUT); 503 if (ret) 504 return ret; 505 506 if (reply.hdr.flags & ICM_FLAGS_ERROR) { 507 tb_warn(tb, "Adding key to switch failed\n"); 508 return -EIO; 509 } 510 511 return 0; 512 } 513 514 static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw, 515 const u8 *challenge, u8 *response) 516 { 517 struct icm_fr_pkg_challenge_device request; 518 struct icm_fr_pkg_challenge_device_response reply; 519 int ret; 520 521 memset(&request, 0, sizeof(request)); 522 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 523 request.hdr.code = ICM_CHALLENGE_DEVICE; 524 request.connection_id = sw->connection_id; 525 request.connection_key = sw->connection_key; 526 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE); 527 528 memset(&reply, 0, sizeof(reply)); 529 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 530 1, ICM_TIMEOUT); 531 if (ret) 532 return ret; 533 534 if (reply.hdr.flags & ICM_FLAGS_ERROR) 535 return -EKEYREJECTED; 536 if (reply.hdr.flags & ICM_FLAGS_NO_KEY) 537 return -ENOKEY; 538 539 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE); 540 541 return 0; 542 } 543 544 static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) 545 { 546 struct icm_fr_pkg_approve_xdomain_response reply; 547 struct icm_fr_pkg_approve_xdomain request; 548 int ret; 549 550 memset(&request, 0, sizeof(request)); 551 request.hdr.code = ICM_APPROVE_XDOMAIN; 552 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link; 553 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); 554 555 request.transmit_path = xd->transmit_path; 556 request.transmit_ring = xd->transmit_ring; 557 request.receive_path = xd->receive_path; 558 request.receive_ring = xd->receive_ring; 559 560 memset(&reply, 0, sizeof(reply)); 561 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 562 1, ICM_TIMEOUT); 563 if (ret) 564 return ret; 565 566 if (reply.hdr.flags & ICM_FLAGS_ERROR) 567 return -EIO; 568 569 return 0; 570 } 571 572 static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) 573 { 574 u8 phy_port; 575 u8 cmd; 576 577 phy_port = tb_phy_port_from_link(xd->link); 578 if (phy_port == 0) 579 cmd = NHI_MAILBOX_DISCONNECT_PA; 580 else 581 cmd = NHI_MAILBOX_DISCONNECT_PB; 582 583 nhi_mailbox_cmd(tb->nhi, cmd, 1); 584 usleep_range(10, 50); 585 nhi_mailbox_cmd(tb->nhi, cmd, 2); 586 return 0; 587 } 588 589 static struct tb_switch *alloc_switch(struct tb_switch *parent_sw, u64 route, 590 const uuid_t *uuid) 591 { 592 struct tb *tb = parent_sw->tb; 593 struct tb_switch *sw; 594 595 sw = tb_switch_alloc(tb, &parent_sw->dev, route); 596 if (IS_ERR(sw)) { 597 tb_warn(tb, "failed to allocate switch at %llx\n", route); 598 return sw; 599 } 600 601 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL); 602 if (!sw->uuid) { 603 tb_switch_put(sw); 604 return ERR_PTR(-ENOMEM); 605 } 606 607 init_completion(&sw->rpm_complete); 608 return sw; 609 } 610 611 static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw) 612 { 613 u64 route = tb_route(sw); 614 int ret; 615 616 /* Link the two switches now */ 617 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); 618 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw); 619 620 ret = tb_switch_add(sw); 621 if (ret) 622 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 623 624 return ret; 625 } 626 627 static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw, 628 u64 route, u8 connection_id, u8 connection_key, 629 u8 link, u8 depth, bool boot) 630 { 631 /* Disconnect from parent */ 632 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 633 /* Re-connect via updated port*/ 634 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); 635 636 /* Update with the new addressing information */ 637 sw->config.route_hi = upper_32_bits(route); 638 sw->config.route_lo = lower_32_bits(route); 639 sw->connection_id = connection_id; 640 sw->connection_key = connection_key; 641 sw->link = link; 642 sw->depth = depth; 643 sw->boot = boot; 644 645 /* This switch still exists */ 646 sw->is_unplugged = false; 647 648 /* Runtime resume is now complete */ 649 complete(&sw->rpm_complete); 650 } 651 652 static void remove_switch(struct tb_switch *sw) 653 { 654 struct tb_switch *parent_sw; 655 656 parent_sw = tb_to_switch(sw->dev.parent); 657 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 658 tb_switch_remove(sw); 659 } 660 661 static void add_xdomain(struct tb_switch *sw, u64 route, 662 const uuid_t *local_uuid, const uuid_t *remote_uuid, 663 u8 link, u8 depth) 664 { 665 struct tb_xdomain *xd; 666 667 pm_runtime_get_sync(&sw->dev); 668 669 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid); 670 if (!xd) 671 goto out; 672 673 xd->link = link; 674 xd->depth = depth; 675 676 tb_port_at(route, sw)->xdomain = xd; 677 678 tb_xdomain_add(xd); 679 680 out: 681 pm_runtime_mark_last_busy(&sw->dev); 682 pm_runtime_put_autosuspend(&sw->dev); 683 } 684 685 static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link) 686 { 687 xd->link = link; 688 xd->route = route; 689 xd->is_unplugged = false; 690 } 691 692 static void remove_xdomain(struct tb_xdomain *xd) 693 { 694 struct tb_switch *sw; 695 696 sw = tb_to_switch(xd->dev.parent); 697 tb_port_at(xd->route, sw)->xdomain = NULL; 698 tb_xdomain_remove(xd); 699 } 700 701 static void 702 icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) 703 { 704 const struct icm_fr_event_device_connected *pkg = 705 (const struct icm_fr_event_device_connected *)hdr; 706 enum tb_security_level security_level; 707 struct tb_switch *sw, *parent_sw; 708 bool boot, dual_lane, speed_gen3; 709 struct icm *icm = tb_priv(tb); 710 bool authorized = false; 711 struct tb_xdomain *xd; 712 u8 link, depth; 713 u64 route; 714 int ret; 715 716 icm_postpone_rescan(tb); 717 718 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; 719 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 720 ICM_LINK_INFO_DEPTH_SHIFT; 721 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED; 722 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> 723 ICM_FLAGS_SLEVEL_SHIFT; 724 boot = pkg->link_info & ICM_LINK_INFO_BOOT; 725 dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE; 726 speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3; 727 728 if (pkg->link_info & ICM_LINK_INFO_REJECTED) { 729 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n", 730 link, depth); 731 return; 732 } 733 734 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid); 735 if (sw) { 736 u8 phy_port, sw_phy_port; 737 738 parent_sw = tb_to_switch(sw->dev.parent); 739 sw_phy_port = tb_phy_port_from_link(sw->link); 740 phy_port = tb_phy_port_from_link(link); 741 742 /* 743 * On resume ICM will send us connected events for the 744 * devices that still are present. However, that 745 * information might have changed for example by the 746 * fact that a switch on a dual-link connection might 747 * have been enumerated using the other link now. Make 748 * sure our book keeping matches that. 749 */ 750 if (sw->depth == depth && sw_phy_port == phy_port && 751 !!sw->authorized == authorized) { 752 /* 753 * It was enumerated through another link so update 754 * route string accordingly. 755 */ 756 if (sw->link != link) { 757 ret = icm->get_route(tb, link, depth, &route); 758 if (ret) { 759 tb_err(tb, "failed to update route string for switch at %u.%u\n", 760 link, depth); 761 tb_switch_put(sw); 762 return; 763 } 764 } else { 765 route = tb_route(sw); 766 } 767 768 update_switch(parent_sw, sw, route, pkg->connection_id, 769 pkg->connection_key, link, depth, boot); 770 tb_switch_put(sw); 771 return; 772 } 773 774 /* 775 * User connected the same switch to another physical 776 * port or to another part of the topology. Remove the 777 * existing switch now before adding the new one. 778 */ 779 remove_switch(sw); 780 tb_switch_put(sw); 781 } 782 783 /* 784 * If the switch was not found by UUID, look for a switch on 785 * same physical port (taking possible link aggregation into 786 * account) and depth. If we found one it is definitely a stale 787 * one so remove it first. 788 */ 789 sw = tb_switch_find_by_link_depth(tb, link, depth); 790 if (!sw) { 791 u8 dual_link; 792 793 dual_link = dual_link_from_link(link); 794 if (dual_link) 795 sw = tb_switch_find_by_link_depth(tb, dual_link, depth); 796 } 797 if (sw) { 798 remove_switch(sw); 799 tb_switch_put(sw); 800 } 801 802 /* Remove existing XDomain connection if found */ 803 xd = tb_xdomain_find_by_link_depth(tb, link, depth); 804 if (xd) { 805 remove_xdomain(xd); 806 tb_xdomain_put(xd); 807 } 808 809 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1); 810 if (!parent_sw) { 811 tb_err(tb, "failed to find parent switch for %u.%u\n", 812 link, depth); 813 return; 814 } 815 816 ret = icm->get_route(tb, link, depth, &route); 817 if (ret) { 818 tb_err(tb, "failed to find route string for switch at %u.%u\n", 819 link, depth); 820 tb_switch_put(parent_sw); 821 return; 822 } 823 824 pm_runtime_get_sync(&parent_sw->dev); 825 826 sw = alloc_switch(parent_sw, route, &pkg->ep_uuid); 827 if (!IS_ERR(sw)) { 828 sw->connection_id = pkg->connection_id; 829 sw->connection_key = pkg->connection_key; 830 sw->link = link; 831 sw->depth = depth; 832 sw->authorized = authorized; 833 sw->security_level = security_level; 834 sw->boot = boot; 835 sw->link_speed = speed_gen3 ? 20 : 10; 836 sw->link_width = dual_lane ? 2 : 1; 837 sw->rpm = intel_vss_is_rtd3(pkg->ep_name, sizeof(pkg->ep_name)); 838 839 if (add_switch(parent_sw, sw)) 840 tb_switch_put(sw); 841 } 842 843 pm_runtime_mark_last_busy(&parent_sw->dev); 844 pm_runtime_put_autosuspend(&parent_sw->dev); 845 846 tb_switch_put(parent_sw); 847 } 848 849 static void 850 icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) 851 { 852 const struct icm_fr_event_device_disconnected *pkg = 853 (const struct icm_fr_event_device_disconnected *)hdr; 854 struct tb_switch *sw; 855 u8 link, depth; 856 857 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; 858 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 859 ICM_LINK_INFO_DEPTH_SHIFT; 860 861 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) { 862 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth); 863 return; 864 } 865 866 sw = tb_switch_find_by_link_depth(tb, link, depth); 867 if (!sw) { 868 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link, 869 depth); 870 return; 871 } 872 873 remove_switch(sw); 874 tb_switch_put(sw); 875 } 876 877 static void 878 icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr) 879 { 880 const struct icm_fr_event_xdomain_connected *pkg = 881 (const struct icm_fr_event_xdomain_connected *)hdr; 882 struct tb_xdomain *xd; 883 struct tb_switch *sw; 884 u8 link, depth; 885 u64 route; 886 887 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; 888 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 889 ICM_LINK_INFO_DEPTH_SHIFT; 890 891 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) { 892 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth); 893 return; 894 } 895 896 route = get_route(pkg->local_route_hi, pkg->local_route_lo); 897 898 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); 899 if (xd) { 900 u8 xd_phy_port, phy_port; 901 902 xd_phy_port = phy_port_from_route(xd->route, xd->depth); 903 phy_port = phy_port_from_route(route, depth); 904 905 if (xd->depth == depth && xd_phy_port == phy_port) { 906 update_xdomain(xd, route, link); 907 tb_xdomain_put(xd); 908 return; 909 } 910 911 /* 912 * If we find an existing XDomain connection remove it 913 * now. We need to go through login handshake and 914 * everything anyway to be able to re-establish the 915 * connection. 916 */ 917 remove_xdomain(xd); 918 tb_xdomain_put(xd); 919 } 920 921 /* 922 * Look if there already exists an XDomain in the same place 923 * than the new one and in that case remove it because it is 924 * most likely another host that got disconnected. 925 */ 926 xd = tb_xdomain_find_by_link_depth(tb, link, depth); 927 if (!xd) { 928 u8 dual_link; 929 930 dual_link = dual_link_from_link(link); 931 if (dual_link) 932 xd = tb_xdomain_find_by_link_depth(tb, dual_link, 933 depth); 934 } 935 if (xd) { 936 remove_xdomain(xd); 937 tb_xdomain_put(xd); 938 } 939 940 /* 941 * If the user disconnected a switch during suspend and 942 * connected another host to the same port, remove the switch 943 * first. 944 */ 945 sw = tb_switch_find_by_route(tb, route); 946 if (sw) { 947 remove_switch(sw); 948 tb_switch_put(sw); 949 } 950 951 sw = tb_switch_find_by_link_depth(tb, link, depth); 952 if (!sw) { 953 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link, 954 depth); 955 return; 956 } 957 958 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link, 959 depth); 960 tb_switch_put(sw); 961 } 962 963 static void 964 icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) 965 { 966 const struct icm_fr_event_xdomain_disconnected *pkg = 967 (const struct icm_fr_event_xdomain_disconnected *)hdr; 968 struct tb_xdomain *xd; 969 970 /* 971 * If the connection is through one or multiple devices, the 972 * XDomain device is removed along with them so it is fine if we 973 * cannot find it here. 974 */ 975 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); 976 if (xd) { 977 remove_xdomain(xd); 978 tb_xdomain_put(xd); 979 } 980 } 981 982 static int icm_tr_cio_reset(struct tb *tb) 983 { 984 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1)); 985 } 986 987 static int 988 icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level, 989 size_t *nboot_acl, bool *rpm) 990 { 991 struct icm_tr_pkg_driver_ready_response reply; 992 struct icm_pkg_driver_ready request = { 993 .hdr.code = ICM_DRIVER_READY, 994 }; 995 int ret; 996 997 memset(&reply, 0, sizeof(reply)); 998 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 999 1, 20000); 1000 if (ret) 1001 return ret; 1002 1003 if (security_level) 1004 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK; 1005 if (nboot_acl) 1006 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >> 1007 ICM_TR_INFO_BOOT_ACL_SHIFT; 1008 if (rpm) 1009 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3); 1010 1011 return 0; 1012 } 1013 1014 static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw) 1015 { 1016 struct icm_tr_pkg_approve_device request; 1017 struct icm_tr_pkg_approve_device reply; 1018 int ret; 1019 1020 memset(&request, 0, sizeof(request)); 1021 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 1022 request.hdr.code = ICM_APPROVE_DEVICE; 1023 request.route_lo = sw->config.route_lo; 1024 request.route_hi = sw->config.route_hi; 1025 request.connection_id = sw->connection_id; 1026 1027 memset(&reply, 0, sizeof(reply)); 1028 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1029 1, ICM_APPROVE_TIMEOUT); 1030 if (ret) 1031 return ret; 1032 1033 if (reply.hdr.flags & ICM_FLAGS_ERROR) { 1034 tb_warn(tb, "PCIe tunnel creation failed\n"); 1035 return -EIO; 1036 } 1037 1038 return 0; 1039 } 1040 1041 static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw) 1042 { 1043 struct icm_tr_pkg_add_device_key_response reply; 1044 struct icm_tr_pkg_add_device_key request; 1045 int ret; 1046 1047 memset(&request, 0, sizeof(request)); 1048 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 1049 request.hdr.code = ICM_ADD_DEVICE_KEY; 1050 request.route_lo = sw->config.route_lo; 1051 request.route_hi = sw->config.route_hi; 1052 request.connection_id = sw->connection_id; 1053 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE); 1054 1055 memset(&reply, 0, sizeof(reply)); 1056 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1057 1, ICM_TIMEOUT); 1058 if (ret) 1059 return ret; 1060 1061 if (reply.hdr.flags & ICM_FLAGS_ERROR) { 1062 tb_warn(tb, "Adding key to switch failed\n"); 1063 return -EIO; 1064 } 1065 1066 return 0; 1067 } 1068 1069 static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw, 1070 const u8 *challenge, u8 *response) 1071 { 1072 struct icm_tr_pkg_challenge_device_response reply; 1073 struct icm_tr_pkg_challenge_device request; 1074 int ret; 1075 1076 memset(&request, 0, sizeof(request)); 1077 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 1078 request.hdr.code = ICM_CHALLENGE_DEVICE; 1079 request.route_lo = sw->config.route_lo; 1080 request.route_hi = sw->config.route_hi; 1081 request.connection_id = sw->connection_id; 1082 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE); 1083 1084 memset(&reply, 0, sizeof(reply)); 1085 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1086 1, ICM_TIMEOUT); 1087 if (ret) 1088 return ret; 1089 1090 if (reply.hdr.flags & ICM_FLAGS_ERROR) 1091 return -EKEYREJECTED; 1092 if (reply.hdr.flags & ICM_FLAGS_NO_KEY) 1093 return -ENOKEY; 1094 1095 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE); 1096 1097 return 0; 1098 } 1099 1100 static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) 1101 { 1102 struct icm_tr_pkg_approve_xdomain_response reply; 1103 struct icm_tr_pkg_approve_xdomain request; 1104 int ret; 1105 1106 memset(&request, 0, sizeof(request)); 1107 request.hdr.code = ICM_APPROVE_XDOMAIN; 1108 request.route_hi = upper_32_bits(xd->route); 1109 request.route_lo = lower_32_bits(xd->route); 1110 request.transmit_path = xd->transmit_path; 1111 request.transmit_ring = xd->transmit_ring; 1112 request.receive_path = xd->receive_path; 1113 request.receive_ring = xd->receive_ring; 1114 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); 1115 1116 memset(&reply, 0, sizeof(reply)); 1117 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1118 1, ICM_TIMEOUT); 1119 if (ret) 1120 return ret; 1121 1122 if (reply.hdr.flags & ICM_FLAGS_ERROR) 1123 return -EIO; 1124 1125 return 0; 1126 } 1127 1128 static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd, 1129 int stage) 1130 { 1131 struct icm_tr_pkg_disconnect_xdomain_response reply; 1132 struct icm_tr_pkg_disconnect_xdomain request; 1133 int ret; 1134 1135 memset(&request, 0, sizeof(request)); 1136 request.hdr.code = ICM_DISCONNECT_XDOMAIN; 1137 request.stage = stage; 1138 request.route_hi = upper_32_bits(xd->route); 1139 request.route_lo = lower_32_bits(xd->route); 1140 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); 1141 1142 memset(&reply, 0, sizeof(reply)); 1143 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1144 1, ICM_TIMEOUT); 1145 if (ret) 1146 return ret; 1147 1148 if (reply.hdr.flags & ICM_FLAGS_ERROR) 1149 return -EIO; 1150 1151 return 0; 1152 } 1153 1154 static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) 1155 { 1156 int ret; 1157 1158 ret = icm_tr_xdomain_tear_down(tb, xd, 1); 1159 if (ret) 1160 return ret; 1161 1162 usleep_range(10, 50); 1163 return icm_tr_xdomain_tear_down(tb, xd, 2); 1164 } 1165 1166 static void 1167 __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr, 1168 bool force_rtd3) 1169 { 1170 const struct icm_tr_event_device_connected *pkg = 1171 (const struct icm_tr_event_device_connected *)hdr; 1172 bool authorized, boot, dual_lane, speed_gen3; 1173 enum tb_security_level security_level; 1174 struct tb_switch *sw, *parent_sw; 1175 struct tb_xdomain *xd; 1176 u64 route; 1177 1178 icm_postpone_rescan(tb); 1179 1180 /* 1181 * Currently we don't use the QoS information coming with the 1182 * device connected message so simply just ignore that extra 1183 * packet for now. 1184 */ 1185 if (pkg->hdr.packet_id) 1186 return; 1187 1188 route = get_route(pkg->route_hi, pkg->route_lo); 1189 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED; 1190 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> 1191 ICM_FLAGS_SLEVEL_SHIFT; 1192 boot = pkg->link_info & ICM_LINK_INFO_BOOT; 1193 dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE; 1194 speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3; 1195 1196 if (pkg->link_info & ICM_LINK_INFO_REJECTED) { 1197 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n", 1198 route); 1199 return; 1200 } 1201 1202 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid); 1203 if (sw) { 1204 /* Update the switch if it is still in the same place */ 1205 if (tb_route(sw) == route && !!sw->authorized == authorized) { 1206 parent_sw = tb_to_switch(sw->dev.parent); 1207 update_switch(parent_sw, sw, route, pkg->connection_id, 1208 0, 0, 0, boot); 1209 tb_switch_put(sw); 1210 return; 1211 } 1212 1213 remove_switch(sw); 1214 tb_switch_put(sw); 1215 } 1216 1217 /* Another switch with the same address */ 1218 sw = tb_switch_find_by_route(tb, route); 1219 if (sw) { 1220 remove_switch(sw); 1221 tb_switch_put(sw); 1222 } 1223 1224 /* XDomain connection with the same address */ 1225 xd = tb_xdomain_find_by_route(tb, route); 1226 if (xd) { 1227 remove_xdomain(xd); 1228 tb_xdomain_put(xd); 1229 } 1230 1231 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route)); 1232 if (!parent_sw) { 1233 tb_err(tb, "failed to find parent switch for %llx\n", route); 1234 return; 1235 } 1236 1237 pm_runtime_get_sync(&parent_sw->dev); 1238 1239 sw = alloc_switch(parent_sw, route, &pkg->ep_uuid); 1240 if (!IS_ERR(sw)) { 1241 sw->connection_id = pkg->connection_id; 1242 sw->authorized = authorized; 1243 sw->security_level = security_level; 1244 sw->boot = boot; 1245 sw->link_speed = speed_gen3 ? 20 : 10; 1246 sw->link_width = dual_lane ? 2 : 1; 1247 sw->rpm = force_rtd3; 1248 if (!sw->rpm) 1249 sw->rpm = intel_vss_is_rtd3(pkg->ep_name, 1250 sizeof(pkg->ep_name)); 1251 1252 if (add_switch(parent_sw, sw)) 1253 tb_switch_put(sw); 1254 } 1255 1256 pm_runtime_mark_last_busy(&parent_sw->dev); 1257 pm_runtime_put_autosuspend(&parent_sw->dev); 1258 1259 tb_switch_put(parent_sw); 1260 } 1261 1262 static void 1263 icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) 1264 { 1265 __icm_tr_device_connected(tb, hdr, false); 1266 } 1267 1268 static void 1269 icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) 1270 { 1271 const struct icm_tr_event_device_disconnected *pkg = 1272 (const struct icm_tr_event_device_disconnected *)hdr; 1273 struct tb_switch *sw; 1274 u64 route; 1275 1276 route = get_route(pkg->route_hi, pkg->route_lo); 1277 1278 sw = tb_switch_find_by_route(tb, route); 1279 if (!sw) { 1280 tb_warn(tb, "no switch exists at %llx, ignoring\n", route); 1281 return; 1282 } 1283 1284 remove_switch(sw); 1285 tb_switch_put(sw); 1286 } 1287 1288 static void 1289 icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr) 1290 { 1291 const struct icm_tr_event_xdomain_connected *pkg = 1292 (const struct icm_tr_event_xdomain_connected *)hdr; 1293 struct tb_xdomain *xd; 1294 struct tb_switch *sw; 1295 u64 route; 1296 1297 if (!tb->root_switch) 1298 return; 1299 1300 route = get_route(pkg->local_route_hi, pkg->local_route_lo); 1301 1302 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); 1303 if (xd) { 1304 if (xd->route == route) { 1305 update_xdomain(xd, route, 0); 1306 tb_xdomain_put(xd); 1307 return; 1308 } 1309 1310 remove_xdomain(xd); 1311 tb_xdomain_put(xd); 1312 } 1313 1314 /* An existing xdomain with the same address */ 1315 xd = tb_xdomain_find_by_route(tb, route); 1316 if (xd) { 1317 remove_xdomain(xd); 1318 tb_xdomain_put(xd); 1319 } 1320 1321 /* 1322 * If the user disconnected a switch during suspend and 1323 * connected another host to the same port, remove the switch 1324 * first. 1325 */ 1326 sw = tb_switch_find_by_route(tb, route); 1327 if (sw) { 1328 remove_switch(sw); 1329 tb_switch_put(sw); 1330 } 1331 1332 sw = tb_switch_find_by_route(tb, get_parent_route(route)); 1333 if (!sw) { 1334 tb_warn(tb, "no switch exists at %llx, ignoring\n", route); 1335 return; 1336 } 1337 1338 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0); 1339 tb_switch_put(sw); 1340 } 1341 1342 static void 1343 icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) 1344 { 1345 const struct icm_tr_event_xdomain_disconnected *pkg = 1346 (const struct icm_tr_event_xdomain_disconnected *)hdr; 1347 struct tb_xdomain *xd; 1348 u64 route; 1349 1350 route = get_route(pkg->route_hi, pkg->route_lo); 1351 1352 xd = tb_xdomain_find_by_route(tb, route); 1353 if (xd) { 1354 remove_xdomain(xd); 1355 tb_xdomain_put(xd); 1356 } 1357 } 1358 1359 static struct pci_dev *get_upstream_port(struct pci_dev *pdev) 1360 { 1361 struct pci_dev *parent; 1362 1363 parent = pci_upstream_bridge(pdev); 1364 while (parent) { 1365 if (!pci_is_pcie(parent)) 1366 return NULL; 1367 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) 1368 break; 1369 parent = pci_upstream_bridge(parent); 1370 } 1371 1372 if (!parent) 1373 return NULL; 1374 1375 switch (parent->device) { 1376 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: 1377 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: 1378 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: 1379 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: 1380 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: 1381 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE: 1382 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE: 1383 return parent; 1384 } 1385 1386 return NULL; 1387 } 1388 1389 static bool icm_ar_is_supported(struct tb *tb) 1390 { 1391 struct pci_dev *upstream_port; 1392 struct icm *icm = tb_priv(tb); 1393 1394 /* 1395 * Starting from Alpine Ridge we can use ICM on Apple machines 1396 * as well. We just need to reset and re-enable it first. 1397 * However, only start it if explicitly asked by the user. 1398 */ 1399 if (icm_firmware_running(tb->nhi)) 1400 return true; 1401 if (!start_icm) 1402 return false; 1403 1404 /* 1405 * Find the upstream PCIe port in case we need to do reset 1406 * through its vendor specific registers. 1407 */ 1408 upstream_port = get_upstream_port(tb->nhi->pdev); 1409 if (upstream_port) { 1410 int cap; 1411 1412 cap = pci_find_ext_capability(upstream_port, 1413 PCI_EXT_CAP_ID_VNDR); 1414 if (cap > 0) { 1415 icm->upstream_port = upstream_port; 1416 icm->vnd_cap = cap; 1417 1418 return true; 1419 } 1420 } 1421 1422 return false; 1423 } 1424 1425 static int icm_ar_cio_reset(struct tb *tb) 1426 { 1427 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9)); 1428 } 1429 1430 static int icm_ar_get_mode(struct tb *tb) 1431 { 1432 struct tb_nhi *nhi = tb->nhi; 1433 int retries = 60; 1434 u32 val; 1435 1436 do { 1437 val = ioread32(nhi->iobase + REG_FW_STS); 1438 if (val & REG_FW_STS_NVM_AUTH_DONE) 1439 break; 1440 msleep(50); 1441 } while (--retries); 1442 1443 if (!retries) { 1444 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n"); 1445 return -ENODEV; 1446 } 1447 1448 return nhi_mailbox_mode(nhi); 1449 } 1450 1451 static int 1452 icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level, 1453 size_t *nboot_acl, bool *rpm) 1454 { 1455 struct icm_ar_pkg_driver_ready_response reply; 1456 struct icm_pkg_driver_ready request = { 1457 .hdr.code = ICM_DRIVER_READY, 1458 }; 1459 int ret; 1460 1461 memset(&reply, 0, sizeof(reply)); 1462 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1463 1, ICM_TIMEOUT); 1464 if (ret) 1465 return ret; 1466 1467 if (security_level) 1468 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK; 1469 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED)) 1470 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >> 1471 ICM_AR_INFO_BOOT_ACL_SHIFT; 1472 if (rpm) 1473 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3); 1474 1475 return 0; 1476 } 1477 1478 static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route) 1479 { 1480 struct icm_ar_pkg_get_route_response reply; 1481 struct icm_ar_pkg_get_route request = { 1482 .hdr = { .code = ICM_GET_ROUTE }, 1483 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link, 1484 }; 1485 int ret; 1486 1487 memset(&reply, 0, sizeof(reply)); 1488 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1489 1, ICM_TIMEOUT); 1490 if (ret) 1491 return ret; 1492 1493 if (reply.hdr.flags & ICM_FLAGS_ERROR) 1494 return -EIO; 1495 1496 *route = get_route(reply.route_hi, reply.route_lo); 1497 return 0; 1498 } 1499 1500 static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids) 1501 { 1502 struct icm_ar_pkg_preboot_acl_response reply; 1503 struct icm_ar_pkg_preboot_acl request = { 1504 .hdr = { .code = ICM_PREBOOT_ACL }, 1505 }; 1506 int ret, i; 1507 1508 memset(&reply, 0, sizeof(reply)); 1509 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1510 1, ICM_TIMEOUT); 1511 if (ret) 1512 return ret; 1513 1514 if (reply.hdr.flags & ICM_FLAGS_ERROR) 1515 return -EIO; 1516 1517 for (i = 0; i < nuuids; i++) { 1518 u32 *uuid = (u32 *)&uuids[i]; 1519 1520 uuid[0] = reply.acl[i].uuid_lo; 1521 uuid[1] = reply.acl[i].uuid_hi; 1522 1523 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) { 1524 /* Map empty entries to null UUID */ 1525 uuid[0] = 0; 1526 uuid[1] = 0; 1527 } else if (uuid[0] != 0 || uuid[1] != 0) { 1528 /* Upper two DWs are always one's */ 1529 uuid[2] = 0xffffffff; 1530 uuid[3] = 0xffffffff; 1531 } 1532 } 1533 1534 return ret; 1535 } 1536 1537 static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids, 1538 size_t nuuids) 1539 { 1540 struct icm_ar_pkg_preboot_acl_response reply; 1541 struct icm_ar_pkg_preboot_acl request = { 1542 .hdr = { 1543 .code = ICM_PREBOOT_ACL, 1544 .flags = ICM_FLAGS_WRITE, 1545 }, 1546 }; 1547 int ret, i; 1548 1549 for (i = 0; i < nuuids; i++) { 1550 const u32 *uuid = (const u32 *)&uuids[i]; 1551 1552 if (uuid_is_null(&uuids[i])) { 1553 /* 1554 * Map null UUID to the empty (all one) entries 1555 * for ICM. 1556 */ 1557 request.acl[i].uuid_lo = 0xffffffff; 1558 request.acl[i].uuid_hi = 0xffffffff; 1559 } else { 1560 /* Two high DWs need to be set to all one */ 1561 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff) 1562 return -EINVAL; 1563 1564 request.acl[i].uuid_lo = uuid[0]; 1565 request.acl[i].uuid_hi = uuid[1]; 1566 } 1567 } 1568 1569 memset(&reply, 0, sizeof(reply)); 1570 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1571 1, ICM_TIMEOUT); 1572 if (ret) 1573 return ret; 1574 1575 if (reply.hdr.flags & ICM_FLAGS_ERROR) 1576 return -EIO; 1577 1578 return 0; 1579 } 1580 1581 static int 1582 icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level, 1583 size_t *nboot_acl, bool *rpm) 1584 { 1585 struct icm_tr_pkg_driver_ready_response reply; 1586 struct icm_pkg_driver_ready request = { 1587 .hdr.code = ICM_DRIVER_READY, 1588 }; 1589 int ret; 1590 1591 memset(&reply, 0, sizeof(reply)); 1592 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 1593 1, 20000); 1594 if (ret) 1595 return ret; 1596 1597 /* Ice Lake always supports RTD3 */ 1598 if (rpm) 1599 *rpm = true; 1600 1601 return 0; 1602 } 1603 1604 static void icm_icl_set_uuid(struct tb *tb) 1605 { 1606 struct tb_nhi *nhi = tb->nhi; 1607 u32 uuid[4]; 1608 1609 pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]); 1610 pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]); 1611 uuid[2] = 0xffffffff; 1612 uuid[3] = 0xffffffff; 1613 1614 tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); 1615 } 1616 1617 static void 1618 icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) 1619 { 1620 __icm_tr_device_connected(tb, hdr, true); 1621 } 1622 1623 static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr) 1624 { 1625 const struct icm_icl_event_rtd3_veto *pkg = 1626 (const struct icm_icl_event_rtd3_veto *)hdr; 1627 1628 tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason); 1629 1630 if (pkg->veto_reason) 1631 icm_veto_begin(tb); 1632 else 1633 icm_veto_end(tb); 1634 } 1635 1636 static void icm_handle_notification(struct work_struct *work) 1637 { 1638 struct icm_notification *n = container_of(work, typeof(*n), work); 1639 struct tb *tb = n->tb; 1640 struct icm *icm = tb_priv(tb); 1641 1642 mutex_lock(&tb->lock); 1643 1644 /* 1645 * When the domain is stopped we flush its workqueue but before 1646 * that the root switch is removed. In that case we should treat 1647 * the queued events as being canceled. 1648 */ 1649 if (tb->root_switch) { 1650 switch (n->pkg->code) { 1651 case ICM_EVENT_DEVICE_CONNECTED: 1652 icm->device_connected(tb, n->pkg); 1653 break; 1654 case ICM_EVENT_DEVICE_DISCONNECTED: 1655 icm->device_disconnected(tb, n->pkg); 1656 break; 1657 case ICM_EVENT_XDOMAIN_CONNECTED: 1658 icm->xdomain_connected(tb, n->pkg); 1659 break; 1660 case ICM_EVENT_XDOMAIN_DISCONNECTED: 1661 icm->xdomain_disconnected(tb, n->pkg); 1662 break; 1663 case ICM_EVENT_RTD3_VETO: 1664 icm->rtd3_veto(tb, n->pkg); 1665 break; 1666 } 1667 } 1668 1669 mutex_unlock(&tb->lock); 1670 1671 kfree(n->pkg); 1672 kfree(n); 1673 } 1674 1675 static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, 1676 const void *buf, size_t size) 1677 { 1678 struct icm_notification *n; 1679 1680 n = kmalloc(sizeof(*n), GFP_KERNEL); 1681 if (!n) 1682 return; 1683 1684 INIT_WORK(&n->work, icm_handle_notification); 1685 n->pkg = kmemdup(buf, size, GFP_KERNEL); 1686 n->tb = tb; 1687 1688 queue_work(tb->wq, &n->work); 1689 } 1690 1691 static int 1692 __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level, 1693 size_t *nboot_acl, bool *rpm) 1694 { 1695 struct icm *icm = tb_priv(tb); 1696 unsigned int retries = 50; 1697 int ret; 1698 1699 ret = icm->driver_ready(tb, security_level, nboot_acl, rpm); 1700 if (ret) { 1701 tb_err(tb, "failed to send driver ready to ICM\n"); 1702 return ret; 1703 } 1704 1705 /* 1706 * Hold on here until the switch config space is accessible so 1707 * that we can read root switch config successfully. 1708 */ 1709 do { 1710 struct tb_cfg_result res; 1711 u32 tmp; 1712 1713 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH, 1714 0, 1, 100); 1715 if (!res.err) 1716 return 0; 1717 1718 msleep(50); 1719 } while (--retries); 1720 1721 tb_err(tb, "failed to read root switch config space, giving up\n"); 1722 return -ETIMEDOUT; 1723 } 1724 1725 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi) 1726 { 1727 struct icm *icm = tb_priv(tb); 1728 u32 val; 1729 1730 if (!icm->upstream_port) 1731 return -ENODEV; 1732 1733 /* Put ARC to wait for CIO reset event to happen */ 1734 val = ioread32(nhi->iobase + REG_FW_STS); 1735 val |= REG_FW_STS_CIO_RESET_REQ; 1736 iowrite32(val, nhi->iobase + REG_FW_STS); 1737 1738 /* Re-start ARC */ 1739 val = ioread32(nhi->iobase + REG_FW_STS); 1740 val |= REG_FW_STS_ICM_EN_INVERT; 1741 val |= REG_FW_STS_ICM_EN_CPU; 1742 iowrite32(val, nhi->iobase + REG_FW_STS); 1743 1744 /* Trigger CIO reset now */ 1745 return icm->cio_reset(tb); 1746 } 1747 1748 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi) 1749 { 1750 unsigned int retries = 10; 1751 int ret; 1752 u32 val; 1753 1754 /* Check if the ICM firmware is already running */ 1755 if (icm_firmware_running(nhi)) 1756 return 0; 1757 1758 dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n"); 1759 1760 ret = icm_firmware_reset(tb, nhi); 1761 if (ret) 1762 return ret; 1763 1764 /* Wait until the ICM firmware tells us it is up and running */ 1765 do { 1766 /* Check that the ICM firmware is running */ 1767 val = ioread32(nhi->iobase + REG_FW_STS); 1768 if (val & REG_FW_STS_NVM_AUTH_DONE) 1769 return 0; 1770 1771 msleep(300); 1772 } while (--retries); 1773 1774 return -ETIMEDOUT; 1775 } 1776 1777 static int icm_reset_phy_port(struct tb *tb, int phy_port) 1778 { 1779 struct icm *icm = tb_priv(tb); 1780 u32 state0, state1; 1781 int port0, port1; 1782 u32 val0, val1; 1783 int ret; 1784 1785 if (!icm->upstream_port) 1786 return 0; 1787 1788 if (phy_port) { 1789 port0 = 3; 1790 port1 = 4; 1791 } else { 1792 port0 = 1; 1793 port1 = 2; 1794 } 1795 1796 /* 1797 * Read link status of both null ports belonging to a single 1798 * physical port. 1799 */ 1800 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0); 1801 if (ret) 1802 return ret; 1803 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1); 1804 if (ret) 1805 return ret; 1806 1807 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK; 1808 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT; 1809 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK; 1810 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT; 1811 1812 /* If they are both up we need to reset them now */ 1813 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP) 1814 return 0; 1815 1816 val0 |= PHY_PORT_CS1_LINK_DISABLE; 1817 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0); 1818 if (ret) 1819 return ret; 1820 1821 val1 |= PHY_PORT_CS1_LINK_DISABLE; 1822 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1); 1823 if (ret) 1824 return ret; 1825 1826 /* Wait a bit and then re-enable both ports */ 1827 usleep_range(10, 100); 1828 1829 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0); 1830 if (ret) 1831 return ret; 1832 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1); 1833 if (ret) 1834 return ret; 1835 1836 val0 &= ~PHY_PORT_CS1_LINK_DISABLE; 1837 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0); 1838 if (ret) 1839 return ret; 1840 1841 val1 &= ~PHY_PORT_CS1_LINK_DISABLE; 1842 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1); 1843 } 1844 1845 static int icm_firmware_init(struct tb *tb) 1846 { 1847 struct icm *icm = tb_priv(tb); 1848 struct tb_nhi *nhi = tb->nhi; 1849 int ret; 1850 1851 ret = icm_firmware_start(tb, nhi); 1852 if (ret) { 1853 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n"); 1854 return ret; 1855 } 1856 1857 if (icm->get_mode) { 1858 ret = icm->get_mode(tb); 1859 1860 switch (ret) { 1861 case NHI_FW_SAFE_MODE: 1862 icm->safe_mode = true; 1863 break; 1864 1865 case NHI_FW_CM_MODE: 1866 /* Ask ICM to accept all Thunderbolt devices */ 1867 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0); 1868 break; 1869 1870 default: 1871 if (ret < 0) 1872 return ret; 1873 1874 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret); 1875 return -ENODEV; 1876 } 1877 } 1878 1879 /* 1880 * Reset both physical ports if there is anything connected to 1881 * them already. 1882 */ 1883 ret = icm_reset_phy_port(tb, 0); 1884 if (ret) 1885 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n"); 1886 ret = icm_reset_phy_port(tb, 1); 1887 if (ret) 1888 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n"); 1889 1890 return 0; 1891 } 1892 1893 static int icm_driver_ready(struct tb *tb) 1894 { 1895 struct icm *icm = tb_priv(tb); 1896 int ret; 1897 1898 ret = icm_firmware_init(tb); 1899 if (ret) 1900 return ret; 1901 1902 if (icm->safe_mode) { 1903 tb_info(tb, "Thunderbolt host controller is in safe mode.\n"); 1904 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n"); 1905 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n"); 1906 return 0; 1907 } 1908 1909 ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl, 1910 &icm->rpm); 1911 if (ret) 1912 return ret; 1913 1914 /* 1915 * Make sure the number of supported preboot ACL matches what we 1916 * expect or disable the whole feature. 1917 */ 1918 if (tb->nboot_acl > icm->max_boot_acl) 1919 tb->nboot_acl = 0; 1920 1921 return 0; 1922 } 1923 1924 static int icm_suspend(struct tb *tb) 1925 { 1926 struct icm *icm = tb_priv(tb); 1927 1928 if (icm->save_devices) 1929 icm->save_devices(tb); 1930 1931 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); 1932 return 0; 1933 } 1934 1935 /* 1936 * Mark all switches (except root switch) below this one unplugged. ICM 1937 * firmware will send us an updated list of switches after we have send 1938 * it driver ready command. If a switch is not in that list it will be 1939 * removed when we perform rescan. 1940 */ 1941 static void icm_unplug_children(struct tb_switch *sw) 1942 { 1943 struct tb_port *port; 1944 1945 if (tb_route(sw)) 1946 sw->is_unplugged = true; 1947 1948 tb_switch_for_each_port(sw, port) { 1949 if (port->xdomain) 1950 port->xdomain->is_unplugged = true; 1951 else if (tb_port_has_remote(port)) 1952 icm_unplug_children(port->remote->sw); 1953 } 1954 } 1955 1956 static int complete_rpm(struct device *dev, void *data) 1957 { 1958 struct tb_switch *sw = tb_to_switch(dev); 1959 1960 if (sw) 1961 complete(&sw->rpm_complete); 1962 return 0; 1963 } 1964 1965 static void remove_unplugged_switch(struct tb_switch *sw) 1966 { 1967 pm_runtime_get_sync(sw->dev.parent); 1968 1969 /* 1970 * Signal this and switches below for rpm_complete because 1971 * tb_switch_remove() calls pm_runtime_get_sync() that then waits 1972 * for it. 1973 */ 1974 complete_rpm(&sw->dev, NULL); 1975 bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm); 1976 tb_switch_remove(sw); 1977 1978 pm_runtime_mark_last_busy(sw->dev.parent); 1979 pm_runtime_put_autosuspend(sw->dev.parent); 1980 } 1981 1982 static void icm_free_unplugged_children(struct tb_switch *sw) 1983 { 1984 struct tb_port *port; 1985 1986 tb_switch_for_each_port(sw, port) { 1987 if (port->xdomain && port->xdomain->is_unplugged) { 1988 tb_xdomain_remove(port->xdomain); 1989 port->xdomain = NULL; 1990 } else if (tb_port_has_remote(port)) { 1991 if (port->remote->sw->is_unplugged) { 1992 remove_unplugged_switch(port->remote->sw); 1993 port->remote = NULL; 1994 } else { 1995 icm_free_unplugged_children(port->remote->sw); 1996 } 1997 } 1998 } 1999 } 2000 2001 static void icm_rescan_work(struct work_struct *work) 2002 { 2003 struct icm *icm = container_of(work, struct icm, rescan_work.work); 2004 struct tb *tb = icm_to_tb(icm); 2005 2006 mutex_lock(&tb->lock); 2007 if (tb->root_switch) 2008 icm_free_unplugged_children(tb->root_switch); 2009 mutex_unlock(&tb->lock); 2010 } 2011 2012 static void icm_complete(struct tb *tb) 2013 { 2014 struct icm *icm = tb_priv(tb); 2015 2016 if (tb->nhi->going_away) 2017 return; 2018 2019 /* 2020 * If RTD3 was vetoed before we entered system suspend allow it 2021 * again now before driver ready is sent. Firmware sends a new RTD3 2022 * veto if it is still the case after we have sent it driver ready 2023 * command. 2024 */ 2025 icm_veto_end(tb); 2026 icm_unplug_children(tb->root_switch); 2027 2028 /* 2029 * Now all existing children should be resumed, start events 2030 * from ICM to get updated status. 2031 */ 2032 __icm_driver_ready(tb, NULL, NULL, NULL); 2033 2034 /* 2035 * We do not get notifications of devices that have been 2036 * unplugged during suspend so schedule rescan to clean them up 2037 * if any. 2038 */ 2039 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500)); 2040 } 2041 2042 static int icm_runtime_suspend(struct tb *tb) 2043 { 2044 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); 2045 return 0; 2046 } 2047 2048 static int icm_runtime_suspend_switch(struct tb_switch *sw) 2049 { 2050 if (tb_route(sw)) 2051 reinit_completion(&sw->rpm_complete); 2052 return 0; 2053 } 2054 2055 static int icm_runtime_resume_switch(struct tb_switch *sw) 2056 { 2057 if (tb_route(sw)) { 2058 if (!wait_for_completion_timeout(&sw->rpm_complete, 2059 msecs_to_jiffies(500))) { 2060 dev_dbg(&sw->dev, "runtime resuming timed out\n"); 2061 } 2062 } 2063 return 0; 2064 } 2065 2066 static int icm_runtime_resume(struct tb *tb) 2067 { 2068 /* 2069 * We can reuse the same resume functionality than with system 2070 * suspend. 2071 */ 2072 icm_complete(tb); 2073 return 0; 2074 } 2075 2076 static int icm_start(struct tb *tb) 2077 { 2078 struct icm *icm = tb_priv(tb); 2079 int ret; 2080 2081 if (icm->safe_mode) 2082 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0); 2083 else 2084 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); 2085 if (IS_ERR(tb->root_switch)) 2086 return PTR_ERR(tb->root_switch); 2087 2088 tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm; 2089 tb->root_switch->rpm = icm->rpm; 2090 2091 if (icm->set_uuid) 2092 icm->set_uuid(tb); 2093 2094 ret = tb_switch_add(tb->root_switch); 2095 if (ret) { 2096 tb_switch_put(tb->root_switch); 2097 tb->root_switch = NULL; 2098 } 2099 2100 return ret; 2101 } 2102 2103 static void icm_stop(struct tb *tb) 2104 { 2105 struct icm *icm = tb_priv(tb); 2106 2107 cancel_delayed_work(&icm->rescan_work); 2108 tb_switch_remove(tb->root_switch); 2109 tb->root_switch = NULL; 2110 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); 2111 } 2112 2113 static int icm_disconnect_pcie_paths(struct tb *tb) 2114 { 2115 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0); 2116 } 2117 2118 /* Falcon Ridge */ 2119 static const struct tb_cm_ops icm_fr_ops = { 2120 .driver_ready = icm_driver_ready, 2121 .start = icm_start, 2122 .stop = icm_stop, 2123 .suspend = icm_suspend, 2124 .complete = icm_complete, 2125 .handle_event = icm_handle_event, 2126 .approve_switch = icm_fr_approve_switch, 2127 .add_switch_key = icm_fr_add_switch_key, 2128 .challenge_switch_key = icm_fr_challenge_switch_key, 2129 .disconnect_pcie_paths = icm_disconnect_pcie_paths, 2130 .approve_xdomain_paths = icm_fr_approve_xdomain_paths, 2131 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths, 2132 }; 2133 2134 /* Alpine Ridge */ 2135 static const struct tb_cm_ops icm_ar_ops = { 2136 .driver_ready = icm_driver_ready, 2137 .start = icm_start, 2138 .stop = icm_stop, 2139 .suspend = icm_suspend, 2140 .complete = icm_complete, 2141 .runtime_suspend = icm_runtime_suspend, 2142 .runtime_resume = icm_runtime_resume, 2143 .runtime_suspend_switch = icm_runtime_suspend_switch, 2144 .runtime_resume_switch = icm_runtime_resume_switch, 2145 .handle_event = icm_handle_event, 2146 .get_boot_acl = icm_ar_get_boot_acl, 2147 .set_boot_acl = icm_ar_set_boot_acl, 2148 .approve_switch = icm_fr_approve_switch, 2149 .add_switch_key = icm_fr_add_switch_key, 2150 .challenge_switch_key = icm_fr_challenge_switch_key, 2151 .disconnect_pcie_paths = icm_disconnect_pcie_paths, 2152 .approve_xdomain_paths = icm_fr_approve_xdomain_paths, 2153 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths, 2154 }; 2155 2156 /* Titan Ridge */ 2157 static const struct tb_cm_ops icm_tr_ops = { 2158 .driver_ready = icm_driver_ready, 2159 .start = icm_start, 2160 .stop = icm_stop, 2161 .suspend = icm_suspend, 2162 .complete = icm_complete, 2163 .runtime_suspend = icm_runtime_suspend, 2164 .runtime_resume = icm_runtime_resume, 2165 .runtime_suspend_switch = icm_runtime_suspend_switch, 2166 .runtime_resume_switch = icm_runtime_resume_switch, 2167 .handle_event = icm_handle_event, 2168 .get_boot_acl = icm_ar_get_boot_acl, 2169 .set_boot_acl = icm_ar_set_boot_acl, 2170 .approve_switch = icm_tr_approve_switch, 2171 .add_switch_key = icm_tr_add_switch_key, 2172 .challenge_switch_key = icm_tr_challenge_switch_key, 2173 .disconnect_pcie_paths = icm_disconnect_pcie_paths, 2174 .approve_xdomain_paths = icm_tr_approve_xdomain_paths, 2175 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths, 2176 }; 2177 2178 /* Ice Lake */ 2179 static const struct tb_cm_ops icm_icl_ops = { 2180 .driver_ready = icm_driver_ready, 2181 .start = icm_start, 2182 .stop = icm_stop, 2183 .complete = icm_complete, 2184 .runtime_suspend = icm_runtime_suspend, 2185 .runtime_resume = icm_runtime_resume, 2186 .handle_event = icm_handle_event, 2187 .approve_xdomain_paths = icm_tr_approve_xdomain_paths, 2188 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths, 2189 }; 2190 2191 struct tb *icm_probe(struct tb_nhi *nhi) 2192 { 2193 struct icm *icm; 2194 struct tb *tb; 2195 2196 tb = tb_domain_alloc(nhi, sizeof(struct icm)); 2197 if (!tb) 2198 return NULL; 2199 2200 icm = tb_priv(tb); 2201 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work); 2202 mutex_init(&icm->request_lock); 2203 2204 switch (nhi->pdev->device) { 2205 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI: 2206 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI: 2207 icm->can_upgrade_nvm = true; 2208 icm->is_supported = icm_fr_is_supported; 2209 icm->get_route = icm_fr_get_route; 2210 icm->save_devices = icm_fr_save_devices; 2211 icm->driver_ready = icm_fr_driver_ready; 2212 icm->device_connected = icm_fr_device_connected; 2213 icm->device_disconnected = icm_fr_device_disconnected; 2214 icm->xdomain_connected = icm_fr_xdomain_connected; 2215 icm->xdomain_disconnected = icm_fr_xdomain_disconnected; 2216 tb->cm_ops = &icm_fr_ops; 2217 break; 2218 2219 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI: 2220 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI: 2221 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI: 2222 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI: 2223 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI: 2224 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES; 2225 /* 2226 * NVM upgrade has not been tested on Apple systems and 2227 * they don't provide images publicly either. To be on 2228 * the safe side prevent root switch NVM upgrade on Macs 2229 * for now. 2230 */ 2231 icm->can_upgrade_nvm = !x86_apple_machine; 2232 icm->is_supported = icm_ar_is_supported; 2233 icm->cio_reset = icm_ar_cio_reset; 2234 icm->get_mode = icm_ar_get_mode; 2235 icm->get_route = icm_ar_get_route; 2236 icm->save_devices = icm_fr_save_devices; 2237 icm->driver_ready = icm_ar_driver_ready; 2238 icm->device_connected = icm_fr_device_connected; 2239 icm->device_disconnected = icm_fr_device_disconnected; 2240 icm->xdomain_connected = icm_fr_xdomain_connected; 2241 icm->xdomain_disconnected = icm_fr_xdomain_disconnected; 2242 tb->cm_ops = &icm_ar_ops; 2243 break; 2244 2245 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI: 2246 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI: 2247 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES; 2248 icm->can_upgrade_nvm = !x86_apple_machine; 2249 icm->is_supported = icm_ar_is_supported; 2250 icm->cio_reset = icm_tr_cio_reset; 2251 icm->get_mode = icm_ar_get_mode; 2252 icm->driver_ready = icm_tr_driver_ready; 2253 icm->device_connected = icm_tr_device_connected; 2254 icm->device_disconnected = icm_tr_device_disconnected; 2255 icm->xdomain_connected = icm_tr_xdomain_connected; 2256 icm->xdomain_disconnected = icm_tr_xdomain_disconnected; 2257 tb->cm_ops = &icm_tr_ops; 2258 break; 2259 2260 case PCI_DEVICE_ID_INTEL_ICL_NHI0: 2261 case PCI_DEVICE_ID_INTEL_ICL_NHI1: 2262 icm->is_supported = icm_fr_is_supported; 2263 icm->driver_ready = icm_icl_driver_ready; 2264 icm->set_uuid = icm_icl_set_uuid; 2265 icm->device_connected = icm_icl_device_connected; 2266 icm->device_disconnected = icm_tr_device_disconnected; 2267 icm->xdomain_connected = icm_tr_xdomain_connected; 2268 icm->xdomain_disconnected = icm_tr_xdomain_disconnected; 2269 icm->rtd3_veto = icm_icl_rtd3_veto; 2270 tb->cm_ops = &icm_icl_ops; 2271 break; 2272 } 2273 2274 if (!icm->is_supported || !icm->is_supported(tb)) { 2275 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n"); 2276 tb_domain_put(tb); 2277 return NULL; 2278 } 2279 2280 return tb; 2281 } 2282