blob: 7e9b62475400c2a7669a6fe94eea118503bbea84 [file] [log] [blame]
Johannes Bergf444de02010-05-05 15:25:02 +02001/*
2 * mac80211 - channel management
3 */
4
Johannes Berg0aaffa92010-05-05 15:28:27 +02005#include <linux/nl80211.h>
Johannes Berg3448c002012-09-11 17:57:42 +02006#include <linux/export.h>
Johannes Berg4d76d212012-12-11 20:38:41 +01007#include <linux/rtnetlink.h>
Paul Stewart3117bbdb2012-03-13 07:46:18 -07008#include <net/cfg80211.h>
Johannes Bergf444de02010-05-05 15:25:02 +02009#include "ieee80211_i.h"
Michal Kazior35f2fce2012-06-26 14:37:20 +020010#include "driver-ops.h"
Johannes Bergf444de02010-05-05 15:25:02 +020011
Michal Kaziorc0166da2014-04-09 15:29:33 +020012static 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
Michal Kaziorc2b90ad2014-04-09 15:29:24 +020047static 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
Michal Kazior5bcae312014-06-25 12:35:06 +020066static struct ieee80211_chanctx *
67ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
68{
Johannes Berg1d4cc302014-07-18 09:47:26 +020069 struct ieee80211_local *local __maybe_unused = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +020070 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
Michal Kazior02881572014-04-09 15:29:28 +020080static 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
Michal Kazior13f348a2014-04-09 15:29:29 +0200103static 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) {
Michal Kazior5bcae312014-06-25 12:35:06 +0200177 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
178 continue;
179
Michal Kazior13f348a2014-04-09 15:29:29 +0200180 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
Eliad Peller21f659b2013-11-11 20:14:01 +0200193static 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:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100273 case NL80211_IFTYPE_OCB:
Eliad Peller21f659b2013-11-11 20:14:01 +0200274 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 }
Eliad Peller1c37a722014-03-03 13:37:14 +0200285
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
Eliad Peller21f659b2013-11-11 20:14:01 +0200291 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
Johannes Berg18942d32013-02-07 21:30:37 +0100334static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100335 struct ieee80211_chanctx *ctx,
336 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200337{
Johannes Berg4bf88532012-11-09 11:39:59 +0100338 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +0200339 return;
340
Johannes Berg4bf88532012-11-09 11:39:59 +0100341 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);
Eliad Peller21f659b2013-11-11 20:14:01 +0200345 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200346
347 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100348 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200349 ieee80211_hw_config(local, 0);
350 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200351}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200352
353static struct ieee80211_chanctx *
354ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100355 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200356 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;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200364
365 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100366 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200367
Michal Kazior5bcae312014-06-25 12:35:06 +0200368 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
369 continue;
370
Michal Kaziord01a1e62012-06-26 14:37:16 +0200371 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
372 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100373
374 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
375 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200376 continue;
377
Michal Kazior02881572014-04-09 15:29:28 +0200378 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
379 compat);
380 if (!compat)
381 continue;
382
Johannes Berg18942d32013-02-07 21:30:37 +0100383 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200384
Michal Kaziord01a1e62012-06-26 14:37:16 +0200385 return ctx;
386 }
387
388 return NULL;
389}
390
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200391bool ieee80211_is_radar_required(struct ieee80211_local *local)
Simon Wunderliche4746852013-04-08 22:43:16 +0200392{
393 struct ieee80211_sub_if_data *sdata;
394
Michal Kaziorcc901de2014-01-29 07:56:20 +0100395 lockdep_assert_held(&local->mtx);
396
Simon Wunderliche4746852013-04-08 22:43:16 +0200397 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
Eliad Pellere7f23372015-01-07 17:50:10 +0200409static 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
Michal Kaziord01a1e62012-06-26 14:37:16 +0200437static struct ieee80211_chanctx *
Michal Kaziored68ebc2014-04-09 15:29:30 +0200438ieee80211_alloc_chanctx(struct ieee80211_local *local,
439 const struct cfg80211_chan_def *chandef,
440 enum ieee80211_chanctx_mode mode)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200441{
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)
Michal Kaziored68ebc2014-04-09 15:29:30 +0200448 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200449
Michal Kazior484298a2014-04-09 15:29:26 +0200450 INIT_LIST_HEAD(&ctx->assigned_vifs);
Michal Kaziore3afb922014-04-09 15:29:27 +0200451 INIT_LIST_HEAD(&ctx->reserved_vifs);
Johannes Berg4bf88532012-11-09 11:39:59 +0100452 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200453 ctx->conf.rx_chains_static = 1;
454 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200455 ctx->mode = mode;
Eliad Pellere7f23372015-01-07 17:50:10 +0200456 ctx->conf.radar_enabled = false;
Eliad Peller21f659b2013-11-11 20:14:01 +0200457 ieee80211_recalc_chanctx_min_def(local, ctx);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200458
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
Simon Wunderliche4746852013-04-08 22:43:16 +0200471 if (!local->use_chanctx)
472 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200473
Johannes Berg382a1032013-03-22 22:30:09 +0100474 /* 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
Johannes Berg55de9082012-07-26 17:24:39 +0200479 if (!local->use_chanctx) {
Michal Kaziored68ebc2014-04-09 15:29:30 +0200480 local->_oper_chandef = ctx->conf.def;
Michal Kazior9b4816f2014-04-04 13:02:43 +0200481 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg55de9082012-07-26 17:24:39 +0200482 } else {
483 err = drv_add_chanctx(local, ctx);
484 if (err) {
Johannes Berg382a1032013-03-22 22:30:09 +0100485 ieee80211_recalc_idle(local);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200486 return err;
Johannes Berg55de9082012-07-26 17:24:39 +0200487 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200488 }
489
Michal Kaziored68ebc2014-04-09 15:29:30 +0200490 return 0;
491}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200492
Michal Kaziored68ebc2014-04-09 15:29:30 +0200493static 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;
500
501 lockdep_assert_held(&local->mtx);
502 lockdep_assert_held(&local->chanctx_mtx);
503
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);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200515 return ctx;
516}
517
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200518static void ieee80211_del_chanctx(struct ieee80211_local *local,
519 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200520{
521 lockdep_assert_held(&local->chanctx_mtx);
522
Johannes Berg55de9082012-07-26 17:24:39 +0200523 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100524 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;
Simon Wunderliche4746852013-04-08 22:43:16 +0200528
529 /* NOTE: Disabling radar is only valid here for
530 * single channel context. To be sure, check it ...
531 */
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200532 WARN_ON(local->hw.conf.radar_enabled &&
533 !list_empty(&local->chanctx_list));
534
Simon Wunderliche4746852013-04-08 22:43:16 +0200535 local->hw.conf.radar_enabled = false;
536
Michal Kazior9b4816f2014-04-04 13:02:43 +0200537 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg55de9082012-07-26 17:24:39 +0200538 } else {
539 drv_remove_chanctx(local, ctx);
540 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200541
Johannes Bergfd0f9792013-02-07 00:14:51 +0100542 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200543}
544
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200545static void ieee80211_free_chanctx(struct ieee80211_local *local,
546 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200547{
Michal Kaziord01a1e62012-06-26 14:37:16 +0200548 lockdep_assert_held(&local->chanctx_mtx);
549
Michal Kaziorc0166da2014-04-09 15:29:33 +0200550 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200551
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200552 list_del_rcu(&ctx->list);
553 ieee80211_del_chanctx(local, ctx);
554 kfree_rcu(ctx, rcu_head);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200555}
556
Johannes Berg4bf88532012-11-09 11:39:59 +0100557static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
558 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200559{
560 struct ieee80211_chanctx_conf *conf = &ctx->conf;
561 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100562 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200563
564 lockdep_assert_held(&local->chanctx_mtx);
565
566 rcu_read_lock();
567 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100568
Michal Kaziore89a96f2012-06-26 14:37:22 +0200569 if (!ieee80211_sdata_running(sdata))
570 continue;
571 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
572 continue;
Felix Fietkau0e67c132014-07-25 16:20:22 +0200573 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
574 continue;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200575
Johannes Berg4bf88532012-11-09 11:39:59 +0100576 if (!compat)
577 compat = &sdata->vif.bss_conf.chandef;
578
579 compat = cfg80211_chandef_compatible(
580 &sdata->vif.bss_conf.chandef, compat);
Michal Kaziora00f4f62014-07-28 15:16:59 +0200581 if (WARN_ON_ONCE(!compat))
Johannes Berg4bf88532012-11-09 11:39:59 +0100582 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200583 }
584 rcu_read_unlock();
585
Michal Kaziora00f4f62014-07-28 15:16:59 +0200586 if (!compat)
Johannes Berg4bf88532012-11-09 11:39:59 +0100587 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200588
Johannes Berg18942d32013-02-07 21:30:37 +0100589 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200590}
591
Johannes Berg367bbd12013-12-18 09:36:09 +0100592static 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);
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200598 /* for ieee80211_is_radar_required */
Johannes Berg34a37402013-12-18 09:43:33 +0100599 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100600
Eliad Pellere7f23372015-01-07 17:50:10 +0200601 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100602
603 if (radar_enabled == chanctx->conf.radar_enabled)
604 return;
605
606 chanctx->conf.radar_enabled = radar_enabled;
Johannes Berg367bbd12013-12-18 09:36:09 +0100607
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
Luciano Coelho77eeba92014-03-11 18:24:12 +0200616static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
617 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200618{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200619 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200620 struct ieee80211_chanctx_conf *conf;
621 struct ieee80211_chanctx *curr_ctx = NULL;
622 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200623
Luciano Coelho77eeba92014-03-11 18:24:12 +0200624 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
625 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200626
Luciano Coelho77eeba92014-03-11 18:24:12 +0200627 if (conf) {
628 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200629
Luciano Coelho77eeba92014-03-11 18:24:12 +0200630 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
631 conf = NULL;
Michal Kazior484298a2014-04-09 15:29:26 +0200632 list_del(&sdata->assigned_chanctx_list);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200633 }
634
635 if (new_ctx) {
636 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
637 if (ret)
638 goto out;
639
Luciano Coelho77eeba92014-03-11 18:24:12 +0200640 conf = &new_ctx->conf;
Michal Kazior484298a2014-04-09 15:29:26 +0200641 list_add(&sdata->assigned_chanctx_list,
642 &new_ctx->assigned_vifs);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200643 }
644
645out:
646 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
647
648 sdata->vif.bss_conf.idle = !conf;
649
Michal Kaziorc0166da2014-04-09 15:29:33 +0200650 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200651 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);
655 }
656
Michal Kaziorc0166da2014-04-09 15:29:33 +0200657 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
Lorenzo Bianconidb82d8a2015-01-14 12:55:08 +0100658 ieee80211_recalc_txpower(sdata, false);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200659 ieee80211_recalc_chanctx_min_def(local, new_ctx);
660 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100661
662 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
663 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200664 ieee80211_bss_info_change_notify(sdata,
665 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100666
Johannes Berg17c18bf2015-03-21 15:25:43 +0100667 ieee80211_check_fast_xmit_iface(sdata);
668
Luciano Coelho77eeba92014-03-11 18:24:12 +0200669 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200670}
671
Johannes Berg04ecd252012-09-11 14:34:12 +0200672void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
673 struct ieee80211_chanctx *chanctx)
674{
675 struct ieee80211_sub_if_data *sdata;
676 u8 rx_chains_static, rx_chains_dynamic;
677
678 lockdep_assert_held(&local->chanctx_mtx);
679
680 rx_chains_static = 1;
681 rx_chains_dynamic = 1;
682
683 rcu_read_lock();
684 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
685 u8 needed_static, needed_dynamic;
686
687 if (!ieee80211_sdata_running(sdata))
688 continue;
689
690 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
691 &chanctx->conf)
692 continue;
693
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:
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100707 case NL80211_IFTYPE_OCB:
Johannes Berg04ecd252012-09-11 14:34:12 +0200708 break;
709 default:
710 WARN_ON_ONCE(1);
711 }
712
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;
730 }
731
732 rx_chains_static = max(rx_chains_static, needed_static);
733 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
734 }
Ido Yariv7b8a9cd2014-03-24 09:55:45 +0200735
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;
741
Johannes Berg04ecd252012-09-11 14:34:12 +0200742 rcu_read_unlock();
743
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);
752 }
753
754 if (rx_chains_static == chanctx->conf.rx_chains_static &&
755 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
756 return;
757
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);
761}
762
Luciano Coelho11335a52013-10-30 13:09:39 +0200763static void
764__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
765 bool clear)
766{
Johannes Berg33926eb2014-04-09 21:31:13 +0200767 struct ieee80211_local *local __maybe_unused = sdata->local;
Luciano Coelho11335a52013-10-30 13:09:39 +0200768 struct ieee80211_sub_if_data *vlan;
769 struct ieee80211_chanctx_conf *conf;
770
771 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
772 return;
773
774 lockdep_assert_held(&local->mtx);
775
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.
781 */
782 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
Johannes Berg33926eb2014-04-09 21:31:13 +0200783 lockdep_is_held(&local->chanctx_mtx));
Luciano Coelho11335a52013-10-30 13:09:39 +0200784 WARN_ON(!conf);
785
786 if (clear)
787 conf = NULL;
788
789 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
790 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
791}
792
793void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
794 bool clear)
795{
796 struct ieee80211_local *local = sdata->local;
797
798 mutex_lock(&local->chanctx_mtx);
799
800 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
801
802 mutex_unlock(&local->chanctx_mtx);
803}
804
805int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
806{
Michal Kaziore3afb922014-04-09 15:29:27 +0200807 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
808
Luciano Coelho11335a52013-10-30 13:09:39 +0200809 lockdep_assert_held(&sdata->local->chanctx_mtx);
810
Michal Kaziore3afb922014-04-09 15:29:27 +0200811 if (WARN_ON(!ctx))
Luciano Coelho11335a52013-10-30 13:09:39 +0200812 return -EINVAL;
813
Michal Kaziore3afb922014-04-09 15:29:27 +0200814 list_del(&sdata->reserved_chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200815 sdata->reserved_chanctx = NULL;
816
Michal Kazior5bcae312014-06-25 12:35:06 +0200817 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;
821
822 WARN_ON(ctx->replace_ctx->replace_state !=
823 IEEE80211_CHANCTX_WILL_BE_REPLACED);
824 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
825
826 ctx->replace_ctx->replace_ctx = NULL;
827 ctx->replace_ctx->replace_state =
828 IEEE80211_CHANCTX_REPLACE_NONE;
829
830 list_del_rcu(&ctx->list);
831 kfree_rcu(ctx, rcu_head);
832 } else {
833 ieee80211_free_chanctx(sdata->local, ctx);
834 }
835 }
Michal Kaziore3afb922014-04-09 15:29:27 +0200836
Luciano Coelho11335a52013-10-30 13:09:39 +0200837 return 0;
838}
839
840int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
841 const struct cfg80211_chan_def *chandef,
Michal Kazior09332482014-04-09 15:29:25 +0200842 enum ieee80211_chanctx_mode mode,
843 bool radar_required)
Luciano Coelho11335a52013-10-30 13:09:39 +0200844{
845 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +0200846 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
Luciano Coelho11335a52013-10-30 13:09:39 +0200847
Michal Kazior5bcae312014-06-25 12:35:06 +0200848 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +0200849
Michal Kazior5bcae312014-06-25 12:35:06 +0200850 curr_ctx = ieee80211_vif_get_chanctx(sdata);
851 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
852 return -ENOTSUPP;
Luciano Coelho11335a52013-10-30 13:09:39 +0200853
Michal Kazior13f348a2014-04-09 15:29:29 +0200854 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
Luciano Coelho11335a52013-10-30 13:09:39 +0200855 if (!new_ctx) {
Michal Kazior5bcae312014-06-25 12:35:06 +0200856 if (ieee80211_can_create_new_chanctx(local)) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200857 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kazior5bcae312014-06-25 12:35:06 +0200858 if (IS_ERR(new_ctx))
859 return PTR_ERR(new_ctx);
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200860 } else {
Michal Kazior5bcae312014-06-25 12:35:06 +0200861 if (!curr_ctx ||
862 (curr_ctx->replace_state ==
863 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
864 !list_empty(&curr_ctx->reserved_vifs)) {
865 /*
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.
870 *
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.
876 *
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.
886 */
887 list_for_each_entry(ctx, &local->chanctx_list,
888 list) {
889 if (ctx->replace_state !=
890 IEEE80211_CHANCTX_REPLACE_NONE)
891 continue;
892
893 if (!list_empty(&ctx->reserved_vifs))
894 continue;
895
896 curr_ctx = ctx;
897 break;
898 }
899 }
900
901 /*
902 * If that's true then all available contexts already
903 * have reservations and cannot be used.
904 */
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;
910
911 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
912 if (!new_ctx)
913 return -ENOMEM;
914
915 new_ctx->replace_ctx = curr_ctx;
916 new_ctx->replace_state =
917 IEEE80211_CHANCTX_REPLACES_OTHER;
918
919 curr_ctx->replace_ctx = new_ctx;
920 curr_ctx->replace_state =
921 IEEE80211_CHANCTX_WILL_BE_REPLACED;
922
923 list_add_rcu(&new_ctx->list, &local->chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200924 }
925 }
926
Michal Kaziore3afb922014-04-09 15:29:27 +0200927 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
Luciano Coelho11335a52013-10-30 13:09:39 +0200928 sdata->reserved_chanctx = new_ctx;
929 sdata->reserved_chandef = *chandef;
Michal Kazior09332482014-04-09 15:29:25 +0200930 sdata->reserved_radar_required = radar_required;
Michal Kazior5bcae312014-06-25 12:35:06 +0200931 sdata->reserved_ready = false;
932
933 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +0200934}
935
Michal Kazior03078de2014-06-25 12:35:08 +0200936static void
937ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
938{
939 switch (sdata->vif.type) {
940 case NL80211_IFTYPE_ADHOC:
941 case NL80211_IFTYPE_AP:
942 case NL80211_IFTYPE_MESH_POINT:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100943 case NL80211_IFTYPE_OCB:
Michal Kazior03078de2014-06-25 12:35:08 +0200944 ieee80211_queue_work(&sdata->local->hw,
945 &sdata->csa_finalize_work);
946 break;
Michal Kazior03078de2014-06-25 12:35:08 +0200947 case NL80211_IFTYPE_STATION:
Michal Kazior4c3ebc52014-06-25 12:35:09 +0200948 ieee80211_queue_work(&sdata->local->hw,
949 &sdata->u.mgd.chswitch_work);
950 break;
951 case NL80211_IFTYPE_UNSPECIFIED:
Michal Kazior03078de2014-06-25 12:35:08 +0200952 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;
961 }
962}
963
Felix Fietkau2967e032014-11-24 18:12:16 +0100964static void
965ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
966 const struct cfg80211_chan_def *chandef)
967{
968 struct ieee80211_sub_if_data *vlan;
969
970 sdata->vif.bss_conf.chandef = *chandef;
971
972 if (sdata->vif.type != NL80211_IFTYPE_AP)
973 return;
974
975 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
976 vlan->vif.bss_conf.chandef = *chandef;
977}
978
Michal Kazior5bcae312014-06-25 12:35:06 +0200979static int
980ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
Luciano Coelho11335a52013-10-30 13:09:39 +0200981{
982 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +0200983 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;
Luciano Coelho11335a52013-10-30 13:09:39 +0200988
989 lockdep_assert_held(&local->mtx);
Michal Kazior5bcae312014-06-25 12:35:06 +0200990 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +0200991
Michal Kazior5bcae312014-06-25 12:35:06 +0200992 new_ctx = sdata->reserved_chanctx;
993 old_ctx = ieee80211_vif_get_chanctx(sdata);
Luciano Coelho11335a52013-10-30 13:09:39 +0200994
Michal Kazior5bcae312014-06-25 12:35:06 +0200995 if (WARN_ON(!sdata->reserved_ready))
996 return -EBUSY;
997
998 if (WARN_ON(!new_ctx))
999 return -EINVAL;
1000
1001 if (WARN_ON(!old_ctx))
1002 return -EINVAL;
1003
1004 if (WARN_ON(new_ctx->replace_state ==
1005 IEEE80211_CHANCTX_REPLACES_OTHER))
1006 return -EINVAL;
1007
1008 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1009 &sdata->reserved_chandef);
1010 if (WARN_ON(!chandef))
1011 return -EINVAL;
1012
1013 vif_chsw[0].vif = &sdata->vif;
1014 vif_chsw[0].old_ctx = &old_ctx->conf;
1015 vif_chsw[0].new_ctx = &new_ctx->conf;
1016
1017 list_del(&sdata->reserved_chanctx_list);
1018 sdata->reserved_chanctx = NULL;
1019
1020 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1021 CHANCTX_SWMODE_REASSIGN_VIF);
1022 if (err) {
1023 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1024 ieee80211_free_chanctx(local, new_ctx);
1025
Michal Kazior03078de2014-06-25 12:35:08 +02001026 goto out;
Luciano Coelho11335a52013-10-30 13:09:39 +02001027 }
1028
Michal Kazior5bcae312014-06-25 12:35:06 +02001029 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1030 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
Luciano Coelho11335a52013-10-30 13:09:39 +02001031
Michal Kazior5bcae312014-06-25 12:35:06 +02001032 if (sdata->vif.type == NL80211_IFTYPE_AP)
1033 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1034
Johannes Berg17c18bf2015-03-21 15:25:43 +01001035 ieee80211_check_fast_xmit_iface(sdata);
1036
Michal Kazior5bcae312014-06-25 12:35:06 +02001037 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1038 ieee80211_free_chanctx(local, old_ctx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001039
1040 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
Michal Kazior5bcae312014-06-25 12:35:06 +02001041 changed = BSS_CHANGED_BANDWIDTH;
Luciano Coelho11335a52013-10-30 13:09:39 +02001042
Felix Fietkau2967e032014-11-24 18:12:16 +01001043 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
Luciano Coelho11335a52013-10-30 13:09:39 +02001044
Emmanuel Grumbach722ddb02014-11-30 17:17:26 +02001045 ieee80211_recalc_smps_chanctx(local, new_ctx);
1046 ieee80211_recalc_radar_chanctx(local, new_ctx);
1047 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1048
Michal Kazior5bcae312014-06-25 12:35:06 +02001049 if (changed)
1050 ieee80211_bss_info_change_notify(sdata, changed);
Luciano Coelho11335a52013-10-30 13:09:39 +02001051
Michal Kazior03078de2014-06-25 12:35:08 +02001052out:
1053 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001054 return err;
1055}
1056
1057static int
1058ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1059{
1060 struct ieee80211_local *local = sdata->local;
1061 struct ieee80211_chanctx *old_ctx, *new_ctx;
1062 const struct cfg80211_chan_def *chandef;
1063 int err;
1064
1065 old_ctx = ieee80211_vif_get_chanctx(sdata);
1066 new_ctx = sdata->reserved_chanctx;
1067
1068 if (WARN_ON(!sdata->reserved_ready))
1069 return -EINVAL;
1070
1071 if (WARN_ON(old_ctx))
1072 return -EINVAL;
1073
1074 if (WARN_ON(!new_ctx))
1075 return -EINVAL;
1076
1077 if (WARN_ON(new_ctx->replace_state ==
1078 IEEE80211_CHANCTX_REPLACES_OTHER))
1079 return -EINVAL;
1080
1081 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1082 &sdata->reserved_chandef);
1083 if (WARN_ON(!chandef))
1084 return -EINVAL;
1085
1086 list_del(&sdata->reserved_chanctx_list);
1087 sdata->reserved_chanctx = NULL;
1088
1089 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1090 if (err) {
1091 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1092 ieee80211_free_chanctx(local, new_ctx);
1093
1094 goto out;
1095 }
1096
1097out:
Michal Kazior03078de2014-06-25 12:35:08 +02001098 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001099 return err;
1100}
1101
1102static bool
1103ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1104{
1105 struct ieee80211_chanctx *old_ctx, *new_ctx;
1106
1107 lockdep_assert_held(&sdata->local->chanctx_mtx);
1108
1109 new_ctx = sdata->reserved_chanctx;
1110 old_ctx = ieee80211_vif_get_chanctx(sdata);
1111
1112 if (!old_ctx)
1113 return false;
1114
1115 if (WARN_ON(!new_ctx))
1116 return false;
1117
1118 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1119 return false;
1120
1121 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1122 return false;
1123
1124 return true;
1125}
1126
1127static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1128 struct ieee80211_chanctx *new_ctx)
1129{
1130 const struct cfg80211_chan_def *chandef;
1131
1132 lockdep_assert_held(&local->mtx);
1133 lockdep_assert_held(&local->chanctx_mtx);
1134
1135 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1136 if (WARN_ON(!chandef))
1137 return -EINVAL;
1138
1139 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1140 local->_oper_chandef = *chandef;
1141 ieee80211_hw_config(local, 0);
1142
1143 return 0;
1144}
1145
1146static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1147 int n_vifs)
1148{
1149 struct ieee80211_vif_chanctx_switch *vif_chsw;
1150 struct ieee80211_sub_if_data *sdata;
1151 struct ieee80211_chanctx *ctx, *old_ctx;
1152 int i, err;
1153
1154 lockdep_assert_held(&local->mtx);
1155 lockdep_assert_held(&local->chanctx_mtx);
1156
1157 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1158 if (!vif_chsw)
1159 return -ENOMEM;
1160
1161 i = 0;
1162 list_for_each_entry(ctx, &local->chanctx_list, list) {
1163 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1164 continue;
1165
1166 if (WARN_ON(!ctx->replace_ctx)) {
1167 err = -EINVAL;
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001168 goto out;
1169 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001170
Michal Kazior5bcae312014-06-25 12:35:06 +02001171 list_for_each_entry(sdata, &ctx->reserved_vifs,
1172 reserved_chanctx_list) {
1173 if (!ieee80211_vif_has_in_place_reservation(
1174 sdata))
1175 continue;
1176
1177 old_ctx = ieee80211_vif_get_chanctx(sdata);
1178 vif_chsw[i].vif = &sdata->vif;
1179 vif_chsw[i].old_ctx = &old_ctx->conf;
1180 vif_chsw[i].new_ctx = &ctx->conf;
1181
1182 i++;
1183 }
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001184 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001185
Michal Kazior5bcae312014-06-25 12:35:06 +02001186 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1187 CHANCTX_SWMODE_SWAP_CONTEXTS);
Luciano Coelho11335a52013-10-30 13:09:39 +02001188
Luciano Coelho11335a52013-10-30 13:09:39 +02001189out:
Michal Kazior5bcae312014-06-25 12:35:06 +02001190 kfree(vif_chsw);
1191 return err;
1192}
1193
1194static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1195{
1196 struct ieee80211_chanctx *ctx;
1197 int err;
1198
1199 lockdep_assert_held(&local->mtx);
1200 lockdep_assert_held(&local->chanctx_mtx);
1201
1202 list_for_each_entry(ctx, &local->chanctx_list, list) {
1203 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1204 continue;
1205
1206 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1207 continue;
1208
1209 ieee80211_del_chanctx(local, ctx->replace_ctx);
1210 err = ieee80211_add_chanctx(local, ctx);
1211 if (err)
1212 goto err;
1213 }
1214
1215 return 0;
1216
1217err:
1218 WARN_ON(ieee80211_add_chanctx(local, ctx));
1219 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1220 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1221 continue;
1222
1223 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1224 continue;
1225
1226 ieee80211_del_chanctx(local, ctx);
1227 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1228 }
1229
1230 return err;
1231}
1232
Johannes Berg649b2a42014-07-25 15:01:59 +02001233static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
Michal Kazior5bcae312014-06-25 12:35:06 +02001234{
1235 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1236 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1237 struct ieee80211_chanctx *new_ctx = NULL;
1238 int i, err, n_assigned, n_reserved, n_ready;
1239 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1240
1241 lockdep_assert_held(&local->mtx);
1242 lockdep_assert_held(&local->chanctx_mtx);
1243
1244 /*
1245 * If there are 2 independent pairs of channel contexts performing
1246 * cross-switch of their vifs this code will still wait until both are
1247 * ready even though it could be possible to switch one before the
1248 * other is ready.
1249 *
1250 * For practical reasons and code simplicity just do a single huge
1251 * switch.
1252 */
1253
1254 /*
1255 * Verify if the reservation is still feasible.
1256 * - if it's not then disconnect
1257 * - if it is but not all vifs necessary are ready then defer
1258 */
1259
1260 list_for_each_entry(ctx, &local->chanctx_list, list) {
1261 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1262 continue;
1263
1264 if (WARN_ON(!ctx->replace_ctx)) {
1265 err = -EINVAL;
1266 goto err;
1267 }
1268
1269 if (!local->use_chanctx)
1270 new_ctx = ctx;
1271
1272 n_ctx++;
1273
1274 n_assigned = 0;
1275 n_reserved = 0;
1276 n_ready = 0;
1277
1278 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1279 assigned_chanctx_list) {
1280 n_assigned++;
1281 if (sdata->reserved_chanctx) {
1282 n_reserved++;
1283 if (sdata->reserved_ready)
1284 n_ready++;
1285 }
1286 }
1287
1288 if (n_assigned != n_reserved) {
1289 if (n_ready == n_reserved) {
1290 wiphy_info(local->hw.wiphy,
1291 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1292 err = -EBUSY;
1293 goto err;
1294 }
1295
1296 return -EAGAIN;
1297 }
1298
1299 ctx->conf.radar_enabled = false;
1300 list_for_each_entry(sdata, &ctx->reserved_vifs,
1301 reserved_chanctx_list) {
1302 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1303 !sdata->reserved_ready)
1304 return -EAGAIN;
1305
1306 old_ctx = ieee80211_vif_get_chanctx(sdata);
1307 if (old_ctx) {
1308 if (old_ctx->replace_state ==
1309 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1310 n_vifs_switch++;
1311 else
1312 n_vifs_assign++;
1313 } else {
1314 n_vifs_ctxless++;
1315 }
1316
1317 if (sdata->reserved_radar_required)
1318 ctx->conf.radar_enabled = true;
1319 }
1320 }
1321
1322 if (WARN_ON(n_ctx == 0) ||
1323 WARN_ON(n_vifs_switch == 0 &&
1324 n_vifs_assign == 0 &&
1325 n_vifs_ctxless == 0) ||
1326 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1327 WARN_ON(!new_ctx && !local->use_chanctx)) {
1328 err = -EINVAL;
1329 goto err;
1330 }
1331
1332 /*
1333 * All necessary vifs are ready. Perform the switch now depending on
1334 * reservations and driver capabilities.
1335 */
1336
1337 if (local->use_chanctx) {
1338 if (n_vifs_switch > 0) {
1339 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1340 if (err)
1341 goto err;
1342 }
1343
1344 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1345 err = ieee80211_chsw_switch_ctxs(local);
1346 if (err)
1347 goto err;
1348 }
1349 } else {
1350 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1351 if (err)
1352 goto err;
1353 }
1354
1355 /*
1356 * Update all structures, values and pointers to point to new channel
1357 * context(s).
1358 */
1359
1360 i = 0;
1361 list_for_each_entry(ctx, &local->chanctx_list, list) {
1362 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1363 continue;
1364
1365 if (WARN_ON(!ctx->replace_ctx)) {
1366 err = -EINVAL;
1367 goto err;
1368 }
1369
1370 list_for_each_entry(sdata, &ctx->reserved_vifs,
1371 reserved_chanctx_list) {
1372 u32 changed = 0;
1373
1374 if (!ieee80211_vif_has_in_place_reservation(sdata))
1375 continue;
1376
1377 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1378
1379 if (sdata->vif.type == NL80211_IFTYPE_AP)
1380 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1381 false);
1382
Johannes Berg17c18bf2015-03-21 15:25:43 +01001383 ieee80211_check_fast_xmit_iface(sdata);
1384
Michal Kazior5bcae312014-06-25 12:35:06 +02001385 sdata->radar_required = sdata->reserved_radar_required;
1386
1387 if (sdata->vif.bss_conf.chandef.width !=
1388 sdata->reserved_chandef.width)
1389 changed = BSS_CHANGED_BANDWIDTH;
1390
Felix Fietkau2967e032014-11-24 18:12:16 +01001391 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
Michal Kazior5bcae312014-06-25 12:35:06 +02001392 if (changed)
1393 ieee80211_bss_info_change_notify(sdata,
1394 changed);
1395
Lorenzo Bianconidb82d8a2015-01-14 12:55:08 +01001396 ieee80211_recalc_txpower(sdata, false);
Michal Kazior5bcae312014-06-25 12:35:06 +02001397 }
1398
1399 ieee80211_recalc_chanctx_chantype(local, ctx);
1400 ieee80211_recalc_smps_chanctx(local, ctx);
1401 ieee80211_recalc_radar_chanctx(local, ctx);
1402 ieee80211_recalc_chanctx_min_def(local, ctx);
1403
1404 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1405 reserved_chanctx_list) {
1406 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1407 continue;
1408
1409 list_del(&sdata->reserved_chanctx_list);
1410 list_move(&sdata->assigned_chanctx_list,
Michal Kazior47e4df92014-08-18 13:19:09 +02001411 &ctx->assigned_vifs);
Michal Kazior5bcae312014-06-25 12:35:06 +02001412 sdata->reserved_chanctx = NULL;
Michal Kazior03078de2014-06-25 12:35:08 +02001413
1414 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001415 }
1416
1417 /*
1418 * This context might have been a dependency for an already
1419 * ready re-assign reservation interface that was deferred. Do
1420 * not propagate error to the caller though. The in-place
1421 * reservation for originally requested interface has already
1422 * succeeded at this point.
1423 */
1424 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1425 reserved_chanctx_list) {
1426 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1427 sdata)))
1428 continue;
1429
1430 if (WARN_ON(sdata->reserved_chanctx != ctx))
1431 continue;
1432
1433 if (!sdata->reserved_ready)
1434 continue;
1435
1436 if (ieee80211_vif_get_chanctx(sdata))
1437 err = ieee80211_vif_use_reserved_reassign(
1438 sdata);
1439 else
1440 err = ieee80211_vif_use_reserved_assign(sdata);
1441
1442 if (err) {
1443 sdata_info(sdata,
1444 "failed to finalize (re-)assign reservation (err=%d)\n",
1445 err);
1446 ieee80211_vif_unreserve_chanctx(sdata);
1447 cfg80211_stop_iface(local->hw.wiphy,
1448 &sdata->wdev,
1449 GFP_KERNEL);
1450 }
1451 }
1452 }
1453
1454 /*
1455 * Finally free old contexts
1456 */
1457
1458 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1459 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1460 continue;
1461
1462 ctx->replace_ctx->replace_ctx = NULL;
1463 ctx->replace_ctx->replace_state =
1464 IEEE80211_CHANCTX_REPLACE_NONE;
1465
1466 list_del_rcu(&ctx->list);
1467 kfree_rcu(ctx, rcu_head);
1468 }
1469
1470 return 0;
1471
1472err:
1473 list_for_each_entry(ctx, &local->chanctx_list, list) {
1474 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1475 continue;
1476
1477 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
Michal Kazior03078de2014-06-25 12:35:08 +02001478 reserved_chanctx_list) {
Michal Kazior5bcae312014-06-25 12:35:06 +02001479 ieee80211_vif_unreserve_chanctx(sdata);
Michal Kazior03078de2014-06-25 12:35:08 +02001480 ieee80211_vif_chanctx_reservation_complete(sdata);
1481 }
Michal Kazior5bcae312014-06-25 12:35:06 +02001482 }
1483
1484 return err;
1485}
1486
Johannes Berg649b2a42014-07-25 15:01:59 +02001487static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1488{
1489 struct ieee80211_local *local = sdata->local;
1490 struct ieee80211_chanctx_conf *conf;
1491 struct ieee80211_chanctx *ctx;
1492 bool use_reserved_switch = false;
1493
1494 lockdep_assert_held(&local->chanctx_mtx);
1495
1496 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1497 lockdep_is_held(&local->chanctx_mtx));
1498 if (!conf)
1499 return;
1500
1501 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1502
1503 if (sdata->reserved_chanctx) {
1504 if (sdata->reserved_chanctx->replace_state ==
1505 IEEE80211_CHANCTX_REPLACES_OTHER &&
1506 ieee80211_chanctx_num_reserved(local,
1507 sdata->reserved_chanctx) > 1)
1508 use_reserved_switch = true;
1509
1510 ieee80211_vif_unreserve_chanctx(sdata);
1511 }
1512
1513 ieee80211_assign_vif_chanctx(sdata, NULL);
1514 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1515 ieee80211_free_chanctx(local, ctx);
1516
Eliad Peller104f5a62015-02-08 12:36:07 +02001517 sdata->radar_required = false;
1518
Johannes Berg649b2a42014-07-25 15:01:59 +02001519 /* Unreserving may ready an in-place reservation. */
1520 if (use_reserved_switch)
1521 ieee80211_vif_use_reserved_switch(local);
1522}
1523
1524int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1525 const struct cfg80211_chan_def *chandef,
1526 enum ieee80211_chanctx_mode mode)
1527{
1528 struct ieee80211_local *local = sdata->local;
1529 struct ieee80211_chanctx *ctx;
1530 u8 radar_detect_width = 0;
1531 int ret;
1532
1533 lockdep_assert_held(&local->mtx);
1534
1535 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1536
1537 mutex_lock(&local->chanctx_mtx);
1538
1539 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1540 chandef,
1541 sdata->wdev.iftype);
1542 if (ret < 0)
1543 goto out;
1544 if (ret > 0)
1545 radar_detect_width = BIT(chandef->width);
1546
1547 sdata->radar_required = ret;
1548
1549 ret = ieee80211_check_combinations(sdata, chandef, mode,
1550 radar_detect_width);
1551 if (ret < 0)
1552 goto out;
1553
1554 __ieee80211_vif_release_channel(sdata);
1555
1556 ctx = ieee80211_find_chanctx(local, chandef, mode);
1557 if (!ctx)
1558 ctx = ieee80211_new_chanctx(local, chandef, mode);
1559 if (IS_ERR(ctx)) {
1560 ret = PTR_ERR(ctx);
1561 goto out;
1562 }
1563
Felix Fietkau2967e032014-11-24 18:12:16 +01001564 ieee80211_vif_update_chandef(sdata, chandef);
Johannes Berg649b2a42014-07-25 15:01:59 +02001565
1566 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1567 if (ret) {
1568 /* if assign fails refcount stays the same */
1569 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1570 ieee80211_free_chanctx(local, ctx);
1571 goto out;
1572 }
1573
1574 ieee80211_recalc_smps_chanctx(local, ctx);
1575 ieee80211_recalc_radar_chanctx(local, ctx);
1576 out:
Eliad Peller104f5a62015-02-08 12:36:07 +02001577 if (ret)
1578 sdata->radar_required = false;
1579
Johannes Berg649b2a42014-07-25 15:01:59 +02001580 mutex_unlock(&local->chanctx_mtx);
1581 return ret;
1582}
1583
Michal Kazior5bcae312014-06-25 12:35:06 +02001584int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1585{
1586 struct ieee80211_local *local = sdata->local;
1587 struct ieee80211_chanctx *new_ctx;
1588 struct ieee80211_chanctx *old_ctx;
1589 int err;
1590
1591 lockdep_assert_held(&local->mtx);
1592 lockdep_assert_held(&local->chanctx_mtx);
1593
1594 new_ctx = sdata->reserved_chanctx;
1595 old_ctx = ieee80211_vif_get_chanctx(sdata);
1596
1597 if (WARN_ON(!new_ctx))
1598 return -EINVAL;
1599
1600 if (WARN_ON(new_ctx->replace_state ==
1601 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1602 return -EINVAL;
1603
1604 if (WARN_ON(sdata->reserved_ready))
1605 return -EINVAL;
1606
1607 sdata->reserved_ready = true;
1608
1609 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1610 if (old_ctx)
1611 err = ieee80211_vif_use_reserved_reassign(sdata);
1612 else
1613 err = ieee80211_vif_use_reserved_assign(sdata);
1614
1615 if (err)
1616 return err;
1617 }
1618
1619 /*
1620 * In-place reservation may need to be finalized now either if:
1621 * a) sdata is taking part in the swapping itself and is the last one
1622 * b) sdata has switched with a re-assign reservation to an existing
1623 * context readying in-place switching of old_ctx
1624 *
1625 * In case of (b) do not propagate the error up because the requested
1626 * sdata already switched successfully. Just spill an extra warning.
1627 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1628 * interfaces upon failure.
1629 */
1630 if ((old_ctx &&
1631 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1632 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1633 err = ieee80211_vif_use_reserved_switch(local);
1634 if (err && err != -EAGAIN) {
1635 if (new_ctx->replace_state ==
1636 IEEE80211_CHANCTX_REPLACES_OTHER)
1637 return err;
1638
1639 wiphy_info(local->hw.wiphy,
1640 "depending in-place reservation failed (err=%d)\n",
1641 err);
1642 }
1643 }
1644
1645 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +02001646}
1647
Johannes Berg2c9b7352013-02-07 21:37:29 +01001648int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1649 const struct cfg80211_chan_def *chandef,
1650 u32 *changed)
1651{
1652 struct ieee80211_local *local = sdata->local;
1653 struct ieee80211_chanctx_conf *conf;
1654 struct ieee80211_chanctx *ctx;
Michal Kazior5bcae312014-06-25 12:35:06 +02001655 const struct cfg80211_chan_def *compat;
Johannes Berg2c9b7352013-02-07 21:37:29 +01001656 int ret;
1657
1658 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1659 IEEE80211_CHAN_DISABLED))
1660 return -EINVAL;
1661
1662 mutex_lock(&local->chanctx_mtx);
1663 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1664 ret = 0;
1665 goto out;
1666 }
1667
1668 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1669 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1670 ret = -EINVAL;
1671 goto out;
1672 }
1673
1674 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1675 lockdep_is_held(&local->chanctx_mtx));
1676 if (!conf) {
1677 ret = -EINVAL;
1678 goto out;
1679 }
1680
1681 ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior5bcae312014-06-25 12:35:06 +02001682
1683 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1684 if (!compat) {
Johannes Berg2c9b7352013-02-07 21:37:29 +01001685 ret = -EINVAL;
1686 goto out;
1687 }
1688
Michal Kazior5bcae312014-06-25 12:35:06 +02001689 switch (ctx->replace_state) {
1690 case IEEE80211_CHANCTX_REPLACE_NONE:
1691 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1692 ret = -EBUSY;
1693 goto out;
1694 }
1695 break;
1696 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
Stephen Hemmingerd070f912014-10-29 22:55:58 -07001697 /* TODO: Perhaps the bandwidth change could be treated as a
Michal Kazior5bcae312014-06-25 12:35:06 +02001698 * reservation itself? */
1699 ret = -EBUSY;
1700 goto out;
1701 case IEEE80211_CHANCTX_REPLACES_OTHER:
1702 /* channel context that is going to replace another channel
1703 * context doesn't really exist and shouldn't be assigned
1704 * anywhere yet */
1705 WARN_ON(1);
1706 break;
1707 }
1708
Felix Fietkau2967e032014-11-24 18:12:16 +01001709 ieee80211_vif_update_chandef(sdata, chandef);
Johannes Berg2c9b7352013-02-07 21:37:29 +01001710
1711 ieee80211_recalc_chanctx_chantype(local, ctx);
1712
1713 *changed |= BSS_CHANGED_BANDWIDTH;
1714 ret = 0;
1715 out:
1716 mutex_unlock(&local->chanctx_mtx);
1717 return ret;
1718}
1719
Michal Kaziord01a1e62012-06-26 14:37:16 +02001720void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1721{
Johannes Berg55de9082012-07-26 17:24:39 +02001722 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1723
Johannes Berg34a37402013-12-18 09:43:33 +01001724 lockdep_assert_held(&sdata->local->mtx);
1725
Michal Kaziord01a1e62012-06-26 14:37:16 +02001726 mutex_lock(&sdata->local->chanctx_mtx);
1727 __ieee80211_vif_release_channel(sdata);
1728 mutex_unlock(&sdata->local->chanctx_mtx);
1729}
Johannes Berg3448c002012-09-11 17:57:42 +02001730
Johannes Berg4d76d212012-12-11 20:38:41 +01001731void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1732{
1733 struct ieee80211_local *local = sdata->local;
1734 struct ieee80211_sub_if_data *ap;
1735 struct ieee80211_chanctx_conf *conf;
1736
1737 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1738 return;
1739
1740 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1741
1742 mutex_lock(&local->chanctx_mtx);
1743
1744 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1745 lockdep_is_held(&local->chanctx_mtx));
1746 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1747 mutex_unlock(&local->chanctx_mtx);
1748}
1749
Johannes Berg3448c002012-09-11 17:57:42 +02001750void ieee80211_iter_chan_contexts_atomic(
1751 struct ieee80211_hw *hw,
1752 void (*iter)(struct ieee80211_hw *hw,
1753 struct ieee80211_chanctx_conf *chanctx_conf,
1754 void *data),
1755 void *iter_data)
1756{
1757 struct ieee80211_local *local = hw_to_local(hw);
1758 struct ieee80211_chanctx *ctx;
1759
1760 rcu_read_lock();
1761 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +01001762 if (ctx->driver_present)
1763 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +02001764 rcu_read_unlock();
1765}
1766EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);