2 * mac80211 - channel management
5 #include <linux/nl80211.h>
6 #include <linux/export.h>
7 #include <linux/rtnetlink.h>
8 #include <net/cfg80211.h>
9 #include "ieee80211_i.h"
10 #include "driver-ops.h"
12 static int ieee80211_chanctx_num_assigned(struct ieee80211_local
*local
,
13 struct ieee80211_chanctx
*ctx
)
15 struct ieee80211_sub_if_data
*sdata
;
18 lockdep_assert_held(&local
->chanctx_mtx
);
20 list_for_each_entry(sdata
, &ctx
->assigned_vifs
, assigned_chanctx_list
)
26 static int ieee80211_chanctx_num_reserved(struct ieee80211_local
*local
,
27 struct ieee80211_chanctx
*ctx
)
29 struct ieee80211_sub_if_data
*sdata
;
32 lockdep_assert_held(&local
->chanctx_mtx
);
34 list_for_each_entry(sdata
, &ctx
->reserved_vifs
, reserved_chanctx_list
)
40 int ieee80211_chanctx_refcount(struct ieee80211_local
*local
,
41 struct ieee80211_chanctx
*ctx
)
43 return ieee80211_chanctx_num_assigned(local
, ctx
) +
44 ieee80211_chanctx_num_reserved(local
, ctx
);
47 static int ieee80211_num_chanctx(struct ieee80211_local
*local
)
49 struct ieee80211_chanctx
*ctx
;
52 lockdep_assert_held(&local
->chanctx_mtx
);
54 list_for_each_entry(ctx
, &local
->chanctx_list
, list
)
60 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local
*local
)
62 lockdep_assert_held(&local
->chanctx_mtx
);
63 return ieee80211_num_chanctx(local
) < ieee80211_max_num_channels(local
);
66 static struct ieee80211_chanctx
*
67 ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data
*sdata
)
69 struct ieee80211_local
*local __maybe_unused
= sdata
->local
;
70 struct ieee80211_chanctx_conf
*conf
;
72 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
73 lockdep_is_held(&local
->chanctx_mtx
));
77 return container_of(conf
, struct ieee80211_chanctx
, conf
);
80 static const struct cfg80211_chan_def
*
81 ieee80211_chanctx_reserved_chandef(struct ieee80211_local
*local
,
82 struct ieee80211_chanctx
*ctx
,
83 const struct cfg80211_chan_def
*compat
)
85 struct ieee80211_sub_if_data
*sdata
;
87 lockdep_assert_held(&local
->chanctx_mtx
);
89 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
90 reserved_chanctx_list
) {
92 compat
= &sdata
->reserved_chandef
;
94 compat
= cfg80211_chandef_compatible(&sdata
->reserved_chandef
,
103 static const struct cfg80211_chan_def
*
104 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local
*local
,
105 struct ieee80211_chanctx
*ctx
,
106 const struct cfg80211_chan_def
*compat
)
108 struct ieee80211_sub_if_data
*sdata
;
110 lockdep_assert_held(&local
->chanctx_mtx
);
112 list_for_each_entry(sdata
, &ctx
->assigned_vifs
,
113 assigned_chanctx_list
) {
114 if (sdata
->reserved_chanctx
!= NULL
)
118 compat
= &sdata
->vif
.bss_conf
.chandef
;
120 compat
= cfg80211_chandef_compatible(
121 &sdata
->vif
.bss_conf
.chandef
, compat
);
129 static const struct cfg80211_chan_def
*
130 ieee80211_chanctx_combined_chandef(struct ieee80211_local
*local
,
131 struct ieee80211_chanctx
*ctx
,
132 const struct cfg80211_chan_def
*compat
)
134 lockdep_assert_held(&local
->chanctx_mtx
);
136 compat
= ieee80211_chanctx_reserved_chandef(local
, ctx
, compat
);
140 compat
= ieee80211_chanctx_non_reserved_chandef(local
, ctx
, compat
);
148 ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local
*local
,
149 struct ieee80211_chanctx
*ctx
,
150 const struct cfg80211_chan_def
*def
)
152 lockdep_assert_held(&local
->chanctx_mtx
);
154 if (ieee80211_chanctx_combined_chandef(local
, ctx
, def
))
157 if (!list_empty(&ctx
->reserved_vifs
) &&
158 ieee80211_chanctx_reserved_chandef(local
, ctx
, def
))
164 static struct ieee80211_chanctx
*
165 ieee80211_find_reservation_chanctx(struct ieee80211_local
*local
,
166 const struct cfg80211_chan_def
*chandef
,
167 enum ieee80211_chanctx_mode mode
)
169 struct ieee80211_chanctx
*ctx
;
171 lockdep_assert_held(&local
->chanctx_mtx
);
173 if (mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
176 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
177 if (ctx
->replace_state
== IEEE80211_CHANCTX_WILL_BE_REPLACED
)
180 if (ctx
->mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
183 if (!ieee80211_chanctx_can_reserve_chandef(local
, ctx
,
193 enum nl80211_chan_width
ieee80211_get_sta_bw(struct ieee80211_sta
*sta
)
195 switch (sta
->bandwidth
) {
196 case IEEE80211_STA_RX_BW_20
:
197 if (sta
->ht_cap
.ht_supported
)
198 return NL80211_CHAN_WIDTH_20
;
200 return NL80211_CHAN_WIDTH_20_NOHT
;
201 case IEEE80211_STA_RX_BW_40
:
202 return NL80211_CHAN_WIDTH_40
;
203 case IEEE80211_STA_RX_BW_80
:
204 return NL80211_CHAN_WIDTH_80
;
205 case IEEE80211_STA_RX_BW_160
:
207 * This applied for both 160 and 80+80. since we use
208 * the returned value to consider degradation of
209 * ctx->conf.min_def, we have to make sure to take
210 * the bigger one (NL80211_CHAN_WIDTH_160).
211 * Otherwise we might try degrading even when not
212 * needed, as the max required sta_bw returned (80+80)
213 * might be smaller than the configured bw (160).
215 return NL80211_CHAN_WIDTH_160
;
218 return NL80211_CHAN_WIDTH_20
;
222 static enum nl80211_chan_width
223 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data
*sdata
)
225 enum nl80211_chan_width max_bw
= NL80211_CHAN_WIDTH_20_NOHT
;
226 struct sta_info
*sta
;
229 list_for_each_entry_rcu(sta
, &sdata
->local
->sta_list
, list
) {
230 if (sdata
!= sta
->sdata
&&
231 !(sta
->sdata
->bss
&& sta
->sdata
->bss
== sdata
->bss
))
234 if (!sta
->uploaded
|| !test_sta_flag(sta
, WLAN_STA_ASSOC
))
237 max_bw
= max(max_bw
, ieee80211_get_sta_bw(&sta
->sta
));
244 static enum nl80211_chan_width
245 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local
*local
,
246 struct ieee80211_chanctx_conf
*conf
)
248 struct ieee80211_sub_if_data
*sdata
;
249 enum nl80211_chan_width max_bw
= NL80211_CHAN_WIDTH_20_NOHT
;
252 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
253 struct ieee80211_vif
*vif
= &sdata
->vif
;
254 enum nl80211_chan_width width
= NL80211_CHAN_WIDTH_20_NOHT
;
256 if (!ieee80211_sdata_running(sdata
))
259 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) != conf
)
263 case NL80211_IFTYPE_AP
:
264 case NL80211_IFTYPE_AP_VLAN
:
265 width
= ieee80211_get_max_required_bw(sdata
);
267 case NL80211_IFTYPE_STATION
:
269 * The ap's sta->bandwidth is not set yet at this
270 * point, so take the width from the chandef, but
271 * account also for TDLS peers
273 width
= max(vif
->bss_conf
.chandef
.width
,
274 ieee80211_get_max_required_bw(sdata
));
276 case NL80211_IFTYPE_P2P_DEVICE
:
277 case NL80211_IFTYPE_NAN
:
279 case NL80211_IFTYPE_ADHOC
:
280 case NL80211_IFTYPE_WDS
:
281 case NL80211_IFTYPE_MESH_POINT
:
282 case NL80211_IFTYPE_OCB
:
283 width
= vif
->bss_conf
.chandef
.width
;
285 case NL80211_IFTYPE_UNSPECIFIED
:
286 case NUM_NL80211_IFTYPES
:
287 case NL80211_IFTYPE_MONITOR
:
288 case NL80211_IFTYPE_P2P_CLIENT
:
289 case NL80211_IFTYPE_P2P_GO
:
292 max_bw
= max(max_bw
, width
);
295 /* use the configured bandwidth in case of monitor interface */
296 sdata
= rcu_dereference(local
->monitor_sdata
);
297 if (sdata
&& rcu_access_pointer(sdata
->vif
.chanctx_conf
) == conf
)
298 max_bw
= max(max_bw
, conf
->def
.width
);
306 * recalc the min required chan width of the channel context, which is
307 * the max of min required widths of all the interfaces bound to this
310 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local
*local
,
311 struct ieee80211_chanctx
*ctx
)
313 enum nl80211_chan_width max_bw
;
314 struct cfg80211_chan_def min_def
;
316 lockdep_assert_held(&local
->chanctx_mtx
);
318 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
319 if (ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_5
||
320 ctx
->conf
.def
.width
== NL80211_CHAN_WIDTH_10
||
321 ctx
->conf
.radar_enabled
) {
322 ctx
->conf
.min_def
= ctx
->conf
.def
;
326 max_bw
= ieee80211_get_chanctx_max_required_bw(local
, &ctx
->conf
);
328 /* downgrade chandef up to max_bw */
329 min_def
= ctx
->conf
.def
;
330 while (min_def
.width
> max_bw
)
331 ieee80211_chandef_downgrade(&min_def
);
333 if (cfg80211_chandef_identical(&ctx
->conf
.min_def
, &min_def
))
336 ctx
->conf
.min_def
= min_def
;
337 if (!ctx
->driver_present
)
340 drv_change_chanctx(local
, ctx
, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH
);
343 static void ieee80211_change_chanctx(struct ieee80211_local
*local
,
344 struct ieee80211_chanctx
*ctx
,
345 const struct cfg80211_chan_def
*chandef
)
347 if (cfg80211_chandef_identical(&ctx
->conf
.def
, chandef
)) {
348 ieee80211_recalc_chanctx_min_def(local
, ctx
);
352 WARN_ON(!cfg80211_chandef_compatible(&ctx
->conf
.def
, chandef
));
354 ctx
->conf
.def
= *chandef
;
355 drv_change_chanctx(local
, ctx
, IEEE80211_CHANCTX_CHANGE_WIDTH
);
356 ieee80211_recalc_chanctx_min_def(local
, ctx
);
358 if (!local
->use_chanctx
) {
359 local
->_oper_chandef
= *chandef
;
360 ieee80211_hw_config(local
, 0);
364 static struct ieee80211_chanctx
*
365 ieee80211_find_chanctx(struct ieee80211_local
*local
,
366 const struct cfg80211_chan_def
*chandef
,
367 enum ieee80211_chanctx_mode mode
)
369 struct ieee80211_chanctx
*ctx
;
371 lockdep_assert_held(&local
->chanctx_mtx
);
373 if (mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
376 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
377 const struct cfg80211_chan_def
*compat
;
379 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACE_NONE
)
382 if (ctx
->mode
== IEEE80211_CHANCTX_EXCLUSIVE
)
385 compat
= cfg80211_chandef_compatible(&ctx
->conf
.def
, chandef
);
389 compat
= ieee80211_chanctx_reserved_chandef(local
, ctx
,
394 ieee80211_change_chanctx(local
, ctx
, compat
);
402 bool ieee80211_is_radar_required(struct ieee80211_local
*local
)
404 struct ieee80211_sub_if_data
*sdata
;
406 lockdep_assert_held(&local
->mtx
);
409 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
410 if (sdata
->radar_required
) {
421 ieee80211_chanctx_radar_required(struct ieee80211_local
*local
,
422 struct ieee80211_chanctx
*ctx
)
424 struct ieee80211_chanctx_conf
*conf
= &ctx
->conf
;
425 struct ieee80211_sub_if_data
*sdata
;
426 bool required
= false;
428 lockdep_assert_held(&local
->chanctx_mtx
);
429 lockdep_assert_held(&local
->mtx
);
432 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
433 if (!ieee80211_sdata_running(sdata
))
435 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) != conf
)
437 if (!sdata
->radar_required
)
448 static struct ieee80211_chanctx
*
449 ieee80211_alloc_chanctx(struct ieee80211_local
*local
,
450 const struct cfg80211_chan_def
*chandef
,
451 enum ieee80211_chanctx_mode mode
)
453 struct ieee80211_chanctx
*ctx
;
455 lockdep_assert_held(&local
->chanctx_mtx
);
457 ctx
= kzalloc(sizeof(*ctx
) + local
->hw
.chanctx_data_size
, GFP_KERNEL
);
461 INIT_LIST_HEAD(&ctx
->assigned_vifs
);
462 INIT_LIST_HEAD(&ctx
->reserved_vifs
);
463 ctx
->conf
.def
= *chandef
;
464 ctx
->conf
.rx_chains_static
= 1;
465 ctx
->conf
.rx_chains_dynamic
= 1;
467 ctx
->conf
.radar_enabled
= false;
468 ieee80211_recalc_chanctx_min_def(local
, ctx
);
473 static int ieee80211_add_chanctx(struct ieee80211_local
*local
,
474 struct ieee80211_chanctx
*ctx
)
479 lockdep_assert_held(&local
->mtx
);
480 lockdep_assert_held(&local
->chanctx_mtx
);
482 if (!local
->use_chanctx
)
483 local
->hw
.conf
.radar_enabled
= ctx
->conf
.radar_enabled
;
485 /* turn idle off *before* setting channel -- some drivers need that */
486 changed
= ieee80211_idle_off(local
);
488 ieee80211_hw_config(local
, changed
);
490 if (!local
->use_chanctx
) {
491 local
->_oper_chandef
= ctx
->conf
.def
;
492 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
494 err
= drv_add_chanctx(local
, ctx
);
496 ieee80211_recalc_idle(local
);
504 static struct ieee80211_chanctx
*
505 ieee80211_new_chanctx(struct ieee80211_local
*local
,
506 const struct cfg80211_chan_def
*chandef
,
507 enum ieee80211_chanctx_mode mode
)
509 struct ieee80211_chanctx
*ctx
;
512 lockdep_assert_held(&local
->mtx
);
513 lockdep_assert_held(&local
->chanctx_mtx
);
515 ctx
= ieee80211_alloc_chanctx(local
, chandef
, mode
);
517 return ERR_PTR(-ENOMEM
);
519 err
= ieee80211_add_chanctx(local
, ctx
);
525 list_add_rcu(&ctx
->list
, &local
->chanctx_list
);
529 static void ieee80211_del_chanctx(struct ieee80211_local
*local
,
530 struct ieee80211_chanctx
*ctx
)
532 lockdep_assert_held(&local
->chanctx_mtx
);
534 if (!local
->use_chanctx
) {
535 struct cfg80211_chan_def
*chandef
= &local
->_oper_chandef
;
536 chandef
->width
= NL80211_CHAN_WIDTH_20_NOHT
;
537 chandef
->center_freq1
= chandef
->chan
->center_freq
;
538 chandef
->center_freq2
= 0;
540 /* NOTE: Disabling radar is only valid here for
541 * single channel context. To be sure, check it ...
543 WARN_ON(local
->hw
.conf
.radar_enabled
&&
544 !list_empty(&local
->chanctx_list
));
546 local
->hw
.conf
.radar_enabled
= false;
548 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
550 drv_remove_chanctx(local
, ctx
);
553 ieee80211_recalc_idle(local
);
556 static void ieee80211_free_chanctx(struct ieee80211_local
*local
,
557 struct ieee80211_chanctx
*ctx
)
559 lockdep_assert_held(&local
->chanctx_mtx
);
561 WARN_ON_ONCE(ieee80211_chanctx_refcount(local
, ctx
) != 0);
563 list_del_rcu(&ctx
->list
);
564 ieee80211_del_chanctx(local
, ctx
);
565 kfree_rcu(ctx
, rcu_head
);
568 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local
*local
,
569 struct ieee80211_chanctx
*ctx
)
571 struct ieee80211_chanctx_conf
*conf
= &ctx
->conf
;
572 struct ieee80211_sub_if_data
*sdata
;
573 const struct cfg80211_chan_def
*compat
= NULL
;
574 struct sta_info
*sta
;
576 lockdep_assert_held(&local
->chanctx_mtx
);
579 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
581 if (!ieee80211_sdata_running(sdata
))
583 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) != conf
)
585 if (sdata
->vif
.type
== NL80211_IFTYPE_AP_VLAN
)
589 compat
= &sdata
->vif
.bss_conf
.chandef
;
591 compat
= cfg80211_chandef_compatible(
592 &sdata
->vif
.bss_conf
.chandef
, compat
);
593 if (WARN_ON_ONCE(!compat
))
597 /* TDLS peers can sometimes affect the chandef width */
598 list_for_each_entry_rcu(sta
, &local
->sta_list
, list
) {
599 if (!sta
->uploaded
||
600 !test_sta_flag(sta
, WLAN_STA_TDLS_WIDER_BW
) ||
601 !test_sta_flag(sta
, WLAN_STA_AUTHORIZED
) ||
602 !sta
->tdls_chandef
.chan
)
605 compat
= cfg80211_chandef_compatible(&sta
->tdls_chandef
,
607 if (WARN_ON_ONCE(!compat
))
615 ieee80211_change_chanctx(local
, ctx
, compat
);
618 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local
*local
,
619 struct ieee80211_chanctx
*chanctx
)
623 lockdep_assert_held(&local
->chanctx_mtx
);
624 /* for ieee80211_is_radar_required */
625 lockdep_assert_held(&local
->mtx
);
627 radar_enabled
= ieee80211_chanctx_radar_required(local
, chanctx
);
629 if (radar_enabled
== chanctx
->conf
.radar_enabled
)
632 chanctx
->conf
.radar_enabled
= radar_enabled
;
634 if (!local
->use_chanctx
) {
635 local
->hw
.conf
.radar_enabled
= chanctx
->conf
.radar_enabled
;
636 ieee80211_hw_config(local
, IEEE80211_CONF_CHANGE_CHANNEL
);
639 drv_change_chanctx(local
, chanctx
, IEEE80211_CHANCTX_CHANGE_RADAR
);
642 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data
*sdata
,
643 struct ieee80211_chanctx
*new_ctx
)
645 struct ieee80211_local
*local
= sdata
->local
;
646 struct ieee80211_chanctx_conf
*conf
;
647 struct ieee80211_chanctx
*curr_ctx
= NULL
;
650 if (WARN_ON(sdata
->vif
.type
== NL80211_IFTYPE_NAN
))
653 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
654 lockdep_is_held(&local
->chanctx_mtx
));
657 curr_ctx
= container_of(conf
, struct ieee80211_chanctx
, conf
);
659 drv_unassign_vif_chanctx(local
, sdata
, curr_ctx
);
661 list_del(&sdata
->assigned_chanctx_list
);
665 ret
= drv_assign_vif_chanctx(local
, sdata
, new_ctx
);
669 conf
= &new_ctx
->conf
;
670 list_add(&sdata
->assigned_chanctx_list
,
671 &new_ctx
->assigned_vifs
);
675 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, conf
);
677 sdata
->vif
.bss_conf
.idle
= !conf
;
679 if (curr_ctx
&& ieee80211_chanctx_num_assigned(local
, curr_ctx
) > 0) {
680 ieee80211_recalc_chanctx_chantype(local
, curr_ctx
);
681 ieee80211_recalc_smps_chanctx(local
, curr_ctx
);
682 ieee80211_recalc_radar_chanctx(local
, curr_ctx
);
683 ieee80211_recalc_chanctx_min_def(local
, curr_ctx
);
686 if (new_ctx
&& ieee80211_chanctx_num_assigned(local
, new_ctx
) > 0) {
687 ieee80211_recalc_txpower(sdata
, false);
688 ieee80211_recalc_chanctx_min_def(local
, new_ctx
);
691 if (sdata
->vif
.type
!= NL80211_IFTYPE_P2P_DEVICE
&&
692 sdata
->vif
.type
!= NL80211_IFTYPE_MONITOR
)
693 ieee80211_bss_info_change_notify(sdata
,
696 ieee80211_check_fast_xmit_iface(sdata
);
701 void ieee80211_recalc_smps_chanctx(struct ieee80211_local
*local
,
702 struct ieee80211_chanctx
*chanctx
)
704 struct ieee80211_sub_if_data
*sdata
;
705 u8 rx_chains_static
, rx_chains_dynamic
;
707 lockdep_assert_held(&local
->chanctx_mtx
);
709 rx_chains_static
= 1;
710 rx_chains_dynamic
= 1;
713 list_for_each_entry_rcu(sdata
, &local
->interfaces
, list
) {
714 u8 needed_static
, needed_dynamic
;
716 if (!ieee80211_sdata_running(sdata
))
719 if (rcu_access_pointer(sdata
->vif
.chanctx_conf
) !=
723 switch (sdata
->vif
.type
) {
724 case NL80211_IFTYPE_P2P_DEVICE
:
725 case NL80211_IFTYPE_NAN
:
727 case NL80211_IFTYPE_STATION
:
728 if (!sdata
->u
.mgd
.associated
)
731 case NL80211_IFTYPE_AP_VLAN
:
733 case NL80211_IFTYPE_AP
:
734 case NL80211_IFTYPE_ADHOC
:
735 case NL80211_IFTYPE_WDS
:
736 case NL80211_IFTYPE_MESH_POINT
:
737 case NL80211_IFTYPE_OCB
:
743 switch (sdata
->smps_mode
) {
745 WARN_ONCE(1, "Invalid SMPS mode %d\n",
748 case IEEE80211_SMPS_OFF
:
749 needed_static
= sdata
->needed_rx_chains
;
750 needed_dynamic
= sdata
->needed_rx_chains
;
752 case IEEE80211_SMPS_DYNAMIC
:
754 needed_dynamic
= sdata
->needed_rx_chains
;
756 case IEEE80211_SMPS_STATIC
:
762 rx_chains_static
= max(rx_chains_static
, needed_static
);
763 rx_chains_dynamic
= max(rx_chains_dynamic
, needed_dynamic
);
766 /* Disable SMPS for the monitor interface */
767 sdata
= rcu_dereference(local
->monitor_sdata
);
769 rcu_access_pointer(sdata
->vif
.chanctx_conf
) == &chanctx
->conf
)
770 rx_chains_dynamic
= rx_chains_static
= local
->rx_chains
;
774 if (!local
->use_chanctx
) {
775 if (rx_chains_static
> 1)
776 local
->smps_mode
= IEEE80211_SMPS_OFF
;
777 else if (rx_chains_dynamic
> 1)
778 local
->smps_mode
= IEEE80211_SMPS_DYNAMIC
;
780 local
->smps_mode
= IEEE80211_SMPS_STATIC
;
781 ieee80211_hw_config(local
, 0);
784 if (rx_chains_static
== chanctx
->conf
.rx_chains_static
&&
785 rx_chains_dynamic
== chanctx
->conf
.rx_chains_dynamic
)
788 chanctx
->conf
.rx_chains_static
= rx_chains_static
;
789 chanctx
->conf
.rx_chains_dynamic
= rx_chains_dynamic
;
790 drv_change_chanctx(local
, chanctx
, IEEE80211_CHANCTX_CHANGE_RX_CHAINS
);
794 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data
*sdata
,
797 struct ieee80211_local
*local __maybe_unused
= sdata
->local
;
798 struct ieee80211_sub_if_data
*vlan
;
799 struct ieee80211_chanctx_conf
*conf
;
801 if (WARN_ON(sdata
->vif
.type
!= NL80211_IFTYPE_AP
))
804 lockdep_assert_held(&local
->mtx
);
806 /* Check that conf exists, even when clearing this function
807 * must be called with the AP's channel context still there
808 * as it would otherwise cause VLANs to have an invalid
809 * channel context pointer for a while, possibly pointing
810 * to a channel context that has already been freed.
812 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
813 lockdep_is_held(&local
->chanctx_mtx
));
819 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
820 rcu_assign_pointer(vlan
->vif
.chanctx_conf
, conf
);
823 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data
*sdata
,
826 struct ieee80211_local
*local
= sdata
->local
;
828 mutex_lock(&local
->chanctx_mtx
);
830 __ieee80211_vif_copy_chanctx_to_vlans(sdata
, clear
);
832 mutex_unlock(&local
->chanctx_mtx
);
835 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data
*sdata
)
837 struct ieee80211_chanctx
*ctx
= sdata
->reserved_chanctx
;
839 lockdep_assert_held(&sdata
->local
->chanctx_mtx
);
844 list_del(&sdata
->reserved_chanctx_list
);
845 sdata
->reserved_chanctx
= NULL
;
847 if (ieee80211_chanctx_refcount(sdata
->local
, ctx
) == 0) {
848 if (ctx
->replace_state
== IEEE80211_CHANCTX_REPLACES_OTHER
) {
849 if (WARN_ON(!ctx
->replace_ctx
))
852 WARN_ON(ctx
->replace_ctx
->replace_state
!=
853 IEEE80211_CHANCTX_WILL_BE_REPLACED
);
854 WARN_ON(ctx
->replace_ctx
->replace_ctx
!= ctx
);
856 ctx
->replace_ctx
->replace_ctx
= NULL
;
857 ctx
->replace_ctx
->replace_state
=
858 IEEE80211_CHANCTX_REPLACE_NONE
;
860 list_del_rcu(&ctx
->list
);
861 kfree_rcu(ctx
, rcu_head
);
863 ieee80211_free_chanctx(sdata
->local
, ctx
);
870 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data
*sdata
,
871 const struct cfg80211_chan_def
*chandef
,
872 enum ieee80211_chanctx_mode mode
,
875 struct ieee80211_local
*local
= sdata
->local
;
876 struct ieee80211_chanctx
*new_ctx
, *curr_ctx
, *ctx
;
878 lockdep_assert_held(&local
->chanctx_mtx
);
880 curr_ctx
= ieee80211_vif_get_chanctx(sdata
);
881 if (curr_ctx
&& local
->use_chanctx
&& !local
->ops
->switch_vif_chanctx
)
884 new_ctx
= ieee80211_find_reservation_chanctx(local
, chandef
, mode
);
886 if (ieee80211_can_create_new_chanctx(local
)) {
887 new_ctx
= ieee80211_new_chanctx(local
, chandef
, mode
);
889 return PTR_ERR(new_ctx
);
892 (curr_ctx
->replace_state
==
893 IEEE80211_CHANCTX_WILL_BE_REPLACED
) ||
894 !list_empty(&curr_ctx
->reserved_vifs
)) {
896 * Another vif already requested this context
897 * for a reservation. Find another one hoping
898 * all vifs assigned to it will also switch
901 * TODO: This needs a little more work as some
902 * cases (more than 2 chanctx capable devices)
903 * may fail which could otherwise succeed
904 * provided some channel context juggling was
907 * Consider ctx1..3, vif1..6, each ctx has 2
908 * vifs. vif1 and vif2 from ctx1 request new
909 * different chandefs starting 2 in-place
910 * reserations with ctx4 and ctx5 replacing
911 * ctx1 and ctx2 respectively. Next vif5 and
912 * vif6 from ctx3 reserve ctx4. If vif3 and
913 * vif4 remain on ctx2 as they are then this
914 * fails unless `replace_ctx` from ctx5 is
915 * replaced with ctx3.
917 list_for_each_entry(ctx
, &local
->chanctx_list
,
919 if (ctx
->replace_state
!=
920 IEEE80211_CHANCTX_REPLACE_NONE
)
923 if (!list_empty(&ctx
->reserved_vifs
))
932 * If that's true then all available contexts already
933 * have reservations and cannot be used.
936 (curr_ctx
->replace_state
==
937 IEEE80211_CHANCTX_WILL_BE_REPLACED
) ||
938 !list_empty(&curr_ctx
->reserved_vifs
))
941 new_ctx
= ieee80211_alloc_chanctx(local
, chandef
, mode
);
945 new_ctx
->replace_ctx
= curr_ctx
;
946 new_ctx
->replace_state
=
947 IEEE80211_CHANCTX_REPLACES_OTHER
;
949 curr_ctx
->replace_ctx
= new_ctx
;
950 curr_ctx
->replace_state
=
951 IEEE80211_CHANCTX_WILL_BE_REPLACED
;
953 list_add_rcu(&new_ctx
->list
, &local
->chanctx_list
);
957 list_add(&sdata
->reserved_chanctx_list
, &new_ctx
->reserved_vifs
);
958 sdata
->reserved_chanctx
= new_ctx
;
959 sdata
->reserved_chandef
= *chandef
;
960 sdata
->reserved_radar_required
= radar_required
;
961 sdata
->reserved_ready
= false;
967 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data
*sdata
)
969 switch (sdata
->vif
.type
) {
970 case NL80211_IFTYPE_ADHOC
:
971 case NL80211_IFTYPE_AP
:
972 case NL80211_IFTYPE_MESH_POINT
:
973 case NL80211_IFTYPE_OCB
:
974 ieee80211_queue_work(&sdata
->local
->hw
,
975 &sdata
->csa_finalize_work
);
977 case NL80211_IFTYPE_STATION
:
978 ieee80211_queue_work(&sdata
->local
->hw
,
979 &sdata
->u
.mgd
.chswitch_work
);
981 case NL80211_IFTYPE_UNSPECIFIED
:
982 case NL80211_IFTYPE_AP_VLAN
:
983 case NL80211_IFTYPE_WDS
:
984 case NL80211_IFTYPE_MONITOR
:
985 case NL80211_IFTYPE_P2P_CLIENT
:
986 case NL80211_IFTYPE_P2P_GO
:
987 case NL80211_IFTYPE_P2P_DEVICE
:
988 case NL80211_IFTYPE_NAN
:
989 case NUM_NL80211_IFTYPES
:
996 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data
*sdata
,
997 const struct cfg80211_chan_def
*chandef
)
999 struct ieee80211_sub_if_data
*vlan
;
1001 sdata
->vif
.bss_conf
.chandef
= *chandef
;
1003 if (sdata
->vif
.type
!= NL80211_IFTYPE_AP
)
1006 list_for_each_entry(vlan
, &sdata
->u
.ap
.vlans
, u
.vlan
.list
)
1007 vlan
->vif
.bss_conf
.chandef
= *chandef
;
1011 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data
*sdata
)
1013 struct ieee80211_local
*local
= sdata
->local
;
1014 struct ieee80211_vif_chanctx_switch vif_chsw
[1] = {};
1015 struct ieee80211_chanctx
*old_ctx
, *new_ctx
;
1016 const struct cfg80211_chan_def
*chandef
;
1020 lockdep_assert_held(&local
->mtx
);
1021 lockdep_assert_held(&local
->chanctx_mtx
);
1023 new_ctx
= sdata
->reserved_chanctx
;
1024 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1026 if (WARN_ON(!sdata
->reserved_ready
))
1029 if (WARN_ON(!new_ctx
))
1032 if (WARN_ON(!old_ctx
))
1035 if (WARN_ON(new_ctx
->replace_state
==
1036 IEEE80211_CHANCTX_REPLACES_OTHER
))
1039 chandef
= ieee80211_chanctx_non_reserved_chandef(local
, new_ctx
,
1040 &sdata
->reserved_chandef
);
1041 if (WARN_ON(!chandef
))
1044 ieee80211_change_chanctx(local
, new_ctx
, chandef
);
1046 vif_chsw
[0].vif
= &sdata
->vif
;
1047 vif_chsw
[0].old_ctx
= &old_ctx
->conf
;
1048 vif_chsw
[0].new_ctx
= &new_ctx
->conf
;
1050 list_del(&sdata
->reserved_chanctx_list
);
1051 sdata
->reserved_chanctx
= NULL
;
1053 err
= drv_switch_vif_chanctx(local
, vif_chsw
, 1,
1054 CHANCTX_SWMODE_REASSIGN_VIF
);
1056 if (ieee80211_chanctx_refcount(local
, new_ctx
) == 0)
1057 ieee80211_free_chanctx(local
, new_ctx
);
1062 list_move(&sdata
->assigned_chanctx_list
, &new_ctx
->assigned_vifs
);
1063 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, &new_ctx
->conf
);
1065 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
)
1066 __ieee80211_vif_copy_chanctx_to_vlans(sdata
, false);
1068 ieee80211_check_fast_xmit_iface(sdata
);
1070 if (ieee80211_chanctx_refcount(local
, old_ctx
) == 0)
1071 ieee80211_free_chanctx(local
, old_ctx
);
1073 if (sdata
->vif
.bss_conf
.chandef
.width
!= sdata
->reserved_chandef
.width
)
1074 changed
= BSS_CHANGED_BANDWIDTH
;
1076 ieee80211_vif_update_chandef(sdata
, &sdata
->reserved_chandef
);
1078 ieee80211_recalc_smps_chanctx(local
, new_ctx
);
1079 ieee80211_recalc_radar_chanctx(local
, new_ctx
);
1080 ieee80211_recalc_chanctx_min_def(local
, new_ctx
);
1083 ieee80211_bss_info_change_notify(sdata
, changed
);
1086 ieee80211_vif_chanctx_reservation_complete(sdata
);
1091 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data
*sdata
)
1093 struct ieee80211_local
*local
= sdata
->local
;
1094 struct ieee80211_chanctx
*old_ctx
, *new_ctx
;
1095 const struct cfg80211_chan_def
*chandef
;
1098 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1099 new_ctx
= sdata
->reserved_chanctx
;
1101 if (WARN_ON(!sdata
->reserved_ready
))
1104 if (WARN_ON(old_ctx
))
1107 if (WARN_ON(!new_ctx
))
1110 if (WARN_ON(new_ctx
->replace_state
==
1111 IEEE80211_CHANCTX_REPLACES_OTHER
))
1114 chandef
= ieee80211_chanctx_non_reserved_chandef(local
, new_ctx
,
1115 &sdata
->reserved_chandef
);
1116 if (WARN_ON(!chandef
))
1119 ieee80211_change_chanctx(local
, new_ctx
, chandef
);
1121 list_del(&sdata
->reserved_chanctx_list
);
1122 sdata
->reserved_chanctx
= NULL
;
1124 err
= ieee80211_assign_vif_chanctx(sdata
, new_ctx
);
1126 if (ieee80211_chanctx_refcount(local
, new_ctx
) == 0)
1127 ieee80211_free_chanctx(local
, new_ctx
);
1133 ieee80211_vif_chanctx_reservation_complete(sdata
);
1138 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data
*sdata
)
1140 struct ieee80211_chanctx
*old_ctx
, *new_ctx
;
1142 lockdep_assert_held(&sdata
->local
->chanctx_mtx
);
1144 new_ctx
= sdata
->reserved_chanctx
;
1145 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1150 if (WARN_ON(!new_ctx
))
1153 if (old_ctx
->replace_state
!= IEEE80211_CHANCTX_WILL_BE_REPLACED
)
1156 if (new_ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1162 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local
*local
,
1163 struct ieee80211_chanctx
*new_ctx
)
1165 const struct cfg80211_chan_def
*chandef
;
1167 lockdep_assert_held(&local
->mtx
);
1168 lockdep_assert_held(&local
->chanctx_mtx
);
1170 chandef
= ieee80211_chanctx_reserved_chandef(local
, new_ctx
, NULL
);
1171 if (WARN_ON(!chandef
))
1174 local
->hw
.conf
.radar_enabled
= new_ctx
->conf
.radar_enabled
;
1175 local
->_oper_chandef
= *chandef
;
1176 ieee80211_hw_config(local
, 0);
1181 static int ieee80211_chsw_switch_vifs(struct ieee80211_local
*local
,
1184 struct ieee80211_vif_chanctx_switch
*vif_chsw
;
1185 struct ieee80211_sub_if_data
*sdata
;
1186 struct ieee80211_chanctx
*ctx
, *old_ctx
;
1189 lockdep_assert_held(&local
->mtx
);
1190 lockdep_assert_held(&local
->chanctx_mtx
);
1192 vif_chsw
= kzalloc(sizeof(vif_chsw
[0]) * n_vifs
, GFP_KERNEL
);
1197 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1198 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1201 if (WARN_ON(!ctx
->replace_ctx
)) {
1206 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
1207 reserved_chanctx_list
) {
1208 if (!ieee80211_vif_has_in_place_reservation(
1212 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1213 vif_chsw
[i
].vif
= &sdata
->vif
;
1214 vif_chsw
[i
].old_ctx
= &old_ctx
->conf
;
1215 vif_chsw
[i
].new_ctx
= &ctx
->conf
;
1221 err
= drv_switch_vif_chanctx(local
, vif_chsw
, n_vifs
,
1222 CHANCTX_SWMODE_SWAP_CONTEXTS
);
1229 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local
*local
)
1231 struct ieee80211_chanctx
*ctx
;
1234 lockdep_assert_held(&local
->mtx
);
1235 lockdep_assert_held(&local
->chanctx_mtx
);
1237 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1238 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1241 if (!list_empty(&ctx
->replace_ctx
->assigned_vifs
))
1244 ieee80211_del_chanctx(local
, ctx
->replace_ctx
);
1245 err
= ieee80211_add_chanctx(local
, ctx
);
1253 WARN_ON(ieee80211_add_chanctx(local
, ctx
));
1254 list_for_each_entry_continue_reverse(ctx
, &local
->chanctx_list
, list
) {
1255 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1258 if (!list_empty(&ctx
->replace_ctx
->assigned_vifs
))
1261 ieee80211_del_chanctx(local
, ctx
);
1262 WARN_ON(ieee80211_add_chanctx(local
, ctx
->replace_ctx
));
1268 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local
*local
)
1270 struct ieee80211_sub_if_data
*sdata
, *sdata_tmp
;
1271 struct ieee80211_chanctx
*ctx
, *ctx_tmp
, *old_ctx
;
1272 struct ieee80211_chanctx
*new_ctx
= NULL
;
1273 int i
, err
, n_assigned
, n_reserved
, n_ready
;
1274 int n_ctx
= 0, n_vifs_switch
= 0, n_vifs_assign
= 0, n_vifs_ctxless
= 0;
1276 lockdep_assert_held(&local
->mtx
);
1277 lockdep_assert_held(&local
->chanctx_mtx
);
1280 * If there are 2 independent pairs of channel contexts performing
1281 * cross-switch of their vifs this code will still wait until both are
1282 * ready even though it could be possible to switch one before the
1285 * For practical reasons and code simplicity just do a single huge
1290 * Verify if the reservation is still feasible.
1291 * - if it's not then disconnect
1292 * - if it is but not all vifs necessary are ready then defer
1295 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1296 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1299 if (WARN_ON(!ctx
->replace_ctx
)) {
1304 if (!local
->use_chanctx
)
1313 list_for_each_entry(sdata
, &ctx
->replace_ctx
->assigned_vifs
,
1314 assigned_chanctx_list
) {
1316 if (sdata
->reserved_chanctx
) {
1318 if (sdata
->reserved_ready
)
1323 if (n_assigned
!= n_reserved
) {
1324 if (n_ready
== n_reserved
) {
1325 wiphy_info(local
->hw
.wiphy
,
1326 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1334 ctx
->conf
.radar_enabled
= false;
1335 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
1336 reserved_chanctx_list
) {
1337 if (ieee80211_vif_has_in_place_reservation(sdata
) &&
1338 !sdata
->reserved_ready
)
1341 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1343 if (old_ctx
->replace_state
==
1344 IEEE80211_CHANCTX_WILL_BE_REPLACED
)
1352 if (sdata
->reserved_radar_required
)
1353 ctx
->conf
.radar_enabled
= true;
1357 if (WARN_ON(n_ctx
== 0) ||
1358 WARN_ON(n_vifs_switch
== 0 &&
1359 n_vifs_assign
== 0 &&
1360 n_vifs_ctxless
== 0) ||
1361 WARN_ON(n_ctx
> 1 && !local
->use_chanctx
) ||
1362 WARN_ON(!new_ctx
&& !local
->use_chanctx
)) {
1368 * All necessary vifs are ready. Perform the switch now depending on
1369 * reservations and driver capabilities.
1372 if (local
->use_chanctx
) {
1373 if (n_vifs_switch
> 0) {
1374 err
= ieee80211_chsw_switch_vifs(local
, n_vifs_switch
);
1379 if (n_vifs_assign
> 0 || n_vifs_ctxless
> 0) {
1380 err
= ieee80211_chsw_switch_ctxs(local
);
1385 err
= ieee80211_chsw_switch_hwconf(local
, new_ctx
);
1391 * Update all structures, values and pointers to point to new channel
1396 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1397 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1400 if (WARN_ON(!ctx
->replace_ctx
)) {
1405 list_for_each_entry(sdata
, &ctx
->reserved_vifs
,
1406 reserved_chanctx_list
) {
1409 if (!ieee80211_vif_has_in_place_reservation(sdata
))
1412 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, &ctx
->conf
);
1414 if (sdata
->vif
.type
== NL80211_IFTYPE_AP
)
1415 __ieee80211_vif_copy_chanctx_to_vlans(sdata
,
1418 ieee80211_check_fast_xmit_iface(sdata
);
1420 sdata
->radar_required
= sdata
->reserved_radar_required
;
1422 if (sdata
->vif
.bss_conf
.chandef
.width
!=
1423 sdata
->reserved_chandef
.width
)
1424 changed
= BSS_CHANGED_BANDWIDTH
;
1426 ieee80211_vif_update_chandef(sdata
, &sdata
->reserved_chandef
);
1428 ieee80211_bss_info_change_notify(sdata
,
1431 ieee80211_recalc_txpower(sdata
, false);
1434 ieee80211_recalc_chanctx_chantype(local
, ctx
);
1435 ieee80211_recalc_smps_chanctx(local
, ctx
);
1436 ieee80211_recalc_radar_chanctx(local
, ctx
);
1437 ieee80211_recalc_chanctx_min_def(local
, ctx
);
1439 list_for_each_entry_safe(sdata
, sdata_tmp
, &ctx
->reserved_vifs
,
1440 reserved_chanctx_list
) {
1441 if (ieee80211_vif_get_chanctx(sdata
) != ctx
)
1444 list_del(&sdata
->reserved_chanctx_list
);
1445 list_move(&sdata
->assigned_chanctx_list
,
1446 &ctx
->assigned_vifs
);
1447 sdata
->reserved_chanctx
= NULL
;
1449 ieee80211_vif_chanctx_reservation_complete(sdata
);
1453 * This context might have been a dependency for an already
1454 * ready re-assign reservation interface that was deferred. Do
1455 * not propagate error to the caller though. The in-place
1456 * reservation for originally requested interface has already
1457 * succeeded at this point.
1459 list_for_each_entry_safe(sdata
, sdata_tmp
, &ctx
->reserved_vifs
,
1460 reserved_chanctx_list
) {
1461 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1465 if (WARN_ON(sdata
->reserved_chanctx
!= ctx
))
1468 if (!sdata
->reserved_ready
)
1471 if (ieee80211_vif_get_chanctx(sdata
))
1472 err
= ieee80211_vif_use_reserved_reassign(
1475 err
= ieee80211_vif_use_reserved_assign(sdata
);
1479 "failed to finalize (re-)assign reservation (err=%d)\n",
1481 ieee80211_vif_unreserve_chanctx(sdata
);
1482 cfg80211_stop_iface(local
->hw
.wiphy
,
1490 * Finally free old contexts
1493 list_for_each_entry_safe(ctx
, ctx_tmp
, &local
->chanctx_list
, list
) {
1494 if (ctx
->replace_state
!= IEEE80211_CHANCTX_WILL_BE_REPLACED
)
1497 ctx
->replace_ctx
->replace_ctx
= NULL
;
1498 ctx
->replace_ctx
->replace_state
=
1499 IEEE80211_CHANCTX_REPLACE_NONE
;
1501 list_del_rcu(&ctx
->list
);
1502 kfree_rcu(ctx
, rcu_head
);
1508 list_for_each_entry(ctx
, &local
->chanctx_list
, list
) {
1509 if (ctx
->replace_state
!= IEEE80211_CHANCTX_REPLACES_OTHER
)
1512 list_for_each_entry_safe(sdata
, sdata_tmp
, &ctx
->reserved_vifs
,
1513 reserved_chanctx_list
) {
1514 ieee80211_vif_unreserve_chanctx(sdata
);
1515 ieee80211_vif_chanctx_reservation_complete(sdata
);
1522 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data
*sdata
)
1524 struct ieee80211_local
*local
= sdata
->local
;
1525 struct ieee80211_chanctx_conf
*conf
;
1526 struct ieee80211_chanctx
*ctx
;
1527 bool use_reserved_switch
= false;
1529 lockdep_assert_held(&local
->chanctx_mtx
);
1531 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
1532 lockdep_is_held(&local
->chanctx_mtx
));
1536 ctx
= container_of(conf
, struct ieee80211_chanctx
, conf
);
1538 if (sdata
->reserved_chanctx
) {
1539 if (sdata
->reserved_chanctx
->replace_state
==
1540 IEEE80211_CHANCTX_REPLACES_OTHER
&&
1541 ieee80211_chanctx_num_reserved(local
,
1542 sdata
->reserved_chanctx
) > 1)
1543 use_reserved_switch
= true;
1545 ieee80211_vif_unreserve_chanctx(sdata
);
1548 ieee80211_assign_vif_chanctx(sdata
, NULL
);
1549 if (ieee80211_chanctx_refcount(local
, ctx
) == 0)
1550 ieee80211_free_chanctx(local
, ctx
);
1552 sdata
->radar_required
= false;
1554 /* Unreserving may ready an in-place reservation. */
1555 if (use_reserved_switch
)
1556 ieee80211_vif_use_reserved_switch(local
);
1559 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data
*sdata
,
1560 const struct cfg80211_chan_def
*chandef
,
1561 enum ieee80211_chanctx_mode mode
)
1563 struct ieee80211_local
*local
= sdata
->local
;
1564 struct ieee80211_chanctx
*ctx
;
1565 u8 radar_detect_width
= 0;
1568 lockdep_assert_held(&local
->mtx
);
1570 WARN_ON(sdata
->dev
&& netif_carrier_ok(sdata
->dev
));
1572 mutex_lock(&local
->chanctx_mtx
);
1574 ret
= cfg80211_chandef_dfs_required(local
->hw
.wiphy
,
1576 sdata
->wdev
.iftype
);
1580 radar_detect_width
= BIT(chandef
->width
);
1582 sdata
->radar_required
= ret
;
1584 ret
= ieee80211_check_combinations(sdata
, chandef
, mode
,
1585 radar_detect_width
);
1589 __ieee80211_vif_release_channel(sdata
);
1591 ctx
= ieee80211_find_chanctx(local
, chandef
, mode
);
1593 ctx
= ieee80211_new_chanctx(local
, chandef
, mode
);
1599 ieee80211_vif_update_chandef(sdata
, chandef
);
1601 ret
= ieee80211_assign_vif_chanctx(sdata
, ctx
);
1603 /* if assign fails refcount stays the same */
1604 if (ieee80211_chanctx_refcount(local
, ctx
) == 0)
1605 ieee80211_free_chanctx(local
, ctx
);
1609 ieee80211_recalc_smps_chanctx(local
, ctx
);
1610 ieee80211_recalc_radar_chanctx(local
, ctx
);
1613 sdata
->radar_required
= false;
1615 mutex_unlock(&local
->chanctx_mtx
);
1619 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data
*sdata
)
1621 struct ieee80211_local
*local
= sdata
->local
;
1622 struct ieee80211_chanctx
*new_ctx
;
1623 struct ieee80211_chanctx
*old_ctx
;
1626 lockdep_assert_held(&local
->mtx
);
1627 lockdep_assert_held(&local
->chanctx_mtx
);
1629 new_ctx
= sdata
->reserved_chanctx
;
1630 old_ctx
= ieee80211_vif_get_chanctx(sdata
);
1632 if (WARN_ON(!new_ctx
))
1635 if (WARN_ON(new_ctx
->replace_state
==
1636 IEEE80211_CHANCTX_WILL_BE_REPLACED
))
1639 if (WARN_ON(sdata
->reserved_ready
))
1642 sdata
->reserved_ready
= true;
1644 if (new_ctx
->replace_state
== IEEE80211_CHANCTX_REPLACE_NONE
) {
1646 err
= ieee80211_vif_use_reserved_reassign(sdata
);
1648 err
= ieee80211_vif_use_reserved_assign(sdata
);
1655 * In-place reservation may need to be finalized now either if:
1656 * a) sdata is taking part in the swapping itself and is the last one
1657 * b) sdata has switched with a re-assign reservation to an existing
1658 * context readying in-place switching of old_ctx
1660 * In case of (b) do not propagate the error up because the requested
1661 * sdata already switched successfully. Just spill an extra warning.
1662 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1663 * interfaces upon failure.
1666 old_ctx
->replace_state
== IEEE80211_CHANCTX_WILL_BE_REPLACED
) ||
1667 new_ctx
->replace_state
== IEEE80211_CHANCTX_REPLACES_OTHER
) {
1668 err
= ieee80211_vif_use_reserved_switch(local
);
1669 if (err
&& err
!= -EAGAIN
) {
1670 if (new_ctx
->replace_state
==
1671 IEEE80211_CHANCTX_REPLACES_OTHER
)
1674 wiphy_info(local
->hw
.wiphy
,
1675 "depending in-place reservation failed (err=%d)\n",
1683 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data
*sdata
,
1684 const struct cfg80211_chan_def
*chandef
,
1687 struct ieee80211_local
*local
= sdata
->local
;
1688 struct ieee80211_chanctx_conf
*conf
;
1689 struct ieee80211_chanctx
*ctx
;
1690 const struct cfg80211_chan_def
*compat
;
1693 if (!cfg80211_chandef_usable(sdata
->local
->hw
.wiphy
, chandef
,
1694 IEEE80211_CHAN_DISABLED
))
1697 mutex_lock(&local
->chanctx_mtx
);
1698 if (cfg80211_chandef_identical(chandef
, &sdata
->vif
.bss_conf
.chandef
)) {
1703 if (chandef
->width
== NL80211_CHAN_WIDTH_20_NOHT
||
1704 sdata
->vif
.bss_conf
.chandef
.width
== NL80211_CHAN_WIDTH_20_NOHT
) {
1709 conf
= rcu_dereference_protected(sdata
->vif
.chanctx_conf
,
1710 lockdep_is_held(&local
->chanctx_mtx
));
1716 ctx
= container_of(conf
, struct ieee80211_chanctx
, conf
);
1718 compat
= cfg80211_chandef_compatible(&conf
->def
, chandef
);
1724 switch (ctx
->replace_state
) {
1725 case IEEE80211_CHANCTX_REPLACE_NONE
:
1726 if (!ieee80211_chanctx_reserved_chandef(local
, ctx
, compat
)) {
1731 case IEEE80211_CHANCTX_WILL_BE_REPLACED
:
1732 /* TODO: Perhaps the bandwidth change could be treated as a
1733 * reservation itself? */
1736 case IEEE80211_CHANCTX_REPLACES_OTHER
:
1737 /* channel context that is going to replace another channel
1738 * context doesn't really exist and shouldn't be assigned
1744 ieee80211_vif_update_chandef(sdata
, chandef
);
1746 ieee80211_recalc_chanctx_chantype(local
, ctx
);
1748 *changed
|= BSS_CHANGED_BANDWIDTH
;
1751 mutex_unlock(&local
->chanctx_mtx
);
1755 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data
*sdata
)
1757 WARN_ON(sdata
->dev
&& netif_carrier_ok(sdata
->dev
));
1759 lockdep_assert_held(&sdata
->local
->mtx
);
1761 mutex_lock(&sdata
->local
->chanctx_mtx
);
1762 __ieee80211_vif_release_channel(sdata
);
1763 mutex_unlock(&sdata
->local
->chanctx_mtx
);
1766 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data
*sdata
)
1768 struct ieee80211_local
*local
= sdata
->local
;
1769 struct ieee80211_sub_if_data
*ap
;
1770 struct ieee80211_chanctx_conf
*conf
;
1772 if (WARN_ON(sdata
->vif
.type
!= NL80211_IFTYPE_AP_VLAN
|| !sdata
->bss
))
1775 ap
= container_of(sdata
->bss
, struct ieee80211_sub_if_data
, u
.ap
);
1777 mutex_lock(&local
->chanctx_mtx
);
1779 conf
= rcu_dereference_protected(ap
->vif
.chanctx_conf
,
1780 lockdep_is_held(&local
->chanctx_mtx
));
1781 rcu_assign_pointer(sdata
->vif
.chanctx_conf
, conf
);
1782 mutex_unlock(&local
->chanctx_mtx
);
1785 void ieee80211_iter_chan_contexts_atomic(
1786 struct ieee80211_hw
*hw
,
1787 void (*iter
)(struct ieee80211_hw
*hw
,
1788 struct ieee80211_chanctx_conf
*chanctx_conf
,
1792 struct ieee80211_local
*local
= hw_to_local(hw
);
1793 struct ieee80211_chanctx
*ctx
;
1796 list_for_each_entry_rcu(ctx
, &local
->chanctx_list
, list
)
1797 if (ctx
->driver_present
)
1798 iter(hw
, &ctx
->conf
, iter_data
);
1801 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic
);