diff --git a/drivers/thunderbolt/tb-cio-sleep.c b/drivers/thunderbolt/tb-cio-sleep.c index 1102fb8fe7405..1111d17851966 100644 --- a/drivers/thunderbolt/tb-cio-sleep.c +++ b/drivers/thunderbolt/tb-cio-sleep.c @@ -172,17 +172,20 @@ EXPORT_SYMBOL_FOR_MODULES(tb_cio_sleep_prepare, "thunderbolt_apple"); static int tb_cio_wait_links(struct tb *tb) { /* - * This runs inside the system's resume. A host that asks for it waits two rounds, a - * little over 2 s: a cold start's training takes 0.45 s from the cable word, and a - * router woken from its own sleep has its retimer's answer 23 ms after it (macOS). + * This runs inside the system's resume. Two rounds, a little over 2 s, outlast a cold + * start's training: it takes 0.45 s from the cable word, and a router woken from its + * own sleep has its retimer's answer 23 ms after it (macOS). A host that asks for it + * waits no longer than that for any peer. On the others a peer whose link still trains + * has 30 rounds, and only an adapter that no peer is seen on is given up after two. */ - unsigned int max = tb->nhi->quirks & QUIRK_CIO_WAKE_GIVES_UP ? 2 : 30; + const unsigned int cold = 2; + unsigned int max = tb->nhi->quirks & QUIRK_CIO_WAKE_GIVES_UP ? cold : 30; struct tb_port *port; int ret, lost = 0; tb_switch_for_each_port(tb->root_switch, port) { + unsigned int tries, unplugged = 0; ktime_t start = ktime_get(); - unsigned int tries; if (!tb_port_has_remote(port)) continue; @@ -190,6 +193,10 @@ static int tb_cio_wait_links(struct tb *tb) ret = tb_wait_for_port(port, true); if (ret) break; + if (tb_port_state(port) != TB_PORT_UNPLUGGED) + unplugged = 0; + else if (++unplugged == cold) + break; if (tries + 1 < max) msleep(100); } diff --git a/tools/asahi/tb-cio-sleep-tests.c b/tools/asahi/tb-cio-sleep-tests.c index 233c774282eeb..68aa646446e83 100644 --- a/tools/asahi/tb-cio-sleep-tests.c +++ b/tools/asahi/tb-cio-sleep-tests.c @@ -53,6 +53,9 @@ static inline void tb_warn(void *tb, const char *fmt, ...) struct tb_port; static bool dock_lost; static int link_waits, enumerated; +enum { TB_PORT_CONNECTING = 1, TB_PORT_UP = 2, TB_PORT_UNPLUGGED = 7 }; +/* Rounds the lost peer's adapter trains for, after @trains_after that see no peer. */ +static int trains_after, trains; static int tb_wait_for_port(struct tb_port *p, bool w) { (void)p; @@ -60,6 +63,15 @@ static int tb_wait_for_port(struct tb_port *p, bool w) link_waits++; return !dock_lost; } + +static int tb_port_state(struct tb_port *p) +{ + (void)p; + if (!dock_lost) + return TB_PORT_UP; + return link_waits > trains_after && link_waits <= trains_after + trains ? + TB_PORT_CONNECTING : TB_PORT_UNPLUGGED; +} static void msleep(unsigned int ms) { (void)ms; @@ -676,6 +688,8 @@ static void setup(void) physical = ctl = clients_awake = true; uid_wrong = dock_lost = false; link_waits = router_wakes = enumerated = 0; + trains_after = 0; + trains = 0; } static void inject(int point, int count) { @@ -887,20 +901,33 @@ int main(void) complete(); cases++; } - /* A dock that is not back is waited for 30 rounds, or 2 by a host that gives up. */ - for (int n = 0; n < 2; n++) { + /* + * A dock that is not back is waited for 30 rounds while its link trains, or 2 by a host + * that gives up; an adapter that sees no peer for 2 rounds in a row ends the wait. + */ + static const struct { + unsigned int quirks; + int trains_after, trains, waits; + } gone[] = { + { 0, 0, 30, 30 }, { QUIRK_CIO_WAKE_GIVES_UP, 0, 30, 2 }, + { 0, 0, 0, 2 }, { QUIRK_CIO_WAKE_GIVES_UP, 0, 0, 2 }, + { 0, 1, 30, 30 }, { 0, 1, 1, 4 }, { 0, 0, 5, 7 }, + }; + for (size_t n = 0; n < ARRAY_SIZE(gone); n++) { bool pci_lost = false; setup(); - nhi.quirks = n ? QUIRK_CIO_WAKE_GIVES_UP : 0; + nhi.quirks = gone[n].quirks; assert(!tb_cio_sleep_prepare(&tb)); clients_awake = false; assert(!tb_cio_sleep(&tb)); poweroff(); physical = true; dock_lost = true; + trains_after = gone[n].trains_after; + trains = gone[n].trains; assert(!tb_cio_wake(&tb, &pci_lost) && pci_lost && child.is_unplugged); - assert(link_waits == (n ? 2 : 30)); + assert(link_waits == gone[n].waits); complete(); cases++; } diff --git a/tools/asahi/tb-cio-sleep-tests.py b/tools/asahi/tb-cio-sleep-tests.py index 9a33f628d611d..9138f0d93e947 100644 --- a/tools/asahi/tb-cio-sleep-tests.py +++ b/tools/asahi/tb-cio-sleep-tests.py @@ -57,7 +57,10 @@ def main(): 'skip-pm-roles': ('ret = tb_switch_cio_pm_roles(port->remote->sw);', 'ret = 0;'), 'save-without-state': ('if (!(tb->nhi->quirks & QUIRK_CIO_NO_ROUTER_STATE)) {', 'if (true) {'), 'restore-failure-fatal': ('if (ret && !(tb->nhi->quirks & QUIRK_CIO_WAKE_GIVES_UP))', 'if (ret)'), - 'wait-links-long': ('QUIRK_CIO_WAKE_GIVES_UP ? 2 : 30', 'QUIRK_CIO_WAKE_GIVES_UP ? 30 : 30'), + 'wait-links-long': ('QUIRK_CIO_WAKE_GIVES_UP ? cold : 30', 'QUIRK_CIO_WAKE_GIVES_UP ? 30 : 30'), + 'wait-for-no-peer': ('else if (++unplugged == cold)', 'else if (++unplugged == cold + max)'), + 'give-up-training-peer': ('if (tb_port_state(port) != TB_PORT_UNPLUGGED)', 'if (false)'), + 'count-unplugged-rounds-apart': ('\t\t\t\tunplugged = 0;\n', '\t\t\t\t(void)0;\n'), 'drain-macos-paths': ('if (path->tb->nhi->quirks & QUIRK_CIO_MACOS_FRAMES)\n\t\treturn 0;', 'if (false)\n\t\treturn 0;'), 'rewrite-unslept-hops': ('!path->sleep_lost && observed.enable', 'false && observed.enable'),