wifi: rtw88: firmware recovery fixes and LPS hang handling
Subsystems (2)
v5 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 at entry and clear si->vif and si->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 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 | 7 +++++++ 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, 21 insertions(+)
Signature · 1 line
2.53.0
PATCH v5 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.
--- drivers/net/wireless/realtek/rtw88/mac80211.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/net/wireless/realtek/rtw88/mac80211.c +3 -0
@@ -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
Re: [PATCH v5 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 withIEEE80211_RECONFIG_TYPE_RESTART. If firmware recovery was in flight during this transition,RTW_FLAG_RESTARTINGremains set permanently, blocking all subsequent firmware recoveries until the module is reloaded.Similarly, if
rtw_core_start()fails duringdrv_start()in a recovery restart,local->startedremains false. When mac80211 cleans up viacfg80211_shutdown_all_interfaces(),drv_stop()returns early without callingrtw_ops_stop(), leavingRTW_FLAG_RESTARTINGset permanently on hardware that failed to recover.
Quoted 8 lines
Clear
RTW_FLAG_RESTARTINGinrtw_ops_stop(), as well as inrtw_ops_start()whenrtw_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")Signed-off-by Kalpesh Solanki <[email protected]>---
You seem to have missed it in v4 so please pick up this tag in v6.
Best regards,
Luka Gejak
RE: [PATCH v5 1/3] wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure
In reply to Kalpesh SolankiQuoted 7 lines · Kalpesh Solanki
When mac80211 stops or suspends the device without WoWLAN,
drv_reconfig_complete()is never invoked withIEEE80211_RECONFIG_TYPE_RESTART. If firmware recovery was in flight during this transition,RTW_FLAG_RESTARTINGremains set permanently, blocking all subsequent firmware recoveries until the module is reloaded.
In this case, I saw you clear RTW_FLAG_RESTARTING in rtw_ops_stop. However, the reconfig flow might not have even run at that moment.
How about clearing RTW_FLAG_RESTARTING when driver gets IEEE80211_RECONFIG_TYPE_SUSPEND?
Quoted 44 lines
Similarly, if
rtw_core_start()fails duringdrv_start()in a recovery restart,local->startedremains false. When mac80211 cleans up viacfg80211_shutdown_all_interfaces(),drv_stop()returns early without callingrtw_ops_stop(), leavingRTW_FLAG_RESTARTINGset permanently on hardware that failed to recover.Clear
RTW_FLAG_RESTARTINGinrtw_ops_stop(), as well as inrtw_ops_start()whenrtw_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")Signed-off-by Kalpesh Solanki <[email protected]>--- drivers/net/wireless/realtek/rtw88/mac80211.c | 3 +++ 1 file changed, 3 insertions(+)drivers/net/wireless/realtek/rtw88/mac80211.c +3
-0@@ -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);}
--
2.53.0
RE: [PATCH v5 1/3] wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure
Hi Zong-Zhe,
Thanks for taking a look.
Quoted 5 lines · Zong-Zhe Yang
In this case, I saw you clear
RTW_FLAG_RESTARTINGinrtw_ops_stop. However, the reconfig flow might not have even run at that moment.How about clearing
RTW_FLAG_RESTARTINGwhen driver getsIEEE80211_RECONFIG_TYPE_SUSPEND?
As far as I can see that misses the stop case. If the last interface goes down during a recovery, the restart work finds open_count at 0 and mac80211 sends neither RESTART nor SUSPEND, so the flag would stay set. Suspend without WoWLAN also goes through drv_stop(), so rtw_ops_stop() covers both.
Best regards,
Luka Gejak
RE: [PATCH v5 1/3] wifi: rtw88: clear RTW_FLAG_RESTARTING on stop and start failure
Quoted 6 lines · Luka Gejak
Quoted 3 lines · Zong-Zhe Yang
In this case, I saw you clear
RTW_FLAG_RESTARTINGinrtw_ops_stop. However, the reconfig flow might not have even run at that moment.
Quoted 8 lines
Quoted 2 lines
How about clearing
RTW_FLAG_RESTARTINGwhen driver getsIEEE80211_RECONFIG_TYPE_SUSPEND?As far as I can see that misses the stop case. If the last interface goes down during a recovery, the restart work finds
open_countat 0 and mac80211 sends neither RESTART nor SUSPEND, so the flag would stay set. Suspend without WoWLAN also goes throughdrv_stop(), sortw_ops_stop()covers both.
I missed to consider this corner case. My previous point was I don't want RTW_FLAG_RESTARTING to be cleared too early (assumed open_count != 0).
I don't seem to have other way for this case except by clearing it in drv_stop(). So, keep the code. Thanks.
PATCH v5 2/3 wifi: rtw88: skip already reset station in rtw_sta_remove()
In reply to Kalpesh SolankiDuring 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.
Check si->vif in rtw_sta_remove() and return early if the station was already reset, setting si->vif and si->mask to NULL upon removal.
--- drivers/net/wireless/realtek/rtw88/main.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/net/wireless/realtek/rtw88/main.c +5 -0
@@ -384,6 +384,9 @@ void rtw_sta_remove(struct rtw_dev *rtwdev, struct ieee80211_sta *sta,struct ieee80211_vif *vif = si->vif;int i; +if (!vif) +return ; +cancel_work_sync (&si->rc_work);if (vif->type !=NL80211_IFTYPE_STATION || sta->tdls)
@@ -395,8 +398,10 @@ 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 ; 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
Re: [PATCH v5 2/3] wifi: rtw88: skip already reset station in rtw_sta_remove()
Hi Kalpesh,
Thanks for the new version. Two comments below.
drivers/net/wireless/realtek/rtw88/main.c void rtw_sta_remove(struct rtw_dev *rtwdev, struct ieee80211_sta *sta, · line 390
Quoted 12 lines · Kalpesh Solanki
drivers/net/wireless/realtek/rtw88/main.c +3
-0@@ -384,6 +384,9 @@ void rtw_sta_remove(struct rtw_dev *rtwdev, struct ieee80211_sta *sta,struct ieee80211_vif *vif = si->vif;int i; +if (!vif) +return ; +cancel_work_sync (&si->rc_work);
I think my comment on v4 about this got missed:
https://lore.kernel.org/all/[email protected]/
rtw_ops_sta_rc_update() can still queue rc_work after the recovery has reset the station. If the station is then removed before the restart adds it back, this return skips the only cancel_work_sync() and rtw_sta_rc_work() could run on a freed station. Moving the check below cancel_work_sync() should be enough.
[ ... ]
drivers/net/wireless/realtek/rtw88/main.c void rtw_sta_remove(struct rtw_dev *rtwdev, struct ieee80211_sta *sta, · line 401
Quoted 2 lines
drivers/net/wireless/realtek/rtw88/main.c +1
-0kfree (si->mask); + si->mask =NULL ;
As far as I can see si->use_cfg_mask stays set here. mac80211 keeps the same station across the restart, and when it adds it back, rtw_sta_add() calls rtw_update_sta_info(), which gets to this:
rtw_rate_mask_cfg()
if (!si->use_cfg_mask)
return ra_mask;
...
cfg_mask = mask->control[band].legacy;So if I read it right, for a station that got a bitrate mask, a recovery now ends in a NULL pointer dereference, where before it read the freed mask. Maybe clear si->use_cfg_mask together with si->mask? The mask is then dropped after a recovery, since mac80211 doesn't pass it down again on restart, but I think that is fine.
Best regards,
Luka Gejak
PATCH v5 3/3 wifi: rtw88: ps: trigger firmware recovery only after consecutive LPS leave failures on PCI
In reply to Kalpesh SolankiA 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
@@ -1536,6 +1536,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
@@ -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
@@ -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
@@ -14,6 +14,7 @@#define LEAVE_LPS_TRY_CNT 5#define LEAVE_LPS_TIMEOUT msecs_to_jiffies(100) +#define RTW_PS_HANG_MAX_CNT 3int rtw_enter_ips (struct rtw_dev *rtwdev);int rtw_leave_ips (struct rtw_dev *rtwdev);
Signature · 1 line
2.53.0
Re: [PATCH v5 3/3] wifi: rtw88: ps: trigger firmware recovery only after consecutive LPS leave failures on PCI
Quoted 26 lines · 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 triggeringrtw_fw_recovery()afterRTW_PS_HANG_MAX_CNT(3) consecutive failures. Reset the counter to 0 on every successful LPS leave and inrtw_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 pollingREG_TCRforBIT_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, clearingps_hang_cntback to 0 and preventing the counter from ever accumulating. Therefore, only count consecutive failures on devices withFW_FEATURE_LPS_C2H, where C2H completion signaling reliably detects consecutive timeouts.Additionally,
rtw_fw_recovery()clears keys viartw_iterate_keys_atomic()under a spinlock. On USB and SDIO, register writes in key clearing sleep (viausb_control_msg()andsdio_claim_host()), which would trigger a
Quoted 6 lines
BUG: scheduling while atomic if executed. Because USB and SDIO do notsupport firmware recovery, restrict firmware recovery invocation to PCI devices via
rtw_hci_type().Signed-off-by Kalpesh Solanki <[email protected]>---
Looks good to me,
Best regards,
Luka Gejak