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