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