2038ee69db71806a5a8841b92121944771fdf1d1
[openwrt/staging/ansuel.git] /
1 From: Lorenzo Bianconi <lorenzo@kernel.org>
2 Date: Sat, 23 Oct 2021 11:10:50 +0200
3 Subject: [PATCH] cfg80211: implement APIs for dedicated radar detection HW
4
5 If a dedicated (off-channel) radar detection hardware (chain)
6 is available in the hardware/driver, allow this to be used by
7 calling the NL80211_CMD_RADAR_DETECT command with a new flag
8 attribute requesting off-channel radar detection is used.
9
10 Offchannel CAC (channel availability check) avoids the CAC
11 downtime when switching to a radar channel or when turning on
12 the AP.
13
14 Drivers advertise support for this using the new feature flag
15 NL80211_EXT_FEATURE_RADAR_OFFCHAN.
16
17 Tested-by: Evelyn Tsai <evelyn.tsai@mediatek.com>
18 Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
19 Link: https://lore.kernel.org/r/7468e291ef5d05d692c1738d25b8f778d8ea5c3f.1634979655.git.lorenzo@kernel.org
20 Link: https://lore.kernel.org/r/1e60e60fef00e14401adae81c3d49f3e5f307537.1634979655.git.lorenzo@kernel.org
21 Link: https://lore.kernel.org/r/85fa50f57fc3adb2934c8d9ca0be30394de6b7e8.1634979655.git.lorenzo@kernel.org
22 Link: https://lore.kernel.org/r/4b6c08671ad59aae0ac46fc94c02f31b1610eb72.1634979655.git.lorenzo@kernel.org
23 Link: https://lore.kernel.org/r/241849ccaf2c228873c6f8495bf87b19159ba458.1634979655.git.lorenzo@kernel.org
24 [remove offchan_mutex, fix cfg80211_stop_offchan_radar_detection(),
25 remove gfp_t argument, fix documentation, fix tracing]
26 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
27 ---
28
29 --- a/include/net/cfg80211.h
30 +++ b/include/net/cfg80211.h
31 @@ -4057,6 +4057,15 @@ struct mgmt_frame_regs {
32 * @set_sar_specs: Update the SAR (TX power) settings.
33 *
34 * @color_change: Initiate a color change.
35 + *
36 + * @set_radar_offchan: Configure dedicated offchannel chain available for
37 + * radar/CAC detection on some hw. This chain can't be used to transmit
38 + * or receive frames and it is bounded to a running wdev.
39 + * Offchannel radar/CAC detection allows to avoid the CAC downtime
40 + * switching to a different channel during CAC detection on the selected
41 + * radar channel.
42 + * The caller is expected to set chandef pointer to NULL in order to
43 + * disable offchannel CAC/radar detection.
44 */
45 struct cfg80211_ops {
46 int (*suspend)(struct wiphy *wiphy, struct cfg80211_wowlan *wow);
47 @@ -4387,6 +4396,8 @@ struct cfg80211_ops {
48 int (*color_change)(struct wiphy *wiphy,
49 struct net_device *dev,
50 struct cfg80211_color_change_settings *params);
51 + int (*set_radar_offchan)(struct wiphy *wiphy,
52 + struct cfg80211_chan_def *chandef);
53 };
54
55 /*
56 @@ -7608,6 +7619,20 @@ void cfg80211_cac_event(struct net_devic
57 const struct cfg80211_chan_def *chandef,
58 enum nl80211_radar_event event, gfp_t gfp);
59
60 +/**
61 + * cfg80211_offchan_cac_event - Channel Availability Check (CAC) offchan event
62 + * @wiphy: the wiphy
63 + * @chandef: chandef for the current channel
64 + * @event: type of event
65 + *
66 + * This function is called when a Channel Availability Check (CAC) is finished,
67 + * started or aborted by a offchannel dedicated chain.
68 + *
69 + * Note that this acquires the wiphy lock.
70 + */
71 +void cfg80211_offchan_cac_event(struct wiphy *wiphy,
72 + const struct cfg80211_chan_def *chandef,
73 + enum nl80211_radar_event event);
74
75 /**
76 * cfg80211_gtk_rekey_notify - notify userspace about driver rekeying
77 --- a/include/uapi/linux/nl80211.h
78 +++ b/include/uapi/linux/nl80211.h
79 @@ -2608,6 +2608,13 @@ enum nl80211_commands {
80 * Mandatory parameter for the transmitting interface to enable MBSSID.
81 * Optional for the non-transmitting interfaces.
82 *
83 + * @NL80211_ATTR_RADAR_OFFCHAN: Configure dedicated offchannel chain available for
84 + * radar/CAC detection on some hw. This chain can't be used to transmit
85 + * or receive frames and it is bounded to a running wdev.
86 + * Offchannel radar/CAC detection allows to avoid the CAC downtime
87 + * switching on a different channel during CAC detection on the selected
88 + * radar channel.
89 + *
90 * @NUM_NL80211_ATTR: total number of nl80211_attrs available
91 * @NL80211_ATTR_MAX: highest attribute number currently defined
92 * @__NL80211_ATTR_AFTER_LAST: internal use
93 @@ -3114,6 +3121,8 @@ enum nl80211_attrs {
94 NL80211_ATTR_MBSSID_CONFIG,
95 NL80211_ATTR_MBSSID_ELEMS,
96
97 + NL80211_ATTR_RADAR_OFFCHAN,
98 +
99 /* add attributes here, update the policy in nl80211.c */
100
101 __NL80211_ATTR_AFTER_LAST,
102 @@ -6013,6 +6022,9 @@ enum nl80211_feature_flags {
103 * @NL80211_EXT_FEATURE_BSS_COLOR: The driver supports BSS color collision
104 * detection and change announcemnts.
105 *
106 + * @NL80211_EXT_FEATURE_RADAR_OFFCHAN: Device supports offchannel radar/CAC
107 + * detection.
108 + *
109 * @NUM_NL80211_EXT_FEATURES: number of extended features.
110 * @MAX_NL80211_EXT_FEATURES: highest extended feature index.
111 */
112 @@ -6078,6 +6090,7 @@ enum nl80211_ext_feature_index {
113 NL80211_EXT_FEATURE_SECURE_RTT,
114 NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE,
115 NL80211_EXT_FEATURE_BSS_COLOR,
116 + NL80211_EXT_FEATURE_RADAR_OFFCHAN,
117
118 /* add new features before the definition below */
119 NUM_NL80211_EXT_FEATURES,
120 --- a/net/wireless/core.c
121 +++ b/net/wireless/core.c
122 @@ -543,6 +543,7 @@ use_default_name:
123 INIT_WORK(&rdev->rfkill_block, cfg80211_rfkill_block_work);
124 INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
125 INIT_WORK(&rdev->event_work, cfg80211_event_work);
126 + INIT_DELAYED_WORK(&rdev->offchan_cac_work, cfg80211_offchan_cac_work);
127
128 init_waitqueue_head(&rdev->dev_wait);
129
130 @@ -1205,6 +1206,8 @@ void __cfg80211_leave(struct cfg80211_re
131
132 cfg80211_pmsr_wdev_down(wdev);
133
134 + cfg80211_stop_offchan_radar_detection(wdev);
135 +
136 switch (wdev->iftype) {
137 case NL80211_IFTYPE_ADHOC:
138 __cfg80211_leave_ibss(rdev, dev, true);
139 --- a/net/wireless/core.h
140 +++ b/net/wireless/core.h
141 @@ -84,6 +84,10 @@ struct cfg80211_registered_device {
142
143 struct delayed_work dfs_update_channels_wk;
144
145 + struct wireless_dev *offchan_radar_wdev;
146 + struct cfg80211_chan_def offchan_radar_chandef;
147 + struct delayed_work offchan_cac_work;
148 +
149 /* netlink port which started critical protocol (0 means not started) */
150 u32 crit_proto_nlportid;
151
152 @@ -491,6 +495,15 @@ cfg80211_chandef_dfs_cac_time(struct wip
153
154 void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev);
155
156 +int
157 +cfg80211_start_offchan_radar_detection(struct cfg80211_registered_device *rdev,
158 + struct wireless_dev *wdev,
159 + struct cfg80211_chan_def *chandef);
160 +
161 +void cfg80211_stop_offchan_radar_detection(struct wireless_dev *wdev);
162 +
163 +void cfg80211_offchan_cac_work(struct work_struct *work);
164 +
165 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
166 struct ieee80211_channel *chan);
167
168 --- a/net/wireless/mlme.c
169 +++ b/net/wireless/mlme.c
170 @@ -970,3 +970,116 @@ void cfg80211_cac_event(struct net_devic
171 nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
172 }
173 EXPORT_SYMBOL(cfg80211_cac_event);
174 +
175 +void cfg80211_offchan_cac_work(struct work_struct *work)
176 +{
177 + struct delayed_work *delayed_work = to_delayed_work(work);
178 + struct cfg80211_registered_device *rdev;
179 +
180 + rdev = container_of(delayed_work, struct cfg80211_registered_device,
181 + offchan_cac_work);
182 + cfg80211_offchan_cac_event(&rdev->wiphy, &rdev->offchan_radar_chandef,
183 + NL80211_RADAR_CAC_FINISHED);
184 +}
185 +
186 +static void
187 +__cfg80211_offchan_cac_event(struct cfg80211_registered_device *rdev,
188 + struct wireless_dev *wdev,
189 + const struct cfg80211_chan_def *chandef,
190 + enum nl80211_radar_event event)
191 +{
192 + struct wiphy *wiphy = &rdev->wiphy;
193 + struct net_device *netdev;
194 +
195 + lockdep_assert_wiphy(&rdev->wiphy);
196 +
197 + if (event != NL80211_RADAR_CAC_STARTED && !rdev->offchan_radar_wdev)
198 + return;
199 +
200 + switch (event) {
201 + case NL80211_RADAR_CAC_FINISHED:
202 + cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
203 + memcpy(&rdev->cac_done_chandef, chandef, sizeof(*chandef));
204 + queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk);
205 + cfg80211_sched_dfs_chan_update(rdev);
206 + wdev = rdev->offchan_radar_wdev;
207 + rdev->offchan_radar_wdev = NULL;
208 + break;
209 + case NL80211_RADAR_CAC_ABORTED:
210 + cancel_delayed_work(&rdev->offchan_cac_work);
211 + wdev = rdev->offchan_radar_wdev;
212 + rdev->offchan_radar_wdev = NULL;
213 + break;
214 + case NL80211_RADAR_CAC_STARTED:
215 + WARN_ON(!wdev);
216 + rdev->offchan_radar_wdev = wdev;
217 + break;
218 + default:
219 + return;
220 + }
221 +
222 + netdev = wdev ? wdev->netdev : NULL;
223 + nl80211_radar_notify(rdev, chandef, event, netdev, GFP_KERNEL);
224 +}
225 +
226 +void cfg80211_offchan_cac_event(struct wiphy *wiphy,
227 + const struct cfg80211_chan_def *chandef,
228 + enum nl80211_radar_event event)
229 +{
230 + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
231 +
232 + wiphy_lock(wiphy);
233 + __cfg80211_offchan_cac_event(rdev, NULL, chandef, event);
234 + wiphy_unlock(wiphy);
235 +}
236 +EXPORT_SYMBOL(cfg80211_offchan_cac_event);
237 +
238 +int
239 +cfg80211_start_offchan_radar_detection(struct cfg80211_registered_device *rdev,
240 + struct wireless_dev *wdev,
241 + struct cfg80211_chan_def *chandef)
242 +{
243 + unsigned int cac_time_ms;
244 + int err;
245 +
246 + lockdep_assert_wiphy(&rdev->wiphy);
247 +
248 + if (!wiphy_ext_feature_isset(&rdev->wiphy,
249 + NL80211_EXT_FEATURE_RADAR_OFFCHAN))
250 + return -EOPNOTSUPP;
251 +
252 + if (rdev->offchan_radar_wdev)
253 + return -EBUSY;
254 +
255 + err = rdev_set_radar_offchan(rdev, chandef);
256 + if (err)
257 + return err;
258 +
259 + cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, chandef);
260 + if (!cac_time_ms)
261 + cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
262 +
263 + rdev->offchan_radar_chandef = *chandef;
264 + __cfg80211_offchan_cac_event(rdev, wdev, chandef,
265 + NL80211_RADAR_CAC_STARTED);
266 + queue_delayed_work(cfg80211_wq, &rdev->offchan_cac_work,
267 + msecs_to_jiffies(cac_time_ms));
268 +
269 + return 0;
270 +}
271 +
272 +void cfg80211_stop_offchan_radar_detection(struct wireless_dev *wdev)
273 +{
274 + struct wiphy *wiphy = wdev->wiphy;
275 + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
276 +
277 + lockdep_assert_wiphy(wiphy);
278 +
279 + if (wdev != rdev->offchan_radar_wdev)
280 + return;
281 +
282 + rdev_set_radar_offchan(rdev, NULL);
283 +
284 + __cfg80211_offchan_cac_event(rdev, NULL, NULL,
285 + NL80211_RADAR_CAC_ABORTED);
286 +}
287 --- a/net/wireless/nl80211.c
288 +++ b/net/wireless/nl80211.c
289 @@ -801,6 +801,7 @@ static const struct nla_policy nl80211_p
290 [NL80211_ATTR_MBSSID_CONFIG] =
291 NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
292 [NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
293 + [NL80211_ATTR_RADAR_OFFCHAN] = { .type = NLA_FLAG },
294 };
295
296 /* policy for the key attributes */
297 @@ -9287,12 +9288,6 @@ static int nl80211_start_radar_detection
298 if (err)
299 return err;
300
301 - if (netif_carrier_ok(dev))
302 - return -EBUSY;
303 -
304 - if (wdev->cac_started)
305 - return -EBUSY;
306 -
307 err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
308 if (err < 0)
309 return err;
310 @@ -9303,6 +9298,16 @@ static int nl80211_start_radar_detection
311 if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
312 return -EINVAL;
313
314 + if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_OFFCHAN]))
315 + return cfg80211_start_offchan_radar_detection(rdev, wdev,
316 + &chandef);
317 +
318 + if (netif_carrier_ok(dev))
319 + return -EBUSY;
320 +
321 + if (wdev->cac_started)
322 + return -EBUSY;
323 +
324 /* CAC start is offloaded to HW and can't be started manually */
325 if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
326 return -EOPNOTSUPP;
327 --- a/net/wireless/rdev-ops.h
328 +++ b/net/wireless/rdev-ops.h
329 @@ -1381,4 +1381,21 @@ static inline int rdev_color_change(stru
330 return ret;
331 }
332
333 +static inline int
334 +rdev_set_radar_offchan(struct cfg80211_registered_device *rdev,
335 + struct cfg80211_chan_def *chandef)
336 +{
337 + struct wiphy *wiphy = &rdev->wiphy;
338 + int ret;
339 +
340 + if (!rdev->ops->set_radar_offchan)
341 + return -EOPNOTSUPP;
342 +
343 + trace_rdev_set_radar_offchan(wiphy, chandef);
344 + ret = rdev->ops->set_radar_offchan(wiphy, chandef);
345 + trace_rdev_return_int(wiphy, ret);
346 +
347 + return ret;
348 +}
349 +
350 #endif /* __CFG80211_RDEV_OPS */
351 --- a/net/wireless/trace.h
352 +++ b/net/wireless/trace.h
353 @@ -3643,6 +3643,25 @@ TRACE_EVENT(cfg80211_bss_color_notify,
354 __entry->color_bitmap)
355 );
356
357 +TRACE_EVENT(rdev_set_radar_offchan,
358 + TP_PROTO(struct wiphy *wiphy, struct cfg80211_chan_def *chandef),
359 +
360 + TP_ARGS(wiphy, chandef),
361 +
362 + TP_STRUCT__entry(
363 + WIPHY_ENTRY
364 + CHAN_DEF_ENTRY
365 + ),
366 +
367 + TP_fast_assign(
368 + WIPHY_ASSIGN;
369 + CHAN_DEF_ASSIGN(chandef)
370 + ),
371 +
372 + TP_printk(WIPHY_PR_FMT ", " CHAN_DEF_PR_FMT,
373 + WIPHY_PR_ARG, CHAN_DEF_PR_ARG)
374 +);
375 +
376 #endif /* !__RDEV_OPS_TRACE || TRACE_HEADER_MULTI_READ */
377
378 #undef TRACE_INCLUDE_PATH