mac80211: Use a cfg80211_chan_def in ieee80211_hw_conf_chan
[linux-block.git] / net / mac80211 / chan.c
CommitLineData
f444de05
JB
1/*
2 * mac80211 - channel management
3 */
4
0aaffa9b 5#include <linux/nl80211.h>
3448c005 6#include <linux/export.h>
4d76d21b 7#include <linux/rtnetlink.h>
3117bbdb 8#include <net/cfg80211.h>
f444de05 9#include "ieee80211_i.h"
35f2fce9 10#include "driver-ops.h"
f444de05 11
18942d3b 12static void ieee80211_change_chanctx(struct ieee80211_local *local,
4bf88530
JB
13 struct ieee80211_chanctx *ctx,
14 const struct cfg80211_chan_def *chandef)
23a85b45 15{
4bf88530 16 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
e89a96f5 17 return;
0aaffa9b 18
4bf88530
JB
19 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
20
21 ctx->conf.def = *chandef;
22 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
55de908a
JB
23
24 if (!local->use_chanctx) {
675a0b04 25 local->_oper_chandef = *chandef;
55de908a
JB
26 ieee80211_hw_config(local, 0);
27 }
0aaffa9b 28}
d01a1e65
MK
29
30static struct ieee80211_chanctx *
31ieee80211_find_chanctx(struct ieee80211_local *local,
4bf88530 32 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
33 enum ieee80211_chanctx_mode mode)
34{
35 struct ieee80211_chanctx *ctx;
36
37 lockdep_assert_held(&local->chanctx_mtx);
38
39 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
40 return NULL;
d01a1e65
MK
41
42 list_for_each_entry(ctx, &local->chanctx_list, list) {
4bf88530 43 const struct cfg80211_chan_def *compat;
e89a96f5 44
d01a1e65
MK
45 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
46 continue;
4bf88530
JB
47
48 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
49 if (!compat)
d01a1e65
MK
50 continue;
51
18942d3b 52 ieee80211_change_chanctx(local, ctx, compat);
e89a96f5 53
d01a1e65
MK
54 return ctx;
55 }
56
57 return NULL;
58}
59
60static struct ieee80211_chanctx *
61ieee80211_new_chanctx(struct ieee80211_local *local,
4bf88530 62 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
63 enum ieee80211_chanctx_mode mode)
64{
65 struct ieee80211_chanctx *ctx;
35f2fce9 66 int err;
d01a1e65
MK
67
68 lockdep_assert_held(&local->chanctx_mtx);
69
70 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
71 if (!ctx)
72 return ERR_PTR(-ENOMEM);
73
4bf88530 74 ctx->conf.def = *chandef;
04ecd257
JB
75 ctx->conf.rx_chains_static = 1;
76 ctx->conf.rx_chains_dynamic = 1;
d01a1e65
MK
77 ctx->mode = mode;
78
55de908a 79 if (!local->use_chanctx) {
675a0b04 80 local->_oper_chandef = *chandef;
55de908a
JB
81 ieee80211_hw_config(local, 0);
82 } else {
83 err = drv_add_chanctx(local, ctx);
84 if (err) {
85 kfree(ctx);
86 return ERR_PTR(err);
87 }
35f2fce9
MK
88 }
89
3448c005 90 list_add_rcu(&ctx->list, &local->chanctx_list);
d01a1e65 91
fd0f979a
JB
92 mutex_lock(&local->mtx);
93 ieee80211_recalc_idle(local);
94 mutex_unlock(&local->mtx);
95
d01a1e65
MK
96 return ctx;
97}
98
99static void ieee80211_free_chanctx(struct ieee80211_local *local,
100 struct ieee80211_chanctx *ctx)
101{
102 lockdep_assert_held(&local->chanctx_mtx);
103
104 WARN_ON_ONCE(ctx->refcount != 0);
105
55de908a 106 if (!local->use_chanctx) {
675a0b04
KB
107 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
108 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
109 chandef->center_freq1 = chandef->chan->center_freq;
110 chandef->center_freq2 = 0;
55de908a
JB
111 ieee80211_hw_config(local, 0);
112 } else {
113 drv_remove_chanctx(local, ctx);
114 }
35f2fce9 115
3448c005 116 list_del_rcu(&ctx->list);
d01a1e65 117 kfree_rcu(ctx, rcu_head);
fd0f979a
JB
118
119 mutex_lock(&local->mtx);
120 ieee80211_recalc_idle(local);
121 mutex_unlock(&local->mtx);
d01a1e65
MK
122}
123
124static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
125 struct ieee80211_chanctx *ctx)
126{
35f2fce9
MK
127 struct ieee80211_local *local = sdata->local;
128 int ret;
d01a1e65
MK
129
130 lockdep_assert_held(&local->chanctx_mtx);
131
35f2fce9
MK
132 ret = drv_assign_vif_chanctx(local, sdata, ctx);
133 if (ret)
134 return ret;
135
d01a1e65
MK
136 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
137 ctx->refcount++;
138
1ea6f9c0 139 ieee80211_recalc_txpower(sdata);
fd0f979a 140 sdata->vif.bss_conf.idle = false;
5bbe754d
JB
141
142 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
143 sdata->vif.type != NL80211_IFTYPE_MONITOR)
144 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
1ea6f9c0 145
d01a1e65
MK
146 return 0;
147}
148
4bf88530
JB
149static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
150 struct ieee80211_chanctx *ctx)
e89a96f5
MK
151{
152 struct ieee80211_chanctx_conf *conf = &ctx->conf;
153 struct ieee80211_sub_if_data *sdata;
4bf88530 154 const struct cfg80211_chan_def *compat = NULL;
e89a96f5
MK
155
156 lockdep_assert_held(&local->chanctx_mtx);
157
158 rcu_read_lock();
159 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4bf88530 160
e89a96f5
MK
161 if (!ieee80211_sdata_running(sdata))
162 continue;
163 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
164 continue;
165
4bf88530
JB
166 if (!compat)
167 compat = &sdata->vif.bss_conf.chandef;
168
169 compat = cfg80211_chandef_compatible(
170 &sdata->vif.bss_conf.chandef, compat);
171 if (!compat)
172 break;
e89a96f5
MK
173 }
174 rcu_read_unlock();
175
4bf88530
JB
176 if (WARN_ON_ONCE(!compat))
177 return;
e89a96f5 178
18942d3b 179 ieee80211_change_chanctx(local, ctx, compat);
e89a96f5
MK
180}
181
d01a1e65
MK
182static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
183 struct ieee80211_chanctx *ctx)
184{
35f2fce9 185 struct ieee80211_local *local = sdata->local;
d01a1e65
MK
186
187 lockdep_assert_held(&local->chanctx_mtx);
188
189 ctx->refcount--;
190 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
35f2fce9 191
fd0f979a 192 sdata->vif.bss_conf.idle = true;
5bbe754d
JB
193
194 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
195 sdata->vif.type != NL80211_IFTYPE_MONITOR)
196 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
fd0f979a 197
35f2fce9 198 drv_unassign_vif_chanctx(local, sdata, ctx);
e89a96f5 199
04ecd257 200 if (ctx->refcount > 0) {
e89a96f5 201 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
04ecd257 202 ieee80211_recalc_smps_chanctx(local, ctx);
164eb02d 203 ieee80211_recalc_radar_chanctx(local, ctx);
04ecd257 204 }
d01a1e65
MK
205}
206
207static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
208{
209 struct ieee80211_local *local = sdata->local;
210 struct ieee80211_chanctx_conf *conf;
211 struct ieee80211_chanctx *ctx;
212
213 lockdep_assert_held(&local->chanctx_mtx);
214
215 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
216 lockdep_is_held(&local->chanctx_mtx));
217 if (!conf)
218 return;
219
220 ctx = container_of(conf, struct ieee80211_chanctx, conf);
221
222 ieee80211_unassign_vif_chanctx(sdata, ctx);
223 if (ctx->refcount == 0)
224 ieee80211_free_chanctx(local, ctx);
225}
226
164eb02d
SW
227void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
228 struct ieee80211_chanctx *chanctx)
229{
230 struct ieee80211_sub_if_data *sdata;
231 bool radar_enabled = false;
232
233 lockdep_assert_held(&local->chanctx_mtx);
234
235 rcu_read_lock();
236 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
237 if (sdata->radar_required) {
238 radar_enabled = true;
239 break;
240 }
241 }
242 rcu_read_unlock();
243
244 if (radar_enabled == chanctx->conf.radar_enabled)
245 return;
246
247 chanctx->conf.radar_enabled = radar_enabled;
248 local->radar_detect_enabled = chanctx->conf.radar_enabled;
249
250 if (!local->use_chanctx) {
251 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
252 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
253 }
254
255 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
256}
257
04ecd257
JB
258void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
259 struct ieee80211_chanctx *chanctx)
260{
261 struct ieee80211_sub_if_data *sdata;
262 u8 rx_chains_static, rx_chains_dynamic;
263
264 lockdep_assert_held(&local->chanctx_mtx);
265
266 rx_chains_static = 1;
267 rx_chains_dynamic = 1;
268
269 rcu_read_lock();
270 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
271 u8 needed_static, needed_dynamic;
272
273 if (!ieee80211_sdata_running(sdata))
274 continue;
275
276 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
277 &chanctx->conf)
278 continue;
279
280 switch (sdata->vif.type) {
281 case NL80211_IFTYPE_P2P_DEVICE:
282 continue;
283 case NL80211_IFTYPE_STATION:
284 if (!sdata->u.mgd.associated)
285 continue;
286 break;
287 case NL80211_IFTYPE_AP_VLAN:
288 continue;
289 case NL80211_IFTYPE_AP:
290 case NL80211_IFTYPE_ADHOC:
291 case NL80211_IFTYPE_WDS:
292 case NL80211_IFTYPE_MESH_POINT:
293 break;
294 default:
295 WARN_ON_ONCE(1);
296 }
297
298 switch (sdata->smps_mode) {
299 default:
300 WARN_ONCE(1, "Invalid SMPS mode %d\n",
301 sdata->smps_mode);
302 /* fall through */
303 case IEEE80211_SMPS_OFF:
304 needed_static = sdata->needed_rx_chains;
305 needed_dynamic = sdata->needed_rx_chains;
306 break;
307 case IEEE80211_SMPS_DYNAMIC:
308 needed_static = 1;
309 needed_dynamic = sdata->needed_rx_chains;
310 break;
311 case IEEE80211_SMPS_STATIC:
312 needed_static = 1;
313 needed_dynamic = 1;
314 break;
315 }
316
317 rx_chains_static = max(rx_chains_static, needed_static);
318 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
319 }
320 rcu_read_unlock();
321
322 if (!local->use_chanctx) {
323 if (rx_chains_static > 1)
324 local->smps_mode = IEEE80211_SMPS_OFF;
325 else if (rx_chains_dynamic > 1)
326 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
327 else
328 local->smps_mode = IEEE80211_SMPS_STATIC;
329 ieee80211_hw_config(local, 0);
330 }
331
332 if (rx_chains_static == chanctx->conf.rx_chains_static &&
333 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
334 return;
335
336 chanctx->conf.rx_chains_static = rx_chains_static;
337 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
338 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
339}
340
d01a1e65 341int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
4bf88530 342 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
343 enum ieee80211_chanctx_mode mode)
344{
345 struct ieee80211_local *local = sdata->local;
346 struct ieee80211_chanctx *ctx;
347 int ret;
348
55de908a
JB
349 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
350
d01a1e65
MK
351 mutex_lock(&local->chanctx_mtx);
352 __ieee80211_vif_release_channel(sdata);
353
4bf88530 354 ctx = ieee80211_find_chanctx(local, chandef, mode);
d01a1e65 355 if (!ctx)
4bf88530 356 ctx = ieee80211_new_chanctx(local, chandef, mode);
d01a1e65
MK
357 if (IS_ERR(ctx)) {
358 ret = PTR_ERR(ctx);
359 goto out;
360 }
361
4bf88530 362 sdata->vif.bss_conf.chandef = *chandef;
55de908a 363
d01a1e65
MK
364 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
365 if (ret) {
366 /* if assign fails refcount stays the same */
367 if (ctx->refcount == 0)
368 ieee80211_free_chanctx(local, ctx);
369 goto out;
370 }
371
04ecd257 372 ieee80211_recalc_smps_chanctx(local, ctx);
164eb02d 373 ieee80211_recalc_radar_chanctx(local, ctx);
d01a1e65
MK
374 out:
375 mutex_unlock(&local->chanctx_mtx);
376 return ret;
377}
378
2c9b7359
JB
379int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
380 const struct cfg80211_chan_def *chandef,
381 u32 *changed)
382{
383 struct ieee80211_local *local = sdata->local;
384 struct ieee80211_chanctx_conf *conf;
385 struct ieee80211_chanctx *ctx;
386 int ret;
387
388 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
389 IEEE80211_CHAN_DISABLED))
390 return -EINVAL;
391
392 mutex_lock(&local->chanctx_mtx);
393 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
394 ret = 0;
395 goto out;
396 }
397
398 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
399 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
400 ret = -EINVAL;
401 goto out;
402 }
403
404 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
405 lockdep_is_held(&local->chanctx_mtx));
406 if (!conf) {
407 ret = -EINVAL;
408 goto out;
409 }
410
411 ctx = container_of(conf, struct ieee80211_chanctx, conf);
412 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
413 ret = -EINVAL;
414 goto out;
415 }
416
417 sdata->vif.bss_conf.chandef = *chandef;
418
419 ieee80211_recalc_chanctx_chantype(local, ctx);
420
421 *changed |= BSS_CHANGED_BANDWIDTH;
422 ret = 0;
423 out:
424 mutex_unlock(&local->chanctx_mtx);
425 return ret;
426}
427
d01a1e65
MK
428void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
429{
55de908a
JB
430 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
431
d01a1e65
MK
432 mutex_lock(&sdata->local->chanctx_mtx);
433 __ieee80211_vif_release_channel(sdata);
434 mutex_unlock(&sdata->local->chanctx_mtx);
435}
3448c005 436
4d76d21b
JB
437void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
438{
439 struct ieee80211_local *local = sdata->local;
440 struct ieee80211_sub_if_data *ap;
441 struct ieee80211_chanctx_conf *conf;
442
443 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
444 return;
445
446 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
447
448 mutex_lock(&local->chanctx_mtx);
449
450 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
451 lockdep_is_held(&local->chanctx_mtx));
452 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
453 mutex_unlock(&local->chanctx_mtx);
454}
455
1f4ac5a6
JB
456void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
457 bool clear)
458{
459 struct ieee80211_local *local = sdata->local;
460 struct ieee80211_sub_if_data *vlan;
461 struct ieee80211_chanctx_conf *conf;
462
463 ASSERT_RTNL();
464
465 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
466 return;
467
468 mutex_lock(&local->chanctx_mtx);
469
470 /*
471 * Check that conf exists, even when clearing this function
472 * must be called with the AP's channel context still there
473 * as it would otherwise cause VLANs to have an invalid
474 * channel context pointer for a while, possibly pointing
475 * to a channel context that has already been freed.
476 */
477 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
478 lockdep_is_held(&local->chanctx_mtx));
479 WARN_ON(!conf);
480
481 if (clear)
482 conf = NULL;
483
484 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
485 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
486
487 mutex_unlock(&local->chanctx_mtx);
488}
489
3448c005
JB
490void ieee80211_iter_chan_contexts_atomic(
491 struct ieee80211_hw *hw,
492 void (*iter)(struct ieee80211_hw *hw,
493 struct ieee80211_chanctx_conf *chanctx_conf,
494 void *data),
495 void *iter_data)
496{
497 struct ieee80211_local *local = hw_to_local(hw);
498 struct ieee80211_chanctx *ctx;
499
500 rcu_read_lock();
501 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
8a61af65
JB
502 if (ctx->driver_present)
503 iter(hw, &ctx->conf, iter_data);
3448c005
JB
504 rcu_read_unlock();
505}
506EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);