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);
141 } else if (tb_tunnel_is_dp(tunnel)) {
142 /* Keep the domain from powering down */
143 pm_runtime_get_sync(&tunnel->src_port->sw->dev);
144 pm_runtime_get_sync(&tunnel->dst_port->sw->dev);
147 list_add_tail(&tunnel->list, &tcm->tunnel_list);
150 tb_switch_for_each_port(sw, port) {
151 if (tb_port_has_remote(port))
152 tb_discover_tunnels(port->remote->sw);
156 static int tb_port_configure_xdomain(struct tb_port *port)
159 * XDomain paths currently only support single lane so we must
160 * disable the other lane according to USB4 spec.
162 tb_port_disable(port->dual_link_port);
164 if (tb_switch_is_usb4(port->sw))
165 return usb4_port_configure_xdomain(port);
166 return tb_lc_configure_xdomain(port);
169 static void tb_port_unconfigure_xdomain(struct tb_port *port)
171 if (tb_switch_is_usb4(port->sw))
172 usb4_port_unconfigure_xdomain(port);
174 tb_lc_unconfigure_xdomain(port);
176 tb_port_enable(port->dual_link_port);
179 static void tb_scan_xdomain(struct tb_port *port)
181 struct tb_switch *sw = port->sw;
182 struct tb *tb = sw->tb;
183 struct tb_xdomain *xd;
186 route = tb_downstream_route(port);
187 xd = tb_xdomain_find_by_route(tb, route);
193 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
196 tb_port_at(route, sw)->xdomain = xd;
197 tb_port_configure_xdomain(port);
202 static int tb_enable_tmu(struct tb_switch *sw)
206 /* If it is already enabled in correct mode, don't touch it */
207 if (tb_switch_tmu_is_enabled(sw))
210 ret = tb_switch_tmu_disable(sw);
214 ret = tb_switch_tmu_post_time(sw);
218 return tb_switch_tmu_enable(sw);
222 * tb_find_unused_port() - return the first inactive port on @sw
223 * @sw: Switch to find the port on
224 * @type: Port type to look for
226 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
227 enum tb_port_type type)
229 struct tb_port *port;
231 tb_switch_for_each_port(sw, port) {
232 if (tb_is_upstream_port(port))
234 if (port->config.type != type)
238 if (tb_port_is_enabled(port))
245 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
246 const struct tb_port *port)
248 struct tb_port *down;
250 down = usb4_switch_map_usb3_down(sw, port);
251 if (down && !tb_usb3_port_is_enabled(down))
256 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
257 struct tb_port *src_port,
258 struct tb_port *dst_port)
260 struct tb_cm *tcm = tb_priv(tb);
261 struct tb_tunnel *tunnel;
263 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
264 if (tunnel->type == type &&
265 ((src_port && src_port == tunnel->src_port) ||
266 (dst_port && dst_port == tunnel->dst_port))) {
274 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
275 struct tb_port *src_port,
276 struct tb_port *dst_port)
278 struct tb_port *port, *usb3_down;
279 struct tb_switch *sw;
281 /* Pick the router that is deepest in the topology */
282 if (dst_port->sw->config.depth > src_port->sw->config.depth)
287 /* Can't be the host router */
288 if (sw == tb->root_switch)
291 /* Find the downstream USB4 port that leads to this router */
292 port = tb_port_at(tb_route(sw), tb->root_switch);
293 /* Find the corresponding host router USB3 downstream port */
294 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
298 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
301 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
302 struct tb_port *dst_port, int *available_up, int *available_down)
304 int usb3_consumed_up, usb3_consumed_down, ret;
305 struct tb_cm *tcm = tb_priv(tb);
306 struct tb_tunnel *tunnel;
307 struct tb_port *port;
309 tb_port_dbg(dst_port, "calculating available bandwidth\n");
311 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
313 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
314 &usb3_consumed_down);
318 usb3_consumed_up = 0;
319 usb3_consumed_down = 0;
322 *available_up = *available_down = 40000;
324 /* Find the minimum available bandwidth over all links */
325 tb_for_each_port_on_path(src_port, dst_port, port) {
326 int link_speed, link_width, up_bw, down_bw;
328 if (!tb_port_is_null(port))
331 if (tb_is_upstream_port(port)) {
332 link_speed = port->sw->link_speed;
334 link_speed = tb_port_get_link_speed(port);
339 link_width = port->bonded ? 2 : 1;
341 up_bw = link_speed * link_width * 1000; /* Mb/s */
342 /* Leave 10% guard band */
346 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
349 * Find all DP tunnels that cross the port and reduce
350 * their consumed bandwidth from the available.
352 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
353 int dp_consumed_up, dp_consumed_down;
355 if (!tb_tunnel_is_dp(tunnel))
358 if (!tb_tunnel_port_on_path(tunnel, port))
361 ret = tb_tunnel_consumed_bandwidth(tunnel,
367 up_bw -= dp_consumed_up;
368 down_bw -= dp_consumed_down;
372 * If USB3 is tunneled from the host router down to the
373 * branch leading to port we need to take USB3 consumed
374 * bandwidth into account regardless whether it actually
377 up_bw -= usb3_consumed_up;
378 down_bw -= usb3_consumed_down;
380 if (up_bw < *available_up)
381 *available_up = up_bw;
382 if (down_bw < *available_down)
383 *available_down = down_bw;
386 if (*available_up < 0)
388 if (*available_down < 0)
394 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
395 struct tb_port *src_port,
396 struct tb_port *dst_port)
398 struct tb_tunnel *tunnel;
400 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
401 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
404 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
405 struct tb_port *dst_port)
407 int ret, available_up, available_down;
408 struct tb_tunnel *tunnel;
410 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
414 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
417 * Calculate available bandwidth for the first hop USB3 tunnel.
418 * That determines the whole USB3 bandwidth for this branch.
420 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
421 &available_up, &available_down);
423 tb_warn(tb, "failed to calculate available bandwidth\n");
427 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
428 available_up, available_down);
430 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
433 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
435 struct tb_switch *parent = tb_switch_parent(sw);
436 int ret, available_up, available_down;
437 struct tb_port *up, *down, *port;
438 struct tb_cm *tcm = tb_priv(tb);
439 struct tb_tunnel *tunnel;
441 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
449 * Look up available down port. Since we are chaining it should
450 * be found right above this switch.
452 port = tb_port_at(tb_route(sw), parent);
453 down = tb_find_usb3_down(parent, port);
457 if (tb_route(parent)) {
458 struct tb_port *parent_up;
460 * Check first that the parent switch has its upstream USB3
461 * port enabled. Otherwise the chain is not complete and
462 * there is no point setting up a new tunnel.
464 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
465 if (!parent_up || !tb_port_is_enabled(parent_up))
468 /* Make all unused bandwidth available for the new tunnel */
469 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
474 ret = tb_available_bandwidth(tb, down, up, &available_up,
479 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
480 available_up, available_down);
482 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
489 if (tb_tunnel_activate(tunnel)) {
491 "USB3 tunnel activation failed, aborting\n");
496 list_add_tail(&tunnel->list, &tcm->tunnel_list);
497 if (tb_route(parent))
498 tb_reclaim_usb3_bandwidth(tb, down, up);
503 tb_tunnel_free(tunnel);
505 if (tb_route(parent))
506 tb_reclaim_usb3_bandwidth(tb, down, up);
511 static int tb_create_usb3_tunnels(struct tb_switch *sw)
513 struct tb_port *port;
517 ret = tb_tunnel_usb3(sw->tb, sw);
522 tb_switch_for_each_port(sw, port) {
523 if (!tb_port_has_remote(port))
525 ret = tb_create_usb3_tunnels(port->remote->sw);
533 static void tb_scan_port(struct tb_port *port);
536 * tb_scan_switch() - scan for and initialize downstream switches
538 static void tb_scan_switch(struct tb_switch *sw)
540 struct tb_port *port;
542 pm_runtime_get_sync(&sw->dev);
544 tb_switch_for_each_port(sw, port)
547 pm_runtime_mark_last_busy(&sw->dev);
548 pm_runtime_put_autosuspend(&sw->dev);
552 * tb_scan_port() - check for and initialize switches below port
554 static void tb_scan_port(struct tb_port *port)
556 struct tb_cm *tcm = tb_priv(port->sw->tb);
557 struct tb_port *upstream_port;
558 struct tb_switch *sw;
560 if (tb_is_upstream_port(port))
563 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
564 !tb_dp_port_is_enabled(port)) {
565 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
566 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
571 if (port->config.type != TB_TYPE_PORT)
573 if (port->dual_link_port && port->link_nr)
575 * Downstream switch is reachable through two ports.
576 * Only scan on the primary port (link_nr == 0).
578 if (tb_wait_for_port(port, false) <= 0)
581 tb_port_dbg(port, "port already has a remote\n");
585 tb_retimer_scan(port);
587 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
588 tb_downstream_route(port));
591 * If there is an error accessing the connected switch
592 * it may be connected to another domain. Also we allow
593 * the other domain to be connected to a max depth switch.
595 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
596 tb_scan_xdomain(port);
600 if (tb_switch_configure(sw)) {
606 * If there was previously another domain connected remove it
610 tb_xdomain_remove(port->xdomain);
611 tb_port_unconfigure_xdomain(port);
612 port->xdomain = NULL;
616 * Do not send uevents until we have discovered all existing
617 * tunnels and know which switches were authorized already by
620 if (!tcm->hotplug_active)
621 dev_set_uevent_suppress(&sw->dev, true);
624 * At the moment Thunderbolt 2 and beyond (devices with LC) we
625 * can support runtime PM.
627 sw->rpm = sw->generation > 1;
629 if (tb_switch_add(sw)) {
634 /* Link the switches using both links if available */
635 upstream_port = tb_upstream_port(sw);
636 port->remote = upstream_port;
637 upstream_port->remote = port;
638 if (port->dual_link_port && upstream_port->dual_link_port) {
639 port->dual_link_port->remote = upstream_port->dual_link_port;
640 upstream_port->dual_link_port->remote = port->dual_link_port;
643 /* Enable lane bonding if supported */
644 tb_switch_lane_bonding_enable(sw);
645 /* Set the link configured */
646 tb_switch_configure_link(sw);
648 if (tb_enable_tmu(sw))
649 tb_sw_warn(sw, "failed to enable TMU\n");
651 /* Scan upstream retimers */
652 tb_retimer_scan(upstream_port);
655 * Create USB 3.x tunnels only when the switch is plugged to the
656 * domain. This is because we scan the domain also during discovery
657 * and want to discover existing USB 3.x tunnels before we create
660 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
661 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
663 tb_add_dp_resources(sw);
667 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
669 struct tb_port *src_port, *dst_port;
675 tb_tunnel_deactivate(tunnel);
676 list_del(&tunnel->list);
679 src_port = tunnel->src_port;
680 dst_port = tunnel->dst_port;
682 switch (tunnel->type) {
685 * In case of DP tunnel make sure the DP IN resource is
686 * deallocated properly.
688 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
689 /* Now we can allow the domain to runtime suspend again */
690 pm_runtime_mark_last_busy(&dst_port->sw->dev);
691 pm_runtime_put_autosuspend(&dst_port->sw->dev);
692 pm_runtime_mark_last_busy(&src_port->sw->dev);
693 pm_runtime_put_autosuspend(&src_port->sw->dev);
697 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
702 * PCIe and DMA tunnels do not consume guaranteed
708 tb_tunnel_free(tunnel);
712 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
714 static void tb_free_invalid_tunnels(struct tb *tb)
716 struct tb_cm *tcm = tb_priv(tb);
717 struct tb_tunnel *tunnel;
720 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
721 if (tb_tunnel_is_invalid(tunnel))
722 tb_deactivate_and_free_tunnel(tunnel);
727 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
729 static void tb_free_unplugged_children(struct tb_switch *sw)
731 struct tb_port *port;
733 tb_switch_for_each_port(sw, port) {
734 if (!tb_port_has_remote(port))
737 if (port->remote->sw->is_unplugged) {
738 tb_retimer_remove_all(port);
739 tb_remove_dp_resources(port->remote->sw);
740 tb_switch_unconfigure_link(port->remote->sw);
741 tb_switch_lane_bonding_disable(port->remote->sw);
742 tb_switch_remove(port->remote->sw);
744 if (port->dual_link_port)
745 port->dual_link_port->remote = NULL;
747 tb_free_unplugged_children(port->remote->sw);
752 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
753 const struct tb_port *port)
755 struct tb_port *down = NULL;
758 * To keep plugging devices consistently in the same PCIe
759 * hierarchy, do mapping here for switch downstream PCIe ports.
761 if (tb_switch_is_usb4(sw)) {
762 down = usb4_switch_map_pcie_down(sw, port);
763 } else if (!tb_route(sw)) {
764 int phy_port = tb_phy_port_from_link(port->port);
768 * Hard-coded Thunderbolt port to PCIe down port mapping
771 if (tb_switch_is_cactus_ridge(sw) ||
772 tb_switch_is_alpine_ridge(sw))
773 index = !phy_port ? 6 : 7;
774 else if (tb_switch_is_falcon_ridge(sw))
775 index = !phy_port ? 6 : 8;
776 else if (tb_switch_is_titan_ridge(sw))
777 index = !phy_port ? 8 : 9;
781 /* Validate the hard-coding */
782 if (WARN_ON(index > sw->config.max_port_number))
785 down = &sw->ports[index];
789 if (WARN_ON(!tb_port_is_pcie_down(down)))
791 if (tb_pci_port_is_enabled(down))
798 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
801 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
803 struct tb_port *host_port, *port;
804 struct tb_cm *tcm = tb_priv(tb);
806 host_port = tb_route(in->sw) ?
807 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
809 list_for_each_entry(port, &tcm->dp_resources, list) {
810 if (!tb_port_is_dpout(port))
813 if (tb_port_is_enabled(port)) {
814 tb_port_dbg(port, "in use\n");
818 tb_port_dbg(port, "DP OUT available\n");
821 * Keep the DP tunnel under the topology starting from
822 * the same host router downstream port.
824 if (host_port && tb_route(port->sw)) {
827 p = tb_port_at(tb_route(port->sw), tb->root_switch);
838 static void tb_tunnel_dp(struct tb *tb)
840 int available_up, available_down, ret;
841 struct tb_cm *tcm = tb_priv(tb);
842 struct tb_port *port, *in, *out;
843 struct tb_tunnel *tunnel;
846 * Find pair of inactive DP IN and DP OUT adapters and then
847 * establish a DP tunnel between them.
849 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
853 list_for_each_entry(port, &tcm->dp_resources, list) {
854 if (!tb_port_is_dpin(port))
857 if (tb_port_is_enabled(port)) {
858 tb_port_dbg(port, "in use\n");
862 tb_port_dbg(port, "DP IN available\n");
864 out = tb_find_dp_out(tb, port);
872 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
876 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
881 * DP stream needs the domain to be active so runtime resume
882 * both ends of the tunnel.
884 * This should bring the routers in the middle active as well
885 * and keeps the domain from runtime suspending while the DP
888 pm_runtime_get_sync(&in->sw->dev);
889 pm_runtime_get_sync(&out->sw->dev);
891 if (tb_switch_alloc_dp_resource(in->sw, in)) {
892 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
896 /* Make all unused USB3 bandwidth available for the new DP tunnel */
897 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
899 tb_warn(tb, "failed to release unused bandwidth\n");
903 ret = tb_available_bandwidth(tb, in, out, &available_up,
908 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
909 available_up, available_down);
911 tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
913 tb_port_dbg(out, "could not allocate DP tunnel\n");
917 if (tb_tunnel_activate(tunnel)) {
918 tb_port_info(out, "DP tunnel activation failed, aborting\n");
922 list_add_tail(&tunnel->list, &tcm->tunnel_list);
923 tb_reclaim_usb3_bandwidth(tb, in, out);
927 tb_tunnel_free(tunnel);
929 tb_reclaim_usb3_bandwidth(tb, in, out);
931 tb_switch_dealloc_dp_resource(in->sw, in);
933 pm_runtime_mark_last_busy(&out->sw->dev);
934 pm_runtime_put_autosuspend(&out->sw->dev);
935 pm_runtime_mark_last_busy(&in->sw->dev);
936 pm_runtime_put_autosuspend(&in->sw->dev);
939 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
941 struct tb_port *in, *out;
942 struct tb_tunnel *tunnel;
944 if (tb_port_is_dpin(port)) {
945 tb_port_dbg(port, "DP IN resource unavailable\n");
949 tb_port_dbg(port, "DP OUT resource unavailable\n");
954 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
955 tb_deactivate_and_free_tunnel(tunnel);
956 list_del_init(&port->list);
959 * See if there is another DP OUT port that can be used for
960 * to create another tunnel.
965 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
967 struct tb_cm *tcm = tb_priv(tb);
970 if (tb_port_is_enabled(port))
973 list_for_each_entry(p, &tcm->dp_resources, list) {
978 tb_port_dbg(port, "DP %s resource available\n",
979 tb_port_is_dpin(port) ? "IN" : "OUT");
980 list_add_tail(&port->list, &tcm->dp_resources);
982 /* Look for suitable DP IN <-> DP OUT pairs now */
986 static void tb_disconnect_and_release_dp(struct tb *tb)
988 struct tb_cm *tcm = tb_priv(tb);
989 struct tb_tunnel *tunnel, *n;
992 * Tear down all DP tunnels and release their resources. They
993 * will be re-established after resume based on plug events.
995 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
996 if (tb_tunnel_is_dp(tunnel))
997 tb_deactivate_and_free_tunnel(tunnel);
1000 while (!list_empty(&tcm->dp_resources)) {
1001 struct tb_port *port;
1003 port = list_first_entry(&tcm->dp_resources,
1004 struct tb_port, list);
1005 list_del_init(&port->list);
1009 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1011 struct tb_port *up, *down, *port;
1012 struct tb_cm *tcm = tb_priv(tb);
1013 struct tb_switch *parent_sw;
1014 struct tb_tunnel *tunnel;
1016 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1021 * Look up available down port. Since we are chaining it should
1022 * be found right above this switch.
1024 parent_sw = tb_to_switch(sw->dev.parent);
1025 port = tb_port_at(tb_route(sw), parent_sw);
1026 down = tb_find_pcie_down(parent_sw, port);
1030 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1034 if (tb_tunnel_activate(tunnel)) {
1036 "PCIe tunnel activation failed, aborting\n");
1037 tb_tunnel_free(tunnel);
1041 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1045 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1047 struct tb_cm *tcm = tb_priv(tb);
1048 struct tb_port *nhi_port, *dst_port;
1049 struct tb_tunnel *tunnel;
1050 struct tb_switch *sw;
1052 sw = tb_to_switch(xd->dev.parent);
1053 dst_port = tb_port_at(xd->route, sw);
1054 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1056 mutex_lock(&tb->lock);
1057 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
1058 xd->transmit_path, xd->receive_ring,
1061 mutex_unlock(&tb->lock);
1065 if (tb_tunnel_activate(tunnel)) {
1066 tb_port_info(nhi_port,
1067 "DMA tunnel activation failed, aborting\n");
1068 tb_tunnel_free(tunnel);
1069 mutex_unlock(&tb->lock);
1073 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1074 mutex_unlock(&tb->lock);
1078 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1080 struct tb_port *dst_port;
1081 struct tb_tunnel *tunnel;
1082 struct tb_switch *sw;
1084 sw = tb_to_switch(xd->dev.parent);
1085 dst_port = tb_port_at(xd->route, sw);
1088 * It is possible that the tunnel was already teared down (in
1089 * case of cable disconnect) so it is fine if we cannot find it
1092 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
1093 tb_deactivate_and_free_tunnel(tunnel);
1096 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1098 if (!xd->is_unplugged) {
1099 mutex_lock(&tb->lock);
1100 __tb_disconnect_xdomain_paths(tb, xd);
1101 mutex_unlock(&tb->lock);
1106 /* hotplug handling */
1109 * tb_handle_hotplug() - handle hotplug event
1111 * Executes on tb->wq.
1113 static void tb_handle_hotplug(struct work_struct *work)
1115 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1116 struct tb *tb = ev->tb;
1117 struct tb_cm *tcm = tb_priv(tb);
1118 struct tb_switch *sw;
1119 struct tb_port *port;
1121 /* Bring the domain back from sleep if it was suspended */
1122 pm_runtime_get_sync(&tb->dev);
1124 mutex_lock(&tb->lock);
1125 if (!tcm->hotplug_active)
1126 goto out; /* during init, suspend or shutdown */
1128 sw = tb_switch_find_by_route(tb, ev->route);
1131 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1132 ev->route, ev->port, ev->unplug);
1135 if (ev->port > sw->config.max_port_number) {
1137 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1138 ev->route, ev->port, ev->unplug);
1141 port = &sw->ports[ev->port];
1142 if (tb_is_upstream_port(port)) {
1143 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1144 ev->route, ev->port, ev->unplug);
1148 pm_runtime_get_sync(&sw->dev);
1151 tb_retimer_remove_all(port);
1153 if (tb_port_has_remote(port)) {
1154 tb_port_dbg(port, "switch unplugged\n");
1155 tb_sw_set_unplugged(port->remote->sw);
1156 tb_free_invalid_tunnels(tb);
1157 tb_remove_dp_resources(port->remote->sw);
1158 tb_switch_tmu_disable(port->remote->sw);
1159 tb_switch_unconfigure_link(port->remote->sw);
1160 tb_switch_lane_bonding_disable(port->remote->sw);
1161 tb_switch_remove(port->remote->sw);
1162 port->remote = NULL;
1163 if (port->dual_link_port)
1164 port->dual_link_port->remote = NULL;
1165 /* Maybe we can create another DP tunnel */
1167 } else if (port->xdomain) {
1168 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1170 tb_port_dbg(port, "xdomain unplugged\n");
1172 * Service drivers are unbound during
1173 * tb_xdomain_remove() so setting XDomain as
1174 * unplugged here prevents deadlock if they call
1175 * tb_xdomain_disable_paths(). We will tear down
1178 xd->is_unplugged = true;
1179 tb_xdomain_remove(xd);
1180 port->xdomain = NULL;
1181 __tb_disconnect_xdomain_paths(tb, xd);
1183 tb_port_unconfigure_xdomain(port);
1184 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1185 tb_dp_resource_unavailable(tb, port);
1188 "got unplug event for disconnected port, ignoring\n");
1190 } else if (port->remote) {
1191 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1193 if (tb_port_is_null(port)) {
1194 tb_port_dbg(port, "hotplug: scanning\n");
1197 tb_port_dbg(port, "hotplug: no switch found\n");
1198 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1199 tb_dp_resource_available(tb, port);
1203 pm_runtime_mark_last_busy(&sw->dev);
1204 pm_runtime_put_autosuspend(&sw->dev);
1209 mutex_unlock(&tb->lock);
1211 pm_runtime_mark_last_busy(&tb->dev);
1212 pm_runtime_put_autosuspend(&tb->dev);
1218 * tb_schedule_hotplug_handler() - callback function for the control channel
1220 * Delegates to tb_handle_hotplug.
1222 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1223 const void *buf, size_t size)
1225 const struct cfg_event_pkg *pkg = buf;
1228 if (type != TB_CFG_PKG_EVENT) {
1229 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1233 route = tb_cfg_get_route(&pkg->header);
1235 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1236 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1240 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1243 static void tb_stop(struct tb *tb)
1245 struct tb_cm *tcm = tb_priv(tb);
1246 struct tb_tunnel *tunnel;
1247 struct tb_tunnel *n;
1249 cancel_delayed_work(&tcm->remove_work);
1250 /* tunnels are only present after everything has been initialized */
1251 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1253 * DMA tunnels require the driver to be functional so we
1254 * tear them down. Other protocol tunnels can be left
1257 if (tb_tunnel_is_dma(tunnel))
1258 tb_tunnel_deactivate(tunnel);
1259 tb_tunnel_free(tunnel);
1261 tb_switch_remove(tb->root_switch);
1262 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1265 static int tb_scan_finalize_switch(struct device *dev, void *data)
1267 if (tb_is_switch(dev)) {
1268 struct tb_switch *sw = tb_to_switch(dev);
1271 * If we found that the switch was already setup by the
1272 * boot firmware, mark it as authorized now before we
1273 * send uevent to userspace.
1278 dev_set_uevent_suppress(dev, false);
1279 kobject_uevent(&dev->kobj, KOBJ_ADD);
1280 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1286 static int tb_start(struct tb *tb)
1288 struct tb_cm *tcm = tb_priv(tb);
1291 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1292 if (IS_ERR(tb->root_switch))
1293 return PTR_ERR(tb->root_switch);
1296 * ICM firmware upgrade needs running firmware and in native
1297 * mode that is not available so disable firmware upgrade of the
1300 tb->root_switch->no_nvm_upgrade = true;
1301 /* All USB4 routers support runtime PM */
1302 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1304 ret = tb_switch_configure(tb->root_switch);
1306 tb_switch_put(tb->root_switch);
1310 /* Announce the switch to the world */
1311 ret = tb_switch_add(tb->root_switch);
1313 tb_switch_put(tb->root_switch);
1317 /* Enable TMU if it is off */
1318 tb_switch_tmu_enable(tb->root_switch);
1319 /* Full scan to discover devices added before the driver was loaded. */
1320 tb_scan_switch(tb->root_switch);
1321 /* Find out tunnels created by the boot firmware */
1322 tb_discover_tunnels(tb->root_switch);
1324 * If the boot firmware did not create USB 3.x tunnels create them
1325 * now for the whole topology.
1327 tb_create_usb3_tunnels(tb->root_switch);
1328 /* Add DP IN resources for the root switch */
1329 tb_add_dp_resources(tb->root_switch);
1330 /* Make the discovered switches available to the userspace */
1331 device_for_each_child(&tb->root_switch->dev, NULL,
1332 tb_scan_finalize_switch);
1334 /* Allow tb_handle_hotplug to progress events */
1335 tcm->hotplug_active = true;
1339 static int tb_suspend_noirq(struct tb *tb)
1341 struct tb_cm *tcm = tb_priv(tb);
1343 tb_dbg(tb, "suspending...\n");
1344 tb_disconnect_and_release_dp(tb);
1345 tb_switch_suspend(tb->root_switch, false);
1346 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1347 tb_dbg(tb, "suspend finished\n");
1352 static void tb_restore_children(struct tb_switch *sw)
1354 struct tb_port *port;
1356 /* No need to restore if the router is already unplugged */
1357 if (sw->is_unplugged)
1360 if (tb_enable_tmu(sw))
1361 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1363 tb_switch_for_each_port(sw, port) {
1364 if (!tb_port_has_remote(port) && !port->xdomain)
1368 tb_switch_lane_bonding_enable(port->remote->sw);
1369 tb_switch_configure_link(port->remote->sw);
1371 tb_restore_children(port->remote->sw);
1372 } else if (port->xdomain) {
1373 tb_port_configure_xdomain(port);
1378 static int tb_resume_noirq(struct tb *tb)
1380 struct tb_cm *tcm = tb_priv(tb);
1381 struct tb_tunnel *tunnel, *n;
1383 tb_dbg(tb, "resuming...\n");
1385 /* remove any pci devices the firmware might have setup */
1386 tb_switch_reset(tb->root_switch);
1388 tb_switch_resume(tb->root_switch, false);
1389 tb_free_invalid_tunnels(tb);
1390 tb_free_unplugged_children(tb->root_switch);
1391 tb_restore_children(tb->root_switch);
1392 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1393 tb_tunnel_restart(tunnel);
1394 if (!list_empty(&tcm->tunnel_list)) {
1396 * the pcie links need some time to get going.
1397 * 100ms works for me...
1399 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1402 /* Allow tb_handle_hotplug to progress events */
1403 tcm->hotplug_active = true;
1404 tb_dbg(tb, "resume finished\n");
1409 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1411 struct tb_port *port;
1414 tb_switch_for_each_port(sw, port) {
1415 if (tb_is_upstream_port(port))
1417 if (port->xdomain && port->xdomain->is_unplugged) {
1418 tb_retimer_remove_all(port);
1419 tb_xdomain_remove(port->xdomain);
1420 tb_port_unconfigure_xdomain(port);
1421 port->xdomain = NULL;
1423 } else if (port->remote) {
1424 ret += tb_free_unplugged_xdomains(port->remote->sw);
1431 static int tb_freeze_noirq(struct tb *tb)
1433 struct tb_cm *tcm = tb_priv(tb);
1435 tcm->hotplug_active = false;
1439 static int tb_thaw_noirq(struct tb *tb)
1441 struct tb_cm *tcm = tb_priv(tb);
1443 tcm->hotplug_active = true;
1447 static void tb_complete(struct tb *tb)
1450 * Release any unplugged XDomains and if there is a case where
1451 * another domain is swapped in place of unplugged XDomain we
1452 * need to run another rescan.
1454 mutex_lock(&tb->lock);
1455 if (tb_free_unplugged_xdomains(tb->root_switch))
1456 tb_scan_switch(tb->root_switch);
1457 mutex_unlock(&tb->lock);
1460 static int tb_runtime_suspend(struct tb *tb)
1462 struct tb_cm *tcm = tb_priv(tb);
1464 mutex_lock(&tb->lock);
1465 tb_switch_suspend(tb->root_switch, true);
1466 tcm->hotplug_active = false;
1467 mutex_unlock(&tb->lock);
1472 static void tb_remove_work(struct work_struct *work)
1474 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1475 struct tb *tb = tcm_to_tb(tcm);
1477 mutex_lock(&tb->lock);
1478 if (tb->root_switch) {
1479 tb_free_unplugged_children(tb->root_switch);
1480 tb_free_unplugged_xdomains(tb->root_switch);
1482 mutex_unlock(&tb->lock);
1485 static int tb_runtime_resume(struct tb *tb)
1487 struct tb_cm *tcm = tb_priv(tb);
1488 struct tb_tunnel *tunnel, *n;
1490 mutex_lock(&tb->lock);
1491 tb_switch_resume(tb->root_switch, true);
1492 tb_free_invalid_tunnels(tb);
1493 tb_restore_children(tb->root_switch);
1494 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1495 tb_tunnel_restart(tunnel);
1496 tcm->hotplug_active = true;
1497 mutex_unlock(&tb->lock);
1500 * Schedule cleanup of any unplugged devices. Run this in a
1501 * separate thread to avoid possible deadlock if the device
1502 * removal runtime resumes the unplugged device.
1504 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1508 static const struct tb_cm_ops tb_cm_ops = {
1511 .suspend_noirq = tb_suspend_noirq,
1512 .resume_noirq = tb_resume_noirq,
1513 .freeze_noirq = tb_freeze_noirq,
1514 .thaw_noirq = tb_thaw_noirq,
1515 .complete = tb_complete,
1516 .runtime_suspend = tb_runtime_suspend,
1517 .runtime_resume = tb_runtime_resume,
1518 .handle_event = tb_handle_event,
1519 .approve_switch = tb_tunnel_pci,
1520 .approve_xdomain_paths = tb_approve_xdomain_paths,
1521 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1524 struct tb *tb_probe(struct tb_nhi *nhi)
1529 tb = tb_domain_alloc(nhi, sizeof(*tcm));
1533 tb->security_level = TB_SECURITY_USER;
1534 tb->cm_ops = &tb_cm_ops;
1537 INIT_LIST_HEAD(&tcm->tunnel_list);
1538 INIT_LIST_HEAD(&tcm->dp_resources);
1539 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);