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>
19 * struct tb_cm - Simple Thunderbolt connection manager
20 * @tunnel_list: List of active tunnels
21 * @dp_resources: List of available DP resources for DP tunneling
22 * @hotplug_active: tb_handle_hotplug will stop progressing plug
23 * events and exit if this is not set (it needs to
24 * acquire the lock one more time). Used to drain wq
25 * after cfg has been paused.
26 * @remove_work: Work used to remove any unplugged routers after
30 struct list_head tunnel_list;
31 struct list_head dp_resources;
33 struct delayed_work remove_work;
36 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
38 return ((void *)tcm - sizeof(struct tb));
41 struct tb_hotplug_event {
42 struct work_struct work;
49 static void tb_handle_hotplug(struct work_struct *work);
51 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
53 struct tb_hotplug_event *ev;
55 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
63 INIT_WORK(&ev->work, tb_handle_hotplug);
64 queue_work(tb->wq, &ev->work);
67 /* enumeration & hot plug handling */
69 static void tb_add_dp_resources(struct tb_switch *sw)
71 struct tb_cm *tcm = tb_priv(sw->tb);
74 tb_switch_for_each_port(sw, port) {
75 if (!tb_port_is_dpin(port))
78 if (!tb_switch_query_dp_resource(sw, port))
81 list_add_tail(&port->list, &tcm->dp_resources);
82 tb_port_dbg(port, "DP IN resource available\n");
86 static void tb_remove_dp_resources(struct tb_switch *sw)
88 struct tb_cm *tcm = tb_priv(sw->tb);
89 struct tb_port *port, *tmp;
91 /* Clear children resources first */
92 tb_switch_for_each_port(sw, port) {
93 if (tb_port_has_remote(port))
94 tb_remove_dp_resources(port->remote->sw);
97 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
99 tb_port_dbg(port, "DP OUT resource unavailable\n");
100 list_del_init(&port->list);
105 static void tb_discover_tunnels(struct tb_switch *sw)
107 struct tb *tb = sw->tb;
108 struct tb_cm *tcm = tb_priv(tb);
109 struct tb_port *port;
111 tb_switch_for_each_port(sw, port) {
112 struct tb_tunnel *tunnel = NULL;
114 switch (port->config.type) {
115 case TB_TYPE_DP_HDMI_IN:
116 tunnel = tb_tunnel_discover_dp(tb, port);
119 case TB_TYPE_PCIE_DOWN:
120 tunnel = tb_tunnel_discover_pci(tb, port);
123 case TB_TYPE_USB3_DOWN:
124 tunnel = tb_tunnel_discover_usb3(tb, port);
134 if (tb_tunnel_is_pci(tunnel)) {
135 struct tb_switch *parent = tunnel->dst_port->sw;
137 while (parent != tunnel->src_port->sw) {
139 parent = tb_switch_parent(parent);
143 list_add_tail(&tunnel->list, &tcm->tunnel_list);
146 tb_switch_for_each_port(sw, port) {
147 if (tb_port_has_remote(port))
148 tb_discover_tunnels(port->remote->sw);
152 static int tb_port_configure_xdomain(struct tb_port *port)
155 * XDomain paths currently only support single lane so we must
156 * disable the other lane according to USB4 spec.
158 tb_port_disable(port->dual_link_port);
160 if (tb_switch_is_usb4(port->sw))
161 return usb4_port_configure_xdomain(port);
162 return tb_lc_configure_xdomain(port);
165 static void tb_port_unconfigure_xdomain(struct tb_port *port)
167 if (tb_switch_is_usb4(port->sw))
168 usb4_port_unconfigure_xdomain(port);
170 tb_lc_unconfigure_xdomain(port);
172 tb_port_enable(port->dual_link_port);
175 static void tb_scan_xdomain(struct tb_port *port)
177 struct tb_switch *sw = port->sw;
178 struct tb *tb = sw->tb;
179 struct tb_xdomain *xd;
182 if (!tb_is_xdomain_enabled())
185 route = tb_downstream_route(port);
186 xd = tb_xdomain_find_by_route(tb, route);
192 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
195 tb_port_at(route, sw)->xdomain = xd;
196 tb_port_configure_xdomain(port);
201 static int tb_enable_tmu(struct tb_switch *sw)
205 /* If it is already enabled in correct mode, don't touch it */
206 if (tb_switch_tmu_is_enabled(sw))
209 ret = tb_switch_tmu_disable(sw);
213 ret = tb_switch_tmu_post_time(sw);
217 return tb_switch_tmu_enable(sw);
221 * tb_find_unused_port() - return the first inactive port on @sw
222 * @sw: Switch to find the port on
223 * @type: Port type to look for
225 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
226 enum tb_port_type type)
228 struct tb_port *port;
230 tb_switch_for_each_port(sw, port) {
231 if (tb_is_upstream_port(port))
233 if (port->config.type != type)
237 if (tb_port_is_enabled(port))
244 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
245 const struct tb_port *port)
247 struct tb_port *down;
249 down = usb4_switch_map_usb3_down(sw, port);
250 if (down && !tb_usb3_port_is_enabled(down))
255 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
256 struct tb_port *src_port,
257 struct tb_port *dst_port)
259 struct tb_cm *tcm = tb_priv(tb);
260 struct tb_tunnel *tunnel;
262 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
263 if (tunnel->type == type &&
264 ((src_port && src_port == tunnel->src_port) ||
265 (dst_port && dst_port == tunnel->dst_port))) {
273 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
274 struct tb_port *src_port,
275 struct tb_port *dst_port)
277 struct tb_port *port, *usb3_down;
278 struct tb_switch *sw;
280 /* Pick the router that is deepest in the topology */
281 if (dst_port->sw->config.depth > src_port->sw->config.depth)
286 /* Can't be the host router */
287 if (sw == tb->root_switch)
290 /* Find the downstream USB4 port that leads to this router */
291 port = tb_port_at(tb_route(sw), tb->root_switch);
292 /* Find the corresponding host router USB3 downstream port */
293 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
297 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
300 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
301 struct tb_port *dst_port, int *available_up, int *available_down)
303 int usb3_consumed_up, usb3_consumed_down, ret;
304 struct tb_cm *tcm = tb_priv(tb);
305 struct tb_tunnel *tunnel;
306 struct tb_port *port;
308 tb_port_dbg(dst_port, "calculating available bandwidth\n");
310 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
312 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
313 &usb3_consumed_down);
317 usb3_consumed_up = 0;
318 usb3_consumed_down = 0;
321 *available_up = *available_down = 40000;
323 /* Find the minimum available bandwidth over all links */
324 tb_for_each_port_on_path(src_port, dst_port, port) {
325 int link_speed, link_width, up_bw, down_bw;
327 if (!tb_port_is_null(port))
330 if (tb_is_upstream_port(port)) {
331 link_speed = port->sw->link_speed;
333 link_speed = tb_port_get_link_speed(port);
338 link_width = port->bonded ? 2 : 1;
340 up_bw = link_speed * link_width * 1000; /* Mb/s */
341 /* Leave 10% guard band */
345 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
348 * Find all DP tunnels that cross the port and reduce
349 * their consumed bandwidth from the available.
351 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
352 int dp_consumed_up, dp_consumed_down;
354 if (!tb_tunnel_is_dp(tunnel))
357 if (!tb_tunnel_port_on_path(tunnel, port))
360 ret = tb_tunnel_consumed_bandwidth(tunnel,
366 up_bw -= dp_consumed_up;
367 down_bw -= dp_consumed_down;
371 * If USB3 is tunneled from the host router down to the
372 * branch leading to port we need to take USB3 consumed
373 * bandwidth into account regardless whether it actually
376 up_bw -= usb3_consumed_up;
377 down_bw -= usb3_consumed_down;
379 if (up_bw < *available_up)
380 *available_up = up_bw;
381 if (down_bw < *available_down)
382 *available_down = down_bw;
385 if (*available_up < 0)
387 if (*available_down < 0)
393 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
394 struct tb_port *src_port,
395 struct tb_port *dst_port)
397 struct tb_tunnel *tunnel;
399 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
400 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
403 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
404 struct tb_port *dst_port)
406 int ret, available_up, available_down;
407 struct tb_tunnel *tunnel;
409 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
413 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
416 * Calculate available bandwidth for the first hop USB3 tunnel.
417 * That determines the whole USB3 bandwidth for this branch.
419 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
420 &available_up, &available_down);
422 tb_warn(tb, "failed to calculate available bandwidth\n");
426 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
427 available_up, available_down);
429 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
432 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
434 struct tb_switch *parent = tb_switch_parent(sw);
435 int ret, available_up, available_down;
436 struct tb_port *up, *down, *port;
437 struct tb_cm *tcm = tb_priv(tb);
438 struct tb_tunnel *tunnel;
440 if (!tb_acpi_may_tunnel_usb3()) {
441 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
445 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
453 * Look up available down port. Since we are chaining it should
454 * be found right above this switch.
456 port = tb_port_at(tb_route(sw), parent);
457 down = tb_find_usb3_down(parent, port);
461 if (tb_route(parent)) {
462 struct tb_port *parent_up;
464 * Check first that the parent switch has its upstream USB3
465 * port enabled. Otherwise the chain is not complete and
466 * there is no point setting up a new tunnel.
468 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
469 if (!parent_up || !tb_port_is_enabled(parent_up))
472 /* Make all unused bandwidth available for the new tunnel */
473 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
478 ret = tb_available_bandwidth(tb, down, up, &available_up,
483 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
484 available_up, available_down);
486 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
493 if (tb_tunnel_activate(tunnel)) {
495 "USB3 tunnel activation failed, aborting\n");
500 list_add_tail(&tunnel->list, &tcm->tunnel_list);
501 if (tb_route(parent))
502 tb_reclaim_usb3_bandwidth(tb, down, up);
507 tb_tunnel_free(tunnel);
509 if (tb_route(parent))
510 tb_reclaim_usb3_bandwidth(tb, down, up);
515 static int tb_create_usb3_tunnels(struct tb_switch *sw)
517 struct tb_port *port;
520 if (!tb_acpi_may_tunnel_usb3())
524 ret = tb_tunnel_usb3(sw->tb, sw);
529 tb_switch_for_each_port(sw, port) {
530 if (!tb_port_has_remote(port))
532 ret = tb_create_usb3_tunnels(port->remote->sw);
540 static void tb_scan_port(struct tb_port *port);
543 * tb_scan_switch() - scan for and initialize downstream switches
545 static void tb_scan_switch(struct tb_switch *sw)
547 struct tb_port *port;
549 pm_runtime_get_sync(&sw->dev);
551 tb_switch_for_each_port(sw, port)
554 pm_runtime_mark_last_busy(&sw->dev);
555 pm_runtime_put_autosuspend(&sw->dev);
559 * tb_scan_port() - check for and initialize switches below port
561 static void tb_scan_port(struct tb_port *port)
563 struct tb_cm *tcm = tb_priv(port->sw->tb);
564 struct tb_port *upstream_port;
565 struct tb_switch *sw;
567 if (tb_is_upstream_port(port))
570 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
571 !tb_dp_port_is_enabled(port)) {
572 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
573 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
578 if (port->config.type != TB_TYPE_PORT)
580 if (port->dual_link_port && port->link_nr)
582 * Downstream switch is reachable through two ports.
583 * Only scan on the primary port (link_nr == 0).
585 if (tb_wait_for_port(port, false) <= 0)
588 tb_port_dbg(port, "port already has a remote\n");
592 tb_retimer_scan(port);
594 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
595 tb_downstream_route(port));
598 * If there is an error accessing the connected switch
599 * it may be connected to another domain. Also we allow
600 * the other domain to be connected to a max depth switch.
602 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
603 tb_scan_xdomain(port);
607 if (tb_switch_configure(sw)) {
613 * If there was previously another domain connected remove it
617 tb_xdomain_remove(port->xdomain);
618 tb_port_unconfigure_xdomain(port);
619 port->xdomain = NULL;
623 * Do not send uevents until we have discovered all existing
624 * tunnels and know which switches were authorized already by
627 if (!tcm->hotplug_active)
628 dev_set_uevent_suppress(&sw->dev, true);
631 * At the moment Thunderbolt 2 and beyond (devices with LC) we
632 * can support runtime PM.
634 sw->rpm = sw->generation > 1;
636 if (tb_switch_add(sw)) {
641 /* Link the switches using both links if available */
642 upstream_port = tb_upstream_port(sw);
643 port->remote = upstream_port;
644 upstream_port->remote = port;
645 if (port->dual_link_port && upstream_port->dual_link_port) {
646 port->dual_link_port->remote = upstream_port->dual_link_port;
647 upstream_port->dual_link_port->remote = port->dual_link_port;
650 /* Enable lane bonding if supported */
651 tb_switch_lane_bonding_enable(sw);
652 /* Set the link configured */
653 tb_switch_configure_link(sw);
655 if (tb_enable_tmu(sw))
656 tb_sw_warn(sw, "failed to enable TMU\n");
658 /* Scan upstream retimers */
659 tb_retimer_scan(upstream_port);
662 * Create USB 3.x tunnels only when the switch is plugged to the
663 * domain. This is because we scan the domain also during discovery
664 * and want to discover existing USB 3.x tunnels before we create
667 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
668 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
670 tb_add_dp_resources(sw);
674 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
676 struct tb_port *src_port, *dst_port;
682 tb_tunnel_deactivate(tunnel);
683 list_del(&tunnel->list);
686 src_port = tunnel->src_port;
687 dst_port = tunnel->dst_port;
689 switch (tunnel->type) {
692 * In case of DP tunnel make sure the DP IN resource is
693 * deallocated properly.
695 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
696 /* Now we can allow the domain to runtime suspend again */
697 pm_runtime_mark_last_busy(&dst_port->sw->dev);
698 pm_runtime_put_autosuspend(&dst_port->sw->dev);
699 pm_runtime_mark_last_busy(&src_port->sw->dev);
700 pm_runtime_put_autosuspend(&src_port->sw->dev);
704 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
709 * PCIe and DMA tunnels do not consume guaranteed
715 tb_tunnel_free(tunnel);
719 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
721 static void tb_free_invalid_tunnels(struct tb *tb)
723 struct tb_cm *tcm = tb_priv(tb);
724 struct tb_tunnel *tunnel;
727 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
728 if (tb_tunnel_is_invalid(tunnel))
729 tb_deactivate_and_free_tunnel(tunnel);
734 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
736 static void tb_free_unplugged_children(struct tb_switch *sw)
738 struct tb_port *port;
740 tb_switch_for_each_port(sw, port) {
741 if (!tb_port_has_remote(port))
744 if (port->remote->sw->is_unplugged) {
745 tb_retimer_remove_all(port);
746 tb_remove_dp_resources(port->remote->sw);
747 tb_switch_unconfigure_link(port->remote->sw);
748 tb_switch_lane_bonding_disable(port->remote->sw);
749 tb_switch_remove(port->remote->sw);
751 if (port->dual_link_port)
752 port->dual_link_port->remote = NULL;
754 tb_free_unplugged_children(port->remote->sw);
759 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
760 const struct tb_port *port)
762 struct tb_port *down = NULL;
765 * To keep plugging devices consistently in the same PCIe
766 * hierarchy, do mapping here for switch downstream PCIe ports.
768 if (tb_switch_is_usb4(sw)) {
769 down = usb4_switch_map_pcie_down(sw, port);
770 } else if (!tb_route(sw)) {
771 int phy_port = tb_phy_port_from_link(port->port);
775 * Hard-coded Thunderbolt port to PCIe down port mapping
778 if (tb_switch_is_cactus_ridge(sw) ||
779 tb_switch_is_alpine_ridge(sw))
780 index = !phy_port ? 6 : 7;
781 else if (tb_switch_is_falcon_ridge(sw))
782 index = !phy_port ? 6 : 8;
783 else if (tb_switch_is_titan_ridge(sw))
784 index = !phy_port ? 8 : 9;
788 /* Validate the hard-coding */
789 if (WARN_ON(index > sw->config.max_port_number))
792 down = &sw->ports[index];
796 if (WARN_ON(!tb_port_is_pcie_down(down)))
798 if (tb_pci_port_is_enabled(down))
805 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
808 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
810 struct tb_port *host_port, *port;
811 struct tb_cm *tcm = tb_priv(tb);
813 host_port = tb_route(in->sw) ?
814 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
816 list_for_each_entry(port, &tcm->dp_resources, list) {
817 if (!tb_port_is_dpout(port))
820 if (tb_port_is_enabled(port)) {
821 tb_port_dbg(port, "in use\n");
825 tb_port_dbg(port, "DP OUT available\n");
828 * Keep the DP tunnel under the topology starting from
829 * the same host router downstream port.
831 if (host_port && tb_route(port->sw)) {
834 p = tb_port_at(tb_route(port->sw), tb->root_switch);
845 static void tb_tunnel_dp(struct tb *tb)
847 int available_up, available_down, ret;
848 struct tb_cm *tcm = tb_priv(tb);
849 struct tb_port *port, *in, *out;
850 struct tb_tunnel *tunnel;
852 if (!tb_acpi_may_tunnel_dp()) {
853 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
858 * Find pair of inactive DP IN and DP OUT adapters and then
859 * establish a DP tunnel between them.
861 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
865 list_for_each_entry(port, &tcm->dp_resources, list) {
866 if (!tb_port_is_dpin(port))
869 if (tb_port_is_enabled(port)) {
870 tb_port_dbg(port, "in use\n");
874 tb_port_dbg(port, "DP IN available\n");
876 out = tb_find_dp_out(tb, port);
884 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
888 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
893 * DP stream needs the domain to be active so runtime resume
894 * both ends of the tunnel.
896 * This should bring the routers in the middle active as well
897 * and keeps the domain from runtime suspending while the DP
900 pm_runtime_get_sync(&in->sw->dev);
901 pm_runtime_get_sync(&out->sw->dev);
903 if (tb_switch_alloc_dp_resource(in->sw, in)) {
904 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
908 /* Make all unused USB3 bandwidth available for the new DP tunnel */
909 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
911 tb_warn(tb, "failed to release unused bandwidth\n");
915 ret = tb_available_bandwidth(tb, in, out, &available_up,
920 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
921 available_up, available_down);
923 tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
925 tb_port_dbg(out, "could not allocate DP tunnel\n");
929 if (tb_tunnel_activate(tunnel)) {
930 tb_port_info(out, "DP tunnel activation failed, aborting\n");
934 list_add_tail(&tunnel->list, &tcm->tunnel_list);
935 tb_reclaim_usb3_bandwidth(tb, in, out);
939 tb_tunnel_free(tunnel);
941 tb_reclaim_usb3_bandwidth(tb, in, out);
943 tb_switch_dealloc_dp_resource(in->sw, in);
945 pm_runtime_mark_last_busy(&out->sw->dev);
946 pm_runtime_put_autosuspend(&out->sw->dev);
947 pm_runtime_mark_last_busy(&in->sw->dev);
948 pm_runtime_put_autosuspend(&in->sw->dev);
951 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
953 struct tb_port *in, *out;
954 struct tb_tunnel *tunnel;
956 if (tb_port_is_dpin(port)) {
957 tb_port_dbg(port, "DP IN resource unavailable\n");
961 tb_port_dbg(port, "DP OUT resource unavailable\n");
966 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
967 tb_deactivate_and_free_tunnel(tunnel);
968 list_del_init(&port->list);
971 * See if there is another DP OUT port that can be used for
972 * to create another tunnel.
977 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
979 struct tb_cm *tcm = tb_priv(tb);
982 if (tb_port_is_enabled(port))
985 list_for_each_entry(p, &tcm->dp_resources, list) {
990 tb_port_dbg(port, "DP %s resource available\n",
991 tb_port_is_dpin(port) ? "IN" : "OUT");
992 list_add_tail(&port->list, &tcm->dp_resources);
994 /* Look for suitable DP IN <-> DP OUT pairs now */
998 static void tb_disconnect_and_release_dp(struct tb *tb)
1000 struct tb_cm *tcm = tb_priv(tb);
1001 struct tb_tunnel *tunnel, *n;
1004 * Tear down all DP tunnels and release their resources. They
1005 * will be re-established after resume based on plug events.
1007 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1008 if (tb_tunnel_is_dp(tunnel))
1009 tb_deactivate_and_free_tunnel(tunnel);
1012 while (!list_empty(&tcm->dp_resources)) {
1013 struct tb_port *port;
1015 port = list_first_entry(&tcm->dp_resources,
1016 struct tb_port, list);
1017 list_del_init(&port->list);
1021 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1023 struct tb_tunnel *tunnel;
1026 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1030 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1031 if (WARN_ON(!tunnel))
1034 tb_tunnel_deactivate(tunnel);
1035 list_del(&tunnel->list);
1036 tb_tunnel_free(tunnel);
1040 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1042 struct tb_port *up, *down, *port;
1043 struct tb_cm *tcm = tb_priv(tb);
1044 struct tb_switch *parent_sw;
1045 struct tb_tunnel *tunnel;
1047 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1052 * Look up available down port. Since we are chaining it should
1053 * be found right above this switch.
1055 parent_sw = tb_to_switch(sw->dev.parent);
1056 port = tb_port_at(tb_route(sw), parent_sw);
1057 down = tb_find_pcie_down(parent_sw, port);
1061 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1065 if (tb_tunnel_activate(tunnel)) {
1067 "PCIe tunnel activation failed, aborting\n");
1068 tb_tunnel_free(tunnel);
1072 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1076 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1078 struct tb_cm *tcm = tb_priv(tb);
1079 struct tb_port *nhi_port, *dst_port;
1080 struct tb_tunnel *tunnel;
1081 struct tb_switch *sw;
1083 sw = tb_to_switch(xd->dev.parent);
1084 dst_port = tb_port_at(xd->route, sw);
1085 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1087 mutex_lock(&tb->lock);
1088 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
1089 xd->transmit_path, xd->receive_ring,
1092 mutex_unlock(&tb->lock);
1096 if (tb_tunnel_activate(tunnel)) {
1097 tb_port_info(nhi_port,
1098 "DMA tunnel activation failed, aborting\n");
1099 tb_tunnel_free(tunnel);
1100 mutex_unlock(&tb->lock);
1104 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1105 mutex_unlock(&tb->lock);
1109 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1111 struct tb_port *dst_port;
1112 struct tb_tunnel *tunnel;
1113 struct tb_switch *sw;
1115 sw = tb_to_switch(xd->dev.parent);
1116 dst_port = tb_port_at(xd->route, sw);
1119 * It is possible that the tunnel was already teared down (in
1120 * case of cable disconnect) so it is fine if we cannot find it
1123 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
1124 tb_deactivate_and_free_tunnel(tunnel);
1127 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1129 if (!xd->is_unplugged) {
1130 mutex_lock(&tb->lock);
1131 __tb_disconnect_xdomain_paths(tb, xd);
1132 mutex_unlock(&tb->lock);
1137 /* hotplug handling */
1140 * tb_handle_hotplug() - handle hotplug event
1142 * Executes on tb->wq.
1144 static void tb_handle_hotplug(struct work_struct *work)
1146 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1147 struct tb *tb = ev->tb;
1148 struct tb_cm *tcm = tb_priv(tb);
1149 struct tb_switch *sw;
1150 struct tb_port *port;
1152 /* Bring the domain back from sleep if it was suspended */
1153 pm_runtime_get_sync(&tb->dev);
1155 mutex_lock(&tb->lock);
1156 if (!tcm->hotplug_active)
1157 goto out; /* during init, suspend or shutdown */
1159 sw = tb_switch_find_by_route(tb, ev->route);
1162 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1163 ev->route, ev->port, ev->unplug);
1166 if (ev->port > sw->config.max_port_number) {
1168 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1169 ev->route, ev->port, ev->unplug);
1172 port = &sw->ports[ev->port];
1173 if (tb_is_upstream_port(port)) {
1174 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1175 ev->route, ev->port, ev->unplug);
1179 pm_runtime_get_sync(&sw->dev);
1182 tb_retimer_remove_all(port);
1184 if (tb_port_has_remote(port)) {
1185 tb_port_dbg(port, "switch unplugged\n");
1186 tb_sw_set_unplugged(port->remote->sw);
1187 tb_free_invalid_tunnels(tb);
1188 tb_remove_dp_resources(port->remote->sw);
1189 tb_switch_tmu_disable(port->remote->sw);
1190 tb_switch_unconfigure_link(port->remote->sw);
1191 tb_switch_lane_bonding_disable(port->remote->sw);
1192 tb_switch_remove(port->remote->sw);
1193 port->remote = NULL;
1194 if (port->dual_link_port)
1195 port->dual_link_port->remote = NULL;
1196 /* Maybe we can create another DP tunnel */
1198 } else if (port->xdomain) {
1199 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1201 tb_port_dbg(port, "xdomain unplugged\n");
1203 * Service drivers are unbound during
1204 * tb_xdomain_remove() so setting XDomain as
1205 * unplugged here prevents deadlock if they call
1206 * tb_xdomain_disable_paths(). We will tear down
1209 xd->is_unplugged = true;
1210 tb_xdomain_remove(xd);
1211 port->xdomain = NULL;
1212 __tb_disconnect_xdomain_paths(tb, xd);
1214 tb_port_unconfigure_xdomain(port);
1215 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1216 tb_dp_resource_unavailable(tb, port);
1219 "got unplug event for disconnected port, ignoring\n");
1221 } else if (port->remote) {
1222 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1224 if (tb_port_is_null(port)) {
1225 tb_port_dbg(port, "hotplug: scanning\n");
1228 tb_port_dbg(port, "hotplug: no switch found\n");
1229 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1230 tb_dp_resource_available(tb, port);
1234 pm_runtime_mark_last_busy(&sw->dev);
1235 pm_runtime_put_autosuspend(&sw->dev);
1240 mutex_unlock(&tb->lock);
1242 pm_runtime_mark_last_busy(&tb->dev);
1243 pm_runtime_put_autosuspend(&tb->dev);
1249 * tb_schedule_hotplug_handler() - callback function for the control channel
1251 * Delegates to tb_handle_hotplug.
1253 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1254 const void *buf, size_t size)
1256 const struct cfg_event_pkg *pkg = buf;
1259 if (type != TB_CFG_PKG_EVENT) {
1260 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1264 route = tb_cfg_get_route(&pkg->header);
1266 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1267 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1271 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1274 static void tb_stop(struct tb *tb)
1276 struct tb_cm *tcm = tb_priv(tb);
1277 struct tb_tunnel *tunnel;
1278 struct tb_tunnel *n;
1280 cancel_delayed_work(&tcm->remove_work);
1281 /* tunnels are only present after everything has been initialized */
1282 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1284 * DMA tunnels require the driver to be functional so we
1285 * tear them down. Other protocol tunnels can be left
1288 if (tb_tunnel_is_dma(tunnel))
1289 tb_tunnel_deactivate(tunnel);
1290 tb_tunnel_free(tunnel);
1292 tb_switch_remove(tb->root_switch);
1293 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1296 static int tb_scan_finalize_switch(struct device *dev, void *data)
1298 if (tb_is_switch(dev)) {
1299 struct tb_switch *sw = tb_to_switch(dev);
1302 * If we found that the switch was already setup by the
1303 * boot firmware, mark it as authorized now before we
1304 * send uevent to userspace.
1309 dev_set_uevent_suppress(dev, false);
1310 kobject_uevent(&dev->kobj, KOBJ_ADD);
1311 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1317 static int tb_start(struct tb *tb)
1319 struct tb_cm *tcm = tb_priv(tb);
1322 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1323 if (IS_ERR(tb->root_switch))
1324 return PTR_ERR(tb->root_switch);
1327 * ICM firmware upgrade needs running firmware and in native
1328 * mode that is not available so disable firmware upgrade of the
1331 tb->root_switch->no_nvm_upgrade = true;
1332 /* All USB4 routers support runtime PM */
1333 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1335 ret = tb_switch_configure(tb->root_switch);
1337 tb_switch_put(tb->root_switch);
1341 /* Announce the switch to the world */
1342 ret = tb_switch_add(tb->root_switch);
1344 tb_switch_put(tb->root_switch);
1348 /* Enable TMU if it is off */
1349 tb_switch_tmu_enable(tb->root_switch);
1350 /* Full scan to discover devices added before the driver was loaded. */
1351 tb_scan_switch(tb->root_switch);
1352 /* Find out tunnels created by the boot firmware */
1353 tb_discover_tunnels(tb->root_switch);
1355 * If the boot firmware did not create USB 3.x tunnels create them
1356 * now for the whole topology.
1358 tb_create_usb3_tunnels(tb->root_switch);
1359 /* Add DP IN resources for the root switch */
1360 tb_add_dp_resources(tb->root_switch);
1361 /* Make the discovered switches available to the userspace */
1362 device_for_each_child(&tb->root_switch->dev, NULL,
1363 tb_scan_finalize_switch);
1365 /* Allow tb_handle_hotplug to progress events */
1366 tcm->hotplug_active = true;
1370 static int tb_suspend_noirq(struct tb *tb)
1372 struct tb_cm *tcm = tb_priv(tb);
1374 tb_dbg(tb, "suspending...\n");
1375 tb_disconnect_and_release_dp(tb);
1376 tb_switch_suspend(tb->root_switch, false);
1377 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1378 tb_dbg(tb, "suspend finished\n");
1383 static void tb_restore_children(struct tb_switch *sw)
1385 struct tb_port *port;
1387 /* No need to restore if the router is already unplugged */
1388 if (sw->is_unplugged)
1391 if (tb_enable_tmu(sw))
1392 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1394 tb_switch_for_each_port(sw, port) {
1395 if (!tb_port_has_remote(port) && !port->xdomain)
1399 tb_switch_lane_bonding_enable(port->remote->sw);
1400 tb_switch_configure_link(port->remote->sw);
1402 tb_restore_children(port->remote->sw);
1403 } else if (port->xdomain) {
1404 tb_port_configure_xdomain(port);
1409 static int tb_resume_noirq(struct tb *tb)
1411 struct tb_cm *tcm = tb_priv(tb);
1412 struct tb_tunnel *tunnel, *n;
1414 tb_dbg(tb, "resuming...\n");
1416 /* remove any pci devices the firmware might have setup */
1417 tb_switch_reset(tb->root_switch);
1419 tb_switch_resume(tb->root_switch);
1420 tb_free_invalid_tunnels(tb);
1421 tb_free_unplugged_children(tb->root_switch);
1422 tb_restore_children(tb->root_switch);
1423 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1424 tb_tunnel_restart(tunnel);
1425 if (!list_empty(&tcm->tunnel_list)) {
1427 * the pcie links need some time to get going.
1428 * 100ms works for me...
1430 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1433 /* Allow tb_handle_hotplug to progress events */
1434 tcm->hotplug_active = true;
1435 tb_dbg(tb, "resume finished\n");
1440 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1442 struct tb_port *port;
1445 tb_switch_for_each_port(sw, port) {
1446 if (tb_is_upstream_port(port))
1448 if (port->xdomain && port->xdomain->is_unplugged) {
1449 tb_retimer_remove_all(port);
1450 tb_xdomain_remove(port->xdomain);
1451 tb_port_unconfigure_xdomain(port);
1452 port->xdomain = NULL;
1454 } else if (port->remote) {
1455 ret += tb_free_unplugged_xdomains(port->remote->sw);
1462 static int tb_freeze_noirq(struct tb *tb)
1464 struct tb_cm *tcm = tb_priv(tb);
1466 tcm->hotplug_active = false;
1470 static int tb_thaw_noirq(struct tb *tb)
1472 struct tb_cm *tcm = tb_priv(tb);
1474 tcm->hotplug_active = true;
1478 static void tb_complete(struct tb *tb)
1481 * Release any unplugged XDomains and if there is a case where
1482 * another domain is swapped in place of unplugged XDomain we
1483 * need to run another rescan.
1485 mutex_lock(&tb->lock);
1486 if (tb_free_unplugged_xdomains(tb->root_switch))
1487 tb_scan_switch(tb->root_switch);
1488 mutex_unlock(&tb->lock);
1491 static int tb_runtime_suspend(struct tb *tb)
1493 struct tb_cm *tcm = tb_priv(tb);
1495 mutex_lock(&tb->lock);
1496 tb_switch_suspend(tb->root_switch, true);
1497 tcm->hotplug_active = false;
1498 mutex_unlock(&tb->lock);
1503 static void tb_remove_work(struct work_struct *work)
1505 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1506 struct tb *tb = tcm_to_tb(tcm);
1508 mutex_lock(&tb->lock);
1509 if (tb->root_switch) {
1510 tb_free_unplugged_children(tb->root_switch);
1511 tb_free_unplugged_xdomains(tb->root_switch);
1513 mutex_unlock(&tb->lock);
1516 static int tb_runtime_resume(struct tb *tb)
1518 struct tb_cm *tcm = tb_priv(tb);
1519 struct tb_tunnel *tunnel, *n;
1521 mutex_lock(&tb->lock);
1522 tb_switch_resume(tb->root_switch);
1523 tb_free_invalid_tunnels(tb);
1524 tb_restore_children(tb->root_switch);
1525 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1526 tb_tunnel_restart(tunnel);
1527 tcm->hotplug_active = true;
1528 mutex_unlock(&tb->lock);
1531 * Schedule cleanup of any unplugged devices. Run this in a
1532 * separate thread to avoid possible deadlock if the device
1533 * removal runtime resumes the unplugged device.
1535 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1539 static const struct tb_cm_ops tb_cm_ops = {
1542 .suspend_noirq = tb_suspend_noirq,
1543 .resume_noirq = tb_resume_noirq,
1544 .freeze_noirq = tb_freeze_noirq,
1545 .thaw_noirq = tb_thaw_noirq,
1546 .complete = tb_complete,
1547 .runtime_suspend = tb_runtime_suspend,
1548 .runtime_resume = tb_runtime_resume,
1549 .handle_event = tb_handle_event,
1550 .disapprove_switch = tb_disconnect_pci,
1551 .approve_switch = tb_tunnel_pci,
1552 .approve_xdomain_paths = tb_approve_xdomain_paths,
1553 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1556 struct tb *tb_probe(struct tb_nhi *nhi)
1561 tb = tb_domain_alloc(nhi, sizeof(*tcm));
1565 if (tb_acpi_may_tunnel_pcie())
1566 tb->security_level = TB_SECURITY_USER;
1568 tb->security_level = TB_SECURITY_NOPCIE;
1570 tb->cm_ops = &tb_cm_ops;
1573 INIT_LIST_HEAD(&tcm->tunnel_list);
1574 INIT_LIST_HEAD(&tcm->dp_resources);
1575 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1577 tb_dbg(tb, "using software connection manager\n");