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"
14 static int ieee80211_chanctx_num_assigned(struct ieee80211_local
*local
,
15 struct ieee80211_chanctx
*ctx
)
17 struct ieee80211_sub_if_data
*sdata
;
20 lockdep_assert_held(&local
->chanctx_mtx
);
22 list_for_each_entry(sdata
, &ctx
->assigned_vifs
, assigned_chanctx_list
)
28 static int ieee80211_chanctx_num_reserved(struct ieee80211_local
*local
,
29 struct ieee80211_chanctx
*ctx
)
31 struct ieee80211_sub_if_data
*sdata
;
34 lockdep_assert_held(&local
->chanctx_mtx
);
36 list_for_each_entry(sdata
, &ctx
->reserved_vifs
, reserved_chanctx_list
)
42 int ieee80211_chanctx_refcount(struct ieee80211_local
*local
,
43 struct ieee80211_chanctx
*ctx
)
45 return ieee80211_chanctx_num_assigned(local
, ctx
) +
46 ieee80211_chanctx_num_reserved(local
, ctx
);
49 static int ieee80211_num_chanctx(struct ieee80211_local
*local
)
51 struct ieee80211_chanctx
*ctx
;
54 lockdep_assert_held(&local
->chanctx_mtx
);
56 list_for_each_entry(ctx
, &local
->chanctx_list
, list
)
62 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local
*local
)
64 lockdep_assert_held(&local
->chanctx_mtx
);
65 return ieee80211_num_chanctx(local
) < ieee80211_max_num_channels(local
);
68 static struct ieee80211_chanctx
*
69 ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data
*sdata
)
71 struct ieee80211_local
*local __maybe_unused
= sdata
->local
;
72 struct ieee80211_chanctx_conf
*conf
;
74 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
75 lockdep_is_held(&local
->chanctx_mtx
));
79 return container_of(conf
, struct ieee80211_chanctx
, conf
);
82 static const struct cfg80211_chan_def
*
83 ieee80211_chanctx_reserved_chandef(struct ieee80211_local
*local
,
84 struct ieee80211_chanctx
*ctx
,
85 const struct cfg80211_chan_def
*compat
)
87 struct ieee80211_sub_if_data
*sdata
;
89 lockdep_assert_held(&local
->chanctx_mtx
);
91 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
92 reserved_chanctx_list
) {
94 compat
= &sdata
->reserved_chandef
;
96 compat
= cfg80211_chandef_compatible(&sdata
->reserved_chandef
,
105 static const struct cfg80211_chan_def
*
106 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local
*local
,
107 struct ieee80211_chanctx
*ctx
,
108 const struct cfg80211_chan_def
*compat
)
110 struct ieee80211_sub_if_data
*sdata
;
112 lockdep_assert_held(&local
->chanctx_mtx
);
114 list_for_each_entry(sdata
, &ctx
->assigned_vifs
,
115 assigned_chanctx_list
) {
116 if (sdata
->reserved_chanctx
!= NULL
)
120 compat
= &sdata
->vif
.bss_conf
.chandef
;
122 compat
= cfg80211_chandef_compatible(
123 &sdata
->vif
.bss_conf
.chandef
, compat
);
131 static const struct cfg80211_chan_def
*
132 ieee80211_chanctx_combined_chandef(struct ieee80211_local
*local
,
133 struct ieee80211_chanctx
*ctx
,
134 const struct cfg80211_chan_def
*compat
)
136 lockdep_assert_held(&local
->chanctx_mtx
);
138 compat
= ieee80211_chanctx_reserved_chandef(local
, ctx
, compat
);
142 compat
= ieee80211_chanctx_non_reserved_chandef(local
, ctx
, compat
);
150 ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local
*local
,
151 struct ieee80211_chanctx
*ctx
,
152 const struct cfg80211_chan_def
*def
)
154 lockdep_assert_held(&local
->chanctx_mtx
);
156 if (ieee80211_chanctx_combined_chandef(local
, ctx
, def
))
159 if (!list_empty(&ctx
->reserved_vifs
) &&
160 ieee80211_chanctx_reserved_chandef(local
, ctx
, def
))
166 static struct ieee80211_chanctx
*
167 ieee80211_find_reservation_chanctx(struct ieee80211_local
*local
,
168 const struct cfg80211_chan_def
*chandef
,
169 enum ieee80211_chanctx_mode mode
)
171 struct ieee80211_chanctx
*ctx
;
173 lockdep_assert_held(&local
->chanctx_mtx
);
175 if (mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
178 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
179 if (ctx
->replace_state
== IEEE80211_CHANCTX_WILL_BE_REPLACED
)
182 if (ctx
->mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
185 if (!ieee80211_chanctx_can_reserve_chandef(local
, ctx
,
195 static enum nl80211_chan_width
ieee80211_get_sta_bw(struct sta_info
*sta
)
197 enum ieee80211_sta_rx_bandwidth width
= ieee80211_sta_cap_rx_bw(sta
);
200 case IEEE80211_STA_RX_BW_20
:
201 if (sta
->sta
.ht_cap
.ht_supported
)
202 return NL80211_CHAN_WIDTH_20
;
204 return NL80211_CHAN_WIDTH_20_NOHT
;
205 case IEEE80211_STA_RX_BW_40
:
206 return NL80211_CHAN_WIDTH_40
;
207 case IEEE80211_STA_RX_BW_80
:
208 return NL80211_CHAN_WIDTH_80
;
209 case IEEE80211_STA_RX_BW_160
:
211 * This applied for both 160 and 80+80. since we use
212 * the returned value to consider degradation of
213 * ctx->conf.min_def, we have to make sure to take
214 * the bigger one (NL80211_CHAN_WIDTH_160).
215 * Otherwise we might try degrading even when not
216 * needed, as the max required sta_bw returned (80+80)
217 * might be smaller than the configured bw (160).
219 return NL80211_CHAN_WIDTH_160
;
222 return NL80211_CHAN_WIDTH_20
;
226 static enum nl80211_chan_width
227 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data
*sdata
)
229 enum nl80211_chan_width max_bw
= NL80211_CHAN_WIDTH_20_NOHT
;
230 struct sta_info
*sta
;
233 list_for_each_entry_rcu(sta
, &sdata
->local
->sta_list
, list
) {
234 if (sdata
!= sta
->sdata
&&
235 !(sta
->sdata
->bss
&& sta
->sdata
->bss
== sdata
->bss
))
238 max_bw
= max(max_bw
, ieee80211_get_sta_bw(sta
));
245 static enum nl80211_chan_width
246 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local
*local
,
247 struct ieee80211_chanctx_conf
*conf
)
249 struct ieee80211_sub_if_data
*sdata
;
250 enum nl80211_chan_width max_bw
= NL80211_CHAN_WIDTH_20_NOHT
;
253 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
254 struct ieee80211_vif
*vif
= &sdata
->vif
;
255 enum nl80211_chan_width width
= NL80211_CHAN_WIDTH_20_NOHT
;
257 if (!ieee80211_sdata_running(sdata
))
260 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) != conf
)
264 case NL80211_IFTYPE_AP
:
265 case NL80211_IFTYPE_AP_VLAN
:
266 width
= ieee80211_get_max_required_bw(sdata
);
268 case NL80211_IFTYPE_STATION
:
270 * The ap's sta->bandwidth is not set yet at this
271 * point, so take the width from the chandef, but
272 * account also for TDLS peers
274 width
= max(vif
->bss_conf
.chandef
.width
,
275 ieee80211_get_max_required_bw(sdata
));
277 case NL80211_IFTYPE_P2P_DEVICE
:
278 case NL80211_IFTYPE_NAN
:
280 case NL80211_IFTYPE_ADHOC
:
281 case NL80211_IFTYPE_MESH_POINT
:
282 case NL80211_IFTYPE_OCB
:
283 width
= vif
->bss_conf
.chandef
.width
;
285 case NL80211_IFTYPE_WDS
:
286 case NL80211_IFTYPE_UNSPECIFIED
:
287 case NUM_NL80211_IFTYPES
:
288 case NL80211_IFTYPE_MONITOR
:
289 case NL80211_IFTYPE_P2P_CLIENT
:
290 case NL80211_IFTYPE_P2P_GO
:
293 max_bw
= max(max_bw
, width
);
296 /* use the configured bandwidth in case of monitor interface */
297 sdata
= rcu_dereference(local
->monitor_sdata
);
298 if (sdata
&& rcu_access_pointer(sdata
->vif
.chanctx_conf
) == conf
)
299 max_bw
= max(max_bw
, conf
->def
.width
);
307 * recalc the min required chan width of the channel context, which is
308 * the max of min required widths of all the interfaces bound to this
311 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local
*local
,
312 struct ieee80211_chanctx
*ctx
)
314 enum nl80211_chan_width max_bw
;
315 struct cfg80211_chan_def min_def
;
317 lockdep_assert_held(&local
->chanctx_mtx
);
319 /* don't optimize non-20MHz based and radar_enabled confs */
320 if (ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_5
||
321 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_10
||
322 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_1
||
323 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_2
||
324 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_4
||
325 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_8
||
326 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_16
||
327 ctx
->conf
.radar_enabled
) {
328 ctx
->conf
.min_def
= ctx
->conf
.def
;
332 max_bw
= ieee80211_get_chanctx_max_required_bw(local
, &ctx
->conf
);
334 /* downgrade chandef up to max_bw */
335 min_def
= ctx
->conf
.def
;
336 while (min_def
.width
> max_bw
)
337 ieee80211_chandef_downgrade(&min_def
);
339 if (cfg80211_chandef_identical(&ctx
->conf
.min_def
, &min_def
))
342 ctx
->conf
.min_def
= min_def
;
343 if (!ctx
->driver_present
)
346 drv_change_chanctx(local
, ctx
, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH
);
349 static void ieee80211_chan_bw_change(struct ieee80211_local
*local
,
350 struct ieee80211_chanctx
*ctx
)
352 struct sta_info
*sta
;
353 struct ieee80211_supported_band
*sband
=
354 local
->hw
.wiphy
->bands
[ctx
->conf
.def
.chan
->band
];
357 list_for_each_entry_rcu(sta
, &local
->sta_list
,
359 enum ieee80211_sta_rx_bandwidth new_sta_bw
;
361 if (!ieee80211_sdata_running(sta
->sdata
))
364 if (rcu_access_pointer(sta
->sdata
->vif
.chanctx_conf
) !=
368 new_sta_bw
= ieee80211_sta_cur_vht_bw(sta
);
369 if (new_sta_bw
== sta
->sta
.bandwidth
)
372 sta
->sta
.bandwidth
= new_sta_bw
;
373 rate_control_rate_update(local
, sband
, sta
,
374 IEEE80211_RC_BW_CHANGED
);
379 static void ieee80211_change_chanctx(struct ieee80211_local
*local
,
380 struct ieee80211_chanctx
*ctx
,
381 const struct cfg80211_chan_def
*chandef
)
383 enum nl80211_chan_width width
;
385 if (cfg80211_chandef_identical(&ctx
->conf
.def
, chandef
)) {
386 ieee80211_recalc_chanctx_min_def(local
, ctx
);
390 WARN_ON(!cfg80211_chandef_compatible(&ctx
->conf
.def
, chandef
));
392 width
= ctx
->conf
.def
.width
;
393 ctx
->conf
.def
= *chandef
;
395 /* expected to handle only 20/40/80/160 channel widths */
396 switch (chandef
->width
) {
397 case NL80211_CHAN_WIDTH_20_NOHT
:
398 case NL80211_CHAN_WIDTH_20
:
399 case NL80211_CHAN_WIDTH_40
:
400 case NL80211_CHAN_WIDTH_80
:
401 case NL80211_CHAN_WIDTH_80P80
:
402 case NL80211_CHAN_WIDTH_160
:
408 if (chandef
->width
< width
)
409 ieee80211_chan_bw_change(local
, ctx
);
411 drv_change_chanctx(local
, ctx
, IEEE80211_CHANCTX_CHANGE_WIDTH
);
412 ieee80211_recalc_chanctx_min_def(local
, ctx
);
414 if (!local
->use_chanctx
) {
415 local
->_oper_chandef
= *chandef
;
416 ieee80211_hw_config(local
, 0);
419 if (chandef
->width
> width
)
420 ieee80211_chan_bw_change(local
, ctx
);
423 static struct ieee80211_chanctx
*
424 ieee80211_find_chanctx(struct ieee80211_local
*local
,
425 const struct cfg80211_chan_def
*chandef
,
426 enum ieee80211_chanctx_mode mode
)
428 struct ieee80211_chanctx
*ctx
;
430 lockdep_assert_held(&local
->chanctx_mtx
);
432 if (mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
435 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
436 const struct cfg80211_chan_def
*compat
;
438 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACE_NONE
)
441 if (ctx
->mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
444 compat
= cfg80211_chandef_compatible(&ctx
->conf
.def
, chandef
);
448 compat
= ieee80211_chanctx_reserved_chandef(local
, ctx
,
453 ieee80211_change_chanctx(local
, ctx
, compat
);
461 bool ieee80211_is_radar_required(struct ieee80211_local
*local
)
463 struct ieee80211_sub_if_data
*sdata
;
465 lockdep_assert_held(&local
->mtx
);
468 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
469 if (sdata
->radar_required
) {
480 ieee80211_chanctx_radar_required(struct ieee80211_local
*local
,
481 struct ieee80211_chanctx
*ctx
)
483 struct ieee80211_chanctx_conf
*conf
= &ctx
->conf
;
484 struct ieee80211_sub_if_data
*sdata
;
485 bool required
= false;
487 lockdep_assert_held(&local
->chanctx_mtx
);
488 lockdep_assert_held(&local
->mtx
);
491 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
492 if (!ieee80211_sdata_running(sdata
))
494 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) != conf
)
496 if (!sdata
->radar_required
)
507 static struct ieee80211_chanctx
*
508 ieee80211_alloc_chanctx(struct ieee80211_local
*local
,
509 const struct cfg80211_chan_def
*chandef
,
510 enum ieee80211_chanctx_mode mode
)
512 struct ieee80211_chanctx
*ctx
;
514 lockdep_assert_held(&local
->chanctx_mtx
);
516 ctx
= kzalloc(sizeof(*ctx
) + local
->hw
.chanctx_data_size
, GFP_KERNEL
);
520 INIT_LIST_HEAD(&ctx
->assigned_vifs
);
521 INIT_LIST_HEAD(&ctx
->reserved_vifs
);
522 ctx
->conf
.def
= *chandef
;
523 ctx
->conf
.rx_chains_static
= 1;
524 ctx
->conf
.rx_chains_dynamic
= 1;
526 ctx
->conf
.radar_enabled
= false;
527 ieee80211_recalc_chanctx_min_def(local
, ctx
);
532 static int ieee80211_add_chanctx(struct ieee80211_local
*local
,
533 struct ieee80211_chanctx
*ctx
)
538 lockdep_assert_held(&local
->mtx
);
539 lockdep_assert_held(&local
->chanctx_mtx
);
541 if (!local
->use_chanctx
)
542 local
->hw
.conf
.radar_enabled
= ctx
->conf
.radar_enabled
;
544 /* turn idle off *before* setting channel -- some drivers need that */
545 changed
= ieee80211_idle_off(local
);
547 ieee80211_hw_config(local
, changed
);
549 if (!local
->use_chanctx
) {
550 local
->_oper_chandef
= ctx
->conf
.def
;
551 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
553 err
= drv_add_chanctx(local
, ctx
);
555 ieee80211_recalc_idle(local
);
563 static struct ieee80211_chanctx
*
564 ieee80211_new_chanctx(struct ieee80211_local
*local
,
565 const struct cfg80211_chan_def
*chandef
,
566 enum ieee80211_chanctx_mode mode
)
568 struct ieee80211_chanctx
*ctx
;
571 lockdep_assert_held(&local
->mtx
);
572 lockdep_assert_held(&local
->chanctx_mtx
);
574 ctx
= ieee80211_alloc_chanctx(local
, chandef
, mode
);
576 return ERR_PTR(-ENOMEM
);
578 err
= ieee80211_add_chanctx(local
, ctx
);
584 list_add_rcu(&ctx
->list
, &local
->chanctx_list
);
588 static void ieee80211_del_chanctx(struct ieee80211_local
*local
,
589 struct ieee80211_chanctx
*ctx
)
591 lockdep_assert_held(&local
->chanctx_mtx
);
593 if (!local
->use_chanctx
) {
594 struct cfg80211_chan_def
*chandef
= &local
->_oper_chandef
;
595 /* S1G doesn't have 20MHz, so get the correct width for the
598 if (chandef
->chan
->band
== NL80211_BAND_S1GHZ
)
600 ieee80211_s1g_channel_width(chandef
->chan
);
602 chandef
->width
= NL80211_CHAN_WIDTH_20_NOHT
;
603 chandef
->center_freq1
= chandef
->chan
->center_freq
;
604 chandef
->freq1_offset
= chandef
->chan
->freq_offset
;
605 chandef
->center_freq2
= 0;
607 /* NOTE: Disabling radar is only valid here for
608 * single channel context. To be sure, check it ...
610 WARN_ON(local
->hw
.conf
.radar_enabled
&&
611 !list_empty(&local
->chanctx_list
));
613 local
->hw
.conf
.radar_enabled
= false;
615 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
617 drv_remove_chanctx(local
, ctx
);
620 ieee80211_recalc_idle(local
);
623 static void ieee80211_free_chanctx(struct ieee80211_local
*local
,
624 struct ieee80211_chanctx
*ctx
)
626 lockdep_assert_held(&local
->chanctx_mtx
);
628 WARN_ON_ONCE(ieee80211_chanctx_refcount(local
, ctx
) != 0);
630 list_del_rcu(&ctx
->list
);
631 ieee80211_del_chanctx(local
, ctx
);
632 kfree_rcu(ctx
, rcu_head
);
635 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local
*local
,
636 struct ieee80211_chanctx
*ctx
)
638 struct ieee80211_chanctx_conf
*conf
= &ctx
->conf
;
639 struct ieee80211_sub_if_data
*sdata
;
640 const struct cfg80211_chan_def
*compat
= NULL
;
641 struct sta_info
*sta
;
643 lockdep_assert_held(&local
->chanctx_mtx
);
646 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
648 if (!ieee80211_sdata_running(sdata
))
650 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) != conf
)
652 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
656 compat
= &sdata
->vif
.bss_conf
.chandef
;
658 compat
= cfg80211_chandef_compatible(
659 &sdata
->vif
.bss_conf
.chandef
, compat
);
660 if (WARN_ON_ONCE(!compat
))
664 /* TDLS peers can sometimes affect the chandef width */
665 list_for_each_entry_rcu(sta
, &local
->sta_list
, list
) {
666 if (!sta
->uploaded
||
667 !test_sta_flag(sta
, WLAN_STA_TDLS_WIDER_BW
) ||
668 !test_sta_flag(sta
, WLAN_STA_AUTHORIZED
) ||
669 !sta
->tdls_chandef
.chan
)
672 compat
= cfg80211_chandef_compatible(&sta
->tdls_chandef
,
674 if (WARN_ON_ONCE(!compat
))
682 ieee80211_change_chanctx(local
, ctx
, compat
);
685 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local
*local
,
686 struct ieee80211_chanctx
*chanctx
)
690 lockdep_assert_held(&local
->chanctx_mtx
);
691 /* for ieee80211_is_radar_required */
692 lockdep_assert_held(&local
->mtx
);
694 radar_enabled
= ieee80211_chanctx_radar_required(local
, chanctx
);
696 if (radar_enabled
== chanctx
->conf
.radar_enabled
)
699 chanctx
->conf
.radar_enabled
= radar_enabled
;
701 if (!local
->use_chanctx
) {
702 local
->hw
.conf
.radar_enabled
= chanctx
->conf
.radar_enabled
;
703 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
706 drv_change_chanctx(local
, chanctx
, IEEE80211_CHANCTX_CHANGE_RADAR
);
709 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data
*sdata
,
710 struct ieee80211_chanctx
*new_ctx
)
712 struct ieee80211_local
*local
= sdata
->local
;
713 struct ieee80211_chanctx_conf
*conf
;
714 struct ieee80211_chanctx
*curr_ctx
= NULL
;
717 if (WARN_ON(sdata
->vif
.type
== NL80211_IFTYPE_NAN
))
720 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
721 lockdep_is_held(&local
->chanctx_mtx
));
724 curr_ctx
= container_of(conf
, struct ieee80211_chanctx
, conf
);
726 drv_unassign_vif_chanctx(local
, sdata
, curr_ctx
);
728 list_del(&sdata
->assigned_chanctx_list
);
732 ret
= drv_assign_vif_chanctx(local
, sdata
, new_ctx
);
736 conf
= &new_ctx
->conf
;
737 list_add(&sdata
->assigned_chanctx_list
,
738 &new_ctx
->assigned_vifs
);
742 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, conf
);
744 sdata
->vif
.bss_conf
.idle
= !conf
;
746 if (curr_ctx
&& ieee80211_chanctx_num_assigned(local
, curr_ctx
) > 0) {
747 ieee80211_recalc_chanctx_chantype(local
, curr_ctx
);
748 ieee80211_recalc_smps_chanctx(local
, curr_ctx
);
749 ieee80211_recalc_radar_chanctx(local
, curr_ctx
);
750 ieee80211_recalc_chanctx_min_def(local
, curr_ctx
);
753 if (new_ctx
&& ieee80211_chanctx_num_assigned(local
, new_ctx
) > 0) {
754 ieee80211_recalc_txpower(sdata
, false);
755 ieee80211_recalc_chanctx_min_def(local
, new_ctx
);
758 if (sdata
->vif
.type
!= NL80211_IFTYPE_P2P_DEVICE
&&
759 sdata
->vif
.type
!= NL80211_IFTYPE_MONITOR
)
760 ieee80211_bss_info_change_notify(sdata
,
763 ieee80211_check_fast_xmit_iface(sdata
);
768 void ieee80211_recalc_smps_chanctx(struct ieee80211_local
*local
,
769 struct ieee80211_chanctx
*chanctx
)
771 struct ieee80211_sub_if_data
*sdata
;
772 u8 rx_chains_static
, rx_chains_dynamic
;
774 lockdep_assert_held(&local
->chanctx_mtx
);
776 rx_chains_static
= 1;
777 rx_chains_dynamic
= 1;
780 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
781 u8 needed_static
, needed_dynamic
;
783 if (!ieee80211_sdata_running(sdata
))
786 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) !=
790 switch (sdata
->vif
.type
) {
791 case NL80211_IFTYPE_P2P_DEVICE
:
792 case NL80211_IFTYPE_NAN
:
794 case NL80211_IFTYPE_STATION
:
795 if (!sdata
->u
.mgd
.associated
)
798 case NL80211_IFTYPE_AP_VLAN
:
800 case NL80211_IFTYPE_AP
:
801 case NL80211_IFTYPE_ADHOC
:
802 case NL80211_IFTYPE_MESH_POINT
:
803 case NL80211_IFTYPE_OCB
:
809 switch (sdata
->smps_mode
) {
811 WARN_ONCE(1, "Invalid SMPS mode %d\n",
814 case IEEE80211_SMPS_OFF
:
815 needed_static
= sdata
->needed_rx_chains
;
816 needed_dynamic
= sdata
->needed_rx_chains
;
818 case IEEE80211_SMPS_DYNAMIC
:
820 needed_dynamic
= sdata
->needed_rx_chains
;
822 case IEEE80211_SMPS_STATIC
:
828 rx_chains_static
= max(rx_chains_static
, needed_static
);
829 rx_chains_dynamic
= max(rx_chains_dynamic
, needed_dynamic
);
832 /* Disable SMPS for the monitor interface */
833 sdata
= rcu_dereference(local
->monitor_sdata
);
835 rcu_access_pointer(sdata
->vif
.chanctx_conf
) == &chanctx
->conf
)
836 rx_chains_dynamic
= rx_chains_static
= local
->rx_chains
;
840 if (!local
->use_chanctx
) {
841 if (rx_chains_static
> 1)
842 local
->smps_mode
= IEEE80211_SMPS_OFF
;
843 else if (rx_chains_dynamic
> 1)
844 local
->smps_mode
= IEEE80211_SMPS_DYNAMIC
;
846 local
->smps_mode
= IEEE80211_SMPS_STATIC
;
847 ieee80211_hw_config(local
, 0);
850 if (rx_chains_static
== chanctx
->conf
.rx_chains_static
&&
851 rx_chains_dynamic
== chanctx
->conf
.rx_chains_dynamic
)
854 chanctx
->conf
.rx_chains_static
= rx_chains_static
;
855 chanctx
->conf
.rx_chains_dynamic
= rx_chains_dynamic
;
856 drv_change_chanctx(local
, chanctx
, IEEE80211_CHANCTX_CHANGE_RX_CHAINS
);
860 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data
*sdata
,
863 struct ieee80211_local
*local __maybe_unused
= sdata
->local
;
864 struct ieee80211_sub_if_data
*vlan
;
865 struct ieee80211_chanctx_conf
*conf
;
867 if (WARN_ON(sdata
->vif
.type
!= NL80211_IFTYPE_AP
))
870 lockdep_assert_held(&local
->mtx
);
872 /* Check that conf exists, even when clearing this function
873 * must be called with the AP's channel context still there
874 * as it would otherwise cause VLANs to have an invalid
875 * channel context pointer for a while, possibly pointing
876 * to a channel context that has already been freed.
878 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
879 lockdep_is_held(&local
->chanctx_mtx
));
885 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
886 rcu_assign_pointer(vlan
->vif
.chanctx_conf
, conf
);
889 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data
*sdata
,
892 struct ieee80211_local
*local
= sdata
->local
;
894 mutex_lock(&local
->chanctx_mtx
);
896 __ieee80211_vif_copy_chanctx_to_vlans(sdata
, clear
);
898 mutex_unlock(&local
->chanctx_mtx
);
901 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data
*sdata
)
903 struct ieee80211_chanctx
*ctx
= sdata
->reserved_chanctx
;
905 lockdep_assert_held(&sdata
->local
->chanctx_mtx
);
910 list_del(&sdata
->reserved_chanctx_list
);
911 sdata
->reserved_chanctx
= NULL
;
913 if (ieee80211_chanctx_refcount(sdata
->local
, ctx
) == 0) {
914 if (ctx
->replace_state
== IEEE80211_CHANCTX_REPLACES_OTHER
) {
915 if (WARN_ON(!ctx
->replace_ctx
))
918 WARN_ON(ctx
->replace_ctx
->replace_state
!=
919 IEEE80211_CHANCTX_WILL_BE_REPLACED
);
920 WARN_ON(ctx
->replace_ctx
->replace_ctx
!= ctx
);
922 ctx
->replace_ctx
->replace_ctx
= NULL
;
923 ctx
->replace_ctx
->replace_state
=
924 IEEE80211_CHANCTX_REPLACE_NONE
;
926 list_del_rcu(&ctx
->list
);
927 kfree_rcu(ctx
, rcu_head
);
929 ieee80211_free_chanctx(sdata
->local
, ctx
);
936 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data
*sdata
,
937 const struct cfg80211_chan_def
*chandef
,
938 enum ieee80211_chanctx_mode mode
,
941 struct ieee80211_local
*local
= sdata
->local
;
942 struct ieee80211_chanctx
*new_ctx
, *curr_ctx
, *ctx
;
944 lockdep_assert_held(&local
->chanctx_mtx
);
946 curr_ctx
= ieee80211_vif_get_chanctx(sdata
);
947 if (curr_ctx
&& local
->use_chanctx
&& !local
->ops
->switch_vif_chanctx
)
950 new_ctx
= ieee80211_find_reservation_chanctx(local
, chandef
, mode
);
952 if (ieee80211_can_create_new_chanctx(local
)) {
953 new_ctx
= ieee80211_new_chanctx(local
, chandef
, mode
);
955 return PTR_ERR(new_ctx
);
958 (curr_ctx
->replace_state
==
959 IEEE80211_CHANCTX_WILL_BE_REPLACED
) ||
960 !list_empty(&curr_ctx
->reserved_vifs
)) {
962 * Another vif already requested this context
963 * for a reservation. Find another one hoping
964 * all vifs assigned to it will also switch
967 * TODO: This needs a little more work as some
968 * cases (more than 2 chanctx capable devices)
969 * may fail which could otherwise succeed
970 * provided some channel context juggling was
973 * Consider ctx1..3, vif1..6, each ctx has 2
974 * vifs. vif1 and vif2 from ctx1 request new
975 * different chandefs starting 2 in-place
976 * reserations with ctx4 and ctx5 replacing
977 * ctx1 and ctx2 respectively. Next vif5 and
978 * vif6 from ctx3 reserve ctx4. If vif3 and
979 * vif4 remain on ctx2 as they are then this
980 * fails unless `replace_ctx` from ctx5 is
981 * replaced with ctx3.
983 list_for_each_entry(ctx
, &local
->chanctx_list
,
985 if (ctx
->replace_state
!=
986 IEEE80211_CHANCTX_REPLACE_NONE
)
989 if (!list_empty(&ctx
->reserved_vifs
))
998 * If that's true then all available contexts already
999 * have reservations and cannot be used.
1002 (curr_ctx
->replace_state
==
1003 IEEE80211_CHANCTX_WILL_BE_REPLACED
) ||
1004 !list_empty(&curr_ctx
->reserved_vifs
))
1007 new_ctx
= ieee80211_alloc_chanctx(local
, chandef
, mode
);
1011 new_ctx
->replace_ctx
= curr_ctx
;
1012 new_ctx
->replace_state
=
1013 IEEE80211_CHANCTX_REPLACES_OTHER
;
1015 curr_ctx
->replace_ctx
= new_ctx
;
1016 curr_ctx
->replace_state
=
1017 IEEE80211_CHANCTX_WILL_BE_REPLACED
;
1019 list_add_rcu(&new_ctx
->list
, &local
->chanctx_list
);
1023 list_add(&sdata
->reserved_chanctx_list
, &new_ctx
->reserved_vifs
);
1024 sdata
->reserved_chanctx
= new_ctx
;
1025 sdata
->reserved_chandef
= *chandef
;
1026 sdata
->reserved_radar_required
= radar_required
;
1027 sdata
->reserved_ready
= false;
1033 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data
*sdata
)
1035 switch (sdata
->vif
.type
) {
1036 case NL80211_IFTYPE_ADHOC
:
1037 case NL80211_IFTYPE_AP
:
1038 case NL80211_IFTYPE_MESH_POINT
:
1039 case NL80211_IFTYPE_OCB
:
1040 ieee80211_queue_work(&sdata
->local
->hw
,
1041 &sdata
->csa_finalize_work
);
1043 case NL80211_IFTYPE_STATION
:
1044 ieee80211_queue_work(&sdata
->local
->hw
,
1045 &sdata
->u
.mgd
.chswitch_work
);
1047 case NL80211_IFTYPE_UNSPECIFIED
:
1048 case NL80211_IFTYPE_AP_VLAN
:
1049 case NL80211_IFTYPE_WDS
:
1050 case NL80211_IFTYPE_MONITOR
:
1051 case NL80211_IFTYPE_P2P_CLIENT
:
1052 case NL80211_IFTYPE_P2P_GO
:
1053 case NL80211_IFTYPE_P2P_DEVICE
:
1054 case NL80211_IFTYPE_NAN
:
1055 case NUM_NL80211_IFTYPES
:
1062 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data
*sdata
,
1063 const struct cfg80211_chan_def
*chandef
)
1065 struct ieee80211_sub_if_data
*vlan
;
1067 sdata
->vif
.bss_conf
.chandef
= *chandef
;
1069 if (sdata
->vif
.type
!= NL80211_IFTYPE_AP
)
1072 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
1073 vlan
->vif
.bss_conf
.chandef
= *chandef
;
1077 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data
*sdata
)
1079 struct ieee80211_local
*local
= sdata
->local
;
1080 struct ieee80211_vif_chanctx_switch vif_chsw
[1] = {};
1081 struct ieee80211_chanctx
*old_ctx
, *new_ctx
;
1082 const struct cfg80211_chan_def
*chandef
;
1086 lockdep_assert_held(&local
->mtx
);
1087 lockdep_assert_held(&local
->chanctx_mtx
);
1089 new_ctx
= sdata
->reserved_chanctx
;
1090 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1092 if (WARN_ON(!sdata
->reserved_ready
))
1095 if (WARN_ON(!new_ctx
))
1098 if (WARN_ON(!old_ctx
))
1101 if (WARN_ON(new_ctx
->replace_state
==
1102 IEEE80211_CHANCTX_REPLACES_OTHER
))
1105 chandef
= ieee80211_chanctx_non_reserved_chandef(local
, new_ctx
,
1106 &sdata
->reserved_chandef
);
1107 if (WARN_ON(!chandef
))
1110 if (old_ctx
->conf
.def
.width
> new_ctx
->conf
.def
.width
)
1111 ieee80211_chan_bw_change(local
, new_ctx
);
1113 ieee80211_change_chanctx(local
, new_ctx
, chandef
);
1115 if (old_ctx
->conf
.def
.width
< new_ctx
->conf
.def
.width
)
1116 ieee80211_chan_bw_change(local
, new_ctx
);
1118 vif_chsw
[0].vif
= &sdata
->vif
;
1119 vif_chsw
[0].old_ctx
= &old_ctx
->conf
;
1120 vif_chsw
[0].new_ctx
= &new_ctx
->conf
;
1122 list_del(&sdata
->reserved_chanctx_list
);
1123 sdata
->reserved_chanctx
= NULL
;
1125 err
= drv_switch_vif_chanctx(local
, vif_chsw
, 1,
1126 CHANCTX_SWMODE_REASSIGN_VIF
);
1128 if (ieee80211_chanctx_refcount(local
, new_ctx
) == 0)
1129 ieee80211_free_chanctx(local
, new_ctx
);
1134 list_move(&sdata
->assigned_chanctx_list
, &new_ctx
->assigned_vifs
);
1135 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, &new_ctx
->conf
);
1137 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
)
1138 __ieee80211_vif_copy_chanctx_to_vlans(sdata
, false);
1140 ieee80211_check_fast_xmit_iface(sdata
);
1142 if (ieee80211_chanctx_refcount(local
, old_ctx
) == 0)
1143 ieee80211_free_chanctx(local
, old_ctx
);
1145 if (sdata
->vif
.bss_conf
.chandef
.width
!= sdata
->reserved_chandef
.width
)
1146 changed
= BSS_CHANGED_BANDWIDTH
;
1148 ieee80211_vif_update_chandef(sdata
, &sdata
->reserved_chandef
);
1150 ieee80211_recalc_smps_chanctx(local
, new_ctx
);
1151 ieee80211_recalc_radar_chanctx(local
, new_ctx
);
1152 ieee80211_recalc_chanctx_min_def(local
, new_ctx
);
1155 ieee80211_bss_info_change_notify(sdata
, changed
);
1158 ieee80211_vif_chanctx_reservation_complete(sdata
);
1163 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data
*sdata
)
1165 struct ieee80211_local
*local
= sdata
->local
;
1166 struct ieee80211_chanctx
*old_ctx
, *new_ctx
;
1167 const struct cfg80211_chan_def
*chandef
;
1170 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1171 new_ctx
= sdata
->reserved_chanctx
;
1173 if (WARN_ON(!sdata
->reserved_ready
))
1176 if (WARN_ON(old_ctx
))
1179 if (WARN_ON(!new_ctx
))
1182 if (WARN_ON(new_ctx
->replace_state
==
1183 IEEE80211_CHANCTX_REPLACES_OTHER
))
1186 chandef
= ieee80211_chanctx_non_reserved_chandef(local
, new_ctx
,
1187 &sdata
->reserved_chandef
);
1188 if (WARN_ON(!chandef
))
1191 ieee80211_change_chanctx(local
, new_ctx
, chandef
);
1193 list_del(&sdata
->reserved_chanctx_list
);
1194 sdata
->reserved_chanctx
= NULL
;
1196 err
= ieee80211_assign_vif_chanctx(sdata
, new_ctx
);
1198 if (ieee80211_chanctx_refcount(local
, new_ctx
) == 0)
1199 ieee80211_free_chanctx(local
, new_ctx
);
1205 ieee80211_vif_chanctx_reservation_complete(sdata
);
1210 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data
*sdata
)
1212 struct ieee80211_chanctx
*old_ctx
, *new_ctx
;
1214 lockdep_assert_held(&sdata
->local
->chanctx_mtx
);
1216 new_ctx
= sdata
->reserved_chanctx
;
1217 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1222 if (WARN_ON(!new_ctx
))
1225 if (old_ctx
->replace_state
!= IEEE80211_CHANCTX_WILL_BE_REPLACED
)
1228 if (new_ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1234 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local
*local
,
1235 struct ieee80211_chanctx
*new_ctx
)
1237 const struct cfg80211_chan_def
*chandef
;
1239 lockdep_assert_held(&local
->mtx
);
1240 lockdep_assert_held(&local
->chanctx_mtx
);
1242 chandef
= ieee80211_chanctx_reserved_chandef(local
, new_ctx
, NULL
);
1243 if (WARN_ON(!chandef
))
1246 local
->hw
.conf
.radar_enabled
= new_ctx
->conf
.radar_enabled
;
1247 local
->_oper_chandef
= *chandef
;
1248 ieee80211_hw_config(local
, 0);
1253 static int ieee80211_chsw_switch_vifs(struct ieee80211_local
*local
,
1256 struct ieee80211_vif_chanctx_switch
*vif_chsw
;
1257 struct ieee80211_sub_if_data
*sdata
;
1258 struct ieee80211_chanctx
*ctx
, *old_ctx
;
1261 lockdep_assert_held(&local
->mtx
);
1262 lockdep_assert_held(&local
->chanctx_mtx
);
1264 vif_chsw
= kcalloc(n_vifs
, sizeof(vif_chsw
[0]), GFP_KERNEL
);
1269 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1270 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1273 if (WARN_ON(!ctx
->replace_ctx
)) {
1278 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
1279 reserved_chanctx_list
) {
1280 if (!ieee80211_vif_has_in_place_reservation(
1284 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1285 vif_chsw
[i
].vif
= &sdata
->vif
;
1286 vif_chsw
[i
].old_ctx
= &old_ctx
->conf
;
1287 vif_chsw
[i
].new_ctx
= &ctx
->conf
;
1293 err
= drv_switch_vif_chanctx(local
, vif_chsw
, n_vifs
,
1294 CHANCTX_SWMODE_SWAP_CONTEXTS
);
1301 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local
*local
)
1303 struct ieee80211_chanctx
*ctx
;
1306 lockdep_assert_held(&local
->mtx
);
1307 lockdep_assert_held(&local
->chanctx_mtx
);
1309 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1310 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1313 if (!list_empty(&ctx
->replace_ctx
->assigned_vifs
))
1316 ieee80211_del_chanctx(local
, ctx
->replace_ctx
);
1317 err
= ieee80211_add_chanctx(local
, ctx
);
1325 WARN_ON(ieee80211_add_chanctx(local
, ctx
));
1326 list_for_each_entry_continue_reverse(ctx
, &local
->chanctx_list
, list
) {
1327 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1330 if (!list_empty(&ctx
->replace_ctx
->assigned_vifs
))
1333 ieee80211_del_chanctx(local
, ctx
);
1334 WARN_ON(ieee80211_add_chanctx(local
, ctx
->replace_ctx
));
1340 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local
*local
)
1342 struct ieee80211_sub_if_data
*sdata
, *sdata_tmp
;
1343 struct ieee80211_chanctx
*ctx
, *ctx_tmp
, *old_ctx
;
1344 struct ieee80211_chanctx
*new_ctx
= NULL
;
1345 int err
, n_assigned
, n_reserved
, n_ready
;
1346 int n_ctx
= 0, n_vifs_switch
= 0, n_vifs_assign
= 0, n_vifs_ctxless
= 0;
1348 lockdep_assert_held(&local
->mtx
);
1349 lockdep_assert_held(&local
->chanctx_mtx
);
1352 * If there are 2 independent pairs of channel contexts performing
1353 * cross-switch of their vifs this code will still wait until both are
1354 * ready even though it could be possible to switch one before the
1357 * For practical reasons and code simplicity just do a single huge
1362 * Verify if the reservation is still feasible.
1363 * - if it's not then disconnect
1364 * - if it is but not all vifs necessary are ready then defer
1367 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1368 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1371 if (WARN_ON(!ctx
->replace_ctx
)) {
1376 if (!local
->use_chanctx
)
1385 list_for_each_entry(sdata
, &ctx
->replace_ctx
->assigned_vifs
,
1386 assigned_chanctx_list
) {
1388 if (sdata
->reserved_chanctx
) {
1390 if (sdata
->reserved_ready
)
1395 if (n_assigned
!= n_reserved
) {
1396 if (n_ready
== n_reserved
) {
1397 wiphy_info(local
->hw
.wiphy
,
1398 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1406 ctx
->conf
.radar_enabled
= false;
1407 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
1408 reserved_chanctx_list
) {
1409 if (ieee80211_vif_has_in_place_reservation(sdata
) &&
1410 !sdata
->reserved_ready
)
1413 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1415 if (old_ctx
->replace_state
==
1416 IEEE80211_CHANCTX_WILL_BE_REPLACED
)
1424 if (sdata
->reserved_radar_required
)
1425 ctx
->conf
.radar_enabled
= true;
1429 if (WARN_ON(n_ctx
== 0) ||
1430 WARN_ON(n_vifs_switch
== 0 &&
1431 n_vifs_assign
== 0 &&
1432 n_vifs_ctxless
== 0) ||
1433 WARN_ON(n_ctx
> 1 && !local
->use_chanctx
) ||
1434 WARN_ON(!new_ctx
&& !local
->use_chanctx
)) {
1440 * All necessary vifs are ready. Perform the switch now depending on
1441 * reservations and driver capabilities.
1444 if (local
->use_chanctx
) {
1445 if (n_vifs_switch
> 0) {
1446 err
= ieee80211_chsw_switch_vifs(local
, n_vifs_switch
);
1451 if (n_vifs_assign
> 0 || n_vifs_ctxless
> 0) {
1452 err
= ieee80211_chsw_switch_ctxs(local
);
1457 err
= ieee80211_chsw_switch_hwconf(local
, new_ctx
);
1463 * Update all structures, values and pointers to point to new channel
1466 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1467 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1470 if (WARN_ON(!ctx
->replace_ctx
)) {
1475 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
1476 reserved_chanctx_list
) {
1479 if (!ieee80211_vif_has_in_place_reservation(sdata
))
1482 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, &ctx
->conf
);
1484 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
)
1485 __ieee80211_vif_copy_chanctx_to_vlans(sdata
,
1488 ieee80211_check_fast_xmit_iface(sdata
);
1490 sdata
->radar_required
= sdata
->reserved_radar_required
;
1492 if (sdata
->vif
.bss_conf
.chandef
.width
!=
1493 sdata
->reserved_chandef
.width
)
1494 changed
= BSS_CHANGED_BANDWIDTH
;
1496 ieee80211_vif_update_chandef(sdata
, &sdata
->reserved_chandef
);
1498 ieee80211_bss_info_change_notify(sdata
,
1501 ieee80211_recalc_txpower(sdata
, false);
1504 ieee80211_recalc_chanctx_chantype(local
, ctx
);
1505 ieee80211_recalc_smps_chanctx(local
, ctx
);
1506 ieee80211_recalc_radar_chanctx(local
, ctx
);
1507 ieee80211_recalc_chanctx_min_def(local
, ctx
);
1508 ieee80211_chan_bw_change(local
, ctx
);
1510 list_for_each_entry_safe(sdata
, sdata_tmp
, &ctx
->reserved_vifs
,
1511 reserved_chanctx_list
) {
1512 if (ieee80211_vif_get_chanctx(sdata
) != ctx
)
1515 list_del(&sdata
->reserved_chanctx_list
);
1516 list_move(&sdata
->assigned_chanctx_list
,
1517 &ctx
->assigned_vifs
);
1518 sdata
->reserved_chanctx
= NULL
;
1520 ieee80211_vif_chanctx_reservation_complete(sdata
);
1524 * This context might have been a dependency for an already
1525 * ready re-assign reservation interface that was deferred. Do
1526 * not propagate error to the caller though. The in-place
1527 * reservation for originally requested interface has already
1528 * succeeded at this point.
1530 list_for_each_entry_safe(sdata
, sdata_tmp
, &ctx
->reserved_vifs
,
1531 reserved_chanctx_list
) {
1532 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1536 if (WARN_ON(sdata
->reserved_chanctx
!= ctx
))
1539 if (!sdata
->reserved_ready
)
1542 if (ieee80211_vif_get_chanctx(sdata
))
1543 err
= ieee80211_vif_use_reserved_reassign(
1546 err
= ieee80211_vif_use_reserved_assign(sdata
);
1550 "failed to finalize (re-)assign reservation (err=%d)\n",
1552 ieee80211_vif_unreserve_chanctx(sdata
);
1553 cfg80211_stop_iface(local
->hw
.wiphy
,
1561 * Finally free old contexts
1564 list_for_each_entry_safe(ctx
, ctx_tmp
, &local
->chanctx_list
, list
) {
1565 if (ctx
->replace_state
!= IEEE80211_CHANCTX_WILL_BE_REPLACED
)
1568 ctx
->replace_ctx
->replace_ctx
= NULL
;
1569 ctx
->replace_ctx
->replace_state
=
1570 IEEE80211_CHANCTX_REPLACE_NONE
;
1572 list_del_rcu(&ctx
->list
);
1573 kfree_rcu(ctx
, rcu_head
);
1579 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1580 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1583 list_for_each_entry_safe(sdata
, sdata_tmp
, &ctx
->reserved_vifs
,
1584 reserved_chanctx_list
) {
1585 ieee80211_vif_unreserve_chanctx(sdata
);
1586 ieee80211_vif_chanctx_reservation_complete(sdata
);
1593 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data
*sdata
)
1595 struct ieee80211_local
*local
= sdata
->local
;
1596 struct ieee80211_chanctx_conf
*conf
;
1597 struct ieee80211_chanctx
*ctx
;
1598 bool use_reserved_switch
= false;
1600 lockdep_assert_held(&local
->chanctx_mtx
);
1602 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
1603 lockdep_is_held(&local
->chanctx_mtx
));
1607 ctx
= container_of(conf
, struct ieee80211_chanctx
, conf
);
1609 if (sdata
->reserved_chanctx
) {
1610 if (sdata
->reserved_chanctx
->replace_state
==
1611 IEEE80211_CHANCTX_REPLACES_OTHER
&&
1612 ieee80211_chanctx_num_reserved(local
,
1613 sdata
->reserved_chanctx
) > 1)
1614 use_reserved_switch
= true;
1616 ieee80211_vif_unreserve_chanctx(sdata
);
1619 ieee80211_assign_vif_chanctx(sdata
, NULL
);
1620 if (ieee80211_chanctx_refcount(local
, ctx
) == 0)
1621 ieee80211_free_chanctx(local
, ctx
);
1623 sdata
->radar_required
= false;
1625 /* Unreserving may ready an in-place reservation. */
1626 if (use_reserved_switch
)
1627 ieee80211_vif_use_reserved_switch(local
);
1630 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data
*sdata
,
1631 const struct cfg80211_chan_def
*chandef
,
1632 enum ieee80211_chanctx_mode mode
)
1634 struct ieee80211_local
*local
= sdata
->local
;
1635 struct ieee80211_chanctx
*ctx
;
1636 u8 radar_detect_width
= 0;
1639 lockdep_assert_held(&local
->mtx
);
1641 WARN_ON(sdata
->dev
&& netif_carrier_ok(sdata
->dev
));
1643 mutex_lock(&local
->chanctx_mtx
);
1645 ret
= cfg80211_chandef_dfs_required(local
->hw
.wiphy
,
1647 sdata
->wdev
.iftype
);
1651 radar_detect_width
= BIT(chandef
->width
);
1653 sdata
->radar_required
= ret
;
1655 ret
= ieee80211_check_combinations(sdata
, chandef
, mode
,
1656 radar_detect_width
);
1660 __ieee80211_vif_release_channel(sdata
);
1662 ctx
= ieee80211_find_chanctx(local
, chandef
, mode
);
1664 ctx
= ieee80211_new_chanctx(local
, chandef
, mode
);
1670 ieee80211_vif_update_chandef(sdata
, chandef
);
1672 ret
= ieee80211_assign_vif_chanctx(sdata
, ctx
);
1674 /* if assign fails refcount stays the same */
1675 if (ieee80211_chanctx_refcount(local
, ctx
) == 0)
1676 ieee80211_free_chanctx(local
, ctx
);
1680 ieee80211_recalc_smps_chanctx(local
, ctx
);
1681 ieee80211_recalc_radar_chanctx(local
, ctx
);
1684 sdata
->radar_required
= false;
1686 mutex_unlock(&local
->chanctx_mtx
);
1690 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data
*sdata
)
1692 struct ieee80211_local
*local
= sdata
->local
;
1693 struct ieee80211_chanctx
*new_ctx
;
1694 struct ieee80211_chanctx
*old_ctx
;
1697 lockdep_assert_held(&local
->mtx
);
1698 lockdep_assert_held(&local
->chanctx_mtx
);
1700 new_ctx
= sdata
->reserved_chanctx
;
1701 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1703 if (WARN_ON(!new_ctx
))
1706 if (WARN_ON(new_ctx
->replace_state
==
1707 IEEE80211_CHANCTX_WILL_BE_REPLACED
))
1710 if (WARN_ON(sdata
->reserved_ready
))
1713 sdata
->reserved_ready
= true;
1715 if (new_ctx
->replace_state
== IEEE80211_CHANCTX_REPLACE_NONE
) {
1717 err
= ieee80211_vif_use_reserved_reassign(sdata
);
1719 err
= ieee80211_vif_use_reserved_assign(sdata
);
1726 * In-place reservation may need to be finalized now either if:
1727 * a) sdata is taking part in the swapping itself and is the last one
1728 * b) sdata has switched with a re-assign reservation to an existing
1729 * context readying in-place switching of old_ctx
1731 * In case of (b) do not propagate the error up because the requested
1732 * sdata already switched successfully. Just spill an extra warning.
1733 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1734 * interfaces upon failure.
1737 old_ctx
->replace_state
== IEEE80211_CHANCTX_WILL_BE_REPLACED
) ||
1738 new_ctx
->replace_state
== IEEE80211_CHANCTX_REPLACES_OTHER
) {
1739 err
= ieee80211_vif_use_reserved_switch(local
);
1740 if (err
&& err
!= -EAGAIN
) {
1741 if (new_ctx
->replace_state
==
1742 IEEE80211_CHANCTX_REPLACES_OTHER
)
1745 wiphy_info(local
->hw
.wiphy
,
1746 "depending in-place reservation failed (err=%d)\n",
1754 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data
*sdata
,
1755 const struct cfg80211_chan_def
*chandef
,
1758 struct ieee80211_local
*local
= sdata
->local
;
1759 struct ieee80211_chanctx_conf
*conf
;
1760 struct ieee80211_chanctx
*ctx
;
1761 const struct cfg80211_chan_def
*compat
;
1764 if (!cfg80211_chandef_usable(sdata
->local
->hw
.wiphy
, chandef
,
1765 IEEE80211_CHAN_DISABLED
))
1768 mutex_lock(&local
->chanctx_mtx
);
1769 if (cfg80211_chandef_identical(chandef
, &sdata
->vif
.bss_conf
.chandef
)) {
1774 if (chandef
->width
== NL80211_CHAN_WIDTH_20_NOHT
||
1775 sdata
->vif
.bss_conf
.chandef
.width
== NL80211_CHAN_WIDTH_20_NOHT
) {
1780 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
1781 lockdep_is_held(&local
->chanctx_mtx
));
1787 ctx
= container_of(conf
, struct ieee80211_chanctx
, conf
);
1789 compat
= cfg80211_chandef_compatible(&conf
->def
, chandef
);
1795 switch (ctx
->replace_state
) {
1796 case IEEE80211_CHANCTX_REPLACE_NONE
:
1797 if (!ieee80211_chanctx_reserved_chandef(local
, ctx
, compat
)) {
1802 case IEEE80211_CHANCTX_WILL_BE_REPLACED
:
1803 /* TODO: Perhaps the bandwidth change could be treated as a
1804 * reservation itself? */
1807 case IEEE80211_CHANCTX_REPLACES_OTHER
:
1808 /* channel context that is going to replace another channel
1809 * context doesn't really exist and shouldn't be assigned
1815 ieee80211_vif_update_chandef(sdata
, chandef
);
1817 ieee80211_recalc_chanctx_chantype(local
, ctx
);
1819 *changed
|= BSS_CHANGED_BANDWIDTH
;
1822 mutex_unlock(&local
->chanctx_mtx
);
1826 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data
*sdata
)
1828 WARN_ON(sdata
->dev
&& netif_carrier_ok(sdata
->dev
));
1830 lockdep_assert_held(&sdata
->local
->mtx
);
1832 mutex_lock(&sdata
->local
->chanctx_mtx
);
1833 __ieee80211_vif_release_channel(sdata
);
1834 mutex_unlock(&sdata
->local
->chanctx_mtx
);
1837 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data
*sdata
)
1839 struct ieee80211_local
*local
= sdata
->local
;
1840 struct ieee80211_sub_if_data
*ap
;
1841 struct ieee80211_chanctx_conf
*conf
;
1843 if (WARN_ON(sdata
->vif
.type
!= NL80211_IFTYPE_AP_VLAN
|| !sdata
->bss
))
1846 ap
= container_of(sdata
->bss
, struct ieee80211_sub_if_data
, u
.ap
);
1848 mutex_lock(&local
->chanctx_mtx
);
1850 conf
= rcu_dereference_protected(ap
->vif
.chanctx_conf
,
1851 lockdep_is_held(&local
->chanctx_mtx
));
1852 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, conf
);
1853 mutex_unlock(&local
->chanctx_mtx
);
1856 void ieee80211_iter_chan_contexts_atomic(
1857 struct ieee80211_hw
*hw
,
1858 void (*iter
)(struct ieee80211_hw
*hw
,
1859 struct ieee80211_chanctx_conf
*chanctx_conf
,
1863 struct ieee80211_local
*local
= hw_to_local(hw
);
1864 struct ieee80211_chanctx
*ctx
;
1867 list_for_each_entry_rcu(ctx
, &local
->chanctx_list
, list
)
1868 if (ctx
->driver_present
)
1869 iter(hw
, &ctx
->conf
, iter_data
);
1872 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic
);