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 enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
14 switch (sta->bandwidth) {
15 case IEEE80211_STA_RX_BW_20:
16 if (sta->ht_cap.ht_supported)
17 return NL80211_CHAN_WIDTH_20;
19 return NL80211_CHAN_WIDTH_20_NOHT;
20 case IEEE80211_STA_RX_BW_40:
21 return NL80211_CHAN_WIDTH_40;
22 case IEEE80211_STA_RX_BW_80:
23 return NL80211_CHAN_WIDTH_80;
24 case IEEE80211_STA_RX_BW_160:
26 * This applied for both 160 and 80+80. since we use
27 * the returned value to consider degradation of
28 * ctx->conf.min_def, we have to make sure to take
29 * the bigger one (NL80211_CHAN_WIDTH_160).
30 * Otherwise we might try degrading even when not
31 * needed, as the max required sta_bw returned (80+80)
32 * might be smaller than the configured bw (160).
34 return NL80211_CHAN_WIDTH_160;
37 return NL80211_CHAN_WIDTH_20;
41 static enum nl80211_chan_width
42 ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
44 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
48 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
49 if (sdata != sta->sdata &&
50 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
56 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
63 static enum nl80211_chan_width
64 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
65 struct ieee80211_chanctx_conf *conf)
67 struct ieee80211_sub_if_data *sdata;
68 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
71 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
72 struct ieee80211_vif *vif = &sdata->vif;
73 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
75 if (!ieee80211_sdata_running(sdata))
78 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
82 case NL80211_IFTYPE_AP:
83 case NL80211_IFTYPE_AP_VLAN:
84 width = ieee80211_get_max_required_bw(sdata);
86 case NL80211_IFTYPE_P2P_DEVICE:
88 case NL80211_IFTYPE_STATION:
89 case NL80211_IFTYPE_ADHOC:
90 case NL80211_IFTYPE_WDS:
91 case NL80211_IFTYPE_MESH_POINT:
92 width = vif->bss_conf.chandef.width;
94 case NL80211_IFTYPE_UNSPECIFIED:
95 case NUM_NL80211_IFTYPES:
96 case NL80211_IFTYPE_MONITOR:
97 case NL80211_IFTYPE_P2P_CLIENT:
98 case NL80211_IFTYPE_P2P_GO:
101 max_bw = max(max_bw, width);
109 * recalc the min required chan width of the channel context, which is
110 * the max of min required widths of all the interfaces bound to this
113 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
114 struct ieee80211_chanctx *ctx)
116 enum nl80211_chan_width max_bw;
117 struct cfg80211_chan_def min_def;
119 lockdep_assert_held(&local->chanctx_mtx);
121 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
122 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
123 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
124 ctx->conf.radar_enabled) {
125 ctx->conf.min_def = ctx->conf.def;
129 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
131 /* downgrade chandef up to max_bw */
132 min_def = ctx->conf.def;
133 while (min_def.width > max_bw)
134 ieee80211_chandef_downgrade(&min_def);
136 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
139 ctx->conf.min_def = min_def;
140 if (!ctx->driver_present)
143 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
146 static void ieee80211_change_chanctx(struct ieee80211_local *local,
147 struct ieee80211_chanctx *ctx,
148 const struct cfg80211_chan_def *chandef)
150 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
153 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
155 ctx->conf.def = *chandef;
156 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
157 ieee80211_recalc_chanctx_min_def(local, ctx);
159 if (!local->use_chanctx) {
160 local->_oper_chandef = *chandef;
161 ieee80211_hw_config(local, 0);
165 static struct ieee80211_chanctx *
166 ieee80211_find_chanctx(struct ieee80211_local *local,
167 const struct cfg80211_chan_def *chandef,
168 enum ieee80211_chanctx_mode mode)
170 struct ieee80211_chanctx *ctx;
172 lockdep_assert_held(&local->chanctx_mtx);
174 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
177 list_for_each_entry(ctx, &local->chanctx_list, list) {
178 const struct cfg80211_chan_def *compat;
180 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
183 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
187 ieee80211_change_chanctx(local, ctx, compat);
195 static bool ieee80211_is_radar_required(struct ieee80211_local *local)
197 struct ieee80211_sub_if_data *sdata;
199 lockdep_assert_held(&local->mtx);
202 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
203 if (sdata->radar_required) {
213 static struct ieee80211_chanctx *
214 ieee80211_new_chanctx(struct ieee80211_local *local,
215 const struct cfg80211_chan_def *chandef,
216 enum ieee80211_chanctx_mode mode)
218 struct ieee80211_chanctx *ctx;
222 lockdep_assert_held(&local->chanctx_mtx);
224 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
226 return ERR_PTR(-ENOMEM);
228 ctx->conf.def = *chandef;
229 ctx->conf.rx_chains_static = 1;
230 ctx->conf.rx_chains_dynamic = 1;
232 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
233 ieee80211_recalc_chanctx_min_def(local, ctx);
234 if (!local->use_chanctx)
235 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
237 /* we hold the mutex to prevent idle from changing */
238 lockdep_assert_held(&local->mtx);
239 /* turn idle off *before* setting channel -- some drivers need that */
240 changed = ieee80211_idle_off(local);
242 ieee80211_hw_config(local, changed);
244 if (!local->use_chanctx) {
245 local->_oper_chandef = *chandef;
246 ieee80211_hw_config(local, 0);
248 err = drv_add_chanctx(local, ctx);
251 ieee80211_recalc_idle(local);
256 /* and keep the mutex held until the new chanctx is on the list */
257 list_add_rcu(&ctx->list, &local->chanctx_list);
262 static void ieee80211_free_chanctx(struct ieee80211_local *local,
263 struct ieee80211_chanctx *ctx)
265 bool check_single_channel = false;
266 lockdep_assert_held(&local->chanctx_mtx);
268 WARN_ON_ONCE(ctx->refcount != 0);
270 if (!local->use_chanctx) {
271 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
272 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
273 chandef->center_freq1 = chandef->chan->center_freq;
274 chandef->center_freq2 = 0;
276 /* NOTE: Disabling radar is only valid here for
277 * single channel context. To be sure, check it ...
279 if (local->hw.conf.radar_enabled)
280 check_single_channel = true;
281 local->hw.conf.radar_enabled = false;
283 ieee80211_hw_config(local, 0);
285 drv_remove_chanctx(local, ctx);
288 list_del_rcu(&ctx->list);
289 kfree_rcu(ctx, rcu_head);
291 /* throw a warning if this wasn't the only channel context. */
292 WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
294 ieee80211_recalc_idle(local);
297 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
298 struct ieee80211_chanctx *ctx)
300 struct ieee80211_local *local = sdata->local;
303 lockdep_assert_held(&local->chanctx_mtx);
305 ret = drv_assign_vif_chanctx(local, sdata, ctx);
309 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
312 ieee80211_recalc_txpower(sdata);
313 ieee80211_recalc_chanctx_min_def(local, ctx);
314 sdata->vif.bss_conf.idle = false;
316 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
317 sdata->vif.type != NL80211_IFTYPE_MONITOR)
318 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
323 static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
324 struct ieee80211_chanctx *ctx)
326 struct ieee80211_chanctx_conf *conf = &ctx->conf;
327 struct ieee80211_sub_if_data *sdata;
328 const struct cfg80211_chan_def *compat = NULL;
330 lockdep_assert_held(&local->chanctx_mtx);
333 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
335 if (!ieee80211_sdata_running(sdata))
337 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
341 compat = &sdata->vif.bss_conf.chandef;
343 compat = cfg80211_chandef_compatible(
344 &sdata->vif.bss_conf.chandef, compat);
350 if (WARN_ON_ONCE(!compat))
353 ieee80211_change_chanctx(local, ctx, compat);
356 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
357 struct ieee80211_chanctx *chanctx)
361 lockdep_assert_held(&local->chanctx_mtx);
362 /* for setting local->radar_detect_enabled */
363 lockdep_assert_held(&local->mtx);
365 radar_enabled = ieee80211_is_radar_required(local);
367 if (radar_enabled == chanctx->conf.radar_enabled)
370 chanctx->conf.radar_enabled = radar_enabled;
371 local->radar_detect_enabled = chanctx->conf.radar_enabled;
373 if (!local->use_chanctx) {
374 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
375 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
378 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
381 static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
382 struct ieee80211_chanctx *ctx)
384 struct ieee80211_local *local = sdata->local;
386 lockdep_assert_held(&local->chanctx_mtx);
389 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
391 sdata->vif.bss_conf.idle = true;
393 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
394 sdata->vif.type != NL80211_IFTYPE_MONITOR)
395 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
397 drv_unassign_vif_chanctx(local, sdata, ctx);
399 if (ctx->refcount > 0) {
400 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
401 ieee80211_recalc_smps_chanctx(local, ctx);
402 ieee80211_recalc_radar_chanctx(local, ctx);
403 ieee80211_recalc_chanctx_min_def(local, ctx);
407 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
409 struct ieee80211_local *local = sdata->local;
410 struct ieee80211_chanctx_conf *conf;
411 struct ieee80211_chanctx *ctx;
413 lockdep_assert_held(&local->chanctx_mtx);
415 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
416 lockdep_is_held(&local->chanctx_mtx));
420 ctx = container_of(conf, struct ieee80211_chanctx, conf);
422 ieee80211_unassign_vif_chanctx(sdata, ctx);
423 if (ctx->refcount == 0)
424 ieee80211_free_chanctx(local, ctx);
427 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
428 struct ieee80211_chanctx *chanctx)
430 struct ieee80211_sub_if_data *sdata;
431 u8 rx_chains_static, rx_chains_dynamic;
433 lockdep_assert_held(&local->chanctx_mtx);
435 rx_chains_static = 1;
436 rx_chains_dynamic = 1;
439 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
440 u8 needed_static, needed_dynamic;
442 if (!ieee80211_sdata_running(sdata))
445 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
449 switch (sdata->vif.type) {
450 case NL80211_IFTYPE_P2P_DEVICE:
452 case NL80211_IFTYPE_STATION:
453 if (!sdata->u.mgd.associated)
456 case NL80211_IFTYPE_AP_VLAN:
458 case NL80211_IFTYPE_AP:
459 case NL80211_IFTYPE_ADHOC:
460 case NL80211_IFTYPE_WDS:
461 case NL80211_IFTYPE_MESH_POINT:
467 switch (sdata->smps_mode) {
469 WARN_ONCE(1, "Invalid SMPS mode %d\n",
472 case IEEE80211_SMPS_OFF:
473 needed_static = sdata->needed_rx_chains;
474 needed_dynamic = sdata->needed_rx_chains;
476 case IEEE80211_SMPS_DYNAMIC:
478 needed_dynamic = sdata->needed_rx_chains;
480 case IEEE80211_SMPS_STATIC:
486 rx_chains_static = max(rx_chains_static, needed_static);
487 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
491 if (!local->use_chanctx) {
492 if (rx_chains_static > 1)
493 local->smps_mode = IEEE80211_SMPS_OFF;
494 else if (rx_chains_dynamic > 1)
495 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
497 local->smps_mode = IEEE80211_SMPS_STATIC;
498 ieee80211_hw_config(local, 0);
501 if (rx_chains_static == chanctx->conf.rx_chains_static &&
502 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
505 chanctx->conf.rx_chains_static = rx_chains_static;
506 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
507 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
510 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
511 const struct cfg80211_chan_def *chandef,
512 enum ieee80211_chanctx_mode mode)
514 struct ieee80211_local *local = sdata->local;
515 struct ieee80211_chanctx *ctx;
518 lockdep_assert_held(&local->mtx);
520 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
522 mutex_lock(&local->chanctx_mtx);
523 __ieee80211_vif_release_channel(sdata);
525 ctx = ieee80211_find_chanctx(local, chandef, mode);
527 ctx = ieee80211_new_chanctx(local, chandef, mode);
533 sdata->vif.bss_conf.chandef = *chandef;
535 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
537 /* if assign fails refcount stays the same */
538 if (ctx->refcount == 0)
539 ieee80211_free_chanctx(local, ctx);
543 ieee80211_recalc_smps_chanctx(local, ctx);
544 ieee80211_recalc_radar_chanctx(local, ctx);
546 mutex_unlock(&local->chanctx_mtx);
550 int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
553 struct ieee80211_local *local = sdata->local;
554 struct ieee80211_chanctx_conf *conf;
555 struct ieee80211_chanctx *ctx;
556 const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
558 u32 chanctx_changed = 0;
560 lockdep_assert_held(&local->mtx);
562 /* should never be called if not performing a channel switch. */
563 if (WARN_ON(!sdata->vif.csa_active))
566 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
567 IEEE80211_CHAN_DISABLED))
570 mutex_lock(&local->chanctx_mtx);
571 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
572 lockdep_is_held(&local->chanctx_mtx));
578 ctx = container_of(conf, struct ieee80211_chanctx, conf);
579 if (ctx->refcount != 1) {
584 if (sdata->vif.bss_conf.chandef.width != chandef->width) {
585 chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
586 *changed |= BSS_CHANGED_BANDWIDTH;
589 sdata->vif.bss_conf.chandef = *chandef;
590 ctx->conf.def = *chandef;
592 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
593 drv_change_chanctx(local, ctx, chanctx_changed);
595 ieee80211_recalc_chanctx_chantype(local, ctx);
596 ieee80211_recalc_smps_chanctx(local, ctx);
597 ieee80211_recalc_radar_chanctx(local, ctx);
598 ieee80211_recalc_chanctx_min_def(local, ctx);
602 mutex_unlock(&local->chanctx_mtx);
606 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
607 const struct cfg80211_chan_def *chandef,
610 struct ieee80211_local *local = sdata->local;
611 struct ieee80211_chanctx_conf *conf;
612 struct ieee80211_chanctx *ctx;
615 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
616 IEEE80211_CHAN_DISABLED))
619 mutex_lock(&local->chanctx_mtx);
620 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
625 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
626 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
631 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
632 lockdep_is_held(&local->chanctx_mtx));
638 ctx = container_of(conf, struct ieee80211_chanctx, conf);
639 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
644 sdata->vif.bss_conf.chandef = *chandef;
646 ieee80211_recalc_chanctx_chantype(local, ctx);
648 *changed |= BSS_CHANGED_BANDWIDTH;
651 mutex_unlock(&local->chanctx_mtx);
655 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
657 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
659 lockdep_assert_held(&sdata->local->mtx);
661 mutex_lock(&sdata->local->chanctx_mtx);
662 __ieee80211_vif_release_channel(sdata);
663 mutex_unlock(&sdata->local->chanctx_mtx);
666 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
668 struct ieee80211_local *local = sdata->local;
669 struct ieee80211_sub_if_data *ap;
670 struct ieee80211_chanctx_conf *conf;
672 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
675 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
677 mutex_lock(&local->chanctx_mtx);
679 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
680 lockdep_is_held(&local->chanctx_mtx));
681 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
682 mutex_unlock(&local->chanctx_mtx);
685 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
688 struct ieee80211_local *local = sdata->local;
689 struct ieee80211_sub_if_data *vlan;
690 struct ieee80211_chanctx_conf *conf;
694 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
697 mutex_lock(&local->chanctx_mtx);
700 * Check that conf exists, even when clearing this function
701 * must be called with the AP's channel context still there
702 * as it would otherwise cause VLANs to have an invalid
703 * channel context pointer for a while, possibly pointing
704 * to a channel context that has already been freed.
706 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
707 lockdep_is_held(&local->chanctx_mtx));
713 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
714 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
716 mutex_unlock(&local->chanctx_mtx);
719 void ieee80211_iter_chan_contexts_atomic(
720 struct ieee80211_hw *hw,
721 void (*iter)(struct ieee80211_hw *hw,
722 struct ieee80211_chanctx_conf *chanctx_conf,
726 struct ieee80211_local *local = hw_to_local(hw);
727 struct ieee80211_chanctx *ctx;
730 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
731 if (ctx->driver_present)
732 iter(hw, &ctx->conf, iter_data);
735 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);