From 84abdc192a8d1c23913e3f5d39faa53eb4649eec Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:44:30 +1100 Subject: [PATCH 1/6] thunderbolt: apple: restart a port that gets no USB4 link after start The router firmware gives up on a link whose sideband drops during training: it logs "SBX disconnected!", enters its Dead End state and waits for the host to power the port off ("ACIO was not powered off after N seconds in Dead End state"). Nothing tells the driver, so the port stays dead until the cable is replugged. The J616s (T6040) gets there on about every second reboot while the M2 on the other end of the cable is still recovering from the dropped link: the M2's router retrains its lanes a minute later, but the J616s never tries again. Check the domain 20 s after a start with a USB4/Thunderbolt cable. When no remote discovery exchange has happened since the start, power the port off and start it again, up to five times per cable connection. A cable unplug resets the count. Can be turned off with link_watch=N. Link: https://github.com/aurora-silicon/linux/issues/1230 Assisted-by: LLM Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- drivers/thunderbolt/apple.c | 81 ++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index ba7ec0bc0296c..682a14396e3fb 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -217,6 +217,21 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { #define APPLE_CIO_SLEEP_RESTART_FALLBACK_MS 10000 /* How long a forced cable cycle may take before it no longer counts. */ #define APPLE_CIO_RESUME_RECOVERY_WINDOW_MS 30000 +/* A started port with a USB4/TBT cable and no remote discovery by then is restarted. */ +#define APPLE_CIO_LINK_WATCH_MS 20000 +#define APPLE_CIO_LINK_WATCH_RESTARTS 5 + +/* + * The router firmware gives up on a link whose sideband drops during + * training ("Dead End") and waits for the host to power the port off. + * A partner whose router is still recovering from our last disconnect + * (a reboot of this Mac, a sleep) does exactly that. Restart such a + * port a few times until a partner answers. + */ +static bool link_watch = true; +module_param(link_watch, bool, 0644); +MODULE_PARM_DESC(link_watch, + "Restart a port that has a USB4/Thunderbolt cable but no link 20 s after start (default Y)"); static bool resume_recovery; module_param(resume_recovery, bool, 0644); @@ -325,6 +340,9 @@ struct apple_cio { bool resume_recovery_failed; /* terminal recovery failure */ bool resume_link_failed; /* protected by lock */ bool resume_link_pending; /* set by NHI resume, consumed by ACIO complete */ + struct delayed_work link_watch_work; + u64 link_watch_activity; /* protected by lock: remote activity at start */ + unsigned int link_watch_restarts; /* protected by lock */ struct notifier_block pm_notifier; struct delayed_work sleep_restart_work; enum { @@ -2617,6 +2635,59 @@ static int apple_cio_lstx_release(struct apple_cio *acio) * dropping the power-domain links. The Type-C PHY must remain configured * until this function returns. The caller must hold @acio->lock. */ +/* Remote discovery exchanges on this port's domain so far, or 0 without an NHI. */ +static u64 apple_cio_remote_activity(struct apple_cio *acio) +{ + struct platform_device *pdev = READ_ONCE(acio->nhi_pdev); + struct apple_nhi *anhi; + u64 activity = 0; + + lockdep_assert_held(&acio->lock); + if (!pdev) + return 0; + device_lock(&pdev->dev); + anhi = platform_get_drvdata(pdev); + if (pdev->dev.driver && anhi && anhi->tb) + activity = atomic64_read(&anhi->tb->remote_activity); + device_unlock(&pdev->dev); + return activity; +} + +static int apple_cio_start(struct apple_cio *acio); + +static void apple_cio_stop(struct apple_cio *acio); + +static void apple_cio_link_watch_work(struct work_struct *work) +{ + struct apple_cio *acio = container_of(to_delayed_work(work), + struct apple_cio, link_watch_work); + int ret; + + guard(mutex)(&acio->lock); + if (!READ_ONCE(link_watch) || acio->removing || + !acio->current_cable_info || + acio->current_cable_info != acio->target_cable_info || + acio->sleep_phase != APPLE_CIO_SLEEP_OPEN) + return; + if (apple_cio_remote_activity(acio) != acio->link_watch_activity) { + acio->link_watch_restarts = 0; + return; + } + if (acio->link_watch_restarts >= APPLE_CIO_LINK_WATCH_RESTARTS) { + dev_warn(acio->dev, "no USB4 link after %u port restarts, leaving the port as it is\n", + acio->link_watch_restarts); + return; + } + acio->link_watch_restarts++; + dev_info(acio->dev, "no USB4 link %u s after start, restarting the port (%u/%u)\n", + APPLE_CIO_LINK_WATCH_MS / 1000, acio->link_watch_restarts, + APPLE_CIO_LINK_WATCH_RESTARTS); + apple_cio_stop(acio); + ret = apple_cio_start(acio); + if (ret) + dev_err(acio->dev, "port restart failed: %d\n", ret); +} + static void apple_cio_stop(struct apple_cio *acio) { int ret, i; @@ -2866,6 +2937,10 @@ static int apple_cio_start(struct apple_cio *acio) } acio->current_cable_info = acio->target_cable_info; + acio->link_watch_activity = apple_cio_remote_activity(acio); + if (READ_ONCE(link_watch)) + mod_delayed_work(system_freezable_wq, &acio->link_watch_work, + msecs_to_jiffies(APPLE_CIO_LINK_WATCH_MS)); return 0; err_depopulate: @@ -3002,8 +3077,9 @@ static int apple_cio_prepare(struct device *dev) { struct apple_cio *acio = dev_get_drvdata(dev); - /* Drain before taking lock; the worker acquires it to check the link. */ + /* Drain before taking lock; the workers acquire it to check the link. */ cancel_delayed_work_sync(&acio->resume_link_work); + cancel_delayed_work_sync(&acio->link_watch_work); scoped_guard(mutex, &acio->lock) { WRITE_ONCE(acio->resume_link_pending, false); acio->resume_link_failed = false; @@ -3066,6 +3142,7 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw, switch (data->state) { case TYPEC_THUNDERBOLT_SWITCH_OFF: acio->target_cable_info = 0; + acio->link_watch_restarts = 0; break; case TYPEC_THUNDERBOLT_SWITCH_TBT: acio->target_cable_info = TB_VSE_CAP_APPLE_CABLE_INFO_PRESENT; @@ -3292,6 +3369,7 @@ static int apple_cio_probe(struct platform_device *pdev) INIT_DELAYED_WORK(&acio->pcie_tunnel_work, apple_cio_pcie_tunnel_work); INIT_DELAYED_WORK(&acio->resume_link_work, apple_cio_resume_link_work); + INIT_DELAYED_WORK(&acio->link_watch_work, apple_cio_link_watch_work); INIT_DELAYED_WORK(&acio->sleep_restart_work, apple_cio_sleep_restart_work); init_completion(&acio->nhi_boot_completion); acio->dev = &pdev->dev; @@ -3492,6 +3570,7 @@ static void apple_cio_remove(struct platform_device *pdev) disable_delayed_work_sync(&acio->sleep_restart_work); disable_delayed_work_sync(&acio->resume_link_work); + disable_delayed_work_sync(&acio->link_watch_work); /* The worker can notify this switch; stop current and future enqueues. */ disable_delayed_work_sync(&acio->pcie_tunnel_work); /* A set may hold the switch read lock while waiting for acio->lock. */ From 2eb9810a907ec0e5f45329ca0b8da5cc1d24c75d Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:41:00 +1100 Subject: [PATCH 2/6] thunderbolt: apple: keep watching a USB4 port for a lost link partner The first version only looked once, 20 s after a start. The J616s also ends up in its router's Dead End state when the partner goes away while linked: the M2 rebooting gave 'host disconnected' and then 'Port Group - entering Dead End state', and the link never came back after the M2 was up again. Check every 20 s while a USB4/Thunderbolt cable is applied, and also after resume, whether the domain has a partner (a device router or an XDomain host on a root lane adapter). Without one, restart the port: five times 20 s apart, then every 120 s for as long as the cable is in. A partner found resets the count. Assisted-by: LLM Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> --- drivers/thunderbolt/apple.c | 95 ++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 682a14396e3fb..b70376cd44293 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -217,8 +217,9 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { #define APPLE_CIO_SLEEP_RESTART_FALLBACK_MS 10000 /* How long a forced cable cycle may take before it no longer counts. */ #define APPLE_CIO_RESUME_RECOVERY_WINDOW_MS 30000 -/* A started port with a USB4/TBT cable and no remote discovery by then is restarted. */ +/* A started port with a USB4/TBT cable and no link partner is restarted. */ #define APPLE_CIO_LINK_WATCH_MS 20000 +#define APPLE_CIO_LINK_WATCH_SLOW_MS 120000 #define APPLE_CIO_LINK_WATCH_RESTARTS 5 /* @@ -231,7 +232,7 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { static bool link_watch = true; module_param(link_watch, bool, 0644); MODULE_PARM_DESC(link_watch, - "Restart a port that has a USB4/Thunderbolt cable but no link 20 s after start (default Y)"); + "Restart a port that has a USB4/Thunderbolt cable but no link partner (checked every 20 s; default Y)"); static bool resume_recovery; module_param(resume_recovery, bool, 0644); @@ -341,7 +342,6 @@ struct apple_cio { bool resume_link_failed; /* protected by lock */ bool resume_link_pending; /* set by NHI resume, consumed by ACIO complete */ struct delayed_work link_watch_work; - u64 link_watch_activity; /* protected by lock: remote activity at start */ unsigned int link_watch_restarts; /* protected by lock */ struct notifier_block pm_notifier; struct delayed_work sleep_restart_work; @@ -2635,22 +2635,39 @@ static int apple_cio_lstx_release(struct apple_cio *acio) * dropping the power-domain links. The Type-C PHY must remain configured * until this function returns. The caller must hold @acio->lock. */ -/* Remote discovery exchanges on this port's domain so far, or 0 without an NHI. */ -static u64 apple_cio_remote_activity(struct apple_cio *acio) +/* + * Whether this port's domain has a partner: a device router or an XDomain + * host behind one of the root switch's lane adapters. -EAGAIN without an NHI. + */ +static int apple_cio_has_partner(struct apple_cio *acio) { struct platform_device *pdev = READ_ONCE(acio->nhi_pdev); struct apple_nhi *anhi; - u64 activity = 0; + struct tb_port *port; + int ret = -EAGAIN; lockdep_assert_held(&acio->lock); if (!pdev) - return 0; + return -EAGAIN; device_lock(&pdev->dev); anhi = platform_get_drvdata(pdev); - if (pdev->dev.driver && anhi && anhi->tb) - activity = atomic64_read(&anhi->tb->remote_activity); + if (!pdev->dev.driver || !anhi || !anhi->tb || !anhi->tb->root_switch) + goto unlock; + ret = 0; + scoped_guard(mutex, &anhi->tb->lock) { + tb_switch_for_each_port(anhi->tb->root_switch, port) { + if (!tb_port_is_null(port)) + continue; + if (port->remote || + (port->xdomain && !port->xdomain->is_unplugged)) { + ret = 1; + break; + } + } + } +unlock: device_unlock(&pdev->dev); - return activity; + return ret; } static int apple_cio_start(struct apple_cio *acio); @@ -2661,31 +2678,45 @@ static void apple_cio_link_watch_work(struct work_struct *work) { struct apple_cio *acio = container_of(to_delayed_work(work), struct apple_cio, link_watch_work); + unsigned int next = APPLE_CIO_LINK_WATCH_MS; int ret; guard(mutex)(&acio->lock); if (!READ_ONCE(link_watch) || acio->removing || !acio->current_cable_info || - acio->current_cable_info != acio->target_cable_info || - acio->sleep_phase != APPLE_CIO_SLEEP_OPEN) - return; - if (apple_cio_remote_activity(acio) != acio->link_watch_activity) { - acio->link_watch_restarts = 0; - return; - } - if (acio->link_watch_restarts >= APPLE_CIO_LINK_WATCH_RESTARTS) { - dev_warn(acio->dev, "no USB4 link after %u port restarts, leaving the port as it is\n", - acio->link_watch_restarts); + acio->current_cable_info != acio->target_cable_info) return; - } + if (acio->sleep_phase != APPLE_CIO_SLEEP_OPEN) + goto rearm; + + ret = apple_cio_has_partner(acio); + if (ret) { + /* A partner, or no NHI yet: look again later. */ + if (ret > 0) + acio->link_watch_restarts = 0; + goto rearm; + } + + /* Past the fast attempts, keep trying slowly: the partner may come back. */ + if (acio->link_watch_restarts >= APPLE_CIO_LINK_WATCH_RESTARTS) + next = APPLE_CIO_LINK_WATCH_SLOW_MS; + if (acio->link_watch_restarts == APPLE_CIO_LINK_WATCH_RESTARTS) + dev_warn(acio->dev, "no USB4 link after %u port restarts, retrying every %u s\n", + acio->link_watch_restarts, APPLE_CIO_LINK_WATCH_SLOW_MS / 1000); acio->link_watch_restarts++; - dev_info(acio->dev, "no USB4 link %u s after start, restarting the port (%u/%u)\n", - APPLE_CIO_LINK_WATCH_MS / 1000, acio->link_watch_restarts, - APPLE_CIO_LINK_WATCH_RESTARTS); + if (acio->link_watch_restarts <= APPLE_CIO_LINK_WATCH_RESTARTS) + dev_info(acio->dev, "USB4 cable but no link partner, restarting the port (%u/%u)\n", + acio->link_watch_restarts, APPLE_CIO_LINK_WATCH_RESTARTS); apple_cio_stop(acio); - ret = apple_cio_start(acio); - if (ret) - dev_err(acio->dev, "port restart failed: %d\n", ret); + ret = apple_cio_start(acio); /* re-arms the watch on success */ + if (!ret) + return; + dev_err(acio->dev, "port restart failed: %d\n", ret); + return; + +rearm: + mod_delayed_work(system_freezable_wq, &acio->link_watch_work, + msecs_to_jiffies(next)); } static void apple_cio_stop(struct apple_cio *acio) @@ -2937,10 +2968,12 @@ static int apple_cio_start(struct apple_cio *acio) } acio->current_cable_info = acio->target_cable_info; - acio->link_watch_activity = apple_cio_remote_activity(acio); if (READ_ONCE(link_watch)) mod_delayed_work(system_freezable_wq, &acio->link_watch_work, - msecs_to_jiffies(APPLE_CIO_LINK_WATCH_MS)); + msecs_to_jiffies(acio->link_watch_restarts > + APPLE_CIO_LINK_WATCH_RESTARTS ? + APPLE_CIO_LINK_WATCH_SLOW_MS : + APPLE_CIO_LINK_WATCH_MS)); return 0; err_depopulate: @@ -3096,6 +3129,10 @@ static void apple_cio_complete(struct device *dev) guard(mutex)(&acio->lock); if (acio->sleep_phase == APPLE_CIO_SLEEP_CLOSED) acio->sleep_phase = APPLE_CIO_SLEEP_RESUMED; + /* Watch the link again; prepare stopped the watch for the sleep. */ + if (READ_ONCE(link_watch) && acio->current_cable_info && !acio->removing) + mod_delayed_work(system_freezable_wq, &acio->link_watch_work, + msecs_to_jiffies(APPLE_CIO_LINK_WATCH_MS)); if (!READ_ONCE(acio->resume_link_pending)) return; WRITE_ONCE(acio->resume_link_pending, false); From 1caac089565f9f68195664936f5a6c123806be6e Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 23:04:45 +1100 Subject: [PATCH 3/6] thunderbolt: apple: Recheck the peer after a port restart After an awake USB4 link loss, the host restarts ACIO and the NHI. If the new domain comes up without discovering a peer, there is no unplug notification to schedule the next attempt. A K114s link loss exhausted only the first of the available restart attempts this way. Remember the cable parameters for a short recovery window and check for a peer after a successful restart with the same parameters. Schedule the existing generation-checked worker and keep its three-attempt budget. A system sleep cancels the outstanding awake check. Signed-off-by: Ryan Murray --- drivers/thunderbolt/apple.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index cb3a94977182a..6930ac9f221b3 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -354,6 +354,7 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { */ #define APPLE_CIO_LINK_LOST_MS 3000 #define APPLE_CIO_LINK_LOST_CYCLES 3 +#define APPLE_CIO_LINK_LOST_RESTART_MS 30000 static bool resume_recovery; module_param(resume_recovery, bool, 0644); @@ -571,6 +572,8 @@ struct apple_cio { unsigned int link_lost_cycles; /* protected by lock */ u64 link_lost_cycle_activity; /* remote activity of this domain at the last cycle */ bool link_lost_restart; /* protected by lock: cycle requested */ + u32 link_lost_retry_cable; /* cable info before the requested cycle */ + unsigned long link_lost_retry_until; struct notifier_block pm_notifier; struct notifier_block reboot_notifier; /* @@ -3967,6 +3970,9 @@ static void apple_cio_link_lost_work(struct work_struct *work) acio->link_lost_cycles++; WRITE_ONCE(acio->link_lost_cycle_activity, activity); acio->link_lost_restart = true; + acio->link_lost_retry_cable = acio->current_cable_info; + acio->link_lost_retry_until = jiffies + + msecs_to_jiffies(APPLE_CIO_LINK_LOST_RESTART_MS); dev_info(acio->dev, "peer link lost with the cable attached, restarting the port (%u/%u)\n", acio->link_lost_cycles, APPLE_CIO_LINK_LOST_CYCLES); @@ -4029,6 +4035,7 @@ static int apple_cio_prepare(struct device *dev) acio->resume_link_failed = false; /* A cycle CD321x did not take up is not carried into this sleep. */ acio->resume_recovery_active = false; + acio->link_lost_retry_cable = 0; } return 0; } @@ -4172,16 +4179,18 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw, if (acio->resume_recovery_failed) return -EHOSTDOWN; ret = apple_cio_start(acio); - if (!ret) + if (!ret) { + bool retry = acio->link_lost_retry_cable == acio->current_cable_info && + time_before(jiffies, acio->link_lost_retry_until); + WRITE_ONCE(acio->sleep_restart_owed, false); - /* - * The port had a dock before its power-off for the sleep: one that is not - * found again gets the port restarted, as a peer lost with the cable in. - */ - if (!ret && acio->sleep_peer_wanted) { - WRITE_ONCE(acio->link_lost_gen, READ_ONCE(acio->start_gen)); - mod_delayed_work(system_freezable_wq, &acio->link_lost_work, - msecs_to_jiffies(APPLE_CIO_LINK_LOST_MS)); + acio->link_lost_retry_cable = 0; + /* An absent peer cannot generate a new link-loss event. */ + if (retry || acio->sleep_peer_wanted) { + WRITE_ONCE(acio->link_lost_gen, READ_ONCE(acio->start_gen)); + mod_delayed_work(system_freezable_wq, &acio->link_lost_work, + msecs_to_jiffies(APPLE_CIO_LINK_LOST_MS)); + } } if (ret && apple_cio_recovering(acio)) { acio->resume_recovery_failed = true; From 26de1f3f962a0664daac381ce08831174210eac0 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 10:19:20 +1100 Subject: [PATCH 4/6] thunderbolt: notify host link loss before peer discovery A host router lane adapter can report unplug events while its link is still training, before any peer was discovered. The J616s sent three of them while the router firmware gave up on the link and entered its Dead End state. The handler dropped those events, so the Apple NHI never got the link-loss callback that restarts the port. Pass them to the existing host link-loss hook as well. Only host router null adapters reach the callback, and the NHI's own restart budget applies. Signed-off-by: Ryan Murray --- drivers/thunderbolt/tb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 41d9bdc44e8df..7818957a522c1 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c @@ -3403,7 +3403,9 @@ static void tb_handle_hotplug(struct work_struct *work) tb_switch_xhci_disconnect(sw); } else { tb_port_dbg(port, - "got unplug event for disconnected port, ignoring\n"); + "got unplug event for disconnected port\n"); + /* Link training can fail before a peer is discovered. */ + tb_host_link_lost(tb, port); } } else if (port->remote) { tb_port_dbg(port, "got plug event for connected port, ignoring\n"); From 7fad8caa43455e7eb80aa24db5dc48f2185a30af Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sun, 29 Nov 2026 10:20:14 +1100 Subject: [PATCH 5/6] thunderbolt: apple: keep retrying a port whose peer stays away After a peer drops the link with the cable still attached, the port is restarted three times, 3 s apart, and then left alone. A partner that is rebooting answers later than that: a J616s whose M2 partner rebooted stayed in the router firmware's Dead End state until the cable was replugged, although the M2 was back a minute later. Once the three restarts are spent, keep restarting the port every 120 s for as long as the cable stays attached. A found peer, a cable change or a system sleep ends the slow retry; link_slow_retry=N restores the old behaviour. Link: https://github.com/aurora-silicon/linux/issues/1230 Signed-off-by: Ryan Murray --- drivers/thunderbolt/apple.c | 59 ++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 6930ac9f221b3..0c3a0bb5ee8f4 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -355,6 +355,18 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { #define APPLE_CIO_LINK_LOST_MS 3000 #define APPLE_CIO_LINK_LOST_CYCLES 3 #define APPLE_CIO_LINK_LOST_RESTART_MS 30000 +/* Past the budget, a port with the cable still in is retried this often. */ +#define APPLE_CIO_LINK_LOST_SLOW_MS 120000 + +/* + * A partner that is still rebooting (another Mac, a dock re-powering) + * answers after the fast restarts are spent, and the router firmware then + * keeps the port in its Dead End state until the cable is replugged. + */ +static bool link_slow_retry = true; +module_param(link_slow_retry, bool, 0644); +MODULE_PARM_DESC(link_slow_retry, + "After the fast port restarts for a lost USB4 peer, keep restarting the port every 120 s while the cable stays attached (default Y)"); static bool resume_recovery; module_param(resume_recovery, bool, 0644); @@ -574,6 +586,8 @@ struct apple_cio { bool link_lost_restart; /* protected by lock: cycle requested */ u32 link_lost_retry_cable; /* cable info before the requested cycle */ unsigned long link_lost_retry_until; + bool link_lost_slow; /* protected by lock: past the fast budget */ + bool link_lost_slow_due; /* protected by lock: the slow wait is over */ struct notifier_block pm_notifier; struct notifier_block reboot_notifier; /* @@ -3954,28 +3968,50 @@ static void apple_cio_link_lost_work(struct work_struct *work) if (acio->sleep_peer_wanted) dev_info(acio->dev, "T6050 sleep: the dock is back after the port's restart (%u more restart(s))\n", acio->link_lost_cycles); + if (acio->link_lost_slow) + dev_info(acio->dev, "peer link back after a slow port restart\n"); acio->sleep_peer_wanted = false; + acio->link_lost_slow = false; + acio->link_lost_slow_due = false; return; } if (acio->sleep_peer_wanted) dev_warn(acio->dev, "T6050 sleep: no dock %d ms after the port's restart\n", APPLE_CIO_LINK_LOST_MS); if (acio->link_lost_cycles >= APPLE_CIO_LINK_LOST_CYCLES) { - dev_warn(acio->dev, - "peer link still down after %u port restarts, leaving the port alone\n", - acio->link_lost_cycles); acio->sleep_peer_wanted = false; - return; + if (!READ_ONCE(link_slow_retry)) { + dev_warn(acio->dev, + "peer link still down after %u port restarts, leaving the port alone\n", + acio->link_lost_cycles); + return; + } + if (!acio->link_lost_slow_due) { + if (!acio->link_lost_slow) + dev_warn(acio->dev, + "peer link still down after %u port restarts, retrying every %u s while the cable is attached\n", + acio->link_lost_cycles, + APPLE_CIO_LINK_LOST_SLOW_MS / 1000); + acio->link_lost_slow = true; + acio->link_lost_slow_due = true; + mod_delayed_work(system_freezable_wq, &acio->link_lost_work, + msecs_to_jiffies(APPLE_CIO_LINK_LOST_SLOW_MS)); + return; + } + /* The slow wait is over: restart once more, outside the budget. */ + acio->link_lost_slow_due = false; + } else { + acio->link_lost_cycles++; } - acio->link_lost_cycles++; WRITE_ONCE(acio->link_lost_cycle_activity, activity); acio->link_lost_restart = true; acio->link_lost_retry_cable = acio->current_cable_info; acio->link_lost_retry_until = jiffies + msecs_to_jiffies(APPLE_CIO_LINK_LOST_RESTART_MS); - dev_info(acio->dev, - "peer link lost with the cable attached, restarting the port (%u/%u)\n", - acio->link_lost_cycles, APPLE_CIO_LINK_LOST_CYCLES); + if (!acio->link_lost_slow) + dev_info(acio->dev, + "peer link lost with the cable attached, restarting the port (%u/%u)\n", + acio->link_lost_cycles, APPLE_CIO_LINK_LOST_CYCLES); notify = true; } @@ -4036,6 +4072,9 @@ static int apple_cio_prepare(struct device *dev) /* A cycle CD321x did not take up is not carried into this sleep. */ acio->resume_recovery_active = false; acio->link_lost_retry_cable = 0; + /* The sleep cancelled the slow retry; the wake starts over. */ + acio->link_lost_slow = false; + acio->link_lost_slow_due = false; } return 0; } @@ -4093,6 +4132,10 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw, switch (data->state) { case TYPEC_THUNDERBOLT_SWITCH_OFF: acio->target_cable_info = 0; + /* A new cable gets a fresh restart budget. */ + WRITE_ONCE(acio->link_lost_cycles, 0); + acio->link_lost_slow = false; + acio->link_lost_slow_due = false; break; case TYPEC_THUNDERBOLT_SWITCH_TBT: acio->target_cable_info = TB_VSE_CAP_APPLE_CABLE_INFO_PRESENT; From 8fc9efe40a9f872afa641e36c6f600efb122bed1 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 5 Dec 2026 13:10:38 +1100 Subject: [PATCH 6/6] Revert "thunderbolt: apple: keep retrying a port whose peer stays away" This reverts commit 7fad8caa43455e. The slow retry adds a link_slow_retry module parameter, which new code may not add, and no failure that main does not already recover has been captured for it: on 11-28 a J473 partner reboot on the J616s recovered on main by itself in about 31 s. If a reproducer turns up, the slow retry comes back inside the port recovery itself, without a switch. Signed-off-by: Ryan Murray --- drivers/thunderbolt/apple.c | 59 +++++-------------------------------- 1 file changed, 8 insertions(+), 51 deletions(-) diff --git a/drivers/thunderbolt/apple.c b/drivers/thunderbolt/apple.c index 0c3a0bb5ee8f4..6930ac9f221b3 100644 --- a/drivers/thunderbolt/apple.c +++ b/drivers/thunderbolt/apple.c @@ -355,18 +355,6 @@ static const struct apple_cio_hw apple_cio_t6040_hw = { #define APPLE_CIO_LINK_LOST_MS 3000 #define APPLE_CIO_LINK_LOST_CYCLES 3 #define APPLE_CIO_LINK_LOST_RESTART_MS 30000 -/* Past the budget, a port with the cable still in is retried this often. */ -#define APPLE_CIO_LINK_LOST_SLOW_MS 120000 - -/* - * A partner that is still rebooting (another Mac, a dock re-powering) - * answers after the fast restarts are spent, and the router firmware then - * keeps the port in its Dead End state until the cable is replugged. - */ -static bool link_slow_retry = true; -module_param(link_slow_retry, bool, 0644); -MODULE_PARM_DESC(link_slow_retry, - "After the fast port restarts for a lost USB4 peer, keep restarting the port every 120 s while the cable stays attached (default Y)"); static bool resume_recovery; module_param(resume_recovery, bool, 0644); @@ -586,8 +574,6 @@ struct apple_cio { bool link_lost_restart; /* protected by lock: cycle requested */ u32 link_lost_retry_cable; /* cable info before the requested cycle */ unsigned long link_lost_retry_until; - bool link_lost_slow; /* protected by lock: past the fast budget */ - bool link_lost_slow_due; /* protected by lock: the slow wait is over */ struct notifier_block pm_notifier; struct notifier_block reboot_notifier; /* @@ -3968,50 +3954,28 @@ static void apple_cio_link_lost_work(struct work_struct *work) if (acio->sleep_peer_wanted) dev_info(acio->dev, "T6050 sleep: the dock is back after the port's restart (%u more restart(s))\n", acio->link_lost_cycles); - if (acio->link_lost_slow) - dev_info(acio->dev, "peer link back after a slow port restart\n"); acio->sleep_peer_wanted = false; - acio->link_lost_slow = false; - acio->link_lost_slow_due = false; return; } if (acio->sleep_peer_wanted) dev_warn(acio->dev, "T6050 sleep: no dock %d ms after the port's restart\n", APPLE_CIO_LINK_LOST_MS); if (acio->link_lost_cycles >= APPLE_CIO_LINK_LOST_CYCLES) { + dev_warn(acio->dev, + "peer link still down after %u port restarts, leaving the port alone\n", + acio->link_lost_cycles); acio->sleep_peer_wanted = false; - if (!READ_ONCE(link_slow_retry)) { - dev_warn(acio->dev, - "peer link still down after %u port restarts, leaving the port alone\n", - acio->link_lost_cycles); - return; - } - if (!acio->link_lost_slow_due) { - if (!acio->link_lost_slow) - dev_warn(acio->dev, - "peer link still down after %u port restarts, retrying every %u s while the cable is attached\n", - acio->link_lost_cycles, - APPLE_CIO_LINK_LOST_SLOW_MS / 1000); - acio->link_lost_slow = true; - acio->link_lost_slow_due = true; - mod_delayed_work(system_freezable_wq, &acio->link_lost_work, - msecs_to_jiffies(APPLE_CIO_LINK_LOST_SLOW_MS)); - return; - } - /* The slow wait is over: restart once more, outside the budget. */ - acio->link_lost_slow_due = false; - } else { - acio->link_lost_cycles++; + return; } + acio->link_lost_cycles++; WRITE_ONCE(acio->link_lost_cycle_activity, activity); acio->link_lost_restart = true; acio->link_lost_retry_cable = acio->current_cable_info; acio->link_lost_retry_until = jiffies + msecs_to_jiffies(APPLE_CIO_LINK_LOST_RESTART_MS); - if (!acio->link_lost_slow) - dev_info(acio->dev, - "peer link lost with the cable attached, restarting the port (%u/%u)\n", - acio->link_lost_cycles, APPLE_CIO_LINK_LOST_CYCLES); + dev_info(acio->dev, + "peer link lost with the cable attached, restarting the port (%u/%u)\n", + acio->link_lost_cycles, APPLE_CIO_LINK_LOST_CYCLES); notify = true; } @@ -4072,9 +4036,6 @@ static int apple_cio_prepare(struct device *dev) /* A cycle CD321x did not take up is not carried into this sleep. */ acio->resume_recovery_active = false; acio->link_lost_retry_cable = 0; - /* The sleep cancelled the slow retry; the wake starts over. */ - acio->link_lost_slow = false; - acio->link_lost_slow_due = false; } return 0; } @@ -4132,10 +4093,6 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw, switch (data->state) { case TYPEC_THUNDERBOLT_SWITCH_OFF: acio->target_cable_info = 0; - /* A new cable gets a fresh restart budget. */ - WRITE_ONCE(acio->link_lost_cycles, 0); - acio->link_lost_slow = false; - acio->link_lost_slow_due = false; break; case TYPEC_THUNDERBOLT_SWITCH_TBT: acio->target_cable_info = TB_VSE_CAP_APPLE_CABLE_INFO_PRESENT;