Skip to contentSkip to conversation

wifi: rtw88: firmware recovery fixes and LPS hang handling

PATCH In review Kalpesh Solanki 3 participants 6 messages Nov 13 – Nov 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover wifi: rtw88: firmware recovery fixes and LPS hang handling

This series addresses firmware recovery robustness and LPS hang handling in rtw88 based on review feedback from Luka Gejak and Ping-Ke Shih.

Patch 1/2 from the v2 series (DMI quirk for HP Laptop 15s-fr5xxx) was already applied to rtw-next. The previous LPS leave recovery patch was dropped from rtw-next by Ping-Ke Shih for respin.

This series splits the recovery fixes into three focused patches:

1. Patch 1/3: Clear RTW_FLAG_RESTARTING on stop and start failure. mac80211's drv_stop() skips local->ops->stop() when local->started is false (such as after a failed drv_start()). If rtw_core_start() fails during restart, clearing the flag only in rtw_ops_stop() left it permanently set. We now clear RTW_FLAG_RESTARTING in both rtw_ops_stop() and in rtw_ops_start() upon rtw_core_start() failure.

2. Patch 2/3: Skip already reset station in rtw_sta_remove(). During station disconnect (e.g., ieee80211_set_disassoc()), mac80211 leaves LPS before removing the AP station. If recovery is triggered by LPS leave failure, __fw_recovery_work() removes all stations via rtw_reset_sta_iter(). When mac80211 subsequently calls rtw_ops_sta_remove(), rtw_sta_remove() ran a second time, resulting in a double free of si->mask and underflow of rtwdev->sta_cnt. We check si->vif after ensuring pending rate control work is cancelled, and clear si->vif, si->mask, and si->use_cfg_mask upon removal.

3. Patch 3/3: Trigger firmware recovery only after consecutive LPS leave failures on PCI.
Restricts recovery to PCI devices (rtw_hci_type == RTW_HCI_TYPE_PCIE) because USB (usb_control_msg()) and SDIO (sdio_claim_host()) sleep, which triggers a "scheduling while atomic" panic under local->key_lock during key clearing in __fw_recovery_work(). Retains rtw_fw_dump_dbg_info() on the first failure (guarded by RTW_DBG_FW), and requires 3 consecutive timeouts (RTW_PS_HANG_MAX_CNT) before triggering hardware recovery. Gated on FW_FEATURE_LPS_C2H because legacy chips clear REG_TCR on failure which masks stuck firmware from accumulating consecutive timeouts. ps_hang_cnt is reset on success and in rtw_core_start().

Changes in v6:
- In patch 1/3:
- Pick up Reviewed-by tag from Luka Gejak.
- In patch 2/3:
- Keep cancel_work_sync(&si->rc_work) before checking !vif so that rate control work scheduled by rtw_ops_sta_rc_update() after recovery reset is not skipped if the station is removed before restart adds it back.
- Clear si->use_cfg_mask = false alongside si->mask = NULL to prevent a NULL pointer dereference in rtw_rate_mask_cfg() if mac80211 re-adds the station across recovery restart.
- In patch 3/3:
- Pick up Reviewed-by tag from Luka Gejak.

Changes in v5:
- In patch 3/3:
- Drop redundant rtw_hci_type check in rtw_fw_recovery(); the check in rtw_fw_leave_lps_check() and the existing PCI-only callers already guarantee non-PCI devices never invoke recovery.
- Only increment ps_hang_cnt and trigger recovery when the firmware supports FW_FEATURE_LPS_C2H. On legacy chipsets without C2H support, clearing REG_TCR on failure causes subsequent register checks to pass immediately, resetting the counter and preventing stuck firmware from ever reaching the threshold.

Changes in v4:
- In patch 1/3: Also clear RTW_FLAG_RESTARTING in rtw_ops_start() if rtw_core_start() fails.
- Add patch 2/3: Prevent double free and sta_cnt underflow in rtw_sta_remove() when recovery races with station disconnect.
- In patch 3/3:
- Keep rtw_fw_dump_dbg_info() in place on first failure.
- Restrict firmware recovery invocation to PCIe devices to prevent scheduling while atomic panic on USB and SDIO.
- Explicitly check rtw_hci_type() in rtw_fw_recovery().

