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 */
44 #define ICM_APPROVE_TIMEOUT 10000 /* ms */
45 #define ICM_MAX_LINK 4
47 static bool start_icm
;
48 module_param(start_icm
, bool, 0444);
49 MODULE_PARM_DESC(start_icm
, "start ICM firmware if it is not running (default: false)");
52 * struct icm - Internal connection manager private data
53 * @request_lock: Makes sure only one message is send to ICM at time
54 * @rescan_work: Work used to rescan the surviving switches after resume
55 * @upstream_port: Pointer to the PCIe upstream port this host
56 * controller is connected. This is only set for systems
57 * where ICM needs to be started manually
58 * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
59 * (only set when @upstream_port is not %NULL)
60 * @safe_mode: ICM is in safe mode
61 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
62 * @rpm: Does the controller support runtime PM (RTD3)
63 * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
64 * @veto: Is RTD3 veto in effect
65 * @is_supported: Checks if we can support ICM on this controller
66 * @cio_reset: Trigger CIO reset
67 * @get_mode: Read and return the ICM firmware mode (optional)
68 * @get_route: Find a route string for given switch
69 * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
70 * @driver_ready: Send driver ready message to ICM
71 * @set_uuid: Set UUID for the root switch (optional)
72 * @device_connected: Handle device connected ICM message
73 * @device_disconnected: Handle device disconnected ICM message
74 * @xdomain_connected - Handle XDomain connected ICM message
75 * @xdomain_disconnected - Handle XDomain disconnected ICM message
76 * @rtd3_veto: Handle RTD3 veto notification ICM message
79 struct mutex request_lock
;
80 struct delayed_work rescan_work
;
81 struct pci_dev
*upstream_port
;
88 bool (*is_supported
)(struct tb
*tb
);
89 int (*cio_reset
)(struct tb
*tb
);
90 int (*get_mode
)(struct tb
*tb
);
91 int (*get_route
)(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
);
92 void (*save_devices
)(struct tb
*tb
);
93 int (*driver_ready
)(struct tb
*tb
,
94 enum tb_security_level
*security_level
,
95 size_t *nboot_acl
, bool *rpm
);
96 void (*set_uuid
)(struct tb
*tb
);
97 void (*device_connected
)(struct tb
*tb
,
98 const struct icm_pkg_header
*hdr
);
99 void (*device_disconnected
)(struct tb
*tb
,
100 const struct icm_pkg_header
*hdr
);
101 void (*xdomain_connected
)(struct tb
*tb
,
102 const struct icm_pkg_header
*hdr
);
103 void (*xdomain_disconnected
)(struct tb
*tb
,
104 const struct icm_pkg_header
*hdr
);
105 void (*rtd3_veto
)(struct tb
*tb
, const struct icm_pkg_header
*hdr
);
108 struct icm_notification
{
109 struct work_struct work
;
110 struct icm_pkg_header
*pkg
;
114 struct ep_name_entry
{
120 #define EP_NAME_INTEL_VSS 0x10
122 /* Intel Vendor specific structure */
132 #define INTEL_VSS_FLAGS_RTD3 BIT(0)
134 static const struct intel_vss
*parse_intel_vss(const void *ep_name
, size_t size
)
136 const void *end
= ep_name
+ size
;
138 while (ep_name
< end
) {
139 const struct ep_name_entry
*ep
= ep_name
;
143 if (ep_name
+ ep
->len
> end
)
146 if (ep
->type
== EP_NAME_INTEL_VSS
)
147 return (const struct intel_vss
*)ep
->data
;
155 static bool intel_vss_is_rtd3(const void *ep_name
, size_t size
)
157 const struct intel_vss
*vss
;
159 vss
= parse_intel_vss(ep_name
, size
);
161 return !!(vss
->flags
& INTEL_VSS_FLAGS_RTD3
);
166 static inline struct tb
*icm_to_tb(struct icm
*icm
)
168 return ((void *)icm
- sizeof(struct tb
));
171 static inline u8
phy_port_from_route(u64 route
, u8 depth
)
175 link
= depth
? route
>> ((depth
- 1) * 8) : route
;
176 return tb_phy_port_from_link(link
);
179 static inline u8
dual_link_from_link(u8 link
)
181 return link
? ((link
- 1) ^ 0x01) + 1 : 0;
184 static inline u64
get_route(u32 route_hi
, u32 route_lo
)
186 return (u64
)route_hi
<< 32 | route_lo
;
189 static inline u64
get_parent_route(u64 route
)
191 int depth
= tb_route_length(route
);
192 return depth
? route
& ~(0xffULL
<< (depth
- 1) * TB_ROUTE_SHIFT
) : 0;
195 static int pci2cio_wait_completion(struct icm
*icm
, unsigned long timeout_msec
)
197 unsigned long end
= jiffies
+ msecs_to_jiffies(timeout_msec
);
201 pci_read_config_dword(icm
->upstream_port
,
202 icm
->vnd_cap
+ PCIE2CIO_CMD
, &cmd
);
203 if (!(cmd
& PCIE2CIO_CMD_START
)) {
204 if (cmd
& PCIE2CIO_CMD_TIMEOUT
)
210 } while (time_before(jiffies
, end
));
215 static int pcie2cio_read(struct icm
*icm
, enum tb_cfg_space cs
,
216 unsigned int port
, unsigned int index
, u32
*data
)
218 struct pci_dev
*pdev
= icm
->upstream_port
;
219 int ret
, vnd_cap
= icm
->vnd_cap
;
223 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
224 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
225 cmd
|= PCIE2CIO_CMD_START
;
226 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
228 ret
= pci2cio_wait_completion(icm
, 5000);
232 pci_read_config_dword(pdev
, vnd_cap
+ PCIE2CIO_RDDATA
, data
);
236 static int pcie2cio_write(struct icm
*icm
, enum tb_cfg_space cs
,
237 unsigned int port
, unsigned int index
, u32 data
)
239 struct pci_dev
*pdev
= icm
->upstream_port
;
240 int vnd_cap
= icm
->vnd_cap
;
243 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_WRDATA
, data
);
246 cmd
|= (port
<< PCIE2CIO_CMD_PORT_SHIFT
) & PCIE2CIO_CMD_PORT_MASK
;
247 cmd
|= (cs
<< PCIE2CIO_CMD_CS_SHIFT
) & PCIE2CIO_CMD_CS_MASK
;
248 cmd
|= PCIE2CIO_CMD_WRITE
| PCIE2CIO_CMD_START
;
249 pci_write_config_dword(pdev
, vnd_cap
+ PCIE2CIO_CMD
, cmd
);
251 return pci2cio_wait_completion(icm
, 5000);
254 static bool icm_match(const struct tb_cfg_request
*req
,
255 const struct ctl_pkg
*pkg
)
257 const struct icm_pkg_header
*res_hdr
= pkg
->buffer
;
258 const struct icm_pkg_header
*req_hdr
= req
->request
;
260 if (pkg
->frame
.eof
!= req
->response_type
)
262 if (res_hdr
->code
!= req_hdr
->code
)
268 static bool icm_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
270 const struct icm_pkg_header
*hdr
= pkg
->buffer
;
272 if (hdr
->packet_id
< req
->npackets
) {
273 size_t offset
= hdr
->packet_id
* req
->response_size
;
275 memcpy(req
->response
+ offset
, pkg
->buffer
, req
->response_size
);
278 return hdr
->packet_id
== hdr
->total_packets
- 1;
281 static int icm_request(struct tb
*tb
, const void *request
, size_t request_size
,
282 void *response
, size_t response_size
, size_t npackets
,
283 unsigned int timeout_msec
)
285 struct icm
*icm
= tb_priv(tb
);
289 struct tb_cfg_request
*req
;
290 struct tb_cfg_result res
;
292 req
= tb_cfg_request_alloc();
296 req
->match
= icm_match
;
297 req
->copy
= icm_copy
;
298 req
->request
= request
;
299 req
->request_size
= request_size
;
300 req
->request_type
= TB_CFG_PKG_ICM_CMD
;
301 req
->response
= response
;
302 req
->npackets
= npackets
;
303 req
->response_size
= response_size
;
304 req
->response_type
= TB_CFG_PKG_ICM_RESP
;
306 mutex_lock(&icm
->request_lock
);
307 res
= tb_cfg_request_sync(tb
->ctl
, req
, timeout_msec
);
308 mutex_unlock(&icm
->request_lock
);
310 tb_cfg_request_put(req
);
312 if (res
.err
!= -ETIMEDOUT
)
313 return res
.err
== 1 ? -EIO
: res
.err
;
315 usleep_range(20, 50);
322 * If rescan is queued to run (we are resuming), postpone it to give the
323 * firmware some more time to send device connected notifications for next
324 * devices in the chain.
326 static void icm_postpone_rescan(struct tb
*tb
)
328 struct icm
*icm
= tb_priv(tb
);
330 if (delayed_work_pending(&icm
->rescan_work
))
331 mod_delayed_work(tb
->wq
, &icm
->rescan_work
,
332 msecs_to_jiffies(500));
335 static void icm_veto_begin(struct tb
*tb
)
337 struct icm
*icm
= tb_priv(tb
);
341 /* Keep the domain powered while veto is in effect */
342 pm_runtime_get(&tb
->dev
);
346 static void icm_veto_end(struct tb
*tb
)
348 struct icm
*icm
= tb_priv(tb
);
352 /* Allow the domain suspend now */
353 pm_runtime_mark_last_busy(&tb
->dev
);
354 pm_runtime_put_autosuspend(&tb
->dev
);
358 static bool icm_firmware_running(const struct tb_nhi
*nhi
)
362 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
363 return !!(val
& REG_FW_STS_ICM_EN
);
366 static bool icm_fr_is_supported(struct tb
*tb
)
368 return !x86_apple_machine
;
371 static inline int icm_fr_get_switch_index(u32 port
)
375 if ((port
& ICM_PORT_TYPE_MASK
) != TB_TYPE_PORT
)
378 index
= port
>> ICM_PORT_INDEX_SHIFT
;
379 return index
!= 0xff ? index
: 0;
382 static int icm_fr_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
384 struct icm_fr_pkg_get_topology_response
*switches
, *sw
;
385 struct icm_fr_pkg_get_topology request
= {
386 .hdr
= { .code
= ICM_GET_TOPOLOGY
},
388 size_t npackets
= ICM_GET_TOPOLOGY_PACKETS
;
392 switches
= kcalloc(npackets
, sizeof(*switches
), GFP_KERNEL
);
396 ret
= icm_request(tb
, &request
, sizeof(request
), switches
,
397 sizeof(*switches
), npackets
, ICM_TIMEOUT
);
402 index
= icm_fr_get_switch_index(sw
->ports
[link
]);
408 sw
= &switches
[index
];
409 for (i
= 1; i
< depth
; i
++) {
412 if (!(sw
->first_data
& ICM_SWITCH_USED
)) {
417 for (j
= 0; j
< ARRAY_SIZE(sw
->ports
); j
++) {
418 index
= icm_fr_get_switch_index(sw
->ports
[j
]);
419 if (index
> sw
->switch_index
) {
420 sw
= &switches
[index
];
426 *route
= get_route(sw
->route_hi
, sw
->route_lo
);
433 static void icm_fr_save_devices(struct tb
*tb
)
435 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_SAVE_DEVS
, 0);
439 icm_fr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
440 size_t *nboot_acl
, bool *rpm
)
442 struct icm_fr_pkg_driver_ready_response reply
;
443 struct icm_pkg_driver_ready request
= {
444 .hdr
.code
= ICM_DRIVER_READY
,
448 memset(&reply
, 0, sizeof(reply
));
449 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
455 *security_level
= reply
.security_level
& ICM_FR_SLEVEL_MASK
;
460 static int icm_fr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
462 struct icm_fr_pkg_approve_device request
;
463 struct icm_fr_pkg_approve_device reply
;
466 memset(&request
, 0, sizeof(request
));
467 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
468 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
469 request
.connection_id
= sw
->connection_id
;
470 request
.connection_key
= sw
->connection_key
;
472 memset(&reply
, 0, sizeof(reply
));
473 /* Use larger timeout as establishing tunnels can take some time */
474 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
475 1, ICM_APPROVE_TIMEOUT
);
479 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
480 tb_warn(tb
, "PCIe tunnel creation failed\n");
487 static int icm_fr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
489 struct icm_fr_pkg_add_device_key request
;
490 struct icm_fr_pkg_add_device_key_response reply
;
493 memset(&request
, 0, sizeof(request
));
494 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
495 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
496 request
.connection_id
= sw
->connection_id
;
497 request
.connection_key
= sw
->connection_key
;
498 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
500 memset(&reply
, 0, sizeof(reply
));
501 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
506 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
507 tb_warn(tb
, "Adding key to switch failed\n");
514 static int icm_fr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
515 const u8
*challenge
, u8
*response
)
517 struct icm_fr_pkg_challenge_device request
;
518 struct icm_fr_pkg_challenge_device_response reply
;
521 memset(&request
, 0, sizeof(request
));
522 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
523 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
524 request
.connection_id
= sw
->connection_id
;
525 request
.connection_key
= sw
->connection_key
;
526 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
528 memset(&reply
, 0, sizeof(reply
));
529 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
534 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
535 return -EKEYREJECTED
;
536 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
539 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
544 static int icm_fr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
546 struct icm_fr_pkg_approve_xdomain_response reply
;
547 struct icm_fr_pkg_approve_xdomain request
;
550 memset(&request
, 0, sizeof(request
));
551 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
552 request
.link_info
= xd
->depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| xd
->link
;
553 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
555 request
.transmit_path
= xd
->transmit_path
;
556 request
.transmit_ring
= xd
->transmit_ring
;
557 request
.receive_path
= xd
->receive_path
;
558 request
.receive_ring
= xd
->receive_ring
;
560 memset(&reply
, 0, sizeof(reply
));
561 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
566 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
572 static int icm_fr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
577 phy_port
= tb_phy_port_from_link(xd
->link
);
579 cmd
= NHI_MAILBOX_DISCONNECT_PA
;
581 cmd
= NHI_MAILBOX_DISCONNECT_PB
;
583 nhi_mailbox_cmd(tb
->nhi
, cmd
, 1);
584 usleep_range(10, 50);
585 nhi_mailbox_cmd(tb
->nhi
, cmd
, 2);
589 static struct tb_switch
*alloc_switch(struct tb_switch
*parent_sw
, u64 route
,
592 struct tb
*tb
= parent_sw
->tb
;
593 struct tb_switch
*sw
;
595 sw
= tb_switch_alloc(tb
, &parent_sw
->dev
, route
);
597 tb_warn(tb
, "failed to allocate switch at %llx\n", route
);
601 sw
->uuid
= kmemdup(uuid
, sizeof(*uuid
), GFP_KERNEL
);
604 return ERR_PTR(-ENOMEM
);
607 init_completion(&sw
->rpm_complete
);
611 static int add_switch(struct tb_switch
*parent_sw
, struct tb_switch
*sw
)
613 u64 route
= tb_route(sw
);
616 /* Link the two switches now */
617 tb_port_at(route
, parent_sw
)->remote
= tb_upstream_port(sw
);
618 tb_upstream_port(sw
)->remote
= tb_port_at(route
, parent_sw
);
620 ret
= tb_switch_add(sw
);
622 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
627 static void update_switch(struct tb_switch
*parent_sw
, struct tb_switch
*sw
,
628 u64 route
, u8 connection_id
, u8 connection_key
,
629 u8 link
, u8 depth
, bool boot
)
631 /* Disconnect from parent */
632 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
633 /* Re-connect via updated port*/
634 tb_port_at(route
, parent_sw
)->remote
= tb_upstream_port(sw
);
636 /* Update with the new addressing information */
637 sw
->config
.route_hi
= upper_32_bits(route
);
638 sw
->config
.route_lo
= lower_32_bits(route
);
639 sw
->connection_id
= connection_id
;
640 sw
->connection_key
= connection_key
;
645 /* This switch still exists */
646 sw
->is_unplugged
= false;
648 /* Runtime resume is now complete */
649 complete(&sw
->rpm_complete
);
652 static void remove_switch(struct tb_switch
*sw
)
654 struct tb_switch
*parent_sw
;
656 parent_sw
= tb_to_switch(sw
->dev
.parent
);
657 tb_port_at(tb_route(sw
), parent_sw
)->remote
= NULL
;
658 tb_switch_remove(sw
);
661 static void add_xdomain(struct tb_switch
*sw
, u64 route
,
662 const uuid_t
*local_uuid
, const uuid_t
*remote_uuid
,
665 struct tb_xdomain
*xd
;
667 pm_runtime_get_sync(&sw
->dev
);
669 xd
= tb_xdomain_alloc(sw
->tb
, &sw
->dev
, route
, local_uuid
, remote_uuid
);
676 tb_port_at(route
, sw
)->xdomain
= xd
;
681 pm_runtime_mark_last_busy(&sw
->dev
);
682 pm_runtime_put_autosuspend(&sw
->dev
);
685 static void update_xdomain(struct tb_xdomain
*xd
, u64 route
, u8 link
)
689 xd
->is_unplugged
= false;
692 static void remove_xdomain(struct tb_xdomain
*xd
)
694 struct tb_switch
*sw
;
696 sw
= tb_to_switch(xd
->dev
.parent
);
697 tb_port_at(xd
->route
, sw
)->xdomain
= NULL
;
698 tb_xdomain_remove(xd
);
702 icm_fr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
704 const struct icm_fr_event_device_connected
*pkg
=
705 (const struct icm_fr_event_device_connected
*)hdr
;
706 enum tb_security_level security_level
;
707 struct tb_switch
*sw
, *parent_sw
;
708 bool boot
, dual_lane
, speed_gen3
;
709 struct icm
*icm
= tb_priv(tb
);
710 bool authorized
= false;
711 struct tb_xdomain
*xd
;
716 icm_postpone_rescan(tb
);
718 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
719 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
720 ICM_LINK_INFO_DEPTH_SHIFT
;
721 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
722 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
723 ICM_FLAGS_SLEVEL_SHIFT
;
724 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
725 dual_lane
= pkg
->hdr
.flags
& ICM_FLAGS_DUAL_LANE
;
726 speed_gen3
= pkg
->hdr
.flags
& ICM_FLAGS_SPEED_GEN3
;
728 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
729 tb_info(tb
, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
734 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
736 u8 phy_port
, sw_phy_port
;
738 parent_sw
= tb_to_switch(sw
->dev
.parent
);
739 sw_phy_port
= tb_phy_port_from_link(sw
->link
);
740 phy_port
= tb_phy_port_from_link(link
);
743 * On resume ICM will send us connected events for the
744 * devices that still are present. However, that
745 * information might have changed for example by the
746 * fact that a switch on a dual-link connection might
747 * have been enumerated using the other link now. Make
748 * sure our book keeping matches that.
750 if (sw
->depth
== depth
&& sw_phy_port
== phy_port
&&
751 !!sw
->authorized
== authorized
) {
753 * It was enumerated through another link so update
754 * route string accordingly.
756 if (sw
->link
!= link
) {
757 ret
= icm
->get_route(tb
, link
, depth
, &route
);
759 tb_err(tb
, "failed to update route string for switch at %u.%u\n",
765 route
= tb_route(sw
);
768 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
769 pkg
->connection_key
, link
, depth
, boot
);
775 * User connected the same switch to another physical
776 * port or to another part of the topology. Remove the
777 * existing switch now before adding the new one.
784 * If the switch was not found by UUID, look for a switch on
785 * same physical port (taking possible link aggregation into
786 * account) and depth. If we found one it is definitely a stale
787 * one so remove it first.
789 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
793 dual_link
= dual_link_from_link(link
);
795 sw
= tb_switch_find_by_link_depth(tb
, dual_link
, depth
);
802 /* Remove existing XDomain connection if found */
803 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
809 parent_sw
= tb_switch_find_by_link_depth(tb
, link
, depth
- 1);
811 tb_err(tb
, "failed to find parent switch for %u.%u\n",
816 ret
= icm
->get_route(tb
, link
, depth
, &route
);
818 tb_err(tb
, "failed to find route string for switch at %u.%u\n",
820 tb_switch_put(parent_sw
);
824 pm_runtime_get_sync(&parent_sw
->dev
);
826 sw
= alloc_switch(parent_sw
, route
, &pkg
->ep_uuid
);
828 sw
->connection_id
= pkg
->connection_id
;
829 sw
->connection_key
= pkg
->connection_key
;
832 sw
->authorized
= authorized
;
833 sw
->security_level
= security_level
;
835 sw
->link_speed
= speed_gen3
? 20 : 10;
836 sw
->link_width
= dual_lane
? 2 : 1;
837 sw
->rpm
= intel_vss_is_rtd3(pkg
->ep_name
, sizeof(pkg
->ep_name
));
839 if (add_switch(parent_sw
, sw
))
843 pm_runtime_mark_last_busy(&parent_sw
->dev
);
844 pm_runtime_put_autosuspend(&parent_sw
->dev
);
846 tb_switch_put(parent_sw
);
850 icm_fr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
852 const struct icm_fr_event_device_disconnected
*pkg
=
853 (const struct icm_fr_event_device_disconnected
*)hdr
;
854 struct tb_switch
*sw
;
857 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
858 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
859 ICM_LINK_INFO_DEPTH_SHIFT
;
861 if (link
> ICM_MAX_LINK
|| depth
> TB_SWITCH_MAX_DEPTH
) {
862 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
866 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
868 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
878 icm_fr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
880 const struct icm_fr_event_xdomain_connected
*pkg
=
881 (const struct icm_fr_event_xdomain_connected
*)hdr
;
882 struct tb_xdomain
*xd
;
883 struct tb_switch
*sw
;
887 link
= pkg
->link_info
& ICM_LINK_INFO_LINK_MASK
;
888 depth
= (pkg
->link_info
& ICM_LINK_INFO_DEPTH_MASK
) >>
889 ICM_LINK_INFO_DEPTH_SHIFT
;
891 if (link
> ICM_MAX_LINK
|| depth
> TB_SWITCH_MAX_DEPTH
) {
892 tb_warn(tb
, "invalid topology %u.%u, ignoring\n", link
, depth
);
896 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
898 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
900 u8 xd_phy_port
, phy_port
;
902 xd_phy_port
= phy_port_from_route(xd
->route
, xd
->depth
);
903 phy_port
= phy_port_from_route(route
, depth
);
905 if (xd
->depth
== depth
&& xd_phy_port
== phy_port
) {
906 update_xdomain(xd
, route
, link
);
912 * If we find an existing XDomain connection remove it
913 * now. We need to go through login handshake and
914 * everything anyway to be able to re-establish the
922 * Look if there already exists an XDomain in the same place
923 * than the new one and in that case remove it because it is
924 * most likely another host that got disconnected.
926 xd
= tb_xdomain_find_by_link_depth(tb
, link
, depth
);
930 dual_link
= dual_link_from_link(link
);
932 xd
= tb_xdomain_find_by_link_depth(tb
, dual_link
,
941 * If the user disconnected a switch during suspend and
942 * connected another host to the same port, remove the switch
945 sw
= tb_switch_find_by_route(tb
, route
);
951 sw
= tb_switch_find_by_link_depth(tb
, link
, depth
);
953 tb_warn(tb
, "no switch exists at %u.%u, ignoring\n", link
,
958 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, link
,
964 icm_fr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
966 const struct icm_fr_event_xdomain_disconnected
*pkg
=
967 (const struct icm_fr_event_xdomain_disconnected
*)hdr
;
968 struct tb_xdomain
*xd
;
971 * If the connection is through one or multiple devices, the
972 * XDomain device is removed along with them so it is fine if we
973 * cannot find it here.
975 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
982 static int icm_tr_cio_reset(struct tb
*tb
)
984 return pcie2cio_write(tb_priv(tb
), TB_CFG_SWITCH
, 0, 0x777, BIT(1));
988 icm_tr_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
989 size_t *nboot_acl
, bool *rpm
)
991 struct icm_tr_pkg_driver_ready_response reply
;
992 struct icm_pkg_driver_ready request
= {
993 .hdr
.code
= ICM_DRIVER_READY
,
997 memset(&reply
, 0, sizeof(reply
));
998 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1004 *security_level
= reply
.info
& ICM_TR_INFO_SLEVEL_MASK
;
1006 *nboot_acl
= (reply
.info
& ICM_TR_INFO_BOOT_ACL_MASK
) >>
1007 ICM_TR_INFO_BOOT_ACL_SHIFT
;
1009 *rpm
= !!(reply
.hdr
.flags
& ICM_TR_FLAGS_RTD3
);
1014 static int icm_tr_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
1016 struct icm_tr_pkg_approve_device request
;
1017 struct icm_tr_pkg_approve_device reply
;
1020 memset(&request
, 0, sizeof(request
));
1021 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
1022 request
.hdr
.code
= ICM_APPROVE_DEVICE
;
1023 request
.route_lo
= sw
->config
.route_lo
;
1024 request
.route_hi
= sw
->config
.route_hi
;
1025 request
.connection_id
= sw
->connection_id
;
1027 memset(&reply
, 0, sizeof(reply
));
1028 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1029 1, ICM_APPROVE_TIMEOUT
);
1033 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
1034 tb_warn(tb
, "PCIe tunnel creation failed\n");
1041 static int icm_tr_add_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
1043 struct icm_tr_pkg_add_device_key_response reply
;
1044 struct icm_tr_pkg_add_device_key request
;
1047 memset(&request
, 0, sizeof(request
));
1048 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
1049 request
.hdr
.code
= ICM_ADD_DEVICE_KEY
;
1050 request
.route_lo
= sw
->config
.route_lo
;
1051 request
.route_hi
= sw
->config
.route_hi
;
1052 request
.connection_id
= sw
->connection_id
;
1053 memcpy(request
.key
, sw
->key
, TB_SWITCH_KEY_SIZE
);
1055 memset(&reply
, 0, sizeof(reply
));
1056 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1061 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
) {
1062 tb_warn(tb
, "Adding key to switch failed\n");
1069 static int icm_tr_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
,
1070 const u8
*challenge
, u8
*response
)
1072 struct icm_tr_pkg_challenge_device_response reply
;
1073 struct icm_tr_pkg_challenge_device request
;
1076 memset(&request
, 0, sizeof(request
));
1077 memcpy(&request
.ep_uuid
, sw
->uuid
, sizeof(request
.ep_uuid
));
1078 request
.hdr
.code
= ICM_CHALLENGE_DEVICE
;
1079 request
.route_lo
= sw
->config
.route_lo
;
1080 request
.route_hi
= sw
->config
.route_hi
;
1081 request
.connection_id
= sw
->connection_id
;
1082 memcpy(request
.challenge
, challenge
, TB_SWITCH_KEY_SIZE
);
1084 memset(&reply
, 0, sizeof(reply
));
1085 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1090 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1091 return -EKEYREJECTED
;
1092 if (reply
.hdr
.flags
& ICM_FLAGS_NO_KEY
)
1095 memcpy(response
, reply
.response
, TB_SWITCH_KEY_SIZE
);
1100 static int icm_tr_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
1102 struct icm_tr_pkg_approve_xdomain_response reply
;
1103 struct icm_tr_pkg_approve_xdomain request
;
1106 memset(&request
, 0, sizeof(request
));
1107 request
.hdr
.code
= ICM_APPROVE_XDOMAIN
;
1108 request
.route_hi
= upper_32_bits(xd
->route
);
1109 request
.route_lo
= lower_32_bits(xd
->route
);
1110 request
.transmit_path
= xd
->transmit_path
;
1111 request
.transmit_ring
= xd
->transmit_ring
;
1112 request
.receive_path
= xd
->receive_path
;
1113 request
.receive_ring
= xd
->receive_ring
;
1114 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
1116 memset(&reply
, 0, sizeof(reply
));
1117 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1122 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1128 static int icm_tr_xdomain_tear_down(struct tb
*tb
, struct tb_xdomain
*xd
,
1131 struct icm_tr_pkg_disconnect_xdomain_response reply
;
1132 struct icm_tr_pkg_disconnect_xdomain request
;
1135 memset(&request
, 0, sizeof(request
));
1136 request
.hdr
.code
= ICM_DISCONNECT_XDOMAIN
;
1137 request
.stage
= stage
;
1138 request
.route_hi
= upper_32_bits(xd
->route
);
1139 request
.route_lo
= lower_32_bits(xd
->route
);
1140 memcpy(&request
.remote_uuid
, xd
->remote_uuid
, sizeof(*xd
->remote_uuid
));
1142 memset(&reply
, 0, sizeof(reply
));
1143 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1148 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1154 static int icm_tr_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
1158 ret
= icm_tr_xdomain_tear_down(tb
, xd
, 1);
1162 usleep_range(10, 50);
1163 return icm_tr_xdomain_tear_down(tb
, xd
, 2);
1167 __icm_tr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
,
1170 const struct icm_tr_event_device_connected
*pkg
=
1171 (const struct icm_tr_event_device_connected
*)hdr
;
1172 bool authorized
, boot
, dual_lane
, speed_gen3
;
1173 enum tb_security_level security_level
;
1174 struct tb_switch
*sw
, *parent_sw
;
1175 struct tb_xdomain
*xd
;
1178 icm_postpone_rescan(tb
);
1181 * Currently we don't use the QoS information coming with the
1182 * device connected message so simply just ignore that extra
1185 if (pkg
->hdr
.packet_id
)
1188 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1189 authorized
= pkg
->link_info
& ICM_LINK_INFO_APPROVED
;
1190 security_level
= (pkg
->hdr
.flags
& ICM_FLAGS_SLEVEL_MASK
) >>
1191 ICM_FLAGS_SLEVEL_SHIFT
;
1192 boot
= pkg
->link_info
& ICM_LINK_INFO_BOOT
;
1193 dual_lane
= pkg
->hdr
.flags
& ICM_FLAGS_DUAL_LANE
;
1194 speed_gen3
= pkg
->hdr
.flags
& ICM_FLAGS_SPEED_GEN3
;
1196 if (pkg
->link_info
& ICM_LINK_INFO_REJECTED
) {
1197 tb_info(tb
, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1202 sw
= tb_switch_find_by_uuid(tb
, &pkg
->ep_uuid
);
1204 /* Update the switch if it is still in the same place */
1205 if (tb_route(sw
) == route
&& !!sw
->authorized
== authorized
) {
1206 parent_sw
= tb_to_switch(sw
->dev
.parent
);
1207 update_switch(parent_sw
, sw
, route
, pkg
->connection_id
,
1217 /* Another switch with the same address */
1218 sw
= tb_switch_find_by_route(tb
, route
);
1224 /* XDomain connection with the same address */
1225 xd
= tb_xdomain_find_by_route(tb
, route
);
1231 parent_sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1233 tb_err(tb
, "failed to find parent switch for %llx\n", route
);
1237 pm_runtime_get_sync(&parent_sw
->dev
);
1239 sw
= alloc_switch(parent_sw
, route
, &pkg
->ep_uuid
);
1241 sw
->connection_id
= pkg
->connection_id
;
1242 sw
->authorized
= authorized
;
1243 sw
->security_level
= security_level
;
1245 sw
->link_speed
= speed_gen3
? 20 : 10;
1246 sw
->link_width
= dual_lane
? 2 : 1;
1247 sw
->rpm
= force_rtd3
;
1249 sw
->rpm
= intel_vss_is_rtd3(pkg
->ep_name
,
1250 sizeof(pkg
->ep_name
));
1252 if (add_switch(parent_sw
, sw
))
1256 pm_runtime_mark_last_busy(&parent_sw
->dev
);
1257 pm_runtime_put_autosuspend(&parent_sw
->dev
);
1259 tb_switch_put(parent_sw
);
1263 icm_tr_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1265 __icm_tr_device_connected(tb
, hdr
, false);
1269 icm_tr_device_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1271 const struct icm_tr_event_device_disconnected
*pkg
=
1272 (const struct icm_tr_event_device_disconnected
*)hdr
;
1273 struct tb_switch
*sw
;
1276 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1278 sw
= tb_switch_find_by_route(tb
, route
);
1280 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1289 icm_tr_xdomain_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1291 const struct icm_tr_event_xdomain_connected
*pkg
=
1292 (const struct icm_tr_event_xdomain_connected
*)hdr
;
1293 struct tb_xdomain
*xd
;
1294 struct tb_switch
*sw
;
1297 if (!tb
->root_switch
)
1300 route
= get_route(pkg
->local_route_hi
, pkg
->local_route_lo
);
1302 xd
= tb_xdomain_find_by_uuid(tb
, &pkg
->remote_uuid
);
1304 if (xd
->route
== route
) {
1305 update_xdomain(xd
, route
, 0);
1314 /* An existing xdomain with the same address */
1315 xd
= tb_xdomain_find_by_route(tb
, route
);
1322 * If the user disconnected a switch during suspend and
1323 * connected another host to the same port, remove the switch
1326 sw
= tb_switch_find_by_route(tb
, route
);
1332 sw
= tb_switch_find_by_route(tb
, get_parent_route(route
));
1334 tb_warn(tb
, "no switch exists at %llx, ignoring\n", route
);
1338 add_xdomain(sw
, route
, &pkg
->local_uuid
, &pkg
->remote_uuid
, 0, 0);
1343 icm_tr_xdomain_disconnected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1345 const struct icm_tr_event_xdomain_disconnected
*pkg
=
1346 (const struct icm_tr_event_xdomain_disconnected
*)hdr
;
1347 struct tb_xdomain
*xd
;
1350 route
= get_route(pkg
->route_hi
, pkg
->route_lo
);
1352 xd
= tb_xdomain_find_by_route(tb
, route
);
1359 static struct pci_dev
*get_upstream_port(struct pci_dev
*pdev
)
1361 struct pci_dev
*parent
;
1363 parent
= pci_upstream_bridge(pdev
);
1365 if (!pci_is_pcie(parent
))
1367 if (pci_pcie_type(parent
) == PCI_EXP_TYPE_UPSTREAM
)
1369 parent
= pci_upstream_bridge(parent
);
1375 switch (parent
->device
) {
1376 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1377 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1378 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1379 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1380 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1381 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
1382 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
1389 static bool icm_ar_is_supported(struct tb
*tb
)
1391 struct pci_dev
*upstream_port
;
1392 struct icm
*icm
= tb_priv(tb
);
1395 * Starting from Alpine Ridge we can use ICM on Apple machines
1396 * as well. We just need to reset and re-enable it first.
1397 * However, only start it if explicitly asked by the user.
1399 if (icm_firmware_running(tb
->nhi
))
1405 * Find the upstream PCIe port in case we need to do reset
1406 * through its vendor specific registers.
1408 upstream_port
= get_upstream_port(tb
->nhi
->pdev
);
1409 if (upstream_port
) {
1412 cap
= pci_find_ext_capability(upstream_port
,
1413 PCI_EXT_CAP_ID_VNDR
);
1415 icm
->upstream_port
= upstream_port
;
1425 static int icm_ar_cio_reset(struct tb
*tb
)
1427 return pcie2cio_write(tb_priv(tb
), TB_CFG_SWITCH
, 0, 0x50, BIT(9));
1430 static int icm_ar_get_mode(struct tb
*tb
)
1432 struct tb_nhi
*nhi
= tb
->nhi
;
1437 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1438 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1441 } while (--retries
);
1444 dev_err(&nhi
->pdev
->dev
, "ICM firmware not authenticated\n");
1448 return nhi_mailbox_mode(nhi
);
1452 icm_ar_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1453 size_t *nboot_acl
, bool *rpm
)
1455 struct icm_ar_pkg_driver_ready_response reply
;
1456 struct icm_pkg_driver_ready request
= {
1457 .hdr
.code
= ICM_DRIVER_READY
,
1461 memset(&reply
, 0, sizeof(reply
));
1462 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1468 *security_level
= reply
.info
& ICM_AR_INFO_SLEVEL_MASK
;
1469 if (nboot_acl
&& (reply
.info
& ICM_AR_INFO_BOOT_ACL_SUPPORTED
))
1470 *nboot_acl
= (reply
.info
& ICM_AR_INFO_BOOT_ACL_MASK
) >>
1471 ICM_AR_INFO_BOOT_ACL_SHIFT
;
1473 *rpm
= !!(reply
.hdr
.flags
& ICM_AR_FLAGS_RTD3
);
1478 static int icm_ar_get_route(struct tb
*tb
, u8 link
, u8 depth
, u64
*route
)
1480 struct icm_ar_pkg_get_route_response reply
;
1481 struct icm_ar_pkg_get_route request
= {
1482 .hdr
= { .code
= ICM_GET_ROUTE
},
1483 .link_info
= depth
<< ICM_LINK_INFO_DEPTH_SHIFT
| link
,
1487 memset(&reply
, 0, sizeof(reply
));
1488 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1493 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1496 *route
= get_route(reply
.route_hi
, reply
.route_lo
);
1500 static int icm_ar_get_boot_acl(struct tb
*tb
, uuid_t
*uuids
, size_t nuuids
)
1502 struct icm_ar_pkg_preboot_acl_response reply
;
1503 struct icm_ar_pkg_preboot_acl request
= {
1504 .hdr
= { .code
= ICM_PREBOOT_ACL
},
1508 memset(&reply
, 0, sizeof(reply
));
1509 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1514 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1517 for (i
= 0; i
< nuuids
; i
++) {
1518 u32
*uuid
= (u32
*)&uuids
[i
];
1520 uuid
[0] = reply
.acl
[i
].uuid_lo
;
1521 uuid
[1] = reply
.acl
[i
].uuid_hi
;
1523 if (uuid
[0] == 0xffffffff && uuid
[1] == 0xffffffff) {
1524 /* Map empty entries to null UUID */
1527 } else if (uuid
[0] != 0 || uuid
[1] != 0) {
1528 /* Upper two DWs are always one's */
1529 uuid
[2] = 0xffffffff;
1530 uuid
[3] = 0xffffffff;
1537 static int icm_ar_set_boot_acl(struct tb
*tb
, const uuid_t
*uuids
,
1540 struct icm_ar_pkg_preboot_acl_response reply
;
1541 struct icm_ar_pkg_preboot_acl request
= {
1543 .code
= ICM_PREBOOT_ACL
,
1544 .flags
= ICM_FLAGS_WRITE
,
1549 for (i
= 0; i
< nuuids
; i
++) {
1550 const u32
*uuid
= (const u32
*)&uuids
[i
];
1552 if (uuid_is_null(&uuids
[i
])) {
1554 * Map null UUID to the empty (all one) entries
1557 request
.acl
[i
].uuid_lo
= 0xffffffff;
1558 request
.acl
[i
].uuid_hi
= 0xffffffff;
1560 /* Two high DWs need to be set to all one */
1561 if (uuid
[2] != 0xffffffff || uuid
[3] != 0xffffffff)
1564 request
.acl
[i
].uuid_lo
= uuid
[0];
1565 request
.acl
[i
].uuid_hi
= uuid
[1];
1569 memset(&reply
, 0, sizeof(reply
));
1570 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1575 if (reply
.hdr
.flags
& ICM_FLAGS_ERROR
)
1582 icm_icl_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1583 size_t *nboot_acl
, bool *rpm
)
1585 struct icm_tr_pkg_driver_ready_response reply
;
1586 struct icm_pkg_driver_ready request
= {
1587 .hdr
.code
= ICM_DRIVER_READY
,
1591 memset(&reply
, 0, sizeof(reply
));
1592 ret
= icm_request(tb
, &request
, sizeof(request
), &reply
, sizeof(reply
),
1597 /* Ice Lake always supports RTD3 */
1604 static void icm_icl_set_uuid(struct tb
*tb
)
1606 struct tb_nhi
*nhi
= tb
->nhi
;
1609 pci_read_config_dword(nhi
->pdev
, VS_CAP_10
, &uuid
[0]);
1610 pci_read_config_dword(nhi
->pdev
, VS_CAP_11
, &uuid
[1]);
1611 uuid
[2] = 0xffffffff;
1612 uuid
[3] = 0xffffffff;
1614 tb
->root_switch
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1618 icm_icl_device_connected(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1620 __icm_tr_device_connected(tb
, hdr
, true);
1623 static void icm_icl_rtd3_veto(struct tb
*tb
, const struct icm_pkg_header
*hdr
)
1625 const struct icm_icl_event_rtd3_veto
*pkg
=
1626 (const struct icm_icl_event_rtd3_veto
*)hdr
;
1628 tb_dbg(tb
, "ICM rtd3 veto=0x%08x\n", pkg
->veto_reason
);
1630 if (pkg
->veto_reason
)
1636 static void icm_handle_notification(struct work_struct
*work
)
1638 struct icm_notification
*n
= container_of(work
, typeof(*n
), work
);
1639 struct tb
*tb
= n
->tb
;
1640 struct icm
*icm
= tb_priv(tb
);
1642 mutex_lock(&tb
->lock
);
1645 * When the domain is stopped we flush its workqueue but before
1646 * that the root switch is removed. In that case we should treat
1647 * the queued events as being canceled.
1649 if (tb
->root_switch
) {
1650 switch (n
->pkg
->code
) {
1651 case ICM_EVENT_DEVICE_CONNECTED
:
1652 icm
->device_connected(tb
, n
->pkg
);
1654 case ICM_EVENT_DEVICE_DISCONNECTED
:
1655 icm
->device_disconnected(tb
, n
->pkg
);
1657 case ICM_EVENT_XDOMAIN_CONNECTED
:
1658 icm
->xdomain_connected(tb
, n
->pkg
);
1660 case ICM_EVENT_XDOMAIN_DISCONNECTED
:
1661 icm
->xdomain_disconnected(tb
, n
->pkg
);
1663 case ICM_EVENT_RTD3_VETO
:
1664 icm
->rtd3_veto(tb
, n
->pkg
);
1669 mutex_unlock(&tb
->lock
);
1675 static void icm_handle_event(struct tb
*tb
, enum tb_cfg_pkg_type type
,
1676 const void *buf
, size_t size
)
1678 struct icm_notification
*n
;
1680 n
= kmalloc(sizeof(*n
), GFP_KERNEL
);
1684 INIT_WORK(&n
->work
, icm_handle_notification
);
1685 n
->pkg
= kmemdup(buf
, size
, GFP_KERNEL
);
1688 queue_work(tb
->wq
, &n
->work
);
1692 __icm_driver_ready(struct tb
*tb
, enum tb_security_level
*security_level
,
1693 size_t *nboot_acl
, bool *rpm
)
1695 struct icm
*icm
= tb_priv(tb
);
1696 unsigned int retries
= 50;
1699 ret
= icm
->driver_ready(tb
, security_level
, nboot_acl
, rpm
);
1701 tb_err(tb
, "failed to send driver ready to ICM\n");
1706 * Hold on here until the switch config space is accessible so
1707 * that we can read root switch config successfully.
1710 struct tb_cfg_result res
;
1713 res
= tb_cfg_read_raw(tb
->ctl
, &tmp
, 0, 0, TB_CFG_SWITCH
,
1719 } while (--retries
);
1721 tb_err(tb
, "failed to read root switch config space, giving up\n");
1725 static int icm_firmware_reset(struct tb
*tb
, struct tb_nhi
*nhi
)
1727 struct icm
*icm
= tb_priv(tb
);
1730 if (!icm
->upstream_port
)
1733 /* Put ARC to wait for CIO reset event to happen */
1734 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1735 val
|= REG_FW_STS_CIO_RESET_REQ
;
1736 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1739 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1740 val
|= REG_FW_STS_ICM_EN_INVERT
;
1741 val
|= REG_FW_STS_ICM_EN_CPU
;
1742 iowrite32(val
, nhi
->iobase
+ REG_FW_STS
);
1744 /* Trigger CIO reset now */
1745 return icm
->cio_reset(tb
);
1748 static int icm_firmware_start(struct tb
*tb
, struct tb_nhi
*nhi
)
1750 unsigned int retries
= 10;
1754 /* Check if the ICM firmware is already running */
1755 if (icm_firmware_running(nhi
))
1758 dev_dbg(&nhi
->pdev
->dev
, "starting ICM firmware\n");
1760 ret
= icm_firmware_reset(tb
, nhi
);
1764 /* Wait until the ICM firmware tells us it is up and running */
1766 /* Check that the ICM firmware is running */
1767 val
= ioread32(nhi
->iobase
+ REG_FW_STS
);
1768 if (val
& REG_FW_STS_NVM_AUTH_DONE
)
1772 } while (--retries
);
1777 static int icm_reset_phy_port(struct tb
*tb
, int phy_port
)
1779 struct icm
*icm
= tb_priv(tb
);
1785 if (!icm
->upstream_port
)
1797 * Read link status of both null ports belonging to a single
1800 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1803 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1807 state0
= val0
& PHY_PORT_CS1_LINK_STATE_MASK
;
1808 state0
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1809 state1
= val1
& PHY_PORT_CS1_LINK_STATE_MASK
;
1810 state1
>>= PHY_PORT_CS1_LINK_STATE_SHIFT
;
1812 /* If they are both up we need to reset them now */
1813 if (state0
!= TB_PORT_UP
|| state1
!= TB_PORT_UP
)
1816 val0
|= PHY_PORT_CS1_LINK_DISABLE
;
1817 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1821 val1
|= PHY_PORT_CS1_LINK_DISABLE
;
1822 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1826 /* Wait a bit and then re-enable both ports */
1827 usleep_range(10, 100);
1829 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, &val0
);
1832 ret
= pcie2cio_read(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, &val1
);
1836 val0
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1837 ret
= pcie2cio_write(icm
, TB_CFG_PORT
, port0
, PHY_PORT_CS1
, val0
);
1841 val1
&= ~PHY_PORT_CS1_LINK_DISABLE
;
1842 return pcie2cio_write(icm
, TB_CFG_PORT
, port1
, PHY_PORT_CS1
, val1
);
1845 static int icm_firmware_init(struct tb
*tb
)
1847 struct icm
*icm
= tb_priv(tb
);
1848 struct tb_nhi
*nhi
= tb
->nhi
;
1851 ret
= icm_firmware_start(tb
, nhi
);
1853 dev_err(&nhi
->pdev
->dev
, "could not start ICM firmware\n");
1857 if (icm
->get_mode
) {
1858 ret
= icm
->get_mode(tb
);
1861 case NHI_FW_SAFE_MODE
:
1862 icm
->safe_mode
= true;
1865 case NHI_FW_CM_MODE
:
1866 /* Ask ICM to accept all Thunderbolt devices */
1867 nhi_mailbox_cmd(nhi
, NHI_MAILBOX_ALLOW_ALL_DEVS
, 0);
1874 tb_err(tb
, "ICM firmware is in wrong mode: %u\n", ret
);
1880 * Reset both physical ports if there is anything connected to
1883 ret
= icm_reset_phy_port(tb
, 0);
1885 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port0\n");
1886 ret
= icm_reset_phy_port(tb
, 1);
1888 dev_warn(&nhi
->pdev
->dev
, "failed to reset links on port1\n");
1893 static int icm_driver_ready(struct tb
*tb
)
1895 struct icm
*icm
= tb_priv(tb
);
1898 ret
= icm_firmware_init(tb
);
1902 if (icm
->safe_mode
) {
1903 tb_info(tb
, "Thunderbolt host controller is in safe mode.\n");
1904 tb_info(tb
, "You need to update NVM firmware of the controller before it can be used.\n");
1905 tb_info(tb
, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1909 ret
= __icm_driver_ready(tb
, &tb
->security_level
, &tb
->nboot_acl
,
1915 * Make sure the number of supported preboot ACL matches what we
1916 * expect or disable the whole feature.
1918 if (tb
->nboot_acl
> icm
->max_boot_acl
)
1924 static int icm_suspend(struct tb
*tb
)
1926 struct icm
*icm
= tb_priv(tb
);
1928 if (icm
->save_devices
)
1929 icm
->save_devices(tb
);
1931 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
1936 * Mark all switches (except root switch) below this one unplugged. ICM
1937 * firmware will send us an updated list of switches after we have send
1938 * it driver ready command. If a switch is not in that list it will be
1939 * removed when we perform rescan.
1941 static void icm_unplug_children(struct tb_switch
*sw
)
1943 struct tb_port
*port
;
1946 sw
->is_unplugged
= true;
1948 tb_switch_for_each_port(sw
, port
) {
1950 port
->xdomain
->is_unplugged
= true;
1951 else if (tb_port_has_remote(port
))
1952 icm_unplug_children(port
->remote
->sw
);
1956 static int complete_rpm(struct device
*dev
, void *data
)
1958 struct tb_switch
*sw
= tb_to_switch(dev
);
1961 complete(&sw
->rpm_complete
);
1965 static void remove_unplugged_switch(struct tb_switch
*sw
)
1967 pm_runtime_get_sync(sw
->dev
.parent
);
1970 * Signal this and switches below for rpm_complete because
1971 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
1974 complete_rpm(&sw
->dev
, NULL
);
1975 bus_for_each_dev(&tb_bus_type
, &sw
->dev
, NULL
, complete_rpm
);
1976 tb_switch_remove(sw
);
1978 pm_runtime_mark_last_busy(sw
->dev
.parent
);
1979 pm_runtime_put_autosuspend(sw
->dev
.parent
);
1982 static void icm_free_unplugged_children(struct tb_switch
*sw
)
1984 struct tb_port
*port
;
1986 tb_switch_for_each_port(sw
, port
) {
1987 if (port
->xdomain
&& port
->xdomain
->is_unplugged
) {
1988 tb_xdomain_remove(port
->xdomain
);
1989 port
->xdomain
= NULL
;
1990 } else if (tb_port_has_remote(port
)) {
1991 if (port
->remote
->sw
->is_unplugged
) {
1992 remove_unplugged_switch(port
->remote
->sw
);
1993 port
->remote
= NULL
;
1995 icm_free_unplugged_children(port
->remote
->sw
);
2001 static void icm_rescan_work(struct work_struct
*work
)
2003 struct icm
*icm
= container_of(work
, struct icm
, rescan_work
.work
);
2004 struct tb
*tb
= icm_to_tb(icm
);
2006 mutex_lock(&tb
->lock
);
2007 if (tb
->root_switch
)
2008 icm_free_unplugged_children(tb
->root_switch
);
2009 mutex_unlock(&tb
->lock
);
2012 static void icm_complete(struct tb
*tb
)
2014 struct icm
*icm
= tb_priv(tb
);
2016 if (tb
->nhi
->going_away
)
2020 * If RTD3 was vetoed before we entered system suspend allow it
2021 * again now before driver ready is sent. Firmware sends a new RTD3
2022 * veto if it is still the case after we have sent it driver ready
2026 icm_unplug_children(tb
->root_switch
);
2029 * Now all existing children should be resumed, start events
2030 * from ICM to get updated status.
2032 __icm_driver_ready(tb
, NULL
, NULL
, NULL
);
2035 * We do not get notifications of devices that have been
2036 * unplugged during suspend so schedule rescan to clean them up
2039 queue_delayed_work(tb
->wq
, &icm
->rescan_work
, msecs_to_jiffies(500));
2042 static int icm_runtime_suspend(struct tb
*tb
)
2044 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
2048 static int icm_runtime_suspend_switch(struct tb_switch
*sw
)
2051 reinit_completion(&sw
->rpm_complete
);
2055 static int icm_runtime_resume_switch(struct tb_switch
*sw
)
2058 if (!wait_for_completion_timeout(&sw
->rpm_complete
,
2059 msecs_to_jiffies(500))) {
2060 dev_dbg(&sw
->dev
, "runtime resuming timed out\n");
2066 static int icm_runtime_resume(struct tb
*tb
)
2069 * We can reuse the same resume functionality than with system
2076 static int icm_start(struct tb
*tb
)
2078 struct icm
*icm
= tb_priv(tb
);
2082 tb
->root_switch
= tb_switch_alloc_safe_mode(tb
, &tb
->dev
, 0);
2084 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
2085 if (IS_ERR(tb
->root_switch
))
2086 return PTR_ERR(tb
->root_switch
);
2088 tb
->root_switch
->no_nvm_upgrade
= !icm
->can_upgrade_nvm
;
2089 tb
->root_switch
->rpm
= icm
->rpm
;
2094 ret
= tb_switch_add(tb
->root_switch
);
2096 tb_switch_put(tb
->root_switch
);
2097 tb
->root_switch
= NULL
;
2103 static void icm_stop(struct tb
*tb
)
2105 struct icm
*icm
= tb_priv(tb
);
2107 cancel_delayed_work(&icm
->rescan_work
);
2108 tb_switch_remove(tb
->root_switch
);
2109 tb
->root_switch
= NULL
;
2110 nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DRV_UNLOADS
, 0);
2113 static int icm_disconnect_pcie_paths(struct tb
*tb
)
2115 return nhi_mailbox_cmd(tb
->nhi
, NHI_MAILBOX_DISCONNECT_PCIE_PATHS
, 0);
2119 static const struct tb_cm_ops icm_fr_ops
= {
2120 .driver_ready
= icm_driver_ready
,
2123 .suspend
= icm_suspend
,
2124 .complete
= icm_complete
,
2125 .handle_event
= icm_handle_event
,
2126 .approve_switch
= icm_fr_approve_switch
,
2127 .add_switch_key
= icm_fr_add_switch_key
,
2128 .challenge_switch_key
= icm_fr_challenge_switch_key
,
2129 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
2130 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
2131 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
2135 static const struct tb_cm_ops icm_ar_ops
= {
2136 .driver_ready
= icm_driver_ready
,
2139 .suspend
= icm_suspend
,
2140 .complete
= icm_complete
,
2141 .runtime_suspend
= icm_runtime_suspend
,
2142 .runtime_resume
= icm_runtime_resume
,
2143 .runtime_suspend_switch
= icm_runtime_suspend_switch
,
2144 .runtime_resume_switch
= icm_runtime_resume_switch
,
2145 .handle_event
= icm_handle_event
,
2146 .get_boot_acl
= icm_ar_get_boot_acl
,
2147 .set_boot_acl
= icm_ar_set_boot_acl
,
2148 .approve_switch
= icm_fr_approve_switch
,
2149 .add_switch_key
= icm_fr_add_switch_key
,
2150 .challenge_switch_key
= icm_fr_challenge_switch_key
,
2151 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
2152 .approve_xdomain_paths
= icm_fr_approve_xdomain_paths
,
2153 .disconnect_xdomain_paths
= icm_fr_disconnect_xdomain_paths
,
2157 static const struct tb_cm_ops icm_tr_ops
= {
2158 .driver_ready
= icm_driver_ready
,
2161 .suspend
= icm_suspend
,
2162 .complete
= icm_complete
,
2163 .runtime_suspend
= icm_runtime_suspend
,
2164 .runtime_resume
= icm_runtime_resume
,
2165 .runtime_suspend_switch
= icm_runtime_suspend_switch
,
2166 .runtime_resume_switch
= icm_runtime_resume_switch
,
2167 .handle_event
= icm_handle_event
,
2168 .get_boot_acl
= icm_ar_get_boot_acl
,
2169 .set_boot_acl
= icm_ar_set_boot_acl
,
2170 .approve_switch
= icm_tr_approve_switch
,
2171 .add_switch_key
= icm_tr_add_switch_key
,
2172 .challenge_switch_key
= icm_tr_challenge_switch_key
,
2173 .disconnect_pcie_paths
= icm_disconnect_pcie_paths
,
2174 .approve_xdomain_paths
= icm_tr_approve_xdomain_paths
,
2175 .disconnect_xdomain_paths
= icm_tr_disconnect_xdomain_paths
,
2179 static const struct tb_cm_ops icm_icl_ops
= {
2180 .driver_ready
= icm_driver_ready
,
2183 .complete
= icm_complete
,
2184 .runtime_suspend
= icm_runtime_suspend
,
2185 .runtime_resume
= icm_runtime_resume
,
2186 .handle_event
= icm_handle_event
,
2187 .approve_xdomain_paths
= icm_tr_approve_xdomain_paths
,
2188 .disconnect_xdomain_paths
= icm_tr_disconnect_xdomain_paths
,
2191 struct tb
*icm_probe(struct tb_nhi
*nhi
)
2196 tb
= tb_domain_alloc(nhi
, sizeof(struct icm
));
2201 INIT_DELAYED_WORK(&icm
->rescan_work
, icm_rescan_work
);
2202 mutex_init(&icm
->request_lock
);
2204 switch (nhi
->pdev
->device
) {
2205 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI
:
2206 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI
:
2207 icm
->can_upgrade_nvm
= true;
2208 icm
->is_supported
= icm_fr_is_supported
;
2209 icm
->get_route
= icm_fr_get_route
;
2210 icm
->save_devices
= icm_fr_save_devices
;
2211 icm
->driver_ready
= icm_fr_driver_ready
;
2212 icm
->device_connected
= icm_fr_device_connected
;
2213 icm
->device_disconnected
= icm_fr_device_disconnected
;
2214 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
2215 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
2216 tb
->cm_ops
= &icm_fr_ops
;
2219 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI
:
2220 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI
:
2221 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI
:
2222 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI
:
2223 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI
:
2224 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
2226 * NVM upgrade has not been tested on Apple systems and
2227 * they don't provide images publicly either. To be on
2228 * the safe side prevent root switch NVM upgrade on Macs
2231 icm
->can_upgrade_nvm
= !x86_apple_machine
;
2232 icm
->is_supported
= icm_ar_is_supported
;
2233 icm
->cio_reset
= icm_ar_cio_reset
;
2234 icm
->get_mode
= icm_ar_get_mode
;
2235 icm
->get_route
= icm_ar_get_route
;
2236 icm
->save_devices
= icm_fr_save_devices
;
2237 icm
->driver_ready
= icm_ar_driver_ready
;
2238 icm
->device_connected
= icm_fr_device_connected
;
2239 icm
->device_disconnected
= icm_fr_device_disconnected
;
2240 icm
->xdomain_connected
= icm_fr_xdomain_connected
;
2241 icm
->xdomain_disconnected
= icm_fr_xdomain_disconnected
;
2242 tb
->cm_ops
= &icm_ar_ops
;
2245 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI
:
2246 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI
:
2247 icm
->max_boot_acl
= ICM_AR_PREBOOT_ACL_ENTRIES
;
2248 icm
->can_upgrade_nvm
= !x86_apple_machine
;
2249 icm
->is_supported
= icm_ar_is_supported
;
2250 icm
->cio_reset
= icm_tr_cio_reset
;
2251 icm
->get_mode
= icm_ar_get_mode
;
2252 icm
->driver_ready
= icm_tr_driver_ready
;
2253 icm
->device_connected
= icm_tr_device_connected
;
2254 icm
->device_disconnected
= icm_tr_device_disconnected
;
2255 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
2256 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
2257 tb
->cm_ops
= &icm_tr_ops
;
2260 case PCI_DEVICE_ID_INTEL_ICL_NHI0
:
2261 case PCI_DEVICE_ID_INTEL_ICL_NHI1
:
2262 icm
->is_supported
= icm_fr_is_supported
;
2263 icm
->driver_ready
= icm_icl_driver_ready
;
2264 icm
->set_uuid
= icm_icl_set_uuid
;
2265 icm
->device_connected
= icm_icl_device_connected
;
2266 icm
->device_disconnected
= icm_tr_device_disconnected
;
2267 icm
->xdomain_connected
= icm_tr_xdomain_connected
;
2268 icm
->xdomain_disconnected
= icm_tr_xdomain_disconnected
;
2269 icm
->rtd3_veto
= icm_icl_rtd3_veto
;
2270 tb
->cm_ops
= &icm_icl_ops
;
2274 if (!icm
->is_supported
|| !icm
->is_supported(tb
)) {
2275 dev_dbg(&nhi
->pdev
->dev
, "ICM not supported on this controller\n");