blob: e75cbf6ecc26e4ec7bde777c385e001c0e49e371 [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
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300193enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
Eliad Peller21f659b2013-11-11 20:14:01 +0200194{
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
Ilan Peera7201a62015-12-13 13:41:43 +0200234 if (!sta->uploaded || !test_sta_flag(sta, WLAN_STA_ASSOC))
Eliad Peller21f659b2013-11-11 20:14:01 +0200235 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;
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300267 case NL80211_IFTYPE_STATION:
268 /*
269 * The ap's sta->bandwidth is not set yet at this
270 * point, so take the width from the chandef, but
271 * account also for TDLS peers
272 */
273 width = max(vif->bss_conf.chandef.width,
274 ieee80211_get_max_required_bw(sdata));
275 break;
Eliad Peller21f659b2013-11-11 20:14:01 +0200276 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300277 case NL80211_IFTYPE_NAN:
Eliad Peller21f659b2013-11-11 20:14:01 +0200278 continue;
Eliad Peller21f659b2013-11-11 20:14:01 +0200279 case NL80211_IFTYPE_ADHOC:
280 case NL80211_IFTYPE_WDS:
281 case NL80211_IFTYPE_MESH_POINT:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100282 case NL80211_IFTYPE_OCB:
Eliad Peller21f659b2013-11-11 20:14:01 +0200283 width = vif->bss_conf.chandef.width;
284 break;
285 case NL80211_IFTYPE_UNSPECIFIED:
286 case NUM_NL80211_IFTYPES:
287 case NL80211_IFTYPE_MONITOR:
288 case NL80211_IFTYPE_P2P_CLIENT:
289 case NL80211_IFTYPE_P2P_GO:
290 WARN_ON_ONCE(1);
291 }
292 max_bw = max(max_bw, width);
293 }
Eliad Peller1c37a722014-03-03 13:37:14 +0200294
295 /* use the configured bandwidth in case of monitor interface */
296 sdata = rcu_dereference(local->monitor_sdata);
297 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
298 max_bw = max(max_bw, conf->def.width);
299
Eliad Peller21f659b2013-11-11 20:14:01 +0200300 rcu_read_unlock();
301
302 return max_bw;
303}
304
305/*
306 * recalc the min required chan width of the channel context, which is
307 * the max of min required widths of all the interfaces bound to this
308 * channel context.
309 */
310void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
311 struct ieee80211_chanctx *ctx)
312{
313 enum nl80211_chan_width max_bw;
314 struct cfg80211_chan_def min_def;
315
316 lockdep_assert_held(&local->chanctx_mtx);
317
318 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
319 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
320 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
321 ctx->conf.radar_enabled) {
322 ctx->conf.min_def = ctx->conf.def;
323 return;
324 }
325
326 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
327
328 /* downgrade chandef up to max_bw */
329 min_def = ctx->conf.def;
330 while (min_def.width > max_bw)
331 ieee80211_chandef_downgrade(&min_def);
332
333 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
334 return;
335
336 ctx->conf.min_def = min_def;
337 if (!ctx->driver_present)
338 return;
339
340 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
341}
342
Johannes Berg18942d32013-02-07 21:30:37 +0100343static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100344 struct ieee80211_chanctx *ctx,
345 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200346{
Arik Nemtsovaa507a72016-03-02 23:28:33 +0200347 if (cfg80211_chandef_identical(&ctx->conf.def, chandef)) {
348 ieee80211_recalc_chanctx_min_def(local, ctx);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200349 return;
Arik Nemtsovaa507a72016-03-02 23:28:33 +0200350 }
Michal Kaziore89a96f2012-06-26 14:37:22 +0200351
Johannes Berg4bf88532012-11-09 11:39:59 +0100352 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
353
354 ctx->conf.def = *chandef;
355 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Eliad Peller21f659b2013-11-11 20:14:01 +0200356 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200357
358 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100359 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200360 ieee80211_hw_config(local, 0);
361 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200362}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200363
364static struct ieee80211_chanctx *
365ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100366 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200367 enum ieee80211_chanctx_mode mode)
368{
369 struct ieee80211_chanctx *ctx;
370
371 lockdep_assert_held(&local->chanctx_mtx);
372
373 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
374 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200375
376 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100377 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200378
Michal Kazior5bcae312014-06-25 12:35:06 +0200379 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
380 continue;
381
Michal Kaziord01a1e62012-06-26 14:37:16 +0200382 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
383 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100384
385 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
386 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200387 continue;
388
Michal Kazior02881572014-04-09 15:29:28 +0200389 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
390 compat);
391 if (!compat)
392 continue;
393
Johannes Berg18942d32013-02-07 21:30:37 +0100394 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200395
Michal Kaziord01a1e62012-06-26 14:37:16 +0200396 return ctx;
397 }
398
399 return NULL;
400}
401
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200402bool ieee80211_is_radar_required(struct ieee80211_local *local)
Simon Wunderliche4746852013-04-08 22:43:16 +0200403{
404 struct ieee80211_sub_if_data *sdata;
405
Michal Kaziorcc901de2014-01-29 07:56:20 +0100406 lockdep_assert_held(&local->mtx);
407
Simon Wunderliche4746852013-04-08 22:43:16 +0200408 rcu_read_lock();
409 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
410 if (sdata->radar_required) {
411 rcu_read_unlock();
412 return true;
413 }
414 }
415 rcu_read_unlock();
416
417 return false;
418}
419
Eliad Pellere7f23372015-01-07 17:50:10 +0200420static bool
421ieee80211_chanctx_radar_required(struct ieee80211_local *local,
422 struct ieee80211_chanctx *ctx)
423{
424 struct ieee80211_chanctx_conf *conf = &ctx->conf;
425 struct ieee80211_sub_if_data *sdata;
426 bool required = false;
427
428 lockdep_assert_held(&local->chanctx_mtx);
429 lockdep_assert_held(&local->mtx);
430
431 rcu_read_lock();
432 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
433 if (!ieee80211_sdata_running(sdata))
434 continue;
435 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
436 continue;
437 if (!sdata->radar_required)
438 continue;
439
440 required = true;
441 break;
442 }
443 rcu_read_unlock();
444
445 return required;
446}
447
Michal Kaziord01a1e62012-06-26 14:37:16 +0200448static struct ieee80211_chanctx *
Michal Kaziored68ebc2014-04-09 15:29:30 +0200449ieee80211_alloc_chanctx(struct ieee80211_local *local,
450 const struct cfg80211_chan_def *chandef,
451 enum ieee80211_chanctx_mode mode)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200452{
453 struct ieee80211_chanctx *ctx;
454
455 lockdep_assert_held(&local->chanctx_mtx);
456
457 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
458 if (!ctx)
Michal Kaziored68ebc2014-04-09 15:29:30 +0200459 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200460
Michal Kazior484298a2014-04-09 15:29:26 +0200461 INIT_LIST_HEAD(&ctx->assigned_vifs);
Michal Kaziore3afb922014-04-09 15:29:27 +0200462 INIT_LIST_HEAD(&ctx->reserved_vifs);
Johannes Berg4bf88532012-11-09 11:39:59 +0100463 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200464 ctx->conf.rx_chains_static = 1;
465 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200466 ctx->mode = mode;
Eliad Pellere7f23372015-01-07 17:50:10 +0200467 ctx->conf.radar_enabled = false;
Eliad Peller21f659b2013-11-11 20:14:01 +0200468 ieee80211_recalc_chanctx_min_def(local, ctx);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200469
470 return ctx;
471}
472
473static int ieee80211_add_chanctx(struct ieee80211_local *local,
474 struct ieee80211_chanctx *ctx)
475{
476 u32 changed;
477 int err;
478
479 lockdep_assert_held(&local->mtx);
480 lockdep_assert_held(&local->chanctx_mtx);
481
Simon Wunderliche4746852013-04-08 22:43:16 +0200482 if (!local->use_chanctx)
483 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200484
Johannes Berg382a1032013-03-22 22:30:09 +0100485 /* turn idle off *before* setting channel -- some drivers need that */
486 changed = ieee80211_idle_off(local);
487 if (changed)
488 ieee80211_hw_config(local, changed);
489
Johannes Berg55de9082012-07-26 17:24:39 +0200490 if (!local->use_chanctx) {
Michal Kaziored68ebc2014-04-09 15:29:30 +0200491 local->_oper_chandef = ctx->conf.def;
Michal Kazior9b4816f2014-04-04 13:02:43 +0200492 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg55de9082012-07-26 17:24:39 +0200493 } else {
494 err = drv_add_chanctx(local, ctx);
495 if (err) {
Johannes Berg382a1032013-03-22 22:30:09 +0100496 ieee80211_recalc_idle(local);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200497 return err;
Johannes Berg55de9082012-07-26 17:24:39 +0200498 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200499 }
500
Michal Kaziored68ebc2014-04-09 15:29:30 +0200501 return 0;
502}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200503
Michal Kaziored68ebc2014-04-09 15:29:30 +0200504static struct ieee80211_chanctx *
505ieee80211_new_chanctx(struct ieee80211_local *local,
506 const struct cfg80211_chan_def *chandef,
507 enum ieee80211_chanctx_mode mode)
508{
509 struct ieee80211_chanctx *ctx;
510 int err;
511
512 lockdep_assert_held(&local->mtx);
513 lockdep_assert_held(&local->chanctx_mtx);
514
515 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
516 if (!ctx)
517 return ERR_PTR(-ENOMEM);
518
519 err = ieee80211_add_chanctx(local, ctx);
520 if (err) {
521 kfree(ctx);
522 return ERR_PTR(err);
523 }
524
525 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200526 return ctx;
527}
528
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200529static void ieee80211_del_chanctx(struct ieee80211_local *local,
530 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200531{
532 lockdep_assert_held(&local->chanctx_mtx);
533
Johannes Berg55de9082012-07-26 17:24:39 +0200534 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100535 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
536 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
537 chandef->center_freq1 = chandef->chan->center_freq;
538 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200539
540 /* NOTE: Disabling radar is only valid here for
541 * single channel context. To be sure, check it ...
542 */
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200543 WARN_ON(local->hw.conf.radar_enabled &&
544 !list_empty(&local->chanctx_list));
545
Simon Wunderliche4746852013-04-08 22:43:16 +0200546 local->hw.conf.radar_enabled = false;
547
Michal Kazior9b4816f2014-04-04 13:02:43 +0200548 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg55de9082012-07-26 17:24:39 +0200549 } else {
550 drv_remove_chanctx(local, ctx);
551 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200552
Johannes Bergfd0f9792013-02-07 00:14:51 +0100553 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200554}
555
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200556static void ieee80211_free_chanctx(struct ieee80211_local *local,
557 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200558{
Michal Kaziord01a1e62012-06-26 14:37:16 +0200559 lockdep_assert_held(&local->chanctx_mtx);
560
Michal Kaziorc0166da2014-04-09 15:29:33 +0200561 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200562
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200563 list_del_rcu(&ctx->list);
564 ieee80211_del_chanctx(local, ctx);
565 kfree_rcu(ctx, rcu_head);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200566}
567
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300568void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
569 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200570{
571 struct ieee80211_chanctx_conf *conf = &ctx->conf;
572 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100573 const struct cfg80211_chan_def *compat = NULL;
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300574 struct sta_info *sta;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200575
576 lockdep_assert_held(&local->chanctx_mtx);
577
578 rcu_read_lock();
579 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100580
Michal Kaziore89a96f2012-06-26 14:37:22 +0200581 if (!ieee80211_sdata_running(sdata))
582 continue;
583 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
584 continue;
Felix Fietkau0e67c132014-07-25 16:20:22 +0200585 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
586 continue;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200587
Johannes Berg4bf88532012-11-09 11:39:59 +0100588 if (!compat)
589 compat = &sdata->vif.bss_conf.chandef;
590
591 compat = cfg80211_chandef_compatible(
592 &sdata->vif.bss_conf.chandef, compat);
Michal Kaziora00f4f62014-07-28 15:16:59 +0200593 if (WARN_ON_ONCE(!compat))
Johannes Berg4bf88532012-11-09 11:39:59 +0100594 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200595 }
Arik Nemtsov0fabfaa2015-06-10 20:41:23 +0300596
597 /* TDLS peers can sometimes affect the chandef width */
598 list_for_each_entry_rcu(sta, &local->sta_list, list) {
599 if (!sta->uploaded ||
600 !test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) ||
601 !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
602 !sta->tdls_chandef.chan)
603 continue;
604
605 compat = cfg80211_chandef_compatible(&sta->tdls_chandef,
606 compat);
607 if (WARN_ON_ONCE(!compat))
608 break;
609 }
Michal Kaziore89a96f2012-06-26 14:37:22 +0200610 rcu_read_unlock();
611
Michal Kaziora00f4f62014-07-28 15:16:59 +0200612 if (!compat)
Johannes Berg4bf88532012-11-09 11:39:59 +0100613 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200614
Johannes Berg18942d32013-02-07 21:30:37 +0100615 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200616}
617
Johannes Berg367bbd12013-12-18 09:36:09 +0100618static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
619 struct ieee80211_chanctx *chanctx)
620{
621 bool radar_enabled;
622
623 lockdep_assert_held(&local->chanctx_mtx);
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200624 /* for ieee80211_is_radar_required */
Johannes Berg34a37402013-12-18 09:43:33 +0100625 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100626
Eliad Pellere7f23372015-01-07 17:50:10 +0200627 radar_enabled = ieee80211_chanctx_radar_required(local, chanctx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100628
629 if (radar_enabled == chanctx->conf.radar_enabled)
630 return;
631
632 chanctx->conf.radar_enabled = radar_enabled;
Johannes Berg367bbd12013-12-18 09:36:09 +0100633
634 if (!local->use_chanctx) {
635 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
636 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
637 }
638
639 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
640}
641
Luciano Coelho77eeba92014-03-11 18:24:12 +0200642static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
643 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200644{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200645 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200646 struct ieee80211_chanctx_conf *conf;
647 struct ieee80211_chanctx *curr_ctx = NULL;
648 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200649
Ayala Beker708d50e2016-09-20 17:31:14 +0300650 if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_NAN))
651 return -ENOTSUPP;
652
Luciano Coelho77eeba92014-03-11 18:24:12 +0200653 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
654 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200655
Luciano Coelho77eeba92014-03-11 18:24:12 +0200656 if (conf) {
657 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200658
Luciano Coelho77eeba92014-03-11 18:24:12 +0200659 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
660 conf = NULL;
Michal Kazior484298a2014-04-09 15:29:26 +0200661 list_del(&sdata->assigned_chanctx_list);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200662 }
663
664 if (new_ctx) {
665 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
666 if (ret)
667 goto out;
668
Luciano Coelho77eeba92014-03-11 18:24:12 +0200669 conf = &new_ctx->conf;
Michal Kazior484298a2014-04-09 15:29:26 +0200670 list_add(&sdata->assigned_chanctx_list,
671 &new_ctx->assigned_vifs);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200672 }
673
674out:
675 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
676
677 sdata->vif.bss_conf.idle = !conf;
678
Michal Kaziorc0166da2014-04-09 15:29:33 +0200679 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200680 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
681 ieee80211_recalc_smps_chanctx(local, curr_ctx);
682 ieee80211_recalc_radar_chanctx(local, curr_ctx);
683 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
684 }
685
Michal Kaziorc0166da2014-04-09 15:29:33 +0200686 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
Lorenzo Bianconidb82d8a2015-01-14 12:55:08 +0100687 ieee80211_recalc_txpower(sdata, false);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200688 ieee80211_recalc_chanctx_min_def(local, new_ctx);
689 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100690
691 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
692 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200693 ieee80211_bss_info_change_notify(sdata,
694 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100695
Johannes Berg17c18bf2015-03-21 15:25:43 +0100696 ieee80211_check_fast_xmit_iface(sdata);
697
Luciano Coelho77eeba92014-03-11 18:24:12 +0200698 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200699}
700
Johannes Berg04ecd252012-09-11 14:34:12 +0200701void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
702 struct ieee80211_chanctx *chanctx)
703{
704 struct ieee80211_sub_if_data *sdata;
705 u8 rx_chains_static, rx_chains_dynamic;
706
707 lockdep_assert_held(&local->chanctx_mtx);
708
709 rx_chains_static = 1;
710 rx_chains_dynamic = 1;
711
712 rcu_read_lock();
713 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
714 u8 needed_static, needed_dynamic;
715
716 if (!ieee80211_sdata_running(sdata))
717 continue;
718
719 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
720 &chanctx->conf)
721 continue;
722
723 switch (sdata->vif.type) {
724 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300725 case NL80211_IFTYPE_NAN:
Johannes Berg04ecd252012-09-11 14:34:12 +0200726 continue;
727 case NL80211_IFTYPE_STATION:
728 if (!sdata->u.mgd.associated)
729 continue;
730 break;
731 case NL80211_IFTYPE_AP_VLAN:
732 continue;
733 case NL80211_IFTYPE_AP:
734 case NL80211_IFTYPE_ADHOC:
735 case NL80211_IFTYPE_WDS:
736 case NL80211_IFTYPE_MESH_POINT:
Rostislav Lisovy239281f2014-11-03 10:33:19 +0100737 case NL80211_IFTYPE_OCB:
Johannes Berg04ecd252012-09-11 14:34:12 +0200738 break;
739 default:
740 WARN_ON_ONCE(1);
741 }
742
743 switch (sdata->smps_mode) {
744 default:
745 WARN_ONCE(1, "Invalid SMPS mode %d\n",
746 sdata->smps_mode);
747 /* fall through */
748 case IEEE80211_SMPS_OFF:
749 needed_static = sdata->needed_rx_chains;
750 needed_dynamic = sdata->needed_rx_chains;
751 break;
752 case IEEE80211_SMPS_DYNAMIC:
753 needed_static = 1;
754 needed_dynamic = sdata->needed_rx_chains;
755 break;
756 case IEEE80211_SMPS_STATIC:
757 needed_static = 1;
758 needed_dynamic = 1;
759 break;
760 }
761
762 rx_chains_static = max(rx_chains_static, needed_static);
763 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
764 }
Ido Yariv7b8a9cd2014-03-24 09:55:45 +0200765
766 /* Disable SMPS for the monitor interface */
767 sdata = rcu_dereference(local->monitor_sdata);
768 if (sdata &&
769 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
770 rx_chains_dynamic = rx_chains_static = local->rx_chains;
771
Johannes Berg04ecd252012-09-11 14:34:12 +0200772 rcu_read_unlock();
773
774 if (!local->use_chanctx) {
775 if (rx_chains_static > 1)
776 local->smps_mode = IEEE80211_SMPS_OFF;
777 else if (rx_chains_dynamic > 1)
778 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
779 else
780 local->smps_mode = IEEE80211_SMPS_STATIC;
781 ieee80211_hw_config(local, 0);
782 }
783
784 if (rx_chains_static == chanctx->conf.rx_chains_static &&
785 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
786 return;
787
788 chanctx->conf.rx_chains_static = rx_chains_static;
789 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
790 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
791}
792
Luciano Coelho11335a52013-10-30 13:09:39 +0200793static void
794__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
795 bool clear)
796{
Johannes Berg33926eb2014-04-09 21:31:13 +0200797 struct ieee80211_local *local __maybe_unused = sdata->local;
Luciano Coelho11335a52013-10-30 13:09:39 +0200798 struct ieee80211_sub_if_data *vlan;
799 struct ieee80211_chanctx_conf *conf;
800
801 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
802 return;
803
804 lockdep_assert_held(&local->mtx);
805
806 /* Check that conf exists, even when clearing this function
807 * must be called with the AP's channel context still there
808 * as it would otherwise cause VLANs to have an invalid
809 * channel context pointer for a while, possibly pointing
810 * to a channel context that has already been freed.
811 */
812 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
Johannes Berg33926eb2014-04-09 21:31:13 +0200813 lockdep_is_held(&local->chanctx_mtx));
Luciano Coelho11335a52013-10-30 13:09:39 +0200814 WARN_ON(!conf);
815
816 if (clear)
817 conf = NULL;
818
819 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
820 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
821}
822
823void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
824 bool clear)
825{
826 struct ieee80211_local *local = sdata->local;
827
828 mutex_lock(&local->chanctx_mtx);
829
830 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
831
832 mutex_unlock(&local->chanctx_mtx);
833}
834
835int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
836{
Michal Kaziore3afb922014-04-09 15:29:27 +0200837 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
838
Luciano Coelho11335a52013-10-30 13:09:39 +0200839 lockdep_assert_held(&sdata->local->chanctx_mtx);
840
Michal Kaziore3afb922014-04-09 15:29:27 +0200841 if (WARN_ON(!ctx))
Luciano Coelho11335a52013-10-30 13:09:39 +0200842 return -EINVAL;
843
Michal Kaziore3afb922014-04-09 15:29:27 +0200844 list_del(&sdata->reserved_chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200845 sdata->reserved_chanctx = NULL;
846
Michal Kazior5bcae312014-06-25 12:35:06 +0200847 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
848 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
849 if (WARN_ON(!ctx->replace_ctx))
850 return -EINVAL;
851
852 WARN_ON(ctx->replace_ctx->replace_state !=
853 IEEE80211_CHANCTX_WILL_BE_REPLACED);
854 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
855
856 ctx->replace_ctx->replace_ctx = NULL;
857 ctx->replace_ctx->replace_state =
858 IEEE80211_CHANCTX_REPLACE_NONE;
859
860 list_del_rcu(&ctx->list);
861 kfree_rcu(ctx, rcu_head);
862 } else {
863 ieee80211_free_chanctx(sdata->local, ctx);
864 }
865 }
Michal Kaziore3afb922014-04-09 15:29:27 +0200866
Luciano Coelho11335a52013-10-30 13:09:39 +0200867 return 0;
868}
869
870int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
871 const struct cfg80211_chan_def *chandef,
Michal Kazior09332482014-04-09 15:29:25 +0200872 enum ieee80211_chanctx_mode mode,
873 bool radar_required)
Luciano Coelho11335a52013-10-30 13:09:39 +0200874{
875 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +0200876 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
Luciano Coelho11335a52013-10-30 13:09:39 +0200877
Michal Kazior5bcae312014-06-25 12:35:06 +0200878 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +0200879
Michal Kazior5bcae312014-06-25 12:35:06 +0200880 curr_ctx = ieee80211_vif_get_chanctx(sdata);
881 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
882 return -ENOTSUPP;
Luciano Coelho11335a52013-10-30 13:09:39 +0200883
Michal Kazior13f348a2014-04-09 15:29:29 +0200884 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
Luciano Coelho11335a52013-10-30 13:09:39 +0200885 if (!new_ctx) {
Michal Kazior5bcae312014-06-25 12:35:06 +0200886 if (ieee80211_can_create_new_chanctx(local)) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200887 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kazior5bcae312014-06-25 12:35:06 +0200888 if (IS_ERR(new_ctx))
889 return PTR_ERR(new_ctx);
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200890 } else {
Michal Kazior5bcae312014-06-25 12:35:06 +0200891 if (!curr_ctx ||
892 (curr_ctx->replace_state ==
893 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
894 !list_empty(&curr_ctx->reserved_vifs)) {
895 /*
896 * Another vif already requested this context
897 * for a reservation. Find another one hoping
898 * all vifs assigned to it will also switch
899 * soon enough.
900 *
901 * TODO: This needs a little more work as some
902 * cases (more than 2 chanctx capable devices)
903 * may fail which could otherwise succeed
904 * provided some channel context juggling was
905 * performed.
906 *
907 * Consider ctx1..3, vif1..6, each ctx has 2
908 * vifs. vif1 and vif2 from ctx1 request new
909 * different chandefs starting 2 in-place
910 * reserations with ctx4 and ctx5 replacing
911 * ctx1 and ctx2 respectively. Next vif5 and
912 * vif6 from ctx3 reserve ctx4. If vif3 and
913 * vif4 remain on ctx2 as they are then this
914 * fails unless `replace_ctx` from ctx5 is
915 * replaced with ctx3.
916 */
917 list_for_each_entry(ctx, &local->chanctx_list,
918 list) {
919 if (ctx->replace_state !=
920 IEEE80211_CHANCTX_REPLACE_NONE)
921 continue;
922
923 if (!list_empty(&ctx->reserved_vifs))
924 continue;
925
926 curr_ctx = ctx;
927 break;
928 }
929 }
930
931 /*
932 * If that's true then all available contexts already
933 * have reservations and cannot be used.
934 */
935 if (!curr_ctx ||
936 (curr_ctx->replace_state ==
937 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
938 !list_empty(&curr_ctx->reserved_vifs))
939 return -EBUSY;
940
941 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
942 if (!new_ctx)
943 return -ENOMEM;
944
945 new_ctx->replace_ctx = curr_ctx;
946 new_ctx->replace_state =
947 IEEE80211_CHANCTX_REPLACES_OTHER;
948
949 curr_ctx->replace_ctx = new_ctx;
950 curr_ctx->replace_state =
951 IEEE80211_CHANCTX_WILL_BE_REPLACED;
952
953 list_add_rcu(&new_ctx->list, &local->chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200954 }
955 }
956
Michal Kaziore3afb922014-04-09 15:29:27 +0200957 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
Luciano Coelho11335a52013-10-30 13:09:39 +0200958 sdata->reserved_chanctx = new_ctx;
959 sdata->reserved_chandef = *chandef;
Michal Kazior09332482014-04-09 15:29:25 +0200960 sdata->reserved_radar_required = radar_required;
Michal Kazior5bcae312014-06-25 12:35:06 +0200961 sdata->reserved_ready = false;
962
963 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +0200964}
965
Michal Kazior03078de2014-06-25 12:35:08 +0200966static void
967ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
968{
969 switch (sdata->vif.type) {
970 case NL80211_IFTYPE_ADHOC:
971 case NL80211_IFTYPE_AP:
972 case NL80211_IFTYPE_MESH_POINT:
Rostislav Lisovy6e0bd6c2014-11-03 10:33:18 +0100973 case NL80211_IFTYPE_OCB:
Michal Kazior03078de2014-06-25 12:35:08 +0200974 ieee80211_queue_work(&sdata->local->hw,
975 &sdata->csa_finalize_work);
976 break;
Michal Kazior03078de2014-06-25 12:35:08 +0200977 case NL80211_IFTYPE_STATION:
Michal Kazior4c3ebc52014-06-25 12:35:09 +0200978 ieee80211_queue_work(&sdata->local->hw,
979 &sdata->u.mgd.chswitch_work);
980 break;
981 case NL80211_IFTYPE_UNSPECIFIED:
Michal Kazior03078de2014-06-25 12:35:08 +0200982 case NL80211_IFTYPE_AP_VLAN:
983 case NL80211_IFTYPE_WDS:
984 case NL80211_IFTYPE_MONITOR:
985 case NL80211_IFTYPE_P2P_CLIENT:
986 case NL80211_IFTYPE_P2P_GO:
987 case NL80211_IFTYPE_P2P_DEVICE:
Ayala Bekercb3b7d82016-09-20 17:31:13 +0300988 case NL80211_IFTYPE_NAN:
Michal Kazior03078de2014-06-25 12:35:08 +0200989 case NUM_NL80211_IFTYPES:
990 WARN_ON(1);
991 break;
992 }
993}
994
Felix Fietkau2967e032014-11-24 18:12:16 +0100995static void
996ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
997 const struct cfg80211_chan_def *chandef)
998{
999 struct ieee80211_sub_if_data *vlan;
1000
1001 sdata->vif.bss_conf.chandef = *chandef;
1002
1003 if (sdata->vif.type != NL80211_IFTYPE_AP)
1004 return;
1005
1006 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
1007 vlan->vif.bss_conf.chandef = *chandef;
1008}
1009
Michal Kazior5bcae312014-06-25 12:35:06 +02001010static int
1011ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
Luciano Coelho11335a52013-10-30 13:09:39 +02001012{
1013 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +02001014 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1015 struct ieee80211_chanctx *old_ctx, *new_ctx;
1016 const struct cfg80211_chan_def *chandef;
1017 u32 changed = 0;
1018 int err;
Luciano Coelho11335a52013-10-30 13:09:39 +02001019
1020 lockdep_assert_held(&local->mtx);
Michal Kazior5bcae312014-06-25 12:35:06 +02001021 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001022
Michal Kazior5bcae312014-06-25 12:35:06 +02001023 new_ctx = sdata->reserved_chanctx;
1024 old_ctx = ieee80211_vif_get_chanctx(sdata);
Luciano Coelho11335a52013-10-30 13:09:39 +02001025
Michal Kazior5bcae312014-06-25 12:35:06 +02001026 if (WARN_ON(!sdata->reserved_ready))
1027 return -EBUSY;
1028
1029 if (WARN_ON(!new_ctx))
1030 return -EINVAL;
1031
1032 if (WARN_ON(!old_ctx))
1033 return -EINVAL;
1034
1035 if (WARN_ON(new_ctx->replace_state ==
1036 IEEE80211_CHANCTX_REPLACES_OTHER))
1037 return -EINVAL;
1038
1039 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1040 &sdata->reserved_chandef);
1041 if (WARN_ON(!chandef))
1042 return -EINVAL;
1043
Andrei Otcheretianskibf45a242015-05-06 18:30:50 +03001044 ieee80211_change_chanctx(local, new_ctx, chandef);
1045
Michal Kazior5bcae312014-06-25 12:35:06 +02001046 vif_chsw[0].vif = &sdata->vif;
1047 vif_chsw[0].old_ctx = &old_ctx->conf;
1048 vif_chsw[0].new_ctx = &new_ctx->conf;
1049
1050 list_del(&sdata->reserved_chanctx_list);
1051 sdata->reserved_chanctx = NULL;
1052
1053 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1054 CHANCTX_SWMODE_REASSIGN_VIF);
1055 if (err) {
1056 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1057 ieee80211_free_chanctx(local, new_ctx);
1058
Michal Kazior03078de2014-06-25 12:35:08 +02001059 goto out;
Luciano Coelho11335a52013-10-30 13:09:39 +02001060 }
1061
Michal Kazior5bcae312014-06-25 12:35:06 +02001062 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1063 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
Luciano Coelho11335a52013-10-30 13:09:39 +02001064
Michal Kazior5bcae312014-06-25 12:35:06 +02001065 if (sdata->vif.type == NL80211_IFTYPE_AP)
1066 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1067
Johannes Berg17c18bf2015-03-21 15:25:43 +01001068 ieee80211_check_fast_xmit_iface(sdata);
1069
Michal Kazior5bcae312014-06-25 12:35:06 +02001070 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1071 ieee80211_free_chanctx(local, old_ctx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001072
1073 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
Michal Kazior5bcae312014-06-25 12:35:06 +02001074 changed = BSS_CHANGED_BANDWIDTH;
Luciano Coelho11335a52013-10-30 13:09:39 +02001075
Felix Fietkau2967e032014-11-24 18:12:16 +01001076 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
Luciano Coelho11335a52013-10-30 13:09:39 +02001077
Emmanuel Grumbach722ddb02014-11-30 17:17:26 +02001078 ieee80211_recalc_smps_chanctx(local, new_ctx);
1079 ieee80211_recalc_radar_chanctx(local, new_ctx);
1080 ieee80211_recalc_chanctx_min_def(local, new_ctx);
1081
Michal Kazior5bcae312014-06-25 12:35:06 +02001082 if (changed)
1083 ieee80211_bss_info_change_notify(sdata, changed);
Luciano Coelho11335a52013-10-30 13:09:39 +02001084
Michal Kazior03078de2014-06-25 12:35:08 +02001085out:
1086 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001087 return err;
1088}
1089
1090static int
1091ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1092{
1093 struct ieee80211_local *local = sdata->local;
1094 struct ieee80211_chanctx *old_ctx, *new_ctx;
1095 const struct cfg80211_chan_def *chandef;
1096 int err;
1097
1098 old_ctx = ieee80211_vif_get_chanctx(sdata);
1099 new_ctx = sdata->reserved_chanctx;
1100
1101 if (WARN_ON(!sdata->reserved_ready))
1102 return -EINVAL;
1103
1104 if (WARN_ON(old_ctx))
1105 return -EINVAL;
1106
1107 if (WARN_ON(!new_ctx))
1108 return -EINVAL;
1109
1110 if (WARN_ON(new_ctx->replace_state ==
1111 IEEE80211_CHANCTX_REPLACES_OTHER))
1112 return -EINVAL;
1113
1114 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1115 &sdata->reserved_chandef);
1116 if (WARN_ON(!chandef))
1117 return -EINVAL;
1118
Andrei Otcheretianskibf45a242015-05-06 18:30:50 +03001119 ieee80211_change_chanctx(local, new_ctx, chandef);
1120
Michal Kazior5bcae312014-06-25 12:35:06 +02001121 list_del(&sdata->reserved_chanctx_list);
1122 sdata->reserved_chanctx = NULL;
1123
1124 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1125 if (err) {
1126 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1127 ieee80211_free_chanctx(local, new_ctx);
1128
1129 goto out;
1130 }
1131
1132out:
Michal Kazior03078de2014-06-25 12:35:08 +02001133 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001134 return err;
1135}
1136
1137static bool
1138ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1139{
1140 struct ieee80211_chanctx *old_ctx, *new_ctx;
1141
1142 lockdep_assert_held(&sdata->local->chanctx_mtx);
1143
1144 new_ctx = sdata->reserved_chanctx;
1145 old_ctx = ieee80211_vif_get_chanctx(sdata);
1146
1147 if (!old_ctx)
1148 return false;
1149
1150 if (WARN_ON(!new_ctx))
1151 return false;
1152
1153 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1154 return false;
1155
1156 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1157 return false;
1158
1159 return true;
1160}
1161
1162static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1163 struct ieee80211_chanctx *new_ctx)
1164{
1165 const struct cfg80211_chan_def *chandef;
1166
1167 lockdep_assert_held(&local->mtx);
1168 lockdep_assert_held(&local->chanctx_mtx);
1169
1170 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1171 if (WARN_ON(!chandef))
1172 return -EINVAL;
1173
1174 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1175 local->_oper_chandef = *chandef;
1176 ieee80211_hw_config(local, 0);
1177
1178 return 0;
1179}
1180
1181static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1182 int n_vifs)
1183{
1184 struct ieee80211_vif_chanctx_switch *vif_chsw;
1185 struct ieee80211_sub_if_data *sdata;
1186 struct ieee80211_chanctx *ctx, *old_ctx;
1187 int i, err;
1188
1189 lockdep_assert_held(&local->mtx);
1190 lockdep_assert_held(&local->chanctx_mtx);
1191
1192 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1193 if (!vif_chsw)
1194 return -ENOMEM;
1195
1196 i = 0;
1197 list_for_each_entry(ctx, &local->chanctx_list, list) {
1198 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1199 continue;
1200
1201 if (WARN_ON(!ctx->replace_ctx)) {
1202 err = -EINVAL;
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001203 goto out;
1204 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001205
Michal Kazior5bcae312014-06-25 12:35:06 +02001206 list_for_each_entry(sdata, &ctx->reserved_vifs,
1207 reserved_chanctx_list) {
1208 if (!ieee80211_vif_has_in_place_reservation(
1209 sdata))
1210 continue;
1211
1212 old_ctx = ieee80211_vif_get_chanctx(sdata);
1213 vif_chsw[i].vif = &sdata->vif;
1214 vif_chsw[i].old_ctx = &old_ctx->conf;
1215 vif_chsw[i].new_ctx = &ctx->conf;
1216
1217 i++;
1218 }
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001219 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001220
Michal Kazior5bcae312014-06-25 12:35:06 +02001221 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1222 CHANCTX_SWMODE_SWAP_CONTEXTS);
Luciano Coelho11335a52013-10-30 13:09:39 +02001223
Luciano Coelho11335a52013-10-30 13:09:39 +02001224out:
Michal Kazior5bcae312014-06-25 12:35:06 +02001225 kfree(vif_chsw);
1226 return err;
1227}
1228
1229static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1230{
1231 struct ieee80211_chanctx *ctx;
1232 int err;
1233
1234 lockdep_assert_held(&local->mtx);
1235 lockdep_assert_held(&local->chanctx_mtx);
1236
1237 list_for_each_entry(ctx, &local->chanctx_list, list) {
1238 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1239 continue;
1240
1241 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1242 continue;
1243
1244 ieee80211_del_chanctx(local, ctx->replace_ctx);
1245 err = ieee80211_add_chanctx(local, ctx);
1246 if (err)
1247 goto err;
1248 }
1249
1250 return 0;
1251
1252err:
1253 WARN_ON(ieee80211_add_chanctx(local, ctx));
1254 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1255 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1256 continue;
1257
1258 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1259 continue;
1260
1261 ieee80211_del_chanctx(local, ctx);
1262 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1263 }
1264
1265 return err;
1266}
1267
Johannes Berg649b2a42014-07-25 15:01:59 +02001268static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
Michal Kazior5bcae312014-06-25 12:35:06 +02001269{
1270 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1271 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1272 struct ieee80211_chanctx *new_ctx = NULL;
1273 int i, err, n_assigned, n_reserved, n_ready;
1274 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1275
1276 lockdep_assert_held(&local->mtx);
1277 lockdep_assert_held(&local->chanctx_mtx);
1278
1279 /*
1280 * If there are 2 independent pairs of channel contexts performing
1281 * cross-switch of their vifs this code will still wait until both are
1282 * ready even though it could be possible to switch one before the
1283 * other is ready.
1284 *
1285 * For practical reasons and code simplicity just do a single huge
1286 * switch.
1287 */
1288
1289 /*
1290 * Verify if the reservation is still feasible.
1291 * - if it's not then disconnect
1292 * - if it is but not all vifs necessary are ready then defer
1293 */
1294
1295 list_for_each_entry(ctx, &local->chanctx_list, list) {
1296 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1297 continue;
1298
1299 if (WARN_ON(!ctx->replace_ctx)) {
1300 err = -EINVAL;
1301 goto err;
1302 }
1303
1304 if (!local->use_chanctx)
1305 new_ctx = ctx;
1306
1307 n_ctx++;
1308
1309 n_assigned = 0;
1310 n_reserved = 0;
1311 n_ready = 0;
1312
1313 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1314 assigned_chanctx_list) {
1315 n_assigned++;
1316 if (sdata->reserved_chanctx) {
1317 n_reserved++;
1318 if (sdata->reserved_ready)
1319 n_ready++;
1320 }
1321 }
1322
1323 if (n_assigned != n_reserved) {
1324 if (n_ready == n_reserved) {
1325 wiphy_info(local->hw.wiphy,
1326 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1327 err = -EBUSY;
1328 goto err;
1329 }
1330
1331 return -EAGAIN;
1332 }
1333
1334 ctx->conf.radar_enabled = false;
1335 list_for_each_entry(sdata, &ctx->reserved_vifs,
1336 reserved_chanctx_list) {
1337 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1338 !sdata->reserved_ready)
1339 return -EAGAIN;
1340
1341 old_ctx = ieee80211_vif_get_chanctx(sdata);
1342 if (old_ctx) {
1343 if (old_ctx->replace_state ==
1344 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1345 n_vifs_switch++;
1346 else
1347 n_vifs_assign++;
1348 } else {
1349 n_vifs_ctxless++;
1350 }
1351
1352 if (sdata->reserved_radar_required)
1353 ctx->conf.radar_enabled = true;
1354 }
1355 }
1356
1357 if (WARN_ON(n_ctx == 0) ||
1358 WARN_ON(n_vifs_switch == 0 &&
1359 n_vifs_assign == 0 &&
1360 n_vifs_ctxless == 0) ||
1361 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1362 WARN_ON(!new_ctx && !local->use_chanctx)) {
1363 err = -EINVAL;
1364 goto err;
1365 }
1366
1367 /*
1368 * All necessary vifs are ready. Perform the switch now depending on
1369 * reservations and driver capabilities.
1370 */
1371
1372 if (local->use_chanctx) {
1373 if (n_vifs_switch > 0) {
1374 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1375 if (err)
1376 goto err;
1377 }
1378
1379 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1380 err = ieee80211_chsw_switch_ctxs(local);
1381 if (err)
1382 goto err;
1383 }
1384 } else {
1385 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1386 if (err)
1387 goto err;
1388 }
1389
1390 /*
1391 * Update all structures, values and pointers to point to new channel
1392 * context(s).
1393 */
1394
1395 i = 0;
1396 list_for_each_entry(ctx, &local->chanctx_list, list) {
1397 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1398 continue;
1399
1400 if (WARN_ON(!ctx->replace_ctx)) {
1401 err = -EINVAL;
1402 goto err;
1403 }
1404
1405 list_for_each_entry(sdata, &ctx->reserved_vifs,
1406 reserved_chanctx_list) {
1407 u32 changed = 0;
1408
1409 if (!ieee80211_vif_has_in_place_reservation(sdata))
1410 continue;
1411
1412 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1413
1414 if (sdata->vif.type == NL80211_IFTYPE_AP)
1415 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1416 false);
1417
Johannes Berg17c18bf2015-03-21 15:25:43 +01001418 ieee80211_check_fast_xmit_iface(sdata);
1419
Michal Kazior5bcae312014-06-25 12:35:06 +02001420 sdata->radar_required = sdata->reserved_radar_required;
1421
1422 if (sdata->vif.bss_conf.chandef.width !=
1423 sdata->reserved_chandef.width)
1424 changed = BSS_CHANGED_BANDWIDTH;
1425
Felix Fietkau2967e032014-11-24 18:12:16 +01001426 ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
Michal Kazior5bcae312014-06-25 12:35:06 +02001427 if (changed)
1428 ieee80211_bss_info_change_notify(sdata,
1429 changed);
1430
Lorenzo Bianconidb82d8a2015-01-14 12:55:08 +01001431 ieee80211_recalc_txpower(sdata, false);
Michal Kazior5bcae312014-06-25 12:35:06 +02001432 }
1433
1434 ieee80211_recalc_chanctx_chantype(local, ctx);
1435 ieee80211_recalc_smps_chanctx(local, ctx);
1436 ieee80211_recalc_radar_chanctx(local, ctx);
1437 ieee80211_recalc_chanctx_min_def(local, ctx);
1438
1439 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1440 reserved_chanctx_list) {
1441 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1442 continue;
1443
1444 list_del(&sdata->reserved_chanctx_list);
1445 list_move(&sdata->assigned_chanctx_list,
Michal Kazior47e4df92014-08-18 13:19:09 +02001446 &ctx->assigned_vifs);
Michal Kazior5bcae312014-06-25 12:35:06 +02001447 sdata->reserved_chanctx = NULL;
Michal Kazior03078de2014-06-25 12:35:08 +02001448
1449 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001450 }
1451
1452 /*
1453 * This context might have been a dependency for an already
1454 * ready re-assign reservation interface that was deferred. Do
1455 * not propagate error to the caller though. The in-place
1456 * reservation for originally requested interface has already
1457 * succeeded at this point.
1458 */
1459 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1460 reserved_chanctx_list) {
1461 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1462 sdata)))
1463 continue;
1464
1465 if (WARN_ON(sdata->reserved_chanctx != ctx))
1466 continue;
1467
1468 if (!sdata->reserved_ready)
1469 continue;
1470
1471 if (ieee80211_vif_get_chanctx(sdata))
1472 err = ieee80211_vif_use_reserved_reassign(
1473 sdata);
1474 else
1475 err = ieee80211_vif_use_reserved_assign(sdata);
1476
1477 if (err) {
1478 sdata_info(sdata,
1479 "failed to finalize (re-)assign reservation (err=%d)\n",
1480 err);
1481 ieee80211_vif_unreserve_chanctx(sdata);
1482 cfg80211_stop_iface(local->hw.wiphy,
1483 &sdata->wdev,
1484 GFP_KERNEL);
1485 }
1486 }
1487 }
1488
1489 /*
1490 * Finally free old contexts
1491 */
1492
1493 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1494 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1495 continue;
1496
1497 ctx->replace_ctx->replace_ctx = NULL;
1498 ctx->replace_ctx->replace_state =
1499 IEEE80211_CHANCTX_REPLACE_NONE;
1500
1501 list_del_rcu(&ctx->list);
1502 kfree_rcu(ctx, rcu_head);
1503 }
1504
1505 return 0;
1506
1507err:
1508 list_for_each_entry(ctx, &local->chanctx_list, list) {
1509 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1510 continue;
1511
1512 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
Michal Kazior03078de2014-06-25 12:35:08 +02001513 reserved_chanctx_list) {
Michal Kazior5bcae312014-06-25 12:35:06 +02001514 ieee80211_vif_unreserve_chanctx(sdata);
Michal Kazior03078de2014-06-25 12:35:08 +02001515 ieee80211_vif_chanctx_reservation_complete(sdata);
1516 }
Michal Kazior5bcae312014-06-25 12:35:06 +02001517 }
1518
1519 return err;
1520}
1521
Johannes Berg649b2a42014-07-25 15:01:59 +02001522static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1523{
1524 struct ieee80211_local *local = sdata->local;
1525 struct ieee80211_chanctx_conf *conf;
1526 struct ieee80211_chanctx *ctx;
1527 bool use_reserved_switch = false;
1528
1529 lockdep_assert_held(&local->chanctx_mtx);
1530
1531 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1532 lockdep_is_held(&local->chanctx_mtx));
1533 if (!conf)
1534 return;
1535
1536 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1537
1538 if (sdata->reserved_chanctx) {
1539 if (sdata->reserved_chanctx->replace_state ==
1540 IEEE80211_CHANCTX_REPLACES_OTHER &&
1541 ieee80211_chanctx_num_reserved(local,
1542 sdata->reserved_chanctx) > 1)
1543 use_reserved_switch = true;
1544
1545 ieee80211_vif_unreserve_chanctx(sdata);
1546 }
1547
1548 ieee80211_assign_vif_chanctx(sdata, NULL);
1549 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1550 ieee80211_free_chanctx(local, ctx);
1551
Eliad Peller104f5a62015-02-08 12:36:07 +02001552 sdata->radar_required = false;
1553
Johannes Berg649b2a42014-07-25 15:01:59 +02001554 /* Unreserving may ready an in-place reservation. */
1555 if (use_reserved_switch)
1556 ieee80211_vif_use_reserved_switch(local);
1557}
1558
1559int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
1560 const struct cfg80211_chan_def *chandef,
1561 enum ieee80211_chanctx_mode mode)
1562{
1563 struct ieee80211_local *local = sdata->local;
1564 struct ieee80211_chanctx *ctx;
1565 u8 radar_detect_width = 0;
1566 int ret;
1567
1568 lockdep_assert_held(&local->mtx);
1569
1570 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1571
1572 mutex_lock(&local->chanctx_mtx);
1573
1574 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
1575 chandef,
1576 sdata->wdev.iftype);
1577 if (ret < 0)
1578 goto out;
1579 if (ret > 0)
1580 radar_detect_width = BIT(chandef->width);
1581
1582 sdata->radar_required = ret;
1583
1584 ret = ieee80211_check_combinations(sdata, chandef, mode,
1585 radar_detect_width);
1586 if (ret < 0)
1587 goto out;
1588
1589 __ieee80211_vif_release_channel(sdata);
1590
1591 ctx = ieee80211_find_chanctx(local, chandef, mode);
1592 if (!ctx)
1593 ctx = ieee80211_new_chanctx(local, chandef, mode);
1594 if (IS_ERR(ctx)) {
1595 ret = PTR_ERR(ctx);
1596 goto out;
1597 }
1598
Felix Fietkau2967e032014-11-24 18:12:16 +01001599 ieee80211_vif_update_chandef(sdata, chandef);
Johannes Berg649b2a42014-07-25 15:01:59 +02001600
1601 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
1602 if (ret) {
1603 /* if assign fails refcount stays the same */
1604 if (ieee80211_chanctx_refcount(local, ctx) == 0)
1605 ieee80211_free_chanctx(local, ctx);
1606 goto out;
1607 }
1608
1609 ieee80211_recalc_smps_chanctx(local, ctx);
1610 ieee80211_recalc_radar_chanctx(local, ctx);
1611 out:
Eliad Peller104f5a62015-02-08 12:36:07 +02001612 if (ret)
1613 sdata->radar_required = false;
1614
Johannes Berg649b2a42014-07-25 15:01:59 +02001615 mutex_unlock(&local->chanctx_mtx);
1616 return ret;
1617}
1618
Michal Kazior5bcae312014-06-25 12:35:06 +02001619int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1620{
1621 struct ieee80211_local *local = sdata->local;
1622 struct ieee80211_chanctx *new_ctx;
1623 struct ieee80211_chanctx *old_ctx;
1624 int err;
1625
1626 lockdep_assert_held(&local->mtx);
1627 lockdep_assert_held(&local->chanctx_mtx);
1628
1629 new_ctx = sdata->reserved_chanctx;
1630 old_ctx = ieee80211_vif_get_chanctx(sdata);
1631
1632 if (WARN_ON(!new_ctx))
1633 return -EINVAL;
1634
1635 if (WARN_ON(new_ctx->replace_state ==
1636 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1637 return -EINVAL;
1638
1639 if (WARN_ON(sdata->reserved_ready))
1640 return -EINVAL;
1641
1642 sdata->reserved_ready = true;
1643
1644 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1645 if (old_ctx)
1646 err = ieee80211_vif_use_reserved_reassign(sdata);
1647 else
1648 err = ieee80211_vif_use_reserved_assign(sdata);
1649
1650 if (err)
1651 return err;
1652 }
1653
1654 /*
1655 * In-place reservation may need to be finalized now either if:
1656 * a) sdata is taking part in the swapping itself and is the last one
1657 * b) sdata has switched with a re-assign reservation to an existing
1658 * context readying in-place switching of old_ctx
1659 *
1660 * In case of (b) do not propagate the error up because the requested
1661 * sdata already switched successfully. Just spill an extra warning.
1662 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1663 * interfaces upon failure.
1664 */
1665 if ((old_ctx &&
1666 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1667 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1668 err = ieee80211_vif_use_reserved_switch(local);
1669 if (err && err != -EAGAIN) {
1670 if (new_ctx->replace_state ==
1671 IEEE80211_CHANCTX_REPLACES_OTHER)
1672 return err;
1673
1674 wiphy_info(local->hw.wiphy,
1675 "depending in-place reservation failed (err=%d)\n",
1676 err);
1677 }
1678 }
1679
1680 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +02001681}
1682
Johannes Berg2c9b7352013-02-07 21:37:29 +01001683int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1684 const struct cfg80211_chan_def *chandef,
1685 u32 *changed)
1686{
1687 struct ieee80211_local *local = sdata->local;
1688 struct ieee80211_chanctx_conf *conf;
1689 struct ieee80211_chanctx *ctx;
Michal Kazior5bcae312014-06-25 12:35:06 +02001690 const struct cfg80211_chan_def *compat;
Johannes Berg2c9b7352013-02-07 21:37:29 +01001691 int ret;
1692
1693 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1694 IEEE80211_CHAN_DISABLED))
1695 return -EINVAL;
1696
1697 mutex_lock(&local->chanctx_mtx);
1698 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1699 ret = 0;
1700 goto out;
1701 }
1702
1703 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1704 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1705 ret = -EINVAL;
1706 goto out;
1707 }
1708
1709 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1710 lockdep_is_held(&local->chanctx_mtx));
1711 if (!conf) {
1712 ret = -EINVAL;
1713 goto out;
1714 }
1715
1716 ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior5bcae312014-06-25 12:35:06 +02001717
1718 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1719 if (!compat) {
Johannes Berg2c9b7352013-02-07 21:37:29 +01001720 ret = -EINVAL;
1721 goto out;
1722 }
1723
Michal Kazior5bcae312014-06-25 12:35:06 +02001724 switch (ctx->replace_state) {
1725 case IEEE80211_CHANCTX_REPLACE_NONE:
1726 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1727 ret = -EBUSY;
1728 goto out;
1729 }
1730 break;
1731 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
Stephen Hemmingerd070f912014-10-29 22:55:58 -07001732 /* TODO: Perhaps the bandwidth change could be treated as a
Michal Kazior5bcae312014-06-25 12:35:06 +02001733 * reservation itself? */
1734 ret = -EBUSY;
1735 goto out;
1736 case IEEE80211_CHANCTX_REPLACES_OTHER:
1737 /* channel context that is going to replace another channel
1738 * context doesn't really exist and shouldn't be assigned
1739 * anywhere yet */
1740 WARN_ON(1);
1741 break;
1742 }
1743
Felix Fietkau2967e032014-11-24 18:12:16 +01001744 ieee80211_vif_update_chandef(sdata, chandef);
Johannes Berg2c9b7352013-02-07 21:37:29 +01001745
1746 ieee80211_recalc_chanctx_chantype(local, ctx);
1747
1748 *changed |= BSS_CHANGED_BANDWIDTH;
1749 ret = 0;
1750 out:
1751 mutex_unlock(&local->chanctx_mtx);
1752 return ret;
1753}
1754
Michal Kaziord01a1e62012-06-26 14:37:16 +02001755void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1756{
Johannes Berg55de9082012-07-26 17:24:39 +02001757 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1758
Johannes Berg34a37402013-12-18 09:43:33 +01001759 lockdep_assert_held(&sdata->local->mtx);
1760
Michal Kaziord01a1e62012-06-26 14:37:16 +02001761 mutex_lock(&sdata->local->chanctx_mtx);
1762 __ieee80211_vif_release_channel(sdata);
1763 mutex_unlock(&sdata->local->chanctx_mtx);
1764}
Johannes Berg3448c002012-09-11 17:57:42 +02001765
Johannes Berg4d76d212012-12-11 20:38:41 +01001766void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1767{
1768 struct ieee80211_local *local = sdata->local;
1769 struct ieee80211_sub_if_data *ap;
1770 struct ieee80211_chanctx_conf *conf;
1771
1772 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1773 return;
1774
1775 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1776
1777 mutex_lock(&local->chanctx_mtx);
1778
1779 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1780 lockdep_is_held(&local->chanctx_mtx));
1781 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1782 mutex_unlock(&local->chanctx_mtx);
1783}
1784
Johannes Berg3448c002012-09-11 17:57:42 +02001785void ieee80211_iter_chan_contexts_atomic(
1786 struct ieee80211_hw *hw,
1787 void (*iter)(struct ieee80211_hw *hw,
1788 struct ieee80211_chanctx_conf *chanctx_conf,
1789 void *data),
1790 void *iter_data)
1791{
1792 struct ieee80211_local *local = hw_to_local(hw);
1793 struct ieee80211_chanctx *ctx;
1794
1795 rcu_read_lock();
1796 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +01001797 if (ctx->driver_present)
1798 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +02001799 rcu_read_unlock();
1800}
1801EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);