mac80211_hwsim: fix error handling in tx_frame_nl
[linux-2.6-block.git] / net / mac80211 / chan.c
CommitLineData
f444de05
JB
1/*
2 * mac80211 - channel management
3 */
4
0aaffa9b 5#include <linux/nl80211.h>
3448c005 6#include <linux/export.h>
4d76d21b 7#include <linux/rtnetlink.h>
3117bbdb 8#include <net/cfg80211.h>
f444de05 9#include "ieee80211_i.h"
35f2fce9 10#include "driver-ops.h"
f444de05 11
c0166da9
MK
12static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
13 struct ieee80211_chanctx *ctx)
14{
15 struct ieee80211_sub_if_data *sdata;
16 int num = 0;
17
18 lockdep_assert_held(&local->chanctx_mtx);
19
20 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
21 num++;
22
23 return num;
24}
25
26static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
27 struct ieee80211_chanctx *ctx)
28{
29 struct ieee80211_sub_if_data *sdata;
30 int num = 0;
31
32 lockdep_assert_held(&local->chanctx_mtx);
33
34 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
35 num++;
36
37 return num;
38}
39
40int ieee80211_chanctx_refcount(struct ieee80211_local *local,
41 struct ieee80211_chanctx *ctx)
42{
43 return ieee80211_chanctx_num_assigned(local, ctx) +
44 ieee80211_chanctx_num_reserved(local, ctx);
45}
46
c2b90ad8
MK
47static int ieee80211_num_chanctx(struct ieee80211_local *local)
48{
49 struct ieee80211_chanctx *ctx;
50 int num = 0;
51
52 lockdep_assert_held(&local->chanctx_mtx);
53
54 list_for_each_entry(ctx, &local->chanctx_list, list)
55 num++;
56
57 return num;
58}
59
60static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
61{
62 lockdep_assert_held(&local->chanctx_mtx);
63 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
64}
65
5bcae31d
MK
66static struct ieee80211_chanctx *
67ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
68{
1d4cc30c 69 struct ieee80211_local *local __maybe_unused = sdata->local;
5bcae31d
MK
70 struct ieee80211_chanctx_conf *conf;
71
72 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
73 lockdep_is_held(&local->chanctx_mtx));
74 if (!conf)
75 return NULL;
76
77 return container_of(conf, struct ieee80211_chanctx, conf);
78}
79
0288157b
MK
80static const struct cfg80211_chan_def *
81ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
82 struct ieee80211_chanctx *ctx,
83 const struct cfg80211_chan_def *compat)
84{
85 struct ieee80211_sub_if_data *sdata;
86
87 lockdep_assert_held(&local->chanctx_mtx);
88
89 list_for_each_entry(sdata, &ctx->reserved_vifs,
90 reserved_chanctx_list) {
91 if (!compat)
92 compat = &sdata->reserved_chandef;
93
94 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
95 compat);
96 if (!compat)
97 break;
98 }
99
100 return compat;
101}
102
13f348a8
MK
103static const struct cfg80211_chan_def *
104ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
105 struct ieee80211_chanctx *ctx,
106 const struct cfg80211_chan_def *compat)
107{
108 struct ieee80211_sub_if_data *sdata;
109
110 lockdep_assert_held(&local->chanctx_mtx);
111
112 list_for_each_entry(sdata, &ctx->assigned_vifs,
113 assigned_chanctx_list) {
114 if (sdata->reserved_chanctx != NULL)
115 continue;
116
117 if (!compat)
118 compat = &sdata->vif.bss_conf.chandef;
119
120 compat = cfg80211_chandef_compatible(
121 &sdata->vif.bss_conf.chandef, compat);
122 if (!compat)
123 break;
124 }
125
126 return compat;
127}
128
129static const struct cfg80211_chan_def *
130ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
131 struct ieee80211_chanctx *ctx,
132 const struct cfg80211_chan_def *compat)
133{
134 lockdep_assert_held(&local->chanctx_mtx);
135
136 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
137 if (!compat)
138 return NULL;
139
140 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
141 if (!compat)
142 return NULL;
143
144 return compat;
145}
146
147static bool
148ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
149 struct ieee80211_chanctx *ctx,
150 const struct cfg80211_chan_def *def)
151{
152 lockdep_assert_held(&local->chanctx_mtx);
153
154 if (ieee80211_chanctx_combined_chandef(local, ctx, def))
155 return true;
156
157 if (!list_empty(&ctx->reserved_vifs) &&
158 ieee80211_chanctx_reserved_chandef(local, ctx, def))
159 return true;
160
161 return false;
162}
163
164static struct ieee80211_chanctx *
165ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
166 const struct cfg80211_chan_def *chandef,
167 enum ieee80211_chanctx_mode mode)
168{
169 struct ieee80211_chanctx *ctx;
170
171 lockdep_assert_held(&local->chanctx_mtx);
172
173 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
174 return NULL;
175
176 list_for_each_entry(ctx, &local->chanctx_list, list) {
5bcae31d
MK
177 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
178 continue;
179
13f348a8
MK
180 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
181 continue;
182
183 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
184 chandef))
185 continue;
186
187 return ctx;
188 }
189
190 return NULL;
191}
192
21f659bf
EP
193static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
194{
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:
206 /*
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).
214 */
215 return NL80211_CHAN_WIDTH_160;
216 default:
217 WARN_ON(1);
218 return NL80211_CHAN_WIDTH_20;
219 }
220}
221
222static enum nl80211_chan_width
223ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
224{
225 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
226 struct sta_info *sta;
227
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;
233
234 if (!sta->uploaded)
235 continue;
236
237 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
238 }
239 rcu_read_unlock();
240
241 return max_bw;
242}
243
244static enum nl80211_chan_width
245ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
246 struct ieee80211_chanctx_conf *conf)
247{
248 struct ieee80211_sub_if_data *sdata;
249 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
250
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;
255
256 if (!ieee80211_sdata_running(sdata))
257 continue;
258
259 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
260 continue;
261
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:
6e0bd6c3 273 case NL80211_IFTYPE_OCB:
21f659bf
EP
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);
282 }
283 max_bw = max(max_bw, width);
284 }
1c37a72c
EP
285
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);
290
21f659bf
EP
291 rcu_read_unlock();
292
293 return max_bw;
294}
295
296/*
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.
300 */
301void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
302 struct ieee80211_chanctx *ctx)
303{
304 enum nl80211_chan_width max_bw;
305 struct cfg80211_chan_def min_def;
306
307 lockdep_assert_held(&local->chanctx_mtx);
308
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;
315 }
316
317 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
318
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);
323
324 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
325 return;
326
327 ctx->conf.min_def = min_def;
328 if (!ctx->driver_present)
329 return;
330
331 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
332}
333
18942d3b 334static void ieee80211_change_chanctx(struct ieee80211_local *local,
4bf88530
JB
335 struct ieee80211_chanctx *ctx,
336 const struct cfg80211_chan_def *chandef)
23a85b45 337{
4bf88530 338 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
e89a96f5 339 return;
0aaffa9b 340
4bf88530
JB
341 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
342
343 ctx->conf.def = *chandef;
344 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
21f659bf 345 ieee80211_recalc_chanctx_min_def(local, ctx);
55de908a
JB
346
347 if (!local->use_chanctx) {
675a0b04 348 local->_oper_chandef = *chandef;
55de908a
JB
349 ieee80211_hw_config(local, 0);
350 }
0aaffa9b 351}
d01a1e65
MK
352
353static struct ieee80211_chanctx *
354ieee80211_find_chanctx(struct ieee80211_local *local,
4bf88530 355 const struct cfg80211_chan_def *chandef,
d01a1e65
MK
356 enum ieee80211_chanctx_mode mode)
357{
358 struct ieee80211_chanctx *ctx;
359
360 lockdep_assert_held(&local->chanctx_mtx);
361
362 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
363 return NULL;
d01a1e65
MK
364
365 list_for_each_entry(ctx, &local->chanctx_list, list) {
4bf88530 366 const struct cfg80211_chan_def *compat;
e89a96f5 367
5bcae31d
MK
368 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
369 continue;
370
d01a1e65
MK
371 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
372 continue;
4bf88530
JB
373
374 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
375 if (!compat)
d01a1e65
MK
376 continue;
377
0288157b
MK
378 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
379 compat);
380 if (!compat)
381 continue;
382
18942d3b 383 ieee80211_change_chanctx(local, ctx, compat);
e89a96f5 384
d01a1e65
MK
385 return ctx;
386 }
387
388 return NULL;
389}
390
5cbc95a7 391bool ieee80211_is_radar_required(struct ieee80211_local *local)
e4746851
SW
392{
393 struct ieee80211_sub_if_data *sdata;
394
cc901de1
MK
395 lockdep_assert_held(&local->mtx);
396
e4746851
SW
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;
402 }
403 }
404 rcu_read_unlock();
405
406 return false;
407}
408
e7f2337a
EP
409static bool
410ieee80211_chanctx_radar_required(struct ieee80211_local *local,
411 struct ieee80211_chanctx *ctx)
412{
413 struct ieee80211_chanctx_conf *conf = &ctx->conf;
414 struct ieee80211_sub_if_data *sdata;
415 bool required = false;
416
417 lockdep_assert_held(&local->chanctx_mtx);
418 lockdep_assert_held(&local->mtx);
419
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;
428
429 required = true;
430 break;
431 }
432 rcu_read_unlock();
433
434 return required;
435}
436
d01a1e65 437static struct ieee80211_chanctx *
ed68ebca
MK
438ieee80211_alloc_chanctx(struct ieee80211_local *local,
439 const struct cfg80211_chan_def *chandef,
440 enum ieee80211_chanctx_mode mode)
d01a1e65
MK
441{
442 struct ieee80211_chanctx *ctx;
443
444 lockdep_assert_held(&local->chanctx_mtx);
445
446 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
447 if (!ctx)
ed68ebca 448 return NULL;
d01a1e65 449
484298ad 450 INIT_LIST_HEAD(&ctx->assigned_vifs);
e3afb920 451 INIT_LIST_HEAD(&ctx->reserved_vifs);
4bf88530 452 ctx->conf.def = *chandef;
04ecd257
JB
453 ctx->conf.rx_chains_static = 1;
454 ctx->conf.rx_chains_dynamic = 1;
d01a1e65 455 ctx->mode = mode;
e7f2337a 456 ctx->conf.radar_enabled = false;
21f659bf 457 ieee80211_recalc_chanctx_min_def(local, ctx);
ed68ebca
MK
458
459 return ctx;
460}
461
462static int ieee80211_add_chanctx(struct ieee80211_local *local,
463 struct ieee80211_chanctx *ctx)
464{
465 u32 changed;
466 int err;
467
468 lockdep_assert_held(&local->mtx);
469 lockdep_assert_held(&local->chanctx_mtx);
470
e4746851
SW
471 if (!local->use_chanctx)
472 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
d01a1e65 473
382a103b
JB
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);
478
55de908a 479 if (!local->use_chanctx) {
ed68ebca 480 local->_oper_chandef = ctx->conf.def;
9b4816f5 481 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
55de908a
JB
482 } else {
483 err = drv_add_chanctx(local, ctx);
484 if (err) {
382a103b 485 ieee80211_recalc_idle(local);
ed68ebca 486 return err;
55de908a 487 }
35f2fce9
MK
488 }
489
ed68ebca
MK
490 return 0;
491}
492
493static struct ieee80211_chanctx *
494ieee80211_new_chanctx(struct ieee80211_local *local,
495 const struct cfg80211_chan_def *chandef,
496 enum ieee80211_chanctx_mode mode)
497{
498 struct ieee80211_chanctx *ctx;
499 int err;
d01a1e65 500
ed68ebca
MK
501 lockdep_assert_held(&local->mtx);
502 lockdep_assert_held(&local->chanctx_mtx);
d01a1e65 503
ed68ebca
MK
504 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
505 if (!ctx)
506 return ERR_PTR(-ENOMEM);
507
508 err = ieee80211_add_chanctx(local, ctx);
509 if (err) {
510 kfree(ctx);
511 return ERR_PTR(err);
512 }
513
514 list_add_rcu(&ctx->list, &local->chanctx_list);
d01a1e65
MK
515 return ctx;
516}
517
1f0d54cd
MK
518static void ieee80211_del_chanctx(struct ieee80211_local *local,
519 struct ieee80211_chanctx *ctx)
d01a1e65
MK
520{
521 lockdep_assert_held(&local->chanctx_mtx);
522
55de908a 523 if (!local->use_chanctx) {
675a0b04
KB
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;
e4746851
SW
528
529 /* NOTE: Disabling radar is only valid here for
530 * single channel context. To be sure, check it ...
531 */
1f0d54cd
MK
532 WARN_ON(local->hw.conf.radar_enabled &&
533 !list_empty(&local->chanctx_list));
534
e4746851
SW
535 local->hw.conf.radar_enabled = false;
536
9b4816f5 537 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
55de908a
JB
538 } else {
539 drv_remove_chanctx(local, ctx);
540 }
35f2fce9 541
fd0f979a 542 ieee80211_recalc_idle(local);
d01a1e65
MK
543}
544
1f0d54cd
MK
545static void ieee80211_free_chanctx(struct ieee80211_local *local,
546 struct ieee80211_chanctx *ctx)
d01a1e65 547{
d01a1e65
MK
548 lockdep_assert_held(&local->chanctx_mtx);
549
c0166da9 550 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
5bbe754d 551
1f0d54cd
MK
552 list_del_rcu(&ctx->list);
553 ieee80211_del_chanctx(local, ctx);
554 kfree_rcu(ctx, rcu_head);
d01a1e65
MK
555}
556
4bf88530
JB
557static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
558 struct ieee80211_chanctx *ctx)
e89a96f5
MK
559{
560 struct ieee80211_chanctx_conf *conf = &ctx->conf;
561 struct ieee80211_sub_if_data *sdata;
4bf88530 562 const struct cfg80211_chan_def *compat = NULL;
e89a96f5
MK
563
564 lockdep_assert_held(&local->chanctx_mtx);
565
566 rcu_read_lock();
567 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4bf88530 568
e89a96f5
MK
569 if (!ieee80211_sdata_running(sdata))
570 continue;
571 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
572 continue;
0e67c136
FF
573 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
574 continue;
e89a96f5 575
4bf88530
JB
576 if (!compat)
577 compat = &sdata->vif.bss_conf.chandef;
578
579 compat = cfg80211_chandef_compatible(
580 &sdata->vif.bss_conf.chandef, compat);
a00f4f6e 581 if (WARN_ON_ONCE(!compat))
4bf88530 582 break;
e89a96f5
MK
583 }
584 rcu_read_unlock();
585
a00f4f6e 586 if (!compat)
4bf88530 587 return;
e89a96f5 588
18942d3b 589 ieee80211_change_chanctx(local, ctx, compat);
e89a96f5
MK
590}
591
367bbd10
JB
592static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
593 struct ieee80211_chanctx *chanctx)
594{
595 bool radar_enabled;
596
597 lockdep_assert_held(&local->chanctx_mtx);
5cbc95a7 598 /* for ieee80211_is_radar_required */
34a3740d 599 lockdep_assert_held(&local->mtx);
367bbd10 600
e7f2337a 601 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
367bbd10
JB
602
603 if (radar_enabled == chanctx->conf.radar_enabled)
604 return;
605
606 chanctx->conf.radar_enabled = radar_enabled;
367bbd10
JB
607
608 if (!local->use_chanctx) {
609 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
610 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
611 }
612
613 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
614}
615
77eeba97
LC
616static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
617 struct ieee80211_chanctx *new_ctx)
d01a1e65 618{
35f2fce9 619 struct ieee80211_local *local = sdata->local;
77eeba97
LC
620 struct ieee80211_chanctx_conf *conf;
621 struct ieee80211_chanctx *curr_ctx = NULL;
622 int ret = 0;
d01a1e65 623
77eeba97
LC
624 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
625 lockdep_is_held(&local->chanctx_mtx));
d01a1e65 626
77eeba97
LC
627 if (conf) {
628 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
35f2fce9 629
77eeba97
LC
630 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
631 conf = NULL;
484298ad 632 list_del(&sdata->assigned_chanctx_list);
77eeba97 633 }
5bbe754d 634
77eeba97
LC
635 if (new_ctx) {
636 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
637 if (ret)
638 goto out;
fd0f979a 639
77eeba97 640 conf = &new_ctx->conf;
484298ad
MK
641 list_add(&sdata->assigned_chanctx_list,
642 &new_ctx->assigned_vifs);
77eeba97 643 }
e89a96f5 644
77eeba97
LC
645out:
646 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
fd0f979a 647
77eeba97
LC
648 sdata->vif.bss_conf.idle = !conf;
649
c0166da9 650 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
77eeba97
LC
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);
04ecd257 655 }
e89a96f5 656
c0166da9 657 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
db82d8a9 658 ieee80211_recalc_txpower(sdata, false);
77eeba97 659 ieee80211_recalc_chanctx_min_def(local, new_ctx);
04ecd257 660 }
77eeba97
LC
661
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);
666
667 return ret;
d01a1e65
MK
668}
669
04ecd257
JB
670void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
671 struct ieee80211_chanctx *chanctx)
672{
673 struct ieee80211_sub_if_data *sdata;
674 u8 rx_chains_static, rx_chains_dynamic;
675
676 lockdep_assert_held(&local->chanctx_mtx);
677
678 rx_chains_static = 1;
679 rx_chains_dynamic = 1;
680
681 rcu_read_lock();
682 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
683 u8 needed_static, needed_dynamic;
684
685 if (!ieee80211_sdata_running(sdata))
686 continue;
687
688 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
689 &chanctx->conf)
690 continue;
691
692 switch (sdata->vif.type) {
693 case NL80211_IFTYPE_P2P_DEVICE:
694 continue;
695 case NL80211_IFTYPE_STATION:
696 if (!sdata->u.mgd.associated)
697 continue;
698 break;
699 case NL80211_IFTYPE_AP_VLAN:
700 continue;
701 case NL80211_IFTYPE_AP:
702 case NL80211_IFTYPE_ADHOC:
703 case NL80211_IFTYPE_WDS:
704 case NL80211_IFTYPE_MESH_POINT:
239281f8 705 case NL80211_IFTYPE_OCB:
04ecd257
JB
706 break;
707 default:
708 WARN_ON_ONCE(1);
709 }
710
711 switch (sdata->smps_mode) {
712 default:
713 WARN_ONCE(1, "Invalid SMPS mode %d\n",
714 sdata->smps_mode);
715 /* fall through */
716 case IEEE80211_SMPS_OFF:
717 needed_static = sdata->needed_rx_chains;
718 needed_dynamic = sdata->needed_rx_chains;
719 break;
720 case IEEE80211_SMPS_DYNAMIC:
721 needed_static = 1;
722 needed_dynamic = sdata->needed_rx_chains;
723 break;
724 case IEEE80211_SMPS_STATIC:
725 needed_static = 1;
726 needed_dynamic = 1;
727 break;
728 }
729
730 rx_chains_static = max(rx_chains_static, needed_static);
731 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
732 }
7b8a9cdd
IY
733
734 /* Disable SMPS for the monitor interface */
735 sdata = rcu_dereference(local->monitor_sdata);
736 if (sdata &&
737 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
738 rx_chains_dynamic = rx_chains_static = local->rx_chains;
739
04ecd257
JB
740 rcu_read_unlock();
741
742 if (!local->use_chanctx) {
743 if (rx_chains_static > 1)
744 local->smps_mode = IEEE80211_SMPS_OFF;
745 else if (rx_chains_dynamic > 1)
746 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
747 else
748 local->smps_mode = IEEE80211_SMPS_STATIC;
749 ieee80211_hw_config(local, 0);
750 }
751
752 if (rx_chains_static == chanctx->conf.rx_chains_static &&
753 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
754 return;
755
756 chanctx->conf.rx_chains_static = rx_chains_static;
757 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
758 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
759}
760
11335a55
LC
761static void
762__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
763 bool clear)
764{
33926eb7 765 struct ieee80211_local *local __maybe_unused = sdata->local;
11335a55
LC
766 struct ieee80211_sub_if_data *vlan;
767 struct ieee80211_chanctx_conf *conf;
768
769 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
770 return;
771
772 lockdep_assert_held(&local->mtx);
773
774 /* Check that conf exists, even when clearing this function
775 * must be called with the AP's channel context still there
776 * as it would otherwise cause VLANs to have an invalid
777 * channel context pointer for a while, possibly pointing
778 * to a channel context that has already been freed.
779 */
780 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
33926eb7 781 lockdep_is_held(&local->chanctx_mtx));
11335a55
LC
782 WARN_ON(!conf);
783
784 if (clear)
785 conf = NULL;
786
787 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
788 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
789}
790
791void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
792 bool clear)
793{
794 struct ieee80211_local *local = sdata->local;
795
796 mutex_lock(&local->chanctx_mtx);
797
798 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
799
800 mutex_unlock(&local->chanctx_mtx);
801}
802
803int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
804{
e3afb920
MK
805 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
806
11335a55
LC
807 lockdep_assert_held(&sdata->local->chanctx_mtx);
808
e3afb920 809 if (WARN_ON(!ctx))
73da7d5b
SW
810 return -EINVAL;
811
e3afb920 812 list_del(&sdata->reserved_chanctx_list);
11335a55
LC
813 sdata->reserved_chanctx = NULL;
814
5bcae31d
MK
815 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
816 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
817 if (WARN_ON(!ctx->replace_ctx))
818 return -EINVAL;
819
820 WARN_ON(ctx->replace_ctx->replace_state !=
821 IEEE80211_CHANCTX_WILL_BE_REPLACED);
822 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
823
824 ctx->replace_ctx->replace_ctx = NULL;
825 ctx->replace_ctx->replace_state =
826 IEEE80211_CHANCTX_REPLACE_NONE;
827
828 list_del_rcu(&ctx->list);
829 kfree_rcu(ctx, rcu_head);
830 } else {
831 ieee80211_free_chanctx(sdata->local, ctx);
832 }
833 }
e3afb920 834
11335a55
LC
835 return 0;
836}
837
838int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
839 const struct cfg80211_chan_def *chandef,
09332481
MK
840 enum ieee80211_chanctx_mode mode,
841 bool radar_required)
11335a55
LC
842{
843 struct ieee80211_local *local = sdata->local;
5bcae31d 844 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
11335a55 845
5bcae31d 846 lockdep_assert_held(&local->chanctx_mtx);
73da7d5b 847
5bcae31d
MK
848 curr_ctx = ieee80211_vif_get_chanctx(sdata);
849 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
850 return -ENOTSUPP;
11335a55 851
13f348a8 852 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
11335a55 853 if (!new_ctx) {
5bcae31d 854 if (ieee80211_can_create_new_chanctx(local)) {
5d52ee81 855 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
5bcae31d
MK
856 if (IS_ERR(new_ctx))
857 return PTR_ERR(new_ctx);
c2b90ad8 858 } else {
5bcae31d
MK
859 if (!curr_ctx ||
860 (curr_ctx->replace_state ==
861 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
862 !list_empty(&curr_ctx->reserved_vifs)) {
863 /*
864 * Another vif already requested this context
865 * for a reservation. Find another one hoping
866 * all vifs assigned to it will also switch
867 * soon enough.
868 *
869 * TODO: This needs a little more work as some
870 * cases (more than 2 chanctx capable devices)
871 * may fail which could otherwise succeed
872 * provided some channel context juggling was
873 * performed.
874 *
875 * Consider ctx1..3, vif1..6, each ctx has 2
876 * vifs. vif1 and vif2 from ctx1 request new
877 * different chandefs starting 2 in-place
878 * reserations with ctx4 and ctx5 replacing
879 * ctx1 and ctx2 respectively. Next vif5 and
880 * vif6 from ctx3 reserve ctx4. If vif3 and
881 * vif4 remain on ctx2 as they are then this
882 * fails unless `replace_ctx` from ctx5 is
883 * replaced with ctx3.
884 */
885 list_for_each_entry(ctx, &local->chanctx_list,
886 list) {
887 if (ctx->replace_state !=
888 IEEE80211_CHANCTX_REPLACE_NONE)
889 continue;
890
891 if (!list_empty(&ctx->reserved_vifs))
892 continue;
893
894 curr_ctx = ctx;
895 break;
896 }
897 }
898
899 /*
900 * If that's true then all available contexts already
901 * have reservations and cannot be used.
902 */
903 if (!curr_ctx ||
904 (curr_ctx->replace_state ==
905 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
906 !list_empty(&curr_ctx->reserved_vifs))
907 return -EBUSY;
908
909 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
910 if (!new_ctx)
911 return -ENOMEM;
912
913 new_ctx->replace_ctx = curr_ctx;
914 new_ctx->replace_state =
915 IEEE80211_CHANCTX_REPLACES_OTHER;
916
917 curr_ctx->replace_ctx = new_ctx;
918 curr_ctx->replace_state =
919 IEEE80211_CHANCTX_WILL_BE_REPLACED;
920
921 list_add_rcu(&new_ctx->list, &local->chanctx_list);
11335a55
LC
922 }
923 }
924
e3afb920 925 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
11335a55
LC
926 sdata->reserved_chanctx = new_ctx;
927 sdata->reserved_chandef = *chandef;
09332481 928 sdata->reserved_radar_required = radar_required;
5bcae31d
MK
929 sdata->reserved_ready = false;
930
931 return 0;
11335a55
LC
932}
933
03078de4
MK
934static void
935ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
936{
937 switch (sdata->vif.type) {
938 case NL80211_IFTYPE_ADHOC:
939 case NL80211_IFTYPE_AP:
940 case NL80211_IFTYPE_MESH_POINT:
6e0bd6c3 941 case NL80211_IFTYPE_OCB:
03078de4
MK
942 ieee80211_queue_work(&sdata->local->hw,
943 &sdata->csa_finalize_work);
944 break;
03078de4 945 case NL80211_IFTYPE_STATION:
4c3ebc56
MK
946 ieee80211_queue_work(&sdata->local->hw,
947 &sdata->u.mgd.chswitch_work);
948 break;
949 case NL80211_IFTYPE_UNSPECIFIED:
03078de4
MK
950 case NL80211_IFTYPE_AP_VLAN:
951 case NL80211_IFTYPE_WDS:
952 case NL80211_IFTYPE_MONITOR:
953 case NL80211_IFTYPE_P2P_CLIENT:
954 case NL80211_IFTYPE_P2P_GO:
955 case NL80211_IFTYPE_P2P_DEVICE:
956 case NUM_NL80211_IFTYPES:
957 WARN_ON(1);
958 break;
959 }
960}
961
2967e031
FF
962static void
963ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
964 const struct cfg80211_chan_def *chandef)
965{
966 struct ieee80211_sub_if_data *vlan;
967
968 sdata->vif.bss_conf.chandef = *chandef;
969
970 if (sdata->vif.type != NL80211_IFTYPE_AP)
971 return;
972
973 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
974 vlan->vif.bss_conf.chandef = *chandef;
975}
976
5bcae31d
MK
977static int
978ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
11335a55
LC
979{
980 struct ieee80211_local *local = sdata->local;
5bcae31d
MK
981 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
982 struct ieee80211_chanctx *old_ctx, *new_ctx;
983 const struct cfg80211_chan_def *chandef;
984 u32 changed = 0;
985 int err;
11335a55
LC
986
987 lockdep_assert_held(&local->mtx);
5bcae31d 988 lockdep_assert_held(&local->chanctx_mtx);
11335a55 989
5bcae31d
MK
990 new_ctx = sdata->reserved_chanctx;
991 old_ctx = ieee80211_vif_get_chanctx(sdata);
11335a55 992
5bcae31d
MK
993 if (WARN_ON(!sdata->reserved_ready))
994 return -EBUSY;
73da7d5b 995
5bcae31d
MK
996 if (WARN_ON(!new_ctx))
997 return -EINVAL;
998
999 if (WARN_ON(!old_ctx))
1000 return -EINVAL;
1001
1002 if (WARN_ON(new_ctx->replace_state ==
1003 IEEE80211_CHANCTX_REPLACES_OTHER))
1004 return -EINVAL;
1005
1006 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1007 &sdata->reserved_chandef);
1008 if (WARN_ON(!chandef))
1009 return -EINVAL;
1010
1011 vif_chsw[0].vif = &sdata->vif;
1012 vif_chsw[0].old_ctx = &old_ctx->conf;
1013 vif_chsw[0].new_ctx = &new_ctx->conf;
1014
1015 list_del(&sdata->reserved_chanctx_list);
1016 sdata->reserved_chanctx = NULL;
1017
1018 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1019 CHANCTX_SWMODE_REASSIGN_VIF);
1020 if (err) {
1021 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1022 ieee80211_free_chanctx(local, new_ctx);
1023
03078de4 1024 goto out;
73da7d5b
SW
1025 }
1026
5bcae31d
MK
1027 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1028 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
1029
1030 if (sdata->vif.type == NL80211_IFTYPE_AP)
1031 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1032
1033 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1034 ieee80211_free_chanctx(local, old_ctx);
73da7d5b 1035
11335a55 1036 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
5bcae31d 1037 changed = BSS_CHANGED_BANDWIDTH;
11335a55 1038
2967e031 1039 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
11335a55 1040
722ddb0d
EG
1041 ieee80211_recalc_smps_chanctx(local, new_ctx);
1042 ieee80211_recalc_radar_chanctx(local, new_ctx);
1043 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1044
5bcae31d
MK
1045 if (changed)
1046 ieee80211_bss_info_change_notify(sdata, changed);
1047
03078de4
MK
1048out:
1049 ieee80211_vif_chanctx_reservation_complete(sdata);
5bcae31d
MK
1050 return err;
1051}
1052
1053static int
1054ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1055{
1056 struct ieee80211_local *local = sdata->local;
1057 struct ieee80211_chanctx *old_ctx, *new_ctx;
1058 const struct cfg80211_chan_def *chandef;
1059 int err;
1060
1061 old_ctx = ieee80211_vif_get_chanctx(sdata);
1062 new_ctx = sdata->reserved_chanctx;
1063
1064 if (WARN_ON(!sdata->reserved_ready))
1065 return -EINVAL;
1066
1067 if (WARN_ON(old_ctx))
1068 return -EINVAL;
1069
1070 if (WARN_ON(!new_ctx))
1071 return -EINVAL;
1072
1073 if (WARN_ON(new_ctx->replace_state ==
1074 IEEE80211_CHANCTX_REPLACES_OTHER))
1075 return -EINVAL;
1076
1077 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1078 &sdata->reserved_chandef);
1079 if (WARN_ON(!chandef))
1080 return -EINVAL;
1081
e3afb920 1082 list_del(&sdata->reserved_chanctx_list);
5bcae31d 1083 sdata->reserved_chanctx = NULL;
11335a55 1084
5bcae31d
MK
1085 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1086 if (err) {
1087 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1088 ieee80211_free_chanctx(local, new_ctx);
1089
1090 goto out;
1091 }
1092
1093out:
03078de4 1094 ieee80211_vif_chanctx_reservation_complete(sdata);
5bcae31d
MK
1095 return err;
1096}
1097
1098static bool
1099ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1100{
1101 struct ieee80211_chanctx *old_ctx, *new_ctx;
1102
1103 lockdep_assert_held(&sdata->local->chanctx_mtx);
1104
1105 new_ctx = sdata->reserved_chanctx;
1106 old_ctx = ieee80211_vif_get_chanctx(sdata);
1107
1108 if (!old_ctx)
1109 return false;
1110
1111 if (WARN_ON(!new_ctx))
1112 return false;
1113
1114 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1115 return false;
1116
1117 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1118 return false;
1119
1120 return true;
1121}
1122
1123static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1124 struct ieee80211_chanctx *new_ctx)
1125{
1126 const struct cfg80211_chan_def *chandef;
1127
1128 lockdep_assert_held(&local->mtx);
1129 lockdep_assert_held(&local->chanctx_mtx);
1130
1131 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1132 if (WARN_ON(!chandef))
1133 return -EINVAL;
1134
1135 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1136 local->_oper_chandef = *chandef;
1137 ieee80211_hw_config(local, 0);
1138
1139 return 0;
1140}
1141
1142static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1143 int n_vifs)
1144{
1145 struct ieee80211_vif_chanctx_switch *vif_chsw;
1146 struct ieee80211_sub_if_data *sdata;
1147 struct ieee80211_chanctx *ctx, *old_ctx;
1148 int i, err;
1149
1150 lockdep_assert_held(&local->mtx);
1151 lockdep_assert_held(&local->chanctx_mtx);
1152
1153 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1154 if (!vif_chsw)
1155 return -ENOMEM;
1156
1157 i = 0;
1158 list_for_each_entry(ctx, &local->chanctx_list, list) {
1159 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1160 continue;
1161
1162 if (WARN_ON(!ctx->replace_ctx)) {
1163 err = -EINVAL;
5d52ee81
LC
1164 goto out;
1165 }
11335a55 1166
5bcae31d
MK
1167 list_for_each_entry(sdata, &ctx->reserved_vifs,
1168 reserved_chanctx_list) {
1169 if (!ieee80211_vif_has_in_place_reservation(
1170 sdata))
1171 continue;
1172
1173 old_ctx = ieee80211_vif_get_chanctx(sdata);
1174 vif_chsw[i].vif = &sdata->vif;
1175 vif_chsw[i].old_ctx = &old_ctx->conf;
1176 vif_chsw[i].new_ctx = &ctx->conf;
1177
1178 i++;
1179 }
5d52ee81 1180 }
11335a55 1181
5bcae31d
MK
1182 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1183 CHANCTX_SWMODE_SWAP_CONTEXTS);
73da7d5b 1184
11335a55 1185out:
5bcae31d
MK
1186 kfree(vif_chsw);
1187 return err;
1188}
1189
1190static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1191{
1192 struct ieee80211_chanctx *ctx;
1193 int err;
1194
1195 lockdep_assert_held(&local->mtx);
1196 lockdep_assert_held(&local->chanctx_mtx);
1197
1198 list_for_each_entry(ctx, &local->chanctx_list, list) {
1199 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1200 continue;
1201
1202 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1203 continue;
1204
1205 ieee80211_del_chanctx(local, ctx->replace_ctx);
1206 err = ieee80211_add_chanctx(local, ctx);
1207 if (err)
1208 goto err;
1209 }
1210
1211 return 0;
1212
1213err:
1214 WARN_ON(ieee80211_add_chanctx(local, ctx));
1215 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1216 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1217 continue;
1218
1219 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1220 continue;
1221
1222 ieee80211_del_chanctx(local, ctx);
1223 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1224 }
1225
1226 return err;
1227}
1228
649b2a4d 1229static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
5bcae31d
MK
1230{
1231 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1232 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1233 struct ieee80211_chanctx *new_ctx = NULL;
1234 int i, err, n_assigned, n_reserved, n_ready;
1235 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1236
1237 lockdep_assert_held(&local->mtx);
1238 lockdep_assert_held(&local->chanctx_mtx);
1239
1240 /*
1241 * If there are 2 independent pairs of channel contexts performing
1242 * cross-switch of their vifs this code will still wait until both are
1243 * ready even though it could be possible to switch one before the
1244 * other is ready.
1245 *
1246 * For practical reasons and code simplicity just do a single huge
1247 * switch.
1248 */
1249
1250 /*
1251 * Verify if the reservation is still feasible.
1252 * - if it's not then disconnect
1253 * - if it is but not all vifs necessary are ready then defer
1254 */
1255
1256 list_for_each_entry(ctx, &local->chanctx_list, list) {
1257 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1258 continue;
1259
1260 if (WARN_ON(!ctx->replace_ctx)) {
1261 err = -EINVAL;
1262 goto err;
1263 }
1264
1265 if (!local->use_chanctx)
1266 new_ctx = ctx;
1267
1268 n_ctx++;
1269
1270 n_assigned = 0;
1271 n_reserved = 0;
1272 n_ready = 0;
1273
1274 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1275 assigned_chanctx_list) {
1276 n_assigned++;
1277 if (sdata->reserved_chanctx) {
1278 n_reserved++;
1279 if (sdata->reserved_ready)
1280 n_ready++;
1281 }
1282 }
1283
1284 if (n_assigned != n_reserved) {
1285 if (n_ready == n_reserved) {
1286 wiphy_info(local->hw.wiphy,
1287 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1288 err = -EBUSY;
1289 goto err;
1290 }
1291
1292 return -EAGAIN;
1293 }
1294
1295 ctx->conf.radar_enabled = false;
1296 list_for_each_entry(sdata, &ctx->reserved_vifs,
1297 reserved_chanctx_list) {
1298 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1299 !sdata->reserved_ready)
1300 return -EAGAIN;
1301
1302 old_ctx = ieee80211_vif_get_chanctx(sdata);
1303 if (old_ctx) {
1304 if (old_ctx->replace_state ==
1305 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1306 n_vifs_switch++;
1307 else
1308 n_vifs_assign++;
1309 } else {
1310 n_vifs_ctxless++;
1311 }
1312
1313 if (sdata->reserved_radar_required)
1314 ctx->conf.radar_enabled = true;
1315 }
1316 }
1317
1318 if (WARN_ON(n_ctx == 0) ||
1319 WARN_ON(n_vifs_switch == 0 &&
1320 n_vifs_assign == 0 &&
1321 n_vifs_ctxless == 0) ||
1322 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1323 WARN_ON(!new_ctx && !local->use_chanctx)) {
1324 err = -EINVAL;
1325 goto err;
1326 }
1327
1328 /*
1329 * All necessary vifs are ready. Perform the switch now depending on
1330 * reservations and driver capabilities.
1331 */
1332
1333 if (local->use_chanctx) {
1334 if (n_vifs_switch > 0) {
1335 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1336 if (err)
1337 goto err;
1338 }
1339
1340 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1341 err = ieee80211_chsw_switch_ctxs(local);
1342 if (err)
1343 goto err;
1344 }
1345 } else {
1346 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1347 if (err)
1348 goto err;
1349 }
1350
1351 /*
1352 * Update all structures, values and pointers to point to new channel
1353 * context(s).
1354 */
1355
1356 i = 0;
1357 list_for_each_entry(ctx, &local->chanctx_list, list) {
1358 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1359 continue;
1360
1361 if (WARN_ON(!ctx->replace_ctx)) {
1362 err = -EINVAL;
1363 goto err;
1364 }
1365
1366 list_for_each_entry(sdata, &ctx->reserved_vifs,
1367 reserved_chanctx_list) {
1368 u32 changed = 0;
1369
1370 if (!ieee80211_vif_has_in_place_reservation(sdata))
1371 continue;
1372
1373 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1374
1375 if (sdata->vif.type == NL80211_IFTYPE_AP)
1376 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1377 false);
1378
1379 sdata->radar_required = sdata->reserved_radar_required;
1380
1381 if (sdata->vif.bss_conf.chandef.width !=
1382 sdata->reserved_chandef.width)
1383 changed = BSS_CHANGED_BANDWIDTH;
1384
2967e031 1385 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
5bcae31d
MK
1386 if (changed)
1387 ieee80211_bss_info_change_notify(sdata,
1388 changed);
1389
db82d8a9 1390 ieee80211_recalc_txpower(sdata, false);
5bcae31d
MK
1391 }
1392
1393 ieee80211_recalc_chanctx_chantype(local, ctx);
1394 ieee80211_recalc_smps_chanctx(local, ctx);
1395 ieee80211_recalc_radar_chanctx(local, ctx);
1396 ieee80211_recalc_chanctx_min_def(local, ctx);
1397
1398 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1399 reserved_chanctx_list) {
1400 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1401 continue;
1402
1403 list_del(&sdata->reserved_chanctx_list);
1404 list_move(&sdata->assigned_chanctx_list,
47e4df94 1405 &ctx->assigned_vifs);
5bcae31d 1406 sdata->reserved_chanctx = NULL;
03078de4
MK
1407
1408 ieee80211_vif_chanctx_reservation_complete(sdata);
5bcae31d
MK
1409 }
1410
1411 /*
1412 * This context might have been a dependency for an already
1413 * ready re-assign reservation interface that was deferred. Do
1414 * not propagate error to the caller though. The in-place
1415 * reservation for originally requested interface has already
1416 * succeeded at this point.
1417 */
1418 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1419 reserved_chanctx_list) {
1420 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1421 sdata)))
1422 continue;
1423
1424 if (WARN_ON(sdata->reserved_chanctx != ctx))
1425 continue;
1426
1427 if (!sdata->reserved_ready)
1428 continue;
1429
1430 if (ieee80211_vif_get_chanctx(sdata))
1431 err = ieee80211_vif_use_reserved_reassign(
1432 sdata);
1433 else
1434 err = ieee80211_vif_use_reserved_assign(sdata);
1435
1436 if (err) {
1437 sdata_info(sdata,
1438 "failed to finalize (re-)assign reservation (err=%d)\n",
1439 err);
1440 ieee80211_vif_unreserve_chanctx(sdata);
1441 cfg80211_stop_iface(local->hw.wiphy,
1442 &sdata->wdev,
1443 GFP_KERNEL);
1444 }
1445 }
1446 }
1447
1448 /*
1449 * Finally free old contexts
1450 */
1451
1452 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1453 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1454 continue;
1455
1456 ctx->replace_ctx->replace_ctx = NULL;
1457 ctx->replace_ctx->replace_state =
1458 IEEE80211_CHANCTX_REPLACE_NONE;
1459
1460 list_del_rcu(&ctx->list);
1461 kfree_rcu(ctx, rcu_head);
1462 }
1463
1464 return 0;
1465
1466err:
1467 list_for_each_entry(ctx, &local->chanctx_list, list) {
1468 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1469 continue;
1470
1471 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
03078de4 1472 reserved_chanctx_list) {
5bcae31d 1473 ieee80211_vif_unreserve_chanctx(sdata);
03078de4
MK
1474 ieee80211_vif_chanctx_reservation_complete(sdata);
1475 }
5bcae31d
MK
1476 }
1477
1478 return err;
1479}
1480
649b2a4d
JB
1481static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1482{
1483 struct ieee80211_local *local = sdata->local;
1484 struct ieee80211_chanctx_conf *conf;
1485 struct ieee80211_chanctx *ctx;
1486 bool use_reserved_switch = false;
1487
1488 lockdep_assert_held(&local->chanctx_mtx);
1489
1490 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1491 lockdep_is_held(&local->chanctx_mtx));
1492 if (!conf)
1493 return;
1494
1495 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1496
1497 if (sdata->reserved_chanctx) {
1498 if (sdata->reserved_chanctx->replace_state ==
1499 IEEE80211_CHANCTX_REPLACES_OTHER &&
1500 ieee80211_chanctx_num_reserved(local,
1501 sdata->reserved_chanctx) > 1)
1502 use_reserved_switch = true;
1503
1504 ieee80211_vif_unreserve_chanctx(sdata);
1505 }
1506
1507 ieee80211_assign_vif_chanctx(sdata, NULL);
1508 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1509 ieee80211_free_chanctx(local, ctx);
1510
1511 /* Unreserving may ready an in-place reservation. */
1512 if (use_reserved_switch)
1513 ieee80211_vif_use_reserved_switch(local);
1514}
1515
1516int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1517 const struct cfg80211_chan_def *chandef,
1518 enum ieee80211_chanctx_mode mode)
1519{
1520 struct ieee80211_local *local = sdata->local;
1521 struct ieee80211_chanctx *ctx;
1522 u8 radar_detect_width = 0;
1523 int ret;
1524
1525 lockdep_assert_held(&local->mtx);
1526
1527 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1528
1529 mutex_lock(&local->chanctx_mtx);
1530
1531 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1532 chandef,
1533 sdata->wdev.iftype);
1534 if (ret < 0)
1535 goto out;
1536 if (ret > 0)
1537 radar_detect_width = BIT(chandef->width);
1538
1539 sdata->radar_required = ret;
1540
1541 ret = ieee80211_check_combinations(sdata, chandef, mode,
1542 radar_detect_width);
1543 if (ret < 0)
1544 goto out;
1545
1546 __ieee80211_vif_release_channel(sdata);
1547
1548 ctx = ieee80211_find_chanctx(local, chandef, mode);
1549 if (!ctx)
1550 ctx = ieee80211_new_chanctx(local, chandef, mode);
1551 if (IS_ERR(ctx)) {
1552 ret = PTR_ERR(ctx);
1553 goto out;
1554 }
1555
2967e031 1556 ieee80211_vif_update_chandef(sdata, chandef);
649b2a4d
JB
1557
1558 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1559 if (ret) {
1560 /* if assign fails refcount stays the same */
1561 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1562 ieee80211_free_chanctx(local, ctx);
1563 goto out;
1564 }
1565
1566 ieee80211_recalc_smps_chanctx(local, ctx);
1567 ieee80211_recalc_radar_chanctx(local, ctx);
1568 out:
1569 mutex_unlock(&local->chanctx_mtx);
1570 return ret;
1571}
1572
5bcae31d
MK
1573int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1574{
1575 struct ieee80211_local *local = sdata->local;
1576 struct ieee80211_chanctx *new_ctx;
1577 struct ieee80211_chanctx *old_ctx;
1578 int err;
1579
1580 lockdep_assert_held(&local->mtx);
1581 lockdep_assert_held(&local->chanctx_mtx);
1582
1583 new_ctx = sdata->reserved_chanctx;
1584 old_ctx = ieee80211_vif_get_chanctx(sdata);
1585
1586 if (WARN_ON(!new_ctx))
1587 return -EINVAL;
1588
1589 if (WARN_ON(new_ctx->replace_state ==
1590 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1591 return -EINVAL;
1592
1593 if (WARN_ON(sdata->reserved_ready))
1594 return -EINVAL;
1595
1596 sdata->reserved_ready = true;
1597
1598 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1599 if (old_ctx)
1600 err = ieee80211_vif_use_reserved_reassign(sdata);
1601 else
1602 err = ieee80211_vif_use_reserved_assign(sdata);
1603
1604 if (err)
1605 return err;
1606 }
1607
1608 /*
1609 * In-place reservation may need to be finalized now either if:
1610 * a) sdata is taking part in the swapping itself and is the last one
1611 * b) sdata has switched with a re-assign reservation to an existing
1612 * context readying in-place switching of old_ctx
1613 *
1614 * In case of (b) do not propagate the error up because the requested
1615 * sdata already switched successfully. Just spill an extra warning.
1616 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1617 * interfaces upon failure.
1618 */
1619 if ((old_ctx &&
1620 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1621 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1622 err = ieee80211_vif_use_reserved_switch(local);
1623 if (err && err != -EAGAIN) {
1624 if (new_ctx->replace_state ==
1625 IEEE80211_CHANCTX_REPLACES_OTHER)
1626 return err;
1627
1628 wiphy_info(local->hw.wiphy,
1629 "depending in-place reservation failed (err=%d)\n",
1630 err);
1631 }
1632 }
1633
1634 return 0;
73da7d5b
SW
1635}
1636
2c9b7359
JB
1637int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1638 const struct cfg80211_chan_def *chandef,
1639 u32 *changed)
1640{
1641 struct ieee80211_local *local = sdata->local;
1642 struct ieee80211_chanctx_conf *conf;
1643 struct ieee80211_chanctx *ctx;
5bcae31d 1644 const struct cfg80211_chan_def *compat;
2c9b7359
JB
1645 int ret;
1646
1647 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1648 IEEE80211_CHAN_DISABLED))
1649 return -EINVAL;
1650
1651 mutex_lock(&local->chanctx_mtx);
1652 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1653 ret = 0;
1654 goto out;
1655 }
1656
1657 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1658 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1659 ret = -EINVAL;
1660 goto out;
1661 }
1662
1663 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1664 lockdep_is_held(&local->chanctx_mtx));
1665 if (!conf) {
1666 ret = -EINVAL;
1667 goto out;
1668 }
1669
1670 ctx = container_of(conf, struct ieee80211_chanctx, conf);
5bcae31d
MK
1671
1672 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1673 if (!compat) {
2c9b7359
JB
1674 ret = -EINVAL;
1675 goto out;
1676 }
1677
5bcae31d
MK
1678 switch (ctx->replace_state) {
1679 case IEEE80211_CHANCTX_REPLACE_NONE:
1680 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1681 ret = -EBUSY;
1682 goto out;
1683 }
1684 break;
1685 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
d070f913 1686 /* TODO: Perhaps the bandwidth change could be treated as a
5bcae31d
MK
1687 * reservation itself? */
1688 ret = -EBUSY;
1689 goto out;
1690 case IEEE80211_CHANCTX_REPLACES_OTHER:
1691 /* channel context that is going to replace another channel
1692 * context doesn't really exist and shouldn't be assigned
1693 * anywhere yet */
1694 WARN_ON(1);
1695 break;
1696 }
1697
2967e031 1698 ieee80211_vif_update_chandef(sdata, chandef);
2c9b7359
JB
1699
1700 ieee80211_recalc_chanctx_chantype(local, ctx);
1701
1702 *changed |= BSS_CHANGED_BANDWIDTH;
1703 ret = 0;
1704 out:
1705 mutex_unlock(&local->chanctx_mtx);
1706 return ret;
1707}
1708
d01a1e65
MK
1709void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1710{
55de908a
JB
1711 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1712
34a3740d
JB
1713 lockdep_assert_held(&sdata->local->mtx);
1714
d01a1e65
MK
1715 mutex_lock(&sdata->local->chanctx_mtx);
1716 __ieee80211_vif_release_channel(sdata);
1717 mutex_unlock(&sdata->local->chanctx_mtx);
1718}
3448c005 1719
4d76d21b
JB
1720void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1721{
1722 struct ieee80211_local *local = sdata->local;
1723 struct ieee80211_sub_if_data *ap;
1724 struct ieee80211_chanctx_conf *conf;
1725
1726 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1727 return;
1728
1729 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1730
1731 mutex_lock(&local->chanctx_mtx);
1732
1733 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1734 lockdep_is_held(&local->chanctx_mtx));
1735 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1736 mutex_unlock(&local->chanctx_mtx);
1737}
1738
3448c005
JB
1739void ieee80211_iter_chan_contexts_atomic(
1740 struct ieee80211_hw *hw,
1741 void (*iter)(struct ieee80211_hw *hw,
1742 struct ieee80211_chanctx_conf *chanctx_conf,
1743 void *data),
1744 void *iter_data)
1745{
1746 struct ieee80211_local *local = hw_to_local(hw);
1747 struct ieee80211_chanctx *ctx;
1748
1749 rcu_read_lock();
1750 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
8a61af65
JB
1751 if (ctx->driver_present)
1752 iter(hw, &ctx->conf, iter_data);
3448c005
JB
1753 rcu_read_unlock();
1754}
1755EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);