1 // SPDX-License-Identifier: GPL-2.0
3 * Internal Thunderbolt Connection Manager. This is a firmware running on
4 * the Thunderbolt host controller performing most of the low-level
7 * Copyright (C) 2017, Intel Corporation
8 * Authors: Michael Jamet <michael.jamet@intel.com>
9 * Mika Westerberg <mika.westerberg@linux.intel.com>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/pci.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/platform_data/x86/apple.h>
17 #include <linux/sizes.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
25 #define PCIE2CIO_CMD 0x30
26 #define PCIE2CIO_CMD_TIMEOUT BIT(31)
27 #define PCIE2CIO_CMD_START BIT(30)
28 #define PCIE2CIO_CMD_WRITE BIT(21)
29 #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
30 #define PCIE2CIO_CMD_CS_SHIFT 19
31 #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
32 #define PCIE2CIO_CMD_PORT_SHIFT 13
34 #define PCIE2CIO_WRDATA 0x34
35 #define PCIE2CIO_RDDATA 0x38
37 #define PHY_PORT_CS1 0x37
38 #define PHY_PORT_CS1_LINK_DISABLE BIT(14)
39 #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
40 #define PHY_PORT_CS1_LINK_STATE_SHIFT 26
42 #define ICM_TIMEOUT 5000 /* ms */
43 #define ICM_APPROVE_TIMEOUT 10000 /* ms */
44 #define ICM_MAX_LINK 4
47 * struct icm - Internal connection manager private data
48 * @request_lock: Makes sure only one message is send to ICM at time
49 * @rescan_work: Work used to rescan the surviving switches after resume
50 * @upstream_port: Pointer to the PCIe upstream port this host
51 * controller is connected. This is only set for systems
52 * where ICM needs to be started manually
53 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
54 * (only set when @upstream_port is not %NULL)
55 * @safe_mode: ICM is in safe mode
56 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
57 * @rpm: Does the controller support runtime PM (RTD3)
58 * @is_supported: Checks if we can support ICM on this controller
59 * @cio_reset: Trigger CIO reset
60 * @get_mode: Read and return the ICM firmware mode (optional)
61 * @get_route: Find a route string for given switch
62 * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
63 * @driver_ready: Send driver ready message to ICM
64 * @device_connected: Handle device connected ICM message
65 * @device_disconnected: Handle device disconnected ICM message
66 * @xdomain_connected - Handle XDomain connected ICM message
67 * @xdomain_disconnected - Handle XDomain disconnected ICM message
70 struct mutex request_lock;
71 struct delayed_work rescan_work;
72 struct pci_dev *upstream_port;
77 bool (*is_supported)(struct tb *tb);
78 int (*cio_reset)(struct tb *tb);
79 int (*get_mode)(struct tb *tb);
80 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
81 void (*save_devices)(struct tb *tb);
82 int (*driver_ready)(struct tb *tb,
83 enum tb_security_level *security_level,
84 size_t *nboot_acl, bool *rpm);
85 void (*device_connected)(struct tb *tb,
86 const struct icm_pkg_header *hdr);
87 void (*device_disconnected)(struct tb *tb,
88 const struct icm_pkg_header *hdr);
89 void (*xdomain_connected)(struct tb *tb,
90 const struct icm_pkg_header *hdr);
91 void (*xdomain_disconnected)(struct tb *tb,
92 const struct icm_pkg_header *hdr);
95 struct icm_notification {
96 struct work_struct work;
97 struct icm_pkg_header *pkg;
101 struct ep_name_entry {
107 #define EP_NAME_INTEL_VSS 0x10
109 /* Intel Vendor specific structure */
119 #define INTEL_VSS_FLAGS_RTD3 BIT(0)
121 static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size)
123 const void *end = ep_name + size;
125 while (ep_name < end) {
126 const struct ep_name_entry *ep = ep_name;
130 if (ep_name + ep->len > end)
133 if (ep->type == EP_NAME_INTEL_VSS)
134 return (const struct intel_vss *)ep->data;
142 static inline struct tb *icm_to_tb(struct icm *icm)
144 return ((void *)icm - sizeof(struct tb));
147 static inline u8 phy_port_from_route(u64 route, u8 depth)
151 link = depth ? route >> ((depth - 1) * 8) : route;
152 return tb_phy_port_from_link(link);
155 static inline u8 dual_link_from_link(u8 link)
157 return link ? ((link - 1) ^ 0x01) + 1 : 0;
160 static inline u64 get_route(u32 route_hi, u32 route_lo)
162 return (u64)route_hi << 32 | route_lo;
165 static inline u64 get_parent_route(u64 route)
167 int depth = tb_route_length(route);
168 return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
171 static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
173 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
177 pci_read_config_dword(icm->upstream_port,
178 icm->vnd_cap + PCIE2CIO_CMD, &cmd);
179 if (!(cmd & PCIE2CIO_CMD_START)) {
180 if (cmd & PCIE2CIO_CMD_TIMEOUT)
186 } while (time_before(jiffies, end));
191 static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
192 unsigned int port, unsigned int index, u32 *data)
194 struct pci_dev *pdev = icm->upstream_port;
195 int ret, vnd_cap = icm->vnd_cap;
199 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
200 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
201 cmd |= PCIE2CIO_CMD_START;
202 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
204 ret = pci2cio_wait_completion(icm, 5000);
208 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
212 static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
213 unsigned int port, unsigned int index, u32 data)
215 struct pci_dev *pdev = icm->upstream_port;
216 int vnd_cap = icm->vnd_cap;
219 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
222 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
223 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
224 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
225 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
227 return pci2cio_wait_completion(icm, 5000);
230 static bool icm_match(const struct tb_cfg_request *req,
231 const struct ctl_pkg *pkg)
233 const struct icm_pkg_header *res_hdr = pkg->buffer;
234 const struct icm_pkg_header *req_hdr = req->request;
236 if (pkg->frame.eof != req->response_type)
238 if (res_hdr->code != req_hdr->code)
244 static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
246 const struct icm_pkg_header *hdr = pkg->buffer;
248 if (hdr->packet_id < req->npackets) {
249 size_t offset = hdr->packet_id * req->response_size;
251 memcpy(req->response + offset, pkg->buffer, req->response_size);
254 return hdr->packet_id == hdr->total_packets - 1;
257 static int icm_request(struct tb *tb, const void *request, size_t request_size,
258 void *response, size_t response_size, size_t npackets,
259 unsigned int timeout_msec)
261 struct icm *icm = tb_priv(tb);
265 struct tb_cfg_request *req;
266 struct tb_cfg_result res;
268 req = tb_cfg_request_alloc();
272 req->match = icm_match;
273 req->copy = icm_copy;
274 req->request = request;
275 req->request_size = request_size;
276 req->request_type = TB_CFG_PKG_ICM_CMD;
277 req->response = response;
278 req->npackets = npackets;
279 req->response_size = response_size;
280 req->response_type = TB_CFG_PKG_ICM_RESP;
282 mutex_lock(&icm->request_lock);
283 res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
284 mutex_unlock(&icm->request_lock);
286 tb_cfg_request_put(req);
288 if (res.err != -ETIMEDOUT)
289 return res.err == 1 ? -EIO : res.err;
291 usleep_range(20, 50);
297 static bool icm_fr_is_supported(struct tb *tb)
299 return !x86_apple_machine;
302 static inline int icm_fr_get_switch_index(u32 port)
306 if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
309 index = port >> ICM_PORT_INDEX_SHIFT;
310 return index != 0xff ? index : 0;
313 static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
315 struct icm_fr_pkg_get_topology_response *switches, *sw;
316 struct icm_fr_pkg_get_topology request = {
317 .hdr = { .code = ICM_GET_TOPOLOGY },
319 size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
323 switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
327 ret = icm_request(tb, &request, sizeof(request), switches,
328 sizeof(*switches), npackets, ICM_TIMEOUT);
333 index = icm_fr_get_switch_index(sw->ports[link]);
339 sw = &switches[index];
340 for (i = 1; i < depth; i++) {
343 if (!(sw->first_data & ICM_SWITCH_USED)) {
348 for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
349 index = icm_fr_get_switch_index(sw->ports[j]);
350 if (index > sw->switch_index) {
351 sw = &switches[index];
357 *route = get_route(sw->route_hi, sw->route_lo);
364 static void icm_fr_save_devices(struct tb *tb)
366 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
370 icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
371 size_t *nboot_acl, bool *rpm)
373 struct icm_fr_pkg_driver_ready_response reply;
374 struct icm_pkg_driver_ready request = {
375 .hdr.code = ICM_DRIVER_READY,
379 memset(&reply, 0, sizeof(reply));
380 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
386 *security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
391 static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
393 struct icm_fr_pkg_approve_device request;
394 struct icm_fr_pkg_approve_device reply;
397 memset(&request, 0, sizeof(request));
398 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
399 request.hdr.code = ICM_APPROVE_DEVICE;
400 request.connection_id = sw->connection_id;
401 request.connection_key = sw->connection_key;
403 memset(&reply, 0, sizeof(reply));
404 /* Use larger timeout as establishing tunnels can take some time */
405 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
406 1, ICM_APPROVE_TIMEOUT);
410 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
411 tb_warn(tb, "PCIe tunnel creation failed\n");
418 static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
420 struct icm_fr_pkg_add_device_key request;
421 struct icm_fr_pkg_add_device_key_response reply;
424 memset(&request, 0, sizeof(request));
425 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
426 request.hdr.code = ICM_ADD_DEVICE_KEY;
427 request.connection_id = sw->connection_id;
428 request.connection_key = sw->connection_key;
429 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
431 memset(&reply, 0, sizeof(reply));
432 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
437 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
438 tb_warn(tb, "Adding key to switch failed\n");
445 static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
446 const u8 *challenge, u8 *response)
448 struct icm_fr_pkg_challenge_device request;
449 struct icm_fr_pkg_challenge_device_response reply;
452 memset(&request, 0, sizeof(request));
453 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
454 request.hdr.code = ICM_CHALLENGE_DEVICE;
455 request.connection_id = sw->connection_id;
456 request.connection_key = sw->connection_key;
457 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
459 memset(&reply, 0, sizeof(reply));
460 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
465 if (reply.hdr.flags & ICM_FLAGS_ERROR)
466 return -EKEYREJECTED;
467 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
470 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
475 static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
477 struct icm_fr_pkg_approve_xdomain_response reply;
478 struct icm_fr_pkg_approve_xdomain request;
481 memset(&request, 0, sizeof(request));
482 request.hdr.code = ICM_APPROVE_XDOMAIN;
483 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
484 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
486 request.transmit_path = xd->transmit_path;
487 request.transmit_ring = xd->transmit_ring;
488 request.receive_path = xd->receive_path;
489 request.receive_ring = xd->receive_ring;
491 memset(&reply, 0, sizeof(reply));
492 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
497 if (reply.hdr.flags & ICM_FLAGS_ERROR)
503 static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
508 phy_port = tb_phy_port_from_link(xd->link);
510 cmd = NHI_MAILBOX_DISCONNECT_PA;
512 cmd = NHI_MAILBOX_DISCONNECT_PB;
514 nhi_mailbox_cmd(tb->nhi, cmd, 1);
515 usleep_range(10, 50);
516 nhi_mailbox_cmd(tb->nhi, cmd, 2);
520 static void add_switch(struct tb_switch *parent_sw, u64 route,
521 const uuid_t *uuid, const u8 *ep_name,
522 size_t ep_name_size, u8 connection_id, u8 connection_key,
523 u8 link, u8 depth, enum tb_security_level security_level,
524 bool authorized, bool boot)
526 const struct intel_vss *vss;
527 struct tb_switch *sw;
529 pm_runtime_get_sync(&parent_sw->dev);
531 sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route);
535 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
537 tb_sw_warn(sw, "cannot allocate memory for switch\n");
541 sw->connection_id = connection_id;
542 sw->connection_key = connection_key;
545 sw->authorized = authorized;
546 sw->security_level = security_level;
548 init_completion(&sw->rpm_complete);
550 vss = parse_intel_vss(ep_name, ep_name_size);
552 sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
554 /* Link the two switches now */
555 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
556 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
558 if (tb_switch_add(sw)) {
559 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
564 pm_runtime_mark_last_busy(&parent_sw->dev);
565 pm_runtime_put_autosuspend(&parent_sw->dev);
568 static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
569 u64 route, u8 connection_id, u8 connection_key,
570 u8 link, u8 depth, bool boot)
572 /* Disconnect from parent */
573 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
574 /* Re-connect via updated port*/
575 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
577 /* Update with the new addressing information */
578 sw->config.route_hi = upper_32_bits(route);
579 sw->config.route_lo = lower_32_bits(route);
580 sw->connection_id = connection_id;
581 sw->connection_key = connection_key;
586 /* This switch still exists */
587 sw->is_unplugged = false;
589 /* Runtime resume is now complete */
590 complete(&sw->rpm_complete);
593 static void remove_switch(struct tb_switch *sw)
595 struct tb_switch *parent_sw;
597 parent_sw = tb_to_switch(sw->dev.parent);
598 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
599 tb_switch_remove(sw);
602 static void add_xdomain(struct tb_switch *sw, u64 route,
603 const uuid_t *local_uuid, const uuid_t *remote_uuid,
606 struct tb_xdomain *xd;
608 pm_runtime_get_sync(&sw->dev);
610 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
617 tb_port_at(route, sw)->xdomain = xd;
622 pm_runtime_mark_last_busy(&sw->dev);
623 pm_runtime_put_autosuspend(&sw->dev);
626 static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
630 xd->is_unplugged = false;
633 static void remove_xdomain(struct tb_xdomain *xd)
635 struct tb_switch *sw;
637 sw = tb_to_switch(xd->dev.parent);
638 tb_port_at(xd->route, sw)->xdomain = NULL;
639 tb_xdomain_remove(xd);
643 icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
645 const struct icm_fr_event_device_connected *pkg =
646 (const struct icm_fr_event_device_connected *)hdr;
647 enum tb_security_level security_level;
648 struct tb_switch *sw, *parent_sw;
649 struct icm *icm = tb_priv(tb);
650 bool authorized = false;
651 struct tb_xdomain *xd;
657 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
658 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
659 ICM_LINK_INFO_DEPTH_SHIFT;
660 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
661 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
662 ICM_FLAGS_SLEVEL_SHIFT;
663 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
665 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
666 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
671 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
673 u8 phy_port, sw_phy_port;
675 parent_sw = tb_to_switch(sw->dev.parent);
676 sw_phy_port = tb_phy_port_from_link(sw->link);
677 phy_port = tb_phy_port_from_link(link);
680 * On resume ICM will send us connected events for the
681 * devices that still are present. However, that
682 * information might have changed for example by the
683 * fact that a switch on a dual-link connection might
684 * have been enumerated using the other link now. Make
685 * sure our book keeping matches that.
687 if (sw->depth == depth && sw_phy_port == phy_port &&
688 !!sw->authorized == authorized) {
690 * It was enumerated through another link so update
691 * route string accordingly.
693 if (sw->link != link) {
694 ret = icm->get_route(tb, link, depth, &route);
696 tb_err(tb, "failed to update route string for switch at %u.%u\n",
702 route = tb_route(sw);
705 update_switch(parent_sw, sw, route, pkg->connection_id,
706 pkg->connection_key, link, depth, boot);
712 * User connected the same switch to another physical
713 * port or to another part of the topology. Remove the
714 * existing switch now before adding the new one.
721 * If the switch was not found by UUID, look for a switch on
722 * same physical port (taking possible link aggregation into
723 * account) and depth. If we found one it is definitely a stale
724 * one so remove it first.
726 sw = tb_switch_find_by_link_depth(tb, link, depth);
730 dual_link = dual_link_from_link(link);
732 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
739 /* Remove existing XDomain connection if found */
740 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
746 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
748 tb_err(tb, "failed to find parent switch for %u.%u\n",
753 ret = icm->get_route(tb, link, depth, &route);
755 tb_err(tb, "failed to find route string for switch at %u.%u\n",
757 tb_switch_put(parent_sw);
761 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
762 sizeof(pkg->ep_name), pkg->connection_id,
763 pkg->connection_key, link, depth, security_level,
766 tb_switch_put(parent_sw);
770 icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
772 const struct icm_fr_event_device_disconnected *pkg =
773 (const struct icm_fr_event_device_disconnected *)hdr;
774 struct tb_switch *sw;
777 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
778 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
779 ICM_LINK_INFO_DEPTH_SHIFT;
781 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
782 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
786 sw = tb_switch_find_by_link_depth(tb, link, depth);
788 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
798 icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
800 const struct icm_fr_event_xdomain_connected *pkg =
801 (const struct icm_fr_event_xdomain_connected *)hdr;
802 struct tb_xdomain *xd;
803 struct tb_switch *sw;
807 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
808 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
809 ICM_LINK_INFO_DEPTH_SHIFT;
811 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
812 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
816 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
818 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
820 u8 xd_phy_port, phy_port;
822 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
823 phy_port = phy_port_from_route(route, depth);
825 if (xd->depth == depth && xd_phy_port == phy_port) {
826 update_xdomain(xd, route, link);
832 * If we find an existing XDomain connection remove it
833 * now. We need to go through login handshake and
834 * everything anyway to be able to re-establish the
842 * Look if there already exists an XDomain in the same place
843 * than the new one and in that case remove it because it is
844 * most likely another host that got disconnected.
846 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
850 dual_link = dual_link_from_link(link);
852 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
861 * If the user disconnected a switch during suspend and
862 * connected another host to the same port, remove the switch
865 sw = tb_switch_find_by_route(tb, route);
871 sw = tb_switch_find_by_link_depth(tb, link, depth);
873 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
878 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
884 icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
886 const struct icm_fr_event_xdomain_disconnected *pkg =
887 (const struct icm_fr_event_xdomain_disconnected *)hdr;
888 struct tb_xdomain *xd;
891 * If the connection is through one or multiple devices, the
892 * XDomain device is removed along with them so it is fine if we
893 * cannot find it here.
895 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
902 static int icm_tr_cio_reset(struct tb *tb)
904 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
908 icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
909 size_t *nboot_acl, bool *rpm)
911 struct icm_tr_pkg_driver_ready_response reply;
912 struct icm_pkg_driver_ready request = {
913 .hdr.code = ICM_DRIVER_READY,
917 memset(&reply, 0, sizeof(reply));
918 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
924 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
926 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
927 ICM_TR_INFO_BOOT_ACL_SHIFT;
929 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
934 static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
936 struct icm_tr_pkg_approve_device request;
937 struct icm_tr_pkg_approve_device reply;
940 memset(&request, 0, sizeof(request));
941 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
942 request.hdr.code = ICM_APPROVE_DEVICE;
943 request.route_lo = sw->config.route_lo;
944 request.route_hi = sw->config.route_hi;
945 request.connection_id = sw->connection_id;
947 memset(&reply, 0, sizeof(reply));
948 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
949 1, ICM_APPROVE_TIMEOUT);
953 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
954 tb_warn(tb, "PCIe tunnel creation failed\n");
961 static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
963 struct icm_tr_pkg_add_device_key_response reply;
964 struct icm_tr_pkg_add_device_key request;
967 memset(&request, 0, sizeof(request));
968 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
969 request.hdr.code = ICM_ADD_DEVICE_KEY;
970 request.route_lo = sw->config.route_lo;
971 request.route_hi = sw->config.route_hi;
972 request.connection_id = sw->connection_id;
973 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
975 memset(&reply, 0, sizeof(reply));
976 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
981 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
982 tb_warn(tb, "Adding key to switch failed\n");
989 static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
990 const u8 *challenge, u8 *response)
992 struct icm_tr_pkg_challenge_device_response reply;
993 struct icm_tr_pkg_challenge_device request;
996 memset(&request, 0, sizeof(request));
997 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
998 request.hdr.code = ICM_CHALLENGE_DEVICE;
999 request.route_lo = sw->config.route_lo;
1000 request.route_hi = sw->config.route_hi;
1001 request.connection_id = sw->connection_id;
1002 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
1004 memset(&reply, 0, sizeof(reply));
1005 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1010 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1011 return -EKEYREJECTED;
1012 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
1015 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
1020 static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1022 struct icm_tr_pkg_approve_xdomain_response reply;
1023 struct icm_tr_pkg_approve_xdomain request;
1026 memset(&request, 0, sizeof(request));
1027 request.hdr.code = ICM_APPROVE_XDOMAIN;
1028 request.route_hi = upper_32_bits(xd->route);
1029 request.route_lo = lower_32_bits(xd->route);
1030 request.transmit_path = xd->transmit_path;
1031 request.transmit_ring = xd->transmit_ring;
1032 request.receive_path = xd->receive_path;
1033 request.receive_ring = xd->receive_ring;
1034 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1036 memset(&reply, 0, sizeof(reply));
1037 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1042 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1048 static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
1051 struct icm_tr_pkg_disconnect_xdomain_response reply;
1052 struct icm_tr_pkg_disconnect_xdomain request;
1055 memset(&request, 0, sizeof(request));
1056 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
1057 request.stage = stage;
1058 request.route_hi = upper_32_bits(xd->route);
1059 request.route_lo = lower_32_bits(xd->route);
1060 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1062 memset(&reply, 0, sizeof(reply));
1063 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1068 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1074 static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1078 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1082 usleep_range(10, 50);
1083 return icm_tr_xdomain_tear_down(tb, xd, 2);
1087 icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1089 const struct icm_tr_event_device_connected *pkg =
1090 (const struct icm_tr_event_device_connected *)hdr;
1091 enum tb_security_level security_level;
1092 struct tb_switch *sw, *parent_sw;
1093 struct tb_xdomain *xd;
1094 bool authorized, boot;
1098 * Currently we don't use the QoS information coming with the
1099 * device connected message so simply just ignore that extra
1102 if (pkg->hdr.packet_id)
1105 route = get_route(pkg->route_hi, pkg->route_lo);
1106 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1107 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1108 ICM_FLAGS_SLEVEL_SHIFT;
1109 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
1111 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1112 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1117 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1119 /* Update the switch if it is still in the same place */
1120 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1121 parent_sw = tb_to_switch(sw->dev.parent);
1122 update_switch(parent_sw, sw, route, pkg->connection_id,
1132 /* Another switch with the same address */
1133 sw = tb_switch_find_by_route(tb, route);
1139 /* XDomain connection with the same address */
1140 xd = tb_xdomain_find_by_route(tb, route);
1146 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1148 tb_err(tb, "failed to find parent switch for %llx\n", route);
1152 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
1153 sizeof(pkg->ep_name), pkg->connection_id,
1154 0, 0, 0, security_level, authorized, boot);
1156 tb_switch_put(parent_sw);
1160 icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1162 const struct icm_tr_event_device_disconnected *pkg =
1163 (const struct icm_tr_event_device_disconnected *)hdr;
1164 struct tb_switch *sw;
1167 route = get_route(pkg->route_hi, pkg->route_lo);
1169 sw = tb_switch_find_by_route(tb, route);
1171 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1180 icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1182 const struct icm_tr_event_xdomain_connected *pkg =
1183 (const struct icm_tr_event_xdomain_connected *)hdr;
1184 struct tb_xdomain *xd;
1185 struct tb_switch *sw;
1188 if (!tb->root_switch)
1191 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1193 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1195 if (xd->route == route) {
1196 update_xdomain(xd, route, 0);
1205 /* An existing xdomain with the same address */
1206 xd = tb_xdomain_find_by_route(tb, route);
1213 * If the user disconnected a switch during suspend and
1214 * connected another host to the same port, remove the switch
1217 sw = tb_switch_find_by_route(tb, route);
1223 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1225 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1229 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1234 icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1236 const struct icm_tr_event_xdomain_disconnected *pkg =
1237 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1238 struct tb_xdomain *xd;
1241 route = get_route(pkg->route_hi, pkg->route_lo);
1243 xd = tb_xdomain_find_by_route(tb, route);
1250 static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1252 struct pci_dev *parent;
1254 parent = pci_upstream_bridge(pdev);
1256 if (!pci_is_pcie(parent))
1258 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1260 parent = pci_upstream_bridge(parent);
1266 switch (parent->device) {
1267 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1268 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1269 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1270 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1271 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1272 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1273 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1280 static bool icm_ar_is_supported(struct tb *tb)
1282 struct pci_dev *upstream_port;
1283 struct icm *icm = tb_priv(tb);
1286 * Starting from Alpine Ridge we can use ICM on Apple machines
1287 * as well. We just need to reset and re-enable it first.
1289 if (!x86_apple_machine)
1293 * Find the upstream PCIe port in case we need to do reset
1294 * through its vendor specific registers.
1296 upstream_port = get_upstream_port(tb->nhi->pdev);
1297 if (upstream_port) {
1300 cap = pci_find_ext_capability(upstream_port,
1301 PCI_EXT_CAP_ID_VNDR);
1303 icm->upstream_port = upstream_port;
1313 static int icm_ar_cio_reset(struct tb *tb)
1315 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
1318 static int icm_ar_get_mode(struct tb *tb)
1320 struct tb_nhi *nhi = tb->nhi;
1325 val = ioread32(nhi->iobase + REG_FW_STS);
1326 if (val & REG_FW_STS_NVM_AUTH_DONE)
1329 } while (--retries);
1332 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1336 return nhi_mailbox_mode(nhi);
1340 icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1341 size_t *nboot_acl, bool *rpm)
1343 struct icm_ar_pkg_driver_ready_response reply;
1344 struct icm_pkg_driver_ready request = {
1345 .hdr.code = ICM_DRIVER_READY,
1349 memset(&reply, 0, sizeof(reply));
1350 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1356 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1357 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1358 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1359 ICM_AR_INFO_BOOT_ACL_SHIFT;
1361 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1366 static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1368 struct icm_ar_pkg_get_route_response reply;
1369 struct icm_ar_pkg_get_route request = {
1370 .hdr = { .code = ICM_GET_ROUTE },
1371 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1375 memset(&reply, 0, sizeof(reply));
1376 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1381 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1384 *route = get_route(reply.route_hi, reply.route_lo);
1388 static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1390 struct icm_ar_pkg_preboot_acl_response reply;
1391 struct icm_ar_pkg_preboot_acl request = {
1392 .hdr = { .code = ICM_PREBOOT_ACL },
1396 memset(&reply, 0, sizeof(reply));
1397 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1402 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1405 for (i = 0; i < nuuids; i++) {
1406 u32 *uuid = (u32 *)&uuids[i];
1408 uuid[0] = reply.acl[i].uuid_lo;
1409 uuid[1] = reply.acl[i].uuid_hi;
1411 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1412 /* Map empty entries to null UUID */
1415 } else if (uuid[0] != 0 || uuid[1] != 0) {
1416 /* Upper two DWs are always one's */
1417 uuid[2] = 0xffffffff;
1418 uuid[3] = 0xffffffff;
1425 static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1428 struct icm_ar_pkg_preboot_acl_response reply;
1429 struct icm_ar_pkg_preboot_acl request = {
1431 .code = ICM_PREBOOT_ACL,
1432 .flags = ICM_FLAGS_WRITE,
1437 for (i = 0; i < nuuids; i++) {
1438 const u32 *uuid = (const u32 *)&uuids[i];
1440 if (uuid_is_null(&uuids[i])) {
1442 * Map null UUID to the empty (all one) entries
1445 request.acl[i].uuid_lo = 0xffffffff;
1446 request.acl[i].uuid_hi = 0xffffffff;
1448 /* Two high DWs need to be set to all one */
1449 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1452 request.acl[i].uuid_lo = uuid[0];
1453 request.acl[i].uuid_hi = uuid[1];
1457 memset(&reply, 0, sizeof(reply));
1458 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1463 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1469 static void icm_handle_notification(struct work_struct *work)
1471 struct icm_notification *n = container_of(work, typeof(*n), work);
1472 struct tb *tb = n->tb;
1473 struct icm *icm = tb_priv(tb);
1475 mutex_lock(&tb->lock);
1478 * When the domain is stopped we flush its workqueue but before
1479 * that the root switch is removed. In that case we should treat
1480 * the queued events as being canceled.
1482 if (tb->root_switch) {
1483 switch (n->pkg->code) {
1484 case ICM_EVENT_DEVICE_CONNECTED:
1485 icm->device_connected(tb, n->pkg);
1487 case ICM_EVENT_DEVICE_DISCONNECTED:
1488 icm->device_disconnected(tb, n->pkg);
1490 case ICM_EVENT_XDOMAIN_CONNECTED:
1491 icm->xdomain_connected(tb, n->pkg);
1493 case ICM_EVENT_XDOMAIN_DISCONNECTED:
1494 icm->xdomain_disconnected(tb, n->pkg);
1499 mutex_unlock(&tb->lock);
1505 static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1506 const void *buf, size_t size)
1508 struct icm_notification *n;
1510 n = kmalloc(sizeof(*n), GFP_KERNEL);
1514 INIT_WORK(&n->work, icm_handle_notification);
1515 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1518 queue_work(tb->wq, &n->work);
1522 __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1523 size_t *nboot_acl, bool *rpm)
1525 struct icm *icm = tb_priv(tb);
1526 unsigned int retries = 50;
1529 ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
1531 tb_err(tb, "failed to send driver ready to ICM\n");
1536 * Hold on here until the switch config space is accessible so
1537 * that we can read root switch config successfully.
1540 struct tb_cfg_result res;
1543 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1549 } while (--retries);
1551 tb_err(tb, "failed to read root switch config space, giving up\n");
1555 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1557 struct icm *icm = tb_priv(tb);
1560 if (!icm->upstream_port)
1563 /* Put ARC to wait for CIO reset event to happen */
1564 val = ioread32(nhi->iobase + REG_FW_STS);
1565 val |= REG_FW_STS_CIO_RESET_REQ;
1566 iowrite32(val, nhi->iobase + REG_FW_STS);
1569 val = ioread32(nhi->iobase + REG_FW_STS);
1570 val |= REG_FW_STS_ICM_EN_INVERT;
1571 val |= REG_FW_STS_ICM_EN_CPU;
1572 iowrite32(val, nhi->iobase + REG_FW_STS);
1574 /* Trigger CIO reset now */
1575 return icm->cio_reset(tb);
1578 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1580 unsigned int retries = 10;
1584 /* Check if the ICM firmware is already running */
1585 val = ioread32(nhi->iobase + REG_FW_STS);
1586 if (val & REG_FW_STS_ICM_EN)
1589 dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
1591 ret = icm_firmware_reset(tb, nhi);
1595 /* Wait until the ICM firmware tells us it is up and running */
1597 /* Check that the ICM firmware is running */
1598 val = ioread32(nhi->iobase + REG_FW_STS);
1599 if (val & REG_FW_STS_NVM_AUTH_DONE)
1603 } while (--retries);
1608 static int icm_reset_phy_port(struct tb *tb, int phy_port)
1610 struct icm *icm = tb_priv(tb);
1616 if (!icm->upstream_port)
1628 * Read link status of both null ports belonging to a single
1631 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1634 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1638 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1639 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1640 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1641 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1643 /* If they are both up we need to reset them now */
1644 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1647 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1648 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1652 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1653 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1657 /* Wait a bit and then re-enable both ports */
1658 usleep_range(10, 100);
1660 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1663 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1667 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1668 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1672 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1673 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1676 static int icm_firmware_init(struct tb *tb)
1678 struct icm *icm = tb_priv(tb);
1679 struct tb_nhi *nhi = tb->nhi;
1682 ret = icm_firmware_start(tb, nhi);
1684 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1688 if (icm->get_mode) {
1689 ret = icm->get_mode(tb);
1692 case NHI_FW_SAFE_MODE:
1693 icm->safe_mode = true;
1696 case NHI_FW_CM_MODE:
1697 /* Ask ICM to accept all Thunderbolt devices */
1698 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1705 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1711 * Reset both physical ports if there is anything connected to
1714 ret = icm_reset_phy_port(tb, 0);
1716 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1717 ret = icm_reset_phy_port(tb, 1);
1719 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1724 static int icm_driver_ready(struct tb *tb)
1726 struct icm *icm = tb_priv(tb);
1729 ret = icm_firmware_init(tb);
1733 if (icm->safe_mode) {
1734 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1735 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1736 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1740 ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
1746 * Make sure the number of supported preboot ACL matches what we
1747 * expect or disable the whole feature.
1749 if (tb->nboot_acl > icm->max_boot_acl)
1755 static int icm_suspend(struct tb *tb)
1757 struct icm *icm = tb_priv(tb);
1759 if (icm->save_devices)
1760 icm->save_devices(tb);
1762 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1767 * Mark all switches (except root switch) below this one unplugged. ICM
1768 * firmware will send us an updated list of switches after we have send
1769 * it driver ready command. If a switch is not in that list it will be
1770 * removed when we perform rescan.
1772 static void icm_unplug_children(struct tb_switch *sw)
1777 sw->is_unplugged = true;
1779 for (i = 1; i <= sw->config.max_port_number; i++) {
1780 struct tb_port *port = &sw->ports[i];
1783 port->xdomain->is_unplugged = true;
1784 else if (tb_port_has_remote(port))
1785 icm_unplug_children(port->remote->sw);
1789 static int complete_rpm(struct device *dev, void *data)
1791 struct tb_switch *sw = tb_to_switch(dev);
1794 complete(&sw->rpm_complete);
1798 static void remove_unplugged_switch(struct tb_switch *sw)
1800 pm_runtime_get_sync(sw->dev.parent);
1803 * Signal this and switches below for rpm_complete because
1804 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
1807 complete_rpm(&sw->dev, NULL);
1808 bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
1809 tb_switch_remove(sw);
1811 pm_runtime_mark_last_busy(sw->dev.parent);
1812 pm_runtime_put_autosuspend(sw->dev.parent);
1815 static void icm_free_unplugged_children(struct tb_switch *sw)
1819 for (i = 1; i <= sw->config.max_port_number; i++) {
1820 struct tb_port *port = &sw->ports[i];
1822 if (port->xdomain && port->xdomain->is_unplugged) {
1823 tb_xdomain_remove(port->xdomain);
1824 port->xdomain = NULL;
1825 } else if (tb_port_has_remote(port)) {
1826 if (port->remote->sw->is_unplugged) {
1827 remove_unplugged_switch(port->remote->sw);
1828 port->remote = NULL;
1830 icm_free_unplugged_children(port->remote->sw);
1836 static void icm_rescan_work(struct work_struct *work)
1838 struct icm *icm = container_of(work, struct icm, rescan_work.work);
1839 struct tb *tb = icm_to_tb(icm);
1841 mutex_lock(&tb->lock);
1842 if (tb->root_switch)
1843 icm_free_unplugged_children(tb->root_switch);
1844 mutex_unlock(&tb->lock);
1847 static void icm_complete(struct tb *tb)
1849 struct icm *icm = tb_priv(tb);
1851 if (tb->nhi->going_away)
1854 icm_unplug_children(tb->root_switch);
1857 * Now all existing children should be resumed, start events
1858 * from ICM to get updated status.
1860 __icm_driver_ready(tb, NULL, NULL, NULL);
1863 * We do not get notifications of devices that have been
1864 * unplugged during suspend so schedule rescan to clean them up
1867 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
1870 static int icm_runtime_suspend(struct tb *tb)
1872 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1876 static int icm_runtime_suspend_switch(struct tb_switch *sw)
1879 reinit_completion(&sw->rpm_complete);
1883 static int icm_runtime_resume_switch(struct tb_switch *sw)
1886 if (!wait_for_completion_timeout(&sw->rpm_complete,
1887 msecs_to_jiffies(500))) {
1888 dev_dbg(&sw->dev, "runtime resuming timed out\n");
1894 static int icm_runtime_resume(struct tb *tb)
1897 * We can reuse the same resume functionality than with system
1904 static int icm_start(struct tb *tb)
1906 struct icm *icm = tb_priv(tb);
1910 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
1912 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1913 if (IS_ERR(tb->root_switch))
1914 return PTR_ERR(tb->root_switch);
1917 * NVM upgrade has not been tested on Apple systems and they
1918 * don't provide images publicly either. To be on the safe side
1919 * prevent root switch NVM upgrade on Macs for now.
1921 tb->root_switch->no_nvm_upgrade = x86_apple_machine;
1922 tb->root_switch->rpm = icm->rpm;
1924 ret = tb_switch_add(tb->root_switch);
1926 tb_switch_put(tb->root_switch);
1927 tb->root_switch = NULL;
1933 static void icm_stop(struct tb *tb)
1935 struct icm *icm = tb_priv(tb);
1937 cancel_delayed_work(&icm->rescan_work);
1938 tb_switch_remove(tb->root_switch);
1939 tb->root_switch = NULL;
1940 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1943 static int icm_disconnect_pcie_paths(struct tb *tb)
1945 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
1949 static const struct tb_cm_ops icm_fr_ops = {
1950 .driver_ready = icm_driver_ready,
1953 .suspend = icm_suspend,
1954 .complete = icm_complete,
1955 .handle_event = icm_handle_event,
1956 .approve_switch = icm_fr_approve_switch,
1957 .add_switch_key = icm_fr_add_switch_key,
1958 .challenge_switch_key = icm_fr_challenge_switch_key,
1959 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1960 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1961 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1965 static const struct tb_cm_ops icm_ar_ops = {
1966 .driver_ready = icm_driver_ready,
1969 .suspend = icm_suspend,
1970 .complete = icm_complete,
1971 .runtime_suspend = icm_runtime_suspend,
1972 .runtime_resume = icm_runtime_resume,
1973 .runtime_suspend_switch = icm_runtime_suspend_switch,
1974 .runtime_resume_switch = icm_runtime_resume_switch,
1975 .handle_event = icm_handle_event,
1976 .get_boot_acl = icm_ar_get_boot_acl,
1977 .set_boot_acl = icm_ar_set_boot_acl,
1978 .approve_switch = icm_fr_approve_switch,
1979 .add_switch_key = icm_fr_add_switch_key,
1980 .challenge_switch_key = icm_fr_challenge_switch_key,
1981 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1982 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1983 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1987 static const struct tb_cm_ops icm_tr_ops = {
1988 .driver_ready = icm_driver_ready,
1991 .suspend = icm_suspend,
1992 .complete = icm_complete,
1993 .runtime_suspend = icm_runtime_suspend,
1994 .runtime_resume = icm_runtime_resume,
1995 .runtime_suspend_switch = icm_runtime_suspend_switch,
1996 .runtime_resume_switch = icm_runtime_resume_switch,
1997 .handle_event = icm_handle_event,
1998 .get_boot_acl = icm_ar_get_boot_acl,
1999 .set_boot_acl = icm_ar_set_boot_acl,
2000 .approve_switch = icm_tr_approve_switch,
2001 .add_switch_key = icm_tr_add_switch_key,
2002 .challenge_switch_key = icm_tr_challenge_switch_key,
2003 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2004 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2005 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
2008 struct tb *icm_probe(struct tb_nhi *nhi)
2013 tb = tb_domain_alloc(nhi, sizeof(struct icm));
2018 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
2019 mutex_init(&icm->request_lock);
2021 switch (nhi->pdev->device) {
2022 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2023 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
2024 icm->is_supported = icm_fr_is_supported;
2025 icm->get_route = icm_fr_get_route;
2026 icm->save_devices = icm_fr_save_devices;
2027 icm->driver_ready = icm_fr_driver_ready;
2028 icm->device_connected = icm_fr_device_connected;
2029 icm->device_disconnected = icm_fr_device_disconnected;
2030 icm->xdomain_connected = icm_fr_xdomain_connected;
2031 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
2032 tb->cm_ops = &icm_fr_ops;
2035 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
2036 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
2037 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
2038 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
2039 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
2040 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2041 icm->is_supported = icm_ar_is_supported;
2042 icm->cio_reset = icm_ar_cio_reset;
2043 icm->get_mode = icm_ar_get_mode;
2044 icm->get_route = icm_ar_get_route;
2045 icm->save_devices = icm_fr_save_devices;
2046 icm->driver_ready = icm_ar_driver_ready;
2047 icm->device_connected = icm_fr_device_connected;
2048 icm->device_disconnected = icm_fr_device_disconnected;
2049 icm->xdomain_connected = icm_fr_xdomain_connected;
2050 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
2051 tb->cm_ops = &icm_ar_ops;
2054 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2055 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2056 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2057 icm->is_supported = icm_ar_is_supported;
2058 icm->cio_reset = icm_tr_cio_reset;
2059 icm->get_mode = icm_ar_get_mode;
2060 icm->driver_ready = icm_tr_driver_ready;
2061 icm->device_connected = icm_tr_device_connected;
2062 icm->device_disconnected = icm_tr_device_disconnected;
2063 icm->xdomain_connected = icm_tr_xdomain_connected;
2064 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2065 tb->cm_ops = &icm_tr_ops;
2069 if (!icm->is_supported || !icm->is_supported(tb)) {
2070 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");