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 * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
59 * @veto: Is RTD3 veto in effect
60 * @is_supported: Checks if we can support ICM on this controller
61 * @cio_reset: Trigger CIO reset
62 * @get_mode: Read and return the ICM firmware mode (optional)
63 * @get_route: Find a route string for given switch
64 * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
65 * @driver_ready: Send driver ready message to ICM
66 * @set_uuid: Set UUID for the root switch (optional)
67 * @device_connected: Handle device connected ICM message
68 * @device_disconnected: Handle device disconnected ICM message
69 * @xdomain_connected - Handle XDomain connected ICM message
70 * @xdomain_disconnected - Handle XDomain disconnected ICM message
71 * @rtd3_veto: Handle RTD3 veto notification ICM message
74 struct mutex request_lock;
75 struct delayed_work rescan_work;
76 struct pci_dev *upstream_port;
83 bool (*is_supported)(struct tb *tb);
84 int (*cio_reset)(struct tb *tb);
85 int (*get_mode)(struct tb *tb);
86 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
87 void (*save_devices)(struct tb *tb);
88 int (*driver_ready)(struct tb *tb,
89 enum tb_security_level *security_level,
90 size_t *nboot_acl, bool *rpm);
91 void (*set_uuid)(struct tb *tb);
92 void (*device_connected)(struct tb *tb,
93 const struct icm_pkg_header *hdr);
94 void (*device_disconnected)(struct tb *tb,
95 const struct icm_pkg_header *hdr);
96 void (*xdomain_connected)(struct tb *tb,
97 const struct icm_pkg_header *hdr);
98 void (*xdomain_disconnected)(struct tb *tb,
99 const struct icm_pkg_header *hdr);
100 void (*rtd3_veto)(struct tb *tb, const struct icm_pkg_header *hdr);
103 struct icm_notification {
104 struct work_struct work;
105 struct icm_pkg_header *pkg;
109 struct ep_name_entry {
115 #define EP_NAME_INTEL_VSS 0x10
117 /* Intel Vendor specific structure */
127 #define INTEL_VSS_FLAGS_RTD3 BIT(0)
129 static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size)
131 const void *end = ep_name + size;
133 while (ep_name < end) {
134 const struct ep_name_entry *ep = ep_name;
138 if (ep_name + ep->len > end)
141 if (ep->type == EP_NAME_INTEL_VSS)
142 return (const struct intel_vss *)ep->data;
150 static inline struct tb *icm_to_tb(struct icm *icm)
152 return ((void *)icm - sizeof(struct tb));
155 static inline u8 phy_port_from_route(u64 route, u8 depth)
159 link = depth ? route >> ((depth - 1) * 8) : route;
160 return tb_phy_port_from_link(link);
163 static inline u8 dual_link_from_link(u8 link)
165 return link ? ((link - 1) ^ 0x01) + 1 : 0;
168 static inline u64 get_route(u32 route_hi, u32 route_lo)
170 return (u64)route_hi << 32 | route_lo;
173 static inline u64 get_parent_route(u64 route)
175 int depth = tb_route_length(route);
176 return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
179 static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
181 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
185 pci_read_config_dword(icm->upstream_port,
186 icm->vnd_cap + PCIE2CIO_CMD, &cmd);
187 if (!(cmd & PCIE2CIO_CMD_START)) {
188 if (cmd & PCIE2CIO_CMD_TIMEOUT)
194 } while (time_before(jiffies, end));
199 static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
200 unsigned int port, unsigned int index, u32 *data)
202 struct pci_dev *pdev = icm->upstream_port;
203 int ret, vnd_cap = icm->vnd_cap;
207 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
208 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
209 cmd |= PCIE2CIO_CMD_START;
210 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
212 ret = pci2cio_wait_completion(icm, 5000);
216 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
220 static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
221 unsigned int port, unsigned int index, u32 data)
223 struct pci_dev *pdev = icm->upstream_port;
224 int vnd_cap = icm->vnd_cap;
227 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
230 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
231 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
232 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
233 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
235 return pci2cio_wait_completion(icm, 5000);
238 static bool icm_match(const struct tb_cfg_request *req,
239 const struct ctl_pkg *pkg)
241 const struct icm_pkg_header *res_hdr = pkg->buffer;
242 const struct icm_pkg_header *req_hdr = req->request;
244 if (pkg->frame.eof != req->response_type)
246 if (res_hdr->code != req_hdr->code)
252 static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
254 const struct icm_pkg_header *hdr = pkg->buffer;
256 if (hdr->packet_id < req->npackets) {
257 size_t offset = hdr->packet_id * req->response_size;
259 memcpy(req->response + offset, pkg->buffer, req->response_size);
262 return hdr->packet_id == hdr->total_packets - 1;
265 static int icm_request(struct tb *tb, const void *request, size_t request_size,
266 void *response, size_t response_size, size_t npackets,
267 unsigned int timeout_msec)
269 struct icm *icm = tb_priv(tb);
273 struct tb_cfg_request *req;
274 struct tb_cfg_result res;
276 req = tb_cfg_request_alloc();
280 req->match = icm_match;
281 req->copy = icm_copy;
282 req->request = request;
283 req->request_size = request_size;
284 req->request_type = TB_CFG_PKG_ICM_CMD;
285 req->response = response;
286 req->npackets = npackets;
287 req->response_size = response_size;
288 req->response_type = TB_CFG_PKG_ICM_RESP;
290 mutex_lock(&icm->request_lock);
291 res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
292 mutex_unlock(&icm->request_lock);
294 tb_cfg_request_put(req);
296 if (res.err != -ETIMEDOUT)
297 return res.err == 1 ? -EIO : res.err;
299 usleep_range(20, 50);
306 * If rescan is queued to run (we are resuming), postpone it to give the
307 * firmware some more time to send device connected notifications for next
308 * devices in the chain.
310 static void icm_postpone_rescan(struct tb *tb)
312 struct icm *icm = tb_priv(tb);
314 if (delayed_work_pending(&icm->rescan_work))
315 mod_delayed_work(tb->wq, &icm->rescan_work,
316 msecs_to_jiffies(500));
319 static void icm_veto_begin(struct tb *tb)
321 struct icm *icm = tb_priv(tb);
325 /* Keep the domain powered while veto is in effect */
326 pm_runtime_get(&tb->dev);
330 static void icm_veto_end(struct tb *tb)
332 struct icm *icm = tb_priv(tb);
336 /* Allow the domain suspend now */
337 pm_runtime_mark_last_busy(&tb->dev);
338 pm_runtime_put_autosuspend(&tb->dev);
342 static bool icm_fr_is_supported(struct tb *tb)
344 return !x86_apple_machine;
347 static inline int icm_fr_get_switch_index(u32 port)
351 if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
354 index = port >> ICM_PORT_INDEX_SHIFT;
355 return index != 0xff ? index : 0;
358 static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
360 struct icm_fr_pkg_get_topology_response *switches, *sw;
361 struct icm_fr_pkg_get_topology request = {
362 .hdr = { .code = ICM_GET_TOPOLOGY },
364 size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
368 switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
372 ret = icm_request(tb, &request, sizeof(request), switches,
373 sizeof(*switches), npackets, ICM_TIMEOUT);
378 index = icm_fr_get_switch_index(sw->ports[link]);
384 sw = &switches[index];
385 for (i = 1; i < depth; i++) {
388 if (!(sw->first_data & ICM_SWITCH_USED)) {
393 for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
394 index = icm_fr_get_switch_index(sw->ports[j]);
395 if (index > sw->switch_index) {
396 sw = &switches[index];
402 *route = get_route(sw->route_hi, sw->route_lo);
409 static void icm_fr_save_devices(struct tb *tb)
411 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
415 icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
416 size_t *nboot_acl, bool *rpm)
418 struct icm_fr_pkg_driver_ready_response reply;
419 struct icm_pkg_driver_ready request = {
420 .hdr.code = ICM_DRIVER_READY,
424 memset(&reply, 0, sizeof(reply));
425 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
431 *security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
436 static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
438 struct icm_fr_pkg_approve_device request;
439 struct icm_fr_pkg_approve_device reply;
442 memset(&request, 0, sizeof(request));
443 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
444 request.hdr.code = ICM_APPROVE_DEVICE;
445 request.connection_id = sw->connection_id;
446 request.connection_key = sw->connection_key;
448 memset(&reply, 0, sizeof(reply));
449 /* Use larger timeout as establishing tunnels can take some time */
450 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
451 1, ICM_APPROVE_TIMEOUT);
455 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
456 tb_warn(tb, "PCIe tunnel creation failed\n");
463 static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
465 struct icm_fr_pkg_add_device_key request;
466 struct icm_fr_pkg_add_device_key_response reply;
469 memset(&request, 0, sizeof(request));
470 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
471 request.hdr.code = ICM_ADD_DEVICE_KEY;
472 request.connection_id = sw->connection_id;
473 request.connection_key = sw->connection_key;
474 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
476 memset(&reply, 0, sizeof(reply));
477 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
482 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
483 tb_warn(tb, "Adding key to switch failed\n");
490 static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
491 const u8 *challenge, u8 *response)
493 struct icm_fr_pkg_challenge_device request;
494 struct icm_fr_pkg_challenge_device_response reply;
497 memset(&request, 0, sizeof(request));
498 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
499 request.hdr.code = ICM_CHALLENGE_DEVICE;
500 request.connection_id = sw->connection_id;
501 request.connection_key = sw->connection_key;
502 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
504 memset(&reply, 0, sizeof(reply));
505 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
510 if (reply.hdr.flags & ICM_FLAGS_ERROR)
511 return -EKEYREJECTED;
512 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
515 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
520 static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
522 struct icm_fr_pkg_approve_xdomain_response reply;
523 struct icm_fr_pkg_approve_xdomain request;
526 memset(&request, 0, sizeof(request));
527 request.hdr.code = ICM_APPROVE_XDOMAIN;
528 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
529 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
531 request.transmit_path = xd->transmit_path;
532 request.transmit_ring = xd->transmit_ring;
533 request.receive_path = xd->receive_path;
534 request.receive_ring = xd->receive_ring;
536 memset(&reply, 0, sizeof(reply));
537 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
542 if (reply.hdr.flags & ICM_FLAGS_ERROR)
548 static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
553 phy_port = tb_phy_port_from_link(xd->link);
555 cmd = NHI_MAILBOX_DISCONNECT_PA;
557 cmd = NHI_MAILBOX_DISCONNECT_PB;
559 nhi_mailbox_cmd(tb->nhi, cmd, 1);
560 usleep_range(10, 50);
561 nhi_mailbox_cmd(tb->nhi, cmd, 2);
565 static struct tb_switch *add_switch(struct tb_switch *parent_sw, u64 route,
566 const uuid_t *uuid, const u8 *ep_name,
567 size_t ep_name_size, u8 connection_id,
568 u8 connection_key, u8 link, u8 depth,
569 enum tb_security_level security_level,
570 bool authorized, bool boot)
572 const struct intel_vss *vss;
573 struct tb_switch *sw;
576 pm_runtime_get_sync(&parent_sw->dev);
578 sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route);
582 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
584 tb_sw_warn(sw, "cannot allocate memory for switch\n");
588 sw->connection_id = connection_id;
589 sw->connection_key = connection_key;
592 sw->authorized = authorized;
593 sw->security_level = security_level;
595 init_completion(&sw->rpm_complete);
597 vss = parse_intel_vss(ep_name, ep_name_size);
599 sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
601 /* Link the two switches now */
602 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
603 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
605 ret = tb_switch_add(sw);
607 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
613 pm_runtime_mark_last_busy(&parent_sw->dev);
614 pm_runtime_put_autosuspend(&parent_sw->dev);
619 static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
620 u64 route, u8 connection_id, u8 connection_key,
621 u8 link, u8 depth, bool boot)
623 /* Disconnect from parent */
624 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
625 /* Re-connect via updated port*/
626 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
628 /* Update with the new addressing information */
629 sw->config.route_hi = upper_32_bits(route);
630 sw->config.route_lo = lower_32_bits(route);
631 sw->connection_id = connection_id;
632 sw->connection_key = connection_key;
637 /* This switch still exists */
638 sw->is_unplugged = false;
640 /* Runtime resume is now complete */
641 complete(&sw->rpm_complete);
644 static void remove_switch(struct tb_switch *sw)
646 struct tb_switch *parent_sw;
648 parent_sw = tb_to_switch(sw->dev.parent);
649 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
650 tb_switch_remove(sw);
653 static void add_xdomain(struct tb_switch *sw, u64 route,
654 const uuid_t *local_uuid, const uuid_t *remote_uuid,
657 struct tb_xdomain *xd;
659 pm_runtime_get_sync(&sw->dev);
661 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
668 tb_port_at(route, sw)->xdomain = xd;
673 pm_runtime_mark_last_busy(&sw->dev);
674 pm_runtime_put_autosuspend(&sw->dev);
677 static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
681 xd->is_unplugged = false;
684 static void remove_xdomain(struct tb_xdomain *xd)
686 struct tb_switch *sw;
688 sw = tb_to_switch(xd->dev.parent);
689 tb_port_at(xd->route, sw)->xdomain = NULL;
690 tb_xdomain_remove(xd);
694 icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
696 const struct icm_fr_event_device_connected *pkg =
697 (const struct icm_fr_event_device_connected *)hdr;
698 enum tb_security_level security_level;
699 struct tb_switch *sw, *parent_sw;
700 struct icm *icm = tb_priv(tb);
701 bool authorized = false;
702 struct tb_xdomain *xd;
708 icm_postpone_rescan(tb);
710 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
711 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
712 ICM_LINK_INFO_DEPTH_SHIFT;
713 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
714 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
715 ICM_FLAGS_SLEVEL_SHIFT;
716 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
718 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
719 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
724 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
726 u8 phy_port, sw_phy_port;
728 parent_sw = tb_to_switch(sw->dev.parent);
729 sw_phy_port = tb_phy_port_from_link(sw->link);
730 phy_port = tb_phy_port_from_link(link);
733 * On resume ICM will send us connected events for the
734 * devices that still are present. However, that
735 * information might have changed for example by the
736 * fact that a switch on a dual-link connection might
737 * have been enumerated using the other link now. Make
738 * sure our book keeping matches that.
740 if (sw->depth == depth && sw_phy_port == phy_port &&
741 !!sw->authorized == authorized) {
743 * It was enumerated through another link so update
744 * route string accordingly.
746 if (sw->link != link) {
747 ret = icm->get_route(tb, link, depth, &route);
749 tb_err(tb, "failed to update route string for switch at %u.%u\n",
755 route = tb_route(sw);
758 update_switch(parent_sw, sw, route, pkg->connection_id,
759 pkg->connection_key, link, depth, boot);
765 * User connected the same switch to another physical
766 * port or to another part of the topology. Remove the
767 * existing switch now before adding the new one.
774 * If the switch was not found by UUID, look for a switch on
775 * same physical port (taking possible link aggregation into
776 * account) and depth. If we found one it is definitely a stale
777 * one so remove it first.
779 sw = tb_switch_find_by_link_depth(tb, link, depth);
783 dual_link = dual_link_from_link(link);
785 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
792 /* Remove existing XDomain connection if found */
793 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
799 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
801 tb_err(tb, "failed to find parent switch for %u.%u\n",
806 ret = icm->get_route(tb, link, depth, &route);
808 tb_err(tb, "failed to find route string for switch at %u.%u\n",
810 tb_switch_put(parent_sw);
814 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
815 sizeof(pkg->ep_name), pkg->connection_id,
816 pkg->connection_key, link, depth, security_level,
819 tb_switch_put(parent_sw);
823 icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
825 const struct icm_fr_event_device_disconnected *pkg =
826 (const struct icm_fr_event_device_disconnected *)hdr;
827 struct tb_switch *sw;
830 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
831 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
832 ICM_LINK_INFO_DEPTH_SHIFT;
834 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
835 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
839 sw = tb_switch_find_by_link_depth(tb, link, depth);
841 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
851 icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
853 const struct icm_fr_event_xdomain_connected *pkg =
854 (const struct icm_fr_event_xdomain_connected *)hdr;
855 struct tb_xdomain *xd;
856 struct tb_switch *sw;
860 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
861 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
862 ICM_LINK_INFO_DEPTH_SHIFT;
864 if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
865 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
869 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
871 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
873 u8 xd_phy_port, phy_port;
875 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
876 phy_port = phy_port_from_route(route, depth);
878 if (xd->depth == depth && xd_phy_port == phy_port) {
879 update_xdomain(xd, route, link);
885 * If we find an existing XDomain connection remove it
886 * now. We need to go through login handshake and
887 * everything anyway to be able to re-establish the
895 * Look if there already exists an XDomain in the same place
896 * than the new one and in that case remove it because it is
897 * most likely another host that got disconnected.
899 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
903 dual_link = dual_link_from_link(link);
905 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
914 * If the user disconnected a switch during suspend and
915 * connected another host to the same port, remove the switch
918 sw = tb_switch_find_by_route(tb, route);
924 sw = tb_switch_find_by_link_depth(tb, link, depth);
926 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
931 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
937 icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
939 const struct icm_fr_event_xdomain_disconnected *pkg =
940 (const struct icm_fr_event_xdomain_disconnected *)hdr;
941 struct tb_xdomain *xd;
944 * If the connection is through one or multiple devices, the
945 * XDomain device is removed along with them so it is fine if we
946 * cannot find it here.
948 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
955 static int icm_tr_cio_reset(struct tb *tb)
957 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
961 icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
962 size_t *nboot_acl, bool *rpm)
964 struct icm_tr_pkg_driver_ready_response reply;
965 struct icm_pkg_driver_ready request = {
966 .hdr.code = ICM_DRIVER_READY,
970 memset(&reply, 0, sizeof(reply));
971 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
977 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
979 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
980 ICM_TR_INFO_BOOT_ACL_SHIFT;
982 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
987 static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
989 struct icm_tr_pkg_approve_device request;
990 struct icm_tr_pkg_approve_device reply;
993 memset(&request, 0, sizeof(request));
994 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
995 request.hdr.code = ICM_APPROVE_DEVICE;
996 request.route_lo = sw->config.route_lo;
997 request.route_hi = sw->config.route_hi;
998 request.connection_id = sw->connection_id;
1000 memset(&reply, 0, sizeof(reply));
1001 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1002 1, ICM_APPROVE_TIMEOUT);
1006 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1007 tb_warn(tb, "PCIe tunnel creation failed\n");
1014 static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
1016 struct icm_tr_pkg_add_device_key_response reply;
1017 struct icm_tr_pkg_add_device_key request;
1020 memset(&request, 0, sizeof(request));
1021 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1022 request.hdr.code = ICM_ADD_DEVICE_KEY;
1023 request.route_lo = sw->config.route_lo;
1024 request.route_hi = sw->config.route_hi;
1025 request.connection_id = sw->connection_id;
1026 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
1028 memset(&reply, 0, sizeof(reply));
1029 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1034 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1035 tb_warn(tb, "Adding key to switch failed\n");
1042 static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
1043 const u8 *challenge, u8 *response)
1045 struct icm_tr_pkg_challenge_device_response reply;
1046 struct icm_tr_pkg_challenge_device request;
1049 memset(&request, 0, sizeof(request));
1050 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1051 request.hdr.code = ICM_CHALLENGE_DEVICE;
1052 request.route_lo = sw->config.route_lo;
1053 request.route_hi = sw->config.route_hi;
1054 request.connection_id = sw->connection_id;
1055 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
1057 memset(&reply, 0, sizeof(reply));
1058 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1063 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1064 return -EKEYREJECTED;
1065 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
1068 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
1073 static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1075 struct icm_tr_pkg_approve_xdomain_response reply;
1076 struct icm_tr_pkg_approve_xdomain request;
1079 memset(&request, 0, sizeof(request));
1080 request.hdr.code = ICM_APPROVE_XDOMAIN;
1081 request.route_hi = upper_32_bits(xd->route);
1082 request.route_lo = lower_32_bits(xd->route);
1083 request.transmit_path = xd->transmit_path;
1084 request.transmit_ring = xd->transmit_ring;
1085 request.receive_path = xd->receive_path;
1086 request.receive_ring = xd->receive_ring;
1087 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1089 memset(&reply, 0, sizeof(reply));
1090 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1095 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1101 static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
1104 struct icm_tr_pkg_disconnect_xdomain_response reply;
1105 struct icm_tr_pkg_disconnect_xdomain request;
1108 memset(&request, 0, sizeof(request));
1109 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
1110 request.stage = stage;
1111 request.route_hi = upper_32_bits(xd->route);
1112 request.route_lo = lower_32_bits(xd->route);
1113 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1115 memset(&reply, 0, sizeof(reply));
1116 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1121 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1127 static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1131 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1135 usleep_range(10, 50);
1136 return icm_tr_xdomain_tear_down(tb, xd, 2);
1140 __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
1143 const struct icm_tr_event_device_connected *pkg =
1144 (const struct icm_tr_event_device_connected *)hdr;
1145 enum tb_security_level security_level;
1146 struct tb_switch *sw, *parent_sw;
1147 struct tb_xdomain *xd;
1148 bool authorized, boot;
1151 icm_postpone_rescan(tb);
1154 * Currently we don't use the QoS information coming with the
1155 * device connected message so simply just ignore that extra
1158 if (pkg->hdr.packet_id)
1161 route = get_route(pkg->route_hi, pkg->route_lo);
1162 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1163 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1164 ICM_FLAGS_SLEVEL_SHIFT;
1165 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
1167 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1168 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1173 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1175 /* Update the switch if it is still in the same place */
1176 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1177 parent_sw = tb_to_switch(sw->dev.parent);
1178 update_switch(parent_sw, sw, route, pkg->connection_id,
1188 /* Another switch with the same address */
1189 sw = tb_switch_find_by_route(tb, route);
1195 /* XDomain connection with the same address */
1196 xd = tb_xdomain_find_by_route(tb, route);
1202 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1204 tb_err(tb, "failed to find parent switch for %llx\n", route);
1208 sw = add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
1209 sizeof(pkg->ep_name), pkg->connection_id, 0, 0, 0,
1210 security_level, authorized, boot);
1211 if (!IS_ERR(sw) && force_rtd3)
1214 tb_switch_put(parent_sw);
1218 icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1220 __icm_tr_device_connected(tb, hdr, false);
1224 icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1226 const struct icm_tr_event_device_disconnected *pkg =
1227 (const struct icm_tr_event_device_disconnected *)hdr;
1228 struct tb_switch *sw;
1231 route = get_route(pkg->route_hi, pkg->route_lo);
1233 sw = tb_switch_find_by_route(tb, route);
1235 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1244 icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1246 const struct icm_tr_event_xdomain_connected *pkg =
1247 (const struct icm_tr_event_xdomain_connected *)hdr;
1248 struct tb_xdomain *xd;
1249 struct tb_switch *sw;
1252 if (!tb->root_switch)
1255 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1257 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1259 if (xd->route == route) {
1260 update_xdomain(xd, route, 0);
1269 /* An existing xdomain with the same address */
1270 xd = tb_xdomain_find_by_route(tb, route);
1277 * If the user disconnected a switch during suspend and
1278 * connected another host to the same port, remove the switch
1281 sw = tb_switch_find_by_route(tb, route);
1287 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1289 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1293 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1298 icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1300 const struct icm_tr_event_xdomain_disconnected *pkg =
1301 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1302 struct tb_xdomain *xd;
1305 route = get_route(pkg->route_hi, pkg->route_lo);
1307 xd = tb_xdomain_find_by_route(tb, route);
1314 static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1316 struct pci_dev *parent;
1318 parent = pci_upstream_bridge(pdev);
1320 if (!pci_is_pcie(parent))
1322 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1324 parent = pci_upstream_bridge(parent);
1330 switch (parent->device) {
1331 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1332 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1333 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1334 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1335 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1336 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1337 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1344 static bool icm_ar_is_supported(struct tb *tb)
1346 struct pci_dev *upstream_port;
1347 struct icm *icm = tb_priv(tb);
1350 * Starting from Alpine Ridge we can use ICM on Apple machines
1351 * as well. We just need to reset and re-enable it first.
1353 if (!x86_apple_machine)
1357 * Find the upstream PCIe port in case we need to do reset
1358 * through its vendor specific registers.
1360 upstream_port = get_upstream_port(tb->nhi->pdev);
1361 if (upstream_port) {
1364 cap = pci_find_ext_capability(upstream_port,
1365 PCI_EXT_CAP_ID_VNDR);
1367 icm->upstream_port = upstream_port;
1377 static int icm_ar_cio_reset(struct tb *tb)
1379 return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
1382 static int icm_ar_get_mode(struct tb *tb)
1384 struct tb_nhi *nhi = tb->nhi;
1389 val = ioread32(nhi->iobase + REG_FW_STS);
1390 if (val & REG_FW_STS_NVM_AUTH_DONE)
1393 } while (--retries);
1396 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1400 return nhi_mailbox_mode(nhi);
1404 icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1405 size_t *nboot_acl, bool *rpm)
1407 struct icm_ar_pkg_driver_ready_response reply;
1408 struct icm_pkg_driver_ready request = {
1409 .hdr.code = ICM_DRIVER_READY,
1413 memset(&reply, 0, sizeof(reply));
1414 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1420 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1421 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1422 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1423 ICM_AR_INFO_BOOT_ACL_SHIFT;
1425 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1430 static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1432 struct icm_ar_pkg_get_route_response reply;
1433 struct icm_ar_pkg_get_route request = {
1434 .hdr = { .code = ICM_GET_ROUTE },
1435 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1439 memset(&reply, 0, sizeof(reply));
1440 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1445 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1448 *route = get_route(reply.route_hi, reply.route_lo);
1452 static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1454 struct icm_ar_pkg_preboot_acl_response reply;
1455 struct icm_ar_pkg_preboot_acl request = {
1456 .hdr = { .code = ICM_PREBOOT_ACL },
1460 memset(&reply, 0, sizeof(reply));
1461 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1466 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1469 for (i = 0; i < nuuids; i++) {
1470 u32 *uuid = (u32 *)&uuids[i];
1472 uuid[0] = reply.acl[i].uuid_lo;
1473 uuid[1] = reply.acl[i].uuid_hi;
1475 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1476 /* Map empty entries to null UUID */
1479 } else if (uuid[0] != 0 || uuid[1] != 0) {
1480 /* Upper two DWs are always one's */
1481 uuid[2] = 0xffffffff;
1482 uuid[3] = 0xffffffff;
1489 static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1492 struct icm_ar_pkg_preboot_acl_response reply;
1493 struct icm_ar_pkg_preboot_acl request = {
1495 .code = ICM_PREBOOT_ACL,
1496 .flags = ICM_FLAGS_WRITE,
1501 for (i = 0; i < nuuids; i++) {
1502 const u32 *uuid = (const u32 *)&uuids[i];
1504 if (uuid_is_null(&uuids[i])) {
1506 * Map null UUID to the empty (all one) entries
1509 request.acl[i].uuid_lo = 0xffffffff;
1510 request.acl[i].uuid_hi = 0xffffffff;
1512 /* Two high DWs need to be set to all one */
1513 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1516 request.acl[i].uuid_lo = uuid[0];
1517 request.acl[i].uuid_hi = uuid[1];
1521 memset(&reply, 0, sizeof(reply));
1522 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1527 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1534 icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1535 size_t *nboot_acl, bool *rpm)
1537 struct icm_tr_pkg_driver_ready_response reply;
1538 struct icm_pkg_driver_ready request = {
1539 .hdr.code = ICM_DRIVER_READY,
1543 memset(&reply, 0, sizeof(reply));
1544 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1549 /* Ice Lake always supports RTD3 */
1556 static void icm_icl_set_uuid(struct tb *tb)
1558 struct tb_nhi *nhi = tb->nhi;
1561 pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]);
1562 pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]);
1563 uuid[2] = 0xffffffff;
1564 uuid[3] = 0xffffffff;
1566 tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1570 icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1572 __icm_tr_device_connected(tb, hdr, true);
1575 static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr)
1577 const struct icm_icl_event_rtd3_veto *pkg =
1578 (const struct icm_icl_event_rtd3_veto *)hdr;
1580 tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason);
1582 if (pkg->veto_reason)
1588 static void icm_handle_notification(struct work_struct *work)
1590 struct icm_notification *n = container_of(work, typeof(*n), work);
1591 struct tb *tb = n->tb;
1592 struct icm *icm = tb_priv(tb);
1594 mutex_lock(&tb->lock);
1597 * When the domain is stopped we flush its workqueue but before
1598 * that the root switch is removed. In that case we should treat
1599 * the queued events as being canceled.
1601 if (tb->root_switch) {
1602 switch (n->pkg->code) {
1603 case ICM_EVENT_DEVICE_CONNECTED:
1604 icm->device_connected(tb, n->pkg);
1606 case ICM_EVENT_DEVICE_DISCONNECTED:
1607 icm->device_disconnected(tb, n->pkg);
1609 case ICM_EVENT_XDOMAIN_CONNECTED:
1610 icm->xdomain_connected(tb, n->pkg);
1612 case ICM_EVENT_XDOMAIN_DISCONNECTED:
1613 icm->xdomain_disconnected(tb, n->pkg);
1615 case ICM_EVENT_RTD3_VETO:
1616 icm->rtd3_veto(tb, n->pkg);
1621 mutex_unlock(&tb->lock);
1627 static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1628 const void *buf, size_t size)
1630 struct icm_notification *n;
1632 n = kmalloc(sizeof(*n), GFP_KERNEL);
1636 INIT_WORK(&n->work, icm_handle_notification);
1637 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1640 queue_work(tb->wq, &n->work);
1644 __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1645 size_t *nboot_acl, bool *rpm)
1647 struct icm *icm = tb_priv(tb);
1648 unsigned int retries = 50;
1651 ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
1653 tb_err(tb, "failed to send driver ready to ICM\n");
1658 * Hold on here until the switch config space is accessible so
1659 * that we can read root switch config successfully.
1662 struct tb_cfg_result res;
1665 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1671 } while (--retries);
1673 tb_err(tb, "failed to read root switch config space, giving up\n");
1677 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1679 struct icm *icm = tb_priv(tb);
1682 if (!icm->upstream_port)
1685 /* Put ARC to wait for CIO reset event to happen */
1686 val = ioread32(nhi->iobase + REG_FW_STS);
1687 val |= REG_FW_STS_CIO_RESET_REQ;
1688 iowrite32(val, nhi->iobase + REG_FW_STS);
1691 val = ioread32(nhi->iobase + REG_FW_STS);
1692 val |= REG_FW_STS_ICM_EN_INVERT;
1693 val |= REG_FW_STS_ICM_EN_CPU;
1694 iowrite32(val, nhi->iobase + REG_FW_STS);
1696 /* Trigger CIO reset now */
1697 return icm->cio_reset(tb);
1700 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1702 unsigned int retries = 10;
1706 /* Check if the ICM firmware is already running */
1707 val = ioread32(nhi->iobase + REG_FW_STS);
1708 if (val & REG_FW_STS_ICM_EN)
1711 dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
1713 ret = icm_firmware_reset(tb, nhi);
1717 /* Wait until the ICM firmware tells us it is up and running */
1719 /* Check that the ICM firmware is running */
1720 val = ioread32(nhi->iobase + REG_FW_STS);
1721 if (val & REG_FW_STS_NVM_AUTH_DONE)
1725 } while (--retries);
1730 static int icm_reset_phy_port(struct tb *tb, int phy_port)
1732 struct icm *icm = tb_priv(tb);
1738 if (!icm->upstream_port)
1750 * Read link status of both null ports belonging to a single
1753 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1756 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1760 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1761 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1762 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1763 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1765 /* If they are both up we need to reset them now */
1766 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1769 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1770 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1774 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1775 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1779 /* Wait a bit and then re-enable both ports */
1780 usleep_range(10, 100);
1782 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1785 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1789 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1790 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1794 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1795 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1798 static int icm_firmware_init(struct tb *tb)
1800 struct icm *icm = tb_priv(tb);
1801 struct tb_nhi *nhi = tb->nhi;
1804 ret = icm_firmware_start(tb, nhi);
1806 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1810 if (icm->get_mode) {
1811 ret = icm->get_mode(tb);
1814 case NHI_FW_SAFE_MODE:
1815 icm->safe_mode = true;
1818 case NHI_FW_CM_MODE:
1819 /* Ask ICM to accept all Thunderbolt devices */
1820 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1827 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1833 * Reset both physical ports if there is anything connected to
1836 ret = icm_reset_phy_port(tb, 0);
1838 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1839 ret = icm_reset_phy_port(tb, 1);
1841 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1846 static int icm_driver_ready(struct tb *tb)
1848 struct icm *icm = tb_priv(tb);
1851 ret = icm_firmware_init(tb);
1855 if (icm->safe_mode) {
1856 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1857 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1858 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1862 ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
1868 * Make sure the number of supported preboot ACL matches what we
1869 * expect or disable the whole feature.
1871 if (tb->nboot_acl > icm->max_boot_acl)
1877 static int icm_suspend(struct tb *tb)
1879 struct icm *icm = tb_priv(tb);
1881 if (icm->save_devices)
1882 icm->save_devices(tb);
1884 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1889 * Mark all switches (except root switch) below this one unplugged. ICM
1890 * firmware will send us an updated list of switches after we have send
1891 * it driver ready command. If a switch is not in that list it will be
1892 * removed when we perform rescan.
1894 static void icm_unplug_children(struct tb_switch *sw)
1899 sw->is_unplugged = true;
1901 for (i = 1; i <= sw->config.max_port_number; i++) {
1902 struct tb_port *port = &sw->ports[i];
1905 port->xdomain->is_unplugged = true;
1906 else if (tb_port_has_remote(port))
1907 icm_unplug_children(port->remote->sw);
1911 static int complete_rpm(struct device *dev, void *data)
1913 struct tb_switch *sw = tb_to_switch(dev);
1916 complete(&sw->rpm_complete);
1920 static void remove_unplugged_switch(struct tb_switch *sw)
1922 pm_runtime_get_sync(sw->dev.parent);
1925 * Signal this and switches below for rpm_complete because
1926 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
1929 complete_rpm(&sw->dev, NULL);
1930 bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
1931 tb_switch_remove(sw);
1933 pm_runtime_mark_last_busy(sw->dev.parent);
1934 pm_runtime_put_autosuspend(sw->dev.parent);
1937 static void icm_free_unplugged_children(struct tb_switch *sw)
1941 for (i = 1; i <= sw->config.max_port_number; i++) {
1942 struct tb_port *port = &sw->ports[i];
1944 if (port->xdomain && port->xdomain->is_unplugged) {
1945 tb_xdomain_remove(port->xdomain);
1946 port->xdomain = NULL;
1947 } else if (tb_port_has_remote(port)) {
1948 if (port->remote->sw->is_unplugged) {
1949 remove_unplugged_switch(port->remote->sw);
1950 port->remote = NULL;
1952 icm_free_unplugged_children(port->remote->sw);
1958 static void icm_rescan_work(struct work_struct *work)
1960 struct icm *icm = container_of(work, struct icm, rescan_work.work);
1961 struct tb *tb = icm_to_tb(icm);
1963 mutex_lock(&tb->lock);
1964 if (tb->root_switch)
1965 icm_free_unplugged_children(tb->root_switch);
1966 mutex_unlock(&tb->lock);
1969 static void icm_complete(struct tb *tb)
1971 struct icm *icm = tb_priv(tb);
1973 if (tb->nhi->going_away)
1977 * If RTD3 was vetoed before we entered system suspend allow it
1978 * again now before driver ready is sent. Firmware sends a new RTD3
1979 * veto if it is still the case after we have sent it driver ready
1983 icm_unplug_children(tb->root_switch);
1986 * Now all existing children should be resumed, start events
1987 * from ICM to get updated status.
1989 __icm_driver_ready(tb, NULL, NULL, NULL);
1992 * We do not get notifications of devices that have been
1993 * unplugged during suspend so schedule rescan to clean them up
1996 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
1999 static int icm_runtime_suspend(struct tb *tb)
2001 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
2005 static int icm_runtime_suspend_switch(struct tb_switch *sw)
2008 reinit_completion(&sw->rpm_complete);
2012 static int icm_runtime_resume_switch(struct tb_switch *sw)
2015 if (!wait_for_completion_timeout(&sw->rpm_complete,
2016 msecs_to_jiffies(500))) {
2017 dev_dbg(&sw->dev, "runtime resuming timed out\n");
2023 static int icm_runtime_resume(struct tb *tb)
2026 * We can reuse the same resume functionality than with system
2033 static int icm_start(struct tb *tb)
2035 struct icm *icm = tb_priv(tb);
2039 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
2041 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
2042 if (IS_ERR(tb->root_switch))
2043 return PTR_ERR(tb->root_switch);
2045 tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
2046 tb->root_switch->rpm = icm->rpm;
2051 ret = tb_switch_add(tb->root_switch);
2053 tb_switch_put(tb->root_switch);
2054 tb->root_switch = NULL;
2060 static void icm_stop(struct tb *tb)
2062 struct icm *icm = tb_priv(tb);
2064 cancel_delayed_work(&icm->rescan_work);
2065 tb_switch_remove(tb->root_switch);
2066 tb->root_switch = NULL;
2067 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
2070 static int icm_disconnect_pcie_paths(struct tb *tb)
2072 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
2076 static const struct tb_cm_ops icm_fr_ops = {
2077 .driver_ready = icm_driver_ready,
2080 .suspend = icm_suspend,
2081 .complete = icm_complete,
2082 .handle_event = icm_handle_event,
2083 .approve_switch = icm_fr_approve_switch,
2084 .add_switch_key = icm_fr_add_switch_key,
2085 .challenge_switch_key = icm_fr_challenge_switch_key,
2086 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2087 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2088 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
2092 static const struct tb_cm_ops icm_ar_ops = {
2093 .driver_ready = icm_driver_ready,
2096 .suspend = icm_suspend,
2097 .complete = icm_complete,
2098 .runtime_suspend = icm_runtime_suspend,
2099 .runtime_resume = icm_runtime_resume,
2100 .runtime_suspend_switch = icm_runtime_suspend_switch,
2101 .runtime_resume_switch = icm_runtime_resume_switch,
2102 .handle_event = icm_handle_event,
2103 .get_boot_acl = icm_ar_get_boot_acl,
2104 .set_boot_acl = icm_ar_set_boot_acl,
2105 .approve_switch = icm_fr_approve_switch,
2106 .add_switch_key = icm_fr_add_switch_key,
2107 .challenge_switch_key = icm_fr_challenge_switch_key,
2108 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2109 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2110 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
2114 static const struct tb_cm_ops icm_tr_ops = {
2115 .driver_ready = icm_driver_ready,
2118 .suspend = icm_suspend,
2119 .complete = icm_complete,
2120 .runtime_suspend = icm_runtime_suspend,
2121 .runtime_resume = icm_runtime_resume,
2122 .runtime_suspend_switch = icm_runtime_suspend_switch,
2123 .runtime_resume_switch = icm_runtime_resume_switch,
2124 .handle_event = icm_handle_event,
2125 .get_boot_acl = icm_ar_get_boot_acl,
2126 .set_boot_acl = icm_ar_set_boot_acl,
2127 .approve_switch = icm_tr_approve_switch,
2128 .add_switch_key = icm_tr_add_switch_key,
2129 .challenge_switch_key = icm_tr_challenge_switch_key,
2130 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
2131 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2132 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
2136 static const struct tb_cm_ops icm_icl_ops = {
2137 .driver_ready = icm_driver_ready,
2140 .complete = icm_complete,
2141 .runtime_suspend = icm_runtime_suspend,
2142 .runtime_resume = icm_runtime_resume,
2143 .handle_event = icm_handle_event,
2144 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2145 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
2148 struct tb *icm_probe(struct tb_nhi *nhi)
2153 tb = tb_domain_alloc(nhi, sizeof(struct icm));
2158 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
2159 mutex_init(&icm->request_lock);
2161 switch (nhi->pdev->device) {
2162 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2163 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
2164 icm->can_upgrade_nvm = true;
2165 icm->is_supported = icm_fr_is_supported;
2166 icm->get_route = icm_fr_get_route;
2167 icm->save_devices = icm_fr_save_devices;
2168 icm->driver_ready = icm_fr_driver_ready;
2169 icm->device_connected = icm_fr_device_connected;
2170 icm->device_disconnected = icm_fr_device_disconnected;
2171 icm->xdomain_connected = icm_fr_xdomain_connected;
2172 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
2173 tb->cm_ops = &icm_fr_ops;
2176 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
2177 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
2178 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
2179 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
2180 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
2181 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2183 * NVM upgrade has not been tested on Apple systems and
2184 * they don't provide images publicly either. To be on
2185 * the safe side prevent root switch NVM upgrade on Macs
2188 icm->can_upgrade_nvm = !x86_apple_machine;
2189 icm->is_supported = icm_ar_is_supported;
2190 icm->cio_reset = icm_ar_cio_reset;
2191 icm->get_mode = icm_ar_get_mode;
2192 icm->get_route = icm_ar_get_route;
2193 icm->save_devices = icm_fr_save_devices;
2194 icm->driver_ready = icm_ar_driver_ready;
2195 icm->device_connected = icm_fr_device_connected;
2196 icm->device_disconnected = icm_fr_device_disconnected;
2197 icm->xdomain_connected = icm_fr_xdomain_connected;
2198 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
2199 tb->cm_ops = &icm_ar_ops;
2202 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2203 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2204 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2205 icm->can_upgrade_nvm = !x86_apple_machine;
2206 icm->is_supported = icm_ar_is_supported;
2207 icm->cio_reset = icm_tr_cio_reset;
2208 icm->get_mode = icm_ar_get_mode;
2209 icm->driver_ready = icm_tr_driver_ready;
2210 icm->device_connected = icm_tr_device_connected;
2211 icm->device_disconnected = icm_tr_device_disconnected;
2212 icm->xdomain_connected = icm_tr_xdomain_connected;
2213 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2214 tb->cm_ops = &icm_tr_ops;
2217 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
2218 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
2219 icm->is_supported = icm_ar_is_supported;
2220 icm->driver_ready = icm_icl_driver_ready;
2221 icm->set_uuid = icm_icl_set_uuid;
2222 icm->device_connected = icm_icl_device_connected;
2223 icm->device_disconnected = icm_tr_device_disconnected;
2224 icm->xdomain_connected = icm_tr_xdomain_connected;
2225 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2226 icm->rtd3_veto = icm_icl_rtd3_veto;
2227 tb->cm_ops = &icm_icl_ops;
2231 if (!icm->is_supported || !icm->is_supported(tb)) {
2232 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");