1: 65e3b604f9cb = 1: 88f815ec12ad wifi: rtw88: download the beacon the reserved page was built with 2: 98a6efdf30e1 ! 2: 8f02f0de48c9 wifi: rtw88: support channel switch in AP mode @@ Commit message 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. + 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, 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, and so does the driver after a WoWLAN resume, which mac80211 - follows with no replay. 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. + rtw_core_stop(), and is not started while the device is stopped; the + BSS_CHANGED_BEACON replay after a hardware restart resumes it, and so + does the driver after a WoWLAN resume, which mac80211 follows with no + replay. + + Only the work advances the count: while a switch is announced the + beacon of the page is fetched as a template, which leaves the count as + it is, and the work steps it before each of its downloads. A download + for another reason, the replay after a hardware restart or the page of + the WoWLAN firmware, then repeats the count that is on the air. 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 in its countdown 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.  Signed-off-by: Mehmet Fide   ## drivers/net/wireless/realtek/rtw88/fw.c ## +@@ drivers/net/wireless/realtek/rtw88/fw.c: static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw, + struct ieee80211_vif *vif; + struct rtw_vif *rtwvif; + struct sk_buff *skb_new; ++ struct ieee80211_mutable_offsets offs = {}; + struct cfg80211_ssid *ssid; + u16 tim_offset = 0; +  +@@ drivers/net/wireless/realtek/rtw88/fw.c: static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw, +  + 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); ++ tim_offset = offs.tim_offset; ++ } else { ++ skb_new = ieee80211_beacon_get_tim(hw, vif, &tim_offset, ++ NULL, 0); ++ } + rsvd_pkt->tim_offset = tim_offset; + break; + case RSVD_PS_POLL:  @@ drivers/net/wireless/realtek/rtw88/fw.c: int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev) return ret; } @@ drivers/net/wireless/realtek/rtw88/fw.c: int rtw_fw_download_rsvd_page(struct rt  +{  + lockdep_assert_held(&rtwdev->mutex);  + -+ if (rtwdev->csa_vif == vif) ++ if (rtwdev->csa_vif == vif || ++ !test_bit(RTW_FLAG_RUNNING, rtwdev->flags))  + return;  +  + rtwdev->csa_vif = vif; @@ drivers/net/wireless/realtek/rtw88/fw.c: int rtw_fw_download_rsvd_page(struct rt  + rtw_fw_csa_queue(rtwdev, vif);  +}  + -+void rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif) ++bool rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)  +{  + lockdep_assert_held(&rtwdev->mutex);  +  + if (!vif || rtwdev->csa_vif != vif) -+ return; ++ return false;  +  + rtwdev->csa_vif = NULL;  + cancel_delayed_work(&rtwdev->csa_beacon_work); ++ ++ return true;  +}  + void rtw_fw_update_beacon_work(struct work_struct *work) @@ 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);  + -+ /* a fetch advances the countdown, the next download carries the TIM */ ++ /* 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/fw.c: int rtw_fw_download_rsvd_page(struct rt  + }  +  + if (!ieee80211_beacon_cntdwn_is_complete(vif, 0)) { ++ ieee80211_beacon_update_cntdwn(vif, 0);  + rtw_fw_download_rsvd_page(rtwdev);  + rtw_send_rsvd_page_h2c(rtwdev);  + rtw_fw_csa_queue(rtwdev, vif); @@ drivers/net/wireless/realtek/rtw88/fw.h: void rtw_add_rsvd_page_pno(struct rtw_d 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); ++bool 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); @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_bss_info_chan 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 */ ++ /* ++ * 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);  + else @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_bss_info_chan  if (changed & BSS_CHANGED_BEACON_ENABLED) {  @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static void rtw_ops_stop_ap(struct ieee80211_hw *hw, + struct ieee80211_bss_conf *link_conf) + { struct rtw_dev *rtwdev = hw->priv; ++ bool csa;  mutex_lock(&rtwdev->mutex); -+ rtw_fw_csa_stop(rtwdev, vif); ++ csa = 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; -@@ drivers/net/wireless/realtek/rtw88/mac80211.c: static int rtw_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, - return 0; +- if (!rtw_core_check_sta_active(rtwdev)) ++ if (!rtw_core_check_sta_active(rtwdev)) { + rtw_clear_op_chan(rtwdev); ++ } else if (csa) { ++ /* what the countdown held back */ ++ rtw_fw_download_rsvd_page(rtwdev); ++ rtw_send_rsvd_page_h2c(rtwdev); ++ } + mutex_unlock(&rtwdev->mutex); }  -+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)  @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static int rtw_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, }  @@ drivers/net/wireless/realtek/rtw88/mac80211.c: static int rtw_ops_hw_scan(struct  return ret; } -@@ drivers/net/wireless/realtek/rtw88/mac80211.c: 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 ##  @@ drivers/net/wireless/realtek/rtw88/main.c: void rtw_core_stop(struct rtw_dev *rtwdev) 3: afa71da8e515 ! 3: bc0f603679f2 wifi: rtw88: time the countdown downloads from the TBTT @@ Commit message  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. + the next at once. A download that ran more than a beacon interval late + restarts the schedule from the next TBTT.  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 @@ drivers/net/wireless/realtek/rtw88/fw.c: bool rtw_fw_csa_active(struct rtw_dev * s64 delay;  + u32 rem;  + -+ if (start) { -+ div_u64_rem(tsf, interval, &rem); -+ rtwdev->csa_tbtt = tsf - rem + interval; -+ } else { -+ rtwdev->csa_tbtt += interval; -+ } -+ ++ rtwdev->csa_tbtt += interval;  + delay = rtwdev->csa_tbtt + RTW_CSA_TBTT_MARGIN_US - tsf; -+ if (delay > interval + RTW_CSA_TBTT_MARGIN_US || ++ if (start || delay > interval + RTW_CSA_TBTT_MARGIN_US ||  + delay < -(s64)interval) { -+ /* the TSF jumped: restart from the next TBTT */ ++ /* at the start, or the download ran too late: 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; @@ drivers/net/wireless/realtek/rtw88/fw.c: void rtw_fw_csa_start(struct rtw_dev *r  + rtw_fw_csa_queue(rtwdev, vif, true); }  - void rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif) + bool rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)  @@ drivers/net/wireless/realtek/rtw88/fw.c: void rtw_fw_csa_beacon_work(struct work_struct *work) - if (!ieee80211_beacon_cntdwn_is_complete(vif, 0)) { + ieee80211_beacon_update_cntdwn(vif, 0); rtw_fw_download_rsvd_page(rtwdev); rtw_send_rsvd_page_h2c(rtwdev);  - rtw_fw_csa_queue(rtwdev, vif); @@ 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);  + } - /* restart replay resumes it, a beacon without CSA ends it */ - if (conf->csa_active) - rtw_fw_csa_start(rtwdev, vif); + /* + * A beacon with CSA starts the countdown, also when a restart + * replays it. A beacon without CSA ends it.  ## drivers/net/wireless/realtek/rtw88/main.h ##  @@ drivers/net/wireless/realtek/rtw88/main.h: struct rtw_dev {