blob: b5b50762f03e6ca1dbfacb924f77a3712a8cd229 [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
Johannes Berg4bf88532012-11-09 11:39:59 +010012static void ieee80211_change_chandef(struct ieee80211_local *local,
13 struct ieee80211_chanctx *ctx,
14 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +020015{
Johannes Berg4bf88532012-11-09 11:39:59 +010016 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +020017 return;
18
Johannes Berg4bf88532012-11-09 11:39:59 +010019 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
20
21 ctx->conf.def = *chandef;
22 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Johannes Berg55de9082012-07-26 17:24:39 +020023
24 if (!local->use_chanctx) {
Johannes Berg4bf88532012-11-09 11:39:59 +010025 local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
Johannes Berg55de9082012-07-26 17:24:39 +020026 ieee80211_hw_config(local, 0);
27 }
Johannes Berg0aaffa92010-05-05 15:28:27 +020028}
Michal Kaziord01a1e62012-06-26 14:37:16 +020029
30static struct ieee80211_chanctx *
31ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +010032 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +020033 enum ieee80211_chanctx_mode mode)
34{
35 struct ieee80211_chanctx *ctx;
36
37 lockdep_assert_held(&local->chanctx_mtx);
38
39 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
40 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +020041
42 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +010043 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +020044
Michal Kaziord01a1e62012-06-26 14:37:16 +020045 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
46 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +010047
48 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
49 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +020050 continue;
51
Johannes Berg4bf88532012-11-09 11:39:59 +010052 ieee80211_change_chandef(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +020053
Michal Kaziord01a1e62012-06-26 14:37:16 +020054 return ctx;
55 }
56
57 return NULL;
58}
59
60static struct ieee80211_chanctx *
61ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +010062 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +020063 enum ieee80211_chanctx_mode mode)
64{
65 struct ieee80211_chanctx *ctx;
Michal Kazior35f2fce2012-06-26 14:37:20 +020066 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +020067
68 lockdep_assert_held(&local->chanctx_mtx);
69
70 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
71 if (!ctx)
72 return ERR_PTR(-ENOMEM);
73
Johannes Berg4bf88532012-11-09 11:39:59 +010074 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +020075 ctx->conf.rx_chains_static = 1;
76 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +020077 ctx->mode = mode;
78
Johannes Berg55de9082012-07-26 17:24:39 +020079 if (!local->use_chanctx) {
Johannes Berg4bf88532012-11-09 11:39:59 +010080 local->_oper_channel_type =
81 cfg80211_get_chandef_type(chandef);
82 local->_oper_channel = chandef->chan;
Johannes Berg55de9082012-07-26 17:24:39 +020083 ieee80211_hw_config(local, 0);
84 } else {
85 err = drv_add_chanctx(local, ctx);
86 if (err) {
87 kfree(ctx);
88 return ERR_PTR(err);
89 }
Michal Kazior35f2fce2012-06-26 14:37:20 +020090 }
91
Johannes Berg3448c002012-09-11 17:57:42 +020092 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +020093
94 return ctx;
95}
96
97static void ieee80211_free_chanctx(struct ieee80211_local *local,
98 struct ieee80211_chanctx *ctx)
99{
100 lockdep_assert_held(&local->chanctx_mtx);
101
102 WARN_ON_ONCE(ctx->refcount != 0);
103
Johannes Berg55de9082012-07-26 17:24:39 +0200104 if (!local->use_chanctx) {
105 local->_oper_channel_type = NL80211_CHAN_NO_HT;
106 ieee80211_hw_config(local, 0);
107 } else {
108 drv_remove_chanctx(local, ctx);
109 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200110
Johannes Berg3448c002012-09-11 17:57:42 +0200111 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200112 kfree_rcu(ctx, rcu_head);
113}
114
115static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
116 struct ieee80211_chanctx *ctx)
117{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200118 struct ieee80211_local *local = sdata->local;
119 int ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200120
121 lockdep_assert_held(&local->chanctx_mtx);
122
Michal Kazior35f2fce2012-06-26 14:37:20 +0200123 ret = drv_assign_vif_chanctx(local, sdata, ctx);
124 if (ret)
125 return ret;
126
Michal Kaziord01a1e62012-06-26 14:37:16 +0200127 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
128 ctx->refcount++;
129
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200130 ieee80211_recalc_txpower(sdata);
131
Michal Kaziord01a1e62012-06-26 14:37:16 +0200132 return 0;
133}
134
Johannes Berg4bf88532012-11-09 11:39:59 +0100135static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
136 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200137{
138 struct ieee80211_chanctx_conf *conf = &ctx->conf;
139 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100140 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200141
142 lockdep_assert_held(&local->chanctx_mtx);
143
144 rcu_read_lock();
145 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100146
Michal Kaziore89a96f2012-06-26 14:37:22 +0200147 if (!ieee80211_sdata_running(sdata))
148 continue;
149 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
150 continue;
151
Johannes Berg4bf88532012-11-09 11:39:59 +0100152 if (!compat)
153 compat = &sdata->vif.bss_conf.chandef;
154
155 compat = cfg80211_chandef_compatible(
156 &sdata->vif.bss_conf.chandef, compat);
157 if (!compat)
158 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200159 }
160 rcu_read_unlock();
161
Johannes Berg4bf88532012-11-09 11:39:59 +0100162 if (WARN_ON_ONCE(!compat))
163 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200164
Johannes Berg4bf88532012-11-09 11:39:59 +0100165 ieee80211_change_chandef(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200166}
167
Michal Kaziord01a1e62012-06-26 14:37:16 +0200168static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
169 struct ieee80211_chanctx *ctx)
170{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200171 struct ieee80211_local *local = sdata->local;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200172
173 lockdep_assert_held(&local->chanctx_mtx);
174
175 ctx->refcount--;
176 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200177
178 drv_unassign_vif_chanctx(local, sdata, ctx);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200179
Johannes Berg04ecd252012-09-11 14:34:12 +0200180 if (ctx->refcount > 0) {
Michal Kaziore89a96f2012-06-26 14:37:22 +0200181 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
Johannes Berg04ecd252012-09-11 14:34:12 +0200182 ieee80211_recalc_smps_chanctx(local, ctx);
183 }
Michal Kaziord01a1e62012-06-26 14:37:16 +0200184}
185
186static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
187{
188 struct ieee80211_local *local = sdata->local;
189 struct ieee80211_chanctx_conf *conf;
190 struct ieee80211_chanctx *ctx;
191
192 lockdep_assert_held(&local->chanctx_mtx);
193
194 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
195 lockdep_is_held(&local->chanctx_mtx));
196 if (!conf)
197 return;
198
199 ctx = container_of(conf, struct ieee80211_chanctx, conf);
200
201 ieee80211_unassign_vif_chanctx(sdata, ctx);
202 if (ctx->refcount == 0)
203 ieee80211_free_chanctx(local, ctx);
204}
205
Johannes Berg04ecd252012-09-11 14:34:12 +0200206void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
207 struct ieee80211_chanctx *chanctx)
208{
209 struct ieee80211_sub_if_data *sdata;
210 u8 rx_chains_static, rx_chains_dynamic;
211
212 lockdep_assert_held(&local->chanctx_mtx);
213
214 rx_chains_static = 1;
215 rx_chains_dynamic = 1;
216
217 rcu_read_lock();
218 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
219 u8 needed_static, needed_dynamic;
220
221 if (!ieee80211_sdata_running(sdata))
222 continue;
223
224 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
225 &chanctx->conf)
226 continue;
227
228 switch (sdata->vif.type) {
229 case NL80211_IFTYPE_P2P_DEVICE:
230 continue;
231 case NL80211_IFTYPE_STATION:
232 if (!sdata->u.mgd.associated)
233 continue;
234 break;
235 case NL80211_IFTYPE_AP_VLAN:
236 continue;
237 case NL80211_IFTYPE_AP:
238 case NL80211_IFTYPE_ADHOC:
239 case NL80211_IFTYPE_WDS:
240 case NL80211_IFTYPE_MESH_POINT:
241 break;
242 default:
243 WARN_ON_ONCE(1);
244 }
245
246 switch (sdata->smps_mode) {
247 default:
248 WARN_ONCE(1, "Invalid SMPS mode %d\n",
249 sdata->smps_mode);
250 /* fall through */
251 case IEEE80211_SMPS_OFF:
252 needed_static = sdata->needed_rx_chains;
253 needed_dynamic = sdata->needed_rx_chains;
254 break;
255 case IEEE80211_SMPS_DYNAMIC:
256 needed_static = 1;
257 needed_dynamic = sdata->needed_rx_chains;
258 break;
259 case IEEE80211_SMPS_STATIC:
260 needed_static = 1;
261 needed_dynamic = 1;
262 break;
263 }
264
265 rx_chains_static = max(rx_chains_static, needed_static);
266 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
267 }
268 rcu_read_unlock();
269
270 if (!local->use_chanctx) {
271 if (rx_chains_static > 1)
272 local->smps_mode = IEEE80211_SMPS_OFF;
273 else if (rx_chains_dynamic > 1)
274 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
275 else
276 local->smps_mode = IEEE80211_SMPS_STATIC;
277 ieee80211_hw_config(local, 0);
278 }
279
280 if (rx_chains_static == chanctx->conf.rx_chains_static &&
281 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
282 return;
283
284 chanctx->conf.rx_chains_static = rx_chains_static;
285 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
286 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
287}
288
Michal Kaziord01a1e62012-06-26 14:37:16 +0200289int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100290 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200291 enum ieee80211_chanctx_mode mode)
292{
293 struct ieee80211_local *local = sdata->local;
294 struct ieee80211_chanctx *ctx;
295 int ret;
296
Johannes Berg55de9082012-07-26 17:24:39 +0200297 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
298
Michal Kaziord01a1e62012-06-26 14:37:16 +0200299 mutex_lock(&local->chanctx_mtx);
300 __ieee80211_vif_release_channel(sdata);
301
Johannes Berg4bf88532012-11-09 11:39:59 +0100302 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200303 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100304 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200305 if (IS_ERR(ctx)) {
306 ret = PTR_ERR(ctx);
307 goto out;
308 }
309
Johannes Berg4bf88532012-11-09 11:39:59 +0100310 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200311
Michal Kaziord01a1e62012-06-26 14:37:16 +0200312 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
313 if (ret) {
314 /* if assign fails refcount stays the same */
315 if (ctx->refcount == 0)
316 ieee80211_free_chanctx(local, ctx);
317 goto out;
318 }
319
Johannes Berg04ecd252012-09-11 14:34:12 +0200320 ieee80211_recalc_smps_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200321 out:
322 mutex_unlock(&local->chanctx_mtx);
323 return ret;
324}
325
326void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
327{
Johannes Berg55de9082012-07-26 17:24:39 +0200328 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
329
Michal Kaziord01a1e62012-06-26 14:37:16 +0200330 mutex_lock(&sdata->local->chanctx_mtx);
331 __ieee80211_vif_release_channel(sdata);
332 mutex_unlock(&sdata->local->chanctx_mtx);
333}
Johannes Berg3448c002012-09-11 17:57:42 +0200334
Johannes Berg4d76d212012-12-11 20:38:41 +0100335void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
336{
337 struct ieee80211_local *local = sdata->local;
338 struct ieee80211_sub_if_data *ap;
339 struct ieee80211_chanctx_conf *conf;
340
341 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
342 return;
343
344 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
345
346 mutex_lock(&local->chanctx_mtx);
347
348 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
349 lockdep_is_held(&local->chanctx_mtx));
350 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
351 mutex_unlock(&local->chanctx_mtx);
352}
353
Johannes Berg1f4ac5a2013-02-08 12:07:44 +0100354void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
355 bool clear)
356{
357 struct ieee80211_local *local = sdata->local;
358 struct ieee80211_sub_if_data *vlan;
359 struct ieee80211_chanctx_conf *conf;
360
361 ASSERT_RTNL();
362
363 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
364 return;
365
366 mutex_lock(&local->chanctx_mtx);
367
368 /*
369 * Check that conf exists, even when clearing this function
370 * must be called with the AP's channel context still there
371 * as it would otherwise cause VLANs to have an invalid
372 * channel context pointer for a while, possibly pointing
373 * to a channel context that has already been freed.
374 */
375 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
376 lockdep_is_held(&local->chanctx_mtx));
377 WARN_ON(!conf);
378
379 if (clear)
380 conf = NULL;
381
382 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
383 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
384
385 mutex_unlock(&local->chanctx_mtx);
386}
387
Johannes Berg3448c002012-09-11 17:57:42 +0200388void ieee80211_iter_chan_contexts_atomic(
389 struct ieee80211_hw *hw,
390 void (*iter)(struct ieee80211_hw *hw,
391 struct ieee80211_chanctx_conf *chanctx_conf,
392 void *data),
393 void *iter_data)
394{
395 struct ieee80211_local *local = hw_to_local(hw);
396 struct ieee80211_chanctx *ctx;
397
398 rcu_read_lock();
399 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +0100400 if (ctx->driver_present)
401 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +0200402 rcu_read_unlock();
403}
404EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);