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