xref: /openbmc/linux/drivers/thunderbolt/tb.c (revision b0407983)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thunderbolt driver - bus logic (NHI independent)
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2019, Intel Corporation
7  */
8 
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 
13 #include "tb.h"
14 #include "tb_regs.h"
15 #include "tunnel.h"
16 
17 /**
18  * struct tb_cm - Simple Thunderbolt connection manager
19  * @tunnel_list: List of active tunnels
20  * @dp_resources: List of available DP resources for DP tunneling
21  * @hotplug_active: tb_handle_hotplug will stop progressing plug
22  *		    events and exit if this is not set (it needs to
23  *		    acquire the lock one more time). Used to drain wq
24  *		    after cfg has been paused.
25  */
26 struct tb_cm {
27 	struct list_head tunnel_list;
28 	struct list_head dp_resources;
29 	bool hotplug_active;
30 };
31 
32 struct tb_hotplug_event {
33 	struct work_struct work;
34 	struct tb *tb;
35 	u64 route;
36 	u8 port;
37 	bool unplug;
38 };
39 
40 static void tb_handle_hotplug(struct work_struct *work);
41 
42 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
43 {
44 	struct tb_hotplug_event *ev;
45 
46 	ev = kmalloc(sizeof(*ev), GFP_KERNEL);
47 	if (!ev)
48 		return;
49 
50 	ev->tb = tb;
51 	ev->route = route;
52 	ev->port = port;
53 	ev->unplug = unplug;
54 	INIT_WORK(&ev->work, tb_handle_hotplug);
55 	queue_work(tb->wq, &ev->work);
56 }
57 
58 /* enumeration & hot plug handling */
59 
60 static void tb_add_dp_resources(struct tb_switch *sw)
61 {
62 	struct tb_cm *tcm = tb_priv(sw->tb);
63 	struct tb_port *port;
64 
65 	tb_switch_for_each_port(sw, port) {
66 		if (!tb_port_is_dpin(port))
67 			continue;
68 
69 		if (!tb_switch_query_dp_resource(sw, port))
70 			continue;
71 
72 		list_add_tail(&port->list, &tcm->dp_resources);
73 		tb_port_dbg(port, "DP IN resource available\n");
74 	}
75 }
76 
77 static void tb_remove_dp_resources(struct tb_switch *sw)
78 {
79 	struct tb_cm *tcm = tb_priv(sw->tb);
80 	struct tb_port *port, *tmp;
81 
82 	/* Clear children resources first */
83 	tb_switch_for_each_port(sw, port) {
84 		if (tb_port_has_remote(port))
85 			tb_remove_dp_resources(port->remote->sw);
86 	}
87 
88 	list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
89 		if (port->sw == sw) {
90 			tb_port_dbg(port, "DP OUT resource unavailable\n");
91 			list_del_init(&port->list);
92 		}
93 	}
94 }
95 
96 static void tb_discover_tunnels(struct tb_switch *sw)
97 {
98 	struct tb *tb = sw->tb;
99 	struct tb_cm *tcm = tb_priv(tb);
100 	struct tb_port *port;
101 
102 	tb_switch_for_each_port(sw, port) {
103 		struct tb_tunnel *tunnel = NULL;
104 
105 		switch (port->config.type) {
106 		case TB_TYPE_DP_HDMI_IN:
107 			tunnel = tb_tunnel_discover_dp(tb, port);
108 			break;
109 
110 		case TB_TYPE_PCIE_DOWN:
111 			tunnel = tb_tunnel_discover_pci(tb, port);
112 			break;
113 
114 		default:
115 			break;
116 		}
117 
118 		if (!tunnel)
119 			continue;
120 
121 		if (tb_tunnel_is_pci(tunnel)) {
122 			struct tb_switch *parent = tunnel->dst_port->sw;
123 
124 			while (parent != tunnel->src_port->sw) {
125 				parent->boot = true;
126 				parent = tb_switch_parent(parent);
127 			}
128 		}
129 
130 		list_add_tail(&tunnel->list, &tcm->tunnel_list);
131 	}
132 
133 	tb_switch_for_each_port(sw, port) {
134 		if (tb_port_has_remote(port))
135 			tb_discover_tunnels(port->remote->sw);
136 	}
137 }
138 
139 static void tb_scan_xdomain(struct tb_port *port)
140 {
141 	struct tb_switch *sw = port->sw;
142 	struct tb *tb = sw->tb;
143 	struct tb_xdomain *xd;
144 	u64 route;
145 
146 	route = tb_downstream_route(port);
147 	xd = tb_xdomain_find_by_route(tb, route);
148 	if (xd) {
149 		tb_xdomain_put(xd);
150 		return;
151 	}
152 
153 	xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
154 			      NULL);
155 	if (xd) {
156 		tb_port_at(route, sw)->xdomain = xd;
157 		tb_xdomain_add(xd);
158 	}
159 }
160 
161 static void tb_scan_port(struct tb_port *port);
162 
163 /**
164  * tb_scan_switch() - scan for and initialize downstream switches
165  */
166 static void tb_scan_switch(struct tb_switch *sw)
167 {
168 	struct tb_port *port;
169 
170 	tb_switch_for_each_port(sw, port)
171 		tb_scan_port(port);
172 }
173 
174 /**
175  * tb_scan_port() - check for and initialize switches below port
176  */
177 static void tb_scan_port(struct tb_port *port)
178 {
179 	struct tb_cm *tcm = tb_priv(port->sw->tb);
180 	struct tb_port *upstream_port;
181 	struct tb_switch *sw;
182 
183 	if (tb_is_upstream_port(port))
184 		return;
185 
186 	if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
187 	    !tb_dp_port_is_enabled(port)) {
188 		tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
189 		tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
190 				 false);
191 		return;
192 	}
193 
194 	if (port->config.type != TB_TYPE_PORT)
195 		return;
196 	if (port->dual_link_port && port->link_nr)
197 		return; /*
198 			 * Downstream switch is reachable through two ports.
199 			 * Only scan on the primary port (link_nr == 0).
200 			 */
201 	if (tb_wait_for_port(port, false) <= 0)
202 		return;
203 	if (port->remote) {
204 		tb_port_dbg(port, "port already has a remote\n");
205 		return;
206 	}
207 	sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
208 			     tb_downstream_route(port));
209 	if (IS_ERR(sw)) {
210 		/*
211 		 * If there is an error accessing the connected switch
212 		 * it may be connected to another domain. Also we allow
213 		 * the other domain to be connected to a max depth switch.
214 		 */
215 		if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
216 			tb_scan_xdomain(port);
217 		return;
218 	}
219 
220 	if (tb_switch_configure(sw)) {
221 		tb_switch_put(sw);
222 		return;
223 	}
224 
225 	/*
226 	 * If there was previously another domain connected remove it
227 	 * first.
228 	 */
229 	if (port->xdomain) {
230 		tb_xdomain_remove(port->xdomain);
231 		port->xdomain = NULL;
232 	}
233 
234 	/*
235 	 * Do not send uevents until we have discovered all existing
236 	 * tunnels and know which switches were authorized already by
237 	 * the boot firmware.
238 	 */
239 	if (!tcm->hotplug_active)
240 		dev_set_uevent_suppress(&sw->dev, true);
241 
242 	if (tb_switch_add(sw)) {
243 		tb_switch_put(sw);
244 		return;
245 	}
246 
247 	/* Link the switches using both links if available */
248 	upstream_port = tb_upstream_port(sw);
249 	port->remote = upstream_port;
250 	upstream_port->remote = port;
251 	if (port->dual_link_port && upstream_port->dual_link_port) {
252 		port->dual_link_port->remote = upstream_port->dual_link_port;
253 		upstream_port->dual_link_port->remote = port->dual_link_port;
254 	}
255 
256 	/* Enable lane bonding if supported */
257 	if (tb_switch_lane_bonding_enable(sw))
258 		tb_sw_warn(sw, "failed to enable lane bonding\n");
259 
260 	tb_scan_switch(sw);
261 }
262 
263 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
264 					struct tb_port *src_port,
265 					struct tb_port *dst_port)
266 {
267 	struct tb_cm *tcm = tb_priv(tb);
268 	struct tb_tunnel *tunnel;
269 
270 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
271 		if (tunnel->type == type &&
272 		    ((src_port && src_port == tunnel->src_port) ||
273 		     (dst_port && dst_port == tunnel->dst_port))) {
274 			return tunnel;
275 		}
276 	}
277 
278 	return NULL;
279 }
280 
281 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
282 {
283 	if (!tunnel)
284 		return;
285 
286 	tb_tunnel_deactivate(tunnel);
287 	list_del(&tunnel->list);
288 
289 	/*
290 	 * In case of DP tunnel make sure the DP IN resource is deallocated
291 	 * properly.
292 	 */
293 	if (tb_tunnel_is_dp(tunnel)) {
294 		struct tb_port *in = tunnel->src_port;
295 
296 		tb_switch_dealloc_dp_resource(in->sw, in);
297 	}
298 
299 	tb_tunnel_free(tunnel);
300 }
301 
302 /**
303  * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
304  */
305 static void tb_free_invalid_tunnels(struct tb *tb)
306 {
307 	struct tb_cm *tcm = tb_priv(tb);
308 	struct tb_tunnel *tunnel;
309 	struct tb_tunnel *n;
310 
311 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
312 		if (tb_tunnel_is_invalid(tunnel))
313 			tb_deactivate_and_free_tunnel(tunnel);
314 	}
315 }
316 
317 /**
318  * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
319  */
320 static void tb_free_unplugged_children(struct tb_switch *sw)
321 {
322 	struct tb_port *port;
323 
324 	tb_switch_for_each_port(sw, port) {
325 		if (!tb_port_has_remote(port))
326 			continue;
327 
328 		if (port->remote->sw->is_unplugged) {
329 			tb_remove_dp_resources(port->remote->sw);
330 			tb_switch_lane_bonding_disable(port->remote->sw);
331 			tb_switch_remove(port->remote->sw);
332 			port->remote = NULL;
333 			if (port->dual_link_port)
334 				port->dual_link_port->remote = NULL;
335 		} else {
336 			tb_free_unplugged_children(port->remote->sw);
337 		}
338 	}
339 }
340 
341 /**
342  * tb_find_unused_port() - return the first inactive port on @sw
343  * @sw: Switch to find the port on
344  * @type: Port type to look for
345  */
346 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
347 					   enum tb_port_type type)
348 {
349 	struct tb_port *port;
350 
351 	tb_switch_for_each_port(sw, port) {
352 		if (tb_is_upstream_port(port))
353 			continue;
354 		if (port->config.type != type)
355 			continue;
356 		if (port->cap_adap)
357 			continue;
358 		if (tb_port_is_enabled(port))
359 			continue;
360 		return port;
361 	}
362 	return NULL;
363 }
364 
365 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
366 					 const struct tb_port *port)
367 {
368 	struct tb_port *down = NULL;
369 
370 	/*
371 	 * To keep plugging devices consistently in the same PCIe
372 	 * hierarchy, do mapping here for switch downstream PCIe ports.
373 	 */
374 	if (tb_switch_is_usb4(sw)) {
375 		down = usb4_switch_map_pcie_down(sw, port);
376 	} else if (!tb_route(sw)) {
377 		int phy_port = tb_phy_port_from_link(port->port);
378 		int index;
379 
380 		/*
381 		 * Hard-coded Thunderbolt port to PCIe down port mapping
382 		 * per controller.
383 		 */
384 		if (tb_switch_is_cactus_ridge(sw) ||
385 		    tb_switch_is_alpine_ridge(sw))
386 			index = !phy_port ? 6 : 7;
387 		else if (tb_switch_is_falcon_ridge(sw))
388 			index = !phy_port ? 6 : 8;
389 		else if (tb_switch_is_titan_ridge(sw))
390 			index = !phy_port ? 8 : 9;
391 		else
392 			goto out;
393 
394 		/* Validate the hard-coding */
395 		if (WARN_ON(index > sw->config.max_port_number))
396 			goto out;
397 
398 		down = &sw->ports[index];
399 	}
400 
401 	if (down) {
402 		if (WARN_ON(!tb_port_is_pcie_down(down)))
403 			goto out;
404 		if (WARN_ON(tb_pci_port_is_enabled(down)))
405 			goto out;
406 
407 		return down;
408 	}
409 
410 out:
411 	return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
412 }
413 
414 static int tb_available_bw(struct tb_cm *tcm, struct tb_port *in,
415 			   struct tb_port *out)
416 {
417 	struct tb_switch *sw = out->sw;
418 	struct tb_tunnel *tunnel;
419 	int bw, available_bw = 40000;
420 
421 	while (sw && sw != in->sw) {
422 		bw = sw->link_speed * sw->link_width * 1000; /* Mb/s */
423 		/* Leave 10% guard band */
424 		bw -= bw / 10;
425 
426 		/*
427 		 * Check for any active DP tunnels that go through this
428 		 * switch and reduce their consumed bandwidth from
429 		 * available.
430 		 */
431 		list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
432 			int consumed_bw;
433 
434 			if (!tb_tunnel_switch_on_path(tunnel, sw))
435 				continue;
436 
437 			consumed_bw = tb_tunnel_consumed_bandwidth(tunnel);
438 			if (consumed_bw < 0)
439 				return consumed_bw;
440 
441 			bw -= consumed_bw;
442 		}
443 
444 		if (bw < available_bw)
445 			available_bw = bw;
446 
447 		sw = tb_switch_parent(sw);
448 	}
449 
450 	return available_bw;
451 }
452 
453 static void tb_tunnel_dp(struct tb *tb)
454 {
455 	struct tb_cm *tcm = tb_priv(tb);
456 	struct tb_port *port, *in, *out;
457 	struct tb_tunnel *tunnel;
458 	int available_bw;
459 
460 	/*
461 	 * Find pair of inactive DP IN and DP OUT adapters and then
462 	 * establish a DP tunnel between them.
463 	 */
464 	tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
465 
466 	in = NULL;
467 	out = NULL;
468 	list_for_each_entry(port, &tcm->dp_resources, list) {
469 		if (tb_port_is_enabled(port)) {
470 			tb_port_dbg(port, "in use\n");
471 			continue;
472 		}
473 
474 		tb_port_dbg(port, "available\n");
475 
476 		if (!in && tb_port_is_dpin(port))
477 			in = port;
478 		else if (!out && tb_port_is_dpout(port))
479 			out = port;
480 	}
481 
482 	if (!in) {
483 		tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
484 		return;
485 	}
486 	if (!out) {
487 		tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
488 		return;
489 	}
490 
491 	if (tb_switch_alloc_dp_resource(in->sw, in)) {
492 		tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
493 		return;
494 	}
495 
496 	/* Calculate available bandwidth between in and out */
497 	available_bw = tb_available_bw(tcm, in, out);
498 	if (available_bw < 0) {
499 		tb_warn(tb, "failed to determine available bandwidth\n");
500 		return;
501 	}
502 
503 	tb_dbg(tb, "available bandwidth for new DP tunnel %u Mb/s\n",
504 	       available_bw);
505 
506 	tunnel = tb_tunnel_alloc_dp(tb, in, out, available_bw);
507 	if (!tunnel) {
508 		tb_port_dbg(out, "could not allocate DP tunnel\n");
509 		goto dealloc_dp;
510 	}
511 
512 	if (tb_tunnel_activate(tunnel)) {
513 		tb_port_info(out, "DP tunnel activation failed, aborting\n");
514 		tb_tunnel_free(tunnel);
515 		goto dealloc_dp;
516 	}
517 
518 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
519 	return;
520 
521 dealloc_dp:
522 	tb_switch_dealloc_dp_resource(in->sw, in);
523 }
524 
525 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
526 {
527 	struct tb_port *in, *out;
528 	struct tb_tunnel *tunnel;
529 
530 	if (tb_port_is_dpin(port)) {
531 		tb_port_dbg(port, "DP IN resource unavailable\n");
532 		in = port;
533 		out = NULL;
534 	} else {
535 		tb_port_dbg(port, "DP OUT resource unavailable\n");
536 		in = NULL;
537 		out = port;
538 	}
539 
540 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
541 	tb_deactivate_and_free_tunnel(tunnel);
542 	list_del_init(&port->list);
543 
544 	/*
545 	 * See if there is another DP OUT port that can be used for
546 	 * to create another tunnel.
547 	 */
548 	tb_tunnel_dp(tb);
549 }
550 
551 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
552 {
553 	struct tb_cm *tcm = tb_priv(tb);
554 	struct tb_port *p;
555 
556 	if (tb_port_is_enabled(port))
557 		return;
558 
559 	list_for_each_entry(p, &tcm->dp_resources, list) {
560 		if (p == port)
561 			return;
562 	}
563 
564 	tb_port_dbg(port, "DP %s resource available\n",
565 		    tb_port_is_dpin(port) ? "IN" : "OUT");
566 	list_add_tail(&port->list, &tcm->dp_resources);
567 
568 	/* Look for suitable DP IN <-> DP OUT pairs now */
569 	tb_tunnel_dp(tb);
570 }
571 
572 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
573 {
574 	struct tb_port *up, *down, *port;
575 	struct tb_cm *tcm = tb_priv(tb);
576 	struct tb_switch *parent_sw;
577 	struct tb_tunnel *tunnel;
578 
579 	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
580 	if (!up)
581 		return 0;
582 
583 	/*
584 	 * Look up available down port. Since we are chaining it should
585 	 * be found right above this switch.
586 	 */
587 	parent_sw = tb_to_switch(sw->dev.parent);
588 	port = tb_port_at(tb_route(sw), parent_sw);
589 	down = tb_find_pcie_down(parent_sw, port);
590 	if (!down)
591 		return 0;
592 
593 	tunnel = tb_tunnel_alloc_pci(tb, up, down);
594 	if (!tunnel)
595 		return -ENOMEM;
596 
597 	if (tb_tunnel_activate(tunnel)) {
598 		tb_port_info(up,
599 			     "PCIe tunnel activation failed, aborting\n");
600 		tb_tunnel_free(tunnel);
601 		return -EIO;
602 	}
603 
604 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
605 	return 0;
606 }
607 
608 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
609 {
610 	struct tb_cm *tcm = tb_priv(tb);
611 	struct tb_port *nhi_port, *dst_port;
612 	struct tb_tunnel *tunnel;
613 	struct tb_switch *sw;
614 
615 	sw = tb_to_switch(xd->dev.parent);
616 	dst_port = tb_port_at(xd->route, sw);
617 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
618 
619 	mutex_lock(&tb->lock);
620 	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
621 				     xd->transmit_path, xd->receive_ring,
622 				     xd->receive_path);
623 	if (!tunnel) {
624 		mutex_unlock(&tb->lock);
625 		return -ENOMEM;
626 	}
627 
628 	if (tb_tunnel_activate(tunnel)) {
629 		tb_port_info(nhi_port,
630 			     "DMA tunnel activation failed, aborting\n");
631 		tb_tunnel_free(tunnel);
632 		mutex_unlock(&tb->lock);
633 		return -EIO;
634 	}
635 
636 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
637 	mutex_unlock(&tb->lock);
638 	return 0;
639 }
640 
641 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
642 {
643 	struct tb_port *dst_port;
644 	struct tb_tunnel *tunnel;
645 	struct tb_switch *sw;
646 
647 	sw = tb_to_switch(xd->dev.parent);
648 	dst_port = tb_port_at(xd->route, sw);
649 
650 	/*
651 	 * It is possible that the tunnel was already teared down (in
652 	 * case of cable disconnect) so it is fine if we cannot find it
653 	 * here anymore.
654 	 */
655 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
656 	tb_deactivate_and_free_tunnel(tunnel);
657 }
658 
659 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
660 {
661 	if (!xd->is_unplugged) {
662 		mutex_lock(&tb->lock);
663 		__tb_disconnect_xdomain_paths(tb, xd);
664 		mutex_unlock(&tb->lock);
665 	}
666 	return 0;
667 }
668 
669 /* hotplug handling */
670 
671 /**
672  * tb_handle_hotplug() - handle hotplug event
673  *
674  * Executes on tb->wq.
675  */
676 static void tb_handle_hotplug(struct work_struct *work)
677 {
678 	struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
679 	struct tb *tb = ev->tb;
680 	struct tb_cm *tcm = tb_priv(tb);
681 	struct tb_switch *sw;
682 	struct tb_port *port;
683 	mutex_lock(&tb->lock);
684 	if (!tcm->hotplug_active)
685 		goto out; /* during init, suspend or shutdown */
686 
687 	sw = tb_switch_find_by_route(tb, ev->route);
688 	if (!sw) {
689 		tb_warn(tb,
690 			"hotplug event from non existent switch %llx:%x (unplug: %d)\n",
691 			ev->route, ev->port, ev->unplug);
692 		goto out;
693 	}
694 	if (ev->port > sw->config.max_port_number) {
695 		tb_warn(tb,
696 			"hotplug event from non existent port %llx:%x (unplug: %d)\n",
697 			ev->route, ev->port, ev->unplug);
698 		goto put_sw;
699 	}
700 	port = &sw->ports[ev->port];
701 	if (tb_is_upstream_port(port)) {
702 		tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
703 		       ev->route, ev->port, ev->unplug);
704 		goto put_sw;
705 	}
706 	if (ev->unplug) {
707 		if (tb_port_has_remote(port)) {
708 			tb_port_dbg(port, "switch unplugged\n");
709 			tb_sw_set_unplugged(port->remote->sw);
710 			tb_free_invalid_tunnels(tb);
711 			tb_remove_dp_resources(port->remote->sw);
712 			tb_switch_lane_bonding_disable(port->remote->sw);
713 			tb_switch_remove(port->remote->sw);
714 			port->remote = NULL;
715 			if (port->dual_link_port)
716 				port->dual_link_port->remote = NULL;
717 			/* Maybe we can create another DP tunnel */
718 			tb_tunnel_dp(tb);
719 		} else if (port->xdomain) {
720 			struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
721 
722 			tb_port_dbg(port, "xdomain unplugged\n");
723 			/*
724 			 * Service drivers are unbound during
725 			 * tb_xdomain_remove() so setting XDomain as
726 			 * unplugged here prevents deadlock if they call
727 			 * tb_xdomain_disable_paths(). We will tear down
728 			 * the path below.
729 			 */
730 			xd->is_unplugged = true;
731 			tb_xdomain_remove(xd);
732 			port->xdomain = NULL;
733 			__tb_disconnect_xdomain_paths(tb, xd);
734 			tb_xdomain_put(xd);
735 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
736 			tb_dp_resource_unavailable(tb, port);
737 		} else {
738 			tb_port_dbg(port,
739 				   "got unplug event for disconnected port, ignoring\n");
740 		}
741 	} else if (port->remote) {
742 		tb_port_dbg(port, "got plug event for connected port, ignoring\n");
743 	} else {
744 		if (tb_port_is_null(port)) {
745 			tb_port_dbg(port, "hotplug: scanning\n");
746 			tb_scan_port(port);
747 			if (!port->remote)
748 				tb_port_dbg(port, "hotplug: no switch found\n");
749 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
750 			tb_dp_resource_available(tb, port);
751 		}
752 	}
753 
754 put_sw:
755 	tb_switch_put(sw);
756 out:
757 	mutex_unlock(&tb->lock);
758 	kfree(ev);
759 }
760 
761 /**
762  * tb_schedule_hotplug_handler() - callback function for the control channel
763  *
764  * Delegates to tb_handle_hotplug.
765  */
766 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
767 			    const void *buf, size_t size)
768 {
769 	const struct cfg_event_pkg *pkg = buf;
770 	u64 route;
771 
772 	if (type != TB_CFG_PKG_EVENT) {
773 		tb_warn(tb, "unexpected event %#x, ignoring\n", type);
774 		return;
775 	}
776 
777 	route = tb_cfg_get_route(&pkg->header);
778 
779 	if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
780 		tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
781 			pkg->port);
782 	}
783 
784 	tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
785 }
786 
787 static void tb_stop(struct tb *tb)
788 {
789 	struct tb_cm *tcm = tb_priv(tb);
790 	struct tb_tunnel *tunnel;
791 	struct tb_tunnel *n;
792 
793 	/* tunnels are only present after everything has been initialized */
794 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
795 		/*
796 		 * DMA tunnels require the driver to be functional so we
797 		 * tear them down. Other protocol tunnels can be left
798 		 * intact.
799 		 */
800 		if (tb_tunnel_is_dma(tunnel))
801 			tb_tunnel_deactivate(tunnel);
802 		tb_tunnel_free(tunnel);
803 	}
804 	tb_switch_remove(tb->root_switch);
805 	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
806 }
807 
808 static int tb_scan_finalize_switch(struct device *dev, void *data)
809 {
810 	if (tb_is_switch(dev)) {
811 		struct tb_switch *sw = tb_to_switch(dev);
812 
813 		/*
814 		 * If we found that the switch was already setup by the
815 		 * boot firmware, mark it as authorized now before we
816 		 * send uevent to userspace.
817 		 */
818 		if (sw->boot)
819 			sw->authorized = 1;
820 
821 		dev_set_uevent_suppress(dev, false);
822 		kobject_uevent(&dev->kobj, KOBJ_ADD);
823 		device_for_each_child(dev, NULL, tb_scan_finalize_switch);
824 	}
825 
826 	return 0;
827 }
828 
829 static int tb_start(struct tb *tb)
830 {
831 	struct tb_cm *tcm = tb_priv(tb);
832 	int ret;
833 
834 	tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
835 	if (IS_ERR(tb->root_switch))
836 		return PTR_ERR(tb->root_switch);
837 
838 	/*
839 	 * ICM firmware upgrade needs running firmware and in native
840 	 * mode that is not available so disable firmware upgrade of the
841 	 * root switch.
842 	 */
843 	tb->root_switch->no_nvm_upgrade = true;
844 
845 	ret = tb_switch_configure(tb->root_switch);
846 	if (ret) {
847 		tb_switch_put(tb->root_switch);
848 		return ret;
849 	}
850 
851 	/* Announce the switch to the world */
852 	ret = tb_switch_add(tb->root_switch);
853 	if (ret) {
854 		tb_switch_put(tb->root_switch);
855 		return ret;
856 	}
857 
858 	/* Full scan to discover devices added before the driver was loaded. */
859 	tb_scan_switch(tb->root_switch);
860 	/* Find out tunnels created by the boot firmware */
861 	tb_discover_tunnels(tb->root_switch);
862 	/* Add DP IN resources for the root switch */
863 	tb_add_dp_resources(tb->root_switch);
864 	/* Make the discovered switches available to the userspace */
865 	device_for_each_child(&tb->root_switch->dev, NULL,
866 			      tb_scan_finalize_switch);
867 
868 	/* Allow tb_handle_hotplug to progress events */
869 	tcm->hotplug_active = true;
870 	return 0;
871 }
872 
873 static int tb_suspend_noirq(struct tb *tb)
874 {
875 	struct tb_cm *tcm = tb_priv(tb);
876 
877 	tb_dbg(tb, "suspending...\n");
878 	tb_switch_suspend(tb->root_switch);
879 	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
880 	tb_dbg(tb, "suspend finished\n");
881 
882 	return 0;
883 }
884 
885 static void tb_restore_children(struct tb_switch *sw)
886 {
887 	struct tb_port *port;
888 
889 	tb_switch_for_each_port(sw, port) {
890 		if (!tb_port_has_remote(port))
891 			continue;
892 
893 		if (tb_switch_lane_bonding_enable(port->remote->sw))
894 			dev_warn(&sw->dev, "failed to restore lane bonding\n");
895 
896 		tb_restore_children(port->remote->sw);
897 	}
898 }
899 
900 static int tb_resume_noirq(struct tb *tb)
901 {
902 	struct tb_cm *tcm = tb_priv(tb);
903 	struct tb_tunnel *tunnel, *n;
904 
905 	tb_dbg(tb, "resuming...\n");
906 
907 	/* remove any pci devices the firmware might have setup */
908 	tb_switch_reset(tb, 0);
909 
910 	tb_switch_resume(tb->root_switch);
911 	tb_free_invalid_tunnels(tb);
912 	tb_free_unplugged_children(tb->root_switch);
913 	tb_restore_children(tb->root_switch);
914 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
915 		tb_tunnel_restart(tunnel);
916 	if (!list_empty(&tcm->tunnel_list)) {
917 		/*
918 		 * the pcie links need some time to get going.
919 		 * 100ms works for me...
920 		 */
921 		tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
922 		msleep(100);
923 	}
924 	 /* Allow tb_handle_hotplug to progress events */
925 	tcm->hotplug_active = true;
926 	tb_dbg(tb, "resume finished\n");
927 
928 	return 0;
929 }
930 
931 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
932 {
933 	struct tb_port *port;
934 	int ret = 0;
935 
936 	tb_switch_for_each_port(sw, port) {
937 		if (tb_is_upstream_port(port))
938 			continue;
939 		if (port->xdomain && port->xdomain->is_unplugged) {
940 			tb_xdomain_remove(port->xdomain);
941 			port->xdomain = NULL;
942 			ret++;
943 		} else if (port->remote) {
944 			ret += tb_free_unplugged_xdomains(port->remote->sw);
945 		}
946 	}
947 
948 	return ret;
949 }
950 
951 static void tb_complete(struct tb *tb)
952 {
953 	/*
954 	 * Release any unplugged XDomains and if there is a case where
955 	 * another domain is swapped in place of unplugged XDomain we
956 	 * need to run another rescan.
957 	 */
958 	mutex_lock(&tb->lock);
959 	if (tb_free_unplugged_xdomains(tb->root_switch))
960 		tb_scan_switch(tb->root_switch);
961 	mutex_unlock(&tb->lock);
962 }
963 
964 static const struct tb_cm_ops tb_cm_ops = {
965 	.start = tb_start,
966 	.stop = tb_stop,
967 	.suspend_noirq = tb_suspend_noirq,
968 	.resume_noirq = tb_resume_noirq,
969 	.complete = tb_complete,
970 	.handle_event = tb_handle_event,
971 	.approve_switch = tb_tunnel_pci,
972 	.approve_xdomain_paths = tb_approve_xdomain_paths,
973 	.disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
974 };
975 
976 struct tb *tb_probe(struct tb_nhi *nhi)
977 {
978 	struct tb_cm *tcm;
979 	struct tb *tb;
980 
981 	tb = tb_domain_alloc(nhi, sizeof(*tcm));
982 	if (!tb)
983 		return NULL;
984 
985 	tb->security_level = TB_SECURITY_USER;
986 	tb->cm_ops = &tb_cm_ops;
987 
988 	tcm = tb_priv(tb);
989 	INIT_LIST_HEAD(&tcm->tunnel_list);
990 	INIT_LIST_HEAD(&tcm->dp_resources);
991 
992 	return tb;
993 }
994