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;
223 return NL80211_CHAN_WIDTH_20;
227 static enum nl80211_chan_width
228 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
230 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
231 struct sta_info *sta;
234 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
235 if (sdata != sta->sdata &&
236 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
239 max_bw = max(max_bw, ieee80211_get_sta_bw(sta));
246 static enum nl80211_chan_width
247 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
248 struct ieee80211_chanctx_conf *conf)
250 struct ieee80211_sub_if_data *sdata;
251 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
254 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
255 struct ieee80211_vif *vif = &sdata->vif;
256 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
258 if (!ieee80211_sdata_running(sdata))
261 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
265 case NL80211_IFTYPE_AP:
266 case NL80211_IFTYPE_AP_VLAN:
267 width = ieee80211_get_max_required_bw(sdata);
269 case NL80211_IFTYPE_STATION:
271 * The ap's sta->bandwidth is not set yet at this
272 * point, so take the width from the chandef, but
273 * account also for TDLS peers
275 width = max(vif->bss_conf.chandef.width,
276 ieee80211_get_max_required_bw(sdata));
278 case NL80211_IFTYPE_P2P_DEVICE:
279 case NL80211_IFTYPE_NAN:
281 case NL80211_IFTYPE_ADHOC:
282 case NL80211_IFTYPE_MESH_POINT:
283 case NL80211_IFTYPE_OCB:
284 width = vif->bss_conf.chandef.width;
286 case NL80211_IFTYPE_WDS:
287 case NL80211_IFTYPE_UNSPECIFIED:
288 case NUM_NL80211_IFTYPES:
289 case NL80211_IFTYPE_MONITOR:
290 case NL80211_IFTYPE_P2P_CLIENT:
291 case NL80211_IFTYPE_P2P_GO:
294 max_bw = max(max_bw, width);
297 /* use the configured bandwidth in case of monitor interface */
298 sdata = rcu_dereference(local->monitor_sdata);
299 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
300 max_bw = max(max_bw, conf->def.width);
308 * recalc the min required chan width of the channel context, which is
309 * the max of min required widths of all the interfaces bound to this
312 static u32 _ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
313 struct ieee80211_chanctx *ctx)
315 enum nl80211_chan_width max_bw;
316 struct cfg80211_chan_def min_def;
318 lockdep_assert_held(&local->chanctx_mtx);
320 /* don't optimize non-20MHz based and radar_enabled confs */
321 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
322 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
323 ctx->conf.def.width == NL80211_CHAN_WIDTH_1 ||
324 ctx->conf.def.width == NL80211_CHAN_WIDTH_2 ||
325 ctx->conf.def.width == NL80211_CHAN_WIDTH_4 ||
326 ctx->conf.def.width == NL80211_CHAN_WIDTH_8 ||
327 ctx->conf.def.width == NL80211_CHAN_WIDTH_16 ||
328 ctx->conf.radar_enabled) {
329 ctx->conf.min_def = ctx->conf.def;
333 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
335 /* downgrade chandef up to max_bw */
336 min_def = ctx->conf.def;
337 while (min_def.width > max_bw)
338 ieee80211_chandef_downgrade(&min_def);
340 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
343 ctx->conf.min_def = min_def;
344 if (!ctx->driver_present)
347 return IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
350 /* calling this function is assuming that station vif is updated to
351 * lates changes by calling ieee80211_vif_update_chandef
353 static void ieee80211_chan_bw_change(struct ieee80211_local *local,
354 struct ieee80211_chanctx *ctx,
357 struct sta_info *sta;
358 struct ieee80211_supported_band *sband =
359 local->hw.wiphy->bands[ctx->conf.def.chan->band];
362 list_for_each_entry_rcu(sta, &local->sta_list,
364 enum ieee80211_sta_rx_bandwidth new_sta_bw;
366 if (!ieee80211_sdata_running(sta->sdata))
369 if (rcu_access_pointer(sta->sdata->vif.chanctx_conf) !=
373 new_sta_bw = ieee80211_sta_cur_vht_bw(sta);
376 if (new_sta_bw == sta->sta.bandwidth)
379 /* vif changed to narrow BW and narrow BW for station wasn't
380 * requested or vise versa */
381 if ((new_sta_bw < sta->sta.bandwidth) == !narrowed)
384 sta->sta.bandwidth = new_sta_bw;
385 rate_control_rate_update(local, sband, sta,
386 IEEE80211_RC_BW_CHANGED);
392 * recalc the min required chan width of the channel context, which is
393 * the max of min required widths of all the interfaces bound to this
396 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
397 struct ieee80211_chanctx *ctx)
399 u32 changed = _ieee80211_recalc_chanctx_min_def(local, ctx);
404 /* check is BW narrowed */
405 ieee80211_chan_bw_change(local, ctx, true);
407 drv_change_chanctx(local, ctx, changed);
409 /* check is BW wider */
410 ieee80211_chan_bw_change(local, ctx, false);
413 static void ieee80211_change_chanctx(struct ieee80211_local *local,
414 struct ieee80211_chanctx *ctx,
415 struct ieee80211_chanctx *old_ctx,
416 const struct cfg80211_chan_def *chandef)
420 /* expected to handle only 20/40/80/160 channel widths */
421 switch (chandef->width) {
422 case NL80211_CHAN_WIDTH_20_NOHT:
423 case NL80211_CHAN_WIDTH_20:
424 case NL80211_CHAN_WIDTH_40:
425 case NL80211_CHAN_WIDTH_80:
426 case NL80211_CHAN_WIDTH_80P80:
427 case NL80211_CHAN_WIDTH_160:
433 /* Check maybe BW narrowed - we do this _before_ calling recalc_chanctx_min_def
434 * due to maybe not returning from it, e.g in case new context was added
435 * first time with all parameters up to date.
437 ieee80211_chan_bw_change(local, old_ctx, true);
439 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
440 ieee80211_recalc_chanctx_min_def(local, ctx);
444 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
446 ctx->conf.def = *chandef;
448 /* check if min chanctx also changed */
449 changed = IEEE80211_CHANCTX_CHANGE_WIDTH |
450 _ieee80211_recalc_chanctx_min_def(local, ctx);
451 drv_change_chanctx(local, ctx, changed);
453 if (!local->use_chanctx) {
454 local->_oper_chandef = *chandef;
455 ieee80211_hw_config(local, 0);
458 /* check is BW wider */
459 ieee80211_chan_bw_change(local, old_ctx, false);
462 static struct ieee80211_chanctx *
463 ieee80211_find_chanctx(struct ieee80211_local *local,
464 const struct cfg80211_chan_def *chandef,
465 enum ieee80211_chanctx_mode mode)
467 struct ieee80211_chanctx *ctx;
469 lockdep_assert_held(&local->chanctx_mtx);
471 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
474 list_for_each_entry(ctx, &local->chanctx_list, list) {
475 const struct cfg80211_chan_def *compat;
477 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
480 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
483 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
487 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
492 ieee80211_change_chanctx(local, ctx, ctx, compat);
500 bool ieee80211_is_radar_required(struct ieee80211_local *local)
502 struct ieee80211_sub_if_data *sdata;
504 lockdep_assert_held(&local->mtx);
507 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
508 if (sdata->radar_required) {
519 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
520 struct ieee80211_chanctx *ctx)
522 struct ieee80211_chanctx_conf *conf = &ctx->conf;
523 struct ieee80211_sub_if_data *sdata;
524 bool required = false;
526 lockdep_assert_held(&local->chanctx_mtx);
527 lockdep_assert_held(&local->mtx);
530 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
531 if (!ieee80211_sdata_running(sdata))
533 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
535 if (!sdata->radar_required)
546 static struct ieee80211_chanctx *
547 ieee80211_alloc_chanctx(struct ieee80211_local *local,
548 const struct cfg80211_chan_def *chandef,
549 enum ieee80211_chanctx_mode mode)
551 struct ieee80211_chanctx *ctx;
553 lockdep_assert_held(&local->chanctx_mtx);
555 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
559 INIT_LIST_HEAD(&ctx->assigned_vifs);
560 INIT_LIST_HEAD(&ctx->reserved_vifs);
561 ctx->conf.def = *chandef;
562 ctx->conf.rx_chains_static = 1;
563 ctx->conf.rx_chains_dynamic = 1;
565 ctx->conf.radar_enabled = false;
566 ieee80211_recalc_chanctx_min_def(local, ctx);
571 static int ieee80211_add_chanctx(struct ieee80211_local *local,
572 struct ieee80211_chanctx *ctx)
577 lockdep_assert_held(&local->mtx);
578 lockdep_assert_held(&local->chanctx_mtx);
580 if (!local->use_chanctx)
581 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
583 /* turn idle off *before* setting channel -- some drivers need that */
584 changed = ieee80211_idle_off(local);
586 ieee80211_hw_config(local, changed);
588 if (!local->use_chanctx) {
589 local->_oper_chandef = ctx->conf.def;
590 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
592 err = drv_add_chanctx(local, ctx);
594 ieee80211_recalc_idle(local);
602 static struct ieee80211_chanctx *
603 ieee80211_new_chanctx(struct ieee80211_local *local,
604 const struct cfg80211_chan_def *chandef,
605 enum ieee80211_chanctx_mode mode)
607 struct ieee80211_chanctx *ctx;
610 lockdep_assert_held(&local->mtx);
611 lockdep_assert_held(&local->chanctx_mtx);
613 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
615 return ERR_PTR(-ENOMEM);
617 err = ieee80211_add_chanctx(local, ctx);
623 list_add_rcu(&ctx->list, &local->chanctx_list);
627 static void ieee80211_del_chanctx(struct ieee80211_local *local,
628 struct ieee80211_chanctx *ctx)
630 lockdep_assert_held(&local->chanctx_mtx);
632 if (!local->use_chanctx) {
633 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
634 /* S1G doesn't have 20MHz, so get the correct width for the
637 if (chandef->chan->band == NL80211_BAND_S1GHZ)
639 ieee80211_s1g_channel_width(chandef->chan);
641 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
642 chandef->center_freq1 = chandef->chan->center_freq;
643 chandef->freq1_offset = chandef->chan->freq_offset;
644 chandef->center_freq2 = 0;
646 /* NOTE: Disabling radar is only valid here for
647 * single channel context. To be sure, check it ...
649 WARN_ON(local->hw.conf.radar_enabled &&
650 !list_empty(&local->chanctx_list));
652 local->hw.conf.radar_enabled = false;
654 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
656 drv_remove_chanctx(local, ctx);
659 ieee80211_recalc_idle(local);
662 static void ieee80211_free_chanctx(struct ieee80211_local *local,
663 struct ieee80211_chanctx *ctx)
665 lockdep_assert_held(&local->chanctx_mtx);
667 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
669 list_del_rcu(&ctx->list);
670 ieee80211_del_chanctx(local, ctx);
671 kfree_rcu(ctx, rcu_head);
674 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
675 struct ieee80211_chanctx *ctx)
677 struct ieee80211_chanctx_conf *conf = &ctx->conf;
678 struct ieee80211_sub_if_data *sdata;
679 const struct cfg80211_chan_def *compat = NULL;
680 struct sta_info *sta;
682 lockdep_assert_held(&local->chanctx_mtx);
685 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
687 if (!ieee80211_sdata_running(sdata))
689 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
691 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
695 compat = &sdata->vif.bss_conf.chandef;
697 compat = cfg80211_chandef_compatible(
698 &sdata->vif.bss_conf.chandef, compat);
699 if (WARN_ON_ONCE(!compat))
703 /* TDLS peers can sometimes affect the chandef width */
704 list_for_each_entry_rcu(sta, &local->sta_list, list) {
705 if (!sta->uploaded ||
706 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
707 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
708 !sta->tdls_chandef.chan)
711 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
713 if (WARN_ON_ONCE(!compat))
721 ieee80211_change_chanctx(local, ctx, ctx, compat);
724 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
725 struct ieee80211_chanctx *chanctx)
729 lockdep_assert_held(&local->chanctx_mtx);
730 /* for ieee80211_is_radar_required */
731 lockdep_assert_held(&local->mtx);
733 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
735 if (radar_enabled == chanctx->conf.radar_enabled)
738 chanctx->conf.radar_enabled = radar_enabled;
740 if (!local->use_chanctx) {
741 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
742 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
745 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
748 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
749 struct ieee80211_chanctx *new_ctx)
751 struct ieee80211_local *local = sdata->local;
752 struct ieee80211_chanctx_conf *conf;
753 struct ieee80211_chanctx *curr_ctx = NULL;
756 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
759 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
760 lockdep_is_held(&local->chanctx_mtx));
763 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
765 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
767 list_del(&sdata->assigned_chanctx_list);
771 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
775 conf = &new_ctx->conf;
776 list_add(&sdata->assigned_chanctx_list,
777 &new_ctx->assigned_vifs);
781 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
783 sdata->vif.bss_conf.idle = !conf;
785 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
786 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
787 ieee80211_recalc_smps_chanctx(local, curr_ctx);
788 ieee80211_recalc_radar_chanctx(local, curr_ctx);
789 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
792 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
793 ieee80211_recalc_txpower(sdata, false);
794 ieee80211_recalc_chanctx_min_def(local, new_ctx);
797 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
798 sdata->vif.type != NL80211_IFTYPE_MONITOR)
799 ieee80211_bss_info_change_notify(sdata,
802 ieee80211_check_fast_xmit_iface(sdata);
807 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
808 struct ieee80211_chanctx *chanctx)
810 struct ieee80211_sub_if_data *sdata;
811 u8 rx_chains_static, rx_chains_dynamic;
813 lockdep_assert_held(&local->chanctx_mtx);
815 rx_chains_static = 1;
816 rx_chains_dynamic = 1;
819 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
820 u8 needed_static, needed_dynamic;
822 if (!ieee80211_sdata_running(sdata))
825 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
829 switch (sdata->vif.type) {
830 case NL80211_IFTYPE_P2P_DEVICE:
831 case NL80211_IFTYPE_NAN:
833 case NL80211_IFTYPE_STATION:
834 if (!sdata->u.mgd.associated)
837 case NL80211_IFTYPE_AP_VLAN:
839 case NL80211_IFTYPE_AP:
840 case NL80211_IFTYPE_ADHOC:
841 case NL80211_IFTYPE_MESH_POINT:
842 case NL80211_IFTYPE_OCB:
848 switch (sdata->smps_mode) {
850 WARN_ONCE(1, "Invalid SMPS mode %d\n",
853 case IEEE80211_SMPS_OFF:
854 needed_static = sdata->needed_rx_chains;
855 needed_dynamic = sdata->needed_rx_chains;
857 case IEEE80211_SMPS_DYNAMIC:
859 needed_dynamic = sdata->needed_rx_chains;
861 case IEEE80211_SMPS_STATIC:
867 rx_chains_static = max(rx_chains_static, needed_static);
868 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
871 /* Disable SMPS for the monitor interface */
872 sdata = rcu_dereference(local->monitor_sdata);
874 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
875 rx_chains_dynamic = rx_chains_static = local->rx_chains;
879 if (!local->use_chanctx) {
880 if (rx_chains_static > 1)
881 local->smps_mode = IEEE80211_SMPS_OFF;
882 else if (rx_chains_dynamic > 1)
883 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
885 local->smps_mode = IEEE80211_SMPS_STATIC;
886 ieee80211_hw_config(local, 0);
889 if (rx_chains_static == chanctx->conf.rx_chains_static &&
890 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
893 chanctx->conf.rx_chains_static = rx_chains_static;
894 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
895 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
899 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
902 struct ieee80211_local *local __maybe_unused = sdata->local;
903 struct ieee80211_sub_if_data *vlan;
904 struct ieee80211_chanctx_conf *conf;
906 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
909 lockdep_assert_held(&local->mtx);
911 /* Check that conf exists, even when clearing this function
912 * must be called with the AP's channel context still there
913 * as it would otherwise cause VLANs to have an invalid
914 * channel context pointer for a while, possibly pointing
915 * to a channel context that has already been freed.
917 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
918 lockdep_is_held(&local->chanctx_mtx));
924 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
925 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
928 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
931 struct ieee80211_local *local = sdata->local;
933 mutex_lock(&local->chanctx_mtx);
935 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
937 mutex_unlock(&local->chanctx_mtx);
940 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
942 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
944 lockdep_assert_held(&sdata->local->chanctx_mtx);
949 list_del(&sdata->reserved_chanctx_list);
950 sdata->reserved_chanctx = NULL;
952 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
953 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
954 if (WARN_ON(!ctx->replace_ctx))
957 WARN_ON(ctx->replace_ctx->replace_state !=
958 IEEE80211_CHANCTX_WILL_BE_REPLACED);
959 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
961 ctx->replace_ctx->replace_ctx = NULL;
962 ctx->replace_ctx->replace_state =
963 IEEE80211_CHANCTX_REPLACE_NONE;
965 list_del_rcu(&ctx->list);
966 kfree_rcu(ctx, rcu_head);
968 ieee80211_free_chanctx(sdata->local, ctx);
975 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
976 const struct cfg80211_chan_def *chandef,
977 enum ieee80211_chanctx_mode mode,
980 struct ieee80211_local *local = sdata->local;
981 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
983 lockdep_assert_held(&local->chanctx_mtx);
985 curr_ctx = ieee80211_vif_get_chanctx(sdata);
986 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
989 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
991 if (ieee80211_can_create_new_chanctx(local)) {
992 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
994 return PTR_ERR(new_ctx);
997 (curr_ctx->replace_state ==
998 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
999 !list_empty(&curr_ctx->reserved_vifs)) {
1001 * Another vif already requested this context
1002 * for a reservation. Find another one hoping
1003 * all vifs assigned to it will also switch
1006 * TODO: This needs a little more work as some
1007 * cases (more than 2 chanctx capable devices)
1008 * may fail which could otherwise succeed
1009 * provided some channel context juggling was
1012 * Consider ctx1..3, vif1..6, each ctx has 2
1013 * vifs. vif1 and vif2 from ctx1 request new
1014 * different chandefs starting 2 in-place
1015 * reserations with ctx4 and ctx5 replacing
1016 * ctx1 and ctx2 respectively. Next vif5 and
1017 * vif6 from ctx3 reserve ctx4. If vif3 and
1018 * vif4 remain on ctx2 as they are then this
1019 * fails unless `replace_ctx` from ctx5 is
1020 * replaced with ctx3.
1022 list_for_each_entry(ctx, &local->chanctx_list,
1024 if (ctx->replace_state !=
1025 IEEE80211_CHANCTX_REPLACE_NONE)
1028 if (!list_empty(&ctx->reserved_vifs))
1037 * If that's true then all available contexts already
1038 * have reservations and cannot be used.
1041 (curr_ctx->replace_state ==
1042 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1043 !list_empty(&curr_ctx->reserved_vifs))
1046 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
1050 new_ctx->replace_ctx = curr_ctx;
1051 new_ctx->replace_state =
1052 IEEE80211_CHANCTX_REPLACES_OTHER;
1054 curr_ctx->replace_ctx = new_ctx;
1055 curr_ctx->replace_state =
1056 IEEE80211_CHANCTX_WILL_BE_REPLACED;
1058 list_add_rcu(&new_ctx->list, &local->chanctx_list);
1062 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
1063 sdata->reserved_chanctx = new_ctx;
1064 sdata->reserved_chandef = *chandef;
1065 sdata->reserved_radar_required = radar_required;
1066 sdata->reserved_ready = false;
1072 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
1074 switch (sdata->vif.type) {
1075 case NL80211_IFTYPE_ADHOC:
1076 case NL80211_IFTYPE_AP:
1077 case NL80211_IFTYPE_MESH_POINT:
1078 case NL80211_IFTYPE_OCB:
1079 ieee80211_queue_work(&sdata->local->hw,
1080 &sdata->csa_finalize_work);
1082 case NL80211_IFTYPE_STATION:
1083 ieee80211_queue_work(&sdata->local->hw,
1084 &sdata->u.mgd.chswitch_work);
1086 case NL80211_IFTYPE_UNSPECIFIED:
1087 case NL80211_IFTYPE_AP_VLAN:
1088 case NL80211_IFTYPE_WDS:
1089 case NL80211_IFTYPE_MONITOR:
1090 case NL80211_IFTYPE_P2P_CLIENT:
1091 case NL80211_IFTYPE_P2P_GO:
1092 case NL80211_IFTYPE_P2P_DEVICE:
1093 case NL80211_IFTYPE_NAN:
1094 case NUM_NL80211_IFTYPES:
1101 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
1102 const struct cfg80211_chan_def *chandef)
1104 struct ieee80211_sub_if_data *vlan;
1106 sdata->vif.bss_conf.chandef = *chandef;
1108 if (sdata->vif.type != NL80211_IFTYPE_AP)
1111 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1112 vlan->vif.bss_conf.chandef = *chandef;
1116 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
1118 struct ieee80211_local *local = sdata->local;
1119 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1120 struct ieee80211_chanctx *old_ctx, *new_ctx;
1121 const struct cfg80211_chan_def *chandef;
1125 lockdep_assert_held(&local->mtx);
1126 lockdep_assert_held(&local->chanctx_mtx);
1128 new_ctx = sdata->reserved_chanctx;
1129 old_ctx = ieee80211_vif_get_chanctx(sdata);
1131 if (WARN_ON(!sdata->reserved_ready))
1134 if (WARN_ON(!new_ctx))
1137 if (WARN_ON(!old_ctx))
1140 if (WARN_ON(new_ctx->replace_state ==
1141 IEEE80211_CHANCTX_REPLACES_OTHER))
1144 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1145 &sdata->reserved_chandef);
1146 if (WARN_ON(!chandef))
1149 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
1150 changed = BSS_CHANGED_BANDWIDTH;
1152 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1154 ieee80211_change_chanctx(local, new_ctx, old_ctx, chandef);
1156 vif_chsw[0].vif = &sdata->vif;
1157 vif_chsw[0].old_ctx = &old_ctx->conf;
1158 vif_chsw[0].new_ctx = &new_ctx->conf;
1160 list_del(&sdata->reserved_chanctx_list);
1161 sdata->reserved_chanctx = NULL;
1163 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1164 CHANCTX_SWMODE_REASSIGN_VIF);
1166 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1167 ieee80211_free_chanctx(local, new_ctx);
1172 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1173 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1175 if (sdata->vif.type == NL80211_IFTYPE_AP)
1176 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1178 ieee80211_check_fast_xmit_iface(sdata);
1180 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1181 ieee80211_free_chanctx(local, old_ctx);
1183 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1184 ieee80211_recalc_smps_chanctx(local, new_ctx);
1185 ieee80211_recalc_radar_chanctx(local, new_ctx);
1188 ieee80211_bss_info_change_notify(sdata, changed);
1191 ieee80211_vif_chanctx_reservation_complete(sdata);
1196 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1198 struct ieee80211_local *local = sdata->local;
1199 struct ieee80211_chanctx *old_ctx, *new_ctx;
1200 const struct cfg80211_chan_def *chandef;
1203 old_ctx = ieee80211_vif_get_chanctx(sdata);
1204 new_ctx = sdata->reserved_chanctx;
1206 if (WARN_ON(!sdata->reserved_ready))
1209 if (WARN_ON(old_ctx))
1212 if (WARN_ON(!new_ctx))
1215 if (WARN_ON(new_ctx->replace_state ==
1216 IEEE80211_CHANCTX_REPLACES_OTHER))
1219 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1220 &sdata->reserved_chandef);
1221 if (WARN_ON(!chandef))
1224 ieee80211_change_chanctx(local, new_ctx, new_ctx, chandef);
1226 list_del(&sdata->reserved_chanctx_list);
1227 sdata->reserved_chanctx = NULL;
1229 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1231 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1232 ieee80211_free_chanctx(local, new_ctx);
1238 ieee80211_vif_chanctx_reservation_complete(sdata);
1243 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1245 struct ieee80211_chanctx *old_ctx, *new_ctx;
1247 lockdep_assert_held(&sdata->local->chanctx_mtx);
1249 new_ctx = sdata->reserved_chanctx;
1250 old_ctx = ieee80211_vif_get_chanctx(sdata);
1255 if (WARN_ON(!new_ctx))
1258 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1261 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1267 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1268 struct ieee80211_chanctx *new_ctx)
1270 const struct cfg80211_chan_def *chandef;
1272 lockdep_assert_held(&local->mtx);
1273 lockdep_assert_held(&local->chanctx_mtx);
1275 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1276 if (WARN_ON(!chandef))
1279 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1280 local->_oper_chandef = *chandef;
1281 ieee80211_hw_config(local, 0);
1286 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1289 struct ieee80211_vif_chanctx_switch *vif_chsw;
1290 struct ieee80211_sub_if_data *sdata;
1291 struct ieee80211_chanctx *ctx, *old_ctx;
1294 lockdep_assert_held(&local->mtx);
1295 lockdep_assert_held(&local->chanctx_mtx);
1297 vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
1302 list_for_each_entry(ctx, &local->chanctx_list, list) {
1303 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1306 if (WARN_ON(!ctx->replace_ctx)) {
1311 list_for_each_entry(sdata, &ctx->reserved_vifs,
1312 reserved_chanctx_list) {
1313 if (!ieee80211_vif_has_in_place_reservation(
1317 old_ctx = ieee80211_vif_get_chanctx(sdata);
1318 vif_chsw[i].vif = &sdata->vif;
1319 vif_chsw[i].old_ctx = &old_ctx->conf;
1320 vif_chsw[i].new_ctx = &ctx->conf;
1326 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1327 CHANCTX_SWMODE_SWAP_CONTEXTS);
1334 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1336 struct ieee80211_chanctx *ctx;
1339 lockdep_assert_held(&local->mtx);
1340 lockdep_assert_held(&local->chanctx_mtx);
1342 list_for_each_entry(ctx, &local->chanctx_list, list) {
1343 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1346 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1349 ieee80211_del_chanctx(local, ctx->replace_ctx);
1350 err = ieee80211_add_chanctx(local, ctx);
1358 WARN_ON(ieee80211_add_chanctx(local, ctx));
1359 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1360 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1363 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1366 ieee80211_del_chanctx(local, ctx);
1367 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1373 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1375 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1376 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1377 struct ieee80211_chanctx *new_ctx = NULL;
1378 int err, n_assigned, n_reserved, n_ready;
1379 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1381 lockdep_assert_held(&local->mtx);
1382 lockdep_assert_held(&local->chanctx_mtx);
1385 * If there are 2 independent pairs of channel contexts performing
1386 * cross-switch of their vifs this code will still wait until both are
1387 * ready even though it could be possible to switch one before the
1390 * For practical reasons and code simplicity just do a single huge
1395 * Verify if the reservation is still feasible.
1396 * - if it's not then disconnect
1397 * - if it is but not all vifs necessary are ready then defer
1400 list_for_each_entry(ctx, &local->chanctx_list, list) {
1401 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1404 if (WARN_ON(!ctx->replace_ctx)) {
1409 if (!local->use_chanctx)
1418 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1419 assigned_chanctx_list) {
1421 if (sdata->reserved_chanctx) {
1423 if (sdata->reserved_ready)
1428 if (n_assigned != n_reserved) {
1429 if (n_ready == n_reserved) {
1430 wiphy_info(local->hw.wiphy,
1431 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1439 ctx->conf.radar_enabled = false;
1440 list_for_each_entry(sdata, &ctx->reserved_vifs,
1441 reserved_chanctx_list) {
1442 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1443 !sdata->reserved_ready)
1446 old_ctx = ieee80211_vif_get_chanctx(sdata);
1448 if (old_ctx->replace_state ==
1449 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1457 if (sdata->reserved_radar_required)
1458 ctx->conf.radar_enabled = true;
1462 if (WARN_ON(n_ctx == 0) ||
1463 WARN_ON(n_vifs_switch == 0 &&
1464 n_vifs_assign == 0 &&
1465 n_vifs_ctxless == 0) ||
1466 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1467 WARN_ON(!new_ctx && !local->use_chanctx)) {
1473 * All necessary vifs are ready. Perform the switch now depending on
1474 * reservations and driver capabilities.
1477 if (local->use_chanctx) {
1478 if (n_vifs_switch > 0) {
1479 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1484 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1485 err = ieee80211_chsw_switch_ctxs(local);
1490 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1496 * Update all structures, values and pointers to point to new channel
1499 list_for_each_entry(ctx, &local->chanctx_list, list) {
1500 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1503 if (WARN_ON(!ctx->replace_ctx)) {
1508 list_for_each_entry(sdata, &ctx->reserved_vifs,
1509 reserved_chanctx_list) {
1512 if (!ieee80211_vif_has_in_place_reservation(sdata))
1515 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1517 if (sdata->vif.type == NL80211_IFTYPE_AP)
1518 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1521 ieee80211_check_fast_xmit_iface(sdata);
1523 sdata->radar_required = sdata->reserved_radar_required;
1525 if (sdata->vif.bss_conf.chandef.width !=
1526 sdata->reserved_chandef.width)
1527 changed = BSS_CHANGED_BANDWIDTH;
1529 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1531 ieee80211_bss_info_change_notify(sdata,
1534 ieee80211_recalc_txpower(sdata, false);
1537 ieee80211_recalc_chanctx_chantype(local, ctx);
1538 ieee80211_recalc_smps_chanctx(local, ctx);
1539 ieee80211_recalc_radar_chanctx(local, ctx);
1540 ieee80211_recalc_chanctx_min_def(local, ctx);
1542 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1543 reserved_chanctx_list) {
1544 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1547 list_del(&sdata->reserved_chanctx_list);
1548 list_move(&sdata->assigned_chanctx_list,
1549 &ctx->assigned_vifs);
1550 sdata->reserved_chanctx = NULL;
1552 ieee80211_vif_chanctx_reservation_complete(sdata);
1556 * This context might have been a dependency for an already
1557 * ready re-assign reservation interface that was deferred. Do
1558 * not propagate error to the caller though. The in-place
1559 * reservation for originally requested interface has already
1560 * succeeded at this point.
1562 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1563 reserved_chanctx_list) {
1564 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1568 if (WARN_ON(sdata->reserved_chanctx != ctx))
1571 if (!sdata->reserved_ready)
1574 if (ieee80211_vif_get_chanctx(sdata))
1575 err = ieee80211_vif_use_reserved_reassign(
1578 err = ieee80211_vif_use_reserved_assign(sdata);
1582 "failed to finalize (re-)assign reservation (err=%d)\n",
1584 ieee80211_vif_unreserve_chanctx(sdata);
1585 cfg80211_stop_iface(local->hw.wiphy,
1593 * Finally free old contexts
1596 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1597 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1600 ctx->replace_ctx->replace_ctx = NULL;
1601 ctx->replace_ctx->replace_state =
1602 IEEE80211_CHANCTX_REPLACE_NONE;
1604 list_del_rcu(&ctx->list);
1605 kfree_rcu(ctx, rcu_head);
1611 list_for_each_entry(ctx, &local->chanctx_list, list) {
1612 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1615 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1616 reserved_chanctx_list) {
1617 ieee80211_vif_unreserve_chanctx(sdata);
1618 ieee80211_vif_chanctx_reservation_complete(sdata);
1625 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1627 struct ieee80211_local *local = sdata->local;
1628 struct ieee80211_chanctx_conf *conf;
1629 struct ieee80211_chanctx *ctx;
1630 bool use_reserved_switch = false;
1632 lockdep_assert_held(&local->chanctx_mtx);
1634 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1635 lockdep_is_held(&local->chanctx_mtx));
1639 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1641 if (sdata->reserved_chanctx) {
1642 if (sdata->reserved_chanctx->replace_state ==
1643 IEEE80211_CHANCTX_REPLACES_OTHER &&
1644 ieee80211_chanctx_num_reserved(local,
1645 sdata->reserved_chanctx) > 1)
1646 use_reserved_switch = true;
1648 ieee80211_vif_unreserve_chanctx(sdata);
1651 ieee80211_assign_vif_chanctx(sdata, NULL);
1652 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1653 ieee80211_free_chanctx(local, ctx);
1655 sdata->radar_required = false;
1657 /* Unreserving may ready an in-place reservation. */
1658 if (use_reserved_switch)
1659 ieee80211_vif_use_reserved_switch(local);
1662 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1663 const struct cfg80211_chan_def *chandef,
1664 enum ieee80211_chanctx_mode mode)
1666 struct ieee80211_local *local = sdata->local;
1667 struct ieee80211_chanctx *ctx;
1668 u8 radar_detect_width = 0;
1671 lockdep_assert_held(&local->mtx);
1673 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1675 mutex_lock(&local->chanctx_mtx);
1677 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1679 sdata->wdev.iftype);
1683 radar_detect_width = BIT(chandef->width);
1685 sdata->radar_required = ret;
1687 ret = ieee80211_check_combinations(sdata, chandef, mode,
1688 radar_detect_width);
1692 __ieee80211_vif_release_channel(sdata);
1694 ctx = ieee80211_find_chanctx(local, chandef, mode);
1696 ctx = ieee80211_new_chanctx(local, chandef, mode);
1702 ieee80211_vif_update_chandef(sdata, chandef);
1704 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1706 /* if assign fails refcount stays the same */
1707 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1708 ieee80211_free_chanctx(local, ctx);
1712 ieee80211_recalc_smps_chanctx(local, ctx);
1713 ieee80211_recalc_radar_chanctx(local, ctx);
1716 sdata->radar_required = false;
1718 mutex_unlock(&local->chanctx_mtx);
1722 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1724 struct ieee80211_local *local = sdata->local;
1725 struct ieee80211_chanctx *new_ctx;
1726 struct ieee80211_chanctx *old_ctx;
1729 lockdep_assert_held(&local->mtx);
1730 lockdep_assert_held(&local->chanctx_mtx);
1732 new_ctx = sdata->reserved_chanctx;
1733 old_ctx = ieee80211_vif_get_chanctx(sdata);
1735 if (WARN_ON(!new_ctx))
1738 if (WARN_ON(new_ctx->replace_state ==
1739 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1742 if (WARN_ON(sdata->reserved_ready))
1745 sdata->reserved_ready = true;
1747 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1749 return ieee80211_vif_use_reserved_reassign(sdata);
1751 return ieee80211_vif_use_reserved_assign(sdata);
1755 * In-place reservation may need to be finalized now either if:
1756 * a) sdata is taking part in the swapping itself and is the last one
1757 * b) sdata has switched with a re-assign reservation to an existing
1758 * context readying in-place switching of old_ctx
1760 * In case of (b) do not propagate the error up because the requested
1761 * sdata already switched successfully. Just spill an extra warning.
1762 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1763 * interfaces upon failure.
1766 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1767 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1768 err = ieee80211_vif_use_reserved_switch(local);
1769 if (err && err != -EAGAIN) {
1770 if (new_ctx->replace_state ==
1771 IEEE80211_CHANCTX_REPLACES_OTHER)
1774 wiphy_info(local->hw.wiphy,
1775 "depending in-place reservation failed (err=%d)\n",
1783 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1784 const struct cfg80211_chan_def *chandef,
1787 struct ieee80211_local *local = sdata->local;
1788 struct ieee80211_chanctx_conf *conf;
1789 struct ieee80211_chanctx *ctx;
1790 const struct cfg80211_chan_def *compat;
1793 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1794 IEEE80211_CHAN_DISABLED))
1797 mutex_lock(&local->chanctx_mtx);
1798 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1803 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1804 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1809 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1810 lockdep_is_held(&local->chanctx_mtx));
1816 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1818 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1824 switch (ctx->replace_state) {
1825 case IEEE80211_CHANCTX_REPLACE_NONE:
1826 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1831 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1832 /* TODO: Perhaps the bandwidth change could be treated as a
1833 * reservation itself? */
1836 case IEEE80211_CHANCTX_REPLACES_OTHER:
1837 /* channel context that is going to replace another channel
1838 * context doesn't really exist and shouldn't be assigned
1844 ieee80211_vif_update_chandef(sdata, chandef);
1846 ieee80211_recalc_chanctx_chantype(local, ctx);
1848 *changed |= BSS_CHANGED_BANDWIDTH;
1851 mutex_unlock(&local->chanctx_mtx);
1855 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1857 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1859 lockdep_assert_held(&sdata->local->mtx);
1861 mutex_lock(&sdata->local->chanctx_mtx);
1862 __ieee80211_vif_release_channel(sdata);
1863 mutex_unlock(&sdata->local->chanctx_mtx);
1866 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1868 struct ieee80211_local *local = sdata->local;
1869 struct ieee80211_sub_if_data *ap;
1870 struct ieee80211_chanctx_conf *conf;
1872 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1875 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1877 mutex_lock(&local->chanctx_mtx);
1879 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1880 lockdep_is_held(&local->chanctx_mtx));
1881 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1882 mutex_unlock(&local->chanctx_mtx);
1885 void ieee80211_iter_chan_contexts_atomic(
1886 struct ieee80211_hw *hw,
1887 void (*iter)(struct ieee80211_hw *hw,
1888 struct ieee80211_chanctx_conf *chanctx_conf,
1892 struct ieee80211_local *local = hw_to_local(hw);
1893 struct ieee80211_chanctx *ctx;
1896 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1897 if (ctx->driver_present)
1898 iter(hw, &ctx->conf, iter_data);
1901 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);