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 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
539 u8 phy_port
, sw_phy_port
;
541 parent_sw
= tb_to_switch(sw
->dev
.parent
);
542 sw_phy_port
= tb_phy_port_from_link(sw
->link
);
543 phy_port
= tb_phy_port_from_link(link
);
546 * On resume ICM will send us connected events for the
547 * devices that still are present. However, that
548 * information might have changed for example by the
549 * fact that a switch on a dual-link connection might
550 * have been enumerated using the other link now. Make
551 * sure our book keeping matches that.
553 if (sw
->depth
== depth
&& sw_phy_port
== phy_port
&&
554 !!sw
->authorized
== authorized
) {
556 * It was enumerated through another link so update
557 * route string accordingly.
559 if (sw
->link
!= link
) {
560 ret
= icm
->get_route(tb
, link
, depth
, &route
);
562 tb_err(tb
, "failed to update route string for switch at %u.%u\n",
568 route
= tb_route(sw
);
571 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
572 pkg
->connection_key
, link
, depth
, boot
);
578 * User connected the same switch to another physical
579 * port or to another part of the topology. Remove the
580 * existing switch now before adding the new one.
587 * If the switch was not found by UUID, look for a switch on
588 * same physical port (taking possible link aggregation into
589 * account) and depth. If we found one it is definitely a stale
590 * one so remove it first.
592 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
596 dual_link
= dual_link_from_link(link
);
598 sw
= tb_switch_find_by_link_depth(tb
, dual_link
, depth
);
605 /* Remove existing XDomain connection if found */
606 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
612 parent_sw
= tb_switch_find_by_link_depth(tb
, link
, depth
- 1);
614 tb_err(tb
, "failed to find parent switch for %u.%u\n",
619 ret
= icm
->get_route(tb
, link
, depth
, &route
);
621 tb_err(tb
, "failed to find route string for switch at %u.%u\n",
623 tb_switch_put(parent_sw
);
627 add_switch(parent_sw
, route
, &pkg
->ep_uuid
, pkg
->connection_id
,
628 pkg
->connection_key
, link
, depth
, security_level
,
631 tb_switch_put(parent_sw
);
635 icm_fr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
637 const struct icm_fr_event_device_disconnected
*pkg
=
638 (const struct icm_fr_event_device_disconnected
*)hdr
;
639 struct tb_switch
*sw
;
642 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
643 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
644 ICM_LINK_INFO_DEPTH_SHIFT
;
646 if (link
> ICM_MAX_LINK
|| depth
> ICM_MAX_DEPTH
) {
647 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
651 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
653 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
663 icm_fr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
665 const struct icm_fr_event_xdomain_connected
*pkg
=
666 (const struct icm_fr_event_xdomain_connected
*)hdr
;
667 struct tb_xdomain
*xd
;
668 struct tb_switch
*sw
;
674 * After NVM upgrade adding root switch device fails because we
675 * initiated reset. During that time ICM might still send
676 * XDomain connected message which we ignore here.
678 if (!tb
->root_switch
)
681 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
682 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
683 ICM_LINK_INFO_DEPTH_SHIFT
;
684 approved
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
686 if (link
> ICM_MAX_LINK
|| depth
> ICM_MAX_DEPTH
) {
687 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
691 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
693 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
695 u8 xd_phy_port
, phy_port
;
697 xd_phy_port
= phy_port_from_route(xd
->route
, xd
->depth
);
698 phy_port
= phy_port_from_route(route
, depth
);
700 if (xd
->depth
== depth
&& xd_phy_port
== phy_port
) {
701 update_xdomain(xd
, route
, link
);
707 * If we find an existing XDomain connection remove it
708 * now. We need to go through login handshake and
709 * everything anyway to be able to re-establish the
717 * Look if there already exists an XDomain in the same place
718 * than the new one and in that case remove it because it is
719 * most likely another host that got disconnected.
721 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
725 dual_link
= dual_link_from_link(link
);
727 xd
= tb_xdomain_find_by_link_depth(tb
, dual_link
,
736 * If the user disconnected a switch during suspend and
737 * connected another host to the same port, remove the switch
740 sw
= get_switch_at_route(tb
->root_switch
, route
);
744 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
746 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
751 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, link
,
757 icm_fr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
759 const struct icm_fr_event_xdomain_disconnected
*pkg
=
760 (const struct icm_fr_event_xdomain_disconnected
*)hdr
;
761 struct tb_xdomain
*xd
;
764 * If the connection is through one or multiple devices, the
765 * XDomain device is removed along with them so it is fine if we
766 * cannot find it here.
768 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
776 icm_tr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
779 struct icm_tr_pkg_driver_ready_response reply
;
780 struct icm_pkg_driver_ready request
= {
781 .hdr
.code
= ICM_DRIVER_READY
,
785 memset(&reply
, 0, sizeof(reply
));
786 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
792 *security_level
= reply
.info
& ICM_TR_INFO_SLEVEL_MASK
;
794 *nboot_acl
= (reply
.info
& ICM_TR_INFO_BOOT_ACL_MASK
) >>
795 ICM_TR_INFO_BOOT_ACL_SHIFT
;
799 static int icm_tr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
801 struct icm_tr_pkg_approve_device request
;
802 struct icm_tr_pkg_approve_device reply
;
805 memset(&request
, 0, sizeof(request
));
806 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
807 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
808 request
.route_lo
= sw
->config
.route_lo
;
809 request
.route_hi
= sw
->config
.route_hi
;
810 request
.connection_id
= sw
->connection_id
;
812 memset(&reply
, 0, sizeof(reply
));
813 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
814 1, ICM_APPROVE_TIMEOUT
);
818 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
819 tb_warn(tb
, "PCIe tunnel creation failed\n");
826 static int icm_tr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
828 struct icm_tr_pkg_add_device_key_response reply
;
829 struct icm_tr_pkg_add_device_key request
;
832 memset(&request
, 0, sizeof(request
));
833 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
834 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
835 request
.route_lo
= sw
->config
.route_lo
;
836 request
.route_hi
= sw
->config
.route_hi
;
837 request
.connection_id
= sw
->connection_id
;
838 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
840 memset(&reply
, 0, sizeof(reply
));
841 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
846 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
847 tb_warn(tb
, "Adding key to switch failed\n");
854 static int icm_tr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
855 const u8
*challenge
, u8
*response
)
857 struct icm_tr_pkg_challenge_device_response reply
;
858 struct icm_tr_pkg_challenge_device request
;
861 memset(&request
, 0, sizeof(request
));
862 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
863 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
864 request
.route_lo
= sw
->config
.route_lo
;
865 request
.route_hi
= sw
->config
.route_hi
;
866 request
.connection_id
= sw
->connection_id
;
867 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
869 memset(&reply
, 0, sizeof(reply
));
870 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
875 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
876 return -EKEYREJECTED
;
877 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
880 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
885 static int icm_tr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
887 struct icm_tr_pkg_approve_xdomain_response reply
;
888 struct icm_tr_pkg_approve_xdomain request
;
891 memset(&request
, 0, sizeof(request
));
892 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
893 request
.route_hi
= upper_32_bits(xd
->route
);
894 request
.route_lo
= lower_32_bits(xd
->route
);
895 request
.transmit_path
= xd
->transmit_path
;
896 request
.transmit_ring
= xd
->transmit_ring
;
897 request
.receive_path
= xd
->receive_path
;
898 request
.receive_ring
= xd
->receive_ring
;
899 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
901 memset(&reply
, 0, sizeof(reply
));
902 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
907 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
913 static int icm_tr_xdomain_tear_down(struct tb
*tb
, struct tb_xdomain
*xd
,
916 struct icm_tr_pkg_disconnect_xdomain_response reply
;
917 struct icm_tr_pkg_disconnect_xdomain request
;
920 memset(&request
, 0, sizeof(request
));
921 request
.hdr
.code
= ICM_DISCONNECT_XDOMAIN
;
922 request
.stage
= stage
;
923 request
.route_hi
= upper_32_bits(xd
->route
);
924 request
.route_lo
= lower_32_bits(xd
->route
);
925 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
927 memset(&reply
, 0, sizeof(reply
));
928 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
933 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
939 static int icm_tr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
943 ret
= icm_tr_xdomain_tear_down(tb
, xd
, 1);
947 usleep_range(10, 50);
948 return icm_tr_xdomain_tear_down(tb
, xd
, 2);
952 icm_tr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
954 const struct icm_tr_event_device_connected
*pkg
=
955 (const struct icm_tr_event_device_connected
*)hdr
;
956 enum tb_security_level security_level
;
957 struct tb_switch
*sw
, *parent_sw
;
958 struct tb_xdomain
*xd
;
959 bool authorized
, boot
;
963 * Currently we don't use the QoS information coming with the
964 * device connected message so simply just ignore that extra
967 if (pkg
->hdr
.packet_id
)
971 * After NVM upgrade adding root switch device fails because we
972 * initiated reset. During that time ICM might still send device
973 * connected message which we ignore here.
975 if (!tb
->root_switch
)
978 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
979 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
980 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
981 ICM_FLAGS_SLEVEL_SHIFT
;
982 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
984 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
985 tb_info(tb
, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
990 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
992 /* Update the switch if it is still in the same place */
993 if (tb_route(sw
) == route
&& !!sw
->authorized
== authorized
) {
994 parent_sw
= tb_to_switch(sw
->dev
.parent
);
995 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
1005 /* Another switch with the same address */
1006 sw
= tb_switch_find_by_route(tb
, route
);
1012 /* XDomain connection with the same address */
1013 xd
= tb_xdomain_find_by_route(tb
, route
);
1019 parent_sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1021 tb_err(tb
, "failed to find parent switch for %llx\n", route
);
1025 add_switch(parent_sw
, route
, &pkg
->ep_uuid
, pkg
->connection_id
,
1026 0, 0, 0, security_level
, authorized
, boot
);
1028 tb_switch_put(parent_sw
);
1032 icm_tr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1034 const struct icm_tr_event_device_disconnected
*pkg
=
1035 (const struct icm_tr_event_device_disconnected
*)hdr
;
1036 struct tb_switch
*sw
;
1039 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1041 sw
= tb_switch_find_by_route(tb
, route
);
1043 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1052 icm_tr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1054 const struct icm_tr_event_xdomain_connected
*pkg
=
1055 (const struct icm_tr_event_xdomain_connected
*)hdr
;
1056 struct tb_xdomain
*xd
;
1057 struct tb_switch
*sw
;
1060 if (!tb
->root_switch
)
1063 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
1065 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
1067 if (xd
->route
== route
) {
1068 update_xdomain(xd
, route
, 0);
1077 /* An existing xdomain with the same address */
1078 xd
= tb_xdomain_find_by_route(tb
, route
);
1085 * If the user disconnected a switch during suspend and
1086 * connected another host to the same port, remove the switch
1089 sw
= get_switch_at_route(tb
->root_switch
, route
);
1093 sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1095 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1099 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, 0, 0);
1104 icm_tr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1106 const struct icm_tr_event_xdomain_disconnected
*pkg
=
1107 (const struct icm_tr_event_xdomain_disconnected
*)hdr
;
1108 struct tb_xdomain
*xd
;
1111 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1113 xd
= tb_xdomain_find_by_route(tb
, route
);
1120 static struct pci_dev
*get_upstream_port(struct pci_dev
*pdev
)
1122 struct pci_dev
*parent
;
1124 parent
= pci_upstream_bridge(pdev
);
1126 if (!pci_is_pcie(parent
))
1128 if (pci_pcie_type(parent
) == PCI_EXP_TYPE_UPSTREAM
)
1130 parent
= pci_upstream_bridge(parent
);
1136 switch (parent
->device
) {
1137 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1138 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1139 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1140 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1141 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1148 static bool icm_ar_is_supported(struct tb
*tb
)
1150 struct pci_dev
*upstream_port
;
1151 struct icm
*icm
= tb_priv(tb
);
1154 * Starting from Alpine Ridge we can use ICM on Apple machines
1155 * as well. We just need to reset and re-enable it first.
1157 if (!x86_apple_machine
)
1161 * Find the upstream PCIe port in case we need to do reset
1162 * through its vendor specific registers.
1164 upstream_port
= get_upstream_port(tb
->nhi
->pdev
);
1165 if (upstream_port
) {
1168 cap
= pci_find_ext_capability(upstream_port
,
1169 PCI_EXT_CAP_ID_VNDR
);
1171 icm
->upstream_port
= upstream_port
;
1181 static int icm_ar_get_mode(struct tb
*tb
)
1183 struct tb_nhi
*nhi
= tb
->nhi
;
1188 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1189 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1192 } while (--retries
);
1195 dev_err(&nhi
->pdev
->dev
, "ICM firmware not authenticated\n");
1199 return nhi_mailbox_mode(nhi
);
1203 icm_ar_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1206 struct icm_ar_pkg_driver_ready_response reply
;
1207 struct icm_pkg_driver_ready request
= {
1208 .hdr
.code
= ICM_DRIVER_READY
,
1212 memset(&reply
, 0, sizeof(reply
));
1213 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1219 *security_level
= reply
.info
& ICM_AR_INFO_SLEVEL_MASK
;
1220 if (nboot_acl
&& (reply
.info
& ICM_AR_INFO_BOOT_ACL_SUPPORTED
))
1221 *nboot_acl
= (reply
.info
& ICM_AR_INFO_BOOT_ACL_MASK
) >>
1222 ICM_AR_INFO_BOOT_ACL_SHIFT
;
1226 static int icm_ar_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
1228 struct icm_ar_pkg_get_route_response reply
;
1229 struct icm_ar_pkg_get_route request
= {
1230 .hdr
= { .code
= ICM_GET_ROUTE
},
1231 .link_info
= depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| link
,
1235 memset(&reply
, 0, sizeof(reply
));
1236 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1241 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1244 *route
= get_route(reply
.route_hi
, reply
.route_lo
);
1248 static int icm_ar_get_boot_acl(struct tb
*tb
, uuid_t
*uuids
, size_t nuuids
)
1250 struct icm_ar_pkg_preboot_acl_response reply
;
1251 struct icm_ar_pkg_preboot_acl request
= {
1252 .hdr
= { .code
= ICM_PREBOOT_ACL
},
1256 memset(&reply
, 0, sizeof(reply
));
1257 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1262 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1265 for (i
= 0; i
< nuuids
; i
++) {
1266 u32
*uuid
= (u32
*)&uuids
[i
];
1268 uuid
[0] = reply
.acl
[i
].uuid_lo
;
1269 uuid
[1] = reply
.acl
[i
].uuid_hi
;
1271 if (uuid
[0] == 0xffffffff && uuid
[1] == 0xffffffff) {
1272 /* Map empty entries to null UUID */
1275 } else if (uuid
[0] != 0 || uuid
[1] != 0) {
1276 /* Upper two DWs are always one's */
1277 uuid
[2] = 0xffffffff;
1278 uuid
[3] = 0xffffffff;
1285 static int icm_ar_set_boot_acl(struct tb
*tb
, const uuid_t
*uuids
,
1288 struct icm_ar_pkg_preboot_acl_response reply
;
1289 struct icm_ar_pkg_preboot_acl request
= {
1291 .code
= ICM_PREBOOT_ACL
,
1292 .flags
= ICM_FLAGS_WRITE
,
1297 for (i
= 0; i
< nuuids
; i
++) {
1298 const u32
*uuid
= (const u32
*)&uuids
[i
];
1300 if (uuid_is_null(&uuids
[i
])) {
1302 * Map null UUID to the empty (all one) entries
1305 request
.acl
[i
].uuid_lo
= 0xffffffff;
1306 request
.acl
[i
].uuid_hi
= 0xffffffff;
1308 /* Two high DWs need to be set to all one */
1309 if (uuid
[2] != 0xffffffff || uuid
[3] != 0xffffffff)
1312 request
.acl
[i
].uuid_lo
= uuid
[0];
1313 request
.acl
[i
].uuid_hi
= uuid
[1];
1317 memset(&reply
, 0, sizeof(reply
));
1318 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1323 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1329 static void icm_handle_notification(struct work_struct
*work
)
1331 struct icm_notification
*n
= container_of(work
, typeof(*n
), work
);
1332 struct tb
*tb
= n
->tb
;
1333 struct icm
*icm
= tb_priv(tb
);
1335 mutex_lock(&tb
->lock
);
1337 switch (n
->pkg
->code
) {
1338 case ICM_EVENT_DEVICE_CONNECTED
:
1339 icm
->device_connected(tb
, n
->pkg
);
1341 case ICM_EVENT_DEVICE_DISCONNECTED
:
1342 icm
->device_disconnected(tb
, n
->pkg
);
1344 case ICM_EVENT_XDOMAIN_CONNECTED
:
1345 icm
->xdomain_connected(tb
, n
->pkg
);
1347 case ICM_EVENT_XDOMAIN_DISCONNECTED
:
1348 icm
->xdomain_disconnected(tb
, n
->pkg
);
1352 mutex_unlock(&tb
->lock
);
1358 static void icm_handle_event(struct tb
*tb
, enum tb_cfg_pkg_type type
,
1359 const void *buf
, size_t size
)
1361 struct icm_notification
*n
;
1363 n
= kmalloc(sizeof(*n
), GFP_KERNEL
);
1367 INIT_WORK(&n
->work
, icm_handle_notification
);
1368 n
->pkg
= kmemdup(buf
, size
, GFP_KERNEL
);
1371 queue_work(tb
->wq
, &n
->work
);
1375 __icm_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1378 struct icm
*icm
= tb_priv(tb
);
1379 unsigned int retries
= 50;
1382 ret
= icm
->driver_ready(tb
, security_level
, nboot_acl
);
1384 tb_err(tb
, "failed to send driver ready to ICM\n");
1389 * Hold on here until the switch config space is accessible so
1390 * that we can read root switch config successfully.
1393 struct tb_cfg_result res
;
1396 res
= tb_cfg_read_raw(tb
->ctl
, &tmp
, 0, 0, TB_CFG_SWITCH
,
1402 } while (--retries
);
1404 tb_err(tb
, "failed to read root switch config space, giving up\n");
1408 static int pci2cio_wait_completion(struct icm
*icm
, unsigned long timeout_msec
)
1410 unsigned long end
= jiffies
+ msecs_to_jiffies(timeout_msec
);
1414 pci_read_config_dword(icm
->upstream_port
,
1415 icm
->vnd_cap
+ PCIE2CIO_CMD
, &cmd
);
1416 if (!(cmd
& PCIE2CIO_CMD_START
)) {
1417 if (cmd
& PCIE2CIO_CMD_TIMEOUT
)
1423 } while (time_before(jiffies
, end
));
1428 static int pcie2cio_read(struct icm
*icm
, enum tb_cfg_space cs
,
1429 unsigned int port
, unsigned int index
, u32
*data
)
1431 struct pci_dev
*pdev
= icm
->upstream_port
;
1432 int ret
, vnd_cap
= icm
->vnd_cap
;
1436 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
1437 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
1438 cmd
|= PCIE2CIO_CMD_START
;
1439 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
1441 ret
= pci2cio_wait_completion(icm
, 5000);
1445 pci_read_config_dword(pdev
, vnd_cap
+ PCIE2CIO_RDDATA
, data
);
1449 static int pcie2cio_write(struct icm
*icm
, enum tb_cfg_space cs
,
1450 unsigned int port
, unsigned int index
, u32 data
)
1452 struct pci_dev
*pdev
= icm
->upstream_port
;
1453 int vnd_cap
= icm
->vnd_cap
;
1456 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_WRDATA
, data
);
1459 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
1460 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
1461 cmd
|= PCIE2CIO_CMD_WRITE
| PCIE2CIO_CMD_START
;
1462 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
1464 return pci2cio_wait_completion(icm
, 5000);
1467 static int icm_firmware_reset(struct tb
*tb
, struct tb_nhi
*nhi
)
1469 struct icm
*icm
= tb_priv(tb
);
1472 if (!icm
->upstream_port
)
1475 /* Put ARC to wait for CIO reset event to happen */
1476 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1477 val
|= REG_FW_STS_CIO_RESET_REQ
;
1478 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1481 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1482 val
|= REG_FW_STS_ICM_EN_INVERT
;
1483 val
|= REG_FW_STS_ICM_EN_CPU
;
1484 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1486 /* Trigger CIO reset now */
1487 return pcie2cio_write(icm
, TB_CFG_SWITCH
, 0, 0x50, BIT(9));
1490 static int icm_firmware_start(struct tb
*tb
, struct tb_nhi
*nhi
)
1492 unsigned int retries
= 10;
1496 /* Check if the ICM firmware is already running */
1497 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1498 if (val
& REG_FW_STS_ICM_EN
)
1501 dev_info(&nhi
->pdev
->dev
, "starting ICM firmware\n");
1503 ret
= icm_firmware_reset(tb
, nhi
);
1507 /* Wait until the ICM firmware tells us it is up and running */
1509 /* Check that the ICM firmware is running */
1510 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1511 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1515 } while (--retries
);
1520 static int icm_reset_phy_port(struct tb
*tb
, int phy_port
)
1522 struct icm
*icm
= tb_priv(tb
);
1528 if (!icm
->upstream_port
)
1540 * Read link status of both null ports belonging to a single
1543 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1546 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1550 state0
= val0
& PHY_PORT_CS1_LINK_STATE_MASK
;
1551 state0
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1552 state1
= val1
& PHY_PORT_CS1_LINK_STATE_MASK
;
1553 state1
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1555 /* If they are both up we need to reset them now */
1556 if (state0
!= TB_PORT_UP
|| state1
!= TB_PORT_UP
)
1559 val0
|= PHY_PORT_CS1_LINK_DISABLE
;
1560 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1564 val1
|= PHY_PORT_CS1_LINK_DISABLE
;
1565 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1569 /* Wait a bit and then re-enable both ports */
1570 usleep_range(10, 100);
1572 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1575 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1579 val0
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1580 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1584 val1
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1585 return pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1588 static int icm_firmware_init(struct tb
*tb
)
1590 struct icm
*icm
= tb_priv(tb
);
1591 struct tb_nhi
*nhi
= tb
->nhi
;
1594 ret
= icm_firmware_start(tb
, nhi
);
1596 dev_err(&nhi
->pdev
->dev
, "could not start ICM firmware\n");
1600 if (icm
->get_mode
) {
1601 ret
= icm
->get_mode(tb
);
1604 case NHI_FW_SAFE_MODE
:
1605 icm
->safe_mode
= true;
1608 case NHI_FW_CM_MODE
:
1609 /* Ask ICM to accept all Thunderbolt devices */
1610 nhi_mailbox_cmd(nhi
, NHI_MAILBOX_ALLOW_ALL_DEVS
, 0);
1617 tb_err(tb
, "ICM firmware is in wrong mode: %u\n", ret
);
1623 * Reset both physical ports if there is anything connected to
1626 ret
= icm_reset_phy_port(tb
, 0);
1628 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port0\n");
1629 ret
= icm_reset_phy_port(tb
, 1);
1631 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port1\n");
1636 static int icm_driver_ready(struct tb
*tb
)
1638 struct icm
*icm
= tb_priv(tb
);
1641 ret
= icm_firmware_init(tb
);
1645 if (icm
->safe_mode
) {
1646 tb_info(tb
, "Thunderbolt host controller is in safe mode.\n");
1647 tb_info(tb
, "You need to update NVM firmware of the controller before it can be used.\n");
1648 tb_info(tb
, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1652 ret
= __icm_driver_ready(tb
, &tb
->security_level
, &tb
->nboot_acl
);
1657 * Make sure the number of supported preboot ACL matches what we
1658 * expect or disable the whole feature.
1660 if (tb
->nboot_acl
> icm
->max_boot_acl
)
1666 static int icm_suspend(struct tb
*tb
)
1670 ret
= nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_SAVE_DEVS
, 0);
1672 tb_info(tb
, "Ignoring mailbox command error (%d) in %s\n",
1679 * Mark all switches (except root switch) below this one unplugged. ICM
1680 * firmware will send us an updated list of switches after we have send
1681 * it driver ready command. If a switch is not in that list it will be
1682 * removed when we perform rescan.
1684 static void icm_unplug_children(struct tb_switch
*sw
)
1689 sw
->is_unplugged
= true;
1691 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1692 struct tb_port
*port
= &sw
->ports
[i
];
1694 if (tb_is_upstream_port(port
))
1696 if (port
->xdomain
) {
1697 port
->xdomain
->is_unplugged
= true;
1703 icm_unplug_children(port
->remote
->sw
);
1707 static void icm_free_unplugged_children(struct tb_switch
*sw
)
1711 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1712 struct tb_port
*port
= &sw
->ports
[i
];
1714 if (tb_is_upstream_port(port
))
1717 if (port
->xdomain
&& port
->xdomain
->is_unplugged
) {
1718 tb_xdomain_remove(port
->xdomain
);
1719 port
->xdomain
= NULL
;
1726 if (port
->remote
->sw
->is_unplugged
) {
1727 tb_switch_remove(port
->remote
->sw
);
1728 port
->remote
= NULL
;
1730 icm_free_unplugged_children(port
->remote
->sw
);
1735 static void icm_rescan_work(struct work_struct
*work
)
1737 struct icm
*icm
= container_of(work
, struct icm
, rescan_work
.work
);
1738 struct tb
*tb
= icm_to_tb(icm
);
1740 mutex_lock(&tb
->lock
);
1741 if (tb
->root_switch
)
1742 icm_free_unplugged_children(tb
->root_switch
);
1743 mutex_unlock(&tb
->lock
);
1746 static void icm_complete(struct tb
*tb
)
1748 struct icm
*icm
= tb_priv(tb
);
1750 if (tb
->nhi
->going_away
)
1753 icm_unplug_children(tb
->root_switch
);
1756 * Now all existing children should be resumed, start events
1757 * from ICM to get updated status.
1759 __icm_driver_ready(tb
, NULL
, NULL
);
1762 * We do not get notifications of devices that have been
1763 * unplugged during suspend so schedule rescan to clean them up
1766 queue_delayed_work(tb
->wq
, &icm
->rescan_work
, msecs_to_jiffies(500));
1769 static int icm_start(struct tb
*tb
)
1771 struct icm
*icm
= tb_priv(tb
);
1775 tb
->root_switch
= tb_switch_alloc_safe_mode(tb
, &tb
->dev
, 0);
1777 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
1778 if (!tb
->root_switch
)
1782 * NVM upgrade has not been tested on Apple systems and they
1783 * don't provide images publicly either. To be on the safe side
1784 * prevent root switch NVM upgrade on Macs for now.
1786 tb
->root_switch
->no_nvm_upgrade
= x86_apple_machine
;
1788 ret
= tb_switch_add(tb
->root_switch
);
1790 tb_switch_put(tb
->root_switch
);
1791 tb
->root_switch
= NULL
;
1797 static void icm_stop(struct tb
*tb
)
1799 struct icm
*icm
= tb_priv(tb
);
1801 cancel_delayed_work(&icm
->rescan_work
);
1802 tb_switch_remove(tb
->root_switch
);
1803 tb
->root_switch
= NULL
;
1804 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
1807 static int icm_disconnect_pcie_paths(struct tb
*tb
)
1809 return nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DISCONNECT_PCIE_PATHS
, 0);
1813 static const struct tb_cm_ops icm_fr_ops
= {
1814 .driver_ready
= icm_driver_ready
,
1817 .suspend
= icm_suspend
,
1818 .complete
= icm_complete
,
1819 .handle_event
= icm_handle_event
,
1820 .approve_switch
= icm_fr_approve_switch
,
1821 .add_switch_key
= icm_fr_add_switch_key
,
1822 .challenge_switch_key
= icm_fr_challenge_switch_key
,
1823 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
1824 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
1825 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
1829 static const struct tb_cm_ops icm_ar_ops
= {
1830 .driver_ready
= icm_driver_ready
,
1833 .suspend
= icm_suspend
,
1834 .complete
= icm_complete
,
1835 .handle_event
= icm_handle_event
,
1836 .get_boot_acl
= icm_ar_get_boot_acl
,
1837 .set_boot_acl
= icm_ar_set_boot_acl
,
1838 .approve_switch
= icm_fr_approve_switch
,
1839 .add_switch_key
= icm_fr_add_switch_key
,
1840 .challenge_switch_key
= icm_fr_challenge_switch_key
,
1841 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
1842 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
1843 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
1847 static const struct tb_cm_ops icm_tr_ops
= {
1848 .driver_ready
= icm_driver_ready
,
1851 .suspend
= icm_suspend
,
1852 .complete
= icm_complete
,
1853 .handle_event
= icm_handle_event
,
1854 .get_boot_acl
= icm_ar_get_boot_acl
,
1855 .set_boot_acl
= icm_ar_set_boot_acl
,
1856 .approve_switch
= icm_tr_approve_switch
,
1857 .add_switch_key
= icm_tr_add_switch_key
,
1858 .challenge_switch_key
= icm_tr_challenge_switch_key
,
1859 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
1860 .approve_xdomain_paths
= icm_tr_approve_xdomain_paths
,
1861 .disconnect_xdomain_paths
= icm_tr_disconnect_xdomain_paths
,
1864 struct tb
*icm_probe(struct tb_nhi
*nhi
)
1869 tb
= tb_domain_alloc(nhi
, sizeof(struct icm
));
1874 INIT_DELAYED_WORK(&icm
->rescan_work
, icm_rescan_work
);
1875 mutex_init(&icm
->request_lock
);
1877 switch (nhi
->pdev
->device
) {
1878 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI
:
1879 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI
:
1880 icm
->is_supported
= icm_fr_is_supported
;
1881 icm
->get_route
= icm_fr_get_route
;
1882 icm
->driver_ready
= icm_fr_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_fr_ops
;
1890 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI
:
1891 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI
:
1892 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI
:
1893 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI
:
1894 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI
:
1895 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
1896 icm
->is_supported
= icm_ar_is_supported
;
1897 icm
->get_mode
= icm_ar_get_mode
;
1898 icm
->get_route
= icm_ar_get_route
;
1899 icm
->driver_ready
= icm_ar_driver_ready
;
1900 icm
->device_connected
= icm_fr_device_connected
;
1901 icm
->device_disconnected
= icm_fr_device_disconnected
;
1902 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
1903 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
1904 tb
->cm_ops
= &icm_ar_ops
;
1907 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI
:
1908 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI
:
1909 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
1910 icm
->is_supported
= icm_ar_is_supported
;
1911 icm
->get_mode
= icm_ar_get_mode
;
1912 icm
->driver_ready
= icm_tr_driver_ready
;
1913 icm
->device_connected
= icm_tr_device_connected
;
1914 icm
->device_disconnected
= icm_tr_device_disconnected
;
1915 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
1916 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
1917 tb
->cm_ops
= &icm_tr_ops
;
1921 if (!icm
->is_supported
|| !icm
->is_supported(tb
)) {
1922 dev_dbg(&nhi
->pdev
->dev
, "ICM not supported on this controller\n");