Changes in v3:
- Note: v2 patch 1/2 (DMI quirk) is already applied in rtw-next.
- Add patch 1/2 to clear RTW_FLAG_RESTARTING in rtw_ops_stop() with Fixes tag.
- In patch 2/2:
- Count consecutive failures before triggering recovery

    (RTW_PS_HANG_MAX_CNT = 3).

- Reset ps_hang_cnt on successful LPS leave and in rtw_core_start().
- Suppress raw register dump on transient failures to avoid log spam.

Changes in v2:
- Rebase on top of rtw-next.

Kalpesh Solanki (3):
wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure
wifi: rtw88: skip already reset station in rtw_sta_remove()
wifi: rtw88: ps: trigger firmware recovery only after consecutive LPS
leave failures on PCI

 drivers/net/wireless/realtek/rtw88/mac80211.c | 3 +++
 drivers/net/wireless/realtek/rtw88/main.c     | 8 ++++++++
 drivers/net/wireless/realtek/rtw88/main.h     | 1 +
 drivers/net/wireless/realtek/rtw88/ps.c       | 9 +++++++++
 drivers/net/wireless/realtek/rtw88/ps.h       | 1 +
 5 files changed, 22 insertions(+)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v6 1/3 wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure

When mac80211 stops or suspends the device without WoWLAN, drv_reconfig_complete() is never invoked with IEEE80211_RECONFIG_TYPE_RESTART. If firmware recovery was in flight during this transition, RTW_FLAG_RESTARTING remains set permanently, blocking all subsequent firmware recoveries until the module is reloaded.

Similarly, if rtw_core_start() fails during drv_start() in a recovery restart, local->started remains false. When mac80211 cleans up via cfg80211_shutdown_all_interfaces(), drv_stop() returns early without calling rtw_ops_stop(), leaving RTW_FLAG_RESTARTING set permanently on hardware that failed to recover.

Clear RTW_FLAG_RESTARTING in rtw_ops_stop(), as well as in rtw_ops_start() when rtw_core_start() returns an error, ensuring the driver always exits the restarting state on error or teardown.

Fixes 5c831644e1f4 ("rtw88: handle and recover when firmware crash")
---
 drivers/net/wireless/realtek/rtw88/mac80211.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/wireless/realtek/rtw88/mac80211.c +3 -0
index 5e40cf06a9e..ffd86f08185 100644
@@ -57,6 +57,8 @@ static int rtw_ops_start(struct ieee80211_hw *hw)
 
 	mutex_lock(&rtwdev->mutex);
 	ret = rtw_core_start(rtwdev);
+	if (ret)
+		clear_bit(RTW_FLAG_RESTARTING, rtwdev->flags);
 	mutex_unlock(&rtwdev->mutex);
 
 	return ret;
@@ -67,6 +69,7 @@ static void rtw_ops_stop(struct ieee80211_hw *hw, bool suspend)
 	struct rtw_dev *rtwdev = hw->priv;
 
 	mutex_lock(&rtwdev->mutex);
+	clear_bit(RTW_FLAG_RESTARTING, rtwdev->flags);
 	rtw_core_stop(rtwdev);
 	mutex_unlock(&rtwdev->mutex);
 }
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH v6 1/3] wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure

Quoted 12 lines · Kalpesh Solanki

When mac80211 stops or suspends the device without WoWLAN, drv_reconfig_complete() is never invoked with IEEE80211_RECONFIG_TYPE_RESTART. If firmware recovery was in flight during this transition, RTW_FLAG_RESTARTING remains set permanently, blocking all subsequent firmware recoveries until the module is reloaded.

Similarly, if rtw_core_start() fails during drv_start() in a recovery restart, local->started remains false. When mac80211 cleans up via cfg80211_shutdown_all_interfaces(), drv_stop() returns early without calling rtw_ops_stop(), leaving RTW_FLAG_RESTARTING set permanently on hardware that failed to recover.

Quoted 8 lines

Clear RTW_FLAG_RESTARTING in rtw_ops_stop(), as well as in rtw_ops_start() when rtw_core_start() returns an error, ensuring the driver always exits the restarting state on error or teardown.

Fixes 5c831644e1f4 ("rtw88: handle and recover when firmware crash")

