1: a89a0f9fd503 ! 1: 3115d8868d53 wifi: ath10k: fix SDIO aux workqueue starvation in recovery detection @@ Metadata ## Commit message ## wifi: ath10k: fix SDIO aux workqueue starvation in recovery detection  - Each recovery trigger queues recovery_check_work on workqueue_aux. If - a recovery is already pending, the work blocks for up to 5 seconds - waiting for it to finish, and marks the device WEDGED after four - failed waits. + Recovery monitoring runs on workqueue_aux, which is also + used by SDIO RX dispatch and WMI SERVICE_READY handling.  - On SDIO, one firmware crash can raise several triggers. workqueue_aux - also runs SDIO RX and WMI SERVICE_READY handling, so the blocked - check stalls the traffic the recovery itself is waiting for. Recovery - then fails with "ctl_resp never came in (-110)", and a recoverable - crash ends up WEDGED. + A single firmware crash may generate multiple recovery + triggers. Recovery detection currently handles each one by + waiting for recovery completion on workqueue_aux. On SDIO, + this blocks the same workqueue needed to deliver recovery + traffic, preventing recovery from completing.  - Stop waiting on workqueue_aux. Let only the first trigger start a - recovery, using atomic_cmpxchg() on pending_recovery, and ignore the - rest. Replace the wait-and-count logic with a single 20 second - delayed work, the same bound as four 5-second waits. If recovery has - not completed when it fires, mark the device WEDGED. Since the - deadline already covers elapsed time, fail_cont_count is no longer - needed and is removed. + As a result, otherwise recoverable firmware crashes fail + with: + ctl_resp never came in (-110)  - Because pending_recovery is now set only once per recovery, any path - that abandons a recovery must clear it. Otherwise the next crash is - ignored: the device is neither restarted nor marked WEDGED. Clear it - in ath10k_stop(), and in ath10k_core_restart() when the device is OFF - and there is nothing to restart. The OFF case was found by code - inspection and has not been reproduced. + Treat duplicate triggers as part of the same recovery attempt. Use an + atomic compare-and-exchange on pending_recovery so only the first + trigger starts restart_work. + + Convert the recovery monitor to self-rearming delayed work. While + recovery remains pending, the monitor periodically checks its state + without blocking workqueue_aux. Cancel the monitor when recovery + succeeds or the device is stopped. + + The monitor is armed for every trigger, so ath10k_core_restart() must + also disarm it when the device is in ATH10K_STATE_OFF and there is + nothing to restart, for example because ath10k_start() failed after the + firmware crashed while booting. Otherwise the stale pending_recovery + blocks the next real recovery and the monitor eventually marks the + device WEDGED even though it is only not started. Clear it there, and + arm the monitor before queueing restart_work so the cancel cannot run + before the monitor is armed. This case was found by code inspection and + has not been reproduced.  Tested-on: QCA6174 hw3.2 PCI WLAN.RM.4.4.1-00288-QCARMSWPZ-1 Tested-on: QCA6174 hw3.2 SDIO WLAN.RMH.4.4.1-00189 @@ drivers/net/wireless/ath/ath10k/core.c: static int ath10k_init_hw_params(struct  - long time_left;  + struct ath10k *ar = container_of(work, struct ath10k,  + recovery_check_work.work); ++ int fail_count;   - /* Sometimes the recovery will fail and then the next all recovery fail,  - * so avoid infinite recovery. @@ drivers/net/wireless/ath/ath10k/core.c: static int ath10k_init_hw_params(struct  + if (ar->state == ATH10K_STATE_WEDGED) return;  - } -- +   - ath10k_dbg(ar, ATH10K_DBG_BOOT, "total recovery count: %d", ++ar->recovery_count);  -  - if (atomic_read(&ar->pending_recovery)) { @@ drivers/net/wireless/ath/ath10k/core.c: static int ath10k_init_hw_params(struct  - ath10k_warn(ar, "previous recovery succeeded, skip this!\n");  - return;  - } ++ /* ++ * Pure monitor: only ath10k_core_start_recovery() starts a fresh ++ * cycle. Bail if there is nothing in flight. ++ */ ++ if (!atomic_read(&ar->pending_recovery)) ++ return;   - /* Record the continuous recovery fail count when recovery failed. */  - atomic_inc(&ar->fail_cont_count); -- ++ /* Record the continuous recovery fail count when recovery is late. */ ++ fail_count = atomic_inc_return(&ar->fail_cont_count); +   - /* Avoid having multiple recoveries at the same time. */ -+ /* -+ * Pure deadline monitor: it is armed by -+ * ath10k_core_start_recovery() and cancelled once recovery -+ * completes. Bail if there is nothing in flight. -+ */ -+ if (!atomic_read(&ar->pending_recovery)) ++ if (fail_count >= ATH10K_RECOVERY_MAX_FAIL_COUNT) { ++ ath10k_err(ar, "consecutive fail %d times, will shutdown driver!", ++ fail_count); ++ ar->state = ATH10K_STATE_WEDGED; return; -- } + }   - atomic_inc(&ar->pending_recovery);  - queue_work(ar->workqueue, &ar->restart_work); -+ ath10k_err(ar, "recovery did not complete in %u ms, will shutdown driver!\n", -+ jiffies_to_msecs(ATH10K_RECOVERY_TIMEOUT_HZ)); -+ ar->state = ATH10K_STATE_WEDGED; ++ /* Recovery still in flight; check again later. */ ++ queue_delayed_work(ar->workqueue_aux, &ar->recovery_check_work, ++ ATH10K_RECOVERY_TIMEOUT_HZ); }  void ath10k_core_start_recovery(struct ath10k *ar) @@ drivers/net/wireless/ath/ath10k/core.c: static int ath10k_init_hw_params(struct  + if (ar->state == ATH10K_STATE_WEDGED)  + return;  + -+ /* -+ * SDIO raises several triggers within microseconds of one crash; -+ * only the first winner starts a fresh cycle. Losers do nothing -+ * while the owner runs recovery. -+ */ ++ /* SDIO raises several triggers per crash; only the first one wins. */  + if (atomic_cmpxchg(&ar->pending_recovery, 0, 1) != 0)  + return;  + ath10k_dbg(ar, ATH10K_DBG_BOOT, "total recovery count: %d",  + ++ar->recovery_count); -+ /* Arm the monitor before queueing restart_work so that restart_work -+ * can cancel it if there turns out to be nothing to restart. -+ */ ++ /* Start a fresh failure budget; the last cycle may not have completed. */ ++ atomic_set(&ar->fail_cont_count, 0); ++ /* Arm the monitor first; restart_work may cancel it. */  + queue_delayed_work(ar->workqueue_aux, &ar->recovery_check_work,  + ATH10K_RECOVERY_TIMEOUT_HZ);  + queue_work(ar->workqueue, &ar->restart_work); @@ drivers/net/wireless/ath/ath10k/core.c: static void ath10k_core_restart(struct w * or if the crash happens during FW probing */ ath10k_warn(ar, "cannot restart a device that hasn't been started\n"); -+ /* Nothing will be restarted, so no recovery is pending any -+ * more. Leaving pending_recovery set would block the next real -+ * recovery and let the monitor wedge a device that is started -+ * later. -+ */ ++ /* Nothing to restart; drop the stale pending_recovery. */  + cancel_delayed_work(&ar->recovery_check_work);  + atomic_set(&ar->pending_recovery, 0); break; @@ drivers/net/wireless/ath/ath10k/core.c: EXPORT_SYMBOL(ath10k_core_create); destroy_workqueue(ar->workqueue_aux);  ## drivers/net/wireless/ath/ath10k/core.h ## -@@ - IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) - #define ATH10K_ITER_RESUME_FLAGS (IEEE80211_IFACE_ITER_RESUME_ALL |\ - IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) --#define ATH10K_RECOVERY_TIMEOUT_HZ (5 * HZ) --#define ATH10K_RECOVERY_MAX_FAIL_COUNT 4 -+#define ATH10K_RECOVERY_TIMEOUT_HZ (20 * HZ) -  - struct ath10k; -   @@ drivers/net/wireless/ath/ath10k/core.h: struct ath10k {  struct work_struct register_work; @@ drivers/net/wireless/ath/ath10k/core.h: struct ath10k { struct work_struct bundle_tx_work; struct work_struct tx_complete_work;  - atomic_t pending_recovery; - unsigned int recovery_count; -- /* continuous recovery fail count */ -- atomic_t fail_cont_count; -  - /* cycle count is reported twice for each visited channel during scan. - * access protected by data_lock  ## drivers/net/wireless/ath/ath10k/mac.c ##  @@ drivers/net/wireless/ath/ath10k/mac.c: static void ath10k_stop(struct ieee80211_hw *hw, bool suspend) @@ drivers/net/wireless/ath/ath10k/mac.c: static void ath10k_stop(struct ieee80211_  - cancel_work_sync(&ar->recovery_check_work);  + cancel_delayed_work_sync(&ar->recovery_check_work);  + -+ /* ath10k_stop() can race with an in-flight recovery. Clear the -+ * recovery state after the works are cancelled so a later -+ * ath10k_start() can start a fresh cycle instead of being blocked -+ * by a stale pending_recovery that nothing will clear. -+ */ ++ /* Works are cancelled; clear stale recovery state for next start. */ ++ atomic_set(&ar->fail_cont_count, 0);  + atomic_set(&ar->pending_recovery, 0); }  @@ drivers/net/wireless/ath/ath10k/mac.c: static void ath10k_reconfig_complete(stru ar->state = ATH10K_STATE_ON; ieee80211_wake_queues(ar->hw);  -+ /* Cancel the deadline monitor first so it cannot declare the -+ * device wedged after recovery has completed. -+ */ ++ /* Cancel the monitor first so it cannot bump the count after reset. */  + cancel_delayed_work_sync(&ar->recovery_check_work);  + /* Clear recovery state. */ complete(&ar->driver_recovery); -- atomic_set(&ar->fail_cont_count, 0); - atomic_set(&ar->pending_recovery, 0); -  - if (ar->hw_params.hw_restart_disconnect) { + atomic_set(&ar->fail_cont_count, 0);  -: ------------ > 2: ab184534de19 wifi: ath10k: replace the recovery failure count with a single deadline  2: 243bf3094623 ! 3: 0cd94de204a6 wifi: ath10k: shorten the recovery timeout to 10 seconds @@ Metadata ## Commit message ## wifi: ath10k: shorten the recovery timeout to 10 seconds  - If a recovery does not complete within ATH10K_RECOVERY_TIMEOUT_HZ - (20 seconds), the device is marked WEDGED. + A recovery that does not complete within ATH10K_RECOVERY_TIMEOUT_HZ + (20 s) marks the device WEDGED. Some platforms configure suspend + watchdogs as low as 25 seconds, so a recovery stuck during suspend can + consume nearly all of that budget before the device enters + ATH10K_STATE_WEDGED. That leaves insufficient time for blocked callers + to exit and for the driver to complete the WEDGED transition and shut + down.  - This is too long when recovery gets stuck during suspend. Some - platforms set the suspend watchdog as low as 25 seconds. A 20 second - timeout uses up most of that time, leaving only about 5 seconds for - blocked callers to return and for the driver to finish the WEDGED - transition. If that is not enough, the watchdog fires and the whole - system crashes. - - Reduce the timeout to 10 seconds. A normal recovery finishes well - within this limit, and the remaining time is enough for the WEDGED - transition to complete before the watchdog fires. + Reduce the timeout to 10 seconds.  Tested-on: QCA6174 hw3.2 PCI WLAN.RM.4.4.1-00288-QCARMSWPZ-1 Tested-on: QCA6174 hw3.2 SDIO WLAN.RMH.4.4.1-00189 @@ drivers/net/wireless/ath/ath10k/core.h #define ATH10K_ITER_RESUME_FLAGS (IEEE80211_IFACE_ITER_RESUME_ALL |\ IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER)  -#define ATH10K_RECOVERY_TIMEOUT_HZ (20 * HZ) -+/* -+ * Some platforms configure suspend watchdogs as low as 25s; keep the -+ * recovery timeout well under that so the WEDGED transition and any -+ * teardown that follows still have room to run before it fires. -+ */ ++/* Keep well under the 25s suspend watchdog some platforms configure. */  +#define ATH10K_RECOVERY_TIMEOUT_HZ (10 * HZ)  struct ath10k;  3: fe0ec6cb6283 = 4: df97ec1050db wifi: ath10k: wake blocked waiters when entering WEDGED state  4: 739b56a90769 ! 5: 2f7407dc0ad9 wifi: ath10k: also wake remaining blocked waiters @@ Commit message peer_delete_done is not otherwise reinitialized before every wait. A completion left signaled by recovery would therefore make a later peer-delete wait return immediately. Reinitialize it before sending - each peer-delete command. + each peer-delete command. Introduce ath10k_peer_delete_start() that + bundles reinit_completion() with ath10k_wmi_peer_delete() and use it + from all three call sites, so the reinit-before-send pattern is + applied in one place.  Tested-on: QCA6174 hw3.2 PCI WLAN.RM.4.4.1-00288-QCARMSWPZ-1 Tested-on: QCA6174 hw3.2 SDIO WLAN.RMH.4.4.1-00189 @@ drivers/net/wireless/ath/ath10k/core.c: static void ath10k_core_wake_all_waiters wake_up(&ar->wmi.tx_credits_wq);  ## drivers/net/wireless/ath/ath10k/mac.c ## -@@ drivers/net/wireless/ath/ath10k/mac.c: static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr) +@@ drivers/net/wireless/ath/ath10k/mac.c: static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value) + return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value); + } +  ++static int ath10k_peer_delete_start(struct ath10k *ar, u32 vdev_id, const u8 *addr) ++{ ++ reinit_completion(&ar->peer_delete_done); ++ return ath10k_wmi_peer_delete(ar, vdev_id, addr); ++} ++ + static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr) + { + int ret;  lockdep_assert_held(&ar->conf_mutex);  -+ reinit_completion(&ar->peer_delete_done); - ret = ath10k_wmi_peer_delete(ar, vdev_id, addr); +- ret = ath10k_wmi_peer_delete(ar, vdev_id, addr); ++ ret = ath10k_peer_delete_start(ar, vdev_id, addr); if (ret) return ret; +   @@ drivers/net/wireless/ath/ath10k/mac.c: static int ath10k_add_interface(struct ieee80211_hw *hw, err_peer_delete: if (arvif->vdev_type == WMI_VDEV_TYPE_AP || arvif->vdev_type == WMI_VDEV_TYPE_IBSS) { -+ reinit_completion(&ar->peer_delete_done); - ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr); +- ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr); ++ ath10k_peer_delete_start(ar, arvif->vdev_id, vif->addr); ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id, vif->addr); + }  @@ drivers/net/wireless/ath/ath10k/mac.c: static void ath10k_remove_interface(struct ieee80211_hw *hw,  if (arvif->vdev_type == WMI_VDEV_TYPE_AP || arvif->vdev_type == WMI_VDEV_TYPE_IBSS) { -+ reinit_completion(&ar->peer_delete_done); - ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id, - vif->addr); +- ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id, +- vif->addr); ++ ret = ath10k_peer_delete_start(arvif->ar, arvif->vdev_id, ++ vif->addr); if (ret) + ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n", + arvif->vdev_id, ret);  5: 6f5121f94d61 ! 6: 721ce6c59e2f wifi: ath10k: hard-block the radio when entering WEDGED state @@ drivers/net/wireless/ath/ath10k/core.c: static void ath10k_core_enter_wedged(str ieee80211_stop_queues(ar->hw); ath10k_core_wake_all_waiters(ar);  + -+ /* -+ * Signal the radio as hard-blocked so userspace (e.g. -+ * wpa_supplicant, NetworkManager) stops looping scan and connect -+ * requests at a device the driver has already given up on. The -+ * state is cleared only when the driver is reloaded. -+ */ ++ /* Hard-block so userspace stops retrying; cleared on driver reload. */  + wiphy_rfkill_set_hw_state(ar->hw->wiphy, true); }   6: fac835d8f105 ! 7: 876dd397a177 wifi: ath10k: remove now-unused driver_recovery completion @@ drivers/net/wireless/ath/ath10k/core.h: struct ath10k {  ## drivers/net/wireless/ath/ath10k/mac.c ##  @@ drivers/net/wireless/ath/ath10k/mac.c: static void ath10k_reconfig_complete(struct ieee80211_hw *hw, - cancel_delayed_work_sync(&ar->recovery_check_work); -  - /* Clear recovery state. */ + ath10k_info(ar, "device successfully recovered\n"); + ar->state = ATH10K_STATE_ON; + ieee80211_wake_queues(ar->hw);  - complete(&ar->driver_recovery); - atomic_set(&ar->pending_recovery, 0);  if (ar->hw_params.hw_restart_disconnect) { + list_for_each_entry(arvif, &ar->arvifs, list) {  ## drivers/net/wireless/ath/ath10k/snoc.c ##  @@ drivers/net/wireless/ath/ath10k/snoc.c: static void ath10k_snoc_remove(struct platform_device *pdev)  7: 702059be44b0 ! 8: 80e4fd0362a7 wifi: ath10k: trigger recovery on PCIe MMIO wake-up failures @@ drivers/net/wireless/ath/ath10k/pci.c: static void ath10k_bus_pci_write32(struct ath10k_warn(ar, "failed to wake target for write32 of 0x%08x at 0x%08x: %d\n", value, offset, ret);  + /* -+ * wake_wait timed out; the device is unresponsive at the -+ * MMIO level. write32 returns void so the write is -+ * silently dropped -- kick recovery here instead of -+ * letting an upper WMI wait notice it seconds later. -+ * Guards confine this to runtime; cmpxchg dedups. ++ * The device is unresponsive and the write is silently ++ * dropped; recover now instead of waiting for a WMI timeout.  + */  + if (test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags) &&  + ar->state == ATH10K_STATE_ON)  8: ff7696fb570b ! 9: 45e2b36fdf43 wifi: ath10k: trigger recovery on corrupted HTC bundle lookahead @@ drivers/net/wireless/ath/ath10k/sdio.c: static int ath10k_sdio_mbox_alloc_bundle le16_to_cpu(htc_hdr->len), max_msgs);  + ath10k_core_start_recovery(ar); -+ /* Recovery is a no-op if already pending, so also mask the -+ * target interrupts to stop the IRQ storm. -+ */ ++ /* Prevent IRQ storm. */  + ath10k_sdio_disable_intrs(ar); return -ENOMEM; }  9: 5cd635df90d5 = 10: d78ebf2d1f90 wifi: ath10k: reject scan requests while the device is not running  -: ------------ > 11: f6f5fe1a08f2 wifi: ath10k: move sdio irqs_disabled completion off the stack 10: f82e7dffbe87 ! 12: 2ce99acac994 wifi: ath10k: drop async writes and sleep timer across power cycles @@ drivers/net/wireless/ath/ath10k/sdio.c: static void ath10k_sdio_sleep_timer_hand queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work); }  -+/* Drop any requests still queued on wr_asyncq without touching the bus. -+ * Caller must ensure wr_async_work is not running concurrently. -+ */ ++/* Drop queued wr_asyncq requests; caller must ensure wr_async_work is idle. */  +static void ath10k_sdio_drop_wr_async_reqs(struct ath10k *ar)  +{  + struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar); @@ drivers/net/wireless/ath/ath10k/sdio.c: static void ath10k_sdio_write_async_work struct ath10k_sdio_bus_request *req, *tmp_req; struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;  -+ /* The card is powered down; drop pending requests instead of -+ * touching the bus. Prevents stale writes from racing with a -+ * concurrent BMI power-up on the next recovery cycle. -+ */ ++ /* Card is powered down; drop requests instead of touching the bus. */  + if (ar_sdio->is_disabled) {  + ath10k_sdio_drop_wr_async_reqs(ar);  + return; @@ drivers/net/wireless/ath/ath10k/sdio.c: static int ath10k_sdio_hif_power_up(stru if (!ar_sdio->is_disabled) return 0;  -+ /* Flush any sleep timer or async work left over from the previous -+ * cycle so they cannot fire during BMI and poke the bus. -+ */ ++ /* Flush leftover sleep timer and async work from the last cycle. */  + timer_delete_sync(&ar_sdio->sleep_timer);  + cancel_work_sync(&ar_sdio->wr_async_work);  + ath10k_sdio_drop_wr_async_reqs(ar); @@ drivers/net/wireless/ath/ath10k/sdio.c: static void ath10k_sdio_hif_power_down(s  ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power off\n");  -+ /* Reject new async writes from ath10k_sdio_hif_tx_sg() and make -+ * ath10k_sdio_write_async_work() drop any already-queued -+ * requests before they touch the bus. -+ */ ++ /* Reject new async writes and drop already-queued ones. */  + ar_sdio->is_disabled = true;  + timer_delete_sync(&ar_sdio->sleep_timer);