Linux 5.1.15
[linux/fpc-iii.git] / drivers / thunderbolt / icm.c
blob8b7f9131e9d1200e4652c0878ae2b44896db6e75
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Internal Thunderbolt Connection Manager. This is a firmware running on
4 * the Thunderbolt host controller performing most of the low-level
5 * handling.
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>
21 #include "ctl.h"
22 #include "nhi_regs.h"
23 #include "tb.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
45 #define ICM_MAX_DEPTH 6
47 /**
48 * struct icm - Internal connection manager private data
49 * @request_lock: Makes sure only one message is send to ICM at time
50 * @rescan_work: Work used to rescan the surviving switches after resume
51 * @upstream_port: Pointer to the PCIe upstream port this host
52 * controller is connected. This is only set for systems
53 * where ICM needs to be started manually
54 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
55 * (only set when @upstream_port is not %NULL)
56 * @safe_mode: ICM is in safe mode
57 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
58 * @rpm: Does the controller support runtime PM (RTD3)
59 * @is_supported: Checks if we can support ICM on this controller
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
69 struct icm {
70 struct mutex request_lock;
71 struct delayed_work rescan_work;
72 struct pci_dev *upstream_port;
73 size_t max_boot_acl;
74 int vnd_cap;
75 bool safe_mode;
76 bool rpm;
77 bool (*is_supported)(struct tb *tb);
78 int (*get_mode)(struct tb *tb);
79 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
80 void (*save_devices)(struct tb *tb);
81 int (*driver_ready)(struct tb *tb,
82 enum tb_security_level *security_level,
83 size_t *nboot_acl, bool *rpm);
84 void (*device_connected)(struct tb *tb,
85 const struct icm_pkg_header *hdr);
86 void (*device_disconnected)(struct tb *tb,
87 const struct icm_pkg_header *hdr);
88 void (*xdomain_connected)(struct tb *tb,
89 const struct icm_pkg_header *hdr);
90 void (*xdomain_disconnected)(struct tb *tb,
91 const struct icm_pkg_header *hdr);
94 struct icm_notification {
95 struct work_struct work;
96 struct icm_pkg_header *pkg;
97 struct tb *tb;
100 struct ep_name_entry {
101 u8 len;
102 u8 type;
103 u8 data[0];
106 #define EP_NAME_INTEL_VSS 0x10
108 /* Intel Vendor specific structure */
109 struct intel_vss {
110 u16 vendor;
111 u16 model;
112 u8 mc;
113 u8 flags;
114 u16 pci_devid;
115 u32 nvm_version;
118 #define INTEL_VSS_FLAGS_RTD3 BIT(0)
120 static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size)
122 const void *end = ep_name + size;
124 while (ep_name < end) {
125 const struct ep_name_entry *ep = ep_name;
127 if (!ep->len)
128 break;
129 if (ep_name + ep->len > end)
130 break;
132 if (ep->type == EP_NAME_INTEL_VSS)
133 return (const struct intel_vss *)ep->data;
135 ep_name += ep->len;
138 return NULL;
141 static inline struct tb *icm_to_tb(struct icm *icm)
143 return ((void *)icm - sizeof(struct tb));
146 static inline u8 phy_port_from_route(u64 route, u8 depth)
148 u8 link;
150 link = depth ? route >> ((depth - 1) * 8) : route;
151 return tb_phy_port_from_link(link);
154 static inline u8 dual_link_from_link(u8 link)
156 return link ? ((link - 1) ^ 0x01) + 1 : 0;
159 static inline u64 get_route(u32 route_hi, u32 route_lo)
161 return (u64)route_hi << 32 | route_lo;
164 static inline u64 get_parent_route(u64 route)
166 int depth = tb_route_length(route);
167 return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
170 static bool icm_match(const struct tb_cfg_request *req,
171 const struct ctl_pkg *pkg)
173 const struct icm_pkg_header *res_hdr = pkg->buffer;
174 const struct icm_pkg_header *req_hdr = req->request;
176 if (pkg->frame.eof != req->response_type)
177 return false;
178 if (res_hdr->code != req_hdr->code)
179 return false;
181 return true;
184 static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
186 const struct icm_pkg_header *hdr = pkg->buffer;
188 if (hdr->packet_id < req->npackets) {
189 size_t offset = hdr->packet_id * req->response_size;
191 memcpy(req->response + offset, pkg->buffer, req->response_size);
194 return hdr->packet_id == hdr->total_packets - 1;
197 static int icm_request(struct tb *tb, const void *request, size_t request_size,
198 void *response, size_t response_size, size_t npackets,
199 unsigned int timeout_msec)
201 struct icm *icm = tb_priv(tb);
202 int retries = 3;
204 do {
205 struct tb_cfg_request *req;
206 struct tb_cfg_result res;
208 req = tb_cfg_request_alloc();
209 if (!req)
210 return -ENOMEM;
212 req->match = icm_match;
213 req->copy = icm_copy;
214 req->request = request;
215 req->request_size = request_size;
216 req->request_type = TB_CFG_PKG_ICM_CMD;
217 req->response = response;
218 req->npackets = npackets;
219 req->response_size = response_size;
220 req->response_type = TB_CFG_PKG_ICM_RESP;
222 mutex_lock(&icm->request_lock);
223 res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
224 mutex_unlock(&icm->request_lock);
226 tb_cfg_request_put(req);
228 if (res.err != -ETIMEDOUT)
229 return res.err == 1 ? -EIO : res.err;
231 usleep_range(20, 50);
232 } while (retries--);
234 return -ETIMEDOUT;
237 static bool icm_fr_is_supported(struct tb *tb)
239 return !x86_apple_machine;
242 static inline int icm_fr_get_switch_index(u32 port)
244 int index;
246 if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
247 return 0;
249 index = port >> ICM_PORT_INDEX_SHIFT;
250 return index != 0xff ? index : 0;
253 static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
255 struct icm_fr_pkg_get_topology_response *switches, *sw;
256 struct icm_fr_pkg_get_topology request = {
257 .hdr = { .code = ICM_GET_TOPOLOGY },
259 size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
260 int ret, index;
261 u8 i;
263 switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
264 if (!switches)
265 return -ENOMEM;
267 ret = icm_request(tb, &request, sizeof(request), switches,
268 sizeof(*switches), npackets, ICM_TIMEOUT);
269 if (ret)
270 goto err_free;
272 sw = &switches[0];
273 index = icm_fr_get_switch_index(sw->ports[link]);
274 if (!index) {
275 ret = -ENODEV;
276 goto err_free;
279 sw = &switches[index];
280 for (i = 1; i < depth; i++) {
281 unsigned int j;
283 if (!(sw->first_data & ICM_SWITCH_USED)) {
284 ret = -ENODEV;
285 goto err_free;
288 for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
289 index = icm_fr_get_switch_index(sw->ports[j]);
290 if (index > sw->switch_index) {
291 sw = &switches[index];
292 break;
297 *route = get_route(sw->route_hi, sw->route_lo);
299 err_free:
300 kfree(switches);
301 return ret;
304 static void icm_fr_save_devices(struct tb *tb)
306 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
309 static int
310 icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
311 size_t *nboot_acl, bool *rpm)
313 struct icm_fr_pkg_driver_ready_response reply;
314 struct icm_pkg_driver_ready request = {
315 .hdr.code = ICM_DRIVER_READY,
317 int ret;
319 memset(&reply, 0, sizeof(reply));
320 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
321 1, ICM_TIMEOUT);
322 if (ret)
323 return ret;
325 if (security_level)
326 *security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
328 return 0;
331 static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
333 struct icm_fr_pkg_approve_device request;
334 struct icm_fr_pkg_approve_device reply;
335 int ret;
337 memset(&request, 0, sizeof(request));
338 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
339 request.hdr.code = ICM_APPROVE_DEVICE;
340 request.connection_id = sw->connection_id;
341 request.connection_key = sw->connection_key;
343 memset(&reply, 0, sizeof(reply));
344 /* Use larger timeout as establishing tunnels can take some time */
345 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
346 1, ICM_APPROVE_TIMEOUT);
347 if (ret)
348 return ret;
350 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
351 tb_warn(tb, "PCIe tunnel creation failed\n");
352 return -EIO;
355 return 0;
358 static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
360 struct icm_fr_pkg_add_device_key request;
361 struct icm_fr_pkg_add_device_key_response reply;
362 int ret;
364 memset(&request, 0, sizeof(request));
365 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
366 request.hdr.code = ICM_ADD_DEVICE_KEY;
367 request.connection_id = sw->connection_id;
368 request.connection_key = sw->connection_key;
369 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
371 memset(&reply, 0, sizeof(reply));
372 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
373 1, ICM_TIMEOUT);
374 if (ret)
375 return ret;
377 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
378 tb_warn(tb, "Adding key to switch failed\n");
379 return -EIO;
382 return 0;
385 static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
386 const u8 *challenge, u8 *response)
388 struct icm_fr_pkg_challenge_device request;
389 struct icm_fr_pkg_challenge_device_response reply;
390 int ret;
392 memset(&request, 0, sizeof(request));
393 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
394 request.hdr.code = ICM_CHALLENGE_DEVICE;
395 request.connection_id = sw->connection_id;
396 request.connection_key = sw->connection_key;
397 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
399 memset(&reply, 0, sizeof(reply));
400 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
401 1, ICM_TIMEOUT);
402 if (ret)
403 return ret;
405 if (reply.hdr.flags & ICM_FLAGS_ERROR)
406 return -EKEYREJECTED;
407 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
408 return -ENOKEY;
410 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
412 return 0;
415 static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
417 struct icm_fr_pkg_approve_xdomain_response reply;
418 struct icm_fr_pkg_approve_xdomain request;
419 int ret;
421 memset(&request, 0, sizeof(request));
422 request.hdr.code = ICM_APPROVE_XDOMAIN;
423 request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
424 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
426 request.transmit_path = xd->transmit_path;
427 request.transmit_ring = xd->transmit_ring;
428 request.receive_path = xd->receive_path;
429 request.receive_ring = xd->receive_ring;
431 memset(&reply, 0, sizeof(reply));
432 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
433 1, ICM_TIMEOUT);
434 if (ret)
435 return ret;
437 if (reply.hdr.flags & ICM_FLAGS_ERROR)
438 return -EIO;
440 return 0;
443 static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
445 u8 phy_port;
446 u8 cmd;
448 phy_port = tb_phy_port_from_link(xd->link);
449 if (phy_port == 0)
450 cmd = NHI_MAILBOX_DISCONNECT_PA;
451 else
452 cmd = NHI_MAILBOX_DISCONNECT_PB;
454 nhi_mailbox_cmd(tb->nhi, cmd, 1);
455 usleep_range(10, 50);
456 nhi_mailbox_cmd(tb->nhi, cmd, 2);
457 return 0;
460 static void add_switch(struct tb_switch *parent_sw, u64 route,
461 const uuid_t *uuid, const u8 *ep_name,
462 size_t ep_name_size, u8 connection_id, u8 connection_key,
463 u8 link, u8 depth, enum tb_security_level security_level,
464 bool authorized, bool boot)
466 const struct intel_vss *vss;
467 struct tb_switch *sw;
469 pm_runtime_get_sync(&parent_sw->dev);
471 sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route);
472 if (!sw)
473 goto out;
475 sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
476 if (!sw->uuid) {
477 tb_sw_warn(sw, "cannot allocate memory for switch\n");
478 tb_switch_put(sw);
479 goto out;
481 sw->connection_id = connection_id;
482 sw->connection_key = connection_key;
483 sw->link = link;
484 sw->depth = depth;
485 sw->authorized = authorized;
486 sw->security_level = security_level;
487 sw->boot = boot;
489 vss = parse_intel_vss(ep_name, ep_name_size);
490 if (vss)
491 sw->rpm = !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
493 /* Link the two switches now */
494 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
495 tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
497 if (tb_switch_add(sw)) {
498 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
499 tb_switch_put(sw);
502 out:
503 pm_runtime_mark_last_busy(&parent_sw->dev);
504 pm_runtime_put_autosuspend(&parent_sw->dev);
507 static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
508 u64 route, u8 connection_id, u8 connection_key,
509 u8 link, u8 depth, bool boot)
511 /* Disconnect from parent */
512 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
513 /* Re-connect via updated port*/
514 tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
516 /* Update with the new addressing information */
517 sw->config.route_hi = upper_32_bits(route);
518 sw->config.route_lo = lower_32_bits(route);
519 sw->connection_id = connection_id;
520 sw->connection_key = connection_key;
521 sw->link = link;
522 sw->depth = depth;
523 sw->boot = boot;
525 /* This switch still exists */
526 sw->is_unplugged = false;
529 static void remove_switch(struct tb_switch *sw)
531 struct tb_switch *parent_sw;
533 parent_sw = tb_to_switch(sw->dev.parent);
534 tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
535 tb_switch_remove(sw);
538 static void add_xdomain(struct tb_switch *sw, u64 route,
539 const uuid_t *local_uuid, const uuid_t *remote_uuid,
540 u8 link, u8 depth)
542 struct tb_xdomain *xd;
544 pm_runtime_get_sync(&sw->dev);
546 xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
547 if (!xd)
548 goto out;
550 xd->link = link;
551 xd->depth = depth;
553 tb_port_at(route, sw)->xdomain = xd;
555 tb_xdomain_add(xd);
557 out:
558 pm_runtime_mark_last_busy(&sw->dev);
559 pm_runtime_put_autosuspend(&sw->dev);
562 static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
564 xd->link = link;
565 xd->route = route;
566 xd->is_unplugged = false;
569 static void remove_xdomain(struct tb_xdomain *xd)
571 struct tb_switch *sw;
573 sw = tb_to_switch(xd->dev.parent);
574 tb_port_at(xd->route, sw)->xdomain = NULL;
575 tb_xdomain_remove(xd);
578 static void
579 icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
581 const struct icm_fr_event_device_connected *pkg =
582 (const struct icm_fr_event_device_connected *)hdr;
583 enum tb_security_level security_level;
584 struct tb_switch *sw, *parent_sw;
585 struct icm *icm = tb_priv(tb);
586 bool authorized = false;
587 struct tb_xdomain *xd;
588 u8 link, depth;
589 bool boot;
590 u64 route;
591 int ret;
593 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
594 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
595 ICM_LINK_INFO_DEPTH_SHIFT;
596 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
597 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
598 ICM_FLAGS_SLEVEL_SHIFT;
599 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
601 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
602 tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
603 link, depth);
604 return;
607 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
608 if (sw) {
609 u8 phy_port, sw_phy_port;
611 parent_sw = tb_to_switch(sw->dev.parent);
612 sw_phy_port = tb_phy_port_from_link(sw->link);
613 phy_port = tb_phy_port_from_link(link);
616 * On resume ICM will send us connected events for the
617 * devices that still are present. However, that
618 * information might have changed for example by the
619 * fact that a switch on a dual-link connection might
620 * have been enumerated using the other link now. Make
621 * sure our book keeping matches that.
623 if (sw->depth == depth && sw_phy_port == phy_port &&
624 !!sw->authorized == authorized) {
626 * It was enumerated through another link so update
627 * route string accordingly.
629 if (sw->link != link) {
630 ret = icm->get_route(tb, link, depth, &route);
631 if (ret) {
632 tb_err(tb, "failed to update route string for switch at %u.%u\n",
633 link, depth);
634 tb_switch_put(sw);
635 return;
637 } else {
638 route = tb_route(sw);
641 update_switch(parent_sw, sw, route, pkg->connection_id,
642 pkg->connection_key, link, depth, boot);
643 tb_switch_put(sw);
644 return;
648 * User connected the same switch to another physical
649 * port or to another part of the topology. Remove the
650 * existing switch now before adding the new one.
652 remove_switch(sw);
653 tb_switch_put(sw);
657 * If the switch was not found by UUID, look for a switch on
658 * same physical port (taking possible link aggregation into
659 * account) and depth. If we found one it is definitely a stale
660 * one so remove it first.
662 sw = tb_switch_find_by_link_depth(tb, link, depth);
663 if (!sw) {
664 u8 dual_link;
666 dual_link = dual_link_from_link(link);
667 if (dual_link)
668 sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
670 if (sw) {
671 remove_switch(sw);
672 tb_switch_put(sw);
675 /* Remove existing XDomain connection if found */
676 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
677 if (xd) {
678 remove_xdomain(xd);
679 tb_xdomain_put(xd);
682 parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
683 if (!parent_sw) {
684 tb_err(tb, "failed to find parent switch for %u.%u\n",
685 link, depth);
686 return;
689 ret = icm->get_route(tb, link, depth, &route);
690 if (ret) {
691 tb_err(tb, "failed to find route string for switch at %u.%u\n",
692 link, depth);
693 tb_switch_put(parent_sw);
694 return;
697 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
698 sizeof(pkg->ep_name), pkg->connection_id,
699 pkg->connection_key, link, depth, security_level,
700 authorized, boot);
702 tb_switch_put(parent_sw);
705 static void
706 icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
708 const struct icm_fr_event_device_disconnected *pkg =
709 (const struct icm_fr_event_device_disconnected *)hdr;
710 struct tb_switch *sw;
711 u8 link, depth;
713 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
714 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
715 ICM_LINK_INFO_DEPTH_SHIFT;
717 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
718 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
719 return;
722 sw = tb_switch_find_by_link_depth(tb, link, depth);
723 if (!sw) {
724 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
725 depth);
726 return;
729 remove_switch(sw);
730 tb_switch_put(sw);
733 static void
734 icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
736 const struct icm_fr_event_xdomain_connected *pkg =
737 (const struct icm_fr_event_xdomain_connected *)hdr;
738 struct tb_xdomain *xd;
739 struct tb_switch *sw;
740 u8 link, depth;
741 u64 route;
743 link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
744 depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
745 ICM_LINK_INFO_DEPTH_SHIFT;
747 if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
748 tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
749 return;
752 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
754 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
755 if (xd) {
756 u8 xd_phy_port, phy_port;
758 xd_phy_port = phy_port_from_route(xd->route, xd->depth);
759 phy_port = phy_port_from_route(route, depth);
761 if (xd->depth == depth && xd_phy_port == phy_port) {
762 update_xdomain(xd, route, link);
763 tb_xdomain_put(xd);
764 return;
768 * If we find an existing XDomain connection remove it
769 * now. We need to go through login handshake and
770 * everything anyway to be able to re-establish the
771 * connection.
773 remove_xdomain(xd);
774 tb_xdomain_put(xd);
778 * Look if there already exists an XDomain in the same place
779 * than the new one and in that case remove it because it is
780 * most likely another host that got disconnected.
782 xd = tb_xdomain_find_by_link_depth(tb, link, depth);
783 if (!xd) {
784 u8 dual_link;
786 dual_link = dual_link_from_link(link);
787 if (dual_link)
788 xd = tb_xdomain_find_by_link_depth(tb, dual_link,
789 depth);
791 if (xd) {
792 remove_xdomain(xd);
793 tb_xdomain_put(xd);
797 * If the user disconnected a switch during suspend and
798 * connected another host to the same port, remove the switch
799 * first.
801 sw = get_switch_at_route(tb->root_switch, route);
802 if (sw)
803 remove_switch(sw);
805 sw = tb_switch_find_by_link_depth(tb, link, depth);
806 if (!sw) {
807 tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
808 depth);
809 return;
812 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
813 depth);
814 tb_switch_put(sw);
817 static void
818 icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
820 const struct icm_fr_event_xdomain_disconnected *pkg =
821 (const struct icm_fr_event_xdomain_disconnected *)hdr;
822 struct tb_xdomain *xd;
825 * If the connection is through one or multiple devices, the
826 * XDomain device is removed along with them so it is fine if we
827 * cannot find it here.
829 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
830 if (xd) {
831 remove_xdomain(xd);
832 tb_xdomain_put(xd);
836 static int
837 icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
838 size_t *nboot_acl, bool *rpm)
840 struct icm_tr_pkg_driver_ready_response reply;
841 struct icm_pkg_driver_ready request = {
842 .hdr.code = ICM_DRIVER_READY,
844 int ret;
846 memset(&reply, 0, sizeof(reply));
847 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
848 1, 20000);
849 if (ret)
850 return ret;
852 if (security_level)
853 *security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
854 if (nboot_acl)
855 *nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
856 ICM_TR_INFO_BOOT_ACL_SHIFT;
857 if (rpm)
858 *rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
860 return 0;
863 static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
865 struct icm_tr_pkg_approve_device request;
866 struct icm_tr_pkg_approve_device reply;
867 int ret;
869 memset(&request, 0, sizeof(request));
870 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
871 request.hdr.code = ICM_APPROVE_DEVICE;
872 request.route_lo = sw->config.route_lo;
873 request.route_hi = sw->config.route_hi;
874 request.connection_id = sw->connection_id;
876 memset(&reply, 0, sizeof(reply));
877 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
878 1, ICM_APPROVE_TIMEOUT);
879 if (ret)
880 return ret;
882 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
883 tb_warn(tb, "PCIe tunnel creation failed\n");
884 return -EIO;
887 return 0;
890 static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
892 struct icm_tr_pkg_add_device_key_response reply;
893 struct icm_tr_pkg_add_device_key request;
894 int ret;
896 memset(&request, 0, sizeof(request));
897 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
898 request.hdr.code = ICM_ADD_DEVICE_KEY;
899 request.route_lo = sw->config.route_lo;
900 request.route_hi = sw->config.route_hi;
901 request.connection_id = sw->connection_id;
902 memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
904 memset(&reply, 0, sizeof(reply));
905 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
906 1, ICM_TIMEOUT);
907 if (ret)
908 return ret;
910 if (reply.hdr.flags & ICM_FLAGS_ERROR) {
911 tb_warn(tb, "Adding key to switch failed\n");
912 return -EIO;
915 return 0;
918 static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
919 const u8 *challenge, u8 *response)
921 struct icm_tr_pkg_challenge_device_response reply;
922 struct icm_tr_pkg_challenge_device request;
923 int ret;
925 memset(&request, 0, sizeof(request));
926 memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
927 request.hdr.code = ICM_CHALLENGE_DEVICE;
928 request.route_lo = sw->config.route_lo;
929 request.route_hi = sw->config.route_hi;
930 request.connection_id = sw->connection_id;
931 memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
933 memset(&reply, 0, sizeof(reply));
934 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
935 1, ICM_TIMEOUT);
936 if (ret)
937 return ret;
939 if (reply.hdr.flags & ICM_FLAGS_ERROR)
940 return -EKEYREJECTED;
941 if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
942 return -ENOKEY;
944 memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
946 return 0;
949 static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
951 struct icm_tr_pkg_approve_xdomain_response reply;
952 struct icm_tr_pkg_approve_xdomain request;
953 int ret;
955 memset(&request, 0, sizeof(request));
956 request.hdr.code = ICM_APPROVE_XDOMAIN;
957 request.route_hi = upper_32_bits(xd->route);
958 request.route_lo = lower_32_bits(xd->route);
959 request.transmit_path = xd->transmit_path;
960 request.transmit_ring = xd->transmit_ring;
961 request.receive_path = xd->receive_path;
962 request.receive_ring = xd->receive_ring;
963 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
965 memset(&reply, 0, sizeof(reply));
966 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
967 1, ICM_TIMEOUT);
968 if (ret)
969 return ret;
971 if (reply.hdr.flags & ICM_FLAGS_ERROR)
972 return -EIO;
974 return 0;
977 static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
978 int stage)
980 struct icm_tr_pkg_disconnect_xdomain_response reply;
981 struct icm_tr_pkg_disconnect_xdomain request;
982 int ret;
984 memset(&request, 0, sizeof(request));
985 request.hdr.code = ICM_DISCONNECT_XDOMAIN;
986 request.stage = stage;
987 request.route_hi = upper_32_bits(xd->route);
988 request.route_lo = lower_32_bits(xd->route);
989 memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
991 memset(&reply, 0, sizeof(reply));
992 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
993 1, ICM_TIMEOUT);
994 if (ret)
995 return ret;
997 if (reply.hdr.flags & ICM_FLAGS_ERROR)
998 return -EIO;
1000 return 0;
1003 static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1005 int ret;
1007 ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1008 if (ret)
1009 return ret;
1011 usleep_range(10, 50);
1012 return icm_tr_xdomain_tear_down(tb, xd, 2);
1015 static void
1016 icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1018 const struct icm_tr_event_device_connected *pkg =
1019 (const struct icm_tr_event_device_connected *)hdr;
1020 enum tb_security_level security_level;
1021 struct tb_switch *sw, *parent_sw;
1022 struct tb_xdomain *xd;
1023 bool authorized, boot;
1024 u64 route;
1027 * Currently we don't use the QoS information coming with the
1028 * device connected message so simply just ignore that extra
1029 * packet for now.
1031 if (pkg->hdr.packet_id)
1032 return;
1034 route = get_route(pkg->route_hi, pkg->route_lo);
1035 authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1036 security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1037 ICM_FLAGS_SLEVEL_SHIFT;
1038 boot = pkg->link_info & ICM_LINK_INFO_BOOT;
1040 if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1041 tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1042 route);
1043 return;
1046 sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1047 if (sw) {
1048 /* Update the switch if it is still in the same place */
1049 if (tb_route(sw) == route && !!sw->authorized == authorized) {
1050 parent_sw = tb_to_switch(sw->dev.parent);
1051 update_switch(parent_sw, sw, route, pkg->connection_id,
1052 0, 0, 0, boot);
1053 tb_switch_put(sw);
1054 return;
1057 remove_switch(sw);
1058 tb_switch_put(sw);
1061 /* Another switch with the same address */
1062 sw = tb_switch_find_by_route(tb, route);
1063 if (sw) {
1064 remove_switch(sw);
1065 tb_switch_put(sw);
1068 /* XDomain connection with the same address */
1069 xd = tb_xdomain_find_by_route(tb, route);
1070 if (xd) {
1071 remove_xdomain(xd);
1072 tb_xdomain_put(xd);
1075 parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1076 if (!parent_sw) {
1077 tb_err(tb, "failed to find parent switch for %llx\n", route);
1078 return;
1081 add_switch(parent_sw, route, &pkg->ep_uuid, (const u8 *)pkg->ep_name,
1082 sizeof(pkg->ep_name), pkg->connection_id,
1083 0, 0, 0, security_level, authorized, boot);
1085 tb_switch_put(parent_sw);
1088 static void
1089 icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1091 const struct icm_tr_event_device_disconnected *pkg =
1092 (const struct icm_tr_event_device_disconnected *)hdr;
1093 struct tb_switch *sw;
1094 u64 route;
1096 route = get_route(pkg->route_hi, pkg->route_lo);
1098 sw = tb_switch_find_by_route(tb, route);
1099 if (!sw) {
1100 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1101 return;
1104 remove_switch(sw);
1105 tb_switch_put(sw);
1108 static void
1109 icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1111 const struct icm_tr_event_xdomain_connected *pkg =
1112 (const struct icm_tr_event_xdomain_connected *)hdr;
1113 struct tb_xdomain *xd;
1114 struct tb_switch *sw;
1115 u64 route;
1117 if (!tb->root_switch)
1118 return;
1120 route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1122 xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1123 if (xd) {
1124 if (xd->route == route) {
1125 update_xdomain(xd, route, 0);
1126 tb_xdomain_put(xd);
1127 return;
1130 remove_xdomain(xd);
1131 tb_xdomain_put(xd);
1134 /* An existing xdomain with the same address */
1135 xd = tb_xdomain_find_by_route(tb, route);
1136 if (xd) {
1137 remove_xdomain(xd);
1138 tb_xdomain_put(xd);
1142 * If the user disconnected a switch during suspend and
1143 * connected another host to the same port, remove the switch
1144 * first.
1146 sw = get_switch_at_route(tb->root_switch, route);
1147 if (sw)
1148 remove_switch(sw);
1150 sw = tb_switch_find_by_route(tb, get_parent_route(route));
1151 if (!sw) {
1152 tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1153 return;
1156 add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1157 tb_switch_put(sw);
1160 static void
1161 icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1163 const struct icm_tr_event_xdomain_disconnected *pkg =
1164 (const struct icm_tr_event_xdomain_disconnected *)hdr;
1165 struct tb_xdomain *xd;
1166 u64 route;
1168 route = get_route(pkg->route_hi, pkg->route_lo);
1170 xd = tb_xdomain_find_by_route(tb, route);
1171 if (xd) {
1172 remove_xdomain(xd);
1173 tb_xdomain_put(xd);
1177 static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1179 struct pci_dev *parent;
1181 parent = pci_upstream_bridge(pdev);
1182 while (parent) {
1183 if (!pci_is_pcie(parent))
1184 return NULL;
1185 if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1186 break;
1187 parent = pci_upstream_bridge(parent);
1190 if (!parent)
1191 return NULL;
1193 switch (parent->device) {
1194 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1195 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1196 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1197 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1198 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1199 return parent;
1202 return NULL;
1205 static bool icm_ar_is_supported(struct tb *tb)
1207 struct pci_dev *upstream_port;
1208 struct icm *icm = tb_priv(tb);
1211 * Starting from Alpine Ridge we can use ICM on Apple machines
1212 * as well. We just need to reset and re-enable it first.
1214 if (!x86_apple_machine)
1215 return true;
1218 * Find the upstream PCIe port in case we need to do reset
1219 * through its vendor specific registers.
1221 upstream_port = get_upstream_port(tb->nhi->pdev);
1222 if (upstream_port) {
1223 int cap;
1225 cap = pci_find_ext_capability(upstream_port,
1226 PCI_EXT_CAP_ID_VNDR);
1227 if (cap > 0) {
1228 icm->upstream_port = upstream_port;
1229 icm->vnd_cap = cap;
1231 return true;
1235 return false;
1238 static int icm_ar_get_mode(struct tb *tb)
1240 struct tb_nhi *nhi = tb->nhi;
1241 int retries = 60;
1242 u32 val;
1244 do {
1245 val = ioread32(nhi->iobase + REG_FW_STS);
1246 if (val & REG_FW_STS_NVM_AUTH_DONE)
1247 break;
1248 msleep(50);
1249 } while (--retries);
1251 if (!retries) {
1252 dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1253 return -ENODEV;
1256 return nhi_mailbox_mode(nhi);
1259 static int
1260 icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1261 size_t *nboot_acl, bool *rpm)
1263 struct icm_ar_pkg_driver_ready_response reply;
1264 struct icm_pkg_driver_ready request = {
1265 .hdr.code = ICM_DRIVER_READY,
1267 int ret;
1269 memset(&reply, 0, sizeof(reply));
1270 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1271 1, ICM_TIMEOUT);
1272 if (ret)
1273 return ret;
1275 if (security_level)
1276 *security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1277 if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1278 *nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1279 ICM_AR_INFO_BOOT_ACL_SHIFT;
1280 if (rpm)
1281 *rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1283 return 0;
1286 static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1288 struct icm_ar_pkg_get_route_response reply;
1289 struct icm_ar_pkg_get_route request = {
1290 .hdr = { .code = ICM_GET_ROUTE },
1291 .link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1293 int ret;
1295 memset(&reply, 0, sizeof(reply));
1296 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1297 1, ICM_TIMEOUT);
1298 if (ret)
1299 return ret;
1301 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1302 return -EIO;
1304 *route = get_route(reply.route_hi, reply.route_lo);
1305 return 0;
1308 static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1310 struct icm_ar_pkg_preboot_acl_response reply;
1311 struct icm_ar_pkg_preboot_acl request = {
1312 .hdr = { .code = ICM_PREBOOT_ACL },
1314 int ret, i;
1316 memset(&reply, 0, sizeof(reply));
1317 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1318 1, ICM_TIMEOUT);
1319 if (ret)
1320 return ret;
1322 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1323 return -EIO;
1325 for (i = 0; i < nuuids; i++) {
1326 u32 *uuid = (u32 *)&uuids[i];
1328 uuid[0] = reply.acl[i].uuid_lo;
1329 uuid[1] = reply.acl[i].uuid_hi;
1331 if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1332 /* Map empty entries to null UUID */
1333 uuid[0] = 0;
1334 uuid[1] = 0;
1335 } else if (uuid[0] != 0 || uuid[1] != 0) {
1336 /* Upper two DWs are always one's */
1337 uuid[2] = 0xffffffff;
1338 uuid[3] = 0xffffffff;
1342 return ret;
1345 static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1346 size_t nuuids)
1348 struct icm_ar_pkg_preboot_acl_response reply;
1349 struct icm_ar_pkg_preboot_acl request = {
1350 .hdr = {
1351 .code = ICM_PREBOOT_ACL,
1352 .flags = ICM_FLAGS_WRITE,
1355 int ret, i;
1357 for (i = 0; i < nuuids; i++) {
1358 const u32 *uuid = (const u32 *)&uuids[i];
1360 if (uuid_is_null(&uuids[i])) {
1362 * Map null UUID to the empty (all one) entries
1363 * for ICM.
1365 request.acl[i].uuid_lo = 0xffffffff;
1366 request.acl[i].uuid_hi = 0xffffffff;
1367 } else {
1368 /* Two high DWs need to be set to all one */
1369 if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1370 return -EINVAL;
1372 request.acl[i].uuid_lo = uuid[0];
1373 request.acl[i].uuid_hi = uuid[1];
1377 memset(&reply, 0, sizeof(reply));
1378 ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1379 1, ICM_TIMEOUT);
1380 if (ret)
1381 return ret;
1383 if (reply.hdr.flags & ICM_FLAGS_ERROR)
1384 return -EIO;
1386 return 0;
1389 static void icm_handle_notification(struct work_struct *work)
1391 struct icm_notification *n = container_of(work, typeof(*n), work);
1392 struct tb *tb = n->tb;
1393 struct icm *icm = tb_priv(tb);
1395 mutex_lock(&tb->lock);
1398 * When the domain is stopped we flush its workqueue but before
1399 * that the root switch is removed. In that case we should treat
1400 * the queued events as being canceled.
1402 if (tb->root_switch) {
1403 switch (n->pkg->code) {
1404 case ICM_EVENT_DEVICE_CONNECTED:
1405 icm->device_connected(tb, n->pkg);
1406 break;
1407 case ICM_EVENT_DEVICE_DISCONNECTED:
1408 icm->device_disconnected(tb, n->pkg);
1409 break;
1410 case ICM_EVENT_XDOMAIN_CONNECTED:
1411 icm->xdomain_connected(tb, n->pkg);
1412 break;
1413 case ICM_EVENT_XDOMAIN_DISCONNECTED:
1414 icm->xdomain_disconnected(tb, n->pkg);
1415 break;
1419 mutex_unlock(&tb->lock);
1421 kfree(n->pkg);
1422 kfree(n);
1425 static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1426 const void *buf, size_t size)
1428 struct icm_notification *n;
1430 n = kmalloc(sizeof(*n), GFP_KERNEL);
1431 if (!n)
1432 return;
1434 INIT_WORK(&n->work, icm_handle_notification);
1435 n->pkg = kmemdup(buf, size, GFP_KERNEL);
1436 n->tb = tb;
1438 queue_work(tb->wq, &n->work);
1441 static int
1442 __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1443 size_t *nboot_acl, bool *rpm)
1445 struct icm *icm = tb_priv(tb);
1446 unsigned int retries = 50;
1447 int ret;
1449 ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
1450 if (ret) {
1451 tb_err(tb, "failed to send driver ready to ICM\n");
1452 return ret;
1456 * Hold on here until the switch config space is accessible so
1457 * that we can read root switch config successfully.
1459 do {
1460 struct tb_cfg_result res;
1461 u32 tmp;
1463 res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1464 0, 1, 100);
1465 if (!res.err)
1466 return 0;
1468 msleep(50);
1469 } while (--retries);
1471 tb_err(tb, "failed to read root switch config space, giving up\n");
1472 return -ETIMEDOUT;
1475 static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
1477 unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
1478 u32 cmd;
1480 do {
1481 pci_read_config_dword(icm->upstream_port,
1482 icm->vnd_cap + PCIE2CIO_CMD, &cmd);
1483 if (!(cmd & PCIE2CIO_CMD_START)) {
1484 if (cmd & PCIE2CIO_CMD_TIMEOUT)
1485 break;
1486 return 0;
1489 msleep(50);
1490 } while (time_before(jiffies, end));
1492 return -ETIMEDOUT;
1495 static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
1496 unsigned int port, unsigned int index, u32 *data)
1498 struct pci_dev *pdev = icm->upstream_port;
1499 int ret, vnd_cap = icm->vnd_cap;
1500 u32 cmd;
1502 cmd = index;
1503 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
1504 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
1505 cmd |= PCIE2CIO_CMD_START;
1506 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
1508 ret = pci2cio_wait_completion(icm, 5000);
1509 if (ret)
1510 return ret;
1512 pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
1513 return 0;
1516 static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
1517 unsigned int port, unsigned int index, u32 data)
1519 struct pci_dev *pdev = icm->upstream_port;
1520 int vnd_cap = icm->vnd_cap;
1521 u32 cmd;
1523 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
1525 cmd = index;
1526 cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
1527 cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
1528 cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
1529 pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
1531 return pci2cio_wait_completion(icm, 5000);
1534 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1536 struct icm *icm = tb_priv(tb);
1537 u32 val;
1539 if (!icm->upstream_port)
1540 return -ENODEV;
1542 /* Put ARC to wait for CIO reset event to happen */
1543 val = ioread32(nhi->iobase + REG_FW_STS);
1544 val |= REG_FW_STS_CIO_RESET_REQ;
1545 iowrite32(val, nhi->iobase + REG_FW_STS);
1547 /* Re-start ARC */
1548 val = ioread32(nhi->iobase + REG_FW_STS);
1549 val |= REG_FW_STS_ICM_EN_INVERT;
1550 val |= REG_FW_STS_ICM_EN_CPU;
1551 iowrite32(val, nhi->iobase + REG_FW_STS);
1553 /* Trigger CIO reset now */
1554 return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
1557 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1559 unsigned int retries = 10;
1560 int ret;
1561 u32 val;
1563 /* Check if the ICM firmware is already running */
1564 val = ioread32(nhi->iobase + REG_FW_STS);
1565 if (val & REG_FW_STS_ICM_EN)
1566 return 0;
1568 dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
1570 ret = icm_firmware_reset(tb, nhi);
1571 if (ret)
1572 return ret;
1574 /* Wait until the ICM firmware tells us it is up and running */
1575 do {
1576 /* Check that the ICM firmware is running */
1577 val = ioread32(nhi->iobase + REG_FW_STS);
1578 if (val & REG_FW_STS_NVM_AUTH_DONE)
1579 return 0;
1581 msleep(300);
1582 } while (--retries);
1584 return -ETIMEDOUT;
1587 static int icm_reset_phy_port(struct tb *tb, int phy_port)
1589 struct icm *icm = tb_priv(tb);
1590 u32 state0, state1;
1591 int port0, port1;
1592 u32 val0, val1;
1593 int ret;
1595 if (!icm->upstream_port)
1596 return 0;
1598 if (phy_port) {
1599 port0 = 3;
1600 port1 = 4;
1601 } else {
1602 port0 = 1;
1603 port1 = 2;
1607 * Read link status of both null ports belonging to a single
1608 * physical port.
1610 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1611 if (ret)
1612 return ret;
1613 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1614 if (ret)
1615 return ret;
1617 state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1618 state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1619 state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1620 state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1622 /* If they are both up we need to reset them now */
1623 if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1624 return 0;
1626 val0 |= PHY_PORT_CS1_LINK_DISABLE;
1627 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1628 if (ret)
1629 return ret;
1631 val1 |= PHY_PORT_CS1_LINK_DISABLE;
1632 ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1633 if (ret)
1634 return ret;
1636 /* Wait a bit and then re-enable both ports */
1637 usleep_range(10, 100);
1639 ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1640 if (ret)
1641 return ret;
1642 ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1643 if (ret)
1644 return ret;
1646 val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1647 ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1648 if (ret)
1649 return ret;
1651 val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1652 return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1655 static int icm_firmware_init(struct tb *tb)
1657 struct icm *icm = tb_priv(tb);
1658 struct tb_nhi *nhi = tb->nhi;
1659 int ret;
1661 ret = icm_firmware_start(tb, nhi);
1662 if (ret) {
1663 dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1664 return ret;
1667 if (icm->get_mode) {
1668 ret = icm->get_mode(tb);
1670 switch (ret) {
1671 case NHI_FW_SAFE_MODE:
1672 icm->safe_mode = true;
1673 break;
1675 case NHI_FW_CM_MODE:
1676 /* Ask ICM to accept all Thunderbolt devices */
1677 nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1678 break;
1680 default:
1681 if (ret < 0)
1682 return ret;
1684 tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1685 return -ENODEV;
1690 * Reset both physical ports if there is anything connected to
1691 * them already.
1693 ret = icm_reset_phy_port(tb, 0);
1694 if (ret)
1695 dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1696 ret = icm_reset_phy_port(tb, 1);
1697 if (ret)
1698 dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1700 return 0;
1703 static int icm_driver_ready(struct tb *tb)
1705 struct icm *icm = tb_priv(tb);
1706 int ret;
1708 ret = icm_firmware_init(tb);
1709 if (ret)
1710 return ret;
1712 if (icm->safe_mode) {
1713 tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1714 tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1715 tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1716 return 0;
1719 ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
1720 &icm->rpm);
1721 if (ret)
1722 return ret;
1725 * Make sure the number of supported preboot ACL matches what we
1726 * expect or disable the whole feature.
1728 if (tb->nboot_acl > icm->max_boot_acl)
1729 tb->nboot_acl = 0;
1731 return 0;
1734 static int icm_suspend(struct tb *tb)
1736 struct icm *icm = tb_priv(tb);
1738 if (icm->save_devices)
1739 icm->save_devices(tb);
1741 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1742 return 0;
1746 * Mark all switches (except root switch) below this one unplugged. ICM
1747 * firmware will send us an updated list of switches after we have send
1748 * it driver ready command. If a switch is not in that list it will be
1749 * removed when we perform rescan.
1751 static void icm_unplug_children(struct tb_switch *sw)
1753 unsigned int i;
1755 if (tb_route(sw))
1756 sw->is_unplugged = true;
1758 for (i = 1; i <= sw->config.max_port_number; i++) {
1759 struct tb_port *port = &sw->ports[i];
1761 if (tb_is_upstream_port(port))
1762 continue;
1763 if (port->xdomain) {
1764 port->xdomain->is_unplugged = true;
1765 continue;
1767 if (!port->remote)
1768 continue;
1770 icm_unplug_children(port->remote->sw);
1774 static void icm_free_unplugged_children(struct tb_switch *sw)
1776 unsigned int i;
1778 for (i = 1; i <= sw->config.max_port_number; i++) {
1779 struct tb_port *port = &sw->ports[i];
1781 if (tb_is_upstream_port(port))
1782 continue;
1784 if (port->xdomain && port->xdomain->is_unplugged) {
1785 tb_xdomain_remove(port->xdomain);
1786 port->xdomain = NULL;
1787 continue;
1790 if (!port->remote)
1791 continue;
1793 if (port->remote->sw->is_unplugged) {
1794 tb_switch_remove(port->remote->sw);
1795 port->remote = NULL;
1796 } else {
1797 icm_free_unplugged_children(port->remote->sw);
1802 static void icm_rescan_work(struct work_struct *work)
1804 struct icm *icm = container_of(work, struct icm, rescan_work.work);
1805 struct tb *tb = icm_to_tb(icm);
1807 mutex_lock(&tb->lock);
1808 if (tb->root_switch)
1809 icm_free_unplugged_children(tb->root_switch);
1810 mutex_unlock(&tb->lock);
1813 static void icm_complete(struct tb *tb)
1815 struct icm *icm = tb_priv(tb);
1817 if (tb->nhi->going_away)
1818 return;
1820 icm_unplug_children(tb->root_switch);
1823 * Now all existing children should be resumed, start events
1824 * from ICM to get updated status.
1826 __icm_driver_ready(tb, NULL, NULL, NULL);
1829 * We do not get notifications of devices that have been
1830 * unplugged during suspend so schedule rescan to clean them up
1831 * if any.
1833 queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
1836 static int icm_runtime_suspend(struct tb *tb)
1838 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1839 return 0;
1842 static int icm_runtime_resume(struct tb *tb)
1845 * We can reuse the same resume functionality than with system
1846 * suspend.
1848 icm_complete(tb);
1849 return 0;
1852 static int icm_start(struct tb *tb)
1854 struct icm *icm = tb_priv(tb);
1855 int ret;
1857 if (icm->safe_mode)
1858 tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
1859 else
1860 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1861 if (!tb->root_switch)
1862 return -ENODEV;
1865 * NVM upgrade has not been tested on Apple systems and they
1866 * don't provide images publicly either. To be on the safe side
1867 * prevent root switch NVM upgrade on Macs for now.
1869 tb->root_switch->no_nvm_upgrade = x86_apple_machine;
1870 tb->root_switch->rpm = icm->rpm;
1872 ret = tb_switch_add(tb->root_switch);
1873 if (ret) {
1874 tb_switch_put(tb->root_switch);
1875 tb->root_switch = NULL;
1878 return ret;
1881 static void icm_stop(struct tb *tb)
1883 struct icm *icm = tb_priv(tb);
1885 cancel_delayed_work(&icm->rescan_work);
1886 tb_switch_remove(tb->root_switch);
1887 tb->root_switch = NULL;
1888 nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1891 static int icm_disconnect_pcie_paths(struct tb *tb)
1893 return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
1896 /* Falcon Ridge */
1897 static const struct tb_cm_ops icm_fr_ops = {
1898 .driver_ready = icm_driver_ready,
1899 .start = icm_start,
1900 .stop = icm_stop,
1901 .suspend = icm_suspend,
1902 .complete = icm_complete,
1903 .handle_event = icm_handle_event,
1904 .approve_switch = icm_fr_approve_switch,
1905 .add_switch_key = icm_fr_add_switch_key,
1906 .challenge_switch_key = icm_fr_challenge_switch_key,
1907 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1908 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1909 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1912 /* Alpine Ridge */
1913 static const struct tb_cm_ops icm_ar_ops = {
1914 .driver_ready = icm_driver_ready,
1915 .start = icm_start,
1916 .stop = icm_stop,
1917 .suspend = icm_suspend,
1918 .complete = icm_complete,
1919 .runtime_suspend = icm_runtime_suspend,
1920 .runtime_resume = icm_runtime_resume,
1921 .handle_event = icm_handle_event,
1922 .get_boot_acl = icm_ar_get_boot_acl,
1923 .set_boot_acl = icm_ar_set_boot_acl,
1924 .approve_switch = icm_fr_approve_switch,
1925 .add_switch_key = icm_fr_add_switch_key,
1926 .challenge_switch_key = icm_fr_challenge_switch_key,
1927 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1928 .approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1929 .disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1932 /* Titan Ridge */
1933 static const struct tb_cm_ops icm_tr_ops = {
1934 .driver_ready = icm_driver_ready,
1935 .start = icm_start,
1936 .stop = icm_stop,
1937 .suspend = icm_suspend,
1938 .complete = icm_complete,
1939 .runtime_suspend = icm_runtime_suspend,
1940 .runtime_resume = icm_runtime_resume,
1941 .handle_event = icm_handle_event,
1942 .get_boot_acl = icm_ar_get_boot_acl,
1943 .set_boot_acl = icm_ar_set_boot_acl,
1944 .approve_switch = icm_tr_approve_switch,
1945 .add_switch_key = icm_tr_add_switch_key,
1946 .challenge_switch_key = icm_tr_challenge_switch_key,
1947 .disconnect_pcie_paths = icm_disconnect_pcie_paths,
1948 .approve_xdomain_paths = icm_tr_approve_xdomain_paths,
1949 .disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
1952 struct tb *icm_probe(struct tb_nhi *nhi)
1954 struct icm *icm;
1955 struct tb *tb;
1957 tb = tb_domain_alloc(nhi, sizeof(struct icm));
1958 if (!tb)
1959 return NULL;
1961 icm = tb_priv(tb);
1962 INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
1963 mutex_init(&icm->request_lock);
1965 switch (nhi->pdev->device) {
1966 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1967 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1968 icm->is_supported = icm_fr_is_supported;
1969 icm->get_route = icm_fr_get_route;
1970 icm->save_devices = icm_fr_save_devices;
1971 icm->driver_ready = icm_fr_driver_ready;
1972 icm->device_connected = icm_fr_device_connected;
1973 icm->device_disconnected = icm_fr_device_disconnected;
1974 icm->xdomain_connected = icm_fr_xdomain_connected;
1975 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
1976 tb->cm_ops = &icm_fr_ops;
1977 break;
1979 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
1980 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
1981 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
1982 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
1983 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
1984 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
1985 icm->is_supported = icm_ar_is_supported;
1986 icm->get_mode = icm_ar_get_mode;
1987 icm->get_route = icm_ar_get_route;
1988 icm->save_devices = icm_fr_save_devices;
1989 icm->driver_ready = icm_ar_driver_ready;
1990 icm->device_connected = icm_fr_device_connected;
1991 icm->device_disconnected = icm_fr_device_disconnected;
1992 icm->xdomain_connected = icm_fr_xdomain_connected;
1993 icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
1994 tb->cm_ops = &icm_ar_ops;
1995 break;
1997 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
1998 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
1999 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2000 icm->is_supported = icm_ar_is_supported;
2001 icm->get_mode = icm_ar_get_mode;
2002 icm->driver_ready = icm_tr_driver_ready;
2003 icm->device_connected = icm_tr_device_connected;
2004 icm->device_disconnected = icm_tr_device_disconnected;
2005 icm->xdomain_connected = icm_tr_xdomain_connected;
2006 icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2007 tb->cm_ops = &icm_tr_ops;
2008 break;
2011 if (!icm->is_supported || !icm->is_supported(tb)) {
2012 dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
2013 tb_domain_put(tb);
2014 return NULL;
2017 return tb;