Re: [PATCH v2 5/5] wifi: brcmfmac: pcie: support inband device wake and deep sleep

From: Arend van Spriel

Date: Thu Nov 26 2026 - 09:30:12 EST


On Wed, 11 Nov 2026 13:07:48 +0100, Andre Eikmeyer <dev@deq.rocks> wrote:
> The BCM4364 and BCM4377 firmware report inband deep sleep in the PCIe
> shared flags. The driver only advertises that it has no out-of-band
> device wake, which leaves deep sleep disabled. The firmware then stays
> awake and keeps its latency tolerance short enough to hold the package
> out of PC10.
>
> Advertise inband device wake when the firmware supports it and follow
> the handshake of the firmware. The device asks for deep sleep while
> device wake is deasserted, the host acknowledges it once it has nothing
> to send, asserts device wake before it uses the rings again and waits
> for the exit note, and deasserts device wake after 200 ms without ring
> writes. Hooks around the ring lock cover every host to device ring
> writer. A system suspend asserts device wake before D3_INFORM and
> acknowledges no deep sleep request until the resume.
>
> Tested on a MacBookPro15,1 (BCM4364) and a MacBookAir9,1 (BCM4377). The
> firmware enters and leaves deep sleep about 40 times a minute at idle
> with Wi-Fi associated, traffic and downloads work, and the package
> reaches PC10 without ignoring the latency tolerance of the Wi-Fi port.
>
> Signed-off-by: Andre Eikmeyer <dev@deq.rocks>

This patch does not apply to wireless-next main: the struct brcmf_pciedev_info hunk
conflicts with the sig_name/fw_size/skip_reset_vector members added by "wifi:
brcmfmac: Load signed PCIe firmware images". Please rebase on wireless-next.

[...]

> ---
> .../broadcom/brcm80211/brcmfmac/commonring.c | 14 ++
> .../broadcom/brcm80211/brcmfmac/commonring.h | 5 +
> .../broadcom/brcm80211/brcmfmac/pcie.c | 233 +++++++++++++++++-
> 3 files changed, 249 insertions(+), 3 deletions(-)

[...]

> diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c
> @@ -892,17 +918,184 @@ brcmf_pcie_send_mb_data(struct brcmf_pciedev_info *devinfo, u32 htod_mb_data)

[...]

> +static void brcmf_pcie_ds_send(struct brcmf_pciedev_info *devinfo, u32 data)
> +{
> + mutex_lock(&devinfo->ds_mutex);
> + WRITE_ONCE(devinfo->ds_sender, current);
> + brcmf_pcie_send_mb_data(devinfo, data);
> + WRITE_ONCE(devinfo->ds_sender, NULL);
> + mutex_unlock(&devinfo->ds_mutex);
> +}
> +

[...]

> +static void brcmf_pcie_ds_wake(struct brcmf_pciedev_info *devinfo)
> +{
> + bool assert = false, wait;
> +
> + if (brcmf_pcie_ds_skip(devinfo))
> + return;
> +
> + spin_lock(&devinfo->ds_lock);
> + devinfo->ds_active++;
> + if (devinfo->ds_state == BRCMF_PCIE_DS_SLEEP) {
> + devinfo->ds_state = BRCMF_PCIE_DS_EXIT_WAIT;
> + assert = true;
> + }
> + wait = devinfo->ds_state == BRCMF_PCIE_DS_EXIT_WAIT;
> + spin_unlock(&devinfo->ds_lock);
> +
> + if (assert) {
> + brcmf_dbg(PCIE, "deep sleep: assert device wake\n");
> + brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DW_ASSERT);
> + }
> + if (!wait)
> + return;
> +
> + /* The exit note arrives through the interrupt thread itself */
> + if (devinfo->in_irq) {
> + usleep_range(1000, 1100);
> + return;
> + }
> + if (wait_event_timeout(devinfo->ds_wait,
> + READ_ONCE(devinfo->ds_state) !=
> + BRCMF_PCIE_DS_EXIT_WAIT,
> + BRCMF_PCIE_DS_EXIT_TIMEOUT))
> + return;
> +
> + brcmf_err(dev_get_drvdata(&devinfo->pdev->dev),
> + "no deep sleep exit after device wake\n");
> + spin_lock(&devinfo->ds_lock);
> + if (devinfo->ds_state == BRCMF_PCIE_DS_EXIT_WAIT)
> + devinfo->ds_state = BRCMF_PCIE_DS_AWAKE;
> + spin_unlock(&devinfo->ds_lock);
> +}
> +

