ARM: rockchip: fix broken build
[linux/fpc-iii.git] / net / mac80211 / chan.c
blobf01c18a3160e11d72dae9e2a0939530ec805f6a5
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 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 bool
410 ieee80211_chanctx_radar_required(struct ieee80211_local *local,
411 struct ieee80211_chanctx *ctx)
413 struct ieee80211_chanctx_conf *conf = &ctx->conf;
414 struct ieee80211_sub_if_data *sdata;
415 bool required = false;
417 lockdep_assert_held(&local->chanctx_mtx);
418 lockdep_assert_held(&local->mtx);
420 rcu_read_lock();
421 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
422 if (!ieee80211_sdata_running(sdata))
423 continue;
424 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
425 continue;
426 if (!sdata->radar_required)
427 continue;
429 required = true;
430 break;
432 rcu_read_unlock();
434 return required;
437 static struct ieee80211_chanctx *
438 ieee80211_alloc_chanctx(struct ieee80211_local *local,
439 const struct cfg80211_chan_def *chandef,
440 enum ieee80211_chanctx_mode mode)
442 struct ieee80211_chanctx *ctx;
444 lockdep_assert_held(&local->chanctx_mtx);
446 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
447 if (!ctx)
448 return NULL;
450 INIT_LIST_HEAD(&ctx->assigned_vifs);
451 INIT_LIST_HEAD(&ctx->reserved_vifs);
452 ctx->conf.def = *chandef;
453 ctx->conf.rx_chains_static = 1;
454 ctx->conf.rx_chains_dynamic = 1;
455 ctx->mode = mode;
456 ctx->conf.radar_enabled = false;
457 ieee80211_recalc_chanctx_min_def(local, ctx);
459 return ctx;
462 static int ieee80211_add_chanctx(struct ieee80211_local *local,
463 struct ieee80211_chanctx *ctx)
465 u32 changed;
466 int err;
468 lockdep_assert_held(&local->mtx);
469 lockdep_assert_held(&local->chanctx_mtx);
471 if (!local->use_chanctx)
472 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
474 /* turn idle off *before* setting channel -- some drivers need that */
475 changed = ieee80211_idle_off(local);
476 if (changed)
477 ieee80211_hw_config(local, changed);
479 if (!local->use_chanctx) {
480 local->_oper_chandef = ctx->conf.def;
481 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
482 } else {
483 err = drv_add_chanctx(local, ctx);
484 if (err) {
485 ieee80211_recalc_idle(local);
486 return err;
490 return 0;
493 static struct ieee80211_chanctx *
494 ieee80211_new_chanctx(struct ieee80211_local *local,
495 const struct cfg80211_chan_def *chandef,
496 enum ieee80211_chanctx_mode mode)
498 struct ieee80211_chanctx *ctx;
499 int err;
501 lockdep_assert_held(&local->mtx);
502 lockdep_assert_held(&local->chanctx_mtx);
504 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
505 if (!ctx)
506 return ERR_PTR(-ENOMEM);
508 err = ieee80211_add_chanctx(local, ctx);
509 if (err) {
510 kfree(ctx);
511 return ERR_PTR(err);
514 list_add_rcu(&ctx->list, &local->chanctx_list);
515 return ctx;
518 static void ieee80211_del_chanctx(struct ieee80211_local *local,
519 struct ieee80211_chanctx *ctx)
521 lockdep_assert_held(&local->chanctx_mtx);
523 if (!local->use_chanctx) {
524 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
525 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
526 chandef->center_freq1 = chandef->chan->center_freq;
527 chandef->center_freq2 = 0;
529 /* NOTE: Disabling radar is only valid here for
530 * single channel context. To be sure, check it ...
532 WARN_ON(local->hw.conf.radar_enabled &&
533 !list_empty(&local->chanctx_list));
535 local->hw.conf.radar_enabled = false;
537 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
538 } else {
539 drv_remove_chanctx(local, ctx);
542 ieee80211_recalc_idle(local);
545 static void ieee80211_free_chanctx(struct ieee80211_local *local,
546 struct ieee80211_chanctx *ctx)
548 lockdep_assert_held(&local->chanctx_mtx);
550 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
552 list_del_rcu(&ctx->list);
553 ieee80211_del_chanctx(local, ctx);
554 kfree_rcu(ctx, rcu_head);
557 static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
558 struct ieee80211_chanctx *ctx)
560 struct ieee80211_chanctx_conf *conf = &ctx->conf;
561 struct ieee80211_sub_if_data *sdata;
562 const struct cfg80211_chan_def *compat = NULL;
564 lockdep_assert_held(&local->chanctx_mtx);
566 rcu_read_lock();
567 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
569 if (!ieee80211_sdata_running(sdata))
570 continue;
571 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
572 continue;
573 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
574 continue;
576 if (!compat)
577 compat = &sdata->vif.bss_conf.chandef;
579 compat = cfg80211_chandef_compatible(
580 &sdata->vif.bss_conf.chandef, compat);
581 if (WARN_ON_ONCE(!compat))
582 break;
584 rcu_read_unlock();
586 if (!compat)
587 return;
589 ieee80211_change_chanctx(local, ctx, compat);
592 static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
593 struct ieee80211_chanctx *chanctx)
595 bool radar_enabled;
597 lockdep_assert_held(&local->chanctx_mtx);
598 /* for ieee80211_is_radar_required */
599 lockdep_assert_held(&local->mtx);
601 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
603 if (radar_enabled == chanctx->conf.radar_enabled)
604 return;
606 chanctx->conf.radar_enabled = radar_enabled;
608 if (!local->use_chanctx) {
609 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
610 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
613 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
616 static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
617 struct ieee80211_chanctx *new_ctx)
619 struct ieee80211_local *local = sdata->local;
620 struct ieee80211_chanctx_conf *conf;
621 struct ieee80211_chanctx *curr_ctx = NULL;
622 int ret = 0;
624 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
625 lockdep_is_held(&local->chanctx_mtx));
627 if (conf) {
628 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
630 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
631 conf = NULL;
632 list_del(&sdata->assigned_chanctx_list);
635 if (new_ctx) {
636 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
637 if (ret)
638 goto out;
640 conf = &new_ctx->conf;
641 list_add(&sdata->assigned_chanctx_list,
642 &new_ctx->assigned_vifs);
645 out:
646 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
648 sdata->vif.bss_conf.idle = !conf;
650 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
651 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
652 ieee80211_recalc_smps_chanctx(local, curr_ctx);
653 ieee80211_recalc_radar_chanctx(local, curr_ctx);
654 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
657 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
658 ieee80211_recalc_txpower(sdata, false);
659 ieee80211_recalc_chanctx_min_def(local, new_ctx);
662 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
663 sdata->vif.type != NL80211_IFTYPE_MONITOR)
664 ieee80211_bss_info_change_notify(sdata,
665 BSS_CHANGED_IDLE);
667 ieee80211_check_fast_xmit_iface(sdata);
669 return ret;
672 void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
673 struct ieee80211_chanctx *chanctx)
675 struct ieee80211_sub_if_data *sdata;
676 u8 rx_chains_static, rx_chains_dynamic;
678 lockdep_assert_held(&local->chanctx_mtx);
680 rx_chains_static = 1;
681 rx_chains_dynamic = 1;
683 rcu_read_lock();
684 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
685 u8 needed_static, needed_dynamic;
687 if (!ieee80211_sdata_running(sdata))
688 continue;
690 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
691 &chanctx->conf)
692 continue;
694 switch (sdata->vif.type) {
695 case NL80211_IFTYPE_P2P_DEVICE:
696 continue;
697 case NL80211_IFTYPE_STATION:
698 if (!sdata->u.mgd.associated)
699 continue;
700 break;
701 case NL80211_IFTYPE_AP_VLAN:
702 continue;
703 case NL80211_IFTYPE_AP:
704 case NL80211_IFTYPE_ADHOC:
705 case NL80211_IFTYPE_WDS:
706 case NL80211_IFTYPE_MESH_POINT:
707 case NL80211_IFTYPE_OCB:
708 break;
709 default:
710 WARN_ON_ONCE(1);
713 switch (sdata->smps_mode) {
714 default:
715 WARN_ONCE(1, "Invalid SMPS mode %d\n",
716 sdata->smps_mode);
717 /* fall through */
718 case IEEE80211_SMPS_OFF:
719 needed_static = sdata->needed_rx_chains;
720 needed_dynamic = sdata->needed_rx_chains;
721 break;
722 case IEEE80211_SMPS_DYNAMIC:
723 needed_static = 1;
724 needed_dynamic = sdata->needed_rx_chains;
725 break;
726 case IEEE80211_SMPS_STATIC:
727 needed_static = 1;
728 needed_dynamic = 1;
729 break;
732 rx_chains_static = max(rx_chains_static, needed_static);
733 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
736 /* Disable SMPS for the monitor interface */
737 sdata = rcu_dereference(local->monitor_sdata);
738 if (sdata &&
739 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
740 rx_chains_dynamic = rx_chains_static = local->rx_chains;
742 rcu_read_unlock();
744 if (!local->use_chanctx) {
745 if (rx_chains_static > 1)
746 local->smps_mode = IEEE80211_SMPS_OFF;
747 else if (rx_chains_dynamic > 1)
748 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
749 else
750 local->smps_mode = IEEE80211_SMPS_STATIC;
751 ieee80211_hw_config(local, 0);
754 if (rx_chains_static == chanctx->conf.rx_chains_static &&
755 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
756 return;
758 chanctx->conf.rx_chains_static = rx_chains_static;
759 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
760 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
763 static void
764 __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
765 bool clear)
767 struct ieee80211_local *local __maybe_unused = sdata->local;
768 struct ieee80211_sub_if_data *vlan;
769 struct ieee80211_chanctx_conf *conf;
771 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
772 return;
774 lockdep_assert_held(&local->mtx);
776 /* Check that conf exists, even when clearing this function
777 * must be called with the AP's channel context still there
778 * as it would otherwise cause VLANs to have an invalid
779 * channel context pointer for a while, possibly pointing
780 * to a channel context that has already been freed.
782 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
783 lockdep_is_held(&local->chanctx_mtx));
784 WARN_ON(!conf);
786 if (clear)
787 conf = NULL;
789 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
790 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
793 void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
794 bool clear)
796 struct ieee80211_local *local = sdata->local;
798 mutex_lock(&local->chanctx_mtx);
800 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
802 mutex_unlock(&local->chanctx_mtx);
805 int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
807 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
809 lockdep_assert_held(&sdata->local->chanctx_mtx);
811 if (WARN_ON(!ctx))
812 return -EINVAL;
814 list_del(&sdata->reserved_chanctx_list);
815 sdata->reserved_chanctx = NULL;
817 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
818 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
819 if (WARN_ON(!ctx->replace_ctx))
820 return -EINVAL;
822 WARN_ON(ctx->replace_ctx->replace_state !=
823 IEEE80211_CHANCTX_WILL_BE_REPLACED);
824 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
826 ctx->replace_ctx->replace_ctx = NULL;
827 ctx->replace_ctx->replace_state =
828 IEEE80211_CHANCTX_REPLACE_NONE;
830 list_del_rcu(&ctx->list);
831 kfree_rcu(ctx, rcu_head);
832 } else {
833 ieee80211_free_chanctx(sdata->local, ctx);
837 return 0;
840 int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
841 const struct cfg80211_chan_def *chandef,
842 enum ieee80211_chanctx_mode mode,
843 bool radar_required)
845 struct ieee80211_local *local = sdata->local;
846 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
848 lockdep_assert_held(&local->chanctx_mtx);
850 curr_ctx = ieee80211_vif_get_chanctx(sdata);
851 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
852 return -ENOTSUPP;
854 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
855 if (!new_ctx) {
856 if (ieee80211_can_create_new_chanctx(local)) {
857 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
858 if (IS_ERR(new_ctx))
859 return PTR_ERR(new_ctx);
860 } else {
861 if (!curr_ctx ||
862 (curr_ctx->replace_state ==
863 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
864 !list_empty(&curr_ctx->reserved_vifs)) {
866 * Another vif already requested this context
867 * for a reservation. Find another one hoping
868 * all vifs assigned to it will also switch
869 * soon enough.
871 * TODO: This needs a little more work as some
872 * cases (more than 2 chanctx capable devices)
873 * may fail which could otherwise succeed
874 * provided some channel context juggling was
875 * performed.
877 * Consider ctx1..3, vif1..6, each ctx has 2
878 * vifs. vif1 and vif2 from ctx1 request new
879 * different chandefs starting 2 in-place
880 * reserations with ctx4 and ctx5 replacing
881 * ctx1 and ctx2 respectively. Next vif5 and
882 * vif6 from ctx3 reserve ctx4. If vif3 and
883 * vif4 remain on ctx2 as they are then this
884 * fails unless `replace_ctx` from ctx5 is
885 * replaced with ctx3.
887 list_for_each_entry(ctx, &local->chanctx_list,
888 list) {
889 if (ctx->replace_state !=
890 IEEE80211_CHANCTX_REPLACE_NONE)
891 continue;
893 if (!list_empty(&ctx->reserved_vifs))
894 continue;
896 curr_ctx = ctx;
897 break;
902 * If that's true then all available contexts already
903 * have reservations and cannot be used.
905 if (!curr_ctx ||
906 (curr_ctx->replace_state ==
907 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
908 !list_empty(&curr_ctx->reserved_vifs))
909 return -EBUSY;
911 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
912 if (!new_ctx)
913 return -ENOMEM;
915 new_ctx->replace_ctx = curr_ctx;
916 new_ctx->replace_state =
917 IEEE80211_CHANCTX_REPLACES_OTHER;
919 curr_ctx->replace_ctx = new_ctx;
920 curr_ctx->replace_state =
921 IEEE80211_CHANCTX_WILL_BE_REPLACED;
923 list_add_rcu(&new_ctx->list, &local->chanctx_list);
927 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
928 sdata->reserved_chanctx = new_ctx;
929 sdata->reserved_chandef = *chandef;
930 sdata->reserved_radar_required = radar_required;
931 sdata->reserved_ready = false;
933 return 0;
936 static void
937 ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
939 switch (sdata->vif.type) {
940 case NL80211_IFTYPE_ADHOC:
941 case NL80211_IFTYPE_AP:
942 case NL80211_IFTYPE_MESH_POINT:
943 case NL80211_IFTYPE_OCB:
944 ieee80211_queue_work(&sdata->local->hw,
945 &sdata->csa_finalize_work);
946 break;
947 case NL80211_IFTYPE_STATION:
948 ieee80211_queue_work(&sdata->local->hw,
949 &sdata->u.mgd.chswitch_work);
950 break;
951 case NL80211_IFTYPE_UNSPECIFIED:
952 case NL80211_IFTYPE_AP_VLAN:
953 case NL80211_IFTYPE_WDS:
954 case NL80211_IFTYPE_MONITOR:
955 case NL80211_IFTYPE_P2P_CLIENT:
956 case NL80211_IFTYPE_P2P_GO:
957 case NL80211_IFTYPE_P2P_DEVICE:
958 case NUM_NL80211_IFTYPES:
959 WARN_ON(1);
960 break;
964 static void
965 ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
966 const struct cfg80211_chan_def *chandef)
968 struct ieee80211_sub_if_data *vlan;
970 sdata->vif.bss_conf.chandef = *chandef;
972 if (sdata->vif.type != NL80211_IFTYPE_AP)
973 return;
975 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
976 vlan->vif.bss_conf.chandef = *chandef;
979 static int
980 ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
982 struct ieee80211_local *local = sdata->local;
983 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
984 struct ieee80211_chanctx *old_ctx, *new_ctx;
985 const struct cfg80211_chan_def *chandef;
986 u32 changed = 0;
987 int err;
989 lockdep_assert_held(&local->mtx);
990 lockdep_assert_held(&local->chanctx_mtx);
992 new_ctx = sdata->reserved_chanctx;
993 old_ctx = ieee80211_vif_get_chanctx(sdata);
995 if (WARN_ON(!sdata->reserved_ready))
996 return -EBUSY;
998 if (WARN_ON(!new_ctx))
999 return -EINVAL;
1001 if (WARN_ON(!old_ctx))
1002 return -EINVAL;
1004 if (WARN_ON(new_ctx->replace_state ==
1005 IEEE80211_CHANCTX_REPLACES_OTHER))
1006 return -EINVAL;
1008 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1009 &sdata->reserved_chandef);
1010 if (WARN_ON(!chandef))
1011 return -EINVAL;
1013 ieee80211_change_chanctx(local, new_ctx, chandef);
1015 vif_chsw[0].vif = &sdata->vif;
1016 vif_chsw[0].old_ctx = &old_ctx->conf;
1017 vif_chsw[0].new_ctx = &new_ctx->conf;
1019 list_del(&sdata->reserved_chanctx_list);
1020 sdata->reserved_chanctx = NULL;
1022 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1023 CHANCTX_SWMODE_REASSIGN_VIF);
1024 if (err) {
1025 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1026 ieee80211_free_chanctx(local, new_ctx);
1028 goto out;
1031 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1032 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1034 if (sdata->vif.type == NL80211_IFTYPE_AP)
1035 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1037 ieee80211_check_fast_xmit_iface(sdata);
1039 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1040 ieee80211_free_chanctx(local, old_ctx);
1042 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
1043 changed = BSS_CHANGED_BANDWIDTH;
1045 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1047 ieee80211_recalc_smps_chanctx(local, new_ctx);
1048 ieee80211_recalc_radar_chanctx(local, new_ctx);
1049 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1051 if (changed)
1052 ieee80211_bss_info_change_notify(sdata, changed);
1054 out:
1055 ieee80211_vif_chanctx_reservation_complete(sdata);
1056 return err;
1059 static int
1060 ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1062 struct ieee80211_local *local = sdata->local;
1063 struct ieee80211_chanctx *old_ctx, *new_ctx;
1064 const struct cfg80211_chan_def *chandef;
1065 int err;
1067 old_ctx = ieee80211_vif_get_chanctx(sdata);
1068 new_ctx = sdata->reserved_chanctx;
1070 if (WARN_ON(!sdata->reserved_ready))
1071 return -EINVAL;
1073 if (WARN_ON(old_ctx))
1074 return -EINVAL;
1076 if (WARN_ON(!new_ctx))
1077 return -EINVAL;
1079 if (WARN_ON(new_ctx->replace_state ==
1080 IEEE80211_CHANCTX_REPLACES_OTHER))
1081 return -EINVAL;
1083 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1084 &sdata->reserved_chandef);
1085 if (WARN_ON(!chandef))
1086 return -EINVAL;
1088 ieee80211_change_chanctx(local, new_ctx, chandef);
1090 list_del(&sdata->reserved_chanctx_list);
1091 sdata->reserved_chanctx = NULL;
1093 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1094 if (err) {
1095 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1096 ieee80211_free_chanctx(local, new_ctx);
1098 goto out;
1101 out:
1102 ieee80211_vif_chanctx_reservation_complete(sdata);
1103 return err;
1106 static bool
1107 ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1109 struct ieee80211_chanctx *old_ctx, *new_ctx;
1111 lockdep_assert_held(&sdata->local->chanctx_mtx);
1113 new_ctx = sdata->reserved_chanctx;
1114 old_ctx = ieee80211_vif_get_chanctx(sdata);
1116 if (!old_ctx)
1117 return false;
1119 if (WARN_ON(!new_ctx))
1120 return false;
1122 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1123 return false;
1125 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1126 return false;
1128 return true;
1131 static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1132 struct ieee80211_chanctx *new_ctx)
1134 const struct cfg80211_chan_def *chandef;
1136 lockdep_assert_held(&local->mtx);
1137 lockdep_assert_held(&local->chanctx_mtx);
1139 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1140 if (WARN_ON(!chandef))
1141 return -EINVAL;
1143 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1144 local->_oper_chandef = *chandef;
1145 ieee80211_hw_config(local, 0);
1147 return 0;
1150 static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1151 int n_vifs)
1153 struct ieee80211_vif_chanctx_switch *vif_chsw;
1154 struct ieee80211_sub_if_data *sdata;
1155 struct ieee80211_chanctx *ctx, *old_ctx;
1156 int i, err;
1158 lockdep_assert_held(&local->mtx);
1159 lockdep_assert_held(&local->chanctx_mtx);
1161 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1162 if (!vif_chsw)
1163 return -ENOMEM;
1165 i = 0;
1166 list_for_each_entry(ctx, &local->chanctx_list, list) {
1167 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1168 continue;
1170 if (WARN_ON(!ctx->replace_ctx)) {
1171 err = -EINVAL;
1172 goto out;
1175 list_for_each_entry(sdata, &ctx->reserved_vifs,
1176 reserved_chanctx_list) {
1177 if (!ieee80211_vif_has_in_place_reservation(
1178 sdata))
1179 continue;
1181 old_ctx = ieee80211_vif_get_chanctx(sdata);
1182 vif_chsw[i].vif = &sdata->vif;
1183 vif_chsw[i].old_ctx = &old_ctx->conf;
1184 vif_chsw[i].new_ctx = &ctx->conf;
1186 i++;
1190 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1191 CHANCTX_SWMODE_SWAP_CONTEXTS);
1193 out:
1194 kfree(vif_chsw);
1195 return err;
1198 static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1200 struct ieee80211_chanctx *ctx;
1201 int err;
1203 lockdep_assert_held(&local->mtx);
1204 lockdep_assert_held(&local->chanctx_mtx);
1206 list_for_each_entry(ctx, &local->chanctx_list, list) {
1207 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1208 continue;
1210 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1211 continue;
1213 ieee80211_del_chanctx(local, ctx->replace_ctx);
1214 err = ieee80211_add_chanctx(local, ctx);
1215 if (err)
1216 goto err;
1219 return 0;
1221 err:
1222 WARN_ON(ieee80211_add_chanctx(local, ctx));
1223 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1224 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1225 continue;
1227 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1228 continue;
1230 ieee80211_del_chanctx(local, ctx);
1231 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1234 return err;
1237 static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1239 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1240 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1241 struct ieee80211_chanctx *new_ctx = NULL;
1242 int i, err, n_assigned, n_reserved, n_ready;
1243 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1245 lockdep_assert_held(&local->mtx);
1246 lockdep_assert_held(&local->chanctx_mtx);
1249 * If there are 2 independent pairs of channel contexts performing
1250 * cross-switch of their vifs this code will still wait until both are
1251 * ready even though it could be possible to switch one before the
1252 * other is ready.
1254 * For practical reasons and code simplicity just do a single huge
1255 * switch.
1259 * Verify if the reservation is still feasible.
1260 * - if it's not then disconnect
1261 * - if it is but not all vifs necessary are ready then defer
1264 list_for_each_entry(ctx, &local->chanctx_list, list) {
1265 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1266 continue;
1268 if (WARN_ON(!ctx->replace_ctx)) {
1269 err = -EINVAL;
1270 goto err;
1273 if (!local->use_chanctx)
1274 new_ctx = ctx;
1276 n_ctx++;
1278 n_assigned = 0;
1279 n_reserved = 0;
1280 n_ready = 0;
1282 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1283 assigned_chanctx_list) {
1284 n_assigned++;
1285 if (sdata->reserved_chanctx) {
1286 n_reserved++;
1287 if (sdata->reserved_ready)
1288 n_ready++;
1292 if (n_assigned != n_reserved) {
1293 if (n_ready == n_reserved) {
1294 wiphy_info(local->hw.wiphy,
1295 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1296 err = -EBUSY;
1297 goto err;
1300 return -EAGAIN;
1303 ctx->conf.radar_enabled = false;
1304 list_for_each_entry(sdata, &ctx->reserved_vifs,
1305 reserved_chanctx_list) {
1306 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1307 !sdata->reserved_ready)
1308 return -EAGAIN;
1310 old_ctx = ieee80211_vif_get_chanctx(sdata);
1311 if (old_ctx) {
1312 if (old_ctx->replace_state ==
1313 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1314 n_vifs_switch++;
1315 else
1316 n_vifs_assign++;
1317 } else {
1318 n_vifs_ctxless++;
1321 if (sdata->reserved_radar_required)
1322 ctx->conf.radar_enabled = true;
1326 if (WARN_ON(n_ctx == 0) ||
1327 WARN_ON(n_vifs_switch == 0 &&
1328 n_vifs_assign == 0 &&
1329 n_vifs_ctxless == 0) ||
1330 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1331 WARN_ON(!new_ctx && !local->use_chanctx)) {
1332 err = -EINVAL;
1333 goto err;
1337 * All necessary vifs are ready. Perform the switch now depending on
1338 * reservations and driver capabilities.
1341 if (local->use_chanctx) {
1342 if (n_vifs_switch > 0) {
1343 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1344 if (err)
1345 goto err;
1348 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1349 err = ieee80211_chsw_switch_ctxs(local);
1350 if (err)
1351 goto err;
1353 } else {
1354 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1355 if (err)
1356 goto err;
1360 * Update all structures, values and pointers to point to new channel
1361 * context(s).
1364 i = 0;
1365 list_for_each_entry(ctx, &local->chanctx_list, list) {
1366 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1367 continue;
1369 if (WARN_ON(!ctx->replace_ctx)) {
1370 err = -EINVAL;
1371 goto err;
1374 list_for_each_entry(sdata, &ctx->reserved_vifs,
1375 reserved_chanctx_list) {
1376 u32 changed = 0;
1378 if (!ieee80211_vif_has_in_place_reservation(sdata))
1379 continue;
1381 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1383 if (sdata->vif.type == NL80211_IFTYPE_AP)
1384 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1385 false);
1387 ieee80211_check_fast_xmit_iface(sdata);
1389 sdata->radar_required = sdata->reserved_radar_required;
1391 if (sdata->vif.bss_conf.chandef.width !=
1392 sdata->reserved_chandef.width)
1393 changed = BSS_CHANGED_BANDWIDTH;
1395 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
1396 if (changed)
1397 ieee80211_bss_info_change_notify(sdata,
1398 changed);
1400 ieee80211_recalc_txpower(sdata, false);
1403 ieee80211_recalc_chanctx_chantype(local, ctx);
1404 ieee80211_recalc_smps_chanctx(local, ctx);
1405 ieee80211_recalc_radar_chanctx(local, ctx);
1406 ieee80211_recalc_chanctx_min_def(local, ctx);
1408 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1409 reserved_chanctx_list) {
1410 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1411 continue;
1413 list_del(&sdata->reserved_chanctx_list);
1414 list_move(&sdata->assigned_chanctx_list,
1415 &ctx->assigned_vifs);
1416 sdata->reserved_chanctx = NULL;
1418 ieee80211_vif_chanctx_reservation_complete(sdata);
1422 * This context might have been a dependency for an already
1423 * ready re-assign reservation interface that was deferred. Do
1424 * not propagate error to the caller though. The in-place
1425 * reservation for originally requested interface has already
1426 * succeeded at this point.
1428 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1429 reserved_chanctx_list) {
1430 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1431 sdata)))
1432 continue;
1434 if (WARN_ON(sdata->reserved_chanctx != ctx))
1435 continue;
1437 if (!sdata->reserved_ready)
1438 continue;
1440 if (ieee80211_vif_get_chanctx(sdata))
1441 err = ieee80211_vif_use_reserved_reassign(
1442 sdata);
1443 else
1444 err = ieee80211_vif_use_reserved_assign(sdata);
1446 if (err) {
1447 sdata_info(sdata,
1448 "failed to finalize (re-)assign reservation (err=%d)\n",
1449 err);
1450 ieee80211_vif_unreserve_chanctx(sdata);
1451 cfg80211_stop_iface(local->hw.wiphy,
1452 &sdata->wdev,
1453 GFP_KERNEL);
1459 * Finally free old contexts
1462 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1463 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1464 continue;
1466 ctx->replace_ctx->replace_ctx = NULL;
1467 ctx->replace_ctx->replace_state =
1468 IEEE80211_CHANCTX_REPLACE_NONE;
1470 list_del_rcu(&ctx->list);
1471 kfree_rcu(ctx, rcu_head);
1474 return 0;
1476 err:
1477 list_for_each_entry(ctx, &local->chanctx_list, list) {
1478 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1479 continue;
1481 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1482 reserved_chanctx_list) {
1483 ieee80211_vif_unreserve_chanctx(sdata);
1484 ieee80211_vif_chanctx_reservation_complete(sdata);
1488 return err;
1491 static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1493 struct ieee80211_local *local = sdata->local;
1494 struct ieee80211_chanctx_conf *conf;
1495 struct ieee80211_chanctx *ctx;
1496 bool use_reserved_switch = false;
1498 lockdep_assert_held(&local->chanctx_mtx);
1500 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1501 lockdep_is_held(&local->chanctx_mtx));
1502 if (!conf)
1503 return;
1505 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1507 if (sdata->reserved_chanctx) {
1508 if (sdata->reserved_chanctx->replace_state ==
1509 IEEE80211_CHANCTX_REPLACES_OTHER &&
1510 ieee80211_chanctx_num_reserved(local,
1511 sdata->reserved_chanctx) > 1)
1512 use_reserved_switch = true;
1514 ieee80211_vif_unreserve_chanctx(sdata);
1517 ieee80211_assign_vif_chanctx(sdata, NULL);
1518 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1519 ieee80211_free_chanctx(local, ctx);
1521 sdata->radar_required = false;
1523 /* Unreserving may ready an in-place reservation. */
1524 if (use_reserved_switch)
1525 ieee80211_vif_use_reserved_switch(local);
1528 int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1529 const struct cfg80211_chan_def *chandef,
1530 enum ieee80211_chanctx_mode mode)
1532 struct ieee80211_local *local = sdata->local;
1533 struct ieee80211_chanctx *ctx;
1534 u8 radar_detect_width = 0;
1535 int ret;
1537 lockdep_assert_held(&local->mtx);
1539 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1541 mutex_lock(&local->chanctx_mtx);
1543 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1544 chandef,
1545 sdata->wdev.iftype);
1546 if (ret < 0)
1547 goto out;
1548 if (ret > 0)
1549 radar_detect_width = BIT(chandef->width);
1551 sdata->radar_required = ret;
1553 ret = ieee80211_check_combinations(sdata, chandef, mode,
1554 radar_detect_width);
1555 if (ret < 0)
1556 goto out;
1558 __ieee80211_vif_release_channel(sdata);
1560 ctx = ieee80211_find_chanctx(local, chandef, mode);
1561 if (!ctx)
1562 ctx = ieee80211_new_chanctx(local, chandef, mode);
1563 if (IS_ERR(ctx)) {
1564 ret = PTR_ERR(ctx);
1565 goto out;
1568 ieee80211_vif_update_chandef(sdata, chandef);
1570 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1571 if (ret) {
1572 /* if assign fails refcount stays the same */
1573 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1574 ieee80211_free_chanctx(local, ctx);
1575 goto out;
1578 ieee80211_recalc_smps_chanctx(local, ctx);
1579 ieee80211_recalc_radar_chanctx(local, ctx);
1580 out:
1581 if (ret)
1582 sdata->radar_required = false;
1584 mutex_unlock(&local->chanctx_mtx);
1585 return ret;
1588 int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1590 struct ieee80211_local *local = sdata->local;
1591 struct ieee80211_chanctx *new_ctx;
1592 struct ieee80211_chanctx *old_ctx;
1593 int err;
1595 lockdep_assert_held(&local->mtx);
1596 lockdep_assert_held(&local->chanctx_mtx);
1598 new_ctx = sdata->reserved_chanctx;
1599 old_ctx = ieee80211_vif_get_chanctx(sdata);
1601 if (WARN_ON(!new_ctx))
1602 return -EINVAL;
1604 if (WARN_ON(new_ctx->replace_state ==
1605 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1606 return -EINVAL;
1608 if (WARN_ON(sdata->reserved_ready))
1609 return -EINVAL;
1611 sdata->reserved_ready = true;
1613 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1614 if (old_ctx)
1615 err = ieee80211_vif_use_reserved_reassign(sdata);
1616 else
1617 err = ieee80211_vif_use_reserved_assign(sdata);
1619 if (err)
1620 return err;
1624 * In-place reservation may need to be finalized now either if:
1625 * a) sdata is taking part in the swapping itself and is the last one
1626 * b) sdata has switched with a re-assign reservation to an existing
1627 * context readying in-place switching of old_ctx
1629 * In case of (b) do not propagate the error up because the requested
1630 * sdata already switched successfully. Just spill an extra warning.
1631 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1632 * interfaces upon failure.
1634 if ((old_ctx &&
1635 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1636 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1637 err = ieee80211_vif_use_reserved_switch(local);
1638 if (err && err != -EAGAIN) {
1639 if (new_ctx->replace_state ==
1640 IEEE80211_CHANCTX_REPLACES_OTHER)
1641 return err;
1643 wiphy_info(local->hw.wiphy,
1644 "depending in-place reservation failed (err=%d)\n",
1645 err);
1649 return 0;
1652 int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1653 const struct cfg80211_chan_def *chandef,
1654 u32 *changed)
1656 struct ieee80211_local *local = sdata->local;
1657 struct ieee80211_chanctx_conf *conf;
1658 struct ieee80211_chanctx *ctx;
1659 const struct cfg80211_chan_def *compat;
1660 int ret;
1662 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1663 IEEE80211_CHAN_DISABLED))
1664 return -EINVAL;
1666 mutex_lock(&local->chanctx_mtx);
1667 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1668 ret = 0;
1669 goto out;
1672 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1673 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1674 ret = -EINVAL;
1675 goto out;
1678 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1679 lockdep_is_held(&local->chanctx_mtx));
1680 if (!conf) {
1681 ret = -EINVAL;
1682 goto out;
1685 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1687 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1688 if (!compat) {
1689 ret = -EINVAL;
1690 goto out;
1693 switch (ctx->replace_state) {
1694 case IEEE80211_CHANCTX_REPLACE_NONE:
1695 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1696 ret = -EBUSY;
1697 goto out;
1699 break;
1700 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1701 /* TODO: Perhaps the bandwidth change could be treated as a
1702 * reservation itself? */
1703 ret = -EBUSY;
1704 goto out;
1705 case IEEE80211_CHANCTX_REPLACES_OTHER:
1706 /* channel context that is going to replace another channel
1707 * context doesn't really exist and shouldn't be assigned
1708 * anywhere yet */
1709 WARN_ON(1);
1710 break;
1713 ieee80211_vif_update_chandef(sdata, chandef);
1715 ieee80211_recalc_chanctx_chantype(local, ctx);
1717 *changed |= BSS_CHANGED_BANDWIDTH;
1718 ret = 0;
1719 out:
1720 mutex_unlock(&local->chanctx_mtx);
1721 return ret;
1724 void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1726 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1728 lockdep_assert_held(&sdata->local->mtx);
1730 mutex_lock(&sdata->local->chanctx_mtx);
1731 __ieee80211_vif_release_channel(sdata);
1732 mutex_unlock(&sdata->local->chanctx_mtx);
1735 void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1737 struct ieee80211_local *local = sdata->local;
1738 struct ieee80211_sub_if_data *ap;
1739 struct ieee80211_chanctx_conf *conf;
1741 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1742 return;
1744 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1746 mutex_lock(&local->chanctx_mtx);
1748 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1749 lockdep_is_held(&local->chanctx_mtx));
1750 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1751 mutex_unlock(&local->chanctx_mtx);
1754 void ieee80211_iter_chan_contexts_atomic(
1755 struct ieee80211_hw *hw,
1756 void (*iter)(struct ieee80211_hw *hw,
1757 struct ieee80211_chanctx_conf *chanctx_conf,
1758 void *data),
1759 void *iter_data)
1761 struct ieee80211_local *local = hw_to_local(hw);
1762 struct ieee80211_chanctx *ctx;
1764 rcu_read_lock();
1765 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
1766 if (ctx->driver_present)
1767 iter(hw, &ctx->conf, iter_data);
1768 rcu_read_unlock();
1770 EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);