Skip to contentSkip to conversation

wifi: rtw88: channel switch in AP mode

PATCH Superseded Mehmet Fide 3 participants 11 messages Oct 21 – Oct 28 Realtek wireless driver (rtw88)

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v8 cover wifi: rtw88: channel switch in AP mode

From: Mehmet Fide <[email protected]>

An rtw88 AP cannot change its channel: the driver does not announce channel switch support, so hostapd's CHAN_SWITCH is refused and the only way is to tear the AP down. The series fixes the beacon download the reserved page build makes twice, implements channel_switch_beacon on top of the firmware's beacon page, and times the countdown downloads from the TBTT so that every count goes out on the air.

Tested on an RTL8821CU, an RTL8822CU and an RTL8822BU (USB) with hostapd 2.10 as AP: switches with counts 1 to 100, scans during a countdown, hostapd stopped and started, the USB driver unbound and bound and a firmware crash during a countdown, IBSS switches, a station interface on the 8822CU associating while its AP counted down, and Windows and Linux stations following the switches without a re-association. The 8821CU and 8822CU set was run on a PROVE_LOCKING kernel without a lockdep report.

Patch 3 was tested on the RTL8821CU with a build that delays every second countdown download: with a 60 ms delay every count still goes out (sniffed from a third machine), with 95 ms the delayed count stays out and the next download follows at once; the AP finishes at the same time in both cases, and a Windows station followed the switches with counts 10 and 50 without a re-association. With the start forced to either side of a TBTT, and with iperf3 traffic, the counts matched the beacon timing in every run on the RTL8821CU and on the RTL8822BU, the first on a PREEMPT_RT kernel, the second on a preemptible one without RT, both with HZ=250. The patch 2 fix was checked with a build that queues a TIM update right after every countdown finish: the WARN came before the fix and not after it. Patch 1 is unchanged since v6.

v8:
- 2/3: Acked-by from Ping-Ke
- 3/3: the TSF read retries once instead of looping, rtw_fw_csa_queue() reads the TSF once also on start, the port 0 comment is gone, empty lines where asked (Ping-Ke)

v7:
- new patch 3: the countdown downloads are timed from the TSF, 10 ms after each TBTT, so a download that runs late by less than a beacon interval still reaches the firmware before the next beacon; one later than that is followed by its successor at once. Ping-Ke's question on v6: with the absolute due time a late download and its successor could land in one beacon period and leave a count out of the air
- patch 3 takes a TBTT within the margin before the start as the one that sent the countdown template: a TBTT between the template download and the first TSF read made the stations see one count twice
- patch 2 keeps csa_vif until the beacon after the switch: a TIM update before it fetched one beacon past count 1 and hit the WARN in ieee80211_beacon_update_cntdwn()
- rebased on rtw-next

v6:
- the wiphy work patch of v5 is gone: the countdown work is an ieee80211 delayed work and csa_vif is driver state, both under the driver mutex, so rtw88 takes no wiphy lock anywhere (Ping-Ke)
- rtw_core_stop() cancels the countdown work and clears csa_vif, which covers the IPS path and the firmware recovery; the wiphy_lock() in the recovery work is gone (Ping-Ke)
- the BSS_CHANGED_BEACON path says why it starts the countdown, the replay after a hardware restart, and stops it when the beacon comes back without a switch announced (Ping-Ke)
- the downloads are paced from the first one with an absolute due time. Queued after each download, they drifted by about 10 ms per beacon interval on the ordered mac80211 workqueue; on a count of 50 the AP switched half a second after its station had, and the station re-associated

v5:
- 2/3 is new: update_beacon_work becomes a wiphy work, so that it can look at the channel switch state under the wiphy lock; it checks RTW_FLAG_RUNNING instead of being cancelled in rtw_core_stop(), which runs without the wiphy lock on the IPS path (Ping-Ke); reverted in v6
- 3/3: rtw_fw_csa_active/start/stop() assert the wiphy lock (Ping-Ke); reverted in v6, they assert the driver mutex
- 3/3: hw_scan() takes the mutex with guard() (Ping-Ke)
- 3/3: a comment on the association download names the STA+AP combination it skips the download for (Ping-Ke)
- the whole set ran on a PROVE_LOCKING kernel without a lockdep report:
switches with counts 1 to 100, hostapd stopped and restarted during a countdown, scans during a countdown, a firmware crash during a countdown, three stop/idle/start cycles, IBSS, 50 switches with a station attached, and on the RTL8822CU a station interface associating while the AP interface counted down

v4:
- 1/2: Reviewed-by from Luka
- 2/2: the countdown work is back in the device, initialised once, with csa_vif naming the interface; no interface iteration and no lookup through the reserved page any more (Ping-Ke)
- 2/2: the countdown is cancelled before a firmware recovery restarts the hardware, instead of guarding the work with flags (Ping-Ke)
- 2/2: the page download on association skips a running countdown, like the TIM update and the PG backup (Luka)
- 2/2: the scan refusal comes before the scan offload test, so that firmware without scan offload does not fall back to a software scan during a countdown (Luka)

v3:
- 1/2: the beacon out-pointer is initialised in the caller (Luka)
- 2/2: the countdown work lives in the interface, remove_interface cancels it unconditionally, the queue wrapper and the comments are gone (Ping-Ke)
- 2/2: the TIM update and the PG backup on set_key skip the page download while a switch is announced; every beacon fetch advances the countdown (Luka)
- 2/2: the commit message names the refused hardware scan (Ping-Ke)

