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