1: 5a336aee7cd8 ! 1: dcb5be39d8bc wifi: brcmfmac: pcie: set the state before D0_INFORM on resume @@ Commit message Set the state before D0_INFORM and back to DOWN when the hot resume fails.  + Fixes: a83a99d063a0 ("wifi: brcmfmac: Negotiate PCIe mailbox transport with firmware") + Tested-by: Yossef Osman  + Acked-by: Arend van Spriel  Signed-off-by: Andre Eikmeyer   ## drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c ## 2: 8b5b53c73125 ! 2: 050749e0b7df wifi: brcmfmac: pcie: skip the host ready doorbell at init once it is a capability @@ Metadata Author: Andre Eikmeyer   ## Commit message ## - wifi: brcmfmac: pcie: skip the host ready doorbell at init once it is a capability + wifi: brcmfmac: pcie: skip the host ready doorbell at init on the BCM4364  With host ready in the host capabilities, the BCM4364 firmware halts on the doorbell that brcmf_pcie_preinit() rings, before it answers the @@ Commit message 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. + The BCM4364 loads without host ready in the host capabilities, or with + it but without the doorbell at init. The BCM4377 does not answer + without host ready in the host capabilities, so the capability has to + stay, and it runs with the doorbell at init. Skip the doorbell at init + on the BCM4364 when host ready is a capability.  + Fixes: a83a99d063a0 ("wifi: brcmfmac: Negotiate PCIe mailbox transport with firmware") + Tested-by: Yossef Osman  Signed-off-by: Andre Eikmeyer   ## drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c ## @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static int brcmf_pcie_p 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. ++ * With host ready in the host capabilities, the BCM4364 firmware halts ++ * on this doorbell.  + */ -+ if (!buspub->devinfo->shared.hostrdy) ++ if (!buspub->devinfo->shared.hostrdy || ++ buspub->devinfo->ci->chip != BRCM_CC_4364_CHIP_ID)  + brcmf_pcie_hostready(buspub->devinfo);  return 0; 3: 1508949b291b < -: ------------ wifi: brcmfmac: pcie: probe again after a resume through firmware 4: fff39f45c30c < -: ------------ wifi: brcmfmac: msgbuf: send the flowring delete without waiting for tx status -: ------------ > 3: 497a8968ca01 wifi: brcmfmac: pcie: probe again after a resume through firmware -: ------------ > 4: cb57a93e8140 wifi: brcmfmac: msgbuf: do not wait for tx status on the BCM4377 5: 3a92f7e83cf1 ! 5: 7323a6b5700e wifi: brcmfmac: pcie: support inband device wake and deep sleep @@ Commit message  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. + device wake. That leaves deep sleep disabled. The firmware stays awake + and its latency tolerance holds the CPU 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. + On these chips, advertise inband device wake when the firmware supports + it and the mailbox goes over the control ring. It comes on top of no + out-of-band device wake, as in Broadcom's DHD driver.  - 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. + The host then follows the handshake of the firmware: + - The device asks for deep sleep while device wake is deasserted. + - The host acknowledges once it has nothing to send. + - Before it uses the rings again, it asserts device wake and waits for + the exit note. + - After 200 ms without ring writes, it deasserts device wake.  + Hooks around the ring lock cover every host to device ring writer. A + system suspend asserts device wake before D3_INFORM and keeps the + device out of deep sleep until the resume. Removing the device asserts + device wake first, so brcmf_detach() still reaches it. + + Tested on a MacBookPro15,1 (BCM4364 B2), a MacBookPro16,1 (BCM4364 B3) + and a MacBookAir9,1 (BCM4377). At idle with Wi-Fi associated, the + firmware enters and leaves deep sleep, also after S3. Traffic works, + and the driver unloads with deep sleep active. The CPU package reaches + PC10 without ignoring the latency tolerance of the Wi-Fi port. + + Tested-by: Yossef Osman  Signed-off-by: Andre Eikmeyer   ## drivers/net/wireless/broadcom/brcm80211/brcmfmac/commonring.c ## @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: struct brcmf_otp_params enum brcmf_pcie_state state; bool in_irq;  @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: struct brcmf_pciedev_info { - struct brcmf_mp_device *settings; - struct brcmf_otp_params otp; - bool fwseed; + char sig_name[BRCMF_FW_NAME_LEN]; + u32 fw_size; + bool skip_reset_vector;  + /* protects ds_state, ds_active and ds_suspend */  + spinlock_t ds_lock;  + enum brcmf_pcie_ds_state ds_state; @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: struct brcmf_pciedev_in  + /* serializes the deep sleep mailbox messages */  + struct mutex ds_mutex;  + struct task_struct *ds_sender; ++ /* the interrupt thread while it runs */ ++ struct task_struct *irq_thread;  + wait_queue_head_t ds_wait;  + struct delayed_work ds_deassert_work; #ifdef DEBUG @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_send_mb_data }   -+static void brcmf_pcie_ds_send(struct brcmf_pciedev_info *devinfo, u32 data) ++static int brcmf_pcie_ds_send(struct brcmf_pciedev_info *devinfo, u32 data)  +{ ++ int err; ++  + mutex_lock(&devinfo->ds_mutex);  + WRITE_ONCE(devinfo->ds_sender, current); -+ brcmf_pcie_send_mb_data(devinfo, data); ++ err = brcmf_pcie_send_mb_data(devinfo, data);  + WRITE_ONCE(devinfo->ds_sender, NULL);  + mutex_unlock(&devinfo->ds_mutex); ++ ++ return err;  +}  +  +/* The handshake messages themselves go through the hooked rings */ @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_send_mb_data  + return false;  +}  + ++/* Without the ack the device stays awake */ ++static void brcmf_pcie_ds_ack(struct brcmf_pciedev_info *devinfo) ++{ ++ if (!brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DS_ACK)) ++ return; ++ ++ spin_lock(&devinfo->ds_lock); ++ if (devinfo->ds_state == BRCMF_PCIE_DS_SLEEP) ++ devinfo->ds_state = BRCMF_PCIE_DS_ALLOWED; ++ spin_unlock(&devinfo->ds_lock); ++} ++ ++static bool brcmf_pcie_ds_exited(struct brcmf_pciedev_info *devinfo) ++{ ++ return READ_ONCE(devinfo->ds_state) != BRCMF_PCIE_DS_EXIT_WAIT || ++ READ_ONCE(devinfo->state) != BRCMFMAC_PCIE_STATE_UP; ++} ++  +static void brcmf_pcie_ds_wake(struct brcmf_pciedev_info *devinfo)  +{ -+ bool assert = false, wait; ++ bool up, assert = false, wait;  +  + if (brcmf_pcie_ds_skip(devinfo))  + return;  + ++ /* A device that is not up does not answer */ ++ up = READ_ONCE(devinfo->state) == BRCMFMAC_PCIE_STATE_UP;  + spin_lock(&devinfo->ds_lock);  + devinfo->ds_active++; -+ if (devinfo->ds_state == BRCMF_PCIE_DS_SLEEP) { ++ if (up && 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; ++ wait = up && 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 (brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DW_ASSERT)) { ++ spin_lock(&devinfo->ds_lock); ++ if (devinfo->ds_state == BRCMF_PCIE_DS_EXIT_WAIT) ++ devinfo->ds_state = BRCMF_PCIE_DS_SLEEP; ++ spin_unlock(&devinfo->ds_lock); ++ wake_up(&devinfo->ds_wait); ++ return; ++ }  + }  + if (!wait)  + return;  +  + /* The exit note arrives through the interrupt thread itself */ -+ if (devinfo->in_irq) { ++ if (READ_ONCE(devinfo->irq_thread) == current) {  + usleep_range(1000, 1100);  + return;  + } -+ if (wait_event_timeout(devinfo->ds_wait, -+ READ_ONCE(devinfo->ds_state) != -+ BRCMF_PCIE_DS_EXIT_WAIT, ++ if (wait_event_timeout(devinfo->ds_wait, brcmf_pcie_ds_exited(devinfo),  + BRCMF_PCIE_DS_EXIT_TIMEOUT))  + return;  + @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_send_mb_data  +  + if (ack) {  + brcmf_dbg(PCIE, "deep sleep: ack pending request\n"); -+ brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DS_ACK); ++ brcmf_pcie_ds_ack(devinfo);  + }  +}  + @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_send_mb_data  + }  + spin_unlock(&devinfo->ds_lock);  + -+ if (deassert) { -+ brcmf_dbg(PCIE, "deep sleep: deassert device wake\n"); -+ brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DW_DEASSERT); ++ if (!deassert) ++ return; ++ ++ brcmf_dbg(PCIE, "deep sleep: deassert device wake\n"); ++ if (brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DW_DEASSERT)) { ++ spin_lock(&devinfo->ds_lock); ++ if (devinfo->ds_state == BRCMF_PCIE_DS_ALLOWED) ++ devinfo->ds_state = BRCMF_PCIE_DS_AWAKE; ++ spin_unlock(&devinfo->ds_lock);  + }  +}  + @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_send_mb_data  + spin_unlock(&devinfo->ds_lock);  +  + if (ack) -+ brcmf_pcie_ds_send(devinfo, BRCMF_H2D_HOST_DS_ACK); ++ brcmf_pcie_ds_ack(devinfo);  +}  +  +static void brcmf_pcie_ds_exit_note(struct brcmf_pciedev_info *devinfo) @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_send_mb_data if (dtoh_mb_data & BRCMF_D2H_DEV_D3_ACK) { brcmf_dbg(PCIE, "D2H_MB_DATA: D3 ACK\n"); devinfo->mbdata_completed = true; +@@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static irqreturn_t brcmf_pcie_isr_thread(int irq, void *arg) + return IRQ_HANDLED; +  + devinfo->in_irq = true; ++ WRITE_ONCE(devinfo->irq_thread, current); + status = brcmf_pcie_read_pcie32(devinfo, devinfo->reginfo->mailboxint); + brcmf_dbg(PCIE, "Enter %x\n", status); + if (status) { +@@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static irqreturn_t brcmf_pcie_isr_thread(int irq, void *arg) + brcmf_pcie_write_pcie32(devinfo, devinfo->reginfo->mailboxmask, + devinfo->reginfo->int_d2h_db | + devinfo->reginfo->int_fn0); ++ WRITE_ONCE(devinfo->irq_thread, NULL); + devinfo->in_irq = false; + return IRQ_HANDLED; + }  @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_alloc_dma_and_ring(struct brcmf_pciedev_info *devinfo, u32 ring_id, brcmf_pcie_ring_mb_update_wptr, brcmf_pcie_ring_mb_write_rptr, @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_init_share_r 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) { ++ /* ++ * Inband device wake comes on top of no out-of-band device ++ * wake, as Broadcom's DHD driver sets it. The handshake is ++ * tested with the mailbox on the control ring of the BCM4364 ++ * and BCM4377. ++ */ ++ if ((shared->flags & BRCMF_PCIE_SHARED_INBAND_DS) && ++ shared->mb_via_ctl && ++ (devinfo->ci->chip == BRCM_CC_4364_CHIP_ID || ++ devinfo->ci->chip == BRCM_CC_4377_CHIP_ID)) {  + host_cap |= BRCMF_HOSTCAP_DS_INBAND_DW;  + devinfo->ds_state = BRCMF_PCIE_DS_ALLOWED;  + } @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_remove(struc return;  devinfo = bus->bus_priv.pcie->devinfo; -+ WRITE_ONCE(devinfo->ds_state, BRCMF_PCIE_DS_NONE); -+ cancel_delayed_work_sync(&devinfo->ds_deassert_work); ++ if (devinfo->ds_state != BRCMF_PCIE_DS_NONE) { ++ /* brcmf_detach() still sends ioctls and flowring deletes */ ++ brcmf_pcie_ds_wake(devinfo); ++ spin_lock(&devinfo->ds_lock); ++ devinfo->ds_state = BRCMF_PCIE_DS_NONE; ++ spin_unlock(&devinfo->ds_lock); ++ /* nothing queues it once the state is NONE */ ++ cancel_delayed_work_sync(&devinfo->ds_deassert_work); ++ } brcmf_pcie_bus_console_read(devinfo, false); brcmf_pcie_fwcon_timer(devinfo, false);  +@@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: brcmf_pcie_remove(struct pci_dev *pdev) + #ifdef CONFIG_PM +  +  ++/* The device is awake, let it sleep again once idle */ ++static void brcmf_pcie_ds_end_suspend(struct brcmf_pciedev_info *devinfo) ++{ ++ spin_lock(&devinfo->ds_lock); ++ if (devinfo->ds_state != BRCMF_PCIE_DS_NONE) { ++ devinfo->ds_suspend = false; ++ devinfo->ds_state = BRCMF_PCIE_DS_AWAKE; ++ brcmf_pcie_ds_idle_locked(devinfo); ++ } ++ spin_unlock(&devinfo->ds_lock); ++} ++ + static int brcmf_pcie_pm_enter_D3(struct device *dev) + { + struct brcmf_pciedev_info *devinfo;  @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static int brcmf_pcie_pm_enter_D3(struct device *dev) brcmf_pcie_fwcon_timer(devinfo, false); brcmf_bus_change_state(bus, BRCMF_BUS_DOWN); @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static int brcmf_pcie_p devinfo->mbdata_completed = false; brcmf_pcie_send_mb_data(devinfo, BRCMF_H2D_HOST_D3_INFORM);  +@@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static int brcmf_pcie_pm_enter_D3(struct device *dev) + if (!devinfo->mbdata_completed) { + if (devinfo->wowl_enabled) { + brcmf_err(bus, "Timeout on response for entering D3 substate\n"); ++ brcmf_pcie_ds_end_suspend(devinfo); + brcmf_bus_change_state(bus, BRCMF_BUS_UP); + return -EIO; + }  @@ drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c: static int brcmf_pcie_pm_leave_D3(struct device *dev) if (brcmf_pcie_send_mb_data(devinfo, BRCMF_H2D_HOST_D0_INFORM)) goto cleanup; brcmf_dbg(PCIE, "Hot resume, continue....\n"); -+ if (devinfo->ds_state != BRCMF_PCIE_DS_NONE) { -+ spin_lock(&devinfo->ds_lock); -+ devinfo->ds_suspend = false; -+ devinfo->ds_state = BRCMF_PCIE_DS_AWAKE; -+ brcmf_pcie_ds_idle_locked(devinfo); -+ spin_unlock(&devinfo->ds_lock); -+ } ++ brcmf_pcie_ds_end_suspend(devinfo); brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); brcmf_bus_change_state(bus, BRCMF_BUS_UP); brcmf_pcie_intr_enable(devinfo);