v2:
- 2/2: a hardware scan is refused while a switch is announced
- the rtw89 mention is gone from the cover

Mehmet Fide (3):
wifi: rtw88: download the beacon the reserved page was built with
wifi: rtw88: support channel switch in AP mode
wifi: rtw88: time the countdown downloads from the TBTT

 drivers/net/wireless/realtek/rtw88/fw.c       | 151 ++++++++++++++++--
 drivers/net/wireless/realtek/rtw88/fw.h       |   4 +
 drivers/net/wireless/realtek/rtw88/mac80211.c |  39 ++++-
 drivers/net/wireless/realtek/rtw88/main.c     |   6 +-
 drivers/net/wireless/realtek/rtw88/main.h     |   3 +
 drivers/net/wireless/realtek/rtw88/reg.h      |   1 +
 6 files changed, 183 insertions(+), 21 deletions(-)

base-commit: 9b0be45d4d52fd223994e151f169743a97abd290

Signature · 1 line
2.55.0
#1 lore raw

PATCH v8 1/3 wifi: rtw88: download the beacon the reserved page was built with

From: Mehmet Fide <[email protected]>

rtw_fw_download_rsvd_page() downloads the beacon twice: once as part of the reserved page and once more on its own, so that the firmware ends up with a TX descriptor that describes the beacon rather than the whole page. The second download fetched a new beacon from mac80211 instead of downloading the one the page already holds.

Besides the extra work, every beacon fetch advances the DTIM count and, while a channel switch is announced, the CSA countdown; doing it twice per update lets a countdown that starts at 2 reach 0, which mac80211 warns about. Hand the beacon skb out of the page build and download that. The hw scan, which downloads the beacon without a page build, keeps fetching its own.

---

Notes:
Changes in v8:
- none

Changes in v7:
- none, rebased on rtw-next

Changes in v6:
- none

Changes in v5:
- none

Changes in v4:
- none, rebased on rtw-next

Changes in v3:
- the beacon out-pointer is initialised in the caller

 drivers/net/wireless/realtek/rtw88/fw.c | 34 +++++++++++++++----------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/net/wireless/realtek/rtw88/fw.c +21 -13
index 945fedcd375b..3cd17a3bb494 100644
@@ -1622,7 +1622,8 @@ static int  __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev)
 	return 0;
 }
 
-static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
+static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size,
+			       struct sk_buff **beacon)
 {
 	const struct rtw_chip_info *chip = rtwdev->chip;
 	struct ieee80211_hw *hw = rtwdev->hw;
@@ -1702,13 +1703,15 @@ static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
 		rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin,
 					  page, buf, rsvd_pkt);
-		if (page == 0)
+		if (page == 0) {
 			page += rtw_len_to_page(rsvd_pkt->skb->len +
 						tx_desc_sz, page_size);
-		else
+			/* the caller downloads it once more on its own */
+			*beacon = rsvd_pkt->skb;
+		} else {
 			page += rtw_len_to_page(rsvd_pkt->skb->len, page_size);
-
-		kfree_skb(rsvd_pkt->skb);
+			kfree_skb(rsvd_pkt->skb);
+		}
 		rsvd_pkt->skb = NULL;
 	}
 
@@ -1723,11 +1726,12 @@ static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
 	return NULL;
 }
 
-static int rtw_download_beacon(struct rtw_dev *rtwdev)
+/* the beacon the page was built with, or a fresh one for the hw scan */
+static int rtw_download_beacon(struct rtw_dev *rtwdev, struct sk_buff *beacon)
 {
 	struct ieee80211_hw *hw = rtwdev->hw;
 	struct rtw_rsvd_page *rsvd_pkt;
-	struct sk_buff *skb;
+	struct sk_buff *skb = beacon;
 	int ret = 0;
 
 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
@@ -1744,7 +1748,8 @@ static int rtw_download_beacon(struct rtw_dev *rtwdev)
 		return -EINVAL;
 	}
 
