blob: d8b1b861484259a426e91ad12d5dbe0ac17f784b [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 Kazior02881572014-04-09 15:29:28 +020066static const struct cfg80211_chan_def *
67ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
68 struct ieee80211_chanctx *ctx,
69 const struct cfg80211_chan_def *compat)
70{
71 struct ieee80211_sub_if_data *sdata;
72
73 lockdep_assert_held(&local->chanctx_mtx);
74
75 list_for_each_entry(sdata, &ctx->reserved_vifs,
76 reserved_chanctx_list) {
77 if (!compat)
78 compat = &sdata->reserved_chandef;
79
80 compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
81 compat);
82 if (!compat)
83 break;
84 }
85
86 return compat;
87}
88
Michal Kazior13f348a2014-04-09 15:29:29 +020089static const struct cfg80211_chan_def *
90ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
91 struct ieee80211_chanctx *ctx,
92 const struct cfg80211_chan_def *compat)
93{
94 struct ieee80211_sub_if_data *sdata;
95
96 lockdep_assert_held(&local->chanctx_mtx);
97
98 list_for_each_entry(sdata, &ctx->assigned_vifs,
99 assigned_chanctx_list) {
100 if (sdata->reserved_chanctx != NULL)
101 continue;
102
103 if (!compat)
104 compat = &sdata->vif.bss_conf.chandef;
105
106 compat = cfg80211_chandef_compatible(
107 &sdata->vif.bss_conf.chandef, compat);
108 if (!compat)
109 break;
110 }
111
112 return compat;
113}
114
115static const struct cfg80211_chan_def *
116ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
117 struct ieee80211_chanctx *ctx,
118 const struct cfg80211_chan_def *compat)
119{
120 lockdep_assert_held(&local->chanctx_mtx);
121
122 compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
123 if (!compat)
124 return NULL;
125
126 compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
127 if (!compat)
128 return NULL;
129
130 return compat;
131}
132
133static bool
134ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
135 struct ieee80211_chanctx *ctx,
136 const struct cfg80211_chan_def *def)
137{
138 lockdep_assert_held(&local->chanctx_mtx);
139
140 if (ieee80211_chanctx_combined_chandef(local, ctx, def))
141 return true;
142
143 if (!list_empty(&ctx->reserved_vifs) &&
144 ieee80211_chanctx_reserved_chandef(local, ctx, def))
145 return true;
146
147 return false;
148}
149
150static struct ieee80211_chanctx *
151ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
152 const struct cfg80211_chan_def *chandef,
153 enum ieee80211_chanctx_mode mode)
154{
155 struct ieee80211_chanctx *ctx;
156
157 lockdep_assert_held(&local->chanctx_mtx);
158
159 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
160 return NULL;
161
162 list_for_each_entry(ctx, &local->chanctx_list, list) {
163 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
164 continue;
165
166 if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
167 chandef))
168 continue;
169
170 return ctx;
171 }
172
173 return NULL;
174}
175
Eliad Peller21f659b2013-11-11 20:14:01 +0200176static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
177{
178 switch (sta->bandwidth) {
179 case IEEE80211_STA_RX_BW_20:
180 if (sta->ht_cap.ht_supported)
181 return NL80211_CHAN_WIDTH_20;
182 else
183 return NL80211_CHAN_WIDTH_20_NOHT;
184 case IEEE80211_STA_RX_BW_40:
185 return NL80211_CHAN_WIDTH_40;
186 case IEEE80211_STA_RX_BW_80:
187 return NL80211_CHAN_WIDTH_80;
188 case IEEE80211_STA_RX_BW_160:
189 /*
190 * This applied for both 160 and 80+80. since we use
191 * the returned value to consider degradation of
192 * ctx->conf.min_def, we have to make sure to take
193 * the bigger one (NL80211_CHAN_WIDTH_160).
194 * Otherwise we might try degrading even when not
195 * needed, as the max required sta_bw returned (80+80)
196 * might be smaller than the configured bw (160).
197 */
198 return NL80211_CHAN_WIDTH_160;
199 default:
200 WARN_ON(1);
201 return NL80211_CHAN_WIDTH_20;
202 }
203}
204
205static enum nl80211_chan_width
206ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
207{
208 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
209 struct sta_info *sta;
210
211 rcu_read_lock();
212 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
213 if (sdata != sta->sdata &&
214 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
215 continue;
216
217 if (!sta->uploaded)
218 continue;
219
220 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
221 }
222 rcu_read_unlock();
223
224 return max_bw;
225}
226
227static enum nl80211_chan_width
228ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
229 struct ieee80211_chanctx_conf *conf)
230{
231 struct ieee80211_sub_if_data *sdata;
232 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
233
234 rcu_read_lock();
235 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
236 struct ieee80211_vif *vif = &sdata->vif;
237 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
238
239 if (!ieee80211_sdata_running(sdata))
240 continue;
241
242 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
243 continue;
244
245 switch (vif->type) {
246 case NL80211_IFTYPE_AP:
247 case NL80211_IFTYPE_AP_VLAN:
248 width = ieee80211_get_max_required_bw(sdata);
249 break;
250 case NL80211_IFTYPE_P2P_DEVICE:
251 continue;
252 case NL80211_IFTYPE_STATION:
253 case NL80211_IFTYPE_ADHOC:
254 case NL80211_IFTYPE_WDS:
255 case NL80211_IFTYPE_MESH_POINT:
256 width = vif->bss_conf.chandef.width;
257 break;
258 case NL80211_IFTYPE_UNSPECIFIED:
259 case NUM_NL80211_IFTYPES:
260 case NL80211_IFTYPE_MONITOR:
261 case NL80211_IFTYPE_P2P_CLIENT:
262 case NL80211_IFTYPE_P2P_GO:
263 WARN_ON_ONCE(1);
264 }
265 max_bw = max(max_bw, width);
266 }
Eliad Peller1c37a722014-03-03 13:37:14 +0200267
268 /* use the configured bandwidth in case of monitor interface */
269 sdata = rcu_dereference(local->monitor_sdata);
270 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
271 max_bw = max(max_bw, conf->def.width);
272
Eliad Peller21f659b2013-11-11 20:14:01 +0200273 rcu_read_unlock();
274
275 return max_bw;
276}
277
278/*
279 * recalc the min required chan width of the channel context, which is
280 * the max of min required widths of all the interfaces bound to this
281 * channel context.
282 */
283void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
284 struct ieee80211_chanctx *ctx)
285{
286 enum nl80211_chan_width max_bw;
287 struct cfg80211_chan_def min_def;
288
289 lockdep_assert_held(&local->chanctx_mtx);
290
291 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
292 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
293 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
294 ctx->conf.radar_enabled) {
295 ctx->conf.min_def = ctx->conf.def;
296 return;
297 }
298
299 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
300
301 /* downgrade chandef up to max_bw */
302 min_def = ctx->conf.def;
303 while (min_def.width > max_bw)
304 ieee80211_chandef_downgrade(&min_def);
305
306 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
307 return;
308
309 ctx->conf.min_def = min_def;
310 if (!ctx->driver_present)
311 return;
312
313 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
314}
315
Johannes Berg18942d32013-02-07 21:30:37 +0100316static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100317 struct ieee80211_chanctx *ctx,
318 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200319{
Johannes Berg4bf88532012-11-09 11:39:59 +0100320 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +0200321 return;
322
Johannes Berg4bf88532012-11-09 11:39:59 +0100323 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
324
325 ctx->conf.def = *chandef;
326 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Eliad Peller21f659b2013-11-11 20:14:01 +0200327 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200328
329 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100330 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200331 ieee80211_hw_config(local, 0);
332 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200333}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200334
335static struct ieee80211_chanctx *
336ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100337 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200338 enum ieee80211_chanctx_mode mode)
339{
340 struct ieee80211_chanctx *ctx;
341
342 lockdep_assert_held(&local->chanctx_mtx);
343
344 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
345 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200346
347 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100348 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200349
Michal Kazior02881572014-04-09 15:29:28 +0200350 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200351 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100352
353 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
354 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200355 continue;
356
Michal Kazior02881572014-04-09 15:29:28 +0200357 compat = ieee80211_chanctx_reserved_chandef(local, ctx,
358 compat);
359 if (!compat)
360 continue;
361
Johannes Berg18942d32013-02-07 21:30:37 +0100362 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200363
Michal Kaziord01a1e62012-06-26 14:37:16 +0200364 return ctx;
365 }
366
367 return NULL;
368}
369
Simon Wunderliche4746852013-04-08 22:43:16 +0200370static bool ieee80211_is_radar_required(struct ieee80211_local *local)
371{
372 struct ieee80211_sub_if_data *sdata;
373
Michal Kaziorcc901de2014-01-29 07:56:20 +0100374 lockdep_assert_held(&local->mtx);
375
Simon Wunderliche4746852013-04-08 22:43:16 +0200376 rcu_read_lock();
377 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
378 if (sdata->radar_required) {
379 rcu_read_unlock();
380 return true;
381 }
382 }
383 rcu_read_unlock();
384
385 return false;
386}
387
Michal Kaziord01a1e62012-06-26 14:37:16 +0200388static struct ieee80211_chanctx *
Michal Kaziored68ebc2014-04-09 15:29:30 +0200389ieee80211_alloc_chanctx(struct ieee80211_local *local,
390 const struct cfg80211_chan_def *chandef,
391 enum ieee80211_chanctx_mode mode)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200392{
393 struct ieee80211_chanctx *ctx;
394
395 lockdep_assert_held(&local->chanctx_mtx);
396
397 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
398 if (!ctx)
Michal Kaziored68ebc2014-04-09 15:29:30 +0200399 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200400
Michal Kazior484298a2014-04-09 15:29:26 +0200401 INIT_LIST_HEAD(&ctx->assigned_vifs);
Michal Kaziore3afb922014-04-09 15:29:27 +0200402 INIT_LIST_HEAD(&ctx->reserved_vifs);
Johannes Berg4bf88532012-11-09 11:39:59 +0100403 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200404 ctx->conf.rx_chains_static = 1;
405 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200406 ctx->mode = mode;
Simon Wunderliche4746852013-04-08 22:43:16 +0200407 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
Eliad Peller21f659b2013-11-11 20:14:01 +0200408 ieee80211_recalc_chanctx_min_def(local, ctx);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200409
410 return ctx;
411}
412
413static int ieee80211_add_chanctx(struct ieee80211_local *local,
414 struct ieee80211_chanctx *ctx)
415{
416 u32 changed;
417 int err;
418
419 lockdep_assert_held(&local->mtx);
420 lockdep_assert_held(&local->chanctx_mtx);
421
Simon Wunderliche4746852013-04-08 22:43:16 +0200422 if (!local->use_chanctx)
423 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200424
Johannes Berg382a1032013-03-22 22:30:09 +0100425 /* turn idle off *before* setting channel -- some drivers need that */
426 changed = ieee80211_idle_off(local);
427 if (changed)
428 ieee80211_hw_config(local, changed);
429
Johannes Berg55de9082012-07-26 17:24:39 +0200430 if (!local->use_chanctx) {
Michal Kaziored68ebc2014-04-09 15:29:30 +0200431 local->_oper_chandef = ctx->conf.def;
Johannes Berg55de9082012-07-26 17:24:39 +0200432 ieee80211_hw_config(local, 0);
433 } else {
434 err = drv_add_chanctx(local, ctx);
435 if (err) {
Johannes Berg382a1032013-03-22 22:30:09 +0100436 ieee80211_recalc_idle(local);
Michal Kaziored68ebc2014-04-09 15:29:30 +0200437 return err;
Johannes Berg55de9082012-07-26 17:24:39 +0200438 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200439 }
440
Michal Kaziored68ebc2014-04-09 15:29:30 +0200441 return 0;
442}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200443
Michal Kaziored68ebc2014-04-09 15:29:30 +0200444static struct ieee80211_chanctx *
445ieee80211_new_chanctx(struct ieee80211_local *local,
446 const struct cfg80211_chan_def *chandef,
447 enum ieee80211_chanctx_mode mode)
448{
449 struct ieee80211_chanctx *ctx;
450 int err;
451
452 lockdep_assert_held(&local->mtx);
453 lockdep_assert_held(&local->chanctx_mtx);
454
455 ctx = ieee80211_alloc_chanctx(local, chandef, mode);
456 if (!ctx)
457 return ERR_PTR(-ENOMEM);
458
459 err = ieee80211_add_chanctx(local, ctx);
460 if (err) {
461 kfree(ctx);
462 return ERR_PTR(err);
463 }
464
465 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200466 return ctx;
467}
468
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200469static void ieee80211_del_chanctx(struct ieee80211_local *local,
470 struct ieee80211_chanctx *ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200471{
472 lockdep_assert_held(&local->chanctx_mtx);
473
Johannes Berg55de9082012-07-26 17:24:39 +0200474 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100475 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
476 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
477 chandef->center_freq1 = chandef->chan->center_freq;
478 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200479
480 /* NOTE: Disabling radar is only valid here for
481 * single channel context. To be sure, check it ...
482 */
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200483 WARN_ON(local->hw.conf.radar_enabled &&
484 !list_empty(&local->chanctx_list));
485
Simon Wunderliche4746852013-04-08 22:43:16 +0200486 local->hw.conf.radar_enabled = false;
487
Johannes Berg55de9082012-07-26 17:24:39 +0200488 ieee80211_hw_config(local, 0);
489 } else {
490 drv_remove_chanctx(local, ctx);
491 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200492
Johannes Bergfd0f9792013-02-07 00:14:51 +0100493 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200494}
495
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200496static void ieee80211_free_chanctx(struct ieee80211_local *local,
497 struct ieee80211_chanctx *ctx)
498{
499 lockdep_assert_held(&local->chanctx_mtx);
500
Michal Kaziorc0166da2014-04-09 15:29:33 +0200501 WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
Michal Kazior1f0d54c2014-04-09 15:29:31 +0200502
503 list_del_rcu(&ctx->list);
504 ieee80211_del_chanctx(local, ctx);
505 kfree_rcu(ctx, rcu_head);
506}
507
Johannes Berg4bf88532012-11-09 11:39:59 +0100508static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
509 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200510{
511 struct ieee80211_chanctx_conf *conf = &ctx->conf;
512 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100513 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200514
515 lockdep_assert_held(&local->chanctx_mtx);
516
517 rcu_read_lock();
518 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100519
Michal Kaziore89a96f2012-06-26 14:37:22 +0200520 if (!ieee80211_sdata_running(sdata))
521 continue;
522 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
523 continue;
524
Johannes Berg4bf88532012-11-09 11:39:59 +0100525 if (!compat)
526 compat = &sdata->vif.bss_conf.chandef;
527
528 compat = cfg80211_chandef_compatible(
529 &sdata->vif.bss_conf.chandef, compat);
530 if (!compat)
531 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200532 }
533 rcu_read_unlock();
534
Johannes Berg4bf88532012-11-09 11:39:59 +0100535 if (WARN_ON_ONCE(!compat))
536 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200537
Johannes Berg18942d32013-02-07 21:30:37 +0100538 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200539}
540
Johannes Berg367bbd12013-12-18 09:36:09 +0100541static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
542 struct ieee80211_chanctx *chanctx)
543{
544 bool radar_enabled;
545
546 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100547 /* for setting local->radar_detect_enabled */
548 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100549
550 radar_enabled = ieee80211_is_radar_required(local);
551
552 if (radar_enabled == chanctx->conf.radar_enabled)
553 return;
554
555 chanctx->conf.radar_enabled = radar_enabled;
556 local->radar_detect_enabled = chanctx->conf.radar_enabled;
557
558 if (!local->use_chanctx) {
559 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
560 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
561 }
562
563 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
564}
565
Luciano Coelho77eeba92014-03-11 18:24:12 +0200566static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
567 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200568{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200569 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200570 struct ieee80211_chanctx_conf *conf;
571 struct ieee80211_chanctx *curr_ctx = NULL;
572 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200573
Luciano Coelho77eeba92014-03-11 18:24:12 +0200574 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
575 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200576
Luciano Coelho77eeba92014-03-11 18:24:12 +0200577 if (conf) {
578 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200579
Luciano Coelho77eeba92014-03-11 18:24:12 +0200580 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
581 conf = NULL;
Michal Kazior484298a2014-04-09 15:29:26 +0200582 list_del(&sdata->assigned_chanctx_list);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200583 }
584
585 if (new_ctx) {
586 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
587 if (ret)
588 goto out;
589
Luciano Coelho77eeba92014-03-11 18:24:12 +0200590 conf = &new_ctx->conf;
Michal Kazior484298a2014-04-09 15:29:26 +0200591 list_add(&sdata->assigned_chanctx_list,
592 &new_ctx->assigned_vifs);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200593 }
594
595out:
596 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
597
598 sdata->vif.bss_conf.idle = !conf;
599
Michal Kaziorc0166da2014-04-09 15:29:33 +0200600 if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200601 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
602 ieee80211_recalc_smps_chanctx(local, curr_ctx);
603 ieee80211_recalc_radar_chanctx(local, curr_ctx);
604 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
605 }
606
Michal Kaziorc0166da2014-04-09 15:29:33 +0200607 if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
Luciano Coelho77eeba92014-03-11 18:24:12 +0200608 ieee80211_recalc_txpower(sdata);
609 ieee80211_recalc_chanctx_min_def(local, new_ctx);
610 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100611
612 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
613 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200614 ieee80211_bss_info_change_notify(sdata,
615 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100616
Luciano Coelho77eeba92014-03-11 18:24:12 +0200617 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200618}
619
620static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
621{
622 struct ieee80211_local *local = sdata->local;
623 struct ieee80211_chanctx_conf *conf;
624 struct ieee80211_chanctx *ctx;
625
626 lockdep_assert_held(&local->chanctx_mtx);
627
628 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
629 lockdep_is_held(&local->chanctx_mtx));
630 if (!conf)
631 return;
632
633 ctx = container_of(conf, struct ieee80211_chanctx, conf);
634
Luciano Coelho11335a52013-10-30 13:09:39 +0200635 if (sdata->reserved_chanctx)
636 ieee80211_vif_unreserve_chanctx(sdata);
637
Luciano Coelho77eeba92014-03-11 18:24:12 +0200638 ieee80211_assign_vif_chanctx(sdata, NULL);
Michal Kaziorc0166da2014-04-09 15:29:33 +0200639 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200640 ieee80211_free_chanctx(local, ctx);
641}
642
Johannes Berg04ecd252012-09-11 14:34:12 +0200643void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
644 struct ieee80211_chanctx *chanctx)
645{
646 struct ieee80211_sub_if_data *sdata;
647 u8 rx_chains_static, rx_chains_dynamic;
648
649 lockdep_assert_held(&local->chanctx_mtx);
650
651 rx_chains_static = 1;
652 rx_chains_dynamic = 1;
653
654 rcu_read_lock();
655 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
656 u8 needed_static, needed_dynamic;
657
658 if (!ieee80211_sdata_running(sdata))
659 continue;
660
661 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
662 &chanctx->conf)
663 continue;
664
665 switch (sdata->vif.type) {
666 case NL80211_IFTYPE_P2P_DEVICE:
667 continue;
668 case NL80211_IFTYPE_STATION:
669 if (!sdata->u.mgd.associated)
670 continue;
671 break;
672 case NL80211_IFTYPE_AP_VLAN:
673 continue;
674 case NL80211_IFTYPE_AP:
675 case NL80211_IFTYPE_ADHOC:
676 case NL80211_IFTYPE_WDS:
677 case NL80211_IFTYPE_MESH_POINT:
678 break;
679 default:
680 WARN_ON_ONCE(1);
681 }
682
683 switch (sdata->smps_mode) {
684 default:
685 WARN_ONCE(1, "Invalid SMPS mode %d\n",
686 sdata->smps_mode);
687 /* fall through */
688 case IEEE80211_SMPS_OFF:
689 needed_static = sdata->needed_rx_chains;
690 needed_dynamic = sdata->needed_rx_chains;
691 break;
692 case IEEE80211_SMPS_DYNAMIC:
693 needed_static = 1;
694 needed_dynamic = sdata->needed_rx_chains;
695 break;
696 case IEEE80211_SMPS_STATIC:
697 needed_static = 1;
698 needed_dynamic = 1;
699 break;
700 }
701
702 rx_chains_static = max(rx_chains_static, needed_static);
703 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
704 }
705 rcu_read_unlock();
706
707 if (!local->use_chanctx) {
708 if (rx_chains_static > 1)
709 local->smps_mode = IEEE80211_SMPS_OFF;
710 else if (rx_chains_dynamic > 1)
711 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
712 else
713 local->smps_mode = IEEE80211_SMPS_STATIC;
714 ieee80211_hw_config(local, 0);
715 }
716
717 if (rx_chains_static == chanctx->conf.rx_chains_static &&
718 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
719 return;
720
721 chanctx->conf.rx_chains_static = rx_chains_static;
722 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
723 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
724}
725
Michal Kaziord01a1e62012-06-26 14:37:16 +0200726int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100727 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200728 enum ieee80211_chanctx_mode mode)
729{
730 struct ieee80211_local *local = sdata->local;
731 struct ieee80211_chanctx *ctx;
Luciano Coelho73de86a2014-02-13 11:31:59 +0200732 u8 radar_detect_width = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200733 int ret;
734
Johannes Berg34a37402013-12-18 09:43:33 +0100735 lockdep_assert_held(&local->mtx);
736
Johannes Berg55de9082012-07-26 17:24:39 +0200737 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
738
Michal Kaziord01a1e62012-06-26 14:37:16 +0200739 mutex_lock(&local->chanctx_mtx);
Luciano Coelho73de86a2014-02-13 11:31:59 +0200740
741 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
742 chandef,
743 sdata->wdev.iftype);
744 if (ret < 0)
745 goto out;
746 if (ret > 0)
747 radar_detect_width = BIT(chandef->width);
748
749 sdata->radar_required = ret;
750
751 ret = ieee80211_check_combinations(sdata, chandef, mode,
752 radar_detect_width);
753 if (ret < 0)
754 goto out;
755
Michal Kaziord01a1e62012-06-26 14:37:16 +0200756 __ieee80211_vif_release_channel(sdata);
757
Johannes Berg4bf88532012-11-09 11:39:59 +0100758 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200759 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100760 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200761 if (IS_ERR(ctx)) {
762 ret = PTR_ERR(ctx);
763 goto out;
764 }
765
Johannes Berg4bf88532012-11-09 11:39:59 +0100766 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200767
Michal Kaziord01a1e62012-06-26 14:37:16 +0200768 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
769 if (ret) {
770 /* if assign fails refcount stays the same */
Michal Kaziorc0166da2014-04-09 15:29:33 +0200771 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200772 ieee80211_free_chanctx(local, ctx);
773 goto out;
774 }
775
Johannes Berg04ecd252012-09-11 14:34:12 +0200776 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100777 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200778 out:
779 mutex_unlock(&local->chanctx_mtx);
780 return ret;
781}
782
Luciano Coelho33ffd952014-01-30 22:08:16 +0200783static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
784 struct ieee80211_chanctx *ctx,
785 u32 *changed)
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200786{
787 struct ieee80211_local *local = sdata->local;
Luciano Coelho33787fc2013-11-11 20:34:54 +0200788 const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200789 u32 chanctx_changed = 0;
790
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200791 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
792 IEEE80211_CHAN_DISABLED))
793 return -EINVAL;
794
Michal Kaziorc0166da2014-04-09 15:29:33 +0200795 if (ieee80211_chanctx_refcount(local, ctx) != 1)
Luciano Coelho33ffd952014-01-30 22:08:16 +0200796 return -EINVAL;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200797
798 if (sdata->vif.bss_conf.chandef.width != chandef->width) {
799 chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
800 *changed |= BSS_CHANGED_BANDWIDTH;
801 }
802
803 sdata->vif.bss_conf.chandef = *chandef;
804 ctx->conf.def = *chandef;
805
806 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
807 drv_change_chanctx(local, ctx, chanctx_changed);
808
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200809 ieee80211_recalc_chanctx_chantype(local, ctx);
810 ieee80211_recalc_smps_chanctx(local, ctx);
811 ieee80211_recalc_radar_chanctx(local, ctx);
Eliad Peller21f659b2013-11-11 20:14:01 +0200812 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200813
Luciano Coelho33ffd952014-01-30 22:08:16 +0200814 return 0;
815}
816
817int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
818 u32 *changed)
819{
820 struct ieee80211_local *local = sdata->local;
821 struct ieee80211_chanctx_conf *conf;
822 struct ieee80211_chanctx *ctx;
823 int ret;
824
825 lockdep_assert_held(&local->mtx);
826
827 /* should never be called if not performing a channel switch. */
828 if (WARN_ON(!sdata->vif.csa_active))
829 return -EINVAL;
830
831 mutex_lock(&local->chanctx_mtx);
832 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
833 lockdep_is_held(&local->chanctx_mtx));
834 if (!conf) {
835 ret = -EINVAL;
836 goto out;
837 }
838
839 ctx = container_of(conf, struct ieee80211_chanctx, conf);
840
841 ret = __ieee80211_vif_change_channel(sdata, ctx, changed);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200842 out:
843 mutex_unlock(&local->chanctx_mtx);
844 return ret;
845}
846
Luciano Coelho11335a52013-10-30 13:09:39 +0200847static void
848__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
849 bool clear)
850{
851 struct ieee80211_local *local = sdata->local;
852 struct ieee80211_sub_if_data *vlan;
853 struct ieee80211_chanctx_conf *conf;
854
855 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
856 return;
857
858 lockdep_assert_held(&local->mtx);
859
860 /* Check that conf exists, even when clearing this function
861 * must be called with the AP's channel context still there
862 * as it would otherwise cause VLANs to have an invalid
863 * channel context pointer for a while, possibly pointing
864 * to a channel context that has already been freed.
865 */
866 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
867 lockdep_is_held(&local->chanctx_mtx));
868 WARN_ON(!conf);
869
870 if (clear)
871 conf = NULL;
872
873 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
874 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
875}
876
877void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
878 bool clear)
879{
880 struct ieee80211_local *local = sdata->local;
881
882 mutex_lock(&local->chanctx_mtx);
883
884 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
885
886 mutex_unlock(&local->chanctx_mtx);
887}
888
889int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
890{
Michal Kaziore3afb922014-04-09 15:29:27 +0200891 struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
892
Luciano Coelho11335a52013-10-30 13:09:39 +0200893 lockdep_assert_held(&sdata->local->chanctx_mtx);
894
Michal Kaziore3afb922014-04-09 15:29:27 +0200895 if (WARN_ON(!ctx))
Luciano Coelho11335a52013-10-30 13:09:39 +0200896 return -EINVAL;
897
Michal Kaziore3afb922014-04-09 15:29:27 +0200898 list_del(&sdata->reserved_chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200899 sdata->reserved_chanctx = NULL;
900
Michal Kaziorc0166da2014-04-09 15:29:33 +0200901 if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0)
Michal Kaziore3afb922014-04-09 15:29:27 +0200902 ieee80211_free_chanctx(sdata->local, ctx);
903
Luciano Coelho11335a52013-10-30 13:09:39 +0200904 return 0;
905}
906
907int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
908 const struct cfg80211_chan_def *chandef,
Michal Kazior09332482014-04-09 15:29:25 +0200909 enum ieee80211_chanctx_mode mode,
910 bool radar_required)
Luciano Coelho11335a52013-10-30 13:09:39 +0200911{
912 struct ieee80211_local *local = sdata->local;
913 struct ieee80211_chanctx_conf *conf;
914 struct ieee80211_chanctx *new_ctx, *curr_ctx;
915 int ret = 0;
916
917 mutex_lock(&local->chanctx_mtx);
918
919 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
920 lockdep_is_held(&local->chanctx_mtx));
921 if (!conf) {
922 ret = -EINVAL;
923 goto out;
924 }
925
926 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
927
Michal Kazior13f348a2014-04-09 15:29:29 +0200928 new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
Luciano Coelho11335a52013-10-30 13:09:39 +0200929 if (!new_ctx) {
Michal Kaziorc0166da2014-04-09 15:29:33 +0200930 if (ieee80211_chanctx_refcount(local, curr_ctx) == 1 &&
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200931 (local->hw.flags & IEEE80211_HW_CHANGE_RUNNING_CHANCTX)) {
932 /* if we're the only users of the chanctx and
933 * the driver supports changing a running
934 * context, reserve our current context
935 */
936 new_ctx = curr_ctx;
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200937 } else if (ieee80211_can_create_new_chanctx(local)) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200938 /* create a new context and reserve it */
939 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
940 if (IS_ERR(new_ctx)) {
941 ret = PTR_ERR(new_ctx);
942 goto out;
943 }
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200944 } else {
945 ret = -EBUSY;
946 goto out;
Luciano Coelho11335a52013-10-30 13:09:39 +0200947 }
948 }
949
Michal Kaziore3afb922014-04-09 15:29:27 +0200950 list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
Luciano Coelho11335a52013-10-30 13:09:39 +0200951 sdata->reserved_chanctx = new_ctx;
952 sdata->reserved_chandef = *chandef;
Michal Kazior09332482014-04-09 15:29:25 +0200953 sdata->reserved_radar_required = radar_required;
Luciano Coelho11335a52013-10-30 13:09:39 +0200954out:
955 mutex_unlock(&local->chanctx_mtx);
956 return ret;
957}
958
959int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata,
960 u32 *changed)
961{
962 struct ieee80211_local *local = sdata->local;
963 struct ieee80211_chanctx *ctx;
964 struct ieee80211_chanctx *old_ctx;
965 struct ieee80211_chanctx_conf *conf;
966 int ret;
967 u32 tmp_changed = *changed;
968
969 /* TODO: need to recheck if the chandef is usable etc.? */
970
971 lockdep_assert_held(&local->mtx);
972
973 mutex_lock(&local->chanctx_mtx);
974
975 ctx = sdata->reserved_chanctx;
976 if (WARN_ON(!ctx)) {
977 ret = -EINVAL;
978 goto out;
979 }
980
981 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
982 lockdep_is_held(&local->chanctx_mtx));
983 if (!conf) {
984 ret = -EINVAL;
985 goto out;
986 }
987
988 old_ctx = container_of(conf, struct ieee80211_chanctx, conf);
989
990 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
991 tmp_changed |= BSS_CHANGED_BANDWIDTH;
992
993 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
994
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200995 /* unref our reservation */
Luciano Coelho11335a52013-10-30 13:09:39 +0200996 sdata->reserved_chanctx = NULL;
Michal Kazior09332482014-04-09 15:29:25 +0200997 sdata->radar_required = sdata->reserved_radar_required;
Michal Kaziore3afb922014-04-09 15:29:27 +0200998 list_del(&sdata->reserved_chanctx_list);
Luciano Coelho11335a52013-10-30 13:09:39 +0200999
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001000 if (old_ctx == ctx) {
1001 /* This is our own context, just change it */
1002 ret = __ieee80211_vif_change_channel(sdata, old_ctx,
1003 &tmp_changed);
1004 if (ret)
1005 goto out;
1006 } else {
1007 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
Michal Kaziorc0166da2014-04-09 15:29:33 +02001008 if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001009 ieee80211_free_chanctx(local, old_ctx);
1010 if (ret) {
1011 /* if assign fails refcount stays the same */
Michal Kaziorc0166da2014-04-09 15:29:33 +02001012 if (ieee80211_chanctx_refcount(local, ctx) == 0)
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001013 ieee80211_free_chanctx(local, ctx);
1014 goto out;
1015 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001016
Luciano Coelho5d52ee82014-02-27 14:33:47 +02001017 if (sdata->vif.type == NL80211_IFTYPE_AP)
1018 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
1019 }
Luciano Coelho11335a52013-10-30 13:09:39 +02001020
1021 *changed = tmp_changed;
1022
1023 ieee80211_recalc_chanctx_chantype(local, ctx);
1024 ieee80211_recalc_smps_chanctx(local, ctx);
1025 ieee80211_recalc_radar_chanctx(local, ctx);
1026 ieee80211_recalc_chanctx_min_def(local, ctx);
1027out:
1028 mutex_unlock(&local->chanctx_mtx);
1029 return ret;
1030}
1031
Johannes Berg2c9b7352013-02-07 21:37:29 +01001032int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
1033 const struct cfg80211_chan_def *chandef,
1034 u32 *changed)
1035{
1036 struct ieee80211_local *local = sdata->local;
1037 struct ieee80211_chanctx_conf *conf;
1038 struct ieee80211_chanctx *ctx;
1039 int ret;
1040
1041 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
1042 IEEE80211_CHAN_DISABLED))
1043 return -EINVAL;
1044
1045 mutex_lock(&local->chanctx_mtx);
1046 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
1047 ret = 0;
1048 goto out;
1049 }
1050
1051 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1052 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
1053 ret = -EINVAL;
1054 goto out;
1055 }
1056
1057 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1058 lockdep_is_held(&local->chanctx_mtx));
1059 if (!conf) {
1060 ret = -EINVAL;
1061 goto out;
1062 }
1063
1064 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1065 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
1066 ret = -EINVAL;
1067 goto out;
1068 }
1069
1070 sdata->vif.bss_conf.chandef = *chandef;
1071
1072 ieee80211_recalc_chanctx_chantype(local, ctx);
1073
1074 *changed |= BSS_CHANGED_BANDWIDTH;
1075 ret = 0;
1076 out:
1077 mutex_unlock(&local->chanctx_mtx);
1078 return ret;
1079}
1080
Michal Kaziord01a1e62012-06-26 14:37:16 +02001081void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
1082{
Johannes Berg55de9082012-07-26 17:24:39 +02001083 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
1084
Johannes Berg34a37402013-12-18 09:43:33 +01001085 lockdep_assert_held(&sdata->local->mtx);
1086
Michal Kaziord01a1e62012-06-26 14:37:16 +02001087 mutex_lock(&sdata->local->chanctx_mtx);
1088 __ieee80211_vif_release_channel(sdata);
1089 mutex_unlock(&sdata->local->chanctx_mtx);
1090}
Johannes Berg3448c002012-09-11 17:57:42 +02001091
Johannes Berg4d76d212012-12-11 20:38:41 +01001092void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
1093{
1094 struct ieee80211_local *local = sdata->local;
1095 struct ieee80211_sub_if_data *ap;
1096 struct ieee80211_chanctx_conf *conf;
1097
1098 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
1099 return;
1100
1101 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1102
1103 mutex_lock(&local->chanctx_mtx);
1104
1105 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
1106 lockdep_is_held(&local->chanctx_mtx));
1107 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
1108 mutex_unlock(&local->chanctx_mtx);
1109}
1110
Johannes Berg3448c002012-09-11 17:57:42 +02001111void ieee80211_iter_chan_contexts_atomic(
1112 struct ieee80211_hw *hw,
1113 void (*iter)(struct ieee80211_hw *hw,
1114 struct ieee80211_chanctx_conf *chanctx_conf,
1115 void *data),
1116 void *iter_data)
1117{
1118 struct ieee80211_local *local = hw_to_local(hw);
1119 struct ieee80211_chanctx *ctx;
1120
1121 rcu_read_lock();
1122 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +01001123 if (ctx->driver_present)
1124 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +02001125 rcu_read_unlock();
1126}
1127EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);