1 // SPDX-License-Identifier: GPL-2.0-only
3 * mac80211 - channel management
4 * Copyright 2020 - 2024 Intel Corporation
7 #include <linux/nl80211.h>
8 #include <linux/export.h>
9 #include <linux/rtnetlink.h>
10 #include <net/cfg80211.h>
11 #include "ieee80211_i.h"
12 #include "driver-ops.h"
15 static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
16 struct ieee80211_chanctx *ctx)
18 struct ieee80211_link_data *link;
21 lockdep_assert_wiphy(local->hw.wiphy);
23 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list)
29 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
30 struct ieee80211_chanctx *ctx)
32 struct ieee80211_link_data *link;
35 lockdep_assert_wiphy(local->hw.wiphy);
37 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list)
43 int ieee80211_chanctx_refcount(struct ieee80211_local *local,
44 struct ieee80211_chanctx *ctx)
46 return ieee80211_chanctx_num_assigned(local, ctx) +
47 ieee80211_chanctx_num_reserved(local, ctx);
50 static int ieee80211_num_chanctx(struct ieee80211_local *local)
52 struct ieee80211_chanctx *ctx;
55 lockdep_assert_wiphy(local->hw.wiphy);
57 list_for_each_entry(ctx, &local->chanctx_list, list)
63 static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
65 lockdep_assert_wiphy(local->hw.wiphy);
67 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
70 static struct ieee80211_chanctx *
71 ieee80211_link_get_chanctx(struct ieee80211_link_data *link)
73 struct ieee80211_local *local __maybe_unused = link->sdata->local;
74 struct ieee80211_chanctx_conf *conf;
76 conf = rcu_dereference_protected(link->conf->chanctx_conf,
77 lockdep_is_held(&local->hw.wiphy->mtx));
81 return container_of(conf, struct ieee80211_chanctx, conf);
84 bool ieee80211_chanreq_identical(const struct ieee80211_chan_req *a,
85 const struct ieee80211_chan_req *b)
87 if (!cfg80211_chandef_identical(&a->oper, &b->oper))
89 if (!a->ap.chan && !b->ap.chan)
91 return cfg80211_chandef_identical(&a->ap, &b->ap);
94 static const struct ieee80211_chan_req *
95 ieee80211_chanreq_compatible(const struct ieee80211_chan_req *a,
96 const struct ieee80211_chan_req *b,
97 struct ieee80211_chan_req *tmp)
99 const struct cfg80211_chan_def *compat;
101 if (a->ap.chan && b->ap.chan &&
102 !cfg80211_chandef_identical(&a->ap, &b->ap))
105 compat = cfg80211_chandef_compatible(&a->oper, &b->oper);
109 /* Note: later code assumes this always fills & returns tmp if compat */
111 tmp->ap = a->ap.chan ? a->ap : b->ap;
115 static const struct ieee80211_chan_req *
116 ieee80211_chanctx_compatible(struct ieee80211_chanctx *ctx,
117 const struct ieee80211_chan_req *req,
118 struct ieee80211_chan_req *tmp)
120 const struct ieee80211_chan_req *ret;
121 struct ieee80211_chan_req tmp2;
123 *tmp = (struct ieee80211_chan_req){
124 .oper = ctx->conf.def,
128 ret = ieee80211_chanreq_compatible(tmp, req, &tmp2);
135 static const struct ieee80211_chan_req *
136 ieee80211_chanctx_reserved_chanreq(struct ieee80211_local *local,
137 struct ieee80211_chanctx *ctx,
138 const struct ieee80211_chan_req *req,
139 struct ieee80211_chan_req *tmp)
141 struct ieee80211_link_data *link;
143 lockdep_assert_wiphy(local->hw.wiphy);
148 list_for_each_entry(link, &ctx->reserved_links, reserved_chanctx_list) {
149 req = ieee80211_chanreq_compatible(&link->reserved, req, tmp);
157 static const struct ieee80211_chan_req *
158 ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
159 struct ieee80211_chanctx *ctx,
160 const struct ieee80211_chan_req *compat,
161 struct ieee80211_chan_req *tmp)
163 struct ieee80211_link_data *link;
164 const struct ieee80211_chan_req *comp_def = compat;
166 lockdep_assert_wiphy(local->hw.wiphy);
168 list_for_each_entry(link, &ctx->assigned_links, assigned_chanctx_list) {
169 struct ieee80211_bss_conf *link_conf = link->conf;
171 if (link->reserved_chanctx)
174 comp_def = ieee80211_chanreq_compatible(&link_conf->chanreq,
184 ieee80211_chanctx_can_reserve(struct ieee80211_local *local,
185 struct ieee80211_chanctx *ctx,
186 const struct ieee80211_chan_req *req)
188 struct ieee80211_chan_req tmp;
190 lockdep_assert_wiphy(local->hw.wiphy);
192 if (!ieee80211_chanctx_reserved_chanreq(local, ctx, req, &tmp))
195 if (!ieee80211_chanctx_non_reserved_chandef(local, ctx, req, &tmp))
198 if (!list_empty(&ctx->reserved_links) &&
199 ieee80211_chanctx_reserved_chanreq(local, ctx, req, &tmp))
205 static struct ieee80211_chanctx *
206 ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
207 const struct ieee80211_chan_req *chanreq,
208 enum ieee80211_chanctx_mode mode)
210 struct ieee80211_chanctx *ctx;
212 lockdep_assert_wiphy(local->hw.wiphy);
214 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
217 list_for_each_entry(ctx, &local->chanctx_list, list) {
218 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
221 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
224 if (!ieee80211_chanctx_can_reserve(local, ctx, chanreq))
233 static enum nl80211_chan_width ieee80211_get_sta_bw(struct sta_info *sta,
234 unsigned int link_id)
236 enum ieee80211_sta_rx_bandwidth width;
237 struct link_sta_info *link_sta;
239 link_sta = wiphy_dereference(sta->local->hw.wiphy, sta->link[link_id]);
241 /* no effect if this STA has no presence on this link */
243 return NL80211_CHAN_WIDTH_20_NOHT;
245 width = ieee80211_sta_cap_rx_bw(link_sta);
248 case IEEE80211_STA_RX_BW_20:
249 if (link_sta->pub->ht_cap.ht_supported)
250 return NL80211_CHAN_WIDTH_20;
252 return NL80211_CHAN_WIDTH_20_NOHT;
253 case IEEE80211_STA_RX_BW_40:
254 return NL80211_CHAN_WIDTH_40;
255 case IEEE80211_STA_RX_BW_80:
256 return NL80211_CHAN_WIDTH_80;
257 case IEEE80211_STA_RX_BW_160:
259 * This applied for both 160 and 80+80. since we use
260 * the returned value to consider degradation of
261 * ctx->conf.min_def, we have to make sure to take
262 * the bigger one (NL80211_CHAN_WIDTH_160).
263 * Otherwise we might try degrading even when not
264 * needed, as the max required sta_bw returned (80+80)
265 * might be smaller than the configured bw (160).
267 return NL80211_CHAN_WIDTH_160;
268 case IEEE80211_STA_RX_BW_320:
269 return NL80211_CHAN_WIDTH_320;
272 return NL80211_CHAN_WIDTH_20;
276 static enum nl80211_chan_width
277 ieee80211_get_max_required_bw(struct ieee80211_link_data *link)
279 struct ieee80211_sub_if_data *sdata = link->sdata;
280 unsigned int link_id = link->link_id;
281 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
282 struct sta_info *sta;
284 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
285 if (sdata != sta->sdata &&
286 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
289 max_bw = max(max_bw, ieee80211_get_sta_bw(sta, link_id));
295 static enum nl80211_chan_width
296 ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
297 struct ieee80211_chanctx *ctx,
298 struct ieee80211_link_data *rsvd_for)
300 struct ieee80211_sub_if_data *sdata;
301 struct ieee80211_link_data *link;
302 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
304 for_each_sdata_link(local, link) {
305 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
307 if (link != rsvd_for &&
308 rcu_access_pointer(link->conf->chanctx_conf) != &ctx->conf)
311 switch (link->sdata->vif.type) {
312 case NL80211_IFTYPE_AP:
313 case NL80211_IFTYPE_AP_VLAN:
314 width = ieee80211_get_max_required_bw(link);
316 case NL80211_IFTYPE_STATION:
318 * The ap's sta->bandwidth is not set yet at this
319 * point, so take the width from the chandef, but
320 * account also for TDLS peers
322 width = max(link->conf->chanreq.oper.width,
323 ieee80211_get_max_required_bw(link));
325 case NL80211_IFTYPE_P2P_DEVICE:
326 case NL80211_IFTYPE_NAN:
328 case NL80211_IFTYPE_ADHOC:
329 case NL80211_IFTYPE_MESH_POINT:
330 case NL80211_IFTYPE_OCB:
331 width = link->conf->chanreq.oper.width;
333 case NL80211_IFTYPE_WDS:
334 case NL80211_IFTYPE_UNSPECIFIED:
335 case NUM_NL80211_IFTYPES:
336 case NL80211_IFTYPE_MONITOR:
337 case NL80211_IFTYPE_P2P_CLIENT:
338 case NL80211_IFTYPE_P2P_GO:
342 max_bw = max(max_bw, width);
345 /* use the configured bandwidth in case of monitor interface */
346 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
348 rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == &ctx->conf)
349 max_bw = max(max_bw, ctx->conf.def.width);
355 * recalc the min required chan width of the channel context, which is
356 * the max of min required widths of all the interfaces bound to this
360 _ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
361 struct ieee80211_chanctx *ctx,
362 struct ieee80211_link_data *rsvd_for)
364 enum nl80211_chan_width max_bw;
365 struct cfg80211_chan_def min_def;
367 lockdep_assert_wiphy(local->hw.wiphy);
369 /* don't optimize non-20MHz based and radar_enabled confs */
370 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
371 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
372 ctx->conf.def.width == NL80211_CHAN_WIDTH_1 ||
373 ctx->conf.def.width == NL80211_CHAN_WIDTH_2 ||
374 ctx->conf.def.width == NL80211_CHAN_WIDTH_4 ||
375 ctx->conf.def.width == NL80211_CHAN_WIDTH_8 ||
376 ctx->conf.def.width == NL80211_CHAN_WIDTH_16 ||
377 ctx->conf.radar_enabled) {
378 ctx->conf.min_def = ctx->conf.def;
382 max_bw = ieee80211_get_chanctx_max_required_bw(local, ctx, rsvd_for);
384 /* downgrade chandef up to max_bw */
385 min_def = ctx->conf.def;
386 while (min_def.width > max_bw)
387 ieee80211_chandef_downgrade(&min_def, NULL);
389 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
392 ctx->conf.min_def = min_def;
393 if (!ctx->driver_present)
396 return IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
399 /* calling this function is assuming that station vif is updated to
400 * lates changes by calling ieee80211_link_update_chanreq
402 static void ieee80211_chan_bw_change(struct ieee80211_local *local,
403 struct ieee80211_chanctx *ctx,
406 struct sta_info *sta;
407 struct ieee80211_supported_band *sband =
408 local->hw.wiphy->bands[ctx->conf.def.chan->band];
411 list_for_each_entry_rcu(sta, &local->sta_list,
413 struct ieee80211_sub_if_data *sdata = sta->sdata;
414 enum ieee80211_sta_rx_bandwidth new_sta_bw;
415 unsigned int link_id;
417 if (!ieee80211_sdata_running(sta->sdata))
420 for (link_id = 0; link_id < ARRAY_SIZE(sta->sdata->link); link_id++) {
421 struct ieee80211_bss_conf *link_conf =
422 rcu_dereference(sdata->vif.link_conf[link_id]);
423 struct link_sta_info *link_sta;
428 if (rcu_access_pointer(link_conf->chanctx_conf) != &ctx->conf)
431 link_sta = rcu_dereference(sta->link[link_id]);
435 new_sta_bw = ieee80211_sta_cur_vht_bw(link_sta);
438 if (new_sta_bw == link_sta->pub->bandwidth)
441 /* vif changed to narrow BW and narrow BW for station wasn't
442 * requested or vise versa */
443 if ((new_sta_bw < link_sta->pub->bandwidth) == !narrowed)
446 link_sta->pub->bandwidth = new_sta_bw;
447 rate_control_rate_update(local, sband, sta, link_id,
448 IEEE80211_RC_BW_CHANGED);
455 * recalc the min required chan width of the channel context, which is
456 * the max of min required widths of all the interfaces bound to this
459 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
460 struct ieee80211_chanctx *ctx,
461 struct ieee80211_link_data *rsvd_for)
463 u32 changed = _ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for);
468 /* check is BW narrowed */
469 ieee80211_chan_bw_change(local, ctx, true);
471 drv_change_chanctx(local, ctx, changed);
473 /* check is BW wider */
474 ieee80211_chan_bw_change(local, ctx, false);
477 static void _ieee80211_change_chanctx(struct ieee80211_local *local,
478 struct ieee80211_chanctx *ctx,
479 struct ieee80211_chanctx *old_ctx,
480 const struct ieee80211_chan_req *chanreq,
481 struct ieee80211_link_data *rsvd_for)
483 const struct cfg80211_chan_def *chandef = &chanreq->oper;
484 struct ieee80211_chan_req ctx_req = {
485 .oper = ctx->conf.def,
490 /* expected to handle only 20/40/80/160/320 channel widths */
491 switch (chandef->width) {
492 case NL80211_CHAN_WIDTH_20_NOHT:
493 case NL80211_CHAN_WIDTH_20:
494 case NL80211_CHAN_WIDTH_40:
495 case NL80211_CHAN_WIDTH_80:
496 case NL80211_CHAN_WIDTH_80P80:
497 case NL80211_CHAN_WIDTH_160:
498 case NL80211_CHAN_WIDTH_320:
504 /* Check maybe BW narrowed - we do this _before_ calling recalc_chanctx_min_def
505 * due to maybe not returning from it, e.g in case new context was added
506 * first time with all parameters up to date.
508 ieee80211_chan_bw_change(local, old_ctx, true);
510 if (ieee80211_chanreq_identical(&ctx_req, chanreq)) {
511 ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for);
515 WARN_ON(ieee80211_chanctx_refcount(local, ctx) > 1 &&
516 !cfg80211_chandef_compatible(&ctx->conf.def, &chanreq->oper));
518 ieee80211_remove_wbrf(local, &ctx->conf.def);
520 if (!cfg80211_chandef_identical(&ctx->conf.def, &chanreq->oper)) {
521 if (ctx->conf.def.width != chanreq->oper.width)
522 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
523 if (ctx->conf.def.punctured != chanreq->oper.punctured)
524 changed |= IEEE80211_CHANCTX_CHANGE_PUNCTURING;
526 if (!cfg80211_chandef_identical(&ctx->conf.ap, &chanreq->ap))
527 changed |= IEEE80211_CHANCTX_CHANGE_AP;
528 ctx->conf.def = *chandef;
529 ctx->conf.ap = chanreq->ap;
531 /* check if min chanctx also changed */
532 changed |= _ieee80211_recalc_chanctx_min_def(local, ctx, rsvd_for);
534 ieee80211_add_wbrf(local, &ctx->conf.def);
536 drv_change_chanctx(local, ctx, changed);
538 /* check if BW is wider */
539 ieee80211_chan_bw_change(local, old_ctx, false);
542 static void ieee80211_change_chanctx(struct ieee80211_local *local,
543 struct ieee80211_chanctx *ctx,
544 struct ieee80211_chanctx *old_ctx,
545 const struct ieee80211_chan_req *chanreq)
547 _ieee80211_change_chanctx(local, ctx, old_ctx, chanreq, NULL);
550 static struct ieee80211_chanctx *
551 ieee80211_find_chanctx(struct ieee80211_local *local,
552 const struct ieee80211_chan_req *chanreq,
553 enum ieee80211_chanctx_mode mode)
555 struct ieee80211_chan_req tmp;
556 struct ieee80211_chanctx *ctx;
558 lockdep_assert_wiphy(local->hw.wiphy);
560 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
563 list_for_each_entry(ctx, &local->chanctx_list, list) {
564 const struct ieee80211_chan_req *compat;
566 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
569 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
572 compat = ieee80211_chanctx_compatible(ctx, chanreq, &tmp);
576 compat = ieee80211_chanctx_reserved_chanreq(local, ctx,
581 ieee80211_change_chanctx(local, ctx, ctx, compat);
589 bool ieee80211_is_radar_required(struct ieee80211_local *local)
591 struct ieee80211_link_data *link;
593 lockdep_assert_wiphy(local->hw.wiphy);
595 for_each_sdata_link(local, link) {
596 if (link->radar_required)
604 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
605 struct ieee80211_chanctx *ctx)
607 struct ieee80211_chanctx_conf *conf = &ctx->conf;
608 struct ieee80211_link_data *link;
610 lockdep_assert_wiphy(local->hw.wiphy);
612 for_each_sdata_link(local, link) {
613 if (rcu_access_pointer(link->conf->chanctx_conf) != conf)
615 if (!link->radar_required)
623 static struct ieee80211_chanctx *
624 ieee80211_alloc_chanctx(struct ieee80211_local *local,
625 const struct ieee80211_chan_req *chanreq,
626 enum ieee80211_chanctx_mode mode)
628 struct ieee80211_chanctx *ctx;
630 lockdep_assert_wiphy(local->hw.wiphy);
632 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
636 INIT_LIST_HEAD(&ctx->assigned_links);
637 INIT_LIST_HEAD(&ctx->reserved_links);
638 ctx->conf.def = chanreq->oper;
639 ctx->conf.ap = chanreq->ap;
640 ctx->conf.rx_chains_static = 1;
641 ctx->conf.rx_chains_dynamic = 1;
643 ctx->conf.radar_enabled = false;
644 _ieee80211_recalc_chanctx_min_def(local, ctx, NULL);
649 static int ieee80211_add_chanctx(struct ieee80211_local *local,
650 struct ieee80211_chanctx *ctx)
655 lockdep_assert_wiphy(local->hw.wiphy);
657 ieee80211_add_wbrf(local, &ctx->conf.def);
659 /* turn idle off *before* setting channel -- some drivers need that */
660 changed = ieee80211_idle_off(local);
662 ieee80211_hw_config(local, changed);
664 err = drv_add_chanctx(local, ctx);
666 ieee80211_recalc_idle(local);
673 static struct ieee80211_chanctx *
674 ieee80211_new_chanctx(struct ieee80211_local *local,
675 const struct ieee80211_chan_req *chanreq,
676 enum ieee80211_chanctx_mode mode)
678 struct ieee80211_chanctx *ctx;
681 lockdep_assert_wiphy(local->hw.wiphy);
683 ctx = ieee80211_alloc_chanctx(local, chanreq, mode);
685 return ERR_PTR(-ENOMEM);
687 err = ieee80211_add_chanctx(local, ctx);
693 list_add_rcu(&ctx->list, &local->chanctx_list);
697 static void ieee80211_del_chanctx(struct ieee80211_local *local,
698 struct ieee80211_chanctx *ctx)
700 lockdep_assert_wiphy(local->hw.wiphy);
702 drv_remove_chanctx(local, ctx);
704 ieee80211_recalc_idle(local);
706 ieee80211_remove_wbrf(local, &ctx->conf.def);
709 static void ieee80211_free_chanctx(struct ieee80211_local *local,
710 struct ieee80211_chanctx *ctx)
712 lockdep_assert_wiphy(local->hw.wiphy);
714 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
716 list_del_rcu(&ctx->list);
717 ieee80211_del_chanctx(local, ctx);
718 kfree_rcu(ctx, rcu_head);
721 void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
722 struct ieee80211_chanctx *ctx)
724 struct ieee80211_chanctx_conf *conf = &ctx->conf;
725 const struct ieee80211_chan_req *compat = NULL;
726 struct ieee80211_link_data *link;
727 struct ieee80211_chan_req tmp;
728 struct sta_info *sta;
730 lockdep_assert_wiphy(local->hw.wiphy);
732 for_each_sdata_link(local, link) {
733 struct ieee80211_bss_conf *link_conf;
735 if (link->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
738 link_conf = link->conf;
740 if (rcu_access_pointer(link_conf->chanctx_conf) != conf)
744 compat = &link_conf->chanreq;
746 compat = ieee80211_chanreq_compatible(&link_conf->chanreq,
748 if (WARN_ON_ONCE(!compat))
752 if (WARN_ON_ONCE(!compat))
755 /* TDLS peers can sometimes affect the chandef width */
756 list_for_each_entry(sta, &local->sta_list, list) {
757 struct ieee80211_chan_req tdls_chanreq = {};
758 if (!sta->uploaded ||
759 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
760 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
761 !sta->tdls_chandef.chan)
764 tdls_chanreq.oper = sta->tdls_chandef;
766 /* note this always fills and returns &tmp if compat */
767 compat = ieee80211_chanreq_compatible(&tdls_chanreq,
769 if (WARN_ON_ONCE(!compat))
773 ieee80211_change_chanctx(local, ctx, ctx, compat);
776 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
777 struct ieee80211_chanctx *chanctx)
781 lockdep_assert_wiphy(local->hw.wiphy);
783 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
785 if (radar_enabled == chanctx->conf.radar_enabled)
788 chanctx->conf.radar_enabled = radar_enabled;
790 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
793 static int ieee80211_assign_link_chanctx(struct ieee80211_link_data *link,
794 struct ieee80211_chanctx *new_ctx)
796 struct ieee80211_sub_if_data *sdata = link->sdata;
797 struct ieee80211_local *local = sdata->local;
798 struct ieee80211_chanctx_conf *conf;
799 struct ieee80211_chanctx *curr_ctx = NULL;
803 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
806 conf = rcu_dereference_protected(link->conf->chanctx_conf,
807 lockdep_is_held(&local->hw.wiphy->mtx));
810 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
812 drv_unassign_vif_chanctx(local, sdata, link->conf, curr_ctx);
814 list_del(&link->assigned_chanctx_list);
818 /* recalc considering the link we'll use it for now */
819 ieee80211_recalc_chanctx_min_def(local, new_ctx, link);
821 ret = drv_assign_vif_chanctx(local, sdata, link->conf, new_ctx);
825 conf = &new_ctx->conf;
826 list_add(&link->assigned_chanctx_list,
827 &new_ctx->assigned_links);
831 rcu_assign_pointer(link->conf->chanctx_conf, conf);
833 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
834 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
835 ieee80211_recalc_smps_chanctx(local, curr_ctx);
836 ieee80211_recalc_radar_chanctx(local, curr_ctx);
837 ieee80211_recalc_chanctx_min_def(local, curr_ctx, NULL);
840 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
841 ieee80211_recalc_txpower(sdata, false);
842 ieee80211_recalc_chanctx_min_def(local, new_ctx, NULL);
848 struct ieee80211_link_data *tmp;
851 for_each_sdata_link(local, tmp) {
852 if (rcu_access_pointer(tmp->conf->chanctx_conf)) {
859 if (new_idle != sdata->vif.cfg.idle) {
860 sdata->vif.cfg.idle = new_idle;
862 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
863 sdata->vif.type != NL80211_IFTYPE_MONITOR)
864 ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_IDLE);
867 ieee80211_check_fast_xmit_iface(sdata);
872 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
873 struct ieee80211_chanctx *chanctx)
875 struct ieee80211_sub_if_data *sdata;
876 u8 rx_chains_static, rx_chains_dynamic;
877 struct ieee80211_link_data *link;
879 lockdep_assert_wiphy(local->hw.wiphy);
881 rx_chains_static = 1;
882 rx_chains_dynamic = 1;
884 for_each_sdata_link(local, link) {
885 u8 needed_static, needed_dynamic;
887 switch (link->sdata->vif.type) {
888 case NL80211_IFTYPE_STATION:
889 if (!link->sdata->u.mgd.associated)
892 case NL80211_IFTYPE_AP:
893 case NL80211_IFTYPE_ADHOC:
894 case NL80211_IFTYPE_MESH_POINT:
895 case NL80211_IFTYPE_OCB:
901 if (rcu_access_pointer(link->conf->chanctx_conf) != &chanctx->conf)
904 switch (link->smps_mode) {
906 WARN_ONCE(1, "Invalid SMPS mode %d\n",
909 case IEEE80211_SMPS_OFF:
910 needed_static = link->needed_rx_chains;
911 needed_dynamic = link->needed_rx_chains;
913 case IEEE80211_SMPS_DYNAMIC:
915 needed_dynamic = link->needed_rx_chains;
917 case IEEE80211_SMPS_STATIC:
923 rx_chains_static = max(rx_chains_static, needed_static);
924 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
927 /* Disable SMPS for the monitor interface */
928 sdata = wiphy_dereference(local->hw.wiphy, local->monitor_sdata);
930 rcu_access_pointer(sdata->vif.bss_conf.chanctx_conf) == &chanctx->conf)
931 rx_chains_dynamic = rx_chains_static = local->rx_chains;
933 if (rx_chains_static == chanctx->conf.rx_chains_static &&
934 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
937 chanctx->conf.rx_chains_static = rx_chains_static;
938 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
939 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
943 __ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link,
946 struct ieee80211_sub_if_data *sdata = link->sdata;
947 unsigned int link_id = link->link_id;
948 struct ieee80211_bss_conf *link_conf = link->conf;
949 struct ieee80211_local *local __maybe_unused = sdata->local;
950 struct ieee80211_sub_if_data *vlan;
951 struct ieee80211_chanctx_conf *conf;
953 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
956 lockdep_assert_wiphy(local->hw.wiphy);
958 /* Check that conf exists, even when clearing this function
959 * must be called with the AP's channel context still there
960 * as it would otherwise cause VLANs to have an invalid
961 * channel context pointer for a while, possibly pointing
962 * to a channel context that has already been freed.
964 conf = rcu_dereference_protected(link_conf->chanctx_conf,
965 lockdep_is_held(&local->hw.wiphy->mtx));
971 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
972 struct ieee80211_bss_conf *vlan_conf;
974 vlan_conf = wiphy_dereference(local->hw.wiphy,
975 vlan->vif.link_conf[link_id]);
976 if (WARN_ON(!vlan_conf))
979 rcu_assign_pointer(vlan_conf->chanctx_conf, conf);
983 void ieee80211_link_copy_chanctx_to_vlans(struct ieee80211_link_data *link,
986 struct ieee80211_local *local = link->sdata->local;
988 lockdep_assert_wiphy(local->hw.wiphy);
990 __ieee80211_link_copy_chanctx_to_vlans(link, clear);
993 int ieee80211_link_unreserve_chanctx(struct ieee80211_link_data *link)
995 struct ieee80211_sub_if_data *sdata = link->sdata;
996 struct ieee80211_chanctx *ctx = link->reserved_chanctx;
998 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1003 list_del(&link->reserved_chanctx_list);
1004 link->reserved_chanctx = NULL;
1006 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
1007 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1008 if (WARN_ON(!ctx->replace_ctx))
1011 WARN_ON(ctx->replace_ctx->replace_state !=
1012 IEEE80211_CHANCTX_WILL_BE_REPLACED);
1013 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
1015 ctx->replace_ctx->replace_ctx = NULL;
1016 ctx->replace_ctx->replace_state =
1017 IEEE80211_CHANCTX_REPLACE_NONE;
1019 list_del_rcu(&ctx->list);
1020 kfree_rcu(ctx, rcu_head);
1022 ieee80211_free_chanctx(sdata->local, ctx);
1029 int ieee80211_link_reserve_chanctx(struct ieee80211_link_data *link,
1030 const struct ieee80211_chan_req *chanreq,
1031 enum ieee80211_chanctx_mode mode,
1032 bool radar_required)
1034 struct ieee80211_sub_if_data *sdata = link->sdata;
1035 struct ieee80211_local *local = sdata->local;
1036 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
1038 lockdep_assert_wiphy(local->hw.wiphy);
1040 curr_ctx = ieee80211_link_get_chanctx(link);
1041 if (curr_ctx && !local->ops->switch_vif_chanctx)
1044 new_ctx = ieee80211_find_reservation_chanctx(local, chanreq, mode);
1046 if (ieee80211_can_create_new_chanctx(local)) {
1047 new_ctx = ieee80211_new_chanctx(local, chanreq, mode);
1048 if (IS_ERR(new_ctx))
1049 return PTR_ERR(new_ctx);
1052 (curr_ctx->replace_state ==
1053 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1054 !list_empty(&curr_ctx->reserved_links)) {
1056 * Another link already requested this context
1057 * for a reservation. Find another one hoping
1058 * all links assigned to it will also switch
1061 * TODO: This needs a little more work as some
1062 * cases (more than 2 chanctx capable devices)
1063 * may fail which could otherwise succeed
1064 * provided some channel context juggling was
1067 * Consider ctx1..3, link1..6, each ctx has 2
1068 * links. link1 and link2 from ctx1 request new
1069 * different chandefs starting 2 in-place
1070 * reserations with ctx4 and ctx5 replacing
1071 * ctx1 and ctx2 respectively. Next link5 and
1072 * link6 from ctx3 reserve ctx4. If link3 and
1073 * link4 remain on ctx2 as they are then this
1074 * fails unless `replace_ctx` from ctx5 is
1075 * replaced with ctx3.
1077 list_for_each_entry(ctx, &local->chanctx_list,
1079 if (ctx->replace_state !=
1080 IEEE80211_CHANCTX_REPLACE_NONE)
1083 if (!list_empty(&ctx->reserved_links))
1092 * If that's true then all available contexts already
1093 * have reservations and cannot be used.
1096 (curr_ctx->replace_state ==
1097 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1098 !list_empty(&curr_ctx->reserved_links))
1101 new_ctx = ieee80211_alloc_chanctx(local, chanreq, mode);
1105 new_ctx->replace_ctx = curr_ctx;
1106 new_ctx->replace_state =
1107 IEEE80211_CHANCTX_REPLACES_OTHER;
1109 curr_ctx->replace_ctx = new_ctx;
1110 curr_ctx->replace_state =
1111 IEEE80211_CHANCTX_WILL_BE_REPLACED;
1113 list_add_rcu(&new_ctx->list, &local->chanctx_list);
1117 list_add(&link->reserved_chanctx_list, &new_ctx->reserved_links);
1118 link->reserved_chanctx = new_ctx;
1119 link->reserved = *chanreq;
1120 link->reserved_radar_required = radar_required;
1121 link->reserved_ready = false;
1127 ieee80211_link_chanctx_reservation_complete(struct ieee80211_link_data *link)
1129 struct ieee80211_sub_if_data *sdata = link->sdata;
1131 switch (sdata->vif.type) {
1132 case NL80211_IFTYPE_ADHOC:
1133 case NL80211_IFTYPE_AP:
1134 case NL80211_IFTYPE_MESH_POINT:
1135 case NL80211_IFTYPE_OCB:
1136 wiphy_work_queue(sdata->local->hw.wiphy,
1137 &link->csa_finalize_work);
1139 case NL80211_IFTYPE_STATION:
1140 wiphy_delayed_work_queue(sdata->local->hw.wiphy,
1141 &link->u.mgd.chswitch_work, 0);
1143 case NL80211_IFTYPE_UNSPECIFIED:
1144 case NL80211_IFTYPE_AP_VLAN:
1145 case NL80211_IFTYPE_WDS:
1146 case NL80211_IFTYPE_MONITOR:
1147 case NL80211_IFTYPE_P2P_CLIENT:
1148 case NL80211_IFTYPE_P2P_GO:
1149 case NL80211_IFTYPE_P2P_DEVICE:
1150 case NL80211_IFTYPE_NAN:
1151 case NUM_NL80211_IFTYPES:
1158 ieee80211_link_update_chanreq(struct ieee80211_link_data *link,
1159 const struct ieee80211_chan_req *chanreq)
1161 struct ieee80211_sub_if_data *sdata = link->sdata;
1162 unsigned int link_id = link->link_id;
1163 struct ieee80211_sub_if_data *vlan;
1165 link->conf->chanreq = *chanreq;
1167 if (sdata->vif.type != NL80211_IFTYPE_AP)
1170 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) {
1171 struct ieee80211_bss_conf *vlan_conf;
1173 vlan_conf = wiphy_dereference(sdata->local->hw.wiphy,
1174 vlan->vif.link_conf[link_id]);
1175 if (WARN_ON(!vlan_conf))
1178 vlan_conf->chanreq = *chanreq;
1183 ieee80211_link_use_reserved_reassign(struct ieee80211_link_data *link)
1185 struct ieee80211_sub_if_data *sdata = link->sdata;
1186 struct ieee80211_bss_conf *link_conf = link->conf;
1187 struct ieee80211_local *local = sdata->local;
1188 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1189 struct ieee80211_chanctx *old_ctx, *new_ctx;
1190 const struct ieee80211_chan_req *chanreq;
1191 struct ieee80211_chan_req tmp;
1195 lockdep_assert_wiphy(local->hw.wiphy);
1197 new_ctx = link->reserved_chanctx;
1198 old_ctx = ieee80211_link_get_chanctx(link);
1200 if (WARN_ON(!link->reserved_ready))
1203 if (WARN_ON(!new_ctx))
1206 if (WARN_ON(!old_ctx))
1209 if (WARN_ON(new_ctx->replace_state ==
1210 IEEE80211_CHANCTX_REPLACES_OTHER))
1213 chanreq = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1216 if (WARN_ON(!chanreq))
1219 if (link_conf->chanreq.oper.width != link->reserved.oper.width)
1220 changed = BSS_CHANGED_BANDWIDTH;
1222 ieee80211_link_update_chanreq(link, &link->reserved);
1224 _ieee80211_change_chanctx(local, new_ctx, old_ctx, chanreq, link);
1226 vif_chsw[0].vif = &sdata->vif;
1227 vif_chsw[0].old_ctx = &old_ctx->conf;
1228 vif_chsw[0].new_ctx = &new_ctx->conf;
1229 vif_chsw[0].link_conf = link->conf;
1231 list_del(&link->reserved_chanctx_list);
1232 link->reserved_chanctx = NULL;
1234 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1235 CHANCTX_SWMODE_REASSIGN_VIF);
1237 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1238 ieee80211_free_chanctx(local, new_ctx);
1243 list_move(&link->assigned_chanctx_list, &new_ctx->assigned_links);
1244 rcu_assign_pointer(link_conf->chanctx_conf, &new_ctx->conf);
1246 if (sdata->vif.type == NL80211_IFTYPE_AP)
1247 __ieee80211_link_copy_chanctx_to_vlans(link, false);
1249 ieee80211_check_fast_xmit_iface(sdata);
1251 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1252 ieee80211_free_chanctx(local, old_ctx);
1254 ieee80211_recalc_chanctx_min_def(local, new_ctx, NULL);
1255 ieee80211_recalc_smps_chanctx(local, new_ctx);
1256 ieee80211_recalc_radar_chanctx(local, new_ctx);
1259 ieee80211_link_info_change_notify(sdata, link, changed);
1262 ieee80211_link_chanctx_reservation_complete(link);
1267 ieee80211_link_use_reserved_assign(struct ieee80211_link_data *link)
1269 struct ieee80211_sub_if_data *sdata = link->sdata;
1270 struct ieee80211_local *local = sdata->local;
1271 struct ieee80211_chanctx *old_ctx, *new_ctx;
1272 const struct ieee80211_chan_req *chanreq;
1273 struct ieee80211_chan_req tmp;
1276 old_ctx = ieee80211_link_get_chanctx(link);
1277 new_ctx = link->reserved_chanctx;
1279 if (WARN_ON(!link->reserved_ready))
1282 if (WARN_ON(old_ctx))
1285 if (WARN_ON(!new_ctx))
1288 if (WARN_ON(new_ctx->replace_state ==
1289 IEEE80211_CHANCTX_REPLACES_OTHER))
1292 chanreq = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1295 if (WARN_ON(!chanreq))
1298 ieee80211_change_chanctx(local, new_ctx, new_ctx, chanreq);
1300 list_del(&link->reserved_chanctx_list);
1301 link->reserved_chanctx = NULL;
1303 err = ieee80211_assign_link_chanctx(link, new_ctx);
1305 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1306 ieee80211_free_chanctx(local, new_ctx);
1312 ieee80211_link_chanctx_reservation_complete(link);
1317 ieee80211_link_has_in_place_reservation(struct ieee80211_link_data *link)
1319 struct ieee80211_sub_if_data *sdata = link->sdata;
1320 struct ieee80211_chanctx *old_ctx, *new_ctx;
1322 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1324 new_ctx = link->reserved_chanctx;
1325 old_ctx = ieee80211_link_get_chanctx(link);
1330 if (WARN_ON(!new_ctx))
1333 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1336 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1342 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1345 struct ieee80211_vif_chanctx_switch *vif_chsw;
1346 struct ieee80211_link_data *link;
1347 struct ieee80211_chanctx *ctx, *old_ctx;
1350 lockdep_assert_wiphy(local->hw.wiphy);
1352 vif_chsw = kcalloc(n_vifs, sizeof(vif_chsw[0]), GFP_KERNEL);
1357 list_for_each_entry(ctx, &local->chanctx_list, list) {
1358 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1361 if (WARN_ON(!ctx->replace_ctx)) {
1366 list_for_each_entry(link, &ctx->reserved_links,
1367 reserved_chanctx_list) {
1368 if (!ieee80211_link_has_in_place_reservation(link))
1371 old_ctx = ieee80211_link_get_chanctx(link);
1372 vif_chsw[i].vif = &link->sdata->vif;
1373 vif_chsw[i].old_ctx = &old_ctx->conf;
1374 vif_chsw[i].new_ctx = &ctx->conf;
1375 vif_chsw[i].link_conf = link->conf;
1381 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1382 CHANCTX_SWMODE_SWAP_CONTEXTS);
1389 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1391 struct ieee80211_chanctx *ctx;
1394 lockdep_assert_wiphy(local->hw.wiphy);
1396 list_for_each_entry(ctx, &local->chanctx_list, list) {
1397 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1400 if (!list_empty(&ctx->replace_ctx->assigned_links))
1403 ieee80211_del_chanctx(local, ctx->replace_ctx);
1404 err = ieee80211_add_chanctx(local, ctx);
1412 WARN_ON(ieee80211_add_chanctx(local, ctx));
1413 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1414 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1417 if (!list_empty(&ctx->replace_ctx->assigned_links))
1420 ieee80211_del_chanctx(local, ctx);
1421 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1427 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1429 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1430 int err, n_assigned, n_reserved, n_ready;
1431 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1433 lockdep_assert_wiphy(local->hw.wiphy);
1436 * If there are 2 independent pairs of channel contexts performing
1437 * cross-switch of their vifs this code will still wait until both are
1438 * ready even though it could be possible to switch one before the
1441 * For practical reasons and code simplicity just do a single huge
1446 * Verify if the reservation is still feasible.
1447 * - if it's not then disconnect
1448 * - if it is but not all vifs necessary are ready then defer
1451 list_for_each_entry(ctx, &local->chanctx_list, list) {
1452 struct ieee80211_link_data *link;
1454 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1457 if (WARN_ON(!ctx->replace_ctx)) {
1468 list_for_each_entry(link, &ctx->replace_ctx->assigned_links,
1469 assigned_chanctx_list) {
1471 if (link->reserved_chanctx) {
1473 if (link->reserved_ready)
1478 if (n_assigned != n_reserved) {
1479 if (n_ready == n_reserved) {
1480 wiphy_info(local->hw.wiphy,
1481 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1489 ctx->conf.radar_enabled = false;
1490 list_for_each_entry(link, &ctx->reserved_links,
1491 reserved_chanctx_list) {
1492 if (ieee80211_link_has_in_place_reservation(link) &&
1493 !link->reserved_ready)
1496 old_ctx = ieee80211_link_get_chanctx(link);
1498 if (old_ctx->replace_state ==
1499 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1507 if (link->reserved_radar_required)
1508 ctx->conf.radar_enabled = true;
1512 if (WARN_ON(n_ctx == 0) ||
1513 WARN_ON(n_vifs_switch == 0 &&
1514 n_vifs_assign == 0 &&
1515 n_vifs_ctxless == 0)) {
1521 * All necessary vifs are ready. Perform the switch now depending on
1522 * reservations and driver capabilities.
1525 if (n_vifs_switch > 0) {
1526 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1531 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1532 err = ieee80211_chsw_switch_ctxs(local);
1538 * Update all structures, values and pointers to point to new channel
1541 list_for_each_entry(ctx, &local->chanctx_list, list) {
1542 struct ieee80211_link_data *link, *link_tmp;
1544 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1547 if (WARN_ON(!ctx->replace_ctx)) {
1552 list_for_each_entry(link, &ctx->reserved_links,
1553 reserved_chanctx_list) {
1554 struct ieee80211_sub_if_data *sdata = link->sdata;
1555 struct ieee80211_bss_conf *link_conf = link->conf;
1558 if (!ieee80211_link_has_in_place_reservation(link))
1561 rcu_assign_pointer(link_conf->chanctx_conf,
1564 if (sdata->vif.type == NL80211_IFTYPE_AP)
1565 __ieee80211_link_copy_chanctx_to_vlans(link,
1568 ieee80211_check_fast_xmit_iface(sdata);
1570 link->radar_required = link->reserved_radar_required;
1572 if (link_conf->chanreq.oper.width != link->reserved.oper.width)
1573 changed = BSS_CHANGED_BANDWIDTH;
1575 ieee80211_link_update_chanreq(link, &link->reserved);
1577 ieee80211_link_info_change_notify(sdata,
1581 ieee80211_recalc_txpower(sdata, false);
1584 ieee80211_recalc_chanctx_chantype(local, ctx);
1585 ieee80211_recalc_smps_chanctx(local, ctx);
1586 ieee80211_recalc_radar_chanctx(local, ctx);
1587 ieee80211_recalc_chanctx_min_def(local, ctx, NULL);
1589 list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
1590 reserved_chanctx_list) {
1591 if (ieee80211_link_get_chanctx(link) != ctx)
1594 list_del(&link->reserved_chanctx_list);
1595 list_move(&link->assigned_chanctx_list,
1596 &ctx->assigned_links);
1597 link->reserved_chanctx = NULL;
1599 ieee80211_link_chanctx_reservation_complete(link);
1603 * This context might have been a dependency for an already
1604 * ready re-assign reservation interface that was deferred. Do
1605 * not propagate error to the caller though. The in-place
1606 * reservation for originally requested interface has already
1607 * succeeded at this point.
1609 list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
1610 reserved_chanctx_list) {
1611 if (WARN_ON(ieee80211_link_has_in_place_reservation(link)))
1614 if (WARN_ON(link->reserved_chanctx != ctx))
1617 if (!link->reserved_ready)
1620 if (ieee80211_link_get_chanctx(link))
1621 err = ieee80211_link_use_reserved_reassign(link);
1623 err = ieee80211_link_use_reserved_assign(link);
1627 "failed to finalize (re-)assign reservation (err=%d)\n",
1629 ieee80211_link_unreserve_chanctx(link);
1630 cfg80211_stop_iface(local->hw.wiphy,
1638 * Finally free old contexts
1641 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1642 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1645 ctx->replace_ctx->replace_ctx = NULL;
1646 ctx->replace_ctx->replace_state =
1647 IEEE80211_CHANCTX_REPLACE_NONE;
1649 list_del_rcu(&ctx->list);
1650 kfree_rcu(ctx, rcu_head);
1656 list_for_each_entry(ctx, &local->chanctx_list, list) {
1657 struct ieee80211_link_data *link, *link_tmp;
1659 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1662 list_for_each_entry_safe(link, link_tmp, &ctx->reserved_links,
1663 reserved_chanctx_list) {
1664 ieee80211_link_unreserve_chanctx(link);
1665 ieee80211_link_chanctx_reservation_complete(link);
1672 static void __ieee80211_link_release_channel(struct ieee80211_link_data *link)
1674 struct ieee80211_sub_if_data *sdata = link->sdata;
1675 struct ieee80211_bss_conf *link_conf = link->conf;
1676 struct ieee80211_local *local = sdata->local;
1677 struct ieee80211_chanctx_conf *conf;
1678 struct ieee80211_chanctx *ctx;
1679 bool use_reserved_switch = false;
1681 lockdep_assert_wiphy(local->hw.wiphy);
1683 conf = rcu_dereference_protected(link_conf->chanctx_conf,
1684 lockdep_is_held(&local->hw.wiphy->mtx));
1688 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1690 if (link->reserved_chanctx) {
1691 if (link->reserved_chanctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
1692 ieee80211_chanctx_num_reserved(local, link->reserved_chanctx) > 1)
1693 use_reserved_switch = true;
1695 ieee80211_link_unreserve_chanctx(link);
1698 ieee80211_assign_link_chanctx(link, NULL);
1699 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1700 ieee80211_free_chanctx(local, ctx);
1702 link->radar_required = false;
1704 /* Unreserving may ready an in-place reservation. */
1705 if (use_reserved_switch)
1706 ieee80211_vif_use_reserved_switch(local);
1709 int ieee80211_link_use_channel(struct ieee80211_link_data *link,
1710 const struct ieee80211_chan_req *chanreq,
1711 enum ieee80211_chanctx_mode mode)
1713 struct ieee80211_sub_if_data *sdata = link->sdata;
1714 struct ieee80211_local *local = sdata->local;
1715 struct ieee80211_chanctx *ctx;
1716 u8 radar_detect_width = 0;
1719 lockdep_assert_wiphy(local->hw.wiphy);
1721 if (!ieee80211_vif_link_active(&sdata->vif, link->link_id)) {
1722 ieee80211_link_update_chanreq(link, chanreq);
1726 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1728 sdata->wdev.iftype);
1732 radar_detect_width = BIT(chanreq->oper.width);
1734 link->radar_required = ret;
1736 ret = ieee80211_check_combinations(sdata, &chanreq->oper, mode,
1737 radar_detect_width);
1741 __ieee80211_link_release_channel(link);
1743 ctx = ieee80211_find_chanctx(local, chanreq, mode);
1745 ctx = ieee80211_new_chanctx(local, chanreq, mode);
1751 ieee80211_link_update_chanreq(link, chanreq);
1753 ret = ieee80211_assign_link_chanctx(link, ctx);
1755 /* if assign fails refcount stays the same */
1756 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1757 ieee80211_free_chanctx(local, ctx);
1761 ieee80211_recalc_smps_chanctx(local, ctx);
1762 ieee80211_recalc_radar_chanctx(local, ctx);
1765 link->radar_required = false;
1770 int ieee80211_link_use_reserved_context(struct ieee80211_link_data *link)
1772 struct ieee80211_sub_if_data *sdata = link->sdata;
1773 struct ieee80211_local *local = sdata->local;
1774 struct ieee80211_chanctx *new_ctx;
1775 struct ieee80211_chanctx *old_ctx;
1778 lockdep_assert_wiphy(local->hw.wiphy);
1780 new_ctx = link->reserved_chanctx;
1781 old_ctx = ieee80211_link_get_chanctx(link);
1783 if (WARN_ON(!new_ctx))
1786 if (WARN_ON(new_ctx->replace_state ==
1787 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1790 if (WARN_ON(link->reserved_ready))
1793 link->reserved_ready = true;
1795 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1797 return ieee80211_link_use_reserved_reassign(link);
1799 return ieee80211_link_use_reserved_assign(link);
1803 * In-place reservation may need to be finalized now either if:
1804 * a) sdata is taking part in the swapping itself and is the last one
1805 * b) sdata has switched with a re-assign reservation to an existing
1806 * context readying in-place switching of old_ctx
1808 * In case of (b) do not propagate the error up because the requested
1809 * sdata already switched successfully. Just spill an extra warning.
1810 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1811 * interfaces upon failure.
1814 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1815 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1816 err = ieee80211_vif_use_reserved_switch(local);
1817 if (err && err != -EAGAIN) {
1818 if (new_ctx->replace_state ==
1819 IEEE80211_CHANCTX_REPLACES_OTHER)
1822 wiphy_info(local->hw.wiphy,
1823 "depending in-place reservation failed (err=%d)\n",
1832 * This is similar to ieee80211_chanctx_compatible(), but rechecks
1833 * against all the links actually using it (except the one that's
1834 * passed, since that one is changing).
1835 * This is done in order to allow changes to the AP's bandwidth for
1836 * wider bandwidth OFDMA purposes, which wouldn't be treated as
1837 * compatible by ieee80211_chanctx_recheck() but is OK if the link
1838 * requesting the update is the only one using it.
1840 static const struct ieee80211_chan_req *
1841 ieee80211_chanctx_recheck(struct ieee80211_local *local,
1842 struct ieee80211_link_data *skip_link,
1843 struct ieee80211_chanctx *ctx,
1844 const struct ieee80211_chan_req *req,
1845 struct ieee80211_chan_req *tmp)
1847 const struct ieee80211_chan_req *ret = req;
1848 struct ieee80211_link_data *link;
1850 lockdep_assert_wiphy(local->hw.wiphy);
1852 for_each_sdata_link(local, link) {
1853 if (link == skip_link)
1856 if (rcu_access_pointer(link->conf->chanctx_conf) == &ctx->conf) {
1857 ret = ieee80211_chanreq_compatible(ret,
1858 &link->conf->chanreq,
1864 if (link->reserved_chanctx == ctx) {
1865 ret = ieee80211_chanreq_compatible(ret,
1877 int ieee80211_link_change_chanreq(struct ieee80211_link_data *link,
1878 const struct ieee80211_chan_req *chanreq,
1881 struct ieee80211_sub_if_data *sdata = link->sdata;
1882 struct ieee80211_bss_conf *link_conf = link->conf;
1883 struct ieee80211_local *local = sdata->local;
1884 struct ieee80211_chanctx_conf *conf;
1885 struct ieee80211_chanctx *ctx;
1886 const struct ieee80211_chan_req *compat;
1887 struct ieee80211_chan_req tmp;
1889 lockdep_assert_wiphy(local->hw.wiphy);
1891 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy,
1893 IEEE80211_CHAN_DISABLED))
1896 /* for non-HT 20 MHz the rest doesn't matter */
1897 if (chanreq->oper.width == NL80211_CHAN_WIDTH_20_NOHT &&
1898 cfg80211_chandef_identical(&chanreq->oper, &link_conf->chanreq.oper))
1901 /* but you cannot switch to/from it */
1902 if (chanreq->oper.width == NL80211_CHAN_WIDTH_20_NOHT ||
1903 link_conf->chanreq.oper.width == NL80211_CHAN_WIDTH_20_NOHT)
1906 conf = rcu_dereference_protected(link_conf->chanctx_conf,
1907 lockdep_is_held(&local->hw.wiphy->mtx));
1911 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1913 compat = ieee80211_chanctx_recheck(local, link, ctx, chanreq, &tmp);
1917 switch (ctx->replace_state) {
1918 case IEEE80211_CHANCTX_REPLACE_NONE:
1919 if (!ieee80211_chanctx_reserved_chanreq(local, ctx, compat,
1923 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1924 /* TODO: Perhaps the bandwidth change could be treated as a
1925 * reservation itself? */
1927 case IEEE80211_CHANCTX_REPLACES_OTHER:
1928 /* channel context that is going to replace another channel
1929 * context doesn't really exist and shouldn't be assigned
1935 ieee80211_link_update_chanreq(link, chanreq);
1937 ieee80211_recalc_chanctx_chantype(local, ctx);
1939 *changed |= BSS_CHANGED_BANDWIDTH;
1943 void ieee80211_link_release_channel(struct ieee80211_link_data *link)
1945 struct ieee80211_sub_if_data *sdata = link->sdata;
1947 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1949 if (rcu_access_pointer(link->conf->chanctx_conf))
1950 __ieee80211_link_release_channel(link);
1953 void ieee80211_link_vlan_copy_chanctx(struct ieee80211_link_data *link)
1955 struct ieee80211_sub_if_data *sdata = link->sdata;
1956 unsigned int link_id = link->link_id;
1957 struct ieee80211_bss_conf *link_conf = link->conf;
1958 struct ieee80211_bss_conf *ap_conf;
1959 struct ieee80211_local *local = sdata->local;
1960 struct ieee80211_sub_if_data *ap;
1961 struct ieee80211_chanctx_conf *conf;
1963 lockdep_assert_wiphy(local->hw.wiphy);
1965 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1968 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1970 ap_conf = wiphy_dereference(local->hw.wiphy,
1971 ap->vif.link_conf[link_id]);
1972 conf = wiphy_dereference(local->hw.wiphy,
1973 ap_conf->chanctx_conf);
1974 rcu_assign_pointer(link_conf->chanctx_conf, conf);
1977 void ieee80211_iter_chan_contexts_atomic(
1978 struct ieee80211_hw *hw,
1979 void (*iter)(struct ieee80211_hw *hw,
1980 struct ieee80211_chanctx_conf *chanctx_conf,
1984 struct ieee80211_local *local = hw_to_local(hw);
1985 struct ieee80211_chanctx *ctx;
1988 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1989 if (ctx->driver_present)
1990 iter(hw, &ctx->conf, iter_data);
1993 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);