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