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