1: a2f5d0de8b9b ! 1: 3f742efb1594 wifi: rtw88: download the beacon the reserved page was built with @@ Metadata ## Commit message ## wifi: rtw88: download the beacon the reserved page was built with  - 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. + rtw_fw_download_rsvd_page() downloads the beacon twice: as part of the + reserved page, and again on its own so that the firmware gets a TX + descriptor for the beacon instead of the whole page. The second + download fetches a new beacon from mac80211 instead of the one in the + page.  - 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 steps a countdown that starts at 2 past 1, which mac80211 - reports as "beacon TX faster than countdown". 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. + Each fetch advances the DTIM count and, during a channel switch, the + CSA countdown. Two fetches per update step a countdown of 2 past 1, + and mac80211 warns "beacon TX faster than countdown". Keep the beacon + from the page build and download that one. The hw scan, which downloads + the beacon without a page build, still fetches its own.  - Without a channel switch nothing on the air depends on it: the hardware - writes the DTIM count of an AP beacon itself (BIT_TCR_UPDATE_TIMIE), so - this is not marked as a fix. + The hardware writes the DTIM count of an AP beacon itself + (BIT_TCR_UPDATE_TIMIE), so nothing on the air changes without a channel + switch, and this is not marked as a fix.  Signed-off-by: Mehmet Fide  Reviewed-by: Luka Gejak  @@ drivers/net/wireless/realtek/rtw88/fw.c: static u8 *rtw_build_rsvd_page(struct r 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); @@ drivers/net/wireless/realtek/rtw88/fw.c: static u8 *rtw_build_rsvd_page(struct r }   -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; @@ drivers/net/wireless/realtek/rtw88/fw.c: static int rtw_download_beacon(struct r  + u8 *buf; int ret;  - if (bitmap_empty(rtwdev->hw_port, RTW_PORT_NUM)) - return 0; -   - buf = rtw_build_rsvd_page(rtwdev, &size);  + buf = rtw_build_rsvd_page(rtwdev, &size, &beacon); if (!buf) { 2: 7d75299485c9 ! 2: a2a595e9376e wifi: rtw88: support channel switch in AP mode @@ Metadata ## Commit message ## wifi: rtw88: support channel switch in AP mode  - 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. + The driver does not set WIPHY_FLAG_HAS_CHANNEL_SWITCH, so hostapd's + CHAN_SWITCH is refused and an rtw88 AP can only change its channel by + restarting.  - Declare WIPHY_FLAG_HAS_CHANNEL_SWITCH and start the countdown when - BSS_CHANGED_BEACON brings a beacon with CSA: 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. The work steps the count with ieee80211_beacon_update_cntdwn() - and calls ieee80211_csa_finish() one beacon interval after the count of - 1 was downloaded; a download that fails is repeated with the same count, - so the switch does not complete before its last count was handed to the - firmware. IBSS shares the page and the work. + The firmware repeats the beacon of the first reserved page, so the + driver has to step the countdown. Set the flag, and when + BSS_CHANGED_BEACON brings a beacon with CSA, start a delayed work that + advances the count with ieee80211_beacon_update_cntdwn() and downloads + the page once per beacon interval. It calls ieee80211_csa_finish() one + interval after the count of 1 was downloaded, and repeats a failed + download with the same count. IBSS uses the same page and work.  - The work is a delayed work of the device and csa_vif names the - interface, both under the driver mutex. The driver clears csa_vif, the - work never does: the countdown is cancelled when the AP stops, when the - interface goes away, on WoWLAN suspend and in rtw_core_stop(), and while - beaconing is disabled, as for an IBSS that is left or for an off-channel - period; it starts again when beaconing is enabled with the switch still - announced. It is not started while the device is stopped. The work - itself only stops, at the end of the countdown or once mac80211 has - taken the beacon away. The BSS_CHANGED_BEACON replay after a hardware - restart resumes the countdown, a failed download of the replay is - repeated before the next step, and the driver resumes the countdown - after a WoWLAN resume, which mac80211 follows with no replay. + The countdown stops when the AP stops, the interface is removed, + beaconing is disabled, on WoWLAN suspend and in rtw_core_stop(). It + starts again when beaconing is enabled, when a hardware restart replays + the beacon, and after a WoWLAN resume. csa_vif and the work are under + the driver mutex, and only the stop paths clear csa_vif.  - While a switch is announced the beacon of the page is fetched as a - template, which leaves the count as it is, so a download for another - reason, the replay after a hardware restart or the page of the WoWLAN - firmware, carries the count as the work left it. The TIM update, the PG - backup and the download on association are left to the work during a - countdown, its next download carries their changes. An AP that stops - downloads the page once more if a station interface stays behind. A - scan is refused: it would take the AP off the channel its stations - count down to. + During a countdown the page beacon is fetched with + ieee80211_beacon_get_template(), which leaves the count alone, so any + other download carries the current count. The TIM update, the PG backup + and the download on association are left to the next step. An AP that + stops with a station interface left downloads the page once more. + Scans are refused, as they would take the AP off its channel.  Signed-off-by: Mehmet Fide   @@ drivers/net/wireless/realtek/rtw88/fw.c: static struct sk_buff *rtw_get_rsvd_pag switch (rsvd_pkt->type) { case RSVD_BEACON:  - skb_new = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL, 0); -+ /* only the countdown work advances the count */  + if (vif->bss_conf.csa_active) {  + skb_new = ieee80211_beacon_get_template(hw, vif, &offs,  + 0); @@ drivers/net/wireless/realtek/rtw88/fw.c: int rtw_fw_download_rsvd_page(struct rt  + 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); @@ drivers/net/wireless/realtek/rtw88/fw.c: int rtw_fw_download_rsvd_page(struct rt  - rtw_fw_download_rsvd_page(rtwdev);  - rtw_send_rsvd_page_h2c(rtwdev);  + -+ /* the countdown's next download carries the TIM */  + if (!rtw_fw_csa_active(rtwdev)) {  + rtw_fw_download_rsvd_page(rtwdev);  + rtw_send_rsvd_page_h2c(rtwdev); @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_bss_info_chan   - 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); @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_bss_info_chan  - rtw_fw_download_rsvd_page(rtwdev);  + ret = rtw_fw_download_rsvd_page(rtwdev); rtw_send_rsvd_page_h2c(rtwdev); -+ /* -+ * A beacon with CSA starts the countdown, also when a restart -+ * replays it. A beacon without CSA ends it. -+ */  + if (conf->csa_active)  + rtw_fw_csa_start(rtwdev, vif, !!ret);  + else @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_stop_ap(struc  + if (!rtw_core_check_sta_active(rtwdev)) { rtw_clear_op_chan(rtwdev);  + } else { -+ /* what a countdown may have held back */  + rtw_fw_download_rsvd_page(rtwdev);  + rtw_send_rsvd_page_h2c(rtwdev);  + } @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static int rtw_ops_hw_scan(struct   + 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;  + 3: b0aad2818b4a ! 3: 0901430f35f2 wifi: rtw88: time the countdown downloads from the TBTT @@ Metadata ## Commit message ## wifi: rtw88: time the countdown downloads from the TBTT  - 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. + The countdown steps run one beacon interval apart with no relation to + the TBTT, and a download close to a TBTT keeps beacons off the air. On + an RTL8821CU and an RTL8822CU a TBTT during the download sent no beacon + or the old one. On an RTL8822CE a TBTT up to about 100 us before the + download also kept the next beacon out, so with a count of 2 no CSA + beacon went out.  - Read the TSF after each download and queue the next step 10 ms after the - first TBTT that is at least 10 ms away. Every count that was downloaded, - the first one too, then has a TBTT to go out at; a download that runs - late delays the switch by a beacon interval instead of taking a count - off the air, and stations that took the time of the switch from an - earlier count then switch up to that interval before the AP. As with a - driver that fetches each beacon with ieee80211_beacon_get(), the first - CSA beacon carries the count after the first step. + Read the TSF and queue each step min(10 ms, interval / 4), but at least + 5 ms, after the first TBTT that is at least min(10 ms, interval / 4) + away. A step that runs less than 5 ms after a TBTT, or less than that + margin before one, waits until after the TBTT. The first count is now + downloaded by the work too, not when mac80211 changes the beacon, so + the first CSA beacon carries the count after the first step, as with + drivers that fetch every beacon with ieee80211_beacon_get(). The replay + after a hardware restart still downloads at once.  - The first count is downloaded the same way, not when mac80211 changes - the beacon: a download close to a TBTT keeps beacons off the air. On an - RTL8821CU and an RTL8822CU a TBTT during the download sent no beacon or - the old one. On an RTL8822CE a TBTT up to about 100 us before the - download sent none and the next beacon stayed out too, and with a count - of 2 no CSA beacon went out. A step, the one that finishes the switch - too, that would run less than 5 ms after a TBTT or less than 10 ms - before one waits until 10 ms after that TBTT instead; with a beacon - interval of 20 ms or less there is no room for that and the steps are - not held. The switch completes up to one beacon interval later. The - replay after a hardware restart still downloads at once. + A late download delays the switch by a beacon interval instead of + taking a count off the air; stations that took the switch time from an + earlier count then switch up to that interval before the AP. At short + intervals a USB download, 6 to 17 ms, can run past the next TBTT, and + the USB chips then send a count more than once.  - 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. + The USB chips raise no TBTT interrupt, hence the TSF read. The beaconing + interface is always on port 0: rtw_core_port_switch() moves an AP + there, and an IBSS runs alone.  Signed-off-by: Mehmet Fide   @@ drivers/net/wireless/realtek/rtw88/fw.c: bool rtw_fw_csa_active(struct rtw_dev * return !!rtwdev->csa_vif; }  --/* paced from the first download: a late one does not delay the next */  +#define RTW_CSA_TBTT_MARGIN_US 10000 ++#define RTW_CSA_TBTT_AFTER_US 5000  +  +static u64 rtw_fw_read_tsf(struct rtw_dev *rtwdev)  +{ @@ drivers/net/wireless/realtek/rtw88/fw.c: bool rtw_fw_csa_active(struct rtw_dev *  + return ((u64)hi << 32) | lo;  +}  + -+/* how long to wait so that a step does not run next to a TBTT */ ++static u32 rtw_fw_csa_margin(u32 interval) ++{ ++ return min_t(u32, RTW_CSA_TBTT_MARGIN_US, interval / 4); ++} ++ ++static u32 rtw_fw_csa_after(u32 interval) ++{ ++ return max_t(u32, rtw_fw_csa_margin(interval), RTW_CSA_TBTT_AFTER_US); ++} ++  +static u32 rtw_fw_csa_hold(struct rtw_dev *rtwdev, u32 interval)  +{  + u32 rem;  + -+ if (interval <= 2 * RTW_CSA_TBTT_MARGIN_US) -+ return 0; -+  + div_u64_rem(rtw_fw_read_tsf(rtwdev), interval, &rem); -+ if (rem < RTW_CSA_TBTT_MARGIN_US / 2) -+ return RTW_CSA_TBTT_MARGIN_US - rem; -+ if (interval - rem < RTW_CSA_TBTT_MARGIN_US) -+ return interval - rem + RTW_CSA_TBTT_MARGIN_US; ++ if (rem < RTW_CSA_TBTT_AFTER_US) ++ return rtw_fw_csa_after(interval) - rem; ++ if (interval - rem < rtw_fw_csa_margin(interval)) ++ return interval - rem + rtw_fw_csa_after(interval);  +  + return 0;  +}  + -+/* after the first TBTT that can send what was downloaded last */ 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); @@ drivers/net/wireless/realtek/rtw88/fw.c: bool rtw_fw_csa_active(struct rtw_dev *  +  + div_u64_rem(rtw_fw_read_tsf(rtwdev), interval, &rem);  + delay = interval - rem; -+ if (delay < RTW_CSA_TBTT_MARGIN_US) ++ if (delay < rtw_fw_csa_margin(interval))  + delay += interval; ++ delay += rtw_fw_csa_after(interval);   - 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))); -+ usecs_to_jiffies(delay + -+ RTW_CSA_TBTT_MARGIN_US)); ++ usecs_to_jiffies(delay)); }  void rtw_fw_csa_start(struct rtw_dev *rtwdev, struct ieee80211_vif *vif, @@ drivers/net/wireless/realtek/rtw88/fw.c: void rtw_fw_csa_beacon_work(struct work  + usecs_to_jiffies(hold));  + goto out;  + } -+ +   + if (!rtwdev->csa_retry && rtwdev->csa_count == 1) {  + ieee80211_csa_finish(vif, 0);  + goto out;  + } -  ++  + if (!rtwdev->csa_retry) { count = ieee80211_beacon_update_cntdwn(vif, 0); if (!count) @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_bss_info_chan rtw_set_dtim_period(rtwdev, conf->dtim_period);  - ret = rtw_fw_download_rsvd_page(rtwdev);  - rtw_send_rsvd_page_h2c(rtwdev); -+ /* the countdown downloads its beacons after a TBTT */  + if (!conf->csa_active || changed & BSS_CHANGED_BEACON_ENABLED) {  + ret = rtw_fw_download_rsvd_page(rtwdev);  + rtw_send_rsvd_page_h2c(rtwdev);  + } - /* - * A beacon with CSA starts the countdown, also when a restart - * replays it. A beacon without CSA ends it. + if (conf->csa_active) + rtw_fw_csa_start(rtwdev, vif, !!ret); + else  ## drivers/net/wireless/realtek/rtw88/main.h ##  @@ drivers/net/wireless/realtek/rtw88/main.h: struct rtw_dev {