blob: 0375009ddc0db39fd5a365db8cb5e4122219d864 [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;
544
Johannes Berg4bf88532012-11-09 11:39:59 +0100545 if (!compat)
546 compat = &sdata->vif.bss_conf.chandef;
547
548 compat = cfg80211_chandef_compatible(
549 &sdata->vif.bss_conf.chandef, compat);
550 if (!compat)
551 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200552 }
553 rcu_read_unlock();
554
Johannes Berg4bf88532012-11-09 11:39:59 +0100555 if (WARN_ON_ONCE(!compat))
556 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200557
Johannes Berg18942d32013-02-07 21:30:37 +0100558 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200559}
560
Johannes Berg367bbd12013-12-18 09:36:09 +0100561static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
562 struct ieee80211_chanctx *chanctx)
563{
564 bool radar_enabled;
565
566 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100567 /* for setting local->radar_detect_enabled */
568 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100569
570 radar_enabled = ieee80211_is_radar_required(local);
571
572 if (radar_enabled == chanctx->conf.radar_enabled)
573 return;
574
575 chanctx->conf.radar_enabled = radar_enabled;
576 local->radar_detect_enabled = chanctx->conf.radar_enabled;
577
578 if (!local->use_chanctx) {
579 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
580 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
581 }
582
583 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
584}
585
Luciano Coelho77eeba92014-03-11 18:24:12 +0200586static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
587 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200588{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200589 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200590 struct ieee80211_chanctx_conf *conf;
591 struct ieee80211_chanctx *curr_ctx = NULL;
592 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200593
Luciano Coelho77eeba92014-03-11 18:24:12 +0200594 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
595 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200596
Luciano Coelho77eeba92014-03-11 18:24:12 +0200597 if (conf) {
598 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200599
Luciano Coelho77eeba92014-03-11 18:24:12 +0200600 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
601 conf = NULL;
Michal Kazior484298a2014-04-09 15:29:26 +0200602 list_del(&sdata->assigned_chanctx_list);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200603 }
604
605 if (new_ctx) {
606 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
607 if (ret)
608 goto out;
609
Luciano Coelho77eeba92014-03-11 18:24:12 +0200610 conf = &new_ctx->conf;
Michal Kazior484298a2014-04-09 15:29:26 +0200611 list_add(&sdata->assigned_chanctx_list,
612 &new_ctx->assigned_vifs);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200613 }
614
615out:
616 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
617
618 sdata->vif.bss_conf.idle = !conf;
619
Michal Kaziorc0166da2014-04-09 15:29:33 +0200620 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200621 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
622 ieee80211_recalc_smps_chanctx(local, curr_ctx);
623 ieee80211_recalc_radar_chanctx(local, curr_ctx);
624 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
625 }
626
Michal Kaziorc0166da2014-04-09 15:29:33 +0200627 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200628 ieee80211_recalc_txpower(sdata);
629 ieee80211_recalc_chanctx_min_def(local, new_ctx);
630 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100631
632 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
633 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200634 ieee80211_bss_info_change_notify(sdata,
635 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100636
Luciano Coelho77eeba92014-03-11 18:24:12 +0200637 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200638}
639
640static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
641{
642 struct ieee80211_local *local = sdata->local;
643 struct ieee80211_chanctx_conf *conf;
644 struct ieee80211_chanctx *ctx;
Michal Kazior5bcae312014-06-25 12:35:06 +0200645 bool use_reserved_switch = false;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200646
647 lockdep_assert_held(&local->chanctx_mtx);
648
649 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
650 lockdep_is_held(&local->chanctx_mtx));
651 if (!conf)
652 return;
653
654 ctx = container_of(conf, struct ieee80211_chanctx, conf);
655
Michal Kazior5bcae312014-06-25 12:35:06 +0200656 if (sdata->reserved_chanctx) {
657 if (sdata->reserved_chanctx->replace_state ==
658 IEEE80211_CHANCTX_REPLACES_OTHER &&
659 ieee80211_chanctx_num_reserved(local,
660 sdata->reserved_chanctx) > 1)
661 use_reserved_switch = true;
662
Luciano Coelho11335a52013-10-30 13:09:39 +0200663 ieee80211_vif_unreserve_chanctx(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +0200664 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200665
Luciano Coelho77eeba92014-03-11 18:24:12 +0200666 ieee80211_assign_vif_chanctx(sdata, NULL);
Michal Kaziorc0166da2014-04-09 15:29:33 +0200667 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200668 ieee80211_free_chanctx(local, ctx);
Michal Kazior5bcae312014-06-25 12:35:06 +0200669
670 /* Unreserving may ready an in-place reservation. */
671 if (use_reserved_switch)
672 ieee80211_vif_use_reserved_switch(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200673}
674
Johannes Berg04ecd252012-09-11 14:34:12 +0200675void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
676 struct ieee80211_chanctx *chanctx)
677{
678 struct ieee80211_sub_if_data *sdata;
679 u8 rx_chains_static, rx_chains_dynamic;
680
681 lockdep_assert_held(&local->chanctx_mtx);
682
683 rx_chains_static = 1;
684 rx_chains_dynamic = 1;
685
686 rcu_read_lock();
687 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
688 u8 needed_static, needed_dynamic;
689
690 if (!ieee80211_sdata_running(sdata))
691 continue;
692
693 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
694 &chanctx->conf)
695 continue;
696
697 switch (sdata->vif.type) {
698 case NL80211_IFTYPE_P2P_DEVICE:
699 continue;
700 case NL80211_IFTYPE_STATION:
701 if (!sdata->u.mgd.associated)
702 continue;
703 break;
704 case NL80211_IFTYPE_AP_VLAN:
705 continue;
706 case NL80211_IFTYPE_AP:
707 case NL80211_IFTYPE_ADHOC:
708 case NL80211_IFTYPE_WDS:
709 case NL80211_IFTYPE_MESH_POINT:
710 break;
711 default:
712 WARN_ON_ONCE(1);
713 }
714
715 switch (sdata->smps_mode) {
716 default:
717 WARN_ONCE(1, "Invalid SMPS mode %d\n",
718 sdata->smps_mode);
719 /* fall through */
720 case IEEE80211_SMPS_OFF:
721 needed_static = sdata->needed_rx_chains;
722 needed_dynamic = sdata->needed_rx_chains;
723 break;
724 case IEEE80211_SMPS_DYNAMIC:
725 needed_static = 1;
726 needed_dynamic = sdata->needed_rx_chains;
727 break;
728 case IEEE80211_SMPS_STATIC:
729 needed_static = 1;
730 needed_dynamic = 1;
731 break;
732 }
733
734 rx_chains_static = max(rx_chains_static, needed_static);
735 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
736 }
Ido Yariv7b8a9cd2014-03-24 09:55:45 +0200737
738 /* Disable SMPS for the monitor interface */
739 sdata = rcu_dereference(local->monitor_sdata);
740 if (sdata &&
741 rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
742 rx_chains_dynamic = rx_chains_static = local->rx_chains;
743
Johannes Berg04ecd252012-09-11 14:34:12 +0200744 rcu_read_unlock();
745
746 if (!local->use_chanctx) {
747 if (rx_chains_static > 1)
748 local->smps_mode = IEEE80211_SMPS_OFF;
749 else if (rx_chains_dynamic > 1)
750 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
751 else
752 local->smps_mode = IEEE80211_SMPS_STATIC;
753 ieee80211_hw_config(local, 0);
754 }
755
756 if (rx_chains_static == chanctx->conf.rx_chains_static &&
757 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
758 return;
759
760 chanctx->conf.rx_chains_static = rx_chains_static;
761 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
762 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
763}
764
Michal Kaziord01a1e62012-06-26 14:37:16 +0200765int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100766 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200767 enum ieee80211_chanctx_mode mode)
768{
769 struct ieee80211_local *local = sdata->local;
770 struct ieee80211_chanctx *ctx;
Luciano Coelho73de86a2014-02-13 11:31:59 +0200771 u8 radar_detect_width = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200772 int ret;
773
Johannes Berg34a37402013-12-18 09:43:33 +0100774 lockdep_assert_held(&local->mtx);
775
Johannes Berg55de9082012-07-26 17:24:39 +0200776 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
777
Michal Kaziord01a1e62012-06-26 14:37:16 +0200778 mutex_lock(&local->chanctx_mtx);
Luciano Coelho73de86a2014-02-13 11:31:59 +0200779
780 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
781 chandef,
782 sdata->wdev.iftype);
783 if (ret < 0)
784 goto out;
785 if (ret > 0)
786 radar_detect_width = BIT(chandef->width);
787
788 sdata->radar_required = ret;
789
790 ret = ieee80211_check_combinations(sdata, chandef, mode,
791 radar_detect_width);
792 if (ret < 0)
793 goto out;
794
Michal Kaziord01a1e62012-06-26 14:37:16 +0200795 __ieee80211_vif_release_channel(sdata);
796
Johannes Berg4bf88532012-11-09 11:39:59 +0100797 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200798 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100799 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200800 if (IS_ERR(ctx)) {
801 ret = PTR_ERR(ctx);
802 goto out;
803 }
804
Johannes Berg4bf88532012-11-09 11:39:59 +0100805 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200806
Michal Kaziord01a1e62012-06-26 14:37:16 +0200807 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
808 if (ret) {
809 /* if assign fails refcount stays the same */
Michal Kaziorc0166da2014-04-09 15:29:33 +0200810 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200811 ieee80211_free_chanctx(local, ctx);
812 goto out;
813 }
814
Johannes Berg04ecd252012-09-11 14:34:12 +0200815 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100816 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200817 out:
818 mutex_unlock(&local->chanctx_mtx);
819 return ret;
820}
821
Luciano Coelho11335a52013-10-30 13:09:39 +0200822static void
823__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
824 bool clear)
825{
Johannes Berg33926eb2014-04-09 21:31:13 +0200826 struct ieee80211_local *local __maybe_unused = sdata->local;
Luciano Coelho11335a52013-10-30 13:09:39 +0200827 struct ieee80211_sub_if_data *vlan;
828 struct ieee80211_chanctx_conf *conf;
829
830 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
831 return;
832
833 lockdep_assert_held(&local->mtx);
834
835 /* Check that conf exists, even when clearing this function
836 * must be called with the AP's channel context still there
837 * as it would otherwise cause VLANs to have an invalid
838 * channel context pointer for a while, possibly pointing
839 * to a channel context that has already been freed.
840 */
841 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
Johannes Berg33926eb2014-04-09 21:31:13 +0200842 lockdep_is_held(&local->chanctx_mtx));
Luciano Coelho11335a52013-10-30 13:09:39 +0200843 WARN_ON(!conf);
844
845 if (clear)
846 conf = NULL;
847
848 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
849 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
850}
851
852void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
853 bool clear)
854{
855 struct ieee80211_local *local = sdata->local;
856
857 mutex_lock(&local->chanctx_mtx);
858
859 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
860
861 mutex_unlock(&local->chanctx_mtx);
862}
863
864int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
865{
Michal Kaziore3afb922014-04-09 15:29:27 +0200866 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
867
Luciano Coelho11335a52013-10-30 13:09:39 +0200868 lockdep_assert_held(&sdata->local->chanctx_mtx);
869
Michal Kaziore3afb922014-04-09 15:29:27 +0200870 if (WARN_ON(!ctx))
Luciano Coelho11335a52013-10-30 13:09:39 +0200871 return -EINVAL;
872
Michal Kaziore3afb922014-04-09 15:29:27 +0200873 list_del(&sdata->reserved_chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200874 sdata->reserved_chanctx = NULL;
875
Michal Kazior5bcae312014-06-25 12:35:06 +0200876 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
877 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
878 if (WARN_ON(!ctx->replace_ctx))
879 return -EINVAL;
880
881 WARN_ON(ctx->replace_ctx->replace_state !=
882 IEEE80211_CHANCTX_WILL_BE_REPLACED);
883 WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
884
885 ctx->replace_ctx->replace_ctx = NULL;
886 ctx->replace_ctx->replace_state =
887 IEEE80211_CHANCTX_REPLACE_NONE;
888
889 list_del_rcu(&ctx->list);
890 kfree_rcu(ctx, rcu_head);
891 } else {
892 ieee80211_free_chanctx(sdata->local, ctx);
893 }
894 }
Michal Kaziore3afb922014-04-09 15:29:27 +0200895
Luciano Coelho11335a52013-10-30 13:09:39 +0200896 return 0;
897}
898
899int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
900 const struct cfg80211_chan_def *chandef,
Michal Kazior09332482014-04-09 15:29:25 +0200901 enum ieee80211_chanctx_mode mode,
902 bool radar_required)
Luciano Coelho11335a52013-10-30 13:09:39 +0200903{
904 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +0200905 struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
Luciano Coelho11335a52013-10-30 13:09:39 +0200906
Michal Kazior5bcae312014-06-25 12:35:06 +0200907 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +0200908
Michal Kazior5bcae312014-06-25 12:35:06 +0200909 curr_ctx = ieee80211_vif_get_chanctx(sdata);
910 if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
911 return -ENOTSUPP;
Luciano Coelho11335a52013-10-30 13:09:39 +0200912
Michal Kazior13f348a2014-04-09 15:29:29 +0200913 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
Luciano Coelho11335a52013-10-30 13:09:39 +0200914 if (!new_ctx) {
Michal Kazior5bcae312014-06-25 12:35:06 +0200915 if (ieee80211_can_create_new_chanctx(local)) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200916 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kazior5bcae312014-06-25 12:35:06 +0200917 if (IS_ERR(new_ctx))
918 return PTR_ERR(new_ctx);
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200919 } else {
Michal Kazior5bcae312014-06-25 12:35:06 +0200920 if (!curr_ctx ||
921 (curr_ctx->replace_state ==
922 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
923 !list_empty(&curr_ctx->reserved_vifs)) {
924 /*
925 * Another vif already requested this context
926 * for a reservation. Find another one hoping
927 * all vifs assigned to it will also switch
928 * soon enough.
929 *
930 * TODO: This needs a little more work as some
931 * cases (more than 2 chanctx capable devices)
932 * may fail which could otherwise succeed
933 * provided some channel context juggling was
934 * performed.
935 *
936 * Consider ctx1..3, vif1..6, each ctx has 2
937 * vifs. vif1 and vif2 from ctx1 request new
938 * different chandefs starting 2 in-place
939 * reserations with ctx4 and ctx5 replacing
940 * ctx1 and ctx2 respectively. Next vif5 and
941 * vif6 from ctx3 reserve ctx4. If vif3 and
942 * vif4 remain on ctx2 as they are then this
943 * fails unless `replace_ctx` from ctx5 is
944 * replaced with ctx3.
945 */
946 list_for_each_entry(ctx, &local->chanctx_list,
947 list) {
948 if (ctx->replace_state !=
949 IEEE80211_CHANCTX_REPLACE_NONE)
950 continue;
951
952 if (!list_empty(&ctx->reserved_vifs))
953 continue;
954
955 curr_ctx = ctx;
956 break;
957 }
958 }
959
960 /*
961 * If that's true then all available contexts already
962 * have reservations and cannot be used.
963 */
964 if (!curr_ctx ||
965 (curr_ctx->replace_state ==
966 IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
967 !list_empty(&curr_ctx->reserved_vifs))
968 return -EBUSY;
969
970 new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
971 if (!new_ctx)
972 return -ENOMEM;
973
974 new_ctx->replace_ctx = curr_ctx;
975 new_ctx->replace_state =
976 IEEE80211_CHANCTX_REPLACES_OTHER;
977
978 curr_ctx->replace_ctx = new_ctx;
979 curr_ctx->replace_state =
980 IEEE80211_CHANCTX_WILL_BE_REPLACED;
981
982 list_add_rcu(&new_ctx->list, &local->chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200983 }
984 }
985
Michal Kaziore3afb922014-04-09 15:29:27 +0200986 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
Luciano Coelho11335a52013-10-30 13:09:39 +0200987 sdata->reserved_chanctx = new_ctx;
988 sdata->reserved_chandef = *chandef;
Michal Kazior09332482014-04-09 15:29:25 +0200989 sdata->reserved_radar_required = radar_required;
Michal Kazior5bcae312014-06-25 12:35:06 +0200990 sdata->reserved_ready = false;
991
992 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +0200993}
994
Michal Kazior03078de2014-06-25 12:35:08 +0200995static void
996ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
997{
998 switch (sdata->vif.type) {
999 case NL80211_IFTYPE_ADHOC:
1000 case NL80211_IFTYPE_AP:
1001 case NL80211_IFTYPE_MESH_POINT:
1002 ieee80211_queue_work(&sdata->local->hw,
1003 &sdata->csa_finalize_work);
1004 break;
Michal Kazior03078de2014-06-25 12:35:08 +02001005 case NL80211_IFTYPE_STATION:
Michal Kazior4c3ebc52014-06-25 12:35:09 +02001006 ieee80211_queue_work(&sdata->local->hw,
1007 &sdata->u.mgd.chswitch_work);
1008 break;
1009 case NL80211_IFTYPE_UNSPECIFIED:
Michal Kazior03078de2014-06-25 12:35:08 +02001010 case NL80211_IFTYPE_AP_VLAN:
1011 case NL80211_IFTYPE_WDS:
1012 case NL80211_IFTYPE_MONITOR:
1013 case NL80211_IFTYPE_P2P_CLIENT:
1014 case NL80211_IFTYPE_P2P_GO:
1015 case NL80211_IFTYPE_P2P_DEVICE:
1016 case NUM_NL80211_IFTYPES:
1017 WARN_ON(1);
1018 break;
1019 }
1020}
1021
Michal Kazior5bcae312014-06-25 12:35:06 +02001022static int
1023ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
Luciano Coelho11335a52013-10-30 13:09:39 +02001024{
1025 struct ieee80211_local *local = sdata->local;
Michal Kazior5bcae312014-06-25 12:35:06 +02001026 struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
1027 struct ieee80211_chanctx *old_ctx, *new_ctx;
1028 const struct cfg80211_chan_def *chandef;
1029 u32 changed = 0;
1030 int err;
Luciano Coelho11335a52013-10-30 13:09:39 +02001031
1032 lockdep_assert_held(&local->mtx);
Michal Kazior5bcae312014-06-25 12:35:06 +02001033 lockdep_assert_held(&local->chanctx_mtx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001034
Michal Kazior5bcae312014-06-25 12:35:06 +02001035 new_ctx = sdata->reserved_chanctx;
1036 old_ctx = ieee80211_vif_get_chanctx(sdata);
Luciano Coelho11335a52013-10-30 13:09:39 +02001037
Michal Kazior5bcae312014-06-25 12:35:06 +02001038 if (WARN_ON(!sdata->reserved_ready))
1039 return -EBUSY;
1040
1041 if (WARN_ON(!new_ctx))
1042 return -EINVAL;
1043
1044 if (WARN_ON(!old_ctx))
1045 return -EINVAL;
1046
1047 if (WARN_ON(new_ctx->replace_state ==
1048 IEEE80211_CHANCTX_REPLACES_OTHER))
1049 return -EINVAL;
1050
1051 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1052 &sdata->reserved_chandef);
1053 if (WARN_ON(!chandef))
1054 return -EINVAL;
1055
1056 vif_chsw[0].vif = &sdata->vif;
1057 vif_chsw[0].old_ctx = &old_ctx->conf;
1058 vif_chsw[0].new_ctx = &new_ctx->conf;
1059
1060 list_del(&sdata->reserved_chanctx_list);
1061 sdata->reserved_chanctx = NULL;
1062
1063 err = drv_switch_vif_chanctx(local, vif_chsw, 1,
1064 CHANCTX_SWMODE_REASSIGN_VIF);
1065 if (err) {
1066 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1067 ieee80211_free_chanctx(local, new_ctx);
1068
Michal Kazior03078de2014-06-25 12:35:08 +02001069 goto out;
Luciano Coelho11335a52013-10-30 13:09:39 +02001070 }
1071
Michal Kazior5bcae312014-06-25 12:35:06 +02001072 list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
1073 rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
Luciano Coelho11335a52013-10-30 13:09:39 +02001074
Michal Kazior5bcae312014-06-25 12:35:06 +02001075 if (sdata->vif.type == NL80211_IFTYPE_AP)
1076 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1077
1078 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
1079 ieee80211_free_chanctx(local, old_ctx);
Luciano Coelho11335a52013-10-30 13:09:39 +02001080
1081 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
Michal Kazior5bcae312014-06-25 12:35:06 +02001082 changed = BSS_CHANGED_BANDWIDTH;
Luciano Coelho11335a52013-10-30 13:09:39 +02001083
1084 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
1085
Michal Kazior5bcae312014-06-25 12:35:06 +02001086 if (changed)
1087 ieee80211_bss_info_change_notify(sdata, changed);
Luciano Coelho11335a52013-10-30 13:09:39 +02001088
Michal Kazior03078de2014-06-25 12:35:08 +02001089out:
1090 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001091 return err;
1092}
1093
1094static int
1095ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
1096{
1097 struct ieee80211_local *local = sdata->local;
1098 struct ieee80211_chanctx *old_ctx, *new_ctx;
1099 const struct cfg80211_chan_def *chandef;
1100 int err;
1101
1102 old_ctx = ieee80211_vif_get_chanctx(sdata);
1103 new_ctx = sdata->reserved_chanctx;
1104
1105 if (WARN_ON(!sdata->reserved_ready))
1106 return -EINVAL;
1107
1108 if (WARN_ON(old_ctx))
1109 return -EINVAL;
1110
1111 if (WARN_ON(!new_ctx))
1112 return -EINVAL;
1113
1114 if (WARN_ON(new_ctx->replace_state ==
1115 IEEE80211_CHANCTX_REPLACES_OTHER))
1116 return -EINVAL;
1117
1118 chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
1119 &sdata->reserved_chandef);
1120 if (WARN_ON(!chandef))
1121 return -EINVAL;
1122
1123 list_del(&sdata->reserved_chanctx_list);
1124 sdata->reserved_chanctx = NULL;
1125
1126 err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
1127 if (err) {
1128 if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
1129 ieee80211_free_chanctx(local, new_ctx);
1130
1131 goto out;
1132 }
1133
1134out:
Michal Kazior03078de2014-06-25 12:35:08 +02001135 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001136 return err;
1137}
1138
1139static bool
1140ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
1141{
1142 struct ieee80211_chanctx *old_ctx, *new_ctx;
1143
1144 lockdep_assert_held(&sdata->local->chanctx_mtx);
1145
1146 new_ctx = sdata->reserved_chanctx;
1147 old_ctx = ieee80211_vif_get_chanctx(sdata);
1148
1149 if (!old_ctx)
1150 return false;
1151
1152 if (WARN_ON(!new_ctx))
1153 return false;
1154
1155 if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1156 return false;
1157
1158 if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1159 return false;
1160
1161 return true;
1162}
1163
1164static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
1165 struct ieee80211_chanctx *new_ctx)
1166{
1167 const struct cfg80211_chan_def *chandef;
1168
1169 lockdep_assert_held(&local->mtx);
1170 lockdep_assert_held(&local->chanctx_mtx);
1171
1172 chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
1173 if (WARN_ON(!chandef))
1174 return -EINVAL;
1175
1176 local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
1177 local->_oper_chandef = *chandef;
1178 ieee80211_hw_config(local, 0);
1179
1180 return 0;
1181}
1182
1183static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
1184 int n_vifs)
1185{
1186 struct ieee80211_vif_chanctx_switch *vif_chsw;
1187 struct ieee80211_sub_if_data *sdata;
1188 struct ieee80211_chanctx *ctx, *old_ctx;
1189 int i, err;
1190
1191 lockdep_assert_held(&local->mtx);
1192 lockdep_assert_held(&local->chanctx_mtx);
1193
1194 vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
1195 if (!vif_chsw)
1196 return -ENOMEM;
1197
1198 i = 0;
1199 list_for_each_entry(ctx, &local->chanctx_list, list) {
1200 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1201 continue;
1202
1203 if (WARN_ON(!ctx->replace_ctx)) {
1204 err = -EINVAL;
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001205 goto out;
1206 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001207
Michal Kazior5bcae312014-06-25 12:35:06 +02001208 list_for_each_entry(sdata, &ctx->reserved_vifs,
1209 reserved_chanctx_list) {
1210 if (!ieee80211_vif_has_in_place_reservation(
1211 sdata))
1212 continue;
1213
1214 old_ctx = ieee80211_vif_get_chanctx(sdata);
1215 vif_chsw[i].vif = &sdata->vif;
1216 vif_chsw[i].old_ctx = &old_ctx->conf;
1217 vif_chsw[i].new_ctx = &ctx->conf;
1218
1219 i++;
1220 }
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001221 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001222
Michal Kazior5bcae312014-06-25 12:35:06 +02001223 err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
1224 CHANCTX_SWMODE_SWAP_CONTEXTS);
Luciano Coelho11335a52013-10-30 13:09:39 +02001225
Luciano Coelho11335a52013-10-30 13:09:39 +02001226out:
Michal Kazior5bcae312014-06-25 12:35:06 +02001227 kfree(vif_chsw);
1228 return err;
1229}
1230
1231static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
1232{
1233 struct ieee80211_chanctx *ctx;
1234 int err;
1235
1236 lockdep_assert_held(&local->mtx);
1237 lockdep_assert_held(&local->chanctx_mtx);
1238
1239 list_for_each_entry(ctx, &local->chanctx_list, list) {
1240 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1241 continue;
1242
1243 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1244 continue;
1245
1246 ieee80211_del_chanctx(local, ctx->replace_ctx);
1247 err = ieee80211_add_chanctx(local, ctx);
1248 if (err)
1249 goto err;
1250 }
1251
1252 return 0;
1253
1254err:
1255 WARN_ON(ieee80211_add_chanctx(local, ctx));
1256 list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
1257 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1258 continue;
1259
1260 if (!list_empty(&ctx->replace_ctx->assigned_vifs))
1261 continue;
1262
1263 ieee80211_del_chanctx(local, ctx);
1264 WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
1265 }
1266
1267 return err;
1268}
1269
1270int
1271ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
1272{
1273 struct ieee80211_sub_if_data *sdata, *sdata_tmp;
1274 struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
1275 struct ieee80211_chanctx *new_ctx = NULL;
1276 int i, err, n_assigned, n_reserved, n_ready;
1277 int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
1278
1279 lockdep_assert_held(&local->mtx);
1280 lockdep_assert_held(&local->chanctx_mtx);
1281
1282 /*
1283 * If there are 2 independent pairs of channel contexts performing
1284 * cross-switch of their vifs this code will still wait until both are
1285 * ready even though it could be possible to switch one before the
1286 * other is ready.
1287 *
1288 * For practical reasons and code simplicity just do a single huge
1289 * switch.
1290 */
1291
1292 /*
1293 * Verify if the reservation is still feasible.
1294 * - if it's not then disconnect
1295 * - if it is but not all vifs necessary are ready then defer
1296 */
1297
1298 list_for_each_entry(ctx, &local->chanctx_list, list) {
1299 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1300 continue;
1301
1302 if (WARN_ON(!ctx->replace_ctx)) {
1303 err = -EINVAL;
1304 goto err;
1305 }
1306
1307 if (!local->use_chanctx)
1308 new_ctx = ctx;
1309
1310 n_ctx++;
1311
1312 n_assigned = 0;
1313 n_reserved = 0;
1314 n_ready = 0;
1315
1316 list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
1317 assigned_chanctx_list) {
1318 n_assigned++;
1319 if (sdata->reserved_chanctx) {
1320 n_reserved++;
1321 if (sdata->reserved_ready)
1322 n_ready++;
1323 }
1324 }
1325
1326 if (n_assigned != n_reserved) {
1327 if (n_ready == n_reserved) {
1328 wiphy_info(local->hw.wiphy,
1329 "channel context reservation cannot be finalized because some interfaces aren't switching\n");
1330 err = -EBUSY;
1331 goto err;
1332 }
1333
1334 return -EAGAIN;
1335 }
1336
1337 ctx->conf.radar_enabled = false;
1338 list_for_each_entry(sdata, &ctx->reserved_vifs,
1339 reserved_chanctx_list) {
1340 if (ieee80211_vif_has_in_place_reservation(sdata) &&
1341 !sdata->reserved_ready)
1342 return -EAGAIN;
1343
1344 old_ctx = ieee80211_vif_get_chanctx(sdata);
1345 if (old_ctx) {
1346 if (old_ctx->replace_state ==
1347 IEEE80211_CHANCTX_WILL_BE_REPLACED)
1348 n_vifs_switch++;
1349 else
1350 n_vifs_assign++;
1351 } else {
1352 n_vifs_ctxless++;
1353 }
1354
1355 if (sdata->reserved_radar_required)
1356 ctx->conf.radar_enabled = true;
1357 }
1358 }
1359
1360 if (WARN_ON(n_ctx == 0) ||
1361 WARN_ON(n_vifs_switch == 0 &&
1362 n_vifs_assign == 0 &&
1363 n_vifs_ctxless == 0) ||
1364 WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
1365 WARN_ON(!new_ctx && !local->use_chanctx)) {
1366 err = -EINVAL;
1367 goto err;
1368 }
1369
1370 /*
1371 * All necessary vifs are ready. Perform the switch now depending on
1372 * reservations and driver capabilities.
1373 */
1374
1375 if (local->use_chanctx) {
1376 if (n_vifs_switch > 0) {
1377 err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
1378 if (err)
1379 goto err;
1380 }
1381
1382 if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
1383 err = ieee80211_chsw_switch_ctxs(local);
1384 if (err)
1385 goto err;
1386 }
1387 } else {
1388 err = ieee80211_chsw_switch_hwconf(local, new_ctx);
1389 if (err)
1390 goto err;
1391 }
1392
1393 /*
1394 * Update all structures, values and pointers to point to new channel
1395 * context(s).
1396 */
1397
1398 i = 0;
1399 list_for_each_entry(ctx, &local->chanctx_list, list) {
1400 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1401 continue;
1402
1403 if (WARN_ON(!ctx->replace_ctx)) {
1404 err = -EINVAL;
1405 goto err;
1406 }
1407
1408 list_for_each_entry(sdata, &ctx->reserved_vifs,
1409 reserved_chanctx_list) {
1410 u32 changed = 0;
1411
1412 if (!ieee80211_vif_has_in_place_reservation(sdata))
1413 continue;
1414
1415 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
1416
1417 if (sdata->vif.type == NL80211_IFTYPE_AP)
1418 __ieee80211_vif_copy_chanctx_to_vlans(sdata,
1419 false);
1420
1421 sdata->radar_required = sdata->reserved_radar_required;
1422
1423 if (sdata->vif.bss_conf.chandef.width !=
1424 sdata->reserved_chandef.width)
1425 changed = BSS_CHANGED_BANDWIDTH;
1426
1427 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
1428 if (changed)
1429 ieee80211_bss_info_change_notify(sdata,
1430 changed);
1431
1432 ieee80211_recalc_txpower(sdata);
1433 }
1434
1435 ieee80211_recalc_chanctx_chantype(local, ctx);
1436 ieee80211_recalc_smps_chanctx(local, ctx);
1437 ieee80211_recalc_radar_chanctx(local, ctx);
1438 ieee80211_recalc_chanctx_min_def(local, ctx);
1439
1440 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1441 reserved_chanctx_list) {
1442 if (ieee80211_vif_get_chanctx(sdata) != ctx)
1443 continue;
1444
1445 list_del(&sdata->reserved_chanctx_list);
1446 list_move(&sdata->assigned_chanctx_list,
Michal Kazior47e4df92014-08-18 13:19:09 +02001447 &ctx->assigned_vifs);
Michal Kazior5bcae312014-06-25 12:35:06 +02001448 sdata->reserved_chanctx = NULL;
Michal Kazior03078de2014-06-25 12:35:08 +02001449
1450 ieee80211_vif_chanctx_reservation_complete(sdata);
Michal Kazior5bcae312014-06-25 12:35:06 +02001451 }
1452
1453 /*
1454 * This context might have been a dependency for an already
1455 * ready re-assign reservation interface that was deferred. Do
1456 * not propagate error to the caller though. The in-place
1457 * reservation for originally requested interface has already
1458 * succeeded at this point.
1459 */
1460 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
1461 reserved_chanctx_list) {
1462 if (WARN_ON(ieee80211_vif_has_in_place_reservation(
1463 sdata)))
1464 continue;
1465
1466 if (WARN_ON(sdata->reserved_chanctx != ctx))
1467 continue;
1468
1469 if (!sdata->reserved_ready)
1470 continue;
1471
1472 if (ieee80211_vif_get_chanctx(sdata))
1473 err = ieee80211_vif_use_reserved_reassign(
1474 sdata);
1475 else
1476 err = ieee80211_vif_use_reserved_assign(sdata);
1477
1478 if (err) {
1479 sdata_info(sdata,
1480 "failed to finalize (re-)assign reservation (err=%d)\n",
1481 err);
1482 ieee80211_vif_unreserve_chanctx(sdata);
1483 cfg80211_stop_iface(local->hw.wiphy,
1484 &sdata->wdev,
1485 GFP_KERNEL);
1486 }
1487 }
1488 }
1489
1490 /*
1491 * Finally free old contexts
1492 */
1493
1494 list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
1495 if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
1496 continue;
1497
1498 ctx->replace_ctx->replace_ctx = NULL;
1499 ctx->replace_ctx->replace_state =
1500 IEEE80211_CHANCTX_REPLACE_NONE;
1501
1502 list_del_rcu(&ctx->list);
1503 kfree_rcu(ctx, rcu_head);
1504 }
1505
1506 return 0;
1507
1508err:
1509 list_for_each_entry(ctx, &local->chanctx_list, list) {
1510 if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
1511 continue;
1512
1513 list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
Michal Kazior03078de2014-06-25 12:35:08 +02001514 reserved_chanctx_list) {
Michal Kazior5bcae312014-06-25 12:35:06 +02001515 ieee80211_vif_unreserve_chanctx(sdata);
Michal Kazior03078de2014-06-25 12:35:08 +02001516 ieee80211_vif_chanctx_reservation_complete(sdata);
1517 }
Michal Kazior5bcae312014-06-25 12:35:06 +02001518 }
1519
1520 return err;
1521}
1522
1523int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
1524{
1525 struct ieee80211_local *local = sdata->local;
1526 struct ieee80211_chanctx *new_ctx;
1527 struct ieee80211_chanctx *old_ctx;
1528 int err;
1529
1530 lockdep_assert_held(&local->mtx);
1531 lockdep_assert_held(&local->chanctx_mtx);
1532
1533 new_ctx = sdata->reserved_chanctx;
1534 old_ctx = ieee80211_vif_get_chanctx(sdata);
1535
1536 if (WARN_ON(!new_ctx))
1537 return -EINVAL;
1538
1539 if (WARN_ON(new_ctx->replace_state ==
1540 IEEE80211_CHANCTX_WILL_BE_REPLACED))
1541 return -EINVAL;
1542
1543 if (WARN_ON(sdata->reserved_ready))
1544 return -EINVAL;
1545
1546 sdata->reserved_ready = true;
1547
1548 if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
1549 if (old_ctx)
1550 err = ieee80211_vif_use_reserved_reassign(sdata);
1551 else
1552 err = ieee80211_vif_use_reserved_assign(sdata);
1553
1554 if (err)
1555 return err;
1556 }
1557
1558 /*
1559 * In-place reservation may need to be finalized now either if:
1560 * a) sdata is taking part in the swapping itself and is the last one
1561 * b) sdata has switched with a re-assign reservation to an existing
1562 * context readying in-place switching of old_ctx
1563 *
1564 * In case of (b) do not propagate the error up because the requested
1565 * sdata already switched successfully. Just spill an extra warning.
1566 * The ieee80211_vif_use_reserved_switch() already stops all necessary
1567 * interfaces upon failure.
1568 */
1569 if ((old_ctx &&
1570 old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
1571 new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
1572 err = ieee80211_vif_use_reserved_switch(local);
1573 if (err && err != -EAGAIN) {
1574 if (new_ctx->replace_state ==
1575 IEEE80211_CHANCTX_REPLACES_OTHER)
1576 return err;
1577
1578 wiphy_info(local->hw.wiphy,
1579 "depending in-place reservation failed (err=%d)\n",
1580 err);
1581 }
1582 }
1583
1584 return 0;
Luciano Coelho11335a52013-10-30 13:09:39 +02001585}
1586
Johannes Berg2c9b7352013-02-07 21:37:29 +01001587int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1588 const struct cfg80211_chan_def *chandef,
1589 u32 *changed)
1590{
1591 struct ieee80211_local *local = sdata->local;
1592 struct ieee80211_chanctx_conf *conf;
1593 struct ieee80211_chanctx *ctx;
Michal Kazior5bcae312014-06-25 12:35:06 +02001594 const struct cfg80211_chan_def *compat;
Johannes Berg2c9b7352013-02-07 21:37:29 +01001595 int ret;
1596
1597 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1598 IEEE80211_CHAN_DISABLED))
1599 return -EINVAL;
1600
1601 mutex_lock(&local->chanctx_mtx);
1602 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1603 ret = 0;
1604 goto out;
1605 }
1606
1607 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1608 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1609 ret = -EINVAL;
1610 goto out;
1611 }
1612
1613 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1614 lockdep_is_held(&local->chanctx_mtx));
1615 if (!conf) {
1616 ret = -EINVAL;
1617 goto out;
1618 }
1619
1620 ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior5bcae312014-06-25 12:35:06 +02001621
1622 compat = cfg80211_chandef_compatible(&conf->def, chandef);
1623 if (!compat) {
Johannes Berg2c9b7352013-02-07 21:37:29 +01001624 ret = -EINVAL;
1625 goto out;
1626 }
1627
Michal Kazior5bcae312014-06-25 12:35:06 +02001628 switch (ctx->replace_state) {
1629 case IEEE80211_CHANCTX_REPLACE_NONE:
1630 if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
1631 ret = -EBUSY;
1632 goto out;
1633 }
1634 break;
1635 case IEEE80211_CHANCTX_WILL_BE_REPLACED:
1636 /* TODO: Perhaps the bandwith change could be treated as a
1637 * reservation itself? */
1638 ret = -EBUSY;
1639 goto out;
1640 case IEEE80211_CHANCTX_REPLACES_OTHER:
1641 /* channel context that is going to replace another channel
1642 * context doesn't really exist and shouldn't be assigned
1643 * anywhere yet */
1644 WARN_ON(1);
1645 break;
1646 }
1647
Johannes Berg2c9b7352013-02-07 21:37:29 +01001648 sdata->vif.bss_conf.chandef = *chandef;
1649
1650 ieee80211_recalc_chanctx_chantype(local, ctx);
1651
1652 *changed |= BSS_CHANGED_BANDWIDTH;
1653 ret = 0;
1654 out:
1655 mutex_unlock(&local->chanctx_mtx);
1656 return ret;
1657}
1658
Michal Kaziord01a1e62012-06-26 14:37:16 +02001659void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1660{
Johannes Berg55de9082012-07-26 17:24:39 +02001661 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1662
Johannes Berg34a37402013-12-18 09:43:33 +01001663 lockdep_assert_held(&sdata->local->mtx);
1664
Michal Kaziord01a1e62012-06-26 14:37:16 +02001665 mutex_lock(&sdata->local->chanctx_mtx);
1666 __ieee80211_vif_release_channel(sdata);
1667 mutex_unlock(&sdata->local->chanctx_mtx);
1668}
Johannes Berg3448c002012-09-11 17:57:42 +02001669
Johannes Berg4d76d212012-12-11 20:38:41 +01001670void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1671{
1672 struct ieee80211_local *local = sdata->local;
1673 struct ieee80211_sub_if_data *ap;
1674 struct ieee80211_chanctx_conf *conf;
1675
1676 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1677 return;
1678
1679 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1680
1681 mutex_lock(&local->chanctx_mtx);
1682
1683 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1684 lockdep_is_held(&local->chanctx_mtx));
1685 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1686 mutex_unlock(&local->chanctx_mtx);
1687}
1688
Johannes Berg3448c002012-09-11 17:57:42 +02001689void ieee80211_iter_chan_contexts_atomic(
1690 struct ieee80211_hw *hw,
1691 void (*iter)(struct ieee80211_hw *hw,
1692 struct ieee80211_chanctx_conf *chanctx_conf,
1693 void *data),
1694 void *iter_data)
1695{
1696 struct ieee80211_local *local = hw_to_local(hw);
1697 struct ieee80211_chanctx *ctx;
1698
1699 rcu_read_lock();
1700 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +01001701 if (ctx->driver_present)
1702 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +02001703 rcu_read_unlock();
1704}
1705EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);