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 /* S1G doesn't have 20MHz, so get the correct width for the
542 if (chandef->chan->band == NL80211_BAND_S1GHZ)
544 ieee80211_s1g_channel_width(chandef->chan);
546 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
547 chandef->center_freq1 = chandef->chan->center_freq;
548 chandef->freq1_offset = chandef->chan->freq_offset;
549 chandef->center_freq2 = 0;
551 /* NOTE: Disabling radar is only valid here for
552 * single channel context. To be sure, check it ...
554 WARN_ON(local->hw.conf.radar_enabled &&
555 !list_empty(&local->chanctx_list));
557 local->hw.conf.radar_enabled = false;
559 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
561 drv_remove_chanctx(local, ctx);
564 ieee80211_recalc_idle(local);
567 static void ieee80211_free_chanctx(struct ieee80211_local *local,
568 struct ieee80211_chanctx *ctx)
570 lockdep_assert_held(&local->chanctx_mtx);
572 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
574 list_del_rcu(&ctx->list);
575 ieee80211_del_chanctx(local, ctx);
576 kfree_rcu(ctx, rcu_head);
579 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
580 struct ieee80211_chanctx *ctx)
582 struct ieee80211_chanctx_conf *conf = &ctx->conf;
583 struct ieee80211_sub_if_data *sdata;
584 const struct cfg80211_chan_def *compat = NULL;
585 struct sta_info *sta;
587 lockdep_assert_held(&local->chanctx_mtx);
590 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
592 if (!ieee80211_sdata_running(sdata))
594 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
596 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
600 compat = &sdata->vif.bss_conf.chandef;
602 compat = cfg80211_chandef_compatible(
603 &sdata->vif.bss_conf.chandef, compat);
604 if (WARN_ON_ONCE(!compat))
608 /* TDLS peers can sometimes affect the chandef width */
609 list_for_each_entry_rcu(sta, &local->sta_list, list) {
610 if (!sta->uploaded ||
611 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
612 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
613 !sta->tdls_chandef.chan)
616 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
618 if (WARN_ON_ONCE(!compat))
626 ieee80211_change_chanctx(local, ctx, compat);
629 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
630 struct ieee80211_chanctx *chanctx)
634 lockdep_assert_held(&local->chanctx_mtx);
635 /* for ieee80211_is_radar_required */
636 lockdep_assert_held(&local->mtx);
638 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
640 if (radar_enabled == chanctx->conf.radar_enabled)
643 chanctx->conf.radar_enabled = radar_enabled;
645 if (!local->use_chanctx) {
646 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
647 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
650 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
653 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
654 struct ieee80211_chanctx *new_ctx)
656 struct ieee80211_local *local = sdata->local;
657 struct ieee80211_chanctx_conf *conf;
658 struct ieee80211_chanctx *curr_ctx = NULL;
661 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
664 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
665 lockdep_is_held(&local->chanctx_mtx));
668 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
670 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
672 list_del(&sdata->assigned_chanctx_list);
676 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
680 conf = &new_ctx->conf;
681 list_add(&sdata->assigned_chanctx_list,
682 &new_ctx->assigned_vifs);
686 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
688 sdata->vif.bss_conf.idle = !conf;
690 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
691 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
692 ieee80211_recalc_smps_chanctx(local, curr_ctx);
693 ieee80211_recalc_radar_chanctx(local, curr_ctx);
694 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
697 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
698 ieee80211_recalc_txpower(sdata, false);
699 ieee80211_recalc_chanctx_min_def(local, new_ctx);
702 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
703 sdata->vif.type != NL80211_IFTYPE_MONITOR)
704 ieee80211_bss_info_change_notify(sdata,
707 ieee80211_check_fast_xmit_iface(sdata);
712 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
713 struct ieee80211_chanctx *chanctx)
715 struct ieee80211_sub_if_data *sdata;
716 u8 rx_chains_static, rx_chains_dynamic;
718 lockdep_assert_held(&local->chanctx_mtx);
720 rx_chains_static = 1;
721 rx_chains_dynamic = 1;
724 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
725 u8 needed_static, needed_dynamic;
727 if (!ieee80211_sdata_running(sdata))
730 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
734 switch (sdata->vif.type) {
735 case NL80211_IFTYPE_P2P_DEVICE:
736 case NL80211_IFTYPE_NAN:
738 case NL80211_IFTYPE_STATION:
739 if (!sdata->u.mgd.associated)
742 case NL80211_IFTYPE_AP_VLAN:
744 case NL80211_IFTYPE_AP:
745 case NL80211_IFTYPE_ADHOC:
746 case NL80211_IFTYPE_WDS:
747 case NL80211_IFTYPE_MESH_POINT:
748 case NL80211_IFTYPE_OCB:
754 switch (sdata->smps_mode) {
756 WARN_ONCE(1, "Invalid SMPS mode %d\n",
759 case IEEE80211_SMPS_OFF:
760 needed_static = sdata->needed_rx_chains;
761 needed_dynamic = sdata->needed_rx_chains;
763 case IEEE80211_SMPS_DYNAMIC:
765 needed_dynamic = sdata->needed_rx_chains;
767 case IEEE80211_SMPS_STATIC:
773 rx_chains_static = max(rx_chains_static, needed_static);
774 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
777 /* Disable SMPS for the monitor interface */
778 sdata = rcu_dereference(local->monitor_sdata);
780 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
781 rx_chains_dynamic = rx_chains_static = local->rx_chains;
785 if (!local->use_chanctx) {
786 if (rx_chains_static > 1)
787 local->smps_mode = IEEE80211_SMPS_OFF;
788 else if (rx_chains_dynamic > 1)
789 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
791 local->smps_mode = IEEE80211_SMPS_STATIC;
792 ieee80211_hw_config(local, 0);
795 if (rx_chains_static == chanctx->conf.rx_chains_static &&
796 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
799 chanctx->conf.rx_chains_static = rx_chains_static;
800 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
801 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
805 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
808 struct ieee80211_local *local __maybe_unused = sdata->local;
809 struct ieee80211_sub_if_data *vlan;
810 struct ieee80211_chanctx_conf *conf;
812 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
815 lockdep_assert_held(&local->mtx);
817 /* Check that conf exists, even when clearing this function
818 * must be called with the AP's channel context still there
819 * as it would otherwise cause VLANs to have an invalid
820 * channel context pointer for a while, possibly pointing
821 * to a channel context that has already been freed.
823 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
824 lockdep_is_held(&local->chanctx_mtx));
830 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
831 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
834 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
837 struct ieee80211_local *local = sdata->local;
839 mutex_lock(&local->chanctx_mtx);
841 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
843 mutex_unlock(&local->chanctx_mtx);
846 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
848 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
850 lockdep_assert_held(&sdata->local->chanctx_mtx);
855 list_del(&sdata->reserved_chanctx_list);
856 sdata->reserved_chanctx = NULL;
858 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
859 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
860 if (WARN_ON(!ctx->replace_ctx))
863 WARN_ON(ctx->replace_ctx->replace_state !=
864 IEEE80211_CHANCTX_WILL_BE_REPLACED);
865 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
867 ctx->replace_ctx->replace_ctx = NULL;
868 ctx->replace_ctx->replace_state =
869 IEEE80211_CHANCTX_REPLACE_NONE;
871 list_del_rcu(&ctx->list);
872 kfree_rcu(ctx, rcu_head);
874 ieee80211_free_chanctx(sdata->local, ctx);
881 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
882 const struct cfg80211_chan_def *chandef,
883 enum ieee80211_chanctx_mode mode,
886 struct ieee80211_local *local = sdata->local;
887 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
889 lockdep_assert_held(&local->chanctx_mtx);
891 curr_ctx = ieee80211_vif_get_chanctx(sdata);
892 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
895 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
897 if (ieee80211_can_create_new_chanctx(local)) {
898 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
900 return PTR_ERR(new_ctx);
903 (curr_ctx->replace_state ==
904 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
905 !list_empty(&curr_ctx->reserved_vifs)) {
907 * Another vif already requested this context
908 * for a reservation. Find another one hoping
909 * all vifs assigned to it will also switch
912 * TODO: This needs a little more work as some
913 * cases (more than 2 chanctx capable devices)
914 * may fail which could otherwise succeed
915 * provided some channel context juggling was
918 * Consider ctx1..3, vif1..6, each ctx has 2
919 * vifs. vif1 and vif2 from ctx1 request new
920 * different chandefs starting 2 in-place
921 * reserations with ctx4 and ctx5 replacing
922 * ctx1 and ctx2 respectively. Next vif5 and
923 * vif6 from ctx3 reserve ctx4. If vif3 and
924 * vif4 remain on ctx2 as they are then this
925 * fails unless `replace_ctx` from ctx5 is
926 * replaced with ctx3.
928 list_for_each_entry(ctx, &local->chanctx_list,
930 if (ctx->replace_state !=
931 IEEE80211_CHANCTX_REPLACE_NONE)
934 if (!list_empty(&ctx->reserved_vifs))
943 * If that's true then all available contexts already
944 * have reservations and cannot be used.
947 (curr_ctx->replace_state ==
948 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
949 !list_empty(&curr_ctx->reserved_vifs))
952 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
956 new_ctx->replace_ctx = curr_ctx;
957 new_ctx->replace_state =
958 IEEE80211_CHANCTX_REPLACES_OTHER;
960 curr_ctx->replace_ctx = new_ctx;
961 curr_ctx->replace_state =
962 IEEE80211_CHANCTX_WILL_BE_REPLACED;
964 list_add_rcu(&new_ctx->list, &local->chanctx_list);
968 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
969 sdata->reserved_chanctx = new_ctx;
970 sdata->reserved_chandef = *chandef;
971 sdata->reserved_radar_required = radar_required;
972 sdata->reserved_ready = false;
978 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
980 switch (sdata->vif.type) {
981 case NL80211_IFTYPE_ADHOC:
982 case NL80211_IFTYPE_AP:
983 case NL80211_IFTYPE_MESH_POINT:
984 case NL80211_IFTYPE_OCB:
985 ieee80211_queue_work(&sdata->local->hw,
986 &sdata->csa_finalize_work);
988 case NL80211_IFTYPE_STATION:
989 ieee80211_queue_work(&sdata->local->hw,
990 &sdata->u.mgd.chswitch_work);
992 case NL80211_IFTYPE_UNSPECIFIED:
993 case NL80211_IFTYPE_AP_VLAN:
994 case NL80211_IFTYPE_WDS:
995 case NL80211_IFTYPE_MONITOR:
996 case NL80211_IFTYPE_P2P_CLIENT:
997 case NL80211_IFTYPE_P2P_GO:
998 case NL80211_IFTYPE_P2P_DEVICE:
999 case NL80211_IFTYPE_NAN:
1000 case NUM_NL80211_IFTYPES:
1007 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
1008 const struct cfg80211_chan_def *chandef)
1010 struct ieee80211_sub_if_data *vlan;
1012 sdata->vif.bss_conf.chandef = *chandef;
1014 if (sdata->vif.type != NL80211_IFTYPE_AP)
1017 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1018 vlan->vif.bss_conf.chandef = *chandef;
1022 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
1024 struct ieee80211_local *local = sdata->local;
1025 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1026 struct ieee80211_chanctx *old_ctx, *new_ctx;
1027 const struct cfg80211_chan_def *chandef;
1031 lockdep_assert_held(&local->mtx);
1032 lockdep_assert_held(&local->chanctx_mtx);
1034 new_ctx = sdata->reserved_chanctx;
1035 old_ctx = ieee80211_vif_get_chanctx(sdata);
1037 if (WARN_ON(!sdata->reserved_ready))
1040 if (WARN_ON(!new_ctx))
1043 if (WARN_ON(!old_ctx))
1046 if (WARN_ON(new_ctx->replace_state ==
1047 IEEE80211_CHANCTX_REPLACES_OTHER))
1050 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1051 &sdata->reserved_chandef);
1052 if (WARN_ON(!chandef))
1055 ieee80211_change_chanctx(local, new_ctx, chandef);
1057 vif_chsw[0].vif = &sdata->vif;
1058 vif_chsw[0].old_ctx = &old_ctx->conf;
1059 vif_chsw[0].new_ctx = &new_ctx->conf;
1061 list_del(&sdata->reserved_chanctx_list);
1062 sdata->reserved_chanctx = NULL;
1064 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1065 CHANCTX_SWMODE_REASSIGN_VIF);
1067 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1068 ieee80211_free_chanctx(local, new_ctx);
1073 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1074 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1076 if (sdata->vif.type == NL80211_IFTYPE_AP)
1077 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1079 ieee80211_check_fast_xmit_iface(sdata);
1081 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1082 ieee80211_free_chanctx(local, old_ctx);
1084 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
1085 changed = BSS_CHANGED_BANDWIDTH;
1087 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1089 ieee80211_recalc_smps_chanctx(local, new_ctx);
1090 ieee80211_recalc_radar_chanctx(local, new_ctx);
1091 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1094 ieee80211_bss_info_change_notify(sdata, changed);
1097 ieee80211_vif_chanctx_reservation_complete(sdata);
1102 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1104 struct ieee80211_local *local = sdata->local;
1105 struct ieee80211_chanctx *old_ctx, *new_ctx;
1106 const struct cfg80211_chan_def *chandef;
1109 old_ctx = ieee80211_vif_get_chanctx(sdata);
1110 new_ctx = sdata->reserved_chanctx;
1112 if (WARN_ON(!sdata->reserved_ready))
1115 if (WARN_ON(old_ctx))
1118 if (WARN_ON(!new_ctx))
1121 if (WARN_ON(new_ctx->replace_state ==
1122 IEEE80211_CHANCTX_REPLACES_OTHER))
1125 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1126 &sdata->reserved_chandef);
1127 if (WARN_ON(!chandef))
1130 ieee80211_change_chanctx(local, new_ctx, chandef);
1132 list_del(&sdata->reserved_chanctx_list);
1133 sdata->reserved_chanctx = NULL;
1135 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1137 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1138 ieee80211_free_chanctx(local, new_ctx);
1144 ieee80211_vif_chanctx_reservation_complete(sdata);
1149 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1151 struct ieee80211_chanctx *old_ctx, *new_ctx;
1153 lockdep_assert_held(&sdata->local->chanctx_mtx);
1155 new_ctx = sdata->reserved_chanctx;
1156 old_ctx = ieee80211_vif_get_chanctx(sdata);
1161 if (WARN_ON(!new_ctx))
1164 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1167 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1173 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1174 struct ieee80211_chanctx *new_ctx)
1176 const struct cfg80211_chan_def *chandef;
1178 lockdep_assert_held(&local->mtx);
1179 lockdep_assert_held(&local->chanctx_mtx);
1181 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1182 if (WARN_ON(!chandef))
1185 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1186 local->_oper_chandef = *chandef;
1187 ieee80211_hw_config(local, 0);
1192 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1195 struct ieee80211_vif_chanctx_switch *vif_chsw;
1196 struct ieee80211_sub_if_data *sdata;
1197 struct ieee80211_chanctx *ctx, *old_ctx;
1200 lockdep_assert_held(&local->mtx);
1201 lockdep_assert_held(&local->chanctx_mtx);
1203 vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
1208 list_for_each_entry(ctx, &local->chanctx_list, list) {
1209 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1212 if (WARN_ON(!ctx->replace_ctx)) {
1217 list_for_each_entry(sdata, &ctx->reserved_vifs,
1218 reserved_chanctx_list) {
1219 if (!ieee80211_vif_has_in_place_reservation(
1223 old_ctx = ieee80211_vif_get_chanctx(sdata);
1224 vif_chsw[i].vif = &sdata->vif;
1225 vif_chsw[i].old_ctx = &old_ctx->conf;
1226 vif_chsw[i].new_ctx = &ctx->conf;
1232 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1233 CHANCTX_SWMODE_SWAP_CONTEXTS);
1240 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1242 struct ieee80211_chanctx *ctx;
1245 lockdep_assert_held(&local->mtx);
1246 lockdep_assert_held(&local->chanctx_mtx);
1248 list_for_each_entry(ctx, &local->chanctx_list, list) {
1249 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1252 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1255 ieee80211_del_chanctx(local, ctx->replace_ctx);
1256 err = ieee80211_add_chanctx(local, ctx);
1264 WARN_ON(ieee80211_add_chanctx(local, ctx));
1265 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1266 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1269 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1272 ieee80211_del_chanctx(local, ctx);
1273 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1279 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1281 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1282 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1283 struct ieee80211_chanctx *new_ctx = NULL;
1284 int err, n_assigned, n_reserved, n_ready;
1285 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1287 lockdep_assert_held(&local->mtx);
1288 lockdep_assert_held(&local->chanctx_mtx);
1291 * If there are 2 independent pairs of channel contexts performing
1292 * cross-switch of their vifs this code will still wait until both are
1293 * ready even though it could be possible to switch one before the
1296 * For practical reasons and code simplicity just do a single huge
1301 * Verify if the reservation is still feasible.
1302 * - if it's not then disconnect
1303 * - if it is but not all vifs necessary are ready then defer
1306 list_for_each_entry(ctx, &local->chanctx_list, list) {
1307 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1310 if (WARN_ON(!ctx->replace_ctx)) {
1315 if (!local->use_chanctx)
1324 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1325 assigned_chanctx_list) {
1327 if (sdata->reserved_chanctx) {
1329 if (sdata->reserved_ready)
1334 if (n_assigned != n_reserved) {
1335 if (n_ready == n_reserved) {
1336 wiphy_info(local->hw.wiphy,
1337 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1345 ctx->conf.radar_enabled = false;
1346 list_for_each_entry(sdata, &ctx->reserved_vifs,
1347 reserved_chanctx_list) {
1348 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1349 !sdata->reserved_ready)
1352 old_ctx = ieee80211_vif_get_chanctx(sdata);
1354 if (old_ctx->replace_state ==
1355 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1363 if (sdata->reserved_radar_required)
1364 ctx->conf.radar_enabled = true;
1368 if (WARN_ON(n_ctx == 0) ||
1369 WARN_ON(n_vifs_switch == 0 &&
1370 n_vifs_assign == 0 &&
1371 n_vifs_ctxless == 0) ||
1372 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1373 WARN_ON(!new_ctx && !local->use_chanctx)) {
1379 * All necessary vifs are ready. Perform the switch now depending on
1380 * reservations and driver capabilities.
1383 if (local->use_chanctx) {
1384 if (n_vifs_switch > 0) {
1385 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1390 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1391 err = ieee80211_chsw_switch_ctxs(local);
1396 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1402 * Update all structures, values and pointers to point to new channel
1405 list_for_each_entry(ctx, &local->chanctx_list, list) {
1406 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1409 if (WARN_ON(!ctx->replace_ctx)) {
1414 list_for_each_entry(sdata, &ctx->reserved_vifs,
1415 reserved_chanctx_list) {
1418 if (!ieee80211_vif_has_in_place_reservation(sdata))
1421 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1423 if (sdata->vif.type == NL80211_IFTYPE_AP)
1424 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1427 ieee80211_check_fast_xmit_iface(sdata);
1429 sdata->radar_required = sdata->reserved_radar_required;
1431 if (sdata->vif.bss_conf.chandef.width !=
1432 sdata->reserved_chandef.width)
1433 changed = BSS_CHANGED_BANDWIDTH;
1435 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1437 ieee80211_bss_info_change_notify(sdata,
1440 ieee80211_recalc_txpower(sdata, false);
1443 ieee80211_recalc_chanctx_chantype(local, ctx);
1444 ieee80211_recalc_smps_chanctx(local, ctx);
1445 ieee80211_recalc_radar_chanctx(local, ctx);
1446 ieee80211_recalc_chanctx_min_def(local, ctx);
1448 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1449 reserved_chanctx_list) {
1450 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1453 list_del(&sdata->reserved_chanctx_list);
1454 list_move(&sdata->assigned_chanctx_list,
1455 &ctx->assigned_vifs);
1456 sdata->reserved_chanctx = NULL;
1458 ieee80211_vif_chanctx_reservation_complete(sdata);
1462 * This context might have been a dependency for an already
1463 * ready re-assign reservation interface that was deferred. Do
1464 * not propagate error to the caller though. The in-place
1465 * reservation for originally requested interface has already
1466 * succeeded at this point.
1468 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1469 reserved_chanctx_list) {
1470 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1474 if (WARN_ON(sdata->reserved_chanctx != ctx))
1477 if (!sdata->reserved_ready)
1480 if (ieee80211_vif_get_chanctx(sdata))
1481 err = ieee80211_vif_use_reserved_reassign(
1484 err = ieee80211_vif_use_reserved_assign(sdata);
1488 "failed to finalize (re-)assign reservation (err=%d)\n",
1490 ieee80211_vif_unreserve_chanctx(sdata);
1491 cfg80211_stop_iface(local->hw.wiphy,
1499 * Finally free old contexts
1502 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1503 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1506 ctx->replace_ctx->replace_ctx = NULL;
1507 ctx->replace_ctx->replace_state =
1508 IEEE80211_CHANCTX_REPLACE_NONE;
1510 list_del_rcu(&ctx->list);
1511 kfree_rcu(ctx, rcu_head);
1517 list_for_each_entry(ctx, &local->chanctx_list, list) {
1518 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1521 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1522 reserved_chanctx_list) {
1523 ieee80211_vif_unreserve_chanctx(sdata);
1524 ieee80211_vif_chanctx_reservation_complete(sdata);
1531 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1533 struct ieee80211_local *local = sdata->local;
1534 struct ieee80211_chanctx_conf *conf;
1535 struct ieee80211_chanctx *ctx;
1536 bool use_reserved_switch = false;
1538 lockdep_assert_held(&local->chanctx_mtx);
1540 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1541 lockdep_is_held(&local->chanctx_mtx));
1545 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1547 if (sdata->reserved_chanctx) {
1548 if (sdata->reserved_chanctx->replace_state ==
1549 IEEE80211_CHANCTX_REPLACES_OTHER &&
1550 ieee80211_chanctx_num_reserved(local,
1551 sdata->reserved_chanctx) > 1)
1552 use_reserved_switch = true;
1554 ieee80211_vif_unreserve_chanctx(sdata);
1557 ieee80211_assign_vif_chanctx(sdata, NULL);
1558 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1559 ieee80211_free_chanctx(local, ctx);
1561 sdata->radar_required = false;
1563 /* Unreserving may ready an in-place reservation. */
1564 if (use_reserved_switch)
1565 ieee80211_vif_use_reserved_switch(local);
1568 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1569 const struct cfg80211_chan_def *chandef,
1570 enum ieee80211_chanctx_mode mode)
1572 struct ieee80211_local *local = sdata->local;
1573 struct ieee80211_chanctx *ctx;
1574 u8 radar_detect_width = 0;
1577 lockdep_assert_held(&local->mtx);
1579 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1581 mutex_lock(&local->chanctx_mtx);
1583 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1585 sdata->wdev.iftype);
1589 radar_detect_width = BIT(chandef->width);
1591 sdata->radar_required = ret;
1593 ret = ieee80211_check_combinations(sdata, chandef, mode,
1594 radar_detect_width);
1598 __ieee80211_vif_release_channel(sdata);
1600 ctx = ieee80211_find_chanctx(local, chandef, mode);
1602 ctx = ieee80211_new_chanctx(local, chandef, mode);
1608 ieee80211_vif_update_chandef(sdata, chandef);
1610 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1612 /* if assign fails refcount stays the same */
1613 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1614 ieee80211_free_chanctx(local, ctx);
1618 ieee80211_recalc_smps_chanctx(local, ctx);
1619 ieee80211_recalc_radar_chanctx(local, ctx);
1622 sdata->radar_required = false;
1624 mutex_unlock(&local->chanctx_mtx);
1628 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1630 struct ieee80211_local *local = sdata->local;
1631 struct ieee80211_chanctx *new_ctx;
1632 struct ieee80211_chanctx *old_ctx;
1635 lockdep_assert_held(&local->mtx);
1636 lockdep_assert_held(&local->chanctx_mtx);
1638 new_ctx = sdata->reserved_chanctx;
1639 old_ctx = ieee80211_vif_get_chanctx(sdata);
1641 if (WARN_ON(!new_ctx))
1644 if (WARN_ON(new_ctx->replace_state ==
1645 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1648 if (WARN_ON(sdata->reserved_ready))
1651 sdata->reserved_ready = true;
1653 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1655 err = ieee80211_vif_use_reserved_reassign(sdata);
1657 err = ieee80211_vif_use_reserved_assign(sdata);
1664 * In-place reservation may need to be finalized now either if:
1665 * a) sdata is taking part in the swapping itself and is the last one
1666 * b) sdata has switched with a re-assign reservation to an existing
1667 * context readying in-place switching of old_ctx
1669 * In case of (b) do not propagate the error up because the requested
1670 * sdata already switched successfully. Just spill an extra warning.
1671 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1672 * interfaces upon failure.
1675 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1676 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1677 err = ieee80211_vif_use_reserved_switch(local);
1678 if (err && err != -EAGAIN) {
1679 if (new_ctx->replace_state ==
1680 IEEE80211_CHANCTX_REPLACES_OTHER)
1683 wiphy_info(local->hw.wiphy,
1684 "depending in-place reservation failed (err=%d)\n",
1692 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1693 const struct cfg80211_chan_def *chandef,
1696 struct ieee80211_local *local = sdata->local;
1697 struct ieee80211_chanctx_conf *conf;
1698 struct ieee80211_chanctx *ctx;
1699 const struct cfg80211_chan_def *compat;
1702 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1703 IEEE80211_CHAN_DISABLED))
1706 mutex_lock(&local->chanctx_mtx);
1707 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1712 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1713 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1718 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1719 lockdep_is_held(&local->chanctx_mtx));
1725 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1727 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1733 switch (ctx->replace_state) {
1734 case IEEE80211_CHANCTX_REPLACE_NONE:
1735 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1740 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1741 /* TODO: Perhaps the bandwidth change could be treated as a
1742 * reservation itself? */
1745 case IEEE80211_CHANCTX_REPLACES_OTHER:
1746 /* channel context that is going to replace another channel
1747 * context doesn't really exist and shouldn't be assigned
1753 ieee80211_vif_update_chandef(sdata, chandef);
1755 ieee80211_recalc_chanctx_chantype(local, ctx);
1757 *changed |= BSS_CHANGED_BANDWIDTH;
1760 mutex_unlock(&local->chanctx_mtx);
1764 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1766 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1768 lockdep_assert_held(&sdata->local->mtx);
1770 mutex_lock(&sdata->local->chanctx_mtx);
1771 __ieee80211_vif_release_channel(sdata);
1772 mutex_unlock(&sdata->local->chanctx_mtx);
1775 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1777 struct ieee80211_local *local = sdata->local;
1778 struct ieee80211_sub_if_data *ap;
1779 struct ieee80211_chanctx_conf *conf;
1781 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1784 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1786 mutex_lock(&local->chanctx_mtx);
1788 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1789 lockdep_is_held(&local->chanctx_mtx));
1790 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1791 mutex_unlock(&local->chanctx_mtx);
1794 void ieee80211_iter_chan_contexts_atomic(
1795 struct ieee80211_hw *hw,
1796 void (*iter)(struct ieee80211_hw *hw,
1797 struct ieee80211_chanctx_conf *chanctx_conf,
1801 struct ieee80211_local *local = hw_to_local(hw);
1802 struct ieee80211_chanctx *ctx;
1805 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1806 if (ctx->driver_present)
1807 iter(hw, &ctx->conf, iter_data);
1810 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);