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/moduleparam.h>
15 #include <linux/pci.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/platform_data/x86/apple.h>
18 #include <linux/sizes.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
26 #define PCIE2CIO_CMD 0x30
27 #define PCIE2CIO_CMD_TIMEOUT BIT(31)
28 #define PCIE2CIO_CMD_START BIT(30)
29 #define PCIE2CIO_CMD_WRITE BIT(21)
30 #define PCIE2CIO_CMD_CS_MASK GENMASK(20, 19)
31 #define PCIE2CIO_CMD_CS_SHIFT 19
32 #define PCIE2CIO_CMD_PORT_MASK GENMASK(18, 13)
33 #define PCIE2CIO_CMD_PORT_SHIFT 13
35 #define PCIE2CIO_WRDATA 0x34
36 #define PCIE2CIO_RDDATA 0x38
38 #define PHY_PORT_CS1 0x37
39 #define PHY_PORT_CS1_LINK_DISABLE BIT(14)
40 #define PHY_PORT_CS1_LINK_STATE_MASK GENMASK(29, 26)
41 #define PHY_PORT_CS1_LINK_STATE_SHIFT 26
43 #define ICM_TIMEOUT 5000 /* ms */
45 #define ICM_APPROVE_TIMEOUT 10000 /* ms */
46 #define ICM_MAX_LINK 4
48 static bool start_icm
;
49 module_param(start_icm
, bool, 0444);
50 MODULE_PARM_DESC(start_icm
, "start ICM firmware if it is not running (default: false)");
53 * struct usb4_switch_nvm_auth - Holds USB4 NVM_AUTH status
54 * @reply: Reply from ICM firmware is placed here
55 * @request: Request that is sent to ICM firmware
56 * @icm: Pointer to ICM private data
58 struct usb4_switch_nvm_auth
{
59 struct icm_usb4_switch_op_response reply
;
60 struct icm_usb4_switch_op request
;
65 * struct icm - Internal connection manager private data
66 * @request_lock: Makes sure only one message is send to ICM at time
67 * @rescan_work: Work used to rescan the surviving switches after resume
68 * @upstream_port: Pointer to the PCIe upstream port this host
69 * controller is connected. This is only set for systems
70 * where ICM needs to be started manually
71 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
72 * (only set when @upstream_port is not %NULL)
73 * @safe_mode: ICM is in safe mode
74 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
75 * @rpm: Does the controller support runtime PM (RTD3)
76 * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
77 * @proto_version: Firmware protocol version
78 * @last_nvm_auth: Last USB4 router NVM_AUTH result (or %NULL if not set)
79 * @veto: Is RTD3 veto in effect
80 * @is_supported: Checks if we can support ICM on this controller
81 * @cio_reset: Trigger CIO reset
82 * @get_mode: Read and return the ICM firmware mode (optional)
83 * @get_route: Find a route string for given switch
84 * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
85 * @driver_ready: Send driver ready message to ICM
86 * @set_uuid: Set UUID for the root switch (optional)
87 * @device_connected: Handle device connected ICM message
88 * @device_disconnected: Handle device disconnected ICM message
89 * @xdomain_connected: Handle XDomain connected ICM message
90 * @xdomain_disconnected: Handle XDomain disconnected ICM message
91 * @rtd3_veto: Handle RTD3 veto notification ICM message
94 struct mutex request_lock
;
95 struct delayed_work rescan_work
;
96 struct pci_dev
*upstream_port
;
101 bool can_upgrade_nvm
;
103 struct usb4_switch_nvm_auth
*last_nvm_auth
;
105 bool (*is_supported
)(struct tb
*tb
);
106 int (*cio_reset
)(struct tb
*tb
);
107 int (*get_mode
)(struct tb
*tb
);
108 int (*get_route
)(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
);
109 void (*save_devices
)(struct tb
*tb
);
110 int (*driver_ready
)(struct tb
*tb
,
111 enum tb_security_level
*security_level
,
112 u8
*proto_version
, size_t *nboot_acl
, bool *rpm
);
113 void (*set_uuid
)(struct tb
*tb
);
114 void (*device_connected
)(struct tb
*tb
,
115 const struct icm_pkg_header
*hdr
);
116 void (*device_disconnected
)(struct tb
*tb
,
117 const struct icm_pkg_header
*hdr
);
118 void (*xdomain_connected
)(struct tb
*tb
,
119 const struct icm_pkg_header
*hdr
);
120 void (*xdomain_disconnected
)(struct tb
*tb
,
121 const struct icm_pkg_header
*hdr
);
122 void (*rtd3_veto
)(struct tb
*tb
, const struct icm_pkg_header
*hdr
);
125 struct icm_notification
{
126 struct work_struct work
;
127 struct icm_pkg_header
*pkg
;
131 struct ep_name_entry
{
137 #define EP_NAME_INTEL_VSS 0x10
139 /* Intel Vendor specific structure */
149 #define INTEL_VSS_FLAGS_RTD3 BIT(0)
151 static const struct intel_vss
*parse_intel_vss(const void *ep_name
, size_t size
)
153 const void *end
= ep_name
+ size
;
155 while (ep_name
< end
) {
156 const struct ep_name_entry
*ep
= ep_name
;
160 if (ep_name
+ ep
->len
> end
)
163 if (ep
->type
== EP_NAME_INTEL_VSS
)
164 return (const struct intel_vss
*)ep
->data
;
172 static bool intel_vss_is_rtd3(const void *ep_name
, size_t size
)
174 const struct intel_vss
*vss
;
176 vss
= parse_intel_vss(ep_name
, size
);
178 return !!(vss
->flags
& INTEL_VSS_FLAGS_RTD3
);
183 static inline struct tb
*icm_to_tb(struct icm
*icm
)
185 return ((void *)icm
- sizeof(struct tb
));
188 static inline u8
phy_port_from_route(u64 route
, u8 depth
)
192 link
= depth
? route
>> ((depth
- 1) * 8) : route
;
193 return tb_phy_port_from_link(link
);
196 static inline u8
dual_link_from_link(u8 link
)
198 return link
? ((link
- 1) ^ 0x01) + 1 : 0;
201 static inline u64
get_route(u32 route_hi
, u32 route_lo
)
203 return (u64
)route_hi
<< 32 | route_lo
;
206 static inline u64
get_parent_route(u64 route
)
208 int depth
= tb_route_length(route
);
209 return depth
? route
& ~(0xffULL
<< (depth
- 1) * TB_ROUTE_SHIFT
) : 0;
212 static int pci2cio_wait_completion(struct icm
*icm
, unsigned long timeout_msec
)
214 unsigned long end
= jiffies
+ msecs_to_jiffies(timeout_msec
);
218 pci_read_config_dword(icm
->upstream_port
,
219 icm
->vnd_cap
+ PCIE2CIO_CMD
, &cmd
);
220 if (!(cmd
& PCIE2CIO_CMD_START
)) {
221 if (cmd
& PCIE2CIO_CMD_TIMEOUT
)
227 } while (time_before(jiffies
, end
));
232 static int pcie2cio_read(struct icm
*icm
, enum tb_cfg_space cs
,
233 unsigned int port
, unsigned int index
, u32
*data
)
235 struct pci_dev
*pdev
= icm
->upstream_port
;
236 int ret
, vnd_cap
= icm
->vnd_cap
;
240 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
241 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
242 cmd
|= PCIE2CIO_CMD_START
;
243 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
245 ret
= pci2cio_wait_completion(icm
, 5000);
249 pci_read_config_dword(pdev
, vnd_cap
+ PCIE2CIO_RDDATA
, data
);
253 static int pcie2cio_write(struct icm
*icm
, enum tb_cfg_space cs
,
254 unsigned int port
, unsigned int index
, u32 data
)
256 struct pci_dev
*pdev
= icm
->upstream_port
;
257 int vnd_cap
= icm
->vnd_cap
;
260 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_WRDATA
, data
);
263 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
264 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
265 cmd
|= PCIE2CIO_CMD_WRITE
| PCIE2CIO_CMD_START
;
266 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
268 return pci2cio_wait_completion(icm
, 5000);
271 static bool icm_match(const struct tb_cfg_request
*req
,
272 const struct ctl_pkg
*pkg
)
274 const struct icm_pkg_header
*res_hdr
= pkg
->buffer
;
275 const struct icm_pkg_header
*req_hdr
= req
->request
;
277 if (pkg
->frame
.eof
!= req
->response_type
)
279 if (res_hdr
->code
!= req_hdr
->code
)
285 static bool icm_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
287 const struct icm_pkg_header
*hdr
= pkg
->buffer
;
289 if (hdr
->packet_id
< req
->npackets
) {
290 size_t offset
= hdr
->packet_id
* req
->response_size
;
292 memcpy(req
->response
+ offset
, pkg
->buffer
, req
->response_size
);
295 return hdr
->packet_id
== hdr
->total_packets
- 1;
298 static int icm_request(struct tb
*tb
, const void *request
, size_t request_size
,
299 void *response
, size_t response_size
, size_t npackets
,
300 int retries
, unsigned int timeout_msec
)
302 struct icm
*icm
= tb_priv(tb
);
305 struct tb_cfg_request
*req
;
306 struct tb_cfg_result res
;
308 req
= tb_cfg_request_alloc();
312 req
->match
= icm_match
;
313 req
->copy
= icm_copy
;
314 req
->request
= request
;
315 req
->request_size
= request_size
;
316 req
->request_type
= TB_CFG_PKG_ICM_CMD
;
317 req
->response
= response
;
318 req
->npackets
= npackets
;
319 req
->response_size
= response_size
;
320 req
->response_type
= TB_CFG_PKG_ICM_RESP
;
322 mutex_lock(&icm
->request_lock
);
323 res
= tb_cfg_request_sync(tb
->ctl
, req
, timeout_msec
);
324 mutex_unlock(&icm
->request_lock
);
326 tb_cfg_request_put(req
);
328 if (res
.err
!= -ETIMEDOUT
)
329 return res
.err
== 1 ? -EIO
: res
.err
;
331 usleep_range(20, 50);
338 * If rescan is queued to run (we are resuming), postpone it to give the
339 * firmware some more time to send device connected notifications for next
340 * devices in the chain.
342 static void icm_postpone_rescan(struct tb
*tb
)
344 struct icm
*icm
= tb_priv(tb
);
346 if (delayed_work_pending(&icm
->rescan_work
))
347 mod_delayed_work(tb
->wq
, &icm
->rescan_work
,
348 msecs_to_jiffies(500));
351 static void icm_veto_begin(struct tb
*tb
)
353 struct icm
*icm
= tb_priv(tb
);
357 /* Keep the domain powered while veto is in effect */
358 pm_runtime_get(&tb
->dev
);
362 static void icm_veto_end(struct tb
*tb
)
364 struct icm
*icm
= tb_priv(tb
);
368 /* Allow the domain suspend now */
369 pm_runtime_mark_last_busy(&tb
->dev
);
370 pm_runtime_put_autosuspend(&tb
->dev
);
374 static bool icm_firmware_running(const struct tb_nhi
*nhi
)
378 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
379 return !!(val
& REG_FW_STS_ICM_EN
);
382 static bool icm_fr_is_supported(struct tb
*tb
)
384 return !x86_apple_machine
;
387 static inline int icm_fr_get_switch_index(u32 port
)
391 if ((port
& ICM_PORT_TYPE_MASK
) != TB_TYPE_PORT
)
394 index
= port
>> ICM_PORT_INDEX_SHIFT
;
395 return index
!= 0xff ? index
: 0;
398 static int icm_fr_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
400 struct icm_fr_pkg_get_topology_response
*switches
, *sw
;
401 struct icm_fr_pkg_get_topology request
= {
402 .hdr
= { .code
= ICM_GET_TOPOLOGY
},
404 size_t npackets
= ICM_GET_TOPOLOGY_PACKETS
;
408 switches
= kcalloc(npackets
, sizeof(*switches
), GFP_KERNEL
);
412 ret
= icm_request(tb
, &request
, sizeof(request
), switches
,
413 sizeof(*switches
), npackets
, ICM_RETRIES
, ICM_TIMEOUT
);
418 index
= icm_fr_get_switch_index(sw
->ports
[link
]);
424 sw
= &switches
[index
];
425 for (i
= 1; i
< depth
; i
++) {
428 if (!(sw
->first_data
& ICM_SWITCH_USED
)) {
433 for (j
= 0; j
< ARRAY_SIZE(sw
->ports
); j
++) {
434 index
= icm_fr_get_switch_index(sw
->ports
[j
]);
435 if (index
> sw
->switch_index
) {
436 sw
= &switches
[index
];
442 *route
= get_route(sw
->route_hi
, sw
->route_lo
);
449 static void icm_fr_save_devices(struct tb
*tb
)
451 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_SAVE_DEVS
, 0);
455 icm_fr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
456 u8
*proto_version
, size_t *nboot_acl
, bool *rpm
)
458 struct icm_fr_pkg_driver_ready_response reply
;
459 struct icm_pkg_driver_ready request
= {
460 .hdr
.code
= ICM_DRIVER_READY
,
464 memset(&reply
, 0, sizeof(reply
));
465 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
466 1, ICM_RETRIES
, ICM_TIMEOUT
);
471 *security_level
= reply
.security_level
& ICM_FR_SLEVEL_MASK
;
476 static int icm_fr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
478 struct icm_fr_pkg_approve_device request
;
479 struct icm_fr_pkg_approve_device reply
;
482 memset(&request
, 0, sizeof(request
));
483 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
484 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
485 request
.connection_id
= sw
->connection_id
;
486 request
.connection_key
= sw
->connection_key
;
488 memset(&reply
, 0, sizeof(reply
));
489 /* Use larger timeout as establishing tunnels can take some time */
490 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
491 1, ICM_RETRIES
, ICM_APPROVE_TIMEOUT
);
495 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
496 tb_warn(tb
, "PCIe tunnel creation failed\n");
503 static int icm_fr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
505 struct icm_fr_pkg_add_device_key request
;
506 struct icm_fr_pkg_add_device_key_response reply
;
509 memset(&request
, 0, sizeof(request
));
510 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
511 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
512 request
.connection_id
= sw
->connection_id
;
513 request
.connection_key
= sw
->connection_key
;
514 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
516 memset(&reply
, 0, sizeof(reply
));
517 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
518 1, ICM_RETRIES
, ICM_TIMEOUT
);
522 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
523 tb_warn(tb
, "Adding key to switch failed\n");
530 static int icm_fr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
531 const u8
*challenge
, u8
*response
)
533 struct icm_fr_pkg_challenge_device request
;
534 struct icm_fr_pkg_challenge_device_response reply
;
537 memset(&request
, 0, sizeof(request
));
538 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
539 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
540 request
.connection_id
= sw
->connection_id
;
541 request
.connection_key
= sw
->connection_key
;
542 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
544 memset(&reply
, 0, sizeof(reply
));
545 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
546 1, ICM_RETRIES
, ICM_TIMEOUT
);
550 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
551 return -EKEYREJECTED
;
552 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
555 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
560 static int icm_fr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
,
561 int transmit_path
, int transmit_ring
,
562 int receive_path
, int receive_ring
)
564 struct icm_fr_pkg_approve_xdomain_response reply
;
565 struct icm_fr_pkg_approve_xdomain request
;
568 memset(&request
, 0, sizeof(request
));
569 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
570 request
.link_info
= xd
->depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| xd
->link
;
571 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
573 request
.transmit_path
= transmit_path
;
574 request
.transmit_ring
= transmit_ring
;
575 request
.receive_path
= receive_path
;
576 request
.receive_ring
= receive_ring
;
578 memset(&reply
, 0, sizeof(reply
));
579 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
580 1, ICM_RETRIES
, ICM_TIMEOUT
);
584 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
590 static int icm_fr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
,
591 int transmit_path
, int transmit_ring
,
592 int receive_path
, int receive_ring
)
597 phy_port
= tb_phy_port_from_link(xd
->link
);
599 cmd
= NHI_MAILBOX_DISCONNECT_PA
;
601 cmd
= NHI_MAILBOX_DISCONNECT_PB
;
603 nhi_mailbox_cmd(tb
->nhi
, cmd
, 1);
604 usleep_range(10, 50);
605 nhi_mailbox_cmd(tb
->nhi
, cmd
, 2);
609 static struct tb_switch
*alloc_switch(struct tb_switch
*parent_sw
, u64 route
,
612 struct tb
*tb
= parent_sw
->tb
;
613 struct tb_switch
*sw
;
615 sw
= tb_switch_alloc(tb
, &parent_sw
->dev
, route
);
617 tb_warn(tb
, "failed to allocate switch at %llx\n", route
);
621 sw
->uuid
= kmemdup(uuid
, sizeof(*uuid
), GFP_KERNEL
);
624 return ERR_PTR(-ENOMEM
);
627 init_completion(&sw
->rpm_complete
);
631 static int add_switch(struct tb_switch
*parent_sw
, struct tb_switch
*sw
)
633 u64 route
= tb_route(sw
);
636 /* Link the two switches now */
637 tb_port_at(route
, parent_sw
)->remote
= tb_upstream_port(sw
);
638 tb_upstream_port(sw
)->remote
= tb_port_at(route
, parent_sw
);
640 ret
= tb_switch_add(sw
);
642 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
647 static void update_switch(struct tb_switch
*sw
, u64 route
, u8 connection_id
,
648 u8 connection_key
, u8 link
, u8 depth
, bool boot
)
650 struct tb_switch
*parent_sw
= tb_switch_parent(sw
);
652 /* Disconnect from parent */
653 tb_switch_downstream_port(sw
)->remote
= NULL
;
654 /* Re-connect via updated port */
655 tb_port_at(route
, parent_sw
)->remote
= tb_upstream_port(sw
);
657 /* Update with the new addressing information */
658 sw
->config
.route_hi
= upper_32_bits(route
);
659 sw
->config
.route_lo
= lower_32_bits(route
);
660 sw
->connection_id
= connection_id
;
661 sw
->connection_key
= connection_key
;
666 /* This switch still exists */
667 sw
->is_unplugged
= false;
669 /* Runtime resume is now complete */
670 complete(&sw
->rpm_complete
);
673 static void remove_switch(struct tb_switch
*sw
)
675 tb_switch_downstream_port(sw
)->remote
= NULL
;
676 tb_switch_remove(sw
);
679 static void add_xdomain(struct tb_switch
*sw
, u64 route
,
680 const uuid_t
*local_uuid
, const uuid_t
*remote_uuid
,
683 struct tb_xdomain
*xd
;
685 pm_runtime_get_sync(&sw
->dev
);
687 xd
= tb_xdomain_alloc(sw
->tb
, &sw
->dev
, route
, local_uuid
, remote_uuid
);
694 tb_port_at(route
, sw
)->xdomain
= xd
;
699 pm_runtime_mark_last_busy(&sw
->dev
);
700 pm_runtime_put_autosuspend(&sw
->dev
);
703 static void update_xdomain(struct tb_xdomain
*xd
, u64 route
, u8 link
)
707 xd
->is_unplugged
= false;
710 static void remove_xdomain(struct tb_xdomain
*xd
)
712 struct tb_switch
*sw
;
714 sw
= tb_to_switch(xd
->dev
.parent
);
715 tb_port_at(xd
->route
, sw
)->xdomain
= NULL
;
716 tb_xdomain_remove(xd
);
720 icm_fr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
722 const struct icm_fr_event_device_connected
*pkg
=
723 (const struct icm_fr_event_device_connected
*)hdr
;
724 enum tb_security_level security_level
;
725 struct tb_switch
*sw
, *parent_sw
;
726 bool boot
, dual_lane
, speed_gen3
;
727 struct icm
*icm
= tb_priv(tb
);
728 bool authorized
= false;
729 struct tb_xdomain
*xd
;
734 icm_postpone_rescan(tb
);
736 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
737 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
738 ICM_LINK_INFO_DEPTH_SHIFT
;
739 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
740 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
741 ICM_FLAGS_SLEVEL_SHIFT
;
742 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
743 dual_lane
= pkg
->hdr
.flags
& ICM_FLAGS_DUAL_LANE
;
744 speed_gen3
= pkg
->hdr
.flags
& ICM_FLAGS_SPEED_GEN3
;
746 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
747 tb_info(tb
, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
752 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
754 u8 phy_port
, sw_phy_port
;
756 sw_phy_port
= tb_phy_port_from_link(sw
->link
);
757 phy_port
= tb_phy_port_from_link(link
);
760 * On resume ICM will send us connected events for the
761 * devices that still are present. However, that
762 * information might have changed for example by the
763 * fact that a switch on a dual-link connection might
764 * have been enumerated using the other link now. Make
765 * sure our book keeping matches that.
767 if (sw
->depth
== depth
&& sw_phy_port
== phy_port
&&
768 !!sw
->authorized
== authorized
) {
770 * It was enumerated through another link so update
771 * route string accordingly.
773 if (sw
->link
!= link
) {
774 ret
= icm
->get_route(tb
, link
, depth
, &route
);
776 tb_err(tb
, "failed to update route string for switch at %u.%u\n",
782 route
= tb_route(sw
);
785 update_switch(sw
, route
, pkg
->connection_id
,
786 pkg
->connection_key
, link
, depth
, boot
);
792 * User connected the same switch to another physical
793 * port or to another part of the topology. Remove the
794 * existing switch now before adding the new one.
801 * If the switch was not found by UUID, look for a switch on
802 * same physical port (taking possible link aggregation into
803 * account) and depth. If we found one it is definitely a stale
804 * one so remove it first.
806 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
810 dual_link
= dual_link_from_link(link
);
812 sw
= tb_switch_find_by_link_depth(tb
, dual_link
, depth
);
819 /* Remove existing XDomain connection if found */
820 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
826 parent_sw
= tb_switch_find_by_link_depth(tb
, link
, depth
- 1);
828 tb_err(tb
, "failed to find parent switch for %u.%u\n",
833 ret
= icm
->get_route(tb
, link
, depth
, &route
);
835 tb_err(tb
, "failed to find route string for switch at %u.%u\n",
837 tb_switch_put(parent_sw
);
841 pm_runtime_get_sync(&parent_sw
->dev
);
843 sw
= alloc_switch(parent_sw
, route
, &pkg
->ep_uuid
);
845 sw
->connection_id
= pkg
->connection_id
;
846 sw
->connection_key
= pkg
->connection_key
;
849 sw
->authorized
= authorized
;
850 sw
->security_level
= security_level
;
852 sw
->link_speed
= speed_gen3
? 20 : 10;
853 sw
->link_width
= dual_lane
? TB_LINK_WIDTH_DUAL
:
854 TB_LINK_WIDTH_SINGLE
;
855 sw
->rpm
= intel_vss_is_rtd3(pkg
->ep_name
, sizeof(pkg
->ep_name
));
857 if (add_switch(parent_sw
, sw
))
861 pm_runtime_mark_last_busy(&parent_sw
->dev
);
862 pm_runtime_put_autosuspend(&parent_sw
->dev
);
864 tb_switch_put(parent_sw
);
868 icm_fr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
870 const struct icm_fr_event_device_disconnected
*pkg
=
871 (const struct icm_fr_event_device_disconnected
*)hdr
;
872 struct tb_switch
*sw
;
875 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
876 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
877 ICM_LINK_INFO_DEPTH_SHIFT
;
879 if (link
> ICM_MAX_LINK
|| depth
> TB_SWITCH_MAX_DEPTH
) {
880 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
884 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
886 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
891 pm_runtime_get_sync(sw
->dev
.parent
);
895 pm_runtime_mark_last_busy(sw
->dev
.parent
);
896 pm_runtime_put_autosuspend(sw
->dev
.parent
);
902 icm_fr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
904 const struct icm_fr_event_xdomain_connected
*pkg
=
905 (const struct icm_fr_event_xdomain_connected
*)hdr
;
906 struct tb_xdomain
*xd
;
907 struct tb_switch
*sw
;
911 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
912 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
913 ICM_LINK_INFO_DEPTH_SHIFT
;
915 if (link
> ICM_MAX_LINK
|| depth
> TB_SWITCH_MAX_DEPTH
) {
916 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
920 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
922 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
924 u8 xd_phy_port
, phy_port
;
926 xd_phy_port
= phy_port_from_route(xd
->route
, xd
->depth
);
927 phy_port
= phy_port_from_route(route
, depth
);
929 if (xd
->depth
== depth
&& xd_phy_port
== phy_port
) {
930 update_xdomain(xd
, route
, link
);
936 * If we find an existing XDomain connection remove it
937 * now. We need to go through login handshake and
938 * everything anyway to be able to re-establish the
946 * Look if there already exists an XDomain in the same place
947 * than the new one and in that case remove it because it is
948 * most likely another host that got disconnected.
950 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
954 dual_link
= dual_link_from_link(link
);
956 xd
= tb_xdomain_find_by_link_depth(tb
, dual_link
,
965 * If the user disconnected a switch during suspend and
966 * connected another host to the same port, remove the switch
969 sw
= tb_switch_find_by_route(tb
, route
);
975 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
977 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
982 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, link
,
988 icm_fr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
990 const struct icm_fr_event_xdomain_disconnected
*pkg
=
991 (const struct icm_fr_event_xdomain_disconnected
*)hdr
;
992 struct tb_xdomain
*xd
;
995 * If the connection is through one or multiple devices, the
996 * XDomain device is removed along with them so it is fine if we
997 * cannot find it here.
999 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
1006 static int icm_tr_cio_reset(struct tb
*tb
)
1008 return pcie2cio_write(tb_priv(tb
), TB_CFG_SWITCH
, 0, 0x777, BIT(1));
1012 icm_tr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1013 u8
*proto_version
, size_t *nboot_acl
, bool *rpm
)
1015 struct icm_tr_pkg_driver_ready_response reply
;
1016 struct icm_pkg_driver_ready request
= {
1017 .hdr
.code
= ICM_DRIVER_READY
,
1021 memset(&reply
, 0, sizeof(reply
));
1022 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1028 *security_level
= reply
.info
& ICM_TR_INFO_SLEVEL_MASK
;
1030 *proto_version
= (reply
.info
& ICM_TR_INFO_PROTO_VERSION_MASK
) >>
1031 ICM_TR_INFO_PROTO_VERSION_SHIFT
;
1033 *nboot_acl
= (reply
.info
& ICM_TR_INFO_BOOT_ACL_MASK
) >>
1034 ICM_TR_INFO_BOOT_ACL_SHIFT
;
1036 *rpm
= !!(reply
.hdr
.flags
& ICM_TR_FLAGS_RTD3
);
1041 static int icm_tr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
1043 struct icm_tr_pkg_approve_device request
;
1044 struct icm_tr_pkg_approve_device reply
;
1047 memset(&request
, 0, sizeof(request
));
1048 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
1049 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
1050 request
.route_lo
= sw
->config
.route_lo
;
1051 request
.route_hi
= sw
->config
.route_hi
;
1052 request
.connection_id
= sw
->connection_id
;
1054 memset(&reply
, 0, sizeof(reply
));
1055 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1056 1, ICM_RETRIES
, ICM_APPROVE_TIMEOUT
);
1060 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
1061 tb_warn(tb
, "PCIe tunnel creation failed\n");
1068 static int icm_tr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
1070 struct icm_tr_pkg_add_device_key_response reply
;
1071 struct icm_tr_pkg_add_device_key request
;
1074 memset(&request
, 0, sizeof(request
));
1075 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
1076 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
1077 request
.route_lo
= sw
->config
.route_lo
;
1078 request
.route_hi
= sw
->config
.route_hi
;
1079 request
.connection_id
= sw
->connection_id
;
1080 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
1082 memset(&reply
, 0, sizeof(reply
));
1083 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1084 1, ICM_RETRIES
, ICM_TIMEOUT
);
1088 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
1089 tb_warn(tb
, "Adding key to switch failed\n");
1096 static int icm_tr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
1097 const u8
*challenge
, u8
*response
)
1099 struct icm_tr_pkg_challenge_device_response reply
;
1100 struct icm_tr_pkg_challenge_device request
;
1103 memset(&request
, 0, sizeof(request
));
1104 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
1105 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
1106 request
.route_lo
= sw
->config
.route_lo
;
1107 request
.route_hi
= sw
->config
.route_hi
;
1108 request
.connection_id
= sw
->connection_id
;
1109 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
1111 memset(&reply
, 0, sizeof(reply
));
1112 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1113 1, ICM_RETRIES
, ICM_TIMEOUT
);
1117 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1118 return -EKEYREJECTED
;
1119 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
1122 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
1127 static int icm_tr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
,
1128 int transmit_path
, int transmit_ring
,
1129 int receive_path
, int receive_ring
)
1131 struct icm_tr_pkg_approve_xdomain_response reply
;
1132 struct icm_tr_pkg_approve_xdomain request
;
1135 memset(&request
, 0, sizeof(request
));
1136 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
1137 request
.route_hi
= upper_32_bits(xd
->route
);
1138 request
.route_lo
= lower_32_bits(xd
->route
);
1139 request
.transmit_path
= transmit_path
;
1140 request
.transmit_ring
= transmit_ring
;
1141 request
.receive_path
= receive_path
;
1142 request
.receive_ring
= receive_ring
;
1143 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
1145 memset(&reply
, 0, sizeof(reply
));
1146 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1147 1, ICM_RETRIES
, ICM_TIMEOUT
);
1151 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1157 static int icm_tr_xdomain_tear_down(struct tb
*tb
, struct tb_xdomain
*xd
,
1160 struct icm_tr_pkg_disconnect_xdomain_response reply
;
1161 struct icm_tr_pkg_disconnect_xdomain request
;
1164 memset(&request
, 0, sizeof(request
));
1165 request
.hdr
.code
= ICM_DISCONNECT_XDOMAIN
;
1166 request
.stage
= stage
;
1167 request
.route_hi
= upper_32_bits(xd
->route
);
1168 request
.route_lo
= lower_32_bits(xd
->route
);
1169 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
1171 memset(&reply
, 0, sizeof(reply
));
1172 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1173 1, ICM_RETRIES
, ICM_TIMEOUT
);
1177 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1183 static int icm_tr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
,
1184 int transmit_path
, int transmit_ring
,
1185 int receive_path
, int receive_ring
)
1189 ret
= icm_tr_xdomain_tear_down(tb
, xd
, 1);
1193 usleep_range(10, 50);
1194 return icm_tr_xdomain_tear_down(tb
, xd
, 2);
1198 __icm_tr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
,
1201 const struct icm_tr_event_device_connected
*pkg
=
1202 (const struct icm_tr_event_device_connected
*)hdr
;
1203 bool authorized
, boot
, dual_lane
, speed_gen3
;
1204 enum tb_security_level security_level
;
1205 struct tb_switch
*sw
, *parent_sw
;
1206 struct tb_xdomain
*xd
;
1209 icm_postpone_rescan(tb
);
1212 * Currently we don't use the QoS information coming with the
1213 * device connected message so simply just ignore that extra
1216 if (pkg
->hdr
.packet_id
)
1219 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1220 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
1221 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
1222 ICM_FLAGS_SLEVEL_SHIFT
;
1223 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
1224 dual_lane
= pkg
->hdr
.flags
& ICM_FLAGS_DUAL_LANE
;
1225 speed_gen3
= pkg
->hdr
.flags
& ICM_FLAGS_SPEED_GEN3
;
1227 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
1228 tb_info(tb
, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1233 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
1235 /* Update the switch if it is still in the same place */
1236 if (tb_route(sw
) == route
&& !!sw
->authorized
== authorized
) {
1237 update_switch(sw
, route
, pkg
->connection_id
, 0, 0, 0,
1247 /* Another switch with the same address */
1248 sw
= tb_switch_find_by_route(tb
, route
);
1254 /* XDomain connection with the same address */
1255 xd
= tb_xdomain_find_by_route(tb
, route
);
1261 parent_sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1263 tb_err(tb
, "failed to find parent switch for %llx\n", route
);
1267 pm_runtime_get_sync(&parent_sw
->dev
);
1269 sw
= alloc_switch(parent_sw
, route
, &pkg
->ep_uuid
);
1271 sw
->connection_id
= pkg
->connection_id
;
1272 sw
->authorized
= authorized
;
1273 sw
->security_level
= security_level
;
1275 sw
->link_speed
= speed_gen3
? 20 : 10;
1276 sw
->link_width
= dual_lane
? TB_LINK_WIDTH_DUAL
:
1277 TB_LINK_WIDTH_SINGLE
;
1278 sw
->rpm
= force_rtd3
;
1280 sw
->rpm
= intel_vss_is_rtd3(pkg
->ep_name
,
1281 sizeof(pkg
->ep_name
));
1283 if (add_switch(parent_sw
, sw
))
1287 pm_runtime_mark_last_busy(&parent_sw
->dev
);
1288 pm_runtime_put_autosuspend(&parent_sw
->dev
);
1290 tb_switch_put(parent_sw
);
1294 icm_tr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1296 __icm_tr_device_connected(tb
, hdr
, false);
1300 icm_tr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1302 const struct icm_tr_event_device_disconnected
*pkg
=
1303 (const struct icm_tr_event_device_disconnected
*)hdr
;
1304 struct tb_switch
*sw
;
1307 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1309 sw
= tb_switch_find_by_route(tb
, route
);
1311 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1314 pm_runtime_get_sync(sw
->dev
.parent
);
1318 pm_runtime_mark_last_busy(sw
->dev
.parent
);
1319 pm_runtime_put_autosuspend(sw
->dev
.parent
);
1325 icm_tr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1327 const struct icm_tr_event_xdomain_connected
*pkg
=
1328 (const struct icm_tr_event_xdomain_connected
*)hdr
;
1329 struct tb_xdomain
*xd
;
1330 struct tb_switch
*sw
;
1333 if (!tb
->root_switch
)
1336 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
1338 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
1340 if (xd
->route
== route
) {
1341 update_xdomain(xd
, route
, 0);
1350 /* An existing xdomain with the same address */
1351 xd
= tb_xdomain_find_by_route(tb
, route
);
1358 * If the user disconnected a switch during suspend and
1359 * connected another host to the same port, remove the switch
1362 sw
= tb_switch_find_by_route(tb
, route
);
1368 sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1370 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1374 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, 0, 0);
1379 icm_tr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1381 const struct icm_tr_event_xdomain_disconnected
*pkg
=
1382 (const struct icm_tr_event_xdomain_disconnected
*)hdr
;
1383 struct tb_xdomain
*xd
;
1386 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1388 xd
= tb_xdomain_find_by_route(tb
, route
);
1395 static struct pci_dev
*get_upstream_port(struct pci_dev
*pdev
)
1397 struct pci_dev
*parent
;
1399 parent
= pci_upstream_bridge(pdev
);
1401 if (!pci_is_pcie(parent
))
1403 if (pci_pcie_type(parent
) == PCI_EXP_TYPE_UPSTREAM
)
1405 parent
= pci_upstream_bridge(parent
);
1411 switch (parent
->device
) {
1412 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1413 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1414 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1415 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1416 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1417 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
1418 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
1425 static bool icm_ar_is_supported(struct tb
*tb
)
1427 struct pci_dev
*upstream_port
;
1428 struct icm
*icm
= tb_priv(tb
);
1431 * Starting from Alpine Ridge we can use ICM on Apple machines
1432 * as well. We just need to reset and re-enable it first.
1433 * However, only start it if explicitly asked by the user.
1435 if (icm_firmware_running(tb
->nhi
))
1441 * Find the upstream PCIe port in case we need to do reset
1442 * through its vendor specific registers.
1444 upstream_port
= get_upstream_port(tb
->nhi
->pdev
);
1445 if (upstream_port
) {
1448 cap
= pci_find_ext_capability(upstream_port
,
1449 PCI_EXT_CAP_ID_VNDR
);
1451 icm
->upstream_port
= upstream_port
;
1461 static int icm_ar_cio_reset(struct tb
*tb
)
1463 return pcie2cio_write(tb_priv(tb
), TB_CFG_SWITCH
, 0, 0x50, BIT(9));
1466 static int icm_ar_get_mode(struct tb
*tb
)
1468 struct tb_nhi
*nhi
= tb
->nhi
;
1473 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1474 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1477 } while (--retries
);
1480 dev_err(&nhi
->pdev
->dev
, "ICM firmware not authenticated\n");
1484 return nhi_mailbox_mode(nhi
);
1488 icm_ar_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1489 u8
*proto_version
, size_t *nboot_acl
, bool *rpm
)
1491 struct icm_ar_pkg_driver_ready_response reply
;
1492 struct icm_pkg_driver_ready request
= {
1493 .hdr
.code
= ICM_DRIVER_READY
,
1497 memset(&reply
, 0, sizeof(reply
));
1498 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1499 1, ICM_RETRIES
, ICM_TIMEOUT
);
1504 *security_level
= reply
.info
& ICM_AR_INFO_SLEVEL_MASK
;
1505 if (nboot_acl
&& (reply
.info
& ICM_AR_INFO_BOOT_ACL_SUPPORTED
))
1506 *nboot_acl
= (reply
.info
& ICM_AR_INFO_BOOT_ACL_MASK
) >>
1507 ICM_AR_INFO_BOOT_ACL_SHIFT
;
1509 *rpm
= !!(reply
.hdr
.flags
& ICM_AR_FLAGS_RTD3
);
1514 static int icm_ar_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
1516 struct icm_ar_pkg_get_route_response reply
;
1517 struct icm_ar_pkg_get_route request
= {
1518 .hdr
= { .code
= ICM_GET_ROUTE
},
1519 .link_info
= depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| link
,
1523 memset(&reply
, 0, sizeof(reply
));
1524 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1525 1, ICM_RETRIES
, ICM_TIMEOUT
);
1529 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1532 *route
= get_route(reply
.route_hi
, reply
.route_lo
);
1536 static int icm_ar_get_boot_acl(struct tb
*tb
, uuid_t
*uuids
, size_t nuuids
)
1538 struct icm_ar_pkg_preboot_acl_response reply
;
1539 struct icm_ar_pkg_preboot_acl request
= {
1540 .hdr
= { .code
= ICM_PREBOOT_ACL
},
1544 memset(&reply
, 0, sizeof(reply
));
1545 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1546 1, ICM_RETRIES
, ICM_TIMEOUT
);
1550 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1553 for (i
= 0; i
< nuuids
; i
++) {
1554 u32
*uuid
= (u32
*)&uuids
[i
];
1556 uuid
[0] = reply
.acl
[i
].uuid_lo
;
1557 uuid
[1] = reply
.acl
[i
].uuid_hi
;
1559 if (uuid
[0] == 0xffffffff && uuid
[1] == 0xffffffff) {
1560 /* Map empty entries to null UUID */
1563 } else if (uuid
[0] != 0 || uuid
[1] != 0) {
1564 /* Upper two DWs are always one's */
1565 uuid
[2] = 0xffffffff;
1566 uuid
[3] = 0xffffffff;
1573 static int icm_ar_set_boot_acl(struct tb
*tb
, const uuid_t
*uuids
,
1576 struct icm_ar_pkg_preboot_acl_response reply
;
1577 struct icm_ar_pkg_preboot_acl request
= {
1579 .code
= ICM_PREBOOT_ACL
,
1580 .flags
= ICM_FLAGS_WRITE
,
1585 for (i
= 0; i
< nuuids
; i
++) {
1586 const u32
*uuid
= (const u32
*)&uuids
[i
];
1588 if (uuid_is_null(&uuids
[i
])) {
1590 * Map null UUID to the empty (all one) entries
1593 request
.acl
[i
].uuid_lo
= 0xffffffff;
1594 request
.acl
[i
].uuid_hi
= 0xffffffff;
1596 /* Two high DWs need to be set to all one */
1597 if (uuid
[2] != 0xffffffff || uuid
[3] != 0xffffffff)
1600 request
.acl
[i
].uuid_lo
= uuid
[0];
1601 request
.acl
[i
].uuid_hi
= uuid
[1];
1605 memset(&reply
, 0, sizeof(reply
));
1606 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1607 1, ICM_RETRIES
, ICM_TIMEOUT
);
1611 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1618 icm_icl_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1619 u8
*proto_version
, size_t *nboot_acl
, bool *rpm
)
1621 struct icm_tr_pkg_driver_ready_response reply
;
1622 struct icm_pkg_driver_ready request
= {
1623 .hdr
.code
= ICM_DRIVER_READY
,
1627 memset(&reply
, 0, sizeof(reply
));
1628 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1629 1, ICM_RETRIES
, 20000);
1634 *proto_version
= (reply
.info
& ICM_TR_INFO_PROTO_VERSION_MASK
) >>
1635 ICM_TR_INFO_PROTO_VERSION_SHIFT
;
1637 /* Ice Lake always supports RTD3 */
1644 static void icm_icl_set_uuid(struct tb
*tb
)
1646 struct tb_nhi
*nhi
= tb
->nhi
;
1649 pci_read_config_dword(nhi
->pdev
, VS_CAP_10
, &uuid
[0]);
1650 pci_read_config_dword(nhi
->pdev
, VS_CAP_11
, &uuid
[1]);
1651 uuid
[2] = 0xffffffff;
1652 uuid
[3] = 0xffffffff;
1654 tb
->root_switch
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1658 icm_icl_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1660 __icm_tr_device_connected(tb
, hdr
, true);
1663 static void icm_icl_rtd3_veto(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1665 const struct icm_icl_event_rtd3_veto
*pkg
=
1666 (const struct icm_icl_event_rtd3_veto
*)hdr
;
1668 tb_dbg(tb
, "ICM rtd3 veto=0x%08x\n", pkg
->veto_reason
);
1670 if (pkg
->veto_reason
)
1676 static bool icm_tgl_is_supported(struct tb
*tb
)
1678 unsigned long end
= jiffies
+ msecs_to_jiffies(10);
1683 val
= ioread32(tb
->nhi
->iobase
+ REG_FW_STS
);
1684 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1686 usleep_range(100, 500);
1687 } while (time_before(jiffies
, end
));
1692 static void icm_handle_notification(struct work_struct
*work
)
1694 struct icm_notification
*n
= container_of(work
, typeof(*n
), work
);
1695 struct tb
*tb
= n
->tb
;
1696 struct icm
*icm
= tb_priv(tb
);
1698 mutex_lock(&tb
->lock
);
1701 * When the domain is stopped we flush its workqueue but before
1702 * that the root switch is removed. In that case we should treat
1703 * the queued events as being canceled.
1705 if (tb
->root_switch
) {
1706 switch (n
->pkg
->code
) {
1707 case ICM_EVENT_DEVICE_CONNECTED
:
1708 icm
->device_connected(tb
, n
->pkg
);
1710 case ICM_EVENT_DEVICE_DISCONNECTED
:
1711 icm
->device_disconnected(tb
, n
->pkg
);
1713 case ICM_EVENT_XDOMAIN_CONNECTED
:
1714 if (tb_is_xdomain_enabled())
1715 icm
->xdomain_connected(tb
, n
->pkg
);
1717 case ICM_EVENT_XDOMAIN_DISCONNECTED
:
1718 if (tb_is_xdomain_enabled())
1719 icm
->xdomain_disconnected(tb
, n
->pkg
);
1721 case ICM_EVENT_RTD3_VETO
:
1722 icm
->rtd3_veto(tb
, n
->pkg
);
1727 mutex_unlock(&tb
->lock
);
1733 static void icm_handle_event(struct tb
*tb
, enum tb_cfg_pkg_type type
,
1734 const void *buf
, size_t size
)
1736 struct icm_notification
*n
;
1738 n
= kmalloc(sizeof(*n
), GFP_KERNEL
);
1742 n
->pkg
= kmemdup(buf
, size
, GFP_KERNEL
);
1748 INIT_WORK(&n
->work
, icm_handle_notification
);
1751 queue_work(tb
->wq
, &n
->work
);
1755 __icm_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1756 u8
*proto_version
, size_t *nboot_acl
, bool *rpm
)
1758 struct icm
*icm
= tb_priv(tb
);
1759 unsigned int retries
= 50;
1762 ret
= icm
->driver_ready(tb
, security_level
, proto_version
, nboot_acl
,
1765 tb_err(tb
, "failed to send driver ready to ICM\n");
1770 * Hold on here until the switch config space is accessible so
1771 * that we can read root switch config successfully.
1774 struct tb_cfg_result res
;
1777 res
= tb_cfg_read_raw(tb
->ctl
, &tmp
, 0, 0, TB_CFG_SWITCH
,
1783 } while (--retries
);
1785 tb_err(tb
, "failed to read root switch config space, giving up\n");
1789 static int icm_firmware_reset(struct tb
*tb
, struct tb_nhi
*nhi
)
1791 struct icm
*icm
= tb_priv(tb
);
1794 if (!icm
->upstream_port
)
1797 /* Put ARC to wait for CIO reset event to happen */
1798 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1799 val
|= REG_FW_STS_CIO_RESET_REQ
;
1800 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1803 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1804 val
|= REG_FW_STS_ICM_EN_INVERT
;
1805 val
|= REG_FW_STS_ICM_EN_CPU
;
1806 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1808 /* Trigger CIO reset now */
1809 return icm
->cio_reset(tb
);
1812 static int icm_firmware_start(struct tb
*tb
, struct tb_nhi
*nhi
)
1814 unsigned int retries
= 10;
1818 /* Check if the ICM firmware is already running */
1819 if (icm_firmware_running(nhi
))
1822 dev_dbg(&nhi
->pdev
->dev
, "starting ICM firmware\n");
1824 ret
= icm_firmware_reset(tb
, nhi
);
1828 /* Wait until the ICM firmware tells us it is up and running */
1830 /* Check that the ICM firmware is running */
1831 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1832 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1836 } while (--retries
);
1841 static int icm_reset_phy_port(struct tb
*tb
, int phy_port
)
1843 struct icm
*icm
= tb_priv(tb
);
1849 if (!icm
->upstream_port
)
1861 * Read link status of both null ports belonging to a single
1864 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1867 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1871 state0
= val0
& PHY_PORT_CS1_LINK_STATE_MASK
;
1872 state0
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1873 state1
= val1
& PHY_PORT_CS1_LINK_STATE_MASK
;
1874 state1
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1876 /* If they are both up we need to reset them now */
1877 if (state0
!= TB_PORT_UP
|| state1
!= TB_PORT_UP
)
1880 val0
|= PHY_PORT_CS1_LINK_DISABLE
;
1881 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1885 val1
|= PHY_PORT_CS1_LINK_DISABLE
;
1886 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1890 /* Wait a bit and then re-enable both ports */
1891 usleep_range(10, 100);
1893 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1896 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1900 val0
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1901 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1905 val1
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1906 return pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1909 static int icm_firmware_init(struct tb
*tb
)
1911 struct icm
*icm
= tb_priv(tb
);
1912 struct tb_nhi
*nhi
= tb
->nhi
;
1915 ret
= icm_firmware_start(tb
, nhi
);
1917 dev_err(&nhi
->pdev
->dev
, "could not start ICM firmware\n");
1921 if (icm
->get_mode
) {
1922 ret
= icm
->get_mode(tb
);
1925 case NHI_FW_SAFE_MODE
:
1926 icm
->safe_mode
= true;
1929 case NHI_FW_CM_MODE
:
1930 /* Ask ICM to accept all Thunderbolt devices */
1931 nhi_mailbox_cmd(nhi
, NHI_MAILBOX_ALLOW_ALL_DEVS
, 0);
1938 tb_err(tb
, "ICM firmware is in wrong mode: %u\n", ret
);
1944 * Reset both physical ports if there is anything connected to
1947 ret
= icm_reset_phy_port(tb
, 0);
1949 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port0\n");
1950 ret
= icm_reset_phy_port(tb
, 1);
1952 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port1\n");
1957 static int icm_driver_ready(struct tb
*tb
)
1959 struct icm
*icm
= tb_priv(tb
);
1962 ret
= icm_firmware_init(tb
);
1966 if (icm
->safe_mode
) {
1967 tb_info(tb
, "Thunderbolt host controller is in safe mode.\n");
1968 tb_info(tb
, "You need to update NVM firmware of the controller before it can be used.\n");
1969 tb_info(tb
, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1973 ret
= __icm_driver_ready(tb
, &tb
->security_level
, &icm
->proto_version
,
1974 &tb
->nboot_acl
, &icm
->rpm
);
1979 * Make sure the number of supported preboot ACL matches what we
1980 * expect or disable the whole feature.
1982 if (tb
->nboot_acl
> icm
->max_boot_acl
)
1985 if (icm
->proto_version
>= 3)
1986 tb_dbg(tb
, "USB4 proxy operations supported\n");
1991 static int icm_suspend(struct tb
*tb
)
1993 struct icm
*icm
= tb_priv(tb
);
1995 if (icm
->save_devices
)
1996 icm
->save_devices(tb
);
1998 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
2003 * Mark all switches (except root switch) below this one unplugged. ICM
2004 * firmware will send us an updated list of switches after we have send
2005 * it driver ready command. If a switch is not in that list it will be
2006 * removed when we perform rescan.
2008 static void icm_unplug_children(struct tb_switch
*sw
)
2010 struct tb_port
*port
;
2013 sw
->is_unplugged
= true;
2015 tb_switch_for_each_port(sw
, port
) {
2017 port
->xdomain
->is_unplugged
= true;
2018 else if (tb_port_has_remote(port
))
2019 icm_unplug_children(port
->remote
->sw
);
2023 static int complete_rpm(struct device
*dev
, void *data
)
2025 struct tb_switch
*sw
= tb_to_switch(dev
);
2028 complete(&sw
->rpm_complete
);
2032 static void remove_unplugged_switch(struct tb_switch
*sw
)
2034 struct device
*parent
= get_device(sw
->dev
.parent
);
2036 pm_runtime_get_sync(parent
);
2039 * Signal this and switches below for rpm_complete because
2040 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
2043 complete_rpm(&sw
->dev
, NULL
);
2044 bus_for_each_dev(&tb_bus_type
, &sw
->dev
, NULL
, complete_rpm
);
2045 tb_switch_remove(sw
);
2047 pm_runtime_mark_last_busy(parent
);
2048 pm_runtime_put_autosuspend(parent
);
2053 static void icm_free_unplugged_children(struct tb_switch
*sw
)
2055 struct tb_port
*port
;
2057 tb_switch_for_each_port(sw
, port
) {
2058 if (port
->xdomain
&& port
->xdomain
->is_unplugged
) {
2059 tb_xdomain_remove(port
->xdomain
);
2060 port
->xdomain
= NULL
;
2061 } else if (tb_port_has_remote(port
)) {
2062 if (port
->remote
->sw
->is_unplugged
) {
2063 remove_unplugged_switch(port
->remote
->sw
);
2064 port
->remote
= NULL
;
2066 icm_free_unplugged_children(port
->remote
->sw
);
2072 static void icm_rescan_work(struct work_struct
*work
)
2074 struct icm
*icm
= container_of(work
, struct icm
, rescan_work
.work
);
2075 struct tb
*tb
= icm_to_tb(icm
);
2077 mutex_lock(&tb
->lock
);
2078 if (tb
->root_switch
)
2079 icm_free_unplugged_children(tb
->root_switch
);
2080 mutex_unlock(&tb
->lock
);
2083 static void icm_complete(struct tb
*tb
)
2085 struct icm
*icm
= tb_priv(tb
);
2087 if (tb
->nhi
->going_away
)
2091 * If RTD3 was vetoed before we entered system suspend allow it
2092 * again now before driver ready is sent. Firmware sends a new RTD3
2093 * veto if it is still the case after we have sent it driver ready
2097 icm_unplug_children(tb
->root_switch
);
2100 * Now all existing children should be resumed, start events
2101 * from ICM to get updated status.
2103 __icm_driver_ready(tb
, NULL
, NULL
, NULL
, NULL
);
2106 * We do not get notifications of devices that have been
2107 * unplugged during suspend so schedule rescan to clean them up
2110 queue_delayed_work(tb
->wq
, &icm
->rescan_work
, msecs_to_jiffies(500));
2113 static int icm_runtime_suspend(struct tb
*tb
)
2115 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
2119 static int icm_runtime_suspend_switch(struct tb_switch
*sw
)
2122 reinit_completion(&sw
->rpm_complete
);
2126 static int icm_runtime_resume_switch(struct tb_switch
*sw
)
2129 if (!wait_for_completion_timeout(&sw
->rpm_complete
,
2130 msecs_to_jiffies(500))) {
2131 dev_dbg(&sw
->dev
, "runtime resuming timed out\n");
2137 static int icm_runtime_resume(struct tb
*tb
)
2140 * We can reuse the same resume functionality than with system
2147 static int icm_start(struct tb
*tb
, bool not_used
)
2149 struct icm
*icm
= tb_priv(tb
);
2153 tb
->root_switch
= tb_switch_alloc_safe_mode(tb
, &tb
->dev
, 0);
2155 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
2156 if (IS_ERR(tb
->root_switch
))
2157 return PTR_ERR(tb
->root_switch
);
2159 tb
->root_switch
->no_nvm_upgrade
= !icm
->can_upgrade_nvm
;
2160 tb
->root_switch
->rpm
= icm
->rpm
;
2165 ret
= tb_switch_add(tb
->root_switch
);
2167 tb_switch_put(tb
->root_switch
);
2168 tb
->root_switch
= NULL
;
2174 static void icm_stop(struct tb
*tb
)
2176 struct icm
*icm
= tb_priv(tb
);
2178 cancel_delayed_work(&icm
->rescan_work
);
2179 tb_switch_remove(tb
->root_switch
);
2180 tb
->root_switch
= NULL
;
2181 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
2182 kfree(icm
->last_nvm_auth
);
2183 icm
->last_nvm_auth
= NULL
;
2186 static int icm_disconnect_pcie_paths(struct tb
*tb
)
2188 return nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DISCONNECT_PCIE_PATHS
, 0);
2191 static void icm_usb4_switch_nvm_auth_complete(void *data
)
2193 struct usb4_switch_nvm_auth
*auth
= data
;
2194 struct icm
*icm
= auth
->icm
;
2195 struct tb
*tb
= icm_to_tb(icm
);
2197 tb_dbg(tb
, "NVM_AUTH response for %llx flags %#x status %#x\n",
2198 get_route(auth
->reply
.route_hi
, auth
->reply
.route_lo
),
2199 auth
->reply
.hdr
.flags
, auth
->reply
.status
);
2201 mutex_lock(&tb
->lock
);
2202 if (WARN_ON(icm
->last_nvm_auth
))
2203 kfree(icm
->last_nvm_auth
);
2204 icm
->last_nvm_auth
= auth
;
2205 mutex_unlock(&tb
->lock
);
2208 static int icm_usb4_switch_nvm_authenticate(struct tb
*tb
, u64 route
)
2210 struct usb4_switch_nvm_auth
*auth
;
2211 struct icm
*icm
= tb_priv(tb
);
2212 struct tb_cfg_request
*req
;
2215 auth
= kzalloc(sizeof(*auth
), GFP_KERNEL
);
2220 auth
->request
.hdr
.code
= ICM_USB4_SWITCH_OP
;
2221 auth
->request
.route_hi
= upper_32_bits(route
);
2222 auth
->request
.route_lo
= lower_32_bits(route
);
2223 auth
->request
.opcode
= USB4_SWITCH_OP_NVM_AUTH
;
2225 req
= tb_cfg_request_alloc();
2231 req
->match
= icm_match
;
2232 req
->copy
= icm_copy
;
2233 req
->request
= &auth
->request
;
2234 req
->request_size
= sizeof(auth
->request
);
2235 req
->request_type
= TB_CFG_PKG_ICM_CMD
;
2236 req
->response
= &auth
->reply
;
2238 req
->response_size
= sizeof(auth
->reply
);
2239 req
->response_type
= TB_CFG_PKG_ICM_RESP
;
2241 tb_dbg(tb
, "NVM_AUTH request for %llx\n", route
);
2243 mutex_lock(&icm
->request_lock
);
2244 ret
= tb_cfg_request(tb
->ctl
, req
, icm_usb4_switch_nvm_auth_complete
,
2246 mutex_unlock(&icm
->request_lock
);
2248 tb_cfg_request_put(req
);
2258 static int icm_usb4_switch_op(struct tb_switch
*sw
, u16 opcode
, u32
*metadata
,
2259 u8
*status
, const void *tx_data
, size_t tx_data_len
,
2260 void *rx_data
, size_t rx_data_len
)
2262 struct icm_usb4_switch_op_response reply
;
2263 struct icm_usb4_switch_op request
;
2264 struct tb
*tb
= sw
->tb
;
2265 struct icm
*icm
= tb_priv(tb
);
2266 u64 route
= tb_route(sw
);
2270 * USB4 router operation proxy is supported in firmware if the
2271 * protocol version is 3 or higher.
2273 if (icm
->proto_version
< 3)
2277 * NVM_AUTH is a special USB4 proxy operation that does not
2278 * return immediately so handle it separately.
2280 if (opcode
== USB4_SWITCH_OP_NVM_AUTH
)
2281 return icm_usb4_switch_nvm_authenticate(tb
, route
);
2283 memset(&request
, 0, sizeof(request
));
2284 request
.hdr
.code
= ICM_USB4_SWITCH_OP
;
2285 request
.route_hi
= upper_32_bits(route
);
2286 request
.route_lo
= lower_32_bits(route
);
2287 request
.opcode
= opcode
;
2289 request
.metadata
= *metadata
;
2292 request
.data_len_valid
|= ICM_USB4_SWITCH_DATA_VALID
;
2293 if (tx_data_len
< ARRAY_SIZE(request
.data
))
2294 request
.data_len_valid
=
2295 tx_data_len
& ICM_USB4_SWITCH_DATA_LEN_MASK
;
2296 memcpy(request
.data
, tx_data
, tx_data_len
* sizeof(u32
));
2299 memset(&reply
, 0, sizeof(reply
));
2300 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
2301 1, ICM_RETRIES
, ICM_TIMEOUT
);
2305 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
2309 *status
= reply
.status
;
2312 *metadata
= reply
.metadata
;
2315 memcpy(rx_data
, reply
.data
, rx_data_len
* sizeof(u32
));
2320 static int icm_usb4_switch_nvm_authenticate_status(struct tb_switch
*sw
,
2323 struct usb4_switch_nvm_auth
*auth
;
2324 struct tb
*tb
= sw
->tb
;
2325 struct icm
*icm
= tb_priv(tb
);
2328 if (icm
->proto_version
< 3)
2331 auth
= icm
->last_nvm_auth
;
2332 icm
->last_nvm_auth
= NULL
;
2334 if (auth
&& auth
->reply
.route_hi
== sw
->config
.route_hi
&&
2335 auth
->reply
.route_lo
== sw
->config
.route_lo
) {
2336 tb_dbg(tb
, "NVM_AUTH found for %llx flags %#x status %#x\n",
2337 tb_route(sw
), auth
->reply
.hdr
.flags
, auth
->reply
.status
);
2338 if (auth
->reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
2341 *status
= auth
->reply
.status
;
2351 static const struct tb_cm_ops icm_fr_ops
= {
2352 .driver_ready
= icm_driver_ready
,
2355 .suspend
= icm_suspend
,
2356 .complete
= icm_complete
,
2357 .handle_event
= icm_handle_event
,
2358 .approve_switch
= icm_fr_approve_switch
,
2359 .add_switch_key
= icm_fr_add_switch_key
,
2360 .challenge_switch_key
= icm_fr_challenge_switch_key
,
2361 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
2362 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
2363 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
2367 static const struct tb_cm_ops icm_ar_ops
= {
2368 .driver_ready
= icm_driver_ready
,
2371 .suspend
= icm_suspend
,
2372 .complete
= icm_complete
,
2373 .runtime_suspend
= icm_runtime_suspend
,
2374 .runtime_resume
= icm_runtime_resume
,
2375 .runtime_suspend_switch
= icm_runtime_suspend_switch
,
2376 .runtime_resume_switch
= icm_runtime_resume_switch
,
2377 .handle_event
= icm_handle_event
,
2378 .get_boot_acl
= icm_ar_get_boot_acl
,
2379 .set_boot_acl
= icm_ar_set_boot_acl
,
2380 .approve_switch
= icm_fr_approve_switch
,
2381 .add_switch_key
= icm_fr_add_switch_key
,
2382 .challenge_switch_key
= icm_fr_challenge_switch_key
,
2383 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
2384 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
2385 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
2389 static const struct tb_cm_ops icm_tr_ops
= {
2390 .driver_ready
= icm_driver_ready
,
2393 .suspend
= icm_suspend
,
2394 .complete
= icm_complete
,
2395 .runtime_suspend
= icm_runtime_suspend
,
2396 .runtime_resume
= icm_runtime_resume
,
2397 .runtime_suspend_switch
= icm_runtime_suspend_switch
,
2398 .runtime_resume_switch
= icm_runtime_resume_switch
,
2399 .handle_event
= icm_handle_event
,
2400 .get_boot_acl
= icm_ar_get_boot_acl
,
2401 .set_boot_acl
= icm_ar_set_boot_acl
,
2402 .approve_switch
= icm_tr_approve_switch
,
2403 .add_switch_key
= icm_tr_add_switch_key
,
2404 .challenge_switch_key
= icm_tr_challenge_switch_key
,
2405 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
2406 .approve_xdomain_paths
= icm_tr_approve_xdomain_paths
,
2407 .disconnect_xdomain_paths
= icm_tr_disconnect_xdomain_paths
,
2408 .usb4_switch_op
= icm_usb4_switch_op
,
2409 .usb4_switch_nvm_authenticate_status
=
2410 icm_usb4_switch_nvm_authenticate_status
,
2414 static const struct tb_cm_ops icm_icl_ops
= {
2415 .driver_ready
= icm_driver_ready
,
2418 .complete
= icm_complete
,
2419 .runtime_suspend
= icm_runtime_suspend
,
2420 .runtime_resume
= icm_runtime_resume
,
2421 .handle_event
= icm_handle_event
,
2422 .approve_xdomain_paths
= icm_tr_approve_xdomain_paths
,
2423 .disconnect_xdomain_paths
= icm_tr_disconnect_xdomain_paths
,
2424 .usb4_switch_op
= icm_usb4_switch_op
,
2425 .usb4_switch_nvm_authenticate_status
=
2426 icm_usb4_switch_nvm_authenticate_status
,
2429 struct tb
*icm_probe(struct tb_nhi
*nhi
)
2434 tb
= tb_domain_alloc(nhi
, ICM_TIMEOUT
, sizeof(struct icm
));
2439 INIT_DELAYED_WORK(&icm
->rescan_work
, icm_rescan_work
);
2440 mutex_init(&icm
->request_lock
);
2442 switch (nhi
->pdev
->device
) {
2443 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI
:
2444 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI
:
2445 icm
->can_upgrade_nvm
= true;
2446 icm
->is_supported
= icm_fr_is_supported
;
2447 icm
->get_route
= icm_fr_get_route
;
2448 icm
->save_devices
= icm_fr_save_devices
;
2449 icm
->driver_ready
= icm_fr_driver_ready
;
2450 icm
->device_connected
= icm_fr_device_connected
;
2451 icm
->device_disconnected
= icm_fr_device_disconnected
;
2452 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
2453 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
2454 tb
->cm_ops
= &icm_fr_ops
;
2457 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI
:
2458 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI
:
2459 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI
:
2460 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI
:
2461 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI
:
2462 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
2464 * NVM upgrade has not been tested on Apple systems and
2465 * they don't provide images publicly either. To be on
2466 * the safe side prevent root switch NVM upgrade on Macs
2469 icm
->can_upgrade_nvm
= !x86_apple_machine
;
2470 icm
->is_supported
= icm_ar_is_supported
;
2471 icm
->cio_reset
= icm_ar_cio_reset
;
2472 icm
->get_mode
= icm_ar_get_mode
;
2473 icm
->get_route
= icm_ar_get_route
;
2474 icm
->save_devices
= icm_fr_save_devices
;
2475 icm
->driver_ready
= icm_ar_driver_ready
;
2476 icm
->device_connected
= icm_fr_device_connected
;
2477 icm
->device_disconnected
= icm_fr_device_disconnected
;
2478 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
2479 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
2480 tb
->cm_ops
= &icm_ar_ops
;
2483 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI
:
2484 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI
:
2485 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
2486 icm
->can_upgrade_nvm
= !x86_apple_machine
;
2487 icm
->is_supported
= icm_ar_is_supported
;
2488 icm
->cio_reset
= icm_tr_cio_reset
;
2489 icm
->get_mode
= icm_ar_get_mode
;
2490 icm
->driver_ready
= icm_tr_driver_ready
;
2491 icm
->device_connected
= icm_tr_device_connected
;
2492 icm
->device_disconnected
= icm_tr_device_disconnected
;
2493 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
2494 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
2495 tb
->cm_ops
= &icm_tr_ops
;
2498 case PCI_DEVICE_ID_INTEL_ICL_NHI0
:
2499 case PCI_DEVICE_ID_INTEL_ICL_NHI1
:
2500 icm
->is_supported
= icm_fr_is_supported
;
2501 icm
->driver_ready
= icm_icl_driver_ready
;
2502 icm
->set_uuid
= icm_icl_set_uuid
;
2503 icm
->device_connected
= icm_icl_device_connected
;
2504 icm
->device_disconnected
= icm_tr_device_disconnected
;
2505 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
2506 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
2507 icm
->rtd3_veto
= icm_icl_rtd3_veto
;
2508 tb
->cm_ops
= &icm_icl_ops
;
2511 case PCI_DEVICE_ID_INTEL_TGL_NHI0
:
2512 case PCI_DEVICE_ID_INTEL_TGL_NHI1
:
2513 case PCI_DEVICE_ID_INTEL_TGL_H_NHI0
:
2514 case PCI_DEVICE_ID_INTEL_TGL_H_NHI1
:
2515 case PCI_DEVICE_ID_INTEL_ADL_NHI0
:
2516 case PCI_DEVICE_ID_INTEL_ADL_NHI1
:
2517 case PCI_DEVICE_ID_INTEL_RPL_NHI0
:
2518 case PCI_DEVICE_ID_INTEL_RPL_NHI1
:
2519 case PCI_DEVICE_ID_INTEL_MTL_M_NHI0
:
2520 case PCI_DEVICE_ID_INTEL_MTL_P_NHI0
:
2521 case PCI_DEVICE_ID_INTEL_MTL_P_NHI1
:
2522 icm
->is_supported
= icm_tgl_is_supported
;
2523 icm
->driver_ready
= icm_icl_driver_ready
;
2524 icm
->set_uuid
= icm_icl_set_uuid
;
2525 icm
->device_connected
= icm_icl_device_connected
;
2526 icm
->device_disconnected
= icm_tr_device_disconnected
;
2527 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
2528 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
2529 icm
->rtd3_veto
= icm_icl_rtd3_veto
;
2530 tb
->cm_ops
= &icm_icl_ops
;
2533 case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_2C_NHI
:
2534 case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_4C_NHI
:
2535 icm
->can_upgrade_nvm
= true;
2536 icm
->is_supported
= icm_tgl_is_supported
;
2537 icm
->get_mode
= icm_ar_get_mode
;
2538 icm
->driver_ready
= icm_tr_driver_ready
;
2539 icm
->device_connected
= icm_tr_device_connected
;
2540 icm
->device_disconnected
= icm_tr_device_disconnected
;
2541 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
2542 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
2543 tb
->cm_ops
= &icm_tr_ops
;
2547 if (!icm
->is_supported
|| !icm
->is_supported(tb
)) {
2548 dev_dbg(&nhi
->pdev
->dev
, "ICM not supported on this controller\n");
2553 tb_dbg(tb
, "using firmware connection manager\n");