1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
18 #define TB_TIMEOUT 100 /* ms */
21 * struct tb_cm - Simple Thunderbolt connection manager
22 * @tunnel_list: List of active tunnels
23 * @dp_resources: List of available DP resources for DP tunneling
24 * @hotplug_active: tb_handle_hotplug will stop progressing plug
25 * events and exit if this is not set (it needs to
26 * acquire the lock one more time). Used to drain wq
27 * after cfg has been paused.
28 * @remove_work: Work used to remove any unplugged routers after
32 struct list_head tunnel_list;
33 struct list_head dp_resources;
35 struct delayed_work remove_work;
38 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
40 return ((void *)tcm - sizeof(struct tb));
43 struct tb_hotplug_event {
44 struct work_struct work;
51 static void tb_handle_hotplug(struct work_struct *work);
53 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
55 struct tb_hotplug_event *ev;
57 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
65 INIT_WORK(&ev->work, tb_handle_hotplug);
66 queue_work(tb->wq, &ev->work);
69 /* enumeration & hot plug handling */
71 static void tb_add_dp_resources(struct tb_switch *sw)
73 struct tb_cm *tcm = tb_priv(sw->tb);
76 tb_switch_for_each_port(sw, port) {
77 if (!tb_port_is_dpin(port))
80 if (!tb_switch_query_dp_resource(sw, port))
83 list_add_tail(&port->list, &tcm->dp_resources);
84 tb_port_dbg(port, "DP IN resource available\n");
88 static void tb_remove_dp_resources(struct tb_switch *sw)
90 struct tb_cm *tcm = tb_priv(sw->tb);
91 struct tb_port *port, *tmp;
93 /* Clear children resources first */
94 tb_switch_for_each_port(sw, port) {
95 if (tb_port_has_remote(port))
96 tb_remove_dp_resources(port->remote->sw);
99 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
100 if (port->sw == sw) {
101 tb_port_dbg(port, "DP OUT resource unavailable\n");
102 list_del_init(&port->list);
107 static void tb_discover_tunnels(struct tb_switch *sw)
109 struct tb *tb = sw->tb;
110 struct tb_cm *tcm = tb_priv(tb);
111 struct tb_port *port;
113 tb_switch_for_each_port(sw, port) {
114 struct tb_tunnel *tunnel = NULL;
116 switch (port->config.type) {
117 case TB_TYPE_DP_HDMI_IN:
118 tunnel = tb_tunnel_discover_dp(tb, port);
121 case TB_TYPE_PCIE_DOWN:
122 tunnel = tb_tunnel_discover_pci(tb, port);
125 case TB_TYPE_USB3_DOWN:
126 tunnel = tb_tunnel_discover_usb3(tb, port);
136 if (tb_tunnel_is_pci(tunnel)) {
137 struct tb_switch *parent = tunnel->dst_port->sw;
139 while (parent != tunnel->src_port->sw) {
141 parent = tb_switch_parent(parent);
145 list_add_tail(&tunnel->list, &tcm->tunnel_list);
148 tb_switch_for_each_port(sw, port) {
149 if (tb_port_has_remote(port))
150 tb_discover_tunnels(port->remote->sw);
154 static int tb_port_configure_xdomain(struct tb_port *port)
157 * XDomain paths currently only support single lane so we must
158 * disable the other lane according to USB4 spec.
160 tb_port_disable(port->dual_link_port);
162 if (tb_switch_is_usb4(port->sw))
163 return usb4_port_configure_xdomain(port);
164 return tb_lc_configure_xdomain(port);
167 static void tb_port_unconfigure_xdomain(struct tb_port *port)
169 if (tb_switch_is_usb4(port->sw))
170 usb4_port_unconfigure_xdomain(port);
172 tb_lc_unconfigure_xdomain(port);
174 tb_port_enable(port->dual_link_port);
177 static void tb_scan_xdomain(struct tb_port *port)
179 struct tb_switch *sw = port->sw;
180 struct tb *tb = sw->tb;
181 struct tb_xdomain *xd;
184 if (!tb_is_xdomain_enabled())
187 route = tb_downstream_route(port);
188 xd = tb_xdomain_find_by_route(tb, route);
194 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
197 tb_port_at(route, sw)->xdomain = xd;
198 tb_port_configure_xdomain(port);
203 static int tb_enable_tmu(struct tb_switch *sw)
207 /* If it is already enabled in correct mode, don't touch it */
208 if (tb_switch_tmu_is_enabled(sw))
211 ret = tb_switch_tmu_disable(sw);
215 ret = tb_switch_tmu_post_time(sw);
219 return tb_switch_tmu_enable(sw);
223 * tb_find_unused_port() - return the first inactive port on @sw
224 * @sw: Switch to find the port on
225 * @type: Port type to look for
227 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
228 enum tb_port_type type)
230 struct tb_port *port;
232 tb_switch_for_each_port(sw, port) {
233 if (tb_is_upstream_port(port))
235 if (port->config.type != type)
239 if (tb_port_is_enabled(port))
246 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
247 const struct tb_port *port)
249 struct tb_port *down;
251 down = usb4_switch_map_usb3_down(sw, port);
252 if (down && !tb_usb3_port_is_enabled(down))
257 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
258 struct tb_port *src_port,
259 struct tb_port *dst_port)
261 struct tb_cm *tcm = tb_priv(tb);
262 struct tb_tunnel *tunnel;
264 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
265 if (tunnel->type == type &&
266 ((src_port && src_port == tunnel->src_port) ||
267 (dst_port && dst_port == tunnel->dst_port))) {
275 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
276 struct tb_port *src_port,
277 struct tb_port *dst_port)
279 struct tb_port *port, *usb3_down;
280 struct tb_switch *sw;
282 /* Pick the router that is deepest in the topology */
283 if (dst_port->sw->config.depth > src_port->sw->config.depth)
288 /* Can't be the host router */
289 if (sw == tb->root_switch)
292 /* Find the downstream USB4 port that leads to this router */
293 port = tb_port_at(tb_route(sw), tb->root_switch);
294 /* Find the corresponding host router USB3 downstream port */
295 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
299 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
302 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
303 struct tb_port *dst_port, int *available_up, int *available_down)
305 int usb3_consumed_up, usb3_consumed_down, ret;
306 struct tb_cm *tcm = tb_priv(tb);
307 struct tb_tunnel *tunnel;
308 struct tb_port *port;
310 tb_port_dbg(dst_port, "calculating available bandwidth\n");
312 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
314 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
315 &usb3_consumed_down);
319 usb3_consumed_up = 0;
320 usb3_consumed_down = 0;
323 *available_up = *available_down = 40000;
325 /* Find the minimum available bandwidth over all links */
326 tb_for_each_port_on_path(src_port, dst_port, port) {
327 int link_speed, link_width, up_bw, down_bw;
329 if (!tb_port_is_null(port))
332 if (tb_is_upstream_port(port)) {
333 link_speed = port->sw->link_speed;
335 link_speed = tb_port_get_link_speed(port);
340 link_width = port->bonded ? 2 : 1;
342 up_bw = link_speed * link_width * 1000; /* Mb/s */
343 /* Leave 10% guard band */
347 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
350 * Find all DP tunnels that cross the port and reduce
351 * their consumed bandwidth from the available.
353 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
354 int dp_consumed_up, dp_consumed_down;
356 if (!tb_tunnel_is_dp(tunnel))
359 if (!tb_tunnel_port_on_path(tunnel, port))
362 ret = tb_tunnel_consumed_bandwidth(tunnel,
368 up_bw -= dp_consumed_up;
369 down_bw -= dp_consumed_down;
373 * If USB3 is tunneled from the host router down to the
374 * branch leading to port we need to take USB3 consumed
375 * bandwidth into account regardless whether it actually
378 up_bw -= usb3_consumed_up;
379 down_bw -= usb3_consumed_down;
381 if (up_bw < *available_up)
382 *available_up = up_bw;
383 if (down_bw < *available_down)
384 *available_down = down_bw;
387 if (*available_up < 0)
389 if (*available_down < 0)
395 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
396 struct tb_port *src_port,
397 struct tb_port *dst_port)
399 struct tb_tunnel *tunnel;
401 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
402 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
405 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
406 struct tb_port *dst_port)
408 int ret, available_up, available_down;
409 struct tb_tunnel *tunnel;
411 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
415 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
418 * Calculate available bandwidth for the first hop USB3 tunnel.
419 * That determines the whole USB3 bandwidth for this branch.
421 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
422 &available_up, &available_down);
424 tb_warn(tb, "failed to calculate available bandwidth\n");
428 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
429 available_up, available_down);
431 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
434 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
436 struct tb_switch *parent = tb_switch_parent(sw);
437 int ret, available_up, available_down;
438 struct tb_port *up, *down, *port;
439 struct tb_cm *tcm = tb_priv(tb);
440 struct tb_tunnel *tunnel;
442 if (!tb_acpi_may_tunnel_usb3()) {
443 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
447 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
455 * Look up available down port. Since we are chaining it should
456 * be found right above this switch.
458 port = tb_port_at(tb_route(sw), parent);
459 down = tb_find_usb3_down(parent, port);
463 if (tb_route(parent)) {
464 struct tb_port *parent_up;
466 * Check first that the parent switch has its upstream USB3
467 * port enabled. Otherwise the chain is not complete and
468 * there is no point setting up a new tunnel.
470 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
471 if (!parent_up || !tb_port_is_enabled(parent_up))
474 /* Make all unused bandwidth available for the new tunnel */
475 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
480 ret = tb_available_bandwidth(tb, down, up, &available_up,
485 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
486 available_up, available_down);
488 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
495 if (tb_tunnel_activate(tunnel)) {
497 "USB3 tunnel activation failed, aborting\n");
502 list_add_tail(&tunnel->list, &tcm->tunnel_list);
503 if (tb_route(parent))
504 tb_reclaim_usb3_bandwidth(tb, down, up);
509 tb_tunnel_free(tunnel);
511 if (tb_route(parent))
512 tb_reclaim_usb3_bandwidth(tb, down, up);
517 static int tb_create_usb3_tunnels(struct tb_switch *sw)
519 struct tb_port *port;
522 if (!tb_acpi_may_tunnel_usb3())
526 ret = tb_tunnel_usb3(sw->tb, sw);
531 tb_switch_for_each_port(sw, port) {
532 if (!tb_port_has_remote(port))
534 ret = tb_create_usb3_tunnels(port->remote->sw);
542 static void tb_scan_port(struct tb_port *port);
545 * tb_scan_switch() - scan for and initialize downstream switches
547 static void tb_scan_switch(struct tb_switch *sw)
549 struct tb_port *port;
551 pm_runtime_get_sync(&sw->dev);
553 tb_switch_for_each_port(sw, port)
556 pm_runtime_mark_last_busy(&sw->dev);
557 pm_runtime_put_autosuspend(&sw->dev);
561 * tb_scan_port() - check for and initialize switches below port
563 static void tb_scan_port(struct tb_port *port)
565 struct tb_cm *tcm = tb_priv(port->sw->tb);
566 struct tb_port *upstream_port;
567 struct tb_switch *sw;
569 if (tb_is_upstream_port(port))
572 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
573 !tb_dp_port_is_enabled(port)) {
574 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
575 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
580 if (port->config.type != TB_TYPE_PORT)
582 if (port->dual_link_port && port->link_nr)
584 * Downstream switch is reachable through two ports.
585 * Only scan on the primary port (link_nr == 0).
587 if (tb_wait_for_port(port, false) <= 0)
590 tb_port_dbg(port, "port already has a remote\n");
594 tb_retimer_scan(port);
596 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
597 tb_downstream_route(port));
600 * If there is an error accessing the connected switch
601 * it may be connected to another domain. Also we allow
602 * the other domain to be connected to a max depth switch.
604 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
605 tb_scan_xdomain(port);
609 if (tb_switch_configure(sw)) {
615 * If there was previously another domain connected remove it
619 tb_xdomain_remove(port->xdomain);
620 tb_port_unconfigure_xdomain(port);
621 port->xdomain = NULL;
625 * Do not send uevents until we have discovered all existing
626 * tunnels and know which switches were authorized already by
629 if (!tcm->hotplug_active)
630 dev_set_uevent_suppress(&sw->dev, true);
633 * At the moment Thunderbolt 2 and beyond (devices with LC) we
634 * can support runtime PM.
636 sw->rpm = sw->generation > 1;
638 if (tb_switch_add(sw)) {
643 /* Link the switches using both links if available */
644 upstream_port = tb_upstream_port(sw);
645 port->remote = upstream_port;
646 upstream_port->remote = port;
647 if (port->dual_link_port && upstream_port->dual_link_port) {
648 port->dual_link_port->remote = upstream_port->dual_link_port;
649 upstream_port->dual_link_port->remote = port->dual_link_port;
652 /* Enable lane bonding if supported */
653 tb_switch_lane_bonding_enable(sw);
654 /* Set the link configured */
655 tb_switch_configure_link(sw);
657 if (tb_enable_tmu(sw))
658 tb_sw_warn(sw, "failed to enable TMU\n");
660 /* Scan upstream retimers */
661 tb_retimer_scan(upstream_port);
664 * Create USB 3.x tunnels only when the switch is plugged to the
665 * domain. This is because we scan the domain also during discovery
666 * and want to discover existing USB 3.x tunnels before we create
669 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
670 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
672 tb_add_dp_resources(sw);
676 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
678 struct tb_port *src_port, *dst_port;
684 tb_tunnel_deactivate(tunnel);
685 list_del(&tunnel->list);
688 src_port = tunnel->src_port;
689 dst_port = tunnel->dst_port;
691 switch (tunnel->type) {
694 * In case of DP tunnel make sure the DP IN resource is
695 * deallocated properly.
697 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
698 /* Now we can allow the domain to runtime suspend again */
699 pm_runtime_mark_last_busy(&dst_port->sw->dev);
700 pm_runtime_put_autosuspend(&dst_port->sw->dev);
701 pm_runtime_mark_last_busy(&src_port->sw->dev);
702 pm_runtime_put_autosuspend(&src_port->sw->dev);
706 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
711 * PCIe and DMA tunnels do not consume guaranteed
717 tb_tunnel_free(tunnel);
721 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
723 static void tb_free_invalid_tunnels(struct tb *tb)
725 struct tb_cm *tcm = tb_priv(tb);
726 struct tb_tunnel *tunnel;
729 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
730 if (tb_tunnel_is_invalid(tunnel))
731 tb_deactivate_and_free_tunnel(tunnel);
736 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
738 static void tb_free_unplugged_children(struct tb_switch *sw)
740 struct tb_port *port;
742 tb_switch_for_each_port(sw, port) {
743 if (!tb_port_has_remote(port))
746 if (port->remote->sw->is_unplugged) {
747 tb_retimer_remove_all(port);
748 tb_remove_dp_resources(port->remote->sw);
749 tb_switch_unconfigure_link(port->remote->sw);
750 tb_switch_lane_bonding_disable(port->remote->sw);
751 tb_switch_remove(port->remote->sw);
753 if (port->dual_link_port)
754 port->dual_link_port->remote = NULL;
756 tb_free_unplugged_children(port->remote->sw);
761 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
762 const struct tb_port *port)
764 struct tb_port *down = NULL;
767 * To keep plugging devices consistently in the same PCIe
768 * hierarchy, do mapping here for switch downstream PCIe ports.
770 if (tb_switch_is_usb4(sw)) {
771 down = usb4_switch_map_pcie_down(sw, port);
772 } else if (!tb_route(sw)) {
773 int phy_port = tb_phy_port_from_link(port->port);
777 * Hard-coded Thunderbolt port to PCIe down port mapping
780 if (tb_switch_is_cactus_ridge(sw) ||
781 tb_switch_is_alpine_ridge(sw))
782 index = !phy_port ? 6 : 7;
783 else if (tb_switch_is_falcon_ridge(sw))
784 index = !phy_port ? 6 : 8;
785 else if (tb_switch_is_titan_ridge(sw))
786 index = !phy_port ? 8 : 9;
790 /* Validate the hard-coding */
791 if (WARN_ON(index > sw->config.max_port_number))
794 down = &sw->ports[index];
798 if (WARN_ON(!tb_port_is_pcie_down(down)))
800 if (tb_pci_port_is_enabled(down))
807 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
810 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
812 struct tb_port *host_port, *port;
813 struct tb_cm *tcm = tb_priv(tb);
815 host_port = tb_route(in->sw) ?
816 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
818 list_for_each_entry(port, &tcm->dp_resources, list) {
819 if (!tb_port_is_dpout(port))
822 if (tb_port_is_enabled(port)) {
823 tb_port_dbg(port, "in use\n");
827 tb_port_dbg(port, "DP OUT available\n");
830 * Keep the DP tunnel under the topology starting from
831 * the same host router downstream port.
833 if (host_port && tb_route(port->sw)) {
836 p = tb_port_at(tb_route(port->sw), tb->root_switch);
847 static void tb_tunnel_dp(struct tb *tb)
849 int available_up, available_down, ret;
850 struct tb_cm *tcm = tb_priv(tb);
851 struct tb_port *port, *in, *out;
852 struct tb_tunnel *tunnel;
854 if (!tb_acpi_may_tunnel_dp()) {
855 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
860 * Find pair of inactive DP IN and DP OUT adapters and then
861 * establish a DP tunnel between them.
863 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
867 list_for_each_entry(port, &tcm->dp_resources, list) {
868 if (!tb_port_is_dpin(port))
871 if (tb_port_is_enabled(port)) {
872 tb_port_dbg(port, "in use\n");
876 tb_port_dbg(port, "DP IN available\n");
878 out = tb_find_dp_out(tb, port);
886 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
890 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
895 * DP stream needs the domain to be active so runtime resume
896 * both ends of the tunnel.
898 * This should bring the routers in the middle active as well
899 * and keeps the domain from runtime suspending while the DP
902 pm_runtime_get_sync(&in->sw->dev);
903 pm_runtime_get_sync(&out->sw->dev);
905 if (tb_switch_alloc_dp_resource(in->sw, in)) {
906 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
910 /* Make all unused USB3 bandwidth available for the new DP tunnel */
911 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
913 tb_warn(tb, "failed to release unused bandwidth\n");
917 ret = tb_available_bandwidth(tb, in, out, &available_up,
922 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
923 available_up, available_down);
925 tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
927 tb_port_dbg(out, "could not allocate DP tunnel\n");
931 if (tb_tunnel_activate(tunnel)) {
932 tb_port_info(out, "DP tunnel activation failed, aborting\n");
936 list_add_tail(&tunnel->list, &tcm->tunnel_list);
937 tb_reclaim_usb3_bandwidth(tb, in, out);
941 tb_tunnel_free(tunnel);
943 tb_reclaim_usb3_bandwidth(tb, in, out);
945 tb_switch_dealloc_dp_resource(in->sw, in);
947 pm_runtime_mark_last_busy(&out->sw->dev);
948 pm_runtime_put_autosuspend(&out->sw->dev);
949 pm_runtime_mark_last_busy(&in->sw->dev);
950 pm_runtime_put_autosuspend(&in->sw->dev);
953 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
955 struct tb_port *in, *out;
956 struct tb_tunnel *tunnel;
958 if (tb_port_is_dpin(port)) {
959 tb_port_dbg(port, "DP IN resource unavailable\n");
963 tb_port_dbg(port, "DP OUT resource unavailable\n");
968 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
969 tb_deactivate_and_free_tunnel(tunnel);
970 list_del_init(&port->list);
973 * See if there is another DP OUT port that can be used for
974 * to create another tunnel.
979 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
981 struct tb_cm *tcm = tb_priv(tb);
984 if (tb_port_is_enabled(port))
987 list_for_each_entry(p, &tcm->dp_resources, list) {
992 tb_port_dbg(port, "DP %s resource available\n",
993 tb_port_is_dpin(port) ? "IN" : "OUT");
994 list_add_tail(&port->list, &tcm->dp_resources);
996 /* Look for suitable DP IN <-> DP OUT pairs now */
1000 static void tb_disconnect_and_release_dp(struct tb *tb)
1002 struct tb_cm *tcm = tb_priv(tb);
1003 struct tb_tunnel *tunnel, *n;
1006 * Tear down all DP tunnels and release their resources. They
1007 * will be re-established after resume based on plug events.
1009 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1010 if (tb_tunnel_is_dp(tunnel))
1011 tb_deactivate_and_free_tunnel(tunnel);
1014 while (!list_empty(&tcm->dp_resources)) {
1015 struct tb_port *port;
1017 port = list_first_entry(&tcm->dp_resources,
1018 struct tb_port, list);
1019 list_del_init(&port->list);
1023 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1025 struct tb_tunnel *tunnel;
1028 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1032 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1033 if (WARN_ON(!tunnel))
1036 tb_tunnel_deactivate(tunnel);
1037 list_del(&tunnel->list);
1038 tb_tunnel_free(tunnel);
1042 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1044 struct tb_port *up, *down, *port;
1045 struct tb_cm *tcm = tb_priv(tb);
1046 struct tb_switch *parent_sw;
1047 struct tb_tunnel *tunnel;
1049 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1054 * Look up available down port. Since we are chaining it should
1055 * be found right above this switch.
1057 parent_sw = tb_to_switch(sw->dev.parent);
1058 port = tb_port_at(tb_route(sw), parent_sw);
1059 down = tb_find_pcie_down(parent_sw, port);
1063 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1067 if (tb_tunnel_activate(tunnel)) {
1069 "PCIe tunnel activation failed, aborting\n");
1070 tb_tunnel_free(tunnel);
1074 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1078 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1080 struct tb_cm *tcm = tb_priv(tb);
1081 struct tb_port *nhi_port, *dst_port;
1082 struct tb_tunnel *tunnel;
1083 struct tb_switch *sw;
1085 sw = tb_to_switch(xd->dev.parent);
1086 dst_port = tb_port_at(xd->route, sw);
1087 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1089 mutex_lock(&tb->lock);
1090 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
1091 xd->transmit_path, xd->receive_ring,
1094 mutex_unlock(&tb->lock);
1098 if (tb_tunnel_activate(tunnel)) {
1099 tb_port_info(nhi_port,
1100 "DMA tunnel activation failed, aborting\n");
1101 tb_tunnel_free(tunnel);
1102 mutex_unlock(&tb->lock);
1106 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1107 mutex_unlock(&tb->lock);
1111 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1113 struct tb_port *dst_port;
1114 struct tb_tunnel *tunnel;
1115 struct tb_switch *sw;
1117 sw = tb_to_switch(xd->dev.parent);
1118 dst_port = tb_port_at(xd->route, sw);
1121 * It is possible that the tunnel was already teared down (in
1122 * case of cable disconnect) so it is fine if we cannot find it
1125 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
1126 tb_deactivate_and_free_tunnel(tunnel);
1129 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1131 if (!xd->is_unplugged) {
1132 mutex_lock(&tb->lock);
1133 __tb_disconnect_xdomain_paths(tb, xd);
1134 mutex_unlock(&tb->lock);
1139 /* hotplug handling */
1142 * tb_handle_hotplug() - handle hotplug event
1144 * Executes on tb->wq.
1146 static void tb_handle_hotplug(struct work_struct *work)
1148 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1149 struct tb *tb = ev->tb;
1150 struct tb_cm *tcm = tb_priv(tb);
1151 struct tb_switch *sw;
1152 struct tb_port *port;
1154 /* Bring the domain back from sleep if it was suspended */
1155 pm_runtime_get_sync(&tb->dev);
1157 mutex_lock(&tb->lock);
1158 if (!tcm->hotplug_active)
1159 goto out; /* during init, suspend or shutdown */
1161 sw = tb_switch_find_by_route(tb, ev->route);
1164 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1165 ev->route, ev->port, ev->unplug);
1168 if (ev->port > sw->config.max_port_number) {
1170 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1171 ev->route, ev->port, ev->unplug);
1174 port = &sw->ports[ev->port];
1175 if (tb_is_upstream_port(port)) {
1176 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1177 ev->route, ev->port, ev->unplug);
1181 pm_runtime_get_sync(&sw->dev);
1184 tb_retimer_remove_all(port);
1186 if (tb_port_has_remote(port)) {
1187 tb_port_dbg(port, "switch unplugged\n");
1188 tb_sw_set_unplugged(port->remote->sw);
1189 tb_free_invalid_tunnels(tb);
1190 tb_remove_dp_resources(port->remote->sw);
1191 tb_switch_tmu_disable(port->remote->sw);
1192 tb_switch_unconfigure_link(port->remote->sw);
1193 tb_switch_lane_bonding_disable(port->remote->sw);
1194 tb_switch_remove(port->remote->sw);
1195 port->remote = NULL;
1196 if (port->dual_link_port)
1197 port->dual_link_port->remote = NULL;
1198 /* Maybe we can create another DP tunnel */
1200 } else if (port->xdomain) {
1201 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1203 tb_port_dbg(port, "xdomain unplugged\n");
1205 * Service drivers are unbound during
1206 * tb_xdomain_remove() so setting XDomain as
1207 * unplugged here prevents deadlock if they call
1208 * tb_xdomain_disable_paths(). We will tear down
1211 xd->is_unplugged = true;
1212 tb_xdomain_remove(xd);
1213 port->xdomain = NULL;
1214 __tb_disconnect_xdomain_paths(tb, xd);
1216 tb_port_unconfigure_xdomain(port);
1217 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1218 tb_dp_resource_unavailable(tb, port);
1221 "got unplug event for disconnected port, ignoring\n");
1223 } else if (port->remote) {
1224 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1226 if (tb_port_is_null(port)) {
1227 tb_port_dbg(port, "hotplug: scanning\n");
1230 tb_port_dbg(port, "hotplug: no switch found\n");
1231 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1232 tb_dp_resource_available(tb, port);
1236 pm_runtime_mark_last_busy(&sw->dev);
1237 pm_runtime_put_autosuspend(&sw->dev);
1242 mutex_unlock(&tb->lock);
1244 pm_runtime_mark_last_busy(&tb->dev);
1245 pm_runtime_put_autosuspend(&tb->dev);
1251 * tb_schedule_hotplug_handler() - callback function for the control channel
1253 * Delegates to tb_handle_hotplug.
1255 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1256 const void *buf, size_t size)
1258 const struct cfg_event_pkg *pkg = buf;
1261 if (type != TB_CFG_PKG_EVENT) {
1262 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1266 route = tb_cfg_get_route(&pkg->header);
1268 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1269 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1273 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1276 static void tb_stop(struct tb *tb)
1278 struct tb_cm *tcm = tb_priv(tb);
1279 struct tb_tunnel *tunnel;
1280 struct tb_tunnel *n;
1282 cancel_delayed_work(&tcm->remove_work);
1283 /* tunnels are only present after everything has been initialized */
1284 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1286 * DMA tunnels require the driver to be functional so we
1287 * tear them down. Other protocol tunnels can be left
1290 if (tb_tunnel_is_dma(tunnel))
1291 tb_tunnel_deactivate(tunnel);
1292 tb_tunnel_free(tunnel);
1294 tb_switch_remove(tb->root_switch);
1295 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1298 static int tb_scan_finalize_switch(struct device *dev, void *data)
1300 if (tb_is_switch(dev)) {
1301 struct tb_switch *sw = tb_to_switch(dev);
1304 * If we found that the switch was already setup by the
1305 * boot firmware, mark it as authorized now before we
1306 * send uevent to userspace.
1311 dev_set_uevent_suppress(dev, false);
1312 kobject_uevent(&dev->kobj, KOBJ_ADD);
1313 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1319 static int tb_start(struct tb *tb)
1321 struct tb_cm *tcm = tb_priv(tb);
1324 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1325 if (IS_ERR(tb->root_switch))
1326 return PTR_ERR(tb->root_switch);
1329 * ICM firmware upgrade needs running firmware and in native
1330 * mode that is not available so disable firmware upgrade of the
1333 tb->root_switch->no_nvm_upgrade = true;
1334 /* All USB4 routers support runtime PM */
1335 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1337 ret = tb_switch_configure(tb->root_switch);
1339 tb_switch_put(tb->root_switch);
1343 /* Announce the switch to the world */
1344 ret = tb_switch_add(tb->root_switch);
1346 tb_switch_put(tb->root_switch);
1350 /* Enable TMU if it is off */
1351 tb_switch_tmu_enable(tb->root_switch);
1352 /* Full scan to discover devices added before the driver was loaded. */
1353 tb_scan_switch(tb->root_switch);
1354 /* Find out tunnels created by the boot firmware */
1355 tb_discover_tunnels(tb->root_switch);
1357 * If the boot firmware did not create USB 3.x tunnels create them
1358 * now for the whole topology.
1360 tb_create_usb3_tunnels(tb->root_switch);
1361 /* Add DP IN resources for the root switch */
1362 tb_add_dp_resources(tb->root_switch);
1363 /* Make the discovered switches available to the userspace */
1364 device_for_each_child(&tb->root_switch->dev, NULL,
1365 tb_scan_finalize_switch);
1367 /* Allow tb_handle_hotplug to progress events */
1368 tcm->hotplug_active = true;
1372 static int tb_suspend_noirq(struct tb *tb)
1374 struct tb_cm *tcm = tb_priv(tb);
1376 tb_dbg(tb, "suspending...\n");
1377 tb_disconnect_and_release_dp(tb);
1378 tb_switch_suspend(tb->root_switch, false);
1379 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1380 tb_dbg(tb, "suspend finished\n");
1385 static void tb_restore_children(struct tb_switch *sw)
1387 struct tb_port *port;
1389 /* No need to restore if the router is already unplugged */
1390 if (sw->is_unplugged)
1393 if (tb_enable_tmu(sw))
1394 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1396 tb_switch_for_each_port(sw, port) {
1397 if (!tb_port_has_remote(port) && !port->xdomain)
1401 tb_switch_lane_bonding_enable(port->remote->sw);
1402 tb_switch_configure_link(port->remote->sw);
1404 tb_restore_children(port->remote->sw);
1405 } else if (port->xdomain) {
1406 tb_port_configure_xdomain(port);
1411 static int tb_resume_noirq(struct tb *tb)
1413 struct tb_cm *tcm = tb_priv(tb);
1414 struct tb_tunnel *tunnel, *n;
1416 tb_dbg(tb, "resuming...\n");
1418 /* remove any pci devices the firmware might have setup */
1419 tb_switch_reset(tb->root_switch);
1421 tb_switch_resume(tb->root_switch);
1422 tb_free_invalid_tunnels(tb);
1423 tb_free_unplugged_children(tb->root_switch);
1424 tb_restore_children(tb->root_switch);
1425 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1426 tb_tunnel_restart(tunnel);
1427 if (!list_empty(&tcm->tunnel_list)) {
1429 * the pcie links need some time to get going.
1430 * 100ms works for me...
1432 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1435 /* Allow tb_handle_hotplug to progress events */
1436 tcm->hotplug_active = true;
1437 tb_dbg(tb, "resume finished\n");
1442 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1444 struct tb_port *port;
1447 tb_switch_for_each_port(sw, port) {
1448 if (tb_is_upstream_port(port))
1450 if (port->xdomain && port->xdomain->is_unplugged) {
1451 tb_retimer_remove_all(port);
1452 tb_xdomain_remove(port->xdomain);
1453 tb_port_unconfigure_xdomain(port);
1454 port->xdomain = NULL;
1456 } else if (port->remote) {
1457 ret += tb_free_unplugged_xdomains(port->remote->sw);
1464 static int tb_freeze_noirq(struct tb *tb)
1466 struct tb_cm *tcm = tb_priv(tb);
1468 tcm->hotplug_active = false;
1472 static int tb_thaw_noirq(struct tb *tb)
1474 struct tb_cm *tcm = tb_priv(tb);
1476 tcm->hotplug_active = true;
1480 static void tb_complete(struct tb *tb)
1483 * Release any unplugged XDomains and if there is a case where
1484 * another domain is swapped in place of unplugged XDomain we
1485 * need to run another rescan.
1487 mutex_lock(&tb->lock);
1488 if (tb_free_unplugged_xdomains(tb->root_switch))
1489 tb_scan_switch(tb->root_switch);
1490 mutex_unlock(&tb->lock);
1493 static int tb_runtime_suspend(struct tb *tb)
1495 struct tb_cm *tcm = tb_priv(tb);
1497 mutex_lock(&tb->lock);
1498 tb_switch_suspend(tb->root_switch, true);
1499 tcm->hotplug_active = false;
1500 mutex_unlock(&tb->lock);
1505 static void tb_remove_work(struct work_struct *work)
1507 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1508 struct tb *tb = tcm_to_tb(tcm);
1510 mutex_lock(&tb->lock);
1511 if (tb->root_switch) {
1512 tb_free_unplugged_children(tb->root_switch);
1513 tb_free_unplugged_xdomains(tb->root_switch);
1515 mutex_unlock(&tb->lock);
1518 static int tb_runtime_resume(struct tb *tb)
1520 struct tb_cm *tcm = tb_priv(tb);
1521 struct tb_tunnel *tunnel, *n;
1523 mutex_lock(&tb->lock);
1524 tb_switch_resume(tb->root_switch);
1525 tb_free_invalid_tunnels(tb);
1526 tb_restore_children(tb->root_switch);
1527 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1528 tb_tunnel_restart(tunnel);
1529 tcm->hotplug_active = true;
1530 mutex_unlock(&tb->lock);
1533 * Schedule cleanup of any unplugged devices. Run this in a
1534 * separate thread to avoid possible deadlock if the device
1535 * removal runtime resumes the unplugged device.
1537 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1541 static const struct tb_cm_ops tb_cm_ops = {
1544 .suspend_noirq = tb_suspend_noirq,
1545 .resume_noirq = tb_resume_noirq,
1546 .freeze_noirq = tb_freeze_noirq,
1547 .thaw_noirq = tb_thaw_noirq,
1548 .complete = tb_complete,
1549 .runtime_suspend = tb_runtime_suspend,
1550 .runtime_resume = tb_runtime_resume,
1551 .handle_event = tb_handle_event,
1552 .disapprove_switch = tb_disconnect_pci,
1553 .approve_switch = tb_tunnel_pci,
1554 .approve_xdomain_paths = tb_approve_xdomain_paths,
1555 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1558 struct tb *tb_probe(struct tb_nhi *nhi)
1563 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
1567 if (tb_acpi_may_tunnel_pcie())
1568 tb->security_level = TB_SECURITY_USER;
1570 tb->security_level = TB_SECURITY_NOPCIE;
1572 tb->cm_ops = &tb_cm_ops;
1575 INIT_LIST_HEAD(&tcm->tunnel_list);
1576 INIT_LIST_HEAD(&tcm->dp_resources);
1577 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1579 tb_dbg(tb, "using software connection manager\n");