xref: /openbmc/linux/drivers/thunderbolt/icm.c (revision ee487dd2)
1 /*
2  * Internal Thunderbolt Connection Manager. This is a firmware running on
3  * the Thunderbolt host controller performing most of the low-level
4  * handling.
5  *
6  * Copyright (C) 2017, Intel Corporation
7  * Authors: Michael Jamet <michael.jamet@intel.com>
8  *          Mika Westerberg <mika.westerberg@linux.intel.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/mutex.h>
17 #include <linux/pci.h>
18 #include <linux/platform_data/x86/apple.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
21 #include <linux/workqueue.h>
22 
23 #include "ctl.h"
24 #include "nhi_regs.h"
25 #include "tb.h"
26 
27 #define PCIE2CIO_CMD			0x30
28 #define PCIE2CIO_CMD_TIMEOUT		BIT(31)
29 #define PCIE2CIO_CMD_START		BIT(30)
30 #define PCIE2CIO_CMD_WRITE		BIT(21)
31 #define PCIE2CIO_CMD_CS_MASK		GENMASK(20, 19)
32 #define PCIE2CIO_CMD_CS_SHIFT		19
33 #define PCIE2CIO_CMD_PORT_MASK		GENMASK(18, 13)
34 #define PCIE2CIO_CMD_PORT_SHIFT		13
35 
36 #define PCIE2CIO_WRDATA			0x34
37 #define PCIE2CIO_RDDATA			0x38
38 
39 #define PHY_PORT_CS1			0x37
40 #define PHY_PORT_CS1_LINK_DISABLE	BIT(14)
41 #define PHY_PORT_CS1_LINK_STATE_MASK	GENMASK(29, 26)
42 #define PHY_PORT_CS1_LINK_STATE_SHIFT	26
43 
44 #define ICM_TIMEOUT			5000 /* ms */
45 #define ICM_MAX_LINK			4
46 #define ICM_MAX_DEPTH			6
47 
48 /**
49  * struct icm - Internal connection manager private data
50  * @request_lock: Makes sure only one message is send to ICM at time
51  * @rescan_work: Work used to rescan the surviving switches after resume
52  * @upstream_port: Pointer to the PCIe upstream port this host
53  *		   controller is connected. This is only set for systems
54  *		   where ICM needs to be started manually
55  * @vnd_cap: Vendor defined capability where PCIe2CIO mailbox resides
56  *	     (only set when @upstream_port is not %NULL)
57  * @safe_mode: ICM is in safe mode
58  * @is_supported: Checks if we can support ICM on this controller
59  * @get_mode: Read and return the ICM firmware mode (optional)
60  * @get_route: Find a route string for given switch
61  * @device_connected: Handle device connected ICM message
62  * @device_disconnected: Handle device disconnected ICM message
63  * @xdomain_connected - Handle XDomain connected ICM message
64  * @xdomain_disconnected - Handle XDomain disconnected ICM message
65  */
66 struct icm {
67 	struct mutex request_lock;
68 	struct delayed_work rescan_work;
69 	struct pci_dev *upstream_port;
70 	int vnd_cap;
71 	bool safe_mode;
72 	bool (*is_supported)(struct tb *tb);
73 	int (*get_mode)(struct tb *tb);
74 	int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route);
75 	void (*device_connected)(struct tb *tb,
76 				 const struct icm_pkg_header *hdr);
77 	void (*device_disconnected)(struct tb *tb,
78 				    const struct icm_pkg_header *hdr);
79 	void (*xdomain_connected)(struct tb *tb,
80 				  const struct icm_pkg_header *hdr);
81 	void (*xdomain_disconnected)(struct tb *tb,
82 				     const struct icm_pkg_header *hdr);
83 };
84 
85 struct icm_notification {
86 	struct work_struct work;
87 	struct icm_pkg_header *pkg;
88 	struct tb *tb;
89 };
90 
91 static inline struct tb *icm_to_tb(struct icm *icm)
92 {
93 	return ((void *)icm - sizeof(struct tb));
94 }
95 
96 static inline u8 phy_port_from_route(u64 route, u8 depth)
97 {
98 	u8 link;
99 
100 	link = depth ? route >> ((depth - 1) * 8) : route;
101 	return tb_phy_port_from_link(link);
102 }
103 
104 static inline u8 dual_link_from_link(u8 link)
105 {
106 	return link ? ((link - 1) ^ 0x01) + 1 : 0;
107 }
108 
109 static inline u64 get_route(u32 route_hi, u32 route_lo)
110 {
111 	return (u64)route_hi << 32 | route_lo;
112 }
113 
114 static bool icm_match(const struct tb_cfg_request *req,
115 		      const struct ctl_pkg *pkg)
116 {
117 	const struct icm_pkg_header *res_hdr = pkg->buffer;
118 	const struct icm_pkg_header *req_hdr = req->request;
119 
120 	if (pkg->frame.eof != req->response_type)
121 		return false;
122 	if (res_hdr->code != req_hdr->code)
123 		return false;
124 
125 	return true;
126 }
127 
128 static bool icm_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
129 {
130 	const struct icm_pkg_header *hdr = pkg->buffer;
131 
132 	if (hdr->packet_id < req->npackets) {
133 		size_t offset = hdr->packet_id * req->response_size;
134 
135 		memcpy(req->response + offset, pkg->buffer, req->response_size);
136 	}
137 
138 	return hdr->packet_id == hdr->total_packets - 1;
139 }
140 
141 static int icm_request(struct tb *tb, const void *request, size_t request_size,
142 		       void *response, size_t response_size, size_t npackets,
143 		       unsigned int timeout_msec)
144 {
145 	struct icm *icm = tb_priv(tb);
146 	int retries = 3;
147 
148 	do {
149 		struct tb_cfg_request *req;
150 		struct tb_cfg_result res;
151 
152 		req = tb_cfg_request_alloc();
153 		if (!req)
154 			return -ENOMEM;
155 
156 		req->match = icm_match;
157 		req->copy = icm_copy;
158 		req->request = request;
159 		req->request_size = request_size;
160 		req->request_type = TB_CFG_PKG_ICM_CMD;
161 		req->response = response;
162 		req->npackets = npackets;
163 		req->response_size = response_size;
164 		req->response_type = TB_CFG_PKG_ICM_RESP;
165 
166 		mutex_lock(&icm->request_lock);
167 		res = tb_cfg_request_sync(tb->ctl, req, timeout_msec);
168 		mutex_unlock(&icm->request_lock);
169 
170 		tb_cfg_request_put(req);
171 
172 		if (res.err != -ETIMEDOUT)
173 			return res.err == 1 ? -EIO : res.err;
174 
175 		usleep_range(20, 50);
176 	} while (retries--);
177 
178 	return -ETIMEDOUT;
179 }
180 
181 static bool icm_fr_is_supported(struct tb *tb)
182 {
183 	return !x86_apple_machine;
184 }
185 
186 static inline int icm_fr_get_switch_index(u32 port)
187 {
188 	int index;
189 
190 	if ((port & ICM_PORT_TYPE_MASK) != TB_TYPE_PORT)
191 		return 0;
192 
193 	index = port >> ICM_PORT_INDEX_SHIFT;
194 	return index != 0xff ? index : 0;
195 }
196 
197 static int icm_fr_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
198 {
199 	struct icm_fr_pkg_get_topology_response *switches, *sw;
200 	struct icm_fr_pkg_get_topology request = {
201 		.hdr = { .code = ICM_GET_TOPOLOGY },
202 	};
203 	size_t npackets = ICM_GET_TOPOLOGY_PACKETS;
204 	int ret, index;
205 	u8 i;
206 
207 	switches = kcalloc(npackets, sizeof(*switches), GFP_KERNEL);
208 	if (!switches)
209 		return -ENOMEM;
210 
211 	ret = icm_request(tb, &request, sizeof(request), switches,
212 			  sizeof(*switches), npackets, ICM_TIMEOUT);
213 	if (ret)
214 		goto err_free;
215 
216 	sw = &switches[0];
217 	index = icm_fr_get_switch_index(sw->ports[link]);
218 	if (!index) {
219 		ret = -ENODEV;
220 		goto err_free;
221 	}
222 
223 	sw = &switches[index];
224 	for (i = 1; i < depth; i++) {
225 		unsigned int j;
226 
227 		if (!(sw->first_data & ICM_SWITCH_USED)) {
228 			ret = -ENODEV;
229 			goto err_free;
230 		}
231 
232 		for (j = 0; j < ARRAY_SIZE(sw->ports); j++) {
233 			index = icm_fr_get_switch_index(sw->ports[j]);
234 			if (index > sw->switch_index) {
235 				sw = &switches[index];
236 				break;
237 			}
238 		}
239 	}
240 
241 	*route = get_route(sw->route_hi, sw->route_lo);
242 
243 err_free:
244 	kfree(switches);
245 	return ret;
246 }
247 
248 static int icm_fr_approve_switch(struct tb *tb, struct tb_switch *sw)
249 {
250 	struct icm_fr_pkg_approve_device request;
251 	struct icm_fr_pkg_approve_device reply;
252 	int ret;
253 
254 	memset(&request, 0, sizeof(request));
255 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
256 	request.hdr.code = ICM_APPROVE_DEVICE;
257 	request.connection_id = sw->connection_id;
258 	request.connection_key = sw->connection_key;
259 
260 	memset(&reply, 0, sizeof(reply));
261 	/* Use larger timeout as establishing tunnels can take some time */
262 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
263 			  1, 10000);
264 	if (ret)
265 		return ret;
266 
267 	if (reply.hdr.flags & ICM_FLAGS_ERROR) {
268 		tb_warn(tb, "PCIe tunnel creation failed\n");
269 		return -EIO;
270 	}
271 
272 	return 0;
273 }
274 
275 static int icm_fr_add_switch_key(struct tb *tb, struct tb_switch *sw)
276 {
277 	struct icm_fr_pkg_add_device_key request;
278 	struct icm_fr_pkg_add_device_key_response reply;
279 	int ret;
280 
281 	memset(&request, 0, sizeof(request));
282 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
283 	request.hdr.code = ICM_ADD_DEVICE_KEY;
284 	request.connection_id = sw->connection_id;
285 	request.connection_key = sw->connection_key;
286 	memcpy(request.key, sw->key, TB_SWITCH_KEY_SIZE);
287 
288 	memset(&reply, 0, sizeof(reply));
289 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
290 			  1, ICM_TIMEOUT);
291 	if (ret)
292 		return ret;
293 
294 	if (reply.hdr.flags & ICM_FLAGS_ERROR) {
295 		tb_warn(tb, "Adding key to switch failed\n");
296 		return -EIO;
297 	}
298 
299 	return 0;
300 }
301 
302 static int icm_fr_challenge_switch_key(struct tb *tb, struct tb_switch *sw,
303 				       const u8 *challenge, u8 *response)
304 {
305 	struct icm_fr_pkg_challenge_device request;
306 	struct icm_fr_pkg_challenge_device_response reply;
307 	int ret;
308 
309 	memset(&request, 0, sizeof(request));
310 	memcpy(&request.ep_uuid, sw->uuid, sizeof(request.ep_uuid));
311 	request.hdr.code = ICM_CHALLENGE_DEVICE;
312 	request.connection_id = sw->connection_id;
313 	request.connection_key = sw->connection_key;
314 	memcpy(request.challenge, challenge, TB_SWITCH_KEY_SIZE);
315 
316 	memset(&reply, 0, sizeof(reply));
317 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
318 			  1, ICM_TIMEOUT);
319 	if (ret)
320 		return ret;
321 
322 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
323 		return -EKEYREJECTED;
324 	if (reply.hdr.flags & ICM_FLAGS_NO_KEY)
325 		return -ENOKEY;
326 
327 	memcpy(response, reply.response, TB_SWITCH_KEY_SIZE);
328 
329 	return 0;
330 }
331 
332 static int icm_fr_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
333 {
334 	struct icm_fr_pkg_approve_xdomain_response reply;
335 	struct icm_fr_pkg_approve_xdomain request;
336 	int ret;
337 
338 	memset(&request, 0, sizeof(request));
339 	request.hdr.code = ICM_APPROVE_XDOMAIN;
340 	request.link_info = xd->depth << ICM_LINK_INFO_DEPTH_SHIFT | xd->link;
341 	memcpy(&request.remote_uuid, xd->remote_uuid, sizeof(*xd->remote_uuid));
342 
343 	request.transmit_path = xd->transmit_path;
344 	request.transmit_ring = xd->transmit_ring;
345 	request.receive_path = xd->receive_path;
346 	request.receive_ring = xd->receive_ring;
347 
348 	memset(&reply, 0, sizeof(reply));
349 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
350 			  1, ICM_TIMEOUT);
351 	if (ret)
352 		return ret;
353 
354 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
355 		return -EIO;
356 
357 	return 0;
358 }
359 
360 static int icm_fr_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
361 {
362 	u8 phy_port;
363 	u8 cmd;
364 
365 	phy_port = tb_phy_port_from_link(xd->link);
366 	if (phy_port == 0)
367 		cmd = NHI_MAILBOX_DISCONNECT_PA;
368 	else
369 		cmd = NHI_MAILBOX_DISCONNECT_PB;
370 
371 	nhi_mailbox_cmd(tb->nhi, cmd, 1);
372 	usleep_range(10, 50);
373 	nhi_mailbox_cmd(tb->nhi, cmd, 2);
374 	return 0;
375 }
376 
377 static void add_switch(struct tb_switch *parent_sw, u64 route,
378 		       const uuid_t *uuid, u8 connection_id, u8 connection_key,
379 		       u8 link, u8 depth, enum tb_security_level security_level,
380 		       bool authorized)
381 {
382 	struct tb_switch *sw;
383 
384 	sw = tb_switch_alloc(parent_sw->tb, &parent_sw->dev, route);
385 	if (!sw)
386 		return;
387 
388 	sw->uuid = kmemdup(uuid, sizeof(*uuid), GFP_KERNEL);
389 	sw->connection_id = connection_id;
390 	sw->connection_key = connection_key;
391 	sw->link = link;
392 	sw->depth = depth;
393 	sw->authorized = authorized;
394 	sw->security_level = security_level;
395 
396 	/* Link the two switches now */
397 	tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
398 	tb_upstream_port(sw)->remote = tb_port_at(route, parent_sw);
399 
400 	if (tb_switch_add(sw)) {
401 		tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
402 		tb_switch_put(sw);
403 		return;
404 	}
405 }
406 
407 static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
408 			  u64 route, u8 connection_id, u8 connection_key,
409 			  u8 link, u8 depth)
410 {
411 	/* Disconnect from parent */
412 	tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
413 	/* Re-connect via updated port*/
414 	tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
415 
416 	/* Update with the new addressing information */
417 	sw->config.route_hi = upper_32_bits(route);
418 	sw->config.route_lo = lower_32_bits(route);
419 	sw->connection_id = connection_id;
420 	sw->connection_key = connection_key;
421 	sw->link = link;
422 	sw->depth = depth;
423 
424 	/* This switch still exists */
425 	sw->is_unplugged = false;
426 }
427 
428 static void remove_switch(struct tb_switch *sw)
429 {
430 	struct tb_switch *parent_sw;
431 
432 	parent_sw = tb_to_switch(sw->dev.parent);
433 	tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
434 	tb_switch_remove(sw);
435 }
436 
437 static void add_xdomain(struct tb_switch *sw, u64 route,
438 			const uuid_t *local_uuid, const uuid_t *remote_uuid,
439 			u8 link, u8 depth)
440 {
441 	struct tb_xdomain *xd;
442 
443 	xd = tb_xdomain_alloc(sw->tb, &sw->dev, route, local_uuid, remote_uuid);
444 	if (!xd)
445 		return;
446 
447 	xd->link = link;
448 	xd->depth = depth;
449 
450 	tb_port_at(route, sw)->xdomain = xd;
451 
452 	tb_xdomain_add(xd);
453 }
454 
455 static void update_xdomain(struct tb_xdomain *xd, u64 route, u8 link)
456 {
457 	xd->link = link;
458 	xd->route = route;
459 	xd->is_unplugged = false;
460 }
461 
462 static void remove_xdomain(struct tb_xdomain *xd)
463 {
464 	struct tb_switch *sw;
465 
466 	sw = tb_to_switch(xd->dev.parent);
467 	tb_port_at(xd->route, sw)->xdomain = NULL;
468 	tb_xdomain_remove(xd);
469 }
470 
471 static void
472 icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
473 {
474 	const struct icm_fr_event_device_connected *pkg =
475 		(const struct icm_fr_event_device_connected *)hdr;
476 	enum tb_security_level security_level;
477 	struct tb_switch *sw, *parent_sw;
478 	struct icm *icm = tb_priv(tb);
479 	bool authorized = false;
480 	struct tb_xdomain *xd;
481 	u8 link, depth;
482 	u64 route;
483 	int ret;
484 
485 	link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
486 	depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
487 		ICM_LINK_INFO_DEPTH_SHIFT;
488 	authorized = pkg->link_info & ICM_LINK_INFO_APPROVED;
489 	security_level = (pkg->hdr.flags & ICM_FLAGS_SLEVEL_MASK) >>
490 			 ICM_FLAGS_SLEVEL_SHIFT;
491 
492 	if (pkg->link_info & ICM_LINK_INFO_REJECTED) {
493 		tb_info(tb, "switch at %u.%u was rejected by ICM firmware because topology limit exceeded\n",
494 			link, depth);
495 		return;
496 	}
497 
498 	ret = icm->get_route(tb, link, depth, &route);
499 	if (ret) {
500 		tb_err(tb, "failed to find route string for switch at %u.%u\n",
501 		       link, depth);
502 		return;
503 	}
504 
505 	sw = tb_switch_find_by_uuid(tb, &pkg->ep_uuid);
506 	if (sw) {
507 		u8 phy_port, sw_phy_port;
508 
509 		parent_sw = tb_to_switch(sw->dev.parent);
510 		sw_phy_port = phy_port_from_route(tb_route(sw), sw->depth);
511 		phy_port = phy_port_from_route(route, depth);
512 
513 		/*
514 		 * On resume ICM will send us connected events for the
515 		 * devices that still are present. However, that
516 		 * information might have changed for example by the
517 		 * fact that a switch on a dual-link connection might
518 		 * have been enumerated using the other link now. Make
519 		 * sure our book keeping matches that.
520 		 */
521 		if (sw->depth == depth && sw_phy_port == phy_port &&
522 		    !!sw->authorized == authorized) {
523 			update_switch(parent_sw, sw, route, pkg->connection_id,
524 				      pkg->connection_key, link, depth);
525 			tb_switch_put(sw);
526 			return;
527 		}
528 
529 		/*
530 		 * User connected the same switch to another physical
531 		 * port or to another part of the topology. Remove the
532 		 * existing switch now before adding the new one.
533 		 */
534 		remove_switch(sw);
535 		tb_switch_put(sw);
536 	}
537 
538 	/*
539 	 * If the switch was not found by UUID, look for a switch on
540 	 * same physical port (taking possible link aggregation into
541 	 * account) and depth. If we found one it is definitely a stale
542 	 * one so remove it first.
543 	 */
544 	sw = tb_switch_find_by_link_depth(tb, link, depth);
545 	if (!sw) {
546 		u8 dual_link;
547 
548 		dual_link = dual_link_from_link(link);
549 		if (dual_link)
550 			sw = tb_switch_find_by_link_depth(tb, dual_link, depth);
551 	}
552 	if (sw) {
553 		remove_switch(sw);
554 		tb_switch_put(sw);
555 	}
556 
557 	/* Remove existing XDomain connection if found */
558 	xd = tb_xdomain_find_by_link_depth(tb, link, depth);
559 	if (xd) {
560 		remove_xdomain(xd);
561 		tb_xdomain_put(xd);
562 	}
563 
564 	parent_sw = tb_switch_find_by_link_depth(tb, link, depth - 1);
565 	if (!parent_sw) {
566 		tb_err(tb, "failed to find parent switch for %u.%u\n",
567 		       link, depth);
568 		return;
569 	}
570 
571 	add_switch(parent_sw, route, &pkg->ep_uuid, pkg->connection_id,
572 		   pkg->connection_key, link, depth, security_level,
573 		   authorized);
574 
575 	tb_switch_put(parent_sw);
576 }
577 
578 static void
579 icm_fr_device_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
580 {
581 	const struct icm_fr_event_device_disconnected *pkg =
582 		(const struct icm_fr_event_device_disconnected *)hdr;
583 	struct tb_switch *sw;
584 	u8 link, depth;
585 
586 	link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
587 	depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
588 		ICM_LINK_INFO_DEPTH_SHIFT;
589 
590 	if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
591 		tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
592 		return;
593 	}
594 
595 	sw = tb_switch_find_by_link_depth(tb, link, depth);
596 	if (!sw) {
597 		tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
598 			depth);
599 		return;
600 	}
601 
602 	remove_switch(sw);
603 	tb_switch_put(sw);
604 }
605 
606 static void
607 icm_fr_xdomain_connected(struct tb *tb, const struct icm_pkg_header *hdr)
608 {
609 	const struct icm_fr_event_xdomain_connected *pkg =
610 		(const struct icm_fr_event_xdomain_connected *)hdr;
611 	struct tb_xdomain *xd;
612 	struct tb_switch *sw;
613 	u8 link, depth;
614 	bool approved;
615 	u64 route;
616 
617 	/*
618 	 * After NVM upgrade adding root switch device fails because we
619 	 * initiated reset. During that time ICM might still send
620 	 * XDomain connected message which we ignore here.
621 	 */
622 	if (!tb->root_switch)
623 		return;
624 
625 	link = pkg->link_info & ICM_LINK_INFO_LINK_MASK;
626 	depth = (pkg->link_info & ICM_LINK_INFO_DEPTH_MASK) >>
627 		ICM_LINK_INFO_DEPTH_SHIFT;
628 	approved = pkg->link_info & ICM_LINK_INFO_APPROVED;
629 
630 	if (link > ICM_MAX_LINK || depth > ICM_MAX_DEPTH) {
631 		tb_warn(tb, "invalid topology %u.%u, ignoring\n", link, depth);
632 		return;
633 	}
634 
635 	route = get_route(pkg->local_route_hi, pkg->local_route_lo);
636 
637 	xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
638 	if (xd) {
639 		u8 xd_phy_port, phy_port;
640 
641 		xd_phy_port = phy_port_from_route(xd->route, xd->depth);
642 		phy_port = phy_port_from_route(route, depth);
643 
644 		if (xd->depth == depth && xd_phy_port == phy_port) {
645 			update_xdomain(xd, route, link);
646 			tb_xdomain_put(xd);
647 			return;
648 		}
649 
650 		/*
651 		 * If we find an existing XDomain connection remove it
652 		 * now. We need to go through login handshake and
653 		 * everything anyway to be able to re-establish the
654 		 * connection.
655 		 */
656 		remove_xdomain(xd);
657 		tb_xdomain_put(xd);
658 	}
659 
660 	/*
661 	 * Look if there already exists an XDomain in the same place
662 	 * than the new one and in that case remove it because it is
663 	 * most likely another host that got disconnected.
664 	 */
665 	xd = tb_xdomain_find_by_link_depth(tb, link, depth);
666 	if (!xd) {
667 		u8 dual_link;
668 
669 		dual_link = dual_link_from_link(link);
670 		if (dual_link)
671 			xd = tb_xdomain_find_by_link_depth(tb, dual_link,
672 							   depth);
673 	}
674 	if (xd) {
675 		remove_xdomain(xd);
676 		tb_xdomain_put(xd);
677 	}
678 
679 	/*
680 	 * If the user disconnected a switch during suspend and
681 	 * connected another host to the same port, remove the switch
682 	 * first.
683 	 */
684 	sw = get_switch_at_route(tb->root_switch, route);
685 	if (sw)
686 		remove_switch(sw);
687 
688 	sw = tb_switch_find_by_link_depth(tb, link, depth);
689 	if (!sw) {
690 		tb_warn(tb, "no switch exists at %u.%u, ignoring\n", link,
691 			depth);
692 		return;
693 	}
694 
695 	add_xdomain(sw, route, &pkg->local_uuid, &pkg->remote_uuid, link,
696 		    depth);
697 	tb_switch_put(sw);
698 }
699 
700 static void
701 icm_fr_xdomain_disconnected(struct tb *tb, const struct icm_pkg_header *hdr)
702 {
703 	const struct icm_fr_event_xdomain_disconnected *pkg =
704 		(const struct icm_fr_event_xdomain_disconnected *)hdr;
705 	struct tb_xdomain *xd;
706 
707 	/*
708 	 * If the connection is through one or multiple devices, the
709 	 * XDomain device is removed along with them so it is fine if we
710 	 * cannot find it here.
711 	 */
712 	xd = tb_xdomain_find_by_uuid(tb, &pkg->remote_uuid);
713 	if (xd) {
714 		remove_xdomain(xd);
715 		tb_xdomain_put(xd);
716 	}
717 }
718 
719 static struct pci_dev *get_upstream_port(struct pci_dev *pdev)
720 {
721 	struct pci_dev *parent;
722 
723 	parent = pci_upstream_bridge(pdev);
724 	while (parent) {
725 		if (!pci_is_pcie(parent))
726 			return NULL;
727 		if (pci_pcie_type(parent) == PCI_EXP_TYPE_UPSTREAM)
728 			break;
729 		parent = pci_upstream_bridge(parent);
730 	}
731 
732 	if (!parent)
733 		return NULL;
734 
735 	switch (parent->device) {
736 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
737 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
738 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
739 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
740 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
741 		return parent;
742 	}
743 
744 	return NULL;
745 }
746 
747 static bool icm_ar_is_supported(struct tb *tb)
748 {
749 	struct pci_dev *upstream_port;
750 	struct icm *icm = tb_priv(tb);
751 
752 	/*
753 	 * Starting from Alpine Ridge we can use ICM on Apple machines
754 	 * as well. We just need to reset and re-enable it first.
755 	 */
756 	if (!x86_apple_machine)
757 		return true;
758 
759 	/*
760 	 * Find the upstream PCIe port in case we need to do reset
761 	 * through its vendor specific registers.
762 	 */
763 	upstream_port = get_upstream_port(tb->nhi->pdev);
764 	if (upstream_port) {
765 		int cap;
766 
767 		cap = pci_find_ext_capability(upstream_port,
768 					      PCI_EXT_CAP_ID_VNDR);
769 		if (cap > 0) {
770 			icm->upstream_port = upstream_port;
771 			icm->vnd_cap = cap;
772 
773 			return true;
774 		}
775 	}
776 
777 	return false;
778 }
779 
780 static int icm_ar_get_mode(struct tb *tb)
781 {
782 	struct tb_nhi *nhi = tb->nhi;
783 	int retries = 60;
784 	u32 val;
785 
786 	do {
787 		val = ioread32(nhi->iobase + REG_FW_STS);
788 		if (val & REG_FW_STS_NVM_AUTH_DONE)
789 			break;
790 		msleep(50);
791 	} while (--retries);
792 
793 	if (!retries) {
794 		dev_err(&nhi->pdev->dev, "ICM firmware not authenticated\n");
795 		return -ENODEV;
796 	}
797 
798 	return nhi_mailbox_mode(nhi);
799 }
800 
801 static int icm_ar_get_route(struct tb *tb, u8 link, u8 depth, u64 *route)
802 {
803 	struct icm_ar_pkg_get_route_response reply;
804 	struct icm_ar_pkg_get_route request = {
805 		.hdr = { .code = ICM_GET_ROUTE },
806 		.link_info = depth << ICM_LINK_INFO_DEPTH_SHIFT | link,
807 	};
808 	int ret;
809 
810 	memset(&reply, 0, sizeof(reply));
811 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
812 			  1, ICM_TIMEOUT);
813 	if (ret)
814 		return ret;
815 
816 	if (reply.hdr.flags & ICM_FLAGS_ERROR)
817 		return -EIO;
818 
819 	*route = get_route(reply.route_hi, reply.route_lo);
820 	return 0;
821 }
822 
823 static void icm_handle_notification(struct work_struct *work)
824 {
825 	struct icm_notification *n = container_of(work, typeof(*n), work);
826 	struct tb *tb = n->tb;
827 	struct icm *icm = tb_priv(tb);
828 
829 	mutex_lock(&tb->lock);
830 
831 	switch (n->pkg->code) {
832 	case ICM_EVENT_DEVICE_CONNECTED:
833 		icm->device_connected(tb, n->pkg);
834 		break;
835 	case ICM_EVENT_DEVICE_DISCONNECTED:
836 		icm->device_disconnected(tb, n->pkg);
837 		break;
838 	case ICM_EVENT_XDOMAIN_CONNECTED:
839 		icm->xdomain_connected(tb, n->pkg);
840 		break;
841 	case ICM_EVENT_XDOMAIN_DISCONNECTED:
842 		icm->xdomain_disconnected(tb, n->pkg);
843 		break;
844 	}
845 
846 	mutex_unlock(&tb->lock);
847 
848 	kfree(n->pkg);
849 	kfree(n);
850 }
851 
852 static void icm_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
853 			     const void *buf, size_t size)
854 {
855 	struct icm_notification *n;
856 
857 	n = kmalloc(sizeof(*n), GFP_KERNEL);
858 	if (!n)
859 		return;
860 
861 	INIT_WORK(&n->work, icm_handle_notification);
862 	n->pkg = kmemdup(buf, size, GFP_KERNEL);
863 	n->tb = tb;
864 
865 	queue_work(tb->wq, &n->work);
866 }
867 
868 static int
869 __icm_driver_ready(struct tb *tb, enum tb_security_level *security_level)
870 {
871 	struct icm_pkg_driver_ready_response reply;
872 	struct icm_pkg_driver_ready request = {
873 		.hdr.code = ICM_DRIVER_READY,
874 	};
875 	unsigned int retries = 50;
876 	int ret;
877 
878 	memset(&reply, 0, sizeof(reply));
879 	ret = icm_request(tb, &request, sizeof(request), &reply, sizeof(reply),
880 			  1, ICM_TIMEOUT);
881 	if (ret)
882 		return ret;
883 
884 	if (security_level)
885 		*security_level = reply.security_level & 0xf;
886 
887 	/*
888 	 * Hold on here until the switch config space is accessible so
889 	 * that we can read root switch config successfully.
890 	 */
891 	do {
892 		struct tb_cfg_result res;
893 		u32 tmp;
894 
895 		res = tb_cfg_read_raw(tb->ctl, &tmp, 0, 0, TB_CFG_SWITCH,
896 				      0, 1, 100);
897 		if (!res.err)
898 			return 0;
899 
900 		msleep(50);
901 	} while (--retries);
902 
903 	tb_err(tb, "failed to read root switch config space, giving up\n");
904 	return -ETIMEDOUT;
905 }
906 
907 static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec)
908 {
909 	unsigned long end = jiffies + msecs_to_jiffies(timeout_msec);
910 	u32 cmd;
911 
912 	do {
913 		pci_read_config_dword(icm->upstream_port,
914 				      icm->vnd_cap + PCIE2CIO_CMD, &cmd);
915 		if (!(cmd & PCIE2CIO_CMD_START)) {
916 			if (cmd & PCIE2CIO_CMD_TIMEOUT)
917 				break;
918 			return 0;
919 		}
920 
921 		msleep(50);
922 	} while (time_before(jiffies, end));
923 
924 	return -ETIMEDOUT;
925 }
926 
927 static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs,
928 			 unsigned int port, unsigned int index, u32 *data)
929 {
930 	struct pci_dev *pdev = icm->upstream_port;
931 	int ret, vnd_cap = icm->vnd_cap;
932 	u32 cmd;
933 
934 	cmd = index;
935 	cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
936 	cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
937 	cmd |= PCIE2CIO_CMD_START;
938 	pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
939 
940 	ret = pci2cio_wait_completion(icm, 5000);
941 	if (ret)
942 		return ret;
943 
944 	pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data);
945 	return 0;
946 }
947 
948 static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
949 			  unsigned int port, unsigned int index, u32 data)
950 {
951 	struct pci_dev *pdev = icm->upstream_port;
952 	int vnd_cap = icm->vnd_cap;
953 	u32 cmd;
954 
955 	pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data);
956 
957 	cmd = index;
958 	cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK;
959 	cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK;
960 	cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START;
961 	pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd);
962 
963 	return pci2cio_wait_completion(icm, 5000);
964 }
965 
966 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi)
967 {
968 	struct icm *icm = tb_priv(tb);
969 	u32 val;
970 
971 	/* Put ARC to wait for CIO reset event to happen */
972 	val = ioread32(nhi->iobase + REG_FW_STS);
973 	val |= REG_FW_STS_CIO_RESET_REQ;
974 	iowrite32(val, nhi->iobase + REG_FW_STS);
975 
976 	/* Re-start ARC */
977 	val = ioread32(nhi->iobase + REG_FW_STS);
978 	val |= REG_FW_STS_ICM_EN_INVERT;
979 	val |= REG_FW_STS_ICM_EN_CPU;
980 	iowrite32(val, nhi->iobase + REG_FW_STS);
981 
982 	/* Trigger CIO reset now */
983 	return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9));
984 }
985 
986 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi)
987 {
988 	unsigned int retries = 10;
989 	int ret;
990 	u32 val;
991 
992 	/* Check if the ICM firmware is already running */
993 	val = ioread32(nhi->iobase + REG_FW_STS);
994 	if (val & REG_FW_STS_ICM_EN)
995 		return 0;
996 
997 	dev_info(&nhi->pdev->dev, "starting ICM firmware\n");
998 
999 	ret = icm_firmware_reset(tb, nhi);
1000 	if (ret)
1001 		return ret;
1002 
1003 	/* Wait until the ICM firmware tells us it is up and running */
1004 	do {
1005 		/* Check that the ICM firmware is running */
1006 		val = ioread32(nhi->iobase + REG_FW_STS);
1007 		if (val & REG_FW_STS_NVM_AUTH_DONE)
1008 			return 0;
1009 
1010 		msleep(300);
1011 	} while (--retries);
1012 
1013 	return -ETIMEDOUT;
1014 }
1015 
1016 static int icm_reset_phy_port(struct tb *tb, int phy_port)
1017 {
1018 	struct icm *icm = tb_priv(tb);
1019 	u32 state0, state1;
1020 	int port0, port1;
1021 	u32 val0, val1;
1022 	int ret;
1023 
1024 	if (!icm->upstream_port)
1025 		return 0;
1026 
1027 	if (phy_port) {
1028 		port0 = 3;
1029 		port1 = 4;
1030 	} else {
1031 		port0 = 1;
1032 		port1 = 2;
1033 	}
1034 
1035 	/*
1036 	 * Read link status of both null ports belonging to a single
1037 	 * physical port.
1038 	 */
1039 	ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1040 	if (ret)
1041 		return ret;
1042 	ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1043 	if (ret)
1044 		return ret;
1045 
1046 	state0 = val0 & PHY_PORT_CS1_LINK_STATE_MASK;
1047 	state0 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1048 	state1 = val1 & PHY_PORT_CS1_LINK_STATE_MASK;
1049 	state1 >>= PHY_PORT_CS1_LINK_STATE_SHIFT;
1050 
1051 	/* If they are both up we need to reset them now */
1052 	if (state0 != TB_PORT_UP || state1 != TB_PORT_UP)
1053 		return 0;
1054 
1055 	val0 |= PHY_PORT_CS1_LINK_DISABLE;
1056 	ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1057 	if (ret)
1058 		return ret;
1059 
1060 	val1 |= PHY_PORT_CS1_LINK_DISABLE;
1061 	ret = pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1062 	if (ret)
1063 		return ret;
1064 
1065 	/* Wait a bit and then re-enable both ports */
1066 	usleep_range(10, 100);
1067 
1068 	ret = pcie2cio_read(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, &val0);
1069 	if (ret)
1070 		return ret;
1071 	ret = pcie2cio_read(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, &val1);
1072 	if (ret)
1073 		return ret;
1074 
1075 	val0 &= ~PHY_PORT_CS1_LINK_DISABLE;
1076 	ret = pcie2cio_write(icm, TB_CFG_PORT, port0, PHY_PORT_CS1, val0);
1077 	if (ret)
1078 		return ret;
1079 
1080 	val1 &= ~PHY_PORT_CS1_LINK_DISABLE;
1081 	return pcie2cio_write(icm, TB_CFG_PORT, port1, PHY_PORT_CS1, val1);
1082 }
1083 
1084 static int icm_firmware_init(struct tb *tb)
1085 {
1086 	struct icm *icm = tb_priv(tb);
1087 	struct tb_nhi *nhi = tb->nhi;
1088 	int ret;
1089 
1090 	ret = icm_firmware_start(tb, nhi);
1091 	if (ret) {
1092 		dev_err(&nhi->pdev->dev, "could not start ICM firmware\n");
1093 		return ret;
1094 	}
1095 
1096 	if (icm->get_mode) {
1097 		ret = icm->get_mode(tb);
1098 
1099 		switch (ret) {
1100 		case NHI_FW_SAFE_MODE:
1101 			icm->safe_mode = true;
1102 			break;
1103 
1104 		case NHI_FW_CM_MODE:
1105 			/* Ask ICM to accept all Thunderbolt devices */
1106 			nhi_mailbox_cmd(nhi, NHI_MAILBOX_ALLOW_ALL_DEVS, 0);
1107 			break;
1108 
1109 		default:
1110 			if (ret < 0)
1111 				return ret;
1112 
1113 			tb_err(tb, "ICM firmware is in wrong mode: %u\n", ret);
1114 			return -ENODEV;
1115 		}
1116 	}
1117 
1118 	/*
1119 	 * Reset both physical ports if there is anything connected to
1120 	 * them already.
1121 	 */
1122 	ret = icm_reset_phy_port(tb, 0);
1123 	if (ret)
1124 		dev_warn(&nhi->pdev->dev, "failed to reset links on port0\n");
1125 	ret = icm_reset_phy_port(tb, 1);
1126 	if (ret)
1127 		dev_warn(&nhi->pdev->dev, "failed to reset links on port1\n");
1128 
1129 	return 0;
1130 }
1131 
1132 static int icm_driver_ready(struct tb *tb)
1133 {
1134 	struct icm *icm = tb_priv(tb);
1135 	int ret;
1136 
1137 	ret = icm_firmware_init(tb);
1138 	if (ret)
1139 		return ret;
1140 
1141 	if (icm->safe_mode) {
1142 		tb_info(tb, "Thunderbolt host controller is in safe mode.\n");
1143 		tb_info(tb, "You need to update NVM firmware of the controller before it can be used.\n");
1144 		tb_info(tb, "For latest updates check https://thunderbolttechnology.net/updates.\n");
1145 		return 0;
1146 	}
1147 
1148 	return __icm_driver_ready(tb, &tb->security_level);
1149 }
1150 
1151 static int icm_suspend(struct tb *tb)
1152 {
1153 	int ret;
1154 
1155 	ret = nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_SAVE_DEVS, 0);
1156 	if (ret)
1157 		tb_info(tb, "Ignoring mailbox command error (%d) in %s\n",
1158 			ret, __func__);
1159 
1160 	return 0;
1161 }
1162 
1163 /*
1164  * Mark all switches (except root switch) below this one unplugged. ICM
1165  * firmware will send us an updated list of switches after we have send
1166  * it driver ready command. If a switch is not in that list it will be
1167  * removed when we perform rescan.
1168  */
1169 static void icm_unplug_children(struct tb_switch *sw)
1170 {
1171 	unsigned int i;
1172 
1173 	if (tb_route(sw))
1174 		sw->is_unplugged = true;
1175 
1176 	for (i = 1; i <= sw->config.max_port_number; i++) {
1177 		struct tb_port *port = &sw->ports[i];
1178 
1179 		if (tb_is_upstream_port(port))
1180 			continue;
1181 		if (port->xdomain) {
1182 			port->xdomain->is_unplugged = true;
1183 			continue;
1184 		}
1185 		if (!port->remote)
1186 			continue;
1187 
1188 		icm_unplug_children(port->remote->sw);
1189 	}
1190 }
1191 
1192 static void icm_free_unplugged_children(struct tb_switch *sw)
1193 {
1194 	unsigned int i;
1195 
1196 	for (i = 1; i <= sw->config.max_port_number; i++) {
1197 		struct tb_port *port = &sw->ports[i];
1198 
1199 		if (tb_is_upstream_port(port))
1200 			continue;
1201 
1202 		if (port->xdomain && port->xdomain->is_unplugged) {
1203 			tb_xdomain_remove(port->xdomain);
1204 			port->xdomain = NULL;
1205 			continue;
1206 		}
1207 
1208 		if (!port->remote)
1209 			continue;
1210 
1211 		if (port->remote->sw->is_unplugged) {
1212 			tb_switch_remove(port->remote->sw);
1213 			port->remote = NULL;
1214 		} else {
1215 			icm_free_unplugged_children(port->remote->sw);
1216 		}
1217 	}
1218 }
1219 
1220 static void icm_rescan_work(struct work_struct *work)
1221 {
1222 	struct icm *icm = container_of(work, struct icm, rescan_work.work);
1223 	struct tb *tb = icm_to_tb(icm);
1224 
1225 	mutex_lock(&tb->lock);
1226 	if (tb->root_switch)
1227 		icm_free_unplugged_children(tb->root_switch);
1228 	mutex_unlock(&tb->lock);
1229 }
1230 
1231 static void icm_complete(struct tb *tb)
1232 {
1233 	struct icm *icm = tb_priv(tb);
1234 
1235 	if (tb->nhi->going_away)
1236 		return;
1237 
1238 	icm_unplug_children(tb->root_switch);
1239 
1240 	/*
1241 	 * Now all existing children should be resumed, start events
1242 	 * from ICM to get updated status.
1243 	 */
1244 	__icm_driver_ready(tb, NULL);
1245 
1246 	/*
1247 	 * We do not get notifications of devices that have been
1248 	 * unplugged during suspend so schedule rescan to clean them up
1249 	 * if any.
1250 	 */
1251 	queue_delayed_work(tb->wq, &icm->rescan_work, msecs_to_jiffies(500));
1252 }
1253 
1254 static int icm_start(struct tb *tb)
1255 {
1256 	struct icm *icm = tb_priv(tb);
1257 	int ret;
1258 
1259 	if (icm->safe_mode)
1260 		tb->root_switch = tb_switch_alloc_safe_mode(tb, &tb->dev, 0);
1261 	else
1262 		tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1263 	if (!tb->root_switch)
1264 		return -ENODEV;
1265 
1266 	/*
1267 	 * NVM upgrade has not been tested on Apple systems and they
1268 	 * don't provide images publicly either. To be on the safe side
1269 	 * prevent root switch NVM upgrade on Macs for now.
1270 	 */
1271 	tb->root_switch->no_nvm_upgrade = x86_apple_machine;
1272 
1273 	ret = tb_switch_add(tb->root_switch);
1274 	if (ret) {
1275 		tb_switch_put(tb->root_switch);
1276 		tb->root_switch = NULL;
1277 	}
1278 
1279 	return ret;
1280 }
1281 
1282 static void icm_stop(struct tb *tb)
1283 {
1284 	struct icm *icm = tb_priv(tb);
1285 
1286 	cancel_delayed_work(&icm->rescan_work);
1287 	tb_switch_remove(tb->root_switch);
1288 	tb->root_switch = NULL;
1289 	nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DRV_UNLOADS, 0);
1290 }
1291 
1292 static int icm_disconnect_pcie_paths(struct tb *tb)
1293 {
1294 	return nhi_mailbox_cmd(tb->nhi, NHI_MAILBOX_DISCONNECT_PCIE_PATHS, 0);
1295 }
1296 
1297 /* Falcon Ridge and Alpine Ridge */
1298 static const struct tb_cm_ops icm_fr_ops = {
1299 	.driver_ready = icm_driver_ready,
1300 	.start = icm_start,
1301 	.stop = icm_stop,
1302 	.suspend = icm_suspend,
1303 	.complete = icm_complete,
1304 	.handle_event = icm_handle_event,
1305 	.approve_switch = icm_fr_approve_switch,
1306 	.add_switch_key = icm_fr_add_switch_key,
1307 	.challenge_switch_key = icm_fr_challenge_switch_key,
1308 	.disconnect_pcie_paths = icm_disconnect_pcie_paths,
1309 	.approve_xdomain_paths = icm_fr_approve_xdomain_paths,
1310 	.disconnect_xdomain_paths = icm_fr_disconnect_xdomain_paths,
1311 };
1312 
1313 struct tb *icm_probe(struct tb_nhi *nhi)
1314 {
1315 	struct icm *icm;
1316 	struct tb *tb;
1317 
1318 	tb = tb_domain_alloc(nhi, sizeof(struct icm));
1319 	if (!tb)
1320 		return NULL;
1321 
1322 	icm = tb_priv(tb);
1323 	INIT_DELAYED_WORK(&icm->rescan_work, icm_rescan_work);
1324 	mutex_init(&icm->request_lock);
1325 
1326 	switch (nhi->pdev->device) {
1327 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1328 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1329 		icm->is_supported = icm_fr_is_supported;
1330 		icm->get_route = icm_fr_get_route;
1331 		icm->device_connected = icm_fr_device_connected;
1332 		icm->device_disconnected = icm_fr_device_disconnected;
1333 		icm->xdomain_connected = icm_fr_xdomain_connected;
1334 		icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
1335 		tb->cm_ops = &icm_fr_ops;
1336 		break;
1337 
1338 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI:
1339 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI:
1340 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI:
1341 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI:
1342 	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI:
1343 		icm->is_supported = icm_ar_is_supported;
1344 		icm->get_mode = icm_ar_get_mode;
1345 		icm->get_route = icm_ar_get_route;
1346 		icm->device_connected = icm_fr_device_connected;
1347 		icm->device_disconnected = icm_fr_device_disconnected;
1348 		icm->xdomain_connected = icm_fr_xdomain_connected;
1349 		icm->xdomain_disconnected = icm_fr_xdomain_disconnected;
1350 		tb->cm_ops = &icm_fr_ops;
1351 		break;
1352 	}
1353 
1354 	if (!icm->is_supported || !icm->is_supported(tb)) {
1355 		dev_dbg(&nhi->pdev->dev, "ICM not supported on this controller\n");
1356 		tb_domain_put(tb);
1357 		return NULL;
1358 	}
1359 
1360 	return tb;
1361 }
1362