1 /* 2 * Internal Thunderbolt Connection Manager. This is a firmware running on 3 * the Thunderbolt host controller performing most of the low-level 4 * handling. 5 * 6 * Copyright (C) 2017, Intel Corporation 7 * Authors: Michael Jamet <michael.jamet@intel.com> 8 * Mika Westerberg <mika.westerberg@linux.intel.com> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 */ 14 15 #include <linux/delay.h> 16 #include <linux/mutex.h> 17 #include <linux/pci.h> 18 #include <linux/platform_data/x86/apple.h> 19 #include <linux/sizes.h> 20 #include <linux/slab.h> 21 #include <linux/workqueue.h> 22 23 #include "ctl.h" 24 #include "nhi_regs.h" 25 #include "tb.h" 26 27 #define PCIE2CIO_CMD 0x30 28 #define PCIE2CIO_CMD_TIMEOUT BIT(31) 29 #define PCIE2CIO_CMD_START BIT(30) 30 #define PCIE2CIO_CMD_WRITE BIT(21) 31 #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19) 32 #define PCIE2CIO_CMD_CS_SHIFT 19 33 #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13) 34 #define PCIE2CIO_CMD_PORT_SHIFT 13 35 36 #define PCIE2CIO_WRDATA 0x34 37 #define PCIE2CIO_RDDATA 0x38 38 39 #define PHY_PORT_CS1 0x37 40 #define PHY_PORT_CS1_LINK_DISABLE BIT(14) 41 #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26) 42 #define PHY_PORT_CS1_LINK_STATE_SHIFT 26 43 44 #define ICM_TIMEOUT 5000 /* ms */ 45 #define ICM_MAX_LINK 4 46 #define ICM_MAX_DEPTH 6 47 48 /** 49 * struct icm - Internal connection manager private data 50 * @request_lock: Makes sure only one message is send to ICM at time 51 * @rescan_work: Work used to rescan the surviving switches after resume 52 * @upstream_port: Pointer to the PCIe upstream port this host 53 * controller is connected. This is only set for systems 54 * where ICM needs to be started manually 55 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides 56 * (only set when @upstream_port is not %NULL) 57 * @safe_mode: ICM is in safe mode 58 * @is_supported: Checks if we can support ICM on this controller 59 * @get_mode: Read and return the ICM firmware mode (optional) 60 * @get_route: Find a route string for given switch 61 * @device_connected: Handle device connected ICM message 62 * @device_disconnected: Handle device disconnected ICM message 63 * @xdomain_connected - Handle XDomain connected ICM message 64 * @xdomain_disconnected - Handle XDomain disconnected ICM message 65 */ 66 struct icm { 67 struct mutex request_lock; 68 struct delayed_work rescan_work; 69 struct pci_dev *upstream_port; 70 int vnd_cap; 71 bool safe_mode; 72 bool (*is_supported)(struct tb *tb); 73 int (*get_mode)(struct tb *tb); 74 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route); 75 void (*device_connected)(struct tb *tb, 76 const struct icm_pkg_header *hdr); 77 void (*device_disconnected)(struct tb *tb, 78 const struct icm_pkg_header *hdr); 79 void (*xdomain_connected)(struct tb *tb, 80 const struct icm_pkg_header *hdr); 81 void (*xdomain_disconnected)(struct tb *tb, 82 const struct icm_pkg_header *hdr); 83 }; 84 85 struct icm_notification { 86 struct work_struct work; 87 struct icm_pkg_header *pkg; 88 struct tb *tb; 89 }; 90 91 static inline struct tb *icm_to_tb(struct icm *icm) 92 { 93 return ((void *)icm - sizeof(struct tb)); 94 } 95 96 static inline u8 phy_port_from_route(u64 route, u8 depth) 97 { 98 u8 link; 99 100 link = depth ? route >> ((depth - 1) * 8) : route; 101 return tb_phy_port_from_link(link); 102 } 103 104 static inline u8 dual_link_from_link(u8 link) 105 { 106 return link ? ((link - 1) ^ 0x01) + 1 : 0; 107 } 108 109 static inline u64 get_route(u32 route_hi, u32 route_lo) 110 { 111 return (u64)route_hi << 32 | route_lo; 112 } 113 114 static bool icm_match(const struct tb_cfg_request *req, 115 const struct ctl_pkg *pkg) 116 { 117 const struct icm_pkg_header *res_hdr = pkg->buffer; 118 const struct icm_pkg_header *req_hdr = req->request; 119 120 if (pkg->frame.eof != req->response_type) 121 return false; 122 if (res_hdr->code != req_hdr->code) 123 return false; 124 125 return true; 126 } 127 128 static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg) 129 { 130 const struct icm_pkg_header *hdr = pkg->buffer; 131 132 if (hdr->packet_id < req->npackets) { 133 size_t offset = hdr->packet_id * req->response_size; 134 135 memcpy(req->response + offset, pkg->buffer, req->response_size); 136 } 137 138 return hdr->packet_id == hdr->total_packets - 1; 139 } 140 141 static int icm_request(struct tb *tb, const void *request, size_t request_size, 142 void *response, size_t response_size, size_t npackets, 143 unsigned int timeout_msec) 144 { 145 struct icm *icm = tb_priv(tb); 146 int retries = 3; 147 148 do { 149 struct tb_cfg_request *req; 150 struct tb_cfg_result res; 151 152 req = tb_cfg_request_alloc(); 153 if (!req) 154 return -ENOMEM; 155 156 req->match = icm_match; 157 req->copy = icm_copy; 158 req->request = request; 159 req->request_size = request_size; 160 req->request_type = TB_CFG_PKG_ICM_CMD; 161 req->response = response; 162 req->npackets = npackets; 163 req->response_size = response_size; 164 req->response_type = TB_CFG_PKG_ICM_RESP; 165 166 mutex_lock(&icm->request_lock); 167 res = tb_cfg_request_sync(tb->ctl, req, timeout_msec); 168 mutex_unlock(&icm->request_lock); 169 170 tb_cfg_request_put(req); 171 172 if (res.err != -ETIMEDOUT) 173 return res.err == 1 ? -EIO : res.err; 174 175 usleep_range(20, 50); 176 } while (retries--); 177 178 return -ETIMEDOUT; 179 } 180 181 static bool icm_fr_is_supported(struct tb *tb) 182 { 183 return !x86_apple_machine; 184 } 185 186 static inline int icm_fr_get_switch_index(u32 port) 187 { 188 int index; 189 190 if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT) 191 return 0; 192 193 index = port >> ICM_PORT_INDEX_SHIFT; 194 return index != 0xff ? index : 0; 195 } 196 197 static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route) 198 { 199 struct icm_fr_pkg_get_topology_response *switches, *sw; 200 struct icm_fr_pkg_get_topology request = { 201 .hdr = { .code = ICM_GET_TOPOLOGY }, 202 }; 203 size_t npackets = ICM_GET_TOPOLOGY_PACKETS; 204 int ret, index; 205 u8 i; 206 207 switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL); 208 if (!switches) 209 return -ENOMEM; 210 211 ret = icm_request(tb, &request, sizeof(request), switches, 212 sizeof(*switches), npackets, ICM_TIMEOUT); 213 if (ret) 214 goto err_free; 215 216 sw = &switches[0]; 217 index = icm_fr_get_switch_index(sw->ports[link]); 218 if (!index) { 219 ret = -ENODEV; 220 goto err_free; 221 } 222 223 sw = &switches[index]; 224 for (i = 1; i < depth; i++) { 225 unsigned int j; 226 227 if (!(sw->first_data & ICM_SWITCH_USED)) { 228 ret = -ENODEV; 229 goto err_free; 230 } 231 232 for (j = 0; j < ARRAY_SIZE(sw->ports); j++) { 233 index = icm_fr_get_switch_index(sw->ports[j]); 234 if (index > sw->switch_index) { 235 sw = &switches[index]; 236 break; 237 } 238 } 239 } 240 241 *route = get_route(sw->route_hi, sw->route_lo); 242 243 err_free: 244 kfree(switches); 245 return ret; 246 } 247 248 static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw) 249 { 250 struct icm_fr_pkg_approve_device request; 251 struct icm_fr_pkg_approve_device reply; 252 int ret; 253 254 memset(&request, 0, sizeof(request)); 255 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 256 request.hdr.code = ICM_APPROVE_DEVICE; 257 request.connection_id = sw->connection_id; 258 request.connection_key = sw->connection_key; 259 260 memset(&reply, 0, sizeof(reply)); 261 /* Use larger timeout as establishing tunnels can take some time */ 262 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 263 1, 10000); 264 if (ret) 265 return ret; 266 267 if (reply.hdr.flags & ICM_FLAGS_ERROR) { 268 tb_warn(tb, "PCIe tunnel creation failed\n"); 269 return -EIO; 270 } 271 272 return 0; 273 } 274 275 static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw) 276 { 277 struct icm_fr_pkg_add_device_key request; 278 struct icm_fr_pkg_add_device_key_response reply; 279 int ret; 280 281 memset(&request, 0, sizeof(request)); 282 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 283 request.hdr.code = ICM_ADD_DEVICE_KEY; 284 request.connection_id = sw->connection_id; 285 request.connection_key = sw->connection_key; 286 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE); 287 288 memset(&reply, 0, sizeof(reply)); 289 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 290 1, ICM_TIMEOUT); 291 if (ret) 292 return ret; 293 294 if (reply.hdr.flags & ICM_FLAGS_ERROR) { 295 tb_warn(tb, "Adding key to switch failed\n"); 296 return -EIO; 297 } 298 299 return 0; 300 } 301 302 static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw, 303 const u8 *challenge, u8 *response) 304 { 305 struct icm_fr_pkg_challenge_device request; 306 struct icm_fr_pkg_challenge_device_response reply; 307 int ret; 308 309 memset(&request, 0, sizeof(request)); 310 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid)); 311 request.hdr.code = ICM_CHALLENGE_DEVICE; 312 request.connection_id = sw->connection_id; 313 request.connection_key = sw->connection_key; 314 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE); 315 316 memset(&reply, 0, sizeof(reply)); 317 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 318 1, ICM_TIMEOUT); 319 if (ret) 320 return ret; 321 322 if (reply.hdr.flags & ICM_FLAGS_ERROR) 323 return -EKEYREJECTED; 324 if (reply.hdr.flags & ICM_FLAGS_NO_KEY) 325 return -ENOKEY; 326 327 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE); 328 329 return 0; 330 } 331 332 static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) 333 { 334 struct icm_fr_pkg_approve_xdomain_response reply; 335 struct icm_fr_pkg_approve_xdomain request; 336 int ret; 337 338 memset(&request, 0, sizeof(request)); 339 request.hdr.code = ICM_APPROVE_XDOMAIN; 340 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link; 341 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid)); 342 343 request.transmit_path = xd->transmit_path; 344 request.transmit_ring = xd->transmit_ring; 345 request.receive_path = xd->receive_path; 346 request.receive_ring = xd->receive_ring; 347 348 memset(&reply, 0, sizeof(reply)); 349 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 350 1, ICM_TIMEOUT); 351 if (ret) 352 return ret; 353 354 if (reply.hdr.flags & ICM_FLAGS_ERROR) 355 return -EIO; 356 357 return 0; 358 } 359 360 static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) 361 { 362 u8 phy_port; 363 u8 cmd; 364 365 phy_port = tb_phy_port_from_link(xd->link); 366 if (phy_port == 0) 367 cmd = NHI_MAILBOX_DISCONNECT_PA; 368 else 369 cmd = NHI_MAILBOX_DISCONNECT_PB; 370 371 nhi_mailbox_cmd(tb->nhi, cmd, 1); 372 usleep_range(10, 50); 373 nhi_mailbox_cmd(tb->nhi, cmd, 2); 374 return 0; 375 } 376 377 static void remove_switch(struct tb_switch *sw) 378 { 379 struct tb_switch *parent_sw; 380 381 parent_sw = tb_to_switch(sw->dev.parent); 382 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 383 tb_switch_remove(sw); 384 } 385 386 static void remove_xdomain(struct tb_xdomain *xd) 387 { 388 struct tb_switch *sw; 389 390 sw = tb_to_switch(xd->dev.parent); 391 tb_port_at(xd->route, sw)->xdomain = NULL; 392 tb_xdomain_remove(xd); 393 } 394 395 static void 396 icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) 397 { 398 const struct icm_fr_event_device_connected *pkg = 399 (const struct icm_fr_event_device_connected *)hdr; 400 struct tb_switch *sw, *parent_sw; 401 struct icm *icm = tb_priv(tb); 402 bool authorized = false; 403 struct tb_xdomain *xd; 404 u8 link, depth; 405 u64 route; 406 int ret; 407 408 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; 409 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 410 ICM_LINK_INFO_DEPTH_SHIFT; 411 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED; 412 413 if (pkg->link_info & ICM_LINK_INFO_REJECTED) { 414 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n", 415 link, depth); 416 return; 417 } 418 419 ret = icm->get_route(tb, link, depth, &route); 420 if (ret) { 421 tb_err(tb, "failed to find route string for switch at %u.%u\n", 422 link, depth); 423 return; 424 } 425 426 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid); 427 if (sw) { 428 u8 phy_port, sw_phy_port; 429 430 parent_sw = tb_to_switch(sw->dev.parent); 431 sw_phy_port = phy_port_from_route(tb_route(sw), sw->depth); 432 phy_port = phy_port_from_route(route, depth); 433 434 /* 435 * On resume ICM will send us connected events for the 436 * devices that still are present. However, that 437 * information might have changed for example by the 438 * fact that a switch on a dual-link connection might 439 * have been enumerated using the other link now. Make 440 * sure our book keeping matches that. 441 */ 442 if (sw->depth == depth && sw_phy_port == phy_port && 443 !!sw->authorized == authorized) { 444 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 445 tb_port_at(route, parent_sw)->remote = 446 tb_upstream_port(sw); 447 sw->config.route_hi = upper_32_bits(route); 448 sw->config.route_lo = lower_32_bits(route); 449 sw->connection_id = pkg->connection_id; 450 sw->connection_key = pkg->connection_key; 451 sw->link = link; 452 sw->depth = depth; 453 sw->is_unplugged = false; 454 tb_switch_put(sw); 455 return; 456 } 457 458 /* 459 * User connected the same switch to another physical 460 * port or to another part of the topology. Remove the 461 * existing switch now before adding the new one. 462 */ 463 remove_switch(sw); 464 tb_switch_put(sw); 465 } 466 467 /* 468 * If the switch was not found by UUID, look for a switch on 469 * same physical port (taking possible link aggregation into 470 * account) and depth. If we found one it is definitely a stale 471 * one so remove it first. 472 */ 473 sw = tb_switch_find_by_link_depth(tb, link, depth); 474 if (!sw) { 475 u8 dual_link; 476 477 dual_link = dual_link_from_link(link); 478 if (dual_link) 479 sw = tb_switch_find_by_link_depth(tb, dual_link, depth); 480 } 481 if (sw) { 482 remove_switch(sw); 483 tb_switch_put(sw); 484 } 485 486 /* Remove existing XDomain connection if found */ 487 xd = tb_xdomain_find_by_link_depth(tb, link, depth); 488 if (xd) { 489 remove_xdomain(xd); 490 tb_xdomain_put(xd); 491 } 492 493 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1); 494 if (!parent_sw) { 495 tb_err(tb, "failed to find parent switch for %u.%u\n", 496 link, depth); 497 return; 498 } 499 500 sw = tb_switch_alloc(tb, &parent_sw->dev, route); 501 if (!sw) { 502 tb_switch_put(parent_sw); 503 return; 504 } 505 506 sw->uuid = kmemdup(&pkg->ep_uuid, sizeof(pkg->ep_uuid), GFP_KERNEL); 507 sw->connection_id = pkg->connection_id; 508 sw->connection_key = pkg->connection_key; 509 sw->link = link; 510 sw->depth = depth; 511 sw->authorized = authorized; 512 sw->security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >> 513 ICM_FLAGS_SLEVEL_SHIFT; 514 515 /* Link the two switches now */ 516 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); 517 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw); 518 519 ret = tb_switch_add(sw); 520 if (ret) { 521 tb_port_at(tb_route(sw), parent_sw)->remote = NULL; 522 tb_switch_put(sw); 523 } 524 tb_switch_put(parent_sw); 525 } 526 527 static void 528 icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) 529 { 530 const struct icm_fr_event_device_disconnected *pkg = 531 (const struct icm_fr_event_device_disconnected *)hdr; 532 struct tb_switch *sw; 533 u8 link, depth; 534 535 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; 536 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 537 ICM_LINK_INFO_DEPTH_SHIFT; 538 539 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) { 540 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth); 541 return; 542 } 543 544 sw = tb_switch_find_by_link_depth(tb, link, depth); 545 if (!sw) { 546 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link, 547 depth); 548 return; 549 } 550 551 remove_switch(sw); 552 tb_switch_put(sw); 553 } 554 555 static void 556 icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr) 557 { 558 const struct icm_fr_event_xdomain_connected *pkg = 559 (const struct icm_fr_event_xdomain_connected *)hdr; 560 struct tb_xdomain *xd; 561 struct tb_switch *sw; 562 u8 link, depth; 563 bool approved; 564 u64 route; 565 566 /* 567 * After NVM upgrade adding root switch device fails because we 568 * initiated reset. During that time ICM might still send 569 * XDomain connected message which we ignore here. 570 */ 571 if (!tb->root_switch) 572 return; 573 574 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK; 575 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >> 576 ICM_LINK_INFO_DEPTH_SHIFT; 577 approved = pkg->link_info & ICM_LINK_INFO_APPROVED; 578 579 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) { 580 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth); 581 return; 582 } 583 584 route = get_route(pkg->local_route_hi, pkg->local_route_lo); 585 586 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); 587 if (xd) { 588 u8 xd_phy_port, phy_port; 589 590 xd_phy_port = phy_port_from_route(xd->route, xd->depth); 591 phy_port = phy_port_from_route(route, depth); 592 593 if (xd->depth == depth && xd_phy_port == phy_port) { 594 xd->link = link; 595 xd->route = route; 596 xd->is_unplugged = false; 597 tb_xdomain_put(xd); 598 return; 599 } 600 601 /* 602 * If we find an existing XDomain connection remove it 603 * now. We need to go through login handshake and 604 * everything anyway to be able to re-establish the 605 * connection. 606 */ 607 remove_xdomain(xd); 608 tb_xdomain_put(xd); 609 } 610 611 /* 612 * Look if there already exists an XDomain in the same place 613 * than the new one and in that case remove it because it is 614 * most likely another host that got disconnected. 615 */ 616 xd = tb_xdomain_find_by_link_depth(tb, link, depth); 617 if (!xd) { 618 u8 dual_link; 619 620 dual_link = dual_link_from_link(link); 621 if (dual_link) 622 xd = tb_xdomain_find_by_link_depth(tb, dual_link, 623 depth); 624 } 625 if (xd) { 626 remove_xdomain(xd); 627 tb_xdomain_put(xd); 628 } 629 630 /* 631 * If the user disconnected a switch during suspend and 632 * connected another host to the same port, remove the switch 633 * first. 634 */ 635 sw = get_switch_at_route(tb->root_switch, route); 636 if (sw) 637 remove_switch(sw); 638 639 sw = tb_switch_find_by_link_depth(tb, link, depth); 640 if (!sw) { 641 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link, 642 depth); 643 return; 644 } 645 646 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, 647 &pkg->local_uuid, &pkg->remote_uuid); 648 if (!xd) { 649 tb_switch_put(sw); 650 return; 651 } 652 653 xd->link = link; 654 xd->depth = depth; 655 656 tb_port_at(route, sw)->xdomain = xd; 657 658 tb_xdomain_add(xd); 659 tb_switch_put(sw); 660 } 661 662 static void 663 icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr) 664 { 665 const struct icm_fr_event_xdomain_disconnected *pkg = 666 (const struct icm_fr_event_xdomain_disconnected *)hdr; 667 struct tb_xdomain *xd; 668 669 /* 670 * If the connection is through one or multiple devices, the 671 * XDomain device is removed along with them so it is fine if we 672 * cannot find it here. 673 */ 674 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid); 675 if (xd) { 676 remove_xdomain(xd); 677 tb_xdomain_put(xd); 678 } 679 } 680 681 static struct pci_dev *get_upstream_port(struct pci_dev *pdev) 682 { 683 struct pci_dev *parent; 684 685 parent = pci_upstream_bridge(pdev); 686 while (parent) { 687 if (!pci_is_pcie(parent)) 688 return NULL; 689 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM) 690 break; 691 parent = pci_upstream_bridge(parent); 692 } 693 694 if (!parent) 695 return NULL; 696 697 switch (parent->device) { 698 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: 699 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: 700 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: 701 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: 702 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: 703 return parent; 704 } 705 706 return NULL; 707 } 708 709 static bool icm_ar_is_supported(struct tb *tb) 710 { 711 struct pci_dev *upstream_port; 712 struct icm *icm = tb_priv(tb); 713 714 /* 715 * Starting from Alpine Ridge we can use ICM on Apple machines 716 * as well. We just need to reset and re-enable it first. 717 */ 718 if (!x86_apple_machine) 719 return true; 720 721 /* 722 * Find the upstream PCIe port in case we need to do reset 723 * through its vendor specific registers. 724 */ 725 upstream_port = get_upstream_port(tb->nhi->pdev); 726 if (upstream_port) { 727 int cap; 728 729 cap = pci_find_ext_capability(upstream_port, 730 PCI_EXT_CAP_ID_VNDR); 731 if (cap > 0) { 732 icm->upstream_port = upstream_port; 733 icm->vnd_cap = cap; 734 735 return true; 736 } 737 } 738 739 return false; 740 } 741 742 static int icm_ar_get_mode(struct tb *tb) 743 { 744 struct tb_nhi *nhi = tb->nhi; 745 int retries = 60; 746 u32 val; 747 748 do { 749 val = ioread32(nhi->iobase + REG_FW_STS); 750 if (val & REG_FW_STS_NVM_AUTH_DONE) 751 break; 752 msleep(50); 753 } while (--retries); 754 755 if (!retries) { 756 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n"); 757 return -ENODEV; 758 } 759 760 return nhi_mailbox_mode(nhi); 761 } 762 763 static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route) 764 { 765 struct icm_ar_pkg_get_route_response reply; 766 struct icm_ar_pkg_get_route request = { 767 .hdr = { .code = ICM_GET_ROUTE }, 768 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link, 769 }; 770 int ret; 771 772 memset(&reply, 0, sizeof(reply)); 773 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 774 1, ICM_TIMEOUT); 775 if (ret) 776 return ret; 777 778 if (reply.hdr.flags & ICM_FLAGS_ERROR) 779 return -EIO; 780 781 *route = get_route(reply.route_hi, reply.route_lo); 782 return 0; 783 } 784 785 static void icm_handle_notification(struct work_struct *work) 786 { 787 struct icm_notification *n = container_of(work, typeof(*n), work); 788 struct tb *tb = n->tb; 789 struct icm *icm = tb_priv(tb); 790 791 mutex_lock(&tb->lock); 792 793 switch (n->pkg->code) { 794 case ICM_EVENT_DEVICE_CONNECTED: 795 icm->device_connected(tb, n->pkg); 796 break; 797 case ICM_EVENT_DEVICE_DISCONNECTED: 798 icm->device_disconnected(tb, n->pkg); 799 break; 800 case ICM_EVENT_XDOMAIN_CONNECTED: 801 icm->xdomain_connected(tb, n->pkg); 802 break; 803 case ICM_EVENT_XDOMAIN_DISCONNECTED: 804 icm->xdomain_disconnected(tb, n->pkg); 805 break; 806 } 807 808 mutex_unlock(&tb->lock); 809 810 kfree(n->pkg); 811 kfree(n); 812 } 813 814 static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, 815 const void *buf, size_t size) 816 { 817 struct icm_notification *n; 818 819 n = kmalloc(sizeof(*n), GFP_KERNEL); 820 if (!n) 821 return; 822 823 INIT_WORK(&n->work, icm_handle_notification); 824 n->pkg = kmemdup(buf, size, GFP_KERNEL); 825 n->tb = tb; 826 827 queue_work(tb->wq, &n->work); 828 } 829 830 static int 831 __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level) 832 { 833 struct icm_pkg_driver_ready_response reply; 834 struct icm_pkg_driver_ready request = { 835 .hdr.code = ICM_DRIVER_READY, 836 }; 837 unsigned int retries = 50; 838 int ret; 839 840 memset(&reply, 0, sizeof(reply)); 841 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply), 842 1, ICM_TIMEOUT); 843 if (ret) 844 return ret; 845 846 if (security_level) 847 *security_level = reply.security_level & 0xf; 848 849 /* 850 * Hold on here until the switch config space is accessible so 851 * that we can read root switch config successfully. 852 */ 853 do { 854 struct tb_cfg_result res; 855 u32 tmp; 856 857 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH, 858 0, 1, 100); 859 if (!res.err) 860 return 0; 861 862 msleep(50); 863 } while (--retries); 864 865 tb_err(tb, "failed to read root switch config space, giving up\n"); 866 return -ETIMEDOUT; 867 } 868 869 static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec) 870 { 871 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec); 872 u32 cmd; 873 874 do { 875 pci_read_config_dword(icm->upstream_port, 876 icm->vnd_cap + PCIE2CIO_CMD, &cmd); 877 if (!(cmd & PCIE2CIO_CMD_START)) { 878 if (cmd & PCIE2CIO_CMD_TIMEOUT) 879 break; 880 return 0; 881 } 882 883 msleep(50); 884 } while (time_before(jiffies, end)); 885 886 return -ETIMEDOUT; 887 } 888 889 static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs, 890 unsigned int port, unsigned int index, u32 *data) 891 { 892 struct pci_dev *pdev = icm->upstream_port; 893 int ret, vnd_cap = icm->vnd_cap; 894 u32 cmd; 895 896 cmd = index; 897 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 898 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 899 cmd |= PCIE2CIO_CMD_START; 900 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 901 902 ret = pci2cio_wait_completion(icm, 5000); 903 if (ret) 904 return ret; 905 906 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data); 907 return 0; 908 } 909 910 static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs, 911 unsigned int port, unsigned int index, u32 data) 912 { 913 struct pci_dev *pdev = icm->upstream_port; 914 int vnd_cap = icm->vnd_cap; 915 u32 cmd; 916 917 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data); 918 919 cmd = index; 920 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 921 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 922 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START; 923 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 924 925 return pci2cio_wait_completion(icm, 5000); 926 } 927 928 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi) 929 { 930 struct icm *icm = tb_priv(tb); 931 u32 val; 932 933 /* Put ARC to wait for CIO reset event to happen */ 934 val = ioread32(nhi->iobase + REG_FW_STS); 935 val |= REG_FW_STS_CIO_RESET_REQ; 936 iowrite32(val, nhi->iobase + REG_FW_STS); 937 938 /* Re-start ARC */ 939 val = ioread32(nhi->iobase + REG_FW_STS); 940 val |= REG_FW_STS_ICM_EN_INVERT; 941 val |= REG_FW_STS_ICM_EN_CPU; 942 iowrite32(val, nhi->iobase + REG_FW_STS); 943 944 /* Trigger CIO reset now */ 945 return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9)); 946 } 947 948 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi) 949 { 950 unsigned int retries = 10; 951 int ret; 952 u32 val; 953 954 /* Check if the ICM firmware is already running */ 955 val = ioread32(nhi->iobase + REG_FW_STS); 956 if (val & REG_FW_STS_ICM_EN) 957 return 0; 958 959 dev_info(&nhi->pdev->dev, "starting ICM firmware\n"); 960 961 ret = icm_firmware_reset(tb, nhi); 962 if (ret) 963 return ret; 964 965 /* Wait until the ICM firmware tells us it is up and running */ 966 do { 967 /* Check that the ICM firmware is running */ 968 val = ioread32(nhi->iobase + REG_FW_STS); 969 if (val & REG_FW_STS_NVM_AUTH_DONE) 970 return 0; 971 972 msleep(300); 973 } while (--retries); 974 975 return -ETIMEDOUT; 976 } 977 978 static int icm_reset_phy_port(struct tb *tb, int phy_port) 979 { 980 struct icm *icm = tb_priv(tb); 981 u32 state0, state1; 982 int port0, port1; 983 u32 val0, val1; 984 int ret; 985 986 if (!icm->upstream_port) 987 return 0; 988 989 if (phy_port) { 990 port0 = 3; 991 port1 = 4; 992 } else { 993 port0 = 1; 994 port1 = 2; 995 } 996 997 /* 998 * Read link status of both null ports belonging to a single 999 * physical port. 1000 */ 1001 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0); 1002 if (ret) 1003 return ret; 1004 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1); 1005 if (ret) 1006 return ret; 1007 1008 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK; 1009 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT; 1010 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK; 1011 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT; 1012 1013 /* If they are both up we need to reset them now */ 1014 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP) 1015 return 0; 1016 1017 val0 |= PHY_PORT_CS1_LINK_DISABLE; 1018 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0); 1019 if (ret) 1020 return ret; 1021 1022 val1 |= PHY_PORT_CS1_LINK_DISABLE; 1023 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1); 1024 if (ret) 1025 return ret; 1026 1027 /* Wait a bit and then re-enable both ports */ 1028 usleep_range(10, 100); 1029 1030 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0); 1031 if (ret) 1032 return ret; 1033 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1); 1034 if (ret) 1035 return ret; 1036 1037 val0 &= ~PHY_PORT_CS1_LINK_DISABLE; 1038 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0); 1039 if (ret) 1040 return ret; 1041 1042 val1 &= ~PHY_PORT_CS1_LINK_DISABLE; 1043 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1); 1044 } 1045 1046 static int icm_firmware_init(struct tb *tb) 1047 { 1048 struct icm *icm = tb_priv(tb); 1049 struct tb_nhi *nhi = tb->nhi; 1050 int ret; 1051 1052 ret = icm_firmware_start(tb, nhi); 1053 if (ret) { 1054 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n"); 1055 return ret; 1056 } 1057 1058 if (icm->get_mode) { 1059 ret = icm->get_mode(tb); 1060 1061 switch (ret) { 1062 case NHI_FW_SAFE_MODE: 1063 icm->safe_mode = true; 1064 break; 1065 1066 case NHI_FW_CM_MODE: 1067 /* Ask ICM to accept all Thunderbolt devices */ 1068 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0); 1069 break; 1070 1071 default: 1072 if (ret < 0) 1073 return ret; 1074 1075 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret); 1076 return -ENODEV; 1077 } 1078 } 1079 1080 /* 1081 * Reset both physical ports if there is anything connected to 1082 * them already. 1083 */ 1084 ret = icm_reset_phy_port(tb, 0); 1085 if (ret) 1086 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n"); 1087 ret = icm_reset_phy_port(tb, 1); 1088 if (ret) 1089 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n"); 1090 1091 return 0; 1092 } 1093 1094 static int icm_driver_ready(struct tb *tb) 1095 { 1096 struct icm *icm = tb_priv(tb); 1097 int ret; 1098 1099 ret = icm_firmware_init(tb); 1100 if (ret) 1101 return ret; 1102 1103 if (icm->safe_mode) { 1104 tb_info(tb, "Thunderbolt host controller is in safe mode.\n"); 1105 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n"); 1106 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n"); 1107 return 0; 1108 } 1109 1110 return __icm_driver_ready(tb, &tb->security_level); 1111 } 1112 1113 static int icm_suspend(struct tb *tb) 1114 { 1115 int ret; 1116 1117 ret = nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0); 1118 if (ret) 1119 tb_info(tb, "Ignoring mailbox command error (%d) in %s\n", 1120 ret, __func__); 1121 1122 return 0; 1123 } 1124 1125 /* 1126 * Mark all switches (except root switch) below this one unplugged. ICM 1127 * firmware will send us an updated list of switches after we have send 1128 * it driver ready command. If a switch is not in that list it will be 1129 * removed when we perform rescan. 1130 */ 1131 static void icm_unplug_children(struct tb_switch *sw) 1132 { 1133 unsigned int i; 1134 1135 if (tb_route(sw)) 1136 sw->is_unplugged = true; 1137 1138 for (i = 1; i <= sw->config.max_port_number; i++) { 1139 struct tb_port *port = &sw->ports[i]; 1140 1141 if (tb_is_upstream_port(port)) 1142 continue; 1143 if (port->xdomain) { 1144 port->xdomain->is_unplugged = true; 1145 continue; 1146 } 1147 if (!port->remote) 1148 continue; 1149 1150 icm_unplug_children(port->remote->sw); 1151 } 1152 } 1153 1154 static void icm_free_unplugged_children(struct tb_switch *sw) 1155 { 1156 unsigned int i; 1157 1158 for (i = 1; i <= sw->config.max_port_number; i++) { 1159 struct tb_port *port = &sw->ports[i]; 1160 1161 if (tb_is_upstream_port(port)) 1162 continue; 1163 1164 if (port->xdomain && port->xdomain->is_unplugged) { 1165 tb_xdomain_remove(port->xdomain); 1166 port->xdomain = NULL; 1167 continue; 1168 } 1169 1170 if (!port->remote) 1171 continue; 1172 1173 if (port->remote->sw->is_unplugged) { 1174 tb_switch_remove(port->remote->sw); 1175 port->remote = NULL; 1176 } else { 1177 icm_free_unplugged_children(port->remote->sw); 1178 } 1179 } 1180 } 1181 1182 static void icm_rescan_work(struct work_struct *work) 1183 { 1184 struct icm *icm = container_of(work, struct icm, rescan_work.work); 1185 struct tb *tb = icm_to_tb(icm); 1186 1187 mutex_lock(&tb->lock); 1188 if (tb->root_switch) 1189 icm_free_unplugged_children(tb->root_switch); 1190 mutex_unlock(&tb->lock); 1191 } 1192 1193 static void icm_complete(struct tb *tb) 1194 { 1195 struct icm *icm = tb_priv(tb); 1196 1197 if (tb->nhi->going_away) 1198 return; 1199 1200 icm_unplug_children(tb->root_switch); 1201 1202 /* 1203 * Now all existing children should be resumed, start events 1204 * from ICM to get updated status. 1205 */ 1206 __icm_driver_ready(tb, NULL); 1207 1208 /* 1209 * We do not get notifications of devices that have been 1210 * unplugged during suspend so schedule rescan to clean them up 1211 * if any. 1212 */ 1213 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500)); 1214 } 1215 1216 static int icm_start(struct tb *tb) 1217 { 1218 struct icm *icm = tb_priv(tb); 1219 int ret; 1220 1221 if (icm->safe_mode) 1222 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0); 1223 else 1224 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); 1225 if (!tb->root_switch) 1226 return -ENODEV; 1227 1228 /* 1229 * NVM upgrade has not been tested on Apple systems and they 1230 * don't provide images publicly either. To be on the safe side 1231 * prevent root switch NVM upgrade on Macs for now. 1232 */ 1233 tb->root_switch->no_nvm_upgrade = x86_apple_machine; 1234 1235 ret = tb_switch_add(tb->root_switch); 1236 if (ret) { 1237 tb_switch_put(tb->root_switch); 1238 tb->root_switch = NULL; 1239 } 1240 1241 return ret; 1242 } 1243 1244 static void icm_stop(struct tb *tb) 1245 { 1246 struct icm *icm = tb_priv(tb); 1247 1248 cancel_delayed_work(&icm->rescan_work); 1249 tb_switch_remove(tb->root_switch); 1250 tb->root_switch = NULL; 1251 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0); 1252 } 1253 1254 static int icm_disconnect_pcie_paths(struct tb *tb) 1255 { 1256 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0); 1257 } 1258 1259 /* Falcon Ridge and Alpine Ridge */ 1260 static const struct tb_cm_ops icm_fr_ops = { 1261 .driver_ready = icm_driver_ready, 1262 .start = icm_start, 1263 .stop = icm_stop, 1264 .suspend = icm_suspend, 1265 .complete = icm_complete, 1266 .handle_event = icm_handle_event, 1267 .approve_switch = icm_fr_approve_switch, 1268 .add_switch_key = icm_fr_add_switch_key, 1269 .challenge_switch_key = icm_fr_challenge_switch_key, 1270 .disconnect_pcie_paths = icm_disconnect_pcie_paths, 1271 .approve_xdomain_paths = icm_fr_approve_xdomain_paths, 1272 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths, 1273 }; 1274 1275 struct tb *icm_probe(struct tb_nhi *nhi) 1276 { 1277 struct icm *icm; 1278 struct tb *tb; 1279 1280 tb = tb_domain_alloc(nhi, sizeof(struct icm)); 1281 if (!tb) 1282 return NULL; 1283 1284 icm = tb_priv(tb); 1285 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work); 1286 mutex_init(&icm->request_lock); 1287 1288 switch (nhi->pdev->device) { 1289 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI: 1290 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI: 1291 icm->is_supported = icm_fr_is_supported; 1292 icm->get_route = icm_fr_get_route; 1293 icm->device_connected = icm_fr_device_connected; 1294 icm->device_disconnected = icm_fr_device_disconnected; 1295 icm->xdomain_connected = icm_fr_xdomain_connected; 1296 icm->xdomain_disconnected = icm_fr_xdomain_disconnected; 1297 tb->cm_ops = &icm_fr_ops; 1298 break; 1299 1300 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI: 1301 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI: 1302 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI: 1303 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI: 1304 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI: 1305 icm->is_supported = icm_ar_is_supported; 1306 icm->get_mode = icm_ar_get_mode; 1307 icm->get_route = icm_ar_get_route; 1308 icm->device_connected = icm_fr_device_connected; 1309 icm->device_disconnected = icm_fr_device_disconnected; 1310 icm->xdomain_connected = icm_fr_xdomain_connected; 1311 icm->xdomain_disconnected = icm_fr_xdomain_disconnected; 1312 tb->cm_ops = &icm_fr_ops; 1313 break; 1314 } 1315 1316 if (!icm->is_supported || !icm->is_supported(tb)) { 1317 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n"); 1318 tb_domain_put(tb); 1319 return NULL; 1320 } 1321 1322 return tb; 1323 } 1324