blob: 57b8ab1bc5c1a069c0693a97f787aa08b13b8df1 [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
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200171static bool ieee80211_chanctx_is_reserved(struct ieee80211_local *local,
172 struct ieee80211_chanctx *ctx)
173{
174 struct ieee80211_sub_if_data *sdata;
175 bool ret = false;
176
177 lockdep_assert_held(&local->chanctx_mtx);
178 rcu_read_lock();
179 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
180 if (!ieee80211_sdata_running(sdata))
181 continue;
182 if (sdata->reserved_chanctx == ctx) {
183 ret = true;
184 break;
185 }
186 }
187
188 rcu_read_unlock();
189 return ret;
190}
191
Michal Kaziord01a1e62012-06-26 14:37:16 +0200192static struct ieee80211_chanctx *
193ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100194 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200195 enum ieee80211_chanctx_mode mode)
196{
197 struct ieee80211_chanctx *ctx;
198
199 lockdep_assert_held(&local->chanctx_mtx);
200
201 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
202 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200203
204 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100205 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200206
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200207 /* We don't support chanctx reservation for multiple
208 * vifs yet, so don't allow reserved chanctxs to be
209 * reused.
210 */
211 if ((ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) ||
212 ieee80211_chanctx_is_reserved(local, ctx))
Michal Kaziord01a1e62012-06-26 14:37:16 +0200213 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100214
215 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
216 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200217 continue;
218
Johannes Berg18942d32013-02-07 21:30:37 +0100219 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200220
Michal Kaziord01a1e62012-06-26 14:37:16 +0200221 return ctx;
222 }
223
224 return NULL;
225}
226
Simon Wunderliche4746852013-04-08 22:43:16 +0200227static bool ieee80211_is_radar_required(struct ieee80211_local *local)
228{
229 struct ieee80211_sub_if_data *sdata;
230
Michal Kaziorcc901de2014-01-29 07:56:20 +0100231 lockdep_assert_held(&local->mtx);
232
Simon Wunderliche4746852013-04-08 22:43:16 +0200233 rcu_read_lock();
234 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
235 if (sdata->radar_required) {
236 rcu_read_unlock();
237 return true;
238 }
239 }
240 rcu_read_unlock();
241
242 return false;
243}
244
Michal Kaziord01a1e62012-06-26 14:37:16 +0200245static struct ieee80211_chanctx *
246ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100247 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200248 enum ieee80211_chanctx_mode mode)
249{
250 struct ieee80211_chanctx *ctx;
Johannes Berg382a1032013-03-22 22:30:09 +0100251 u32 changed;
Michal Kazior35f2fce2012-06-26 14:37:20 +0200252 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200253
254 lockdep_assert_held(&local->chanctx_mtx);
255
256 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
257 if (!ctx)
258 return ERR_PTR(-ENOMEM);
259
Johannes Berg4bf88532012-11-09 11:39:59 +0100260 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200261 ctx->conf.rx_chains_static = 1;
262 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200263 ctx->mode = mode;
Simon Wunderliche4746852013-04-08 22:43:16 +0200264 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
Eliad Peller21f659b2013-11-11 20:14:01 +0200265 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderliche4746852013-04-08 22:43:16 +0200266 if (!local->use_chanctx)
267 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200268
Johannes Berg34a37402013-12-18 09:43:33 +0100269 /* we hold the mutex to prevent idle from changing */
270 lockdep_assert_held(&local->mtx);
Johannes Berg382a1032013-03-22 22:30:09 +0100271 /* turn idle off *before* setting channel -- some drivers need that */
272 changed = ieee80211_idle_off(local);
273 if (changed)
274 ieee80211_hw_config(local, changed);
275
Johannes Berg55de9082012-07-26 17:24:39 +0200276 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100277 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200278 ieee80211_hw_config(local, 0);
279 } else {
280 err = drv_add_chanctx(local, ctx);
281 if (err) {
282 kfree(ctx);
Johannes Berg382a1032013-03-22 22:30:09 +0100283 ieee80211_recalc_idle(local);
Johannes Berg34a37402013-12-18 09:43:33 +0100284 return ERR_PTR(err);
Johannes Berg55de9082012-07-26 17:24:39 +0200285 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200286 }
287
Johannes Berg382a1032013-03-22 22:30:09 +0100288 /* and keep the mutex held until the new chanctx is on the list */
Johannes Berg3448c002012-09-11 17:57:42 +0200289 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200290
291 return ctx;
292}
293
294static void ieee80211_free_chanctx(struct ieee80211_local *local,
295 struct ieee80211_chanctx *ctx)
296{
Simon Wunderliche4746852013-04-08 22:43:16 +0200297 bool check_single_channel = false;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200298 lockdep_assert_held(&local->chanctx_mtx);
299
300 WARN_ON_ONCE(ctx->refcount != 0);
301
Johannes Berg55de9082012-07-26 17:24:39 +0200302 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100303 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
304 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
305 chandef->center_freq1 = chandef->chan->center_freq;
306 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200307
308 /* NOTE: Disabling radar is only valid here for
309 * single channel context. To be sure, check it ...
310 */
311 if (local->hw.conf.radar_enabled)
312 check_single_channel = true;
313 local->hw.conf.radar_enabled = false;
314
Johannes Berg55de9082012-07-26 17:24:39 +0200315 ieee80211_hw_config(local, 0);
316 } else {
317 drv_remove_chanctx(local, ctx);
318 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200319
Johannes Berg3448c002012-09-11 17:57:42 +0200320 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200321 kfree_rcu(ctx, rcu_head);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100322
Simon Wunderliche4746852013-04-08 22:43:16 +0200323 /* throw a warning if this wasn't the only channel context. */
324 WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
325
Johannes Bergfd0f9792013-02-07 00:14:51 +0100326 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200327}
328
Johannes Berg4bf88532012-11-09 11:39:59 +0100329static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
330 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200331{
332 struct ieee80211_chanctx_conf *conf = &ctx->conf;
333 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100334 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200335
336 lockdep_assert_held(&local->chanctx_mtx);
337
338 rcu_read_lock();
339 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100340
Michal Kaziore89a96f2012-06-26 14:37:22 +0200341 if (!ieee80211_sdata_running(sdata))
342 continue;
343 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
344 continue;
345
Johannes Berg4bf88532012-11-09 11:39:59 +0100346 if (!compat)
347 compat = &sdata->vif.bss_conf.chandef;
348
349 compat = cfg80211_chandef_compatible(
350 &sdata->vif.bss_conf.chandef, compat);
351 if (!compat)
352 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200353 }
354 rcu_read_unlock();
355
Johannes Berg4bf88532012-11-09 11:39:59 +0100356 if (WARN_ON_ONCE(!compat))
357 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200358
Johannes Berg18942d32013-02-07 21:30:37 +0100359 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200360}
361
Johannes Berg367bbd12013-12-18 09:36:09 +0100362static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
363 struct ieee80211_chanctx *chanctx)
364{
365 bool radar_enabled;
366
367 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100368 /* for setting local->radar_detect_enabled */
369 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100370
371 radar_enabled = ieee80211_is_radar_required(local);
372
373 if (radar_enabled == chanctx->conf.radar_enabled)
374 return;
375
376 chanctx->conf.radar_enabled = radar_enabled;
377 local->radar_detect_enabled = chanctx->conf.radar_enabled;
378
379 if (!local->use_chanctx) {
380 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
381 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
382 }
383
384 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
385}
386
Luciano Coelho77eeba92014-03-11 18:24:12 +0200387static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
388 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200389{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200390 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200391 struct ieee80211_chanctx_conf *conf;
392 struct ieee80211_chanctx *curr_ctx = NULL;
393 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200394
Luciano Coelho77eeba92014-03-11 18:24:12 +0200395 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
396 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200397
Luciano Coelho77eeba92014-03-11 18:24:12 +0200398 if (conf) {
399 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200400
Luciano Coelho77eeba92014-03-11 18:24:12 +0200401 curr_ctx->refcount--;
402 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
403 conf = NULL;
404 }
405
406 if (new_ctx) {
407 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
408 if (ret)
409 goto out;
410
411 new_ctx->refcount++;
412 conf = &new_ctx->conf;
413 }
414
415out:
416 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
417
418 sdata->vif.bss_conf.idle = !conf;
419
420 if (curr_ctx && curr_ctx->refcount > 0) {
421 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
422 ieee80211_recalc_smps_chanctx(local, curr_ctx);
423 ieee80211_recalc_radar_chanctx(local, curr_ctx);
424 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
425 }
426
427 if (new_ctx && new_ctx->refcount > 0) {
428 ieee80211_recalc_txpower(sdata);
429 ieee80211_recalc_chanctx_min_def(local, new_ctx);
430 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100431
432 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
433 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200434 ieee80211_bss_info_change_notify(sdata,
435 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100436
Luciano Coelho77eeba92014-03-11 18:24:12 +0200437 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200438}
439
440static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
441{
442 struct ieee80211_local *local = sdata->local;
443 struct ieee80211_chanctx_conf *conf;
444 struct ieee80211_chanctx *ctx;
445
446 lockdep_assert_held(&local->chanctx_mtx);
447
448 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
449 lockdep_is_held(&local->chanctx_mtx));
450 if (!conf)
451 return;
452
453 ctx = container_of(conf, struct ieee80211_chanctx, conf);
454
Luciano Coelho11335a52013-10-30 13:09:39 +0200455 if (sdata->reserved_chanctx)
456 ieee80211_vif_unreserve_chanctx(sdata);
457
Luciano Coelho77eeba92014-03-11 18:24:12 +0200458 ieee80211_assign_vif_chanctx(sdata, NULL);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200459 if (ctx->refcount == 0)
460 ieee80211_free_chanctx(local, ctx);
461}
462
Johannes Berg04ecd252012-09-11 14:34:12 +0200463void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
464 struct ieee80211_chanctx *chanctx)
465{
466 struct ieee80211_sub_if_data *sdata;
467 u8 rx_chains_static, rx_chains_dynamic;
468
469 lockdep_assert_held(&local->chanctx_mtx);
470
471 rx_chains_static = 1;
472 rx_chains_dynamic = 1;
473
474 rcu_read_lock();
475 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
476 u8 needed_static, needed_dynamic;
477
478 if (!ieee80211_sdata_running(sdata))
479 continue;
480
481 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
482 &chanctx->conf)
483 continue;
484
485 switch (sdata->vif.type) {
486 case NL80211_IFTYPE_P2P_DEVICE:
487 continue;
488 case NL80211_IFTYPE_STATION:
489 if (!sdata->u.mgd.associated)
490 continue;
491 break;
492 case NL80211_IFTYPE_AP_VLAN:
493 continue;
494 case NL80211_IFTYPE_AP:
495 case NL80211_IFTYPE_ADHOC:
496 case NL80211_IFTYPE_WDS:
497 case NL80211_IFTYPE_MESH_POINT:
498 break;
499 default:
500 WARN_ON_ONCE(1);
501 }
502
503 switch (sdata->smps_mode) {
504 default:
505 WARN_ONCE(1, "Invalid SMPS mode %d\n",
506 sdata->smps_mode);
507 /* fall through */
508 case IEEE80211_SMPS_OFF:
509 needed_static = sdata->needed_rx_chains;
510 needed_dynamic = sdata->needed_rx_chains;
511 break;
512 case IEEE80211_SMPS_DYNAMIC:
513 needed_static = 1;
514 needed_dynamic = sdata->needed_rx_chains;
515 break;
516 case IEEE80211_SMPS_STATIC:
517 needed_static = 1;
518 needed_dynamic = 1;
519 break;
520 }
521
522 rx_chains_static = max(rx_chains_static, needed_static);
523 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
524 }
525 rcu_read_unlock();
526
527 if (!local->use_chanctx) {
528 if (rx_chains_static > 1)
529 local->smps_mode = IEEE80211_SMPS_OFF;
530 else if (rx_chains_dynamic > 1)
531 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
532 else
533 local->smps_mode = IEEE80211_SMPS_STATIC;
534 ieee80211_hw_config(local, 0);
535 }
536
537 if (rx_chains_static == chanctx->conf.rx_chains_static &&
538 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
539 return;
540
541 chanctx->conf.rx_chains_static = rx_chains_static;
542 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
543 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
544}
545
Michal Kaziord01a1e62012-06-26 14:37:16 +0200546int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100547 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200548 enum ieee80211_chanctx_mode mode)
549{
550 struct ieee80211_local *local = sdata->local;
551 struct ieee80211_chanctx *ctx;
Luciano Coelho73de86a2014-02-13 11:31:59 +0200552 u8 radar_detect_width = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200553 int ret;
554
Johannes Berg34a37402013-12-18 09:43:33 +0100555 lockdep_assert_held(&local->mtx);
556
Johannes Berg55de9082012-07-26 17:24:39 +0200557 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
558
Michal Kaziord01a1e62012-06-26 14:37:16 +0200559 mutex_lock(&local->chanctx_mtx);
Luciano Coelho73de86a2014-02-13 11:31:59 +0200560
561 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
562 chandef,
563 sdata->wdev.iftype);
564 if (ret < 0)
565 goto out;
566 if (ret > 0)
567 radar_detect_width = BIT(chandef->width);
568
569 sdata->radar_required = ret;
570
571 ret = ieee80211_check_combinations(sdata, chandef, mode,
572 radar_detect_width);
573 if (ret < 0)
574 goto out;
575
Michal Kaziord01a1e62012-06-26 14:37:16 +0200576 __ieee80211_vif_release_channel(sdata);
577
Johannes Berg4bf88532012-11-09 11:39:59 +0100578 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200579 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100580 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200581 if (IS_ERR(ctx)) {
582 ret = PTR_ERR(ctx);
583 goto out;
584 }
585
Johannes Berg4bf88532012-11-09 11:39:59 +0100586 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200587
Michal Kaziord01a1e62012-06-26 14:37:16 +0200588 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
589 if (ret) {
590 /* if assign fails refcount stays the same */
591 if (ctx->refcount == 0)
592 ieee80211_free_chanctx(local, ctx);
593 goto out;
594 }
595
Johannes Berg04ecd252012-09-11 14:34:12 +0200596 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100597 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200598 out:
599 mutex_unlock(&local->chanctx_mtx);
600 return ret;
601}
602
Luciano Coelho33ffd952014-01-30 22:08:16 +0200603static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
604 struct ieee80211_chanctx *ctx,
605 u32 *changed)
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200606{
607 struct ieee80211_local *local = sdata->local;
Luciano Coelho33787fc2013-11-11 20:34:54 +0200608 const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200609 u32 chanctx_changed = 0;
610
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200611 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
612 IEEE80211_CHAN_DISABLED))
613 return -EINVAL;
614
Luciano Coelho33ffd952014-01-30 22:08:16 +0200615 if (ctx->refcount != 1)
616 return -EINVAL;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200617
618 if (sdata->vif.bss_conf.chandef.width != chandef->width) {
619 chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
620 *changed |= BSS_CHANGED_BANDWIDTH;
621 }
622
623 sdata->vif.bss_conf.chandef = *chandef;
624 ctx->conf.def = *chandef;
625
626 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
627 drv_change_chanctx(local, ctx, chanctx_changed);
628
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200629 ieee80211_recalc_chanctx_chantype(local, ctx);
630 ieee80211_recalc_smps_chanctx(local, ctx);
631 ieee80211_recalc_radar_chanctx(local, ctx);
Eliad Peller21f659b2013-11-11 20:14:01 +0200632 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200633
Luciano Coelho33ffd952014-01-30 22:08:16 +0200634 return 0;
635}
636
637int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
638 u32 *changed)
639{
640 struct ieee80211_local *local = sdata->local;
641 struct ieee80211_chanctx_conf *conf;
642 struct ieee80211_chanctx *ctx;
643 int ret;
644
645 lockdep_assert_held(&local->mtx);
646
647 /* should never be called if not performing a channel switch. */
648 if (WARN_ON(!sdata->vif.csa_active))
649 return -EINVAL;
650
651 mutex_lock(&local->chanctx_mtx);
652 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
653 lockdep_is_held(&local->chanctx_mtx));
654 if (!conf) {
655 ret = -EINVAL;
656 goto out;
657 }
658
659 ctx = container_of(conf, struct ieee80211_chanctx, conf);
660
661 ret = __ieee80211_vif_change_channel(sdata, ctx, changed);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200662 out:
663 mutex_unlock(&local->chanctx_mtx);
664 return ret;
665}
666
Luciano Coelho11335a52013-10-30 13:09:39 +0200667static void
668__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
669 bool clear)
670{
671 struct ieee80211_local *local = sdata->local;
672 struct ieee80211_sub_if_data *vlan;
673 struct ieee80211_chanctx_conf *conf;
674
675 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
676 return;
677
678 lockdep_assert_held(&local->mtx);
679
680 /* Check that conf exists, even when clearing this function
681 * must be called with the AP's channel context still there
682 * as it would otherwise cause VLANs to have an invalid
683 * channel context pointer for a while, possibly pointing
684 * to a channel context that has already been freed.
685 */
686 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
687 lockdep_is_held(&local->chanctx_mtx));
688 WARN_ON(!conf);
689
690 if (clear)
691 conf = NULL;
692
693 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
694 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
695}
696
697void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
698 bool clear)
699{
700 struct ieee80211_local *local = sdata->local;
701
702 mutex_lock(&local->chanctx_mtx);
703
704 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
705
706 mutex_unlock(&local->chanctx_mtx);
707}
708
709int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
710{
711 lockdep_assert_held(&sdata->local->chanctx_mtx);
712
713 if (WARN_ON(!sdata->reserved_chanctx))
714 return -EINVAL;
715
716 if (--sdata->reserved_chanctx->refcount == 0)
717 ieee80211_free_chanctx(sdata->local, sdata->reserved_chanctx);
718
719 sdata->reserved_chanctx = NULL;
720
721 return 0;
722}
723
724int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
725 const struct cfg80211_chan_def *chandef,
726 enum ieee80211_chanctx_mode mode)
727{
728 struct ieee80211_local *local = sdata->local;
729 struct ieee80211_chanctx_conf *conf;
730 struct ieee80211_chanctx *new_ctx, *curr_ctx;
731 int ret = 0;
732
733 mutex_lock(&local->chanctx_mtx);
734
735 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
736 lockdep_is_held(&local->chanctx_mtx));
737 if (!conf) {
738 ret = -EINVAL;
739 goto out;
740 }
741
742 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
743
744 /* try to find another context with the chandef we want */
745 new_ctx = ieee80211_find_chanctx(local, chandef, mode);
746 if (!new_ctx) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200747 if (curr_ctx->refcount == 1 &&
748 (local->hw.flags & IEEE80211_HW_CHANGE_RUNNING_CHANCTX)) {
749 /* if we're the only users of the chanctx and
750 * the driver supports changing a running
751 * context, reserve our current context
752 */
753 new_ctx = curr_ctx;
754 } else {
755 /* create a new context and reserve it */
756 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
757 if (IS_ERR(new_ctx)) {
758 ret = PTR_ERR(new_ctx);
759 goto out;
760 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200761 }
762 }
763
764 new_ctx->refcount++;
765 sdata->reserved_chanctx = new_ctx;
766 sdata->reserved_chandef = *chandef;
767out:
768 mutex_unlock(&local->chanctx_mtx);
769 return ret;
770}
771
772int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata,
773 u32 *changed)
774{
775 struct ieee80211_local *local = sdata->local;
776 struct ieee80211_chanctx *ctx;
777 struct ieee80211_chanctx *old_ctx;
778 struct ieee80211_chanctx_conf *conf;
779 int ret;
780 u32 tmp_changed = *changed;
781
782 /* TODO: need to recheck if the chandef is usable etc.? */
783
784 lockdep_assert_held(&local->mtx);
785
786 mutex_lock(&local->chanctx_mtx);
787
788 ctx = sdata->reserved_chanctx;
789 if (WARN_ON(!ctx)) {
790 ret = -EINVAL;
791 goto out;
792 }
793
794 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
795 lockdep_is_held(&local->chanctx_mtx));
796 if (!conf) {
797 ret = -EINVAL;
798 goto out;
799 }
800
801 old_ctx = container_of(conf, struct ieee80211_chanctx, conf);
802
803 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
804 tmp_changed |= BSS_CHANGED_BANDWIDTH;
805
806 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
807
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200808 /* unref our reservation */
Luciano Coelho11335a52013-10-30 13:09:39 +0200809 ctx->refcount--;
810 sdata->reserved_chanctx = NULL;
811
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200812 if (old_ctx == ctx) {
813 /* This is our own context, just change it */
814 ret = __ieee80211_vif_change_channel(sdata, old_ctx,
815 &tmp_changed);
816 if (ret)
817 goto out;
818 } else {
819 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
820 if (old_ctx->refcount == 0)
821 ieee80211_free_chanctx(local, old_ctx);
822 if (ret) {
823 /* if assign fails refcount stays the same */
824 if (ctx->refcount == 0)
825 ieee80211_free_chanctx(local, ctx);
826 goto out;
827 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200828
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200829 if (sdata->vif.type == NL80211_IFTYPE_AP)
830 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
831 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200832
833 *changed = tmp_changed;
834
835 ieee80211_recalc_chanctx_chantype(local, ctx);
836 ieee80211_recalc_smps_chanctx(local, ctx);
837 ieee80211_recalc_radar_chanctx(local, ctx);
838 ieee80211_recalc_chanctx_min_def(local, ctx);
839out:
840 mutex_unlock(&local->chanctx_mtx);
841 return ret;
842}
843
Johannes Berg2c9b7352013-02-07 21:37:29 +0100844int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
845 const struct cfg80211_chan_def *chandef,
846 u32 *changed)
847{
848 struct ieee80211_local *local = sdata->local;
849 struct ieee80211_chanctx_conf *conf;
850 struct ieee80211_chanctx *ctx;
851 int ret;
852
853 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
854 IEEE80211_CHAN_DISABLED))
855 return -EINVAL;
856
857 mutex_lock(&local->chanctx_mtx);
858 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
859 ret = 0;
860 goto out;
861 }
862
863 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
864 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
865 ret = -EINVAL;
866 goto out;
867 }
868
869 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
870 lockdep_is_held(&local->chanctx_mtx));
871 if (!conf) {
872 ret = -EINVAL;
873 goto out;
874 }
875
876 ctx = container_of(conf, struct ieee80211_chanctx, conf);
877 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
878 ret = -EINVAL;
879 goto out;
880 }
881
882 sdata->vif.bss_conf.chandef = *chandef;
883
884 ieee80211_recalc_chanctx_chantype(local, ctx);
885
886 *changed |= BSS_CHANGED_BANDWIDTH;
887 ret = 0;
888 out:
889 mutex_unlock(&local->chanctx_mtx);
890 return ret;
891}
892
Michal Kaziord01a1e62012-06-26 14:37:16 +0200893void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
894{
Johannes Berg55de9082012-07-26 17:24:39 +0200895 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
896
Johannes Berg34a37402013-12-18 09:43:33 +0100897 lockdep_assert_held(&sdata->local->mtx);
898
Michal Kaziord01a1e62012-06-26 14:37:16 +0200899 mutex_lock(&sdata->local->chanctx_mtx);
900 __ieee80211_vif_release_channel(sdata);
901 mutex_unlock(&sdata->local->chanctx_mtx);
902}
Johannes Berg3448c002012-09-11 17:57:42 +0200903
Johannes Berg4d76d212012-12-11 20:38:41 +0100904void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
905{
906 struct ieee80211_local *local = sdata->local;
907 struct ieee80211_sub_if_data *ap;
908 struct ieee80211_chanctx_conf *conf;
909
910 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
911 return;
912
913 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
914
915 mutex_lock(&local->chanctx_mtx);
916
917 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
918 lockdep_is_held(&local->chanctx_mtx));
919 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
920 mutex_unlock(&local->chanctx_mtx);
921}
922
Johannes Berg3448c002012-09-11 17:57:42 +0200923void ieee80211_iter_chan_contexts_atomic(
924 struct ieee80211_hw *hw,
925 void (*iter)(struct ieee80211_hw *hw,
926 struct ieee80211_chanctx_conf *chanctx_conf,
927 void *data),
928 void *iter_data)
929{
930 struct ieee80211_local *local = hw_to_local(hw);
931 struct ieee80211_chanctx *ctx;
932
933 rcu_read_lock();
934 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +0100935 if (ctx->driver_present)
936 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +0200937 rcu_read_unlock();
938}
939EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);