devinfo->in_irq is a device wide flag that is true whenever the interrupt thread is
running. Another thread (tx worker, ioctl, flowring worker) that gets here while the
interrupt thread is running takes this branch, sleeps 1 ms and writes the ring while
the device is still waking up. Please check that current is the interrupt thread (for
instance record it like ds_sender) instead of looking at in_irq.

The return value of brcmf_pcie_ds_send() is ignored here and in the ack paths. If the
write fails, every ring lock waits the full 500 ms for the exit note, or the state
says sleep while the device is awake. Please handle the failure.

[...]

> diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c
> @@ -1821,6 +2021,10 @@ brcmf_pcie_init_share_ram_info(struct brcmf_pciedev_info *devinfo,
> if (shared->flags & BRCMF_PCIE_SHARED_DAR)
> host_cap |= BRCMF_HOSTCAP_H2D_DAR;
> host_cap |= BRCMF_HOSTCAP_DS_NO_OOB_DW;
> + if (shared->flags & BRCMF_PCIE_SHARED_INBAND_DS) {
> + host_cap |= BRCMF_HOSTCAP_DS_INBAND_DW;
> + devinfo->ds_state = BRCMF_PCIE_DS_ALLOWED;
> + }
>
> brcmf_pcie_write_tcm32(devinfo, sharedram_addr +
> BRCMF_SHARED_HOST_CAP_OFFSET,

The cover letter says this relies on the mailbox over the control ring, but nothing
checks mb_via_ctl here. With the shared memory mailbox the wake and ack messages go
through a different path and I doubt the handshake holds. Please require mb_via_ctl.
Does the firmware want DS_NO_OOB_DW and DS_INBAND_DW both set? This also enables the
feature for every other firmware that sets the flag (BCM4378/4387/4388, CYW), which
were not tested. Please limit it to the tested chips or get it tested on the others.

[...]

> diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c
> @@ -2757,6 +2966,8 @@ brcmf_pcie_remove(struct pci_dev *pdev)
> return;
>
> devinfo = bus->bus_priv.pcie->devinfo;
> + WRITE_ONCE(devinfo->ds_state, BRCMF_PCIE_DS_NONE);
> + cancel_delayed_work_sync(&devinfo->ds_deassert_work);
> brcmf_pcie_bus_console_read(devinfo, false);
> brcmf_pcie_fwcon_timer(devinfo, false);
>

This makes all hooks a no-op but does not wake the device first. At idle the device
sleeps most of the time, so the ioctls and flowring deletes that brcmf_detach() sends
after this go to a sleeping device and time out. Also, ds_state is not changed under
ds_lock, so a thread that passed ds_skip() just before can still call
mod_delayed_work() after cancel_delayed_work_sync() (tx worker unlock path or the
interrupt thread exit note), and the work then runs after devinfo is freed. Please
wake the device before disabling, set NONE under ds_lock, check for it under the lock
before queueing the work, and cancel the work after the tx worker and interrupt
handling are stopped.

The series "wifi: brcmfmac: pcie: recover from fatal PCIe errors"
(https://patchwork.kernel.org/series/1203582/) re-probes the device after a fatal
PCIe error with device_reprobe(), so brcmf_pcie_remove() then runs on a device that
just had its link reset and does not answer. Waking the device first would stall
there, and the ring lock hooks would wait on it while devinfo->state is already
DOWN. Please make the wake in remove and the ring lock hooks give up when the
device is not up. Whichever series is applied second will need a rebase.

[...]

> diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c
> @@ -2811,6 +3022,15 @@ static int brcmf_pcie_pm_enter_D3(struct device *dev)
> brcmf_pcie_fwcon_timer(devinfo, false);
> brcmf_bus_change_state(bus, BRCMF_BUS_DOWN);
>
> + if (devinfo->ds_state != BRCMF_PCIE_DS_NONE) {
> + spin_lock(&devinfo->ds_lock);
> + devinfo->ds_suspend = true;
> + spin_unlock(&devinfo->ds_lock);
> + cancel_delayed_work_sync(&devinfo->ds_deassert_work);
> + brcmf_pcie_ds_wake(devinfo);
> + brcmf_pcie_ds_idle(devinfo);
> + }
> +
> devinfo->mbdata_completed = false;
> brcmf_pcie_send_mb_data(devinfo, BRCMF_H2D_HOST_D3_INFORM);
>

ds_suspend is not cleared when brcmf_pcie_pm_enter_D3() fails further down (the
WoWL D3 timeout returns -EIO). The PM core does not resume this device then, so no
deep sleep request is acked anymore and the deassert work never runs again. Please
clear it on that error path.

Regards,
Arend