xref: /openbmc/linux/drivers/thunderbolt/tb.c (revision 341d4518)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thunderbolt driver - bus logic (NHI independent)
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2019, Intel Corporation
7  */
8 
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 
13 #include "tb.h"
14 #include "tb_regs.h"
15 #include "tunnel.h"
16 
17 /**
18  * struct tb_cm - Simple Thunderbolt connection manager
19  * @tunnel_list: List of active tunnels
20  * @dp_resources: List of available DP resources for DP tunneling
21  * @hotplug_active: tb_handle_hotplug will stop progressing plug
22  *		    events and exit if this is not set (it needs to
23  *		    acquire the lock one more time). Used to drain wq
24  *		    after cfg has been paused.
25  */
26 struct tb_cm {
27 	struct list_head tunnel_list;
28 	struct list_head dp_resources;
29 	bool hotplug_active;
30 };
31 
32 struct tb_hotplug_event {
33 	struct work_struct work;
34 	struct tb *tb;
35 	u64 route;
36 	u8 port;
37 	bool unplug;
38 };
39 
40 static void tb_handle_hotplug(struct work_struct *work);
41 
42 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
43 {
44 	struct tb_hotplug_event *ev;
45 
46 	ev = kmalloc(sizeof(*ev), GFP_KERNEL);
47 	if (!ev)
48 		return;
49 
50 	ev->tb = tb;
51 	ev->route = route;
52 	ev->port = port;
53 	ev->unplug = unplug;
54 	INIT_WORK(&ev->work, tb_handle_hotplug);
55 	queue_work(tb->wq, &ev->work);
56 }
57 
58 /* enumeration & hot plug handling */
59 
60 static void tb_add_dp_resources(struct tb_switch *sw)
61 {
62 	struct tb_cm *tcm = tb_priv(sw->tb);
63 	struct tb_port *port;
64 
65 	tb_switch_for_each_port(sw, port) {
66 		if (!tb_port_is_dpin(port))
67 			continue;
68 
69 		if (!tb_switch_query_dp_resource(sw, port))
70 			continue;
71 
72 		list_add_tail(&port->list, &tcm->dp_resources);
73 		tb_port_dbg(port, "DP IN resource available\n");
74 	}
75 }
76 
77 static void tb_remove_dp_resources(struct tb_switch *sw)
78 {
79 	struct tb_cm *tcm = tb_priv(sw->tb);
80 	struct tb_port *port, *tmp;
81 
82 	/* Clear children resources first */
83 	tb_switch_for_each_port(sw, port) {
84 		if (tb_port_has_remote(port))
85 			tb_remove_dp_resources(port->remote->sw);
86 	}
87 
88 	list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
89 		if (port->sw == sw) {
90 			tb_port_dbg(port, "DP OUT resource unavailable\n");
91 			list_del_init(&port->list);
92 		}
93 	}
94 }
95 
96 static void tb_discover_tunnels(struct tb_switch *sw)
97 {
98 	struct tb *tb = sw->tb;
99 	struct tb_cm *tcm = tb_priv(tb);
100 	struct tb_port *port;
101 
102 	tb_switch_for_each_port(sw, port) {
103 		struct tb_tunnel *tunnel = NULL;
104 
105 		switch (port->config.type) {
106 		case TB_TYPE_DP_HDMI_IN:
107 			tunnel = tb_tunnel_discover_dp(tb, port);
108 			break;
109 
110 		case TB_TYPE_PCIE_DOWN:
111 			tunnel = tb_tunnel_discover_pci(tb, port);
112 			break;
113 
114 		case TB_TYPE_USB3_DOWN:
115 			tunnel = tb_tunnel_discover_usb3(tb, port);
116 			break;
117 
118 		default:
119 			break;
120 		}
121 
122 		if (!tunnel)
123 			continue;
124 
125 		if (tb_tunnel_is_pci(tunnel)) {
126 			struct tb_switch *parent = tunnel->dst_port->sw;
127 
128 			while (parent != tunnel->src_port->sw) {
129 				parent->boot = true;
130 				parent = tb_switch_parent(parent);
131 			}
132 		}
133 
134 		list_add_tail(&tunnel->list, &tcm->tunnel_list);
135 	}
136 
137 	tb_switch_for_each_port(sw, port) {
138 		if (tb_port_has_remote(port))
139 			tb_discover_tunnels(port->remote->sw);
140 	}
141 }
142 
143 static int tb_port_configure_xdomain(struct tb_port *port)
144 {
145 	/*
146 	 * XDomain paths currently only support single lane so we must
147 	 * disable the other lane according to USB4 spec.
148 	 */
149 	tb_port_disable(port->dual_link_port);
150 
151 	if (tb_switch_is_usb4(port->sw))
152 		return usb4_port_configure_xdomain(port);
153 	return tb_lc_configure_xdomain(port);
154 }
155 
156 static void tb_port_unconfigure_xdomain(struct tb_port *port)
157 {
158 	if (tb_switch_is_usb4(port->sw))
159 		usb4_port_unconfigure_xdomain(port);
160 	else
161 		tb_lc_unconfigure_xdomain(port);
162 
163 	tb_port_enable(port->dual_link_port);
164 }
165 
166 static void tb_scan_xdomain(struct tb_port *port)
167 {
168 	struct tb_switch *sw = port->sw;
169 	struct tb *tb = sw->tb;
170 	struct tb_xdomain *xd;
171 	u64 route;
172 
173 	route = tb_downstream_route(port);
174 	xd = tb_xdomain_find_by_route(tb, route);
175 	if (xd) {
176 		tb_xdomain_put(xd);
177 		return;
178 	}
179 
180 	xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
181 			      NULL);
182 	if (xd) {
183 		tb_port_at(route, sw)->xdomain = xd;
184 		tb_port_configure_xdomain(port);
185 		tb_xdomain_add(xd);
186 	}
187 }
188 
189 static int tb_enable_tmu(struct tb_switch *sw)
190 {
191 	int ret;
192 
193 	/* If it is already enabled in correct mode, don't touch it */
194 	if (tb_switch_tmu_is_enabled(sw))
195 		return 0;
196 
197 	ret = tb_switch_tmu_disable(sw);
198 	if (ret)
199 		return ret;
200 
201 	ret = tb_switch_tmu_post_time(sw);
202 	if (ret)
203 		return ret;
204 
205 	return tb_switch_tmu_enable(sw);
206 }
207 
208 /**
209  * tb_find_unused_port() - return the first inactive port on @sw
210  * @sw: Switch to find the port on
211  * @type: Port type to look for
212  */
213 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
214 					   enum tb_port_type type)
215 {
216 	struct tb_port *port;
217 
218 	tb_switch_for_each_port(sw, port) {
219 		if (tb_is_upstream_port(port))
220 			continue;
221 		if (port->config.type != type)
222 			continue;
223 		if (!port->cap_adap)
224 			continue;
225 		if (tb_port_is_enabled(port))
226 			continue;
227 		return port;
228 	}
229 	return NULL;
230 }
231 
232 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
233 					 const struct tb_port *port)
234 {
235 	struct tb_port *down;
236 
237 	down = usb4_switch_map_usb3_down(sw, port);
238 	if (down && !tb_usb3_port_is_enabled(down))
239 		return down;
240 	return NULL;
241 }
242 
243 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
244 					struct tb_port *src_port,
245 					struct tb_port *dst_port)
246 {
247 	struct tb_cm *tcm = tb_priv(tb);
248 	struct tb_tunnel *tunnel;
249 
250 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
251 		if (tunnel->type == type &&
252 		    ((src_port && src_port == tunnel->src_port) ||
253 		     (dst_port && dst_port == tunnel->dst_port))) {
254 			return tunnel;
255 		}
256 	}
257 
258 	return NULL;
259 }
260 
261 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
262 						   struct tb_port *src_port,
263 						   struct tb_port *dst_port)
264 {
265 	struct tb_port *port, *usb3_down;
266 	struct tb_switch *sw;
267 
268 	/* Pick the router that is deepest in the topology */
269 	if (dst_port->sw->config.depth > src_port->sw->config.depth)
270 		sw = dst_port->sw;
271 	else
272 		sw = src_port->sw;
273 
274 	/* Can't be the host router */
275 	if (sw == tb->root_switch)
276 		return NULL;
277 
278 	/* Find the downstream USB4 port that leads to this router */
279 	port = tb_port_at(tb_route(sw), tb->root_switch);
280 	/* Find the corresponding host router USB3 downstream port */
281 	usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
282 	if (!usb3_down)
283 		return NULL;
284 
285 	return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
286 }
287 
288 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
289 	struct tb_port *dst_port, int *available_up, int *available_down)
290 {
291 	int usb3_consumed_up, usb3_consumed_down, ret;
292 	struct tb_cm *tcm = tb_priv(tb);
293 	struct tb_tunnel *tunnel;
294 	struct tb_port *port;
295 
296 	tb_port_dbg(dst_port, "calculating available bandwidth\n");
297 
298 	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
299 	if (tunnel) {
300 		ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
301 						   &usb3_consumed_down);
302 		if (ret)
303 			return ret;
304 	} else {
305 		usb3_consumed_up = 0;
306 		usb3_consumed_down = 0;
307 	}
308 
309 	*available_up = *available_down = 40000;
310 
311 	/* Find the minimum available bandwidth over all links */
312 	tb_for_each_port_on_path(src_port, dst_port, port) {
313 		int link_speed, link_width, up_bw, down_bw;
314 
315 		if (!tb_port_is_null(port))
316 			continue;
317 
318 		if (tb_is_upstream_port(port)) {
319 			link_speed = port->sw->link_speed;
320 		} else {
321 			link_speed = tb_port_get_link_speed(port);
322 			if (link_speed < 0)
323 				return link_speed;
324 		}
325 
326 		link_width = port->bonded ? 2 : 1;
327 
328 		up_bw = link_speed * link_width * 1000; /* Mb/s */
329 		/* Leave 10% guard band */
330 		up_bw -= up_bw / 10;
331 		down_bw = up_bw;
332 
333 		tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
334 
335 		/*
336 		 * Find all DP tunnels that cross the port and reduce
337 		 * their consumed bandwidth from the available.
338 		 */
339 		list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
340 			int dp_consumed_up, dp_consumed_down;
341 
342 			if (!tb_tunnel_is_dp(tunnel))
343 				continue;
344 
345 			if (!tb_tunnel_port_on_path(tunnel, port))
346 				continue;
347 
348 			ret = tb_tunnel_consumed_bandwidth(tunnel,
349 							   &dp_consumed_up,
350 							   &dp_consumed_down);
351 			if (ret)
352 				return ret;
353 
354 			up_bw -= dp_consumed_up;
355 			down_bw -= dp_consumed_down;
356 		}
357 
358 		/*
359 		 * If USB3 is tunneled from the host router down to the
360 		 * branch leading to port we need to take USB3 consumed
361 		 * bandwidth into account regardless whether it actually
362 		 * crosses the port.
363 		 */
364 		up_bw -= usb3_consumed_up;
365 		down_bw -= usb3_consumed_down;
366 
367 		if (up_bw < *available_up)
368 			*available_up = up_bw;
369 		if (down_bw < *available_down)
370 			*available_down = down_bw;
371 	}
372 
373 	if (*available_up < 0)
374 		*available_up = 0;
375 	if (*available_down < 0)
376 		*available_down = 0;
377 
378 	return 0;
379 }
380 
381 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
382 					    struct tb_port *src_port,
383 					    struct tb_port *dst_port)
384 {
385 	struct tb_tunnel *tunnel;
386 
387 	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
388 	return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
389 }
390 
391 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
392 				      struct tb_port *dst_port)
393 {
394 	int ret, available_up, available_down;
395 	struct tb_tunnel *tunnel;
396 
397 	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
398 	if (!tunnel)
399 		return;
400 
401 	tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
402 
403 	/*
404 	 * Calculate available bandwidth for the first hop USB3 tunnel.
405 	 * That determines the whole USB3 bandwidth for this branch.
406 	 */
407 	ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
408 				     &available_up, &available_down);
409 	if (ret) {
410 		tb_warn(tb, "failed to calculate available bandwidth\n");
411 		return;
412 	}
413 
414 	tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
415 	       available_up, available_down);
416 
417 	tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
418 }
419 
420 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
421 {
422 	struct tb_switch *parent = tb_switch_parent(sw);
423 	int ret, available_up, available_down;
424 	struct tb_port *up, *down, *port;
425 	struct tb_cm *tcm = tb_priv(tb);
426 	struct tb_tunnel *tunnel;
427 
428 	up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
429 	if (!up)
430 		return 0;
431 
432 	if (!sw->link_usb4)
433 		return 0;
434 
435 	/*
436 	 * Look up available down port. Since we are chaining it should
437 	 * be found right above this switch.
438 	 */
439 	port = tb_port_at(tb_route(sw), parent);
440 	down = tb_find_usb3_down(parent, port);
441 	if (!down)
442 		return 0;
443 
444 	if (tb_route(parent)) {
445 		struct tb_port *parent_up;
446 		/*
447 		 * Check first that the parent switch has its upstream USB3
448 		 * port enabled. Otherwise the chain is not complete and
449 		 * there is no point setting up a new tunnel.
450 		 */
451 		parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
452 		if (!parent_up || !tb_port_is_enabled(parent_up))
453 			return 0;
454 
455 		/* Make all unused bandwidth available for the new tunnel */
456 		ret = tb_release_unused_usb3_bandwidth(tb, down, up);
457 		if (ret)
458 			return ret;
459 	}
460 
461 	ret = tb_available_bandwidth(tb, down, up, &available_up,
462 				     &available_down);
463 	if (ret)
464 		goto err_reclaim;
465 
466 	tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
467 		    available_up, available_down);
468 
469 	tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
470 				      available_down);
471 	if (!tunnel) {
472 		ret = -ENOMEM;
473 		goto err_reclaim;
474 	}
475 
476 	if (tb_tunnel_activate(tunnel)) {
477 		tb_port_info(up,
478 			     "USB3 tunnel activation failed, aborting\n");
479 		ret = -EIO;
480 		goto err_free;
481 	}
482 
483 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
484 	if (tb_route(parent))
485 		tb_reclaim_usb3_bandwidth(tb, down, up);
486 
487 	return 0;
488 
489 err_free:
490 	tb_tunnel_free(tunnel);
491 err_reclaim:
492 	if (tb_route(parent))
493 		tb_reclaim_usb3_bandwidth(tb, down, up);
494 
495 	return ret;
496 }
497 
498 static int tb_create_usb3_tunnels(struct tb_switch *sw)
499 {
500 	struct tb_port *port;
501 	int ret;
502 
503 	if (tb_route(sw)) {
504 		ret = tb_tunnel_usb3(sw->tb, sw);
505 		if (ret)
506 			return ret;
507 	}
508 
509 	tb_switch_for_each_port(sw, port) {
510 		if (!tb_port_has_remote(port))
511 			continue;
512 		ret = tb_create_usb3_tunnels(port->remote->sw);
513 		if (ret)
514 			return ret;
515 	}
516 
517 	return 0;
518 }
519 
520 static void tb_scan_port(struct tb_port *port);
521 
522 /**
523  * tb_scan_switch() - scan for and initialize downstream switches
524  */
525 static void tb_scan_switch(struct tb_switch *sw)
526 {
527 	struct tb_port *port;
528 
529 	tb_switch_for_each_port(sw, port)
530 		tb_scan_port(port);
531 }
532 
533 /**
534  * tb_scan_port() - check for and initialize switches below port
535  */
536 static void tb_scan_port(struct tb_port *port)
537 {
538 	struct tb_cm *tcm = tb_priv(port->sw->tb);
539 	struct tb_port *upstream_port;
540 	struct tb_switch *sw;
541 
542 	if (tb_is_upstream_port(port))
543 		return;
544 
545 	if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
546 	    !tb_dp_port_is_enabled(port)) {
547 		tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
548 		tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
549 				 false);
550 		return;
551 	}
552 
553 	if (port->config.type != TB_TYPE_PORT)
554 		return;
555 	if (port->dual_link_port && port->link_nr)
556 		return; /*
557 			 * Downstream switch is reachable through two ports.
558 			 * Only scan on the primary port (link_nr == 0).
559 			 */
560 	if (tb_wait_for_port(port, false) <= 0)
561 		return;
562 	if (port->remote) {
563 		tb_port_dbg(port, "port already has a remote\n");
564 		return;
565 	}
566 
567 	tb_retimer_scan(port);
568 
569 	sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
570 			     tb_downstream_route(port));
571 	if (IS_ERR(sw)) {
572 		/*
573 		 * If there is an error accessing the connected switch
574 		 * it may be connected to another domain. Also we allow
575 		 * the other domain to be connected to a max depth switch.
576 		 */
577 		if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
578 			tb_scan_xdomain(port);
579 		return;
580 	}
581 
582 	if (tb_switch_configure(sw)) {
583 		tb_switch_put(sw);
584 		return;
585 	}
586 
587 	/*
588 	 * If there was previously another domain connected remove it
589 	 * first.
590 	 */
591 	if (port->xdomain) {
592 		tb_xdomain_remove(port->xdomain);
593 		tb_port_unconfigure_xdomain(port);
594 		port->xdomain = NULL;
595 	}
596 
597 	/*
598 	 * Do not send uevents until we have discovered all existing
599 	 * tunnels and know which switches were authorized already by
600 	 * the boot firmware.
601 	 */
602 	if (!tcm->hotplug_active)
603 		dev_set_uevent_suppress(&sw->dev, true);
604 
605 	if (tb_switch_add(sw)) {
606 		tb_switch_put(sw);
607 		return;
608 	}
609 
610 	/* Link the switches using both links if available */
611 	upstream_port = tb_upstream_port(sw);
612 	port->remote = upstream_port;
613 	upstream_port->remote = port;
614 	if (port->dual_link_port && upstream_port->dual_link_port) {
615 		port->dual_link_port->remote = upstream_port->dual_link_port;
616 		upstream_port->dual_link_port->remote = port->dual_link_port;
617 	}
618 
619 	/* Enable lane bonding if supported */
620 	tb_switch_lane_bonding_enable(sw);
621 	/* Set the link configured */
622 	tb_switch_configure_link(sw);
623 
624 	if (tb_enable_tmu(sw))
625 		tb_sw_warn(sw, "failed to enable TMU\n");
626 
627 	/* Scan upstream retimers */
628 	tb_retimer_scan(upstream_port);
629 
630 	/*
631 	 * Create USB 3.x tunnels only when the switch is plugged to the
632 	 * domain. This is because we scan the domain also during discovery
633 	 * and want to discover existing USB 3.x tunnels before we create
634 	 * any new.
635 	 */
636 	if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
637 		tb_sw_warn(sw, "USB3 tunnel creation failed\n");
638 
639 	tb_add_dp_resources(sw);
640 	tb_scan_switch(sw);
641 }
642 
643 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
644 {
645 	struct tb_port *src_port, *dst_port;
646 	struct tb *tb;
647 
648 	if (!tunnel)
649 		return;
650 
651 	tb_tunnel_deactivate(tunnel);
652 	list_del(&tunnel->list);
653 
654 	tb = tunnel->tb;
655 	src_port = tunnel->src_port;
656 	dst_port = tunnel->dst_port;
657 
658 	switch (tunnel->type) {
659 	case TB_TUNNEL_DP:
660 		/*
661 		 * In case of DP tunnel make sure the DP IN resource is
662 		 * deallocated properly.
663 		 */
664 		tb_switch_dealloc_dp_resource(src_port->sw, src_port);
665 		fallthrough;
666 
667 	case TB_TUNNEL_USB3:
668 		tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
669 		break;
670 
671 	default:
672 		/*
673 		 * PCIe and DMA tunnels do not consume guaranteed
674 		 * bandwidth.
675 		 */
676 		break;
677 	}
678 
679 	tb_tunnel_free(tunnel);
680 }
681 
682 /**
683  * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
684  */
685 static void tb_free_invalid_tunnels(struct tb *tb)
686 {
687 	struct tb_cm *tcm = tb_priv(tb);
688 	struct tb_tunnel *tunnel;
689 	struct tb_tunnel *n;
690 
691 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
692 		if (tb_tunnel_is_invalid(tunnel))
693 			tb_deactivate_and_free_tunnel(tunnel);
694 	}
695 }
696 
697 /**
698  * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
699  */
700 static void tb_free_unplugged_children(struct tb_switch *sw)
701 {
702 	struct tb_port *port;
703 
704 	tb_switch_for_each_port(sw, port) {
705 		if (!tb_port_has_remote(port))
706 			continue;
707 
708 		if (port->remote->sw->is_unplugged) {
709 			tb_retimer_remove_all(port);
710 			tb_remove_dp_resources(port->remote->sw);
711 			tb_switch_unconfigure_link(port->remote->sw);
712 			tb_switch_lane_bonding_disable(port->remote->sw);
713 			tb_switch_remove(port->remote->sw);
714 			port->remote = NULL;
715 			if (port->dual_link_port)
716 				port->dual_link_port->remote = NULL;
717 		} else {
718 			tb_free_unplugged_children(port->remote->sw);
719 		}
720 	}
721 }
722 
723 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
724 					 const struct tb_port *port)
725 {
726 	struct tb_port *down = NULL;
727 
728 	/*
729 	 * To keep plugging devices consistently in the same PCIe
730 	 * hierarchy, do mapping here for switch downstream PCIe ports.
731 	 */
732 	if (tb_switch_is_usb4(sw)) {
733 		down = usb4_switch_map_pcie_down(sw, port);
734 	} else if (!tb_route(sw)) {
735 		int phy_port = tb_phy_port_from_link(port->port);
736 		int index;
737 
738 		/*
739 		 * Hard-coded Thunderbolt port to PCIe down port mapping
740 		 * per controller.
741 		 */
742 		if (tb_switch_is_cactus_ridge(sw) ||
743 		    tb_switch_is_alpine_ridge(sw))
744 			index = !phy_port ? 6 : 7;
745 		else if (tb_switch_is_falcon_ridge(sw))
746 			index = !phy_port ? 6 : 8;
747 		else if (tb_switch_is_titan_ridge(sw))
748 			index = !phy_port ? 8 : 9;
749 		else
750 			goto out;
751 
752 		/* Validate the hard-coding */
753 		if (WARN_ON(index > sw->config.max_port_number))
754 			goto out;
755 
756 		down = &sw->ports[index];
757 	}
758 
759 	if (down) {
760 		if (WARN_ON(!tb_port_is_pcie_down(down)))
761 			goto out;
762 		if (tb_pci_port_is_enabled(down))
763 			goto out;
764 
765 		return down;
766 	}
767 
768 out:
769 	return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
770 }
771 
772 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
773 {
774 	struct tb_port *host_port, *port;
775 	struct tb_cm *tcm = tb_priv(tb);
776 
777 	host_port = tb_route(in->sw) ?
778 		tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
779 
780 	list_for_each_entry(port, &tcm->dp_resources, list) {
781 		if (!tb_port_is_dpout(port))
782 			continue;
783 
784 		if (tb_port_is_enabled(port)) {
785 			tb_port_dbg(port, "in use\n");
786 			continue;
787 		}
788 
789 		tb_port_dbg(port, "DP OUT available\n");
790 
791 		/*
792 		 * Keep the DP tunnel under the topology starting from
793 		 * the same host router downstream port.
794 		 */
795 		if (host_port && tb_route(port->sw)) {
796 			struct tb_port *p;
797 
798 			p = tb_port_at(tb_route(port->sw), tb->root_switch);
799 			if (p != host_port)
800 				continue;
801 		}
802 
803 		return port;
804 	}
805 
806 	return NULL;
807 }
808 
809 static void tb_tunnel_dp(struct tb *tb)
810 {
811 	int available_up, available_down, ret;
812 	struct tb_cm *tcm = tb_priv(tb);
813 	struct tb_port *port, *in, *out;
814 	struct tb_tunnel *tunnel;
815 
816 	/*
817 	 * Find pair of inactive DP IN and DP OUT adapters and then
818 	 * establish a DP tunnel between them.
819 	 */
820 	tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
821 
822 	in = NULL;
823 	out = NULL;
824 	list_for_each_entry(port, &tcm->dp_resources, list) {
825 		if (!tb_port_is_dpin(port))
826 			continue;
827 
828 		if (tb_port_is_enabled(port)) {
829 			tb_port_dbg(port, "in use\n");
830 			continue;
831 		}
832 
833 		tb_port_dbg(port, "DP IN available\n");
834 
835 		out = tb_find_dp_out(tb, port);
836 		if (out) {
837 			in = port;
838 			break;
839 		}
840 	}
841 
842 	if (!in) {
843 		tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
844 		return;
845 	}
846 	if (!out) {
847 		tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
848 		return;
849 	}
850 
851 	if (tb_switch_alloc_dp_resource(in->sw, in)) {
852 		tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
853 		return;
854 	}
855 
856 	/* Make all unused USB3 bandwidth available for the new DP tunnel */
857 	ret = tb_release_unused_usb3_bandwidth(tb, in, out);
858 	if (ret) {
859 		tb_warn(tb, "failed to release unused bandwidth\n");
860 		goto err_dealloc_dp;
861 	}
862 
863 	ret = tb_available_bandwidth(tb, in, out, &available_up,
864 				     &available_down);
865 	if (ret)
866 		goto err_reclaim;
867 
868 	tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
869 	       available_up, available_down);
870 
871 	tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
872 	if (!tunnel) {
873 		tb_port_dbg(out, "could not allocate DP tunnel\n");
874 		goto err_reclaim;
875 	}
876 
877 	if (tb_tunnel_activate(tunnel)) {
878 		tb_port_info(out, "DP tunnel activation failed, aborting\n");
879 		goto err_free;
880 	}
881 
882 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
883 	tb_reclaim_usb3_bandwidth(tb, in, out);
884 	return;
885 
886 err_free:
887 	tb_tunnel_free(tunnel);
888 err_reclaim:
889 	tb_reclaim_usb3_bandwidth(tb, in, out);
890 err_dealloc_dp:
891 	tb_switch_dealloc_dp_resource(in->sw, in);
892 }
893 
894 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
895 {
896 	struct tb_port *in, *out;
897 	struct tb_tunnel *tunnel;
898 
899 	if (tb_port_is_dpin(port)) {
900 		tb_port_dbg(port, "DP IN resource unavailable\n");
901 		in = port;
902 		out = NULL;
903 	} else {
904 		tb_port_dbg(port, "DP OUT resource unavailable\n");
905 		in = NULL;
906 		out = port;
907 	}
908 
909 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
910 	tb_deactivate_and_free_tunnel(tunnel);
911 	list_del_init(&port->list);
912 
913 	/*
914 	 * See if there is another DP OUT port that can be used for
915 	 * to create another tunnel.
916 	 */
917 	tb_tunnel_dp(tb);
918 }
919 
920 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
921 {
922 	struct tb_cm *tcm = tb_priv(tb);
923 	struct tb_port *p;
924 
925 	if (tb_port_is_enabled(port))
926 		return;
927 
928 	list_for_each_entry(p, &tcm->dp_resources, list) {
929 		if (p == port)
930 			return;
931 	}
932 
933 	tb_port_dbg(port, "DP %s resource available\n",
934 		    tb_port_is_dpin(port) ? "IN" : "OUT");
935 	list_add_tail(&port->list, &tcm->dp_resources);
936 
937 	/* Look for suitable DP IN <-> DP OUT pairs now */
938 	tb_tunnel_dp(tb);
939 }
940 
941 static void tb_disconnect_and_release_dp(struct tb *tb)
942 {
943 	struct tb_cm *tcm = tb_priv(tb);
944 	struct tb_tunnel *tunnel, *n;
945 
946 	/*
947 	 * Tear down all DP tunnels and release their resources. They
948 	 * will be re-established after resume based on plug events.
949 	 */
950 	list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
951 		if (tb_tunnel_is_dp(tunnel))
952 			tb_deactivate_and_free_tunnel(tunnel);
953 	}
954 
955 	while (!list_empty(&tcm->dp_resources)) {
956 		struct tb_port *port;
957 
958 		port = list_first_entry(&tcm->dp_resources,
959 					struct tb_port, list);
960 		list_del_init(&port->list);
961 	}
962 }
963 
964 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
965 {
966 	struct tb_port *up, *down, *port;
967 	struct tb_cm *tcm = tb_priv(tb);
968 	struct tb_switch *parent_sw;
969 	struct tb_tunnel *tunnel;
970 
971 	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
972 	if (!up)
973 		return 0;
974 
975 	/*
976 	 * Look up available down port. Since we are chaining it should
977 	 * be found right above this switch.
978 	 */
979 	parent_sw = tb_to_switch(sw->dev.parent);
980 	port = tb_port_at(tb_route(sw), parent_sw);
981 	down = tb_find_pcie_down(parent_sw, port);
982 	if (!down)
983 		return 0;
984 
985 	tunnel = tb_tunnel_alloc_pci(tb, up, down);
986 	if (!tunnel)
987 		return -ENOMEM;
988 
989 	if (tb_tunnel_activate(tunnel)) {
990 		tb_port_info(up,
991 			     "PCIe tunnel activation failed, aborting\n");
992 		tb_tunnel_free(tunnel);
993 		return -EIO;
994 	}
995 
996 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
997 	return 0;
998 }
999 
1000 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1001 {
1002 	struct tb_cm *tcm = tb_priv(tb);
1003 	struct tb_port *nhi_port, *dst_port;
1004 	struct tb_tunnel *tunnel;
1005 	struct tb_switch *sw;
1006 
1007 	sw = tb_to_switch(xd->dev.parent);
1008 	dst_port = tb_port_at(xd->route, sw);
1009 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1010 
1011 	mutex_lock(&tb->lock);
1012 	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
1013 				     xd->transmit_path, xd->receive_ring,
1014 				     xd->receive_path);
1015 	if (!tunnel) {
1016 		mutex_unlock(&tb->lock);
1017 		return -ENOMEM;
1018 	}
1019 
1020 	if (tb_tunnel_activate(tunnel)) {
1021 		tb_port_info(nhi_port,
1022 			     "DMA tunnel activation failed, aborting\n");
1023 		tb_tunnel_free(tunnel);
1024 		mutex_unlock(&tb->lock);
1025 		return -EIO;
1026 	}
1027 
1028 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
1029 	mutex_unlock(&tb->lock);
1030 	return 0;
1031 }
1032 
1033 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1034 {
1035 	struct tb_port *dst_port;
1036 	struct tb_tunnel *tunnel;
1037 	struct tb_switch *sw;
1038 
1039 	sw = tb_to_switch(xd->dev.parent);
1040 	dst_port = tb_port_at(xd->route, sw);
1041 
1042 	/*
1043 	 * It is possible that the tunnel was already teared down (in
1044 	 * case of cable disconnect) so it is fine if we cannot find it
1045 	 * here anymore.
1046 	 */
1047 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
1048 	tb_deactivate_and_free_tunnel(tunnel);
1049 }
1050 
1051 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1052 {
1053 	if (!xd->is_unplugged) {
1054 		mutex_lock(&tb->lock);
1055 		__tb_disconnect_xdomain_paths(tb, xd);
1056 		mutex_unlock(&tb->lock);
1057 	}
1058 	return 0;
1059 }
1060 
1061 /* hotplug handling */
1062 
1063 /**
1064  * tb_handle_hotplug() - handle hotplug event
1065  *
1066  * Executes on tb->wq.
1067  */
1068 static void tb_handle_hotplug(struct work_struct *work)
1069 {
1070 	struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1071 	struct tb *tb = ev->tb;
1072 	struct tb_cm *tcm = tb_priv(tb);
1073 	struct tb_switch *sw;
1074 	struct tb_port *port;
1075 
1076 	mutex_lock(&tb->lock);
1077 	if (!tcm->hotplug_active)
1078 		goto out; /* during init, suspend or shutdown */
1079 
1080 	sw = tb_switch_find_by_route(tb, ev->route);
1081 	if (!sw) {
1082 		tb_warn(tb,
1083 			"hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1084 			ev->route, ev->port, ev->unplug);
1085 		goto out;
1086 	}
1087 	if (ev->port > sw->config.max_port_number) {
1088 		tb_warn(tb,
1089 			"hotplug event from non existent port %llx:%x (unplug: %d)\n",
1090 			ev->route, ev->port, ev->unplug);
1091 		goto put_sw;
1092 	}
1093 	port = &sw->ports[ev->port];
1094 	if (tb_is_upstream_port(port)) {
1095 		tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1096 		       ev->route, ev->port, ev->unplug);
1097 		goto put_sw;
1098 	}
1099 	if (ev->unplug) {
1100 		tb_retimer_remove_all(port);
1101 
1102 		if (tb_port_has_remote(port)) {
1103 			tb_port_dbg(port, "switch unplugged\n");
1104 			tb_sw_set_unplugged(port->remote->sw);
1105 			tb_free_invalid_tunnels(tb);
1106 			tb_remove_dp_resources(port->remote->sw);
1107 			tb_switch_tmu_disable(port->remote->sw);
1108 			tb_switch_unconfigure_link(port->remote->sw);
1109 			tb_switch_lane_bonding_disable(port->remote->sw);
1110 			tb_switch_remove(port->remote->sw);
1111 			port->remote = NULL;
1112 			if (port->dual_link_port)
1113 				port->dual_link_port->remote = NULL;
1114 			/* Maybe we can create another DP tunnel */
1115 			tb_tunnel_dp(tb);
1116 		} else if (port->xdomain) {
1117 			struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1118 
1119 			tb_port_dbg(port, "xdomain unplugged\n");
1120 			/*
1121 			 * Service drivers are unbound during
1122 			 * tb_xdomain_remove() so setting XDomain as
1123 			 * unplugged here prevents deadlock if they call
1124 			 * tb_xdomain_disable_paths(). We will tear down
1125 			 * the path below.
1126 			 */
1127 			xd->is_unplugged = true;
1128 			tb_xdomain_remove(xd);
1129 			port->xdomain = NULL;
1130 			__tb_disconnect_xdomain_paths(tb, xd);
1131 			tb_xdomain_put(xd);
1132 			tb_port_unconfigure_xdomain(port);
1133 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1134 			tb_dp_resource_unavailable(tb, port);
1135 		} else {
1136 			tb_port_dbg(port,
1137 				   "got unplug event for disconnected port, ignoring\n");
1138 		}
1139 	} else if (port->remote) {
1140 		tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1141 	} else {
1142 		if (tb_port_is_null(port)) {
1143 			tb_port_dbg(port, "hotplug: scanning\n");
1144 			tb_scan_port(port);
1145 			if (!port->remote)
1146 				tb_port_dbg(port, "hotplug: no switch found\n");
1147 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1148 			tb_dp_resource_available(tb, port);
1149 		}
1150 	}
1151 
1152 put_sw:
1153 	tb_switch_put(sw);
1154 out:
1155 	mutex_unlock(&tb->lock);
1156 	kfree(ev);
1157 }
1158 
1159 /**
1160  * tb_schedule_hotplug_handler() - callback function for the control channel
1161  *
1162  * Delegates to tb_handle_hotplug.
1163  */
1164 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1165 			    const void *buf, size_t size)
1166 {
1167 	const struct cfg_event_pkg *pkg = buf;
1168 	u64 route;
1169 
1170 	if (type != TB_CFG_PKG_EVENT) {
1171 		tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1172 		return;
1173 	}
1174 
1175 	route = tb_cfg_get_route(&pkg->header);
1176 
1177 	if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1178 		tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1179 			pkg->port);
1180 	}
1181 
1182 	tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1183 }
1184 
1185 static void tb_stop(struct tb *tb)
1186 {
1187 	struct tb_cm *tcm = tb_priv(tb);
1188 	struct tb_tunnel *tunnel;
1189 	struct tb_tunnel *n;
1190 
1191 	/* tunnels are only present after everything has been initialized */
1192 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1193 		/*
1194 		 * DMA tunnels require the driver to be functional so we
1195 		 * tear them down. Other protocol tunnels can be left
1196 		 * intact.
1197 		 */
1198 		if (tb_tunnel_is_dma(tunnel))
1199 			tb_tunnel_deactivate(tunnel);
1200 		tb_tunnel_free(tunnel);
1201 	}
1202 	tb_switch_remove(tb->root_switch);
1203 	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1204 }
1205 
1206 static int tb_scan_finalize_switch(struct device *dev, void *data)
1207 {
1208 	if (tb_is_switch(dev)) {
1209 		struct tb_switch *sw = tb_to_switch(dev);
1210 
1211 		/*
1212 		 * If we found that the switch was already setup by the
1213 		 * boot firmware, mark it as authorized now before we
1214 		 * send uevent to userspace.
1215 		 */
1216 		if (sw->boot)
1217 			sw->authorized = 1;
1218 
1219 		dev_set_uevent_suppress(dev, false);
1220 		kobject_uevent(&dev->kobj, KOBJ_ADD);
1221 		device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1222 	}
1223 
1224 	return 0;
1225 }
1226 
1227 static int tb_start(struct tb *tb)
1228 {
1229 	struct tb_cm *tcm = tb_priv(tb);
1230 	int ret;
1231 
1232 	tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1233 	if (IS_ERR(tb->root_switch))
1234 		return PTR_ERR(tb->root_switch);
1235 
1236 	/*
1237 	 * ICM firmware upgrade needs running firmware and in native
1238 	 * mode that is not available so disable firmware upgrade of the
1239 	 * root switch.
1240 	 */
1241 	tb->root_switch->no_nvm_upgrade = true;
1242 
1243 	ret = tb_switch_configure(tb->root_switch);
1244 	if (ret) {
1245 		tb_switch_put(tb->root_switch);
1246 		return ret;
1247 	}
1248 
1249 	/* Announce the switch to the world */
1250 	ret = tb_switch_add(tb->root_switch);
1251 	if (ret) {
1252 		tb_switch_put(tb->root_switch);
1253 		return ret;
1254 	}
1255 
1256 	/* Enable TMU if it is off */
1257 	tb_switch_tmu_enable(tb->root_switch);
1258 	/* Full scan to discover devices added before the driver was loaded. */
1259 	tb_scan_switch(tb->root_switch);
1260 	/* Find out tunnels created by the boot firmware */
1261 	tb_discover_tunnels(tb->root_switch);
1262 	/*
1263 	 * If the boot firmware did not create USB 3.x tunnels create them
1264 	 * now for the whole topology.
1265 	 */
1266 	tb_create_usb3_tunnels(tb->root_switch);
1267 	/* Add DP IN resources for the root switch */
1268 	tb_add_dp_resources(tb->root_switch);
1269 	/* Make the discovered switches available to the userspace */
1270 	device_for_each_child(&tb->root_switch->dev, NULL,
1271 			      tb_scan_finalize_switch);
1272 
1273 	/* Allow tb_handle_hotplug to progress events */
1274 	tcm->hotplug_active = true;
1275 	return 0;
1276 }
1277 
1278 static int tb_suspend_noirq(struct tb *tb)
1279 {
1280 	struct tb_cm *tcm = tb_priv(tb);
1281 
1282 	tb_dbg(tb, "suspending...\n");
1283 	tb_disconnect_and_release_dp(tb);
1284 	tb_switch_suspend(tb->root_switch);
1285 	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1286 	tb_dbg(tb, "suspend finished\n");
1287 
1288 	return 0;
1289 }
1290 
1291 static void tb_restore_children(struct tb_switch *sw)
1292 {
1293 	struct tb_port *port;
1294 
1295 	if (tb_enable_tmu(sw))
1296 		tb_sw_warn(sw, "failed to restore TMU configuration\n");
1297 
1298 	tb_switch_for_each_port(sw, port) {
1299 		if (!tb_port_has_remote(port) && !port->xdomain)
1300 			continue;
1301 
1302 		if (port->remote) {
1303 			tb_switch_lane_bonding_enable(port->remote->sw);
1304 			tb_switch_configure_link(port->remote->sw);
1305 
1306 			tb_restore_children(port->remote->sw);
1307 		} else if (port->xdomain) {
1308 			tb_port_configure_xdomain(port);
1309 		}
1310 	}
1311 }
1312 
1313 static int tb_resume_noirq(struct tb *tb)
1314 {
1315 	struct tb_cm *tcm = tb_priv(tb);
1316 	struct tb_tunnel *tunnel, *n;
1317 
1318 	tb_dbg(tb, "resuming...\n");
1319 
1320 	/* remove any pci devices the firmware might have setup */
1321 	tb_switch_reset(tb->root_switch);
1322 
1323 	tb_switch_resume(tb->root_switch);
1324 	tb_free_invalid_tunnels(tb);
1325 	tb_free_unplugged_children(tb->root_switch);
1326 	tb_restore_children(tb->root_switch);
1327 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1328 		tb_tunnel_restart(tunnel);
1329 	if (!list_empty(&tcm->tunnel_list)) {
1330 		/*
1331 		 * the pcie links need some time to get going.
1332 		 * 100ms works for me...
1333 		 */
1334 		tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1335 		msleep(100);
1336 	}
1337 	 /* Allow tb_handle_hotplug to progress events */
1338 	tcm->hotplug_active = true;
1339 	tb_dbg(tb, "resume finished\n");
1340 
1341 	return 0;
1342 }
1343 
1344 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1345 {
1346 	struct tb_port *port;
1347 	int ret = 0;
1348 
1349 	tb_switch_for_each_port(sw, port) {
1350 		if (tb_is_upstream_port(port))
1351 			continue;
1352 		if (port->xdomain && port->xdomain->is_unplugged) {
1353 			tb_retimer_remove_all(port);
1354 			tb_xdomain_remove(port->xdomain);
1355 			tb_port_unconfigure_xdomain(port);
1356 			port->xdomain = NULL;
1357 			ret++;
1358 		} else if (port->remote) {
1359 			ret += tb_free_unplugged_xdomains(port->remote->sw);
1360 		}
1361 	}
1362 
1363 	return ret;
1364 }
1365 
1366 static void tb_complete(struct tb *tb)
1367 {
1368 	/*
1369 	 * Release any unplugged XDomains and if there is a case where
1370 	 * another domain is swapped in place of unplugged XDomain we
1371 	 * need to run another rescan.
1372 	 */
1373 	mutex_lock(&tb->lock);
1374 	if (tb_free_unplugged_xdomains(tb->root_switch))
1375 		tb_scan_switch(tb->root_switch);
1376 	mutex_unlock(&tb->lock);
1377 }
1378 
1379 static const struct tb_cm_ops tb_cm_ops = {
1380 	.start = tb_start,
1381 	.stop = tb_stop,
1382 	.suspend_noirq = tb_suspend_noirq,
1383 	.resume_noirq = tb_resume_noirq,
1384 	.complete = tb_complete,
1385 	.handle_event = tb_handle_event,
1386 	.approve_switch = tb_tunnel_pci,
1387 	.approve_xdomain_paths = tb_approve_xdomain_paths,
1388 	.disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1389 };
1390 
1391 struct tb *tb_probe(struct tb_nhi *nhi)
1392 {
1393 	struct tb_cm *tcm;
1394 	struct tb *tb;
1395 
1396 	tb = tb_domain_alloc(nhi, sizeof(*tcm));
1397 	if (!tb)
1398 		return NULL;
1399 
1400 	tb->security_level = TB_SECURITY_USER;
1401 	tb->cm_ops = &tb_cm_ops;
1402 
1403 	tcm = tb_priv(tb);
1404 	INIT_LIST_HEAD(&tcm->tunnel_list);
1405 	INIT_LIST_HEAD(&tcm->dp_resources);
1406 
1407 	return tb;
1408 }
1409