xref: /OK3568_Linux_fs/kernel/drivers/thunderbolt/icm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Internal Thunderbolt Connection Manager. This is a firmware running on
4*4882a593Smuzhiyun  * the Thunderbolt host controller performing most of the low-level
5*4882a593Smuzhiyun  * handling.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2017, Intel Corporation
8*4882a593Smuzhiyun  * Authors: Michael Jamet <michael.jamet@intel.com>
9*4882a593Smuzhiyun  *          Mika Westerberg <mika.westerberg@linux.intel.com>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/mutex.h>
14*4882a593Smuzhiyun #include <linux/moduleparam.h>
15*4882a593Smuzhiyun #include <linux/pci.h>
16*4882a593Smuzhiyun #include <linux/pm_runtime.h>
17*4882a593Smuzhiyun #include <linux/platform_data/x86/apple.h>
18*4882a593Smuzhiyun #include <linux/sizes.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/workqueue.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include "ctl.h"
23*4882a593Smuzhiyun #include "nhi_regs.h"
24*4882a593Smuzhiyun #include "tb.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define PCIE2CIO_CMD			0x30
27*4882a593Smuzhiyun #define PCIE2CIO_CMD_TIMEOUT		BIT(31)
28*4882a593Smuzhiyun #define PCIE2CIO_CMD_START		BIT(30)
29*4882a593Smuzhiyun #define PCIE2CIO_CMD_WRITE		BIT(21)
30*4882a593Smuzhiyun #define PCIE2CIO_CMD_CS_MASK		GENMASK(20, 19)
31*4882a593Smuzhiyun #define PCIE2CIO_CMD_CS_SHIFT		19
32*4882a593Smuzhiyun #define PCIE2CIO_CMD_PORT_MASK		GENMASK(18, 13)
33*4882a593Smuzhiyun #define PCIE2CIO_CMD_PORT_SHIFT		13
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define PCIE2CIO_WRDATA			0x34
36*4882a593Smuzhiyun #define PCIE2CIO_RDDATA			0x38
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define PHY_PORT_CS1			0x37
39*4882a593Smuzhiyun #define PHY_PORT_CS1_LINK_DISABLE	BIT(14)
40*4882a593Smuzhiyun #define PHY_PORT_CS1_LINK_STATE_MASK	GENMASK(29, 26)
41*4882a593Smuzhiyun #define PHY_PORT_CS1_LINK_STATE_SHIFT	26
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define ICM_TIMEOUT			5000	/* ms */
44*4882a593Smuzhiyun #define ICM_APPROVE_TIMEOUT		10000	/* ms */
45*4882a593Smuzhiyun #define ICM_MAX_LINK			4
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static bool start_icm;
48*4882a593Smuzhiyun module_param(start_icm, bool, 0444);
49*4882a593Smuzhiyun MODULE_PARM_DESC(start_icm, "start ICM firmware if it is not running (default: false)");
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /**
52*4882a593Smuzhiyun  * struct icm - Internal connection manager private data
53*4882a593Smuzhiyun  * @request_lock: Makes sure only one message is send to ICM at time
54*4882a593Smuzhiyun  * @rescan_work: Work used to rescan the surviving switches after resume
55*4882a593Smuzhiyun  * @upstream_port: Pointer to the PCIe upstream port this host
56*4882a593Smuzhiyun  *		   controller is connected. This is only set for systems
57*4882a593Smuzhiyun  *		   where ICM needs to be started manually
58*4882a593Smuzhiyun  * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
59*4882a593Smuzhiyun  *	     (only set when @upstream_port is not %NULL)
60*4882a593Smuzhiyun  * @safe_mode: ICM is in safe mode
61*4882a593Smuzhiyun  * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported)
62*4882a593Smuzhiyun  * @rpm: Does the controller support runtime PM (RTD3)
63*4882a593Smuzhiyun  * @can_upgrade_nvm: Can the NVM firmware be upgrade on this controller
64*4882a593Smuzhiyun  * @veto: Is RTD3 veto in effect
65*4882a593Smuzhiyun  * @is_supported: Checks if we can support ICM on this controller
66*4882a593Smuzhiyun  * @cio_reset: Trigger CIO reset
67*4882a593Smuzhiyun  * @get_mode: Read and return the ICM firmware mode (optional)
68*4882a593Smuzhiyun  * @get_route: Find a route string for given switch
69*4882a593Smuzhiyun  * @save_devices: Ask ICM to save devices to ACL when suspending (optional)
70*4882a593Smuzhiyun  * @driver_ready: Send driver ready message to ICM
71*4882a593Smuzhiyun  * @set_uuid: Set UUID for the root switch (optional)
72*4882a593Smuzhiyun  * @device_connected: Handle device connected ICM message
73*4882a593Smuzhiyun  * @device_disconnected: Handle device disconnected ICM message
74*4882a593Smuzhiyun  * @xdomain_connected - Handle XDomain connected ICM message
75*4882a593Smuzhiyun  * @xdomain_disconnected - Handle XDomain disconnected ICM message
76*4882a593Smuzhiyun  * @rtd3_veto: Handle RTD3 veto notification ICM message
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun struct icm {
79*4882a593Smuzhiyun 	struct mutex request_lock;
80*4882a593Smuzhiyun 	struct delayed_work rescan_work;
81*4882a593Smuzhiyun 	struct pci_dev *upstream_port;
82*4882a593Smuzhiyun 	size_t max_boot_acl;
83*4882a593Smuzhiyun 	int vnd_cap;
84*4882a593Smuzhiyun 	bool safe_mode;
85*4882a593Smuzhiyun 	bool rpm;
86*4882a593Smuzhiyun 	bool can_upgrade_nvm;
87*4882a593Smuzhiyun 	bool veto;
88*4882a593Smuzhiyun 	bool (*is_supported)(struct tb *tb);
89*4882a593Smuzhiyun 	int (*cio_reset)(struct tb *tb);
90*4882a593Smuzhiyun 	int (*get_mode)(struct tb *tb);
91*4882a593Smuzhiyun 	int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
92*4882a593Smuzhiyun 	void (*save_devices)(struct tb *tb);
93*4882a593Smuzhiyun 	int (*driver_ready)(struct tb *tb,
94*4882a593Smuzhiyun 			    enum tb_security_level *security_level,
95*4882a593Smuzhiyun 			    size_t *nboot_acl, bool *rpm);
96*4882a593Smuzhiyun 	void (*set_uuid)(struct tb *tb);
97*4882a593Smuzhiyun 	void (*device_connected)(struct tb *tb,
98*4882a593Smuzhiyun 				 const struct icm_pkg_header *hdr);
99*4882a593Smuzhiyun 	void (*device_disconnected)(struct tb *tb,
100*4882a593Smuzhiyun 				    const struct icm_pkg_header *hdr);
101*4882a593Smuzhiyun 	void (*xdomain_connected)(struct tb *tb,
102*4882a593Smuzhiyun 				  const struct icm_pkg_header *hdr);
103*4882a593Smuzhiyun 	void (*xdomain_disconnected)(struct tb *tb,
104*4882a593Smuzhiyun 				     const struct icm_pkg_header *hdr);
105*4882a593Smuzhiyun 	void (*rtd3_veto)(struct tb *tb, const struct icm_pkg_header *hdr);
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun struct icm_notification {
109*4882a593Smuzhiyun 	struct work_struct work;
110*4882a593Smuzhiyun 	struct icm_pkg_header *pkg;
111*4882a593Smuzhiyun 	struct tb *tb;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun struct ep_name_entry {
115*4882a593Smuzhiyun 	u8 len;
116*4882a593Smuzhiyun 	u8 type;
117*4882a593Smuzhiyun 	u8 data[];
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #define EP_NAME_INTEL_VSS	0x10
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /* Intel Vendor specific structure */
123*4882a593Smuzhiyun struct intel_vss {
124*4882a593Smuzhiyun 	u16 vendor;
125*4882a593Smuzhiyun 	u16 model;
126*4882a593Smuzhiyun 	u8 mc;
127*4882a593Smuzhiyun 	u8 flags;
128*4882a593Smuzhiyun 	u16 pci_devid;
129*4882a593Smuzhiyun 	u32 nvm_version;
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun #define INTEL_VSS_FLAGS_RTD3	BIT(0)
133*4882a593Smuzhiyun 
parse_intel_vss(const void * ep_name,size_t size)134*4882a593Smuzhiyun static const struct intel_vss *parse_intel_vss(const void *ep_name, size_t size)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	const void *end = ep_name + size;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	while (ep_name < end) {
139*4882a593Smuzhiyun 		const struct ep_name_entry *ep = ep_name;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		if (!ep->len)
142*4882a593Smuzhiyun 			break;
143*4882a593Smuzhiyun 		if (ep_name + ep->len > end)
144*4882a593Smuzhiyun 			break;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 		if (ep->type == EP_NAME_INTEL_VSS)
147*4882a593Smuzhiyun 			return (const struct intel_vss *)ep->data;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		ep_name += ep->len;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	return NULL;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
intel_vss_is_rtd3(const void * ep_name,size_t size)155*4882a593Smuzhiyun static bool intel_vss_is_rtd3(const void *ep_name, size_t size)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	const struct intel_vss *vss;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	vss = parse_intel_vss(ep_name, size);
160*4882a593Smuzhiyun 	if (vss)
161*4882a593Smuzhiyun 		return !!(vss->flags & INTEL_VSS_FLAGS_RTD3);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	return false;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
icm_to_tb(struct icm * icm)166*4882a593Smuzhiyun static inline struct tb *icm_to_tb(struct icm *icm)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	return ((void *)icm - sizeof(struct tb));
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
phy_port_from_route(u64 route,u8 depth)171*4882a593Smuzhiyun static inline u8 phy_port_from_route(u64 route, u8 depth)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	u8 link;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	link = depth ? route >> ((depth - 1) * 8) : route;
176*4882a593Smuzhiyun 	return tb_phy_port_from_link(link);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
dual_link_from_link(u8 link)179*4882a593Smuzhiyun static inline u8 dual_link_from_link(u8 link)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	return link ? ((link - 1) ^ 0x01) + 1 : 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
get_route(u32 route_hi,u32 route_lo)184*4882a593Smuzhiyun static inline u64 get_route(u32 route_hi, u32 route_lo)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	return (u64)route_hi << 32 | route_lo;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
get_parent_route(u64 route)189*4882a593Smuzhiyun static inline u64 get_parent_route(u64 route)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	int depth = tb_route_length(route);
192*4882a593Smuzhiyun 	return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
pci2cio_wait_completion(struct icm * icm,unsigned long timeout_msec)195*4882a593Smuzhiyun static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
198*4882a593Smuzhiyun 	u32 cmd;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	do {
201*4882a593Smuzhiyun 		pci_read_config_dword(icm->upstream_port,
202*4882a593Smuzhiyun 				      icm->vnd_cap + PCIE2CIO_CMD, &cmd);
203*4882a593Smuzhiyun 		if (!(cmd & PCIE2CIO_CMD_START)) {
204*4882a593Smuzhiyun 			if (cmd & PCIE2CIO_CMD_TIMEOUT)
205*4882a593Smuzhiyun 				break;
206*4882a593Smuzhiyun 			return 0;
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		msleep(50);
210*4882a593Smuzhiyun 	} while (time_before(jiffies, end));
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	return -ETIMEDOUT;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
pcie2cio_read(struct icm * icm,enum tb_cfg_space cs,unsigned int port,unsigned int index,u32 * data)215*4882a593Smuzhiyun static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
216*4882a593Smuzhiyun 			 unsigned int port, unsigned int index, u32 *data)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	struct pci_dev *pdev = icm->upstream_port;
219*4882a593Smuzhiyun 	int ret, vnd_cap = icm->vnd_cap;
220*4882a593Smuzhiyun 	u32 cmd;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	cmd = index;
223*4882a593Smuzhiyun 	cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
224*4882a593Smuzhiyun 	cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
225*4882a593Smuzhiyun 	cmd |= PCIE2CIO_CMD_START;
226*4882a593Smuzhiyun 	pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	ret = pci2cio_wait_completion(icm, 5000);
229*4882a593Smuzhiyun 	if (ret)
230*4882a593Smuzhiyun 		return ret;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
233*4882a593Smuzhiyun 	return 0;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
pcie2cio_write(struct icm * icm,enum tb_cfg_space cs,unsigned int port,unsigned int index,u32 data)236*4882a593Smuzhiyun static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
237*4882a593Smuzhiyun 			  unsigned int port, unsigned int index, u32 data)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	struct pci_dev *pdev = icm->upstream_port;
240*4882a593Smuzhiyun 	int vnd_cap = icm->vnd_cap;
241*4882a593Smuzhiyun 	u32 cmd;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	cmd = index;
246*4882a593Smuzhiyun 	cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
247*4882a593Smuzhiyun 	cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
248*4882a593Smuzhiyun 	cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
249*4882a593Smuzhiyun 	pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return pci2cio_wait_completion(icm, 5000);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
icm_match(const struct tb_cfg_request * req,const struct ctl_pkg * pkg)254*4882a593Smuzhiyun static bool icm_match(const struct tb_cfg_request *req,
255*4882a593Smuzhiyun 		      const struct ctl_pkg *pkg)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	const struct icm_pkg_header *res_hdr = pkg->buffer;
258*4882a593Smuzhiyun 	const struct icm_pkg_header *req_hdr = req->request;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	if (pkg->frame.eof != req->response_type)
261*4882a593Smuzhiyun 		return false;
262*4882a593Smuzhiyun 	if (res_hdr->code != req_hdr->code)
263*4882a593Smuzhiyun 		return false;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	return true;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
icm_copy(struct tb_cfg_request * req,const struct ctl_pkg * pkg)268*4882a593Smuzhiyun static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	const struct icm_pkg_header *hdr = pkg->buffer;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (hdr->packet_id < req->npackets) {
273*4882a593Smuzhiyun 		size_t offset = hdr->packet_id * req->response_size;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 		memcpy(req->response + offset, pkg->buffer, req->response_size);
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	return hdr->packet_id == hdr->total_packets - 1;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
icm_request(struct tb * tb,const void * request,size_t request_size,void * response,size_t response_size,size_t npackets,unsigned int timeout_msec)281*4882a593Smuzhiyun static int icm_request(struct tb *tb, const void *request, size_t request_size,
282*4882a593Smuzhiyun 		       void *response, size_t response_size, size_t npackets,
283*4882a593Smuzhiyun 		       unsigned int timeout_msec)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
286*4882a593Smuzhiyun 	int retries = 3;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	do {
289*4882a593Smuzhiyun 		struct tb_cfg_request *req;
290*4882a593Smuzhiyun 		struct tb_cfg_result res;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 		req = tb_cfg_request_alloc();
293*4882a593Smuzhiyun 		if (!req)
294*4882a593Smuzhiyun 			return -ENOMEM;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 		req->match = icm_match;
297*4882a593Smuzhiyun 		req->copy = icm_copy;
298*4882a593Smuzhiyun 		req->request = request;
299*4882a593Smuzhiyun 		req->request_size = request_size;
300*4882a593Smuzhiyun 		req->request_type = TB_CFG_PKG_ICM_CMD;
301*4882a593Smuzhiyun 		req->response = response;
302*4882a593Smuzhiyun 		req->npackets = npackets;
303*4882a593Smuzhiyun 		req->response_size = response_size;
304*4882a593Smuzhiyun 		req->response_type = TB_CFG_PKG_ICM_RESP;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		mutex_lock(&icm->request_lock);
307*4882a593Smuzhiyun 		res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
308*4882a593Smuzhiyun 		mutex_unlock(&icm->request_lock);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 		tb_cfg_request_put(req);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 		if (res.err != -ETIMEDOUT)
313*4882a593Smuzhiyun 			return res.err == 1 ? -EIO : res.err;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 		usleep_range(20, 50);
316*4882a593Smuzhiyun 	} while (retries--);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return -ETIMEDOUT;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun  * If rescan is queued to run (we are resuming), postpone it to give the
323*4882a593Smuzhiyun  * firmware some more time to send device connected notifications for next
324*4882a593Smuzhiyun  * devices in the chain.
325*4882a593Smuzhiyun  */
icm_postpone_rescan(struct tb * tb)326*4882a593Smuzhiyun static void icm_postpone_rescan(struct tb *tb)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (delayed_work_pending(&icm->rescan_work))
331*4882a593Smuzhiyun 		mod_delayed_work(tb->wq, &icm->rescan_work,
332*4882a593Smuzhiyun 				 msecs_to_jiffies(500));
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
icm_veto_begin(struct tb * tb)335*4882a593Smuzhiyun static void icm_veto_begin(struct tb *tb)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	if (!icm->veto) {
340*4882a593Smuzhiyun 		icm->veto = true;
341*4882a593Smuzhiyun 		/* Keep the domain powered while veto is in effect */
342*4882a593Smuzhiyun 		pm_runtime_get(&tb->dev);
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
icm_veto_end(struct tb * tb)346*4882a593Smuzhiyun static void icm_veto_end(struct tb *tb)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	if (icm->veto) {
351*4882a593Smuzhiyun 		icm->veto = false;
352*4882a593Smuzhiyun 		/* Allow the domain suspend now */
353*4882a593Smuzhiyun 		pm_runtime_mark_last_busy(&tb->dev);
354*4882a593Smuzhiyun 		pm_runtime_put_autosuspend(&tb->dev);
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
icm_firmware_running(const struct tb_nhi * nhi)358*4882a593Smuzhiyun static bool icm_firmware_running(const struct tb_nhi *nhi)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	u32 val;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	val = ioread32(nhi->iobase + REG_FW_STS);
363*4882a593Smuzhiyun 	return !!(val & REG_FW_STS_ICM_EN);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
icm_fr_is_supported(struct tb * tb)366*4882a593Smuzhiyun static bool icm_fr_is_supported(struct tb *tb)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	return !x86_apple_machine;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
icm_fr_get_switch_index(u32 port)371*4882a593Smuzhiyun static inline int icm_fr_get_switch_index(u32 port)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	int index;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
376*4882a593Smuzhiyun 		return 0;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	index = port >> ICM_PORT_INDEX_SHIFT;
379*4882a593Smuzhiyun 	return index != 0xff ? index : 0;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
icm_fr_get_route(struct tb * tb,u8 link,u8 depth,u64 * route)382*4882a593Smuzhiyun static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	struct icm_fr_pkg_get_topology_response *switches, *sw;
385*4882a593Smuzhiyun 	struct icm_fr_pkg_get_topology request = {
386*4882a593Smuzhiyun 		.hdr = { .code = ICM_GET_TOPOLOGY },
387*4882a593Smuzhiyun 	};
388*4882a593Smuzhiyun 	size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
389*4882a593Smuzhiyun 	int ret, index;
390*4882a593Smuzhiyun 	u8 i;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
393*4882a593Smuzhiyun 	if (!switches)
394*4882a593Smuzhiyun 		return -ENOMEM;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), switches,
397*4882a593Smuzhiyun 			  sizeof(*switches), npackets, ICM_TIMEOUT);
398*4882a593Smuzhiyun 	if (ret)
399*4882a593Smuzhiyun 		goto err_free;
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	sw = &switches[0];
402*4882a593Smuzhiyun 	index = icm_fr_get_switch_index(sw->ports[link]);
403*4882a593Smuzhiyun 	if (!index) {
404*4882a593Smuzhiyun 		ret = -ENODEV;
405*4882a593Smuzhiyun 		goto err_free;
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	sw = &switches[index];
409*4882a593Smuzhiyun 	for (i = 1; i < depth; i++) {
410*4882a593Smuzhiyun 		unsigned int j;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 		if (!(sw->first_data & ICM_SWITCH_USED)) {
413*4882a593Smuzhiyun 			ret = -ENODEV;
414*4882a593Smuzhiyun 			goto err_free;
415*4882a593Smuzhiyun 		}
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 		for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
418*4882a593Smuzhiyun 			index = icm_fr_get_switch_index(sw->ports[j]);
419*4882a593Smuzhiyun 			if (index > sw->switch_index) {
420*4882a593Smuzhiyun 				sw = &switches[index];
421*4882a593Smuzhiyun 				break;
422*4882a593Smuzhiyun 			}
423*4882a593Smuzhiyun 		}
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	*route = get_route(sw->route_hi, sw->route_lo);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun err_free:
429*4882a593Smuzhiyun 	kfree(switches);
430*4882a593Smuzhiyun 	return ret;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
icm_fr_save_devices(struct tb * tb)433*4882a593Smuzhiyun static void icm_fr_save_devices(struct tb *tb)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun static int
icm_fr_driver_ready(struct tb * tb,enum tb_security_level * security_level,size_t * nboot_acl,bool * rpm)439*4882a593Smuzhiyun icm_fr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
440*4882a593Smuzhiyun 		    size_t *nboot_acl, bool *rpm)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun 	struct icm_fr_pkg_driver_ready_response reply;
443*4882a593Smuzhiyun 	struct icm_pkg_driver_ready request = {
444*4882a593Smuzhiyun 		.hdr.code = ICM_DRIVER_READY,
445*4882a593Smuzhiyun 	};
446*4882a593Smuzhiyun 	int ret;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
449*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
450*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
451*4882a593Smuzhiyun 	if (ret)
452*4882a593Smuzhiyun 		return ret;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	if (security_level)
455*4882a593Smuzhiyun 		*security_level = reply.security_level & ICM_FR_SLEVEL_MASK;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	return 0;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
icm_fr_approve_switch(struct tb * tb,struct tb_switch * sw)460*4882a593Smuzhiyun static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	struct icm_fr_pkg_approve_device request;
463*4882a593Smuzhiyun 	struct icm_fr_pkg_approve_device reply;
464*4882a593Smuzhiyun 	int ret;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
467*4882a593Smuzhiyun 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
468*4882a593Smuzhiyun 	request.hdr.code = ICM_APPROVE_DEVICE;
469*4882a593Smuzhiyun 	request.connection_id = sw->connection_id;
470*4882a593Smuzhiyun 	request.connection_key = sw->connection_key;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
473*4882a593Smuzhiyun 	/* Use larger timeout as establishing tunnels can take some time */
474*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
475*4882a593Smuzhiyun 			  1, ICM_APPROVE_TIMEOUT);
476*4882a593Smuzhiyun 	if (ret)
477*4882a593Smuzhiyun 		return ret;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR) {
480*4882a593Smuzhiyun 		tb_warn(tb, "PCIe tunnel creation failed\n");
481*4882a593Smuzhiyun 		return -EIO;
482*4882a593Smuzhiyun 	}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
icm_fr_add_switch_key(struct tb * tb,struct tb_switch * sw)487*4882a593Smuzhiyun static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	struct icm_fr_pkg_add_device_key request;
490*4882a593Smuzhiyun 	struct icm_fr_pkg_add_device_key_response reply;
491*4882a593Smuzhiyun 	int ret;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
494*4882a593Smuzhiyun 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
495*4882a593Smuzhiyun 	request.hdr.code = ICM_ADD_DEVICE_KEY;
496*4882a593Smuzhiyun 	request.connection_id = sw->connection_id;
497*4882a593Smuzhiyun 	request.connection_key = sw->connection_key;
498*4882a593Smuzhiyun 	memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
501*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
502*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
503*4882a593Smuzhiyun 	if (ret)
504*4882a593Smuzhiyun 		return ret;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR) {
507*4882a593Smuzhiyun 		tb_warn(tb, "Adding key to switch failed\n");
508*4882a593Smuzhiyun 		return -EIO;
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	return 0;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
icm_fr_challenge_switch_key(struct tb * tb,struct tb_switch * sw,const u8 * challenge,u8 * response)514*4882a593Smuzhiyun static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
515*4882a593Smuzhiyun 				       const u8 *challenge, u8 *response)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun 	struct icm_fr_pkg_challenge_device request;
518*4882a593Smuzhiyun 	struct icm_fr_pkg_challenge_device_response reply;
519*4882a593Smuzhiyun 	int ret;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
522*4882a593Smuzhiyun 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
523*4882a593Smuzhiyun 	request.hdr.code = ICM_CHALLENGE_DEVICE;
524*4882a593Smuzhiyun 	request.connection_id = sw->connection_id;
525*4882a593Smuzhiyun 	request.connection_key = sw->connection_key;
526*4882a593Smuzhiyun 	memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
529*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
530*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
531*4882a593Smuzhiyun 	if (ret)
532*4882a593Smuzhiyun 		return ret;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
535*4882a593Smuzhiyun 		return -EKEYREJECTED;
536*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
537*4882a593Smuzhiyun 		return -ENOKEY;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	return 0;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun 
icm_fr_approve_xdomain_paths(struct tb * tb,struct tb_xdomain * xd)544*4882a593Smuzhiyun static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	struct icm_fr_pkg_approve_xdomain_response reply;
547*4882a593Smuzhiyun 	struct icm_fr_pkg_approve_xdomain request;
548*4882a593Smuzhiyun 	int ret;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
551*4882a593Smuzhiyun 	request.hdr.code = ICM_APPROVE_XDOMAIN;
552*4882a593Smuzhiyun 	request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
553*4882a593Smuzhiyun 	memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	request.transmit_path = xd->transmit_path;
556*4882a593Smuzhiyun 	request.transmit_ring = xd->transmit_ring;
557*4882a593Smuzhiyun 	request.receive_path = xd->receive_path;
558*4882a593Smuzhiyun 	request.receive_ring = xd->receive_ring;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
561*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
562*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
563*4882a593Smuzhiyun 	if (ret)
564*4882a593Smuzhiyun 		return ret;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
567*4882a593Smuzhiyun 		return -EIO;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	return 0;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
icm_fr_disconnect_xdomain_paths(struct tb * tb,struct tb_xdomain * xd)572*4882a593Smuzhiyun static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun 	u8 phy_port;
575*4882a593Smuzhiyun 	u8 cmd;
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	phy_port = tb_phy_port_from_link(xd->link);
578*4882a593Smuzhiyun 	if (phy_port == 0)
579*4882a593Smuzhiyun 		cmd = NHI_MAILBOX_DISCONNECT_PA;
580*4882a593Smuzhiyun 	else
581*4882a593Smuzhiyun 		cmd = NHI_MAILBOX_DISCONNECT_PB;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	nhi_mailbox_cmd(tb->nhi, cmd, 1);
584*4882a593Smuzhiyun 	usleep_range(10, 50);
585*4882a593Smuzhiyun 	nhi_mailbox_cmd(tb->nhi, cmd, 2);
586*4882a593Smuzhiyun 	return 0;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun 
alloc_switch(struct tb_switch * parent_sw,u64 route,const uuid_t * uuid)589*4882a593Smuzhiyun static struct tb_switch *alloc_switch(struct tb_switch *parent_sw, u64 route,
590*4882a593Smuzhiyun 				      const uuid_t *uuid)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun 	struct tb *tb = parent_sw->tb;
593*4882a593Smuzhiyun 	struct tb_switch *sw;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	sw = tb_switch_alloc(tb, &parent_sw->dev, route);
596*4882a593Smuzhiyun 	if (IS_ERR(sw)) {
597*4882a593Smuzhiyun 		tb_warn(tb, "failed to allocate switch at %llx\n", route);
598*4882a593Smuzhiyun 		return sw;
599*4882a593Smuzhiyun 	}
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
602*4882a593Smuzhiyun 	if (!sw->uuid) {
603*4882a593Smuzhiyun 		tb_switch_put(sw);
604*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	init_completion(&sw->rpm_complete);
608*4882a593Smuzhiyun 	return sw;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
add_switch(struct tb_switch * parent_sw,struct tb_switch * sw)611*4882a593Smuzhiyun static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	u64 route = tb_route(sw);
614*4882a593Smuzhiyun 	int ret;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/* Link the two switches now */
617*4882a593Smuzhiyun 	tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
618*4882a593Smuzhiyun 	tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	ret = tb_switch_add(sw);
621*4882a593Smuzhiyun 	if (ret)
622*4882a593Smuzhiyun 		tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	return ret;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
update_switch(struct tb_switch * parent_sw,struct tb_switch * sw,u64 route,u8 connection_id,u8 connection_key,u8 link,u8 depth,bool boot)627*4882a593Smuzhiyun static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
628*4882a593Smuzhiyun 			  u64 route, u8 connection_id, u8 connection_key,
629*4882a593Smuzhiyun 			  u8 link, u8 depth, bool boot)
630*4882a593Smuzhiyun {
631*4882a593Smuzhiyun 	/* Disconnect from parent */
632*4882a593Smuzhiyun 	tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
633*4882a593Smuzhiyun 	/* Re-connect via updated port*/
634*4882a593Smuzhiyun 	tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/* Update with the new addressing information */
637*4882a593Smuzhiyun 	sw->config.route_hi = upper_32_bits(route);
638*4882a593Smuzhiyun 	sw->config.route_lo = lower_32_bits(route);
639*4882a593Smuzhiyun 	sw->connection_id = connection_id;
640*4882a593Smuzhiyun 	sw->connection_key = connection_key;
641*4882a593Smuzhiyun 	sw->link = link;
642*4882a593Smuzhiyun 	sw->depth = depth;
643*4882a593Smuzhiyun 	sw->boot = boot;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	/* This switch still exists */
646*4882a593Smuzhiyun 	sw->is_unplugged = false;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	/* Runtime resume is now complete */
649*4882a593Smuzhiyun 	complete(&sw->rpm_complete);
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun 
remove_switch(struct tb_switch * sw)652*4882a593Smuzhiyun static void remove_switch(struct tb_switch *sw)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun 	struct tb_switch *parent_sw;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	parent_sw = tb_to_switch(sw->dev.parent);
657*4882a593Smuzhiyun 	tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
658*4882a593Smuzhiyun 	tb_switch_remove(sw);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun 
add_xdomain(struct tb_switch * sw,u64 route,const uuid_t * local_uuid,const uuid_t * remote_uuid,u8 link,u8 depth)661*4882a593Smuzhiyun static void add_xdomain(struct tb_switch *sw, u64 route,
662*4882a593Smuzhiyun 			const uuid_t *local_uuid, const uuid_t *remote_uuid,
663*4882a593Smuzhiyun 			u8 link, u8 depth)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	struct tb_xdomain *xd;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	pm_runtime_get_sync(&sw->dev);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
670*4882a593Smuzhiyun 	if (!xd)
671*4882a593Smuzhiyun 		goto out;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	xd->link = link;
674*4882a593Smuzhiyun 	xd->depth = depth;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	tb_port_at(route, sw)->xdomain = xd;
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	tb_xdomain_add(xd);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun out:
681*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(&sw->dev);
682*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(&sw->dev);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
update_xdomain(struct tb_xdomain * xd,u64 route,u8 link)685*4882a593Smuzhiyun static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	xd->link = link;
688*4882a593Smuzhiyun 	xd->route = route;
689*4882a593Smuzhiyun 	xd->is_unplugged = false;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun 
remove_xdomain(struct tb_xdomain * xd)692*4882a593Smuzhiyun static void remove_xdomain(struct tb_xdomain *xd)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun 	struct tb_switch *sw;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	sw = tb_to_switch(xd->dev.parent);
697*4882a593Smuzhiyun 	tb_port_at(xd->route, sw)->xdomain = NULL;
698*4882a593Smuzhiyun 	tb_xdomain_remove(xd);
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun static void
icm_fr_device_connected(struct tb * tb,const struct icm_pkg_header * hdr)702*4882a593Smuzhiyun icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	const struct icm_fr_event_device_connected *pkg =
705*4882a593Smuzhiyun 		(const struct icm_fr_event_device_connected *)hdr;
706*4882a593Smuzhiyun 	enum tb_security_level security_level;
707*4882a593Smuzhiyun 	struct tb_switch *sw, *parent_sw;
708*4882a593Smuzhiyun 	bool boot, dual_lane, speed_gen3;
709*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
710*4882a593Smuzhiyun 	bool authorized = false;
711*4882a593Smuzhiyun 	struct tb_xdomain *xd;
712*4882a593Smuzhiyun 	u8 link, depth;
713*4882a593Smuzhiyun 	u64 route;
714*4882a593Smuzhiyun 	int ret;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	icm_postpone_rescan(tb);
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
719*4882a593Smuzhiyun 	depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
720*4882a593Smuzhiyun 		ICM_LINK_INFO_DEPTH_SHIFT;
721*4882a593Smuzhiyun 	authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
722*4882a593Smuzhiyun 	security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
723*4882a593Smuzhiyun 			 ICM_FLAGS_SLEVEL_SHIFT;
724*4882a593Smuzhiyun 	boot = pkg->link_info & ICM_LINK_INFO_BOOT;
725*4882a593Smuzhiyun 	dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
726*4882a593Smuzhiyun 	speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
729*4882a593Smuzhiyun 		tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
730*4882a593Smuzhiyun 			link, depth);
731*4882a593Smuzhiyun 		return;
732*4882a593Smuzhiyun 	}
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
735*4882a593Smuzhiyun 	if (sw) {
736*4882a593Smuzhiyun 		u8 phy_port, sw_phy_port;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		parent_sw = tb_to_switch(sw->dev.parent);
739*4882a593Smuzhiyun 		sw_phy_port = tb_phy_port_from_link(sw->link);
740*4882a593Smuzhiyun 		phy_port = tb_phy_port_from_link(link);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 		/*
743*4882a593Smuzhiyun 		 * On resume ICM will send us connected events for the
744*4882a593Smuzhiyun 		 * devices that still are present. However, that
745*4882a593Smuzhiyun 		 * information might have changed for example by the
746*4882a593Smuzhiyun 		 * fact that a switch on a dual-link connection might
747*4882a593Smuzhiyun 		 * have been enumerated using the other link now. Make
748*4882a593Smuzhiyun 		 * sure our book keeping matches that.
749*4882a593Smuzhiyun 		 */
750*4882a593Smuzhiyun 		if (sw->depth == depth && sw_phy_port == phy_port &&
751*4882a593Smuzhiyun 		    !!sw->authorized == authorized) {
752*4882a593Smuzhiyun 			/*
753*4882a593Smuzhiyun 			 * It was enumerated through another link so update
754*4882a593Smuzhiyun 			 * route string accordingly.
755*4882a593Smuzhiyun 			 */
756*4882a593Smuzhiyun 			if (sw->link != link) {
757*4882a593Smuzhiyun 				ret = icm->get_route(tb, link, depth, &route);
758*4882a593Smuzhiyun 				if (ret) {
759*4882a593Smuzhiyun 					tb_err(tb, "failed to update route string for switch at %u.%u\n",
760*4882a593Smuzhiyun 					       link, depth);
761*4882a593Smuzhiyun 					tb_switch_put(sw);
762*4882a593Smuzhiyun 					return;
763*4882a593Smuzhiyun 				}
764*4882a593Smuzhiyun 			} else {
765*4882a593Smuzhiyun 				route = tb_route(sw);
766*4882a593Smuzhiyun 			}
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 			update_switch(parent_sw, sw, route, pkg->connection_id,
769*4882a593Smuzhiyun 				      pkg->connection_key, link, depth, boot);
770*4882a593Smuzhiyun 			tb_switch_put(sw);
771*4882a593Smuzhiyun 			return;
772*4882a593Smuzhiyun 		}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 		/*
775*4882a593Smuzhiyun 		 * User connected the same switch to another physical
776*4882a593Smuzhiyun 		 * port or to another part of the topology. Remove the
777*4882a593Smuzhiyun 		 * existing switch now before adding the new one.
778*4882a593Smuzhiyun 		 */
779*4882a593Smuzhiyun 		remove_switch(sw);
780*4882a593Smuzhiyun 		tb_switch_put(sw);
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/*
784*4882a593Smuzhiyun 	 * If the switch was not found by UUID, look for a switch on
785*4882a593Smuzhiyun 	 * same physical port (taking possible link aggregation into
786*4882a593Smuzhiyun 	 * account) and depth. If we found one it is definitely a stale
787*4882a593Smuzhiyun 	 * one so remove it first.
788*4882a593Smuzhiyun 	 */
789*4882a593Smuzhiyun 	sw = tb_switch_find_by_link_depth(tb, link, depth);
790*4882a593Smuzhiyun 	if (!sw) {
791*4882a593Smuzhiyun 		u8 dual_link;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 		dual_link = dual_link_from_link(link);
794*4882a593Smuzhiyun 		if (dual_link)
795*4882a593Smuzhiyun 			sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
796*4882a593Smuzhiyun 	}
797*4882a593Smuzhiyun 	if (sw) {
798*4882a593Smuzhiyun 		remove_switch(sw);
799*4882a593Smuzhiyun 		tb_switch_put(sw);
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	/* Remove existing XDomain connection if found */
803*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_link_depth(tb, link, depth);
804*4882a593Smuzhiyun 	if (xd) {
805*4882a593Smuzhiyun 		remove_xdomain(xd);
806*4882a593Smuzhiyun 		tb_xdomain_put(xd);
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
810*4882a593Smuzhiyun 	if (!parent_sw) {
811*4882a593Smuzhiyun 		tb_err(tb, "failed to find parent switch for %u.%u\n",
812*4882a593Smuzhiyun 		       link, depth);
813*4882a593Smuzhiyun 		return;
814*4882a593Smuzhiyun 	}
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	ret = icm->get_route(tb, link, depth, &route);
817*4882a593Smuzhiyun 	if (ret) {
818*4882a593Smuzhiyun 		tb_err(tb, "failed to find route string for switch at %u.%u\n",
819*4882a593Smuzhiyun 		       link, depth);
820*4882a593Smuzhiyun 		tb_switch_put(parent_sw);
821*4882a593Smuzhiyun 		return;
822*4882a593Smuzhiyun 	}
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	pm_runtime_get_sync(&parent_sw->dev);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
827*4882a593Smuzhiyun 	if (!IS_ERR(sw)) {
828*4882a593Smuzhiyun 		sw->connection_id = pkg->connection_id;
829*4882a593Smuzhiyun 		sw->connection_key = pkg->connection_key;
830*4882a593Smuzhiyun 		sw->link = link;
831*4882a593Smuzhiyun 		sw->depth = depth;
832*4882a593Smuzhiyun 		sw->authorized = authorized;
833*4882a593Smuzhiyun 		sw->security_level = security_level;
834*4882a593Smuzhiyun 		sw->boot = boot;
835*4882a593Smuzhiyun 		sw->link_speed = speed_gen3 ? 20 : 10;
836*4882a593Smuzhiyun 		sw->link_width = dual_lane ? 2 : 1;
837*4882a593Smuzhiyun 		sw->rpm = intel_vss_is_rtd3(pkg->ep_name, sizeof(pkg->ep_name));
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 		if (add_switch(parent_sw, sw))
840*4882a593Smuzhiyun 			tb_switch_put(sw);
841*4882a593Smuzhiyun 	}
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(&parent_sw->dev);
844*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(&parent_sw->dev);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	tb_switch_put(parent_sw);
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun static void
icm_fr_device_disconnected(struct tb * tb,const struct icm_pkg_header * hdr)850*4882a593Smuzhiyun icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
851*4882a593Smuzhiyun {
852*4882a593Smuzhiyun 	const struct icm_fr_event_device_disconnected *pkg =
853*4882a593Smuzhiyun 		(const struct icm_fr_event_device_disconnected *)hdr;
854*4882a593Smuzhiyun 	struct tb_switch *sw;
855*4882a593Smuzhiyun 	u8 link, depth;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
858*4882a593Smuzhiyun 	depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
859*4882a593Smuzhiyun 		ICM_LINK_INFO_DEPTH_SHIFT;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
862*4882a593Smuzhiyun 		tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
863*4882a593Smuzhiyun 		return;
864*4882a593Smuzhiyun 	}
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	sw = tb_switch_find_by_link_depth(tb, link, depth);
867*4882a593Smuzhiyun 	if (!sw) {
868*4882a593Smuzhiyun 		tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
869*4882a593Smuzhiyun 			depth);
870*4882a593Smuzhiyun 		return;
871*4882a593Smuzhiyun 	}
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	remove_switch(sw);
874*4882a593Smuzhiyun 	tb_switch_put(sw);
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun static void
icm_fr_xdomain_connected(struct tb * tb,const struct icm_pkg_header * hdr)878*4882a593Smuzhiyun icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun 	const struct icm_fr_event_xdomain_connected *pkg =
881*4882a593Smuzhiyun 		(const struct icm_fr_event_xdomain_connected *)hdr;
882*4882a593Smuzhiyun 	struct tb_xdomain *xd;
883*4882a593Smuzhiyun 	struct tb_switch *sw;
884*4882a593Smuzhiyun 	u8 link, depth;
885*4882a593Smuzhiyun 	u64 route;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
888*4882a593Smuzhiyun 	depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
889*4882a593Smuzhiyun 		ICM_LINK_INFO_DEPTH_SHIFT;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	if (link > ICM_MAX_LINK || depth > TB_SWITCH_MAX_DEPTH) {
892*4882a593Smuzhiyun 		tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
893*4882a593Smuzhiyun 		return;
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	route = get_route(pkg->local_route_hi, pkg->local_route_lo);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
899*4882a593Smuzhiyun 	if (xd) {
900*4882a593Smuzhiyun 		u8 xd_phy_port, phy_port;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 		xd_phy_port = phy_port_from_route(xd->route, xd->depth);
903*4882a593Smuzhiyun 		phy_port = phy_port_from_route(route, depth);
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 		if (xd->depth == depth && xd_phy_port == phy_port) {
906*4882a593Smuzhiyun 			update_xdomain(xd, route, link);
907*4882a593Smuzhiyun 			tb_xdomain_put(xd);
908*4882a593Smuzhiyun 			return;
909*4882a593Smuzhiyun 		}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 		/*
912*4882a593Smuzhiyun 		 * If we find an existing XDomain connection remove it
913*4882a593Smuzhiyun 		 * now. We need to go through login handshake and
914*4882a593Smuzhiyun 		 * everything anyway to be able to re-establish the
915*4882a593Smuzhiyun 		 * connection.
916*4882a593Smuzhiyun 		 */
917*4882a593Smuzhiyun 		remove_xdomain(xd);
918*4882a593Smuzhiyun 		tb_xdomain_put(xd);
919*4882a593Smuzhiyun 	}
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	/*
922*4882a593Smuzhiyun 	 * Look if there already exists an XDomain in the same place
923*4882a593Smuzhiyun 	 * than the new one and in that case remove it because it is
924*4882a593Smuzhiyun 	 * most likely another host that got disconnected.
925*4882a593Smuzhiyun 	 */
926*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_link_depth(tb, link, depth);
927*4882a593Smuzhiyun 	if (!xd) {
928*4882a593Smuzhiyun 		u8 dual_link;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 		dual_link = dual_link_from_link(link);
931*4882a593Smuzhiyun 		if (dual_link)
932*4882a593Smuzhiyun 			xd = tb_xdomain_find_by_link_depth(tb, dual_link,
933*4882a593Smuzhiyun 							   depth);
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 	if (xd) {
936*4882a593Smuzhiyun 		remove_xdomain(xd);
937*4882a593Smuzhiyun 		tb_xdomain_put(xd);
938*4882a593Smuzhiyun 	}
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	/*
941*4882a593Smuzhiyun 	 * If the user disconnected a switch during suspend and
942*4882a593Smuzhiyun 	 * connected another host to the same port, remove the switch
943*4882a593Smuzhiyun 	 * first.
944*4882a593Smuzhiyun 	 */
945*4882a593Smuzhiyun 	sw = tb_switch_find_by_route(tb, route);
946*4882a593Smuzhiyun 	if (sw) {
947*4882a593Smuzhiyun 		remove_switch(sw);
948*4882a593Smuzhiyun 		tb_switch_put(sw);
949*4882a593Smuzhiyun 	}
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	sw = tb_switch_find_by_link_depth(tb, link, depth);
952*4882a593Smuzhiyun 	if (!sw) {
953*4882a593Smuzhiyun 		tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
954*4882a593Smuzhiyun 			depth);
955*4882a593Smuzhiyun 		return;
956*4882a593Smuzhiyun 	}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
959*4882a593Smuzhiyun 		    depth);
960*4882a593Smuzhiyun 	tb_switch_put(sw);
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun static void
icm_fr_xdomain_disconnected(struct tb * tb,const struct icm_pkg_header * hdr)964*4882a593Smuzhiyun icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
965*4882a593Smuzhiyun {
966*4882a593Smuzhiyun 	const struct icm_fr_event_xdomain_disconnected *pkg =
967*4882a593Smuzhiyun 		(const struct icm_fr_event_xdomain_disconnected *)hdr;
968*4882a593Smuzhiyun 	struct tb_xdomain *xd;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	/*
971*4882a593Smuzhiyun 	 * If the connection is through one or multiple devices, the
972*4882a593Smuzhiyun 	 * XDomain device is removed along with them so it is fine if we
973*4882a593Smuzhiyun 	 * cannot find it here.
974*4882a593Smuzhiyun 	 */
975*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
976*4882a593Smuzhiyun 	if (xd) {
977*4882a593Smuzhiyun 		remove_xdomain(xd);
978*4882a593Smuzhiyun 		tb_xdomain_put(xd);
979*4882a593Smuzhiyun 	}
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
icm_tr_cio_reset(struct tb * tb)982*4882a593Smuzhiyun static int icm_tr_cio_reset(struct tb *tb)
983*4882a593Smuzhiyun {
984*4882a593Smuzhiyun 	return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1));
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun static int
icm_tr_driver_ready(struct tb * tb,enum tb_security_level * security_level,size_t * nboot_acl,bool * rpm)988*4882a593Smuzhiyun icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level,
989*4882a593Smuzhiyun 		    size_t *nboot_acl, bool *rpm)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	struct icm_tr_pkg_driver_ready_response reply;
992*4882a593Smuzhiyun 	struct icm_pkg_driver_ready request = {
993*4882a593Smuzhiyun 		.hdr.code = ICM_DRIVER_READY,
994*4882a593Smuzhiyun 	};
995*4882a593Smuzhiyun 	int ret;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
998*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
999*4882a593Smuzhiyun 			  1, 20000);
1000*4882a593Smuzhiyun 	if (ret)
1001*4882a593Smuzhiyun 		return ret;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	if (security_level)
1004*4882a593Smuzhiyun 		*security_level = reply.info & ICM_TR_INFO_SLEVEL_MASK;
1005*4882a593Smuzhiyun 	if (nboot_acl)
1006*4882a593Smuzhiyun 		*nboot_acl = (reply.info & ICM_TR_INFO_BOOT_ACL_MASK) >>
1007*4882a593Smuzhiyun 				ICM_TR_INFO_BOOT_ACL_SHIFT;
1008*4882a593Smuzhiyun 	if (rpm)
1009*4882a593Smuzhiyun 		*rpm = !!(reply.hdr.flags & ICM_TR_FLAGS_RTD3);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	return 0;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun 
icm_tr_approve_switch(struct tb * tb,struct tb_switch * sw)1014*4882a593Smuzhiyun static int icm_tr_approve_switch(struct tb *tb, struct tb_switch *sw)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	struct icm_tr_pkg_approve_device request;
1017*4882a593Smuzhiyun 	struct icm_tr_pkg_approve_device reply;
1018*4882a593Smuzhiyun 	int ret;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
1021*4882a593Smuzhiyun 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1022*4882a593Smuzhiyun 	request.hdr.code = ICM_APPROVE_DEVICE;
1023*4882a593Smuzhiyun 	request.route_lo = sw->config.route_lo;
1024*4882a593Smuzhiyun 	request.route_hi = sw->config.route_hi;
1025*4882a593Smuzhiyun 	request.connection_id = sw->connection_id;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1028*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1029*4882a593Smuzhiyun 			  1, ICM_APPROVE_TIMEOUT);
1030*4882a593Smuzhiyun 	if (ret)
1031*4882a593Smuzhiyun 		return ret;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1034*4882a593Smuzhiyun 		tb_warn(tb, "PCIe tunnel creation failed\n");
1035*4882a593Smuzhiyun 		return -EIO;
1036*4882a593Smuzhiyun 	}
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	return 0;
1039*4882a593Smuzhiyun }
1040*4882a593Smuzhiyun 
icm_tr_add_switch_key(struct tb * tb,struct tb_switch * sw)1041*4882a593Smuzhiyun static int icm_tr_add_switch_key(struct tb *tb, struct tb_switch *sw)
1042*4882a593Smuzhiyun {
1043*4882a593Smuzhiyun 	struct icm_tr_pkg_add_device_key_response reply;
1044*4882a593Smuzhiyun 	struct icm_tr_pkg_add_device_key request;
1045*4882a593Smuzhiyun 	int ret;
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
1048*4882a593Smuzhiyun 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1049*4882a593Smuzhiyun 	request.hdr.code = ICM_ADD_DEVICE_KEY;
1050*4882a593Smuzhiyun 	request.route_lo = sw->config.route_lo;
1051*4882a593Smuzhiyun 	request.route_hi = sw->config.route_hi;
1052*4882a593Smuzhiyun 	request.connection_id = sw->connection_id;
1053*4882a593Smuzhiyun 	memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1056*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1057*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1058*4882a593Smuzhiyun 	if (ret)
1059*4882a593Smuzhiyun 		return ret;
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR) {
1062*4882a593Smuzhiyun 		tb_warn(tb, "Adding key to switch failed\n");
1063*4882a593Smuzhiyun 		return -EIO;
1064*4882a593Smuzhiyun 	}
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	return 0;
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun 
icm_tr_challenge_switch_key(struct tb * tb,struct tb_switch * sw,const u8 * challenge,u8 * response)1069*4882a593Smuzhiyun static int icm_tr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
1070*4882a593Smuzhiyun 				       const u8 *challenge, u8 *response)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun 	struct icm_tr_pkg_challenge_device_response reply;
1073*4882a593Smuzhiyun 	struct icm_tr_pkg_challenge_device request;
1074*4882a593Smuzhiyun 	int ret;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
1077*4882a593Smuzhiyun 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
1078*4882a593Smuzhiyun 	request.hdr.code = ICM_CHALLENGE_DEVICE;
1079*4882a593Smuzhiyun 	request.route_lo = sw->config.route_lo;
1080*4882a593Smuzhiyun 	request.route_hi = sw->config.route_hi;
1081*4882a593Smuzhiyun 	request.connection_id = sw->connection_id;
1082*4882a593Smuzhiyun 	memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1085*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1086*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1087*4882a593Smuzhiyun 	if (ret)
1088*4882a593Smuzhiyun 		return ret;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
1091*4882a593Smuzhiyun 		return -EKEYREJECTED;
1092*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
1093*4882a593Smuzhiyun 		return -ENOKEY;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	return 0;
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun 
icm_tr_approve_xdomain_paths(struct tb * tb,struct tb_xdomain * xd)1100*4882a593Smuzhiyun static int icm_tr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1101*4882a593Smuzhiyun {
1102*4882a593Smuzhiyun 	struct icm_tr_pkg_approve_xdomain_response reply;
1103*4882a593Smuzhiyun 	struct icm_tr_pkg_approve_xdomain request;
1104*4882a593Smuzhiyun 	int ret;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
1107*4882a593Smuzhiyun 	request.hdr.code = ICM_APPROVE_XDOMAIN;
1108*4882a593Smuzhiyun 	request.route_hi = upper_32_bits(xd->route);
1109*4882a593Smuzhiyun 	request.route_lo = lower_32_bits(xd->route);
1110*4882a593Smuzhiyun 	request.transmit_path = xd->transmit_path;
1111*4882a593Smuzhiyun 	request.transmit_ring = xd->transmit_ring;
1112*4882a593Smuzhiyun 	request.receive_path = xd->receive_path;
1113*4882a593Smuzhiyun 	request.receive_ring = xd->receive_ring;
1114*4882a593Smuzhiyun 	memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1117*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1118*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1119*4882a593Smuzhiyun 	if (ret)
1120*4882a593Smuzhiyun 		return ret;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
1123*4882a593Smuzhiyun 		return -EIO;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	return 0;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun 
icm_tr_xdomain_tear_down(struct tb * tb,struct tb_xdomain * xd,int stage)1128*4882a593Smuzhiyun static int icm_tr_xdomain_tear_down(struct tb *tb, struct tb_xdomain *xd,
1129*4882a593Smuzhiyun 				    int stage)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun 	struct icm_tr_pkg_disconnect_xdomain_response reply;
1132*4882a593Smuzhiyun 	struct icm_tr_pkg_disconnect_xdomain request;
1133*4882a593Smuzhiyun 	int ret;
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	memset(&request, 0, sizeof(request));
1136*4882a593Smuzhiyun 	request.hdr.code = ICM_DISCONNECT_XDOMAIN;
1137*4882a593Smuzhiyun 	request.stage = stage;
1138*4882a593Smuzhiyun 	request.route_hi = upper_32_bits(xd->route);
1139*4882a593Smuzhiyun 	request.route_lo = lower_32_bits(xd->route);
1140*4882a593Smuzhiyun 	memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1143*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1144*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1145*4882a593Smuzhiyun 	if (ret)
1146*4882a593Smuzhiyun 		return ret;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
1149*4882a593Smuzhiyun 		return -EIO;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	return 0;
1152*4882a593Smuzhiyun }
1153*4882a593Smuzhiyun 
icm_tr_disconnect_xdomain_paths(struct tb * tb,struct tb_xdomain * xd)1154*4882a593Smuzhiyun static int icm_tr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun 	int ret;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	ret = icm_tr_xdomain_tear_down(tb, xd, 1);
1159*4882a593Smuzhiyun 	if (ret)
1160*4882a593Smuzhiyun 		return ret;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	usleep_range(10, 50);
1163*4882a593Smuzhiyun 	return icm_tr_xdomain_tear_down(tb, xd, 2);
1164*4882a593Smuzhiyun }
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun static void
__icm_tr_device_connected(struct tb * tb,const struct icm_pkg_header * hdr,bool force_rtd3)1167*4882a593Smuzhiyun __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
1168*4882a593Smuzhiyun 			  bool force_rtd3)
1169*4882a593Smuzhiyun {
1170*4882a593Smuzhiyun 	const struct icm_tr_event_device_connected *pkg =
1171*4882a593Smuzhiyun 		(const struct icm_tr_event_device_connected *)hdr;
1172*4882a593Smuzhiyun 	bool authorized, boot, dual_lane, speed_gen3;
1173*4882a593Smuzhiyun 	enum tb_security_level security_level;
1174*4882a593Smuzhiyun 	struct tb_switch *sw, *parent_sw;
1175*4882a593Smuzhiyun 	struct tb_xdomain *xd;
1176*4882a593Smuzhiyun 	u64 route;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	icm_postpone_rescan(tb);
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	/*
1181*4882a593Smuzhiyun 	 * Currently we don't use the QoS information coming with the
1182*4882a593Smuzhiyun 	 * device connected message so simply just ignore that extra
1183*4882a593Smuzhiyun 	 * packet for now.
1184*4882a593Smuzhiyun 	 */
1185*4882a593Smuzhiyun 	if (pkg->hdr.packet_id)
1186*4882a593Smuzhiyun 		return;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	route = get_route(pkg->route_hi, pkg->route_lo);
1189*4882a593Smuzhiyun 	authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
1190*4882a593Smuzhiyun 	security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
1191*4882a593Smuzhiyun 			 ICM_FLAGS_SLEVEL_SHIFT;
1192*4882a593Smuzhiyun 	boot = pkg->link_info & ICM_LINK_INFO_BOOT;
1193*4882a593Smuzhiyun 	dual_lane = pkg->hdr.flags & ICM_FLAGS_DUAL_LANE;
1194*4882a593Smuzhiyun 	speed_gen3 = pkg->hdr.flags & ICM_FLAGS_SPEED_GEN3;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
1197*4882a593Smuzhiyun 		tb_info(tb, "switch at %llx was rejected by ICM firmware because topology limit exceeded\n",
1198*4882a593Smuzhiyun 			route);
1199*4882a593Smuzhiyun 		return;
1200*4882a593Smuzhiyun 	}
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
1203*4882a593Smuzhiyun 	if (sw) {
1204*4882a593Smuzhiyun 		/* Update the switch if it is still in the same place */
1205*4882a593Smuzhiyun 		if (tb_route(sw) == route && !!sw->authorized == authorized) {
1206*4882a593Smuzhiyun 			parent_sw = tb_to_switch(sw->dev.parent);
1207*4882a593Smuzhiyun 			update_switch(parent_sw, sw, route, pkg->connection_id,
1208*4882a593Smuzhiyun 				      0, 0, 0, boot);
1209*4882a593Smuzhiyun 			tb_switch_put(sw);
1210*4882a593Smuzhiyun 			return;
1211*4882a593Smuzhiyun 		}
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 		remove_switch(sw);
1214*4882a593Smuzhiyun 		tb_switch_put(sw);
1215*4882a593Smuzhiyun 	}
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	/* Another switch with the same address */
1218*4882a593Smuzhiyun 	sw = tb_switch_find_by_route(tb, route);
1219*4882a593Smuzhiyun 	if (sw) {
1220*4882a593Smuzhiyun 		remove_switch(sw);
1221*4882a593Smuzhiyun 		tb_switch_put(sw);
1222*4882a593Smuzhiyun 	}
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	/* XDomain connection with the same address */
1225*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_route(tb, route);
1226*4882a593Smuzhiyun 	if (xd) {
1227*4882a593Smuzhiyun 		remove_xdomain(xd);
1228*4882a593Smuzhiyun 		tb_xdomain_put(xd);
1229*4882a593Smuzhiyun 	}
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	parent_sw = tb_switch_find_by_route(tb, get_parent_route(route));
1232*4882a593Smuzhiyun 	if (!parent_sw) {
1233*4882a593Smuzhiyun 		tb_err(tb, "failed to find parent switch for %llx\n", route);
1234*4882a593Smuzhiyun 		return;
1235*4882a593Smuzhiyun 	}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	pm_runtime_get_sync(&parent_sw->dev);
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	sw = alloc_switch(parent_sw, route, &pkg->ep_uuid);
1240*4882a593Smuzhiyun 	if (!IS_ERR(sw)) {
1241*4882a593Smuzhiyun 		sw->connection_id = pkg->connection_id;
1242*4882a593Smuzhiyun 		sw->authorized = authorized;
1243*4882a593Smuzhiyun 		sw->security_level = security_level;
1244*4882a593Smuzhiyun 		sw->boot = boot;
1245*4882a593Smuzhiyun 		sw->link_speed = speed_gen3 ? 20 : 10;
1246*4882a593Smuzhiyun 		sw->link_width = dual_lane ? 2 : 1;
1247*4882a593Smuzhiyun 		sw->rpm = force_rtd3;
1248*4882a593Smuzhiyun 		if (!sw->rpm)
1249*4882a593Smuzhiyun 			sw->rpm = intel_vss_is_rtd3(pkg->ep_name,
1250*4882a593Smuzhiyun 						    sizeof(pkg->ep_name));
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 		if (add_switch(parent_sw, sw))
1253*4882a593Smuzhiyun 			tb_switch_put(sw);
1254*4882a593Smuzhiyun 	}
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(&parent_sw->dev);
1257*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(&parent_sw->dev);
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	tb_switch_put(parent_sw);
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun static void
icm_tr_device_connected(struct tb * tb,const struct icm_pkg_header * hdr)1263*4882a593Smuzhiyun icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1264*4882a593Smuzhiyun {
1265*4882a593Smuzhiyun 	__icm_tr_device_connected(tb, hdr, false);
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun static void
icm_tr_device_disconnected(struct tb * tb,const struct icm_pkg_header * hdr)1269*4882a593Smuzhiyun icm_tr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun 	const struct icm_tr_event_device_disconnected *pkg =
1272*4882a593Smuzhiyun 		(const struct icm_tr_event_device_disconnected *)hdr;
1273*4882a593Smuzhiyun 	struct tb_switch *sw;
1274*4882a593Smuzhiyun 	u64 route;
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	route = get_route(pkg->route_hi, pkg->route_lo);
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	sw = tb_switch_find_by_route(tb, route);
1279*4882a593Smuzhiyun 	if (!sw) {
1280*4882a593Smuzhiyun 		tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1281*4882a593Smuzhiyun 		return;
1282*4882a593Smuzhiyun 	}
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	remove_switch(sw);
1285*4882a593Smuzhiyun 	tb_switch_put(sw);
1286*4882a593Smuzhiyun }
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun static void
icm_tr_xdomain_connected(struct tb * tb,const struct icm_pkg_header * hdr)1289*4882a593Smuzhiyun icm_tr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1290*4882a593Smuzhiyun {
1291*4882a593Smuzhiyun 	const struct icm_tr_event_xdomain_connected *pkg =
1292*4882a593Smuzhiyun 		(const struct icm_tr_event_xdomain_connected *)hdr;
1293*4882a593Smuzhiyun 	struct tb_xdomain *xd;
1294*4882a593Smuzhiyun 	struct tb_switch *sw;
1295*4882a593Smuzhiyun 	u64 route;
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	if (!tb->root_switch)
1298*4882a593Smuzhiyun 		return;
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	route = get_route(pkg->local_route_hi, pkg->local_route_lo);
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
1303*4882a593Smuzhiyun 	if (xd) {
1304*4882a593Smuzhiyun 		if (xd->route == route) {
1305*4882a593Smuzhiyun 			update_xdomain(xd, route, 0);
1306*4882a593Smuzhiyun 			tb_xdomain_put(xd);
1307*4882a593Smuzhiyun 			return;
1308*4882a593Smuzhiyun 		}
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 		remove_xdomain(xd);
1311*4882a593Smuzhiyun 		tb_xdomain_put(xd);
1312*4882a593Smuzhiyun 	}
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	/* An existing xdomain with the same address */
1315*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_route(tb, route);
1316*4882a593Smuzhiyun 	if (xd) {
1317*4882a593Smuzhiyun 		remove_xdomain(xd);
1318*4882a593Smuzhiyun 		tb_xdomain_put(xd);
1319*4882a593Smuzhiyun 	}
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	/*
1322*4882a593Smuzhiyun 	 * If the user disconnected a switch during suspend and
1323*4882a593Smuzhiyun 	 * connected another host to the same port, remove the switch
1324*4882a593Smuzhiyun 	 * first.
1325*4882a593Smuzhiyun 	 */
1326*4882a593Smuzhiyun 	sw = tb_switch_find_by_route(tb, route);
1327*4882a593Smuzhiyun 	if (sw) {
1328*4882a593Smuzhiyun 		remove_switch(sw);
1329*4882a593Smuzhiyun 		tb_switch_put(sw);
1330*4882a593Smuzhiyun 	}
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	sw = tb_switch_find_by_route(tb, get_parent_route(route));
1333*4882a593Smuzhiyun 	if (!sw) {
1334*4882a593Smuzhiyun 		tb_warn(tb, "no switch exists at %llx, ignoring\n", route);
1335*4882a593Smuzhiyun 		return;
1336*4882a593Smuzhiyun 	}
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, 0, 0);
1339*4882a593Smuzhiyun 	tb_switch_put(sw);
1340*4882a593Smuzhiyun }
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun static void
icm_tr_xdomain_disconnected(struct tb * tb,const struct icm_pkg_header * hdr)1343*4882a593Smuzhiyun icm_tr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
1344*4882a593Smuzhiyun {
1345*4882a593Smuzhiyun 	const struct icm_tr_event_xdomain_disconnected *pkg =
1346*4882a593Smuzhiyun 		(const struct icm_tr_event_xdomain_disconnected *)hdr;
1347*4882a593Smuzhiyun 	struct tb_xdomain *xd;
1348*4882a593Smuzhiyun 	u64 route;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	route = get_route(pkg->route_hi, pkg->route_lo);
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	xd = tb_xdomain_find_by_route(tb, route);
1353*4882a593Smuzhiyun 	if (xd) {
1354*4882a593Smuzhiyun 		remove_xdomain(xd);
1355*4882a593Smuzhiyun 		tb_xdomain_put(xd);
1356*4882a593Smuzhiyun 	}
1357*4882a593Smuzhiyun }
1358*4882a593Smuzhiyun 
get_upstream_port(struct pci_dev * pdev)1359*4882a593Smuzhiyun static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
1360*4882a593Smuzhiyun {
1361*4882a593Smuzhiyun 	struct pci_dev *parent;
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun 	parent = pci_upstream_bridge(pdev);
1364*4882a593Smuzhiyun 	while (parent) {
1365*4882a593Smuzhiyun 		if (!pci_is_pcie(parent))
1366*4882a593Smuzhiyun 			return NULL;
1367*4882a593Smuzhiyun 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
1368*4882a593Smuzhiyun 			break;
1369*4882a593Smuzhiyun 		parent = pci_upstream_bridge(parent);
1370*4882a593Smuzhiyun 	}
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	if (!parent)
1373*4882a593Smuzhiyun 		return NULL;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	switch (parent->device) {
1376*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1377*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1378*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1379*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1380*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1381*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1382*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1383*4882a593Smuzhiyun 		return parent;
1384*4882a593Smuzhiyun 	}
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	return NULL;
1387*4882a593Smuzhiyun }
1388*4882a593Smuzhiyun 
icm_ar_is_supported(struct tb * tb)1389*4882a593Smuzhiyun static bool icm_ar_is_supported(struct tb *tb)
1390*4882a593Smuzhiyun {
1391*4882a593Smuzhiyun 	struct pci_dev *upstream_port;
1392*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	/*
1395*4882a593Smuzhiyun 	 * Starting from Alpine Ridge we can use ICM on Apple machines
1396*4882a593Smuzhiyun 	 * as well. We just need to reset and re-enable it first.
1397*4882a593Smuzhiyun 	 * However, only start it if explicitly asked by the user.
1398*4882a593Smuzhiyun 	 */
1399*4882a593Smuzhiyun 	if (icm_firmware_running(tb->nhi))
1400*4882a593Smuzhiyun 		return true;
1401*4882a593Smuzhiyun 	if (!start_icm)
1402*4882a593Smuzhiyun 		return false;
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 	/*
1405*4882a593Smuzhiyun 	 * Find the upstream PCIe port in case we need to do reset
1406*4882a593Smuzhiyun 	 * through its vendor specific registers.
1407*4882a593Smuzhiyun 	 */
1408*4882a593Smuzhiyun 	upstream_port = get_upstream_port(tb->nhi->pdev);
1409*4882a593Smuzhiyun 	if (upstream_port) {
1410*4882a593Smuzhiyun 		int cap;
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 		cap = pci_find_ext_capability(upstream_port,
1413*4882a593Smuzhiyun 					      PCI_EXT_CAP_ID_VNDR);
1414*4882a593Smuzhiyun 		if (cap > 0) {
1415*4882a593Smuzhiyun 			icm->upstream_port = upstream_port;
1416*4882a593Smuzhiyun 			icm->vnd_cap = cap;
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 			return true;
1419*4882a593Smuzhiyun 		}
1420*4882a593Smuzhiyun 	}
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	return false;
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun 
icm_ar_cio_reset(struct tb * tb)1425*4882a593Smuzhiyun static int icm_ar_cio_reset(struct tb *tb)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun 	return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9));
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun 
icm_ar_get_mode(struct tb * tb)1430*4882a593Smuzhiyun static int icm_ar_get_mode(struct tb *tb)
1431*4882a593Smuzhiyun {
1432*4882a593Smuzhiyun 	struct tb_nhi *nhi = tb->nhi;
1433*4882a593Smuzhiyun 	int retries = 60;
1434*4882a593Smuzhiyun 	u32 val;
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 	do {
1437*4882a593Smuzhiyun 		val = ioread32(nhi->iobase + REG_FW_STS);
1438*4882a593Smuzhiyun 		if (val & REG_FW_STS_NVM_AUTH_DONE)
1439*4882a593Smuzhiyun 			break;
1440*4882a593Smuzhiyun 		msleep(50);
1441*4882a593Smuzhiyun 	} while (--retries);
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	if (!retries) {
1444*4882a593Smuzhiyun 		dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
1445*4882a593Smuzhiyun 		return -ENODEV;
1446*4882a593Smuzhiyun 	}
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	return nhi_mailbox_mode(nhi);
1449*4882a593Smuzhiyun }
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun static int
icm_ar_driver_ready(struct tb * tb,enum tb_security_level * security_level,size_t * nboot_acl,bool * rpm)1452*4882a593Smuzhiyun icm_ar_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1453*4882a593Smuzhiyun 		    size_t *nboot_acl, bool *rpm)
1454*4882a593Smuzhiyun {
1455*4882a593Smuzhiyun 	struct icm_ar_pkg_driver_ready_response reply;
1456*4882a593Smuzhiyun 	struct icm_pkg_driver_ready request = {
1457*4882a593Smuzhiyun 		.hdr.code = ICM_DRIVER_READY,
1458*4882a593Smuzhiyun 	};
1459*4882a593Smuzhiyun 	int ret;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1462*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1463*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1464*4882a593Smuzhiyun 	if (ret)
1465*4882a593Smuzhiyun 		return ret;
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	if (security_level)
1468*4882a593Smuzhiyun 		*security_level = reply.info & ICM_AR_INFO_SLEVEL_MASK;
1469*4882a593Smuzhiyun 	if (nboot_acl && (reply.info & ICM_AR_INFO_BOOT_ACL_SUPPORTED))
1470*4882a593Smuzhiyun 		*nboot_acl = (reply.info & ICM_AR_INFO_BOOT_ACL_MASK) >>
1471*4882a593Smuzhiyun 				ICM_AR_INFO_BOOT_ACL_SHIFT;
1472*4882a593Smuzhiyun 	if (rpm)
1473*4882a593Smuzhiyun 		*rpm = !!(reply.hdr.flags & ICM_AR_FLAGS_RTD3);
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun 	return 0;
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun 
icm_ar_get_route(struct tb * tb,u8 link,u8 depth,u64 * route)1478*4882a593Smuzhiyun static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
1479*4882a593Smuzhiyun {
1480*4882a593Smuzhiyun 	struct icm_ar_pkg_get_route_response reply;
1481*4882a593Smuzhiyun 	struct icm_ar_pkg_get_route request = {
1482*4882a593Smuzhiyun 		.hdr = { .code = ICM_GET_ROUTE },
1483*4882a593Smuzhiyun 		.link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
1484*4882a593Smuzhiyun 	};
1485*4882a593Smuzhiyun 	int ret;
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1488*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1489*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1490*4882a593Smuzhiyun 	if (ret)
1491*4882a593Smuzhiyun 		return ret;
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
1494*4882a593Smuzhiyun 		return -EIO;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	*route = get_route(reply.route_hi, reply.route_lo);
1497*4882a593Smuzhiyun 	return 0;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun 
icm_ar_get_boot_acl(struct tb * tb,uuid_t * uuids,size_t nuuids)1500*4882a593Smuzhiyun static int icm_ar_get_boot_acl(struct tb *tb, uuid_t *uuids, size_t nuuids)
1501*4882a593Smuzhiyun {
1502*4882a593Smuzhiyun 	struct icm_ar_pkg_preboot_acl_response reply;
1503*4882a593Smuzhiyun 	struct icm_ar_pkg_preboot_acl request = {
1504*4882a593Smuzhiyun 		.hdr = { .code = ICM_PREBOOT_ACL },
1505*4882a593Smuzhiyun 	};
1506*4882a593Smuzhiyun 	int ret, i;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1509*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1510*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1511*4882a593Smuzhiyun 	if (ret)
1512*4882a593Smuzhiyun 		return ret;
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
1515*4882a593Smuzhiyun 		return -EIO;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	for (i = 0; i < nuuids; i++) {
1518*4882a593Smuzhiyun 		u32 *uuid = (u32 *)&uuids[i];
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 		uuid[0] = reply.acl[i].uuid_lo;
1521*4882a593Smuzhiyun 		uuid[1] = reply.acl[i].uuid_hi;
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun 		if (uuid[0] == 0xffffffff && uuid[1] == 0xffffffff) {
1524*4882a593Smuzhiyun 			/* Map empty entries to null UUID */
1525*4882a593Smuzhiyun 			uuid[0] = 0;
1526*4882a593Smuzhiyun 			uuid[1] = 0;
1527*4882a593Smuzhiyun 		} else if (uuid[0] != 0 || uuid[1] != 0) {
1528*4882a593Smuzhiyun 			/* Upper two DWs are always one's */
1529*4882a593Smuzhiyun 			uuid[2] = 0xffffffff;
1530*4882a593Smuzhiyun 			uuid[3] = 0xffffffff;
1531*4882a593Smuzhiyun 		}
1532*4882a593Smuzhiyun 	}
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	return ret;
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun 
icm_ar_set_boot_acl(struct tb * tb,const uuid_t * uuids,size_t nuuids)1537*4882a593Smuzhiyun static int icm_ar_set_boot_acl(struct tb *tb, const uuid_t *uuids,
1538*4882a593Smuzhiyun 			       size_t nuuids)
1539*4882a593Smuzhiyun {
1540*4882a593Smuzhiyun 	struct icm_ar_pkg_preboot_acl_response reply;
1541*4882a593Smuzhiyun 	struct icm_ar_pkg_preboot_acl request = {
1542*4882a593Smuzhiyun 		.hdr = {
1543*4882a593Smuzhiyun 			.code = ICM_PREBOOT_ACL,
1544*4882a593Smuzhiyun 			.flags = ICM_FLAGS_WRITE,
1545*4882a593Smuzhiyun 		},
1546*4882a593Smuzhiyun 	};
1547*4882a593Smuzhiyun 	int ret, i;
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 	for (i = 0; i < nuuids; i++) {
1550*4882a593Smuzhiyun 		const u32 *uuid = (const u32 *)&uuids[i];
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 		if (uuid_is_null(&uuids[i])) {
1553*4882a593Smuzhiyun 			/*
1554*4882a593Smuzhiyun 			 * Map null UUID to the empty (all one) entries
1555*4882a593Smuzhiyun 			 * for ICM.
1556*4882a593Smuzhiyun 			 */
1557*4882a593Smuzhiyun 			request.acl[i].uuid_lo = 0xffffffff;
1558*4882a593Smuzhiyun 			request.acl[i].uuid_hi = 0xffffffff;
1559*4882a593Smuzhiyun 		} else {
1560*4882a593Smuzhiyun 			/* Two high DWs need to be set to all one */
1561*4882a593Smuzhiyun 			if (uuid[2] != 0xffffffff || uuid[3] != 0xffffffff)
1562*4882a593Smuzhiyun 				return -EINVAL;
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 			request.acl[i].uuid_lo = uuid[0];
1565*4882a593Smuzhiyun 			request.acl[i].uuid_hi = uuid[1];
1566*4882a593Smuzhiyun 		}
1567*4882a593Smuzhiyun 	}
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1570*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1571*4882a593Smuzhiyun 			  1, ICM_TIMEOUT);
1572*4882a593Smuzhiyun 	if (ret)
1573*4882a593Smuzhiyun 		return ret;
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
1576*4882a593Smuzhiyun 		return -EIO;
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 	return 0;
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun static int
icm_icl_driver_ready(struct tb * tb,enum tb_security_level * security_level,size_t * nboot_acl,bool * rpm)1582*4882a593Smuzhiyun icm_icl_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1583*4882a593Smuzhiyun 		    size_t *nboot_acl, bool *rpm)
1584*4882a593Smuzhiyun {
1585*4882a593Smuzhiyun 	struct icm_tr_pkg_driver_ready_response reply;
1586*4882a593Smuzhiyun 	struct icm_pkg_driver_ready request = {
1587*4882a593Smuzhiyun 		.hdr.code = ICM_DRIVER_READY,
1588*4882a593Smuzhiyun 	};
1589*4882a593Smuzhiyun 	int ret;
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	memset(&reply, 0, sizeof(reply));
1592*4882a593Smuzhiyun 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
1593*4882a593Smuzhiyun 			  1, 20000);
1594*4882a593Smuzhiyun 	if (ret)
1595*4882a593Smuzhiyun 		return ret;
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	/* Ice Lake always supports RTD3 */
1598*4882a593Smuzhiyun 	if (rpm)
1599*4882a593Smuzhiyun 		*rpm = true;
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 	return 0;
1602*4882a593Smuzhiyun }
1603*4882a593Smuzhiyun 
icm_icl_set_uuid(struct tb * tb)1604*4882a593Smuzhiyun static void icm_icl_set_uuid(struct tb *tb)
1605*4882a593Smuzhiyun {
1606*4882a593Smuzhiyun 	struct tb_nhi *nhi = tb->nhi;
1607*4882a593Smuzhiyun 	u32 uuid[4];
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun 	pci_read_config_dword(nhi->pdev, VS_CAP_10, &uuid[0]);
1610*4882a593Smuzhiyun 	pci_read_config_dword(nhi->pdev, VS_CAP_11, &uuid[1]);
1611*4882a593Smuzhiyun 	uuid[2] = 0xffffffff;
1612*4882a593Smuzhiyun 	uuid[3] = 0xffffffff;
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	tb->root_switch->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1615*4882a593Smuzhiyun }
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun static void
icm_icl_device_connected(struct tb * tb,const struct icm_pkg_header * hdr)1618*4882a593Smuzhiyun icm_icl_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun 	__icm_tr_device_connected(tb, hdr, true);
1621*4882a593Smuzhiyun }
1622*4882a593Smuzhiyun 
icm_icl_rtd3_veto(struct tb * tb,const struct icm_pkg_header * hdr)1623*4882a593Smuzhiyun static void icm_icl_rtd3_veto(struct tb *tb, const struct icm_pkg_header *hdr)
1624*4882a593Smuzhiyun {
1625*4882a593Smuzhiyun 	const struct icm_icl_event_rtd3_veto *pkg =
1626*4882a593Smuzhiyun 		(const struct icm_icl_event_rtd3_veto *)hdr;
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	tb_dbg(tb, "ICM rtd3 veto=0x%08x\n", pkg->veto_reason);
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	if (pkg->veto_reason)
1631*4882a593Smuzhiyun 		icm_veto_begin(tb);
1632*4882a593Smuzhiyun 	else
1633*4882a593Smuzhiyun 		icm_veto_end(tb);
1634*4882a593Smuzhiyun }
1635*4882a593Smuzhiyun 
icm_tgl_is_supported(struct tb * tb)1636*4882a593Smuzhiyun static bool icm_tgl_is_supported(struct tb *tb)
1637*4882a593Smuzhiyun {
1638*4882a593Smuzhiyun 	u32 val;
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun 	/*
1641*4882a593Smuzhiyun 	 * If the firmware is not running use software CM. This platform
1642*4882a593Smuzhiyun 	 * should fully support both.
1643*4882a593Smuzhiyun 	 */
1644*4882a593Smuzhiyun 	val = ioread32(tb->nhi->iobase + REG_FW_STS);
1645*4882a593Smuzhiyun 	return !!(val & REG_FW_STS_NVM_AUTH_DONE);
1646*4882a593Smuzhiyun }
1647*4882a593Smuzhiyun 
icm_handle_notification(struct work_struct * work)1648*4882a593Smuzhiyun static void icm_handle_notification(struct work_struct *work)
1649*4882a593Smuzhiyun {
1650*4882a593Smuzhiyun 	struct icm_notification *n = container_of(work, typeof(*n), work);
1651*4882a593Smuzhiyun 	struct tb *tb = n->tb;
1652*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	mutex_lock(&tb->lock);
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	/*
1657*4882a593Smuzhiyun 	 * When the domain is stopped we flush its workqueue but before
1658*4882a593Smuzhiyun 	 * that the root switch is removed. In that case we should treat
1659*4882a593Smuzhiyun 	 * the queued events as being canceled.
1660*4882a593Smuzhiyun 	 */
1661*4882a593Smuzhiyun 	if (tb->root_switch) {
1662*4882a593Smuzhiyun 		switch (n->pkg->code) {
1663*4882a593Smuzhiyun 		case ICM_EVENT_DEVICE_CONNECTED:
1664*4882a593Smuzhiyun 			icm->device_connected(tb, n->pkg);
1665*4882a593Smuzhiyun 			break;
1666*4882a593Smuzhiyun 		case ICM_EVENT_DEVICE_DISCONNECTED:
1667*4882a593Smuzhiyun 			icm->device_disconnected(tb, n->pkg);
1668*4882a593Smuzhiyun 			break;
1669*4882a593Smuzhiyun 		case ICM_EVENT_XDOMAIN_CONNECTED:
1670*4882a593Smuzhiyun 			icm->xdomain_connected(tb, n->pkg);
1671*4882a593Smuzhiyun 			break;
1672*4882a593Smuzhiyun 		case ICM_EVENT_XDOMAIN_DISCONNECTED:
1673*4882a593Smuzhiyun 			icm->xdomain_disconnected(tb, n->pkg);
1674*4882a593Smuzhiyun 			break;
1675*4882a593Smuzhiyun 		case ICM_EVENT_RTD3_VETO:
1676*4882a593Smuzhiyun 			icm->rtd3_veto(tb, n->pkg);
1677*4882a593Smuzhiyun 			break;
1678*4882a593Smuzhiyun 		}
1679*4882a593Smuzhiyun 	}
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 	mutex_unlock(&tb->lock);
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun 	kfree(n->pkg);
1684*4882a593Smuzhiyun 	kfree(n);
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun 
icm_handle_event(struct tb * tb,enum tb_cfg_pkg_type type,const void * buf,size_t size)1687*4882a593Smuzhiyun static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1688*4882a593Smuzhiyun 			     const void *buf, size_t size)
1689*4882a593Smuzhiyun {
1690*4882a593Smuzhiyun 	struct icm_notification *n;
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 	n = kmalloc(sizeof(*n), GFP_KERNEL);
1693*4882a593Smuzhiyun 	if (!n)
1694*4882a593Smuzhiyun 		return;
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun 	INIT_WORK(&n->work, icm_handle_notification);
1697*4882a593Smuzhiyun 	n->pkg = kmemdup(buf, size, GFP_KERNEL);
1698*4882a593Smuzhiyun 	n->tb = tb;
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	queue_work(tb->wq, &n->work);
1701*4882a593Smuzhiyun }
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun static int
__icm_driver_ready(struct tb * tb,enum tb_security_level * security_level,size_t * nboot_acl,bool * rpm)1704*4882a593Smuzhiyun __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level,
1705*4882a593Smuzhiyun 		   size_t *nboot_acl, bool *rpm)
1706*4882a593Smuzhiyun {
1707*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1708*4882a593Smuzhiyun 	unsigned int retries = 50;
1709*4882a593Smuzhiyun 	int ret;
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 	ret = icm->driver_ready(tb, security_level, nboot_acl, rpm);
1712*4882a593Smuzhiyun 	if (ret) {
1713*4882a593Smuzhiyun 		tb_err(tb, "failed to send driver ready to ICM\n");
1714*4882a593Smuzhiyun 		return ret;
1715*4882a593Smuzhiyun 	}
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	/*
1718*4882a593Smuzhiyun 	 * Hold on here until the switch config space is accessible so
1719*4882a593Smuzhiyun 	 * that we can read root switch config successfully.
1720*4882a593Smuzhiyun 	 */
1721*4882a593Smuzhiyun 	do {
1722*4882a593Smuzhiyun 		struct tb_cfg_result res;
1723*4882a593Smuzhiyun 		u32 tmp;
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 		res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
1726*4882a593Smuzhiyun 				      0, 1, 100);
1727*4882a593Smuzhiyun 		if (!res.err)
1728*4882a593Smuzhiyun 			return 0;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 		msleep(50);
1731*4882a593Smuzhiyun 	} while (--retries);
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	tb_err(tb, "failed to read root switch config space, giving up\n");
1734*4882a593Smuzhiyun 	return -ETIMEDOUT;
1735*4882a593Smuzhiyun }
1736*4882a593Smuzhiyun 
icm_firmware_reset(struct tb * tb,struct tb_nhi * nhi)1737*4882a593Smuzhiyun static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
1738*4882a593Smuzhiyun {
1739*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1740*4882a593Smuzhiyun 	u32 val;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 	if (!icm->upstream_port)
1743*4882a593Smuzhiyun 		return -ENODEV;
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 	/* Put ARC to wait for CIO reset event to happen */
1746*4882a593Smuzhiyun 	val = ioread32(nhi->iobase + REG_FW_STS);
1747*4882a593Smuzhiyun 	val |= REG_FW_STS_CIO_RESET_REQ;
1748*4882a593Smuzhiyun 	iowrite32(val, nhi->iobase + REG_FW_STS);
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 	/* Re-start ARC */
1751*4882a593Smuzhiyun 	val = ioread32(nhi->iobase + REG_FW_STS);
1752*4882a593Smuzhiyun 	val |= REG_FW_STS_ICM_EN_INVERT;
1753*4882a593Smuzhiyun 	val |= REG_FW_STS_ICM_EN_CPU;
1754*4882a593Smuzhiyun 	iowrite32(val, nhi->iobase + REG_FW_STS);
1755*4882a593Smuzhiyun 
1756*4882a593Smuzhiyun 	/* Trigger CIO reset now */
1757*4882a593Smuzhiyun 	return icm->cio_reset(tb);
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun 
icm_firmware_start(struct tb * tb,struct tb_nhi * nhi)1760*4882a593Smuzhiyun static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
1761*4882a593Smuzhiyun {
1762*4882a593Smuzhiyun 	unsigned int retries = 10;
1763*4882a593Smuzhiyun 	int ret;
1764*4882a593Smuzhiyun 	u32 val;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	/* Check if the ICM firmware is already running */
1767*4882a593Smuzhiyun 	if (icm_firmware_running(nhi))
1768*4882a593Smuzhiyun 		return 0;
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 	dev_dbg(&nhi->pdev->dev, "starting ICM firmware\n");
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	ret = icm_firmware_reset(tb, nhi);
1773*4882a593Smuzhiyun 	if (ret)
1774*4882a593Smuzhiyun 		return ret;
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	/* Wait until the ICM firmware tells us it is up and running */
1777*4882a593Smuzhiyun 	do {
1778*4882a593Smuzhiyun 		/* Check that the ICM firmware is running */
1779*4882a593Smuzhiyun 		val = ioread32(nhi->iobase + REG_FW_STS);
1780*4882a593Smuzhiyun 		if (val & REG_FW_STS_NVM_AUTH_DONE)
1781*4882a593Smuzhiyun 			return 0;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 		msleep(300);
1784*4882a593Smuzhiyun 	} while (--retries);
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	return -ETIMEDOUT;
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun 
icm_reset_phy_port(struct tb * tb,int phy_port)1789*4882a593Smuzhiyun static int icm_reset_phy_port(struct tb *tb, int phy_port)
1790*4882a593Smuzhiyun {
1791*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1792*4882a593Smuzhiyun 	u32 state0, state1;
1793*4882a593Smuzhiyun 	int port0, port1;
1794*4882a593Smuzhiyun 	u32 val0, val1;
1795*4882a593Smuzhiyun 	int ret;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 	if (!icm->upstream_port)
1798*4882a593Smuzhiyun 		return 0;
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	if (phy_port) {
1801*4882a593Smuzhiyun 		port0 = 3;
1802*4882a593Smuzhiyun 		port1 = 4;
1803*4882a593Smuzhiyun 	} else {
1804*4882a593Smuzhiyun 		port0 = 1;
1805*4882a593Smuzhiyun 		port1 = 2;
1806*4882a593Smuzhiyun 	}
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	/*
1809*4882a593Smuzhiyun 	 * Read link status of both null ports belonging to a single
1810*4882a593Smuzhiyun 	 * physical port.
1811*4882a593Smuzhiyun 	 */
1812*4882a593Smuzhiyun 	ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1813*4882a593Smuzhiyun 	if (ret)
1814*4882a593Smuzhiyun 		return ret;
1815*4882a593Smuzhiyun 	ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1816*4882a593Smuzhiyun 	if (ret)
1817*4882a593Smuzhiyun 		return ret;
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1820*4882a593Smuzhiyun 	state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1821*4882a593Smuzhiyun 	state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1822*4882a593Smuzhiyun 	state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	/* If they are both up we need to reset them now */
1825*4882a593Smuzhiyun 	if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1826*4882a593Smuzhiyun 		return 0;
1827*4882a593Smuzhiyun 
1828*4882a593Smuzhiyun 	val0 |= PHY_PORT_CS1_LINK_DISABLE;
1829*4882a593Smuzhiyun 	ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1830*4882a593Smuzhiyun 	if (ret)
1831*4882a593Smuzhiyun 		return ret;
1832*4882a593Smuzhiyun 
1833*4882a593Smuzhiyun 	val1 |= PHY_PORT_CS1_LINK_DISABLE;
1834*4882a593Smuzhiyun 	ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1835*4882a593Smuzhiyun 	if (ret)
1836*4882a593Smuzhiyun 		return ret;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	/* Wait a bit and then re-enable both ports */
1839*4882a593Smuzhiyun 	usleep_range(10, 100);
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1842*4882a593Smuzhiyun 	if (ret)
1843*4882a593Smuzhiyun 		return ret;
1844*4882a593Smuzhiyun 	ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1845*4882a593Smuzhiyun 	if (ret)
1846*4882a593Smuzhiyun 		return ret;
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1849*4882a593Smuzhiyun 	ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1850*4882a593Smuzhiyun 	if (ret)
1851*4882a593Smuzhiyun 		return ret;
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun 	val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1854*4882a593Smuzhiyun 	return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1855*4882a593Smuzhiyun }
1856*4882a593Smuzhiyun 
icm_firmware_init(struct tb * tb)1857*4882a593Smuzhiyun static int icm_firmware_init(struct tb *tb)
1858*4882a593Smuzhiyun {
1859*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1860*4882a593Smuzhiyun 	struct tb_nhi *nhi = tb->nhi;
1861*4882a593Smuzhiyun 	int ret;
1862*4882a593Smuzhiyun 
1863*4882a593Smuzhiyun 	ret = icm_firmware_start(tb, nhi);
1864*4882a593Smuzhiyun 	if (ret) {
1865*4882a593Smuzhiyun 		dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1866*4882a593Smuzhiyun 		return ret;
1867*4882a593Smuzhiyun 	}
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	if (icm->get_mode) {
1870*4882a593Smuzhiyun 		ret = icm->get_mode(tb);
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 		switch (ret) {
1873*4882a593Smuzhiyun 		case NHI_FW_SAFE_MODE:
1874*4882a593Smuzhiyun 			icm->safe_mode = true;
1875*4882a593Smuzhiyun 			break;
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun 		case NHI_FW_CM_MODE:
1878*4882a593Smuzhiyun 			/* Ask ICM to accept all Thunderbolt devices */
1879*4882a593Smuzhiyun 			nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1880*4882a593Smuzhiyun 			break;
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 		default:
1883*4882a593Smuzhiyun 			if (ret < 0)
1884*4882a593Smuzhiyun 				return ret;
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun 			tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1887*4882a593Smuzhiyun 			return -ENODEV;
1888*4882a593Smuzhiyun 		}
1889*4882a593Smuzhiyun 	}
1890*4882a593Smuzhiyun 
1891*4882a593Smuzhiyun 	/*
1892*4882a593Smuzhiyun 	 * Reset both physical ports if there is anything connected to
1893*4882a593Smuzhiyun 	 * them already.
1894*4882a593Smuzhiyun 	 */
1895*4882a593Smuzhiyun 	ret = icm_reset_phy_port(tb, 0);
1896*4882a593Smuzhiyun 	if (ret)
1897*4882a593Smuzhiyun 		dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1898*4882a593Smuzhiyun 	ret = icm_reset_phy_port(tb, 1);
1899*4882a593Smuzhiyun 	if (ret)
1900*4882a593Smuzhiyun 		dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	return 0;
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun 
icm_driver_ready(struct tb * tb)1905*4882a593Smuzhiyun static int icm_driver_ready(struct tb *tb)
1906*4882a593Smuzhiyun {
1907*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1908*4882a593Smuzhiyun 	int ret;
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun 	ret = icm_firmware_init(tb);
1911*4882a593Smuzhiyun 	if (ret)
1912*4882a593Smuzhiyun 		return ret;
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun 	if (icm->safe_mode) {
1915*4882a593Smuzhiyun 		tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1916*4882a593Smuzhiyun 		tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1917*4882a593Smuzhiyun 		tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1918*4882a593Smuzhiyun 		return 0;
1919*4882a593Smuzhiyun 	}
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun 	ret = __icm_driver_ready(tb, &tb->security_level, &tb->nboot_acl,
1922*4882a593Smuzhiyun 				 &icm->rpm);
1923*4882a593Smuzhiyun 	if (ret)
1924*4882a593Smuzhiyun 		return ret;
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	/*
1927*4882a593Smuzhiyun 	 * Make sure the number of supported preboot ACL matches what we
1928*4882a593Smuzhiyun 	 * expect or disable the whole feature.
1929*4882a593Smuzhiyun 	 */
1930*4882a593Smuzhiyun 	if (tb->nboot_acl > icm->max_boot_acl)
1931*4882a593Smuzhiyun 		tb->nboot_acl = 0;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	return 0;
1934*4882a593Smuzhiyun }
1935*4882a593Smuzhiyun 
icm_suspend(struct tb * tb)1936*4882a593Smuzhiyun static int icm_suspend(struct tb *tb)
1937*4882a593Smuzhiyun {
1938*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun 	if (icm->save_devices)
1941*4882a593Smuzhiyun 		icm->save_devices(tb);
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1944*4882a593Smuzhiyun 	return 0;
1945*4882a593Smuzhiyun }
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun /*
1948*4882a593Smuzhiyun  * Mark all switches (except root switch) below this one unplugged. ICM
1949*4882a593Smuzhiyun  * firmware will send us an updated list of switches after we have send
1950*4882a593Smuzhiyun  * it driver ready command. If a switch is not in that list it will be
1951*4882a593Smuzhiyun  * removed when we perform rescan.
1952*4882a593Smuzhiyun  */
icm_unplug_children(struct tb_switch * sw)1953*4882a593Smuzhiyun static void icm_unplug_children(struct tb_switch *sw)
1954*4882a593Smuzhiyun {
1955*4882a593Smuzhiyun 	struct tb_port *port;
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 	if (tb_route(sw))
1958*4882a593Smuzhiyun 		sw->is_unplugged = true;
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	tb_switch_for_each_port(sw, port) {
1961*4882a593Smuzhiyun 		if (port->xdomain)
1962*4882a593Smuzhiyun 			port->xdomain->is_unplugged = true;
1963*4882a593Smuzhiyun 		else if (tb_port_has_remote(port))
1964*4882a593Smuzhiyun 			icm_unplug_children(port->remote->sw);
1965*4882a593Smuzhiyun 	}
1966*4882a593Smuzhiyun }
1967*4882a593Smuzhiyun 
complete_rpm(struct device * dev,void * data)1968*4882a593Smuzhiyun static int complete_rpm(struct device *dev, void *data)
1969*4882a593Smuzhiyun {
1970*4882a593Smuzhiyun 	struct tb_switch *sw = tb_to_switch(dev);
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun 	if (sw)
1973*4882a593Smuzhiyun 		complete(&sw->rpm_complete);
1974*4882a593Smuzhiyun 	return 0;
1975*4882a593Smuzhiyun }
1976*4882a593Smuzhiyun 
remove_unplugged_switch(struct tb_switch * sw)1977*4882a593Smuzhiyun static void remove_unplugged_switch(struct tb_switch *sw)
1978*4882a593Smuzhiyun {
1979*4882a593Smuzhiyun 	struct device *parent = get_device(sw->dev.parent);
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	pm_runtime_get_sync(parent);
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	/*
1984*4882a593Smuzhiyun 	 * Signal this and switches below for rpm_complete because
1985*4882a593Smuzhiyun 	 * tb_switch_remove() calls pm_runtime_get_sync() that then waits
1986*4882a593Smuzhiyun 	 * for it.
1987*4882a593Smuzhiyun 	 */
1988*4882a593Smuzhiyun 	complete_rpm(&sw->dev, NULL);
1989*4882a593Smuzhiyun 	bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
1990*4882a593Smuzhiyun 	tb_switch_remove(sw);
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(parent);
1993*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(parent);
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun 	put_device(parent);
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun 
icm_free_unplugged_children(struct tb_switch * sw)1998*4882a593Smuzhiyun static void icm_free_unplugged_children(struct tb_switch *sw)
1999*4882a593Smuzhiyun {
2000*4882a593Smuzhiyun 	struct tb_port *port;
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 	tb_switch_for_each_port(sw, port) {
2003*4882a593Smuzhiyun 		if (port->xdomain && port->xdomain->is_unplugged) {
2004*4882a593Smuzhiyun 			tb_xdomain_remove(port->xdomain);
2005*4882a593Smuzhiyun 			port->xdomain = NULL;
2006*4882a593Smuzhiyun 		} else if (tb_port_has_remote(port)) {
2007*4882a593Smuzhiyun 			if (port->remote->sw->is_unplugged) {
2008*4882a593Smuzhiyun 				remove_unplugged_switch(port->remote->sw);
2009*4882a593Smuzhiyun 				port->remote = NULL;
2010*4882a593Smuzhiyun 			} else {
2011*4882a593Smuzhiyun 				icm_free_unplugged_children(port->remote->sw);
2012*4882a593Smuzhiyun 			}
2013*4882a593Smuzhiyun 		}
2014*4882a593Smuzhiyun 	}
2015*4882a593Smuzhiyun }
2016*4882a593Smuzhiyun 
icm_rescan_work(struct work_struct * work)2017*4882a593Smuzhiyun static void icm_rescan_work(struct work_struct *work)
2018*4882a593Smuzhiyun {
2019*4882a593Smuzhiyun 	struct icm *icm = container_of(work, struct icm, rescan_work.work);
2020*4882a593Smuzhiyun 	struct tb *tb = icm_to_tb(icm);
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun 	mutex_lock(&tb->lock);
2023*4882a593Smuzhiyun 	if (tb->root_switch)
2024*4882a593Smuzhiyun 		icm_free_unplugged_children(tb->root_switch);
2025*4882a593Smuzhiyun 	mutex_unlock(&tb->lock);
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun 
icm_complete(struct tb * tb)2028*4882a593Smuzhiyun static void icm_complete(struct tb *tb)
2029*4882a593Smuzhiyun {
2030*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
2031*4882a593Smuzhiyun 
2032*4882a593Smuzhiyun 	if (tb->nhi->going_away)
2033*4882a593Smuzhiyun 		return;
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	/*
2036*4882a593Smuzhiyun 	 * If RTD3 was vetoed before we entered system suspend allow it
2037*4882a593Smuzhiyun 	 * again now before driver ready is sent. Firmware sends a new RTD3
2038*4882a593Smuzhiyun 	 * veto if it is still the case after we have sent it driver ready
2039*4882a593Smuzhiyun 	 * command.
2040*4882a593Smuzhiyun 	 */
2041*4882a593Smuzhiyun 	icm_veto_end(tb);
2042*4882a593Smuzhiyun 	icm_unplug_children(tb->root_switch);
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	/*
2045*4882a593Smuzhiyun 	 * Now all existing children should be resumed, start events
2046*4882a593Smuzhiyun 	 * from ICM to get updated status.
2047*4882a593Smuzhiyun 	 */
2048*4882a593Smuzhiyun 	__icm_driver_ready(tb, NULL, NULL, NULL);
2049*4882a593Smuzhiyun 
2050*4882a593Smuzhiyun 	/*
2051*4882a593Smuzhiyun 	 * We do not get notifications of devices that have been
2052*4882a593Smuzhiyun 	 * unplugged during suspend so schedule rescan to clean them up
2053*4882a593Smuzhiyun 	 * if any.
2054*4882a593Smuzhiyun 	 */
2055*4882a593Smuzhiyun 	queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun 
icm_runtime_suspend(struct tb * tb)2058*4882a593Smuzhiyun static int icm_runtime_suspend(struct tb *tb)
2059*4882a593Smuzhiyun {
2060*4882a593Smuzhiyun 	nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
2061*4882a593Smuzhiyun 	return 0;
2062*4882a593Smuzhiyun }
2063*4882a593Smuzhiyun 
icm_runtime_suspend_switch(struct tb_switch * sw)2064*4882a593Smuzhiyun static int icm_runtime_suspend_switch(struct tb_switch *sw)
2065*4882a593Smuzhiyun {
2066*4882a593Smuzhiyun 	if (tb_route(sw))
2067*4882a593Smuzhiyun 		reinit_completion(&sw->rpm_complete);
2068*4882a593Smuzhiyun 	return 0;
2069*4882a593Smuzhiyun }
2070*4882a593Smuzhiyun 
icm_runtime_resume_switch(struct tb_switch * sw)2071*4882a593Smuzhiyun static int icm_runtime_resume_switch(struct tb_switch *sw)
2072*4882a593Smuzhiyun {
2073*4882a593Smuzhiyun 	if (tb_route(sw)) {
2074*4882a593Smuzhiyun 		if (!wait_for_completion_timeout(&sw->rpm_complete,
2075*4882a593Smuzhiyun 						 msecs_to_jiffies(500))) {
2076*4882a593Smuzhiyun 			dev_dbg(&sw->dev, "runtime resuming timed out\n");
2077*4882a593Smuzhiyun 		}
2078*4882a593Smuzhiyun 	}
2079*4882a593Smuzhiyun 	return 0;
2080*4882a593Smuzhiyun }
2081*4882a593Smuzhiyun 
icm_runtime_resume(struct tb * tb)2082*4882a593Smuzhiyun static int icm_runtime_resume(struct tb *tb)
2083*4882a593Smuzhiyun {
2084*4882a593Smuzhiyun 	/*
2085*4882a593Smuzhiyun 	 * We can reuse the same resume functionality than with system
2086*4882a593Smuzhiyun 	 * suspend.
2087*4882a593Smuzhiyun 	 */
2088*4882a593Smuzhiyun 	icm_complete(tb);
2089*4882a593Smuzhiyun 	return 0;
2090*4882a593Smuzhiyun }
2091*4882a593Smuzhiyun 
icm_start(struct tb * tb)2092*4882a593Smuzhiyun static int icm_start(struct tb *tb)
2093*4882a593Smuzhiyun {
2094*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
2095*4882a593Smuzhiyun 	int ret;
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	if (icm->safe_mode)
2098*4882a593Smuzhiyun 		tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
2099*4882a593Smuzhiyun 	else
2100*4882a593Smuzhiyun 		tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
2101*4882a593Smuzhiyun 	if (IS_ERR(tb->root_switch))
2102*4882a593Smuzhiyun 		return PTR_ERR(tb->root_switch);
2103*4882a593Smuzhiyun 
2104*4882a593Smuzhiyun 	tb->root_switch->no_nvm_upgrade = !icm->can_upgrade_nvm;
2105*4882a593Smuzhiyun 	tb->root_switch->rpm = icm->rpm;
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	if (icm->set_uuid)
2108*4882a593Smuzhiyun 		icm->set_uuid(tb);
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	ret = tb_switch_add(tb->root_switch);
2111*4882a593Smuzhiyun 	if (ret) {
2112*4882a593Smuzhiyun 		tb_switch_put(tb->root_switch);
2113*4882a593Smuzhiyun 		tb->root_switch = NULL;
2114*4882a593Smuzhiyun 	}
2115*4882a593Smuzhiyun 
2116*4882a593Smuzhiyun 	return ret;
2117*4882a593Smuzhiyun }
2118*4882a593Smuzhiyun 
icm_stop(struct tb * tb)2119*4882a593Smuzhiyun static void icm_stop(struct tb *tb)
2120*4882a593Smuzhiyun {
2121*4882a593Smuzhiyun 	struct icm *icm = tb_priv(tb);
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	cancel_delayed_work(&icm->rescan_work);
2124*4882a593Smuzhiyun 	tb_switch_remove(tb->root_switch);
2125*4882a593Smuzhiyun 	tb->root_switch = NULL;
2126*4882a593Smuzhiyun 	nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
2127*4882a593Smuzhiyun }
2128*4882a593Smuzhiyun 
icm_disconnect_pcie_paths(struct tb * tb)2129*4882a593Smuzhiyun static int icm_disconnect_pcie_paths(struct tb *tb)
2130*4882a593Smuzhiyun {
2131*4882a593Smuzhiyun 	return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
2132*4882a593Smuzhiyun }
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun /* Falcon Ridge */
2135*4882a593Smuzhiyun static const struct tb_cm_ops icm_fr_ops = {
2136*4882a593Smuzhiyun 	.driver_ready = icm_driver_ready,
2137*4882a593Smuzhiyun 	.start = icm_start,
2138*4882a593Smuzhiyun 	.stop = icm_stop,
2139*4882a593Smuzhiyun 	.suspend = icm_suspend,
2140*4882a593Smuzhiyun 	.complete = icm_complete,
2141*4882a593Smuzhiyun 	.handle_event = icm_handle_event,
2142*4882a593Smuzhiyun 	.approve_switch = icm_fr_approve_switch,
2143*4882a593Smuzhiyun 	.add_switch_key = icm_fr_add_switch_key,
2144*4882a593Smuzhiyun 	.challenge_switch_key = icm_fr_challenge_switch_key,
2145*4882a593Smuzhiyun 	.disconnect_pcie_paths = icm_disconnect_pcie_paths,
2146*4882a593Smuzhiyun 	.approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2147*4882a593Smuzhiyun 	.disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
2148*4882a593Smuzhiyun };
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun /* Alpine Ridge */
2151*4882a593Smuzhiyun static const struct tb_cm_ops icm_ar_ops = {
2152*4882a593Smuzhiyun 	.driver_ready = icm_driver_ready,
2153*4882a593Smuzhiyun 	.start = icm_start,
2154*4882a593Smuzhiyun 	.stop = icm_stop,
2155*4882a593Smuzhiyun 	.suspend = icm_suspend,
2156*4882a593Smuzhiyun 	.complete = icm_complete,
2157*4882a593Smuzhiyun 	.runtime_suspend = icm_runtime_suspend,
2158*4882a593Smuzhiyun 	.runtime_resume = icm_runtime_resume,
2159*4882a593Smuzhiyun 	.runtime_suspend_switch = icm_runtime_suspend_switch,
2160*4882a593Smuzhiyun 	.runtime_resume_switch = icm_runtime_resume_switch,
2161*4882a593Smuzhiyun 	.handle_event = icm_handle_event,
2162*4882a593Smuzhiyun 	.get_boot_acl = icm_ar_get_boot_acl,
2163*4882a593Smuzhiyun 	.set_boot_acl = icm_ar_set_boot_acl,
2164*4882a593Smuzhiyun 	.approve_switch = icm_fr_approve_switch,
2165*4882a593Smuzhiyun 	.add_switch_key = icm_fr_add_switch_key,
2166*4882a593Smuzhiyun 	.challenge_switch_key = icm_fr_challenge_switch_key,
2167*4882a593Smuzhiyun 	.disconnect_pcie_paths = icm_disconnect_pcie_paths,
2168*4882a593Smuzhiyun 	.approve_xdomain_paths = icm_fr_approve_xdomain_paths,
2169*4882a593Smuzhiyun 	.disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
2170*4882a593Smuzhiyun };
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun /* Titan Ridge */
2173*4882a593Smuzhiyun static const struct tb_cm_ops icm_tr_ops = {
2174*4882a593Smuzhiyun 	.driver_ready = icm_driver_ready,
2175*4882a593Smuzhiyun 	.start = icm_start,
2176*4882a593Smuzhiyun 	.stop = icm_stop,
2177*4882a593Smuzhiyun 	.suspend = icm_suspend,
2178*4882a593Smuzhiyun 	.complete = icm_complete,
2179*4882a593Smuzhiyun 	.runtime_suspend = icm_runtime_suspend,
2180*4882a593Smuzhiyun 	.runtime_resume = icm_runtime_resume,
2181*4882a593Smuzhiyun 	.runtime_suspend_switch = icm_runtime_suspend_switch,
2182*4882a593Smuzhiyun 	.runtime_resume_switch = icm_runtime_resume_switch,
2183*4882a593Smuzhiyun 	.handle_event = icm_handle_event,
2184*4882a593Smuzhiyun 	.get_boot_acl = icm_ar_get_boot_acl,
2185*4882a593Smuzhiyun 	.set_boot_acl = icm_ar_set_boot_acl,
2186*4882a593Smuzhiyun 	.approve_switch = icm_tr_approve_switch,
2187*4882a593Smuzhiyun 	.add_switch_key = icm_tr_add_switch_key,
2188*4882a593Smuzhiyun 	.challenge_switch_key = icm_tr_challenge_switch_key,
2189*4882a593Smuzhiyun 	.disconnect_pcie_paths = icm_disconnect_pcie_paths,
2190*4882a593Smuzhiyun 	.approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2191*4882a593Smuzhiyun 	.disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
2192*4882a593Smuzhiyun };
2193*4882a593Smuzhiyun 
2194*4882a593Smuzhiyun /* Ice Lake */
2195*4882a593Smuzhiyun static const struct tb_cm_ops icm_icl_ops = {
2196*4882a593Smuzhiyun 	.driver_ready = icm_driver_ready,
2197*4882a593Smuzhiyun 	.start = icm_start,
2198*4882a593Smuzhiyun 	.stop = icm_stop,
2199*4882a593Smuzhiyun 	.complete = icm_complete,
2200*4882a593Smuzhiyun 	.runtime_suspend = icm_runtime_suspend,
2201*4882a593Smuzhiyun 	.runtime_resume = icm_runtime_resume,
2202*4882a593Smuzhiyun 	.handle_event = icm_handle_event,
2203*4882a593Smuzhiyun 	.approve_xdomain_paths = icm_tr_approve_xdomain_paths,
2204*4882a593Smuzhiyun 	.disconnect_xdomain_paths = icm_tr_disconnect_xdomain_paths,
2205*4882a593Smuzhiyun };
2206*4882a593Smuzhiyun 
icm_probe(struct tb_nhi * nhi)2207*4882a593Smuzhiyun struct tb *icm_probe(struct tb_nhi *nhi)
2208*4882a593Smuzhiyun {
2209*4882a593Smuzhiyun 	struct icm *icm;
2210*4882a593Smuzhiyun 	struct tb *tb;
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	tb = tb_domain_alloc(nhi, sizeof(struct icm));
2213*4882a593Smuzhiyun 	if (!tb)
2214*4882a593Smuzhiyun 		return NULL;
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	icm = tb_priv(tb);
2217*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
2218*4882a593Smuzhiyun 	mutex_init(&icm->request_lock);
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun 	switch (nhi->pdev->device) {
2221*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2222*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
2223*4882a593Smuzhiyun 		icm->can_upgrade_nvm = true;
2224*4882a593Smuzhiyun 		icm->is_supported = icm_fr_is_supported;
2225*4882a593Smuzhiyun 		icm->get_route = icm_fr_get_route;
2226*4882a593Smuzhiyun 		icm->save_devices = icm_fr_save_devices;
2227*4882a593Smuzhiyun 		icm->driver_ready = icm_fr_driver_ready;
2228*4882a593Smuzhiyun 		icm->device_connected = icm_fr_device_connected;
2229*4882a593Smuzhiyun 		icm->device_disconnected = icm_fr_device_disconnected;
2230*4882a593Smuzhiyun 		icm->xdomain_connected = icm_fr_xdomain_connected;
2231*4882a593Smuzhiyun 		icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
2232*4882a593Smuzhiyun 		tb->cm_ops = &icm_fr_ops;
2233*4882a593Smuzhiyun 		break;
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
2236*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
2237*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
2238*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
2239*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
2240*4882a593Smuzhiyun 		icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2241*4882a593Smuzhiyun 		/*
2242*4882a593Smuzhiyun 		 * NVM upgrade has not been tested on Apple systems and
2243*4882a593Smuzhiyun 		 * they don't provide images publicly either. To be on
2244*4882a593Smuzhiyun 		 * the safe side prevent root switch NVM upgrade on Macs
2245*4882a593Smuzhiyun 		 * for now.
2246*4882a593Smuzhiyun 		 */
2247*4882a593Smuzhiyun 		icm->can_upgrade_nvm = !x86_apple_machine;
2248*4882a593Smuzhiyun 		icm->is_supported = icm_ar_is_supported;
2249*4882a593Smuzhiyun 		icm->cio_reset = icm_ar_cio_reset;
2250*4882a593Smuzhiyun 		icm->get_mode = icm_ar_get_mode;
2251*4882a593Smuzhiyun 		icm->get_route = icm_ar_get_route;
2252*4882a593Smuzhiyun 		icm->save_devices = icm_fr_save_devices;
2253*4882a593Smuzhiyun 		icm->driver_ready = icm_ar_driver_ready;
2254*4882a593Smuzhiyun 		icm->device_connected = icm_fr_device_connected;
2255*4882a593Smuzhiyun 		icm->device_disconnected = icm_fr_device_disconnected;
2256*4882a593Smuzhiyun 		icm->xdomain_connected = icm_fr_xdomain_connected;
2257*4882a593Smuzhiyun 		icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
2258*4882a593Smuzhiyun 		tb->cm_ops = &icm_ar_ops;
2259*4882a593Smuzhiyun 		break;
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI:
2262*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI:
2263*4882a593Smuzhiyun 		icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES;
2264*4882a593Smuzhiyun 		icm->can_upgrade_nvm = !x86_apple_machine;
2265*4882a593Smuzhiyun 		icm->is_supported = icm_ar_is_supported;
2266*4882a593Smuzhiyun 		icm->cio_reset = icm_tr_cio_reset;
2267*4882a593Smuzhiyun 		icm->get_mode = icm_ar_get_mode;
2268*4882a593Smuzhiyun 		icm->driver_ready = icm_tr_driver_ready;
2269*4882a593Smuzhiyun 		icm->device_connected = icm_tr_device_connected;
2270*4882a593Smuzhiyun 		icm->device_disconnected = icm_tr_device_disconnected;
2271*4882a593Smuzhiyun 		icm->xdomain_connected = icm_tr_xdomain_connected;
2272*4882a593Smuzhiyun 		icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2273*4882a593Smuzhiyun 		tb->cm_ops = &icm_tr_ops;
2274*4882a593Smuzhiyun 		break;
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ICL_NHI0:
2277*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_ICL_NHI1:
2278*4882a593Smuzhiyun 		icm->is_supported = icm_fr_is_supported;
2279*4882a593Smuzhiyun 		icm->driver_ready = icm_icl_driver_ready;
2280*4882a593Smuzhiyun 		icm->set_uuid = icm_icl_set_uuid;
2281*4882a593Smuzhiyun 		icm->device_connected = icm_icl_device_connected;
2282*4882a593Smuzhiyun 		icm->device_disconnected = icm_tr_device_disconnected;
2283*4882a593Smuzhiyun 		icm->xdomain_connected = icm_tr_xdomain_connected;
2284*4882a593Smuzhiyun 		icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2285*4882a593Smuzhiyun 		icm->rtd3_veto = icm_icl_rtd3_veto;
2286*4882a593Smuzhiyun 		tb->cm_ops = &icm_icl_ops;
2287*4882a593Smuzhiyun 		break;
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TGL_NHI0:
2290*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TGL_NHI1:
2291*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
2292*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
2293*4882a593Smuzhiyun 		icm->is_supported = icm_tgl_is_supported;
2294*4882a593Smuzhiyun 		icm->driver_ready = icm_icl_driver_ready;
2295*4882a593Smuzhiyun 		icm->set_uuid = icm_icl_set_uuid;
2296*4882a593Smuzhiyun 		icm->device_connected = icm_icl_device_connected;
2297*4882a593Smuzhiyun 		icm->device_disconnected = icm_tr_device_disconnected;
2298*4882a593Smuzhiyun 		icm->xdomain_connected = icm_tr_xdomain_connected;
2299*4882a593Smuzhiyun 		icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2300*4882a593Smuzhiyun 		icm->rtd3_veto = icm_icl_rtd3_veto;
2301*4882a593Smuzhiyun 		tb->cm_ops = &icm_icl_ops;
2302*4882a593Smuzhiyun 		break;
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_2C_NHI:
2305*4882a593Smuzhiyun 	case PCI_DEVICE_ID_INTEL_MAPLE_RIDGE_4C_NHI:
2306*4882a593Smuzhiyun 		icm->is_supported = icm_tgl_is_supported;
2307*4882a593Smuzhiyun 		icm->get_mode = icm_ar_get_mode;
2308*4882a593Smuzhiyun 		icm->driver_ready = icm_tr_driver_ready;
2309*4882a593Smuzhiyun 		icm->device_connected = icm_tr_device_connected;
2310*4882a593Smuzhiyun 		icm->device_disconnected = icm_tr_device_disconnected;
2311*4882a593Smuzhiyun 		icm->xdomain_connected = icm_tr_xdomain_connected;
2312*4882a593Smuzhiyun 		icm->xdomain_disconnected = icm_tr_xdomain_disconnected;
2313*4882a593Smuzhiyun 		tb->cm_ops = &icm_tr_ops;
2314*4882a593Smuzhiyun 		break;
2315*4882a593Smuzhiyun 	}
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun 	if (!icm->is_supported || !icm->is_supported(tb)) {
2318*4882a593Smuzhiyun 		dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
2319*4882a593Smuzhiyun 		tb_domain_put(tb);
2320*4882a593Smuzhiyun 		return NULL;
2321*4882a593Smuzhiyun 	}
2322*4882a593Smuzhiyun 
2323*4882a593Smuzhiyun 	return tb;
2324*4882a593Smuzhiyun }
2325