2 * Internal Thunderbolt Connection Manager. This is a firmware running on
3 * the Thunderbolt host controller performing most of the low-level
6 * Copyright (C) 2017, Intel Corporation
7 * Authors: Michael Jamet <michael.jamet@intel.com>
8 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/delay.h>
16 #include <linux/mutex.h>
17 #include <linux/pci.h>
18 #include <linux/platform_data/x86/apple.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
27 #define PCIE2CIO_CMD 0x30
28 #define PCIE2CIO_CMD_TIMEOUT BIT(31)
29 #define PCIE2CIO_CMD_START BIT(30)
30 #define PCIE2CIO_CMD_WRITE BIT(21)
31 #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
32 #define PCIE2CIO_CMD_CS_SHIFT 19
33 #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
34 #define PCIE2CIO_CMD_PORT_SHIFT 13
36 #define PCIE2CIO_WRDATA 0x34
37 #define PCIE2CIO_RDDATA 0x38
39 #define PHY_PORT_CS1 0x37
40 #define PHY_PORT_CS1_LINK_DISABLE BIT(14)
41 #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
42 #define PHY_PORT_CS1_LINK_STATE_SHIFT 26
44 #define ICM_TIMEOUT 5000 /* ms */
45 #define ICM_APPROVE_TIMEOUT 10000 /* ms */
46 #define ICM_MAX_LINK 4
47 #define ICM_MAX_DEPTH 6
50 * struct icm - Internal connection manager private data
51 * @request_lock: Makes sure only one message is send to ICM at time
52 * @rescan_work: Work used to rescan the surviving switches after resume
53 * @upstream_port: Pointer to the PCIe upstream port this host
54 * controller is connected. This is only set for systems
55 * where ICM needs to be started manually
56 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
57 * (only set when @upstream_port is not %NULL)
58 * @safe_mode: ICM is in safe mode
59 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
60 * @is_supported: Checks if we can support ICM on this controller
61 * @get_mode: Read and return the ICM firmware mode (optional)
62 * @get_route: Find a route string for given switch
63 * @driver_ready: Send driver ready message to ICM
64 * @device_connected: Handle device connected ICM message
65 * @device_disconnected: Handle device disconnected ICM message
66 * @xdomain_connected - Handle XDomain connected ICM message
67 * @xdomain_disconnected - Handle XDomain disconnected ICM message
70 struct mutex request_lock
;
71 struct delayed_work rescan_work
;
72 struct pci_dev
*upstream_port
;
76 bool (*is_supported
)(struct tb
*tb
);
77 int (*get_mode
)(struct tb
*tb
);
78 int (*get_route
)(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
);
79 int (*driver_ready
)(struct tb
*tb
,
80 enum tb_security_level
*security_level
,
82 void (*device_connected
)(struct tb
*tb
,
83 const struct icm_pkg_header
*hdr
);
84 void (*device_disconnected
)(struct tb
*tb
,
85 const struct icm_pkg_header
*hdr
);
86 void (*xdomain_connected
)(struct tb
*tb
,
87 const struct icm_pkg_header
*hdr
);
88 void (*xdomain_disconnected
)(struct tb
*tb
,
89 const struct icm_pkg_header
*hdr
);
92 struct icm_notification
{
93 struct work_struct work
;
94 struct icm_pkg_header
*pkg
;
98 static inline struct tb
*icm_to_tb(struct icm
*icm
)
100 return ((void *)icm
- sizeof(struct tb
));
103 static inline u8
phy_port_from_route(u64 route
, u8 depth
)
107 link
= depth
? route
>> ((depth
- 1) * 8) : route
;
108 return tb_phy_port_from_link(link
);
111 static inline u8
dual_link_from_link(u8 link
)
113 return link
? ((link
- 1) ^ 0x01) + 1 : 0;
116 static inline u64
get_route(u32 route_hi
, u32 route_lo
)
118 return (u64
)route_hi
<< 32 | route_lo
;
121 static inline u64
get_parent_route(u64 route
)
123 int depth
= tb_route_length(route
);
124 return depth
? route
& ~(0xffULL
<< (depth
- 1) * TB_ROUTE_SHIFT
) : 0;
127 static bool icm_match(const struct tb_cfg_request
*req
,
128 const struct ctl_pkg
*pkg
)
130 const struct icm_pkg_header
*res_hdr
= pkg
->buffer
;
131 const struct icm_pkg_header
*req_hdr
= req
->request
;
133 if (pkg
->frame
.eof
!= req
->response_type
)
135 if (res_hdr
->code
!= req_hdr
->code
)
141 static bool icm_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
143 const struct icm_pkg_header
*hdr
= pkg
->buffer
;
145 if (hdr
->packet_id
< req
->npackets
) {
146 size_t offset
= hdr
->packet_id
* req
->response_size
;
148 memcpy(req
->response
+ offset
, pkg
->buffer
, req
->response_size
);
151 return hdr
->packet_id
== hdr
->total_packets
- 1;
154 static int icm_request(struct tb
*tb
, const void *request
, size_t request_size
,
155 void *response
, size_t response_size
, size_t npackets
,
156 unsigned int timeout_msec
)
158 struct icm
*icm
= tb_priv(tb
);
162 struct tb_cfg_request
*req
;
163 struct tb_cfg_result res
;
165 req
= tb_cfg_request_alloc();
169 req
->match
= icm_match
;
170 req
->copy
= icm_copy
;
171 req
->request
= request
;
172 req
->request_size
= request_size
;
173 req
->request_type
= TB_CFG_PKG_ICM_CMD
;
174 req
->response
= response
;
175 req
->npackets
= npackets
;
176 req
->response_size
= response_size
;
177 req
->response_type
= TB_CFG_PKG_ICM_RESP
;
179 mutex_lock(&icm
->request_lock
);
180 res
= tb_cfg_request_sync(tb
->ctl
, req
, timeout_msec
);
181 mutex_unlock(&icm
->request_lock
);
183 tb_cfg_request_put(req
);
185 if (res
.err
!= -ETIMEDOUT
)
186 return res
.err
== 1 ? -EIO
: res
.err
;
188 usleep_range(20, 50);
194 static bool icm_fr_is_supported(struct tb
*tb
)
196 return !x86_apple_machine
;
199 static inline int icm_fr_get_switch_index(u32 port
)
203 if ((port
& ICM_PORT_TYPE_MASK
) != TB_TYPE_PORT
)
206 index
= port
>> ICM_PORT_INDEX_SHIFT
;
207 return index
!= 0xff ? index
: 0;
210 static int icm_fr_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
212 struct icm_fr_pkg_get_topology_response
*switches
, *sw
;
213 struct icm_fr_pkg_get_topology request
= {
214 .hdr
= { .code
= ICM_GET_TOPOLOGY
},
216 size_t npackets
= ICM_GET_TOPOLOGY_PACKETS
;
220 switches
= kcalloc(npackets
, sizeof(*switches
), GFP_KERNEL
);
224 ret
= icm_request(tb
, &request
, sizeof(request
), switches
,
225 sizeof(*switches
), npackets
, ICM_TIMEOUT
);
230 index
= icm_fr_get_switch_index(sw
->ports
[link
]);
236 sw
= &switches
[index
];
237 for (i
= 1; i
< depth
; i
++) {
240 if (!(sw
->first_data
& ICM_SWITCH_USED
)) {
245 for (j
= 0; j
< ARRAY_SIZE(sw
->ports
); j
++) {
246 index
= icm_fr_get_switch_index(sw
->ports
[j
]);
247 if (index
> sw
->switch_index
) {
248 sw
= &switches
[index
];
254 *route
= get_route(sw
->route_hi
, sw
->route_lo
);
262 icm_fr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
265 struct icm_fr_pkg_driver_ready_response reply
;
266 struct icm_pkg_driver_ready request
= {
267 .hdr
.code
= ICM_DRIVER_READY
,
271 memset(&reply
, 0, sizeof(reply
));
272 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
278 *security_level
= reply
.security_level
& ICM_FR_SLEVEL_MASK
;
283 static int icm_fr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
285 struct icm_fr_pkg_approve_device request
;
286 struct icm_fr_pkg_approve_device reply
;
289 memset(&request
, 0, sizeof(request
));
290 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
291 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
292 request
.connection_id
= sw
->connection_id
;
293 request
.connection_key
= sw
->connection_key
;
295 memset(&reply
, 0, sizeof(reply
));
296 /* Use larger timeout as establishing tunnels can take some time */
297 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
298 1, ICM_APPROVE_TIMEOUT
);
302 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
303 tb_warn(tb
, "PCIe tunnel creation failed\n");
310 static int icm_fr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
312 struct icm_fr_pkg_add_device_key request
;
313 struct icm_fr_pkg_add_device_key_response reply
;
316 memset(&request
, 0, sizeof(request
));
317 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
318 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
319 request
.connection_id
= sw
->connection_id
;
320 request
.connection_key
= sw
->connection_key
;
321 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
323 memset(&reply
, 0, sizeof(reply
));
324 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
329 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
330 tb_warn(tb
, "Adding key to switch failed\n");
337 static int icm_fr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
338 const u8
*challenge
, u8
*response
)
340 struct icm_fr_pkg_challenge_device request
;
341 struct icm_fr_pkg_challenge_device_response reply
;
344 memset(&request
, 0, sizeof(request
));
345 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
346 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
347 request
.connection_id
= sw
->connection_id
;
348 request
.connection_key
= sw
->connection_key
;
349 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
351 memset(&reply
, 0, sizeof(reply
));
352 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
357 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
358 return -EKEYREJECTED
;
359 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
362 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
367 static int icm_fr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
369 struct icm_fr_pkg_approve_xdomain_response reply
;
370 struct icm_fr_pkg_approve_xdomain request
;
373 memset(&request
, 0, sizeof(request
));
374 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
375 request
.link_info
= xd
->depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| xd
->link
;
376 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
378 request
.transmit_path
= xd
->transmit_path
;
379 request
.transmit_ring
= xd
->transmit_ring
;
380 request
.receive_path
= xd
->receive_path
;
381 request
.receive_ring
= xd
->receive_ring
;
383 memset(&reply
, 0, sizeof(reply
));
384 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
389 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
395 static int icm_fr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
400 phy_port
= tb_phy_port_from_link(xd
->link
);
402 cmd
= NHI_MAILBOX_DISCONNECT_PA
;
404 cmd
= NHI_MAILBOX_DISCONNECT_PB
;
406 nhi_mailbox_cmd(tb
->nhi
, cmd
, 1);
407 usleep_range(10, 50);
408 nhi_mailbox_cmd(tb
->nhi
, cmd
, 2);
412 static void add_switch(struct tb_switch
*parent_sw
, u64 route
,
413 const uuid_t
*uuid
, u8 connection_id
, u8 connection_key
,
414 u8 link
, u8 depth
, enum tb_security_level security_level
,
415 bool authorized
, bool boot
)
417 struct tb_switch
*sw
;
419 sw
= tb_switch_alloc(parent_sw
->tb
, &parent_sw
->dev
, route
);
423 sw
->uuid
= kmemdup(uuid
, sizeof(*uuid
), GFP_KERNEL
);
424 sw
->connection_id
= connection_id
;
425 sw
->connection_key
= connection_key
;
428 sw
->authorized
= authorized
;
429 sw
->security_level
= security_level
;
432 /* Link the two switches now */
433 tb_port_at(route
, parent_sw
)->remote
= tb_upstream_port(sw
);
434 tb_upstream_port(sw
)->remote
= tb_port_at(route
, parent_sw
);
436 if (tb_switch_add(sw
)) {
437 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
443 static void update_switch(struct tb_switch
*parent_sw
, struct tb_switch
*sw
,
444 u64 route
, u8 connection_id
, u8 connection_key
,
445 u8 link
, u8 depth
, bool boot
)
447 /* Disconnect from parent */
448 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
449 /* Re-connect via updated port*/
450 tb_port_at(route
, parent_sw
)->remote
= tb_upstream_port(sw
);
452 /* Update with the new addressing information */
453 sw
->config
.route_hi
= upper_32_bits(route
);
454 sw
->config
.route_lo
= lower_32_bits(route
);
455 sw
->connection_id
= connection_id
;
456 sw
->connection_key
= connection_key
;
461 /* This switch still exists */
462 sw
->is_unplugged
= false;
465 static void remove_switch(struct tb_switch
*sw
)
467 struct tb_switch
*parent_sw
;
469 parent_sw
= tb_to_switch(sw
->dev
.parent
);
470 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
471 tb_switch_remove(sw
);
474 static void add_xdomain(struct tb_switch
*sw
, u64 route
,
475 const uuid_t
*local_uuid
, const uuid_t
*remote_uuid
,
478 struct tb_xdomain
*xd
;
480 xd
= tb_xdomain_alloc(sw
->tb
, &sw
->dev
, route
, local_uuid
, remote_uuid
);
487 tb_port_at(route
, sw
)->xdomain
= xd
;
492 static void update_xdomain(struct tb_xdomain
*xd
, u64 route
, u8 link
)
496 xd
->is_unplugged
= false;
499 static void remove_xdomain(struct tb_xdomain
*xd
)
501 struct tb_switch
*sw
;
503 sw
= tb_to_switch(xd
->dev
.parent
);
504 tb_port_at(xd
->route
, sw
)->xdomain
= NULL
;
505 tb_xdomain_remove(xd
);
509 icm_fr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
511 const struct icm_fr_event_device_connected
*pkg
=
512 (const struct icm_fr_event_device_connected
*)hdr
;
513 enum tb_security_level security_level
;
514 struct tb_switch
*sw
, *parent_sw
;
515 struct icm
*icm
= tb_priv(tb
);
516 bool authorized
= false;
517 struct tb_xdomain
*xd
;
523 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
524 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
525 ICM_LINK_INFO_DEPTH_SHIFT
;
526 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
527 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
528 ICM_FLAGS_SLEVEL_SHIFT
;
529 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
531 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
532 tb_info(tb
, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
537 ret
= icm
->get_route(tb
, link
, depth
, &route
);
539 tb_err(tb
, "failed to find route string for switch at %u.%u\n",
544 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
546 u8 phy_port
, sw_phy_port
;
548 parent_sw
= tb_to_switch(sw
->dev
.parent
);
549 sw_phy_port
= phy_port_from_route(tb_route(sw
), sw
->depth
);
550 phy_port
= phy_port_from_route(route
, depth
);
553 * On resume ICM will send us connected events for the
554 * devices that still are present. However, that
555 * information might have changed for example by the
556 * fact that a switch on a dual-link connection might
557 * have been enumerated using the other link now. Make
558 * sure our book keeping matches that.
560 if (sw
->depth
== depth
&& sw_phy_port
== phy_port
&&
561 !!sw
->authorized
== authorized
) {
562 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
563 pkg
->connection_key
, link
, depth
, boot
);
569 * User connected the same switch to another physical
570 * port or to another part of the topology. Remove the
571 * existing switch now before adding the new one.
578 * If the switch was not found by UUID, look for a switch on
579 * same physical port (taking possible link aggregation into
580 * account) and depth. If we found one it is definitely a stale
581 * one so remove it first.
583 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
587 dual_link
= dual_link_from_link(link
);
589 sw
= tb_switch_find_by_link_depth(tb
, dual_link
, depth
);
596 /* Remove existing XDomain connection if found */
597 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
603 parent_sw
= tb_switch_find_by_link_depth(tb
, link
, depth
- 1);
605 tb_err(tb
, "failed to find parent switch for %u.%u\n",
610 add_switch(parent_sw
, route
, &pkg
->ep_uuid
, pkg
->connection_id
,
611 pkg
->connection_key
, link
, depth
, security_level
,
614 tb_switch_put(parent_sw
);
618 icm_fr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
620 const struct icm_fr_event_device_disconnected
*pkg
=
621 (const struct icm_fr_event_device_disconnected
*)hdr
;
622 struct tb_switch
*sw
;
625 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
626 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
627 ICM_LINK_INFO_DEPTH_SHIFT
;
629 if (link
> ICM_MAX_LINK
|| depth
> ICM_MAX_DEPTH
) {
630 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
634 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
636 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
646 icm_fr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
648 const struct icm_fr_event_xdomain_connected
*pkg
=
649 (const struct icm_fr_event_xdomain_connected
*)hdr
;
650 struct tb_xdomain
*xd
;
651 struct tb_switch
*sw
;
657 * After NVM upgrade adding root switch device fails because we
658 * initiated reset. During that time ICM might still send
659 * XDomain connected message which we ignore here.
661 if (!tb
->root_switch
)
664 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
665 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
666 ICM_LINK_INFO_DEPTH_SHIFT
;
667 approved
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
669 if (link
> ICM_MAX_LINK
|| depth
> ICM_MAX_DEPTH
) {
670 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
674 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
676 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
678 u8 xd_phy_port
, phy_port
;
680 xd_phy_port
= phy_port_from_route(xd
->route
, xd
->depth
);
681 phy_port
= phy_port_from_route(route
, depth
);
683 if (xd
->depth
== depth
&& xd_phy_port
== phy_port
) {
684 update_xdomain(xd
, route
, link
);
690 * If we find an existing XDomain connection remove it
691 * now. We need to go through login handshake and
692 * everything anyway to be able to re-establish the
700 * Look if there already exists an XDomain in the same place
701 * than the new one and in that case remove it because it is
702 * most likely another host that got disconnected.
704 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
708 dual_link
= dual_link_from_link(link
);
710 xd
= tb_xdomain_find_by_link_depth(tb
, dual_link
,
719 * If the user disconnected a switch during suspend and
720 * connected another host to the same port, remove the switch
723 sw
= get_switch_at_route(tb
->root_switch
, route
);
727 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
729 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
734 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, link
,
740 icm_fr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
742 const struct icm_fr_event_xdomain_disconnected
*pkg
=
743 (const struct icm_fr_event_xdomain_disconnected
*)hdr
;
744 struct tb_xdomain
*xd
;
747 * If the connection is through one or multiple devices, the
748 * XDomain device is removed along with them so it is fine if we
749 * cannot find it here.
751 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
759 icm_tr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
762 struct icm_tr_pkg_driver_ready_response reply
;
763 struct icm_pkg_driver_ready request
= {
764 .hdr
.code
= ICM_DRIVER_READY
,
768 memset(&reply
, 0, sizeof(reply
));
769 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
775 *security_level
= reply
.info
& ICM_TR_INFO_SLEVEL_MASK
;
777 *nboot_acl
= (reply
.info
& ICM_TR_INFO_BOOT_ACL_MASK
) >>
778 ICM_TR_INFO_BOOT_ACL_SHIFT
;
782 static int icm_tr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
784 struct icm_tr_pkg_approve_device request
;
785 struct icm_tr_pkg_approve_device reply
;
788 memset(&request
, 0, sizeof(request
));
789 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
790 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
791 request
.route_lo
= sw
->config
.route_lo
;
792 request
.route_hi
= sw
->config
.route_hi
;
793 request
.connection_id
= sw
->connection_id
;
795 memset(&reply
, 0, sizeof(reply
));
796 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
797 1, ICM_APPROVE_TIMEOUT
);
801 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
802 tb_warn(tb
, "PCIe tunnel creation failed\n");
809 static int icm_tr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
811 struct icm_tr_pkg_add_device_key_response reply
;
812 struct icm_tr_pkg_add_device_key request
;
815 memset(&request
, 0, sizeof(request
));
816 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
817 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
818 request
.route_lo
= sw
->config
.route_lo
;
819 request
.route_hi
= sw
->config
.route_hi
;
820 request
.connection_id
= sw
->connection_id
;
821 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
823 memset(&reply
, 0, sizeof(reply
));
824 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
829 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
830 tb_warn(tb
, "Adding key to switch failed\n");
837 static int icm_tr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
838 const u8
*challenge
, u8
*response
)
840 struct icm_tr_pkg_challenge_device_response reply
;
841 struct icm_tr_pkg_challenge_device request
;
844 memset(&request
, 0, sizeof(request
));
845 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
846 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
847 request
.route_lo
= sw
->config
.route_lo
;
848 request
.route_hi
= sw
->config
.route_hi
;
849 request
.connection_id
= sw
->connection_id
;
850 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
852 memset(&reply
, 0, sizeof(reply
));
853 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
858 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
859 return -EKEYREJECTED
;
860 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
863 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
868 static int icm_tr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
870 struct icm_tr_pkg_approve_xdomain_response reply
;
871 struct icm_tr_pkg_approve_xdomain request
;
874 memset(&request
, 0, sizeof(request
));
875 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
876 request
.route_hi
= upper_32_bits(xd
->route
);
877 request
.route_lo
= lower_32_bits(xd
->route
);
878 request
.transmit_path
= xd
->transmit_path
;
879 request
.transmit_ring
= xd
->transmit_ring
;
880 request
.receive_path
= xd
->receive_path
;
881 request
.receive_ring
= xd
->receive_ring
;
882 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
884 memset(&reply
, 0, sizeof(reply
));
885 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
890 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
896 static int icm_tr_xdomain_tear_down(struct tb
*tb
, struct tb_xdomain
*xd
,
899 struct icm_tr_pkg_disconnect_xdomain_response reply
;
900 struct icm_tr_pkg_disconnect_xdomain request
;
903 memset(&request
, 0, sizeof(request
));
904 request
.hdr
.code
= ICM_DISCONNECT_XDOMAIN
;
905 request
.stage
= stage
;
906 request
.route_hi
= upper_32_bits(xd
->route
);
907 request
.route_lo
= lower_32_bits(xd
->route
);
908 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
910 memset(&reply
, 0, sizeof(reply
));
911 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
916 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
922 static int icm_tr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
926 ret
= icm_tr_xdomain_tear_down(tb
, xd
, 1);
930 usleep_range(10, 50);
931 return icm_tr_xdomain_tear_down(tb
, xd
, 2);
935 icm_tr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
937 const struct icm_tr_event_device_connected
*pkg
=
938 (const struct icm_tr_event_device_connected
*)hdr
;
939 enum tb_security_level security_level
;
940 struct tb_switch
*sw
, *parent_sw
;
941 struct tb_xdomain
*xd
;
942 bool authorized
, boot
;
946 * Currently we don't use the QoS information coming with the
947 * device connected message so simply just ignore that extra
950 if (pkg
->hdr
.packet_id
)
954 * After NVM upgrade adding root switch device fails because we
955 * initiated reset. During that time ICM might still send device
956 * connected message which we ignore here.
958 if (!tb
->root_switch
)
961 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
962 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
963 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
964 ICM_FLAGS_SLEVEL_SHIFT
;
965 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
967 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
968 tb_info(tb
, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
973 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
975 /* Update the switch if it is still in the same place */
976 if (tb_route(sw
) == route
&& !!sw
->authorized
== authorized
) {
977 parent_sw
= tb_to_switch(sw
->dev
.parent
);
978 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
988 /* Another switch with the same address */
989 sw
= tb_switch_find_by_route(tb
, route
);
995 /* XDomain connection with the same address */
996 xd
= tb_xdomain_find_by_route(tb
, route
);
1002 parent_sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1004 tb_err(tb
, "failed to find parent switch for %llx\n", route
);
1008 add_switch(parent_sw
, route
, &pkg
->ep_uuid
, pkg
->connection_id
,
1009 0, 0, 0, security_level
, authorized
, boot
);
1011 tb_switch_put(parent_sw
);
1015 icm_tr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1017 const struct icm_tr_event_device_disconnected
*pkg
=
1018 (const struct icm_tr_event_device_disconnected
*)hdr
;
1019 struct tb_switch
*sw
;
1022 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1024 sw
= tb_switch_find_by_route(tb
, route
);
1026 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1035 icm_tr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1037 const struct icm_tr_event_xdomain_connected
*pkg
=
1038 (const struct icm_tr_event_xdomain_connected
*)hdr
;
1039 struct tb_xdomain
*xd
;
1040 struct tb_switch
*sw
;
1043 if (!tb
->root_switch
)
1046 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
1048 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
1050 if (xd
->route
== route
) {
1051 update_xdomain(xd
, route
, 0);
1060 /* An existing xdomain with the same address */
1061 xd
= tb_xdomain_find_by_route(tb
, route
);
1068 * If the user disconnected a switch during suspend and
1069 * connected another host to the same port, remove the switch
1072 sw
= get_switch_at_route(tb
->root_switch
, route
);
1076 sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1078 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1082 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, 0, 0);
1087 icm_tr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1089 const struct icm_tr_event_xdomain_disconnected
*pkg
=
1090 (const struct icm_tr_event_xdomain_disconnected
*)hdr
;
1091 struct tb_xdomain
*xd
;
1094 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1096 xd
= tb_xdomain_find_by_route(tb
, route
);
1103 static struct pci_dev
*get_upstream_port(struct pci_dev
*pdev
)
1105 struct pci_dev
*parent
;
1107 parent
= pci_upstream_bridge(pdev
);
1109 if (!pci_is_pcie(parent
))
1111 if (pci_pcie_type(parent
) == PCI_EXP_TYPE_UPSTREAM
)
1113 parent
= pci_upstream_bridge(parent
);
1119 switch (parent
->device
) {
1120 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1121 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1122 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1123 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1124 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1131 static bool icm_ar_is_supported(struct tb
*tb
)
1133 struct pci_dev
*upstream_port
;
1134 struct icm
*icm
= tb_priv(tb
);
1137 * Starting from Alpine Ridge we can use ICM on Apple machines
1138 * as well. We just need to reset and re-enable it first.
1140 if (!x86_apple_machine
)
1144 * Find the upstream PCIe port in case we need to do reset
1145 * through its vendor specific registers.
1147 upstream_port
= get_upstream_port(tb
->nhi
->pdev
);
1148 if (upstream_port
) {
1151 cap
= pci_find_ext_capability(upstream_port
,
1152 PCI_EXT_CAP_ID_VNDR
);
1154 icm
->upstream_port
= upstream_port
;
1164 static int icm_ar_get_mode(struct tb
*tb
)
1166 struct tb_nhi
*nhi
= tb
->nhi
;
1171 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1172 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1175 } while (--retries
);
1178 dev_err(&nhi
->pdev
->dev
, "ICM firmware not authenticated\n");
1182 return nhi_mailbox_mode(nhi
);
1186 icm_ar_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1189 struct icm_ar_pkg_driver_ready_response reply
;
1190 struct icm_pkg_driver_ready request
= {
1191 .hdr
.code
= ICM_DRIVER_READY
,
1195 memset(&reply
, 0, sizeof(reply
));
1196 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1202 *security_level
= reply
.info
& ICM_AR_INFO_SLEVEL_MASK
;
1203 if (nboot_acl
&& (reply
.info
& ICM_AR_INFO_BOOT_ACL_SUPPORTED
))
1204 *nboot_acl
= (reply
.info
& ICM_AR_INFO_BOOT_ACL_MASK
) >>
1205 ICM_AR_INFO_BOOT_ACL_SHIFT
;
1209 static int icm_ar_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
1211 struct icm_ar_pkg_get_route_response reply
;
1212 struct icm_ar_pkg_get_route request
= {
1213 .hdr
= { .code
= ICM_GET_ROUTE
},
1214 .link_info
= depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| link
,
1218 memset(&reply
, 0, sizeof(reply
));
1219 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1224 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1227 *route
= get_route(reply
.route_hi
, reply
.route_lo
);
1231 static int icm_ar_get_boot_acl(struct tb
*tb
, uuid_t
*uuids
, size_t nuuids
)
1233 struct icm_ar_pkg_preboot_acl_response reply
;
1234 struct icm_ar_pkg_preboot_acl request
= {
1235 .hdr
= { .code
= ICM_PREBOOT_ACL
},
1239 memset(&reply
, 0, sizeof(reply
));
1240 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1245 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1248 for (i
= 0; i
< nuuids
; i
++) {
1249 u32
*uuid
= (u32
*)&uuids
[i
];
1251 uuid
[0] = reply
.acl
[i
].uuid_lo
;
1252 uuid
[1] = reply
.acl
[i
].uuid_hi
;
1254 if (uuid
[0] == 0xffffffff && uuid
[1] == 0xffffffff) {
1255 /* Map empty entries to null UUID */
1258 } else if (uuid
[0] != 0 || uuid
[1] != 0) {
1259 /* Upper two DWs are always one's */
1260 uuid
[2] = 0xffffffff;
1261 uuid
[3] = 0xffffffff;
1268 static int icm_ar_set_boot_acl(struct tb
*tb
, const uuid_t
*uuids
,
1271 struct icm_ar_pkg_preboot_acl_response reply
;
1272 struct icm_ar_pkg_preboot_acl request
= {
1274 .code
= ICM_PREBOOT_ACL
,
1275 .flags
= ICM_FLAGS_WRITE
,
1280 for (i
= 0; i
< nuuids
; i
++) {
1281 const u32
*uuid
= (const u32
*)&uuids
[i
];
1283 if (uuid_is_null(&uuids
[i
])) {
1285 * Map null UUID to the empty (all one) entries
1288 request
.acl
[i
].uuid_lo
= 0xffffffff;
1289 request
.acl
[i
].uuid_hi
= 0xffffffff;
1291 /* Two high DWs need to be set to all one */
1292 if (uuid
[2] != 0xffffffff || uuid
[3] != 0xffffffff)
1295 request
.acl
[i
].uuid_lo
= uuid
[0];
1296 request
.acl
[i
].uuid_hi
= uuid
[1];
1300 memset(&reply
, 0, sizeof(reply
));
1301 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1306 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1312 static void icm_handle_notification(struct work_struct
*work
)
1314 struct icm_notification
*n
= container_of(work
, typeof(*n
), work
);
1315 struct tb
*tb
= n
->tb
;
1316 struct icm
*icm
= tb_priv(tb
);
1318 mutex_lock(&tb
->lock
);
1320 switch (n
->pkg
->code
) {
1321 case ICM_EVENT_DEVICE_CONNECTED
:
1322 icm
->device_connected(tb
, n
->pkg
);
1324 case ICM_EVENT_DEVICE_DISCONNECTED
:
1325 icm
->device_disconnected(tb
, n
->pkg
);
1327 case ICM_EVENT_XDOMAIN_CONNECTED
:
1328 icm
->xdomain_connected(tb
, n
->pkg
);
1330 case ICM_EVENT_XDOMAIN_DISCONNECTED
:
1331 icm
->xdomain_disconnected(tb
, n
->pkg
);
1335 mutex_unlock(&tb
->lock
);
1341 static void icm_handle_event(struct tb
*tb
, enum tb_cfg_pkg_type type
,
1342 const void *buf
, size_t size
)
1344 struct icm_notification
*n
;
1346 n
= kmalloc(sizeof(*n
), GFP_KERNEL
);
1350 INIT_WORK(&n
->work
, icm_handle_notification
);
1351 n
->pkg
= kmemdup(buf
, size
, GFP_KERNEL
);
1354 queue_work(tb
->wq
, &n
->work
);
1358 __icm_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1361 struct icm
*icm
= tb_priv(tb
);
1362 unsigned int retries
= 50;
1365 ret
= icm
->driver_ready(tb
, security_level
, nboot_acl
);
1367 tb_err(tb
, "failed to send driver ready to ICM\n");
1372 * Hold on here until the switch config space is accessible so
1373 * that we can read root switch config successfully.
1376 struct tb_cfg_result res
;
1379 res
= tb_cfg_read_raw(tb
->ctl
, &tmp
, 0, 0, TB_CFG_SWITCH
,
1385 } while (--retries
);
1387 tb_err(tb
, "failed to read root switch config space, giving up\n");
1391 static int pci2cio_wait_completion(struct icm
*icm
, unsigned long timeout_msec
)
1393 unsigned long end
= jiffies
+ msecs_to_jiffies(timeout_msec
);
1397 pci_read_config_dword(icm
->upstream_port
,
1398 icm
->vnd_cap
+ PCIE2CIO_CMD
, &cmd
);
1399 if (!(cmd
& PCIE2CIO_CMD_START
)) {
1400 if (cmd
& PCIE2CIO_CMD_TIMEOUT
)
1406 } while (time_before(jiffies
, end
));
1411 static int pcie2cio_read(struct icm
*icm
, enum tb_cfg_space cs
,
1412 unsigned int port
, unsigned int index
, u32
*data
)
1414 struct pci_dev
*pdev
= icm
->upstream_port
;
1415 int ret
, vnd_cap
= icm
->vnd_cap
;
1419 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
1420 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
1421 cmd
|= PCIE2CIO_CMD_START
;
1422 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
1424 ret
= pci2cio_wait_completion(icm
, 5000);
1428 pci_read_config_dword(pdev
, vnd_cap
+ PCIE2CIO_RDDATA
, data
);
1432 static int pcie2cio_write(struct icm
*icm
, enum tb_cfg_space cs
,
1433 unsigned int port
, unsigned int index
, u32 data
)
1435 struct pci_dev
*pdev
= icm
->upstream_port
;
1436 int vnd_cap
= icm
->vnd_cap
;
1439 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_WRDATA
, data
);
1442 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
1443 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
1444 cmd
|= PCIE2CIO_CMD_WRITE
| PCIE2CIO_CMD_START
;
1445 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
1447 return pci2cio_wait_completion(icm
, 5000);
1450 static int icm_firmware_reset(struct tb
*tb
, struct tb_nhi
*nhi
)
1452 struct icm
*icm
= tb_priv(tb
);
1455 if (!icm
->upstream_port
)
1458 /* Put ARC to wait for CIO reset event to happen */
1459 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1460 val
|= REG_FW_STS_CIO_RESET_REQ
;
1461 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1464 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1465 val
|= REG_FW_STS_ICM_EN_INVERT
;
1466 val
|= REG_FW_STS_ICM_EN_CPU
;
1467 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1469 /* Trigger CIO reset now */
1470 return pcie2cio_write(icm
, TB_CFG_SWITCH
, 0, 0x50, BIT(9));
1473 static int icm_firmware_start(struct tb
*tb
, struct tb_nhi
*nhi
)
1475 unsigned int retries
= 10;
1479 /* Check if the ICM firmware is already running */
1480 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1481 if (val
& REG_FW_STS_ICM_EN
)
1484 dev_info(&nhi
->pdev
->dev
, "starting ICM firmware\n");
1486 ret
= icm_firmware_reset(tb
, nhi
);
1490 /* Wait until the ICM firmware tells us it is up and running */
1492 /* Check that the ICM firmware is running */
1493 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1494 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1498 } while (--retries
);
1503 static int icm_reset_phy_port(struct tb
*tb
, int phy_port
)
1505 struct icm
*icm
= tb_priv(tb
);
1511 if (!icm
->upstream_port
)
1523 * Read link status of both null ports belonging to a single
1526 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1529 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1533 state0
= val0
& PHY_PORT_CS1_LINK_STATE_MASK
;
1534 state0
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1535 state1
= val1
& PHY_PORT_CS1_LINK_STATE_MASK
;
1536 state1
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1538 /* If they are both up we need to reset them now */
1539 if (state0
!= TB_PORT_UP
|| state1
!= TB_PORT_UP
)
1542 val0
|= PHY_PORT_CS1_LINK_DISABLE
;
1543 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1547 val1
|= PHY_PORT_CS1_LINK_DISABLE
;
1548 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1552 /* Wait a bit and then re-enable both ports */
1553 usleep_range(10, 100);
1555 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1558 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1562 val0
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1563 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1567 val1
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1568 return pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1571 static int icm_firmware_init(struct tb
*tb
)
1573 struct icm
*icm
= tb_priv(tb
);
1574 struct tb_nhi
*nhi
= tb
->nhi
;
1577 ret
= icm_firmware_start(tb
, nhi
);
1579 dev_err(&nhi
->pdev
->dev
, "could not start ICM firmware\n");
1583 if (icm
->get_mode
) {
1584 ret
= icm
->get_mode(tb
);
1587 case NHI_FW_SAFE_MODE
:
1588 icm
->safe_mode
= true;
1591 case NHI_FW_CM_MODE
:
1592 /* Ask ICM to accept all Thunderbolt devices */
1593 nhi_mailbox_cmd(nhi
, NHI_MAILBOX_ALLOW_ALL_DEVS
, 0);
1600 tb_err(tb
, "ICM firmware is in wrong mode: %u\n", ret
);
1606 * Reset both physical ports if there is anything connected to
1609 ret
= icm_reset_phy_port(tb
, 0);
1611 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port0\n");
1612 ret
= icm_reset_phy_port(tb
, 1);
1614 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port1\n");
1619 static int icm_driver_ready(struct tb
*tb
)
1621 struct icm
*icm
= tb_priv(tb
);
1624 ret
= icm_firmware_init(tb
);
1628 if (icm
->safe_mode
) {
1629 tb_info(tb
, "Thunderbolt host controller is in safe mode.\n");
1630 tb_info(tb
, "You need to update NVM firmware of the controller before it can be used.\n");
1631 tb_info(tb
, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1635 ret
= __icm_driver_ready(tb
, &tb
->security_level
, &tb
->nboot_acl
);
1640 * Make sure the number of supported preboot ACL matches what we
1641 * expect or disable the whole feature.
1643 if (tb
->nboot_acl
> icm
->max_boot_acl
)
1649 static int icm_suspend(struct tb
*tb
)
1653 ret
= nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_SAVE_DEVS
, 0);
1655 tb_info(tb
, "Ignoring mailbox command error (%d) in %s\n",
1662 * Mark all switches (except root switch) below this one unplugged. ICM
1663 * firmware will send us an updated list of switches after we have send
1664 * it driver ready command. If a switch is not in that list it will be
1665 * removed when we perform rescan.
1667 static void icm_unplug_children(struct tb_switch
*sw
)
1672 sw
->is_unplugged
= true;
1674 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1675 struct tb_port
*port
= &sw
->ports
[i
];
1677 if (tb_is_upstream_port(port
))
1679 if (port
->xdomain
) {
1680 port
->xdomain
->is_unplugged
= true;
1686 icm_unplug_children(port
->remote
->sw
);
1690 static void icm_free_unplugged_children(struct tb_switch
*sw
)
1694 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1695 struct tb_port
*port
= &sw
->ports
[i
];
1697 if (tb_is_upstream_port(port
))
1700 if (port
->xdomain
&& port
->xdomain
->is_unplugged
) {
1701 tb_xdomain_remove(port
->xdomain
);
1702 port
->xdomain
= NULL
;
1709 if (port
->remote
->sw
->is_unplugged
) {
1710 tb_switch_remove(port
->remote
->sw
);
1711 port
->remote
= NULL
;
1713 icm_free_unplugged_children(port
->remote
->sw
);
1718 static void icm_rescan_work(struct work_struct
*work
)
1720 struct icm
*icm
= container_of(work
, struct icm
, rescan_work
.work
);
1721 struct tb
*tb
= icm_to_tb(icm
);
1723 mutex_lock(&tb
->lock
);
1724 if (tb
->root_switch
)
1725 icm_free_unplugged_children(tb
->root_switch
);
1726 mutex_unlock(&tb
->lock
);
1729 static void icm_complete(struct tb
*tb
)
1731 struct icm
*icm
= tb_priv(tb
);
1733 if (tb
->nhi
->going_away
)
1736 icm_unplug_children(tb
->root_switch
);
1739 * Now all existing children should be resumed, start events
1740 * from ICM to get updated status.
1742 __icm_driver_ready(tb
, NULL
, NULL
);
1745 * We do not get notifications of devices that have been
1746 * unplugged during suspend so schedule rescan to clean them up
1749 queue_delayed_work(tb
->wq
, &icm
->rescan_work
, msecs_to_jiffies(500));
1752 static int icm_start(struct tb
*tb
)
1754 struct icm
*icm
= tb_priv(tb
);
1758 tb
->root_switch
= tb_switch_alloc_safe_mode(tb
, &tb
->dev
, 0);
1760 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
1761 if (!tb
->root_switch
)
1765 * NVM upgrade has not been tested on Apple systems and they
1766 * don't provide images publicly either. To be on the safe side
1767 * prevent root switch NVM upgrade on Macs for now.
1769 tb
->root_switch
->no_nvm_upgrade
= x86_apple_machine
;
1771 ret
= tb_switch_add(tb
->root_switch
);
1773 tb_switch_put(tb
->root_switch
);
1774 tb
->root_switch
= NULL
;
1780 static void icm_stop(struct tb
*tb
)
1782 struct icm
*icm
= tb_priv(tb
);
1784 cancel_delayed_work(&icm
->rescan_work
);
1785 tb_switch_remove(tb
->root_switch
);
1786 tb
->root_switch
= NULL
;
1787 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
1790 static int icm_disconnect_pcie_paths(struct tb
*tb
)
1792 return nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DISCONNECT_PCIE_PATHS
, 0);
1796 static const struct tb_cm_ops icm_fr_ops
= {
1797 .driver_ready
= icm_driver_ready
,
1800 .suspend
= icm_suspend
,
1801 .complete
= icm_complete
,
1802 .handle_event
= icm_handle_event
,
1803 .approve_switch
= icm_fr_approve_switch
,
1804 .add_switch_key
= icm_fr_add_switch_key
,
1805 .challenge_switch_key
= icm_fr_challenge_switch_key
,
1806 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
1807 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
1808 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
1812 static const struct tb_cm_ops icm_ar_ops
= {
1813 .driver_ready
= icm_driver_ready
,
1816 .suspend
= icm_suspend
,
1817 .complete
= icm_complete
,
1818 .handle_event
= icm_handle_event
,
1819 .get_boot_acl
= icm_ar_get_boot_acl
,
1820 .set_boot_acl
= icm_ar_set_boot_acl
,
1821 .approve_switch
= icm_fr_approve_switch
,
1822 .add_switch_key
= icm_fr_add_switch_key
,
1823 .challenge_switch_key
= icm_fr_challenge_switch_key
,
1824 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
1825 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
1826 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
1830 static const struct tb_cm_ops icm_tr_ops
= {
1831 .driver_ready
= icm_driver_ready
,
1834 .suspend
= icm_suspend
,
1835 .complete
= icm_complete
,
1836 .handle_event
= icm_handle_event
,
1837 .get_boot_acl
= icm_ar_get_boot_acl
,
1838 .set_boot_acl
= icm_ar_set_boot_acl
,
1839 .approve_switch
= icm_tr_approve_switch
,
1840 .add_switch_key
= icm_tr_add_switch_key
,
1841 .challenge_switch_key
= icm_tr_challenge_switch_key
,
1842 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
1843 .approve_xdomain_paths
= icm_tr_approve_xdomain_paths
,
1844 .disconnect_xdomain_paths
= icm_tr_disconnect_xdomain_paths
,
1847 struct tb
*icm_probe(struct tb_nhi
*nhi
)
1852 tb
= tb_domain_alloc(nhi
, sizeof(struct icm
));
1857 INIT_DELAYED_WORK(&icm
->rescan_work
, icm_rescan_work
);
1858 mutex_init(&icm
->request_lock
);
1860 switch (nhi
->pdev
->device
) {
1861 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI
:
1862 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI
:
1863 icm
->is_supported
= icm_fr_is_supported
;
1864 icm
->get_route
= icm_fr_get_route
;
1865 icm
->driver_ready
= icm_fr_driver_ready
;
1866 icm
->device_connected
= icm_fr_device_connected
;
1867 icm
->device_disconnected
= icm_fr_device_disconnected
;
1868 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
1869 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
1870 tb
->cm_ops
= &icm_fr_ops
;
1873 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI
:
1874 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI
:
1875 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI
:
1876 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI
:
1877 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI
:
1878 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
1879 icm
->is_supported
= icm_ar_is_supported
;
1880 icm
->get_mode
= icm_ar_get_mode
;
1881 icm
->get_route
= icm_ar_get_route
;
1882 icm
->driver_ready
= icm_ar_driver_ready
;
1883 icm
->device_connected
= icm_fr_device_connected
;
1884 icm
->device_disconnected
= icm_fr_device_disconnected
;
1885 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
1886 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
1887 tb
->cm_ops
= &icm_ar_ops
;
1890 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI
:
1891 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI
:
1892 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
1893 icm
->is_supported
= icm_ar_is_supported
;
1894 icm
->get_mode
= icm_ar_get_mode
;
1895 icm
->driver_ready
= icm_tr_driver_ready
;
1896 icm
->device_connected
= icm_tr_device_connected
;
1897 icm
->device_disconnected
= icm_tr_device_disconnected
;
1898 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
1899 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
1900 tb
->cm_ops
= &icm_tr_ops
;
1904 if (!icm
->is_supported
|| !icm
->is_supported(tb
)) {
1905 dev_dbg(&nhi
->pdev
->dev
, "ICM not supported on this controller\n");