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
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.
10 Offchannel CAC (channel availability check) avoids the CAC
11 downtime when switching to a radar channel or when turning on
14 Drivers advertise support for this using the new feature flag
15 NL80211_EXT_FEATURE_RADAR_OFFCHAN.
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>
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.
34 * @color_change: Initiate a color change.
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
42 + * The caller is expected to set chandef pointer to NULL in order to
43 + * disable offchannel CAC/radar detection.
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);
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);
61 + * cfg80211_offchan_cac_event - Channel Availability Check (CAC) offchan event
63 + * @chandef: chandef for the current channel
64 + * @event: type of event
66 + * This function is called when a Channel Availability Check (CAC) is finished,
67 + * started or aborted by a offchannel dedicated chain.
69 + * Note that this acquires the wiphy lock.
71 +void cfg80211_offchan_cac_event(struct wiphy *wiphy,
72 + const struct cfg80211_chan_def *chandef,
73 + enum nl80211_radar_event event);
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.
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
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,
97 + NL80211_ATTR_RADAR_OFFCHAN,
99 /* add attributes here, update the policy in nl80211.c */
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.
106 + * @NL80211_EXT_FEATURE_RADAR_OFFCHAN: Device supports offchannel radar/CAC
109 * @NUM_NL80211_EXT_FEATURES: number of extended features.
110 * @MAX_NL80211_EXT_FEATURES: highest extended feature index.
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,
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);
128 init_waitqueue_head(&rdev->dev_wait);
130 @@ -1205,6 +1206,8 @@ void __cfg80211_leave(struct cfg80211_re
132 cfg80211_pmsr_wdev_down(wdev);
134 + cfg80211_stop_offchan_radar_detection(wdev);
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 {
143 struct delayed_work dfs_update_channels_wk;
145 + struct wireless_dev *offchan_radar_wdev;
146 + struct cfg80211_chan_def offchan_radar_chandef;
147 + struct delayed_work offchan_cac_work;
149 /* netlink port which started critical protocol (0 means not started) */
150 u32 crit_proto_nlportid;
152 @@ -491,6 +495,15 @@ cfg80211_chandef_dfs_cac_time(struct wip
154 void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev);
157 +cfg80211_start_offchan_radar_detection(struct cfg80211_registered_device *rdev,
158 + struct wireless_dev *wdev,
159 + struct cfg80211_chan_def *chandef);
161 +void cfg80211_stop_offchan_radar_detection(struct wireless_dev *wdev);
163 +void cfg80211_offchan_cac_work(struct work_struct *work);
165 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
166 struct ieee80211_channel *chan);
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);
173 EXPORT_SYMBOL(cfg80211_cac_event);
175 +void cfg80211_offchan_cac_work(struct work_struct *work)
177 + struct delayed_work *delayed_work = to_delayed_work(work);
178 + struct cfg80211_registered_device *rdev;
180 + rdev = container_of(delayed_work, struct cfg80211_registered_device,
182 + cfg80211_offchan_cac_event(&rdev->wiphy, &rdev->offchan_radar_chandef,
183 + NL80211_RADAR_CAC_FINISHED);
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)
192 + struct wiphy *wiphy = &rdev->wiphy;
193 + struct net_device *netdev;
195 + lockdep_assert_wiphy(&rdev->wiphy);
197 + if (event != NL80211_RADAR_CAC_STARTED && !rdev->offchan_radar_wdev)
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;
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;
214 + case NL80211_RADAR_CAC_STARTED:
216 + rdev->offchan_radar_wdev = wdev;
222 + netdev = wdev ? wdev->netdev : NULL;
223 + nl80211_radar_notify(rdev, chandef, event, netdev, GFP_KERNEL);
226 +void cfg80211_offchan_cac_event(struct wiphy *wiphy,
227 + const struct cfg80211_chan_def *chandef,
228 + enum nl80211_radar_event event)
230 + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
233 + __cfg80211_offchan_cac_event(rdev, NULL, chandef, event);
234 + wiphy_unlock(wiphy);
236 +EXPORT_SYMBOL(cfg80211_offchan_cac_event);
239 +cfg80211_start_offchan_radar_detection(struct cfg80211_registered_device *rdev,
240 + struct wireless_dev *wdev,
241 + struct cfg80211_chan_def *chandef)
243 + unsigned int cac_time_ms;
246 + lockdep_assert_wiphy(&rdev->wiphy);
248 + if (!wiphy_ext_feature_isset(&rdev->wiphy,
249 + NL80211_EXT_FEATURE_RADAR_OFFCHAN))
250 + return -EOPNOTSUPP;
252 + if (rdev->offchan_radar_wdev)
255 + err = rdev_set_radar_offchan(rdev, chandef);
259 + cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, chandef);
261 + cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
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));
272 +void cfg80211_stop_offchan_radar_detection(struct wireless_dev *wdev)
274 + struct wiphy *wiphy = wdev->wiphy;
275 + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
277 + lockdep_assert_wiphy(wiphy);
279 + if (wdev != rdev->offchan_radar_wdev)
282 + rdev_set_radar_offchan(rdev, NULL);
284 + __cfg80211_offchan_cac_event(rdev, NULL, NULL,
285 + NL80211_RADAR_CAC_ABORTED);
287 --- a/net/wireless/nl80211.c
288 +++ b/net/wireless/nl80211.c
289 @@ -796,6 +796,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 },
296 /* policy for the key attributes */
297 @@ -9272,12 +9273,6 @@ static int nl80211_start_radar_detection
301 - if (netif_carrier_ok(dev))
304 - if (wdev->cac_started)
307 err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
310 @@ -9288,6 +9283,16 @@ static int nl80211_start_radar_detection
311 if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
314 + if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_OFFCHAN]))
315 + return cfg80211_start_offchan_radar_detection(rdev, wdev,
318 + if (netif_carrier_ok(dev))
321 + if (wdev->cac_started)
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))
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
334 +rdev_set_radar_offchan(struct cfg80211_registered_device *rdev,
335 + struct cfg80211_chan_def *chandef)
337 + struct wiphy *wiphy = &rdev->wiphy;
340 + if (!rdev->ops->set_radar_offchan)
341 + return -EOPNOTSUPP;
343 + trace_rdev_set_radar_offchan(wiphy, chandef);
344 + ret = rdev->ops->set_radar_offchan(wiphy, chandef);
345 + trace_rdev_return_int(wiphy, ret);
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)
357 +TRACE_EVENT(rdev_set_radar_offchan,
358 + TP_PROTO(struct wiphy *wiphy, struct cfg80211_chan_def *chandef),
360 + TP_ARGS(wiphy, chandef),
369 + CHAN_DEF_ASSIGN(chandef)
372 + TP_printk(WIPHY_PR_FMT ", " CHAN_DEF_PR_FMT,
373 + WIPHY_PR_ARG, CHAN_DEF_PR_ARG)
376 #endif /* !__RDEV_OPS_TRACE || TRACE_HEADER_MULTI_READ */
378 #undef TRACE_INCLUDE_PATH