1 From 94dd7ce1885e530a7b10bbe50d5d68ba1bb99e6e Mon Sep 17 00:00:00 2001
2 From: Martin Kaistra <martin.kaistra@linutronix.de>
3 Date: Mon, 5 Feb 2024 10:30:40 +0100
4 Subject: [PATCH] wifi: rtl8xxxu: update rate mask per sta
6 Until now, rtl8xxxu_watchdog_callback() only fetches RSSI and updates
7 the rate mask in station mode. This means, in AP mode only the default
10 In order to have the rate mask reflect the actual connection quality,
11 extend rtl8xxxu_watchdog_callback() to iterate over every sta. Like in
12 the rtw88 driver, add a function to collect all currently present stas
13 and then iterate over a list of copies to ensure no RCU lock problems
14 for register access via USB. Remove the existing RCU lock in
15 rtl8xxxu_refresh_rate_mask().
17 Since the currently used ieee80211_ave_rssi() is only for 'vif', add
18 driver-level tracking of RSSI per sta.
20 Signed-off-by: Martin Kaistra <martin.kaistra@linutronix.de>
21 Reviewed-by: Ping-Ke Shih <pkshih@realtek.com>
22 Signed-off-by: Kalle Valo <kvalo@kernel.org>
23 Link: https://msgid.link/20240205093040.1941140-1-martin.kaistra@linutronix.de
25 .../net/wireless/realtek/rtl8xxxu/rtl8xxxu.h | 8 +-
26 .../wireless/realtek/rtl8xxxu/rtl8xxxu_core.c | 188 ++++++++++++++----
27 2 files changed, 158 insertions(+), 38 deletions(-)
29 --- a/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu.h
30 +++ b/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu.h
34 #include <asm/byteorder.h>
35 +#include <linux/average.h>
37 #define RTL8XXXU_DEBUG_REG_WRITE 0x01
38 #define RTL8XXXU_DEBUG_REG_READ 0x02
39 @@ -1858,6 +1859,8 @@ struct rtl8xxxu_priv {
43 + /* Ensure no added or deleted stas while iterating */
44 + struct mutex sta_mutex;
45 struct mutex h2c_mutex;
46 /* Protect the indirect register accesses of RTL8710BU. */
47 struct mutex syson_indirect_access_mutex;
48 @@ -1892,7 +1895,6 @@ struct rtl8xxxu_priv {
51 u8 int_buf[USB_INTR_CONTENT_LENGTH];
53 DECLARE_BITMAP(tx_aggr_started, IEEE80211_NUM_TIDS);
54 DECLARE_BITMAP(tid_tx_operational, IEEE80211_NUM_TIDS);
56 @@ -1913,11 +1915,15 @@ struct rtl8xxxu_priv {
57 DECLARE_BITMAP(cam_map, RTL8XXXU_MAX_SEC_CAM_NUM);
60 +DECLARE_EWMA(rssi, 10, 16);
62 struct rtl8xxxu_sta_info {
63 struct ieee80211_sta *sta;
64 struct ieee80211_vif *vif;
67 + struct ewma_rssi avg_rssi;
72 --- a/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_core.c
73 +++ b/drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_core.c
74 @@ -4991,10 +4991,11 @@ rtl8xxxu_bss_info_changed(struct ieee802
75 struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
76 struct rtl8xxxu_priv *priv = hw->priv;
77 struct device *dev = &priv->udev->dev;
78 + struct rtl8xxxu_sta_info *sta_info;
79 struct ieee80211_sta *sta;
80 struct rtl8xxxu_ra_report *rarpt;
85 rarpt = &priv->ra_report;
87 @@ -5017,6 +5018,7 @@ rtl8xxxu_bss_info_changed(struct ieee802
91 + macid = rtl8xxxu_get_macid(priv, sta);
93 if (sta->deflink.ht_cap.ht_supported)
94 dev_info(dev, "%s: HT supported\n", __func__);
95 @@ -5037,14 +5039,15 @@ rtl8xxxu_bss_info_changed(struct ieee802
100 + sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
101 + sta_info->rssi_level = RTL8XXXU_RATR_STA_INIT;
104 rtl8xxxu_update_ra_report(rarpt, highest_rate, sgi, bw);
106 - priv->rssi_level = RTL8XXXU_RATR_STA_INIT;
108 priv->fops->update_rate_mask(priv, ramask, 0, sgi,
109 - bw == RATE_INFO_BW_40, 0);
110 + bw == RATE_INFO_BW_40, macid);
112 rtl8xxxu_write8(priv, REG_BCN_MAX_ERR, 0xff);
114 @@ -6317,6 +6320,76 @@ static void rtl8188e_c2hcmd_callback(str
118 +#define rtl8xxxu_iterate_vifs_atomic(priv, iterator, data) \
119 + ieee80211_iterate_active_interfaces_atomic((priv)->hw, \
120 + IEEE80211_IFACE_ITER_NORMAL, iterator, data)
122 +struct rtl8xxxu_rx_update_rssi_data {
123 + struct rtl8xxxu_priv *priv;
124 + struct ieee80211_hdr *hdr;
125 + struct ieee80211_rx_status *rx_status;
129 +static void rtl8xxxu_rx_update_rssi_iter(void *data, u8 *mac,
130 + struct ieee80211_vif *vif)
132 + struct rtl8xxxu_rx_update_rssi_data *iter_data = data;
133 + struct ieee80211_sta *sta;
134 + struct ieee80211_hdr *hdr = iter_data->hdr;
135 + struct rtl8xxxu_priv *priv = iter_data->priv;
136 + struct rtl8xxxu_sta_info *sta_info;
137 + struct ieee80211_rx_status *rx_status = iter_data->rx_status;
138 + u8 *bssid = iter_data->bssid;
140 + if (!ether_addr_equal(vif->bss_conf.bssid, bssid))
143 + if (!(ether_addr_equal(vif->addr, hdr->addr1) ||
144 + ieee80211_is_beacon(hdr->frame_control)))
147 + sta = ieee80211_find_sta_by_ifaddr(priv->hw, hdr->addr2,
152 + sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
153 + ewma_rssi_add(&sta_info->avg_rssi, -rx_status->signal);
156 +static inline u8 *get_hdr_bssid(struct ieee80211_hdr *hdr)
158 + __le16 fc = hdr->frame_control;
161 + if (ieee80211_has_tods(fc))
162 + bssid = hdr->addr1;
163 + else if (ieee80211_has_fromds(fc))
164 + bssid = hdr->addr2;
166 + bssid = hdr->addr3;
171 +static void rtl8xxxu_rx_update_rssi(struct rtl8xxxu_priv *priv,
172 + struct ieee80211_rx_status *rx_status,
173 + struct ieee80211_hdr *hdr)
175 + struct rtl8xxxu_rx_update_rssi_data data = {};
177 + if (ieee80211_is_ctl(hdr->frame_control))
182 + data.rx_status = rx_status;
183 + data.bssid = get_hdr_bssid(hdr);
185 + rtl8xxxu_iterate_vifs_atomic(priv, rtl8xxxu_rx_update_rssi_iter, &data);
188 int rtl8xxxu_parse_rxdesc16(struct rtl8xxxu_priv *priv, struct sk_buff *skb)
190 struct ieee80211_hw *hw = priv->hw;
191 @@ -6376,18 +6449,26 @@ int rtl8xxxu_parse_rxdesc16(struct rtl8x
192 skb_queue_tail(&priv->c2hcmd_queue, skb);
193 schedule_work(&priv->c2hcmd_work);
195 + struct ieee80211_hdr *hdr;
197 phy_stats = (struct rtl8723au_phy_stats *)skb->data;
199 skb_pull(skb, drvinfo_sz + desc_shift);
201 skb_trim(skb, pkt_len);
203 - if (rx_desc->phy_stats)
204 + hdr = (struct ieee80211_hdr *)skb->data;
205 + if (rx_desc->phy_stats) {
206 priv->fops->parse_phystats(
207 priv, rx_status, phy_stats,
209 - (struct ieee80211_hdr *)skb->data,
211 rx_desc->crc32 || rx_desc->icverr);
212 + if (!rx_desc->crc32 && !rx_desc->icverr)
213 + rtl8xxxu_rx_update_rssi(priv,
218 rx_status->mactime = rx_desc->tsfl;
219 rx_status->flag |= RX_FLAG_MACTIME_START;
220 @@ -6484,10 +6565,15 @@ int rtl8xxxu_parse_rxdesc24(struct rtl8x
222 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
224 - if (rx_desc->phy_stats)
225 + if (rx_desc->phy_stats) {
226 priv->fops->parse_phystats(priv, rx_status, phy_stats,
228 rx_desc->crc32 || rx_desc->icverr);
229 + if (!rx_desc->crc32 && !rx_desc->icverr)
230 + rtl8xxxu_rx_update_rssi(priv,
235 rx_status->mactime = rx_desc->tsfl;
236 rx_status->flag |= RX_FLAG_MACTIME_START;
237 @@ -7111,6 +7197,7 @@ static void rtl8xxxu_refresh_rate_mask(s
238 int signal, struct ieee80211_sta *sta,
241 + struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
242 struct ieee80211_hw *hw = priv->hw;
244 u8 rssi_level, ratr_idx;
245 @@ -7119,7 +7206,7 @@ static void rtl8xxxu_refresh_rate_mask(s
247 u8 macid = rtl8xxxu_get_macid(priv, sta);
249 - rssi_level = priv->rssi_level;
250 + rssi_level = sta_info->rssi_level;
251 snr = rtl8xxxu_signal_to_snr(signal);
252 snr_thresh_high = RTL8XXXU_SNR_THRESH_HIGH;
253 snr_thresh_low = RTL8XXXU_SNR_THRESH_LOW;
254 @@ -7144,18 +7231,16 @@ static void rtl8xxxu_refresh_rate_mask(s
256 rssi_level = RTL8XXXU_RATR_STA_LOW;
258 - if (rssi_level != priv->rssi_level || force) {
259 + if (rssi_level != sta_info->rssi_level || force) {
264 rate_bitmap = (sta->deflink.supp_rates[0] & 0xfff) |
265 (sta->deflink.ht_cap.mcs.rx_mask[0] << 12) |
266 (sta->deflink.ht_cap.mcs.rx_mask[1] << 20);
267 if (sta->deflink.ht_cap.cap &
268 (IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20))
272 wireless_mode = rtl8xxxu_wireless_mode(hw, sta);
273 switch (wireless_mode) {
274 @@ -7236,7 +7321,7 @@ static void rtl8xxxu_refresh_rate_mask(s
278 - priv->rssi_level = rssi_level;
279 + sta_info->rssi_level = rssi_level;
280 priv->fops->update_rate_mask(priv, rate_bitmap, ratr_idx, sgi, txbw_40mhz, macid);
283 @@ -7329,40 +7414,60 @@ static void rtl8xxxu_track_cfo(struct rt
284 rtl8xxxu_set_atc_status(priv, abs(cfo_average) >= CFO_TH_ATC);
287 -static void rtl8xxxu_watchdog_callback(struct work_struct *work)
288 +static void rtl8xxxu_ra_iter(void *data, struct ieee80211_sta *sta)
290 - struct ieee80211_vif *vif;
291 - struct rtl8xxxu_priv *priv;
293 + struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
294 + struct rtl8xxxu_priv *priv = data;
295 + int signal = -ewma_rssi_read(&sta_info->avg_rssi);
297 - priv = container_of(work, struct rtl8xxxu_priv, ra_watchdog.work);
298 - for (i = 0; i < ARRAY_SIZE(priv->vifs); i++) {
299 - vif = priv->vifs[i];
300 + priv->fops->report_rssi(priv, rtl8xxxu_get_macid(priv, sta),
301 + rtl8xxxu_signal_to_snr(signal));
302 + rtl8xxxu_refresh_rate_mask(priv, signal, sta, false);
305 +struct rtl8xxxu_stas_entry {
306 + struct list_head list;
307 + struct ieee80211_sta *sta;
310 - if (!vif || vif->type != NL80211_IFTYPE_STATION)
312 +struct rtl8xxxu_iter_stas_data {
313 + struct rtl8xxxu_priv *priv;
314 + struct list_head list;
318 - struct ieee80211_sta *sta;
319 +static void rtl8xxxu_collect_sta_iter(void *data, struct ieee80211_sta *sta)
321 + struct rtl8xxxu_iter_stas_data *iter_stas = data;
322 + struct rtl8xxxu_stas_entry *stas_entry;
325 - sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
327 - struct device *dev = &priv->udev->dev;
328 + stas_entry = kmalloc(sizeof(*stas_entry), GFP_ATOMIC);
332 - dev_dbg(dev, "%s: no sta found\n", __func__);
337 + stas_entry->sta = sta;
338 + list_add_tail(&stas_entry->list, &iter_stas->list);
341 - signal = ieee80211_ave_rssi(vif);
342 +static void rtl8xxxu_watchdog_callback(struct work_struct *work)
345 - priv->fops->report_rssi(priv, rtl8xxxu_get_macid(priv, sta),
346 - rtl8xxxu_signal_to_snr(signal));
347 + struct rtl8xxxu_iter_stas_data iter_data;
348 + struct rtl8xxxu_stas_entry *sta_entry, *tmp;
349 + struct rtl8xxxu_priv *priv;
351 - rtl8xxxu_refresh_rate_mask(priv, signal, sta, false);
352 + priv = container_of(work, struct rtl8xxxu_priv, ra_watchdog.work);
353 + iter_data.priv = priv;
354 + INIT_LIST_HEAD(&iter_data.list);
356 + mutex_lock(&priv->sta_mutex);
357 + ieee80211_iterate_stations_atomic(priv->hw, rtl8xxxu_collect_sta_iter,
359 + list_for_each_entry_safe(sta_entry, tmp, &iter_data.list, list) {
360 + list_del_init(&sta_entry->list);
361 + rtl8xxxu_ra_iter(priv, sta_entry->sta);
364 + mutex_unlock(&priv->sta_mutex);
366 if (priv->fops->set_crystal_cap)
367 rtl8xxxu_track_cfo(priv);
368 @@ -7504,10 +7609,15 @@ static int rtl8xxxu_sta_add(struct ieee8
369 struct rtl8xxxu_vif *rtlvif = (struct rtl8xxxu_vif *)vif->drv_priv;
370 struct rtl8xxxu_priv *priv = hw->priv;
372 + mutex_lock(&priv->sta_mutex);
373 + ewma_rssi_init(&sta_info->avg_rssi);
374 if (vif->type == NL80211_IFTYPE_AP) {
375 + sta_info->rssi_level = RTL8XXXU_RATR_STA_INIT;
376 sta_info->macid = rtl8xxxu_acquire_macid(priv);
377 - if (sta_info->macid >= RTL8XXXU_MAX_MAC_ID_NUM)
378 + if (sta_info->macid >= RTL8XXXU_MAX_MAC_ID_NUM) {
379 + mutex_unlock(&priv->sta_mutex);
383 rtl8xxxu_refresh_rate_mask(priv, 0, sta, true);
384 priv->fops->report_connect(priv, sta_info->macid, H2C_MACID_ROLE_STA, true);
385 @@ -7523,6 +7633,7 @@ static int rtl8xxxu_sta_add(struct ieee8
389 + mutex_unlock(&priv->sta_mutex);
393 @@ -7534,8 +7645,10 @@ static int rtl8xxxu_sta_remove(struct ie
394 struct rtl8xxxu_sta_info *sta_info = (struct rtl8xxxu_sta_info *)sta->drv_priv;
395 struct rtl8xxxu_priv *priv = hw->priv;
397 + mutex_lock(&priv->sta_mutex);
398 if (vif->type == NL80211_IFTYPE_AP)
399 rtl8xxxu_release_macid(priv, sta_info->macid);
400 + mutex_unlock(&priv->sta_mutex);
404 @@ -7766,6 +7879,7 @@ static int rtl8xxxu_probe(struct usb_int
405 mutex_init(&priv->usb_buf_mutex);
406 mutex_init(&priv->syson_indirect_access_mutex);
407 mutex_init(&priv->h2c_mutex);
408 + mutex_init(&priv->sta_mutex);
409 INIT_LIST_HEAD(&priv->tx_urb_free_list);
410 spin_lock_init(&priv->tx_urb_lock);
411 INIT_LIST_HEAD(&priv->rx_urb_pending_list);