diff --git a/drivers/thunderbolt/tb-cio-sleep.c b/drivers/thunderbolt/tb-cio-sleep.c index a352925edc6b6..1102fb8fe7405 100644 --- a/drivers/thunderbolt/tb-cio-sleep.c +++ b/drivers/thunderbolt/tb-cio-sleep.c @@ -26,6 +26,27 @@ static int tb_cio_check_topology(struct tb_switch *sw, bool wake) return 0; } +/* + * The adapter that holds the peer at @route, found below @sw as tb_xdomain_find_by_route() + * finds the peer: through the adapters' pointers, which hold no reference. + */ +static struct tb_port *tb_cio_xdomain_port(struct tb_switch *sw, u64 route) +{ + struct tb_port *port, *holder; + + tb_switch_for_each_port(sw, port) { + if (port->xdomain) { + if (port->xdomain->route == route) + return port; + } else if (tb_port_has_remote(port)) { + holder = tb_cio_xdomain_port(port->remote->sw, route); + if (holder) + return holder; + } + } + return NULL; +} + /* * A host at the other end of a link (an XDomain, such as a host-to-host cable to another * Mac) owns its half of it, which the router's power cycle cannot keep: release it as an @@ -38,6 +59,7 @@ static void tb_cio_release_xdomains(struct tb *tb, struct tb_switch *sw) struct tb_port *port; tb_switch_for_each_port(sw, port) { + struct tb_port *holder; struct tb_xdomain *xd; if (!tb_port_is_null(port) || tb_is_upstream_port(port)) @@ -49,21 +71,23 @@ static void tb_cio_release_xdomains(struct tb *tb, struct tb_switch *sw) /* * By its route, through the router tree: on the J713 the adapter iterated here * did not hold the peer itself when the release first ran (0:1's xdomain was - * unset), while the route lookup finds it. + * unset), while the route lookup finds it. The peer is taken off the adapter + * that does hold it, before its last reference can go. */ - xd = tb_xdomain_find_by_route(tb, tb_downstream_route(port)); - tb_port_dbg(port, "sleep: xdomain %s, port's %s\n", xd ? "found" : "none", - port->xdomain ? "set" : "unset"); - if (!xd) + holder = tb_cio_xdomain_port(tb->root_switch, tb_downstream_route(port)); + if (!holder) continue; - tb_port_info(port, "xdomain released for the sleep\n"); + if (holder != port) + tb_port_dbg(port, "sleep: xdomain held by %llx:%u\n", + tb_route(holder->sw), holder->port); + xd = tb_xdomain_get(holder->xdomain); + tb_port_info(holder, "xdomain released for the sleep\n"); xd->is_unplugged = true; tb_xdomain_remove(xd); - if (port->xdomain == xd) - port->xdomain = NULL; + holder->xdomain = NULL; __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); tb_xdomain_put(xd); - tb_port_unconfigure_xdomain(port); + tb_port_unconfigure_xdomain(holder); } } diff --git a/tools/asahi/tb-cio-sleep-tests.c b/tools/asahi/tb-cio-sleep-tests.c index c456b33529b85..233c774282eeb 100644 --- a/tools/asahi/tb-cio-sleep-tests.c +++ b/tools/asahi/tb-cio-sleep-tests.c @@ -497,14 +497,11 @@ static unsigned long tb_downstream_route(struct tb_port *p) { return (tb_route(p->sw) << 8) | (unsigned long)p->port; } -static struct tb_xdomain *tb_xdomain_find_by_route(struct tb *t, unsigned long route) +static struct tb_xdomain *tb_xdomain_get(struct tb_xdomain *xd) { - for (int i = 0; i < 2; i++) - if (xds[i].present && !xds[i].is_unplugged && xds[i].route == route) { - xds[i].refs++; - return &xds[i]; - } - return NULL; + if (xd) + xd->refs++; + return xd; } static void tb_xdomain_remove(struct tb_xdomain *xd) { @@ -522,10 +519,12 @@ static void tb_xdomain_put(struct tb_xdomain *xd) assert(xd->refs > 0); xd->refs--; } +static struct tb_port *xd_unconfigured_port; static void tb_port_unconfigure_xdomain(struct tb_port *p) { assert(tb.lock); xd_unconfigured++; + xd_unconfigured_port = p; } /* Its services unbind and give back their rings: never under the domain lock. */ static void tb_domain_unregister_unplugged_xdomains(struct tb *t) @@ -552,6 +551,17 @@ static void add_xdomain(int i) link[0].next = &xports[0]; } } +/* + * As on the J713: the adapter that the release iterates first holds no peer, while the + * lookup by its route finds the one that another adapter holds. + */ +static struct tb_port alias; +static void add_alias(void) +{ + alias = (struct tb_port){ .sw = &root, .port = xports[0].port, .null = true, + .next = link[0].next }; + link[0].next = &alias; +} /* The refused path's rescan: a released peer whose link is still up is found again. */ static int rescans, cleanups; static void tb_resume_cleanup(struct tb *t) @@ -612,6 +622,7 @@ static void setup(void) memset(xds, 0, sizeof(xds)); memset(xports, 0, sizeof(xports)); xd_unconfigured = 0; + xd_unconfigured_port = NULL; nhi.ops = &nhi_ops; nhi.quirks = 0; tb = (struct tb){ .cm_ops = &tb_cm_ops, @@ -948,6 +959,15 @@ int main(void) assert(xd_unconfigured == 1); complete(); cases++; + /* Found by the route of an adapter that does not hold it: taken off its holder. */ + setup(); + add_xdomain(0); + add_alias(); + assert(!tb_cio_sleep_prepare(&tb)); + check_released(0); + assert(!alias.xdomain && xd_unconfigured == 1 && xd_unconfigured_port == &xports[0]); + complete(); + cases++; /* Both at once. */ setup(); add_xdomain(0); diff --git a/tools/asahi/tb-cio-sleep-tests.py b/tools/asahi/tb-cio-sleep-tests.py index c16dd65524878..9a33f628d611d 100644 --- a/tools/asahi/tb-cio-sleep-tests.py +++ b/tools/asahi/tb-cio-sleep-tests.py @@ -68,7 +68,11 @@ def main(): 'refused-skips-rescan': ('\t\ttb_scan_switch(tb->root_switch);\n}', '\t\t(void)0;\n}'), 'rescan-while-stopping': ('if (tcm->hotplug_active && !tcm->stopping)\n\t\ttb_scan_switch', 'if (true)\n\t\ttb_scan_switch'), 'refused-keeps-cleanup': ('\t\t\ttcm->resume_cleanup_pending = false;\n\t\t}\n\t}\n\ttb_domain_unregister', '\t\t}\n\t}\n\ttb_domain_unregister'), - 'keep-xdomain-port': ('\t\tif (port->xdomain == xd)\n\t\t\tport->xdomain = NULL;\n', ''), + 'keep-xdomain-port': ('\t\tholder->xdomain = NULL;\n', ''), + 'clear-iterated-port': ('\t\tholder->xdomain = NULL;\n', + '\t\tif (port->xdomain == xd)\n\t\t\tport->xdomain = NULL;\n'), + 'unconfigure-iterated-port': ('tb_port_unconfigure_xdomain(holder);', + 'tb_port_unconfigure_xdomain(port);'), } variants = {'real': unit} for name, (old, new) in mutations.items():