blob: 42c659229a090a7c86f6ad42a33df11afd40b6c8 [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 }
103 rcu_read_unlock();
104
105 return max_bw;
106}
107
108/*
109 * recalc the min required chan width of the channel context, which is
110 * the max of min required widths of all the interfaces bound to this
111 * channel context.
112 */
113void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
114 struct ieee80211_chanctx *ctx)
115{
116 enum nl80211_chan_width max_bw;
117 struct cfg80211_chan_def min_def;
118
119 lockdep_assert_held(&local->chanctx_mtx);
120
121 /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
122 if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
123 ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
124 ctx->conf.radar_enabled) {
125 ctx->conf.min_def = ctx->conf.def;
126 return;
127 }
128
129 max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
130
131 /* downgrade chandef up to max_bw */
132 min_def = ctx->conf.def;
133 while (min_def.width > max_bw)
134 ieee80211_chandef_downgrade(&min_def);
135
136 if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
137 return;
138
139 ctx->conf.min_def = min_def;
140 if (!ctx->driver_present)
141 return;
142
143 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
144}
145
Johannes Berg18942d32013-02-07 21:30:37 +0100146static void ieee80211_change_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100147 struct ieee80211_chanctx *ctx,
148 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +0200149{
Johannes Berg4bf88532012-11-09 11:39:59 +0100150 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +0200151 return;
152
Johannes Berg4bf88532012-11-09 11:39:59 +0100153 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
154
155 ctx->conf.def = *chandef;
156 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Eliad Peller21f659b2013-11-11 20:14:01 +0200157 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg55de9082012-07-26 17:24:39 +0200158
159 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100160 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200161 ieee80211_hw_config(local, 0);
162 }
Johannes Berg0aaffa92010-05-05 15:28:27 +0200163}
Michal Kaziord01a1e62012-06-26 14:37:16 +0200164
165static struct ieee80211_chanctx *
166ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100167 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200168 enum ieee80211_chanctx_mode mode)
169{
170 struct ieee80211_chanctx *ctx;
171
172 lockdep_assert_held(&local->chanctx_mtx);
173
174 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
175 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200176
177 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100178 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200179
Michal Kaziord01a1e62012-06-26 14:37:16 +0200180 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
181 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +0100182
183 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
184 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +0200185 continue;
186
Johannes Berg18942d32013-02-07 21:30:37 +0100187 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200188
Michal Kaziord01a1e62012-06-26 14:37:16 +0200189 return ctx;
190 }
191
192 return NULL;
193}
194
Simon Wunderliche4746852013-04-08 22:43:16 +0200195static bool ieee80211_is_radar_required(struct ieee80211_local *local)
196{
197 struct ieee80211_sub_if_data *sdata;
198
Michal Kaziorcc901de2014-01-29 07:56:20 +0100199 lockdep_assert_held(&local->mtx);
200
Simon Wunderliche4746852013-04-08 22:43:16 +0200201 rcu_read_lock();
202 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
203 if (sdata->radar_required) {
204 rcu_read_unlock();
205 return true;
206 }
207 }
208 rcu_read_unlock();
209
210 return false;
211}
212
Michal Kaziord01a1e62012-06-26 14:37:16 +0200213static struct ieee80211_chanctx *
214ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +0100215 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200216 enum ieee80211_chanctx_mode mode)
217{
218 struct ieee80211_chanctx *ctx;
Johannes Berg382a1032013-03-22 22:30:09 +0100219 u32 changed;
Michal Kazior35f2fce2012-06-26 14:37:20 +0200220 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200221
222 lockdep_assert_held(&local->chanctx_mtx);
223
224 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
225 if (!ctx)
226 return ERR_PTR(-ENOMEM);
227
Johannes Berg4bf88532012-11-09 11:39:59 +0100228 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +0200229 ctx->conf.rx_chains_static = 1;
230 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200231 ctx->mode = mode;
Simon Wunderliche4746852013-04-08 22:43:16 +0200232 ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
Eliad Peller21f659b2013-11-11 20:14:01 +0200233 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderliche4746852013-04-08 22:43:16 +0200234 if (!local->use_chanctx)
235 local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200236
Johannes Berg34a37402013-12-18 09:43:33 +0100237 /* we hold the mutex to prevent idle from changing */
238 lockdep_assert_held(&local->mtx);
Johannes Berg382a1032013-03-22 22:30:09 +0100239 /* turn idle off *before* setting channel -- some drivers need that */
240 changed = ieee80211_idle_off(local);
241 if (changed)
242 ieee80211_hw_config(local, changed);
243
Johannes Berg55de9082012-07-26 17:24:39 +0200244 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100245 local->_oper_chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200246 ieee80211_hw_config(local, 0);
247 } else {
248 err = drv_add_chanctx(local, ctx);
249 if (err) {
250 kfree(ctx);
Johannes Berg382a1032013-03-22 22:30:09 +0100251 ieee80211_recalc_idle(local);
Johannes Berg34a37402013-12-18 09:43:33 +0100252 return ERR_PTR(err);
Johannes Berg55de9082012-07-26 17:24:39 +0200253 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200254 }
255
Johannes Berg382a1032013-03-22 22:30:09 +0100256 /* and keep the mutex held until the new chanctx is on the list */
Johannes Berg3448c002012-09-11 17:57:42 +0200257 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200258
259 return ctx;
260}
261
262static void ieee80211_free_chanctx(struct ieee80211_local *local,
263 struct ieee80211_chanctx *ctx)
264{
Simon Wunderliche4746852013-04-08 22:43:16 +0200265 bool check_single_channel = false;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200266 lockdep_assert_held(&local->chanctx_mtx);
267
268 WARN_ON_ONCE(ctx->refcount != 0);
269
Johannes Berg55de9082012-07-26 17:24:39 +0200270 if (!local->use_chanctx) {
Karl Beldan675a0b02013-03-25 16:26:57 +0100271 struct cfg80211_chan_def *chandef = &local->_oper_chandef;
272 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
273 chandef->center_freq1 = chandef->chan->center_freq;
274 chandef->center_freq2 = 0;
Simon Wunderliche4746852013-04-08 22:43:16 +0200275
276 /* NOTE: Disabling radar is only valid here for
277 * single channel context. To be sure, check it ...
278 */
279 if (local->hw.conf.radar_enabled)
280 check_single_channel = true;
281 local->hw.conf.radar_enabled = false;
282
Johannes Berg55de9082012-07-26 17:24:39 +0200283 ieee80211_hw_config(local, 0);
284 } else {
285 drv_remove_chanctx(local, ctx);
286 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200287
Johannes Berg3448c002012-09-11 17:57:42 +0200288 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200289 kfree_rcu(ctx, rcu_head);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100290
Simon Wunderliche4746852013-04-08 22:43:16 +0200291 /* throw a warning if this wasn't the only channel context. */
292 WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
293
Johannes Bergfd0f9792013-02-07 00:14:51 +0100294 ieee80211_recalc_idle(local);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200295}
296
297static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
298 struct ieee80211_chanctx *ctx)
299{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200300 struct ieee80211_local *local = sdata->local;
301 int ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200302
303 lockdep_assert_held(&local->chanctx_mtx);
304
Michal Kazior35f2fce2012-06-26 14:37:20 +0200305 ret = drv_assign_vif_chanctx(local, sdata, ctx);
306 if (ret)
307 return ret;
308
Michal Kaziord01a1e62012-06-26 14:37:16 +0200309 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
310 ctx->refcount++;
311
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200312 ieee80211_recalc_txpower(sdata);
Eliad Peller21f659b2013-11-11 20:14:01 +0200313 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100314 sdata->vif.bss_conf.idle = false;
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100315
316 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
317 sdata->vif.type != NL80211_IFTYPE_MONITOR)
318 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200319
Michal Kaziord01a1e62012-06-26 14:37:16 +0200320 return 0;
321}
322
Johannes Berg4bf88532012-11-09 11:39:59 +0100323static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
324 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200325{
326 struct ieee80211_chanctx_conf *conf = &ctx->conf;
327 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100328 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200329
330 lockdep_assert_held(&local->chanctx_mtx);
331
332 rcu_read_lock();
333 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100334
Michal Kaziore89a96f2012-06-26 14:37:22 +0200335 if (!ieee80211_sdata_running(sdata))
336 continue;
337 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
338 continue;
339
Johannes Berg4bf88532012-11-09 11:39:59 +0100340 if (!compat)
341 compat = &sdata->vif.bss_conf.chandef;
342
343 compat = cfg80211_chandef_compatible(
344 &sdata->vif.bss_conf.chandef, compat);
345 if (!compat)
346 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200347 }
348 rcu_read_unlock();
349
Johannes Berg4bf88532012-11-09 11:39:59 +0100350 if (WARN_ON_ONCE(!compat))
351 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200352
Johannes Berg18942d32013-02-07 21:30:37 +0100353 ieee80211_change_chanctx(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200354}
355
Johannes Berg367bbd12013-12-18 09:36:09 +0100356static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
357 struct ieee80211_chanctx *chanctx)
358{
359 bool radar_enabled;
360
361 lockdep_assert_held(&local->chanctx_mtx);
Johannes Berg34a37402013-12-18 09:43:33 +0100362 /* for setting local->radar_detect_enabled */
363 lockdep_assert_held(&local->mtx);
Johannes Berg367bbd12013-12-18 09:36:09 +0100364
365 radar_enabled = ieee80211_is_radar_required(local);
366
367 if (radar_enabled == chanctx->conf.radar_enabled)
368 return;
369
370 chanctx->conf.radar_enabled = radar_enabled;
371 local->radar_detect_enabled = chanctx->conf.radar_enabled;
372
373 if (!local->use_chanctx) {
374 local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
375 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
376 }
377
378 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
379}
380
Michal Kaziord01a1e62012-06-26 14:37:16 +0200381static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
382 struct ieee80211_chanctx *ctx)
383{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200384 struct ieee80211_local *local = sdata->local;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200385
386 lockdep_assert_held(&local->chanctx_mtx);
387
388 ctx->refcount--;
389 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200390
Johannes Bergfd0f9792013-02-07 00:14:51 +0100391 sdata->vif.bss_conf.idle = true;
Johannes Berg5bbe754d2013-02-13 13:50:51 +0100392
393 if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
394 sdata->vif.type != NL80211_IFTYPE_MONITOR)
395 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100396
Michal Kazior35f2fce2012-06-26 14:37:20 +0200397 drv_unassign_vif_chanctx(local, sdata, ctx);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200398
Johannes Berg04ecd252012-09-11 14:34:12 +0200399 if (ctx->refcount > 0) {
Michal Kaziore89a96f2012-06-26 14:37:22 +0200400 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
Johannes Berg04ecd252012-09-11 14:34:12 +0200401 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100402 ieee80211_recalc_radar_chanctx(local, ctx);
Eliad Peller21f659b2013-11-11 20:14:01 +0200403 ieee80211_recalc_chanctx_min_def(local, ctx);
Johannes Berg04ecd252012-09-11 14:34:12 +0200404 }
Michal Kaziord01a1e62012-06-26 14:37:16 +0200405}
406
407static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
408{
409 struct ieee80211_local *local = sdata->local;
410 struct ieee80211_chanctx_conf *conf;
411 struct ieee80211_chanctx *ctx;
412
413 lockdep_assert_held(&local->chanctx_mtx);
414
415 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
416 lockdep_is_held(&local->chanctx_mtx));
417 if (!conf)
418 return;
419
420 ctx = container_of(conf, struct ieee80211_chanctx, conf);
421
422 ieee80211_unassign_vif_chanctx(sdata, ctx);
423 if (ctx->refcount == 0)
424 ieee80211_free_chanctx(local, ctx);
425}
426
Johannes Berg04ecd252012-09-11 14:34:12 +0200427void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
428 struct ieee80211_chanctx *chanctx)
429{
430 struct ieee80211_sub_if_data *sdata;
431 u8 rx_chains_static, rx_chains_dynamic;
432
433 lockdep_assert_held(&local->chanctx_mtx);
434
435 rx_chains_static = 1;
436 rx_chains_dynamic = 1;
437
438 rcu_read_lock();
439 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
440 u8 needed_static, needed_dynamic;
441
442 if (!ieee80211_sdata_running(sdata))
443 continue;
444
445 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
446 &chanctx->conf)
447 continue;
448
449 switch (sdata->vif.type) {
450 case NL80211_IFTYPE_P2P_DEVICE:
451 continue;
452 case NL80211_IFTYPE_STATION:
453 if (!sdata->u.mgd.associated)
454 continue;
455 break;
456 case NL80211_IFTYPE_AP_VLAN:
457 continue;
458 case NL80211_IFTYPE_AP:
459 case NL80211_IFTYPE_ADHOC:
460 case NL80211_IFTYPE_WDS:
461 case NL80211_IFTYPE_MESH_POINT:
462 break;
463 default:
464 WARN_ON_ONCE(1);
465 }
466
467 switch (sdata->smps_mode) {
468 default:
469 WARN_ONCE(1, "Invalid SMPS mode %d\n",
470 sdata->smps_mode);
471 /* fall through */
472 case IEEE80211_SMPS_OFF:
473 needed_static = sdata->needed_rx_chains;
474 needed_dynamic = sdata->needed_rx_chains;
475 break;
476 case IEEE80211_SMPS_DYNAMIC:
477 needed_static = 1;
478 needed_dynamic = sdata->needed_rx_chains;
479 break;
480 case IEEE80211_SMPS_STATIC:
481 needed_static = 1;
482 needed_dynamic = 1;
483 break;
484 }
485
486 rx_chains_static = max(rx_chains_static, needed_static);
487 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
488 }
489 rcu_read_unlock();
490
491 if (!local->use_chanctx) {
492 if (rx_chains_static > 1)
493 local->smps_mode = IEEE80211_SMPS_OFF;
494 else if (rx_chains_dynamic > 1)
495 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
496 else
497 local->smps_mode = IEEE80211_SMPS_STATIC;
498 ieee80211_hw_config(local, 0);
499 }
500
501 if (rx_chains_static == chanctx->conf.rx_chains_static &&
502 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
503 return;
504
505 chanctx->conf.rx_chains_static = rx_chains_static;
506 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
507 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
508}
509
Michal Kaziord01a1e62012-06-26 14:37:16 +0200510int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100511 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200512 enum ieee80211_chanctx_mode mode)
513{
514 struct ieee80211_local *local = sdata->local;
515 struct ieee80211_chanctx *ctx;
516 int ret;
517
Johannes Berg34a37402013-12-18 09:43:33 +0100518 lockdep_assert_held(&local->mtx);
519
Johannes Berg55de9082012-07-26 17:24:39 +0200520 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
521
Michal Kaziord01a1e62012-06-26 14:37:16 +0200522 mutex_lock(&local->chanctx_mtx);
523 __ieee80211_vif_release_channel(sdata);
524
Johannes Berg4bf88532012-11-09 11:39:59 +0100525 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200526 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100527 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200528 if (IS_ERR(ctx)) {
529 ret = PTR_ERR(ctx);
530 goto out;
531 }
532
Johannes Berg4bf88532012-11-09 11:39:59 +0100533 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200534
Michal Kaziord01a1e62012-06-26 14:37:16 +0200535 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
536 if (ret) {
537 /* if assign fails refcount stays the same */
538 if (ctx->refcount == 0)
539 ieee80211_free_chanctx(local, ctx);
540 goto out;
541 }
542
Johannes Berg04ecd252012-09-11 14:34:12 +0200543 ieee80211_recalc_smps_chanctx(local, ctx);
Simon Wunderlich164eb022013-02-08 18:16:20 +0100544 ieee80211_recalc_radar_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200545 out:
546 mutex_unlock(&local->chanctx_mtx);
547 return ret;
548}
549
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200550int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200551 u32 *changed)
552{
553 struct ieee80211_local *local = sdata->local;
554 struct ieee80211_chanctx_conf *conf;
555 struct ieee80211_chanctx *ctx;
Luciano Coelho33787fc2013-11-11 20:34:54 +0200556 const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200557 int ret;
558 u32 chanctx_changed = 0;
559
Johannes Berg34a37402013-12-18 09:43:33 +0100560 lockdep_assert_held(&local->mtx);
561
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200562 /* should never be called if not performing a channel switch. */
563 if (WARN_ON(!sdata->vif.csa_active))
564 return -EINVAL;
565
566 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
567 IEEE80211_CHAN_DISABLED))
568 return -EINVAL;
569
570 mutex_lock(&local->chanctx_mtx);
571 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
572 lockdep_is_held(&local->chanctx_mtx));
573 if (!conf) {
574 ret = -EINVAL;
575 goto out;
576 }
577
578 ctx = container_of(conf, struct ieee80211_chanctx, conf);
579 if (ctx->refcount != 1) {
580 ret = -EINVAL;
581 goto out;
582 }
583
584 if (sdata->vif.bss_conf.chandef.width != chandef->width) {
585 chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
586 *changed |= BSS_CHANGED_BANDWIDTH;
587 }
588
589 sdata->vif.bss_conf.chandef = *chandef;
590 ctx->conf.def = *chandef;
591
592 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
593 drv_change_chanctx(local, ctx, chanctx_changed);
594
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200595 ieee80211_recalc_chanctx_chantype(local, ctx);
596 ieee80211_recalc_smps_chanctx(local, ctx);
597 ieee80211_recalc_radar_chanctx(local, ctx);
Eliad Peller21f659b2013-11-11 20:14:01 +0200598 ieee80211_recalc_chanctx_min_def(local, ctx);
Simon Wunderlich73da7d52013-07-11 16:09:06 +0200599
600 ret = 0;
601 out:
602 mutex_unlock(&local->chanctx_mtx);
603 return ret;
604}
605
Johannes Berg2c9b7352013-02-07 21:37:29 +0100606int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
607 const struct cfg80211_chan_def *chandef,
608 u32 *changed)
609{
610 struct ieee80211_local *local = sdata->local;
611 struct ieee80211_chanctx_conf *conf;
612 struct ieee80211_chanctx *ctx;
613 int ret;
614
615 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
616 IEEE80211_CHAN_DISABLED))
617 return -EINVAL;
618
619 mutex_lock(&local->chanctx_mtx);
620 if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
621 ret = 0;
622 goto out;
623 }
624
625 if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
626 sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
627 ret = -EINVAL;
628 goto out;
629 }
630
631 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
632 lockdep_is_held(&local->chanctx_mtx));
633 if (!conf) {
634 ret = -EINVAL;
635 goto out;
636 }
637
638 ctx = container_of(conf, struct ieee80211_chanctx, conf);
639 if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
640 ret = -EINVAL;
641 goto out;
642 }
643
644 sdata->vif.bss_conf.chandef = *chandef;
645
646 ieee80211_recalc_chanctx_chantype(local, ctx);
647
648 *changed |= BSS_CHANGED_BANDWIDTH;
649 ret = 0;
650 out:
651 mutex_unlock(&local->chanctx_mtx);
652 return ret;
653}
654
Michal Kaziord01a1e62012-06-26 14:37:16 +0200655void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
656{
Johannes Berg55de9082012-07-26 17:24:39 +0200657 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
658
Johannes Berg34a37402013-12-18 09:43:33 +0100659 lockdep_assert_held(&sdata->local->mtx);
660
Michal Kaziord01a1e62012-06-26 14:37:16 +0200661 mutex_lock(&sdata->local->chanctx_mtx);
662 __ieee80211_vif_release_channel(sdata);
663 mutex_unlock(&sdata->local->chanctx_mtx);
664}
Johannes Berg3448c002012-09-11 17:57:42 +0200665
Johannes Berg4d76d212012-12-11 20:38:41 +0100666void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
667{
668 struct ieee80211_local *local = sdata->local;
669 struct ieee80211_sub_if_data *ap;
670 struct ieee80211_chanctx_conf *conf;
671
672 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
673 return;
674
675 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
676
677 mutex_lock(&local->chanctx_mtx);
678
679 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
680 lockdep_is_held(&local->chanctx_mtx));
681 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
682 mutex_unlock(&local->chanctx_mtx);
683}
684
Johannes Berg1f4ac5a2013-02-08 12:07:44 +0100685void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
686 bool clear)
687{
688 struct ieee80211_local *local = sdata->local;
689 struct ieee80211_sub_if_data *vlan;
690 struct ieee80211_chanctx_conf *conf;
691
692 ASSERT_RTNL();
693
694 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
695 return;
696
697 mutex_lock(&local->chanctx_mtx);
698
699 /*
700 * Check that conf exists, even when clearing this function
701 * must be called with the AP's channel context still there
702 * as it would otherwise cause VLANs to have an invalid
703 * channel context pointer for a while, possibly pointing
704 * to a channel context that has already been freed.
705 */
706 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
707 lockdep_is_held(&local->chanctx_mtx));
708 WARN_ON(!conf);
709
710 if (clear)
711 conf = NULL;
712
713 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
714 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
715
716 mutex_unlock(&local->chanctx_mtx);
717}
718
Johannes Berg3448c002012-09-11 17:57:42 +0200719void ieee80211_iter_chan_contexts_atomic(
720 struct ieee80211_hw *hw,
721 void (*iter)(struct ieee80211_hw *hw,
722 struct ieee80211_chanctx_conf *chanctx_conf,
723 void *data),
724 void *iter_data)
725{
726 struct ieee80211_local *local = hw_to_local(hw);
727 struct ieee80211_chanctx *ctx;
728
729 rcu_read_lock();
730 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +0100731 if (ctx->driver_present)
732 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +0200733 rcu_read_unlock();
734}
735EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);