From 748c27a5be2deda24ea8f2f14217827ba7d158fc Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sun, 22 Nov 2026 14:05:28 -0500 Subject: [PATCH 1/2] thunderbolt: preserve direct XDomain discovery across s2idle on awake Apple routers On Apple NHIs the host router stays powered through suspend-to-idle: the Apple driver sets QUIRK_NO_SYSTEM_SLEEP whenever its routers are not put to sleep (router_sleep off, the default). tb_domain_suspend_noirq() still stops the control channel. As the host suspends, a direct XDomain peer sends a round of discovery requests: on J775c with a Mac as the peer, four UUID and four properties requests. With the channel stopped they go unanswered, and the peer drops the link. On resume the router reports a cold start, port 0:1 fails to reach TB_PORT_UP, and the XDomain is disconnected and not found again: thunderbolt0 is gone after every s2idle. Keep the control channel running through noirq for that case only: a suspend without platform support (pm_suspend_no_platform()) on a host with QUIRK_NO_SYSTEM_SLEEP, and a root switch whose links are direct XDomain links with no device router behind them (tb_domain_retain_noirq_control()). Only the Apple driver sets QUIRK_NO_SYSTEM_SLEEP, so other NHIs are unchanged. While the channel runs, only XDomain discovery requests are handled: UUID, properties and link-state status are answered. Everything else is dropped, and an error-state handshake is not restarted until resume. On J775c the requests arrive and are answered at noirq suspend, before the system idles, and none follow during the sleep (10 s or 120 s) or after it. A peer that leaves during the sleep is still found at resume: tb_switch_resume() waits for each XDomain port and marks the XDomain unplugged if its link does not come back. Every other suspend stops the channel as before. Signed-off-by: Satya Benson --- drivers/thunderbolt/domain.c | 47 ++++++++++++++++++++++++++++++++--- drivers/thunderbolt/xdomain.c | 18 +++++++++++++- include/linux/thunderbolt.h | 2 ++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c index a06539264971c..e77c0eab18cc4 100644 --- a/drivers/thunderbolt/domain.c +++ b/drivers/thunderbolt/domain.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -338,6 +339,10 @@ static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, { struct tb *tb = data; + /* During noirq only XDomain discovery requests can receive a reply. */ + if (READ_ONCE(tb->noirq_ctl_retained) && type != TB_CFG_PKG_XDOMAIN_REQ) + return true; + if (!tb->cm_ops->handle_event) { tb_warn(tb, "domain does not have event handler\n"); return true; @@ -528,11 +533,34 @@ void tb_domain_remove(struct tb *tb) } EXPORT_SYMBOL_FOR_MODULES(tb_domain_remove, "thunderbolt_apple"); +static bool tb_domain_retain_noirq_control(struct tb *tb) +{ + struct tb_port *port; + bool xdomain = false; + + lockdep_assert_held(&tb->lock); + + if (!pm_suspend_no_platform() || + !(tb->nhi->quirks & QUIRK_NO_SYSTEM_SLEEP) || + !tb->root_switch) + return false; + + tb_switch_for_each_port(tb->root_switch, port) { + if (tb_port_has_remote(port)) + return false; + if (port->xdomain) + xdomain = true; + } + + return xdomain; +} + /** * tb_domain_suspend_noirq() - Suspend a domain * @tb: Domain to suspend * - * Suspends all devices in the domain and stops the control channel. + * Suspends all devices in the domain. Stops the control channel unless + * an awake host router needs it for direct XDomain discovery. * * Return: %0 on success, negative errno otherwise. */ @@ -548,8 +576,14 @@ int tb_domain_suspend_noirq(struct tb *tb) mutex_lock(&tb->lock); if (tb->cm_ops->suspend_noirq) ret = tb->cm_ops->suspend_noirq(tb); - if (!ret) - tb_ctl_stop(tb->ctl); + if (!ret) { + if (tb_domain_retain_noirq_control(tb)) { + WRITE_ONCE(tb->noirq_ctl_retained, true); + tb_dbg(tb, "XDomain control channel retained for s2idle\n"); + } else { + tb_ctl_stop(tb->ctl); + } + } mutex_unlock(&tb->lock); return ret; @@ -570,9 +604,14 @@ int tb_domain_resume_noirq(struct tb *tb) int ret = 0; mutex_lock(&tb->lock); - tb_ctl_start(tb->ctl); + if (!tb->noirq_ctl_retained) + tb_ctl_start(tb->ctl); if (tb->cm_ops->resume_noirq) ret = tb->cm_ops->resume_noirq(tb); + if (tb->noirq_ctl_retained) { + WRITE_ONCE(tb->noirq_ctl_retained, false); + tb_dbg(tb, "XDomain control channel remained active\n"); + } mutex_unlock(&tb->lock); return ret; diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index 857219a9ac8da..fd45c77a8bf34 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -831,7 +831,8 @@ static void tb_xdp_handle_request(struct work_struct *work) * timing out, we will restart the handshake now that we * received UUID request from the remote host. */ - if (!ret && xd && xd->state == XDOMAIN_STATE_ERROR) { + if (!READ_ONCE(tb->noirq_ctl_retained) && !ret && xd && + xd->state == XDOMAIN_STATE_ERROR) { mutex_lock(&xd->lock); if (!xd->removing) { dev_dbg(&xd->dev, "restarting handshake\n"); @@ -2666,6 +2667,21 @@ bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type, if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4) return true; + if (READ_ONCE(tb->noirq_ctl_retained)) { + if (type != TB_CFG_PKG_XDOMAIN_REQ || !uuid_equal(&hdr->uuid, &tb_xdp_uuid)) + return true; + switch (hdr->type) { + case UUID_REQUEST_OLD: + case UUID_REQUEST: + case PROPERTIES_REQUEST: + case LINK_STATE_STATUS_REQUEST: + tb_dbg(tb, "XDomain discovery request %#x during s2idle\n", hdr->type); + break; + default: + return true; + } + } + /* * Handle XDomain discovery protocol packets directly here. For * other protocols (based on their UUID) we call registered diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h index acd1668ed93d7..bf1b07f993b8e 100644 --- a/include/linux/thunderbolt.h +++ b/include/linux/thunderbolt.h @@ -74,6 +74,7 @@ enum tb_security_level { * @remote_activity: Successful stock remote discovery exchanges * @nhi: Pointer to the NHI structure * @ctl: Control channel for this domain + * @noirq_ctl_retained: Control rings stayed active during noirq suspend * @wq: Ordered workqueue for all domain specific work * @root_switch: Root switch of this domain * @cm_ops: Connection manager specific operations vector @@ -88,6 +89,7 @@ struct tb { atomic64_t remote_activity; struct tb_nhi *nhi; struct tb_ctl *ctl; + bool noirq_ctl_retained; struct workqueue_struct *wq; struct tb_switch *root_switch; const struct tb_cm_ops *cm_ops; From bd272902b68c0ac9944852174d3fffeef4d0586b Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sun, 22 Nov 2026 14:05:29 -0500 Subject: [PATCH 2/2] thunderbolt: xdomain: handle discovery requests under the domain lock With the control channel kept through s2idle, tb_xdomain_handle_request() only schedules the read-only discovery requests (UUID, properties, link state status). But the request work runs later on system_wq, and only the scheduling side checked noirq_ctl_retained: CPU0 (system_wq) CPU1 (suspend) tb_xdomain_handle_request() noirq_ctl_retained == false schedule_work(PROPERTIES_CHANGED) tb_domain_suspend_noirq() mutex_lock(&tb->lock) ->suspend_noirq() noirq_ctl_retained = true mutex_unlock(&tb->lock) tb_xdp_handle_request() PROPERTIES_CHANGED_REQUEST queue xd->state_work (noirq: the domain is suspended) A LINK_STATE_CHANGE_REQUEST queued the same way does the same, and an allowed LINK_STATE_STATUS_REQUEST reads the downstream port with tb_port_read() without tb->lock, so it can run while tb_domain_suspend_noirq() or tb_domain_resume_noirq() works on the same domain. Handle each request under tb->lock, which those two hold while they set and clear noirq_ctl_retained, and re-check the flag there: a request other than discovery that arrives at the handler while the channel is retained gets ERROR_NOT_READY, and the peer asks again after resume. The port read now always runs under tb->lock. Both sides use one helper for the allowed requests. Lock order: tb->lock is taken before xdomain_lock and xd->lock here, as in the existing callers that hold tb->lock and take xd->lock; nothing takes tb->lock under either. tb_xdp_handle_request() then looks the XDomain up with tb_xdomain_find_by_route(), which expects the caller to hold tb->lock, not tb_xdomain_find_by_route_locked(), which takes it again. Signed-off-by: Satya Benson --- drivers/thunderbolt/xdomain.c | 44 +++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c index fd45c77a8bf34..d174e7129191c 100644 --- a/drivers/thunderbolt/xdomain.c +++ b/drivers/thunderbolt/xdomain.c @@ -759,6 +759,23 @@ static void stop_handshake(struct tb_xdomain *xd) __stop_handshake(xd); } +/* + * The XDomain discovery requests answered while the control channel is kept + * through s2idle (tb->noirq_ctl_retained): they only read state. + */ +static bool tb_xdp_noirq_request(enum tb_xdp_type type) +{ + switch (type) { + case UUID_REQUEST_OLD: + case UUID_REQUEST: + case PROPERTIES_REQUEST: + case LINK_STATE_STATUS_REQUEST: + return true; + default: + return false; + } +} + static void tb_xdp_handle_request(struct work_struct *work) { struct xdomain_request_work *xw = container_of(work, typeof(*xw), work); @@ -777,19 +794,28 @@ static void tb_xdp_handle_request(struct work_struct *work) sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK; sequence >>= TB_XDOMAIN_SN_SHIFT; + /* + * Handle the request under tb->lock. tb_domain_suspend_noirq() and + * tb_domain_resume_noirq() set and clear noirq_ctl_retained under it, + * so a request queued before the control channel was retained cannot + * run a non-discovery request (or touch a port) during noirq, and the + * port read below cannot overlap the domain's suspend or resume. + */ mutex_lock(&tb->lock); if (tb->root_switch) uuid = kmemdup(tb->root_switch->uuid, sizeof(*uuid), GFP_KERNEL); else uuid = NULL; - mutex_unlock(&tb->lock); - if (!uuid) { + if (!uuid || (READ_ONCE(tb->noirq_ctl_retained) && + !tb_xdp_noirq_request(pkg->type))) { tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY); + mutex_unlock(&tb->lock); goto out; } - xd = tb_xdomain_find_by_route_locked(tb, route); + /* tb->lock is held: the _locked helper would take it again. */ + xd = tb_xdomain_find_by_route(tb, route); if (xd) update_property_block(xd); @@ -926,6 +952,7 @@ static void tb_xdp_handle_request(struct work_struct *work) } tb_xdomain_put(xd); + mutex_unlock(&tb->lock); if (ret) { tb_warn(tb, "failed to send XDomain response for %#x\n", @@ -2670,16 +2697,9 @@ bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type, if (READ_ONCE(tb->noirq_ctl_retained)) { if (type != TB_CFG_PKG_XDOMAIN_REQ || !uuid_equal(&hdr->uuid, &tb_xdp_uuid)) return true; - switch (hdr->type) { - case UUID_REQUEST_OLD: - case UUID_REQUEST: - case PROPERTIES_REQUEST: - case LINK_STATE_STATUS_REQUEST: - tb_dbg(tb, "XDomain discovery request %#x during s2idle\n", hdr->type); - break; - default: + if (!tb_xdp_noirq_request(hdr->type)) return true; - } + tb_dbg(tb, "XDomain discovery request %#x during s2idle\n", hdr->type); } /*