1 // SPDX-License-Identifier: GPL-2.0
3 * Internal Thunderbolt Connection Manager. This is a firmware running on
4 * the Thunderbolt host controller performing most of the low-level
7 * Copyright (C) 2017, Intel Corporation
8 * Authors: Michael Jamet <michael.jamet@intel.com>
9 * Mika Westerberg <mika.westerberg@linux.intel.com>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/pci.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/platform_data/x86/apple.h>
17 #include <linux/sizes.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
25 #define PCIE2CIO_CMD 0x30
26 #define PCIE2CIO_CMD_TIMEOUT BIT(31)
27 #define PCIE2CIO_CMD_START BIT(30)
28 #define PCIE2CIO_CMD_WRITE BIT(21)
29 #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
30 #define PCIE2CIO_CMD_CS_SHIFT 19
31 #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
32 #define PCIE2CIO_CMD_PORT_SHIFT 13
34 #define PCIE2CIO_WRDATA 0x34
35 #define PCIE2CIO_RDDATA 0x38
37 #define PHY_PORT_CS1 0x37
38 #define PHY_PORT_CS1_LINK_DISABLE BIT(14)
39 #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
40 #define PHY_PORT_CS1_LINK_STATE_SHIFT 26
42 #define ICM_TIMEOUT 5000 /* ms */
43 #define ICM_APPROVE_TIMEOUT 10000 /* ms */
44 #define ICM_MAX_LINK 4
45 #define ICM_MAX_DEPTH 6
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
70 struct mutex request_lock
;
71 struct delayed_work rescan_work
;
72 struct pci_dev
*upstream_port
;
77 bool (*is_supported
)(struct tb
*tb
);
78 int (*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
;
100 struct ep_name_entry
{
106 #define EP_NAME_INTEL_VSS 0x10
108 /* Intel Vendor specific structure */
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
;
129 if (ep_name
+ ep
->len
> end
)
132 if (ep
->type
== EP_NAME_INTEL_VSS
)
133 return (const struct intel_vss
*)ep
->data
;
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
)
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
)
178 if (res_hdr
->code
!= req_hdr
->code
)
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
);
205 struct tb_cfg_request
*req
;
206 struct tb_cfg_result res
;
208 req
= tb_cfg_request_alloc();
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);
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
)
246 if ((port
& ICM_PORT_TYPE_MASK
) != TB_TYPE_PORT
)
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
;
263 switches
= kcalloc(npackets
, sizeof(*switches
), GFP_KERNEL
);
267 ret
= icm_request(tb
, &request
, sizeof(request
), switches
,
268 sizeof(*switches
), npackets
, ICM_TIMEOUT
);
273 index
= icm_fr_get_switch_index(sw
->ports
[link
]);
279 sw
= &switches
[index
];
280 for (i
= 1; i
< depth
; i
++) {
283 if (!(sw
->first_data
& ICM_SWITCH_USED
)) {
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
];
297 *route
= get_route(sw
->route_hi
, sw
->route_lo
);
304 static void icm_fr_save_devices(struct tb
*tb
)
306 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_SAVE_DEVS
, 0);
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
,
319 memset(&reply
, 0, sizeof(reply
));
320 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
326 *security_level
= reply
.security_level
& ICM_FR_SLEVEL_MASK
;
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
;
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
);
350 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
351 tb_warn(tb
, "PCIe tunnel creation failed\n");
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
;
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
),
377 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
378 tb_warn(tb
, "Adding key to switch failed\n");
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
;
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
),
405 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
406 return -EKEYREJECTED
;
407 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
410 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
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
;
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
),
437 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
443 static int icm_fr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
448 phy_port
= tb_phy_port_from_link(xd
->link
);
450 cmd
= NHI_MAILBOX_DISCONNECT_PA
;
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);
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
);
475 sw
->uuid
= kmemdup(uuid
, sizeof(*uuid
), GFP_KERNEL
);
477 tb_sw_warn(sw
, "cannot allocate memory for switch\n");
481 sw
->connection_id
= connection_id
;
482 sw
->connection_key
= connection_key
;
485 sw
->authorized
= authorized
;
486 sw
->security_level
= security_level
;
489 vss
= parse_intel_vss(ep_name
, ep_name_size
);
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
;
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
;
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
,
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
);
553 tb_port_at(route
, sw
)->xdomain
= xd
;
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
)
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
);
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
;
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",
607 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
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
);
632 tb_err(tb
, "failed to update route string for switch at %u.%u\n",
638 route
= tb_route(sw
);
641 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
642 pkg
->connection_key
, link
, depth
, boot
);
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.
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
);
666 dual_link
= dual_link_from_link(link
);
668 sw
= tb_switch_find_by_link_depth(tb
, dual_link
, depth
);
675 /* Remove existing XDomain connection if found */
676 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
682 parent_sw
= tb_switch_find_by_link_depth(tb
, link
, depth
- 1);
684 tb_err(tb
, "failed to find parent switch for %u.%u\n",
689 ret
= icm
->get_route(tb
, link
, depth
, &route
);
691 tb_err(tb
, "failed to find route string for switch at %u.%u\n",
693 tb_switch_put(parent_sw
);
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
,
702 tb_switch_put(parent_sw
);
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
;
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
);
722 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
724 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
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
;
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
);
752 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
754 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
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
);
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
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
);
786 dual_link
= dual_link_from_link(link
);
788 xd
= tb_xdomain_find_by_link_depth(tb
, dual_link
,
797 * If the user disconnected a switch during suspend and
798 * connected another host to the same port, remove the switch
801 sw
= get_switch_at_route(tb
->root_switch
, route
);
805 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
807 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
812 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, link
,
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
);
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
,
846 memset(&reply
, 0, sizeof(reply
));
847 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
853 *security_level
= reply
.info
& ICM_TR_INFO_SLEVEL_MASK
;
855 *nboot_acl
= (reply
.info
& ICM_TR_INFO_BOOT_ACL_MASK
) >>
856 ICM_TR_INFO_BOOT_ACL_SHIFT
;
858 *rpm
= !!(reply
.hdr
.flags
& ICM_TR_FLAGS_RTD3
);
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
;
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
);
882 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
883 tb_warn(tb
, "PCIe tunnel creation failed\n");
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
;
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
),
910 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
911 tb_warn(tb
, "Adding key to switch failed\n");
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
;
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
),
939 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
940 return -EKEYREJECTED
;
941 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
944 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
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
;
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
),
971 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
977 static int icm_tr_xdomain_tear_down(struct tb
*tb
, struct tb_xdomain
*xd
,
980 struct icm_tr_pkg_disconnect_xdomain_response reply
;
981 struct icm_tr_pkg_disconnect_xdomain request
;
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
),
997 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1003 static int icm_tr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
1007 ret
= icm_tr_xdomain_tear_down(tb
, xd
, 1);
1011 usleep_range(10, 50);
1012 return icm_tr_xdomain_tear_down(tb
, xd
, 2);
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
;
1027 * Currently we don't use the QoS information coming with the
1028 * device connected message so simply just ignore that extra
1031 if (pkg
->hdr
.packet_id
)
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",
1046 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
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
,
1061 /* Another switch with the same address */
1062 sw
= tb_switch_find_by_route(tb
, route
);
1068 /* XDomain connection with the same address */
1069 xd
= tb_xdomain_find_by_route(tb
, route
);
1075 parent_sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1077 tb_err(tb
, "failed to find parent switch for %llx\n", route
);
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
);
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
;
1096 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1098 sw
= tb_switch_find_by_route(tb
, route
);
1100 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
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
;
1117 if (!tb
->root_switch
)
1120 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
1122 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
1124 if (xd
->route
== route
) {
1125 update_xdomain(xd
, route
, 0);
1134 /* An existing xdomain with the same address */
1135 xd
= tb_xdomain_find_by_route(tb
, route
);
1142 * If the user disconnected a switch during suspend and
1143 * connected another host to the same port, remove the switch
1146 sw
= get_switch_at_route(tb
->root_switch
, route
);
1150 sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1152 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1156 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, 0, 0);
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
;
1168 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1170 xd
= tb_xdomain_find_by_route(tb
, route
);
1177 static struct pci_dev
*get_upstream_port(struct pci_dev
*pdev
)
1179 struct pci_dev
*parent
;
1181 parent
= pci_upstream_bridge(pdev
);
1183 if (!pci_is_pcie(parent
))
1185 if (pci_pcie_type(parent
) == PCI_EXP_TYPE_UPSTREAM
)
1187 parent
= pci_upstream_bridge(parent
);
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
:
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
)
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
) {
1225 cap
= pci_find_ext_capability(upstream_port
,
1226 PCI_EXT_CAP_ID_VNDR
);
1228 icm
->upstream_port
= upstream_port
;
1238 static int icm_ar_get_mode(struct tb
*tb
)
1240 struct tb_nhi
*nhi
= tb
->nhi
;
1245 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1246 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1249 } while (--retries
);
1252 dev_err(&nhi
->pdev
->dev
, "ICM firmware not authenticated\n");
1256 return nhi_mailbox_mode(nhi
);
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
,
1269 memset(&reply
, 0, sizeof(reply
));
1270 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
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
;
1281 *rpm
= !!(reply
.hdr
.flags
& ICM_AR_FLAGS_RTD3
);
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
,
1295 memset(&reply
, 0, sizeof(reply
));
1296 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1301 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1304 *route
= get_route(reply
.route_hi
, reply
.route_lo
);
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
},
1316 memset(&reply
, 0, sizeof(reply
));
1317 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1322 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
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 */
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;
1345 static int icm_ar_set_boot_acl(struct tb
*tb
, const uuid_t
*uuids
,
1348 struct icm_ar_pkg_preboot_acl_response reply
;
1349 struct icm_ar_pkg_preboot_acl request
= {
1351 .code
= ICM_PREBOOT_ACL
,
1352 .flags
= ICM_FLAGS_WRITE
,
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
1365 request
.acl
[i
].uuid_lo
= 0xffffffff;
1366 request
.acl
[i
].uuid_hi
= 0xffffffff;
1368 /* Two high DWs need to be set to all one */
1369 if (uuid
[2] != 0xffffffff || uuid
[3] != 0xffffffff)
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
),
1383 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
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
);
1407 case ICM_EVENT_DEVICE_DISCONNECTED
:
1408 icm
->device_disconnected(tb
, n
->pkg
);
1410 case ICM_EVENT_XDOMAIN_CONNECTED
:
1411 icm
->xdomain_connected(tb
, n
->pkg
);
1413 case ICM_EVENT_XDOMAIN_DISCONNECTED
:
1414 icm
->xdomain_disconnected(tb
, n
->pkg
);
1419 mutex_unlock(&tb
->lock
);
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
);
1434 INIT_WORK(&n
->work
, icm_handle_notification
);
1435 n
->pkg
= kmemdup(buf
, size
, GFP_KERNEL
);
1438 queue_work(tb
->wq
, &n
->work
);
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;
1449 ret
= icm
->driver_ready(tb
, security_level
, nboot_acl
, rpm
);
1451 tb_err(tb
, "failed to send driver ready to ICM\n");
1456 * Hold on here until the switch config space is accessible so
1457 * that we can read root switch config successfully.
1460 struct tb_cfg_result res
;
1463 res
= tb_cfg_read_raw(tb
->ctl
, &tmp
, 0, 0, TB_CFG_SWITCH
,
1469 } while (--retries
);
1471 tb_err(tb
, "failed to read root switch config space, giving up\n");
1475 static int pci2cio_wait_completion(struct icm
*icm
, unsigned long timeout_msec
)
1477 unsigned long end
= jiffies
+ msecs_to_jiffies(timeout_msec
);
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
)
1490 } while (time_before(jiffies
, end
));
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
;
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);
1512 pci_read_config_dword(pdev
, vnd_cap
+ PCIE2CIO_RDDATA
, data
);
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
;
1523 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_WRDATA
, data
);
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
);
1539 if (!icm
->upstream_port
)
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
);
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;
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
)
1568 dev_info(&nhi
->pdev
->dev
, "starting ICM firmware\n");
1570 ret
= icm_firmware_reset(tb
, nhi
);
1574 /* Wait until the ICM firmware tells us it is up and running */
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
)
1582 } while (--retries
);
1587 static int icm_reset_phy_port(struct tb
*tb
, int phy_port
)
1589 struct icm
*icm
= tb_priv(tb
);
1595 if (!icm
->upstream_port
)
1607 * Read link status of both null ports belonging to a single
1610 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1613 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
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
)
1626 val0
|= PHY_PORT_CS1_LINK_DISABLE
;
1627 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1631 val1
|= PHY_PORT_CS1_LINK_DISABLE
;
1632 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
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
);
1642 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1646 val0
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1647 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
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
;
1661 ret
= icm_firmware_start(tb
, nhi
);
1663 dev_err(&nhi
->pdev
->dev
, "could not start ICM firmware\n");
1667 if (icm
->get_mode
) {
1668 ret
= icm
->get_mode(tb
);
1671 case NHI_FW_SAFE_MODE
:
1672 icm
->safe_mode
= true;
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);
1684 tb_err(tb
, "ICM firmware is in wrong mode: %u\n", ret
);
1690 * Reset both physical ports if there is anything connected to
1693 ret
= icm_reset_phy_port(tb
, 0);
1695 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port0\n");
1696 ret
= icm_reset_phy_port(tb
, 1);
1698 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port1\n");
1703 static int icm_driver_ready(struct tb
*tb
)
1705 struct icm
*icm
= tb_priv(tb
);
1708 ret
= icm_firmware_init(tb
);
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");
1719 ret
= __icm_driver_ready(tb
, &tb
->security_level
, &tb
->nboot_acl
,
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
)
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);
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
)
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
))
1763 if (port
->xdomain
) {
1764 port
->xdomain
->is_unplugged
= true;
1770 icm_unplug_children(port
->remote
->sw
);
1774 static void icm_free_unplugged_children(struct tb_switch
*sw
)
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
))
1784 if (port
->xdomain
&& port
->xdomain
->is_unplugged
) {
1785 tb_xdomain_remove(port
->xdomain
);
1786 port
->xdomain
= NULL
;
1793 if (port
->remote
->sw
->is_unplugged
) {
1794 tb_switch_remove(port
->remote
->sw
);
1795 port
->remote
= NULL
;
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
)
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
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);
1842 static int icm_runtime_resume(struct tb
*tb
)
1845 * We can reuse the same resume functionality than with system
1852 static int icm_start(struct tb
*tb
)
1854 struct icm
*icm
= tb_priv(tb
);
1858 tb
->root_switch
= tb_switch_alloc_safe_mode(tb
, &tb
->dev
, 0);
1860 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
1861 if (!tb
->root_switch
)
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
);
1874 tb_switch_put(tb
->root_switch
);
1875 tb
->root_switch
= NULL
;
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);
1897 static const struct tb_cm_ops icm_fr_ops
= {
1898 .driver_ready
= icm_driver_ready
,
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
,
1913 static const struct tb_cm_ops icm_ar_ops
= {
1914 .driver_ready
= icm_driver_ready
,
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
,
1933 static const struct tb_cm_ops icm_tr_ops
= {
1934 .driver_ready
= icm_driver_ready
,
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
)
1957 tb
= tb_domain_alloc(nhi
, sizeof(struct icm
));
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
;
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
;
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
;
2011 if (!icm
->is_supported
|| !icm
->is_supported(tb
)) {
2012 dev_dbg(&nhi
->pdev
->dev
, "ICM not supported on this controller\n");