1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211 - channel management
6 #include <linux/nl80211.h>
7 #include <linux/export.h>
8 #include <linux/rtnetlink.h>
9 #include <net/cfg80211.h>
10 #include "ieee80211_i.h"
11 #include "driver-ops.h"
13 static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
14 struct ieee80211_chanctx *ctx)
16 struct ieee80211_sub_if_data *sdata;
19 lockdep_assert_held(&local->chanctx_mtx);
21 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
27 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
28 struct ieee80211_chanctx *ctx)
30 struct ieee80211_sub_if_data *sdata;
33 lockdep_assert_held(&local->chanctx_mtx);
35 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
41 int ieee80211_chanctx_refcount(struct ieee80211_local *local,
42 struct ieee80211_chanctx *ctx)
44 return ieee80211_chanctx_num_assigned(local, ctx) +
45 ieee80211_chanctx_num_reserved(local, ctx);
48 static int ieee80211_num_chanctx(struct ieee80211_local *local)
50 struct ieee80211_chanctx *ctx;
53 lockdep_assert_held(&local->chanctx_mtx);
55 list_for_each_entry(ctx, &local->chanctx_list, list)
61 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
63 lockdep_assert_held(&local->chanctx_mtx);
64 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
67 static struct ieee80211_chanctx *
68 ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
70 struct ieee80211_local *local __maybe_unused = sdata->local;
71 struct ieee80211_chanctx_conf *conf;
73 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
74 lockdep_is_held(&local->chanctx_mtx));
78 return container_of(conf, struct ieee80211_chanctx, conf);
81 static const struct cfg80211_chan_def *
82 ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
83 struct ieee80211_chanctx *ctx,
84 const struct cfg80211_chan_def *compat)
86 struct ieee80211_sub_if_data *sdata;
88 lockdep_assert_held(&local->chanctx_mtx);
90 list_for_each_entry(sdata, &ctx->reserved_vifs,
91 reserved_chanctx_list) {
93 compat = &sdata->reserved_chandef;
95 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
104 static const struct cfg80211_chan_def *
105 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
106 struct ieee80211_chanctx *ctx,
107 const struct cfg80211_chan_def *compat)
109 struct ieee80211_sub_if_data *sdata;
111 lockdep_assert_held(&local->chanctx_mtx);
113 list_for_each_entry(sdata, &ctx->assigned_vifs,
114 assigned_chanctx_list) {
115 if (sdata->reserved_chanctx != NULL)
119 compat = &sdata->vif.bss_conf.chandef;
121 compat = cfg80211_chandef_compatible(
122 &sdata->vif.bss_conf.chandef, compat);
130 static const struct cfg80211_chan_def *
131 ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
132 struct ieee80211_chanctx *ctx,
133 const struct cfg80211_chan_def *compat)
135 lockdep_assert_held(&local->chanctx_mtx);
137 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
141 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
149 ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
150 struct ieee80211_chanctx *ctx,
151 const struct cfg80211_chan_def *def)
153 lockdep_assert_held(&local->chanctx_mtx);
155 if (ieee80211_chanctx_combined_chandef(local, ctx, def))
158 if (!list_empty(&ctx->reserved_vifs) &&
159 ieee80211_chanctx_reserved_chandef(local, ctx, def))
165 static struct ieee80211_chanctx *
166 ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
167 const struct cfg80211_chan_def *chandef,
168 enum ieee80211_chanctx_mode mode)
170 struct ieee80211_chanctx *ctx;
172 lockdep_assert_held(&local->chanctx_mtx);
174 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
177 list_for_each_entry(ctx, &local->chanctx_list, list) {
178 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
181 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
184 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
194 enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
196 switch (sta->bandwidth) {
197 case IEEE80211_STA_RX_BW_20:
198 if (sta->ht_cap.ht_supported)
199 return NL80211_CHAN_WIDTH_20;
201 return NL80211_CHAN_WIDTH_20_NOHT;
202 case IEEE80211_STA_RX_BW_40:
203 return NL80211_CHAN_WIDTH_40;
204 case IEEE80211_STA_RX_BW_80:
205 return NL80211_CHAN_WIDTH_80;
206 case IEEE80211_STA_RX_BW_160:
208 * This applied for both 160 and 80+80. since we use
209 * the returned value to consider degradation of
210 * ctx->conf.min_def, we have to make sure to take
211 * the bigger one (NL80211_CHAN_WIDTH_160).
212 * Otherwise we might try degrading even when not
213 * needed, as the max required sta_bw returned (80+80)
214 * might be smaller than the configured bw (160).
216 return NL80211_CHAN_WIDTH_160;
219 return NL80211_CHAN_WIDTH_20;
223 static enum nl80211_chan_width
224 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
226 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
227 struct sta_info *sta;
230 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
231 if (sdata != sta->sdata &&
232 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
235 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
242 static enum nl80211_chan_width
243 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
244 struct ieee80211_chanctx_conf *conf)
246 struct ieee80211_sub_if_data *sdata;
247 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
250 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
251 struct ieee80211_vif *vif = &sdata->vif;
252 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
254 if (!ieee80211_sdata_running(sdata))
257 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
261 case NL80211_IFTYPE_AP:
262 case NL80211_IFTYPE_AP_VLAN:
263 width = ieee80211_get_max_required_bw(sdata);
265 case NL80211_IFTYPE_STATION:
267 * The ap's sta->bandwidth is not set yet at this
268 * point, so take the width from the chandef, but
269 * account also for TDLS peers
271 width = max(vif->bss_conf.chandef.width,
272 ieee80211_get_max_required_bw(sdata));
274 case NL80211_IFTYPE_P2P_DEVICE:
275 case NL80211_IFTYPE_NAN:
277 case NL80211_IFTYPE_ADHOC:
278 case NL80211_IFTYPE_WDS:
279 case NL80211_IFTYPE_MESH_POINT:
280 case NL80211_IFTYPE_OCB:
281 width = vif->bss_conf.chandef.width;
283 case NL80211_IFTYPE_UNSPECIFIED:
284 case NUM_NL80211_IFTYPES:
285 case NL80211_IFTYPE_MONITOR:
286 case NL80211_IFTYPE_P2P_CLIENT:
287 case NL80211_IFTYPE_P2P_GO:
290 max_bw = max(max_bw, width);
293 /* use the configured bandwidth in case of monitor interface */
294 sdata = rcu_dereference(local->monitor_sdata);
295 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
296 max_bw = max(max_bw, conf->def.width);
304 * recalc the min required chan width of the channel context, which is
305 * the max of min required widths of all the interfaces bound to this
308 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
309 struct ieee80211_chanctx *ctx)
311 enum nl80211_chan_width max_bw;
312 struct cfg80211_chan_def min_def;
314 lockdep_assert_held(&local->chanctx_mtx);
316 /* don't optimize non-20MHz based and radar_enabled confs */
317 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
318 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
319 ctx->conf.def.width == NL80211_CHAN_WIDTH_1 ||
320 ctx->conf.def.width == NL80211_CHAN_WIDTH_2 ||
321 ctx->conf.def.width == NL80211_CHAN_WIDTH_4 ||
322 ctx->conf.def.width == NL80211_CHAN_WIDTH_8 ||
323 ctx->conf.def.width == NL80211_CHAN_WIDTH_16 ||
324 ctx->conf.radar_enabled) {
325 ctx->conf.min_def = ctx->conf.def;
329 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
331 /* downgrade chandef up to max_bw */
332 min_def = ctx->conf.def;
333 while (min_def.width > max_bw)
334 ieee80211_chandef_downgrade(&min_def);
336 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
339 ctx->conf.min_def = min_def;
340 if (!ctx->driver_present)
343 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
346 static void ieee80211_change_chanctx(struct ieee80211_local *local,
347 struct ieee80211_chanctx *ctx,
348 const struct cfg80211_chan_def *chandef)
350 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
351 ieee80211_recalc_chanctx_min_def(local, ctx);
355 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
357 ctx->conf.def = *chandef;
358 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
359 ieee80211_recalc_chanctx_min_def(local, ctx);
361 if (!local->use_chanctx) {
362 local->_oper_chandef = *chandef;
363 ieee80211_hw_config(local, 0);
367 static struct ieee80211_chanctx *
368 ieee80211_find_chanctx(struct ieee80211_local *local,
369 const struct cfg80211_chan_def *chandef,
370 enum ieee80211_chanctx_mode mode)
372 struct ieee80211_chanctx *ctx;
374 lockdep_assert_held(&local->chanctx_mtx);
376 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
379 list_for_each_entry(ctx, &local->chanctx_list, list) {
380 const struct cfg80211_chan_def *compat;
382 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
385 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
388 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
392 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
397 ieee80211_change_chanctx(local, ctx, compat);
405 bool ieee80211_is_radar_required(struct ieee80211_local *local)
407 struct ieee80211_sub_if_data *sdata;
409 lockdep_assert_held(&local->mtx);
412 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
413 if (sdata->radar_required) {
424 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
425 struct ieee80211_chanctx *ctx)
427 struct ieee80211_chanctx_conf *conf = &ctx->conf;
428 struct ieee80211_sub_if_data *sdata;
429 bool required = false;
431 lockdep_assert_held(&local->chanctx_mtx);
432 lockdep_assert_held(&local->mtx);
435 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
436 if (!ieee80211_sdata_running(sdata))
438 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
440 if (!sdata->radar_required)
451 static struct ieee80211_chanctx *
452 ieee80211_alloc_chanctx(struct ieee80211_local *local,
453 const struct cfg80211_chan_def *chandef,
454 enum ieee80211_chanctx_mode mode)
456 struct ieee80211_chanctx *ctx;
458 lockdep_assert_held(&local->chanctx_mtx);
460 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
464 INIT_LIST_HEAD(&ctx->assigned_vifs);
465 INIT_LIST_HEAD(&ctx->reserved_vifs);
466 ctx->conf.def = *chandef;
467 ctx->conf.rx_chains_static = 1;
468 ctx->conf.rx_chains_dynamic = 1;
470 ctx->conf.radar_enabled = false;
471 ieee80211_recalc_chanctx_min_def(local, ctx);
476 static int ieee80211_add_chanctx(struct ieee80211_local *local,
477 struct ieee80211_chanctx *ctx)
482 lockdep_assert_held(&local->mtx);
483 lockdep_assert_held(&local->chanctx_mtx);
485 if (!local->use_chanctx)
486 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
488 /* turn idle off *before* setting channel -- some drivers need that */
489 changed = ieee80211_idle_off(local);
491 ieee80211_hw_config(local, changed);
493 if (!local->use_chanctx) {
494 local->_oper_chandef = ctx->conf.def;
495 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
497 err = drv_add_chanctx(local, ctx);
499 ieee80211_recalc_idle(local);
507 static struct ieee80211_chanctx *
508 ieee80211_new_chanctx(struct ieee80211_local *local,
509 const struct cfg80211_chan_def *chandef,
510 enum ieee80211_chanctx_mode mode)
512 struct ieee80211_chanctx *ctx;
515 lockdep_assert_held(&local->mtx);
516 lockdep_assert_held(&local->chanctx_mtx);
518 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
520 return ERR_PTR(-ENOMEM);
522 err = ieee80211_add_chanctx(local, ctx);
528 list_add_rcu(&ctx->list, &local->chanctx_list);
532 static void ieee80211_del_chanctx(struct ieee80211_local *local,
533 struct ieee80211_chanctx *ctx)
535 lockdep_assert_held(&local->chanctx_mtx);
537 if (!local->use_chanctx) {
538 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
539 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
540 chandef->center_freq1 = chandef->chan->center_freq;
541 chandef->freq1_offset = chandef->chan->freq_offset;
542 chandef->center_freq2 = 0;
544 /* NOTE: Disabling radar is only valid here for
545 * single channel context. To be sure, check it ...
547 WARN_ON(local->hw.conf.radar_enabled &&
548 !list_empty(&local->chanctx_list));
550 local->hw.conf.radar_enabled = false;
552 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
554 drv_remove_chanctx(local, ctx);
557 ieee80211_recalc_idle(local);
560 static void ieee80211_free_chanctx(struct ieee80211_local *local,
561 struct ieee80211_chanctx *ctx)
563 lockdep_assert_held(&local->chanctx_mtx);
565 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
567 list_del_rcu(&ctx->list);
568 ieee80211_del_chanctx(local, ctx);
569 kfree_rcu(ctx, rcu_head);
572 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
573 struct ieee80211_chanctx *ctx)
575 struct ieee80211_chanctx_conf *conf = &ctx->conf;
576 struct ieee80211_sub_if_data *sdata;
577 const struct cfg80211_chan_def *compat = NULL;
578 struct sta_info *sta;
580 lockdep_assert_held(&local->chanctx_mtx);
583 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
585 if (!ieee80211_sdata_running(sdata))
587 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
589 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
593 compat = &sdata->vif.bss_conf.chandef;
595 compat = cfg80211_chandef_compatible(
596 &sdata->vif.bss_conf.chandef, compat);
597 if (WARN_ON_ONCE(!compat))
601 /* TDLS peers can sometimes affect the chandef width */
602 list_for_each_entry_rcu(sta, &local->sta_list, list) {
603 if (!sta->uploaded ||
604 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
605 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
606 !sta->tdls_chandef.chan)
609 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
611 if (WARN_ON_ONCE(!compat))
619 ieee80211_change_chanctx(local, ctx, compat);
622 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
623 struct ieee80211_chanctx *chanctx)
627 lockdep_assert_held(&local->chanctx_mtx);
628 /* for ieee80211_is_radar_required */
629 lockdep_assert_held(&local->mtx);
631 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
633 if (radar_enabled == chanctx->conf.radar_enabled)
636 chanctx->conf.radar_enabled = radar_enabled;
638 if (!local->use_chanctx) {
639 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
640 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
643 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
646 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
647 struct ieee80211_chanctx *new_ctx)
649 struct ieee80211_local *local = sdata->local;
650 struct ieee80211_chanctx_conf *conf;
651 struct ieee80211_chanctx *curr_ctx = NULL;
654 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
657 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
658 lockdep_is_held(&local->chanctx_mtx));
661 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
663 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
665 list_del(&sdata->assigned_chanctx_list);
669 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
673 conf = &new_ctx->conf;
674 list_add(&sdata->assigned_chanctx_list,
675 &new_ctx->assigned_vifs);
679 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
681 sdata->vif.bss_conf.idle = !conf;
683 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
684 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
685 ieee80211_recalc_smps_chanctx(local, curr_ctx);
686 ieee80211_recalc_radar_chanctx(local, curr_ctx);
687 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
690 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
691 ieee80211_recalc_txpower(sdata, false);
692 ieee80211_recalc_chanctx_min_def(local, new_ctx);
695 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
696 sdata->vif.type != NL80211_IFTYPE_MONITOR)
697 ieee80211_bss_info_change_notify(sdata,
700 ieee80211_check_fast_xmit_iface(sdata);
705 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
706 struct ieee80211_chanctx *chanctx)
708 struct ieee80211_sub_if_data *sdata;
709 u8 rx_chains_static, rx_chains_dynamic;
711 lockdep_assert_held(&local->chanctx_mtx);
713 rx_chains_static = 1;
714 rx_chains_dynamic = 1;
717 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
718 u8 needed_static, needed_dynamic;
720 if (!ieee80211_sdata_running(sdata))
723 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
727 switch (sdata->vif.type) {
728 case NL80211_IFTYPE_P2P_DEVICE:
729 case NL80211_IFTYPE_NAN:
731 case NL80211_IFTYPE_STATION:
732 if (!sdata->u.mgd.associated)
735 case NL80211_IFTYPE_AP_VLAN:
737 case NL80211_IFTYPE_AP:
738 case NL80211_IFTYPE_ADHOC:
739 case NL80211_IFTYPE_WDS:
740 case NL80211_IFTYPE_MESH_POINT:
741 case NL80211_IFTYPE_OCB:
747 switch (sdata->smps_mode) {
749 WARN_ONCE(1, "Invalid SMPS mode %d\n",
752 case IEEE80211_SMPS_OFF:
753 needed_static = sdata->needed_rx_chains;
754 needed_dynamic = sdata->needed_rx_chains;
756 case IEEE80211_SMPS_DYNAMIC:
758 needed_dynamic = sdata->needed_rx_chains;
760 case IEEE80211_SMPS_STATIC:
766 rx_chains_static = max(rx_chains_static, needed_static);
767 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
770 /* Disable SMPS for the monitor interface */
771 sdata = rcu_dereference(local->monitor_sdata);
773 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
774 rx_chains_dynamic = rx_chains_static = local->rx_chains;
778 if (!local->use_chanctx) {
779 if (rx_chains_static > 1)
780 local->smps_mode = IEEE80211_SMPS_OFF;
781 else if (rx_chains_dynamic > 1)
782 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
784 local->smps_mode = IEEE80211_SMPS_STATIC;
785 ieee80211_hw_config(local, 0);
788 if (rx_chains_static == chanctx->conf.rx_chains_static &&
789 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
792 chanctx->conf.rx_chains_static = rx_chains_static;
793 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
794 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
798 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
801 struct ieee80211_local *local __maybe_unused = sdata->local;
802 struct ieee80211_sub_if_data *vlan;
803 struct ieee80211_chanctx_conf *conf;
805 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
808 lockdep_assert_held(&local->mtx);
810 /* Check that conf exists, even when clearing this function
811 * must be called with the AP's channel context still there
812 * as it would otherwise cause VLANs to have an invalid
813 * channel context pointer for a while, possibly pointing
814 * to a channel context that has already been freed.
816 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
817 lockdep_is_held(&local->chanctx_mtx));
823 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
824 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
827 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
830 struct ieee80211_local *local = sdata->local;
832 mutex_lock(&local->chanctx_mtx);
834 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
836 mutex_unlock(&local->chanctx_mtx);
839 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
841 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
843 lockdep_assert_held(&sdata->local->chanctx_mtx);
848 list_del(&sdata->reserved_chanctx_list);
849 sdata->reserved_chanctx = NULL;
851 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
852 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
853 if (WARN_ON(!ctx->replace_ctx))
856 WARN_ON(ctx->replace_ctx->replace_state !=
857 IEEE80211_CHANCTX_WILL_BE_REPLACED);
858 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
860 ctx->replace_ctx->replace_ctx = NULL;
861 ctx->replace_ctx->replace_state =
862 IEEE80211_CHANCTX_REPLACE_NONE;
864 list_del_rcu(&ctx->list);
865 kfree_rcu(ctx, rcu_head);
867 ieee80211_free_chanctx(sdata->local, ctx);
874 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
875 const struct cfg80211_chan_def *chandef,
876 enum ieee80211_chanctx_mode mode,
879 struct ieee80211_local *local = sdata->local;
880 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
882 lockdep_assert_held(&local->chanctx_mtx);
884 curr_ctx = ieee80211_vif_get_chanctx(sdata);
885 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
888 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
890 if (ieee80211_can_create_new_chanctx(local)) {
891 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
893 return PTR_ERR(new_ctx);
896 (curr_ctx->replace_state ==
897 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
898 !list_empty(&curr_ctx->reserved_vifs)) {
900 * Another vif already requested this context
901 * for a reservation. Find another one hoping
902 * all vifs assigned to it will also switch
905 * TODO: This needs a little more work as some
906 * cases (more than 2 chanctx capable devices)
907 * may fail which could otherwise succeed
908 * provided some channel context juggling was
911 * Consider ctx1..3, vif1..6, each ctx has 2
912 * vifs. vif1 and vif2 from ctx1 request new
913 * different chandefs starting 2 in-place
914 * reserations with ctx4 and ctx5 replacing
915 * ctx1 and ctx2 respectively. Next vif5 and
916 * vif6 from ctx3 reserve ctx4. If vif3 and
917 * vif4 remain on ctx2 as they are then this
918 * fails unless `replace_ctx` from ctx5 is
919 * replaced with ctx3.
921 list_for_each_entry(ctx, &local->chanctx_list,
923 if (ctx->replace_state !=
924 IEEE80211_CHANCTX_REPLACE_NONE)
927 if (!list_empty(&ctx->reserved_vifs))
936 * If that's true then all available contexts already
937 * have reservations and cannot be used.
940 (curr_ctx->replace_state ==
941 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
942 !list_empty(&curr_ctx->reserved_vifs))
945 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
949 new_ctx->replace_ctx = curr_ctx;
950 new_ctx->replace_state =
951 IEEE80211_CHANCTX_REPLACES_OTHER;
953 curr_ctx->replace_ctx = new_ctx;
954 curr_ctx->replace_state =
955 IEEE80211_CHANCTX_WILL_BE_REPLACED;
957 list_add_rcu(&new_ctx->list, &local->chanctx_list);
961 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
962 sdata->reserved_chanctx = new_ctx;
963 sdata->reserved_chandef = *chandef;
964 sdata->reserved_radar_required = radar_required;
965 sdata->reserved_ready = false;
971 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
973 switch (sdata->vif.type) {
974 case NL80211_IFTYPE_ADHOC:
975 case NL80211_IFTYPE_AP:
976 case NL80211_IFTYPE_MESH_POINT:
977 case NL80211_IFTYPE_OCB:
978 ieee80211_queue_work(&sdata->local->hw,
979 &sdata->csa_finalize_work);
981 case NL80211_IFTYPE_STATION:
982 ieee80211_queue_work(&sdata->local->hw,
983 &sdata->u.mgd.chswitch_work);
985 case NL80211_IFTYPE_UNSPECIFIED:
986 case NL80211_IFTYPE_AP_VLAN:
987 case NL80211_IFTYPE_WDS:
988 case NL80211_IFTYPE_MONITOR:
989 case NL80211_IFTYPE_P2P_CLIENT:
990 case NL80211_IFTYPE_P2P_GO:
991 case NL80211_IFTYPE_P2P_DEVICE:
992 case NL80211_IFTYPE_NAN:
993 case NUM_NL80211_IFTYPES:
1000 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
1001 const struct cfg80211_chan_def *chandef)
1003 struct ieee80211_sub_if_data *vlan;
1005 sdata->vif.bss_conf.chandef = *chandef;
1007 if (sdata->vif.type != NL80211_IFTYPE_AP)
1010 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1011 vlan->vif.bss_conf.chandef = *chandef;
1015 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
1017 struct ieee80211_local *local = sdata->local;
1018 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1019 struct ieee80211_chanctx *old_ctx, *new_ctx;
1020 const struct cfg80211_chan_def *chandef;
1024 lockdep_assert_held(&local->mtx);
1025 lockdep_assert_held(&local->chanctx_mtx);
1027 new_ctx = sdata->reserved_chanctx;
1028 old_ctx = ieee80211_vif_get_chanctx(sdata);
1030 if (WARN_ON(!sdata->reserved_ready))
1033 if (WARN_ON(!new_ctx))
1036 if (WARN_ON(!old_ctx))
1039 if (WARN_ON(new_ctx->replace_state ==
1040 IEEE80211_CHANCTX_REPLACES_OTHER))
1043 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1044 &sdata->reserved_chandef);
1045 if (WARN_ON(!chandef))
1048 ieee80211_change_chanctx(local, new_ctx, chandef);
1050 vif_chsw[0].vif = &sdata->vif;
1051 vif_chsw[0].old_ctx = &old_ctx->conf;
1052 vif_chsw[0].new_ctx = &new_ctx->conf;
1054 list_del(&sdata->reserved_chanctx_list);
1055 sdata->reserved_chanctx = NULL;
1057 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1058 CHANCTX_SWMODE_REASSIGN_VIF);
1060 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1061 ieee80211_free_chanctx(local, new_ctx);
1066 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1067 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1069 if (sdata->vif.type == NL80211_IFTYPE_AP)
1070 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1072 ieee80211_check_fast_xmit_iface(sdata);
1074 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1075 ieee80211_free_chanctx(local, old_ctx);
1077 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
1078 changed = BSS_CHANGED_BANDWIDTH;
1080 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1082 ieee80211_recalc_smps_chanctx(local, new_ctx);
1083 ieee80211_recalc_radar_chanctx(local, new_ctx);
1084 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1087 ieee80211_bss_info_change_notify(sdata, changed);
1090 ieee80211_vif_chanctx_reservation_complete(sdata);
1095 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1097 struct ieee80211_local *local = sdata->local;
1098 struct ieee80211_chanctx *old_ctx, *new_ctx;
1099 const struct cfg80211_chan_def *chandef;
1102 old_ctx = ieee80211_vif_get_chanctx(sdata);
1103 new_ctx = sdata->reserved_chanctx;
1105 if (WARN_ON(!sdata->reserved_ready))
1108 if (WARN_ON(old_ctx))
1111 if (WARN_ON(!new_ctx))
1114 if (WARN_ON(new_ctx->replace_state ==
1115 IEEE80211_CHANCTX_REPLACES_OTHER))
1118 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1119 &sdata->reserved_chandef);
1120 if (WARN_ON(!chandef))
1123 ieee80211_change_chanctx(local, new_ctx, chandef);
1125 list_del(&sdata->reserved_chanctx_list);
1126 sdata->reserved_chanctx = NULL;
1128 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1130 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1131 ieee80211_free_chanctx(local, new_ctx);
1137 ieee80211_vif_chanctx_reservation_complete(sdata);
1142 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1144 struct ieee80211_chanctx *old_ctx, *new_ctx;
1146 lockdep_assert_held(&sdata->local->chanctx_mtx);
1148 new_ctx = sdata->reserved_chanctx;
1149 old_ctx = ieee80211_vif_get_chanctx(sdata);
1154 if (WARN_ON(!new_ctx))
1157 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1160 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1166 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1167 struct ieee80211_chanctx *new_ctx)
1169 const struct cfg80211_chan_def *chandef;
1171 lockdep_assert_held(&local->mtx);
1172 lockdep_assert_held(&local->chanctx_mtx);
1174 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1175 if (WARN_ON(!chandef))
1178 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1179 local->_oper_chandef = *chandef;
1180 ieee80211_hw_config(local, 0);
1185 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1188 struct ieee80211_vif_chanctx_switch *vif_chsw;
1189 struct ieee80211_sub_if_data *sdata;
1190 struct ieee80211_chanctx *ctx, *old_ctx;
1193 lockdep_assert_held(&local->mtx);
1194 lockdep_assert_held(&local->chanctx_mtx);
1196 vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
1201 list_for_each_entry(ctx, &local->chanctx_list, list) {
1202 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1205 if (WARN_ON(!ctx->replace_ctx)) {
1210 list_for_each_entry(sdata, &ctx->reserved_vifs,
1211 reserved_chanctx_list) {
1212 if (!ieee80211_vif_has_in_place_reservation(
1216 old_ctx = ieee80211_vif_get_chanctx(sdata);
1217 vif_chsw[i].vif = &sdata->vif;
1218 vif_chsw[i].old_ctx = &old_ctx->conf;
1219 vif_chsw[i].new_ctx = &ctx->conf;
1225 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1226 CHANCTX_SWMODE_SWAP_CONTEXTS);
1233 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1235 struct ieee80211_chanctx *ctx;
1238 lockdep_assert_held(&local->mtx);
1239 lockdep_assert_held(&local->chanctx_mtx);
1241 list_for_each_entry(ctx, &local->chanctx_list, list) {
1242 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1245 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1248 ieee80211_del_chanctx(local, ctx->replace_ctx);
1249 err = ieee80211_add_chanctx(local, ctx);
1257 WARN_ON(ieee80211_add_chanctx(local, ctx));
1258 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1259 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1262 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1265 ieee80211_del_chanctx(local, ctx);
1266 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1272 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1274 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1275 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1276 struct ieee80211_chanctx *new_ctx = NULL;
1277 int err, n_assigned, n_reserved, n_ready;
1278 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1280 lockdep_assert_held(&local->mtx);
1281 lockdep_assert_held(&local->chanctx_mtx);
1284 * If there are 2 independent pairs of channel contexts performing
1285 * cross-switch of their vifs this code will still wait until both are
1286 * ready even though it could be possible to switch one before the
1289 * For practical reasons and code simplicity just do a single huge
1294 * Verify if the reservation is still feasible.
1295 * - if it's not then disconnect
1296 * - if it is but not all vifs necessary are ready then defer
1299 list_for_each_entry(ctx, &local->chanctx_list, list) {
1300 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1303 if (WARN_ON(!ctx->replace_ctx)) {
1308 if (!local->use_chanctx)
1317 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1318 assigned_chanctx_list) {
1320 if (sdata->reserved_chanctx) {
1322 if (sdata->reserved_ready)
1327 if (n_assigned != n_reserved) {
1328 if (n_ready == n_reserved) {
1329 wiphy_info(local->hw.wiphy,
1330 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1338 ctx->conf.radar_enabled = false;
1339 list_for_each_entry(sdata, &ctx->reserved_vifs,
1340 reserved_chanctx_list) {
1341 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1342 !sdata->reserved_ready)
1345 old_ctx = ieee80211_vif_get_chanctx(sdata);
1347 if (old_ctx->replace_state ==
1348 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1356 if (sdata->reserved_radar_required)
1357 ctx->conf.radar_enabled = true;
1361 if (WARN_ON(n_ctx == 0) ||
1362 WARN_ON(n_vifs_switch == 0 &&
1363 n_vifs_assign == 0 &&
1364 n_vifs_ctxless == 0) ||
1365 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1366 WARN_ON(!new_ctx && !local->use_chanctx)) {
1372 * All necessary vifs are ready. Perform the switch now depending on
1373 * reservations and driver capabilities.
1376 if (local->use_chanctx) {
1377 if (n_vifs_switch > 0) {
1378 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1383 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1384 err = ieee80211_chsw_switch_ctxs(local);
1389 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1395 * Update all structures, values and pointers to point to new channel
1398 list_for_each_entry(ctx, &local->chanctx_list, list) {
1399 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1402 if (WARN_ON(!ctx->replace_ctx)) {
1407 list_for_each_entry(sdata, &ctx->reserved_vifs,
1408 reserved_chanctx_list) {
1411 if (!ieee80211_vif_has_in_place_reservation(sdata))
1414 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1416 if (sdata->vif.type == NL80211_IFTYPE_AP)
1417 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1420 ieee80211_check_fast_xmit_iface(sdata);
1422 sdata->radar_required = sdata->reserved_radar_required;
1424 if (sdata->vif.bss_conf.chandef.width !=
1425 sdata->reserved_chandef.width)
1426 changed = BSS_CHANGED_BANDWIDTH;
1428 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1430 ieee80211_bss_info_change_notify(sdata,
1433 ieee80211_recalc_txpower(sdata, false);
1436 ieee80211_recalc_chanctx_chantype(local, ctx);
1437 ieee80211_recalc_smps_chanctx(local, ctx);
1438 ieee80211_recalc_radar_chanctx(local, ctx);
1439 ieee80211_recalc_chanctx_min_def(local, ctx);
1441 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1442 reserved_chanctx_list) {
1443 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1446 list_del(&sdata->reserved_chanctx_list);
1447 list_move(&sdata->assigned_chanctx_list,
1448 &ctx->assigned_vifs);
1449 sdata->reserved_chanctx = NULL;
1451 ieee80211_vif_chanctx_reservation_complete(sdata);
1455 * This context might have been a dependency for an already
1456 * ready re-assign reservation interface that was deferred. Do
1457 * not propagate error to the caller though. The in-place
1458 * reservation for originally requested interface has already
1459 * succeeded at this point.
1461 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1462 reserved_chanctx_list) {
1463 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1467 if (WARN_ON(sdata->reserved_chanctx != ctx))
1470 if (!sdata->reserved_ready)
1473 if (ieee80211_vif_get_chanctx(sdata))
1474 err = ieee80211_vif_use_reserved_reassign(
1477 err = ieee80211_vif_use_reserved_assign(sdata);
1481 "failed to finalize (re-)assign reservation (err=%d)\n",
1483 ieee80211_vif_unreserve_chanctx(sdata);
1484 cfg80211_stop_iface(local->hw.wiphy,
1492 * Finally free old contexts
1495 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1496 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1499 ctx->replace_ctx->replace_ctx = NULL;
1500 ctx->replace_ctx->replace_state =
1501 IEEE80211_CHANCTX_REPLACE_NONE;
1503 list_del_rcu(&ctx->list);
1504 kfree_rcu(ctx, rcu_head);
1510 list_for_each_entry(ctx, &local->chanctx_list, list) {
1511 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1514 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1515 reserved_chanctx_list) {
1516 ieee80211_vif_unreserve_chanctx(sdata);
1517 ieee80211_vif_chanctx_reservation_complete(sdata);
1524 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1526 struct ieee80211_local *local = sdata->local;
1527 struct ieee80211_chanctx_conf *conf;
1528 struct ieee80211_chanctx *ctx;
1529 bool use_reserved_switch = false;
1531 lockdep_assert_held(&local->chanctx_mtx);
1533 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1534 lockdep_is_held(&local->chanctx_mtx));
1538 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1540 if (sdata->reserved_chanctx) {
1541 if (sdata->reserved_chanctx->replace_state ==
1542 IEEE80211_CHANCTX_REPLACES_OTHER &&
1543 ieee80211_chanctx_num_reserved(local,
1544 sdata->reserved_chanctx) > 1)
1545 use_reserved_switch = true;
1547 ieee80211_vif_unreserve_chanctx(sdata);
1550 ieee80211_assign_vif_chanctx(sdata, NULL);
1551 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1552 ieee80211_free_chanctx(local, ctx);
1554 sdata->radar_required = false;
1556 /* Unreserving may ready an in-place reservation. */
1557 if (use_reserved_switch)
1558 ieee80211_vif_use_reserved_switch(local);
1561 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1562 const struct cfg80211_chan_def *chandef,
1563 enum ieee80211_chanctx_mode mode)
1565 struct ieee80211_local *local = sdata->local;
1566 struct ieee80211_chanctx *ctx;
1567 u8 radar_detect_width = 0;
1570 lockdep_assert_held(&local->mtx);
1572 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1574 mutex_lock(&local->chanctx_mtx);
1576 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1578 sdata->wdev.iftype);
1582 radar_detect_width = BIT(chandef->width);
1584 sdata->radar_required = ret;
1586 ret = ieee80211_check_combinations(sdata, chandef, mode,
1587 radar_detect_width);
1591 __ieee80211_vif_release_channel(sdata);
1593 ctx = ieee80211_find_chanctx(local, chandef, mode);
1595 ctx = ieee80211_new_chanctx(local, chandef, mode);
1601 ieee80211_vif_update_chandef(sdata, chandef);
1603 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1605 /* if assign fails refcount stays the same */
1606 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1607 ieee80211_free_chanctx(local, ctx);
1611 ieee80211_recalc_smps_chanctx(local, ctx);
1612 ieee80211_recalc_radar_chanctx(local, ctx);
1615 sdata->radar_required = false;
1617 mutex_unlock(&local->chanctx_mtx);
1621 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1623 struct ieee80211_local *local = sdata->local;
1624 struct ieee80211_chanctx *new_ctx;
1625 struct ieee80211_chanctx *old_ctx;
1628 lockdep_assert_held(&local->mtx);
1629 lockdep_assert_held(&local->chanctx_mtx);
1631 new_ctx = sdata->reserved_chanctx;
1632 old_ctx = ieee80211_vif_get_chanctx(sdata);
1634 if (WARN_ON(!new_ctx))
1637 if (WARN_ON(new_ctx->replace_state ==
1638 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1641 if (WARN_ON(sdata->reserved_ready))
1644 sdata->reserved_ready = true;
1646 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1648 err = ieee80211_vif_use_reserved_reassign(sdata);
1650 err = ieee80211_vif_use_reserved_assign(sdata);
1657 * In-place reservation may need to be finalized now either if:
1658 * a) sdata is taking part in the swapping itself and is the last one
1659 * b) sdata has switched with a re-assign reservation to an existing
1660 * context readying in-place switching of old_ctx
1662 * In case of (b) do not propagate the error up because the requested
1663 * sdata already switched successfully. Just spill an extra warning.
1664 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1665 * interfaces upon failure.
1668 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1669 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1670 err = ieee80211_vif_use_reserved_switch(local);
1671 if (err && err != -EAGAIN) {
1672 if (new_ctx->replace_state ==
1673 IEEE80211_CHANCTX_REPLACES_OTHER)
1676 wiphy_info(local->hw.wiphy,
1677 "depending in-place reservation failed (err=%d)\n",
1685 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1686 const struct cfg80211_chan_def *chandef,
1689 struct ieee80211_local *local = sdata->local;
1690 struct ieee80211_chanctx_conf *conf;
1691 struct ieee80211_chanctx *ctx;
1692 const struct cfg80211_chan_def *compat;
1695 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1696 IEEE80211_CHAN_DISABLED))
1699 mutex_lock(&local->chanctx_mtx);
1700 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1705 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1706 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1711 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1712 lockdep_is_held(&local->chanctx_mtx));
1718 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1720 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1726 switch (ctx->replace_state) {
1727 case IEEE80211_CHANCTX_REPLACE_NONE:
1728 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1733 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1734 /* TODO: Perhaps the bandwidth change could be treated as a
1735 * reservation itself? */
1738 case IEEE80211_CHANCTX_REPLACES_OTHER:
1739 /* channel context that is going to replace another channel
1740 * context doesn't really exist and shouldn't be assigned
1746 ieee80211_vif_update_chandef(sdata, chandef);
1748 ieee80211_recalc_chanctx_chantype(local, ctx);
1750 *changed |= BSS_CHANGED_BANDWIDTH;
1753 mutex_unlock(&local->chanctx_mtx);
1757 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1759 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1761 lockdep_assert_held(&sdata->local->mtx);
1763 mutex_lock(&sdata->local->chanctx_mtx);
1764 __ieee80211_vif_release_channel(sdata);
1765 mutex_unlock(&sdata->local->chanctx_mtx);
1768 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1770 struct ieee80211_local *local = sdata->local;
1771 struct ieee80211_sub_if_data *ap;
1772 struct ieee80211_chanctx_conf *conf;
1774 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1777 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1779 mutex_lock(&local->chanctx_mtx);
1781 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1782 lockdep_is_held(&local->chanctx_mtx));
1783 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1784 mutex_unlock(&local->chanctx_mtx);
1787 void ieee80211_iter_chan_contexts_atomic(
1788 struct ieee80211_hw *hw,
1789 void (*iter)(struct ieee80211_hw *hw,
1790 struct ieee80211_chanctx_conf *chanctx_conf,
1794 struct ieee80211_local *local = hw_to_local(hw);
1795 struct ieee80211_chanctx *ctx;
1798 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1799 if (ctx->driver_present)
1800 iter(hw, &ctx->conf, iter_data);
1803 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);