xref: /openbmc/linux/drivers/thunderbolt/tb.c (revision 3dfbe6a73ae80429ccd268749e91c0d8d1526107)
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  * Minimum bandwidth (in Mb/s) that is needed in the single transmitter/receiver
23  * direction. This is 40G - 10% guard band bandwidth.
24  */
25 #define TB_ASYM_MIN		(40000 * 90 / 100)
26 
27 /*
28  * Threshold bandwidth (in Mb/s) that is used to switch the links to
29  * asymmetric and back. This is selected as 45G which means when the
30  * request is higher than this, we switch the link to asymmetric, and
31  * when it is less than this we switch it back. The 45G is selected so
32  * that we still have 27G (of the total 72G) for bulk PCIe traffic when
33  * switching back to symmetric.
34  */
35 #define TB_ASYM_THRESHOLD	45000
36 
37 #define MAX_GROUPS		7	/* max Group_ID is 7 */
38 
39 static unsigned int asym_threshold = TB_ASYM_THRESHOLD;
40 module_param_named(asym_threshold, asym_threshold, uint, 0444);
41 MODULE_PARM_DESC(asym_threshold,
42 		"threshold (Mb/s) when to Gen 4 switch link symmetry. 0 disables. (default: "
43 		__MODULE_STRING(TB_ASYM_THRESHOLD) ")");
44 
45 /**
46  * struct tb_cm - Simple Thunderbolt connection manager
47  * @tunnel_list: List of active tunnels
48  * @dp_resources: List of available DP resources for DP tunneling
49  * @hotplug_active: tb_handle_hotplug will stop progressing plug
50  *		    events and exit if this is not set (it needs to
51  *		    acquire the lock one more time). Used to drain wq
52  *		    after cfg has been paused.
53  * @remove_work: Work used to remove any unplugged routers after
54  *		 runtime resume
55  * @groups: Bandwidth groups used in this domain.
56  */
57 struct tb_cm {
58 	struct list_head tunnel_list;
59 	struct list_head dp_resources;
60 	bool hotplug_active;
61 	struct delayed_work remove_work;
62 	struct tb_bandwidth_group groups[MAX_GROUPS];
63 };
64 
tcm_to_tb(struct tb_cm * tcm)65 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
66 {
67 	return ((void *)tcm - sizeof(struct tb));
68 }
69 
70 struct tb_hotplug_event {
71 	struct work_struct work;
72 	struct tb *tb;
73 	u64 route;
74 	u8 port;
75 	bool unplug;
76 };
77 
tb_init_bandwidth_groups(struct tb_cm * tcm)78 static void tb_init_bandwidth_groups(struct tb_cm *tcm)
79 {
80 	int i;
81 
82 	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
83 		struct tb_bandwidth_group *group = &tcm->groups[i];
84 
85 		group->tb = tcm_to_tb(tcm);
86 		group->index = i + 1;
87 		INIT_LIST_HEAD(&group->ports);
88 	}
89 }
90 
tb_bandwidth_group_attach_port(struct tb_bandwidth_group * group,struct tb_port * in)91 static void tb_bandwidth_group_attach_port(struct tb_bandwidth_group *group,
92 					   struct tb_port *in)
93 {
94 	if (!group || WARN_ON(in->group))
95 		return;
96 
97 	in->group = group;
98 	list_add_tail(&in->group_list, &group->ports);
99 
100 	tb_port_dbg(in, "attached to bandwidth group %d\n", group->index);
101 }
102 
tb_find_free_bandwidth_group(struct tb_cm * tcm)103 static struct tb_bandwidth_group *tb_find_free_bandwidth_group(struct tb_cm *tcm)
104 {
105 	int i;
106 
107 	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
108 		struct tb_bandwidth_group *group = &tcm->groups[i];
109 
110 		if (list_empty(&group->ports))
111 			return group;
112 	}
113 
114 	return NULL;
115 }
116 
117 static struct tb_bandwidth_group *
tb_attach_bandwidth_group(struct tb_cm * tcm,struct tb_port * in,struct tb_port * out)118 tb_attach_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
119 			  struct tb_port *out)
120 {
121 	struct tb_bandwidth_group *group;
122 	struct tb_tunnel *tunnel;
123 
124 	/*
125 	 * Find all DP tunnels that go through all the same USB4 links
126 	 * as this one. Because we always setup tunnels the same way we
127 	 * can just check for the routers at both ends of the tunnels
128 	 * and if they are the same we have a match.
129 	 */
130 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
131 		if (!tb_tunnel_is_dp(tunnel))
132 			continue;
133 
134 		if (tunnel->src_port->sw == in->sw &&
135 		    tunnel->dst_port->sw == out->sw) {
136 			group = tunnel->src_port->group;
137 			if (group) {
138 				tb_bandwidth_group_attach_port(group, in);
139 				return group;
140 			}
141 		}
142 	}
143 
144 	/* Pick up next available group then */
145 	group = tb_find_free_bandwidth_group(tcm);
146 	if (group)
147 		tb_bandwidth_group_attach_port(group, in);
148 	else
149 		tb_port_warn(in, "no available bandwidth groups\n");
150 
151 	return group;
152 }
153 
tb_discover_bandwidth_group(struct tb_cm * tcm,struct tb_port * in,struct tb_port * out)154 static void tb_discover_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
155 					struct tb_port *out)
156 {
157 	if (usb4_dp_port_bandwidth_mode_enabled(in)) {
158 		int index, i;
159 
160 		index = usb4_dp_port_group_id(in);
161 		for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
162 			if (tcm->groups[i].index == index) {
163 				tb_bandwidth_group_attach_port(&tcm->groups[i], in);
164 				return;
165 			}
166 		}
167 	}
168 
169 	tb_attach_bandwidth_group(tcm, in, out);
170 }
171 
tb_detach_bandwidth_group(struct tb_port * in)172 static void tb_detach_bandwidth_group(struct tb_port *in)
173 {
174 	struct tb_bandwidth_group *group = in->group;
175 
176 	if (group) {
177 		in->group = NULL;
178 		list_del_init(&in->group_list);
179 
180 		tb_port_dbg(in, "detached from bandwidth group %d\n", group->index);
181 	}
182 }
183 
184 static void tb_handle_hotplug(struct work_struct *work);
185 
tb_queue_hotplug(struct tb * tb,u64 route,u8 port,bool unplug)186 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
187 {
188 	struct tb_hotplug_event *ev;
189 
190 	ev = kmalloc(sizeof(*ev), GFP_KERNEL);
191 	if (!ev)
192 		return;
193 
194 	ev->tb = tb;
195 	ev->route = route;
196 	ev->port = port;
197 	ev->unplug = unplug;
198 	INIT_WORK(&ev->work, tb_handle_hotplug);
199 	queue_work(tb->wq, &ev->work);
200 }
201 
202 /* enumeration & hot plug handling */
203 
tb_add_dp_resources(struct tb_switch * sw)204 static void tb_add_dp_resources(struct tb_switch *sw)
205 {
206 	struct tb_cm *tcm = tb_priv(sw->tb);
207 	struct tb_port *port;
208 
209 	tb_switch_for_each_port(sw, port) {
210 		if (!tb_port_is_dpin(port))
211 			continue;
212 
213 		if (!tb_switch_query_dp_resource(sw, port))
214 			continue;
215 
216 		list_add_tail(&port->list, &tcm->dp_resources);
217 		tb_port_dbg(port, "DP IN resource available\n");
218 	}
219 }
220 
tb_remove_dp_resources(struct tb_switch * sw)221 static void tb_remove_dp_resources(struct tb_switch *sw)
222 {
223 	struct tb_cm *tcm = tb_priv(sw->tb);
224 	struct tb_port *port, *tmp;
225 
226 	/* Clear children resources first */
227 	tb_switch_for_each_port(sw, port) {
228 		if (tb_port_has_remote(port))
229 			tb_remove_dp_resources(port->remote->sw);
230 	}
231 
232 	list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
233 		if (port->sw == sw) {
234 			tb_port_dbg(port, "DP OUT resource unavailable\n");
235 			list_del_init(&port->list);
236 		}
237 	}
238 }
239 
tb_discover_dp_resource(struct tb * tb,struct tb_port * port)240 static void tb_discover_dp_resource(struct tb *tb, struct tb_port *port)
241 {
242 	struct tb_cm *tcm = tb_priv(tb);
243 	struct tb_port *p;
244 
245 	list_for_each_entry(p, &tcm->dp_resources, list) {
246 		if (p == port)
247 			return;
248 	}
249 
250 	tb_port_dbg(port, "DP %s resource available discovered\n",
251 		    tb_port_is_dpin(port) ? "IN" : "OUT");
252 	list_add_tail(&port->list, &tcm->dp_resources);
253 }
254 
tb_discover_dp_resources(struct tb * tb)255 static void tb_discover_dp_resources(struct tb *tb)
256 {
257 	struct tb_cm *tcm = tb_priv(tb);
258 	struct tb_tunnel *tunnel;
259 
260 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
261 		if (tb_tunnel_is_dp(tunnel))
262 			tb_discover_dp_resource(tb, tunnel->dst_port);
263 	}
264 }
265 
266 /* Enables CL states up to host router */
tb_enable_clx(struct tb_switch * sw)267 static int tb_enable_clx(struct tb_switch *sw)
268 {
269 	struct tb_cm *tcm = tb_priv(sw->tb);
270 	unsigned int clx = TB_CL0S | TB_CL1;
271 	const struct tb_tunnel *tunnel;
272 	int ret;
273 
274 	/*
275 	 * Currently only enable CLx for the first link. This is enough
276 	 * to allow the CPU to save energy at least on Intel hardware
277 	 * and makes it slightly simpler to implement. We may change
278 	 * this in the future to cover the whole topology if it turns
279 	 * out to be beneficial.
280 	 */
281 	while (sw && tb_switch_depth(sw) > 1)
282 		sw = tb_switch_parent(sw);
283 
284 	if (!sw)
285 		return 0;
286 
287 	if (tb_switch_depth(sw) != 1)
288 		return 0;
289 
290 	/*
291 	 * If we are re-enabling then check if there is an active DMA
292 	 * tunnel and in that case bail out.
293 	 */
294 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
295 		if (tb_tunnel_is_dma(tunnel)) {
296 			if (tb_tunnel_port_on_path(tunnel, tb_upstream_port(sw)))
297 				return 0;
298 		}
299 	}
300 
301 	/*
302 	 * Initially try with CL2. If that's not supported by the
303 	 * topology try with CL0s and CL1 and then give up.
304 	 */
305 	ret = tb_switch_clx_enable(sw, clx | TB_CL2);
306 	if (ret == -EOPNOTSUPP)
307 		ret = tb_switch_clx_enable(sw, clx);
308 	return ret == -EOPNOTSUPP ? 0 : ret;
309 }
310 
311 /**
312  * tb_disable_clx() - Disable CL states up to host router
313  * @sw: Router to start
314  *
315  * Disables CL states from @sw up to the host router. Returns true if
316  * any CL state were disabled. This can be used to figure out whether
317  * the link was setup by us or the boot firmware so we don't
318  * accidentally enable them if they were not enabled during discovery.
319  */
tb_disable_clx(struct tb_switch * sw)320 static bool tb_disable_clx(struct tb_switch *sw)
321 {
322 	bool disabled = false;
323 
324 	do {
325 		int ret;
326 
327 		ret = tb_switch_clx_disable(sw);
328 		if (ret > 0)
329 			disabled = true;
330 		else if (ret < 0)
331 			tb_sw_warn(sw, "failed to disable CL states\n");
332 
333 		sw = tb_switch_parent(sw);
334 	} while (sw);
335 
336 	return disabled;
337 }
338 
tb_increase_switch_tmu_accuracy(struct device * dev,void * data)339 static int tb_increase_switch_tmu_accuracy(struct device *dev, void *data)
340 {
341 	struct tb_switch *sw;
342 
343 	sw = tb_to_switch(dev);
344 	if (!sw)
345 		return 0;
346 
347 	if (tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_LOWRES)) {
348 		enum tb_switch_tmu_mode mode;
349 		int ret;
350 
351 		if (tb_switch_clx_is_enabled(sw, TB_CL1))
352 			mode = TB_SWITCH_TMU_MODE_HIFI_UNI;
353 		else
354 			mode = TB_SWITCH_TMU_MODE_HIFI_BI;
355 
356 		ret = tb_switch_tmu_configure(sw, mode);
357 		if (ret)
358 			return ret;
359 
360 		return tb_switch_tmu_enable(sw);
361 	}
362 
363 	return 0;
364 }
365 
tb_increase_tmu_accuracy(struct tb_tunnel * tunnel)366 static void tb_increase_tmu_accuracy(struct tb_tunnel *tunnel)
367 {
368 	struct tb_switch *sw;
369 
370 	if (!tunnel)
371 		return;
372 
373 	/*
374 	 * Once first DP tunnel is established we change the TMU
375 	 * accuracy of first depth child routers (and the host router)
376 	 * to the highest. This is needed for the DP tunneling to work
377 	 * but also allows CL0s.
378 	 *
379 	 * If both routers are v2 then we don't need to do anything as
380 	 * they are using enhanced TMU mode that allows all CLx.
381 	 */
382 	sw = tunnel->tb->root_switch;
383 	device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy);
384 }
385 
tb_switch_tmu_hifi_uni_required(struct device * dev,void * not_used)386 static int tb_switch_tmu_hifi_uni_required(struct device *dev, void *not_used)
387 {
388 	struct tb_switch *sw = tb_to_switch(dev);
389 
390 	if (sw && tb_switch_tmu_is_enabled(sw) &&
391 	    tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_HIFI_UNI))
392 		return 1;
393 
394 	return device_for_each_child(dev, NULL,
395 				     tb_switch_tmu_hifi_uni_required);
396 }
397 
tb_tmu_hifi_uni_required(struct tb * tb)398 static bool tb_tmu_hifi_uni_required(struct tb *tb)
399 {
400 	return device_for_each_child(&tb->dev, NULL,
401 				     tb_switch_tmu_hifi_uni_required) == 1;
402 }
403 
tb_enable_tmu(struct tb_switch * sw)404 static int tb_enable_tmu(struct tb_switch *sw)
405 {
406 	int ret;
407 
408 	/*
409 	 * If both routers at the end of the link are v2 we simply
410 	 * enable the enhanched uni-directional mode. That covers all
411 	 * the CL states. For v1 and before we need to use the normal
412 	 * rate to allow CL1 (when supported). Otherwise we keep the TMU
413 	 * running at the highest accuracy.
414 	 */
415 	ret = tb_switch_tmu_configure(sw,
416 			TB_SWITCH_TMU_MODE_MEDRES_ENHANCED_UNI);
417 	if (ret == -EOPNOTSUPP) {
418 		if (tb_switch_clx_is_enabled(sw, TB_CL1)) {
419 			/*
420 			 * Figure out uni-directional HiFi TMU requirements
421 			 * currently in the domain. If there are no
422 			 * uni-directional HiFi requirements we can put the TMU
423 			 * into LowRes mode.
424 			 *
425 			 * Deliberately skip bi-directional HiFi links
426 			 * as these work independently of other links
427 			 * (and they do not allow any CL states anyway).
428 			 */
429 			if (tb_tmu_hifi_uni_required(sw->tb))
430 				ret = tb_switch_tmu_configure(sw,
431 						TB_SWITCH_TMU_MODE_HIFI_UNI);
432 			else
433 				ret = tb_switch_tmu_configure(sw,
434 						TB_SWITCH_TMU_MODE_LOWRES);
435 		} else {
436 			ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI);
437 		}
438 
439 		/* If not supported, fallback to bi-directional HiFi */
440 		if (ret == -EOPNOTSUPP)
441 			ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI);
442 	}
443 	if (ret)
444 		return ret;
445 
446 	/* If it is already enabled in correct mode, don't touch it */
447 	if (tb_switch_tmu_is_enabled(sw))
448 		return 0;
449 
450 	ret = tb_switch_tmu_disable(sw);
451 	if (ret)
452 		return ret;
453 
454 	ret = tb_switch_tmu_post_time(sw);
455 	if (ret)
456 		return ret;
457 
458 	return tb_switch_tmu_enable(sw);
459 }
460 
tb_switch_discover_tunnels(struct tb_switch * sw,struct list_head * list,bool alloc_hopids)461 static void tb_switch_discover_tunnels(struct tb_switch *sw,
462 				       struct list_head *list,
463 				       bool alloc_hopids)
464 {
465 	struct tb *tb = sw->tb;
466 	struct tb_port *port;
467 
468 	tb_switch_for_each_port(sw, port) {
469 		struct tb_tunnel *tunnel = NULL;
470 
471 		switch (port->config.type) {
472 		case TB_TYPE_DP_HDMI_IN:
473 			tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids);
474 			tb_increase_tmu_accuracy(tunnel);
475 			break;
476 
477 		case TB_TYPE_PCIE_DOWN:
478 			tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
479 			break;
480 
481 		case TB_TYPE_USB3_DOWN:
482 			tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
483 			break;
484 
485 		default:
486 			break;
487 		}
488 
489 		if (tunnel)
490 			list_add_tail(&tunnel->list, list);
491 	}
492 
493 	tb_switch_for_each_port(sw, port) {
494 		if (tb_port_has_remote(port)) {
495 			tb_switch_discover_tunnels(port->remote->sw, list,
496 						   alloc_hopids);
497 		}
498 	}
499 }
500 
tb_discover_tunnels(struct tb * tb)501 static void tb_discover_tunnels(struct tb *tb)
502 {
503 	struct tb_cm *tcm = tb_priv(tb);
504 	struct tb_tunnel *tunnel;
505 
506 	tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
507 
508 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
509 		if (tb_tunnel_is_pci(tunnel)) {
510 			struct tb_switch *parent = tunnel->dst_port->sw;
511 
512 			while (parent != tunnel->src_port->sw) {
513 				parent->boot = true;
514 				parent = tb_switch_parent(parent);
515 			}
516 		} else if (tb_tunnel_is_dp(tunnel)) {
517 			struct tb_port *in = tunnel->src_port;
518 			struct tb_port *out = tunnel->dst_port;
519 
520 			/* Keep the domain from powering down */
521 			pm_runtime_get_sync(&in->sw->dev);
522 			pm_runtime_get_sync(&out->sw->dev);
523 
524 			tb_discover_bandwidth_group(tcm, in, out);
525 		}
526 	}
527 }
528 
tb_port_configure_xdomain(struct tb_port * port,struct tb_xdomain * xd)529 static int tb_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
530 {
531 	if (tb_switch_is_usb4(port->sw))
532 		return usb4_port_configure_xdomain(port, xd);
533 	return tb_lc_configure_xdomain(port);
534 }
535 
tb_port_unconfigure_xdomain(struct tb_port * port)536 static void tb_port_unconfigure_xdomain(struct tb_port *port)
537 {
538 	if (tb_switch_is_usb4(port->sw))
539 		usb4_port_unconfigure_xdomain(port);
540 	else
541 		tb_lc_unconfigure_xdomain(port);
542 
543 	tb_port_enable(port->dual_link_port);
544 }
545 
tb_scan_xdomain(struct tb_port * port)546 static void tb_scan_xdomain(struct tb_port *port)
547 {
548 	struct tb_switch *sw = port->sw;
549 	struct tb *tb = sw->tb;
550 	struct tb_xdomain *xd;
551 	u64 route;
552 
553 	if (!tb_is_xdomain_enabled())
554 		return;
555 
556 	route = tb_downstream_route(port);
557 	xd = tb_xdomain_find_by_route(tb, route);
558 	if (xd) {
559 		tb_xdomain_put(xd);
560 		return;
561 	}
562 
563 	xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
564 			      NULL);
565 	if (xd) {
566 		tb_port_at(route, sw)->xdomain = xd;
567 		tb_port_configure_xdomain(port, xd);
568 		tb_xdomain_add(xd);
569 	}
570 }
571 
572 /**
573  * tb_find_unused_port() - return the first inactive port on @sw
574  * @sw: Switch to find the port on
575  * @type: Port type to look for
576  */
tb_find_unused_port(struct tb_switch * sw,enum tb_port_type type)577 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
578 					   enum tb_port_type type)
579 {
580 	struct tb_port *port;
581 
582 	tb_switch_for_each_port(sw, port) {
583 		if (tb_is_upstream_port(port))
584 			continue;
585 		if (port->config.type != type)
586 			continue;
587 		if (!port->cap_adap)
588 			continue;
589 		if (tb_port_is_enabled(port))
590 			continue;
591 		return port;
592 	}
593 	return NULL;
594 }
595 
tb_find_usb3_down(struct tb_switch * sw,const struct tb_port * port)596 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
597 					 const struct tb_port *port)
598 {
599 	struct tb_port *down;
600 
601 	down = usb4_switch_map_usb3_down(sw, port);
602 	if (down && !tb_usb3_port_is_enabled(down))
603 		return down;
604 	return NULL;
605 }
606 
tb_find_tunnel(struct tb * tb,enum tb_tunnel_type type,struct tb_port * src_port,struct tb_port * dst_port)607 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
608 					struct tb_port *src_port,
609 					struct tb_port *dst_port)
610 {
611 	struct tb_cm *tcm = tb_priv(tb);
612 	struct tb_tunnel *tunnel;
613 
614 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
615 		if (tunnel->type == type &&
616 		    ((src_port && src_port == tunnel->src_port) ||
617 		     (dst_port && dst_port == tunnel->dst_port))) {
618 			return tunnel;
619 		}
620 	}
621 
622 	return NULL;
623 }
624 
tb_find_first_usb3_tunnel(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port)625 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
626 						   struct tb_port *src_port,
627 						   struct tb_port *dst_port)
628 {
629 	struct tb_port *port, *usb3_down;
630 	struct tb_switch *sw;
631 
632 	/* Pick the router that is deepest in the topology */
633 	if (tb_port_path_direction_downstream(src_port, dst_port))
634 		sw = dst_port->sw;
635 	else
636 		sw = src_port->sw;
637 
638 	/* Can't be the host router */
639 	if (sw == tb->root_switch)
640 		return NULL;
641 
642 	/* Find the downstream USB4 port that leads to this router */
643 	port = tb_port_at(tb_route(sw), tb->root_switch);
644 	/* Find the corresponding host router USB3 downstream port */
645 	usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
646 	if (!usb3_down)
647 		return NULL;
648 
649 	return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
650 }
651 
652 /**
653  * tb_consumed_usb3_pcie_bandwidth() - Consumed USB3/PCIe bandwidth over a single link
654  * @tb: Domain structure
655  * @src_port: Source protocol adapter
656  * @dst_port: Destination protocol adapter
657  * @port: USB4 port the consumed bandwidth is calculated
658  * @consumed_up: Consumed upsream bandwidth (Mb/s)
659  * @consumed_down: Consumed downstream bandwidth (Mb/s)
660  *
661  * Calculates consumed USB3 and PCIe bandwidth at @port between path
662  * from @src_port to @dst_port. Does not take tunnel starting from
663  * @src_port and ending from @src_port into account.
664  */
tb_consumed_usb3_pcie_bandwidth(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port,struct tb_port * port,int * consumed_up,int * consumed_down)665 static int tb_consumed_usb3_pcie_bandwidth(struct tb *tb,
666 					   struct tb_port *src_port,
667 					   struct tb_port *dst_port,
668 					   struct tb_port *port,
669 					   int *consumed_up,
670 					   int *consumed_down)
671 {
672 	int pci_consumed_up, pci_consumed_down;
673 	struct tb_tunnel *tunnel;
674 
675 	*consumed_up = *consumed_down = 0;
676 
677 	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
678 	if (tunnel && tunnel->src_port != src_port &&
679 	    tunnel->dst_port != dst_port) {
680 		int ret;
681 
682 		ret = tb_tunnel_consumed_bandwidth(tunnel, consumed_up,
683 						   consumed_down);
684 		if (ret)
685 			return ret;
686 	}
687 
688 	/*
689 	 * If there is anything reserved for PCIe bulk traffic take it
690 	 * into account here too.
691 	 */
692 	if (tb_tunnel_reserved_pci(port, &pci_consumed_up, &pci_consumed_down)) {
693 		*consumed_up += pci_consumed_up;
694 		*consumed_down += pci_consumed_down;
695 	}
696 
697 	return 0;
698 }
699 
700 /**
701  * tb_consumed_dp_bandwidth() - Consumed DP bandwidth over a single link
702  * @tb: Domain structure
703  * @src_port: Source protocol adapter
704  * @dst_port: Destination protocol adapter
705  * @port: USB4 port the consumed bandwidth is calculated
706  * @consumed_up: Consumed upsream bandwidth (Mb/s)
707  * @consumed_down: Consumed downstream bandwidth (Mb/s)
708  *
709  * Calculates consumed DP bandwidth at @port between path from @src_port
710  * to @dst_port. Does not take tunnel starting from @src_port and ending
711  * from @src_port into account.
712  */
tb_consumed_dp_bandwidth(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port,struct tb_port * port,int * consumed_up,int * consumed_down)713 static int tb_consumed_dp_bandwidth(struct tb *tb,
714 				    struct tb_port *src_port,
715 				    struct tb_port *dst_port,
716 				    struct tb_port *port,
717 				    int *consumed_up,
718 				    int *consumed_down)
719 {
720 	struct tb_cm *tcm = tb_priv(tb);
721 	struct tb_tunnel *tunnel;
722 	int ret;
723 
724 	*consumed_up = *consumed_down = 0;
725 
726 	/*
727 	 * Find all DP tunnels that cross the port and reduce
728 	 * their consumed bandwidth from the available.
729 	 */
730 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
731 		int dp_consumed_up, dp_consumed_down;
732 
733 		if (tb_tunnel_is_invalid(tunnel))
734 			continue;
735 
736 		if (!tb_tunnel_is_dp(tunnel))
737 			continue;
738 
739 		if (!tb_tunnel_port_on_path(tunnel, port))
740 			continue;
741 
742 		/*
743 		 * Ignore the DP tunnel between src_port and dst_port
744 		 * because it is the same tunnel and we may be
745 		 * re-calculating estimated bandwidth.
746 		 */
747 		if (tunnel->src_port == src_port &&
748 		    tunnel->dst_port == dst_port)
749 			continue;
750 
751 		ret = tb_tunnel_consumed_bandwidth(tunnel, &dp_consumed_up,
752 						   &dp_consumed_down);
753 		if (ret)
754 			return ret;
755 
756 		*consumed_up += dp_consumed_up;
757 		*consumed_down += dp_consumed_down;
758 	}
759 
760 	return 0;
761 }
762 
tb_asym_supported(struct tb_port * src_port,struct tb_port * dst_port,struct tb_port * port)763 static bool tb_asym_supported(struct tb_port *src_port, struct tb_port *dst_port,
764 			      struct tb_port *port)
765 {
766 	bool downstream = tb_port_path_direction_downstream(src_port, dst_port);
767 	enum tb_link_width width;
768 
769 	if (tb_is_upstream_port(port))
770 		width = downstream ? TB_LINK_WIDTH_ASYM_RX : TB_LINK_WIDTH_ASYM_TX;
771 	else
772 		width = downstream ? TB_LINK_WIDTH_ASYM_TX : TB_LINK_WIDTH_ASYM_RX;
773 
774 	return tb_port_width_supported(port, width);
775 }
776 
777 /**
778  * tb_maximum_banwidth() - Maximum bandwidth over a single link
779  * @tb: Domain structure
780  * @src_port: Source protocol adapter
781  * @dst_port: Destination protocol adapter
782  * @port: USB4 port the total bandwidth is calculated
783  * @max_up: Maximum upstream bandwidth (Mb/s)
784  * @max_down: Maximum downstream bandwidth (Mb/s)
785  * @include_asym: Include bandwidth if the link is switched from
786  *		  symmetric to asymmetric
787  *
788  * Returns maximum possible bandwidth in @max_up and @max_down over a
789  * single link at @port. If @include_asym is set then includes the
790  * additional banwdith if the links are transitioned into asymmetric to
791  * direction from @src_port to @dst_port.
792  */
tb_maximum_bandwidth(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port,struct tb_port * port,int * max_up,int * max_down,bool include_asym)793 static int tb_maximum_bandwidth(struct tb *tb, struct tb_port *src_port,
794 				struct tb_port *dst_port, struct tb_port *port,
795 				int *max_up, int *max_down, bool include_asym)
796 {
797 	bool downstream = tb_port_path_direction_downstream(src_port, dst_port);
798 	int link_speed, link_width, up_bw, down_bw;
799 
800 	/*
801 	 * Can include asymmetric, only if it is actually supported by
802 	 * the lane adapter.
803 	 */
804 	if (!tb_asym_supported(src_port, dst_port, port))
805 		include_asym = false;
806 
807 	if (tb_is_upstream_port(port)) {
808 		link_speed = port->sw->link_speed;
809 		/*
810 		 * sw->link_width is from upstream perspective so we use
811 		 * the opposite for downstream of the host router.
812 		 */
813 		if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) {
814 			up_bw = link_speed * 3 * 1000;
815 			down_bw = link_speed * 1 * 1000;
816 		} else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) {
817 			up_bw = link_speed * 1 * 1000;
818 			down_bw = link_speed * 3 * 1000;
819 		} else if (include_asym) {
820 			/*
821 			 * The link is symmetric at the moment but we
822 			 * can switch it to asymmetric as needed. Report
823 			 * this bandwidth as available (even though it
824 			 * is not yet enabled).
825 			 */
826 			if (downstream) {
827 				up_bw = link_speed * 1 * 1000;
828 				down_bw = link_speed * 3 * 1000;
829 			} else {
830 				up_bw = link_speed * 3 * 1000;
831 				down_bw = link_speed * 1 * 1000;
832 			}
833 		} else {
834 			up_bw = link_speed * port->sw->link_width * 1000;
835 			down_bw = up_bw;
836 		}
837 	} else {
838 		link_speed = tb_port_get_link_speed(port);
839 		if (link_speed < 0)
840 			return link_speed;
841 
842 		link_width = tb_port_get_link_width(port);
843 		if (link_width < 0)
844 			return link_width;
845 
846 		if (link_width == TB_LINK_WIDTH_ASYM_TX) {
847 			up_bw = link_speed * 1 * 1000;
848 			down_bw = link_speed * 3 * 1000;
849 		} else if (link_width == TB_LINK_WIDTH_ASYM_RX) {
850 			up_bw = link_speed * 3 * 1000;
851 			down_bw = link_speed * 1 * 1000;
852 		} else if (include_asym) {
853 			/*
854 			 * The link is symmetric at the moment but we
855 			 * can switch it to asymmetric as needed. Report
856 			 * this bandwidth as available (even though it
857 			 * is not yet enabled).
858 			 */
859 			if (downstream) {
860 				up_bw = link_speed * 1 * 1000;
861 				down_bw = link_speed * 3 * 1000;
862 			} else {
863 				up_bw = link_speed * 3 * 1000;
864 				down_bw = link_speed * 1 * 1000;
865 			}
866 		} else {
867 			up_bw = link_speed * link_width * 1000;
868 			down_bw = up_bw;
869 		}
870 	}
871 
872 	/* Leave 10% guard band */
873 	*max_up = up_bw - up_bw / 10;
874 	*max_down = down_bw - down_bw / 10;
875 
876 	tb_port_dbg(port, "link maximum bandwidth %d/%d Mb/s\n", *max_up, *max_down);
877 	return 0;
878 }
879 
880 /**
881  * tb_available_bandwidth() - Available bandwidth for tunneling
882  * @tb: Domain structure
883  * @src_port: Source protocol adapter
884  * @dst_port: Destination protocol adapter
885  * @available_up: Available bandwidth upstream (Mb/s)
886  * @available_down: Available bandwidth downstream (Mb/s)
887  * @include_asym: Include bandwidth if the link is switched from
888  *		  symmetric to asymmetric
889  *
890  * Calculates maximum available bandwidth for protocol tunneling between
891  * @src_port and @dst_port at the moment. This is minimum of maximum
892  * link bandwidth across all links reduced by currently consumed
893  * bandwidth on that link.
894  *
895  * If @include_asym is true then includes also bandwidth that can be
896  * added when the links are transitioned into asymmetric (but does not
897  * transition the links).
898  */
tb_available_bandwidth(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port,int * available_up,int * available_down,bool include_asym)899 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
900 				 struct tb_port *dst_port, int *available_up,
901 				 int *available_down, bool include_asym)
902 {
903 	struct tb_port *port;
904 	int ret;
905 
906 	/* Maximum possible bandwidth asymmetric Gen 4 link is 120 Gb/s */
907 	*available_up = *available_down = 120000;
908 
909 	/* Find the minimum available bandwidth over all links */
910 	tb_for_each_port_on_path(src_port, dst_port, port) {
911 		int max_up, max_down, consumed_up, consumed_down;
912 
913 		if (!tb_port_is_null(port))
914 			continue;
915 
916 		ret = tb_maximum_bandwidth(tb, src_port, dst_port, port,
917 					   &max_up, &max_down, include_asym);
918 		if (ret)
919 			return ret;
920 
921 		ret = tb_consumed_usb3_pcie_bandwidth(tb, src_port, dst_port,
922 						      port, &consumed_up,
923 						      &consumed_down);
924 		if (ret)
925 			return ret;
926 		max_up -= consumed_up;
927 		max_down -= consumed_down;
928 
929 		ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, port,
930 					       &consumed_up, &consumed_down);
931 		if (ret)
932 			return ret;
933 		max_up -= consumed_up;
934 		max_down -= consumed_down;
935 
936 		if (max_up < *available_up)
937 			*available_up = max_up;
938 		if (max_down < *available_down)
939 			*available_down = max_down;
940 	}
941 
942 	if (*available_up < 0)
943 		*available_up = 0;
944 	if (*available_down < 0)
945 		*available_down = 0;
946 
947 	return 0;
948 }
949 
tb_release_unused_usb3_bandwidth(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port)950 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
951 					    struct tb_port *src_port,
952 					    struct tb_port *dst_port)
953 {
954 	struct tb_tunnel *tunnel;
955 
956 	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
957 	return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
958 }
959 
tb_reclaim_usb3_bandwidth(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port)960 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
961 				      struct tb_port *dst_port)
962 {
963 	int ret, available_up, available_down;
964 	struct tb_tunnel *tunnel;
965 
966 	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
967 	if (!tunnel)
968 		return;
969 
970 	tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
971 
972 	/*
973 	 * Calculate available bandwidth for the first hop USB3 tunnel.
974 	 * That determines the whole USB3 bandwidth for this branch.
975 	 */
976 	ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
977 				     &available_up, &available_down, false);
978 	if (ret) {
979 		tb_warn(tb, "failed to calculate available bandwidth\n");
980 		return;
981 	}
982 
983 	tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
984 	       available_up, available_down);
985 
986 	tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
987 }
988 
tb_tunnel_usb3(struct tb * tb,struct tb_switch * sw)989 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
990 {
991 	struct tb_switch *parent = tb_switch_parent(sw);
992 	int ret, available_up, available_down;
993 	struct tb_port *up, *down, *port;
994 	struct tb_cm *tcm = tb_priv(tb);
995 	struct tb_tunnel *tunnel;
996 
997 	if (!tb_acpi_may_tunnel_usb3()) {
998 		tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
999 		return 0;
1000 	}
1001 
1002 	up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
1003 	if (!up)
1004 		return 0;
1005 
1006 	if (!sw->link_usb4)
1007 		return 0;
1008 
1009 	/*
1010 	 * Look up available down port. Since we are chaining it should
1011 	 * be found right above this switch.
1012 	 */
1013 	port = tb_switch_downstream_port(sw);
1014 	down = tb_find_usb3_down(parent, port);
1015 	if (!down)
1016 		return 0;
1017 
1018 	if (tb_route(parent)) {
1019 		struct tb_port *parent_up;
1020 		/*
1021 		 * Check first that the parent switch has its upstream USB3
1022 		 * port enabled. Otherwise the chain is not complete and
1023 		 * there is no point setting up a new tunnel.
1024 		 */
1025 		parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
1026 		if (!parent_up || !tb_port_is_enabled(parent_up))
1027 			return 0;
1028 
1029 		/* Make all unused bandwidth available for the new tunnel */
1030 		ret = tb_release_unused_usb3_bandwidth(tb, down, up);
1031 		if (ret)
1032 			return ret;
1033 	}
1034 
1035 	ret = tb_available_bandwidth(tb, down, up, &available_up, &available_down,
1036 				     false);
1037 	if (ret)
1038 		goto err_reclaim;
1039 
1040 	tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
1041 		    available_up, available_down);
1042 
1043 	tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
1044 				      available_down);
1045 	if (!tunnel) {
1046 		ret = -ENOMEM;
1047 		goto err_reclaim;
1048 	}
1049 
1050 	if (tb_tunnel_activate(tunnel)) {
1051 		tb_port_info(up,
1052 			     "USB3 tunnel activation failed, aborting\n");
1053 		ret = -EIO;
1054 		goto err_free;
1055 	}
1056 
1057 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
1058 	if (tb_route(parent))
1059 		tb_reclaim_usb3_bandwidth(tb, down, up);
1060 
1061 	return 0;
1062 
1063 err_free:
1064 	tb_tunnel_free(tunnel);
1065 err_reclaim:
1066 	if (tb_route(parent))
1067 		tb_reclaim_usb3_bandwidth(tb, down, up);
1068 
1069 	return ret;
1070 }
1071 
tb_create_usb3_tunnels(struct tb_switch * sw)1072 static int tb_create_usb3_tunnels(struct tb_switch *sw)
1073 {
1074 	struct tb_port *port;
1075 	int ret;
1076 
1077 	if (!tb_acpi_may_tunnel_usb3())
1078 		return 0;
1079 
1080 	if (tb_route(sw)) {
1081 		ret = tb_tunnel_usb3(sw->tb, sw);
1082 		if (ret)
1083 			return ret;
1084 	}
1085 
1086 	tb_switch_for_each_port(sw, port) {
1087 		if (!tb_port_has_remote(port))
1088 			continue;
1089 		ret = tb_create_usb3_tunnels(port->remote->sw);
1090 		if (ret)
1091 			return ret;
1092 	}
1093 
1094 	return 0;
1095 }
1096 
1097 /**
1098  * tb_configure_asym() - Transition links to asymmetric if needed
1099  * @tb: Domain structure
1100  * @src_port: Source adapter to start the transition
1101  * @dst_port: Destination adapter
1102  * @requested_up: Additional bandwidth (Mb/s) required upstream
1103  * @requested_down: Additional bandwidth (Mb/s) required downstream
1104  *
1105  * Transition links between @src_port and @dst_port into asymmetric, with
1106  * three lanes in the direction from @src_port towards @dst_port and one lane
1107  * in the opposite direction, if the bandwidth requirements
1108  * (requested + currently consumed) on that link exceed @asym_threshold.
1109  *
1110  * Must be called with available >= requested over all links.
1111  */
tb_configure_asym(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port,int requested_up,int requested_down)1112 static int tb_configure_asym(struct tb *tb, struct tb_port *src_port,
1113 			     struct tb_port *dst_port, int requested_up,
1114 			     int requested_down)
1115 {
1116 	struct tb_switch *sw;
1117 	bool clx, downstream;
1118 	struct tb_port *up;
1119 	int ret = 0;
1120 
1121 	if (!asym_threshold)
1122 		return 0;
1123 
1124 	/* Disable CL states before doing any transitions */
1125 	downstream = tb_port_path_direction_downstream(src_port, dst_port);
1126 	/* Pick up router deepest in the hierarchy */
1127 	if (downstream)
1128 		sw = dst_port->sw;
1129 	else
1130 		sw = src_port->sw;
1131 
1132 	clx = tb_disable_clx(sw);
1133 
1134 	tb_for_each_upstream_port_on_path(src_port, dst_port, up) {
1135 		int consumed_up, consumed_down;
1136 		enum tb_link_width width;
1137 
1138 		ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, up,
1139 					       &consumed_up, &consumed_down);
1140 		if (ret)
1141 			break;
1142 
1143 		if (downstream) {
1144 			/*
1145 			 * Downstream so make sure upstream is within the 36G
1146 			 * (40G - guard band 10%), and the requested is above
1147 			 * what the threshold is.
1148 			 */
1149 			if (consumed_up + requested_up >= TB_ASYM_MIN) {
1150 				ret = -ENOBUFS;
1151 				break;
1152 			}
1153 			/* Does consumed + requested exceed the threshold */
1154 			if (consumed_down + requested_down < asym_threshold)
1155 				continue;
1156 
1157 			width = TB_LINK_WIDTH_ASYM_RX;
1158 		} else {
1159 			/* Upstream, the opposite of above */
1160 			if (consumed_down + requested_down >= TB_ASYM_MIN) {
1161 				ret = -ENOBUFS;
1162 				break;
1163 			}
1164 			if (consumed_up + requested_up < asym_threshold)
1165 				continue;
1166 
1167 			width = TB_LINK_WIDTH_ASYM_TX;
1168 		}
1169 
1170 		if (up->sw->link_width == width)
1171 			continue;
1172 
1173 		if (!tb_port_width_supported(up, width))
1174 			continue;
1175 
1176 		tb_sw_dbg(up->sw, "configuring asymmetric link\n");
1177 
1178 		/*
1179 		 * Here requested + consumed > threshold so we need to
1180 		 * transtion the link into asymmetric now.
1181 		 */
1182 		ret = tb_switch_set_link_width(up->sw, width);
1183 		if (ret) {
1184 			tb_sw_warn(up->sw, "failed to set link width\n");
1185 			break;
1186 		}
1187 	}
1188 
1189 	/* Re-enable CL states if they were previosly enabled */
1190 	if (clx)
1191 		tb_enable_clx(sw);
1192 
1193 	return ret;
1194 }
1195 
1196 /**
1197  * tb_configure_sym() - Transition links to symmetric if possible
1198  * @tb: Domain structure
1199  * @src_port: Source adapter to start the transition
1200  * @dst_port: Destination adapter
1201  * @requested_up: New lower bandwidth request upstream (Mb/s)
1202  * @requested_down: New lower bandwidth request downstream (Mb/s)
1203  *
1204  * Goes over each link from @src_port to @dst_port and tries to
1205  * transition the link to symmetric if the currently consumed bandwidth
1206  * allows.
1207  */
tb_configure_sym(struct tb * tb,struct tb_port * src_port,struct tb_port * dst_port,int requested_up,int requested_down)1208 static int tb_configure_sym(struct tb *tb, struct tb_port *src_port,
1209 			    struct tb_port *dst_port, int requested_up,
1210 			    int requested_down)
1211 {
1212 	struct tb_switch *sw;
1213 	bool clx, downstream;
1214 	struct tb_port *up;
1215 	int ret = 0;
1216 
1217 	if (!asym_threshold)
1218 		return 0;
1219 
1220 	/* Disable CL states before doing any transitions */
1221 	downstream = tb_port_path_direction_downstream(src_port, dst_port);
1222 	/* Pick up router deepest in the hierarchy */
1223 	if (downstream)
1224 		sw = dst_port->sw;
1225 	else
1226 		sw = src_port->sw;
1227 
1228 	clx = tb_disable_clx(sw);
1229 
1230 	tb_for_each_upstream_port_on_path(src_port, dst_port, up) {
1231 		int consumed_up, consumed_down;
1232 
1233 		/* Already symmetric */
1234 		if (up->sw->link_width <= TB_LINK_WIDTH_DUAL)
1235 			continue;
1236 		/* Unplugged, no need to switch */
1237 		if (up->sw->is_unplugged)
1238 			continue;
1239 
1240 		ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, up,
1241 					       &consumed_up, &consumed_down);
1242 		if (ret)
1243 			break;
1244 
1245 		if (downstream) {
1246 			/*
1247 			 * Downstream so we want the consumed_down < threshold.
1248 			 * Upstream traffic should be less than 36G (40G
1249 			 * guard band 10%) as the link was configured asymmetric
1250 			 * already.
1251 			 */
1252 			if (consumed_down + requested_down >= asym_threshold)
1253 				continue;
1254 		} else {
1255 			if (consumed_up + requested_up >= asym_threshold)
1256 				continue;
1257 		}
1258 
1259 		if (up->sw->link_width == TB_LINK_WIDTH_DUAL)
1260 			continue;
1261 
1262 		tb_sw_dbg(up->sw, "configuring symmetric link\n");
1263 
1264 		ret = tb_switch_set_link_width(up->sw, TB_LINK_WIDTH_DUAL);
1265 		if (ret) {
1266 			tb_sw_warn(up->sw, "failed to set link width\n");
1267 			break;
1268 		}
1269 	}
1270 
1271 	/* Re-enable CL states if they were previosly enabled */
1272 	if (clx)
1273 		tb_enable_clx(sw);
1274 
1275 	return ret;
1276 }
1277 
tb_configure_link(struct tb_port * down,struct tb_port * up,struct tb_switch * sw)1278 static void tb_configure_link(struct tb_port *down, struct tb_port *up,
1279 			      struct tb_switch *sw)
1280 {
1281 	struct tb *tb = sw->tb;
1282 
1283 	/* Link the routers using both links if available */
1284 	down->remote = up;
1285 	up->remote = down;
1286 	if (down->dual_link_port && up->dual_link_port) {
1287 		down->dual_link_port->remote = up->dual_link_port;
1288 		up->dual_link_port->remote = down->dual_link_port;
1289 	}
1290 
1291 	/*
1292 	 * Enable lane bonding if the link is currently two single lane
1293 	 * links.
1294 	 */
1295 	if (sw->link_width < TB_LINK_WIDTH_DUAL)
1296 		tb_switch_set_link_width(sw, TB_LINK_WIDTH_DUAL);
1297 
1298 	/*
1299 	 * Device router that comes up as symmetric link is
1300 	 * connected deeper in the hierarchy, we transition the links
1301 	 * above into symmetric if bandwidth allows.
1302 	 */
1303 	if (tb_switch_depth(sw) > 1 &&
1304 	    tb_port_get_link_generation(up) >= 4 &&
1305 	    up->sw->link_width == TB_LINK_WIDTH_DUAL) {
1306 		struct tb_port *host_port;
1307 
1308 		host_port = tb_port_at(tb_route(sw), tb->root_switch);
1309 		tb_configure_sym(tb, host_port, up, 0, 0);
1310 	}
1311 
1312 	/* Set the link configured */
1313 	tb_switch_configure_link(sw);
1314 }
1315 
1316 static void tb_scan_port(struct tb_port *port);
1317 
1318 /*
1319  * tb_scan_switch() - scan for and initialize downstream switches
1320  */
tb_scan_switch(struct tb_switch * sw)1321 static void tb_scan_switch(struct tb_switch *sw)
1322 {
1323 	struct tb_port *port;
1324 
1325 	pm_runtime_get_sync(&sw->dev);
1326 
1327 	tb_switch_for_each_port(sw, port)
1328 		tb_scan_port(port);
1329 
1330 	pm_runtime_mark_last_busy(&sw->dev);
1331 	pm_runtime_put_autosuspend(&sw->dev);
1332 }
1333 
1334 /*
1335  * tb_scan_port() - check for and initialize switches below port
1336  */
tb_scan_port(struct tb_port * port)1337 static void tb_scan_port(struct tb_port *port)
1338 {
1339 	struct tb_cm *tcm = tb_priv(port->sw->tb);
1340 	struct tb_port *upstream_port;
1341 	bool discovery = false;
1342 	struct tb_switch *sw;
1343 
1344 	if (tb_is_upstream_port(port))
1345 		return;
1346 
1347 	if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
1348 	    !tb_dp_port_is_enabled(port)) {
1349 		tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
1350 		tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
1351 				 false);
1352 		return;
1353 	}
1354 
1355 	if (port->config.type != TB_TYPE_PORT)
1356 		return;
1357 	if (port->dual_link_port && port->link_nr)
1358 		return; /*
1359 			 * Downstream switch is reachable through two ports.
1360 			 * Only scan on the primary port (link_nr == 0).
1361 			 */
1362 
1363 	if (port->usb4)
1364 		pm_runtime_get_sync(&port->usb4->dev);
1365 
1366 	if (tb_wait_for_port(port, false) <= 0)
1367 		goto out_rpm_put;
1368 	if (port->remote) {
1369 		tb_port_dbg(port, "port already has a remote\n");
1370 		goto out_rpm_put;
1371 	}
1372 
1373 	sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
1374 			     tb_downstream_route(port));
1375 	if (IS_ERR(sw)) {
1376 		/*
1377 		 * Make the downstream retimers available even if there
1378 		 * is no router connected.
1379 		 */
1380 		tb_retimer_scan(port, true);
1381 
1382 		/*
1383 		 * If there is an error accessing the connected switch
1384 		 * it may be connected to another domain. Also we allow
1385 		 * the other domain to be connected to a max depth switch.
1386 		 */
1387 		if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
1388 			tb_scan_xdomain(port);
1389 		goto out_rpm_put;
1390 	}
1391 
1392 	if (tb_switch_configure(sw)) {
1393 		tb_switch_put(sw);
1394 		goto out_rpm_put;
1395 	}
1396 
1397 	/*
1398 	 * If there was previously another domain connected remove it
1399 	 * first.
1400 	 */
1401 	if (port->xdomain) {
1402 		tb_xdomain_remove(port->xdomain);
1403 		tb_port_unconfigure_xdomain(port);
1404 		port->xdomain = NULL;
1405 	}
1406 
1407 	/*
1408 	 * Do not send uevents until we have discovered all existing
1409 	 * tunnels and know which switches were authorized already by
1410 	 * the boot firmware.
1411 	 */
1412 	if (!tcm->hotplug_active) {
1413 		dev_set_uevent_suppress(&sw->dev, true);
1414 		discovery = true;
1415 	}
1416 
1417 	/*
1418 	 * At the moment Thunderbolt 2 and beyond (devices with LC) we
1419 	 * can support runtime PM.
1420 	 */
1421 	sw->rpm = sw->generation > 1;
1422 
1423 	if (tb_switch_add(sw)) {
1424 		tb_switch_put(sw);
1425 		goto out_rpm_put;
1426 	}
1427 
1428 	upstream_port = tb_upstream_port(sw);
1429 	tb_configure_link(port, upstream_port, sw);
1430 
1431 	/*
1432 	 * Scan for downstream retimers. We only scan them after the
1433 	 * router has been enumerated to avoid issues with certain
1434 	 * Pluggable devices that expect the host to enumerate them
1435 	 * within certain timeout.
1436 	 */
1437 	tb_retimer_scan(port, true);
1438 
1439 	/*
1440 	 * CL0s and CL1 are enabled and supported together.
1441 	 * Silently ignore CLx enabling in case CLx is not supported.
1442 	 */
1443 	if (discovery)
1444 		tb_sw_dbg(sw, "discovery, not touching CL states\n");
1445 	else if (tb_enable_clx(sw))
1446 		tb_sw_warn(sw, "failed to enable CL states\n");
1447 
1448 	if (tb_enable_tmu(sw))
1449 		tb_sw_warn(sw, "failed to enable TMU\n");
1450 
1451 	/*
1452 	 * Configuration valid needs to be set after the TMU has been
1453 	 * enabled for the upstream port of the router so we do it here.
1454 	 */
1455 	tb_switch_configuration_valid(sw);
1456 
1457 	/* Scan upstream retimers */
1458 	tb_retimer_scan(upstream_port, true);
1459 
1460 	/*
1461 	 * Create USB 3.x tunnels only when the switch is plugged to the
1462 	 * domain. This is because we scan the domain also during discovery
1463 	 * and want to discover existing USB 3.x tunnels before we create
1464 	 * any new.
1465 	 */
1466 	if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
1467 		tb_sw_warn(sw, "USB3 tunnel creation failed\n");
1468 
1469 	tb_add_dp_resources(sw);
1470 	tb_scan_switch(sw);
1471 
1472 out_rpm_put:
1473 	if (port->usb4) {
1474 		pm_runtime_mark_last_busy(&port->usb4->dev);
1475 		pm_runtime_put_autosuspend(&port->usb4->dev);
1476 	}
1477 }
1478 
tb_deactivate_and_free_tunnel(struct tb_tunnel * tunnel)1479 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
1480 {
1481 	struct tb_port *src_port, *dst_port;
1482 	struct tb *tb;
1483 
1484 	if (!tunnel)
1485 		return;
1486 
1487 	tb_tunnel_deactivate(tunnel);
1488 	list_del(&tunnel->list);
1489 
1490 	tb = tunnel->tb;
1491 	src_port = tunnel->src_port;
1492 	dst_port = tunnel->dst_port;
1493 
1494 	switch (tunnel->type) {
1495 	case TB_TUNNEL_DP:
1496 		tb_detach_bandwidth_group(src_port);
1497 		/*
1498 		 * In case of DP tunnel make sure the DP IN resource is
1499 		 * deallocated properly.
1500 		 */
1501 		tb_switch_dealloc_dp_resource(src_port->sw, src_port);
1502 		/*
1503 		 * If bandwidth on a link is < asym_threshold
1504 		 * transition the link to symmetric.
1505 		 */
1506 		tb_configure_sym(tb, src_port, dst_port, 0, 0);
1507 		/* Now we can allow the domain to runtime suspend again */
1508 		pm_runtime_mark_last_busy(&dst_port->sw->dev);
1509 		pm_runtime_put_autosuspend(&dst_port->sw->dev);
1510 		pm_runtime_mark_last_busy(&src_port->sw->dev);
1511 		pm_runtime_put_autosuspend(&src_port->sw->dev);
1512 		fallthrough;
1513 
1514 	case TB_TUNNEL_USB3:
1515 		tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
1516 		break;
1517 
1518 	default:
1519 		/*
1520 		 * PCIe and DMA tunnels do not consume guaranteed
1521 		 * bandwidth.
1522 		 */
1523 		break;
1524 	}
1525 
1526 	tb_tunnel_free(tunnel);
1527 }
1528 
1529 /*
1530  * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
1531  */
tb_free_invalid_tunnels(struct tb * tb)1532 static void tb_free_invalid_tunnels(struct tb *tb)
1533 {
1534 	struct tb_cm *tcm = tb_priv(tb);
1535 	struct tb_tunnel *tunnel;
1536 	struct tb_tunnel *n;
1537 
1538 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1539 		if (tb_tunnel_is_invalid(tunnel))
1540 			tb_deactivate_and_free_tunnel(tunnel);
1541 	}
1542 }
1543 
1544 /*
1545  * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
1546  */
tb_free_unplugged_children(struct tb_switch * sw)1547 static void tb_free_unplugged_children(struct tb_switch *sw)
1548 {
1549 	struct tb_port *port;
1550 
1551 	tb_switch_for_each_port(sw, port) {
1552 		if (!tb_port_has_remote(port))
1553 			continue;
1554 
1555 		if (port->remote->sw->is_unplugged) {
1556 			tb_retimer_remove_all(port);
1557 			tb_remove_dp_resources(port->remote->sw);
1558 			tb_switch_unconfigure_link(port->remote->sw);
1559 			tb_switch_set_link_width(port->remote->sw,
1560 						 TB_LINK_WIDTH_SINGLE);
1561 			tb_switch_remove(port->remote->sw);
1562 			port->remote = NULL;
1563 			if (port->dual_link_port)
1564 				port->dual_link_port->remote = NULL;
1565 		} else {
1566 			tb_free_unplugged_children(port->remote->sw);
1567 		}
1568 	}
1569 }
1570 
tb_find_pcie_down(struct tb_switch * sw,const struct tb_port * port)1571 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
1572 					 const struct tb_port *port)
1573 {
1574 	struct tb_port *down = NULL;
1575 
1576 	/*
1577 	 * To keep plugging devices consistently in the same PCIe
1578 	 * hierarchy, do mapping here for switch downstream PCIe ports.
1579 	 */
1580 	if (tb_switch_is_usb4(sw)) {
1581 		down = usb4_switch_map_pcie_down(sw, port);
1582 	} else if (!tb_route(sw)) {
1583 		int phy_port = tb_phy_port_from_link(port->port);
1584 		int index;
1585 
1586 		/*
1587 		 * Hard-coded Thunderbolt port to PCIe down port mapping
1588 		 * per controller.
1589 		 */
1590 		if (tb_switch_is_cactus_ridge(sw) ||
1591 		    tb_switch_is_alpine_ridge(sw))
1592 			index = !phy_port ? 6 : 7;
1593 		else if (tb_switch_is_falcon_ridge(sw))
1594 			index = !phy_port ? 6 : 8;
1595 		else if (tb_switch_is_titan_ridge(sw))
1596 			index = !phy_port ? 8 : 9;
1597 		else
1598 			goto out;
1599 
1600 		/* Validate the hard-coding */
1601 		if (WARN_ON(index > sw->config.max_port_number))
1602 			goto out;
1603 
1604 		down = &sw->ports[index];
1605 	}
1606 
1607 	if (down) {
1608 		if (WARN_ON(!tb_port_is_pcie_down(down)))
1609 			goto out;
1610 		if (tb_pci_port_is_enabled(down))
1611 			goto out;
1612 
1613 		return down;
1614 	}
1615 
1616 out:
1617 	return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
1618 }
1619 
1620 static void
tb_recalc_estimated_bandwidth_for_group(struct tb_bandwidth_group * group)1621 tb_recalc_estimated_bandwidth_for_group(struct tb_bandwidth_group *group)
1622 {
1623 	struct tb_tunnel *first_tunnel;
1624 	struct tb *tb = group->tb;
1625 	struct tb_port *in;
1626 	int ret;
1627 
1628 	tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n",
1629 	       group->index);
1630 
1631 	first_tunnel = NULL;
1632 	list_for_each_entry(in, &group->ports, group_list) {
1633 		int estimated_bw, estimated_up, estimated_down;
1634 		struct tb_tunnel *tunnel;
1635 		struct tb_port *out;
1636 
1637 		if (!usb4_dp_port_bandwidth_mode_enabled(in))
1638 			continue;
1639 
1640 		tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
1641 		if (WARN_ON(!tunnel))
1642 			break;
1643 
1644 		if (!first_tunnel) {
1645 			/*
1646 			 * Since USB3 bandwidth is shared by all DP
1647 			 * tunnels under the host router USB4 port, even
1648 			 * if they do not begin from the host router, we
1649 			 * can release USB3 bandwidth just once and not
1650 			 * for each tunnel separately.
1651 			 */
1652 			first_tunnel = tunnel;
1653 			ret = tb_release_unused_usb3_bandwidth(tb,
1654 				first_tunnel->src_port, first_tunnel->dst_port);
1655 			if (ret) {
1656 				tb_port_warn(in,
1657 					"failed to release unused bandwidth\n");
1658 				break;
1659 			}
1660 		}
1661 
1662 		out = tunnel->dst_port;
1663 		ret = tb_available_bandwidth(tb, in, out, &estimated_up,
1664 					     &estimated_down, true);
1665 		if (ret) {
1666 			tb_port_warn(in,
1667 				"failed to re-calculate estimated bandwidth\n");
1668 			break;
1669 		}
1670 
1671 		/*
1672 		 * Estimated bandwidth includes:
1673 		 *  - already allocated bandwidth for the DP tunnel
1674 		 *  - available bandwidth along the path
1675 		 *  - bandwidth allocated for USB 3.x but not used.
1676 		 */
1677 		tb_port_dbg(in, "re-calculated estimated bandwidth %u/%u Mb/s\n",
1678 			    estimated_up, estimated_down);
1679 
1680 		if (tb_port_path_direction_downstream(in, out))
1681 			estimated_bw = estimated_down;
1682 		else
1683 			estimated_bw = estimated_up;
1684 
1685 		if (usb4_dp_port_set_estimated_bandwidth(in, estimated_bw))
1686 			tb_port_warn(in, "failed to update estimated bandwidth\n");
1687 	}
1688 
1689 	if (first_tunnel)
1690 		tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port,
1691 					  first_tunnel->dst_port);
1692 
1693 	tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index);
1694 }
1695 
tb_recalc_estimated_bandwidth(struct tb * tb)1696 static void tb_recalc_estimated_bandwidth(struct tb *tb)
1697 {
1698 	struct tb_cm *tcm = tb_priv(tb);
1699 	int i;
1700 
1701 	tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n");
1702 
1703 	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
1704 		struct tb_bandwidth_group *group = &tcm->groups[i];
1705 
1706 		if (!list_empty(&group->ports))
1707 			tb_recalc_estimated_bandwidth_for_group(group);
1708 	}
1709 
1710 	tb_dbg(tb, "bandwidth re-calculation done\n");
1711 }
1712 
tb_find_dp_out(struct tb * tb,struct tb_port * in)1713 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
1714 {
1715 	struct tb_port *host_port, *port;
1716 	struct tb_cm *tcm = tb_priv(tb);
1717 
1718 	host_port = tb_route(in->sw) ?
1719 		tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
1720 
1721 	list_for_each_entry(port, &tcm->dp_resources, list) {
1722 		if (!tb_port_is_dpout(port))
1723 			continue;
1724 
1725 		if (tb_port_is_enabled(port)) {
1726 			tb_port_dbg(port, "DP OUT in use\n");
1727 			continue;
1728 		}
1729 
1730 		tb_port_dbg(port, "DP OUT available\n");
1731 
1732 		/*
1733 		 * Keep the DP tunnel under the topology starting from
1734 		 * the same host router downstream port.
1735 		 */
1736 		if (host_port && tb_route(port->sw)) {
1737 			struct tb_port *p;
1738 
1739 			p = tb_port_at(tb_route(port->sw), tb->root_switch);
1740 			if (p != host_port)
1741 				continue;
1742 		}
1743 
1744 		return port;
1745 	}
1746 
1747 	return NULL;
1748 }
1749 
tb_tunnel_one_dp(struct tb * tb,struct tb_port * in,struct tb_port * out)1750 static bool tb_tunnel_one_dp(struct tb *tb, struct tb_port *in,
1751 			     struct tb_port *out)
1752 {
1753 	int available_up, available_down, ret, link_nr;
1754 	struct tb_cm *tcm = tb_priv(tb);
1755 	int consumed_up, consumed_down;
1756 	struct tb_tunnel *tunnel;
1757 
1758 	/*
1759 	 * This is only applicable to links that are not bonded (so
1760 	 * when Thunderbolt 1 hardware is involved somewhere in the
1761 	 * topology). For these try to share the DP bandwidth between
1762 	 * the two lanes.
1763 	 */
1764 	link_nr = 1;
1765 	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
1766 		if (tb_tunnel_is_dp(tunnel)) {
1767 			link_nr = 0;
1768 			break;
1769 		}
1770 	}
1771 
1772 	/*
1773 	 * DP stream needs the domain to be active so runtime resume
1774 	 * both ends of the tunnel.
1775 	 *
1776 	 * This should bring the routers in the middle active as well
1777 	 * and keeps the domain from runtime suspending while the DP
1778 	 * tunnel is active.
1779 	 */
1780 	pm_runtime_get_sync(&in->sw->dev);
1781 	pm_runtime_get_sync(&out->sw->dev);
1782 
1783 	if (tb_switch_alloc_dp_resource(in->sw, in)) {
1784 		tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
1785 		goto err_rpm_put;
1786 	}
1787 
1788 	if (!tb_attach_bandwidth_group(tcm, in, out))
1789 		goto err_dealloc_dp;
1790 
1791 	/* Make all unused USB3 bandwidth available for the new DP tunnel */
1792 	ret = tb_release_unused_usb3_bandwidth(tb, in, out);
1793 	if (ret) {
1794 		tb_warn(tb, "failed to release unused bandwidth\n");
1795 		goto err_detach_group;
1796 	}
1797 
1798 	ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down,
1799 				     true);
1800 	if (ret)
1801 		goto err_reclaim_usb;
1802 
1803 	tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
1804 	       available_up, available_down);
1805 
1806 	tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
1807 				    available_down);
1808 	if (!tunnel) {
1809 		tb_port_dbg(out, "could not allocate DP tunnel\n");
1810 		goto err_reclaim_usb;
1811 	}
1812 
1813 	if (tb_tunnel_activate(tunnel)) {
1814 		tb_port_info(out, "DP tunnel activation failed, aborting\n");
1815 		goto err_free;
1816 	}
1817 
1818 	/* If fail reading tunnel's consumed bandwidth, tear it down */
1819 	ret = tb_tunnel_consumed_bandwidth(tunnel, &consumed_up, &consumed_down);
1820 	if (ret)
1821 		goto err_deactivate;
1822 
1823 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
1824 
1825 	tb_reclaim_usb3_bandwidth(tb, in, out);
1826 	/*
1827 	 * Transition the links to asymmetric if the consumption exceeds
1828 	 * the threshold.
1829 	 */
1830 	tb_configure_asym(tb, in, out, consumed_up, consumed_down);
1831 
1832 	/* Update the domain with the new bandwidth estimation */
1833 	tb_recalc_estimated_bandwidth(tb);
1834 
1835 	/*
1836 	 * In case of DP tunnel exists, change host router's 1st children
1837 	 * TMU mode to HiFi for CL0s to work.
1838 	 */
1839 	tb_increase_tmu_accuracy(tunnel);
1840 	return true;
1841 
1842 err_deactivate:
1843 	tb_tunnel_deactivate(tunnel);
1844 err_free:
1845 	tb_tunnel_free(tunnel);
1846 err_reclaim_usb:
1847 	tb_reclaim_usb3_bandwidth(tb, in, out);
1848 err_detach_group:
1849 	tb_detach_bandwidth_group(in);
1850 err_dealloc_dp:
1851 	tb_switch_dealloc_dp_resource(in->sw, in);
1852 err_rpm_put:
1853 	pm_runtime_mark_last_busy(&out->sw->dev);
1854 	pm_runtime_put_autosuspend(&out->sw->dev);
1855 	pm_runtime_mark_last_busy(&in->sw->dev);
1856 	pm_runtime_put_autosuspend(&in->sw->dev);
1857 
1858 	return false;
1859 }
1860 
tb_tunnel_dp(struct tb * tb)1861 static void tb_tunnel_dp(struct tb *tb)
1862 {
1863 	struct tb_cm *tcm = tb_priv(tb);
1864 	struct tb_port *port, *in, *out;
1865 
1866 	if (!tb_acpi_may_tunnel_dp()) {
1867 		tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
1868 		return;
1869 	}
1870 
1871 	/*
1872 	 * Find pair of inactive DP IN and DP OUT adapters and then
1873 	 * establish a DP tunnel between them.
1874 	 */
1875 	tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
1876 
1877 	in = NULL;
1878 	out = NULL;
1879 	list_for_each_entry(port, &tcm->dp_resources, list) {
1880 		if (!tb_port_is_dpin(port))
1881 			continue;
1882 
1883 		if (tb_port_is_enabled(port)) {
1884 			tb_port_dbg(port, "DP IN in use\n");
1885 			continue;
1886 		}
1887 
1888 		in = port;
1889 		tb_port_dbg(in, "DP IN available\n");
1890 
1891 		out = tb_find_dp_out(tb, port);
1892 		if (out)
1893 			tb_tunnel_one_dp(tb, in, out);
1894 		else
1895 			tb_port_dbg(in, "no suitable DP OUT adapter available, not tunneling\n");
1896 	}
1897 
1898 	if (!in)
1899 		tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
1900 }
1901 
tb_enter_redrive(struct tb_port * port)1902 static void tb_enter_redrive(struct tb_port *port)
1903 {
1904 	struct tb_switch *sw = port->sw;
1905 
1906 	if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE))
1907 		return;
1908 
1909 	/*
1910 	 * If we get hot-unplug for the DP IN port of the host router
1911 	 * and the DP resource is not available anymore it means there
1912 	 * is a monitor connected directly to the Type-C port and we are
1913 	 * in "redrive" mode. For this to work we cannot enter RTD3 so
1914 	 * we bump up the runtime PM reference count here.
1915 	 */
1916 	if (!tb_port_is_dpin(port))
1917 		return;
1918 	if (tb_route(sw))
1919 		return;
1920 	if (!tb_switch_query_dp_resource(sw, port)) {
1921 		port->redrive = true;
1922 		pm_runtime_get(&sw->dev);
1923 		tb_port_dbg(port, "enter redrive mode, keeping powered\n");
1924 	}
1925 }
1926 
tb_exit_redrive(struct tb_port * port)1927 static void tb_exit_redrive(struct tb_port *port)
1928 {
1929 	struct tb_switch *sw = port->sw;
1930 
1931 	if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE))
1932 		return;
1933 
1934 	if (!tb_port_is_dpin(port))
1935 		return;
1936 	if (tb_route(sw))
1937 		return;
1938 	if (port->redrive && tb_switch_query_dp_resource(sw, port)) {
1939 		port->redrive = false;
1940 		pm_runtime_put(&sw->dev);
1941 		tb_port_dbg(port, "exit redrive mode\n");
1942 	}
1943 }
1944 
tb_switch_enter_redrive(struct tb_switch * sw)1945 static void tb_switch_enter_redrive(struct tb_switch *sw)
1946 {
1947 	struct tb_port *port;
1948 
1949 	tb_switch_for_each_port(sw, port)
1950 		tb_enter_redrive(port);
1951 }
1952 
1953 /*
1954  * Called during system and runtime suspend to forcefully exit redrive
1955  * mode without querying whether the resource is available.
1956  */
tb_switch_exit_redrive(struct tb_switch * sw)1957 static void tb_switch_exit_redrive(struct tb_switch *sw)
1958 {
1959 	struct tb_port *port;
1960 
1961 	if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE))
1962 		return;
1963 
1964 	tb_switch_for_each_port(sw, port) {
1965 		if (!tb_port_is_dpin(port))
1966 			continue;
1967 
1968 		if (port->redrive) {
1969 			port->redrive = false;
1970 			pm_runtime_put(&sw->dev);
1971 			tb_port_dbg(port, "exit redrive mode\n");
1972 		}
1973 	}
1974 }
1975 
tb_dp_resource_unavailable(struct tb * tb,struct tb_port * port)1976 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
1977 {
1978 	struct tb_port *in, *out;
1979 	struct tb_tunnel *tunnel;
1980 
1981 	if (tb_port_is_dpin(port)) {
1982 		tb_port_dbg(port, "DP IN resource unavailable\n");
1983 		in = port;
1984 		out = NULL;
1985 	} else {
1986 		tb_port_dbg(port, "DP OUT resource unavailable\n");
1987 		in = NULL;
1988 		out = port;
1989 	}
1990 
1991 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
1992 	if (tunnel)
1993 		tb_deactivate_and_free_tunnel(tunnel);
1994 	else
1995 		tb_enter_redrive(port);
1996 	list_del_init(&port->list);
1997 
1998 	/*
1999 	 * See if there is another DP OUT port that can be used for
2000 	 * to create another tunnel.
2001 	 */
2002 	tb_recalc_estimated_bandwidth(tb);
2003 	tb_tunnel_dp(tb);
2004 }
2005 
tb_dp_resource_available(struct tb * tb,struct tb_port * port)2006 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
2007 {
2008 	struct tb_cm *tcm = tb_priv(tb);
2009 	struct tb_port *p;
2010 
2011 	if (tb_port_is_enabled(port))
2012 		return;
2013 
2014 	list_for_each_entry(p, &tcm->dp_resources, list) {
2015 		if (p == port)
2016 			return;
2017 	}
2018 
2019 	tb_port_dbg(port, "DP %s resource available\n",
2020 		    tb_port_is_dpin(port) ? "IN" : "OUT");
2021 	list_add_tail(&port->list, &tcm->dp_resources);
2022 	tb_exit_redrive(port);
2023 
2024 	/* Look for suitable DP IN <-> DP OUT pairs now */
2025 	tb_tunnel_dp(tb);
2026 }
2027 
tb_disconnect_and_release_dp(struct tb * tb)2028 static void tb_disconnect_and_release_dp(struct tb *tb)
2029 {
2030 	struct tb_cm *tcm = tb_priv(tb);
2031 	struct tb_tunnel *tunnel, *n;
2032 
2033 	/*
2034 	 * Tear down all DP tunnels and release their resources. They
2035 	 * will be re-established after resume based on plug events.
2036 	 */
2037 	list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
2038 		if (tb_tunnel_is_dp(tunnel))
2039 			tb_deactivate_and_free_tunnel(tunnel);
2040 	}
2041 
2042 	while (!list_empty(&tcm->dp_resources)) {
2043 		struct tb_port *port;
2044 
2045 		port = list_first_entry(&tcm->dp_resources,
2046 					struct tb_port, list);
2047 		list_del_init(&port->list);
2048 	}
2049 }
2050 
tb_disconnect_pci(struct tb * tb,struct tb_switch * sw)2051 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
2052 {
2053 	struct tb_tunnel *tunnel;
2054 	struct tb_port *up;
2055 
2056 	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
2057 	if (WARN_ON(!up))
2058 		return -ENODEV;
2059 
2060 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
2061 	if (WARN_ON(!tunnel))
2062 		return -ENODEV;
2063 
2064 	tb_switch_xhci_disconnect(sw);
2065 
2066 	tb_tunnel_deactivate(tunnel);
2067 	list_del(&tunnel->list);
2068 	tb_tunnel_free(tunnel);
2069 	return 0;
2070 }
2071 
tb_tunnel_pci(struct tb * tb,struct tb_switch * sw)2072 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
2073 {
2074 	struct tb_port *up, *down, *port;
2075 	struct tb_cm *tcm = tb_priv(tb);
2076 	struct tb_tunnel *tunnel;
2077 
2078 	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
2079 	if (!up)
2080 		return 0;
2081 
2082 	/*
2083 	 * Look up available down port. Since we are chaining it should
2084 	 * be found right above this switch.
2085 	 */
2086 	port = tb_switch_downstream_port(sw);
2087 	down = tb_find_pcie_down(tb_switch_parent(sw), port);
2088 	if (!down)
2089 		return 0;
2090 
2091 	tunnel = tb_tunnel_alloc_pci(tb, up, down);
2092 	if (!tunnel)
2093 		return -ENOMEM;
2094 
2095 	if (tb_tunnel_activate(tunnel)) {
2096 		tb_port_info(up,
2097 			     "PCIe tunnel activation failed, aborting\n");
2098 		tb_tunnel_free(tunnel);
2099 		return -EIO;
2100 	}
2101 
2102 	/*
2103 	 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
2104 	 * here.
2105 	 */
2106 	if (tb_switch_pcie_l1_enable(sw))
2107 		tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
2108 
2109 	if (tb_switch_xhci_connect(sw))
2110 		tb_sw_warn(sw, "failed to connect xHCI\n");
2111 
2112 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
2113 	return 0;
2114 }
2115 
tb_approve_xdomain_paths(struct tb * tb,struct tb_xdomain * xd,int transmit_path,int transmit_ring,int receive_path,int receive_ring)2116 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
2117 				    int transmit_path, int transmit_ring,
2118 				    int receive_path, int receive_ring)
2119 {
2120 	struct tb_cm *tcm = tb_priv(tb);
2121 	struct tb_port *nhi_port, *dst_port;
2122 	struct tb_tunnel *tunnel;
2123 	struct tb_switch *sw;
2124 	int ret;
2125 
2126 	sw = tb_to_switch(xd->dev.parent);
2127 	dst_port = tb_port_at(xd->route, sw);
2128 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
2129 
2130 	mutex_lock(&tb->lock);
2131 
2132 	/*
2133 	 * When tunneling DMA paths the link should not enter CL states
2134 	 * so disable them now.
2135 	 */
2136 	tb_disable_clx(sw);
2137 
2138 	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
2139 				     transmit_ring, receive_path, receive_ring);
2140 	if (!tunnel) {
2141 		ret = -ENOMEM;
2142 		goto err_clx;
2143 	}
2144 
2145 	if (tb_tunnel_activate(tunnel)) {
2146 		tb_port_info(nhi_port,
2147 			     "DMA tunnel activation failed, aborting\n");
2148 		ret = -EIO;
2149 		goto err_free;
2150 	}
2151 
2152 	list_add_tail(&tunnel->list, &tcm->tunnel_list);
2153 	mutex_unlock(&tb->lock);
2154 	return 0;
2155 
2156 err_free:
2157 	tb_tunnel_free(tunnel);
2158 err_clx:
2159 	tb_enable_clx(sw);
2160 	mutex_unlock(&tb->lock);
2161 
2162 	return ret;
2163 }
2164 
__tb_disconnect_xdomain_paths(struct tb * tb,struct tb_xdomain * xd,int transmit_path,int transmit_ring,int receive_path,int receive_ring)2165 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
2166 					  int transmit_path, int transmit_ring,
2167 					  int receive_path, int receive_ring)
2168 {
2169 	struct tb_cm *tcm = tb_priv(tb);
2170 	struct tb_port *nhi_port, *dst_port;
2171 	struct tb_tunnel *tunnel, *n;
2172 	struct tb_switch *sw;
2173 
2174 	sw = tb_to_switch(xd->dev.parent);
2175 	dst_port = tb_port_at(xd->route, sw);
2176 	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
2177 
2178 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
2179 		if (!tb_tunnel_is_dma(tunnel))
2180 			continue;
2181 		if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
2182 			continue;
2183 
2184 		if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
2185 					receive_path, receive_ring))
2186 			tb_deactivate_and_free_tunnel(tunnel);
2187 	}
2188 
2189 	/*
2190 	 * Try to re-enable CL states now, it is OK if this fails
2191 	 * because we may still have another DMA tunnel active through
2192 	 * the same host router USB4 downstream port.
2193 	 */
2194 	tb_enable_clx(sw);
2195 }
2196 
tb_disconnect_xdomain_paths(struct tb * tb,struct tb_xdomain * xd,int transmit_path,int transmit_ring,int receive_path,int receive_ring)2197 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
2198 				       int transmit_path, int transmit_ring,
2199 				       int receive_path, int receive_ring)
2200 {
2201 	if (!xd->is_unplugged) {
2202 		mutex_lock(&tb->lock);
2203 		__tb_disconnect_xdomain_paths(tb, xd, transmit_path,
2204 					      transmit_ring, receive_path,
2205 					      receive_ring);
2206 		mutex_unlock(&tb->lock);
2207 	}
2208 	return 0;
2209 }
2210 
2211 /* hotplug handling */
2212 
2213 /*
2214  * tb_handle_hotplug() - handle hotplug event
2215  *
2216  * Executes on tb->wq.
2217  */
tb_handle_hotplug(struct work_struct * work)2218 static void tb_handle_hotplug(struct work_struct *work)
2219 {
2220 	struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
2221 	struct tb *tb = ev->tb;
2222 	struct tb_cm *tcm = tb_priv(tb);
2223 	struct tb_switch *sw;
2224 	struct tb_port *port;
2225 
2226 	/* Bring the domain back from sleep if it was suspended */
2227 	pm_runtime_get_sync(&tb->dev);
2228 
2229 	mutex_lock(&tb->lock);
2230 	if (!tcm->hotplug_active)
2231 		goto out; /* during init, suspend or shutdown */
2232 
2233 	sw = tb_switch_find_by_route(tb, ev->route);
2234 	if (!sw) {
2235 		tb_warn(tb,
2236 			"hotplug event from non existent switch %llx:%x (unplug: %d)\n",
2237 			ev->route, ev->port, ev->unplug);
2238 		goto out;
2239 	}
2240 	if (ev->port > sw->config.max_port_number) {
2241 		tb_warn(tb,
2242 			"hotplug event from non existent port %llx:%x (unplug: %d)\n",
2243 			ev->route, ev->port, ev->unplug);
2244 		goto put_sw;
2245 	}
2246 	port = &sw->ports[ev->port];
2247 	if (tb_is_upstream_port(port)) {
2248 		tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
2249 		       ev->route, ev->port, ev->unplug);
2250 		goto put_sw;
2251 	}
2252 
2253 	pm_runtime_get_sync(&sw->dev);
2254 
2255 	if (ev->unplug) {
2256 		tb_retimer_remove_all(port);
2257 
2258 		if (tb_port_has_remote(port)) {
2259 			tb_port_dbg(port, "switch unplugged\n");
2260 			tb_sw_set_unplugged(port->remote->sw);
2261 			tb_free_invalid_tunnels(tb);
2262 			tb_remove_dp_resources(port->remote->sw);
2263 			tb_switch_tmu_disable(port->remote->sw);
2264 			tb_switch_unconfigure_link(port->remote->sw);
2265 			tb_switch_set_link_width(port->remote->sw,
2266 						 TB_LINK_WIDTH_SINGLE);
2267 			tb_switch_remove(port->remote->sw);
2268 			port->remote = NULL;
2269 			if (port->dual_link_port)
2270 				port->dual_link_port->remote = NULL;
2271 			/* Maybe we can create another DP tunnel */
2272 			tb_recalc_estimated_bandwidth(tb);
2273 			tb_tunnel_dp(tb);
2274 		} else if (port->xdomain) {
2275 			struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
2276 
2277 			tb_port_dbg(port, "xdomain unplugged\n");
2278 			/*
2279 			 * Service drivers are unbound during
2280 			 * tb_xdomain_remove() so setting XDomain as
2281 			 * unplugged here prevents deadlock if they call
2282 			 * tb_xdomain_disable_paths(). We will tear down
2283 			 * all the tunnels below.
2284 			 */
2285 			xd->is_unplugged = true;
2286 			tb_xdomain_remove(xd);
2287 			port->xdomain = NULL;
2288 			__tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
2289 			tb_xdomain_put(xd);
2290 			tb_port_unconfigure_xdomain(port);
2291 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
2292 			tb_dp_resource_unavailable(tb, port);
2293 		} else if (!port->port) {
2294 			tb_sw_dbg(sw, "xHCI disconnect request\n");
2295 			tb_switch_xhci_disconnect(sw);
2296 		} else {
2297 			tb_port_dbg(port,
2298 				   "got unplug event for disconnected port, ignoring\n");
2299 		}
2300 	} else if (port->remote) {
2301 		tb_port_dbg(port, "got plug event for connected port, ignoring\n");
2302 	} else if (!port->port && sw->authorized) {
2303 		tb_sw_dbg(sw, "xHCI connect request\n");
2304 		tb_switch_xhci_connect(sw);
2305 	} else {
2306 		if (tb_port_is_null(port)) {
2307 			tb_port_dbg(port, "hotplug: scanning\n");
2308 			tb_scan_port(port);
2309 			if (!port->remote)
2310 				tb_port_dbg(port, "hotplug: no switch found\n");
2311 		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
2312 			tb_dp_resource_available(tb, port);
2313 		}
2314 	}
2315 
2316 	pm_runtime_mark_last_busy(&sw->dev);
2317 	pm_runtime_put_autosuspend(&sw->dev);
2318 
2319 put_sw:
2320 	tb_switch_put(sw);
2321 out:
2322 	mutex_unlock(&tb->lock);
2323 
2324 	pm_runtime_mark_last_busy(&tb->dev);
2325 	pm_runtime_put_autosuspend(&tb->dev);
2326 
2327 	kfree(ev);
2328 }
2329 
tb_alloc_dp_bandwidth(struct tb_tunnel * tunnel,int * requested_up,int * requested_down)2330 static int tb_alloc_dp_bandwidth(struct tb_tunnel *tunnel, int *requested_up,
2331 				 int *requested_down)
2332 {
2333 	int allocated_up, allocated_down, available_up, available_down, ret;
2334 	int requested_up_corrected, requested_down_corrected, granularity;
2335 	int max_up, max_down, max_up_rounded, max_down_rounded;
2336 	struct tb *tb = tunnel->tb;
2337 	struct tb_port *in, *out;
2338 
2339 	ret = tb_tunnel_allocated_bandwidth(tunnel, &allocated_up, &allocated_down);
2340 	if (ret)
2341 		return ret;
2342 
2343 	in = tunnel->src_port;
2344 	out = tunnel->dst_port;
2345 
2346 	tb_port_dbg(in, "bandwidth allocated currently %d/%d Mb/s\n",
2347 		    allocated_up, allocated_down);
2348 
2349 	/*
2350 	 * If we get rounded up request from graphics side, say HBR2 x 4
2351 	 * that is 17500 instead of 17280 (this is because of the
2352 	 * granularity), we allow it too. Here the graphics has already
2353 	 * negotiated with the DPRX the maximum possible rates (which is
2354 	 * 17280 in this case).
2355 	 *
2356 	 * Since the link cannot go higher than 17280 we use that in our
2357 	 * calculations but the DP IN adapter Allocated BW write must be
2358 	 * the same value (17500) otherwise the adapter will mark it as
2359 	 * failed for graphics.
2360 	 */
2361 	ret = tb_tunnel_maximum_bandwidth(tunnel, &max_up, &max_down);
2362 	if (ret)
2363 		return ret;
2364 
2365 	ret = usb4_dp_port_granularity(in);
2366 	if (ret < 0)
2367 		return ret;
2368 	granularity = ret;
2369 
2370 	max_up_rounded = roundup(max_up, granularity);
2371 	max_down_rounded = roundup(max_down, granularity);
2372 
2373 	/*
2374 	 * This will "fix" the request down to the maximum supported
2375 	 * rate * lanes if it is at the maximum rounded up level.
2376 	 */
2377 	requested_up_corrected = *requested_up;
2378 	if (requested_up_corrected == max_up_rounded)
2379 		requested_up_corrected = max_up;
2380 	else if (requested_up_corrected < 0)
2381 		requested_up_corrected = 0;
2382 	requested_down_corrected = *requested_down;
2383 	if (requested_down_corrected == max_down_rounded)
2384 		requested_down_corrected = max_down;
2385 	else if (requested_down_corrected < 0)
2386 		requested_down_corrected = 0;
2387 
2388 	tb_port_dbg(in, "corrected bandwidth request %d/%d Mb/s\n",
2389 		    requested_up_corrected, requested_down_corrected);
2390 
2391 	if ((*requested_up >= 0 && requested_up_corrected > max_up_rounded) ||
2392 	    (*requested_down >= 0 && requested_down_corrected > max_down_rounded)) {
2393 		tb_port_dbg(in, "bandwidth request too high (%d/%d Mb/s > %d/%d Mb/s)\n",
2394 			    requested_up_corrected, requested_down_corrected,
2395 			    max_up_rounded, max_down_rounded);
2396 		return -ENOBUFS;
2397 	}
2398 
2399 	if ((*requested_up >= 0 && requested_up_corrected <= allocated_up) ||
2400 	    (*requested_down >= 0 && requested_down_corrected <= allocated_down)) {
2401 		/*
2402 		 * If bandwidth on a link is < asym_threshold transition
2403 		 * the link to symmetric.
2404 		 */
2405 		tb_configure_sym(tb, in, out, *requested_up, *requested_down);
2406 		/*
2407 		 * If requested bandwidth is less or equal than what is
2408 		 * currently allocated to that tunnel we simply change
2409 		 * the reservation of the tunnel. Since all the tunnels
2410 		 * going out from the same USB4 port are in the same
2411 		 * group the released bandwidth will be taken into
2412 		 * account for the other tunnels automatically below.
2413 		 */
2414 		return tb_tunnel_alloc_bandwidth(tunnel, requested_up,
2415 						 requested_down);
2416 	}
2417 
2418 	/*
2419 	 * More bandwidth is requested. Release all the potential
2420 	 * bandwidth from USB3 first.
2421 	 */
2422 	ret = tb_release_unused_usb3_bandwidth(tb, in, out);
2423 	if (ret)
2424 		return ret;
2425 
2426 	/*
2427 	 * Then go over all tunnels that cross the same USB4 ports (they
2428 	 * are also in the same group but we use the same function here
2429 	 * that we use with the normal bandwidth allocation).
2430 	 */
2431 	ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down,
2432 				     true);
2433 	if (ret)
2434 		goto reclaim;
2435 
2436 	tb_port_dbg(in, "bandwidth available for allocation %d/%d Mb/s\n",
2437 		    available_up, available_down);
2438 
2439 	if ((*requested_up >= 0 && available_up >= requested_up_corrected) ||
2440 	    (*requested_down >= 0 && available_down >= requested_down_corrected)) {
2441 		/*
2442 		 * If bandwidth on a link is >= asym_threshold
2443 		 * transition the link to asymmetric.
2444 		 */
2445 		ret = tb_configure_asym(tb, in, out, *requested_up,
2446 					*requested_down);
2447 		if (ret) {
2448 			tb_configure_sym(tb, in, out, 0, 0);
2449 			return ret;
2450 		}
2451 
2452 		ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up,
2453 						requested_down);
2454 		if (ret) {
2455 			tb_tunnel_warn(tunnel, "failed to allocate bandwidth\n");
2456 			tb_configure_sym(tb, in, out, 0, 0);
2457 		}
2458 	} else {
2459 		ret = -ENOBUFS;
2460 	}
2461 
2462 reclaim:
2463 	tb_reclaim_usb3_bandwidth(tb, in, out);
2464 	return ret;
2465 }
2466 
tb_handle_dp_bandwidth_request(struct work_struct * work)2467 static void tb_handle_dp_bandwidth_request(struct work_struct *work)
2468 {
2469 	struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
2470 	int requested_bw, requested_up, requested_down, ret;
2471 	struct tb_port *in, *out;
2472 	struct tb_tunnel *tunnel;
2473 	struct tb *tb = ev->tb;
2474 	struct tb_cm *tcm = tb_priv(tb);
2475 	struct tb_switch *sw;
2476 
2477 	pm_runtime_get_sync(&tb->dev);
2478 
2479 	mutex_lock(&tb->lock);
2480 	if (!tcm->hotplug_active)
2481 		goto unlock;
2482 
2483 	sw = tb_switch_find_by_route(tb, ev->route);
2484 	if (!sw) {
2485 		tb_warn(tb, "bandwidth request from non-existent router %llx\n",
2486 			ev->route);
2487 		goto unlock;
2488 	}
2489 
2490 	in = &sw->ports[ev->port];
2491 	if (!tb_port_is_dpin(in)) {
2492 		tb_port_warn(in, "bandwidth request to non-DP IN adapter\n");
2493 		goto put_sw;
2494 	}
2495 
2496 	tb_port_dbg(in, "handling bandwidth allocation request\n");
2497 
2498 	if (!usb4_dp_port_bandwidth_mode_enabled(in)) {
2499 		tb_port_warn(in, "bandwidth allocation mode not enabled\n");
2500 		goto put_sw;
2501 	}
2502 
2503 	ret = usb4_dp_port_requested_bandwidth(in);
2504 	if (ret < 0) {
2505 		if (ret == -ENODATA)
2506 			tb_port_dbg(in, "no bandwidth request active\n");
2507 		else
2508 			tb_port_warn(in, "failed to read requested bandwidth\n");
2509 		goto put_sw;
2510 	}
2511 	requested_bw = ret;
2512 
2513 	tb_port_dbg(in, "requested bandwidth %d Mb/s\n", requested_bw);
2514 
2515 	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
2516 	if (!tunnel) {
2517 		tb_port_warn(in, "failed to find tunnel\n");
2518 		goto put_sw;
2519 	}
2520 
2521 	out = tunnel->dst_port;
2522 
2523 	if (tb_port_path_direction_downstream(in, out)) {
2524 		requested_up = -1;
2525 		requested_down = requested_bw;
2526 	} else {
2527 		requested_up = requested_bw;
2528 		requested_down = -1;
2529 	}
2530 
2531 	ret = tb_alloc_dp_bandwidth(tunnel, &requested_up, &requested_down);
2532 	if (ret) {
2533 		if (ret == -ENOBUFS)
2534 			tb_port_warn(in, "not enough bandwidth available\n");
2535 		else
2536 			tb_port_warn(in, "failed to change bandwidth allocation\n");
2537 	} else {
2538 		tb_port_dbg(in, "bandwidth allocation changed to %d/%d Mb/s\n",
2539 			    requested_up, requested_down);
2540 
2541 		/* Update other clients about the allocation change */
2542 		tb_recalc_estimated_bandwidth(tb);
2543 	}
2544 
2545 put_sw:
2546 	tb_switch_put(sw);
2547 unlock:
2548 	mutex_unlock(&tb->lock);
2549 
2550 	pm_runtime_mark_last_busy(&tb->dev);
2551 	pm_runtime_put_autosuspend(&tb->dev);
2552 
2553 	kfree(ev);
2554 }
2555 
tb_queue_dp_bandwidth_request(struct tb * tb,u64 route,u8 port)2556 static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port)
2557 {
2558 	struct tb_hotplug_event *ev;
2559 
2560 	ev = kmalloc(sizeof(*ev), GFP_KERNEL);
2561 	if (!ev)
2562 		return;
2563 
2564 	ev->tb = tb;
2565 	ev->route = route;
2566 	ev->port = port;
2567 	INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request);
2568 	queue_work(tb->wq, &ev->work);
2569 }
2570 
tb_handle_notification(struct tb * tb,u64 route,const struct cfg_error_pkg * error)2571 static void tb_handle_notification(struct tb *tb, u64 route,
2572 				   const struct cfg_error_pkg *error)
2573 {
2574 
2575 	switch (error->error) {
2576 	case TB_CFG_ERROR_PCIE_WAKE:
2577 	case TB_CFG_ERROR_DP_CON_CHANGE:
2578 	case TB_CFG_ERROR_DPTX_DISCOVERY:
2579 		if (tb_cfg_ack_notification(tb->ctl, route, error))
2580 			tb_warn(tb, "could not ack notification on %llx\n",
2581 				route);
2582 		break;
2583 
2584 	case TB_CFG_ERROR_DP_BW:
2585 		if (tb_cfg_ack_notification(tb->ctl, route, error))
2586 			tb_warn(tb, "could not ack notification on %llx\n",
2587 				route);
2588 		tb_queue_dp_bandwidth_request(tb, route, error->port);
2589 		break;
2590 
2591 	default:
2592 		/* Ignore for now */
2593 		break;
2594 	}
2595 }
2596 
2597 /*
2598  * tb_schedule_hotplug_handler() - callback function for the control channel
2599  *
2600  * Delegates to tb_handle_hotplug.
2601  */
tb_handle_event(struct tb * tb,enum tb_cfg_pkg_type type,const void * buf,size_t size)2602 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
2603 			    const void *buf, size_t size)
2604 {
2605 	const struct cfg_event_pkg *pkg = buf;
2606 	u64 route = tb_cfg_get_route(&pkg->header);
2607 
2608 	switch (type) {
2609 	case TB_CFG_PKG_ERROR:
2610 		tb_handle_notification(tb, route, (const struct cfg_error_pkg *)buf);
2611 		return;
2612 	case TB_CFG_PKG_EVENT:
2613 		break;
2614 	default:
2615 		tb_warn(tb, "unexpected event %#x, ignoring\n", type);
2616 		return;
2617 	}
2618 
2619 	if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
2620 		tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
2621 			pkg->port);
2622 	}
2623 
2624 	tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
2625 }
2626 
tb_stop(struct tb * tb)2627 static void tb_stop(struct tb *tb)
2628 {
2629 	struct tb_cm *tcm = tb_priv(tb);
2630 	struct tb_tunnel *tunnel;
2631 	struct tb_tunnel *n;
2632 
2633 	cancel_delayed_work(&tcm->remove_work);
2634 	/* tunnels are only present after everything has been initialized */
2635 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
2636 		/*
2637 		 * DMA tunnels require the driver to be functional so we
2638 		 * tear them down. Other protocol tunnels can be left
2639 		 * intact.
2640 		 */
2641 		if (tb_tunnel_is_dma(tunnel))
2642 			tb_tunnel_deactivate(tunnel);
2643 		tb_tunnel_free(tunnel);
2644 	}
2645 	tb_switch_remove(tb->root_switch);
2646 	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
2647 }
2648 
tb_scan_finalize_switch(struct device * dev,void * data)2649 static int tb_scan_finalize_switch(struct device *dev, void *data)
2650 {
2651 	if (tb_is_switch(dev)) {
2652 		struct tb_switch *sw = tb_to_switch(dev);
2653 
2654 		/*
2655 		 * If we found that the switch was already setup by the
2656 		 * boot firmware, mark it as authorized now before we
2657 		 * send uevent to userspace.
2658 		 */
2659 		if (sw->boot)
2660 			sw->authorized = 1;
2661 
2662 		dev_set_uevent_suppress(dev, false);
2663 		kobject_uevent(&dev->kobj, KOBJ_ADD);
2664 		device_for_each_child(dev, NULL, tb_scan_finalize_switch);
2665 	}
2666 
2667 	return 0;
2668 }
2669 
tb_start(struct tb * tb,bool reset)2670 static int tb_start(struct tb *tb, bool reset)
2671 {
2672 	struct tb_cm *tcm = tb_priv(tb);
2673 	int ret;
2674 
2675 	tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
2676 	if (IS_ERR(tb->root_switch))
2677 		return PTR_ERR(tb->root_switch);
2678 
2679 	/*
2680 	 * ICM firmware upgrade needs running firmware and in native
2681 	 * mode that is not available so disable firmware upgrade of the
2682 	 * root switch.
2683 	 *
2684 	 * However, USB4 routers support NVM firmware upgrade if they
2685 	 * implement the necessary router operations.
2686 	 */
2687 	tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch);
2688 	/* All USB4 routers support runtime PM */
2689 	tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
2690 
2691 	ret = tb_switch_configure(tb->root_switch);
2692 	if (ret) {
2693 		tb_switch_put(tb->root_switch);
2694 		return ret;
2695 	}
2696 
2697 	/* Announce the switch to the world */
2698 	ret = tb_switch_add(tb->root_switch);
2699 	if (ret) {
2700 		tb_switch_put(tb->root_switch);
2701 		return ret;
2702 	}
2703 
2704 	/*
2705 	 * To support highest CLx state, we set host router's TMU to
2706 	 * Normal mode.
2707 	 */
2708 	tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES);
2709 	/* Enable TMU if it is off */
2710 	tb_switch_tmu_enable(tb->root_switch);
2711 
2712 	/*
2713 	 * Boot firmware might have created tunnels of its own. Since we
2714 	 * cannot be sure they are usable for us, tear them down and
2715 	 * reset the ports to handle it as new hotplug for USB4 v1
2716 	 * routers (for USB4 v2 and beyond we already do host reset).
2717 	 */
2718 	if (reset && usb4_switch_version(tb->root_switch) == 1) {
2719 		tb_switch_reset(tb->root_switch);
2720 	} else {
2721 		/* Full scan to discover devices added before the driver was loaded. */
2722 		tb_scan_switch(tb->root_switch);
2723 		/* Find out tunnels created by the boot firmware */
2724 		tb_discover_tunnels(tb);
2725 		/* Add DP resources from the DP tunnels created by the boot firmware */
2726 		tb_discover_dp_resources(tb);
2727 	}
2728 
2729 	/*
2730 	 * If the boot firmware did not create USB 3.x tunnels create them
2731 	 * now for the whole topology.
2732 	 */
2733 	tb_create_usb3_tunnels(tb->root_switch);
2734 	/* Add DP IN resources for the root switch */
2735 	tb_add_dp_resources(tb->root_switch);
2736 	tb_switch_enter_redrive(tb->root_switch);
2737 	/* Make the discovered switches available to the userspace */
2738 	device_for_each_child(&tb->root_switch->dev, NULL,
2739 			      tb_scan_finalize_switch);
2740 
2741 	/* Allow tb_handle_hotplug to progress events */
2742 	tcm->hotplug_active = true;
2743 	return 0;
2744 }
2745 
tb_suspend_noirq(struct tb * tb)2746 static int tb_suspend_noirq(struct tb *tb)
2747 {
2748 	struct tb_cm *tcm = tb_priv(tb);
2749 
2750 	tb_dbg(tb, "suspending...\n");
2751 	tb_disconnect_and_release_dp(tb);
2752 	tb_switch_exit_redrive(tb->root_switch);
2753 	tb_switch_suspend(tb->root_switch, false);
2754 	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
2755 	tb_dbg(tb, "suspend finished\n");
2756 
2757 	return 0;
2758 }
2759 
tb_restore_children(struct tb_switch * sw)2760 static void tb_restore_children(struct tb_switch *sw)
2761 {
2762 	struct tb_port *port;
2763 
2764 	/* No need to restore if the router is already unplugged */
2765 	if (sw->is_unplugged)
2766 		return;
2767 
2768 	if (tb_enable_clx(sw))
2769 		tb_sw_warn(sw, "failed to re-enable CL states\n");
2770 
2771 	if (tb_enable_tmu(sw))
2772 		tb_sw_warn(sw, "failed to restore TMU configuration\n");
2773 
2774 	tb_switch_configuration_valid(sw);
2775 
2776 	tb_switch_for_each_port(sw, port) {
2777 		if (!tb_port_has_remote(port) && !port->xdomain)
2778 			continue;
2779 
2780 		if (port->remote) {
2781 			tb_switch_set_link_width(port->remote->sw,
2782 						 port->remote->sw->link_width);
2783 			tb_switch_configure_link(port->remote->sw);
2784 
2785 			tb_restore_children(port->remote->sw);
2786 		} else if (port->xdomain) {
2787 			tb_port_configure_xdomain(port, port->xdomain);
2788 		}
2789 	}
2790 }
2791 
tb_resume_noirq(struct tb * tb)2792 static int tb_resume_noirq(struct tb *tb)
2793 {
2794 	struct tb_cm *tcm = tb_priv(tb);
2795 	struct tb_tunnel *tunnel, *n;
2796 	unsigned int usb3_delay = 0;
2797 	LIST_HEAD(tunnels);
2798 
2799 	tb_dbg(tb, "resuming...\n");
2800 
2801 	/*
2802 	 * For non-USB4 hosts (Apple systems) remove any PCIe devices
2803 	 * the firmware might have setup.
2804 	 */
2805 	if (!tb_switch_is_usb4(tb->root_switch))
2806 		tb_switch_reset(tb->root_switch);
2807 
2808 	tb_switch_resume(tb->root_switch, false);
2809 	tb_free_invalid_tunnels(tb);
2810 	tb_free_unplugged_children(tb->root_switch);
2811 	tb_restore_children(tb->root_switch);
2812 
2813 	/*
2814 	 * If we get here from suspend to disk the boot firmware or the
2815 	 * restore kernel might have created tunnels of its own. Since
2816 	 * we cannot be sure they are usable for us we find and tear
2817 	 * them down.
2818 	 */
2819 	tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
2820 	list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
2821 		if (tb_tunnel_is_usb3(tunnel))
2822 			usb3_delay = 500;
2823 		tb_tunnel_deactivate(tunnel);
2824 		tb_tunnel_free(tunnel);
2825 	}
2826 
2827 	/* Re-create our tunnels now */
2828 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
2829 		/* USB3 requires delay before it can be re-activated */
2830 		if (tb_tunnel_is_usb3(tunnel)) {
2831 			msleep(usb3_delay);
2832 			/* Only need to do it once */
2833 			usb3_delay = 0;
2834 		}
2835 		tb_tunnel_restart(tunnel);
2836 	}
2837 	if (!list_empty(&tcm->tunnel_list)) {
2838 		/*
2839 		 * the pcie links need some time to get going.
2840 		 * 100ms works for me...
2841 		 */
2842 		tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
2843 		msleep(100);
2844 	}
2845 	tb_switch_enter_redrive(tb->root_switch);
2846 	 /* Allow tb_handle_hotplug to progress events */
2847 	tcm->hotplug_active = true;
2848 	tb_dbg(tb, "resume finished\n");
2849 
2850 	return 0;
2851 }
2852 
tb_free_unplugged_xdomains(struct tb_switch * sw)2853 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
2854 {
2855 	struct tb_port *port;
2856 	int ret = 0;
2857 
2858 	tb_switch_for_each_port(sw, port) {
2859 		if (tb_is_upstream_port(port))
2860 			continue;
2861 		if (port->xdomain && port->xdomain->is_unplugged) {
2862 			tb_retimer_remove_all(port);
2863 			tb_xdomain_remove(port->xdomain);
2864 			tb_port_unconfigure_xdomain(port);
2865 			port->xdomain = NULL;
2866 			ret++;
2867 		} else if (port->remote) {
2868 			ret += tb_free_unplugged_xdomains(port->remote->sw);
2869 		}
2870 	}
2871 
2872 	return ret;
2873 }
2874 
tb_freeze_noirq(struct tb * tb)2875 static int tb_freeze_noirq(struct tb *tb)
2876 {
2877 	struct tb_cm *tcm = tb_priv(tb);
2878 
2879 	tcm->hotplug_active = false;
2880 	return 0;
2881 }
2882 
tb_thaw_noirq(struct tb * tb)2883 static int tb_thaw_noirq(struct tb *tb)
2884 {
2885 	struct tb_cm *tcm = tb_priv(tb);
2886 
2887 	tcm->hotplug_active = true;
2888 	return 0;
2889 }
2890 
tb_complete(struct tb * tb)2891 static void tb_complete(struct tb *tb)
2892 {
2893 	/*
2894 	 * Release any unplugged XDomains and if there is a case where
2895 	 * another domain is swapped in place of unplugged XDomain we
2896 	 * need to run another rescan.
2897 	 */
2898 	mutex_lock(&tb->lock);
2899 	if (tb_free_unplugged_xdomains(tb->root_switch))
2900 		tb_scan_switch(tb->root_switch);
2901 	mutex_unlock(&tb->lock);
2902 }
2903 
tb_runtime_suspend(struct tb * tb)2904 static int tb_runtime_suspend(struct tb *tb)
2905 {
2906 	struct tb_cm *tcm = tb_priv(tb);
2907 
2908 	mutex_lock(&tb->lock);
2909 	/*
2910 	 * The below call only releases DP resources to allow exiting and
2911 	 * re-entering redrive mode.
2912 	 */
2913 	tb_disconnect_and_release_dp(tb);
2914 	tb_switch_exit_redrive(tb->root_switch);
2915 	tb_switch_suspend(tb->root_switch, true);
2916 	tcm->hotplug_active = false;
2917 	mutex_unlock(&tb->lock);
2918 
2919 	return 0;
2920 }
2921 
tb_remove_work(struct work_struct * work)2922 static void tb_remove_work(struct work_struct *work)
2923 {
2924 	struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
2925 	struct tb *tb = tcm_to_tb(tcm);
2926 
2927 	mutex_lock(&tb->lock);
2928 	if (tb->root_switch) {
2929 		tb_free_unplugged_children(tb->root_switch);
2930 		tb_free_unplugged_xdomains(tb->root_switch);
2931 	}
2932 	mutex_unlock(&tb->lock);
2933 }
2934 
tb_runtime_resume(struct tb * tb)2935 static int tb_runtime_resume(struct tb *tb)
2936 {
2937 	struct tb_cm *tcm = tb_priv(tb);
2938 	struct tb_tunnel *tunnel, *n;
2939 
2940 	mutex_lock(&tb->lock);
2941 	tb_switch_resume(tb->root_switch, true);
2942 	tb_free_invalid_tunnels(tb);
2943 	tb_restore_children(tb->root_switch);
2944 	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
2945 		tb_tunnel_restart(tunnel);
2946 	tb_switch_enter_redrive(tb->root_switch);
2947 	tcm->hotplug_active = true;
2948 	mutex_unlock(&tb->lock);
2949 
2950 	/*
2951 	 * Schedule cleanup of any unplugged devices. Run this in a
2952 	 * separate thread to avoid possible deadlock if the device
2953 	 * removal runtime resumes the unplugged device.
2954 	 */
2955 	queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
2956 	return 0;
2957 }
2958 
2959 static const struct tb_cm_ops tb_cm_ops = {
2960 	.start = tb_start,
2961 	.stop = tb_stop,
2962 	.suspend_noirq = tb_suspend_noirq,
2963 	.resume_noirq = tb_resume_noirq,
2964 	.freeze_noirq = tb_freeze_noirq,
2965 	.thaw_noirq = tb_thaw_noirq,
2966 	.complete = tb_complete,
2967 	.runtime_suspend = tb_runtime_suspend,
2968 	.runtime_resume = tb_runtime_resume,
2969 	.handle_event = tb_handle_event,
2970 	.disapprove_switch = tb_disconnect_pci,
2971 	.approve_switch = tb_tunnel_pci,
2972 	.approve_xdomain_paths = tb_approve_xdomain_paths,
2973 	.disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
2974 };
2975 
2976 /*
2977  * During suspend the Thunderbolt controller is reset and all PCIe
2978  * tunnels are lost. The NHI driver will try to reestablish all tunnels
2979  * during resume. This adds device links between the tunneled PCIe
2980  * downstream ports and the NHI so that the device core will make sure
2981  * NHI is resumed first before the rest.
2982  */
tb_apple_add_links(struct tb_nhi * nhi)2983 static bool tb_apple_add_links(struct tb_nhi *nhi)
2984 {
2985 	struct pci_dev *upstream, *pdev;
2986 	bool ret;
2987 
2988 	if (!x86_apple_machine)
2989 		return false;
2990 
2991 	switch (nhi->pdev->device) {
2992 	case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
2993 	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
2994 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
2995 	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
2996 		break;
2997 	default:
2998 		return false;
2999 	}
3000 
3001 	upstream = pci_upstream_bridge(nhi->pdev);
3002 	while (upstream) {
3003 		if (!pci_is_pcie(upstream))
3004 			return false;
3005 		if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
3006 			break;
3007 		upstream = pci_upstream_bridge(upstream);
3008 	}
3009 
3010 	if (!upstream)
3011 		return false;
3012 
3013 	/*
3014 	 * For each hotplug downstream port, create add device link
3015 	 * back to NHI so that PCIe tunnels can be re-established after
3016 	 * sleep.
3017 	 */
3018 	ret = false;
3019 	for_each_pci_bridge(pdev, upstream->subordinate) {
3020 		const struct device_link *link;
3021 
3022 		if (!pci_is_pcie(pdev))
3023 			continue;
3024 		if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
3025 		    !pdev->is_hotplug_bridge)
3026 			continue;
3027 
3028 		link = device_link_add(&pdev->dev, &nhi->pdev->dev,
3029 				       DL_FLAG_AUTOREMOVE_SUPPLIER |
3030 				       DL_FLAG_PM_RUNTIME);
3031 		if (link) {
3032 			dev_dbg(&nhi->pdev->dev, "created link from %s\n",
3033 				dev_name(&pdev->dev));
3034 			ret = true;
3035 		} else {
3036 			dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
3037 				 dev_name(&pdev->dev));
3038 		}
3039 	}
3040 
3041 	return ret;
3042 }
3043 
tb_probe(struct tb_nhi * nhi)3044 struct tb *tb_probe(struct tb_nhi *nhi)
3045 {
3046 	struct tb_cm *tcm;
3047 	struct tb *tb;
3048 
3049 	tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
3050 	if (!tb)
3051 		return NULL;
3052 
3053 	if (tb_acpi_may_tunnel_pcie())
3054 		tb->security_level = TB_SECURITY_USER;
3055 	else
3056 		tb->security_level = TB_SECURITY_NOPCIE;
3057 
3058 	tb->cm_ops = &tb_cm_ops;
3059 
3060 	tcm = tb_priv(tb);
3061 	INIT_LIST_HEAD(&tcm->tunnel_list);
3062 	INIT_LIST_HEAD(&tcm->dp_resources);
3063 	INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
3064 	tb_init_bandwidth_groups(tcm);
3065 
3066 	tb_dbg(tb, "using software connection manager\n");
3067 
3068 	/*
3069 	 * Device links are needed to make sure we establish tunnels
3070 	 * before the PCIe/USB stack is resumed so complain here if we
3071 	 * found them missing.
3072 	 */
3073 	if (!tb_apple_add_links(nhi) && !tb_acpi_add_links(nhi))
3074 		tb_warn(tb, "device links to tunneled native ports are missing!\n");
3075 
3076 	return tb;
3077 }
3078