hyperv: Add processing of MTU reduced by the host
[linux/fpc-iii.git] / net / mac80211 / chan.c
blobc7c51422029822269762e40527a7ad44ddcbe991
1 /*
2 * mac80211 - channel management
3 */
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;
16 int num = 0;
18 lockdep_assert_held(&local->chanctx_mtx);
20 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
21 num++;
23 return num;
26 static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
27 struct ieee80211_chanctx *ctx)
29 struct ieee80211_sub_if_data *sdata;
30 int num = 0;
32 lockdep_assert_held(&local->chanctx_mtx);
34 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
35 num++;
37 return num;
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;
50 int num = 0;
52 lockdep_assert_held(&local->chanctx_mtx);
54 list_for_each_entry(ctx, &local->chanctx_list, list)
55 num++;
57 return num;
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));
74 if (!conf)
75 return NULL;
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) {
91 if (!compat)
92 compat = &sdata->reserved_chandef;
94 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
95 compat);
96 if (!compat)
97 break;
100 return compat;
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)
115 continue;
117 if (!compat)
118 compat = &sdata->vif.bss_conf.chandef;
120 compat = cfg80211_chandef_compatible(
121 &sdata->vif.bss_conf.chandef, compat);
122 if (!compat)
123 break;
126 return 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);
137 if (!compat)
138 return NULL;
140 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
141 if (!compat)
142 return NULL;
144 return compat;
147 static bool
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))
155 return true;
157 if (!list_empty(&ctx->reserved_vifs) &&
158 ieee80211_chanctx_reserved_chandef(local, ctx, def))
159 return true;
161 return false;
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)
174 return NULL;
176 list_for_each_entry(ctx, &local->chanctx_list, list) {
177 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
178 continue;
180 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
181 continue;
183 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
184 chandef))
185 continue;
187 return ctx;
190 return NULL;
193 static 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;
199 else
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;
216 default:
217 WARN_ON(1);
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;
228 rcu_read_lock();
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))
232 continue;
234 if (!sta->uploaded)
235 continue;
237 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
239 rcu_read_unlock();
241 return max_bw;
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;
251 rcu_read_lock();
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))
257 continue;
259 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
260 continue;
262 switch (vif->type) {
263 case NL80211_IFTYPE_AP:
264 case NL80211_IFTYPE_AP_VLAN:
265 width = ieee80211_get_max_required_bw(sdata);
266 break;
267 case NL80211_IFTYPE_P2P_DEVICE:
268 continue;
269 case NL80211_IFTYPE_STATION:
270 case NL80211_IFTYPE_ADHOC:
271 case NL80211_IFTYPE_WDS:
272 case NL80211_IFTYPE_MESH_POINT:
273 case NL80211_IFTYPE_OCB:
274 width = vif->bss_conf.chandef.width;
275 break;
276 case NL80211_IFTYPE_UNSPECIFIED:
277 case NUM_NL80211_IFTYPES:
278 case NL80211_IFTYPE_MONITOR:
279 case NL80211_IFTYPE_P2P_CLIENT:
280 case NL80211_IFTYPE_P2P_GO:
281 WARN_ON_ONCE(1);
283 max_bw = max(max_bw, width);
286 /* use the configured bandwidth in case of monitor interface */
287 sdata = rcu_dereference(local->monitor_sdata);
288 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
289 max_bw = max(max_bw, conf->def.width);
291 rcu_read_unlock();
293 return max_bw;
297 * recalc the min required chan width of the channel context, which is
298 * the max of min required widths of all the interfaces bound to this
299 * channel context.
301 void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
302 struct ieee80211_chanctx *ctx)
304 enum nl80211_chan_width max_bw;
305 struct cfg80211_chan_def min_def;
307 lockdep_assert_held(&local->chanctx_mtx);
309 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
310 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
311 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
312 ctx->conf.radar_enabled) {
313 ctx->conf.min_def = ctx->conf.def;
314 return;
317 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
319 /* downgrade chandef up to max_bw */
320 min_def = ctx->conf.def;
321 while (min_def.width > max_bw)
322 ieee80211_chandef_downgrade(&min_def);
324 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
325 return;
327 ctx->conf.min_def = min_def;
328 if (!ctx->driver_present)
329 return;
331 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
334 static void ieee80211_change_chanctx(struct ieee80211_local *local,
335 struct ieee80211_chanctx *ctx,
336 const struct cfg80211_chan_def *chandef)
338 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
339 return;
341 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
343 ctx->conf.def = *chandef;
344 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
345 ieee80211_recalc_chanctx_min_def(local, ctx);
347 if (!local->use_chanctx) {
348 local->_oper_chandef = *chandef;
349 ieee80211_hw_config(local, 0);
353 static struct ieee80211_chanctx *
354 ieee80211_find_chanctx(struct ieee80211_local *local,
355 const struct cfg80211_chan_def *chandef,
356 enum ieee80211_chanctx_mode mode)
358 struct ieee80211_chanctx *ctx;
360 lockdep_assert_held(&local->chanctx_mtx);
362 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
363 return NULL;
365 list_for_each_entry(ctx, &local->chanctx_list, list) {
366 const struct cfg80211_chan_def *compat;
368 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
369 continue;
371 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
372 continue;
374 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
375 if (!compat)
376 continue;
378 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
379 compat);
380 if (!compat)
381 continue;
383 ieee80211_change_chanctx(local, ctx, compat);
385 return ctx;
388 return NULL;
391 static bool ieee80211_is_radar_required(struct ieee80211_local *local)
393 struct ieee80211_sub_if_data *sdata;
395 lockdep_assert_held(&local->mtx);
397 rcu_read_lock();
398 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
399 if (sdata->radar_required) {
400 rcu_read_unlock();
401 return true;
404 rcu_read_unlock();
406 return false;
409 static struct ieee80211_chanctx *
410 ieee80211_alloc_chanctx(struct ieee80211_local *local,
411 const struct cfg80211_chan_def *chandef,
412 enum ieee80211_chanctx_mode mode)
414 struct ieee80211_chanctx *ctx;
416 lockdep_assert_held(&local->chanctx_mtx);
418 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
419 if (!ctx)
420 return NULL;
422 INIT_LIST_HEAD(&ctx->assigned_vifs);
423 INIT_LIST_HEAD(&ctx->reserved_vifs);
424 ctx->conf.def = *chandef;
425 ctx->conf.rx_chains_static = 1;
426 ctx->conf.rx_chains_dynamic = 1;
427 ctx->mode = mode;
428 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
429 ieee80211_recalc_chanctx_min_def(local, ctx);
431 return ctx;
434 static int ieee80211_add_chanctx(struct ieee80211_local *local,
435 struct ieee80211_chanctx *ctx)
437 u32 changed;
438 int err;
440 lockdep_assert_held(&local->mtx);
441 lockdep_assert_held(&local->chanctx_mtx);
443 if (!local->use_chanctx)
444 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
446 /* turn idle off *before* setting channel -- some drivers need that */
447 changed = ieee80211_idle_off(local);
448 if (changed)
449 ieee80211_hw_config(local, changed);
451 if (!local->use_chanctx) {
452 local->_oper_chandef = ctx->conf.def;
453 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
454 } else {
455 err = drv_add_chanctx(local, ctx);
456 if (err) {
457 ieee80211_recalc_idle(local);
458 return err;
462 return 0;
465 static struct ieee80211_chanctx *
466 ieee80211_new_chanctx(struct ieee80211_local *local,
467 const struct cfg80211_chan_def *chandef,
468 enum ieee80211_chanctx_mode mode)
470 struct ieee80211_chanctx *ctx;
471 int err;
473 lockdep_assert_held(&local->mtx);
474 lockdep_assert_held(&local->chanctx_mtx);
476 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
477 if (!ctx)
478 return ERR_PTR(-ENOMEM);
480 err = ieee80211_add_chanctx(local, ctx);
481 if (err) {
482 kfree(ctx);
483 return ERR_PTR(err);
486 list_add_rcu(&ctx->list, &local->chanctx_list);
487 return ctx;
490 static void ieee80211_del_chanctx(struct ieee80211_local *local,
491 struct ieee80211_chanctx *ctx)
493 lockdep_assert_held(&local->chanctx_mtx);
495 if (!local->use_chanctx) {
496 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
497 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
498 chandef->center_freq1 = chandef->chan->center_freq;
499 chandef->center_freq2 = 0;
501 /* NOTE: Disabling radar is only valid here for
502 * single channel context. To be sure, check it ...
504 WARN_ON(local->hw.conf.radar_enabled &&
505 !list_empty(&local->chanctx_list));
507 local->hw.conf.radar_enabled = false;
509 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
510 } else {
511 drv_remove_chanctx(local, ctx);
514 ieee80211_recalc_idle(local);
517 static void ieee80211_free_chanctx(struct ieee80211_local *local,
518 struct ieee80211_chanctx *ctx)
520 lockdep_assert_held(&local->chanctx_mtx);
522 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
524 list_del_rcu(&ctx->list);
525 ieee80211_del_chanctx(local, ctx);
526 kfree_rcu(ctx, rcu_head);
529 static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
530 struct ieee80211_chanctx *ctx)
532 struct ieee80211_chanctx_conf *conf = &ctx->conf;
533 struct ieee80211_sub_if_data *sdata;
534 const struct cfg80211_chan_def *compat = NULL;
536 lockdep_assert_held(&local->chanctx_mtx);
538 rcu_read_lock();
539 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
541 if (!ieee80211_sdata_running(sdata))
542 continue;
543 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
544 continue;
545 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
546 continue;
548 if (!compat)
549 compat = &sdata->vif.bss_conf.chandef;
551 compat = cfg80211_chandef_compatible(
552 &sdata->vif.bss_conf.chandef, compat);
553 if (WARN_ON_ONCE(!compat))
554 break;
556 rcu_read_unlock();
558 if (!compat)
559 return;
561 ieee80211_change_chanctx(local, ctx, compat);
564 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
565 struct ieee80211_chanctx *chanctx)
567 bool radar_enabled;
569 lockdep_assert_held(&local->chanctx_mtx);
570 /* for setting local->radar_detect_enabled */
571 lockdep_assert_held(&local->mtx);
573 radar_enabled = ieee80211_is_radar_required(local);
575 if (radar_enabled == chanctx->conf.radar_enabled)
576 return;
578 chanctx->conf.radar_enabled = radar_enabled;
579 local->radar_detect_enabled = chanctx->conf.radar_enabled;
581 if (!local->use_chanctx) {
582 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
583 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
586 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
589 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
590 struct ieee80211_chanctx *new_ctx)
592 struct ieee80211_local *local = sdata->local;
593 struct ieee80211_chanctx_conf *conf;
594 struct ieee80211_chanctx *curr_ctx = NULL;
595 int ret = 0;
597 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
598 lockdep_is_held(&local->chanctx_mtx));
600 if (conf) {
601 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
603 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
604 conf = NULL;
605 list_del(&sdata->assigned_chanctx_list);
608 if (new_ctx) {
609 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
610 if (ret)
611 goto out;
613 conf = &new_ctx->conf;
614 list_add(&sdata->assigned_chanctx_list,
615 &new_ctx->assigned_vifs);
618 out:
619 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
621 sdata->vif.bss_conf.idle = !conf;
623 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
624 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
625 ieee80211_recalc_smps_chanctx(local, curr_ctx);
626 ieee80211_recalc_radar_chanctx(local, curr_ctx);
627 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
630 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
631 ieee80211_recalc_txpower(sdata);
632 ieee80211_recalc_chanctx_min_def(local, new_ctx);
635 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
636 sdata->vif.type != NL80211_IFTYPE_MONITOR)
637 ieee80211_bss_info_change_notify(sdata,
638 BSS_CHANGED_IDLE);
640 return ret;
643 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
644 struct ieee80211_chanctx *chanctx)
646 struct ieee80211_sub_if_data *sdata;
647 u8 rx_chains_static, rx_chains_dynamic;
649 lockdep_assert_held(&local->chanctx_mtx);
651 rx_chains_static = 1;
652 rx_chains_dynamic = 1;
654 rcu_read_lock();
655 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
656 u8 needed_static, needed_dynamic;
658 if (!ieee80211_sdata_running(sdata))
659 continue;
661 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
662 &chanctx->conf)
663 continue;
665 switch (sdata->vif.type) {
666 case NL80211_IFTYPE_P2P_DEVICE:
667 continue;
668 case NL80211_IFTYPE_STATION:
669 if (!sdata->u.mgd.associated)
670 continue;
671 break;
672 case NL80211_IFTYPE_AP_VLAN:
673 continue;
674 case NL80211_IFTYPE_AP:
675 case NL80211_IFTYPE_ADHOC:
676 case NL80211_IFTYPE_WDS:
677 case NL80211_IFTYPE_MESH_POINT:
678 case NL80211_IFTYPE_OCB:
679 break;
680 default:
681 WARN_ON_ONCE(1);
684 switch (sdata->smps_mode) {
685 default:
686 WARN_ONCE(1, "Invalid SMPS mode %d\n",
687 sdata->smps_mode);
688 /* fall through */
689 case IEEE80211_SMPS_OFF:
690 needed_static = sdata->needed_rx_chains;
691 needed_dynamic = sdata->needed_rx_chains;
692 break;
693 case IEEE80211_SMPS_DYNAMIC:
694 needed_static = 1;
695 needed_dynamic = sdata->needed_rx_chains;
696 break;
697 case IEEE80211_SMPS_STATIC:
698 needed_static = 1;
699 needed_dynamic = 1;
700 break;
703 rx_chains_static = max(rx_chains_static, needed_static);
704 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
707 /* Disable SMPS for the monitor interface */
708 sdata = rcu_dereference(local->monitor_sdata);
709 if (sdata &&
710 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
711 rx_chains_dynamic = rx_chains_static = local->rx_chains;
713 rcu_read_unlock();
715 if (!local->use_chanctx) {
716 if (rx_chains_static > 1)
717 local->smps_mode = IEEE80211_SMPS_OFF;
718 else if (rx_chains_dynamic > 1)
719 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
720 else
721 local->smps_mode = IEEE80211_SMPS_STATIC;
722 ieee80211_hw_config(local, 0);
725 if (rx_chains_static == chanctx->conf.rx_chains_static &&
726 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
727 return;
729 chanctx->conf.rx_chains_static = rx_chains_static;
730 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
731 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
734 static void
735 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
736 bool clear)
738 struct ieee80211_local *local __maybe_unused = sdata->local;
739 struct ieee80211_sub_if_data *vlan;
740 struct ieee80211_chanctx_conf *conf;
742 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
743 return;
745 lockdep_assert_held(&local->mtx);
747 /* Check that conf exists, even when clearing this function
748 * must be called with the AP's channel context still there
749 * as it would otherwise cause VLANs to have an invalid
750 * channel context pointer for a while, possibly pointing
751 * to a channel context that has already been freed.
753 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
754 lockdep_is_held(&local->chanctx_mtx));
755 WARN_ON(!conf);
757 if (clear)
758 conf = NULL;
760 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
761 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
764 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
765 bool clear)
767 struct ieee80211_local *local = sdata->local;
769 mutex_lock(&local->chanctx_mtx);
771 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
773 mutex_unlock(&local->chanctx_mtx);
776 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
778 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
780 lockdep_assert_held(&sdata->local->chanctx_mtx);
782 if (WARN_ON(!ctx))
783 return -EINVAL;
785 list_del(&sdata->reserved_chanctx_list);
786 sdata->reserved_chanctx = NULL;
788 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
789 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
790 if (WARN_ON(!ctx->replace_ctx))
791 return -EINVAL;
793 WARN_ON(ctx->replace_ctx->replace_state !=
794 IEEE80211_CHANCTX_WILL_BE_REPLACED);
795 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
797 ctx->replace_ctx->replace_ctx = NULL;
798 ctx->replace_ctx->replace_state =
799 IEEE80211_CHANCTX_REPLACE_NONE;
801 list_del_rcu(&ctx->list);
802 kfree_rcu(ctx, rcu_head);
803 } else {
804 ieee80211_free_chanctx(sdata->local, ctx);
808 return 0;
811 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
812 const struct cfg80211_chan_def *chandef,
813 enum ieee80211_chanctx_mode mode,
814 bool radar_required)
816 struct ieee80211_local *local = sdata->local;
817 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
819 lockdep_assert_held(&local->chanctx_mtx);
821 curr_ctx = ieee80211_vif_get_chanctx(sdata);
822 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
823 return -ENOTSUPP;
825 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
826 if (!new_ctx) {
827 if (ieee80211_can_create_new_chanctx(local)) {
828 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
829 if (IS_ERR(new_ctx))
830 return PTR_ERR(new_ctx);
831 } else {
832 if (!curr_ctx ||
833 (curr_ctx->replace_state ==
834 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
835 !list_empty(&curr_ctx->reserved_vifs)) {
837 * Another vif already requested this context
838 * for a reservation. Find another one hoping
839 * all vifs assigned to it will also switch
840 * soon enough.
842 * TODO: This needs a little more work as some
843 * cases (more than 2 chanctx capable devices)
844 * may fail which could otherwise succeed
845 * provided some channel context juggling was
846 * performed.
848 * Consider ctx1..3, vif1..6, each ctx has 2
849 * vifs. vif1 and vif2 from ctx1 request new
850 * different chandefs starting 2 in-place
851 * reserations with ctx4 and ctx5 replacing
852 * ctx1 and ctx2 respectively. Next vif5 and
853 * vif6 from ctx3 reserve ctx4. If vif3 and
854 * vif4 remain on ctx2 as they are then this
855 * fails unless `replace_ctx` from ctx5 is
856 * replaced with ctx3.
858 list_for_each_entry(ctx, &local->chanctx_list,
859 list) {
860 if (ctx->replace_state !=
861 IEEE80211_CHANCTX_REPLACE_NONE)
862 continue;
864 if (!list_empty(&ctx->reserved_vifs))
865 continue;
867 curr_ctx = ctx;
868 break;
873 * If that's true then all available contexts already
874 * have reservations and cannot be used.
876 if (!curr_ctx ||
877 (curr_ctx->replace_state ==
878 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
879 !list_empty(&curr_ctx->reserved_vifs))
880 return -EBUSY;
882 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
883 if (!new_ctx)
884 return -ENOMEM;
886 new_ctx->replace_ctx = curr_ctx;
887 new_ctx->replace_state =
888 IEEE80211_CHANCTX_REPLACES_OTHER;
890 curr_ctx->replace_ctx = new_ctx;
891 curr_ctx->replace_state =
892 IEEE80211_CHANCTX_WILL_BE_REPLACED;
894 list_add_rcu(&new_ctx->list, &local->chanctx_list);
898 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
899 sdata->reserved_chanctx = new_ctx;
900 sdata->reserved_chandef = *chandef;
901 sdata->reserved_radar_required = radar_required;
902 sdata->reserved_ready = false;
904 return 0;
907 static void
908 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
910 switch (sdata->vif.type) {
911 case NL80211_IFTYPE_ADHOC:
912 case NL80211_IFTYPE_AP:
913 case NL80211_IFTYPE_MESH_POINT:
914 case NL80211_IFTYPE_OCB:
915 ieee80211_queue_work(&sdata->local->hw,
916 &sdata->csa_finalize_work);
917 break;
918 case NL80211_IFTYPE_STATION:
919 ieee80211_queue_work(&sdata->local->hw,
920 &sdata->u.mgd.chswitch_work);
921 break;
922 case NL80211_IFTYPE_UNSPECIFIED:
923 case NL80211_IFTYPE_AP_VLAN:
924 case NL80211_IFTYPE_WDS:
925 case NL80211_IFTYPE_MONITOR:
926 case NL80211_IFTYPE_P2P_CLIENT:
927 case NL80211_IFTYPE_P2P_GO:
928 case NL80211_IFTYPE_P2P_DEVICE:
929 case NUM_NL80211_IFTYPES:
930 WARN_ON(1);
931 break;
935 static int
936 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
938 struct ieee80211_local *local = sdata->local;
939 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
940 struct ieee80211_chanctx *old_ctx, *new_ctx;
941 const struct cfg80211_chan_def *chandef;
942 u32 changed = 0;
943 int err;
945 lockdep_assert_held(&local->mtx);
946 lockdep_assert_held(&local->chanctx_mtx);
948 new_ctx = sdata->reserved_chanctx;
949 old_ctx = ieee80211_vif_get_chanctx(sdata);
951 if (WARN_ON(!sdata->reserved_ready))
952 return -EBUSY;
954 if (WARN_ON(!new_ctx))
955 return -EINVAL;
957 if (WARN_ON(!old_ctx))
958 return -EINVAL;
960 if (WARN_ON(new_ctx->replace_state ==
961 IEEE80211_CHANCTX_REPLACES_OTHER))
962 return -EINVAL;
964 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
965 &sdata->reserved_chandef);
966 if (WARN_ON(!chandef))
967 return -EINVAL;
969 vif_chsw[0].vif = &sdata->vif;
970 vif_chsw[0].old_ctx = &old_ctx->conf;
971 vif_chsw[0].new_ctx = &new_ctx->conf;
973 list_del(&sdata->reserved_chanctx_list);
974 sdata->reserved_chanctx = NULL;
976 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
977 CHANCTX_SWMODE_REASSIGN_VIF);
978 if (err) {
979 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
980 ieee80211_free_chanctx(local, new_ctx);
982 goto out;
985 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
986 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
988 if (sdata->vif.type == NL80211_IFTYPE_AP)
989 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
991 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
992 ieee80211_free_chanctx(local, old_ctx);
994 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
995 changed = BSS_CHANGED_BANDWIDTH;
997 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
999 if (changed)
1000 ieee80211_bss_info_change_notify(sdata, changed);
1002 out:
1003 ieee80211_vif_chanctx_reservation_complete(sdata);
1004 return err;
1007 static int
1008 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1010 struct ieee80211_local *local = sdata->local;
1011 struct ieee80211_chanctx *old_ctx, *new_ctx;
1012 const struct cfg80211_chan_def *chandef;
1013 int err;
1015 old_ctx = ieee80211_vif_get_chanctx(sdata);
1016 new_ctx = sdata->reserved_chanctx;
1018 if (WARN_ON(!sdata->reserved_ready))
1019 return -EINVAL;
1021 if (WARN_ON(old_ctx))
1022 return -EINVAL;
1024 if (WARN_ON(!new_ctx))
1025 return -EINVAL;
1027 if (WARN_ON(new_ctx->replace_state ==
1028 IEEE80211_CHANCTX_REPLACES_OTHER))
1029 return -EINVAL;
1031 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1032 &sdata->reserved_chandef);
1033 if (WARN_ON(!chandef))
1034 return -EINVAL;
1036 list_del(&sdata->reserved_chanctx_list);
1037 sdata->reserved_chanctx = NULL;
1039 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1040 if (err) {
1041 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1042 ieee80211_free_chanctx(local, new_ctx);
1044 goto out;
1047 out:
1048 ieee80211_vif_chanctx_reservation_complete(sdata);
1049 return err;
1052 static bool
1053 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1055 struct ieee80211_chanctx *old_ctx, *new_ctx;
1057 lockdep_assert_held(&sdata->local->chanctx_mtx);
1059 new_ctx = sdata->reserved_chanctx;
1060 old_ctx = ieee80211_vif_get_chanctx(sdata);
1062 if (!old_ctx)
1063 return false;
1065 if (WARN_ON(!new_ctx))
1066 return false;
1068 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1069 return false;
1071 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1072 return false;
1074 return true;
1077 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1078 struct ieee80211_chanctx *new_ctx)
1080 const struct cfg80211_chan_def *chandef;
1082 lockdep_assert_held(&local->mtx);
1083 lockdep_assert_held(&local->chanctx_mtx);
1085 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1086 if (WARN_ON(!chandef))
1087 return -EINVAL;
1089 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1090 local->_oper_chandef = *chandef;
1091 ieee80211_hw_config(local, 0);
1093 return 0;
1096 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1097 int n_vifs)
1099 struct ieee80211_vif_chanctx_switch *vif_chsw;
1100 struct ieee80211_sub_if_data *sdata;
1101 struct ieee80211_chanctx *ctx, *old_ctx;
1102 int i, err;
1104 lockdep_assert_held(&local->mtx);
1105 lockdep_assert_held(&local->chanctx_mtx);
1107 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1108 if (!vif_chsw)
1109 return -ENOMEM;
1111 i = 0;
1112 list_for_each_entry(ctx, &local->chanctx_list, list) {
1113 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1114 continue;
1116 if (WARN_ON(!ctx->replace_ctx)) {
1117 err = -EINVAL;
1118 goto out;
1121 list_for_each_entry(sdata, &ctx->reserved_vifs,
1122 reserved_chanctx_list) {
1123 if (!ieee80211_vif_has_in_place_reservation(
1124 sdata))
1125 continue;
1127 old_ctx = ieee80211_vif_get_chanctx(sdata);
1128 vif_chsw[i].vif = &sdata->vif;
1129 vif_chsw[i].old_ctx = &old_ctx->conf;
1130 vif_chsw[i].new_ctx = &ctx->conf;
1132 i++;
1136 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1137 CHANCTX_SWMODE_SWAP_CONTEXTS);
1139 out:
1140 kfree(vif_chsw);
1141 return err;
1144 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1146 struct ieee80211_chanctx *ctx;
1147 int err;
1149 lockdep_assert_held(&local->mtx);
1150 lockdep_assert_held(&local->chanctx_mtx);
1152 list_for_each_entry(ctx, &local->chanctx_list, list) {
1153 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1154 continue;
1156 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1157 continue;
1159 ieee80211_del_chanctx(local, ctx->replace_ctx);
1160 err = ieee80211_add_chanctx(local, ctx);
1161 if (err)
1162 goto err;
1165 return 0;
1167 err:
1168 WARN_ON(ieee80211_add_chanctx(local, ctx));
1169 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1170 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1171 continue;
1173 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1174 continue;
1176 ieee80211_del_chanctx(local, ctx);
1177 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1180 return err;
1183 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1185 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1186 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1187 struct ieee80211_chanctx *new_ctx = NULL;
1188 int i, err, n_assigned, n_reserved, n_ready;
1189 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1191 lockdep_assert_held(&local->mtx);
1192 lockdep_assert_held(&local->chanctx_mtx);
1195 * If there are 2 independent pairs of channel contexts performing
1196 * cross-switch of their vifs this code will still wait until both are
1197 * ready even though it could be possible to switch one before the
1198 * other is ready.
1200 * For practical reasons and code simplicity just do a single huge
1201 * switch.
1205 * Verify if the reservation is still feasible.
1206 * - if it's not then disconnect
1207 * - if it is but not all vifs necessary are ready then defer
1210 list_for_each_entry(ctx, &local->chanctx_list, list) {
1211 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1212 continue;
1214 if (WARN_ON(!ctx->replace_ctx)) {
1215 err = -EINVAL;
1216 goto err;
1219 if (!local->use_chanctx)
1220 new_ctx = ctx;
1222 n_ctx++;
1224 n_assigned = 0;
1225 n_reserved = 0;
1226 n_ready = 0;
1228 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1229 assigned_chanctx_list) {
1230 n_assigned++;
1231 if (sdata->reserved_chanctx) {
1232 n_reserved++;
1233 if (sdata->reserved_ready)
1234 n_ready++;
1238 if (n_assigned != n_reserved) {
1239 if (n_ready == n_reserved) {
1240 wiphy_info(local->hw.wiphy,
1241 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1242 err = -EBUSY;
1243 goto err;
1246 return -EAGAIN;
1249 ctx->conf.radar_enabled = false;
1250 list_for_each_entry(sdata, &ctx->reserved_vifs,
1251 reserved_chanctx_list) {
1252 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1253 !sdata->reserved_ready)
1254 return -EAGAIN;
1256 old_ctx = ieee80211_vif_get_chanctx(sdata);
1257 if (old_ctx) {
1258 if (old_ctx->replace_state ==
1259 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1260 n_vifs_switch++;
1261 else
1262 n_vifs_assign++;
1263 } else {
1264 n_vifs_ctxless++;
1267 if (sdata->reserved_radar_required)
1268 ctx->conf.radar_enabled = true;
1272 if (WARN_ON(n_ctx == 0) ||
1273 WARN_ON(n_vifs_switch == 0 &&
1274 n_vifs_assign == 0 &&
1275 n_vifs_ctxless == 0) ||
1276 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1277 WARN_ON(!new_ctx && !local->use_chanctx)) {
1278 err = -EINVAL;
1279 goto err;
1283 * All necessary vifs are ready. Perform the switch now depending on
1284 * reservations and driver capabilities.
1287 if (local->use_chanctx) {
1288 if (n_vifs_switch > 0) {
1289 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1290 if (err)
1291 goto err;
1294 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1295 err = ieee80211_chsw_switch_ctxs(local);
1296 if (err)
1297 goto err;
1299 } else {
1300 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1301 if (err)
1302 goto err;
1306 * Update all structures, values and pointers to point to new channel
1307 * context(s).
1310 i = 0;
1311 list_for_each_entry(ctx, &local->chanctx_list, list) {
1312 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1313 continue;
1315 if (WARN_ON(!ctx->replace_ctx)) {
1316 err = -EINVAL;
1317 goto err;
1320 list_for_each_entry(sdata, &ctx->reserved_vifs,
1321 reserved_chanctx_list) {
1322 u32 changed = 0;
1324 if (!ieee80211_vif_has_in_place_reservation(sdata))
1325 continue;
1327 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1329 if (sdata->vif.type == NL80211_IFTYPE_AP)
1330 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1331 false);
1333 sdata->radar_required = sdata->reserved_radar_required;
1335 if (sdata->vif.bss_conf.chandef.width !=
1336 sdata->reserved_chandef.width)
1337 changed = BSS_CHANGED_BANDWIDTH;
1339 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
1340 if (changed)
1341 ieee80211_bss_info_change_notify(sdata,
1342 changed);
1344 ieee80211_recalc_txpower(sdata);
1347 ieee80211_recalc_chanctx_chantype(local, ctx);
1348 ieee80211_recalc_smps_chanctx(local, ctx);
1349 ieee80211_recalc_radar_chanctx(local, ctx);
1350 ieee80211_recalc_chanctx_min_def(local, ctx);
1352 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1353 reserved_chanctx_list) {
1354 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1355 continue;
1357 list_del(&sdata->reserved_chanctx_list);
1358 list_move(&sdata->assigned_chanctx_list,
1359 &ctx->assigned_vifs);
1360 sdata->reserved_chanctx = NULL;
1362 ieee80211_vif_chanctx_reservation_complete(sdata);
1366 * This context might have been a dependency for an already
1367 * ready re-assign reservation interface that was deferred. Do
1368 * not propagate error to the caller though. The in-place
1369 * reservation for originally requested interface has already
1370 * succeeded at this point.
1372 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1373 reserved_chanctx_list) {
1374 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1375 sdata)))
1376 continue;
1378 if (WARN_ON(sdata->reserved_chanctx != ctx))
1379 continue;
1381 if (!sdata->reserved_ready)
1382 continue;
1384 if (ieee80211_vif_get_chanctx(sdata))
1385 err = ieee80211_vif_use_reserved_reassign(
1386 sdata);
1387 else
1388 err = ieee80211_vif_use_reserved_assign(sdata);
1390 if (err) {
1391 sdata_info(sdata,
1392 "failed to finalize (re-)assign reservation (err=%d)\n",
1393 err);
1394 ieee80211_vif_unreserve_chanctx(sdata);
1395 cfg80211_stop_iface(local->hw.wiphy,
1396 &sdata->wdev,
1397 GFP_KERNEL);
1403 * Finally free old contexts
1406 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1407 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1408 continue;
1410 ctx->replace_ctx->replace_ctx = NULL;
1411 ctx->replace_ctx->replace_state =
1412 IEEE80211_CHANCTX_REPLACE_NONE;
1414 list_del_rcu(&ctx->list);
1415 kfree_rcu(ctx, rcu_head);
1418 return 0;
1420 err:
1421 list_for_each_entry(ctx, &local->chanctx_list, list) {
1422 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1423 continue;
1425 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1426 reserved_chanctx_list) {
1427 ieee80211_vif_unreserve_chanctx(sdata);
1428 ieee80211_vif_chanctx_reservation_complete(sdata);
1432 return err;
1435 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1437 struct ieee80211_local *local = sdata->local;
1438 struct ieee80211_chanctx_conf *conf;
1439 struct ieee80211_chanctx *ctx;
1440 bool use_reserved_switch = false;
1442 lockdep_assert_held(&local->chanctx_mtx);
1444 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1445 lockdep_is_held(&local->chanctx_mtx));
1446 if (!conf)
1447 return;
1449 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1451 if (sdata->reserved_chanctx) {
1452 if (sdata->reserved_chanctx->replace_state ==
1453 IEEE80211_CHANCTX_REPLACES_OTHER &&
1454 ieee80211_chanctx_num_reserved(local,
1455 sdata->reserved_chanctx) > 1)
1456 use_reserved_switch = true;
1458 ieee80211_vif_unreserve_chanctx(sdata);
1461 ieee80211_assign_vif_chanctx(sdata, NULL);
1462 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1463 ieee80211_free_chanctx(local, ctx);
1465 /* Unreserving may ready an in-place reservation. */
1466 if (use_reserved_switch)
1467 ieee80211_vif_use_reserved_switch(local);
1470 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1471 const struct cfg80211_chan_def *chandef,
1472 enum ieee80211_chanctx_mode mode)
1474 struct ieee80211_local *local = sdata->local;
1475 struct ieee80211_chanctx *ctx;
1476 u8 radar_detect_width = 0;
1477 int ret;
1479 lockdep_assert_held(&local->mtx);
1481 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1483 mutex_lock(&local->chanctx_mtx);
1485 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1486 chandef,
1487 sdata->wdev.iftype);
1488 if (ret < 0)
1489 goto out;
1490 if (ret > 0)
1491 radar_detect_width = BIT(chandef->width);
1493 sdata->radar_required = ret;
1495 ret = ieee80211_check_combinations(sdata, chandef, mode,
1496 radar_detect_width);
1497 if (ret < 0)
1498 goto out;
1500 __ieee80211_vif_release_channel(sdata);
1502 ctx = ieee80211_find_chanctx(local, chandef, mode);
1503 if (!ctx)
1504 ctx = ieee80211_new_chanctx(local, chandef, mode);
1505 if (IS_ERR(ctx)) {
1506 ret = PTR_ERR(ctx);
1507 goto out;
1510 sdata->vif.bss_conf.chandef = *chandef;
1512 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1513 if (ret) {
1514 /* if assign fails refcount stays the same */
1515 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1516 ieee80211_free_chanctx(local, ctx);
1517 goto out;
1520 ieee80211_recalc_smps_chanctx(local, ctx);
1521 ieee80211_recalc_radar_chanctx(local, ctx);
1522 out:
1523 mutex_unlock(&local->chanctx_mtx);
1524 return ret;
1527 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1529 struct ieee80211_local *local = sdata->local;
1530 struct ieee80211_chanctx *new_ctx;
1531 struct ieee80211_chanctx *old_ctx;
1532 int err;
1534 lockdep_assert_held(&local->mtx);
1535 lockdep_assert_held(&local->chanctx_mtx);
1537 new_ctx = sdata->reserved_chanctx;
1538 old_ctx = ieee80211_vif_get_chanctx(sdata);
1540 if (WARN_ON(!new_ctx))
1541 return -EINVAL;
1543 if (WARN_ON(new_ctx->replace_state ==
1544 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1545 return -EINVAL;
1547 if (WARN_ON(sdata->reserved_ready))
1548 return -EINVAL;
1550 sdata->reserved_ready = true;
1552 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1553 if (old_ctx)
1554 err = ieee80211_vif_use_reserved_reassign(sdata);
1555 else
1556 err = ieee80211_vif_use_reserved_assign(sdata);
1558 if (err)
1559 return err;
1563 * In-place reservation may need to be finalized now either if:
1564 * a) sdata is taking part in the swapping itself and is the last one
1565 * b) sdata has switched with a re-assign reservation to an existing
1566 * context readying in-place switching of old_ctx
1568 * In case of (b) do not propagate the error up because the requested
1569 * sdata already switched successfully. Just spill an extra warning.
1570 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1571 * interfaces upon failure.
1573 if ((old_ctx &&
1574 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1575 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1576 err = ieee80211_vif_use_reserved_switch(local);
1577 if (err && err != -EAGAIN) {
1578 if (new_ctx->replace_state ==
1579 IEEE80211_CHANCTX_REPLACES_OTHER)
1580 return err;
1582 wiphy_info(local->hw.wiphy,
1583 "depending in-place reservation failed (err=%d)\n",
1584 err);
1588 return 0;
1591 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1592 const struct cfg80211_chan_def *chandef,
1593 u32 *changed)
1595 struct ieee80211_local *local = sdata->local;
1596 struct ieee80211_chanctx_conf *conf;
1597 struct ieee80211_chanctx *ctx;
1598 const struct cfg80211_chan_def *compat;
1599 int ret;
1601 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1602 IEEE80211_CHAN_DISABLED))
1603 return -EINVAL;
1605 mutex_lock(&local->chanctx_mtx);
1606 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1607 ret = 0;
1608 goto out;
1611 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1612 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1613 ret = -EINVAL;
1614 goto out;
1617 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1618 lockdep_is_held(&local->chanctx_mtx));
1619 if (!conf) {
1620 ret = -EINVAL;
1621 goto out;
1624 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1626 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1627 if (!compat) {
1628 ret = -EINVAL;
1629 goto out;
1632 switch (ctx->replace_state) {
1633 case IEEE80211_CHANCTX_REPLACE_NONE:
1634 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1635 ret = -EBUSY;
1636 goto out;
1638 break;
1639 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1640 /* TODO: Perhaps the bandwidth change could be treated as a
1641 * reservation itself? */
1642 ret = -EBUSY;
1643 goto out;
1644 case IEEE80211_CHANCTX_REPLACES_OTHER:
1645 /* channel context that is going to replace another channel
1646 * context doesn't really exist and shouldn't be assigned
1647 * anywhere yet */
1648 WARN_ON(1);
1649 break;
1652 sdata->vif.bss_conf.chandef = *chandef;
1654 ieee80211_recalc_chanctx_chantype(local, ctx);
1656 *changed |= BSS_CHANGED_BANDWIDTH;
1657 ret = 0;
1658 out:
1659 mutex_unlock(&local->chanctx_mtx);
1660 return ret;
1663 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1665 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1667 lockdep_assert_held(&sdata->local->mtx);
1669 mutex_lock(&sdata->local->chanctx_mtx);
1670 __ieee80211_vif_release_channel(sdata);
1671 mutex_unlock(&sdata->local->chanctx_mtx);
1674 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1676 struct ieee80211_local *local = sdata->local;
1677 struct ieee80211_sub_if_data *ap;
1678 struct ieee80211_chanctx_conf *conf;
1680 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1681 return;
1683 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1685 mutex_lock(&local->chanctx_mtx);
1687 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1688 lockdep_is_held(&local->chanctx_mtx));
1689 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1690 mutex_unlock(&local->chanctx_mtx);
1693 void ieee80211_iter_chan_contexts_atomic(
1694 struct ieee80211_hw *hw,
1695 void (*iter)(struct ieee80211_hw *hw,
1696 struct ieee80211_chanctx_conf *chanctx_conf,
1697 void *data),
1698 void *iter_data)
1700 struct ieee80211_local *local = hw_to_local(hw);
1701 struct ieee80211_chanctx *ctx;
1703 rcu_read_lock();
1704 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1705 if (ctx->driver_present)
1706 iter(hw, &ctx->conf, iter_data);
1707 rcu_read_unlock();
1709 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);