brcmfmac registers no pci_error_handlers, so a fatal AER error leaves the device dead until reboot ("AER: can't recover (no error_detected callback)"). On a BCM4366 this happens after some firmware traps: the trap starts a driver reset, and about 0.7 s later the downstream port reports an FCP error. Add error handlers: - error_detected: mark the bus down, block the driver's own reset work and request a link reset. - slot_reset: restore the config space saved at probe, then check that the backplane answers; a wedged card would stall the CPU on every MMIO read during firmware download. - resume: re-probe the device from a work item. The link reset returns the chip to its power-on state, which brcmf_pcie_reset() does not fully re-initialise: after it, association failed in 5 of 9 runs, while 16 of 16 re-probes recovered. A dongle setup that fails while recovery is in progress no longer releases the driver, as that would race with the re-probe. Tested with aer_inject (FCP on the downstream port), alone and 0.3 s after a debugfs-triggered reset: WiFi back within 6-12 s in 16 of 16 runs, on a BCM4366/4 (14e4:43c3) with a kernel built from the Ubuntu 7.0.0-42 source, which is based on v7.0.14. Also tested against real FCP errors, without injection: resets under WiFi traffic caused 3 in 47 runs, and all 3 recovered within 4-11 s. This relies on the console read fix earlier in this series: without it, a reset that is already running on the dead device never finishes, and error_detected() waits for it forever. Found and written with the help of an AI coding assistant (Claude Opus 5.5); the cover letter describes how. Signed-off-by: Soeren Coremans --- .../broadcom/brcm80211/brcmfmac/pcie.c | 128 +++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index 5abc85013110..9c23e5b83b73 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -344,6 +344,7 @@ struct brcmf_otp_params { struct brcmf_pciedev_info { enum brcmf_pcie_state state; resource_size_t tcm_size; + bool in_pci_recovery; bool in_irq; struct pci_dev *pdev; char fw_name[BRCMF_FW_NAME_LEN]; @@ -2137,6 +2138,7 @@ static int brcmf_pcie_get_resource(struct brcmf_pciedev_info *devinfo) } pci_set_master(pdev); + pci_save_state(pdev); /* Bar-0 mapped address */ bar0_addr = pci_resource_start(pdev, 0); @@ -2598,6 +2600,11 @@ static void brcmf_pcie_setup(struct device *dev, int ret, fail: brcmf_err(bus, "Dongle setup failed\n"); + /* PCIe error recovery is in progress and re-probes the device once + * the link is back; releasing the driver here would race with it. + */ + if (READ_ONCE(devinfo->in_pci_recovery)) + return; brcmf_pcie_bus_console_read(devinfo, true); brcmf_fw_crashed(dev); device_release_driver(dev); @@ -3153,11 +3160,117 @@ static const struct pci_device_id brcmf_pcie_devid_table[] = { MODULE_DEVICE_TABLE(pci, brcmf_pcie_devid_table); +static pci_ers_result_t brcmf_pcie_error_detected(struct pci_dev *pdev, + pci_channel_state_t state) +{ + struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); + struct brcmf_pciedev_info *devinfo; + + if (!bus) + return PCI_ERS_RESULT_DISCONNECT; + if (state == pci_channel_io_perm_failure) + return PCI_ERS_RESULT_DISCONNECT; + if (state == pci_channel_io_normal) + return PCI_ERS_RESULT_CAN_RECOVER; + + devinfo = bus->bus_priv.pcie->devinfo; + brcmf_err(bus, "PCIe link frozen, resetting\n"); + WRITE_ONCE(devinfo->in_pci_recovery, true); + devinfo->state = BRCMFMAC_PCIE_STATE_DOWN; + brcmf_bus_cancel_reset_work(bus); + + return PCI_ERS_RESULT_NEED_RESET; +} + +static pci_ers_result_t brcmf_pcie_slot_reset(struct pci_dev *pdev) +{ + struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); + struct brcmf_pciedev_info *devinfo; + u16 vendor; + + if (!bus) + return PCI_ERS_RESULT_DISCONNECT; + devinfo = bus->bus_priv.pcie->devinfo; + + pci_restore_state(pdev); + pci_save_state(pdev); + pci_set_master(pdev); + + pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor); + if (vendor != pdev->vendor) + return PCI_ERS_RESULT_DISCONNECT; + + /* Config space can answer while the backplane is wedged; reloading + * firmware into such a card stalls the CPU on every MMIO read. + */ + if (devinfo->ci) { + u32 chipid; + + brcmf_pcie_select_core(devinfo, BCMA_CORE_CHIPCOMMON); + chipid = READCC32(devinfo, chipid); + brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); + if (chipid == 0xffffffff) { + brcmf_err(bus, "card not responding after link reset\n"); + return PCI_ERS_RESULT_DISCONNECT; + } + } + + return PCI_ERS_RESULT_RECOVERED; +} + +static struct workqueue_struct *brcmf_pcie_err_wq; + +struct brcmf_pcie_reprobe { + struct work_struct work; + struct pci_dev *pdev; +}; + +static void brcmf_pcie_reprobe_worker(struct work_struct *work) +{ + struct brcmf_pcie_reprobe *rp = + container_of(work, struct brcmf_pcie_reprobe, work); + int err; + + err = device_reprobe(&rp->pdev->dev); + if (err) + pci_err(rp->pdev, "re-probe after PCIe error failed: %d\n", err); + pci_dev_put(rp->pdev); + kfree(rp); +} + +/* A link reset returns the chip to its power-on state, which only a full + * probe initialises correctly. The re-probe frees all driver data, so it + * runs from a work item that holds nothing but the PCI device. + */ +static void brcmf_pcie_resume_from_error(struct pci_dev *pdev) +{ + struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); + struct brcmf_pcie_reprobe *rp; + + if (!bus || !READ_ONCE(bus->bus_priv.pcie->devinfo->in_pci_recovery)) + return; + + rp = kzalloc_obj(*rp); + if (!rp) + return; + brcmf_err(bus, "PCIe link recovered, re-probing\n"); + INIT_WORK(&rp->work, brcmf_pcie_reprobe_worker); + rp->pdev = pci_dev_get(pdev); + queue_work(brcmf_pcie_err_wq, &rp->work); +} + +static const struct pci_error_handlers brcmf_pcie_err_handler = { + .error_detected = brcmf_pcie_error_detected, + .slot_reset = brcmf_pcie_slot_reset, + .resume = brcmf_pcie_resume_from_error, +}; + static struct pci_driver brcmf_pciedrvr = { .name = KBUILD_MODNAME, .id_table = brcmf_pcie_devid_table, .probe = brcmf_pcie_probe, .remove = brcmf_pcie_remove, + .err_handler = &brcmf_pcie_err_handler, #ifdef CONFIG_PM .driver.pm = &brcmf_pciedrvr_pm, #endif @@ -3167,8 +3280,17 @@ static struct pci_driver brcmf_pciedrvr = { int brcmf_pcie_register(void) { + int err; + brcmf_dbg(PCIE, "Enter\n"); - return pci_register_driver(&brcmf_pciedrvr); + brcmf_pcie_err_wq = alloc_workqueue("brcmf_pcie_err", 0, 0); + if (!brcmf_pcie_err_wq) + return -ENOMEM; + + err = pci_register_driver(&brcmf_pciedrvr); + if (err) + destroy_workqueue(brcmf_pcie_err_wq); + return err; } @@ -3176,4 +3298,8 @@ void brcmf_pcie_exit(void) { brcmf_dbg(PCIE, "Enter\n"); pci_unregister_driver(&brcmf_pciedrvr); + /* Unregistered first so no new re-probe can be queued; this drains + * a pending one before the module text goes away. + */ + destroy_workqueue(brcmf_pcie_err_wq); } -- 2.53.0