rt2x00usb: fix anchor initialization
[linux/fpc-iii.git] / net / mac80211 / chan.c
blobe75cbf6ecc26e4ec7bde777c385e001c0e49e371
1 /*
2 * mac80211 - channel management
3 */
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"
12 static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
13 struct ieee80211_chanctx *ctx)
15 struct ieee80211_sub_if_data *sdata;
16 int num = 0;
18 lockdep_assert_held(&local->chanctx_mtx);
20 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
21 num++;
23 return num;
26 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
27 struct ieee80211_chanctx *ctx)
29 struct ieee80211_sub_if_data *sdata;
30 int num = 0;
32 lockdep_assert_held(&local->chanctx_mtx);
34 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
35 num++;
37 return num;
40 int ieee80211_chanctx_refcount(struct ieee80211_local *local,
41 struct ieee80211_chanctx *ctx)
43 return ieee80211_chanctx_num_assigned(local, ctx) +
44 ieee80211_chanctx_num_reserved(local, ctx);
47 static int ieee80211_num_chanctx(struct ieee80211_local *local)
49 struct ieee80211_chanctx *ctx;
50 int num = 0;
52 lockdep_assert_held(&local->chanctx_mtx);
54 list_for_each_entry(ctx, &local->chanctx_list, list)
55 num++;
57 return num;
60 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
62 lockdep_assert_held(&local->chanctx_mtx);
63 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
66 static struct ieee80211_chanctx *
67 ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
69 struct ieee80211_local *local __maybe_unused = sdata->local;
70 struct ieee80211_chanctx_conf *conf;
72 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
73 lockdep_is_held(&local->chanctx_mtx));
74 if (!conf)
75 return NULL;
77 return container_of(conf, struct ieee80211_chanctx, conf);
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)
85 struct ieee80211_sub_if_data *sdata;
87 lockdep_assert_held(&local->chanctx_mtx);
89 list_for_each_entry(sdata, &ctx->reserved_vifs,
90 reserved_chanctx_list) {
91 if (!compat)
92 compat = &sdata->reserved_chandef;
94 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
95 compat);
96 if (!compat)
97 break;
100 return compat;
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)
108 struct ieee80211_sub_if_data *sdata;
110 lockdep_assert_held(&local->chanctx_mtx);
112 list_for_each_entry(sdata, &ctx->assigned_vifs,
113 assigned_chanctx_list) {
114 if (sdata->reserved_chanctx != NULL)
115 continue;
117 if (!compat)
118 compat = &sdata->vif.bss_conf.chandef;
120 compat = cfg80211_chandef_compatible(
121 &sdata->vif.bss_conf.chandef, compat);
122 if (!compat)
123 break;
126 return compat;
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)
134 lockdep_assert_held(&local->chanctx_mtx);
136 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
137 if (!compat)
138 return NULL;
140 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
141 if (!compat)
142 return NULL;
144 return compat;
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)
152 lockdep_assert_held(&local->chanctx_mtx);
154 if (ieee80211_chanctx_combined_chandef(local, ctx, def))
155 return true;
157 if (!list_empty(&ctx->reserved_vifs) &&
158 ieee80211_chanctx_reserved_chandef(local, ctx, def))
159 return true;
161 return false;
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)
169 struct ieee80211_chanctx *ctx;
171 lockdep_assert_held(&local->chanctx_mtx);
173 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
174 return NULL;
176 list_for_each_entry(ctx, &local->chanctx_list, list) {
177 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
178 continue;
180 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
181 continue;
183 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
184 chandef))
185 continue;
187 return ctx;
190 return NULL;
193 enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
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:
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).
215 return NL80211_CHAN_WIDTH_160;
216 default:
217 WARN_ON(1);
218 return NL80211_CHAN_WIDTH_20;
222 static enum nl80211_chan_width
223 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
225 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
226 struct sta_info *sta;
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;
234 if (!sta->uploaded || !test_sta_flag(sta, WLAN_STA_ASSOC))
235 continue;
237 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
239 rcu_read_unlock();
241 return max_bw;
244 static enum nl80211_chan_width
245 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
246 struct ieee80211_chanctx_conf *conf)
248 struct ieee80211_sub_if_data *sdata;
249 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
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;
256 if (!ieee80211_sdata_running(sdata))
257 continue;
259 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
260 continue;
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_STATION:
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
273 width = max(vif->bss_conf.chandef.width,
274 ieee80211_get_max_required_bw(sdata));
275 break;
276 case NL80211_IFTYPE_P2P_DEVICE:
277 case NL80211_IFTYPE_NAN:
278 continue;
279 case NL80211_IFTYPE_ADHOC:
280 case NL80211_IFTYPE_WDS:
281 case NL80211_IFTYPE_MESH_POINT:
282 case NL80211_IFTYPE_OCB:
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);
292 max_bw = max(max_bw, width);
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);
300 rcu_read_unlock();
302 return max_bw;
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.
310 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
311 struct ieee80211_chanctx *ctx)
313 enum nl80211_chan_width max_bw;
314 struct cfg80211_chan_def min_def;
316 lockdep_assert_held(&local->chanctx_mtx);
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;
326 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
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);
333 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
334 return;
336 ctx->conf.min_def = min_def;
337 if (!ctx->driver_present)
338 return;
340 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
343 static void ieee80211_change_chanctx(struct ieee80211_local *local,
344 struct ieee80211_chanctx *ctx,
345 const struct cfg80211_chan_def *chandef)
347 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
348 ieee80211_recalc_chanctx_min_def(local, ctx);
349 return;
352 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
354 ctx->conf.def = *chandef;
355 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
356 ieee80211_recalc_chanctx_min_def(local, ctx);
358 if (!local->use_chanctx) {
359 local->_oper_chandef = *chandef;
360 ieee80211_hw_config(local, 0);
364 static struct ieee80211_chanctx *
365 ieee80211_find_chanctx(struct ieee80211_local *local,
366 const struct cfg80211_chan_def *chandef,
367 enum ieee80211_chanctx_mode mode)
369 struct ieee80211_chanctx *ctx;
371 lockdep_assert_held(&local->chanctx_mtx);
373 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
374 return NULL;
376 list_for_each_entry(ctx, &local->chanctx_list, list) {
377 const struct cfg80211_chan_def *compat;
379 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
380 continue;
382 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
383 continue;
385 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
386 if (!compat)
387 continue;
389 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
390 compat);
391 if (!compat)
392 continue;
394 ieee80211_change_chanctx(local, ctx, compat);
396 return ctx;
399 return NULL;
402 bool ieee80211_is_radar_required(struct ieee80211_local *local)
404 struct ieee80211_sub_if_data *sdata;
406 lockdep_assert_held(&local->mtx);
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;
415 rcu_read_unlock();
417 return false;
420 static bool
421 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
422 struct ieee80211_chanctx *ctx)
424 struct ieee80211_chanctx_conf *conf = &ctx->conf;
425 struct ieee80211_sub_if_data *sdata;
426 bool required = false;
428 lockdep_assert_held(&local->chanctx_mtx);
429 lockdep_assert_held(&local->mtx);
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;
440 required = true;
441 break;
443 rcu_read_unlock();
445 return required;
448 static struct ieee80211_chanctx *
449 ieee80211_alloc_chanctx(struct ieee80211_local *local,
450 const struct cfg80211_chan_def *chandef,
451 enum ieee80211_chanctx_mode mode)
453 struct ieee80211_chanctx *ctx;
455 lockdep_assert_held(&local->chanctx_mtx);
457 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
458 if (!ctx)
459 return NULL;
461 INIT_LIST_HEAD(&ctx->assigned_vifs);
462 INIT_LIST_HEAD(&ctx->reserved_vifs);
463 ctx->conf.def = *chandef;
464 ctx->conf.rx_chains_static = 1;
465 ctx->conf.rx_chains_dynamic = 1;
466 ctx->mode = mode;
467 ctx->conf.radar_enabled = false;
468 ieee80211_recalc_chanctx_min_def(local, ctx);
470 return ctx;
473 static int ieee80211_add_chanctx(struct ieee80211_local *local,
474 struct ieee80211_chanctx *ctx)
476 u32 changed;
477 int err;
479 lockdep_assert_held(&local->mtx);
480 lockdep_assert_held(&local->chanctx_mtx);
482 if (!local->use_chanctx)
483 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
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);
490 if (!local->use_chanctx) {
491 local->_oper_chandef = ctx->conf.def;
492 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
493 } else {
494 err = drv_add_chanctx(local, ctx);
495 if (err) {
496 ieee80211_recalc_idle(local);
497 return err;
501 return 0;
504 static struct ieee80211_chanctx *
505 ieee80211_new_chanctx(struct ieee80211_local *local,
506 const struct cfg80211_chan_def *chandef,
507 enum ieee80211_chanctx_mode mode)
509 struct ieee80211_chanctx *ctx;
510 int err;
512 lockdep_assert_held(&local->mtx);
513 lockdep_assert_held(&local->chanctx_mtx);
515 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
516 if (!ctx)
517 return ERR_PTR(-ENOMEM);
519 err = ieee80211_add_chanctx(local, ctx);
520 if (err) {
521 kfree(ctx);
522 return ERR_PTR(err);
525 list_add_rcu(&ctx->list, &local->chanctx_list);
526 return ctx;
529 static void ieee80211_del_chanctx(struct ieee80211_local *local,
530 struct ieee80211_chanctx *ctx)
532 lockdep_assert_held(&local->chanctx_mtx);
534 if (!local->use_chanctx) {
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;
540 /* NOTE: Disabling radar is only valid here for
541 * single channel context. To be sure, check it ...
543 WARN_ON(local->hw.conf.radar_enabled &&
544 !list_empty(&local->chanctx_list));
546 local->hw.conf.radar_enabled = false;
548 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
549 } else {
550 drv_remove_chanctx(local, ctx);
553 ieee80211_recalc_idle(local);
556 static void ieee80211_free_chanctx(struct ieee80211_local *local,
557 struct ieee80211_chanctx *ctx)
559 lockdep_assert_held(&local->chanctx_mtx);
561 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
563 list_del_rcu(&ctx->list);
564 ieee80211_del_chanctx(local, ctx);
565 kfree_rcu(ctx, rcu_head);
568 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
569 struct ieee80211_chanctx *ctx)
571 struct ieee80211_chanctx_conf *conf = &ctx->conf;
572 struct ieee80211_sub_if_data *sdata;
573 const struct cfg80211_chan_def *compat = NULL;
574 struct sta_info *sta;
576 lockdep_assert_held(&local->chanctx_mtx);
578 rcu_read_lock();
579 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
581 if (!ieee80211_sdata_running(sdata))
582 continue;
583 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
584 continue;
585 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
586 continue;
588 if (!compat)
589 compat = &sdata->vif.bss_conf.chandef;
591 compat = cfg80211_chandef_compatible(
592 &sdata->vif.bss_conf.chandef, compat);
593 if (WARN_ON_ONCE(!compat))
594 break;
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;
605 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
606 compat);
607 if (WARN_ON_ONCE(!compat))
608 break;
610 rcu_read_unlock();
612 if (!compat)
613 return;
615 ieee80211_change_chanctx(local, ctx, compat);
618 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
619 struct ieee80211_chanctx *chanctx)
621 bool radar_enabled;
623 lockdep_assert_held(&local->chanctx_mtx);
624 /* for ieee80211_is_radar_required */
625 lockdep_assert_held(&local->mtx);
627 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
629 if (radar_enabled == chanctx->conf.radar_enabled)
630 return;
632 chanctx->conf.radar_enabled = radar_enabled;
634 if (!local->use_chanctx) {
635 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
636 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
639 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
642 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
643 struct ieee80211_chanctx *new_ctx)
645 struct ieee80211_local *local = sdata->local;
646 struct ieee80211_chanctx_conf *conf;
647 struct ieee80211_chanctx *curr_ctx = NULL;
648 int ret = 0;
650 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
651 return -ENOTSUPP;
653 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
654 lockdep_is_held(&local->chanctx_mtx));
656 if (conf) {
657 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
659 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
660 conf = NULL;
661 list_del(&sdata->assigned_chanctx_list);
664 if (new_ctx) {
665 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
666 if (ret)
667 goto out;
669 conf = &new_ctx->conf;
670 list_add(&sdata->assigned_chanctx_list,
671 &new_ctx->assigned_vifs);
674 out:
675 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
677 sdata->vif.bss_conf.idle = !conf;
679 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
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);
686 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
687 ieee80211_recalc_txpower(sdata, false);
688 ieee80211_recalc_chanctx_min_def(local, new_ctx);
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);
696 ieee80211_check_fast_xmit_iface(sdata);
698 return ret;
701 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
702 struct ieee80211_chanctx *chanctx)
704 struct ieee80211_sub_if_data *sdata;
705 u8 rx_chains_static, rx_chains_dynamic;
707 lockdep_assert_held(&local->chanctx_mtx);
709 rx_chains_static = 1;
710 rx_chains_dynamic = 1;
712 rcu_read_lock();
713 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
714 u8 needed_static, needed_dynamic;
716 if (!ieee80211_sdata_running(sdata))
717 continue;
719 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
720 &chanctx->conf)
721 continue;
723 switch (sdata->vif.type) {
724 case NL80211_IFTYPE_P2P_DEVICE:
725 case NL80211_IFTYPE_NAN:
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:
737 case NL80211_IFTYPE_OCB:
738 break;
739 default:
740 WARN_ON_ONCE(1);
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;
762 rx_chains_static = max(rx_chains_static, needed_static);
763 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
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;
772 rcu_read_unlock();
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);
784 if (rx_chains_static == chanctx->conf.rx_chains_static &&
785 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
786 return;
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);
793 static void
794 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
795 bool clear)
797 struct ieee80211_local *local __maybe_unused = sdata->local;
798 struct ieee80211_sub_if_data *vlan;
799 struct ieee80211_chanctx_conf *conf;
801 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
802 return;
804 lockdep_assert_held(&local->mtx);
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.
812 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
813 lockdep_is_held(&local->chanctx_mtx));
814 WARN_ON(!conf);
816 if (clear)
817 conf = NULL;
819 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
820 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
823 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
824 bool clear)
826 struct ieee80211_local *local = sdata->local;
828 mutex_lock(&local->chanctx_mtx);
830 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
832 mutex_unlock(&local->chanctx_mtx);
835 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
837 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
839 lockdep_assert_held(&sdata->local->chanctx_mtx);
841 if (WARN_ON(!ctx))
842 return -EINVAL;
844 list_del(&sdata->reserved_chanctx_list);
845 sdata->reserved_chanctx = NULL;
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;
852 WARN_ON(ctx->replace_ctx->replace_state !=
853 IEEE80211_CHANCTX_WILL_BE_REPLACED);
854 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
856 ctx->replace_ctx->replace_ctx = NULL;
857 ctx->replace_ctx->replace_state =
858 IEEE80211_CHANCTX_REPLACE_NONE;
860 list_del_rcu(&ctx->list);
861 kfree_rcu(ctx, rcu_head);
862 } else {
863 ieee80211_free_chanctx(sdata->local, ctx);
867 return 0;
870 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
871 const struct cfg80211_chan_def *chandef,
872 enum ieee80211_chanctx_mode mode,
873 bool radar_required)
875 struct ieee80211_local *local = sdata->local;
876 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
878 lockdep_assert_held(&local->chanctx_mtx);
880 curr_ctx = ieee80211_vif_get_chanctx(sdata);
881 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
882 return -ENOTSUPP;
884 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
885 if (!new_ctx) {
886 if (ieee80211_can_create_new_chanctx(local)) {
887 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
888 if (IS_ERR(new_ctx))
889 return PTR_ERR(new_ctx);
890 } else {
891 if (!curr_ctx ||
892 (curr_ctx->replace_state ==
893 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
894 !list_empty(&curr_ctx->reserved_vifs)) {
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.
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.
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.
917 list_for_each_entry(ctx, &local->chanctx_list,
918 list) {
919 if (ctx->replace_state !=
920 IEEE80211_CHANCTX_REPLACE_NONE)
921 continue;
923 if (!list_empty(&ctx->reserved_vifs))
924 continue;
926 curr_ctx = ctx;
927 break;
932 * If that's true then all available contexts already
933 * have reservations and cannot be used.
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;
941 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
942 if (!new_ctx)
943 return -ENOMEM;
945 new_ctx->replace_ctx = curr_ctx;
946 new_ctx->replace_state =
947 IEEE80211_CHANCTX_REPLACES_OTHER;
949 curr_ctx->replace_ctx = new_ctx;
950 curr_ctx->replace_state =
951 IEEE80211_CHANCTX_WILL_BE_REPLACED;
953 list_add_rcu(&new_ctx->list, &local->chanctx_list);
957 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
958 sdata->reserved_chanctx = new_ctx;
959 sdata->reserved_chandef = *chandef;
960 sdata->reserved_radar_required = radar_required;
961 sdata->reserved_ready = false;
963 return 0;
966 static void
967 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
969 switch (sdata->vif.type) {
970 case NL80211_IFTYPE_ADHOC:
971 case NL80211_IFTYPE_AP:
972 case NL80211_IFTYPE_MESH_POINT:
973 case NL80211_IFTYPE_OCB:
974 ieee80211_queue_work(&sdata->local->hw,
975 &sdata->csa_finalize_work);
976 break;
977 case NL80211_IFTYPE_STATION:
978 ieee80211_queue_work(&sdata->local->hw,
979 &sdata->u.mgd.chswitch_work);
980 break;
981 case NL80211_IFTYPE_UNSPECIFIED:
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:
988 case NL80211_IFTYPE_NAN:
989 case NUM_NL80211_IFTYPES:
990 WARN_ON(1);
991 break;
995 static void
996 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
997 const struct cfg80211_chan_def *chandef)
999 struct ieee80211_sub_if_data *vlan;
1001 sdata->vif.bss_conf.chandef = *chandef;
1003 if (sdata->vif.type != NL80211_IFTYPE_AP)
1004 return;
1006 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1007 vlan->vif.bss_conf.chandef = *chandef;
1010 static int
1011 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
1013 struct ieee80211_local *local = sdata->local;
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;
1020 lockdep_assert_held(&local->mtx);
1021 lockdep_assert_held(&local->chanctx_mtx);
1023 new_ctx = sdata->reserved_chanctx;
1024 old_ctx = ieee80211_vif_get_chanctx(sdata);
1026 if (WARN_ON(!sdata->reserved_ready))
1027 return -EBUSY;
1029 if (WARN_ON(!new_ctx))
1030 return -EINVAL;
1032 if (WARN_ON(!old_ctx))
1033 return -EINVAL;
1035 if (WARN_ON(new_ctx->replace_state ==
1036 IEEE80211_CHANCTX_REPLACES_OTHER))
1037 return -EINVAL;
1039 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1040 &sdata->reserved_chandef);
1041 if (WARN_ON(!chandef))
1042 return -EINVAL;
1044 ieee80211_change_chanctx(local, new_ctx, chandef);
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;
1050 list_del(&sdata->reserved_chanctx_list);
1051 sdata->reserved_chanctx = NULL;
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);
1059 goto out;
1062 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1063 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1065 if (sdata->vif.type == NL80211_IFTYPE_AP)
1066 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1068 ieee80211_check_fast_xmit_iface(sdata);
1070 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1071 ieee80211_free_chanctx(local, old_ctx);
1073 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
1074 changed = BSS_CHANGED_BANDWIDTH;
1076 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
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);
1082 if (changed)
1083 ieee80211_bss_info_change_notify(sdata, changed);
1085 out:
1086 ieee80211_vif_chanctx_reservation_complete(sdata);
1087 return err;
1090 static int
1091 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
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;
1098 old_ctx = ieee80211_vif_get_chanctx(sdata);
1099 new_ctx = sdata->reserved_chanctx;
1101 if (WARN_ON(!sdata->reserved_ready))
1102 return -EINVAL;
1104 if (WARN_ON(old_ctx))
1105 return -EINVAL;
1107 if (WARN_ON(!new_ctx))
1108 return -EINVAL;
1110 if (WARN_ON(new_ctx->replace_state ==
1111 IEEE80211_CHANCTX_REPLACES_OTHER))
1112 return -EINVAL;
1114 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1115 &sdata->reserved_chandef);
1116 if (WARN_ON(!chandef))
1117 return -EINVAL;
1119 ieee80211_change_chanctx(local, new_ctx, chandef);
1121 list_del(&sdata->reserved_chanctx_list);
1122 sdata->reserved_chanctx = NULL;
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);
1129 goto out;
1132 out:
1133 ieee80211_vif_chanctx_reservation_complete(sdata);
1134 return err;
1137 static bool
1138 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1140 struct ieee80211_chanctx *old_ctx, *new_ctx;
1142 lockdep_assert_held(&sdata->local->chanctx_mtx);
1144 new_ctx = sdata->reserved_chanctx;
1145 old_ctx = ieee80211_vif_get_chanctx(sdata);
1147 if (!old_ctx)
1148 return false;
1150 if (WARN_ON(!new_ctx))
1151 return false;
1153 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1154 return false;
1156 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1157 return false;
1159 return true;
1162 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1163 struct ieee80211_chanctx *new_ctx)
1165 const struct cfg80211_chan_def *chandef;
1167 lockdep_assert_held(&local->mtx);
1168 lockdep_assert_held(&local->chanctx_mtx);
1170 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1171 if (WARN_ON(!chandef))
1172 return -EINVAL;
1174 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1175 local->_oper_chandef = *chandef;
1176 ieee80211_hw_config(local, 0);
1178 return 0;
1181 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1182 int n_vifs)
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;
1189 lockdep_assert_held(&local->mtx);
1190 lockdep_assert_held(&local->chanctx_mtx);
1192 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1193 if (!vif_chsw)
1194 return -ENOMEM;
1196 i = 0;
1197 list_for_each_entry(ctx, &local->chanctx_list, list) {
1198 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1199 continue;
1201 if (WARN_ON(!ctx->replace_ctx)) {
1202 err = -EINVAL;
1203 goto out;
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;
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;
1217 i++;
1221 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1222 CHANCTX_SWMODE_SWAP_CONTEXTS);
1224 out:
1225 kfree(vif_chsw);
1226 return err;
1229 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1231 struct ieee80211_chanctx *ctx;
1232 int err;
1234 lockdep_assert_held(&local->mtx);
1235 lockdep_assert_held(&local->chanctx_mtx);
1237 list_for_each_entry(ctx, &local->chanctx_list, list) {
1238 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1239 continue;
1241 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1242 continue;
1244 ieee80211_del_chanctx(local, ctx->replace_ctx);
1245 err = ieee80211_add_chanctx(local, ctx);
1246 if (err)
1247 goto err;
1250 return 0;
1252 err:
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;
1258 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1259 continue;
1261 ieee80211_del_chanctx(local, ctx);
1262 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1265 return err;
1268 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
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;
1276 lockdep_assert_held(&local->mtx);
1277 lockdep_assert_held(&local->chanctx_mtx);
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.
1285 * For practical reasons and code simplicity just do a single huge
1286 * switch.
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
1295 list_for_each_entry(ctx, &local->chanctx_list, list) {
1296 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1297 continue;
1299 if (WARN_ON(!ctx->replace_ctx)) {
1300 err = -EINVAL;
1301 goto err;
1304 if (!local->use_chanctx)
1305 new_ctx = ctx;
1307 n_ctx++;
1309 n_assigned = 0;
1310 n_reserved = 0;
1311 n_ready = 0;
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++;
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;
1331 return -EAGAIN;
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;
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++;
1352 if (sdata->reserved_radar_required)
1353 ctx->conf.radar_enabled = true;
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;
1368 * All necessary vifs are ready. Perform the switch now depending on
1369 * reservations and driver capabilities.
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;
1379 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1380 err = ieee80211_chsw_switch_ctxs(local);
1381 if (err)
1382 goto err;
1384 } else {
1385 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1386 if (err)
1387 goto err;
1391 * Update all structures, values and pointers to point to new channel
1392 * context(s).
1395 i = 0;
1396 list_for_each_entry(ctx, &local->chanctx_list, list) {
1397 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1398 continue;
1400 if (WARN_ON(!ctx->replace_ctx)) {
1401 err = -EINVAL;
1402 goto err;
1405 list_for_each_entry(sdata, &ctx->reserved_vifs,
1406 reserved_chanctx_list) {
1407 u32 changed = 0;
1409 if (!ieee80211_vif_has_in_place_reservation(sdata))
1410 continue;
1412 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1414 if (sdata->vif.type == NL80211_IFTYPE_AP)
1415 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1416 false);
1418 ieee80211_check_fast_xmit_iface(sdata);
1420 sdata->radar_required = sdata->reserved_radar_required;
1422 if (sdata->vif.bss_conf.chandef.width !=
1423 sdata->reserved_chandef.width)
1424 changed = BSS_CHANGED_BANDWIDTH;
1426 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1427 if (changed)
1428 ieee80211_bss_info_change_notify(sdata,
1429 changed);
1431 ieee80211_recalc_txpower(sdata, false);
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);
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;
1444 list_del(&sdata->reserved_chanctx_list);
1445 list_move(&sdata->assigned_chanctx_list,
1446 &ctx->assigned_vifs);
1447 sdata->reserved_chanctx = NULL;
1449 ieee80211_vif_chanctx_reservation_complete(sdata);
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.
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;
1465 if (WARN_ON(sdata->reserved_chanctx != ctx))
1466 continue;
1468 if (!sdata->reserved_ready)
1469 continue;
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);
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);
1490 * Finally free old contexts
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;
1497 ctx->replace_ctx->replace_ctx = NULL;
1498 ctx->replace_ctx->replace_state =
1499 IEEE80211_CHANCTX_REPLACE_NONE;
1501 list_del_rcu(&ctx->list);
1502 kfree_rcu(ctx, rcu_head);
1505 return 0;
1507 err:
1508 list_for_each_entry(ctx, &local->chanctx_list, list) {
1509 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1510 continue;
1512 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1513 reserved_chanctx_list) {
1514 ieee80211_vif_unreserve_chanctx(sdata);
1515 ieee80211_vif_chanctx_reservation_complete(sdata);
1519 return err;
1522 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1524 struct ieee80211_local *local = sdata->local;
1525 struct ieee80211_chanctx_conf *conf;
1526 struct ieee80211_chanctx *ctx;
1527 bool use_reserved_switch = false;
1529 lockdep_assert_held(&local->chanctx_mtx);
1531 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1532 lockdep_is_held(&local->chanctx_mtx));
1533 if (!conf)
1534 return;
1536 ctx = container_of(conf, struct ieee80211_chanctx, conf);
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;
1545 ieee80211_vif_unreserve_chanctx(sdata);
1548 ieee80211_assign_vif_chanctx(sdata, NULL);
1549 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1550 ieee80211_free_chanctx(local, ctx);
1552 sdata->radar_required = false;
1554 /* Unreserving may ready an in-place reservation. */
1555 if (use_reserved_switch)
1556 ieee80211_vif_use_reserved_switch(local);
1559 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1560 const struct cfg80211_chan_def *chandef,
1561 enum ieee80211_chanctx_mode mode)
1563 struct ieee80211_local *local = sdata->local;
1564 struct ieee80211_chanctx *ctx;
1565 u8 radar_detect_width = 0;
1566 int ret;
1568 lockdep_assert_held(&local->mtx);
1570 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1572 mutex_lock(&local->chanctx_mtx);
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);
1582 sdata->radar_required = ret;
1584 ret = ieee80211_check_combinations(sdata, chandef, mode,
1585 radar_detect_width);
1586 if (ret < 0)
1587 goto out;
1589 __ieee80211_vif_release_channel(sdata);
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;
1599 ieee80211_vif_update_chandef(sdata, chandef);
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;
1609 ieee80211_recalc_smps_chanctx(local, ctx);
1610 ieee80211_recalc_radar_chanctx(local, ctx);
1611 out:
1612 if (ret)
1613 sdata->radar_required = false;
1615 mutex_unlock(&local->chanctx_mtx);
1616 return ret;
1619 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1621 struct ieee80211_local *local = sdata->local;
1622 struct ieee80211_chanctx *new_ctx;
1623 struct ieee80211_chanctx *old_ctx;
1624 int err;
1626 lockdep_assert_held(&local->mtx);
1627 lockdep_assert_held(&local->chanctx_mtx);
1629 new_ctx = sdata->reserved_chanctx;
1630 old_ctx = ieee80211_vif_get_chanctx(sdata);
1632 if (WARN_ON(!new_ctx))
1633 return -EINVAL;
1635 if (WARN_ON(new_ctx->replace_state ==
1636 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1637 return -EINVAL;
1639 if (WARN_ON(sdata->reserved_ready))
1640 return -EINVAL;
1642 sdata->reserved_ready = true;
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);
1650 if (err)
1651 return err;
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
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.
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;
1674 wiphy_info(local->hw.wiphy,
1675 "depending in-place reservation failed (err=%d)\n",
1676 err);
1680 return 0;
1683 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1684 const struct cfg80211_chan_def *chandef,
1685 u32 *changed)
1687 struct ieee80211_local *local = sdata->local;
1688 struct ieee80211_chanctx_conf *conf;
1689 struct ieee80211_chanctx *ctx;
1690 const struct cfg80211_chan_def *compat;
1691 int ret;
1693 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1694 IEEE80211_CHAN_DISABLED))
1695 return -EINVAL;
1697 mutex_lock(&local->chanctx_mtx);
1698 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1699 ret = 0;
1700 goto out;
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;
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;
1716 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1718 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1719 if (!compat) {
1720 ret = -EINVAL;
1721 goto out;
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;
1730 break;
1731 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1732 /* TODO: Perhaps the bandwidth change could be treated as a
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;
1744 ieee80211_vif_update_chandef(sdata, chandef);
1746 ieee80211_recalc_chanctx_chantype(local, ctx);
1748 *changed |= BSS_CHANGED_BANDWIDTH;
1749 ret = 0;
1750 out:
1751 mutex_unlock(&local->chanctx_mtx);
1752 return ret;
1755 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1757 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1759 lockdep_assert_held(&sdata->local->mtx);
1761 mutex_lock(&sdata->local->chanctx_mtx);
1762 __ieee80211_vif_release_channel(sdata);
1763 mutex_unlock(&sdata->local->chanctx_mtx);
1766 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1768 struct ieee80211_local *local = sdata->local;
1769 struct ieee80211_sub_if_data *ap;
1770 struct ieee80211_chanctx_conf *conf;
1772 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1773 return;
1775 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1777 mutex_lock(&local->chanctx_mtx);
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);
1785 void 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)
1792 struct ieee80211_local *local = hw_to_local(hw);
1793 struct ieee80211_chanctx *ctx;
1795 rcu_read_lock();
1796 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1797 if (ctx->driver_present)
1798 iter(hw, &ctx->conf, iter_data);
1799 rcu_read_unlock();
1801 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);