blob: eddb9924a7b038bec4268f41868b54a22ca596d9 [file] [log] [blame]
developer05f3b2b2024-08-19 19:17:34 +08001From 70526aabf704d778796dfbaa042fe48e03aa7d61 Mon Sep 17 00:00:00 2001
developer1a173672023-12-21 14:49:33 +08002From: StanleyYP Wang <StanleyYP.Wang@mediatek.com>
3Date: Wed, 15 Nov 2023 15:05:17 +0800
4Subject: [PATCH] mac80211: mtk: add DFS CAC countdown in CSA flow
5
6Signed-off-by: StanleyYP Wang <StanleyYP.Wang@mediatek.com>
7---
developerdc9eeae2024-04-08 14:36:46 +08008 include/net/cfg80211.h | 32 +++++++++++++++
9 net/mac80211/cfg.c | 84 +++++++++++++++++++++++++++++++++++---
10 net/mac80211/ieee80211_i.h | 2 +
11 net/mac80211/iface.c | 2 +
12 net/mac80211/mlme.c | 6 ++-
developer05f3b2b2024-08-19 19:17:34 +080013 net/mac80211/util.c | 16 +++++++-
developerdc9eeae2024-04-08 14:36:46 +080014 net/wireless/chan.c | 72 ++++++++++++++++++++++++++++++++
15 net/wireless/nl80211.c | 5 ++-
16 net/wireless/rdev-ops.h | 17 ++++++++
developer05f3b2b2024-08-19 19:17:34 +080017 9 files changed, 226 insertions(+), 10 deletions(-)
developer1a173672023-12-21 14:49:33 +080018
19diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h
20index 03f072f..a443b0d 100644
21--- a/include/net/cfg80211.h
22+++ b/include/net/cfg80211.h
23@@ -4308,6 +4308,10 @@ struct cfg80211_ops {
24 struct net_device *dev,
25 struct cfg80211_chan_def *chandef,
26 u32 cac_time_ms);
27+ int (*start_radar_detection_post_csa)(struct wiphy *wiphy,
28+ struct net_device *dev,
29+ struct cfg80211_chan_def *chandef,
30+ u32 cac_time_ms);
31 void (*end_cac)(struct wiphy *wiphy,
32 struct net_device *dev);
33 int (*update_ft_ies)(struct wiphy *wiphy, struct net_device *dev,
34@@ -7796,6 +7800,34 @@ bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
35 struct cfg80211_chan_def *chandef,
36 enum nl80211_iftype iftype);
37
38+/**
39+ * cfg80211_reg_can_beacon_dfs_relax - check if beaconing is allowed with DFS & IR-relaxation
40+ * @wiphy: the wiphy
41+ * @chandef: the channel definition
42+ * @iftype: interface type
43+ *
44+ * Return: %true if there is no secondary channel or the secondary channel(s)
45+ * can be used for beaconing. This version bypasses radar channel check, allowing
46+ * channel switch to a USABLE DFS channel and performing CAC after the channel switch.
47+ * It also checks if IR-relaxation conditions apply, to allow beaconing under more
48+ * permissive conditions.
49+ *
50+ * Requires the wiphy mutex to be held.
51+ */
52+bool cfg80211_reg_can_beacon_dfs_relax(struct wiphy *wiphy,
53+ struct cfg80211_chan_def *chandef,
54+ enum nl80211_iftype iftype);
55+
56+/**
57+ * cfg80211_start_radar_detection_post_csa - start radar detection after CSA
58+ * @wiphy: the wiphy
59+ * @wdev: the wireless device
60+ * @chandef: the channel definition to start radar detection on
61+ */
62+int cfg80211_start_radar_detection_post_csa(struct wiphy *wiphy,
63+ struct wireless_dev *wdev,
64+ struct cfg80211_chan_def *chandef);
65+
66 /*
67 * cfg80211_ch_switch_notify - update wdev channel and notify userspace
68 * @dev: the device which switched channels
69diff --git a/net/mac80211/cfg.c b/net/mac80211/cfg.c
developerdc9eeae2024-04-08 14:36:46 +080070index 56381f8..7a30ca6 100644
developer1a173672023-12-21 14:49:33 +080071--- a/net/mac80211/cfg.c
72+++ b/net/mac80211/cfg.c
developereb155692024-01-11 14:08:37 +080073@@ -3328,6 +3328,39 @@ static int ieee80211_set_after_csa_beacon(struct ieee80211_sub_if_data *sdata,
developer1a173672023-12-21 14:49:33 +080074 return 0;
75 }
76
developereb155692024-01-11 14:08:37 +080077+void ieee80211_cac_sta_flush_work(struct work_struct *work)
78+{
79+ struct ieee80211_sub_if_data *sdata =
80+ container_of(work, struct ieee80211_sub_if_data,
81+ cac_sta_flush_work);
82+
83+ __sta_info_flush(sdata, true);
84+}
85+
developer1a173672023-12-21 14:49:33 +080086+static int ieee80211_start_radar_detection_post_csa(struct wiphy *wiphy,
87+ struct net_device *dev,
88+ struct cfg80211_chan_def *chandef,
89+ u32 cac_time_ms)
90+{
91+ struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
92+ struct ieee80211_local *local = sdata->local;
93+
94+ if (!list_empty(&local->roc_list) || local->scanning)
95+ return -EBUSY;
96+
97+ /* whatever, but channel contexts should not complain about that one */
98+ sdata->smps_mode = IEEE80211_SMPS_OFF;
99+ sdata->needed_rx_chains = local->rx_chains;
100+
developereb155692024-01-11 14:08:37 +0800101+ ieee80211_queue_work(&local->hw, &sdata->cac_sta_flush_work);
102+
103+ ieee80211_queue_delayed_work(&local->hw,
developer1a173672023-12-21 14:49:33 +0800104+ &sdata->dfs_cac_timer_work,
105+ msecs_to_jiffies(cac_time_ms));
106+
107+ return 1;
108+}
109+
110 static int __ieee80211_csa_finalize(struct ieee80211_sub_if_data *sdata)
111 {
112 struct ieee80211_local *local = sdata->local;
developereb155692024-01-11 14:08:37 +0800113@@ -3361,6 +3394,11 @@ static int __ieee80211_csa_finalize(struct ieee80211_sub_if_data *sdata)
developer1a173672023-12-21 14:49:33 +0800114 &sdata->csa_chandef))
115 return -EINVAL;
116
117+ err = cfg80211_start_radar_detection_post_csa(local->hw.wiphy, &sdata->wdev,
118+ &sdata->vif.bss_conf.chandef);
119+ if (err)
120+ return err > 0 ? 0 : err;
121+
122 sdata->vif.csa_active = false;
123
124 err = ieee80211_set_after_csa_beacon(sdata, &changed);
developerdc9eeae2024-04-08 14:36:46 +0800125@@ -3428,6 +3466,11 @@ static int ieee80211_set_csa_beacon(struct ieee80211_sub_if_data *sdata,
126
127 switch (sdata->vif.type) {
128 case NL80211_IFTYPE_AP:
129+ if (sdata->u.ap.next_beacon) {
130+ kfree(sdata->u.ap.next_beacon->mbssid_ies);
131+ kfree(sdata->u.ap.next_beacon);
132+ sdata->u.ap.next_beacon = NULL;
133+ }
134 sdata->u.ap.next_beacon =
135 cfg80211_beacon_dup(&params->beacon_after);
136 if (!sdata->u.ap.next_beacon)
137@@ -3586,15 +3629,14 @@ __ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
138 if (!list_empty(&local->roc_list) || local->scanning)
139 return -EBUSY;
140
141- if (sdata->wdev.cac_started)
142- return -EBUSY;
143-
144 if (cfg80211_chandef_identical(&params->chandef,
145 &sdata->vif.bss_conf.chandef))
146 return -EINVAL;
147
148- /* don't allow another channel switch if one is already active. */
149- if (sdata->vif.csa_active)
150+ /* don't allow another channel switch if one is already active
151+ * unless its during post CSA radar detection.
152+ */
153+ if (sdata->vif.csa_active && !sdata->wdev.cac_started)
154 return -EBUSY;
155
156 mutex_lock(&local->chanctx_mtx);
157@@ -3646,6 +3688,14 @@ __ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
158 goto out;
159 }
160
161+ /* Finalize CSA immediately if CAC is started during last channel switch */
162+ if (sdata->wdev.cac_started) {
163+ ieee80211_wake_vif_queues(local, sdata, IEEE80211_QUEUE_STOP_REASON_CSA);
164+ cancel_delayed_work(&sdata->dfs_cac_timer_work);
165+ sdata->wdev.cac_started = false;
166+ changed = 0;
167+ }
168+
169 sdata->csa_chandef = params->chandef;
170 sdata->csa_block_tx = params->block_tx;
171 sdata->vif.csa_active = true;
172@@ -3661,6 +3711,23 @@ __ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
173 ieee80211_bss_info_change_notify(sdata, changed);
174 drv_channel_switch_beacon(sdata, &params->chandef);
175 } else {
176+ struct ieee80211_sub_if_data *tmp;
177+ int n_assigned = 0, n_reserved = 0;
178+
179+ list_for_each_entry(tmp, &chanctx->assigned_vifs,
180+ assigned_chanctx_list) {
181+ n_assigned++;
182+ if (tmp->reserved_chanctx)
183+ n_reserved++;
184+ }
185+
186+ /* Wait for all interfaces to be ready */
187+ if (n_assigned != n_reserved) {
188+ sdata->reserved_ready = true;
189+ err = 0;
190+ goto out;
191+ }
192+
193 /* if the beacon didn't change, we can finalize immediately */
194 ieee80211_csa_finalize(sdata);
195 }
196@@ -4538,7 +4605,11 @@ ieee80211_skip_cac(struct wireless_dev *wdev)
developer1a173672023-12-21 14:49:33 +0800197
198 cancel_delayed_work(&sdata->dfs_cac_timer_work);
199 if (wdev->cac_started) {
200- ieee80211_vif_release_channel(sdata);
201+ if (sdata->vif.csa_active)
202+ ieee80211_queue_work(&sdata->local->hw,
203+ &sdata->csa_finalize_work);
204+ else
205+ ieee80211_vif_release_channel(sdata);
206 cac_time_ms = wdev->cac_time_ms;
207 wdev->cac_start_time = jiffies -
208 msecs_to_jiffies(cac_time_ms + 1);
developerdc9eeae2024-04-08 14:36:46 +0800209@@ -4630,6 +4701,7 @@ const struct cfg80211_ops mac80211_config_ops = {
developer1a173672023-12-21 14:49:33 +0800210 #endif
211 .get_channel = ieee80211_cfg_get_channel,
212 .start_radar_detection = ieee80211_start_radar_detection,
213+ .start_radar_detection_post_csa = ieee80211_start_radar_detection_post_csa,
214 .end_cac = ieee80211_end_cac,
215 .channel_switch = ieee80211_channel_switch,
216 .set_qos_map = ieee80211_set_qos_map,
developereb155692024-01-11 14:08:37 +0800217diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h
218index 2519c14..bb5906d 100644
219--- a/net/mac80211/ieee80211_i.h
220+++ b/net/mac80211/ieee80211_i.h
221@@ -962,6 +962,7 @@ struct ieee80211_sub_if_data {
222 struct mac80211_qos_map __rcu *qos_map;
223
224 struct work_struct csa_finalize_work;
225+ struct work_struct cac_sta_flush_work;
226 bool csa_block_tx; /* write-protected by sdata_lock and local->mtx */
227 struct cfg80211_chan_def csa_chandef;
228
229@@ -1812,6 +1813,7 @@ int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
230 void ieee80211_csa_finalize_work(struct work_struct *work);
231 int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
232 struct cfg80211_csa_settings *params);
233+void ieee80211_cac_sta_flush_work(struct work_struct *work);
234
235 #define IEEE80211_BSS_COLOR_AGEOUT_TIME 10
236 #define IEEE80211_BSS_COLOR_MAX 64
237diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c
238index 00b0443..ef32d53 100644
239--- a/net/mac80211/iface.c
240+++ b/net/mac80211/iface.c
241@@ -463,6 +463,7 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata, bool going_do
242 sdata_unlock(sdata);
243
244 cancel_work_sync(&sdata->csa_finalize_work);
245+ cancel_work_sync(&sdata->cac_sta_flush_work);
246 cancel_work_sync(&sdata->color_change_finalize_work);
247
248 cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
249@@ -1749,6 +1750,7 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
250 INIT_WORK(&sdata->work, ieee80211_iface_work);
251 INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
252 INIT_WORK(&sdata->csa_finalize_work, ieee80211_csa_finalize_work);
253+ INIT_WORK(&sdata->cac_sta_flush_work, ieee80211_cac_sta_flush_work);
254 INIT_WORK(&sdata->color_change_finalize_work, ieee80211_color_change_finalize_work);
255 INIT_LIST_HEAD(&sdata->assigned_chanctx_list);
256 INIT_LIST_HEAD(&sdata->reserved_chanctx_list);
developer1a173672023-12-21 14:49:33 +0800257diff --git a/net/mac80211/mlme.c b/net/mac80211/mlme.c
developerdc9eeae2024-04-08 14:36:46 +0800258index 2dbc18c..ed81ebf 100644
developer1a173672023-12-21 14:49:33 +0800259--- a/net/mac80211/mlme.c
260+++ b/net/mac80211/mlme.c
261@@ -1870,7 +1870,11 @@ void ieee80211_dfs_cac_timer_work(struct work_struct *work)
262
263 mutex_lock(&sdata->local->mtx);
264 if (sdata->wdev.cac_started) {
265- ieee80211_vif_release_channel(sdata);
266+ if (sdata->vif.csa_active)
267+ ieee80211_queue_work(&sdata->local->hw,
268+ &sdata->csa_finalize_work);
269+ else
270+ ieee80211_vif_release_channel(sdata);
271 cfg80211_cac_event(sdata->dev, &chandef,
272 NL80211_RADAR_CAC_FINISHED,
273 GFP_KERNEL);
274diff --git a/net/mac80211/util.c b/net/mac80211/util.c
developer05f3b2b2024-08-19 19:17:34 +0800275index 26cd627..1e8420d 100644
developer1a173672023-12-21 14:49:33 +0800276--- a/net/mac80211/util.c
277+++ b/net/mac80211/util.c
developer05f3b2b2024-08-19 19:17:34 +0800278@@ -3873,7 +3873,21 @@ void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
developer1a173672023-12-21 14:49:33 +0800279
280 if (sdata->wdev.cac_started) {
281 chandef = sdata->vif.bss_conf.chandef;
282- ieee80211_vif_release_channel(sdata);
283+ if (sdata->vif.csa_active) {
284+ sdata->vif.csa_active = false;
developer05f3b2b2024-08-19 19:17:34 +0800285+ if (sdata->csa_block_tx) {
286+ ieee80211_wake_vif_queues(local, sdata,
287+ IEEE80211_QUEUE_STOP_REASON_CSA);
288+ sdata->csa_block_tx = false;
289+ }
developer1a173672023-12-21 14:49:33 +0800290+ if (sdata->u.ap.next_beacon) {
291+ kfree(sdata->u.ap.next_beacon->mbssid_ies);
292+ kfree(sdata->u.ap.next_beacon);
293+ sdata->u.ap.next_beacon = NULL;
294+ }
295+ } else {
296+ ieee80211_vif_release_channel(sdata);
297+ }
298 cfg80211_cac_event(sdata->dev,
299 &chandef,
300 NL80211_RADAR_CAC_ABORTED,
301diff --git a/net/wireless/chan.c b/net/wireless/chan.c
developer05f3b2b2024-08-19 19:17:34 +0800302index f48995c..c7bfa6b 100644
developer1a173672023-12-21 14:49:33 +0800303--- a/net/wireless/chan.c
304+++ b/net/wireless/chan.c
developerdc9eeae2024-04-08 14:36:46 +0800305@@ -1262,6 +1262,78 @@ bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
developer1a173672023-12-21 14:49:33 +0800306 }
307 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
308
309+bool cfg80211_reg_can_beacon_dfs_relax(struct wiphy *wiphy,
310+ struct cfg80211_chan_def *chandef,
311+ enum nl80211_iftype iftype)
312+{
313+ struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
314+ u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
315+ IEEE80211_CHAN_RADAR;
316+
317+ lockdep_assert_held(&rdev->wiphy.mtx);
318+
319+ /* Bypass available and usable dfs channel */
320+ if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
321+ (cfg80211_chandef_dfs_usable(wiphy, chandef) ||
322+ cfg80211_chandef_dfs_available(wiphy, chandef)))
323+ prohibited_flags = IEEE80211_CHAN_DISABLED;
324+
325+ /* Under certain conditions suggested by some regulatory bodies a
326+ * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
327+ * only if such relaxations are not enabled and the conditions are not
328+ * met.
329+ */
330+ if (!cfg80211_ir_permissive_chan(wiphy, iftype, chandef->chan))
331+ prohibited_flags |= IEEE80211_CHAN_NO_IR;
332+
333+ return cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
334+}
335+EXPORT_SYMBOL(cfg80211_reg_can_beacon_dfs_relax);
336+
337+int cfg80211_start_radar_detection_post_csa(struct wiphy *wiphy,
338+ struct wireless_dev *wdev,
339+ struct cfg80211_chan_def *chandef)
340+{
341+ struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
342+ u32 cac_time_ms;
343+ enum nl80211_dfs_regions dfs_region;
344+ int ret = 0;
345+
developerdc9eeae2024-04-08 14:36:46 +0800346+ /* Update DFS channel state especially when original channel include DFS channel */
347+ cfg80211_sched_dfs_chan_update(rdev);
348+
developer1a173672023-12-21 14:49:33 +0800349+ if (cfg80211_chandef_dfs_available(wiphy, chandef))
350+ goto out;
351+
developer1a173672023-12-21 14:49:33 +0800352+ dfs_region = reg_get_dfs_region(wiphy);
353+ if (dfs_region == NL80211_DFS_UNSET)
354+ goto out;
355+
356+ cac_time_ms = cfg80211_chandef_dfs_cac_time(wiphy, chandef);
357+ if (WARN_ON(!cac_time_ms))
358+ cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
359+
360+ pr_info("%s: region = %u, center freq1 = %u, center freq2 = %u, cac time ms = %u\n",
361+ __func__, dfs_region, chandef->center_freq1, chandef->center_freq2, cac_time_ms);
362+
363+ ret = rdev_start_radar_detection_post_csa(rdev, wdev->netdev, chandef, cac_time_ms);
364+ if (ret > 0) {
365+ wdev->chandef = *chandef;
366+ wdev->cac_started = true;
367+ wdev->cac_start_time = jiffies;
368+ wdev->cac_time_ms = cac_time_ms;
369+ if (rdev->background_cac_started &&
370+ cfg80211_is_sub_chan(chandef, rdev->background_radar_chandef.chan)) {
371+ cfg80211_stop_background_radar_detection(rdev->background_radar_wdev);
372+ }
373+ cfg80211_cac_event(wdev->netdev, chandef, NL80211_RADAR_CAC_STARTED, GFP_KERNEL);
374+ }
375+
376+out:
377+ return ret;
378+}
379+EXPORT_SYMBOL(cfg80211_start_radar_detection_post_csa);
380+
381 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
382 struct cfg80211_chan_def *chandef)
383 {
384diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c
385index 97c2833..4883b1f 100644
386--- a/net/wireless/nl80211.c
387+++ b/net/wireless/nl80211.c
388@@ -9625,8 +9625,9 @@ skip_beacons:
389 cfg80211_set_dfs_state(&rdev->wiphy, &params.chandef, NL80211_DFS_AVAILABLE);
390 }
391
392- if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
393- wdev->iftype)) {
394+ /* handle DFS CAC after CSA is sent */
395+ if (!cfg80211_reg_can_beacon_dfs_relax(&rdev->wiphy, &params.chandef,
396+ wdev->iftype)) {
397 err = -EINVAL;
398 goto free;
399 }
400diff --git a/net/wireless/rdev-ops.h b/net/wireless/rdev-ops.h
401index 26f4604..f4d050b 100644
402--- a/net/wireless/rdev-ops.h
403+++ b/net/wireless/rdev-ops.h
404@@ -1190,6 +1190,23 @@ rdev_start_radar_detection(struct cfg80211_registered_device *rdev,
405 return ret;
406 }
407
408+static inline int
409+rdev_start_radar_detection_post_csa(struct cfg80211_registered_device *rdev,
410+ struct net_device *dev,
411+ struct cfg80211_chan_def *chandef,
412+ u32 cac_time_ms)
413+{
414+ int ret = -EOPNOTSUPP;
415+
416+ trace_rdev_start_radar_detection(&rdev->wiphy, dev, chandef,
417+ cac_time_ms);
418+ if (rdev->ops->start_radar_detection_post_csa)
419+ ret = rdev->ops->start_radar_detection_post_csa(&rdev->wiphy, dev,
420+ chandef, cac_time_ms);
421+ trace_rdev_return_int(&rdev->wiphy, ret);
422+ return ret;
423+}
424+
425 static inline void
426 rdev_end_cac(struct cfg80211_registered_device *rdev,
427 struct net_device *dev)
428--
4292.18.0
430