blob: 35079c8061ae4ba737d3942f24ba461e578b022d [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
Eliad Peller21f659b2013-11-11 20:14:01 +020012static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
13{
14 switch (sta->bandwidth) {
15 case IEEE80211_STA_RX_BW_20:
16 if (sta->ht_cap.ht_supported)
17 return NL80211_CHAN_WIDTH_20;
18 else
19 return NL80211_CHAN_WIDTH_20_NOHT;
20 case IEEE80211_STA_RX_BW_40:
21 return NL80211_CHAN_WIDTH_40;
22 case IEEE80211_STA_RX_BW_80:
23 return NL80211_CHAN_WIDTH_80;
24 case IEEE80211_STA_RX_BW_160:
25 /*
26 * This applied for both 160 and 80+80. since we use
27 * the returned value to consider degradation of
28 * ctx->conf.min_def, we have to make sure to take
29 * the bigger one (NL80211_CHAN_WIDTH_160).
30 * Otherwise we might try degrading even when not
31 * needed, as the max required sta_bw returned (80+80)
32 * might be smaller than the configured bw (160).
33 */
34 return NL80211_CHAN_WIDTH_160;
35 default:
36 WARN_ON(1);
37 return NL80211_CHAN_WIDTH_20;
38 }
39}
40
41static enum nl80211_chan_width
42ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
43{
44 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
45 struct sta_info *sta;
46
47 rcu_read_lock();
48 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
49 if (sdata != sta->sdata &&
50 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
51 continue;
52
53 if (!sta->uploaded)
54 continue;
55
56 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
57 }
58 rcu_read_unlock();
59
60 return max_bw;
61}
62
63static enum nl80211_chan_width
64ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
65 struct ieee80211_chanctx_conf *conf)
66{
67 struct ieee80211_sub_if_data *sdata;
68 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
69
70 rcu_read_lock();
71 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
72 struct ieee80211_vif *vif = &sdata->vif;
73 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
74
75 if (!ieee80211_sdata_running(sdata))
76 continue;
77
78 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
79 continue;
80
81 switch (vif->type) {
82 case NL80211_IFTYPE_AP:
83 case NL80211_IFTYPE_AP_VLAN:
84 width = ieee80211_get_max_required_bw(sdata);
85 break;
86 case NL80211_IFTYPE_P2P_DEVICE:
87 continue;
88 case NL80211_IFTYPE_STATION:
89 case NL80211_IFTYPE_ADHOC:
90 case NL80211_IFTYPE_WDS:
91 case NL80211_IFTYPE_MESH_POINT:
92 width = vif->bss_conf.chandef.width;
93 break;
94 case NL80211_IFTYPE_UNSPECIFIED:
95 case NUM_NL80211_IFTYPES:
96 case NL80211_IFTYPE_MONITOR:
97 case NL80211_IFTYPE_P2P_CLIENT:
98 case NL80211_IFTYPE_P2P_GO:
99 WARN_ON_ONCE(1);
100 }
101 max_bw = max(max_bw, width);
102 }
Eliad Peller1c37a722014-03-03 13:37:14 +0200103
104 /* use the configured bandwidth in case of monitor interface */
105 sdata = rcu_dereference(local->monitor_sdata);
106 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
107 max_bw = max(max_bw, conf->def.width);
108
Eliad Peller21f659b2013-11-11 20:14:01 +0200109 rcu_read_unlock();
110
111 return max_bw;
112}
113
114/*
115 * recalc the min required chan width of the channel context, which is
116 * the max of min required widths of all the interfaces bound to this
117 * channel context.
118 */
119void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
120 struct ieee80211_chanctx *ctx)
121{
122 enum nl80211_chan_width max_bw;
123 struct cfg80211_chan_def min_def;
124
125 lockdep_assert_held(&local->chanctx_mtx);
126
127 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
128 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
129 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
130 ctx->conf.radar_enabled) {
131 ctx->conf.min_def = ctx->conf.def;
132 return;
133 }
134
135 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
136
137 /* downgrade chandef up to max_bw */
138 min_def = ctx->conf.def;
139 while (min_def.width > max_bw)
140 ieee80211_chandef_downgrade(&min_def);
141
142 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
143 return;
144
145 ctx->conf.min_def = min_def;
146 if (!ctx->driver_present)
147 return;
148
149 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
150}
151
Johannes Berg18942d32013-02-07 21:30:37 +0100152static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100153 struct ieee80211_chanctx *ctx,
154 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200155{
Johannes Berg4bf88532012-11-09 11:39:59 +0100156 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +0200157 return;
158
Johannes Berg4bf88532012-11-09 11:39:59 +0100159 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
160
161 ctx->conf.def = *chandef;
162 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Eliad Peller21f659b2013-11-11 20:14:01 +0200163 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200164
165 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100166 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200167 ieee80211_hw_config(local, 0);
168 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200169}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200170
171static struct ieee80211_chanctx *
172ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100173 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200174 enum ieee80211_chanctx_mode mode)
175{
176 struct ieee80211_chanctx *ctx;
177
178 lockdep_assert_held(&local->chanctx_mtx);
179
180 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
181 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200182
183 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100184 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200185
Michal Kaziord01a1e62012-06-26 14:37:16 +0200186 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
187 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100188
189 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
190 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200191 continue;
192
Johannes Berg18942d32013-02-07 21:30:37 +0100193 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200194
Michal Kaziord01a1e62012-06-26 14:37:16 +0200195 return ctx;
196 }
197
198 return NULL;
199}
200
Simon Wunderliche4746852013-04-08 22:43:16 +0200201static bool ieee80211_is_radar_required(struct ieee80211_local *local)
202{
203 struct ieee80211_sub_if_data *sdata;
204
Michal Kaziorcc901de2014-01-29 07:56:20 +0100205 lockdep_assert_held(&local->mtx);
206
Simon Wunderliche4746852013-04-08 22:43:16 +0200207 rcu_read_lock();
208 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
209 if (sdata->radar_required) {
210 rcu_read_unlock();
211 return true;
212 }
213 }
214 rcu_read_unlock();
215
216 return false;
217}
218
Michal Kaziord01a1e62012-06-26 14:37:16 +0200219static struct ieee80211_chanctx *
220ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100221 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200222 enum ieee80211_chanctx_mode mode)
223{
224 struct ieee80211_chanctx *ctx;
Johannes Berg382a1032013-03-22 22:30:09 +0100225 u32 changed;
Michal Kazior35f2fce2012-06-26 14:37:20 +0200226 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200227
228 lockdep_assert_held(&local->chanctx_mtx);
229
230 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
231 if (!ctx)
232 return ERR_PTR(-ENOMEM);
233
Johannes Berg4bf88532012-11-09 11:39:59 +0100234 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200235 ctx->conf.rx_chains_static = 1;
236 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200237 ctx->mode = mode;
Simon Wunderliche4746852013-04-08 22:43:16 +0200238 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
Eliad Peller21f659b2013-11-11 20:14:01 +0200239 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderliche4746852013-04-08 22:43:16 +0200240 if (!local->use_chanctx)
241 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200242
Johannes Berg34a37402013-12-18 09:43:33 +0100243 /* we hold the mutex to prevent idle from changing */
244 lockdep_assert_held(&local->mtx);
Johannes Berg382a1032013-03-22 22:30:09 +0100245 /* turn idle off *before* setting channel -- some drivers need that */
246 changed = ieee80211_idle_off(local);
247 if (changed)
248 ieee80211_hw_config(local, changed);
249
Johannes Berg55de9082012-07-26 17:24:39 +0200250 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100251 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200252 ieee80211_hw_config(local, 0);
253 } else {
254 err = drv_add_chanctx(local, ctx);
255 if (err) {
256 kfree(ctx);
Johannes Berg382a1032013-03-22 22:30:09 +0100257 ieee80211_recalc_idle(local);
Johannes Berg34a37402013-12-18 09:43:33 +0100258 return ERR_PTR(err);
Johannes Berg55de9082012-07-26 17:24:39 +0200259 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200260 }
261
Johannes Berg382a1032013-03-22 22:30:09 +0100262 /* and keep the mutex held until the new chanctx is on the list */
Johannes Berg3448c002012-09-11 17:57:42 +0200263 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200264
265 return ctx;
266}
267
268static void ieee80211_free_chanctx(struct ieee80211_local *local,
269 struct ieee80211_chanctx *ctx)
270{
Simon Wunderliche4746852013-04-08 22:43:16 +0200271 bool check_single_channel = false;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200272 lockdep_assert_held(&local->chanctx_mtx);
273
274 WARN_ON_ONCE(ctx->refcount != 0);
275
Johannes Berg55de9082012-07-26 17:24:39 +0200276 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100277 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
278 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
279 chandef->center_freq1 = chandef->chan->center_freq;
280 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200281
282 /* NOTE: Disabling radar is only valid here for
283 * single channel context. To be sure, check it ...
284 */
285 if (local->hw.conf.radar_enabled)
286 check_single_channel = true;
287 local->hw.conf.radar_enabled = false;
288
Johannes Berg55de9082012-07-26 17:24:39 +0200289 ieee80211_hw_config(local, 0);
290 } else {
291 drv_remove_chanctx(local, ctx);
292 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200293
Johannes Berg3448c002012-09-11 17:57:42 +0200294 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200295 kfree_rcu(ctx, rcu_head);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100296
Simon Wunderliche4746852013-04-08 22:43:16 +0200297 /* throw a warning if this wasn't the only channel context. */
298 WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
299
Johannes Bergfd0f9792013-02-07 00:14:51 +0100300 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200301}
302
Johannes Berg4bf88532012-11-09 11:39:59 +0100303static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
304 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200305{
306 struct ieee80211_chanctx_conf *conf = &ctx->conf;
307 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100308 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200309
310 lockdep_assert_held(&local->chanctx_mtx);
311
312 rcu_read_lock();
313 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100314
Michal Kaziore89a96f2012-06-26 14:37:22 +0200315 if (!ieee80211_sdata_running(sdata))
316 continue;
317 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
318 continue;
319
Johannes Berg4bf88532012-11-09 11:39:59 +0100320 if (!compat)
321 compat = &sdata->vif.bss_conf.chandef;
322
323 compat = cfg80211_chandef_compatible(
324 &sdata->vif.bss_conf.chandef, compat);
325 if (!compat)
326 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200327 }
328 rcu_read_unlock();
329
Johannes Berg4bf88532012-11-09 11:39:59 +0100330 if (WARN_ON_ONCE(!compat))
331 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200332
Johannes Berg18942d32013-02-07 21:30:37 +0100333 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200334}
335
Johannes Berg367bbd12013-12-18 09:36:09 +0100336static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
337 struct ieee80211_chanctx *chanctx)
338{
339 bool radar_enabled;
340
341 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100342 /* for setting local->radar_detect_enabled */
343 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100344
345 radar_enabled = ieee80211_is_radar_required(local);
346
347 if (radar_enabled == chanctx->conf.radar_enabled)
348 return;
349
350 chanctx->conf.radar_enabled = radar_enabled;
351 local->radar_detect_enabled = chanctx->conf.radar_enabled;
352
353 if (!local->use_chanctx) {
354 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
355 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
356 }
357
358 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
359}
360
Luciano Coelho77eeba92014-03-11 18:24:12 +0200361static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
362 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200363{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200364 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200365 struct ieee80211_chanctx_conf *conf;
366 struct ieee80211_chanctx *curr_ctx = NULL;
367 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200368
Luciano Coelho77eeba92014-03-11 18:24:12 +0200369 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
370 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200371
Luciano Coelho77eeba92014-03-11 18:24:12 +0200372 if (conf) {
373 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200374
Luciano Coelho77eeba92014-03-11 18:24:12 +0200375 curr_ctx->refcount--;
376 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
377 conf = NULL;
378 }
379
380 if (new_ctx) {
381 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
382 if (ret)
383 goto out;
384
385 new_ctx->refcount++;
386 conf = &new_ctx->conf;
387 }
388
389out:
390 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
391
392 sdata->vif.bss_conf.idle = !conf;
393
394 if (curr_ctx && curr_ctx->refcount > 0) {
395 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
396 ieee80211_recalc_smps_chanctx(local, curr_ctx);
397 ieee80211_recalc_radar_chanctx(local, curr_ctx);
398 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
399 }
400
401 if (new_ctx && new_ctx->refcount > 0) {
402 ieee80211_recalc_txpower(sdata);
403 ieee80211_recalc_chanctx_min_def(local, new_ctx);
404 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100405
406 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
407 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200408 ieee80211_bss_info_change_notify(sdata,
409 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100410
Luciano Coelho77eeba92014-03-11 18:24:12 +0200411 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200412}
413
414static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
415{
416 struct ieee80211_local *local = sdata->local;
417 struct ieee80211_chanctx_conf *conf;
418 struct ieee80211_chanctx *ctx;
419
420 lockdep_assert_held(&local->chanctx_mtx);
421
422 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
423 lockdep_is_held(&local->chanctx_mtx));
424 if (!conf)
425 return;
426
427 ctx = container_of(conf, struct ieee80211_chanctx, conf);
428
Luciano Coelho77eeba92014-03-11 18:24:12 +0200429 ieee80211_assign_vif_chanctx(sdata, NULL);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200430 if (ctx->refcount == 0)
431 ieee80211_free_chanctx(local, ctx);
432}
433
Johannes Berg04ecd252012-09-11 14:34:12 +0200434void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
435 struct ieee80211_chanctx *chanctx)
436{
437 struct ieee80211_sub_if_data *sdata;
438 u8 rx_chains_static, rx_chains_dynamic;
439
440 lockdep_assert_held(&local->chanctx_mtx);
441
442 rx_chains_static = 1;
443 rx_chains_dynamic = 1;
444
445 rcu_read_lock();
446 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
447 u8 needed_static, needed_dynamic;
448
449 if (!ieee80211_sdata_running(sdata))
450 continue;
451
452 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
453 &chanctx->conf)
454 continue;
455
456 switch (sdata->vif.type) {
457 case NL80211_IFTYPE_P2P_DEVICE:
458 continue;
459 case NL80211_IFTYPE_STATION:
460 if (!sdata->u.mgd.associated)
461 continue;
462 break;
463 case NL80211_IFTYPE_AP_VLAN:
464 continue;
465 case NL80211_IFTYPE_AP:
466 case NL80211_IFTYPE_ADHOC:
467 case NL80211_IFTYPE_WDS:
468 case NL80211_IFTYPE_MESH_POINT:
469 break;
470 default:
471 WARN_ON_ONCE(1);
472 }
473
474 switch (sdata->smps_mode) {
475 default:
476 WARN_ONCE(1, "Invalid SMPS mode %d\n",
477 sdata->smps_mode);
478 /* fall through */
479 case IEEE80211_SMPS_OFF:
480 needed_static = sdata->needed_rx_chains;
481 needed_dynamic = sdata->needed_rx_chains;
482 break;
483 case IEEE80211_SMPS_DYNAMIC:
484 needed_static = 1;
485 needed_dynamic = sdata->needed_rx_chains;
486 break;
487 case IEEE80211_SMPS_STATIC:
488 needed_static = 1;
489 needed_dynamic = 1;
490 break;
491 }
492
493 rx_chains_static = max(rx_chains_static, needed_static);
494 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
495 }
496 rcu_read_unlock();
497
498 if (!local->use_chanctx) {
499 if (rx_chains_static > 1)
500 local->smps_mode = IEEE80211_SMPS_OFF;
501 else if (rx_chains_dynamic > 1)
502 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
503 else
504 local->smps_mode = IEEE80211_SMPS_STATIC;
505 ieee80211_hw_config(local, 0);
506 }
507
508 if (rx_chains_static == chanctx->conf.rx_chains_static &&
509 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
510 return;
511
512 chanctx->conf.rx_chains_static = rx_chains_static;
513 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
514 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
515}
516
Michal Kaziord01a1e62012-06-26 14:37:16 +0200517int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100518 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200519 enum ieee80211_chanctx_mode mode)
520{
521 struct ieee80211_local *local = sdata->local;
522 struct ieee80211_chanctx *ctx;
Luciano Coelho73de86a2014-02-13 11:31:59 +0200523 u8 radar_detect_width = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200524 int ret;
525
Johannes Berg34a37402013-12-18 09:43:33 +0100526 lockdep_assert_held(&local->mtx);
527
Johannes Berg55de9082012-07-26 17:24:39 +0200528 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
529
Michal Kaziord01a1e62012-06-26 14:37:16 +0200530 mutex_lock(&local->chanctx_mtx);
Luciano Coelho73de86a2014-02-13 11:31:59 +0200531
532 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
533 chandef,
534 sdata->wdev.iftype);
535 if (ret < 0)
536 goto out;
537 if (ret > 0)
538 radar_detect_width = BIT(chandef->width);
539
540 sdata->radar_required = ret;
541
542 ret = ieee80211_check_combinations(sdata, chandef, mode,
543 radar_detect_width);
544 if (ret < 0)
545 goto out;
546
Michal Kaziord01a1e62012-06-26 14:37:16 +0200547 __ieee80211_vif_release_channel(sdata);
548
Johannes Berg4bf88532012-11-09 11:39:59 +0100549 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200550 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100551 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200552 if (IS_ERR(ctx)) {
553 ret = PTR_ERR(ctx);
554 goto out;
555 }
556
Johannes Berg4bf88532012-11-09 11:39:59 +0100557 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200558
Michal Kaziord01a1e62012-06-26 14:37:16 +0200559 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
560 if (ret) {
561 /* if assign fails refcount stays the same */
562 if (ctx->refcount == 0)
563 ieee80211_free_chanctx(local, ctx);
564 goto out;
565 }
566
Johannes Berg04ecd252012-09-11 14:34:12 +0200567 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100568 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200569 out:
570 mutex_unlock(&local->chanctx_mtx);
571 return ret;
572}
573
Luciano Coelho33ffd952014-01-30 22:08:16 +0200574static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
575 struct ieee80211_chanctx *ctx,
576 u32 *changed)
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200577{
578 struct ieee80211_local *local = sdata->local;
Luciano Coelho33787fc2013-11-11 20:34:54 +0200579 const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200580 u32 chanctx_changed = 0;
581
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200582 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
583 IEEE80211_CHAN_DISABLED))
584 return -EINVAL;
585
Luciano Coelho33ffd952014-01-30 22:08:16 +0200586 if (ctx->refcount != 1)
587 return -EINVAL;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200588
589 if (sdata->vif.bss_conf.chandef.width != chandef->width) {
590 chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
591 *changed |= BSS_CHANGED_BANDWIDTH;
592 }
593
594 sdata->vif.bss_conf.chandef = *chandef;
595 ctx->conf.def = *chandef;
596
597 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
598 drv_change_chanctx(local, ctx, chanctx_changed);
599
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200600 ieee80211_recalc_chanctx_chantype(local, ctx);
601 ieee80211_recalc_smps_chanctx(local, ctx);
602 ieee80211_recalc_radar_chanctx(local, ctx);
Eliad Peller21f659b2013-11-11 20:14:01 +0200603 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200604
Luciano Coelho33ffd952014-01-30 22:08:16 +0200605 return 0;
606}
607
608int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
609 u32 *changed)
610{
611 struct ieee80211_local *local = sdata->local;
612 struct ieee80211_chanctx_conf *conf;
613 struct ieee80211_chanctx *ctx;
614 int ret;
615
616 lockdep_assert_held(&local->mtx);
617
618 /* should never be called if not performing a channel switch. */
619 if (WARN_ON(!sdata->vif.csa_active))
620 return -EINVAL;
621
622 mutex_lock(&local->chanctx_mtx);
623 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
624 lockdep_is_held(&local->chanctx_mtx));
625 if (!conf) {
626 ret = -EINVAL;
627 goto out;
628 }
629
630 ctx = container_of(conf, struct ieee80211_chanctx, conf);
631
632 ret = __ieee80211_vif_change_channel(sdata, ctx, changed);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200633 out:
634 mutex_unlock(&local->chanctx_mtx);
635 return ret;
636}
637
Johannes Berg2c9b7352013-02-07 21:37:29 +0100638int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
639 const struct cfg80211_chan_def *chandef,
640 u32 *changed)
641{
642 struct ieee80211_local *local = sdata->local;
643 struct ieee80211_chanctx_conf *conf;
644 struct ieee80211_chanctx *ctx;
645 int ret;
646
647 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
648 IEEE80211_CHAN_DISABLED))
649 return -EINVAL;
650
651 mutex_lock(&local->chanctx_mtx);
652 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
653 ret = 0;
654 goto out;
655 }
656
657 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
658 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
659 ret = -EINVAL;
660 goto out;
661 }
662
663 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
664 lockdep_is_held(&local->chanctx_mtx));
665 if (!conf) {
666 ret = -EINVAL;
667 goto out;
668 }
669
670 ctx = container_of(conf, struct ieee80211_chanctx, conf);
671 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
672 ret = -EINVAL;
673 goto out;
674 }
675
676 sdata->vif.bss_conf.chandef = *chandef;
677
678 ieee80211_recalc_chanctx_chantype(local, ctx);
679
680 *changed |= BSS_CHANGED_BANDWIDTH;
681 ret = 0;
682 out:
683 mutex_unlock(&local->chanctx_mtx);
684 return ret;
685}
686
Michal Kaziord01a1e62012-06-26 14:37:16 +0200687void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
688{
Johannes Berg55de9082012-07-26 17:24:39 +0200689 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
690
Johannes Berg34a37402013-12-18 09:43:33 +0100691 lockdep_assert_held(&sdata->local->mtx);
692
Michal Kaziord01a1e62012-06-26 14:37:16 +0200693 mutex_lock(&sdata->local->chanctx_mtx);
694 __ieee80211_vif_release_channel(sdata);
695 mutex_unlock(&sdata->local->chanctx_mtx);
696}
Johannes Berg3448c002012-09-11 17:57:42 +0200697
Johannes Berg4d76d212012-12-11 20:38:41 +0100698void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
699{
700 struct ieee80211_local *local = sdata->local;
701 struct ieee80211_sub_if_data *ap;
702 struct ieee80211_chanctx_conf *conf;
703
704 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
705 return;
706
707 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
708
709 mutex_lock(&local->chanctx_mtx);
710
711 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
712 lockdep_is_held(&local->chanctx_mtx));
713 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
714 mutex_unlock(&local->chanctx_mtx);
715}
716
Johannes Berg1f4ac5a2013-02-08 12:07:44 +0100717void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
718 bool clear)
719{
720 struct ieee80211_local *local = sdata->local;
721 struct ieee80211_sub_if_data *vlan;
722 struct ieee80211_chanctx_conf *conf;
723
Michal Kazior4e141da2014-03-05 13:14:08 +0100724 lockdep_assert_held(&local->mtx);
Johannes Berg1f4ac5a2013-02-08 12:07:44 +0100725
726 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
727 return;
728
729 mutex_lock(&local->chanctx_mtx);
730
731 /*
732 * Check that conf exists, even when clearing this function
733 * must be called with the AP's channel context still there
734 * as it would otherwise cause VLANs to have an invalid
735 * channel context pointer for a while, possibly pointing
736 * to a channel context that has already been freed.
737 */
738 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
739 lockdep_is_held(&local->chanctx_mtx));
740 WARN_ON(!conf);
741
742 if (clear)
743 conf = NULL;
744
745 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
746 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
747
748 mutex_unlock(&local->chanctx_mtx);
749}
750
Johannes Berg3448c002012-09-11 17:57:42 +0200751void ieee80211_iter_chan_contexts_atomic(
752 struct ieee80211_hw *hw,
753 void (*iter)(struct ieee80211_hw *hw,
754 struct ieee80211_chanctx_conf *chanctx_conf,
755 void *data),
756 void *iter_data)
757{
758 struct ieee80211_local *local = hw_to_local(hw);
759 struct ieee80211_chanctx *ctx;
760
761 rcu_read_lock();
762 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +0100763 if (ctx->driver_present)
764 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +0200765 rcu_read_unlock();
766}
767EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);