3 patch(es) applied to rtw-next branch of rtw.git, thanks.

3f2238709f5d wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure b719e9bced93 wifi: rtw88: skip already reset station in rtw_sta_remove()
27fdc67865bd wifi: rtw88: ps: trigger firmware recovery only after consecutive LPS leave failures on PCI

---
https://github.com/pkshih/rtw.git

#3 lore raw

PATCH v6 2/3 wifi: rtw88: skip already reset station in rtw_sta_remove()

In reply to Kalpesh Solanki

During firmware recovery, __fw_recovery_work() iterates over all stations and invokes rtw_sta_remove() via rtw_reset_sta_iter().

If recovery is triggered concurrently with station teardown (such as during ieee80211_set_disassoc() where mac80211 leaves LPS before removing the AP station), rtw_ops_sta_remove() is subsequently called for the same station. This causes rtw_sta_remove() to execute twice, leading to a double free of si->mask and an underflow of rtwdev->sta_cnt.

Cancel pending rate control work first, as rtw_ops_sta_rc_update() can still schedule rc_work after the station was reset. Then check si->vif and return early if the station was already reset. Clear si->vif, si->mask, and si->use_cfg_mask upon removal to ensure clean teardown and avoid a NULL pointer dereference in rtw_rate_mask_cfg() if mac80211 re-adds the station across recovery restart.

Fixes 5c831644e1f4 ("rtw88: handle and recover when firmware crash")
---
 drivers/net/wireless/realtek/rtw88/main.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/net/wireless/realtek/rtw88/main.c +6 -0
index f7708ad17b6..8e62d38a269 100644
@@ -386,6 +386,9 @@ void rtw_sta_remove(struct rtw_dev *rtwdev, struct ieee80211_sta *sta,
 
 	cancel_work_sync(&si->rc_work);
 
+	if (!vif)
+		return;
+
 	if (vif->type != NL80211_IFTYPE_STATION || sta->tdls)
 		rtw_release_macid(rtwdev, si->mac_id);
 	if (fw_exist)
@@ -395,8 +398,11 @@ void rtw_sta_remove(struct rtw_dev *rtwdev, struct ieee80211_sta *sta,
 		rtw_txq_cleanup(rtwdev, sta->txq[i]);
 
 	kfree(si->mask);
+	si->mask = NULL;
+	si->use_cfg_mask = false;
 
 	rtwdev->sta_cnt--;
+	si->vif = NULL;
 	rtw_dbg(rtwdev, RTW_DBG_STATE, "sta %pM with macid %d left\n",
 		sta->addr, si->mac_id);
 }
Signature · 1 line
2.53.0
Reviewed-by on v6 2/3
#4 lore raw

Re: [PATCH v6 2/3] wifi: rtw88: skip already reset station in rtw_sta_remove()

Quoted 16 lines · Kalpesh Solanki

During firmware recovery, __fw_recovery_work() iterates over all stations and invokes rtw_sta_remove() via rtw_reset_sta_iter().

If recovery is triggered concurrently with station teardown (such as during ieee80211_set_disassoc() where mac80211 leaves LPS before removing the AP station), rtw_ops_sta_remove() is subsequently called for the same station. This causes rtw_sta_remove() to execute twice, leading to a double free of si->mask and an underflow of rtwdev->sta_cnt.

Cancel pending rate control work first, as rtw_ops_sta_rc_update() can still schedule rc_work after the station was reset. Then check si->vif and return early if the station was already reset. Clear si->vif, si->mask, and si->use_cfg_mask upon removal to ensure clean teardown and avoid a NULL pointer dereference in rtw_rate_mask_cfg() if mac80211 re-adds the station across recovery restart.

Quoted 4 lines
Fixes 5c831644e1f4 ("rtw88: handle and recover when firmware crash")

---

Looks good to me,

Best regards,
Luka Gejak

#5 lore raw

PATCH v6 3/3 wifi: rtw88: ps: trigger firmware recovery only after consecutive LPS leave failures on PCI

In reply to Kalpesh Solanki

A single timeout in rtw_fw_leave_lps_check() does not always indicate a frozen firmware; in noisy RF environments or during beacon loss, the 802.11 null data frame ACK can be delayed or dropped. Immediately triggering firmware recovery and a full hardware restart on a single timeout causes unnecessary disconnections and kernel warning traces on idle links.

Follow rtw89 by introducing a consecutive failure counter (ps_hang_cnt) and only triggering rtw_fw_recovery() after RTW_PS_HANG_MAX_CNT (3) consecutive failures. Reset the counter to 0 on every successful LPS leave and in rtw_core_start() so that cold starts and hardware restarts always begin with a clean slate.

On chipsets without FW_FEATURE_LPS_C2H (such as RTL8822B and RTL8723D), rtw_fw_leave_lps_check() falls back to polling REG_TCR for BIT_PWRMGT_HWDATA_EN. On timeout, the driver forcefully clears this bit so subsequent packets can be transmitted. Because stuck firmware never re-asserts the bit, subsequent register checks immediately pass, clearing ps_hang_cnt back to 0 and preventing the counter from ever accumulating. Therefore, only count consecutive failures on devices with FW_FEATURE_LPS_C2H, where C2H completion signaling reliably detects consecutive timeouts.

Additionally, rtw_fw_recovery() clears keys via rtw_iterate_keys_atomic() under a spinlock. On USB and SDIO, register writes in key clearing sleep (via usb_control_msg() and sdio_claim_host()), which would trigger a

BUG: scheduling while atomic if executed. Because USB and SDIO do not

support firmware recovery, restrict firmware recovery invocation to PCI devices via rtw_hci_type().

---
 drivers/net/wireless/realtek/rtw88/main.c | 2 ++
 drivers/net/wireless/realtek/rtw88/main.h | 1 +
 drivers/net/wireless/realtek/rtw88/ps.c   | 9 +++++++++
 drivers/net/wireless/realtek/rtw88/ps.h   | 1 +
 4 files changed, 13 insertions(+)
drivers/net/wireless/realtek/rtw88/main.c +2 -0
index 8e62d38a269..2a38a6828e3 100644
@@ -1537,6 +1537,8 @@ int rtw_core_start(struct rtw_dev *rtwdev)
 
 	set_bit(RTW_FLAG_RUNNING, rtwdev->flags);
 
+	rtwdev->ps_hang_cnt = 0;
+
 	return 0;
 }
 
