From 6b8d44fe357361eb43570246c91408a59f5e7ef8 Mon Sep 17 00:00:00 2001 From: Naeem Malik Date: Thu, 5 Nov 2026 10:20:04 +0000 Subject: [PATCH 1/2] wifi: brcmfmac: pcie: don't drop ring completions in the hard IRQ handler brcmf_pcie_quick_check_isr() masks the dongle with brcmf_pcie_intr_disable() whenever mailboxint reads non-zero. Since commit f04be345ad51 ("wifi: brcmfmac: Do not service msgbuf IRQs until ready in MSI mode") that helper also clears irq_ready, and the threaded handler services the rings only while irq_ready is set. The thread therefore skips brcmf_proto_msgbuf_rx_trigger() for exactly the interrupts the hard handler caught with mailboxint still set, and then sets irq_ready again on its way out. In MSI mode the firmware clears mailboxint itself, so whether the hard handler sees it set is a race. When it does, completions that arrived with that interrupt stay in the D2H rings until a later interrupt. On firmware that sends mailbox data over the control ring (no BRCMF_PCIE_SHARED_USE_MAILBOX) that includes the D3 ACK, and ioctl responses: brcmf_msgbuf_query_dcmd_len() and brcmf_pcie_pm_enter_D3() then time out, the latter aborting system suspend. On a MacBook Air (M1) with BCM4378 this became frequent with PCIe ASPM L1.2 enabled. In INTx mode the hard handler always clears irq_ready first, so the rings are never serviced at all. Let the hard handler mask the dongle without touching irq_ready, and let the thread unmask without setting it, so irq_ready is controlled only by the code that sets the rings up and tears them down. Fixes: f04be345ad51 ("wifi: brcmfmac: Do not service msgbuf IRQs until ready in MSI mode") Signed-off-by: Naeem Malik Link: https://github.com/omacom/linux-aurora/commit/3a936edf2c49afddf0e173ad7c8593ae2ba4d885 --- .../wireless/broadcom/brcm80211/brcmfmac/pcie.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index 53b148fa71e79..db15a45235a81 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -1295,7 +1295,10 @@ static irqreturn_t brcmf_pcie_quick_check_isr(int irq, void *arg) struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)arg; if (brcmf_pcie_read_pcie32(devinfo, devinfo->reginfo->mailboxint)) { - brcmf_pcie_intr_disable(devinfo); + /* Mask the dongle only: irq_ready gates ring servicing while the + * rings are not set up or are being torn down, not this IRQ. + */ + brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, 0); brcmf_dbg(PCIE, "Enter\n"); return IRQ_WAKE_THREAD; } @@ -1335,8 +1338,13 @@ static irqreturn_t brcmf_pcie_isr_thread(int irq, void *arg) } brcmf_pcie_bus_console_read(devinfo, false); - if (devinfo->state == BRCMFMAC_PCIE_STATE_UP) - brcmf_pcie_intr_enable(devinfo); + /* Unmask, but leave irq_ready to the code that sets the rings up and + * tears them down. + */ + if (devinfo->state == BRCMFMAC_PCIE_STATE_UP && READ_ONCE(devinfo->irq_ready)) + brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, + devinfo->reginfo->int_d2h_db | + devinfo->reginfo->int_fn0); devinfo->in_irq = false; return IRQ_HANDLED; } From 9dab09e9226ffb88f7afbdcd4a4449d243fe54d6 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Tue, 1 Dec 2026 07:04:08 -0500 Subject: [PATCH 2/2] wifi: brcmfmac: pcie: stop ring servicing across the D0 sleeps With irq_ready no longer cleared by the hard handler, nothing cleared it for the two sleeps that keep the function in D0 (WoWLAN and the Apple s2idle path): it stayed set from before the suspend. Every interrupt in that window then had its thread service the rings and unmask the dongle, also while brcmf_pcie_pm_leave_D3() runs pci_restore_state() on the live link, which rewrites its config space, ASPM/L1SS and MSI state. A J473 (BCM4388) reset seconds after every s2idle resume with the previous commit alone, and survived without it. Make the ownership explicit: - intr_disable() clears irq_ready before masking and intr_enable() sets it before unmasking, so an interrupt raised as the mask opens cannot leave the dongle masked. - Both D0 sleeps stop ring servicing with intr_quiesce(), which also waits for a running handler and masks again. WoWLAN unmasks the dongle for its wake interrupt with irq_ready still clear, after wowl_sleeping is set. - leave_D3() keeps the IRQ disabled around pci_restore_state() in both D0 sleeps. - brcmf_pcie_wowl_wake() sets irq_ready and the mask explicitly before the bus comes up, and runs the thread once for completions posted while the dongle was masked. brcmf-suspend-tests checks both sleeps keep ring servicing off, that the restore runs with the IRQ disabled and that the resume kicks the thread, with a mutation for each. Signed-off-by: Satya Benson --- .../broadcom/brcm80211/brcmfmac/pcie.c | 82 ++++++++++++++++--- tools/asahi/brcmf-suspend-tests.py | 40 +++++++-- tools/asahi/run-host-tests.py | 3 +- 3 files changed, 105 insertions(+), 20 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index db15a45235a81..716aa8e69d670 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -1263,21 +1263,46 @@ static void brcmf_pcie_bus_console_read(struct brcmf_pciedev_info *devinfo, } +static void brcmf_pcie_intr_unmask(struct brcmf_pciedev_info *devinfo) +{ + brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, + devinfo->reginfo->int_d2h_db | + devinfo->reginfo->int_fn0); +} + +/* + * The threaded handler services the rings and unmasks the dongle only while + * irq_ready is set. Clear it before masking, so a thread that sees it clear + * does neither. + */ static void brcmf_pcie_intr_disable(struct brcmf_pciedev_info *devinfo) { + WRITE_ONCE(devinfo->irq_ready, false); brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, 0); - - devinfo->irq_ready = false; } - +/* + * Set irq_ready before unmasking: an interrupt raised as soon as the mask + * opens must find it set, or its thread would leave the dongle masked. The + * MMIO write orders the store before the unmask. + */ static void brcmf_pcie_intr_enable(struct brcmf_pciedev_info *devinfo) { - brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, - devinfo->reginfo->int_d2h_db | - devinfo->reginfo->int_fn0); + WRITE_ONCE(devinfo->irq_ready, true); + brcmf_pcie_intr_unmask(devinfo); +} - devinfo->irq_ready = true; +/* + * Stop ring servicing and wait for a handler already running. A thread that + * read irq_ready before it was cleared can still have unmasked the dongle, so + * mask it again once the handlers are done. + */ +static void brcmf_pcie_intr_quiesce(struct brcmf_pciedev_info *devinfo) +{ + brcmf_pcie_intr_disable(devinfo); + if (devinfo->irq_allocated) + synchronize_irq(devinfo->pdev->irq); + brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, 0); } static void brcmf_pcie_hostready(struct brcmf_pciedev_info *devinfo) @@ -1333,7 +1358,8 @@ static irqreturn_t brcmf_pcie_isr_thread(int irq, void *arg) brcmf_pcie_poll_mb_data(devinfo); } if (devinfo->have_msi || status & devinfo->reginfo->int_d2h_db) { - if (devinfo->state == BRCMFMAC_PCIE_STATE_UP && devinfo->irq_ready) + if (devinfo->state == BRCMFMAC_PCIE_STATE_UP && + READ_ONCE(devinfo->irq_ready)) brcmf_proto_msgbuf_rx_trigger(&devinfo->pdev->dev); } @@ -1342,9 +1368,7 @@ static irqreturn_t brcmf_pcie_isr_thread(int irq, void *arg) * tears them down. */ if (devinfo->state == BRCMFMAC_PCIE_STATE_UP && READ_ONCE(devinfo->irq_ready)) - brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, - devinfo->reginfo->int_d2h_db | - devinfo->reginfo->int_fn0); + brcmf_pcie_intr_unmask(devinfo); devinfo->in_irq = false; return IRQ_HANDLED; } @@ -3475,10 +3499,22 @@ static void brcmf_pcie_wowl_wake(struct brcmf_bus *bus, struct brcmf_pciedev_info *devinfo) { devinfo->wowl_sleeping = false; + /* + * Ring servicing stopped at the sleep. Turn it back on, with the mask + * set explicitly rather than left to whichever interrupt comes next, + * before the bus is up and TX queues wake. + */ brcmf_pcie_intr_enable(devinfo); brcmf_bus_change_state(bus, BRCMF_BUS_UP); brcmf_msgbuf_resume(bus->drvr); brcmf_pcie_fwcon_timer(devinfo, true); + /* + * Completions posted while the dongle was masked may have no interrupt + * of their own (in MSI mode the firmware clears mailboxint itself), so + * run the thread once. + */ + if (devinfo->irq_allocated) + irq_wake_thread(devinfo->pdev->irq, devinfo); } /* @@ -3503,12 +3539,21 @@ static int brcmf_pcie_wowl_sleep(struct device *dev) } devinfo->irq_wake_enabled = true; + /* + * No ring servicing until the wake. A matched frame must still raise + * the MSI, so unmask the dongle again with irq_ready clear: its thread + * reports the wakeup and leaves the dongle masked. wowl_sleeping is set + * first so that interrupt is never taken for an ordinary one. + */ + brcmf_pcie_intr_quiesce(devinfo); + devinfo->wowl_sleeping = true; + brcmf_pcie_intr_unmask(devinfo); + ret = pci_save_state(devinfo->pdev); if (ret) { brcmf_pcie_disable_irq_wake(devinfo); goto fail; } - devinfo->wowl_sleeping = true; return 0; fail: @@ -3534,8 +3579,12 @@ static int brcmf_pcie_s2idle_sleep(struct device *dev) struct brcmf_pciedev_info *devinfo = bus->bus_priv.pcie->devinfo; int ret; + /* Nothing touches the rings until brcmf_pcie_wowl_wake() at resume. */ + brcmf_pcie_intr_quiesce(devinfo); + ret = pci_save_state(devinfo->pdev); if (ret) { + brcmf_pcie_intr_enable(devinfo); brcmf_bus_change_state(bus, BRCMF_BUS_UP); brcmf_msgbuf_resume(bus->drvr); brcmf_pcie_fwcon_timer(devinfo, true); @@ -3633,8 +3682,15 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) devinfo = bus->bus_priv.pcie->devinfo; brcmf_dbg(PCIE, "Enter, dev=%p, bus=%p\n", dev, bus); + /* + * In both sleeps the function and its link stayed in D0. Keep the + * handlers off the device while pci_restore_state() rewrites its config + * space, ASPM/L1SS and MSI state. + */ if (devinfo->wowl_sleeping) { + disable_irq(devinfo->pdev->irq); pci_restore_state(devinfo->pdev); + enable_irq(devinfo->pdev->irq); brcmf_pcie_disable_irq_wake(devinfo); brcmf_pcie_wowl_wake(bus, devinfo); return 0; @@ -3642,7 +3698,9 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) if (devinfo->s2idle_d0) { devinfo->s2idle_d0 = false; + disable_irq(devinfo->pdev->irq); pci_restore_state(devinfo->pdev); + enable_irq(devinfo->pdev->irq); brcmf_pcie_wowl_wake(bus, devinfo); return 0; } diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index fca45b4d0f748..6ffce5c538f8f 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -71,6 +71,7 @@ def function(source, name): struct pci_dev { struct device dev; int irq; bool state_saved; }; struct brcmf_pciedev_info { bool mbdata_completed, intr, ready, timer, sleep_reset_blocked, setup_complete; bool wowl_irq, wowl_enabled, irq_wake_enabled, wowl_sleeping, fwseed, s2idle_d0; + bool irq_allocated, unmasked; int state, mbdata_resp_wait; void *ci; struct pci_dev *pdev; struct { int intmask, mailboxmask; } *reginfo; }; struct pcie_priv { struct brcmf_pciedev_info *devinfo; }; @@ -161,7 +162,12 @@ def function(source, name): static int wake_error, save_error; static unsigned d3_informs, d0_informs; static int enable_irq_wake(int irq) {(void)irq;return wake_error;} static int pci_save_state(struct pci_dev *p) {if(save_error)return save_error;p->state_saved=true;return 0;} -static void pci_restore_state(struct pci_dev *p) {p->state_saved=false;} +static int irq_depth; static unsigned kicks; +static void disable_irq(int irq) {(void)irq;irq_depth++;} +static void enable_irq(int irq) {(void)irq;assert(irq_depth>0);irq_depth--;} +static void irq_wake_thread(int irq,void *id) {(void)irq;(void)id;kicks++;} +/* The function stayed in D0 with its link up: no handler may run during the restore. */ +static void pci_restore_state(struct pci_dev *p) {assert(irq_depth==1);p->state_saved=false;} static void brcmf_pcie_disable_irq_wake(struct brcmf_pciedev_info *d) {d->irq_wake_enabled=false;} static int brcmf_pcie_pm_leave_D3(struct device *dev); static int brcmf_pcie_send_mb_data(struct brcmf_pciedev_info *d,int mb) { @@ -173,7 +179,11 @@ def function(source, name): } static int brcmf_pcie_read_pcie32(struct brcmf_pciedev_info *d,int r) {(void)d;(void)r;return retain;} static void brcmf_pcie_select_core(struct brcmf_pciedev_info *d,int c) {(void)d;(void)c;} -static void brcmf_pcie_intr_enable(struct brcmf_pciedev_info *d) {d->intr=true;} +static void brcmf_pcie_intr_enable(struct brcmf_pciedev_info *d) {d->intr=d->unmasked=true;} +/* Ring servicing off and no handler running: the thread neither services the rings nor unmasks. */ +static void brcmf_pcie_intr_quiesce(struct brcmf_pciedev_info *d) {d->intr=d->unmasked=false;} +/* The dongle's interrupt only, for the WoWLAN wake: ring servicing stays off. */ +static void brcmf_pcie_intr_unmask(struct brcmf_pciedev_info *d) {assert(d->wowl_sleeping);d->unmasked=true;} static void brcmf_pcie_hostready(struct brcmf_pciedev_info *d) {assert(d->intr);d->ready=true;} static void brcmf_chip_detach(void *c) {(void)c;} static void brcmf_pcie_remove(struct pci_dev *p) {(void)p;removes++;} @@ -285,6 +295,7 @@ def function(source, name): cfg.pub=&pub;cfg.wiphy=&wiphy;wiphy.cfg=&cfg;vif_up=true;device.data=&bus;devinfo.pdev=&pdev; static typeof(*devinfo.reginfo) regs;devinfo.reginfo=®s; devinfo.state=BRCMFMAC_PCIE_STATE_UP;devinfo.intr=devinfo.ready=devinfo.timer=true; + devinfo.irq_allocated=devinfo.unmasked=true;irq_depth=0;kicks=0; country_gets=sets=bands=txflows=creates=cancels=removes=probes=0; failure=translation=d0_error=probe_error=wake_error=save_error=0;ack=true;retain=1;d3_informs=d0_informs=0; pm_suspend_target_state=PM_SUSPEND_TO_IDLE;feats=0; @@ -442,13 +453,16 @@ def function(source, name): assert(!d3_informs && devinfo.state==BRCMFMAC_PCIE_STATE_UP); if(fail) { assert(ret==-EINVAL && !devinfo.wowl_sleeping && !devinfo.irq_wake_enabled); - assert(bus.state==BRCMF_BUS_UP && !msgbuf.tx_suspended && devinfo.timer);cases++;continue; + assert(bus.state==BRCMF_BUS_UP && !msgbuf.tx_suspended && devinfo.timer); + assert(devinfo.intr && devinfo.unmasked);cases++;continue; } assert(!ret && devinfo.wowl_sleeping && devinfo.irq_wake_enabled && pdev.state_saved); assert(bus.state==BRCMF_BUS_DOWN && msgbuf.tx_suspended && !devinfo.timer && ndev[0].stopped); - devinfo.intr=false; + /* No ring servicing across the sleep, but the wake interrupt stays unmasked. */ + assert(!devinfo.intr && devinfo.unmasked); assert(!brcmf_pcie_pm_leave_D3(&device)); assert(!devinfo.wowl_sleeping && !devinfo.irq_wake_enabled && !pdev.state_saved && devinfo.intr); + assert(devinfo.unmasked && !irq_depth && kicks==1); assert(bus.state==BRCMF_BUS_UP && !msgbuf.tx_suspended && devinfo.timer && !ndev[0].stopped); assert(!d0_informs && !removes && !probes);cases++; } @@ -461,13 +475,16 @@ def function(source, name): assert(!d3_informs && devinfo.state==BRCMFMAC_PCIE_STATE_UP && !devinfo.wowl_sleeping); if(fail) { assert(ret==-EINVAL && !devinfo.s2idle_d0 && !pdev.state_saved); - assert(bus.state==BRCMF_BUS_UP && !msgbuf.tx_suspended && devinfo.timer);cases++;continue; + assert(bus.state==BRCMF_BUS_UP && !msgbuf.tx_suspended && devinfo.timer); + assert(devinfo.intr && devinfo.unmasked);cases++;continue; } assert(!ret && devinfo.s2idle_d0 && pdev.state_saved && !devinfo.irq_wake_enabled); assert(bus.state==BRCMF_BUS_DOWN && msgbuf.tx_suspended && !devinfo.timer && ndev[0].stopped); - devinfo.intr=false; + /* No ring servicing and the dongle masked across the sleep. */ + assert(!devinfo.intr && !devinfo.unmasked); assert(!brcmf_pcie_pm_leave_D3(&device)); assert(!devinfo.s2idle_d0 && !pdev.state_saved && devinfo.intr); + assert(devinfo.unmasked && !irq_depth && kicks==1); assert(bus.state==BRCMF_BUS_UP && !msgbuf.tx_suspended && devinfo.timer && !ndev[0].stopped); assert(msgbuf.flowring_work.queued && msgbuf.txflow_work.queued); assert(!d0_informs && !removes && !probes);cases++; @@ -523,7 +540,8 @@ def main(): parser.add_argument('--mutation', choices=['country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', 's2idle-d3', 'wowl-s2idle-only', 'restore-pm', - 'resume-join', 'down-error-fence']) + 'resume-join', 'down-error-fence', + 'd0-quiesce', 'd0-restore-irq', 'd0-kick']) args = parser.parse_args() sources = {name: (DRIVER / (name + '.c')).read_text() for name in ('cfg80211', 'core', 'pcie', 'msgbuf')} @@ -566,6 +584,14 @@ def main(): elif args.mutation == 'wowl-s2idle-only': body = body.replace('if (wowl && cfg->pub->bus_if->wowl_s2idle_only &&\n\t pm_suspend_target_state != PM_SUSPEND_TO_IDLE)', 'if (false)') + elif args.mutation == 'd0-quiesce': + body = body.replace('\t/* Nothing touches the rings until brcmf_pcie_wowl_wake() at resume. */\n' + '\tbrcmf_pcie_intr_quiesce(devinfo);\n', '') + elif args.mutation == 'd0-restore-irq': + body = body.replace('\t\tdisable_irq(devinfo->pdev->irq);\n', '') + body = body.replace('\t\tenable_irq(devinfo->pdev->irq);\n', '') + elif args.mutation == 'd0-kick': + body = body.replace('irq_wake_thread(devinfo->pdev->irq, devinfo);', ';') elif args.mutation == 'tx-drain': body = body.replace('netif_tx_disable(ifp->ndev);', '(void)ifp;') code = PREFIX.replace('/* STOP_REASONS */', reasons) + CALLBACK_STUBS + body + MAIN diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index b1ca113d45363..7f0d32da6957a 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -234,7 +234,8 @@ def mutations(flag, names, base, expect=KILL): + mutations('--mutation', ('country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', 'wowl-s2idle-only', 'restore-pm', 'resume-join', - 'down-error-fence'), out()), + 'down-error-fence', 'd0-quiesce', 'd0-restore-irq', + 'd0-kick'), out()), 'brcmf-wowl-filter-tests.py': [(out(), PASS)] + mutations('--mutation', ('base-order', 'mask-last', 'totsize', 'no-track'), out()), 'brcmfmac-scan-timeout-tests.py': [(out('--output'), PASS)],