]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/mac80211/chan.c
Merge branch 'stable-4.10' of git://git.infradead.org/users/pcmoore/audit
[mirror_ubuntu-artful-kernel.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
c0166da9
MK
12static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
13 struct ieee80211_chanctx *ctx)
14{
15 struct ieee80211_sub_if_data *sdata;
16 int num = 0;
17
18 lockdep_assert_held(&local->chanctx_mtx);
19
20 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
21 num++;
22
23 return num;
24}
25
26static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
27 struct ieee80211_chanctx *ctx)
28{
29 struct ieee80211_sub_if_data *sdata;
30 int num = 0;
31
32 lockdep_assert_held(&local->chanctx_mtx);
33
34 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
35 num++;
36
37 return num;
38}
39
40int ieee80211_chanctx_refcount(struct ieee80211_local *local,
41 struct ieee80211_chanctx *ctx)
42{
43 return ieee80211_chanctx_num_assigned(local, ctx) +
44 ieee80211_chanctx_num_reserved(local, ctx);
45}
46
c2b90ad8
MK
47static int ieee80211_num_chanctx(struct ieee80211_local *local)
48{
49 struct ieee80211_chanctx *ctx;
50 int num = 0;
51
52 lockdep_assert_held(&local->chanctx_mtx);
53
54 list_for_each_entry(ctx, &local->chanctx_list, list)
55 num++;
56
57 return num;
58}
59
60static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
61{
62 lockdep_assert_held(&local->chanctx_mtx);
63 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
64}
65
5bcae31d
MK
66static struct ieee80211_chanctx *
67ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
68{
1d4cc30c 69 struct ieee80211_local *local __maybe_unused = sdata->local;
5bcae31d
MK
70 struct ieee80211_chanctx_conf *conf;
71
72 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
73 lockdep_is_held(&local->chanctx_mtx));
74 if (!conf)
75 return NULL;
76
77 return container_of(conf, struct ieee80211_chanctx, conf);
78}
79
0288157b
MK
80static const struct cfg80211_chan_def *
81ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
82 struct ieee80211_chanctx *ctx,
83 const struct cfg80211_chan_def *compat)
84{
85 struct ieee80211_sub_if_data *sdata;
86
87 lockdep_assert_held(&local->chanctx_mtx);
88
89 list_for_each_entry(sdata, &ctx->reserved_vifs,
90 reserved_chanctx_list) {
91 if (!compat)
92 compat = &sdata->reserved_chandef;
93
94 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
95 compat);
96 if (!compat)
97 break;
98 }
99
100 return compat;
101}
102
13f348a8
MK
103static const struct cfg80211_chan_def *
104ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
105 struct ieee80211_chanctx *ctx,
106 const struct cfg80211_chan_def *compat)
107{
108 struct ieee80211_sub_if_data *sdata;
109
110 lockdep_assert_held(&local->chanctx_mtx);
111
112 list_for_each_entry(sdata, &ctx->assigned_vifs,
113 assigned_chanctx_list) {
114 if (sdata->reserved_chanctx != NULL)
115 continue;
116
117 if (!compat)
118 compat = &sdata->vif.bss_conf.chandef;
119
120 compat = cfg80211_chandef_compatible(
121 &sdata->vif.bss_conf.chandef, compat);
122 if (!compat)
123 break;
124 }
125
126 return compat;
127}
128
129static const struct cfg80211_chan_def *
130ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
131 struct ieee80211_chanctx *ctx,
132 const struct cfg80211_chan_def *compat)
133{
134 lockdep_assert_held(&local->chanctx_mtx);
135
136 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
137 if (!compat)
138 return NULL;
139
140 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
141 if (!compat)
142 return NULL;
143
144 return compat;
145}
146
147static bool
148ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
149 struct ieee80211_chanctx *ctx,
150 const struct cfg80211_chan_def *def)
151{
152 lockdep_assert_held(&local->chanctx_mtx);
153
154 if (ieee80211_chanctx_combined_chandef(local, ctx, def))
155 return true;
156
157 if (!list_empty(&ctx->reserved_vifs) &&
158 ieee80211_chanctx_reserved_chandef(local, ctx, def))
159 return true;
160
161 return false;
162}
163
164static struct ieee80211_chanctx *
165ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
166 const struct cfg80211_chan_def *chandef,
167 enum ieee80211_chanctx_mode mode)
168{
169 struct ieee80211_chanctx *ctx;
170
171 lockdep_assert_held(&local->chanctx_mtx);
172
173 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
174 return NULL;
175
176 list_for_each_entry(ctx, &local->chanctx_list, list) {
5bcae31d
MK
177 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
178 continue;
179
13f348a8
MK
180 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
181 continue;
182
183 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
184 chandef))
185 continue;
186
187 return ctx;
188 }
189
190 return NULL;
191}
192
0fabfaaf 193enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
21f659bf
EP
194{
195 switch (sta->bandwidth) {
196 case IEEE80211_STA_RX_BW_20:
197 if (sta->ht_cap.ht_supported)
198 return NL80211_CHAN_WIDTH_20;
199 else
200 return NL80211_CHAN_WIDTH_20_NOHT;
201 case IEEE80211_STA_RX_BW_40:
202 return NL80211_CHAN_WIDTH_40;
203 case IEEE80211_STA_RX_BW_80:
204 return NL80211_CHAN_WIDTH_80;
205 case IEEE80211_STA_RX_BW_160:
206 /*
207 * This applied for both 160 and 80+80. since we use
208 * the returned value to consider degradation of
209 * ctx->conf.min_def, we have to make sure to take
210 * the bigger one (NL80211_CHAN_WIDTH_160).
211 * Otherwise we might try degrading even when not
212 * needed, as the max required sta_bw returned (80+80)
213 * might be smaller than the configured bw (160).
214 */
215 return NL80211_CHAN_WIDTH_160;
216 default:
217 WARN_ON(1);
218 return NL80211_CHAN_WIDTH_20;
219 }
220}
221
222static enum nl80211_chan_width
223ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
224{
225 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
226 struct sta_info *sta;
227
228 rcu_read_lock();
229 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
230 if (sdata != sta->sdata &&
231 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
232 continue;
233
a7201a6c 234 if (!sta->uploaded || !test_sta_flag(sta, WLAN_STA_ASSOC))
21f659bf
EP
235 continue;
236
237 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
238 }
239 rcu_read_unlock();
240
241 return max_bw;
242}
243
244static enum nl80211_chan_width
245ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
246 struct ieee80211_chanctx_conf *conf)
247{
248 struct ieee80211_sub_if_data *sdata;
249 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
250
251 rcu_read_lock();
252 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
253 struct ieee80211_vif *vif = &sdata->vif;
254 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
255
256 if (!ieee80211_sdata_running(sdata))
257 continue;
258
259 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
260 continue;
261
262 switch (vif->type) {
263 case NL80211_IFTYPE_AP:
264 case NL80211_IFTYPE_AP_VLAN:
265 width = ieee80211_get_max_required_bw(sdata);
266 break;
0fabfaaf
AN
267 case NL80211_IFTYPE_STATION:
268 /*
269 * The ap's sta->bandwidth is not set yet at this
270 * point, so take the width from the chandef, but
271 * account also for TDLS peers
272 */
273 width = max(vif->bss_conf.chandef.width,
274 ieee80211_get_max_required_bw(sdata));
275 break;
21f659bf 276 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 277 case NL80211_IFTYPE_NAN:
21f659bf 278 continue;
21f659bf
EP
279 case NL80211_IFTYPE_ADHOC:
280 case NL80211_IFTYPE_WDS:
281 case NL80211_IFTYPE_MESH_POINT:
6e0bd6c3 282 case NL80211_IFTYPE_OCB:
21f659bf
EP
283 width = vif->bss_conf.chandef.width;
284 break;
285 case NL80211_IFTYPE_UNSPECIFIED:
286 case NUM_NL80211_IFTYPES:
287 case NL80211_IFTYPE_MONITOR:
288 case NL80211_IFTYPE_P2P_CLIENT:
289 case NL80211_IFTYPE_P2P_GO:
290 WARN_ON_ONCE(1);
291 }
292 max_bw = max(max_bw, width);
293 }
1c37a72c
EP
294
295 /* use the configured bandwidth in case of monitor interface */
296 sdata = rcu_dereference(local->monitor_sdata);
297 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
298 max_bw = max(max_bw, conf->def.width);
299
21f659bf
EP
300 rcu_read_unlock();
301
302 return max_bw;
303}
304
305/*
306 * recalc the min required chan width of the channel context, which is
307 * the max of min required widths of all the interfaces bound to this
308 * channel context.
309 */
310void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
311 struct ieee80211_chanctx *ctx)
312{
313 enum nl80211_chan_width max_bw;
314 struct cfg80211_chan_def min_def;
315
316 lockdep_assert_held(&local->chanctx_mtx);
317
318 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
319 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
320 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
321 ctx->conf.radar_enabled) {
322 ctx->conf.min_def = ctx->conf.def;
323 return;
324 }
325
326 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
327
328 /* downgrade chandef up to max_bw */
329 min_def = ctx->conf.def;
330 while (min_def.width > max_bw)
331 ieee80211_chandef_downgrade(&min_def);
332
333 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
334 return;
335
336 ctx->conf.min_def = min_def;
337 if (!ctx->driver_present)
338 return;
339
340 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
341}
342
18942d3b 343static void ieee80211_change_chanctx(struct ieee80211_local *local,
4bf88530
JB
344 struct ieee80211_chanctx *ctx,
345 const struct cfg80211_chan_def *chandef)
23a85b45 346{
aa507a7b
AN
347 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
348 ieee80211_recalc_chanctx_min_def(local, ctx);
e89a96f5 349 return;
aa507a7b 350 }
0aaffa9b 351
4bf88530
JB
352 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
353
354 ctx->conf.def = *chandef;
355 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
21f659bf 356 ieee80211_recalc_chanctx_min_def(local, ctx);
55de908a
JB
357
358 if (!local->use_chanctx) {
675a0b04 359 local->_oper_chandef = *chandef;
55de908a
JB
360 ieee80211_hw_config(local, 0);
361 }
0aaffa9b 362}
d01a1e65
MK
363
364static struct ieee80211_chanctx *
365ieee80211_find_chanctx(struct ieee80211_local *local,
4bf88530 366 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
367 enum ieee80211_chanctx_mode mode)
368{
369 struct ieee80211_chanctx *ctx;
370
371 lockdep_assert_held(&local->chanctx_mtx);
372
373 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
374 return NULL;
d01a1e65
MK
375
376 list_for_each_entry(ctx, &local->chanctx_list, list) {
4bf88530 377 const struct cfg80211_chan_def *compat;
e89a96f5 378
5bcae31d
MK
379 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
380 continue;
381
d01a1e65
MK
382 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
383 continue;
4bf88530
JB
384
385 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
386 if (!compat)
d01a1e65
MK
387 continue;
388
0288157b
MK
389 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
390 compat);
391 if (!compat)
392 continue;
393
18942d3b 394 ieee80211_change_chanctx(local, ctx, compat);
e89a96f5 395
d01a1e65
MK
396 return ctx;
397 }
398
399 return NULL;
400}
401
5cbc95a7 402bool ieee80211_is_radar_required(struct ieee80211_local *local)
e4746851
SW
403{
404 struct ieee80211_sub_if_data *sdata;
405
cc901de1
MK
406 lockdep_assert_held(&local->mtx);
407
e4746851
SW
408 rcu_read_lock();
409 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
410 if (sdata->radar_required) {
411 rcu_read_unlock();
412 return true;
413 }
414 }
415 rcu_read_unlock();
416
417 return false;
418}
419
e7f2337a
EP
420static bool
421ieee80211_chanctx_radar_required(struct ieee80211_local *local,
422 struct ieee80211_chanctx *ctx)
423{
424 struct ieee80211_chanctx_conf *conf = &ctx->conf;
425 struct ieee80211_sub_if_data *sdata;
426 bool required = false;
427
428 lockdep_assert_held(&local->chanctx_mtx);
429 lockdep_assert_held(&local->mtx);
430
431 rcu_read_lock();
432 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
433 if (!ieee80211_sdata_running(sdata))
434 continue;
435 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
436 continue;
437 if (!sdata->radar_required)
438 continue;
439
440 required = true;
441 break;
442 }
443 rcu_read_unlock();
444
445 return required;
446}
447
d01a1e65 448static struct ieee80211_chanctx *
ed68ebca
MK
449ieee80211_alloc_chanctx(struct ieee80211_local *local,
450 const struct cfg80211_chan_def *chandef,
451 enum ieee80211_chanctx_mode mode)
d01a1e65
MK
452{
453 struct ieee80211_chanctx *ctx;
454
455 lockdep_assert_held(&local->chanctx_mtx);
456
457 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
458 if (!ctx)
ed68ebca 459 return NULL;
d01a1e65 460
484298ad 461 INIT_LIST_HEAD(&ctx->assigned_vifs);
e3afb920 462 INIT_LIST_HEAD(&ctx->reserved_vifs);
4bf88530 463 ctx->conf.def = *chandef;
04ecd257
JB
464 ctx->conf.rx_chains_static = 1;
465 ctx->conf.rx_chains_dynamic = 1;
d01a1e65 466 ctx->mode = mode;
e7f2337a 467 ctx->conf.radar_enabled = false;
21f659bf 468 ieee80211_recalc_chanctx_min_def(local, ctx);
ed68ebca
MK
469
470 return ctx;
471}
472
473static int ieee80211_add_chanctx(struct ieee80211_local *local,
474 struct ieee80211_chanctx *ctx)
475{
476 u32 changed;
477 int err;
478
479 lockdep_assert_held(&local->mtx);
480 lockdep_assert_held(&local->chanctx_mtx);
481
e4746851
SW
482 if (!local->use_chanctx)
483 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
d01a1e65 484
382a103b
JB
485 /* turn idle off *before* setting channel -- some drivers need that */
486 changed = ieee80211_idle_off(local);
487 if (changed)
488 ieee80211_hw_config(local, changed);
489
55de908a 490 if (!local->use_chanctx) {
ed68ebca 491 local->_oper_chandef = ctx->conf.def;
9b4816f5 492 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
55de908a
JB
493 } else {
494 err = drv_add_chanctx(local, ctx);
495 if (err) {
382a103b 496 ieee80211_recalc_idle(local);
ed68ebca 497 return err;
55de908a 498 }
35f2fce9
MK
499 }
500
ed68ebca
MK
501 return 0;
502}
503
504static struct ieee80211_chanctx *
505ieee80211_new_chanctx(struct ieee80211_local *local,
506 const struct cfg80211_chan_def *chandef,
507 enum ieee80211_chanctx_mode mode)
508{
509 struct ieee80211_chanctx *ctx;
510 int err;
d01a1e65 511
ed68ebca
MK
512 lockdep_assert_held(&local->mtx);
513 lockdep_assert_held(&local->chanctx_mtx);
d01a1e65 514
ed68ebca
MK
515 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
516 if (!ctx)
517 return ERR_PTR(-ENOMEM);
518
519 err = ieee80211_add_chanctx(local, ctx);
520 if (err) {
521 kfree(ctx);
522 return ERR_PTR(err);
523 }
524
525 list_add_rcu(&ctx->list, &local->chanctx_list);
d01a1e65
MK
526 return ctx;
527}
528
1f0d54cd
MK
529static void ieee80211_del_chanctx(struct ieee80211_local *local,
530 struct ieee80211_chanctx *ctx)
d01a1e65
MK
531{
532 lockdep_assert_held(&local->chanctx_mtx);
533
55de908a 534 if (!local->use_chanctx) {
675a0b04
KB
535 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
536 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
537 chandef->center_freq1 = chandef->chan->center_freq;
538 chandef->center_freq2 = 0;
e4746851
SW
539
540 /* NOTE: Disabling radar is only valid here for
541 * single channel context. To be sure, check it ...
542 */
1f0d54cd
MK
543 WARN_ON(local->hw.conf.radar_enabled &&
544 !list_empty(&local->chanctx_list));
545
e4746851
SW
546 local->hw.conf.radar_enabled = false;
547
9b4816f5 548 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
55de908a
JB
549 } else {
550 drv_remove_chanctx(local, ctx);
551 }
35f2fce9 552
fd0f979a 553 ieee80211_recalc_idle(local);
d01a1e65
MK
554}
555
1f0d54cd
MK
556static void ieee80211_free_chanctx(struct ieee80211_local *local,
557 struct ieee80211_chanctx *ctx)
d01a1e65 558{
d01a1e65
MK
559 lockdep_assert_held(&local->chanctx_mtx);
560
c0166da9 561 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
5bbe754d 562
1f0d54cd
MK
563 list_del_rcu(&ctx->list);
564 ieee80211_del_chanctx(local, ctx);
565 kfree_rcu(ctx, rcu_head);
d01a1e65
MK
566}
567
0fabfaaf
AN
568void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
569 struct ieee80211_chanctx *ctx)
e89a96f5
MK
570{
571 struct ieee80211_chanctx_conf *conf = &ctx->conf;
572 struct ieee80211_sub_if_data *sdata;
4bf88530 573 const struct cfg80211_chan_def *compat = NULL;
0fabfaaf 574 struct sta_info *sta;
e89a96f5
MK
575
576 lockdep_assert_held(&local->chanctx_mtx);
577
578 rcu_read_lock();
579 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4bf88530 580
e89a96f5
MK
581 if (!ieee80211_sdata_running(sdata))
582 continue;
583 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
584 continue;
0e67c136
FF
585 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
586 continue;
e89a96f5 587
4bf88530
JB
588 if (!compat)
589 compat = &sdata->vif.bss_conf.chandef;
590
591 compat = cfg80211_chandef_compatible(
592 &sdata->vif.bss_conf.chandef, compat);
a00f4f6e 593 if (WARN_ON_ONCE(!compat))
4bf88530 594 break;
e89a96f5 595 }
0fabfaaf
AN
596
597 /* TDLS peers can sometimes affect the chandef width */
598 list_for_each_entry_rcu(sta, &local->sta_list, list) {
599 if (!sta->uploaded ||
600 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
601 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
602 !sta->tdls_chandef.chan)
603 continue;
604
605 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
606 compat);
607 if (WARN_ON_ONCE(!compat))
608 break;
609 }
e89a96f5
MK
610 rcu_read_unlock();
611
a00f4f6e 612 if (!compat)
4bf88530 613 return;
e89a96f5 614
18942d3b 615 ieee80211_change_chanctx(local, ctx, compat);
e89a96f5
MK
616}
617
367bbd10
JB
618static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
619 struct ieee80211_chanctx *chanctx)
620{
621 bool radar_enabled;
622
623 lockdep_assert_held(&local->chanctx_mtx);
5cbc95a7 624 /* for ieee80211_is_radar_required */
34a3740d 625 lockdep_assert_held(&local->mtx);
367bbd10 626
e7f2337a 627 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
367bbd10
JB
628
629 if (radar_enabled == chanctx->conf.radar_enabled)
630 return;
631
632 chanctx->conf.radar_enabled = radar_enabled;
367bbd10
JB
633
634 if (!local->use_chanctx) {
635 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
636 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
637 }
638
639 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
640}
641
77eeba97
LC
642static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
643 struct ieee80211_chanctx *new_ctx)
d01a1e65 644{
35f2fce9 645 struct ieee80211_local *local = sdata->local;
77eeba97
LC
646 struct ieee80211_chanctx_conf *conf;
647 struct ieee80211_chanctx *curr_ctx = NULL;
648 int ret = 0;
d01a1e65 649
708d50ed
AB
650 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
651 return -ENOTSUPP;
652
77eeba97
LC
653 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
654 lockdep_is_held(&local->chanctx_mtx));
d01a1e65 655
77eeba97
LC
656 if (conf) {
657 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
35f2fce9 658
77eeba97
LC
659 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
660 conf = NULL;
484298ad 661 list_del(&sdata->assigned_chanctx_list);
77eeba97 662 }
5bbe754d 663
77eeba97
LC
664 if (new_ctx) {
665 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
666 if (ret)
667 goto out;
fd0f979a 668
77eeba97 669 conf = &new_ctx->conf;
484298ad
MK
670 list_add(&sdata->assigned_chanctx_list,
671 &new_ctx->assigned_vifs);
77eeba97 672 }
e89a96f5 673
77eeba97
LC
674out:
675 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
fd0f979a 676
77eeba97
LC
677 sdata->vif.bss_conf.idle = !conf;
678
c0166da9 679 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
77eeba97
LC
680 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
681 ieee80211_recalc_smps_chanctx(local, curr_ctx);
682 ieee80211_recalc_radar_chanctx(local, curr_ctx);
683 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
04ecd257 684 }
e89a96f5 685
c0166da9 686 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
db82d8a9 687 ieee80211_recalc_txpower(sdata, false);
77eeba97 688 ieee80211_recalc_chanctx_min_def(local, new_ctx);
04ecd257 689 }
77eeba97
LC
690
691 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
692 sdata->vif.type != NL80211_IFTYPE_MONITOR)
693 ieee80211_bss_info_change_notify(sdata,
694 BSS_CHANGED_IDLE);
695
17c18bf8
JB
696 ieee80211_check_fast_xmit_iface(sdata);
697
77eeba97 698 return ret;
d01a1e65
MK
699}
700
04ecd257
JB
701void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
702 struct ieee80211_chanctx *chanctx)
703{
704 struct ieee80211_sub_if_data *sdata;
705 u8 rx_chains_static, rx_chains_dynamic;
706
707 lockdep_assert_held(&local->chanctx_mtx);
708
709 rx_chains_static = 1;
710 rx_chains_dynamic = 1;
711
712 rcu_read_lock();
713 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
714 u8 needed_static, needed_dynamic;
715
716 if (!ieee80211_sdata_running(sdata))
717 continue;
718
719 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
720 &chanctx->conf)
721 continue;
722
723 switch (sdata->vif.type) {
724 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 725 case NL80211_IFTYPE_NAN:
04ecd257
JB
726 continue;
727 case NL80211_IFTYPE_STATION:
728 if (!sdata->u.mgd.associated)
729 continue;
730 break;
731 case NL80211_IFTYPE_AP_VLAN:
732 continue;
733 case NL80211_IFTYPE_AP:
734 case NL80211_IFTYPE_ADHOC:
735 case NL80211_IFTYPE_WDS:
736 case NL80211_IFTYPE_MESH_POINT:
239281f8 737 case NL80211_IFTYPE_OCB:
04ecd257
JB
738 break;
739 default:
740 WARN_ON_ONCE(1);
741 }
742
743 switch (sdata->smps_mode) {
744 default:
745 WARN_ONCE(1, "Invalid SMPS mode %d\n",
746 sdata->smps_mode);
747 /* fall through */
748 case IEEE80211_SMPS_OFF:
749 needed_static = sdata->needed_rx_chains;
750 needed_dynamic = sdata->needed_rx_chains;
751 break;
752 case IEEE80211_SMPS_DYNAMIC:
753 needed_static = 1;
754 needed_dynamic = sdata->needed_rx_chains;
755 break;
756 case IEEE80211_SMPS_STATIC:
757 needed_static = 1;
758 needed_dynamic = 1;
759 break;
760 }
761
762 rx_chains_static = max(rx_chains_static, needed_static);
763 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
764 }
7b8a9cdd
IY
765
766 /* Disable SMPS for the monitor interface */
767 sdata = rcu_dereference(local->monitor_sdata);
768 if (sdata &&
769 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
770 rx_chains_dynamic = rx_chains_static = local->rx_chains;
771
04ecd257
JB
772 rcu_read_unlock();
773
774 if (!local->use_chanctx) {
775 if (rx_chains_static > 1)
776 local->smps_mode = IEEE80211_SMPS_OFF;
777 else if (rx_chains_dynamic > 1)
778 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
779 else
780 local->smps_mode = IEEE80211_SMPS_STATIC;
781 ieee80211_hw_config(local, 0);
782 }
783
784 if (rx_chains_static == chanctx->conf.rx_chains_static &&
785 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
786 return;
787
788 chanctx->conf.rx_chains_static = rx_chains_static;
789 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
790 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
791}
792
11335a55
LC
793static void
794__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
795 bool clear)
796{
33926eb7 797 struct ieee80211_local *local __maybe_unused = sdata->local;
11335a55
LC
798 struct ieee80211_sub_if_data *vlan;
799 struct ieee80211_chanctx_conf *conf;
800
801 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
802 return;
803
804 lockdep_assert_held(&local->mtx);
805
806 /* Check that conf exists, even when clearing this function
807 * must be called with the AP's channel context still there
808 * as it would otherwise cause VLANs to have an invalid
809 * channel context pointer for a while, possibly pointing
810 * to a channel context that has already been freed.
811 */
812 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
33926eb7 813 lockdep_is_held(&local->chanctx_mtx));
11335a55
LC
814 WARN_ON(!conf);
815
816 if (clear)
817 conf = NULL;
818
819 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
820 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
821}
822
823void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
824 bool clear)
825{
826 struct ieee80211_local *local = sdata->local;
827
828 mutex_lock(&local->chanctx_mtx);
829
830 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
831
832 mutex_unlock(&local->chanctx_mtx);
833}
834
835int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
836{
e3afb920
MK
837 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
838
11335a55
LC
839 lockdep_assert_held(&sdata->local->chanctx_mtx);
840
e3afb920 841 if (WARN_ON(!ctx))
73da7d5b
SW
842 return -EINVAL;
843
e3afb920 844 list_del(&sdata->reserved_chanctx_list);
11335a55
LC
845 sdata->reserved_chanctx = NULL;
846
5bcae31d
MK
847 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
848 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
849 if (WARN_ON(!ctx->replace_ctx))
850 return -EINVAL;
851
852 WARN_ON(ctx->replace_ctx->replace_state !=
853 IEEE80211_CHANCTX_WILL_BE_REPLACED);
854 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
855
856 ctx->replace_ctx->replace_ctx = NULL;
857 ctx->replace_ctx->replace_state =
858 IEEE80211_CHANCTX_REPLACE_NONE;
859
860 list_del_rcu(&ctx->list);
861 kfree_rcu(ctx, rcu_head);
862 } else {
863 ieee80211_free_chanctx(sdata->local, ctx);
864 }
865 }
e3afb920 866
11335a55
LC
867 return 0;
868}
869
870int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
871 const struct cfg80211_chan_def *chandef,
09332481
MK
872 enum ieee80211_chanctx_mode mode,
873 bool radar_required)
11335a55
LC
874{
875 struct ieee80211_local *local = sdata->local;
5bcae31d 876 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
11335a55 877
5bcae31d 878 lockdep_assert_held(&local->chanctx_mtx);
73da7d5b 879
5bcae31d
MK
880 curr_ctx = ieee80211_vif_get_chanctx(sdata);
881 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
882 return -ENOTSUPP;
11335a55 883
13f348a8 884 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
11335a55 885 if (!new_ctx) {
5bcae31d 886 if (ieee80211_can_create_new_chanctx(local)) {
5d52ee81 887 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
5bcae31d
MK
888 if (IS_ERR(new_ctx))
889 return PTR_ERR(new_ctx);
c2b90ad8 890 } else {
5bcae31d
MK
891 if (!curr_ctx ||
892 (curr_ctx->replace_state ==
893 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
894 !list_empty(&curr_ctx->reserved_vifs)) {
895 /*
896 * Another vif already requested this context
897 * for a reservation. Find another one hoping
898 * all vifs assigned to it will also switch
899 * soon enough.
900 *
901 * TODO: This needs a little more work as some
902 * cases (more than 2 chanctx capable devices)
903 * may fail which could otherwise succeed
904 * provided some channel context juggling was
905 * performed.
906 *
907 * Consider ctx1..3, vif1..6, each ctx has 2
908 * vifs. vif1 and vif2 from ctx1 request new
909 * different chandefs starting 2 in-place
910 * reserations with ctx4 and ctx5 replacing
911 * ctx1 and ctx2 respectively. Next vif5 and
912 * vif6 from ctx3 reserve ctx4. If vif3 and
913 * vif4 remain on ctx2 as they are then this
914 * fails unless `replace_ctx` from ctx5 is
915 * replaced with ctx3.
916 */
917 list_for_each_entry(ctx, &local->chanctx_list,
918 list) {
919 if (ctx->replace_state !=
920 IEEE80211_CHANCTX_REPLACE_NONE)
921 continue;
922
923 if (!list_empty(&ctx->reserved_vifs))
924 continue;
925
926 curr_ctx = ctx;
927 break;
928 }
929 }
930
931 /*
932 * If that's true then all available contexts already
933 * have reservations and cannot be used.
934 */
935 if (!curr_ctx ||
936 (curr_ctx->replace_state ==
937 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
938 !list_empty(&curr_ctx->reserved_vifs))
939 return -EBUSY;
940
941 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
942 if (!new_ctx)
943 return -ENOMEM;
944
945 new_ctx->replace_ctx = curr_ctx;
946 new_ctx->replace_state =
947 IEEE80211_CHANCTX_REPLACES_OTHER;
948
949 curr_ctx->replace_ctx = new_ctx;
950 curr_ctx->replace_state =
951 IEEE80211_CHANCTX_WILL_BE_REPLACED;
952
953 list_add_rcu(&new_ctx->list, &local->chanctx_list);
11335a55
LC
954 }
955 }
956
e3afb920 957 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
11335a55
LC
958 sdata->reserved_chanctx = new_ctx;
959 sdata->reserved_chandef = *chandef;
09332481 960 sdata->reserved_radar_required = radar_required;
5bcae31d
MK
961 sdata->reserved_ready = false;
962
963 return 0;
11335a55
LC
964}
965
03078de4
MK
966static void
967ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
968{
969 switch (sdata->vif.type) {
970 case NL80211_IFTYPE_ADHOC:
971 case NL80211_IFTYPE_AP:
972 case NL80211_IFTYPE_MESH_POINT:
6e0bd6c3 973 case NL80211_IFTYPE_OCB:
03078de4
MK
974 ieee80211_queue_work(&sdata->local->hw,
975 &sdata->csa_finalize_work);
976 break;
03078de4 977 case NL80211_IFTYPE_STATION:
4c3ebc56
MK
978 ieee80211_queue_work(&sdata->local->hw,
979 &sdata->u.mgd.chswitch_work);
980 break;
981 case NL80211_IFTYPE_UNSPECIFIED:
03078de4
MK
982 case NL80211_IFTYPE_AP_VLAN:
983 case NL80211_IFTYPE_WDS:
984 case NL80211_IFTYPE_MONITOR:
985 case NL80211_IFTYPE_P2P_CLIENT:
986 case NL80211_IFTYPE_P2P_GO:
987 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 988 case NL80211_IFTYPE_NAN:
03078de4
MK
989 case NUM_NL80211_IFTYPES:
990 WARN_ON(1);
991 break;
992 }
993}
994
2967e031
FF
995static void
996ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
997 const struct cfg80211_chan_def *chandef)
998{
999 struct ieee80211_sub_if_data *vlan;
1000
1001 sdata->vif.bss_conf.chandef = *chandef;
1002
1003 if (sdata->vif.type != NL80211_IFTYPE_AP)
1004 return;
1005
1006 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1007 vlan->vif.bss_conf.chandef = *chandef;
1008}
1009
5bcae31d
MK
1010static int
1011ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
11335a55
LC
1012{
1013 struct ieee80211_local *local = sdata->local;
5bcae31d
MK
1014 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1015 struct ieee80211_chanctx *old_ctx, *new_ctx;
1016 const struct cfg80211_chan_def *chandef;
1017 u32 changed = 0;
1018 int err;
11335a55
LC
1019
1020 lockdep_assert_held(&local->mtx);
5bcae31d 1021 lockdep_assert_held(&local->chanctx_mtx);
11335a55 1022
5bcae31d
MK
1023 new_ctx = sdata->reserved_chanctx;
1024 old_ctx = ieee80211_vif_get_chanctx(sdata);
11335a55 1025
5bcae31d
MK
1026 if (WARN_ON(!sdata->reserved_ready))
1027 return -EBUSY;
73da7d5b 1028
5bcae31d
MK
1029 if (WARN_ON(!new_ctx))
1030 return -EINVAL;
1031
1032 if (WARN_ON(!old_ctx))
1033 return -EINVAL;
1034
1035 if (WARN_ON(new_ctx->replace_state ==
1036 IEEE80211_CHANCTX_REPLACES_OTHER))
1037 return -EINVAL;
1038
1039 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1040 &sdata->reserved_chandef);
1041 if (WARN_ON(!chandef))
1042 return -EINVAL;
1043
bf45a242
AO
1044 ieee80211_change_chanctx(local, new_ctx, chandef);
1045
5bcae31d
MK
1046 vif_chsw[0].vif = &sdata->vif;
1047 vif_chsw[0].old_ctx = &old_ctx->conf;
1048 vif_chsw[0].new_ctx = &new_ctx->conf;
1049
1050 list_del(&sdata->reserved_chanctx_list);
1051 sdata->reserved_chanctx = NULL;
1052
1053 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1054 CHANCTX_SWMODE_REASSIGN_VIF);
1055 if (err) {
1056 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1057 ieee80211_free_chanctx(local, new_ctx);
1058
03078de4 1059 goto out;
73da7d5b
SW
1060 }
1061
5bcae31d
MK
1062 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1063 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1064
1065 if (sdata->vif.type == NL80211_IFTYPE_AP)
1066 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1067
17c18bf8
JB
1068 ieee80211_check_fast_xmit_iface(sdata);
1069
5bcae31d
MK
1070 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1071 ieee80211_free_chanctx(local, old_ctx);
73da7d5b 1072
11335a55 1073 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
5bcae31d 1074 changed = BSS_CHANGED_BANDWIDTH;
11335a55 1075
2967e031 1076 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
11335a55 1077
722ddb0d
EG
1078 ieee80211_recalc_smps_chanctx(local, new_ctx);
1079 ieee80211_recalc_radar_chanctx(local, new_ctx);
1080 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1081
5bcae31d
MK
1082 if (changed)
1083 ieee80211_bss_info_change_notify(sdata, changed);
1084
03078de4
MK
1085out:
1086 ieee80211_vif_chanctx_reservation_complete(sdata);
5bcae31d
MK
1087 return err;
1088}
1089
1090static int
1091ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1092{
1093 struct ieee80211_local *local = sdata->local;
1094 struct ieee80211_chanctx *old_ctx, *new_ctx;
1095 const struct cfg80211_chan_def *chandef;
1096 int err;
1097
1098 old_ctx = ieee80211_vif_get_chanctx(sdata);
1099 new_ctx = sdata->reserved_chanctx;
1100
1101 if (WARN_ON(!sdata->reserved_ready))
1102 return -EINVAL;
1103
1104 if (WARN_ON(old_ctx))
1105 return -EINVAL;
1106
1107 if (WARN_ON(!new_ctx))
1108 return -EINVAL;
1109
1110 if (WARN_ON(new_ctx->replace_state ==
1111 IEEE80211_CHANCTX_REPLACES_OTHER))
1112 return -EINVAL;
1113
1114 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1115 &sdata->reserved_chandef);
1116 if (WARN_ON(!chandef))
1117 return -EINVAL;
1118
bf45a242
AO
1119 ieee80211_change_chanctx(local, new_ctx, chandef);
1120
e3afb920 1121 list_del(&sdata->reserved_chanctx_list);
5bcae31d 1122 sdata->reserved_chanctx = NULL;
11335a55 1123
5bcae31d
MK
1124 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1125 if (err) {
1126 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1127 ieee80211_free_chanctx(local, new_ctx);
1128
1129 goto out;
1130 }
1131
1132out:
03078de4 1133 ieee80211_vif_chanctx_reservation_complete(sdata);
5bcae31d
MK
1134 return err;
1135}
1136
1137static bool
1138ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1139{
1140 struct ieee80211_chanctx *old_ctx, *new_ctx;
1141
1142 lockdep_assert_held(&sdata->local->chanctx_mtx);
1143
1144 new_ctx = sdata->reserved_chanctx;
1145 old_ctx = ieee80211_vif_get_chanctx(sdata);
1146
1147 if (!old_ctx)
1148 return false;
1149
1150 if (WARN_ON(!new_ctx))
1151 return false;
1152
1153 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1154 return false;
1155
1156 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1157 return false;
1158
1159 return true;
1160}
1161
1162static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1163 struct ieee80211_chanctx *new_ctx)
1164{
1165 const struct cfg80211_chan_def *chandef;
1166
1167 lockdep_assert_held(&local->mtx);
1168 lockdep_assert_held(&local->chanctx_mtx);
1169
1170 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1171 if (WARN_ON(!chandef))
1172 return -EINVAL;
1173
1174 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1175 local->_oper_chandef = *chandef;
1176 ieee80211_hw_config(local, 0);
1177
1178 return 0;
1179}
1180
1181static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1182 int n_vifs)
1183{
1184 struct ieee80211_vif_chanctx_switch *vif_chsw;
1185 struct ieee80211_sub_if_data *sdata;
1186 struct ieee80211_chanctx *ctx, *old_ctx;
1187 int i, err;
1188
1189 lockdep_assert_held(&local->mtx);
1190 lockdep_assert_held(&local->chanctx_mtx);
1191
1192 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1193 if (!vif_chsw)
1194 return -ENOMEM;
1195
1196 i = 0;
1197 list_for_each_entry(ctx, &local->chanctx_list, list) {
1198 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1199 continue;
1200
1201 if (WARN_ON(!ctx->replace_ctx)) {
1202 err = -EINVAL;
5d52ee81
LC
1203 goto out;
1204 }
11335a55 1205
5bcae31d
MK
1206 list_for_each_entry(sdata, &ctx->reserved_vifs,
1207 reserved_chanctx_list) {
1208 if (!ieee80211_vif_has_in_place_reservation(
1209 sdata))
1210 continue;
1211
1212 old_ctx = ieee80211_vif_get_chanctx(sdata);
1213 vif_chsw[i].vif = &sdata->vif;
1214 vif_chsw[i].old_ctx = &old_ctx->conf;
1215 vif_chsw[i].new_ctx = &ctx->conf;
1216
1217 i++;
1218 }
5d52ee81 1219 }
11335a55 1220
5bcae31d
MK
1221 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1222 CHANCTX_SWMODE_SWAP_CONTEXTS);
73da7d5b 1223
11335a55 1224out:
5bcae31d
MK
1225 kfree(vif_chsw);
1226 return err;
1227}
1228
1229static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1230{
1231 struct ieee80211_chanctx *ctx;
1232 int err;
1233
1234 lockdep_assert_held(&local->mtx);
1235 lockdep_assert_held(&local->chanctx_mtx);
1236
1237 list_for_each_entry(ctx, &local->chanctx_list, list) {
1238 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1239 continue;
1240
1241 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1242 continue;
1243
1244 ieee80211_del_chanctx(local, ctx->replace_ctx);
1245 err = ieee80211_add_chanctx(local, ctx);
1246 if (err)
1247 goto err;
1248 }
1249
1250 return 0;
1251
1252err:
1253 WARN_ON(ieee80211_add_chanctx(local, ctx));
1254 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1255 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1256 continue;
1257
1258 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1259 continue;
1260
1261 ieee80211_del_chanctx(local, ctx);
1262 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1263 }
1264
1265 return err;
1266}
1267
649b2a4d 1268static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
5bcae31d
MK
1269{
1270 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1271 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1272 struct ieee80211_chanctx *new_ctx = NULL;
1273 int i, err, n_assigned, n_reserved, n_ready;
1274 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1275
1276 lockdep_assert_held(&local->mtx);
1277 lockdep_assert_held(&local->chanctx_mtx);
1278
1279 /*
1280 * If there are 2 independent pairs of channel contexts performing
1281 * cross-switch of their vifs this code will still wait until both are
1282 * ready even though it could be possible to switch one before the
1283 * other is ready.
1284 *
1285 * For practical reasons and code simplicity just do a single huge
1286 * switch.
1287 */
1288
1289 /*
1290 * Verify if the reservation is still feasible.
1291 * - if it's not then disconnect
1292 * - if it is but not all vifs necessary are ready then defer
1293 */
1294
1295 list_for_each_entry(ctx, &local->chanctx_list, list) {
1296 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1297 continue;
1298
1299 if (WARN_ON(!ctx->replace_ctx)) {
1300 err = -EINVAL;
1301 goto err;
1302 }
1303
1304 if (!local->use_chanctx)
1305 new_ctx = ctx;
1306
1307 n_ctx++;
1308
1309 n_assigned = 0;
1310 n_reserved = 0;
1311 n_ready = 0;
1312
1313 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1314 assigned_chanctx_list) {
1315 n_assigned++;
1316 if (sdata->reserved_chanctx) {
1317 n_reserved++;
1318 if (sdata->reserved_ready)
1319 n_ready++;
1320 }
1321 }
1322
1323 if (n_assigned != n_reserved) {
1324 if (n_ready == n_reserved) {
1325 wiphy_info(local->hw.wiphy,
1326 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1327 err = -EBUSY;
1328 goto err;
1329 }
1330
1331 return -EAGAIN;
1332 }
1333
1334 ctx->conf.radar_enabled = false;
1335 list_for_each_entry(sdata, &ctx->reserved_vifs,
1336 reserved_chanctx_list) {
1337 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1338 !sdata->reserved_ready)
1339 return -EAGAIN;
1340
1341 old_ctx = ieee80211_vif_get_chanctx(sdata);
1342 if (old_ctx) {
1343 if (old_ctx->replace_state ==
1344 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1345 n_vifs_switch++;
1346 else
1347 n_vifs_assign++;
1348 } else {
1349 n_vifs_ctxless++;
1350 }
1351
1352 if (sdata->reserved_radar_required)
1353 ctx->conf.radar_enabled = true;
1354 }
1355 }
1356
1357 if (WARN_ON(n_ctx == 0) ||
1358 WARN_ON(n_vifs_switch == 0 &&
1359 n_vifs_assign == 0 &&
1360 n_vifs_ctxless == 0) ||
1361 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1362 WARN_ON(!new_ctx && !local->use_chanctx)) {
1363 err = -EINVAL;
1364 goto err;
1365 }
1366
1367 /*
1368 * All necessary vifs are ready. Perform the switch now depending on
1369 * reservations and driver capabilities.
1370 */
1371
1372 if (local->use_chanctx) {
1373 if (n_vifs_switch > 0) {
1374 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1375 if (err)
1376 goto err;
1377 }
1378
1379 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1380 err = ieee80211_chsw_switch_ctxs(local);
1381 if (err)
1382 goto err;
1383 }
1384 } else {
1385 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1386 if (err)
1387 goto err;
1388 }
1389
1390 /*
1391 * Update all structures, values and pointers to point to new channel
1392 * context(s).
1393 */
1394
1395 i = 0;
1396 list_for_each_entry(ctx, &local->chanctx_list, list) {
1397 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1398 continue;
1399
1400 if (WARN_ON(!ctx->replace_ctx)) {
1401 err = -EINVAL;
1402 goto err;
1403 }
1404
1405 list_for_each_entry(sdata, &ctx->reserved_vifs,
1406 reserved_chanctx_list) {
1407 u32 changed = 0;
1408
1409 if (!ieee80211_vif_has_in_place_reservation(sdata))
1410 continue;
1411
1412 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1413
1414 if (sdata->vif.type == NL80211_IFTYPE_AP)
1415 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1416 false);
1417
17c18bf8
JB
1418 ieee80211_check_fast_xmit_iface(sdata);
1419
5bcae31d
MK
1420 sdata->radar_required = sdata->reserved_radar_required;
1421
1422 if (sdata->vif.bss_conf.chandef.width !=
1423 sdata->reserved_chandef.width)
1424 changed = BSS_CHANGED_BANDWIDTH;
1425
2967e031 1426 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
5bcae31d
MK
1427 if (changed)
1428 ieee80211_bss_info_change_notify(sdata,
1429 changed);
1430
db82d8a9 1431 ieee80211_recalc_txpower(sdata, false);
5bcae31d
MK
1432 }
1433
1434 ieee80211_recalc_chanctx_chantype(local, ctx);
1435 ieee80211_recalc_smps_chanctx(local, ctx);
1436 ieee80211_recalc_radar_chanctx(local, ctx);
1437 ieee80211_recalc_chanctx_min_def(local, ctx);
1438
1439 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1440 reserved_chanctx_list) {
1441 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1442 continue;
1443
1444 list_del(&sdata->reserved_chanctx_list);
1445 list_move(&sdata->assigned_chanctx_list,
47e4df94 1446 &ctx->assigned_vifs);
5bcae31d 1447 sdata->reserved_chanctx = NULL;
03078de4
MK
1448
1449 ieee80211_vif_chanctx_reservation_complete(sdata);
5bcae31d
MK
1450 }
1451
1452 /*
1453 * This context might have been a dependency for an already
1454 * ready re-assign reservation interface that was deferred. Do
1455 * not propagate error to the caller though. The in-place
1456 * reservation for originally requested interface has already
1457 * succeeded at this point.
1458 */
1459 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1460 reserved_chanctx_list) {
1461 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1462 sdata)))
1463 continue;
1464
1465 if (WARN_ON(sdata->reserved_chanctx != ctx))
1466 continue;
1467
1468 if (!sdata->reserved_ready)
1469 continue;
1470
1471 if (ieee80211_vif_get_chanctx(sdata))
1472 err = ieee80211_vif_use_reserved_reassign(
1473 sdata);
1474 else
1475 err = ieee80211_vif_use_reserved_assign(sdata);
1476
1477 if (err) {
1478 sdata_info(sdata,
1479 "failed to finalize (re-)assign reservation (err=%d)\n",
1480 err);
1481 ieee80211_vif_unreserve_chanctx(sdata);
1482 cfg80211_stop_iface(local->hw.wiphy,
1483 &sdata->wdev,
1484 GFP_KERNEL);
1485 }
1486 }
1487 }
1488
1489 /*
1490 * Finally free old contexts
1491 */
1492
1493 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1494 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1495 continue;
1496
1497 ctx->replace_ctx->replace_ctx = NULL;
1498 ctx->replace_ctx->replace_state =
1499 IEEE80211_CHANCTX_REPLACE_NONE;
1500
1501 list_del_rcu(&ctx->list);
1502 kfree_rcu(ctx, rcu_head);
1503 }
1504
1505 return 0;
1506
1507err:
1508 list_for_each_entry(ctx, &local->chanctx_list, list) {
1509 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1510 continue;
1511
1512 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
03078de4 1513 reserved_chanctx_list) {
5bcae31d 1514 ieee80211_vif_unreserve_chanctx(sdata);
03078de4
MK
1515 ieee80211_vif_chanctx_reservation_complete(sdata);
1516 }
5bcae31d
MK
1517 }
1518
1519 return err;
1520}
1521
649b2a4d
JB
1522static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1523{
1524 struct ieee80211_local *local = sdata->local;
1525 struct ieee80211_chanctx_conf *conf;
1526 struct ieee80211_chanctx *ctx;
1527 bool use_reserved_switch = false;
1528
1529 lockdep_assert_held(&local->chanctx_mtx);
1530
1531 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1532 lockdep_is_held(&local->chanctx_mtx));
1533 if (!conf)
1534 return;
1535
1536 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1537
1538 if (sdata->reserved_chanctx) {
1539 if (sdata->reserved_chanctx->replace_state ==
1540 IEEE80211_CHANCTX_REPLACES_OTHER &&
1541 ieee80211_chanctx_num_reserved(local,
1542 sdata->reserved_chanctx) > 1)
1543 use_reserved_switch = true;
1544
1545 ieee80211_vif_unreserve_chanctx(sdata);
1546 }
1547
1548 ieee80211_assign_vif_chanctx(sdata, NULL);
1549 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1550 ieee80211_free_chanctx(local, ctx);
1551
104f5a62
EP
1552 sdata->radar_required = false;
1553
649b2a4d
JB
1554 /* Unreserving may ready an in-place reservation. */
1555 if (use_reserved_switch)
1556 ieee80211_vif_use_reserved_switch(local);
1557}
1558
1559int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1560 const struct cfg80211_chan_def *chandef,
1561 enum ieee80211_chanctx_mode mode)
1562{
1563 struct ieee80211_local *local = sdata->local;
1564 struct ieee80211_chanctx *ctx;
1565 u8 radar_detect_width = 0;
1566 int ret;
1567
1568 lockdep_assert_held(&local->mtx);
1569
1570 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1571
1572 mutex_lock(&local->chanctx_mtx);
1573
1574 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1575 chandef,
1576 sdata->wdev.iftype);
1577 if (ret < 0)
1578 goto out;
1579 if (ret > 0)
1580 radar_detect_width = BIT(chandef->width);
1581
1582 sdata->radar_required = ret;
1583
1584 ret = ieee80211_check_combinations(sdata, chandef, mode,
1585 radar_detect_width);
1586 if (ret < 0)
1587 goto out;
1588
1589 __ieee80211_vif_release_channel(sdata);
1590
1591 ctx = ieee80211_find_chanctx(local, chandef, mode);
1592 if (!ctx)
1593 ctx = ieee80211_new_chanctx(local, chandef, mode);
1594 if (IS_ERR(ctx)) {
1595 ret = PTR_ERR(ctx);
1596 goto out;
1597 }
1598
2967e031 1599 ieee80211_vif_update_chandef(sdata, chandef);
649b2a4d
JB
1600
1601 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1602 if (ret) {
1603 /* if assign fails refcount stays the same */
1604 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1605 ieee80211_free_chanctx(local, ctx);
1606 goto out;
1607 }
1608
1609 ieee80211_recalc_smps_chanctx(local, ctx);
1610 ieee80211_recalc_radar_chanctx(local, ctx);
1611 out:
104f5a62
EP
1612 if (ret)
1613 sdata->radar_required = false;
1614
649b2a4d
JB
1615 mutex_unlock(&local->chanctx_mtx);
1616 return ret;
1617}
1618
5bcae31d
MK
1619int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1620{
1621 struct ieee80211_local *local = sdata->local;
1622 struct ieee80211_chanctx *new_ctx;
1623 struct ieee80211_chanctx *old_ctx;
1624 int err;
1625
1626 lockdep_assert_held(&local->mtx);
1627 lockdep_assert_held(&local->chanctx_mtx);
1628
1629 new_ctx = sdata->reserved_chanctx;
1630 old_ctx = ieee80211_vif_get_chanctx(sdata);
1631
1632 if (WARN_ON(!new_ctx))
1633 return -EINVAL;
1634
1635 if (WARN_ON(new_ctx->replace_state ==
1636 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1637 return -EINVAL;
1638
1639 if (WARN_ON(sdata->reserved_ready))
1640 return -EINVAL;
1641
1642 sdata->reserved_ready = true;
1643
1644 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1645 if (old_ctx)
1646 err = ieee80211_vif_use_reserved_reassign(sdata);
1647 else
1648 err = ieee80211_vif_use_reserved_assign(sdata);
1649
1650 if (err)
1651 return err;
1652 }
1653
1654 /*
1655 * In-place reservation may need to be finalized now either if:
1656 * a) sdata is taking part in the swapping itself and is the last one
1657 * b) sdata has switched with a re-assign reservation to an existing
1658 * context readying in-place switching of old_ctx
1659 *
1660 * In case of (b) do not propagate the error up because the requested
1661 * sdata already switched successfully. Just spill an extra warning.
1662 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1663 * interfaces upon failure.
1664 */
1665 if ((old_ctx &&
1666 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1667 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1668 err = ieee80211_vif_use_reserved_switch(local);
1669 if (err && err != -EAGAIN) {
1670 if (new_ctx->replace_state ==
1671 IEEE80211_CHANCTX_REPLACES_OTHER)
1672 return err;
1673
1674 wiphy_info(local->hw.wiphy,
1675 "depending in-place reservation failed (err=%d)\n",
1676 err);
1677 }
1678 }
1679
1680 return 0;
73da7d5b
SW
1681}
1682
2c9b7359
JB
1683int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1684 const struct cfg80211_chan_def *chandef,
1685 u32 *changed)
1686{
1687 struct ieee80211_local *local = sdata->local;
1688 struct ieee80211_chanctx_conf *conf;
1689 struct ieee80211_chanctx *ctx;
5bcae31d 1690 const struct cfg80211_chan_def *compat;
2c9b7359
JB
1691 int ret;
1692
1693 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1694 IEEE80211_CHAN_DISABLED))
1695 return -EINVAL;
1696
1697 mutex_lock(&local->chanctx_mtx);
1698 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1699 ret = 0;
1700 goto out;
1701 }
1702
1703 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1704 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1705 ret = -EINVAL;
1706 goto out;
1707 }
1708
1709 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1710 lockdep_is_held(&local->chanctx_mtx));
1711 if (!conf) {
1712 ret = -EINVAL;
1713 goto out;
1714 }
1715
1716 ctx = container_of(conf, struct ieee80211_chanctx, conf);
5bcae31d
MK
1717
1718 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1719 if (!compat) {
2c9b7359
JB
1720 ret = -EINVAL;
1721 goto out;
1722 }
1723
5bcae31d
MK
1724 switch (ctx->replace_state) {
1725 case IEEE80211_CHANCTX_REPLACE_NONE:
1726 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1727 ret = -EBUSY;
1728 goto out;
1729 }
1730 break;
1731 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
d070f913 1732 /* TODO: Perhaps the bandwidth change could be treated as a
5bcae31d
MK
1733 * reservation itself? */
1734 ret = -EBUSY;
1735 goto out;
1736 case IEEE80211_CHANCTX_REPLACES_OTHER:
1737 /* channel context that is going to replace another channel
1738 * context doesn't really exist and shouldn't be assigned
1739 * anywhere yet */
1740 WARN_ON(1);
1741 break;
1742 }
1743
2967e031 1744 ieee80211_vif_update_chandef(sdata, chandef);
2c9b7359
JB
1745
1746 ieee80211_recalc_chanctx_chantype(local, ctx);
1747
1748 *changed |= BSS_CHANGED_BANDWIDTH;
1749 ret = 0;
1750 out:
1751 mutex_unlock(&local->chanctx_mtx);
1752 return ret;
1753}
1754
d01a1e65
MK
1755void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1756{
55de908a
JB
1757 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1758
34a3740d
JB
1759 lockdep_assert_held(&sdata->local->mtx);
1760
d01a1e65
MK
1761 mutex_lock(&sdata->local->chanctx_mtx);
1762 __ieee80211_vif_release_channel(sdata);
1763 mutex_unlock(&sdata->local->chanctx_mtx);
1764}
3448c005 1765
4d76d21b
JB
1766void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1767{
1768 struct ieee80211_local *local = sdata->local;
1769 struct ieee80211_sub_if_data *ap;
1770 struct ieee80211_chanctx_conf *conf;
1771
1772 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1773 return;
1774
1775 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1776
1777 mutex_lock(&local->chanctx_mtx);
1778
1779 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1780 lockdep_is_held(&local->chanctx_mtx));
1781 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1782 mutex_unlock(&local->chanctx_mtx);
1783}
1784
3448c005
JB
1785void ieee80211_iter_chan_contexts_atomic(
1786 struct ieee80211_hw *hw,
1787 void (*iter)(struct ieee80211_hw *hw,
1788 struct ieee80211_chanctx_conf *chanctx_conf,
1789 void *data),
1790 void *iter_data)
1791{
1792 struct ieee80211_local *local = hw_to_local(hw);
1793 struct ieee80211_chanctx *ctx;
1794
1795 rcu_read_lock();
1796 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
8a61af65
JB
1797 if (ctx->driver_present)
1798 iter(hw, &ctx->conf, iter_data);
3448c005
JB
1799 rcu_read_unlock();
1800}
1801EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);