-	skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
+	if (!skb)
+		skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
 	if (!skb) {
 		rtw_err(rtwdev, "failed to get beacon skb\n");
 		return -ENOMEM;
@@ -1754,18 +1759,20 @@ static int rtw_download_beacon(struct rtw_dev *rtwdev)
 	if (ret)
 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
 
-	dev_kfree_skb(skb);
+	if (!beacon)
+		dev_kfree_skb(skb);
 
 	return ret;
 }
 
 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
 {
-	u8 *buf;
+	struct sk_buff *beacon = NULL;
 	u32 size;
+	u8 *buf;
 	int ret;
 
-	buf = rtw_build_rsvd_page(rtwdev, &size);
+	buf = rtw_build_rsvd_page(rtwdev, &size, &beacon);
 	if (!buf) {
 		rtw_err(rtwdev, "failed to build rsvd page pkt\n");
 		return -ENOMEM;
@@ -1782,13 +1789,14 @@ int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
 	 * the beacon again to replace the TX desc header, and we will get
 	 * a correct tx_desc for the beacon in the rsvd page.
 	 */
-	ret = rtw_download_beacon(rtwdev);
+	ret = rtw_download_beacon(rtwdev, beacon);
 	if (ret) {
 		rtw_err(rtwdev, "failed to download beacon\n");
 		goto free;
 	}
 
 free:
+	dev_kfree_skb(beacon);
 	kfree(buf);
 
 	return ret;
@@ -2345,7 +2353,7 @@ int rtw_hw_scan_offload(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
 	rtw_fw_set_scan_offload(rtwdev, &cs_option, rtwvif, &chan_list);
 out:
 	if (rtwdev->ap_active) {
-		ret = rtw_download_beacon(rtwdev);
+		ret = rtw_download_beacon(rtwdev, NULL);
 		if (ret)
 			rtw_err(rtwdev, "HW scan download beacon failed\n");
 	}
Signature · 1 line
2.55.0
#2 lore raw

PATCH v8 2/3 wifi: rtw88: support channel switch in AP mode

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

hostapd's CHAN_SWITCH is refused because the driver does not announce channel switch support, so an rtw88 AP can only change its channel by being torn down and started again.

Declare WIPHY_FLAG_HAS_CHANNEL_SWITCH and implement
channel_switch_beacon: the firmware repeats the beacon held in the first reserved page, so while a switch is announced the page is downloaded again every beacon interval to advance the countdown, and ieee80211_csa_finish() is called once it completes. IBSS shares the page and the work.

The work is a delayed work of the device and csa_vif names the interface, both under the driver mutex. The countdown is cancelled when the AP stops, when the interface goes away, on WoWLAN suspend and in rtw_core_stop(); the BSS_CHANGED_BEACON replay after a hardware restart resumes it. The other page downloads during a countdown, the TIM update, the PG backup and the one on association, are skipped until the beacon after the switch, since every beacon fetch advances the countdown; the next download carries their changes. A scan is refused: it would take the AP off the channel its stations count down to.

---

Notes:
Changes in v8:
- Acked-by from Ping-Ke

Changes in v7:
- csa_vif is kept until the beacon after the switch: a TIM update before it fetched one beacon past count 1 and hit the WARN in ieee80211_beacon_update_cntdwn()
- rebased on rtw-next

Changes in v6:
- the countdown work is an ieee80211 delayed work under the driver mutex, csa_vif is driver state under the mutex; no wiphy lock in rtw88 (Ping-Ke)
- rtw_core_stop() cancels the work and clears csa_vif, which covers the IPS path and the firmware recovery (Ping-Ke)
- the downloads are paced from the first one with an absolute due time:
queued after each download, the countdown drifted by about 10 ms per beacon interval on the ordered mac80211 workqueue, and a station switched ahead of the AP on a count of 50
- the BSS_CHANGED_BEACON path says why it starts the countdown (the replay after a hardware restart) and stops it when the beacon comes back without a switch announced (Ping-Ke)

Changes in v5:
- rtw_fw_csa_active/start/stop() assert the wiphy lock
- hw_scan() takes the mutex with guard()
- the ASSOC path says why it skips the download: the STA+AP combination

Changes in v4:
- the work and csa_vif live in rtw_dev; no lookup through the reserved page
- the countdown is stopped before a firmware recovery restarts the hardware
- a scan is refused while a switch is announced
- one beacon fetch per interval during the countdown

 drivers/net/wireless/realtek/rtw88/fw.c       | 80 ++++++++++++++++++-
 drivers/net/wireless/realtek/rtw88/fw.h       |  4 +
 drivers/net/wireless/realtek/rtw88/mac80211.c | 39 +++++++--
 drivers/net/wireless/realtek/rtw88/main.c     |  6 +-
 drivers/net/wireless/realtek/rtw88/main.h     |  3 +
 5 files changed, 124 insertions(+), 8 deletions(-)
drivers/net/wireless/realtek/rtw88/fw.c +78 -2
index 3cd17a3bb494..2d81aaeed0d7 100644
@@ -1802,14 +1802,90 @@ int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
 	return ret;
 }
 
+bool rtw_fw_csa_active(struct rtw_dev *rtwdev)
+{
+	lockdep_assert_held(&rtwdev->mutex);
+
+	return !!rtwdev->csa_vif;
+}
+
+/* paced from the first download: a late one does not delay the next */
+static void rtw_fw_csa_queue(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
+{
+	u32 interval = ieee80211_tu_to_usec(vif->bss_conf.beacon_int);
+	s64 delay;
+
+	rtwdev->csa_due = ktime_add_us(rtwdev->csa_due, interval);
+	delay = ktime_us_delta(rtwdev->csa_due, ktime_get());
+	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->csa_beacon_work,
+				     usecs_to_jiffies(max(delay, 0)));
+}
+
+void rtw_fw_csa_start(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
+{
+	lockdep_assert_held(&rtwdev->mutex);
+
+	if (rtwdev->csa_vif == vif)
+		return;
+
+	rtwdev->csa_vif = vif;
+	rtwdev->csa_due = ktime_get();
+	rtw_fw_csa_queue(rtwdev, vif);
+}
+
+void rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
+{
+	lockdep_assert_held(&rtwdev->mutex);
+
+	if (!vif || rtwdev->csa_vif != vif)
+		return;
+
+	rtwdev->csa_vif = NULL;
+	cancel_delayed_work(&rtwdev->csa_beacon_work);
+}
+
 void rtw_fw_update_beacon_work(struct work_struct *work)
 {
 	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
 					      update_beacon_work);
 
 	mutex_lock(&rtwdev->mutex);
-	rtw_fw_download_rsvd_page(rtwdev);
-	rtw_send_rsvd_page_h2c(rtwdev);
+
+	/* a fetch advances the countdown, the next download carries the TIM */
+	if (!rtw_fw_csa_active(rtwdev)) {
+		rtw_fw_download_rsvd_page(rtwdev);
+		rtw_send_rsvd_page_h2c(rtwdev);
+	}
+
+	mutex_unlock(&rtwdev->mutex);
+}
+
+void rtw_fw_csa_beacon_work(struct work_struct *work)
+{
+	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
+					      csa_beacon_work.work);
+	struct ieee80211_vif *vif;
+
+	mutex_lock(&rtwdev->mutex);
+
+	vif = rtwdev->csa_vif;
+	if (!vif)
+		goto out;
+
+	if (!vif->bss_conf.csa_active) {
+		rtwdev->csa_vif = NULL;
+		goto out;
+	}
+
+	if (!ieee80211_beacon_cntdwn_is_complete(vif, 0)) {
+		rtw_fw_download_rsvd_page(rtwdev);
+		rtw_send_rsvd_page_h2c(rtwdev);
+		rtw_fw_csa_queue(rtwdev, vif);
+	} else {
+		ieee80211_csa_finish(vif, 0);
+	}
+
+out:
 	mutex_unlock(&rtwdev->mutex);
 }
 
drivers/net/wireless/realtek/rtw88/fw.h +4 -0
index 48ad9ceab6ea..d8e58e4ed836 100644
@@ -863,7 +863,11 @@ void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev,
 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev,
 			   struct rtw_vif *rtwvif);
 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev);
