blob: 504526c9786715b71bfcb2dc07c90aa215b7c1dd [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 Kaziorc2b90ad2014-04-09 15:29:24 +020012static int ieee80211_num_chanctx(struct ieee80211_local *local)
13{
14 struct ieee80211_chanctx *ctx;
15 int num = 0;
16
17 lockdep_assert_held(&local->chanctx_mtx);
18
19 list_for_each_entry(ctx, &local->chanctx_list, list)
20 num++;
21
22 return num;
23}
24
25static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
26{
27 lockdep_assert_held(&local->chanctx_mtx);
28 return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
29}
30
Eliad Peller21f659b2013-11-11 20:14:01 +020031static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
32{
33 switch (sta->bandwidth) {
34 case IEEE80211_STA_RX_BW_20:
35 if (sta->ht_cap.ht_supported)
36 return NL80211_CHAN_WIDTH_20;
37 else
38 return NL80211_CHAN_WIDTH_20_NOHT;
39 case IEEE80211_STA_RX_BW_40:
40 return NL80211_CHAN_WIDTH_40;
41 case IEEE80211_STA_RX_BW_80:
42 return NL80211_CHAN_WIDTH_80;
43 case IEEE80211_STA_RX_BW_160:
44 /*
45 * This applied for both 160 and 80+80. since we use
46 * the returned value to consider degradation of
47 * ctx->conf.min_def, we have to make sure to take
48 * the bigger one (NL80211_CHAN_WIDTH_160).
49 * Otherwise we might try degrading even when not
50 * needed, as the max required sta_bw returned (80+80)
51 * might be smaller than the configured bw (160).
52 */
53 return NL80211_CHAN_WIDTH_160;
54 default:
55 WARN_ON(1);
56 return NL80211_CHAN_WIDTH_20;
57 }
58}
59
60static enum nl80211_chan_width
61ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
62{
63 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
64 struct sta_info *sta;
65
66 rcu_read_lock();
67 list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
68 if (sdata != sta->sdata &&
69 !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
70 continue;
71
72 if (!sta->uploaded)
73 continue;
74
75 max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
76 }
77 rcu_read_unlock();
78
79 return max_bw;
80}
81
82static enum nl80211_chan_width
83ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
84 struct ieee80211_chanctx_conf *conf)
85{
86 struct ieee80211_sub_if_data *sdata;
87 enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
88
89 rcu_read_lock();
90 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
91 struct ieee80211_vif *vif = &sdata->vif;
92 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
93
94 if (!ieee80211_sdata_running(sdata))
95 continue;
96
97 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
98 continue;
99
100 switch (vif->type) {
101 case NL80211_IFTYPE_AP:
102 case NL80211_IFTYPE_AP_VLAN:
103 width = ieee80211_get_max_required_bw(sdata);
104 break;
105 case NL80211_IFTYPE_P2P_DEVICE:
106 continue;
107 case NL80211_IFTYPE_STATION:
108 case NL80211_IFTYPE_ADHOC:
109 case NL80211_IFTYPE_WDS:
110 case NL80211_IFTYPE_MESH_POINT:
111 width = vif->bss_conf.chandef.width;
112 break;
113 case NL80211_IFTYPE_UNSPECIFIED:
114 case NUM_NL80211_IFTYPES:
115 case NL80211_IFTYPE_MONITOR:
116 case NL80211_IFTYPE_P2P_CLIENT:
117 case NL80211_IFTYPE_P2P_GO:
118 WARN_ON_ONCE(1);
119 }
120 max_bw = max(max_bw, width);
121 }
Eliad Peller1c37a722014-03-03 13:37:14 +0200122
123 /* use the configured bandwidth in case of monitor interface */
124 sdata = rcu_dereference(local->monitor_sdata);
125 if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
126 max_bw = max(max_bw, conf->def.width);
127
Eliad Peller21f659b2013-11-11 20:14:01 +0200128 rcu_read_unlock();
129
130 return max_bw;
131}
132
133/*
134 * recalc the min required chan width of the channel context, which is
135 * the max of min required widths of all the interfaces bound to this
136 * channel context.
137 */
138void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
139 struct ieee80211_chanctx *ctx)
140{
141 enum nl80211_chan_width max_bw;
142 struct cfg80211_chan_def min_def;
143
144 lockdep_assert_held(&local->chanctx_mtx);
145
146 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
147 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
148 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
149 ctx->conf.radar_enabled) {
150 ctx->conf.min_def = ctx->conf.def;
151 return;
152 }
153
154 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
155
156 /* downgrade chandef up to max_bw */
157 min_def = ctx->conf.def;
158 while (min_def.width > max_bw)
159 ieee80211_chandef_downgrade(&min_def);
160
161 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
162 return;
163
164 ctx->conf.min_def = min_def;
165 if (!ctx->driver_present)
166 return;
167
168 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
169}
170
Johannes Berg18942d32013-02-07 21:30:37 +0100171static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100172 struct ieee80211_chanctx *ctx,
173 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200174{
Johannes Berg4bf88532012-11-09 11:39:59 +0100175 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +0200176 return;
177
Johannes Berg4bf88532012-11-09 11:39:59 +0100178 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
179
180 ctx->conf.def = *chandef;
181 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Eliad Peller21f659b2013-11-11 20:14:01 +0200182 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200183
184 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100185 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200186 ieee80211_hw_config(local, 0);
187 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200188}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200189
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200190static bool ieee80211_chanctx_is_reserved(struct ieee80211_local *local,
191 struct ieee80211_chanctx *ctx)
192{
193 struct ieee80211_sub_if_data *sdata;
194 bool ret = false;
195
196 lockdep_assert_held(&local->chanctx_mtx);
197 rcu_read_lock();
198 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
199 if (!ieee80211_sdata_running(sdata))
200 continue;
201 if (sdata->reserved_chanctx == ctx) {
202 ret = true;
203 break;
204 }
205 }
206
207 rcu_read_unlock();
208 return ret;
209}
210
Michal Kaziord01a1e62012-06-26 14:37:16 +0200211static struct ieee80211_chanctx *
212ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100213 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200214 enum ieee80211_chanctx_mode mode)
215{
216 struct ieee80211_chanctx *ctx;
217
218 lockdep_assert_held(&local->chanctx_mtx);
219
220 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
221 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200222
223 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100224 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200225
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200226 /* We don't support chanctx reservation for multiple
227 * vifs yet, so don't allow reserved chanctxs to be
228 * reused.
229 */
230 if ((ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) ||
231 ieee80211_chanctx_is_reserved(local, ctx))
Michal Kaziord01a1e62012-06-26 14:37:16 +0200232 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100233
234 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
235 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200236 continue;
237
Johannes Berg18942d32013-02-07 21:30:37 +0100238 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200239
Michal Kaziord01a1e62012-06-26 14:37:16 +0200240 return ctx;
241 }
242
243 return NULL;
244}
245
Simon Wunderliche4746852013-04-08 22:43:16 +0200246static bool ieee80211_is_radar_required(struct ieee80211_local *local)
247{
248 struct ieee80211_sub_if_data *sdata;
249
Michal Kaziorcc901de2014-01-29 07:56:20 +0100250 lockdep_assert_held(&local->mtx);
251
Simon Wunderliche4746852013-04-08 22:43:16 +0200252 rcu_read_lock();
253 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
254 if (sdata->radar_required) {
255 rcu_read_unlock();
256 return true;
257 }
258 }
259 rcu_read_unlock();
260
261 return false;
262}
263
Michal Kaziord01a1e62012-06-26 14:37:16 +0200264static struct ieee80211_chanctx *
265ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100266 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200267 enum ieee80211_chanctx_mode mode)
268{
269 struct ieee80211_chanctx *ctx;
Johannes Berg382a1032013-03-22 22:30:09 +0100270 u32 changed;
Michal Kazior35f2fce2012-06-26 14:37:20 +0200271 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200272
273 lockdep_assert_held(&local->chanctx_mtx);
274
275 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
276 if (!ctx)
277 return ERR_PTR(-ENOMEM);
278
Michal Kazior484298a2014-04-09 15:29:26 +0200279 INIT_LIST_HEAD(&ctx->assigned_vifs);
Johannes Berg4bf88532012-11-09 11:39:59 +0100280 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200281 ctx->conf.rx_chains_static = 1;
282 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200283 ctx->mode = mode;
Simon Wunderliche4746852013-04-08 22:43:16 +0200284 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
Eliad Peller21f659b2013-11-11 20:14:01 +0200285 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderliche4746852013-04-08 22:43:16 +0200286 if (!local->use_chanctx)
287 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200288
Johannes Berg34a37402013-12-18 09:43:33 +0100289 /* we hold the mutex to prevent idle from changing */
290 lockdep_assert_held(&local->mtx);
Johannes Berg382a1032013-03-22 22:30:09 +0100291 /* turn idle off *before* setting channel -- some drivers need that */
292 changed = ieee80211_idle_off(local);
293 if (changed)
294 ieee80211_hw_config(local, changed);
295
Johannes Berg55de9082012-07-26 17:24:39 +0200296 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100297 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200298 ieee80211_hw_config(local, 0);
299 } else {
300 err = drv_add_chanctx(local, ctx);
301 if (err) {
302 kfree(ctx);
Johannes Berg382a1032013-03-22 22:30:09 +0100303 ieee80211_recalc_idle(local);
Johannes Berg34a37402013-12-18 09:43:33 +0100304 return ERR_PTR(err);
Johannes Berg55de9082012-07-26 17:24:39 +0200305 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200306 }
307
Johannes Berg382a1032013-03-22 22:30:09 +0100308 /* and keep the mutex held until the new chanctx is on the list */
Johannes Berg3448c002012-09-11 17:57:42 +0200309 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200310
311 return ctx;
312}
313
314static void ieee80211_free_chanctx(struct ieee80211_local *local,
315 struct ieee80211_chanctx *ctx)
316{
Simon Wunderliche4746852013-04-08 22:43:16 +0200317 bool check_single_channel = false;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200318 lockdep_assert_held(&local->chanctx_mtx);
319
320 WARN_ON_ONCE(ctx->refcount != 0);
321
Johannes Berg55de9082012-07-26 17:24:39 +0200322 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100323 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
324 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
325 chandef->center_freq1 = chandef->chan->center_freq;
326 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200327
328 /* NOTE: Disabling radar is only valid here for
329 * single channel context. To be sure, check it ...
330 */
331 if (local->hw.conf.radar_enabled)
332 check_single_channel = true;
333 local->hw.conf.radar_enabled = false;
334
Johannes Berg55de9082012-07-26 17:24:39 +0200335 ieee80211_hw_config(local, 0);
336 } else {
337 drv_remove_chanctx(local, ctx);
338 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200339
Johannes Berg3448c002012-09-11 17:57:42 +0200340 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200341 kfree_rcu(ctx, rcu_head);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100342
Simon Wunderliche4746852013-04-08 22:43:16 +0200343 /* throw a warning if this wasn't the only channel context. */
344 WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
345
Johannes Bergfd0f9792013-02-07 00:14:51 +0100346 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200347}
348
Johannes Berg4bf88532012-11-09 11:39:59 +0100349static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
350 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200351{
352 struct ieee80211_chanctx_conf *conf = &ctx->conf;
353 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100354 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200355
356 lockdep_assert_held(&local->chanctx_mtx);
357
358 rcu_read_lock();
359 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100360
Michal Kaziore89a96f2012-06-26 14:37:22 +0200361 if (!ieee80211_sdata_running(sdata))
362 continue;
363 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
364 continue;
365
Johannes Berg4bf88532012-11-09 11:39:59 +0100366 if (!compat)
367 compat = &sdata->vif.bss_conf.chandef;
368
369 compat = cfg80211_chandef_compatible(
370 &sdata->vif.bss_conf.chandef, compat);
371 if (!compat)
372 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200373 }
374 rcu_read_unlock();
375
Johannes Berg4bf88532012-11-09 11:39:59 +0100376 if (WARN_ON_ONCE(!compat))
377 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200378
Johannes Berg18942d32013-02-07 21:30:37 +0100379 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200380}
381
Johannes Berg367bbd12013-12-18 09:36:09 +0100382static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
383 struct ieee80211_chanctx *chanctx)
384{
385 bool radar_enabled;
386
387 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100388 /* for setting local->radar_detect_enabled */
389 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100390
391 radar_enabled = ieee80211_is_radar_required(local);
392
393 if (radar_enabled == chanctx->conf.radar_enabled)
394 return;
395
396 chanctx->conf.radar_enabled = radar_enabled;
397 local->radar_detect_enabled = chanctx->conf.radar_enabled;
398
399 if (!local->use_chanctx) {
400 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
401 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
402 }
403
404 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
405}
406
Luciano Coelho77eeba92014-03-11 18:24:12 +0200407static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
408 struct ieee80211_chanctx *new_ctx)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200409{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200410 struct ieee80211_local *local = sdata->local;
Luciano Coelho77eeba92014-03-11 18:24:12 +0200411 struct ieee80211_chanctx_conf *conf;
412 struct ieee80211_chanctx *curr_ctx = NULL;
413 int ret = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200414
Luciano Coelho77eeba92014-03-11 18:24:12 +0200415 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
416 lockdep_is_held(&local->chanctx_mtx));
Michal Kaziord01a1e62012-06-26 14:37:16 +0200417
Luciano Coelho77eeba92014-03-11 18:24:12 +0200418 if (conf) {
419 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200420
Luciano Coelho77eeba92014-03-11 18:24:12 +0200421 curr_ctx->refcount--;
422 drv_unassign_vif_chanctx(local, sdata, curr_ctx);
423 conf = NULL;
Michal Kazior484298a2014-04-09 15:29:26 +0200424 list_del(&sdata->assigned_chanctx_list);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200425 }
426
427 if (new_ctx) {
428 ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
429 if (ret)
430 goto out;
431
432 new_ctx->refcount++;
433 conf = &new_ctx->conf;
Michal Kazior484298a2014-04-09 15:29:26 +0200434 list_add(&sdata->assigned_chanctx_list,
435 &new_ctx->assigned_vifs);
Luciano Coelho77eeba92014-03-11 18:24:12 +0200436 }
437
438out:
439 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
440
441 sdata->vif.bss_conf.idle = !conf;
442
443 if (curr_ctx && curr_ctx->refcount > 0) {
444 ieee80211_recalc_chanctx_chantype(local, curr_ctx);
445 ieee80211_recalc_smps_chanctx(local, curr_ctx);
446 ieee80211_recalc_radar_chanctx(local, curr_ctx);
447 ieee80211_recalc_chanctx_min_def(local, curr_ctx);
448 }
449
450 if (new_ctx && new_ctx->refcount > 0) {
451 ieee80211_recalc_txpower(sdata);
452 ieee80211_recalc_chanctx_min_def(local, new_ctx);
453 }
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100454
455 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
456 sdata->vif.type != NL80211_IFTYPE_MONITOR)
Luciano Coelho77eeba92014-03-11 18:24:12 +0200457 ieee80211_bss_info_change_notify(sdata,
458 BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100459
Luciano Coelho77eeba92014-03-11 18:24:12 +0200460 return ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200461}
462
463static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
464{
465 struct ieee80211_local *local = sdata->local;
466 struct ieee80211_chanctx_conf *conf;
467 struct ieee80211_chanctx *ctx;
468
469 lockdep_assert_held(&local->chanctx_mtx);
470
471 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
472 lockdep_is_held(&local->chanctx_mtx));
473 if (!conf)
474 return;
475
476 ctx = container_of(conf, struct ieee80211_chanctx, conf);
477
Luciano Coelho11335a52013-10-30 13:09:39 +0200478 if (sdata->reserved_chanctx)
479 ieee80211_vif_unreserve_chanctx(sdata);
480
Luciano Coelho77eeba92014-03-11 18:24:12 +0200481 ieee80211_assign_vif_chanctx(sdata, NULL);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200482 if (ctx->refcount == 0)
483 ieee80211_free_chanctx(local, ctx);
484}
485
Johannes Berg04ecd252012-09-11 14:34:12 +0200486void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
487 struct ieee80211_chanctx *chanctx)
488{
489 struct ieee80211_sub_if_data *sdata;
490 u8 rx_chains_static, rx_chains_dynamic;
491
492 lockdep_assert_held(&local->chanctx_mtx);
493
494 rx_chains_static = 1;
495 rx_chains_dynamic = 1;
496
497 rcu_read_lock();
498 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
499 u8 needed_static, needed_dynamic;
500
501 if (!ieee80211_sdata_running(sdata))
502 continue;
503
504 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
505 &chanctx->conf)
506 continue;
507
508 switch (sdata->vif.type) {
509 case NL80211_IFTYPE_P2P_DEVICE:
510 continue;
511 case NL80211_IFTYPE_STATION:
512 if (!sdata->u.mgd.associated)
513 continue;
514 break;
515 case NL80211_IFTYPE_AP_VLAN:
516 continue;
517 case NL80211_IFTYPE_AP:
518 case NL80211_IFTYPE_ADHOC:
519 case NL80211_IFTYPE_WDS:
520 case NL80211_IFTYPE_MESH_POINT:
521 break;
522 default:
523 WARN_ON_ONCE(1);
524 }
525
526 switch (sdata->smps_mode) {
527 default:
528 WARN_ONCE(1, "Invalid SMPS mode %d\n",
529 sdata->smps_mode);
530 /* fall through */
531 case IEEE80211_SMPS_OFF:
532 needed_static = sdata->needed_rx_chains;
533 needed_dynamic = sdata->needed_rx_chains;
534 break;
535 case IEEE80211_SMPS_DYNAMIC:
536 needed_static = 1;
537 needed_dynamic = sdata->needed_rx_chains;
538 break;
539 case IEEE80211_SMPS_STATIC:
540 needed_static = 1;
541 needed_dynamic = 1;
542 break;
543 }
544
545 rx_chains_static = max(rx_chains_static, needed_static);
546 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
547 }
548 rcu_read_unlock();
549
550 if (!local->use_chanctx) {
551 if (rx_chains_static > 1)
552 local->smps_mode = IEEE80211_SMPS_OFF;
553 else if (rx_chains_dynamic > 1)
554 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
555 else
556 local->smps_mode = IEEE80211_SMPS_STATIC;
557 ieee80211_hw_config(local, 0);
558 }
559
560 if (rx_chains_static == chanctx->conf.rx_chains_static &&
561 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
562 return;
563
564 chanctx->conf.rx_chains_static = rx_chains_static;
565 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
566 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
567}
568
Michal Kaziord01a1e62012-06-26 14:37:16 +0200569int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100570 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200571 enum ieee80211_chanctx_mode mode)
572{
573 struct ieee80211_local *local = sdata->local;
574 struct ieee80211_chanctx *ctx;
Luciano Coelho73de86a2014-02-13 11:31:59 +0200575 u8 radar_detect_width = 0;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200576 int ret;
577
Johannes Berg34a37402013-12-18 09:43:33 +0100578 lockdep_assert_held(&local->mtx);
579
Johannes Berg55de9082012-07-26 17:24:39 +0200580 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
581
Michal Kaziord01a1e62012-06-26 14:37:16 +0200582 mutex_lock(&local->chanctx_mtx);
Luciano Coelho73de86a2014-02-13 11:31:59 +0200583
584 ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
585 chandef,
586 sdata->wdev.iftype);
587 if (ret < 0)
588 goto out;
589 if (ret > 0)
590 radar_detect_width = BIT(chandef->width);
591
592 sdata->radar_required = ret;
593
594 ret = ieee80211_check_combinations(sdata, chandef, mode,
595 radar_detect_width);
596 if (ret < 0)
597 goto out;
598
Michal Kaziord01a1e62012-06-26 14:37:16 +0200599 __ieee80211_vif_release_channel(sdata);
600
Johannes Berg4bf88532012-11-09 11:39:59 +0100601 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200602 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100603 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200604 if (IS_ERR(ctx)) {
605 ret = PTR_ERR(ctx);
606 goto out;
607 }
608
Johannes Berg4bf88532012-11-09 11:39:59 +0100609 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200610
Michal Kaziord01a1e62012-06-26 14:37:16 +0200611 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
612 if (ret) {
613 /* if assign fails refcount stays the same */
614 if (ctx->refcount == 0)
615 ieee80211_free_chanctx(local, ctx);
616 goto out;
617 }
618
Johannes Berg04ecd252012-09-11 14:34:12 +0200619 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100620 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200621 out:
622 mutex_unlock(&local->chanctx_mtx);
623 return ret;
624}
625
Luciano Coelho33ffd952014-01-30 22:08:16 +0200626static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
627 struct ieee80211_chanctx *ctx,
628 u32 *changed)
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200629{
630 struct ieee80211_local *local = sdata->local;
Luciano Coelho33787fc2013-11-11 20:34:54 +0200631 const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200632 u32 chanctx_changed = 0;
633
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200634 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
635 IEEE80211_CHAN_DISABLED))
636 return -EINVAL;
637
Luciano Coelho33ffd952014-01-30 22:08:16 +0200638 if (ctx->refcount != 1)
639 return -EINVAL;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200640
641 if (sdata->vif.bss_conf.chandef.width != chandef->width) {
642 chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
643 *changed |= BSS_CHANGED_BANDWIDTH;
644 }
645
646 sdata->vif.bss_conf.chandef = *chandef;
647 ctx->conf.def = *chandef;
648
649 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
650 drv_change_chanctx(local, ctx, chanctx_changed);
651
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200652 ieee80211_recalc_chanctx_chantype(local, ctx);
653 ieee80211_recalc_smps_chanctx(local, ctx);
654 ieee80211_recalc_radar_chanctx(local, ctx);
Eliad Peller21f659b2013-11-11 20:14:01 +0200655 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200656
Luciano Coelho33ffd952014-01-30 22:08:16 +0200657 return 0;
658}
659
660int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
661 u32 *changed)
662{
663 struct ieee80211_local *local = sdata->local;
664 struct ieee80211_chanctx_conf *conf;
665 struct ieee80211_chanctx *ctx;
666 int ret;
667
668 lockdep_assert_held(&local->mtx);
669
670 /* should never be called if not performing a channel switch. */
671 if (WARN_ON(!sdata->vif.csa_active))
672 return -EINVAL;
673
674 mutex_lock(&local->chanctx_mtx);
675 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
676 lockdep_is_held(&local->chanctx_mtx));
677 if (!conf) {
678 ret = -EINVAL;
679 goto out;
680 }
681
682 ctx = container_of(conf, struct ieee80211_chanctx, conf);
683
684 ret = __ieee80211_vif_change_channel(sdata, ctx, changed);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200685 out:
686 mutex_unlock(&local->chanctx_mtx);
687 return ret;
688}
689
Luciano Coelho11335a52013-10-30 13:09:39 +0200690static void
691__ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
692 bool clear)
693{
694 struct ieee80211_local *local = sdata->local;
695 struct ieee80211_sub_if_data *vlan;
696 struct ieee80211_chanctx_conf *conf;
697
698 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
699 return;
700
701 lockdep_assert_held(&local->mtx);
702
703 /* Check that conf exists, even when clearing this function
704 * must be called with the AP's channel context still there
705 * as it would otherwise cause VLANs to have an invalid
706 * channel context pointer for a while, possibly pointing
707 * to a channel context that has already been freed.
708 */
709 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
710 lockdep_is_held(&local->chanctx_mtx));
711 WARN_ON(!conf);
712
713 if (clear)
714 conf = NULL;
715
716 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
717 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
718}
719
720void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
721 bool clear)
722{
723 struct ieee80211_local *local = sdata->local;
724
725 mutex_lock(&local->chanctx_mtx);
726
727 __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
728
729 mutex_unlock(&local->chanctx_mtx);
730}
731
732int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
733{
734 lockdep_assert_held(&sdata->local->chanctx_mtx);
735
736 if (WARN_ON(!sdata->reserved_chanctx))
737 return -EINVAL;
738
739 if (--sdata->reserved_chanctx->refcount == 0)
740 ieee80211_free_chanctx(sdata->local, sdata->reserved_chanctx);
741
742 sdata->reserved_chanctx = NULL;
743
744 return 0;
745}
746
747int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
748 const struct cfg80211_chan_def *chandef,
Michal Kazior09332482014-04-09 15:29:25 +0200749 enum ieee80211_chanctx_mode mode,
750 bool radar_required)
Luciano Coelho11335a52013-10-30 13:09:39 +0200751{
752 struct ieee80211_local *local = sdata->local;
753 struct ieee80211_chanctx_conf *conf;
754 struct ieee80211_chanctx *new_ctx, *curr_ctx;
755 int ret = 0;
756
757 mutex_lock(&local->chanctx_mtx);
758
759 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
760 lockdep_is_held(&local->chanctx_mtx));
761 if (!conf) {
762 ret = -EINVAL;
763 goto out;
764 }
765
766 curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
767
768 /* try to find another context with the chandef we want */
769 new_ctx = ieee80211_find_chanctx(local, chandef, mode);
770 if (!new_ctx) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200771 if (curr_ctx->refcount == 1 &&
772 (local->hw.flags & IEEE80211_HW_CHANGE_RUNNING_CHANCTX)) {
773 /* if we're the only users of the chanctx and
774 * the driver supports changing a running
775 * context, reserve our current context
776 */
777 new_ctx = curr_ctx;
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200778 } else if (ieee80211_can_create_new_chanctx(local)) {
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200779 /* create a new context and reserve it */
780 new_ctx = ieee80211_new_chanctx(local, chandef, mode);
781 if (IS_ERR(new_ctx)) {
782 ret = PTR_ERR(new_ctx);
783 goto out;
784 }
Michal Kaziorc2b90ad2014-04-09 15:29:24 +0200785 } else {
786 ret = -EBUSY;
787 goto out;
Luciano Coelho11335a52013-10-30 13:09:39 +0200788 }
789 }
790
791 new_ctx->refcount++;
792 sdata->reserved_chanctx = new_ctx;
793 sdata->reserved_chandef = *chandef;
Michal Kazior09332482014-04-09 15:29:25 +0200794 sdata->reserved_radar_required = radar_required;
Luciano Coelho11335a52013-10-30 13:09:39 +0200795out:
796 mutex_unlock(&local->chanctx_mtx);
797 return ret;
798}
799
800int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata,
801 u32 *changed)
802{
803 struct ieee80211_local *local = sdata->local;
804 struct ieee80211_chanctx *ctx;
805 struct ieee80211_chanctx *old_ctx;
806 struct ieee80211_chanctx_conf *conf;
807 int ret;
808 u32 tmp_changed = *changed;
809
810 /* TODO: need to recheck if the chandef is usable etc.? */
811
812 lockdep_assert_held(&local->mtx);
813
814 mutex_lock(&local->chanctx_mtx);
815
816 ctx = sdata->reserved_chanctx;
817 if (WARN_ON(!ctx)) {
818 ret = -EINVAL;
819 goto out;
820 }
821
822 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
823 lockdep_is_held(&local->chanctx_mtx));
824 if (!conf) {
825 ret = -EINVAL;
826 goto out;
827 }
828
829 old_ctx = container_of(conf, struct ieee80211_chanctx, conf);
830
831 if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
832 tmp_changed |= BSS_CHANGED_BANDWIDTH;
833
834 sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
835
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200836 /* unref our reservation */
Luciano Coelho11335a52013-10-30 13:09:39 +0200837 ctx->refcount--;
838 sdata->reserved_chanctx = NULL;
Michal Kazior09332482014-04-09 15:29:25 +0200839 sdata->radar_required = sdata->reserved_radar_required;
Luciano Coelho11335a52013-10-30 13:09:39 +0200840
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200841 if (old_ctx == ctx) {
842 /* This is our own context, just change it */
843 ret = __ieee80211_vif_change_channel(sdata, old_ctx,
844 &tmp_changed);
845 if (ret)
846 goto out;
847 } else {
848 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
849 if (old_ctx->refcount == 0)
850 ieee80211_free_chanctx(local, old_ctx);
851 if (ret) {
852 /* if assign fails refcount stays the same */
853 if (ctx->refcount == 0)
854 ieee80211_free_chanctx(local, ctx);
855 goto out;
856 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200857
Luciano Coelho5d52ee82014-02-27 14:33:47 +0200858 if (sdata->vif.type == NL80211_IFTYPE_AP)
859 __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
860 }
Luciano Coelho11335a52013-10-30 13:09:39 +0200861
862 *changed = tmp_changed;
863
864 ieee80211_recalc_chanctx_chantype(local, ctx);
865 ieee80211_recalc_smps_chanctx(local, ctx);
866 ieee80211_recalc_radar_chanctx(local, ctx);
867 ieee80211_recalc_chanctx_min_def(local, ctx);
868out:
869 mutex_unlock(&local->chanctx_mtx);
870 return ret;
871}
872
Johannes Berg2c9b7352013-02-07 21:37:29 +0100873int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
874 const struct cfg80211_chan_def *chandef,
875 u32 *changed)
876{
877 struct ieee80211_local *local = sdata->local;
878 struct ieee80211_chanctx_conf *conf;
879 struct ieee80211_chanctx *ctx;
880 int ret;
881
882 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
883 IEEE80211_CHAN_DISABLED))
884 return -EINVAL;
885
886 mutex_lock(&local->chanctx_mtx);
887 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
888 ret = 0;
889 goto out;
890 }
891
892 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
893 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
894 ret = -EINVAL;
895 goto out;
896 }
897
898 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
899 lockdep_is_held(&local->chanctx_mtx));
900 if (!conf) {
901 ret = -EINVAL;
902 goto out;
903 }
904
905 ctx = container_of(conf, struct ieee80211_chanctx, conf);
906 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
907 ret = -EINVAL;
908 goto out;
909 }
910
911 sdata->vif.bss_conf.chandef = *chandef;
912
913 ieee80211_recalc_chanctx_chantype(local, ctx);
914
915 *changed |= BSS_CHANGED_BANDWIDTH;
916 ret = 0;
917 out:
918 mutex_unlock(&local->chanctx_mtx);
919 return ret;
920}
921
Michal Kaziord01a1e62012-06-26 14:37:16 +0200922void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
923{
Johannes Berg55de9082012-07-26 17:24:39 +0200924 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
925
Johannes Berg34a37402013-12-18 09:43:33 +0100926 lockdep_assert_held(&sdata->local->mtx);
927
Michal Kaziord01a1e62012-06-26 14:37:16 +0200928 mutex_lock(&sdata->local->chanctx_mtx);
929 __ieee80211_vif_release_channel(sdata);
930 mutex_unlock(&sdata->local->chanctx_mtx);
931}
Johannes Berg3448c002012-09-11 17:57:42 +0200932
Johannes Berg4d76d212012-12-11 20:38:41 +0100933void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
934{
935 struct ieee80211_local *local = sdata->local;
936 struct ieee80211_sub_if_data *ap;
937 struct ieee80211_chanctx_conf *conf;
938
939 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
940 return;
941
942 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
943
944 mutex_lock(&local->chanctx_mtx);
945
946 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
947 lockdep_is_held(&local->chanctx_mtx));
948 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
949 mutex_unlock(&local->chanctx_mtx);
950}
951
Johannes Berg3448c002012-09-11 17:57:42 +0200952void ieee80211_iter_chan_contexts_atomic(
953 struct ieee80211_hw *hw,
954 void (*iter)(struct ieee80211_hw *hw,
955 struct ieee80211_chanctx_conf *chanctx_conf,
956 void *data),
957 void *iter_data)
958{
959 struct ieee80211_local *local = hw_to_local(hw);
960 struct ieee80211_chanctx *ctx;
961
962 rcu_read_lock();
963 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +0100964 if (ctx->driver_present)
965 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +0200966 rcu_read_unlock();
967}
968EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);