wifi: brcmfmac: pcie: S3 and deep sleep on the T2 Mac chips
v2 cover wifi: brcmfmac: pcie: S3 and deep sleep on the T2 Mac chips
This goes on top of Michael Reeves' BCM4388 series [1], which also changes how brcmfmac talks to the BCM4364 and BCM4377 of the T2 Macs. These patches are follow-ups to it.
Michael's Patch 5 moves the mailbox to the control ring when the firmware does not set BRCMF_PCIE_SHARED_USE_MAILBOX. On the BCM4377 of the T2 Macs that makes D3 work at all, since this firmware doesn't read the TCM mailbox the driver used before. But that caused some other issues as an aftermath.
Patches 1 and 2 fix two things in that path. D0_INFORM on resume fails while the PCIe state is still DOWN, which I reported on patch 5 [2]. And with host ready as a capability, the host ready doorbell at init halts the BCM4364 firmware.
Patch 3 probes again after a resume through the platform firmware, since the chip loses its firmware in S3. Patch 4 stands on its own and drops a tx status wait on flowring delete that the BCM4377 firmware doesn't satisfy.
Patch 5 is new in v2. The BCM4364 and BCM4377 firmware support inband deep sleep, but the driver only advertises that it has no out-of-band device wake, which leaves deep sleep disabled. The firmware then stays awake and its latency tolerance holds the package out of PC10. With patch 5 the firmware enters deep sleep at idle and the package reaches PC10 on both machines. It also relies on the mailbox over the control ring from Michael's patch 5.
The Bluetooth function of the BCM4377 gets the same resume handling in "Bluetooth: hci_bcm4377: resume after ACPI S3" on linux-bluetooth.
Successfully tested on the BCM4377 (MacBookAir9,1), BCM4364B2 (MacBookPro15,1), and on the BCM4364B3 (MacBookPro16,1).
v1 [3] has not received any feedback yet. Without these patches the T2 Mac chips do not survive S3 on top of Michael's series.
Changes in v2:
- Add patch 5, inband device wake and deep sleep
- Patches 1 to 4 are unchanged
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/20261106153612.2aadef2a@kait2en-mbp15-1/
[3] https://lore.kernel.org/all/[email protected]/
Andre Eikmeyer (5):
wifi: brcmfmac: pcie: set the state before D0_INFORM on resume
wifi: brcmfmac: pcie: skip the host ready doorbell at init once it is
a capability
wifi: brcmfmac: pcie: probe again after a resume through firmware
wifi: brcmfmac: msgbuf: send the flowring delete without waiting for
tx status
wifi: brcmfmac: pcie: support inband device wake and deep sleep
.../broadcom/brcm80211/brcmfmac/commonring.c | 14 + .../broadcom/brcm80211/brcmfmac/commonring.h | 5 + .../broadcom/brcm80211/brcmfmac/msgbuf.c | 23 +- .../broadcom/brcm80211/brcmfmac/pcie.c | 259 +++++++++++++++++- 4 files changed, 282 insertions(+), 19 deletions(-)
base-commit: 991a73f4909f1348ffb4b38765cdb522a775e4c2
prerequisite-patch-id: e3427132df0f9740b79d9b3f84bc14c25f787514
prerequisite-patch-id: 1a0aae8b057854813ca6d1f20d3bf153a7b761ec
prerequisite-patch-id: 3453364de54d0d786a6126fca792567f13aa3679
prerequisite-patch-id: d40a127ef83ad7d898ca7c3d643b45b829051477
prerequisite-patch-id: 5bd50905bb42d138eb6fda7d5ad795615348ecdd
PATCH v2 1/5 wifi: brcmfmac: pcie: set the state before D0_INFORM on resume
With the mailbox on the control ring, D0_INFORM goes through the ring write path, which refuses to write while devinfo->state is DOWN. brcmf_pcie_pm_leave_D3() sets the state only after D0_INFORM, so every hot resume fails and falls back to remove and re-probe:
brcmf_pcie_send_mb_data: Failed to send H2D mailbox data (-5)
Set the state before D0_INFORM and back to DOWN when the hot resume fails.
--- drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +2 -1
@@ -2838,10 +2838,10 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev)/* Check if device is still up and running, if so we are ready */ if (brcmf_pcie_read_pcie32 (devinfo, devinfo->reginfo->intmask) !=0 ) {brcmf_dbg (PCIE ,"Try to wakeup device....\n" ); + devinfo->state =BRCMFMAC_PCIE_STATE_UP ;if (brcmf_pcie_send_mb_data (devinfo,BRCMF_H2D_HOST_D0_INFORM )) goto cleanup;brcmf_dbg (PCIE ,"Hot resume, continue....\n" ); - devinfo->state =BRCMFMAC_PCIE_STATE_UP ;brcmf_pcie_select_core (devinfo,BCMA_CORE_PCIE2 );brcmf_bus_change_state (bus,BRCMF_BUS_UP );brcmf_pcie_intr_enable (devinfo);
@@ -2851,6 +2851,7 @@ static int brcmf_pcie_pm_leave_D3(struct device *dev) } cleanup: + devinfo->state =BRCMFMAC_PCIE_STATE_DOWN ;brcmf_chip_detach (devinfo->ci); devinfo->ci =NULL ; pdev = devinfo->pdev;
Re: [PATCH v2 1/5] wifi: brcmfmac: pcie: set the state before D0_INFORM on resume
Quoted 11 lines · Andre Eikmeyer
With the mailbox on the control ring,
D0_INFORMgoes through the ring write path, which refuses to write whiledevinfo->stateis DOWN.brcmf_pcie_pm_leave_D3()sets the state only afterD0_INFORM, so every hot resume fails and falls back to remove and re-probe:
brcmf_pcie_send_mb_data: Failed to send H2D mailbox data (-5)Set the state before
D0_INFORMand back to DOWN when the hot resume fails.Signed-off-by Andre Eikmeyer <[email protected]>
Please add a Fixes: tag for the commit that moved the mailbox to the control ring:
Quoted 3 lines
--- drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
You may add my acknowledgement in v3.
Regards,
Arend
PATCH v2 2/5 wifi: brcmfmac: pcie: skip the host ready doorbell at init once it is a capability
In reply to Andre EikmeyerWith host ready in the host capabilities, the BCM4364 firmware halts on the doorbell that brcmf_pcie_preinit() rings, before it answers the first ioctl. The B3 of the MacBookPro16,1 does so on every load, the B2 of the MacBookPro15,1 on some:
brcmf_pcie_handle_mb_data D2H_MB_DATA: FW HALT
brcmf_attach: dongle is not responding: err=-5
The BCM4377 stays silent without the capability, so it stays. Once host ready is a capability the firmware takes the doorbell as the exit from D3, where Broadcom's DHD driver rings it, after D0_INFORM. Ring it at init only when host ready is not a capability.
--- .../net/wireless/broadcom/brcm80211/brcmfmac/pcie.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +11 -2
@@ -325,6 +325,7 @@ struct brcmf_pcie_shared_info {dma_addr_t ringupd_dmahandle;u8 version;bool mb_via_ctl; +bool hostrdy; };#define BRCMF_OTP_MAX_PARAM_LEN 16
@@ -1599,7 +1600,13 @@ static int brcmf_pcie_preinit(struct device *dev)brcmf_dbg (PCIE ,"Enter\n" );brcmf_pcie_intr_enable (buspub->devinfo); -brcmf_pcie_hostready (buspub->devinfo); +/* +* Once the host capabilities enable host ready, the firmware takes the +* doorbell as the exit from D3 only. The BCM4364 firmware halts on it +* here. +*/ +if (!buspub->devinfo->shared.hostrdy) +brcmf_pcie_hostready (buspub->devinfo);return 0 ; }
@@ -1806,8 +1813,10 @@ brcmf_pcie_init_share_ram_info(struct brcmf_pciedev_info *devinfo, !(shared->flags &BRCMF_PCIE_SHARED_USE_MAILBOX ); host_cap = shared->version; -if (shared->flags &BRCMF_PCIE_SHARED_HOSTRDY_DB1 ) +if (shared->flags &BRCMF_PCIE_SHARED_HOSTRDY_DB1 ) { host_cap |=BRCMF_HOSTCAP_H2D_ENABLE_HOSTRDY ; + shared->hostrdy = true; + }if (shared->flags &BRCMF_PCIE_SHARED_DAR ) host_cap |=BRCMF_HOSTCAP_H2D_DAR ; host_cap |=BRCMF_HOSTCAP_DS_NO_OOB_DW ;
Arend van Spriel
maintainerRe: [PATCH v2 2/5] wifi: brcmfmac: pcie: skip the host ready doorbell at init once it is a capability
Quoted 8 lines · Andre Eikmeyer
With host ready in the host capabilities, the BCM4364 firmware halts on the doorbell that
brcmf_pcie_preinit()rings, before it answers the first ioctl. The B3 of the MacBookPro16,1 does so on every load, the B2 of the MacBookPro15,1 on some:
brcmf_pcie_handle_mb_dataD2H_MB_DATA: FW HALT
brcmf_attach: dongle is not responding: err=-5
Quoted 6 lines
The BCM4377 stays silent without the capability, so it stays. Once host ready is a capability the firmware takes the doorbell as the exit from D3, where Broadcom's DHD driver rings it, after
D0_INFORM. Ring it at init only when host ready is not a capability.Signed-off-by Andre Eikmeyer <[email protected]>
The sentence "The BCM4377 stays silent without the capability, so it stays." is unclear. What stays, the capability or the doorbell? Also please add a Fixes: tag for a83a99d063a0 ("wifi: brcmfmac: Negotiate PCIe mailbox transport with firmware"), which introduced the host ready capability.
[...]
Quoted 3 lines
--- .../net/wireless/broadcom/brcm80211/brcmfmac/pcie.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
[...]
Quoted 6 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +0
-1@@ -1599,7 +1600,13 @@ static int brcmf_pcie_preinit(struct device *dev)brcmf_dbg (PCIE ,"Enter\n" );brcmf_pcie_intr_enable (buspub->devinfo); -brcmf_pcie_hostready (buspub->devinfo);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c static int brcmf_pcie_preinit(struct device *dev) · line 1612
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +7
-0+/* +* Once the host capabilities enable host ready, the firmware takes the +* doorbell as the exit from D3 only. The BCM4364 firmware halts on it +* here. +*/ +if (!buspub->devinfo->shared.hostrdy) +brcmf_pcie_hostready (buspub->devinfo);return 0 ; }
This skips the doorbell at init for every firmware with shared version >= 6 and HOSTRDY_DB1, not only the BCM4364. That includes the newer chips the BCM4388 series was written for, which as far as I know ran with the doorbell at init. Doorbell 1 was introduced to tell the firmware that the common rings are initialized, so I would not drop it for chips we have no data on. The B2 only halting on some loads also suggests the doorbell is rung too early rather than that it must not be rung. Please either limit this to the chips that need it, or ring it at a later point if that avoids the halt. Otherwise I would like Michael and Hector to confirm that the other chips do not need it at init.
Regards,
Arend
PATCH v2 3/5 wifi: brcmfmac: pcie: probe again after a resume through firmware
In reply to Andre EikmeyerAfter ACPI S3 the chip comes back without its firmware, but the interrupt mask can read back nonzero, so brcmf_pcie_pm_leave_D3() takes the hot resume path and every command times out afterwards. Seen on the BCM4377 and BCM4364 of the T2 Macs.
When the platform firmware handled the resume and WoWL is off, go straight to the existing remove and re-probe path.
--- .../net/wireless/broadcom/brcm80211/brcmfmac/pcie.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +10 -0
@@ -7,6 +7,7 @@#include <linux/module.h> #include <linux/firmware.h> #include <linux/pci.h> +#include <linux/suspend.h> #include <linux/vmalloc.h> #include <linux/delay.h> #include <linux/interrupt.h>
@@ -2844,6 +2845,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); +/* +* Without WoWL the device is powered off when the platform firmware +* handles the system sleep, as with ACPI S3, and comes back without +* its firmware. Its registers can still read back nonzero, so probe +* it again. +*/ +if (pm_resume_via_firmware () && !devinfo->wowl_enabled) + goto cleanup; +/* Check if device is still up and running, if so we are ready */ if (brcmf_pcie_read_pcie32 (devinfo, devinfo->reginfo->intmask) !=0 ) {brcmf_dbg (PCIE ,"Try to wakeup device....\n" );
Arend van Spriel
maintainerRe: [PATCH v2 3/5] wifi: brcmfmac: pcie: probe again after a resume through firmware
Quoted 9 lines · Andre Eikmeyer
After ACPI S3 the chip comes back without its firmware, but the interrupt mask can read back nonzero, so
brcmf_pcie_pm_leave_D3()takes the hot resume path and every command times out afterwards. Seen on the BCM4377 and BCM4364 of the T2 Macs.When the platform firmware handled the resume and WoWL is off, go straight to the existing remove and re-probe path.
Signed-off-by Andre Eikmeyer <[email protected]>
[...]
Quoted 3 lines
--- .../net/wireless/broadcom/brcm80211/brcmfmac/pcie.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
[...]
Quoted 7 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +2
-0@@ -2844,6 +2845,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); +/* +* Without WoWL the device is powered off when the platform firmware
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c static int brcmf_pcie_pm_leave_D3(struct device *dev) · line 2859
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +7
-0+* handles the system sleep, as with ACPI S3, and comes back without +* its firmware. Its registers can still read back nonzero, so probe +* it again. +*/ +if (pm_resume_via_firmware () && !devinfo->wowl_enabled) + goto cleanup; +/* Check if device is still up and running, if so we are ready */ if (brcmf_pcie_read_pcie32 (devinfo, devinfo->reginfo->intmask) !=0 ) {brcmf_dbg (PCIE ,"Try to wakeup device....\n" );
This applies to every chip handled by brcmfmac PCIe on any platform that resumes through platform firmware, while the problem was seen on the BCM4377 and BCM4364 of the T2 Macs. A device that keeps its power across S3 elsewhere resumed fine through the hot resume path and now gets a full firmware reload. Please limit this to the affected chips (e.g. check devinfo->ci->chip) or explain why a re-probe is right for all of them.
The series "wifi: brcmfmac: pcie: recover from fatal PCIe errors"
(https://patchwork.kernel.org/series/1203582/) also re-probes the device, from its PCI error resume callback with device_reprobe(). An error that hits during a resume through platform firmware could start both re-probes. Please check how the two interact, and make sure only one of them runs.
Regards,
Arend
PATCH v2 4/5 wifi: brcmfmac: msgbuf: send the flowring delete without waiting for tx status
In reply to Andre EikmeyerThe BCM4377 firmware returns the tx status of queued packets only when it gets the flowring delete request. brcmf_msgbuf_delete_flowring() waits up to 100 ms for them before it sends the request, so every disconnect logs a timeout, and the cleared counter goes below zero when the status arrives.
The race the wait was added for in commit 0fa24196e425 ("wifi: brcmfmac:
fix continuous 802.1x tx pending timeout error") is covered by RING_CLOSING since commit 09be7546a602 ("wifi: brcmfmac: fix scheduling while atomic issue when deleting flowring"), and late tx status still releases the packet. Send the delete right away and reset the counter when the ring is created.
--- .../broadcom/brcm80211/brcmfmac/msgbuf.c | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +10 -13
@@ -73,7 +73,6 @@#define BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS 32#define BRCMF_MSGBUF_UPDATE_RX_PTR_THRS 48 -#define BRCMF_MAX_TXSTATUS_WAIT_RETRIES 10struct msgbuf_common_hdr {u8 msgtype;
@@ -645,6 +644,7 @@ brcmf_msgbuf_flowring_create_worker(struct brcmf_msgbuf *msgbuf,brcmf_commonring_config (msgbuf->flowrings[flowid],BRCMF_H2D_TXFLOWRING_MAX_ITEM ,BRCMF_H2D_TXFLOWRING_ITEMSIZE , dma_buf); +atomic_set (&msgbuf->flowrings[flowid]->outstanding_tx,0 ); commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT ];brcmf_commonring_lock (commonring);
@@ -1330,6 +1330,10 @@ brcmf_msgbuf_process_flow_ring_delete_response(struct brcmf_msgbuf *msgbuf, }brcmf_dbg (MSGBUF ,"Flowring %d Delete response status %d\n" , flowid, status); +if (atomic_read (&msgbuf->flowrings[flowid]->outstanding_tx)) +brcmf_dbg (MSGBUF ,"Flowring %d deleted with %d tx status outstanding\n" , + flowid, +atomic_read (&msgbuf->flowrings[flowid]->outstanding_tx));brcmf_msgbuf_remove_flowring (msgbuf, flowid); }
@@ -1471,23 +1475,16 @@ void brcmf_msgbuf_delete_flowring(struct brcmf_pub *drvr, u16 flowid)void *ret_ptr;u8 ifidx;int err; -int retry =BRCMF_MAX_TXSTATUS_WAIT_RETRIES ; -/* make sure it is not in txflow */ +/* +* Make sure txflow posts nothing more to the ring. The firmware returns +* the tx status of the posted packets when it deletes the ring, before +* it sends the delete response. +*/ brcmf_commonring_lock (commonring_del); flow->rings[flowid]->status =RING_CLOSING ;brcmf_commonring_unlock (commonring_del); -/* wait for commonring txflow finished */ -while (retry &&atomic_read (&commonring_del->outstanding_tx)) { -usleep_range (5000 ,10000 ); - retry--; - } -if (!retry) { -brcmf_err ("timed out waiting for txstatus\n" ); -atomic_set (&commonring_del->outstanding_tx,0 ); - } -/* no need to submit if firmware can not be reached */ if (drvr->bus_if->state !=BRCMF_BUS_UP ) {brcmf_dbg (MSGBUF ,"bus down, flowring will be removed\n" );
Arend van Spriel
maintainerRe: [PATCH v2 4/5] wifi: brcmfmac: msgbuf: send the flowring delete without waiting for tx status
Quoted 6 lines · Andre Eikmeyer
The BCM4377 firmware returns the tx status of queued packets only when it gets the flowring delete request.
brcmf_msgbuf_delete_flowring()waits up to 100 ms for them before it sends the request, so every disconnect logs a timeout, and the cleared counter goes below zero when the status arrives.
Quoted 8 lines
The race the wait was added for in commit
0fa24196e425("wifi: brcmfmac:
fix continuous 802.1x tx pending timeout error") is covered byRING_CLOSINGsince commit09be7546a602("wifi: brcmfmac: fix scheduling while atomic issue when deleting flowring"), and late tx status still releases the packet. Send the delete right away and reset the counter when the ring is created.Signed-off-by Andre Eikmeyer <[email protected]>
The race handled by 09be7546a602 is the posting side. What 0fa24196e425 also states is that the flowring must be deleted only after all tx status is back, otherwise pend_8021x_cnt never drains and every connection is delayed by 950 ms. Dropping the wait is only safe if the firmware returns the tx status of the queued packets on flowring delete, before the delete response. That was only observed on the BCM4377. Would be good if this can be confirmed for other chips as well otherwise (probably) better to keep the wait for those.
[...]
Quoted 3 lines
--- .../broadcom/brcm80211/brcmfmac/msgbuf.c | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-)
[...]
drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c line 78
Quoted 9 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +0
-1@@ -73,7 +73,6 @@#define BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS 32#define BRCMF_MSGBUF_UPDATE_RX_PTR_THRS 48 -#define BRCMF_MAX_TXSTATUS_WAIT_RETRIES 10struct msgbuf_common_hdr {u8 msgtype;
This leaves two consecutive blank lines before struct msgbuf_common_hdr.
[...]
Quoted 20 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +5
-5@@ -1471,23 +1475,16 @@ void brcmf_msgbuf_delete_flowring(struct brcmf_pub *drvr, u16 flowid)void *ret_ptr;u8 ifidx;int err; -int retry =BRCMF_MAX_TXSTATUS_WAIT_RETRIES ; -/* make sure it is not in txflow */ +/* +* Make sure txflow posts nothing more to the ring. The firmware returns +* the tx status of the posted packets when it deletes the ring, before +* it sends the delete response. +*/ brcmf_commonring_lock (commonring_del); flow->rings[flowid]->status =RING_CLOSING ;brcmf_commonring_unlock (commonring_del); -/* wait for commonring txflow finished */ -while (retry &&atomic_read (&commonring_del->outstanding_tx)) { -usleep_range (5000 ,10000 );
drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c void brcmf_msgbuf_delete_flowring(struct brcmf_pub *drvr, u16 flowid) · line 1490
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +0
-7- retry--; - } -if (!retry) { -brcmf_err ("timed out waiting for txstatus\n" ); -atomic_set (&commonring_del->outstanding_tx,0 ); - } -/* no need to submit if firmware can not be reached */ if (drvr->bus_if->state !=BRCMF_BUS_UP ) {brcmf_dbg (MSGBUF ,"bus down, flowring will be removed\n" );
As said above, I do not think the wait can go for all chips on the evidence of the BCM4377 only. I will see if I can replicate this on my 43664 device.
Regards,
Arend
PATCH v2 5/5 wifi: brcmfmac: pcie: support inband device wake and deep sleep
In reply to Andre EikmeyerThe 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.
--- .../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(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/commonring.c +14 -0
@@ -28,6 +28,16 @@ void brcmf_commonring_register_cb(struct brcmf_commonring *commonring, } +/* Both run outside the ring lock and may sleep */ +void brcmf_commonring_register_pm_cb (struct brcmf_commonring *commonring, +void (*cr_wake)(void *ctx), +void (*cr_idle)(void *ctx)) +{ + commonring->cr_wake = cr_wake; + commonring->cr_idle = cr_idle; +} + +void brcmf_commonring_config (struct brcmf_commonring *commonring,u16 depth,u16 item_len,void *buf_addr) {
@@ -53,6 +63,8 @@ void brcmf_commonring_lock(struct brcmf_commonring *commonring) {unsigned long flags; +if (commonring->cr_wake) + commonring->cr_wake (commonring->cr_ctx);spin_lock_irqsave (&commonring->lock, flags); commonring->flags = flags; }
@@ -62,6 +74,8 @@ void brcmf_commonring_unlock(struct brcmf_commonring *commonring) __releases(&commonring->lock) {spin_unlock_irqrestore (&commonring->lock, commonring->flags); +if (commonring->cr_idle) + commonring->cr_idle (commonring->cr_ctx); }
drivers/net/wireless/broadcom/brcm80211/brcmfmac/commonring.h +5 -0
@@ -20,6 +20,8 @@ struct brcmf_commonring {int (*cr_update_wptr)(void *ctx);int (*cr_write_rptr)(void *ctx);int (*cr_write_wptr)(void *ctx); +void (*cr_wake)(void *ctx); +void (*cr_idle)(void *ctx);void *cr_ctx;
@@ -38,6 +40,9 @@ void brcmf_commonring_register_cb(struct brcmf_commonring *commonring,int (*cr_update_wptr)(void *ctx), int (*cr_write_rptr)(void *ctx), int (*cr_write_wptr)(void *ctx), void *ctx); +void brcmf_commonring_register_pm_cb (struct brcmf_commonring *commonring, +void (*cr_wake)(void *ctx), +void (*cr_idle)(void *ctx));void brcmf_commonring_config (struct brcmf_commonring *commonring,u16 depth,u16 item_len,void *buf_addr);void brcmf_commonring_lock (struct brcmf_commonring *commonring);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +230 -3
317 lines not on this page · raw message
Arend van Spriel
maintainerRe: [PATCH v2 5/5] wifi: brcmfmac: pcie: support inband device wake and deep sleep
Quoted 15 lines · Andre Eikmeyer
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_INFORMand acknowledges no deep sleep request until the resume.
Quoted 6 lines
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 <[email protected]>
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.
[...]
Quoted 5 lines
--- .../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(-)
[...]
Quoted 2 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +0
-0@@ -892,17 +918,184 @@ brcmf_pcie_send_mb_data(struct brcmf_pciedev_info *devinfo, u32 htod_mb_data)
[...]
Quoted 9 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +9
-0+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); +} +
[...]
Quoted 32 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +32
-0+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 ))
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c brcmf_pcie_send_mb_data(struct brcmf_pciedev_info *devinfo, u32 htod_mb_data) · line 995
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +10
-0+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.
[...]
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c brcmf_pcie_init_share_ram_info(struct brcmf_pciedev_info *devinfo, · line 2030
Quoted 12 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +4
-0@@ -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.
[...]
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c brcmf_pcie_remove(struct pci_dev *pdev) · line 2973
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +2
-0@@ -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.
[...]
Quoted 7 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +2
-0@@ -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);
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c static int brcmf_pcie_pm_enter_D3(struct device *dev) · line 3036
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +7
-0+ 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