+bool rtw_fw_csa_active(struct rtw_dev *rtwdev);
+void rtw_fw_csa_start(struct rtw_dev *rtwdev, struct ieee80211_vif *vif);
+void rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif);
 void rtw_fw_update_beacon_work(struct work_struct *work);
+void rtw_fw_csa_beacon_work(struct work_struct *work);
 void rtw_send_rsvd_page_h2c(struct rtw_dev *rtwdev);
 int rtw_dump_drv_rsvd_page(struct rtw_dev *rtwdev,
 			   u32 offset, u32 size, u32 *buf);
drivers/net/wireless/realtek/rtw88/mac80211.c +34 -5
index 2a9b09fa76e7..182aecc76820 100644
@@ -237,6 +237,8 @@ static void rtw_ops_remove_interface(struct ieee80211_hw *hw,
 
 	mutex_lock(&rtwdev->mutex);
 
+	rtw_fw_csa_stop(rtwdev, vif);
+
 	rtw_leave_lps_deep(rtwdev);
 
 	rtw_txq_cleanup(rtwdev, vif->txq);
@@ -395,8 +397,11 @@ static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
 		if (vif->cfg.assoc) {
 			rtw_coex_connect_notify(rtwdev, COEX_ASSOCIATE_FINISH);
 
-			rtw_fw_download_rsvd_page(rtwdev);
-			rtw_send_rsvd_page_h2c(rtwdev);
+			/* a station associating while the AP counts down */
+			if (!rtw_fw_csa_active(rtwdev)) {
+				rtw_fw_download_rsvd_page(rtwdev);
+				rtw_send_rsvd_page_h2c(rtwdev);
+			}
 			rtw_fw_default_port(rtwdev, rtwvif);
 			rtw_coex_media_status_notify(rtwdev, vif->cfg.assoc);
 			if (rtw_bf_support)
@@ -438,6 +443,11 @@ static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
 		rtw_set_dtim_period(rtwdev, conf->dtim_period);
 		rtw_fw_download_rsvd_page(rtwdev);
 		rtw_send_rsvd_page_h2c(rtwdev);
+		/* restart replay resumes it, a beacon without CSA ends it */
+		if (conf->csa_active)
+			rtw_fw_csa_start(rtwdev, vif);
+		else
+			rtw_fw_csa_stop(rtwdev, vif);
 	}
 
 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
@@ -490,6 +500,7 @@ static void rtw_ops_stop_ap(struct ieee80211_hw *hw,
 	struct rtw_dev *rtwdev = hw->priv;
 
 	mutex_lock(&rtwdev->mutex);
+	rtw_fw_csa_stop(rtwdev, vif);
 	rtw_write32_clr(rtwdev, REG_TCR, BIT_TCR_UPDATE_HGQMD);
 	rtw_write16(rtwdev, REG_ATIMWND, ATIMWND_DEFAULT);
 	rtwdev->ap_active = false;
@@ -556,6 +567,17 @@ static int rtw_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
 	return 0;
 }
 
+static void rtw_ops_channel_switch_beacon(struct ieee80211_hw *hw,
+					  struct ieee80211_vif *vif,
+					  struct cfg80211_chan_def *chandef)
+{
+	struct rtw_dev *rtwdev = hw->priv;
+
+	mutex_lock(&rtwdev->mutex);
+	rtw_fw_csa_start(rtwdev, vif);
+	mutex_unlock(&rtwdev->mutex);
+}
+
 static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 			   struct ieee80211_vif *vif, struct ieee80211_sta *sta,
 			   struct ieee80211_key_conf *key)
@@ -626,7 +648,8 @@ static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 	}
 
 	/* download new cam settings for PG to backup */
