From: Mehmet Fide 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 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 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. The switch completes up to one beacon interval later. The replay after a hardware restart still downloads at 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. Signed-off-by: Mehmet Fide --- drivers/net/wireless/realtek/rtw88/fw.c | 42 +++++++++++++++---- drivers/net/wireless/realtek/rtw88/mac80211.c | 7 +++- drivers/net/wireless/realtek/rtw88/main.h | 2 +- drivers/net/wireless/realtek/rtw88/reg.h | 1 + 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/drivers/net/wireless/realtek/rtw88/fw.c b/drivers/net/wireless/realtek/rtw88/fw.c index 6968b608c54f..2b5d6268e7f9 100644 --- a/drivers/net/wireless/realtek/rtw88/fw.c +++ b/drivers/net/wireless/realtek/rtw88/fw.c @@ -3,6 +3,7 @@ */ #include +#include #include "main.h" #include "coex.h" @@ -1818,14 +1819,41 @@ 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; + + rtwdev->csa_tbtt += interval; + delay = rtwdev->csa_tbtt + RTW_CSA_TBTT_MARGIN_US - tsf; + if (start || delay > interval + RTW_CSA_TBTT_MARGIN_US || + delay < -(s64)interval) { + /* 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; + } - 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))); } @@ -1839,8 +1867,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); } bool rtw_fw_csa_stop(struct rtw_dev *rtwdev, struct ieee80211_vif *vif) @@ -1893,7 +1921,7 @@ void rtw_fw_csa_beacon_work(struct work_struct *work) ieee80211_beacon_update_cntdwn(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); } diff --git a/drivers/net/wireless/realtek/rtw88/mac80211.c b/drivers/net/wireless/realtek/rtw88/mac80211.c index 4bf47116ac73..b88fc420058f 100644 --- a/drivers/net/wireless/realtek/rtw88/mac80211.c +++ b/drivers/net/wireless/realtek/rtw88/mac80211.c @@ -441,8 +441,11 @@ static void rtw_ops_bss_info_changed(struct ieee80211_hw *hw, if (changed & BSS_CHANGED_BEACON) { rtw_set_dtim_period(rtwdev, conf->dtim_period); - 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) { + 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. diff --git a/drivers/net/wireless/realtek/rtw88/main.h b/drivers/net/wireless/realtek/rtw88/main.h index 5d18841a705f..466992b85c43 100644 --- a/drivers/net/wireless/realtek/rtw88/main.h +++ b/drivers/net/wireless/realtek/rtw88/main.h @@ -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; diff --git a/drivers/net/wireless/realtek/rtw88/reg.h b/drivers/net/wireless/realtek/rtw88/reg.h index afcdb66fdb9a..3ff4ae0885ef 100644 --- a/drivers/net/wireless/realtek/rtw88/reg.h +++ b/drivers/net/wireless/realtek/rtw88/reg.h @@ -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)) -- 2.56.0