drivers/net/wireless/realtek/rtw88/main.h +1 -0
index 6ecbeb4ffe4..525f17590ad 100644
@@ -2113,6 +2113,7 @@ struct rtw_dev {
 	/* lps power state & handler work */
 	struct rtw_lps_conf lps_conf;
 	bool ps_enabled;
+	u8 ps_hang_cnt;
 	bool beacon_loss;
 	struct completion lps_leave_check;
 
drivers/net/wireless/realtek/rtw88/ps.c +9 -0
index add5a20b843..908d2ea3572 100644
@@ -166,6 +166,15 @@ static void rtw_fw_leave_lps_check(struct rtw_dev *rtwdev)
 		rtw_write32_clr(rtwdev, REG_TCR, BIT_PWRMGT_HWDATA_EN);
 		rtw_warn(rtwdev, "firmware failed to leave lps state\n");
 		rtw_fw_dump_dbg_info(rtwdev);
+
+		if (rtw_fw_feature_check(fw, FW_FEATURE_LPS_C2H) &&
+		    rtw_hci_type(rtwdev) == RTW_HCI_TYPE_PCIE) {
+			rtwdev->ps_hang_cnt++;
+			if (rtwdev->ps_hang_cnt >= RTW_PS_HANG_MAX_CNT)
+				rtw_fw_recovery(rtwdev);
+		}
+	} else {
+		rtwdev->ps_hang_cnt = 0;
 	}
 }
 
drivers/net/wireless/realtek/rtw88/ps.h +1 -0
index 5ae83d2526c..b088fad7cd8 100644
@@ -14,6 +14,7 @@
 
 #define LEAVE_LPS_TRY_CNT	5
 #define LEAVE_LPS_TIMEOUT	msecs_to_jiffies(100)
+#define RTW_PS_HANG_MAX_CNT	3
 
 int rtw_enter_ips(struct rtw_dev *rtwdev);
 int rtw_leave_ips(struct rtw_dev *rtwdev);
Signature · 1 line
2.53.0