-	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG)
+	if (rtw_get_lps_deep_mode(rtwdev) == LPS_DEEP_MODE_PG &&
+	    !rtw_fw_csa_active(rtwdev))
 		rtw_fw_download_rsvd_page(rtwdev);
 
 out:
@@ -846,6 +869,7 @@ static int rtw_ops_suspend(struct ieee80211_hw *hw,
 	int ret;
 
 	mutex_lock(&rtwdev->mutex);
+	rtw_fw_csa_stop(rtwdev, rtwdev->csa_vif);
 	ret = rtw_wow_suspend(rtwdev, wowlan);
 	if (ret)
 		rtw_err(rtwdev, "failed to suspend for wow %d\n", ret);
@@ -893,20 +917,24 @@ static int rtw_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
 	struct rtw_dev *rtwdev = hw->priv;
 	int ret;
 
+	guard(mutex)(&rtwdev->mutex);
+
+	/* a scan takes the AP off the channel its stations count down to */
+	if (rtw_fw_csa_active(rtwdev))
+		return -EBUSY;
+
 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
 		return 1;
 
 	if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
 		return -EBUSY;
 
-	mutex_lock(&rtwdev->mutex);
 	rtw_hw_scan_start(rtwdev, vif, req);
 	ret = rtw_hw_scan_offload(rtwdev, vif, true);
 	if (ret) {
 		rtw_hw_scan_abort(rtwdev);
 		rtw_err(rtwdev, "HW scan failed with status: %d\n", ret);
 	}
-	mutex_unlock(&rtwdev->mutex);
 
 	return ret;
 }
