blob: 4206a11bf8d79c270a08da8351c8b04eda3c899e [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:
273 width = vif->bss_conf.chandef.width;
274 break;
275 case NL80211_IFTYPE_UNSPECIFIED:
276 case NUM_NL80211_IFTYPES:
277 case NL80211_IFTYPE_MONITOR:
278 case NL80211_IFTYPE_P2P_CLIENT:
279 case NL80211_IFTYPE_P2P_GO:
280 WARN_ON_ONCE(1);
281 }
282 max_bw = max(max_bw, width);
283 }
Eliad Peller1c37a722014-03-03 13:37:14 +0200284
285 /* use the configured bandwidth in case of monitor interface */
286 sdata = rcu_dereference(local->monitor_sdata);
287 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
288 max_bw = max(max_bw, conf->def.width);
289
Eliad Peller21f659b2013-11-11 20:14:01 +0200290 rcu_read_unlock();
291
292 return max_bw;
293}
294
295/*
296 * recalc the min required chan width of the channel context, which is
297 * the max of min required widths of all the interfaces bound to this
298 * channel context.
299 */
300void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
301 struct ieee80211_chanctx *ctx)
302{
303 enum nl80211_chan_width max_bw;
304 struct cfg80211_chan_def min_def;
305
306 lockdep_assert_held(&local->chanctx_mtx);
307
308 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
309 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
310 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
311 ctx->conf.radar_enabled) {
312 ctx->conf.min_def = ctx->conf.def;
313 return;
314 }
315
316 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
317
318 /* downgrade chandef up to max_bw */
319 min_def = ctx->conf.def;
320 while (min_def.width > max_bw)
321 ieee80211_chandef_downgrade(&min_def);
322
323 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
324 return;
325
326 ctx->conf.min_def = min_def;
327 if (!ctx->driver_present)
328 return;
329
330 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
331}
332
Johannes Berg18942d32013-02-07 21:30:37 +0100333static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100334 struct ieee80211_chanctx *ctx,
335 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200336{
Johannes Berg4bf88532012-11-09 11:39:59 +0100337 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +0200338 return;
339
Johannes Berg4bf88532012-11-09 11:39:59 +0100340 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
341
342 ctx->conf.def = *chandef;
343 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Eliad Peller21f659b2013-11-11 20:14:01 +0200344 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200345
346 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100347 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200348 ieee80211_hw_config(local, 0);
349 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200350}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200351
352static struct ieee80211_chanctx *
353ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100354 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200355 enum ieee80211_chanctx_mode mode)
356{
357 struct ieee80211_chanctx *ctx;
358
359 lockdep_assert_held(&local->chanctx_mtx);
360
361 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
362 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200363
364 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100365 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200366
Michal Kazior5bcae312014-06-25 12:35:06 +0200367 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
368 continue;
369
Michal Kaziord01a1e62012-06-26 14:37:16 +0200370 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
371 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100372
373 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
374 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200375 continue;
376
Michal Kazior02881572014-04-09 15:29:28 +0200377 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
378 compat);
379 if (!compat)
380 continue;
381
Johannes Berg18942d32013-02-07 21:30:37 +0100382 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200383
Michal Kaziord01a1e62012-06-26 14:37:16 +0200384 return ctx;
385 }
386
387 return NULL;
388}
389
Simon Wunderliche4746852013-04-08 22:43:16 +0200390static bool ieee80211_is_radar_required(struct ieee80211_local *local)
391{
392 struct ieee80211_sub_if_data *sdata;
393
Michal Kaziorcc901de2014-01-29 07:56:20 +0100394 lockdep_assert_held(&local->mtx);
395
Simon Wunderliche4746852013-04-08 22:43:16 +0200396 rcu_read_lock();
397 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
398 if (sdata->radar_required) {
399 rcu_read_unlock();
400 return true;
401 }
402 }
403 rcu_read_unlock();
404
405 return false;
406}
407
Michal Kaziord01a1e62012-06-26 14:37:16 +0200408static struct ieee80211_chanctx *
Michal Kaziored68ebc2014-04-09 15:29:30 +0200409ieee80211_alloc_chanctx(struct ieee80211_local *local,
410 const struct cfg80211_chan_def *chandef,
411 enum ieee80211_chanctx_mode mode)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200412{
413 struct ieee80211_chanctx *ctx;
414
415 lockdep_assert_held(&local->chanctx_mtx);
416
417 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
418 if (!ctx)
Michal Kaziored68ebc2014-04-09 15:29:30 +0200419 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200420
Michal Kazior484298a2014-04-09 15:29:26 +0200421 INIT_LIST_HEAD(&ctx->assigned_vifs);
Michal Kaziore3afb922014-04-09 15:29:27 +0200422 INIT_LIST_HEAD(&ctx->reserved_vifs);
Johannes Berg4bf88532012-11-09 11:39:59 +0100423 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200424 ctx->conf.rx_chains_static = 1;
425 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200426 ctx->mode = mode;
Simon Wunderliche4746852013-04-08 22:43:16 +0200427 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
Eliad Peller21f659b2013-11-11 20:14:01 +0200428 ieee80211_recalc_chanctx_min_def(local, ctx);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200429
430 return ctx;
431}
432
433static int ieee80211_add_chanctx(struct ieee80211_local *local,
434 struct ieee80211_chanctx *ctx)
435{
436 u32 changed;
437 int err;
438
439 lockdep_assert_held(&local->mtx);
440 lockdep_assert_held(&local->chanctx_mtx);
441
Simon Wunderliche4746852013-04-08 22:43:16 +0200442 if (!local->use_chanctx)
443 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200444
Johannes Berg382a1032013-03-22 22:30:09 +0100445 /* turn idle off *before* setting channel -- some drivers need that */
446 changed = ieee80211_idle_off(local);
447 if (changed)
448 ieee80211_hw_config(local, changed);
449
Johannes Berg55de9082012-07-26 17:24:39 +0200450 if (!local->use_chanctx) {
Michal Kaziored68ebc2014-04-09 15:29:30 +0200451 local->_oper_chandef = ctx->conf.def;
Michal Kazior9b4816f2014-04-04 13:02:43 +0200452 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg55de9082012-07-26 17:24:39 +0200453 } else {
454 err = drv_add_chanctx(local, ctx);
455 if (err) {
Johannes Berg382a1032013-03-22 22:30:09 +0100456 ieee80211_recalc_idle(local);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200457 return err;
Johannes Berg55de9082012-07-26 17:24:39 +0200458 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200459 }
460
Michal Kaziored68ebc2014-04-09 15:29:30 +0200461 return 0;
462}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200463
Michal Kaziored68ebc2014-04-09 15:29:30 +0200464static struct ieee80211_chanctx *
465ieee80211_new_chanctx(struct ieee80211_local *local,
466 const struct cfg80211_chan_def *chandef,
467 enum ieee80211_chanctx_mode mode)
468{
469 struct ieee80211_chanctx *ctx;
470 int err;
471
472 lockdep_assert_held(&local->mtx);
473 lockdep_assert_held(&local->chanctx_mtx);
474
475 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
476 if (!ctx)
477 return ERR_PTR(-ENOMEM);
478
479 err = ieee80211_add_chanctx(local, ctx);
480 if (err) {
481 kfree(ctx);
482 return ERR_PTR(err);
483 }
484
485 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200486 return ctx;
487}
488
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200489static void ieee80211_del_chanctx(struct ieee80211_local *local,
490 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200491{
492 lockdep_assert_held(&local->chanctx_mtx);
493
Johannes Berg55de9082012-07-26 17:24:39 +0200494 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100495 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
496 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
497 chandef->center_freq1 = chandef->chan->center_freq;
498 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200499
500 /* NOTE: Disabling radar is only valid here for
501 * single channel context. To be sure, check it ...
502 */
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200503 WARN_ON(local->hw.conf.radar_enabled &&
504 !list_empty(&local->chanctx_list));
505
Simon Wunderliche4746852013-04-08 22:43:16 +0200506 local->hw.conf.radar_enabled = false;
507
Michal Kazior9b4816f2014-04-04 13:02:43 +0200508 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg55de9082012-07-26 17:24:39 +0200509 } else {
510 drv_remove_chanctx(local, ctx);
511 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200512
Johannes Bergfd0f9792013-02-07 00:14:51 +0100513 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200514}
515
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200516static void ieee80211_free_chanctx(struct ieee80211_local *local,
517 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200518{
Michal Kaziord01a1e62012-06-26 14:37:16 +0200519 lockdep_assert_held(&local->chanctx_mtx);
520
Michal Kaziorc0166da2014-04-09 15:29:33 +0200521 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200522
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200523 list_del_rcu(&ctx->list);
524 ieee80211_del_chanctx(local, ctx);
525 kfree_rcu(ctx, rcu_head);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200526}
527
Johannes Berg4bf88532012-11-09 11:39:59 +0100528static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
529 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200530{
531 struct ieee80211_chanctx_conf *conf = &ctx->conf;
532 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100533 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200534
535 lockdep_assert_held(&local->chanctx_mtx);
536
537 rcu_read_lock();
538 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100539
Michal Kaziore89a96f2012-06-26 14:37:22 +0200540 if (!ieee80211_sdata_running(sdata))
541 continue;
542 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
543 continue;
Felix Fietkau0e67c132014-07-25 16:20:22 +0200544 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
545 continue;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200546
Johannes Berg4bf88532012-11-09 11:39:59 +0100547 if (!compat)
548 compat = &sdata->vif.bss_conf.chandef;
549
550 compat = cfg80211_chandef_compatible(
551 &sdata->vif.bss_conf.chandef, compat);
552 if (!compat)
553 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200554 }
555 rcu_read_unlock();
556
Johannes Berg4bf88532012-11-09 11:39:59 +0100557 if (WARN_ON_ONCE(!compat))
558 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200559
Johannes Berg18942d32013-02-07 21:30:37 +0100560 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200561}
562
Johannes Berg367bbd12013-12-18 09:36:09 +0100563static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
564 struct ieee80211_chanctx *chanctx)
565{
566 bool radar_enabled;
567
568 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100569 /* for setting local->radar_detect_enabled */
570 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100571
572 radar_enabled = ieee80211_is_radar_required(local);
573
574 if (radar_enabled == chanctx->conf.radar_enabled)
575 return;
576
577 chanctx->conf.radar_enabled = radar_enabled;
578 local->radar_detect_enabled = chanctx->conf.radar_enabled;
579
580 if (!local->use_chanctx) {
581 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
582 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
583 }
584
585 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
586}
587
Luciano Coelho77eeba92014-03-11 18:24:12 +0200588static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
589 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200590{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200591 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200592 struct ieee80211_chanctx_conf *conf;
593 struct ieee80211_chanctx *curr_ctx = NULL;
594 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200595
Luciano Coelho77eeba92014-03-11 18:24:12 +0200596 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
597 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200598
Luciano Coelho77eeba92014-03-11 18:24:12 +0200599 if (conf) {
600 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200601
Luciano Coelho77eeba92014-03-11 18:24:12 +0200602 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
603 conf = NULL;
Michal Kazior484298a2014-04-09 15:29:26 +0200604 list_del(&sdata->assigned_chanctx_list);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200605 }
606
607 if (new_ctx) {
608 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
609 if (ret)
610 goto out;
611
Luciano Coelho77eeba92014-03-11 18:24:12 +0200612 conf = &new_ctx->conf;
Michal Kazior484298a2014-04-09 15:29:26 +0200613 list_add(&sdata->assigned_chanctx_list,
614 &new_ctx->assigned_vifs);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200615 }
616
617out:
618 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
619
620 sdata->vif.bss_conf.idle = !conf;
621
Michal Kaziorc0166da2014-04-09 15:29:33 +0200622 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200623 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
624 ieee80211_recalc_smps_chanctx(local, curr_ctx);
625 ieee80211_recalc_radar_chanctx(local, curr_ctx);
626 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
627 }
628
Michal Kaziorc0166da2014-04-09 15:29:33 +0200629 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200630 ieee80211_recalc_txpower(sdata);
631 ieee80211_recalc_chanctx_min_def(local, new_ctx);
632 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100633
634 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
635 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200636 ieee80211_bss_info_change_notify(sdata,
637 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100638
Luciano Coelho77eeba92014-03-11 18:24:12 +0200639 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200640}
641
642static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
643{
644 struct ieee80211_local *local = sdata->local;
645 struct ieee80211_chanctx_conf *conf;
646 struct ieee80211_chanctx *ctx;
Michal Kazior5bcae312014-06-25 12:35:06 +0200647 bool use_reserved_switch = false;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200648
649 lockdep_assert_held(&local->chanctx_mtx);
650
651 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
652 lockdep_is_held(&local->chanctx_mtx));
653 if (!conf)
654 return;
655
656 ctx = container_of(conf, struct ieee80211_chanctx, conf);
657
Michal Kazior5bcae312014-06-25 12:35:06 +0200658 if (sdata->reserved_chanctx) {
659 if (sdata->reserved_chanctx->replace_state ==
660 IEEE80211_CHANCTX_REPLACES_OTHER &&
661 ieee80211_chanctx_num_reserved(local,
662 sdata->reserved_chanctx) > 1)
663 use_reserved_switch = true;
664
Luciano Coelho11335a52013-10-30 13:09:39 +0200665 ieee80211_vif_unreserve_chanctx(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +0200666 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200667
Luciano Coelho77eeba92014-03-11 18:24:12 +0200668 ieee80211_assign_vif_chanctx(sdata, NULL);
Michal Kaziorc0166da2014-04-09 15:29:33 +0200669 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200670 ieee80211_free_chanctx(local, ctx);
Michal Kazior5bcae312014-06-25 12:35:06 +0200671
672 /* Unreserving may ready an in-place reservation. */
673 if (use_reserved_switch)
674 ieee80211_vif_use_reserved_switch(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200675}
676
Johannes Berg04ecd252012-09-11 14:34:12 +0200677void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
678 struct ieee80211_chanctx *chanctx)
679{
680 struct ieee80211_sub_if_data *sdata;
681 u8 rx_chains_static, rx_chains_dynamic;
682
683 lockdep_assert_held(&local->chanctx_mtx);
684
685 rx_chains_static = 1;
686 rx_chains_dynamic = 1;
687
688 rcu_read_lock();
689 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
690 u8 needed_static, needed_dynamic;
691
692 if (!ieee80211_sdata_running(sdata))
693 continue;
694
695 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
696 &chanctx->conf)
697 continue;
698
699 switch (sdata->vif.type) {
700 case NL80211_IFTYPE_P2P_DEVICE:
701 continue;
702 case NL80211_IFTYPE_STATION:
703 if (!sdata->u.mgd.associated)
704 continue;
705 break;
706 case NL80211_IFTYPE_AP_VLAN:
707 continue;
708 case NL80211_IFTYPE_AP:
709 case NL80211_IFTYPE_ADHOC:
710 case NL80211_IFTYPE_WDS:
711 case NL80211_IFTYPE_MESH_POINT:
712 break;
713 default:
714 WARN_ON_ONCE(1);
715 }
716
717 switch (sdata->smps_mode) {
718 default:
719 WARN_ONCE(1, "Invalid SMPS mode %d\n",
720 sdata->smps_mode);
721 /* fall through */
722 case IEEE80211_SMPS_OFF:
723 needed_static = sdata->needed_rx_chains;
724 needed_dynamic = sdata->needed_rx_chains;
725 break;
726 case IEEE80211_SMPS_DYNAMIC:
727 needed_static = 1;
728 needed_dynamic = sdata->needed_rx_chains;
729 break;
730 case IEEE80211_SMPS_STATIC:
731 needed_static = 1;
732 needed_dynamic = 1;
733 break;
734 }
735
736 rx_chains_static = max(rx_chains_static, needed_static);
737 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
738 }
Ido Yariv7b8a9cd2014-03-24 09:55:45 +0200739
740 /* Disable SMPS for the monitor interface */
741 sdata = rcu_dereference(local->monitor_sdata);
742 if (sdata &&
743 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
744 rx_chains_dynamic = rx_chains_static = local->rx_chains;
745
Johannes Berg04ecd252012-09-11 14:34:12 +0200746 rcu_read_unlock();
747
748 if (!local->use_chanctx) {
749 if (rx_chains_static > 1)
750 local->smps_mode = IEEE80211_SMPS_OFF;
751 else if (rx_chains_dynamic > 1)
752 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
753 else
754 local->smps_mode = IEEE80211_SMPS_STATIC;
755 ieee80211_hw_config(local, 0);
756 }
757
758 if (rx_chains_static == chanctx->conf.rx_chains_static &&
759 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
760 return;
761
762 chanctx->conf.rx_chains_static = rx_chains_static;
763 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
764 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
765}
766
Michal Kaziord01a1e62012-06-26 14:37:16 +0200767int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100768 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200769 enum ieee80211_chanctx_mode mode)
770{
771 struct ieee80211_local *local = sdata->local;
772 struct ieee80211_chanctx *ctx;
Luciano Coelho73de86a2014-02-13 11:31:59 +0200773 u8 radar_detect_width = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200774 int ret;
775
Johannes Berg34a37402013-12-18 09:43:33 +0100776 lockdep_assert_held(&local->mtx);
777
Johannes Berg55de9082012-07-26 17:24:39 +0200778 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
779
Michal Kaziord01a1e62012-06-26 14:37:16 +0200780 mutex_lock(&local->chanctx_mtx);
Luciano Coelho73de86a2014-02-13 11:31:59 +0200781
782 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
783 chandef,
784 sdata->wdev.iftype);
785 if (ret < 0)
786 goto out;
787 if (ret > 0)
788 radar_detect_width = BIT(chandef->width);
789
790 sdata->radar_required = ret;
791
792 ret = ieee80211_check_combinations(sdata, chandef, mode,
793 radar_detect_width);
794 if (ret < 0)
795 goto out;
796
Michal Kaziord01a1e62012-06-26 14:37:16 +0200797 __ieee80211_vif_release_channel(sdata);
798
Johannes Berg4bf88532012-11-09 11:39:59 +0100799 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200800 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100801 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200802 if (IS_ERR(ctx)) {
803 ret = PTR_ERR(ctx);
804 goto out;
805 }
806
Johannes Berg4bf88532012-11-09 11:39:59 +0100807 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200808
Michal Kaziord01a1e62012-06-26 14:37:16 +0200809 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
810 if (ret) {
811 /* if assign fails refcount stays the same */
Michal Kaziorc0166da2014-04-09 15:29:33 +0200812 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200813 ieee80211_free_chanctx(local, ctx);
814 goto out;
815 }
816
Johannes Berg04ecd252012-09-11 14:34:12 +0200817 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100818 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200819 out:
820 mutex_unlock(&local->chanctx_mtx);
821 return ret;
822}
823
Luciano Coelho11335a52013-10-30 13:09:39 +0200824static void
825__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
826 bool clear)
827{
Johannes Berg33926eb2014-04-09 21:31:13 +0200828 struct ieee80211_local *local __maybe_unused = sdata->local;
Luciano Coelho11335a52013-10-30 13:09:39 +0200829 struct ieee80211_sub_if_data *vlan;
830 struct ieee80211_chanctx_conf *conf;
831
832 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
833 return;
834
835 lockdep_assert_held(&local->mtx);
836
837 /* Check that conf exists, even when clearing this function
838 * must be called with the AP's channel context still there
839 * as it would otherwise cause VLANs to have an invalid
840 * channel context pointer for a while, possibly pointing
841 * to a channel context that has already been freed.
842 */
843 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
Johannes Berg33926eb2014-04-09 21:31:13 +0200844 lockdep_is_held(&local->chanctx_mtx));
Luciano Coelho11335a52013-10-30 13:09:39 +0200845 WARN_ON(!conf);
846
847 if (clear)
848 conf = NULL;
849
850 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
851 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
852}
853
854void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
855 bool clear)
856{
857 struct ieee80211_local *local = sdata->local;
858
859 mutex_lock(&local->chanctx_mtx);
860
861 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
862
863 mutex_unlock(&local->chanctx_mtx);
864}
865
866int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
867{
Michal Kaziore3afb922014-04-09 15:29:27 +0200868 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
869
Luciano Coelho11335a52013-10-30 13:09:39 +0200870 lockdep_assert_held(&sdata->local->chanctx_mtx);
871
Michal Kaziore3afb922014-04-09 15:29:27 +0200872 if (WARN_ON(!ctx))
Luciano Coelho11335a52013-10-30 13:09:39 +0200873 return -EINVAL;
874
Michal Kaziore3afb922014-04-09 15:29:27 +0200875 list_del(&sdata->reserved_chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200876 sdata->reserved_chanctx = NULL;
877
Michal Kazior5bcae312014-06-25 12:35:06 +0200878 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
879 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
880 if (WARN_ON(!ctx->replace_ctx))
881 return -EINVAL;
882
883 WARN_ON(ctx->replace_ctx->replace_state !=
884 IEEE80211_CHANCTX_WILL_BE_REPLACED);
885 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
886
887 ctx->replace_ctx->replace_ctx = NULL;
888 ctx->replace_ctx->replace_state =
889 IEEE80211_CHANCTX_REPLACE_NONE;
890
891 list_del_rcu(&ctx->list);
892 kfree_rcu(ctx, rcu_head);
893 } else {
894 ieee80211_free_chanctx(sdata->local, ctx);
895 }
896 }
Michal Kaziore3afb922014-04-09 15:29:27 +0200897
Luciano Coelho11335a52013-10-30 13:09:39 +0200898 return 0;
899}
900
901int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
902 const struct cfg80211_chan_def *chandef,
Michal Kazior09332482014-04-09 15:29:25 +0200903 enum ieee80211_chanctx_mode mode,
904 bool radar_required)
Luciano Coelho11335a52013-10-30 13:09:39 +0200905{
906 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +0200907 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
Luciano Coelho11335a52013-10-30 13:09:39 +0200908
Michal Kazior5bcae312014-06-25 12:35:06 +0200909 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +0200910
Michal Kazior5bcae312014-06-25 12:35:06 +0200911 curr_ctx = ieee80211_vif_get_chanctx(sdata);
912 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
913 return -ENOTSUPP;
Luciano Coelho11335a52013-10-30 13:09:39 +0200914
Michal Kazior13f348a2014-04-09 15:29:29 +0200915 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
Luciano Coelho11335a52013-10-30 13:09:39 +0200916 if (!new_ctx) {
Michal Kazior5bcae312014-06-25 12:35:06 +0200917 if (ieee80211_can_create_new_chanctx(local)) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200918 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kazior5bcae312014-06-25 12:35:06 +0200919 if (IS_ERR(new_ctx))
920 return PTR_ERR(new_ctx);
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200921 } else {
Michal Kazior5bcae312014-06-25 12:35:06 +0200922 if (!curr_ctx ||
923 (curr_ctx->replace_state ==
924 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
925 !list_empty(&curr_ctx->reserved_vifs)) {
926 /*
927 * Another vif already requested this context
928 * for a reservation. Find another one hoping
929 * all vifs assigned to it will also switch
930 * soon enough.
931 *
932 * TODO: This needs a little more work as some
933 * cases (more than 2 chanctx capable devices)
934 * may fail which could otherwise succeed
935 * provided some channel context juggling was
936 * performed.
937 *
938 * Consider ctx1..3, vif1..6, each ctx has 2
939 * vifs. vif1 and vif2 from ctx1 request new
940 * different chandefs starting 2 in-place
941 * reserations with ctx4 and ctx5 replacing
942 * ctx1 and ctx2 respectively. Next vif5 and
943 * vif6 from ctx3 reserve ctx4. If vif3 and
944 * vif4 remain on ctx2 as they are then this
945 * fails unless `replace_ctx` from ctx5 is
946 * replaced with ctx3.
947 */
948 list_for_each_entry(ctx, &local->chanctx_list,
949 list) {
950 if (ctx->replace_state !=
951 IEEE80211_CHANCTX_REPLACE_NONE)
952 continue;
953
954 if (!list_empty(&ctx->reserved_vifs))
955 continue;
956
957 curr_ctx = ctx;
958 break;
959 }
960 }
961
962 /*
963 * If that's true then all available contexts already
964 * have reservations and cannot be used.
965 */
966 if (!curr_ctx ||
967 (curr_ctx->replace_state ==
968 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
969 !list_empty(&curr_ctx->reserved_vifs))
970 return -EBUSY;
971
972 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
973 if (!new_ctx)
974 return -ENOMEM;
975
976 new_ctx->replace_ctx = curr_ctx;
977 new_ctx->replace_state =
978 IEEE80211_CHANCTX_REPLACES_OTHER;
979
980 curr_ctx->replace_ctx = new_ctx;
981 curr_ctx->replace_state =
982 IEEE80211_CHANCTX_WILL_BE_REPLACED;
983
984 list_add_rcu(&new_ctx->list, &local->chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200985 }
986 }
987
Michal Kaziore3afb922014-04-09 15:29:27 +0200988 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
Luciano Coelho11335a52013-10-30 13:09:39 +0200989 sdata->reserved_chanctx = new_ctx;
990 sdata->reserved_chandef = *chandef;
Michal Kazior09332482014-04-09 15:29:25 +0200991 sdata->reserved_radar_required = radar_required;
Michal Kazior5bcae312014-06-25 12:35:06 +0200992 sdata->reserved_ready = false;
993
994 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +0200995}
996
Michal Kazior03078de2014-06-25 12:35:08 +0200997static void
998ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
999{
1000 switch (sdata->vif.type) {
1001 case NL80211_IFTYPE_ADHOC:
1002 case NL80211_IFTYPE_AP:
1003 case NL80211_IFTYPE_MESH_POINT:
1004 ieee80211_queue_work(&sdata->local->hw,
1005 &sdata->csa_finalize_work);
1006 break;
Michal Kazior03078de2014-06-25 12:35:08 +02001007 case NL80211_IFTYPE_STATION:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001008 ieee80211_queue_work(&sdata->local->hw,
1009 &sdata->u.mgd.chswitch_work);
1010 break;
1011 case NL80211_IFTYPE_UNSPECIFIED:
Michal Kazior03078de2014-06-25 12:35:08 +02001012 case NL80211_IFTYPE_AP_VLAN:
1013 case NL80211_IFTYPE_WDS:
1014 case NL80211_IFTYPE_MONITOR:
1015 case NL80211_IFTYPE_P2P_CLIENT:
1016 case NL80211_IFTYPE_P2P_GO:
1017 case NL80211_IFTYPE_P2P_DEVICE:
1018 case NUM_NL80211_IFTYPES:
1019 WARN_ON(1);
1020 break;
1021 }
1022}
1023
Michal Kazior5bcae312014-06-25 12:35:06 +02001024static int
1025ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
Luciano Coelho11335a52013-10-30 13:09:39 +02001026{
1027 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +02001028 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1029 struct ieee80211_chanctx *old_ctx, *new_ctx;
1030 const struct cfg80211_chan_def *chandef;
1031 u32 changed = 0;
1032 int err;
Luciano Coelho11335a52013-10-30 13:09:39 +02001033
1034 lockdep_assert_held(&local->mtx);
Michal Kazior5bcae312014-06-25 12:35:06 +02001035 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001036
Michal Kazior5bcae312014-06-25 12:35:06 +02001037 new_ctx = sdata->reserved_chanctx;
1038 old_ctx = ieee80211_vif_get_chanctx(sdata);
Luciano Coelho11335a52013-10-30 13:09:39 +02001039
Michal Kazior5bcae312014-06-25 12:35:06 +02001040 if (WARN_ON(!sdata->reserved_ready))
1041 return -EBUSY;
1042
1043 if (WARN_ON(!new_ctx))
1044 return -EINVAL;
1045
1046 if (WARN_ON(!old_ctx))
1047 return -EINVAL;
1048
1049 if (WARN_ON(new_ctx->replace_state ==
1050 IEEE80211_CHANCTX_REPLACES_OTHER))
1051 return -EINVAL;
1052
1053 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1054 &sdata->reserved_chandef);
1055 if (WARN_ON(!chandef))
1056 return -EINVAL;
1057
1058 vif_chsw[0].vif = &sdata->vif;
1059 vif_chsw[0].old_ctx = &old_ctx->conf;
1060 vif_chsw[0].new_ctx = &new_ctx->conf;
1061
1062 list_del(&sdata->reserved_chanctx_list);
1063 sdata->reserved_chanctx = NULL;
1064
1065 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1066 CHANCTX_SWMODE_REASSIGN_VIF);
1067 if (err) {
1068 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1069 ieee80211_free_chanctx(local, new_ctx);
1070
Michal Kazior03078de2014-06-25 12:35:08 +02001071 goto out;
Luciano Coelho11335a52013-10-30 13:09:39 +02001072 }
1073
Michal Kazior5bcae312014-06-25 12:35:06 +02001074 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1075 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
Luciano Coelho11335a52013-10-30 13:09:39 +02001076
Michal Kazior5bcae312014-06-25 12:35:06 +02001077 if (sdata->vif.type == NL80211_IFTYPE_AP)
1078 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1079
1080 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1081 ieee80211_free_chanctx(local, old_ctx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001082
1083 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
Michal Kazior5bcae312014-06-25 12:35:06 +02001084 changed = BSS_CHANGED_BANDWIDTH;
Luciano Coelho11335a52013-10-30 13:09:39 +02001085
1086 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
1087
Michal Kazior5bcae312014-06-25 12:35:06 +02001088 if (changed)
1089 ieee80211_bss_info_change_notify(sdata, changed);
Luciano Coelho11335a52013-10-30 13:09:39 +02001090
Michal Kazior03078de2014-06-25 12:35:08 +02001091out:
1092 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001093 return err;
1094}
1095
1096static int
1097ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1098{
1099 struct ieee80211_local *local = sdata->local;
1100 struct ieee80211_chanctx *old_ctx, *new_ctx;
1101 const struct cfg80211_chan_def *chandef;
1102 int err;
1103
1104 old_ctx = ieee80211_vif_get_chanctx(sdata);
1105 new_ctx = sdata->reserved_chanctx;
1106
1107 if (WARN_ON(!sdata->reserved_ready))
1108 return -EINVAL;
1109
1110 if (WARN_ON(old_ctx))
1111 return -EINVAL;
1112
1113 if (WARN_ON(!new_ctx))
1114 return -EINVAL;
1115
1116 if (WARN_ON(new_ctx->replace_state ==
1117 IEEE80211_CHANCTX_REPLACES_OTHER))
1118 return -EINVAL;
1119
1120 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1121 &sdata->reserved_chandef);
1122 if (WARN_ON(!chandef))
1123 return -EINVAL;
1124
1125 list_del(&sdata->reserved_chanctx_list);
1126 sdata->reserved_chanctx = NULL;
1127
1128 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1129 if (err) {
1130 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1131 ieee80211_free_chanctx(local, new_ctx);
1132
1133 goto out;
1134 }
1135
1136out:
Michal Kazior03078de2014-06-25 12:35:08 +02001137 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001138 return err;
1139}
1140
1141static bool
1142ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1143{
1144 struct ieee80211_chanctx *old_ctx, *new_ctx;
1145
1146 lockdep_assert_held(&sdata->local->chanctx_mtx);
1147
1148 new_ctx = sdata->reserved_chanctx;
1149 old_ctx = ieee80211_vif_get_chanctx(sdata);
1150
1151 if (!old_ctx)
1152 return false;
1153
1154 if (WARN_ON(!new_ctx))
1155 return false;
1156
1157 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1158 return false;
1159
1160 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1161 return false;
1162
1163 return true;
1164}
1165
1166static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1167 struct ieee80211_chanctx *new_ctx)
1168{
1169 const struct cfg80211_chan_def *chandef;
1170
1171 lockdep_assert_held(&local->mtx);
1172 lockdep_assert_held(&local->chanctx_mtx);
1173
1174 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1175 if (WARN_ON(!chandef))
1176 return -EINVAL;
1177
1178 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1179 local->_oper_chandef = *chandef;
1180 ieee80211_hw_config(local, 0);
1181
1182 return 0;
1183}
1184
1185static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1186 int n_vifs)
1187{
1188 struct ieee80211_vif_chanctx_switch *vif_chsw;
1189 struct ieee80211_sub_if_data *sdata;
1190 struct ieee80211_chanctx *ctx, *old_ctx;
1191 int i, err;
1192
1193 lockdep_assert_held(&local->mtx);
1194 lockdep_assert_held(&local->chanctx_mtx);
1195
1196 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1197 if (!vif_chsw)
1198 return -ENOMEM;
1199
1200 i = 0;
1201 list_for_each_entry(ctx, &local->chanctx_list, list) {
1202 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1203 continue;
1204
1205 if (WARN_ON(!ctx->replace_ctx)) {
1206 err = -EINVAL;
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001207 goto out;
1208 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001209
Michal Kazior5bcae312014-06-25 12:35:06 +02001210 list_for_each_entry(sdata, &ctx->reserved_vifs,
1211 reserved_chanctx_list) {
1212 if (!ieee80211_vif_has_in_place_reservation(
1213 sdata))
1214 continue;
1215
1216 old_ctx = ieee80211_vif_get_chanctx(sdata);
1217 vif_chsw[i].vif = &sdata->vif;
1218 vif_chsw[i].old_ctx = &old_ctx->conf;
1219 vif_chsw[i].new_ctx = &ctx->conf;
1220
1221 i++;
1222 }
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001223 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001224
Michal Kazior5bcae312014-06-25 12:35:06 +02001225 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1226 CHANCTX_SWMODE_SWAP_CONTEXTS);
Luciano Coelho11335a52013-10-30 13:09:39 +02001227
Luciano Coelho11335a52013-10-30 13:09:39 +02001228out:
Michal Kazior5bcae312014-06-25 12:35:06 +02001229 kfree(vif_chsw);
1230 return err;
1231}
1232
1233static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1234{
1235 struct ieee80211_chanctx *ctx;
1236 int err;
1237
1238 lockdep_assert_held(&local->mtx);
1239 lockdep_assert_held(&local->chanctx_mtx);
1240
1241 list_for_each_entry(ctx, &local->chanctx_list, list) {
1242 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1243 continue;
1244
1245 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1246 continue;
1247
1248 ieee80211_del_chanctx(local, ctx->replace_ctx);
1249 err = ieee80211_add_chanctx(local, ctx);
1250 if (err)
1251 goto err;
1252 }
1253
1254 return 0;
1255
1256err:
1257 WARN_ON(ieee80211_add_chanctx(local, ctx));
1258 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1259 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1260 continue;
1261
1262 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1263 continue;
1264
1265 ieee80211_del_chanctx(local, ctx);
1266 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1267 }
1268
1269 return err;
1270}
1271
1272int
1273ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1274{
1275 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1276 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1277 struct ieee80211_chanctx *new_ctx = NULL;
1278 int i, err, n_assigned, n_reserved, n_ready;
1279 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1280
1281 lockdep_assert_held(&local->mtx);
1282 lockdep_assert_held(&local->chanctx_mtx);
1283
1284 /*
1285 * If there are 2 independent pairs of channel contexts performing
1286 * cross-switch of their vifs this code will still wait until both are
1287 * ready even though it could be possible to switch one before the
1288 * other is ready.
1289 *
1290 * For practical reasons and code simplicity just do a single huge
1291 * switch.
1292 */
1293
1294 /*
1295 * Verify if the reservation is still feasible.
1296 * - if it's not then disconnect
1297 * - if it is but not all vifs necessary are ready then defer
1298 */
1299
1300 list_for_each_entry(ctx, &local->chanctx_list, list) {
1301 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1302 continue;
1303
1304 if (WARN_ON(!ctx->replace_ctx)) {
1305 err = -EINVAL;
1306 goto err;
1307 }
1308
1309 if (!local->use_chanctx)
1310 new_ctx = ctx;
1311
1312 n_ctx++;
1313
1314 n_assigned = 0;
1315 n_reserved = 0;
1316 n_ready = 0;
1317
1318 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1319 assigned_chanctx_list) {
1320 n_assigned++;
1321 if (sdata->reserved_chanctx) {
1322 n_reserved++;
1323 if (sdata->reserved_ready)
1324 n_ready++;
1325 }
1326 }
1327
1328 if (n_assigned != n_reserved) {
1329 if (n_ready == n_reserved) {
1330 wiphy_info(local->hw.wiphy,
1331 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1332 err = -EBUSY;
1333 goto err;
1334 }
1335
1336 return -EAGAIN;
1337 }
1338
1339 ctx->conf.radar_enabled = false;
1340 list_for_each_entry(sdata, &ctx->reserved_vifs,
1341 reserved_chanctx_list) {
1342 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1343 !sdata->reserved_ready)
1344 return -EAGAIN;
1345
1346 old_ctx = ieee80211_vif_get_chanctx(sdata);
1347 if (old_ctx) {
1348 if (old_ctx->replace_state ==
1349 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1350 n_vifs_switch++;
1351 else
1352 n_vifs_assign++;
1353 } else {
1354 n_vifs_ctxless++;
1355 }
1356
1357 if (sdata->reserved_radar_required)
1358 ctx->conf.radar_enabled = true;
1359 }
1360 }
1361
1362 if (WARN_ON(n_ctx == 0) ||
1363 WARN_ON(n_vifs_switch == 0 &&
1364 n_vifs_assign == 0 &&
1365 n_vifs_ctxless == 0) ||
1366 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1367 WARN_ON(!new_ctx && !local->use_chanctx)) {
1368 err = -EINVAL;
1369 goto err;
1370 }
1371
1372 /*
1373 * All necessary vifs are ready. Perform the switch now depending on
1374 * reservations and driver capabilities.
1375 */
1376
1377 if (local->use_chanctx) {
1378 if (n_vifs_switch > 0) {
1379 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1380 if (err)
1381 goto err;
1382 }
1383
1384 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1385 err = ieee80211_chsw_switch_ctxs(local);
1386 if (err)
1387 goto err;
1388 }
1389 } else {
1390 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1391 if (err)
1392 goto err;
1393 }
1394
1395 /*
1396 * Update all structures, values and pointers to point to new channel
1397 * context(s).
1398 */
1399
1400 i = 0;
1401 list_for_each_entry(ctx, &local->chanctx_list, list) {
1402 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1403 continue;
1404
1405 if (WARN_ON(!ctx->replace_ctx)) {
1406 err = -EINVAL;
1407 goto err;
1408 }
1409
1410 list_for_each_entry(sdata, &ctx->reserved_vifs,
1411 reserved_chanctx_list) {
1412 u32 changed = 0;
1413
1414 if (!ieee80211_vif_has_in_place_reservation(sdata))
1415 continue;
1416
1417 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1418
1419 if (sdata->vif.type == NL80211_IFTYPE_AP)
1420 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1421 false);
1422
1423 sdata->radar_required = sdata->reserved_radar_required;
1424
1425 if (sdata->vif.bss_conf.chandef.width !=
1426 sdata->reserved_chandef.width)
1427 changed = BSS_CHANGED_BANDWIDTH;
1428
1429 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
1430 if (changed)
1431 ieee80211_bss_info_change_notify(sdata,
1432 changed);
1433
1434 ieee80211_recalc_txpower(sdata);
1435 }
1436
1437 ieee80211_recalc_chanctx_chantype(local, ctx);
1438 ieee80211_recalc_smps_chanctx(local, ctx);
1439 ieee80211_recalc_radar_chanctx(local, ctx);
1440 ieee80211_recalc_chanctx_min_def(local, ctx);
1441
1442 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1443 reserved_chanctx_list) {
1444 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1445 continue;
1446
1447 list_del(&sdata->reserved_chanctx_list);
1448 list_move(&sdata->assigned_chanctx_list,
1449 &new_ctx->assigned_vifs);
1450 sdata->reserved_chanctx = NULL;
Michal Kazior03078de2014-06-25 12:35:08 +02001451
1452 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001453 }
1454
1455 /*
1456 * This context might have been a dependency for an already
1457 * ready re-assign reservation interface that was deferred. Do
1458 * not propagate error to the caller though. The in-place
1459 * reservation for originally requested interface has already
1460 * succeeded at this point.
1461 */
1462 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1463 reserved_chanctx_list) {
1464 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1465 sdata)))
1466 continue;
1467
1468 if (WARN_ON(sdata->reserved_chanctx != ctx))
1469 continue;
1470
1471 if (!sdata->reserved_ready)
1472 continue;
1473
1474 if (ieee80211_vif_get_chanctx(sdata))
1475 err = ieee80211_vif_use_reserved_reassign(
1476 sdata);
1477 else
1478 err = ieee80211_vif_use_reserved_assign(sdata);
1479
1480 if (err) {
1481 sdata_info(sdata,
1482 "failed to finalize (re-)assign reservation (err=%d)\n",
1483 err);
1484 ieee80211_vif_unreserve_chanctx(sdata);
1485 cfg80211_stop_iface(local->hw.wiphy,
1486 &sdata->wdev,
1487 GFP_KERNEL);
1488 }
1489 }
1490 }
1491
1492 /*
1493 * Finally free old contexts
1494 */
1495
1496 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1497 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1498 continue;
1499
1500 ctx->replace_ctx->replace_ctx = NULL;
1501 ctx->replace_ctx->replace_state =
1502 IEEE80211_CHANCTX_REPLACE_NONE;
1503
1504 list_del_rcu(&ctx->list);
1505 kfree_rcu(ctx, rcu_head);
1506 }
1507
1508 return 0;
1509
1510err:
1511 list_for_each_entry(ctx, &local->chanctx_list, list) {
1512 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1513 continue;
1514
1515 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
Michal Kazior03078de2014-06-25 12:35:08 +02001516 reserved_chanctx_list) {
Michal Kazior5bcae312014-06-25 12:35:06 +02001517 ieee80211_vif_unreserve_chanctx(sdata);
Michal Kazior03078de2014-06-25 12:35:08 +02001518 ieee80211_vif_chanctx_reservation_complete(sdata);
1519 }
Michal Kazior5bcae312014-06-25 12:35:06 +02001520 }
1521
1522 return err;
1523}
1524
1525int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1526{
1527 struct ieee80211_local *local = sdata->local;
1528 struct ieee80211_chanctx *new_ctx;
1529 struct ieee80211_chanctx *old_ctx;
1530 int err;
1531
1532 lockdep_assert_held(&local->mtx);
1533 lockdep_assert_held(&local->chanctx_mtx);
1534
1535 new_ctx = sdata->reserved_chanctx;
1536 old_ctx = ieee80211_vif_get_chanctx(sdata);
1537
1538 if (WARN_ON(!new_ctx))
1539 return -EINVAL;
1540
1541 if (WARN_ON(new_ctx->replace_state ==
1542 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1543 return -EINVAL;
1544
1545 if (WARN_ON(sdata->reserved_ready))
1546 return -EINVAL;
1547
1548 sdata->reserved_ready = true;
1549
1550 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1551 if (old_ctx)
1552 err = ieee80211_vif_use_reserved_reassign(sdata);
1553 else
1554 err = ieee80211_vif_use_reserved_assign(sdata);
1555
1556 if (err)
1557 return err;
1558 }
1559
1560 /*
1561 * In-place reservation may need to be finalized now either if:
1562 * a) sdata is taking part in the swapping itself and is the last one
1563 * b) sdata has switched with a re-assign reservation to an existing
1564 * context readying in-place switching of old_ctx
1565 *
1566 * In case of (b) do not propagate the error up because the requested
1567 * sdata already switched successfully. Just spill an extra warning.
1568 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1569 * interfaces upon failure.
1570 */
1571 if ((old_ctx &&
1572 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1573 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1574 err = ieee80211_vif_use_reserved_switch(local);
1575 if (err && err != -EAGAIN) {
1576 if (new_ctx->replace_state ==
1577 IEEE80211_CHANCTX_REPLACES_OTHER)
1578 return err;
1579
1580 wiphy_info(local->hw.wiphy,
1581 "depending in-place reservation failed (err=%d)\n",
1582 err);
1583 }
1584 }
1585
1586 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +02001587}
1588
Johannes Berg2c9b7352013-02-07 21:37:29 +01001589int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1590 const struct cfg80211_chan_def *chandef,
1591 u32 *changed)
1592{
1593 struct ieee80211_local *local = sdata->local;
1594 struct ieee80211_chanctx_conf *conf;
1595 struct ieee80211_chanctx *ctx;
Michal Kazior5bcae312014-06-25 12:35:06 +02001596 const struct cfg80211_chan_def *compat;
Johannes Berg2c9b7352013-02-07 21:37:29 +01001597 int ret;
1598
1599 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1600 IEEE80211_CHAN_DISABLED))
1601 return -EINVAL;
1602
1603 mutex_lock(&local->chanctx_mtx);
1604 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1605 ret = 0;
1606 goto out;
1607 }
1608
1609 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1610 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1611 ret = -EINVAL;
1612 goto out;
1613 }
1614
1615 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1616 lockdep_is_held(&local->chanctx_mtx));
1617 if (!conf) {
1618 ret = -EINVAL;
1619 goto out;
1620 }
1621
1622 ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior5bcae312014-06-25 12:35:06 +02001623
1624 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1625 if (!compat) {
Johannes Berg2c9b7352013-02-07 21:37:29 +01001626 ret = -EINVAL;
1627 goto out;
1628 }
1629
Michal Kazior5bcae312014-06-25 12:35:06 +02001630 switch (ctx->replace_state) {
1631 case IEEE80211_CHANCTX_REPLACE_NONE:
1632 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1633 ret = -EBUSY;
1634 goto out;
1635 }
1636 break;
1637 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1638 /* TODO: Perhaps the bandwith change could be treated as a
1639 * reservation itself? */
1640 ret = -EBUSY;
1641 goto out;
1642 case IEEE80211_CHANCTX_REPLACES_OTHER:
1643 /* channel context that is going to replace another channel
1644 * context doesn't really exist and shouldn't be assigned
1645 * anywhere yet */
1646 WARN_ON(1);
1647 break;
1648 }
1649
Johannes Berg2c9b7352013-02-07 21:37:29 +01001650 sdata->vif.bss_conf.chandef = *chandef;
1651
1652 ieee80211_recalc_chanctx_chantype(local, ctx);
1653
1654 *changed |= BSS_CHANGED_BANDWIDTH;
1655 ret = 0;
1656 out:
1657 mutex_unlock(&local->chanctx_mtx);
1658 return ret;
1659}
1660
Michal Kaziord01a1e62012-06-26 14:37:16 +02001661void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1662{
Johannes Berg55de9082012-07-26 17:24:39 +02001663 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1664
Johannes Berg34a37402013-12-18 09:43:33 +01001665 lockdep_assert_held(&sdata->local->mtx);
1666
Michal Kaziord01a1e62012-06-26 14:37:16 +02001667 mutex_lock(&sdata->local->chanctx_mtx);
1668 __ieee80211_vif_release_channel(sdata);
1669 mutex_unlock(&sdata->local->chanctx_mtx);
1670}
Johannes Berg3448c002012-09-11 17:57:42 +02001671
Johannes Berg4d76d212012-12-11 20:38:41 +01001672void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1673{
1674 struct ieee80211_local *local = sdata->local;
1675 struct ieee80211_sub_if_data *ap;
1676 struct ieee80211_chanctx_conf *conf;
1677
1678 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1679 return;
1680
1681 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1682
1683 mutex_lock(&local->chanctx_mtx);
1684
1685 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1686 lockdep_is_held(&local->chanctx_mtx));
1687 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1688 mutex_unlock(&local->chanctx_mtx);
1689}
1690
Johannes Berg3448c002012-09-11 17:57:42 +02001691void ieee80211_iter_chan_contexts_atomic(
1692 struct ieee80211_hw *hw,
1693 void (*iter)(struct ieee80211_hw *hw,
1694 struct ieee80211_chanctx_conf *chanctx_conf,
1695 void *data),
1696 void *iter_data)
1697{
1698 struct ieee80211_local *local = hw_to_local(hw);
1699 struct ieee80211_chanctx *ctx;
1700
1701 rcu_read_lock();
1702 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +01001703 if (ctx->driver_present)
1704 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +02001705 rcu_read_unlock();
1706}
1707EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);