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