diff --git a/drivers/thunderbolt/tb-cio-sleep.c b/drivers/thunderbolt/tb-cio-sleep.c index 1102fb8fe7405..dc6e999b5502c 100644 --- a/drivers/thunderbolt/tb-cio-sleep.c +++ b/drivers/thunderbolt/tb-cio-sleep.c @@ -203,10 +203,40 @@ static int tb_cio_wait_links(struct tb *tb) return lost; } +/* + * Mark the routers behind @sw that stopped answering since the router resume, + * as it marks those that did not come back. Return whether there are any. + */ +static bool tb_cio_retire_gone(struct tb_switch *sw) +{ + struct tb_port *port; + bool gone = false; + u32 id; + + tb_switch_for_each_port(sw, port) { + struct tb_switch *peer; + + if (!tb_port_has_remote(port)) + continue; + peer = port->remote->sw; + if (peer->is_unplugged) + continue; + if (!tb_sw_read(peer, &id, TB_CFG_SWITCH, 0, 1)) { + gone |= tb_cio_retire_gone(peer); + continue; + } + tb_port_warn(port, "lost during the CIO wake, disconnecting\n"); + tb_sw_set_unplugged(peer); + gone = true; + } + return gone; +} + /* * Only the host side has to come back. Routers that left or lost power during * the sleep are marked unplugged by the router resume, and their tunnels are - * skipped here and retired at complete, as after a hot unplug. + * skipped here and retired at complete, as after a hot unplug. One that leaves + * between that resume and the end of the replay is no different. */ static void tb_cio_test_usb3_back(struct tb *tb, struct tb_tunnel *tunnel, bool restarted); @@ -217,6 +247,7 @@ static int tb_cio_wake_locked(struct tb *tb, bool *host_pci_lost) const char *step; bool restarted; u64 uid = 0; + u32 id; int ret; lockdep_assert_held(&tb->lock); @@ -277,6 +308,7 @@ static int tb_cio_wake_locked(struct tb *tb, bool *host_pci_lost) goto fail; tb_restore_children(tb->root_switch); step = "tunnel replay"; +replay: list_for_each_entry(tunnel, &tcm->tunnel_list, list) { if (tb_tunnel_is_invalid(tunnel)) { tb_tunnel_info(tunnel, "lost during CIO sleep\n"); @@ -288,8 +320,15 @@ static int tb_cio_wake_locked(struct tb *tb, bool *host_pci_lost) /* After a refused sleep the hops left enabled are found, not written. */ tunnel->sleep_lost = restarted; ret = tb_tunnel_wake(tunnel); - if (ret) - goto fail; + if (ret) { + /* Only a wake can lose a router, and only behind a host that answers. */ + if (!host_pci_lost || + tb_sw_read(tb->root_switch, &id, TB_CFG_SWITCH, 0, 1) || + !tb_cio_retire_gone(tb->root_switch)) + goto fail; + tb_warn(tb, "CIO wake: a router left during the tunnel replay (%d)\n", ret); + goto replay; + } tb_cio_test_usb3_back(tb, tunnel, restarted); /* TEST */ } tb_switch_enter_redrive(tb->root_switch); diff --git a/tools/asahi/tb-cio-sleep-tests.c b/tools/asahi/tb-cio-sleep-tests.c index 233c774282eeb..38543ae0b1fbd 100644 --- a/tools/asahi/tb-cio-sleep-tests.c +++ b/tools/asahi/tb-cio-sleep-tests.c @@ -29,6 +29,7 @@ static bool of_machine_is_compatible(const char *compat) #define TB_PATH_MAX_HOPS 16 #define TB_CFG_HOPS 1 #define TB_CFG_PORT 2 +#define TB_CFG_SWITCH 3 #define ADP_CS_4 4 #define ADP_USB3_CS_0 0 #define ADP_USB3_CS_0_PE BIT(31) @@ -52,6 +53,8 @@ static inline void tb_warn(void *tb, const char *fmt, ...) #define tb_tunnel_info(t, ...) ((void)(t)) struct tb_port; static bool dock_lost; +/* Gone since the router resume: the dock, and the host router itself. */ +static bool dock_gone, host_gone; static int link_waits, enumerated; static int tb_wait_for_port(struct tb_port *p, bool w) { @@ -397,6 +400,20 @@ static int tb_switch_resume(struct tb_switch *s, bool runtime) child.is_unplugged = dock_lost; return 0; } + +static int tb_sw_read(struct tb_switch *s, void *value, int space, int off, int count) +{ + assert(physical && ctl && tb.lock && !s->is_unplugged); + assert(space == TB_CFG_SWITCH && !off && count == 1); + *(u32 *)value = 0; + return (s == &root ? host_gone : dock_gone) ? -EIO : 0; +} + +static void tb_sw_set_unplugged(struct tb_switch *s) +{ + assert(tb.lock && s == &child && !s->is_unplugged); + s->is_unplugged = true; +} static void tb_restore_children(struct tb_switch *s) { assert(physical && ctl); @@ -489,6 +506,7 @@ static struct tb_port xports[2]; static int xd_unconfigured; #define tb_port_dbg(p, ...) ((void)(p)) #define tb_port_info(p, ...) ((void)(p)) +#define tb_port_warn(p, ...) ((void)(p)) static bool tb_is_upstream_port(struct tb_port *p) { return p == &link[1]; @@ -675,6 +693,8 @@ static void setup(void) nth = 1; physical = ctl = clients_awake = true; uid_wrong = dock_lost = false; + dock_gone = false; + host_gone = false; link_waits = router_wakes = enumerated = 0; } static void inject(int point, int count) @@ -818,6 +838,42 @@ int main(void) assert(cm.resume_cleanup_pending); cases++; } + /* + * A dock that leaves during the replay, before its first tunnel or after it, is an + * unplug as well. A replay that fails with the dock or the host router in any other + * state, or in the suspend's unwind, fails the wake. + */ + for (int n = 0; n < 6; n++) { + bool pci_lost = false; + + setup(); + assert(!tb_cio_sleep_prepare(&tb)); + clients_awake = false; + assert(!tb_cio_sleep(&tb)); + poweroff(); + physical = true; + dock_gone = n != 2; + host_gone = n == 3; + tunnels[0].pci = n != 5; + /* Point 4, adapter and hop writes: the first tunnel, then the second one. */ + inject(4, n == 1 ? 6 : 1); + if (n >= 2 && n <= 4) { + assert(tb_cio_wake(&tb, n == 4 ? NULL : &pci_lost) == -EIO); + assert(cm.cio_changed && !child.is_unplugged && !pci_lost); + dock_gone = false; + host_gone = false; + assert(!tb_cio_wake(&tb, &pci_lost) && !pci_lost); + check_restored(); + } else { + assert(!tb_cio_wake(&tb, &pci_lost)); + assert(pci_lost == (n != 5) && child.is_unplugged && resumes == 1); + assert(!cm.cio_changed && !cm.cio_ctl_stopped && !cm.cio_router.valid); + assert(tunnels[0].sleep_changed == (n != 1) && tunnels[1].sleep_changed); + assert(adapters == (n == 1)); + } + complete(); + cases++; + } /* Before the sleep a lost router refuses it. */ setup(); child.is_unplugged = true; diff --git a/tools/asahi/tb-cio-sleep-tests.py b/tools/asahi/tb-cio-sleep-tests.py index 9a33f628d611d..00bae7702c052 100644 --- a/tools/asahi/tb-cio-sleep-tests.py +++ b/tools/asahi/tb-cio-sleep-tests.py @@ -47,6 +47,12 @@ def main(): 'replay-lost-tunnel': ('if (tb_tunnel_is_invalid(tunnel)) {', 'if (false) {'), 'hide-lost-pci': ('*host_pci_lost = true;', '(void)host_pci_lost;'), 'lost-router-topology': ('if (wake && port->remote->sw->is_unplugged)', 'if (false)'), + 'fail-on-left-router': ('!tb_cio_retire_gone(tb->root_switch))', 'true)'), + 'retire-answering-router': ('if (!tb_sw_read(peer, &id, TB_CFG_SWITCH, 0, 1)) {', + 'if (tb_sw_read(peer, &id, TB_CFG_SWITCH, 0, 1) > 0) {'), + 'retire-behind-dead-host': ('tb_sw_read(tb->root_switch, &id, TB_CFG_SWITCH, 0, 1) ||', + 'tb_sw_read(tb->root_switch, &id, TB_CFG_SWITCH, 0, 1) > 0 ||'), + 'retire-in-unwind': ('if (!host_pci_lost ||\n', 'if (false ||\n'), 'restart-needs-saved-state': ('restarted = tcm->cio_host_slept;', 'restarted = tcm->cio_router.valid;'), 'unwind-unsaved-sleep': ('tb_warn(tb, "CIO router state not saved: %d\\n", ret);',