@@ -973,6 +1001,7 @@ const struct ieee80211_ops rtw_ops = {
 	.sta_add		= rtw_ops_sta_add,
 	.sta_remove		= rtw_ops_sta_remove,
 	.set_tim		= rtw_ops_set_tim,
+	.channel_switch_beacon	= rtw_ops_channel_switch_beacon,
 	.set_key		= rtw_ops_set_key,
 	.ampdu_action		= rtw_ops_ampdu_action,
 	.can_aggregate_in_amsdu	= rtw_ops_can_aggregate_in_amsdu,
drivers/net/wireless/realtek/rtw88/main.c +5 -1
index 0f23498b5c96..48e8cad221b9 100644
@@ -1550,11 +1550,13 @@ void rtw_core_stop(struct rtw_dev *rtwdev)
 
 	clear_bit(RTW_FLAG_RUNNING, rtwdev->flags);
 	clear_bit(RTW_FLAG_FW_RUNNING, rtwdev->flags);
+	rtwdev->csa_vif = NULL;
 
 	mutex_unlock(&rtwdev->mutex);
 
 	cancel_work_sync(&rtwdev->c2h_work);
 	cancel_work_sync(&rtwdev->update_beacon_work);
+	cancel_delayed_work_sync(&rtwdev->csa_beacon_work);
 	cancel_delayed_work_sync(&rtwdev->watch_dog_work);
 	cancel_delayed_work_sync(&coex->bt_relink_work);
 	cancel_delayed_work_sync(&coex->bt_reenable_work);
@@ -2170,6 +2172,7 @@ int rtw_core_init(struct rtw_dev *rtwdev)
 	INIT_WORK(&rtwdev->ips_work, rtw_ips_work);
 	INIT_WORK(&rtwdev->fw_recovery_work, rtw_fw_recovery_work);
 	INIT_WORK(&rtwdev->update_beacon_work, rtw_fw_update_beacon_work);
+	INIT_DELAYED_WORK(&rtwdev->csa_beacon_work, rtw_fw_csa_beacon_work);
 	INIT_WORK(&rtwdev->ba_work, rtw_txq_ba_work);
 	skb_queue_head_init(&rtwdev->c2h_queue);
 	skb_queue_head_init(&rtwdev->coex.queue);
@@ -2293,7 +2296,8 @@ int rtw_register_hw(struct rtw_dev *rtwdev, struct ieee80211_hw *hw)
 	hw->wiphy->available_antennas_rx = hal->antenna_rx;
 
 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
-			    WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
+			    WIPHY_FLAG_TDLS_EXTERNAL_SETUP |
+			    WIPHY_FLAG_HAS_CHANNEL_SWITCH;
 
 	hw->wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
 	hw->wiphy->max_scan_ssids = RTW_SCAN_MAX_SSIDS;
drivers/net/wireless/realtek/rtw88/main.h +3 -0
index d59f6e323adf..bc3ebfec11ae 100644
@@ -2094,6 +2094,9 @@ struct rtw_dev {
 	struct work_struct ips_work;
 	struct work_struct fw_recovery_work;
 	struct work_struct update_beacon_work;
+	struct delayed_work csa_beacon_work;
+	struct ieee80211_vif *csa_vif;
+	ktime_t csa_due;
 
 	/* used to protect txqs list */
 	spinlock_t txq_lock;
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH rtw-next v8 2/3] wifi: rtw88: support channel switch in AP mode

Hi Mehmet, Ping-Ke,

Sorry for late review, few comments below.

Quoted 27 lines · Mehmet Fide

From: Mehmet Fide <[email protected]>

hostapd's CHAN_SWITCH is refused because the driver does not announce channel switch support, so an rtw88 AP can only change its channel by being torn down and started again.

Declare WIPHY_FLAG_HAS_CHANNEL_SWITCH and implement
channel_switch_beacon: the firmware repeats the beacon held in the first reserved page, so while a switch is announced the page is downloaded again every beacon interval to advance the countdown, and ieee80211_csa_finish() is called once it completes. IBSS shares the page and the work.

The work is a delayed work of the device and csa_vif names the interface, both under the driver mutex. The countdown is cancelled when the AP stops, when the interface goes away, on WoWLAN suspend and in rtw_core_stop(); the BSS_CHANGED_BEACON replay after a hardware restart resumes it. The other page downloads during a countdown, the TIM update, the PG backup and the one on association, are skipped until the beacon after the switch, since every beacon fetch advances the countdown; the next download carries their changes. A scan is refused: it would take the AP off the channel its stations count down to.

---

[ ... ]

Quoted 4 lines
drivers/net/wireless/realtek/rtw88/mac80211.c +0 -0
index 2a9b09fa76e7..182aecc76820 100644

[ ... ]

drivers/net/wireless/realtek/rtw88/mac80211.c static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw, · line 453

Quoted 12 lines
drivers/net/wireless/realtek/rtw88/mac80211.c +5 -0
@@ -438,6 +443,11 @@ static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw,
              rtw_set_dtim_period(rtwdev, conf->dtim_period);
              rtw_fw_download_rsvd_page(rtwdev);
              rtw_send_rsvd_page_h2c(rtwdev);
+             /* restart replay resumes it, a beacon without CSA ends it */
+             if (conf->csa_active)
+                     rtw_fw_csa_start(rtwdev, vif);
+             else
+                     rtw_fw_csa_stop(rtwdev, vif);
      }
 
      if (changed & BSS_CHANGED_BEACON_ENABLED) {

What happens when an IBSS is left during its countdown and joined again?

ieee80211_ibss_disconnect() leaves csa_active set and reports no BSS_CHANGED_BEACON, so only the idle path through rtw_core_stop() clears csa_vif. The next join reports BSS_CHANGED_BEACON with csa_active still set and this starts a new countdown. The new beacon has no counter, so ieee80211_beacon_cntdwn_is_complete() never returns true. The page is then downloaded every beacon interval and scans return -EBUSY until the interface goes down.

Would clearing csa_active in ieee80211_ibss_disconnect(), same way ieee80211_stop_ap() does, be the right fix?

[ ... ]

drivers/net/wireless/realtek/rtw88/mac80211.c static int rtw_ops_suspend(struct ieee80211_hw *hw, · line 875

Quoted 8 lines
drivers/net/wireless/realtek/rtw88/mac80211.c +1 -0
@@ -846,6 +869,7 @@ static int rtw_ops_suspend(struct ieee80211_hw *hw,
      int ret;
 
      mutex_lock(&rtwdev->mutex);
+     rtw_fw_csa_stop(rtwdev, rtwdev->csa_vif);
      ret = rtw_wow_suspend(rtwdev, wowlan);
      if (ret)
              rtw_err(rtwdev, "failed to suspend for wow %d\n", ret);

What restarts the countdown after a successful WoWLAN resume?

The 8822C registers rtw_iface_combs and a wowlan_stub, so an AP can be counting down next to an associated station when the system suspends. rtw_wow_vif_iter() only picks the station, so rtw_wow_suspend() succeeds and mac80211 keeps the AP running with csa_active set.

rtw_ops_resume() then returns 0 and ieee80211_reconfig() skips the replay:

        res = drv_resume(local);
        ...
        if (res == 0)
                goto wake_up;

So no BSS_CHANGED_BEACON arrives, csa_vif stays NULL and ieee80211_csa_finish() is never called. csa_active stays set on the AP, so ieee80211_change_beacon() and any later channel switch return -EBUSY until the AP is restarted. Also each TIM download fetches the beacon again, since csa_vif no longer gates it, and keeps stepping the count.

Maybe rtw_ops_resume() could restart the countdown when the AP still has csa_active set?

Best regards,
Luka Gejak

#4 lore raw

Re: [PATCH rtw-next v8 2/3] wifi: rtw88: support channel switch in AP mode

Hi Luka,

Thanks for the review.

Quoted 2 lines

Would clearing csa_active in ieee80211_ibss_disconnect(), same way ieee80211_stop_ap() does, be the right fix?

Yes. I reproduced it on an RTL8822CU. mac80211 refuses the next switch itself as well, so I will send the fix to mac80211 separately.

Quoted 2 lines

Maybe rtw_ops_resume() could restart the countdown when the AP still has csa_active set?

Agreed, v9 does that. It is not run here, I have no 8822CE.

Mehmet

maintainer
#5 lore raw

RE: [PATCH rtw-next v8 2/3] wifi: rtw88: support channel switch in AP mode

Quoted 7 lines · Mehmet Fide

Hi Luka,

Thanks for the review.

Quoted 2 lines

Would clearing csa_active in ieee80211_ibss_disconnect(), same way ieee80211_stop_ap() does, be the right fix?

Quoted 7 lines

Yes. I reproduced it on an RTL8822CU. mac80211 refuses the next switch itself as well, so I will send the fix to mac80211 separately.

Quoted 2 lines

Maybe rtw_ops_resume() could restart the countdown when the AP still has csa_active set?

Agreed, v9 does that. It is not run here, I have no 8822CE.

During the WoW suspend, I think AP stays on original channel and sends beacon without countdown. How does associated STA do? Will it switch to target channel and disconnect result from no beacon?

#6 lore raw

Re: [PATCH rtw-next v8 2/3] wifi: rtw88: support channel switch in AP mode

Hi Ping-Ke,

Quoted 3 lines

During the WoW suspend, I think AP stays on original channel and sends beacon without countdown. How does associated STA do? Will it switch to target channel and disconnect result from no beacon?

Yes. I tried it with mac80211_hwsim on wireless/main: the station switches on its timer even when the AP goes on beaconing on the old channel without the CSA element, and disconnects after the beacon loss. From the code, only drivers with abort_channel_switch (iwlwifi, mt7921, mt7925) abort the switch on such a beacon and stay.

The AP stays up during WoWLAN only with AP + station on the 8822C:
WoWLAN takes a station vif only, so with an AP alone rtw_ops_suspend() returns 1 and cfg80211 stops the AP before suspending. I checked this on an RTL8822CU during a countdown.

Refusing WoWLAN during a switch would not help the stations, cfg80211 would stop the AP as well, so I would keep v9. The resume path does not run on USB, the device is unbound during suspend; an 8822CE is on order, it takes two to three weeks, and I can test it when it arrives.

Mehmet

maintainer
#7 lore raw

RE: [PATCH rtw-next v8 2/3] wifi: rtw88: support channel switch in AP mode

Quoted 17 lines · Mehmet Fide

Hi Ping-Ke,

Quoted 3 lines

During the WoW suspend, I think AP stays on original channel and sends beacon without countdown. How does associated STA do? Will it switch to target channel and disconnect result from no beacon?

Yes. I tried it with mac80211_hwsim on wireless/main: the station switches on its timer even when the AP goes on beaconing on the old channel without the CSA element, and disconnects after the beacon loss. From the code, only drivers with abort_channel_switch (iwlwifi, mt7921, mt7925) abort the switch on such a beacon and stay.

The AP stays up during WoWLAN only with AP + station on the 8822C:
WoWLAN takes a station vif only, so with an AP alone rtw_ops_suspend() returns 1 and cfg80211 stops the AP before suspending. I checked this on an RTL8822CU during a countdown.

Quoted 4 lines

Refusing WoWLAN during a switch would not help the stations, cfg80211 would stop the AP as well, so I would keep v9. The resume path does not run on USB, the device is unbound during suspend; an 8822CE is on order, it takes two to three weeks, and I can test it when it arrives.

Understood. Then, AP mode continues to switch to target channel after the resume.

Ping-Ke

#8 lore raw

PATCH v8 3/3 wifi: rtw88: time the countdown downloads from the TBTT

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

The countdown downloads are queued one beacon interval apart, with no relation to the TBTT. A download that runs late past the next TBTT leaves a count out of the air.

Queue each download 10 ms after the next TBTT, read from the TSF, so it may run late by almost a beacon interval; a later one is followed by the next at once. A TSF jump, an IBSS merge or a reset, restarts the schedule from the next TBTT.

A TBTT just before the start may already have sent the first count; it is taken as the one that did, so no count is sent twice.

The USB chips raise no TBTT interrupt, so the TSF register is read. The beaconing interface is always on port 0: rtw_core_port_switch() moves an AP there, an IBSS runs alone.

---

Notes:
Changes in v8:
- the TSF read retries once, if the high word changed, instead of looping (Ping-Ke)
- rtw_fw_csa_queue() takes a start argument and reads the TSF once, also on start (Ping-Ke)
- the port 0 comment is gone, the commit message says it (Ping-Ke)
- empty lines where asked (Ping-Ke)

Changes in v7:
- new patch, from Ping-Ke's review of the countdown pacing in v6

 drivers/net/wireless/realtek/rtw88/fw.c   | 51 +++++++++++++++++++----
 drivers/net/wireless/realtek/rtw88/main.h |  2 +-
 drivers/net/wireless/realtek/rtw88/reg.h  |  1 +
 3 files changed, 46 insertions(+), 8 deletions(-)
drivers/net/wireless/realtek/rtw88/fw.c +44 -7
index 2d81aaeed0d7..9ba4f1c244ec 100644
@@ -3,6 +3,7 @@
  */
 
 #include <linux/iopoll.h>
+#include <linux/math64.h>
 
 #include "main.h"
 #include "coex.h"
@@ -1809,14 +1810,50 @@ bool rtw_fw_csa_active(struct rtw_dev *rtwdev)
 	return !!rtwdev->csa_vif;
 }
 
-/* paced from the first download: a late one does not delay the next */
-static void rtw_fw_csa_queue(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
+#define RTW_CSA_TBTT_MARGIN_US	10000
+
+static u64 rtw_fw_read_tsf(struct rtw_dev *rtwdev)
+{
+	u32 hi, lo, tmp;
+
+	hi = rtw_read32(rtwdev, REG_TSFTR + 4);
+	lo = rtw_read32(rtwdev, REG_TSFTR);
+	tmp = rtw_read32(rtwdev, REG_TSFTR + 4);
+	if (tmp != hi) {
+		hi = tmp;
+		lo = rtw_read32(rtwdev, REG_TSFTR);
+	}
+
+	return ((u64)hi << 32) | lo;
+}
+
+static void rtw_fw_csa_queue(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
+			     bool start)
 {
 	u32 interval = ieee80211_tu_to_usec(vif->bss_conf.beacon_int);
+	u64 tsf = rtw_fw_read_tsf(rtwdev);
 	s64 delay;
+	u32 rem;
+
+	if (start) {
+		div_u64_rem(tsf, interval, &rem);
+		rtwdev->csa_tbtt = tsf - rem + interval;
+		/* a TBTT just passed may have sent the first count */
+		if (rem < RTW_CSA_TBTT_MARGIN_US)
+			rtwdev->csa_tbtt -= interval;
+	} else {
+		rtwdev->csa_tbtt += interval;
+	}
+
+	delay = rtwdev->csa_tbtt + RTW_CSA_TBTT_MARGIN_US - tsf;
+	if (delay > interval + RTW_CSA_TBTT_MARGIN_US ||
+	    delay < -(s64)interval) {
+		/* the TSF jumped: restart from the next TBTT */
+		div_u64_rem(tsf, interval, &rem);
+		rtwdev->csa_tbtt = tsf - rem + interval;
+		delay = rtwdev->csa_tbtt + RTW_CSA_TBTT_MARGIN_US - tsf;
+	}
 
-	rtwdev->csa_due = ktime_add_us(rtwdev->csa_due, interval);
-	delay = ktime_us_delta(rtwdev->csa_due, ktime_get());
 	ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->csa_beacon_work,
 				     usecs_to_jiffies(max(delay, 0)));
 }
@@ -1829,8 +1866,8 @@ void rtw_fw_csa_start(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
 		return;
 
 	rtwdev->csa_vif = vif;
-	rtwdev->csa_due = ktime_get();
-	rtw_fw_csa_queue(rtwdev, vif);
+
+	rtw_fw_csa_queue(rtwdev, vif, true);
 }
 
 void rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
@@ -1880,7 +1917,7 @@ void rtw_fw_csa_beacon_work(struct work_struct *work)
 	if (!ieee80211_beacon_cntdwn_is_complete(vif, 0)) {
 		rtw_fw_download_rsvd_page(rtwdev);
 		rtw_send_rsvd_page_h2c(rtwdev);
-		rtw_fw_csa_queue(rtwdev, vif);
+		rtw_fw_csa_queue(rtwdev, vif, false);
 	} else {
 		ieee80211_csa_finish(vif, 0);
 	}
drivers/net/wireless/realtek/rtw88/main.h +1 -1
index bc3ebfec11ae..3be0a96d1186 100644
@@ -2096,7 +2096,7 @@ struct rtw_dev {
 	struct work_struct update_beacon_work;
 	struct delayed_work csa_beacon_work;
 	struct ieee80211_vif *csa_vif;
-	ktime_t csa_due;
+	u64 csa_tbtt;
 
 	/* used to protect txqs list */
 	spinlock_t txq_lock;
drivers/net/wireless/realtek/rtw88/reg.h +1 -0
index afcdb66fdb9a..3ff4ae0885ef 100644
@@ -506,6 +506,7 @@
 #define REG_USTIME_TSF		0x055C
 #define REG_BCN_MAX_ERR		0x055D
 #define REG_RXTSF_OFFSET_CCK	0x055E
+#define REG_TSFTR		0x0560
 #define REG_MISC_CTRL		0x0577
 #define BIT_EN_FREE_CNT		BIT(3)
 #define BIT_DIS_SECOND_CCA	(BIT(0) | BIT(1))
Signature · 1 line
2.55.0
maintainer Acked-by on v8 3/3
#9 lore raw

RE: [PATCH rtw-next v8 3/3] wifi: rtw88: time the countdown downloads from the TBTT

Quoted 11 lines · Mehmet Fide

From: Mehmet Fide <[email protected]>

The countdown downloads are queued one beacon interval apart, with no relation to the TBTT. A download that runs late past the next TBTT leaves a count out of the air.

Queue each download 10 ms after the next TBTT, read from the TSF, so it may run late by almost a beacon interval; a later one is followed by the next at once. A TSF jump, an IBSS merge or a reset, restarts the schedule from the next TBTT.

Quoted 8 lines

A TBTT just before the start may already have sent the first count; it is taken as the one that did, so no count is sent twice.

The USB chips raise no TBTT interrupt, so the TSF register is read. The beaconing interface is always on port 0: rtw_core_port_switch() moves an AP there, an IBSS runs alone.

Reviewed-by on v8 3/3
#10 lore raw

Re: [PATCH rtw-next v8 3/3] wifi: rtw88: time the countdown downloads from the TBTT

In reply to Mehmet Fide
Quoted 12 lines · Mehmet Fide

From: Mehmet Fide <[email protected]>

The countdown downloads are queued one beacon interval apart, with no relation to the TBTT. A download that runs late past the next TBTT leaves a count out of the air.

Queue each download 10 ms after the next TBTT, read from the TSF, so it may run late by almost a beacon interval; a later one is followed by the next at once. A TSF jump, an IBSS merge or a reset, restarts the schedule from the next TBTT.

Quoted 8 lines

A TBTT just before the start may already have sent the first count; it is taken as the one that did, so no count is sent twice.

The USB chips raise no TBTT interrupt, so the TSF register is read. The beaconing interface is always on port 0: rtw_core_port_switch() moves an AP there, an IBSS runs alone.

Looking good to me.

Best regards,
Luka Gejak