1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211 - channel management
4 * Copyright 2020 - 2021 Intel Corporation
7 #include <linux/nl80211.h>
8 #include <linux/export.h>
9 #include <linux/rtnetlink.h>
10 #include <net/cfg80211.h>
11 #include "ieee80211_i.h"
12 #include "driver-ops.h"
15 static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
16 struct ieee80211_chanctx *ctx)
18 struct ieee80211_sub_if_data *sdata;
21 lockdep_assert_held(&local->chanctx_mtx);
23 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
29 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
30 struct ieee80211_chanctx *ctx)
32 struct ieee80211_sub_if_data *sdata;
35 lockdep_assert_held(&local->chanctx_mtx);
37 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
43 int ieee80211_chanctx_refcount(struct ieee80211_local *local,
44 struct ieee80211_chanctx *ctx)
46 return ieee80211_chanctx_num_assigned(local, ctx) +
47 ieee80211_chanctx_num_reserved(local, ctx);
50 static int ieee80211_num_chanctx(struct ieee80211_local *local)
52 struct ieee80211_chanctx *ctx;
55 lockdep_assert_held(&local->chanctx_mtx);
57 list_for_each_entry(ctx, &local->chanctx_list, list)
63 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
65 lockdep_assert_held(&local->chanctx_mtx);
66 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
69 static struct ieee80211_chanctx *
70 ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
72 struct ieee80211_local *local __maybe_unused = sdata->local;
73 struct ieee80211_chanctx_conf *conf;
75 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
76 lockdep_is_held(&local->chanctx_mtx));
80 return container_of(conf, struct ieee80211_chanctx, conf);
83 static const struct cfg80211_chan_def *
84 ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
85 struct ieee80211_chanctx *ctx,
86 const struct cfg80211_chan_def *compat)
88 struct ieee80211_sub_if_data *sdata;
90 lockdep_assert_held(&local->chanctx_mtx);
92 list_for_each_entry(sdata, &ctx->reserved_vifs,
93 reserved_chanctx_list) {
95 compat = &sdata->reserved_chandef;
97 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
106 static const struct cfg80211_chan_def *
107 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
108 struct ieee80211_chanctx *ctx,
109 const struct cfg80211_chan_def *compat)
111 struct ieee80211_sub_if_data *sdata;
113 lockdep_assert_held(&local->chanctx_mtx);
115 list_for_each_entry(sdata, &ctx->assigned_vifs,
116 assigned_chanctx_list) {
117 if (sdata->reserved_chanctx != NULL)
121 compat = &sdata->vif.bss_conf.chandef;
123 compat = cfg80211_chandef_compatible(
124 &sdata->vif.bss_conf.chandef, compat);
132 static const struct cfg80211_chan_def *
133 ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
134 struct ieee80211_chanctx *ctx,
135 const struct cfg80211_chan_def *compat)
137 lockdep_assert_held(&local->chanctx_mtx);
139 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
143 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
151 ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
152 struct ieee80211_chanctx *ctx,
153 const struct cfg80211_chan_def *def)
155 lockdep_assert_held(&local->chanctx_mtx);
157 if (ieee80211_chanctx_combined_chandef(local, ctx, def))
160 if (!list_empty(&ctx->reserved_vifs) &&
161 ieee80211_chanctx_reserved_chandef(local, ctx, def))
167 static struct ieee80211_chanctx *
168 ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
169 const struct cfg80211_chan_def *chandef,
170 enum ieee80211_chanctx_mode mode)
172 struct ieee80211_chanctx *ctx;
174 lockdep_assert_held(&local->chanctx_mtx);
176 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
179 list_for_each_entry(ctx, &local->chanctx_list, list) {
180 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
183 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
186 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
196 static enum nl80211_chan_width ieee80211_get_sta_bw(struct sta_info *sta)
198 enum ieee80211_sta_rx_bandwidth width = ieee80211_sta_cap_rx_bw(sta);
201 case IEEE80211_STA_RX_BW_20:
202 if (sta->sta.ht_cap.ht_supported)
203 return NL80211_CHAN_WIDTH_20;
205 return NL80211_CHAN_WIDTH_20_NOHT;
206 case IEEE80211_STA_RX_BW_40:
207 return NL80211_CHAN_WIDTH_40;
208 case IEEE80211_STA_RX_BW_80:
209 return NL80211_CHAN_WIDTH_80;
210 case IEEE80211_STA_RX_BW_160:
212 * This applied for both 160 and 80+80. since we use
213 * the returned value to consider degradation of
214 * ctx->conf.min_def, we have to make sure to take
215 * the bigger one (NL80211_CHAN_WIDTH_160).
216 * Otherwise we might try degrading even when not
217 * needed, as the max required sta_bw returned (80+80)
218 * might be smaller than the configured bw (160).
220 return NL80211_CHAN_WIDTH_160;
221 case IEEE80211_STA_RX_BW_320:
222 return NL80211_CHAN_WIDTH_320;
225 return NL80211_CHAN_WIDTH_20;
229 static enum nl80211_chan_width
230 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
232 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
233 struct sta_info *sta;
236 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
237 if (sdata != sta->sdata &&
238 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
241 max_bw = max(max_bw, ieee80211_get_sta_bw(sta));
248 static enum nl80211_chan_width
249 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
250 struct ieee80211_chanctx_conf *conf)
252 struct ieee80211_sub_if_data *sdata;
253 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
256 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
257 struct ieee80211_vif *vif = &sdata->vif;
258 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
260 if (!ieee80211_sdata_running(sdata))
263 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
267 case NL80211_IFTYPE_AP:
268 case NL80211_IFTYPE_AP_VLAN:
269 width = ieee80211_get_max_required_bw(sdata);
271 case NL80211_IFTYPE_STATION:
273 * The ap's sta->bandwidth is not set yet at this
274 * point, so take the width from the chandef, but
275 * account also for TDLS peers
277 width = max(vif->bss_conf.chandef.width,
278 ieee80211_get_max_required_bw(sdata));
280 case NL80211_IFTYPE_P2P_DEVICE:
281 case NL80211_IFTYPE_NAN:
283 case NL80211_IFTYPE_ADHOC:
284 case NL80211_IFTYPE_MESH_POINT:
285 case NL80211_IFTYPE_OCB:
286 width = vif->bss_conf.chandef.width;
288 case NL80211_IFTYPE_WDS:
289 case NL80211_IFTYPE_UNSPECIFIED:
290 case NUM_NL80211_IFTYPES:
291 case NL80211_IFTYPE_MONITOR:
292 case NL80211_IFTYPE_P2P_CLIENT:
293 case NL80211_IFTYPE_P2P_GO:
296 max_bw = max(max_bw, width);
299 /* use the configured bandwidth in case of monitor interface */
300 sdata = rcu_dereference(local->monitor_sdata);
301 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
302 max_bw = max(max_bw, conf->def.width);
310 * recalc the min required chan width of the channel context, which is
311 * the max of min required widths of all the interfaces bound to this
314 static u32 _ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
315 struct ieee80211_chanctx *ctx)
317 enum nl80211_chan_width max_bw;
318 struct cfg80211_chan_def min_def;
320 lockdep_assert_held(&local->chanctx_mtx);
322 /* don't optimize non-20MHz based and radar_enabled confs */
323 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
324 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
325 ctx->conf.def.width == NL80211_CHAN_WIDTH_1 ||
326 ctx->conf.def.width == NL80211_CHAN_WIDTH_2 ||
327 ctx->conf.def.width == NL80211_CHAN_WIDTH_4 ||
328 ctx->conf.def.width == NL80211_CHAN_WIDTH_8 ||
329 ctx->conf.def.width == NL80211_CHAN_WIDTH_16 ||
330 ctx->conf.radar_enabled) {
331 ctx->conf.min_def = ctx->conf.def;
335 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
337 /* downgrade chandef up to max_bw */
338 min_def = ctx->conf.def;
339 while (min_def.width > max_bw)
340 ieee80211_chandef_downgrade(&min_def);
342 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
345 ctx->conf.min_def = min_def;
346 if (!ctx->driver_present)
349 return IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
352 /* calling this function is assuming that station vif is updated to
353 * lates changes by calling ieee80211_vif_update_chandef
355 static void ieee80211_chan_bw_change(struct ieee80211_local *local,
356 struct ieee80211_chanctx *ctx,
359 struct sta_info *sta;
360 struct ieee80211_supported_band *sband =
361 local->hw.wiphy->bands[ctx->conf.def.chan->band];
364 list_for_each_entry_rcu(sta, &local->sta_list,
366 enum ieee80211_sta_rx_bandwidth new_sta_bw;
368 if (!ieee80211_sdata_running(sta->sdata))
371 if (rcu_access_pointer(sta->sdata->vif.chanctx_conf) !=
375 new_sta_bw = ieee80211_sta_cur_vht_bw(sta);
378 if (new_sta_bw == sta->sta.bandwidth)
381 /* vif changed to narrow BW and narrow BW for station wasn't
382 * requested or vise versa */
383 if ((new_sta_bw < sta->sta.bandwidth) == !narrowed)
386 sta->sta.bandwidth = new_sta_bw;
387 rate_control_rate_update(local, sband, sta,
388 IEEE80211_RC_BW_CHANGED);
394 * recalc the min required chan width of the channel context, which is
395 * the max of min required widths of all the interfaces bound to this
398 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
399 struct ieee80211_chanctx *ctx)
401 u32 changed = _ieee80211_recalc_chanctx_min_def(local, ctx);
406 /* check is BW narrowed */
407 ieee80211_chan_bw_change(local, ctx, true);
409 drv_change_chanctx(local, ctx, changed);
411 /* check is BW wider */
412 ieee80211_chan_bw_change(local, ctx, false);
415 static void ieee80211_change_chanctx(struct ieee80211_local *local,
416 struct ieee80211_chanctx *ctx,
417 struct ieee80211_chanctx *old_ctx,
418 const struct cfg80211_chan_def *chandef)
422 /* expected to handle only 20/40/80/160/320 channel widths */
423 switch (chandef->width) {
424 case NL80211_CHAN_WIDTH_20_NOHT:
425 case NL80211_CHAN_WIDTH_20:
426 case NL80211_CHAN_WIDTH_40:
427 case NL80211_CHAN_WIDTH_80:
428 case NL80211_CHAN_WIDTH_80P80:
429 case NL80211_CHAN_WIDTH_160:
430 case NL80211_CHAN_WIDTH_320:
436 /* Check maybe BW narrowed - we do this _before_ calling recalc_chanctx_min_def
437 * due to maybe not returning from it, e.g in case new context was added
438 * first time with all parameters up to date.
440 ieee80211_chan_bw_change(local, old_ctx, true);
442 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
443 ieee80211_recalc_chanctx_min_def(local, ctx);
447 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
449 ctx->conf.def = *chandef;
451 /* check if min chanctx also changed */
452 changed = IEEE80211_CHANCTX_CHANGE_WIDTH |
453 _ieee80211_recalc_chanctx_min_def(local, ctx);
454 drv_change_chanctx(local, ctx, changed);
456 if (!local->use_chanctx) {
457 local->_oper_chandef = *chandef;
458 ieee80211_hw_config(local, 0);
461 /* check is BW wider */
462 ieee80211_chan_bw_change(local, old_ctx, false);
465 static struct ieee80211_chanctx *
466 ieee80211_find_chanctx(struct ieee80211_local *local,
467 const struct cfg80211_chan_def *chandef,
468 enum ieee80211_chanctx_mode mode)
470 struct ieee80211_chanctx *ctx;
472 lockdep_assert_held(&local->chanctx_mtx);
474 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
477 list_for_each_entry(ctx, &local->chanctx_list, list) {
478 const struct cfg80211_chan_def *compat;
480 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
483 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
486 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
490 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
495 ieee80211_change_chanctx(local, ctx, ctx, compat);
503 bool ieee80211_is_radar_required(struct ieee80211_local *local)
505 struct ieee80211_sub_if_data *sdata;
507 lockdep_assert_held(&local->mtx);
510 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
511 if (sdata->radar_required) {
522 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
523 struct ieee80211_chanctx *ctx)
525 struct ieee80211_chanctx_conf *conf = &ctx->conf;
526 struct ieee80211_sub_if_data *sdata;
527 bool required = false;
529 lockdep_assert_held(&local->chanctx_mtx);
530 lockdep_assert_held(&local->mtx);
533 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
534 if (!ieee80211_sdata_running(sdata))
536 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
538 if (!sdata->radar_required)
549 static struct ieee80211_chanctx *
550 ieee80211_alloc_chanctx(struct ieee80211_local *local,
551 const struct cfg80211_chan_def *chandef,
552 enum ieee80211_chanctx_mode mode)
554 struct ieee80211_chanctx *ctx;
556 lockdep_assert_held(&local->chanctx_mtx);
558 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
562 INIT_LIST_HEAD(&ctx->assigned_vifs);
563 INIT_LIST_HEAD(&ctx->reserved_vifs);
564 ctx->conf.def = *chandef;
565 ctx->conf.rx_chains_static = 1;
566 ctx->conf.rx_chains_dynamic = 1;
568 ctx->conf.radar_enabled = false;
569 ieee80211_recalc_chanctx_min_def(local, ctx);
574 static int ieee80211_add_chanctx(struct ieee80211_local *local,
575 struct ieee80211_chanctx *ctx)
580 lockdep_assert_held(&local->mtx);
581 lockdep_assert_held(&local->chanctx_mtx);
583 if (!local->use_chanctx)
584 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
586 /* turn idle off *before* setting channel -- some drivers need that */
587 changed = ieee80211_idle_off(local);
589 ieee80211_hw_config(local, changed);
591 if (!local->use_chanctx) {
592 local->_oper_chandef = ctx->conf.def;
593 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
595 err = drv_add_chanctx(local, ctx);
597 ieee80211_recalc_idle(local);
605 static struct ieee80211_chanctx *
606 ieee80211_new_chanctx(struct ieee80211_local *local,
607 const struct cfg80211_chan_def *chandef,
608 enum ieee80211_chanctx_mode mode)
610 struct ieee80211_chanctx *ctx;
613 lockdep_assert_held(&local->mtx);
614 lockdep_assert_held(&local->chanctx_mtx);
616 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
618 return ERR_PTR(-ENOMEM);
620 err = ieee80211_add_chanctx(local, ctx);
626 list_add_rcu(&ctx->list, &local->chanctx_list);
630 static void ieee80211_del_chanctx(struct ieee80211_local *local,
631 struct ieee80211_chanctx *ctx)
633 lockdep_assert_held(&local->chanctx_mtx);
635 if (!local->use_chanctx) {
636 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
637 /* S1G doesn't have 20MHz, so get the correct width for the
640 if (chandef->chan->band == NL80211_BAND_S1GHZ)
642 ieee80211_s1g_channel_width(chandef->chan);
644 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
645 chandef->center_freq1 = chandef->chan->center_freq;
646 chandef->freq1_offset = chandef->chan->freq_offset;
647 chandef->center_freq2 = 0;
649 /* NOTE: Disabling radar is only valid here for
650 * single channel context. To be sure, check it ...
652 WARN_ON(local->hw.conf.radar_enabled &&
653 !list_empty(&local->chanctx_list));
655 local->hw.conf.radar_enabled = false;
657 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
659 drv_remove_chanctx(local, ctx);
662 ieee80211_recalc_idle(local);
665 static void ieee80211_free_chanctx(struct ieee80211_local *local,
666 struct ieee80211_chanctx *ctx)
668 lockdep_assert_held(&local->chanctx_mtx);
670 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
672 list_del_rcu(&ctx->list);
673 ieee80211_del_chanctx(local, ctx);
674 kfree_rcu(ctx, rcu_head);
677 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
678 struct ieee80211_chanctx *ctx)
680 struct ieee80211_chanctx_conf *conf = &ctx->conf;
681 struct ieee80211_sub_if_data *sdata;
682 const struct cfg80211_chan_def *compat = NULL;
683 struct sta_info *sta;
685 lockdep_assert_held(&local->chanctx_mtx);
688 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
690 if (!ieee80211_sdata_running(sdata))
692 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
694 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
698 compat = &sdata->vif.bss_conf.chandef;
700 compat = cfg80211_chandef_compatible(
701 &sdata->vif.bss_conf.chandef, compat);
702 if (WARN_ON_ONCE(!compat))
706 /* TDLS peers can sometimes affect the chandef width */
707 list_for_each_entry_rcu(sta, &local->sta_list, list) {
708 if (!sta->uploaded ||
709 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
710 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
711 !sta->tdls_chandef.chan)
714 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
716 if (WARN_ON_ONCE(!compat))
724 ieee80211_change_chanctx(local, ctx, ctx, compat);
727 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
728 struct ieee80211_chanctx *chanctx)
732 lockdep_assert_held(&local->chanctx_mtx);
733 /* for ieee80211_is_radar_required */
734 lockdep_assert_held(&local->mtx);
736 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
738 if (radar_enabled == chanctx->conf.radar_enabled)
741 chanctx->conf.radar_enabled = radar_enabled;
743 if (!local->use_chanctx) {
744 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
745 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
748 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
751 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
752 struct ieee80211_chanctx *new_ctx)
754 struct ieee80211_local *local = sdata->local;
755 struct ieee80211_chanctx_conf *conf;
756 struct ieee80211_chanctx *curr_ctx = NULL;
759 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
762 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
763 lockdep_is_held(&local->chanctx_mtx));
766 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
768 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
770 list_del(&sdata->assigned_chanctx_list);
774 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
778 conf = &new_ctx->conf;
779 list_add(&sdata->assigned_chanctx_list,
780 &new_ctx->assigned_vifs);
784 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
786 sdata->vif.bss_conf.idle = !conf;
788 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
789 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
790 ieee80211_recalc_smps_chanctx(local, curr_ctx);
791 ieee80211_recalc_radar_chanctx(local, curr_ctx);
792 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
795 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
796 ieee80211_recalc_txpower(sdata, false);
797 ieee80211_recalc_chanctx_min_def(local, new_ctx);
800 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
801 sdata->vif.type != NL80211_IFTYPE_MONITOR)
802 ieee80211_bss_info_change_notify(sdata,
805 ieee80211_check_fast_xmit_iface(sdata);
810 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
811 struct ieee80211_chanctx *chanctx)
813 struct ieee80211_sub_if_data *sdata;
814 u8 rx_chains_static, rx_chains_dynamic;
816 lockdep_assert_held(&local->chanctx_mtx);
818 rx_chains_static = 1;
819 rx_chains_dynamic = 1;
822 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
823 u8 needed_static, needed_dynamic;
825 if (!ieee80211_sdata_running(sdata))
828 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
832 switch (sdata->vif.type) {
833 case NL80211_IFTYPE_P2P_DEVICE:
834 case NL80211_IFTYPE_NAN:
836 case NL80211_IFTYPE_STATION:
837 if (!sdata->u.mgd.associated)
840 case NL80211_IFTYPE_AP_VLAN:
842 case NL80211_IFTYPE_AP:
843 case NL80211_IFTYPE_ADHOC:
844 case NL80211_IFTYPE_MESH_POINT:
845 case NL80211_IFTYPE_OCB:
851 switch (sdata->smps_mode) {
853 WARN_ONCE(1, "Invalid SMPS mode %d\n",
856 case IEEE80211_SMPS_OFF:
857 needed_static = sdata->needed_rx_chains;
858 needed_dynamic = sdata->needed_rx_chains;
860 case IEEE80211_SMPS_DYNAMIC:
862 needed_dynamic = sdata->needed_rx_chains;
864 case IEEE80211_SMPS_STATIC:
870 rx_chains_static = max(rx_chains_static, needed_static);
871 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
874 /* Disable SMPS for the monitor interface */
875 sdata = rcu_dereference(local->monitor_sdata);
877 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
878 rx_chains_dynamic = rx_chains_static = local->rx_chains;
882 if (!local->use_chanctx) {
883 if (rx_chains_static > 1)
884 local->smps_mode = IEEE80211_SMPS_OFF;
885 else if (rx_chains_dynamic > 1)
886 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
888 local->smps_mode = IEEE80211_SMPS_STATIC;
889 ieee80211_hw_config(local, 0);
892 if (rx_chains_static == chanctx->conf.rx_chains_static &&
893 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
896 chanctx->conf.rx_chains_static = rx_chains_static;
897 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
898 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
902 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
905 struct ieee80211_local *local __maybe_unused = sdata->local;
906 struct ieee80211_sub_if_data *vlan;
907 struct ieee80211_chanctx_conf *conf;
909 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
912 lockdep_assert_held(&local->mtx);
914 /* Check that conf exists, even when clearing this function
915 * must be called with the AP's channel context still there
916 * as it would otherwise cause VLANs to have an invalid
917 * channel context pointer for a while, possibly pointing
918 * to a channel context that has already been freed.
920 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
921 lockdep_is_held(&local->chanctx_mtx));
927 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
928 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
931 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
934 struct ieee80211_local *local = sdata->local;
936 mutex_lock(&local->chanctx_mtx);
938 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
940 mutex_unlock(&local->chanctx_mtx);
943 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
945 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
947 lockdep_assert_held(&sdata->local->chanctx_mtx);
952 list_del(&sdata->reserved_chanctx_list);
953 sdata->reserved_chanctx = NULL;
955 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
956 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
957 if (WARN_ON(!ctx->replace_ctx))
960 WARN_ON(ctx->replace_ctx->replace_state !=
961 IEEE80211_CHANCTX_WILL_BE_REPLACED);
962 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
964 ctx->replace_ctx->replace_ctx = NULL;
965 ctx->replace_ctx->replace_state =
966 IEEE80211_CHANCTX_REPLACE_NONE;
968 list_del_rcu(&ctx->list);
969 kfree_rcu(ctx, rcu_head);
971 ieee80211_free_chanctx(sdata->local, ctx);
978 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
979 const struct cfg80211_chan_def *chandef,
980 enum ieee80211_chanctx_mode mode,
983 struct ieee80211_local *local = sdata->local;
984 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
986 lockdep_assert_held(&local->chanctx_mtx);
988 curr_ctx = ieee80211_vif_get_chanctx(sdata);
989 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
992 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
994 if (ieee80211_can_create_new_chanctx(local)) {
995 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
997 return PTR_ERR(new_ctx);
1000 (curr_ctx->replace_state ==
1001 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1002 !list_empty(&curr_ctx->reserved_vifs)) {
1004 * Another vif already requested this context
1005 * for a reservation. Find another one hoping
1006 * all vifs assigned to it will also switch
1009 * TODO: This needs a little more work as some
1010 * cases (more than 2 chanctx capable devices)
1011 * may fail which could otherwise succeed
1012 * provided some channel context juggling was
1015 * Consider ctx1..3, vif1..6, each ctx has 2
1016 * vifs. vif1 and vif2 from ctx1 request new
1017 * different chandefs starting 2 in-place
1018 * reserations with ctx4 and ctx5 replacing
1019 * ctx1 and ctx2 respectively. Next vif5 and
1020 * vif6 from ctx3 reserve ctx4. If vif3 and
1021 * vif4 remain on ctx2 as they are then this
1022 * fails unless `replace_ctx` from ctx5 is
1023 * replaced with ctx3.
1025 list_for_each_entry(ctx, &local->chanctx_list,
1027 if (ctx->replace_state !=
1028 IEEE80211_CHANCTX_REPLACE_NONE)
1031 if (!list_empty(&ctx->reserved_vifs))
1040 * If that's true then all available contexts already
1041 * have reservations and cannot be used.
1044 (curr_ctx->replace_state ==
1045 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1046 !list_empty(&curr_ctx->reserved_vifs))
1049 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
1053 new_ctx->replace_ctx = curr_ctx;
1054 new_ctx->replace_state =
1055 IEEE80211_CHANCTX_REPLACES_OTHER;
1057 curr_ctx->replace_ctx = new_ctx;
1058 curr_ctx->replace_state =
1059 IEEE80211_CHANCTX_WILL_BE_REPLACED;
1061 list_add_rcu(&new_ctx->list, &local->chanctx_list);
1065 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
1066 sdata->reserved_chanctx = new_ctx;
1067 sdata->reserved_chandef = *chandef;
1068 sdata->reserved_radar_required = radar_required;
1069 sdata->reserved_ready = false;
1075 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
1077 switch (sdata->vif.type) {
1078 case NL80211_IFTYPE_ADHOC:
1079 case NL80211_IFTYPE_AP:
1080 case NL80211_IFTYPE_MESH_POINT:
1081 case NL80211_IFTYPE_OCB:
1082 ieee80211_queue_work(&sdata->local->hw,
1083 &sdata->csa_finalize_work);
1085 case NL80211_IFTYPE_STATION:
1086 ieee80211_queue_work(&sdata->local->hw,
1087 &sdata->u.mgd.chswitch_work);
1089 case NL80211_IFTYPE_UNSPECIFIED:
1090 case NL80211_IFTYPE_AP_VLAN:
1091 case NL80211_IFTYPE_WDS:
1092 case NL80211_IFTYPE_MONITOR:
1093 case NL80211_IFTYPE_P2P_CLIENT:
1094 case NL80211_IFTYPE_P2P_GO:
1095 case NL80211_IFTYPE_P2P_DEVICE:
1096 case NL80211_IFTYPE_NAN:
1097 case NUM_NL80211_IFTYPES:
1104 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
1105 const struct cfg80211_chan_def *chandef)
1107 struct ieee80211_sub_if_data *vlan;
1109 sdata->vif.bss_conf.chandef = *chandef;
1111 if (sdata->vif.type != NL80211_IFTYPE_AP)
1114 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1115 vlan->vif.bss_conf.chandef = *chandef;
1119 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
1121 struct ieee80211_local *local = sdata->local;
1122 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1123 struct ieee80211_chanctx *old_ctx, *new_ctx;
1124 const struct cfg80211_chan_def *chandef;
1128 lockdep_assert_held(&local->mtx);
1129 lockdep_assert_held(&local->chanctx_mtx);
1131 new_ctx = sdata->reserved_chanctx;
1132 old_ctx = ieee80211_vif_get_chanctx(sdata);
1134 if (WARN_ON(!sdata->reserved_ready))
1137 if (WARN_ON(!new_ctx))
1140 if (WARN_ON(!old_ctx))
1143 if (WARN_ON(new_ctx->replace_state ==
1144 IEEE80211_CHANCTX_REPLACES_OTHER))
1147 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1148 &sdata->reserved_chandef);
1149 if (WARN_ON(!chandef))
1152 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
1153 changed = BSS_CHANGED_BANDWIDTH;
1155 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1157 ieee80211_change_chanctx(local, new_ctx, old_ctx, chandef);
1159 vif_chsw[0].vif = &sdata->vif;
1160 vif_chsw[0].old_ctx = &old_ctx->conf;
1161 vif_chsw[0].new_ctx = &new_ctx->conf;
1163 list_del(&sdata->reserved_chanctx_list);
1164 sdata->reserved_chanctx = NULL;
1166 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1167 CHANCTX_SWMODE_REASSIGN_VIF);
1169 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1170 ieee80211_free_chanctx(local, new_ctx);
1175 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1176 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1178 if (sdata->vif.type == NL80211_IFTYPE_AP)
1179 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1181 ieee80211_check_fast_xmit_iface(sdata);
1183 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1184 ieee80211_free_chanctx(local, old_ctx);
1186 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1187 ieee80211_recalc_smps_chanctx(local, new_ctx);
1188 ieee80211_recalc_radar_chanctx(local, new_ctx);
1191 ieee80211_bss_info_change_notify(sdata, changed);
1194 ieee80211_vif_chanctx_reservation_complete(sdata);
1199 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1201 struct ieee80211_local *local = sdata->local;
1202 struct ieee80211_chanctx *old_ctx, *new_ctx;
1203 const struct cfg80211_chan_def *chandef;
1206 old_ctx = ieee80211_vif_get_chanctx(sdata);
1207 new_ctx = sdata->reserved_chanctx;
1209 if (WARN_ON(!sdata->reserved_ready))
1212 if (WARN_ON(old_ctx))
1215 if (WARN_ON(!new_ctx))
1218 if (WARN_ON(new_ctx->replace_state ==
1219 IEEE80211_CHANCTX_REPLACES_OTHER))
1222 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1223 &sdata->reserved_chandef);
1224 if (WARN_ON(!chandef))
1227 ieee80211_change_chanctx(local, new_ctx, new_ctx, chandef);
1229 list_del(&sdata->reserved_chanctx_list);
1230 sdata->reserved_chanctx = NULL;
1232 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1234 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1235 ieee80211_free_chanctx(local, new_ctx);
1241 ieee80211_vif_chanctx_reservation_complete(sdata);
1246 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1248 struct ieee80211_chanctx *old_ctx, *new_ctx;
1250 lockdep_assert_held(&sdata->local->chanctx_mtx);
1252 new_ctx = sdata->reserved_chanctx;
1253 old_ctx = ieee80211_vif_get_chanctx(sdata);
1258 if (WARN_ON(!new_ctx))
1261 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1264 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1270 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1271 struct ieee80211_chanctx *new_ctx)
1273 const struct cfg80211_chan_def *chandef;
1275 lockdep_assert_held(&local->mtx);
1276 lockdep_assert_held(&local->chanctx_mtx);
1278 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1279 if (WARN_ON(!chandef))
1282 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1283 local->_oper_chandef = *chandef;
1284 ieee80211_hw_config(local, 0);
1289 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1292 struct ieee80211_vif_chanctx_switch *vif_chsw;
1293 struct ieee80211_sub_if_data *sdata;
1294 struct ieee80211_chanctx *ctx, *old_ctx;
1297 lockdep_assert_held(&local->mtx);
1298 lockdep_assert_held(&local->chanctx_mtx);
1300 vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
1305 list_for_each_entry(ctx, &local->chanctx_list, list) {
1306 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1309 if (WARN_ON(!ctx->replace_ctx)) {
1314 list_for_each_entry(sdata, &ctx->reserved_vifs,
1315 reserved_chanctx_list) {
1316 if (!ieee80211_vif_has_in_place_reservation(
1320 old_ctx = ieee80211_vif_get_chanctx(sdata);
1321 vif_chsw[i].vif = &sdata->vif;
1322 vif_chsw[i].old_ctx = &old_ctx->conf;
1323 vif_chsw[i].new_ctx = &ctx->conf;
1329 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1330 CHANCTX_SWMODE_SWAP_CONTEXTS);
1337 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1339 struct ieee80211_chanctx *ctx;
1342 lockdep_assert_held(&local->mtx);
1343 lockdep_assert_held(&local->chanctx_mtx);
1345 list_for_each_entry(ctx, &local->chanctx_list, list) {
1346 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1349 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1352 ieee80211_del_chanctx(local, ctx->replace_ctx);
1353 err = ieee80211_add_chanctx(local, ctx);
1361 WARN_ON(ieee80211_add_chanctx(local, ctx));
1362 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1363 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1366 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1369 ieee80211_del_chanctx(local, ctx);
1370 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1376 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1378 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1379 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1380 struct ieee80211_chanctx *new_ctx = NULL;
1381 int err, n_assigned, n_reserved, n_ready;
1382 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1384 lockdep_assert_held(&local->mtx);
1385 lockdep_assert_held(&local->chanctx_mtx);
1388 * If there are 2 independent pairs of channel contexts performing
1389 * cross-switch of their vifs this code will still wait until both are
1390 * ready even though it could be possible to switch one before the
1393 * For practical reasons and code simplicity just do a single huge
1398 * Verify if the reservation is still feasible.
1399 * - if it's not then disconnect
1400 * - if it is but not all vifs necessary are ready then defer
1403 list_for_each_entry(ctx, &local->chanctx_list, list) {
1404 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1407 if (WARN_ON(!ctx->replace_ctx)) {
1412 if (!local->use_chanctx)
1421 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1422 assigned_chanctx_list) {
1424 if (sdata->reserved_chanctx) {
1426 if (sdata->reserved_ready)
1431 if (n_assigned != n_reserved) {
1432 if (n_ready == n_reserved) {
1433 wiphy_info(local->hw.wiphy,
1434 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1442 ctx->conf.radar_enabled = false;
1443 list_for_each_entry(sdata, &ctx->reserved_vifs,
1444 reserved_chanctx_list) {
1445 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1446 !sdata->reserved_ready)
1449 old_ctx = ieee80211_vif_get_chanctx(sdata);
1451 if (old_ctx->replace_state ==
1452 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1460 if (sdata->reserved_radar_required)
1461 ctx->conf.radar_enabled = true;
1465 if (WARN_ON(n_ctx == 0) ||
1466 WARN_ON(n_vifs_switch == 0 &&
1467 n_vifs_assign == 0 &&
1468 n_vifs_ctxless == 0) ||
1469 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1470 WARN_ON(!new_ctx && !local->use_chanctx)) {
1476 * All necessary vifs are ready. Perform the switch now depending on
1477 * reservations and driver capabilities.
1480 if (local->use_chanctx) {
1481 if (n_vifs_switch > 0) {
1482 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1487 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1488 err = ieee80211_chsw_switch_ctxs(local);
1493 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1499 * Update all structures, values and pointers to point to new channel
1502 list_for_each_entry(ctx, &local->chanctx_list, list) {
1503 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1506 if (WARN_ON(!ctx->replace_ctx)) {
1511 list_for_each_entry(sdata, &ctx->reserved_vifs,
1512 reserved_chanctx_list) {
1515 if (!ieee80211_vif_has_in_place_reservation(sdata))
1518 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1520 if (sdata->vif.type == NL80211_IFTYPE_AP)
1521 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1524 ieee80211_check_fast_xmit_iface(sdata);
1526 sdata->radar_required = sdata->reserved_radar_required;
1528 if (sdata->vif.bss_conf.chandef.width !=
1529 sdata->reserved_chandef.width)
1530 changed = BSS_CHANGED_BANDWIDTH;
1532 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1534 ieee80211_bss_info_change_notify(sdata,
1537 ieee80211_recalc_txpower(sdata, false);
1540 ieee80211_recalc_chanctx_chantype(local, ctx);
1541 ieee80211_recalc_smps_chanctx(local, ctx);
1542 ieee80211_recalc_radar_chanctx(local, ctx);
1543 ieee80211_recalc_chanctx_min_def(local, ctx);
1545 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1546 reserved_chanctx_list) {
1547 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1550 list_del(&sdata->reserved_chanctx_list);
1551 list_move(&sdata->assigned_chanctx_list,
1552 &ctx->assigned_vifs);
1553 sdata->reserved_chanctx = NULL;
1555 ieee80211_vif_chanctx_reservation_complete(sdata);
1559 * This context might have been a dependency for an already
1560 * ready re-assign reservation interface that was deferred. Do
1561 * not propagate error to the caller though. The in-place
1562 * reservation for originally requested interface has already
1563 * succeeded at this point.
1565 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1566 reserved_chanctx_list) {
1567 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1571 if (WARN_ON(sdata->reserved_chanctx != ctx))
1574 if (!sdata->reserved_ready)
1577 if (ieee80211_vif_get_chanctx(sdata))
1578 err = ieee80211_vif_use_reserved_reassign(
1581 err = ieee80211_vif_use_reserved_assign(sdata);
1585 "failed to finalize (re-)assign reservation (err=%d)\n",
1587 ieee80211_vif_unreserve_chanctx(sdata);
1588 cfg80211_stop_iface(local->hw.wiphy,
1596 * Finally free old contexts
1599 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1600 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1603 ctx->replace_ctx->replace_ctx = NULL;
1604 ctx->replace_ctx->replace_state =
1605 IEEE80211_CHANCTX_REPLACE_NONE;
1607 list_del_rcu(&ctx->list);
1608 kfree_rcu(ctx, rcu_head);
1614 list_for_each_entry(ctx, &local->chanctx_list, list) {
1615 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1618 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1619 reserved_chanctx_list) {
1620 ieee80211_vif_unreserve_chanctx(sdata);
1621 ieee80211_vif_chanctx_reservation_complete(sdata);
1628 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1630 struct ieee80211_local *local = sdata->local;
1631 struct ieee80211_chanctx_conf *conf;
1632 struct ieee80211_chanctx *ctx;
1633 bool use_reserved_switch = false;
1635 lockdep_assert_held(&local->chanctx_mtx);
1637 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1638 lockdep_is_held(&local->chanctx_mtx));
1642 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1644 if (sdata->reserved_chanctx) {
1645 if (sdata->reserved_chanctx->replace_state ==
1646 IEEE80211_CHANCTX_REPLACES_OTHER &&
1647 ieee80211_chanctx_num_reserved(local,
1648 sdata->reserved_chanctx) > 1)
1649 use_reserved_switch = true;
1651 ieee80211_vif_unreserve_chanctx(sdata);
1654 ieee80211_assign_vif_chanctx(sdata, NULL);
1655 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1656 ieee80211_free_chanctx(local, ctx);
1658 sdata->radar_required = false;
1660 /* Unreserving may ready an in-place reservation. */
1661 if (use_reserved_switch)
1662 ieee80211_vif_use_reserved_switch(local);
1665 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1666 const struct cfg80211_chan_def *chandef,
1667 enum ieee80211_chanctx_mode mode)
1669 struct ieee80211_local *local = sdata->local;
1670 struct ieee80211_chanctx *ctx;
1671 u8 radar_detect_width = 0;
1674 lockdep_assert_held(&local->mtx);
1676 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1678 mutex_lock(&local->chanctx_mtx);
1680 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1682 sdata->wdev.iftype);
1686 radar_detect_width = BIT(chandef->width);
1688 sdata->radar_required = ret;
1690 ret = ieee80211_check_combinations(sdata, chandef, mode,
1691 radar_detect_width);
1695 __ieee80211_vif_release_channel(sdata);
1697 ctx = ieee80211_find_chanctx(local, chandef, mode);
1699 ctx = ieee80211_new_chanctx(local, chandef, mode);
1705 ieee80211_vif_update_chandef(sdata, chandef);
1707 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1709 /* if assign fails refcount stays the same */
1710 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1711 ieee80211_free_chanctx(local, ctx);
1715 ieee80211_recalc_smps_chanctx(local, ctx);
1716 ieee80211_recalc_radar_chanctx(local, ctx);
1719 sdata->radar_required = false;
1721 mutex_unlock(&local->chanctx_mtx);
1725 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1727 struct ieee80211_local *local = sdata->local;
1728 struct ieee80211_chanctx *new_ctx;
1729 struct ieee80211_chanctx *old_ctx;
1732 lockdep_assert_held(&local->mtx);
1733 lockdep_assert_held(&local->chanctx_mtx);
1735 new_ctx = sdata->reserved_chanctx;
1736 old_ctx = ieee80211_vif_get_chanctx(sdata);
1738 if (WARN_ON(!new_ctx))
1741 if (WARN_ON(new_ctx->replace_state ==
1742 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1745 if (WARN_ON(sdata->reserved_ready))
1748 sdata->reserved_ready = true;
1750 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1752 err = ieee80211_vif_use_reserved_reassign(sdata);
1754 err = ieee80211_vif_use_reserved_assign(sdata);
1761 * In-place reservation may need to be finalized now either if:
1762 * a) sdata is taking part in the swapping itself and is the last one
1763 * b) sdata has switched with a re-assign reservation to an existing
1764 * context readying in-place switching of old_ctx
1766 * In case of (b) do not propagate the error up because the requested
1767 * sdata already switched successfully. Just spill an extra warning.
1768 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1769 * interfaces upon failure.
1772 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1773 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1774 err = ieee80211_vif_use_reserved_switch(local);
1775 if (err && err != -EAGAIN) {
1776 if (new_ctx->replace_state ==
1777 IEEE80211_CHANCTX_REPLACES_OTHER)
1780 wiphy_info(local->hw.wiphy,
1781 "depending in-place reservation failed (err=%d)\n",
1789 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1790 const struct cfg80211_chan_def *chandef,
1793 struct ieee80211_local *local = sdata->local;
1794 struct ieee80211_chanctx_conf *conf;
1795 struct ieee80211_chanctx *ctx;
1796 const struct cfg80211_chan_def *compat;
1799 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1800 IEEE80211_CHAN_DISABLED))
1803 mutex_lock(&local->chanctx_mtx);
1804 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1809 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1810 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1815 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1816 lockdep_is_held(&local->chanctx_mtx));
1822 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1824 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1830 switch (ctx->replace_state) {
1831 case IEEE80211_CHANCTX_REPLACE_NONE:
1832 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1837 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1838 /* TODO: Perhaps the bandwidth change could be treated as a
1839 * reservation itself? */
1842 case IEEE80211_CHANCTX_REPLACES_OTHER:
1843 /* channel context that is going to replace another channel
1844 * context doesn't really exist and shouldn't be assigned
1850 ieee80211_vif_update_chandef(sdata, chandef);
1852 ieee80211_recalc_chanctx_chantype(local, ctx);
1854 *changed |= BSS_CHANGED_BANDWIDTH;
1857 mutex_unlock(&local->chanctx_mtx);
1861 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1863 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1865 lockdep_assert_held(&sdata->local->mtx);
1867 mutex_lock(&sdata->local->chanctx_mtx);
1868 __ieee80211_vif_release_channel(sdata);
1869 mutex_unlock(&sdata->local->chanctx_mtx);
1872 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1874 struct ieee80211_local *local = sdata->local;
1875 struct ieee80211_sub_if_data *ap;
1876 struct ieee80211_chanctx_conf *conf;
1878 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1881 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1883 mutex_lock(&local->chanctx_mtx);
1885 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1886 lockdep_is_held(&local->chanctx_mtx));
1887 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1888 mutex_unlock(&local->chanctx_mtx);
1891 void ieee80211_iter_chan_contexts_atomic(
1892 struct ieee80211_hw *hw,
1893 void (*iter)(struct ieee80211_hw *hw,
1894 struct ieee80211_chanctx_conf *chanctx_conf,
1898 struct ieee80211_local *local = hw_to_local(hw);
1899 struct ieee80211_chanctx *ctx;
1902 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1903 if (ctx->driver_present)
1904 iter(hw, &ctx->conf, iter_data);
1907 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);