blob: 53f03120db55dc3d2c1e8c775a054182d21dac69 [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>
Paul Stewart3117bbdb2012-03-13 07:46:18 -07007#include <net/cfg80211.h>
Johannes Bergf444de02010-05-05 15:25:02 +02008#include "ieee80211_i.h"
Michal Kazior35f2fce2012-06-26 14:37:20 +02009#include "driver-ops.h"
Johannes Bergf444de02010-05-05 15:25:02 +020010
Johannes Berg4bf88532012-11-09 11:39:59 +010011static void ieee80211_change_chandef(struct ieee80211_local *local,
12 struct ieee80211_chanctx *ctx,
13 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +020014{
Johannes Berg4bf88532012-11-09 11:39:59 +010015 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +020016 return;
17
Johannes Berg4bf88532012-11-09 11:39:59 +010018 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
19
20 ctx->conf.def = *chandef;
21 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Johannes Berg55de9082012-07-26 17:24:39 +020022
23 if (!local->use_chanctx) {
Johannes Berg4bf88532012-11-09 11:39:59 +010024 local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
Johannes Berg55de9082012-07-26 17:24:39 +020025 ieee80211_hw_config(local, 0);
26 }
Johannes Berg0aaffa92010-05-05 15:28:27 +020027}
Michal Kaziord01a1e62012-06-26 14:37:16 +020028
29static struct ieee80211_chanctx *
30ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +010031 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +020032 enum ieee80211_chanctx_mode mode)
33{
34 struct ieee80211_chanctx *ctx;
35
36 lockdep_assert_held(&local->chanctx_mtx);
37
38 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
39 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +020040
41 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +010042 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +020043
Michal Kaziord01a1e62012-06-26 14:37:16 +020044 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
45 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +010046
47 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
48 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +020049 continue;
50
Johannes Berg4bf88532012-11-09 11:39:59 +010051 ieee80211_change_chandef(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +020052
Michal Kaziord01a1e62012-06-26 14:37:16 +020053 return ctx;
54 }
55
56 return NULL;
57}
58
59static struct ieee80211_chanctx *
60ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +010061 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +020062 enum ieee80211_chanctx_mode mode)
63{
64 struct ieee80211_chanctx *ctx;
Michal Kazior35f2fce2012-06-26 14:37:20 +020065 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +020066
67 lockdep_assert_held(&local->chanctx_mtx);
68
69 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
70 if (!ctx)
71 return ERR_PTR(-ENOMEM);
72
Johannes Berg4bf88532012-11-09 11:39:59 +010073 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +020074 ctx->conf.rx_chains_static = 1;
75 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +020076 ctx->mode = mode;
77
Johannes Berg55de9082012-07-26 17:24:39 +020078 if (!local->use_chanctx) {
Johannes Berg4bf88532012-11-09 11:39:59 +010079 local->_oper_channel_type =
80 cfg80211_get_chandef_type(chandef);
81 local->_oper_channel = chandef->chan;
Johannes Berg55de9082012-07-26 17:24:39 +020082 ieee80211_hw_config(local, 0);
83 } else {
84 err = drv_add_chanctx(local, ctx);
85 if (err) {
86 kfree(ctx);
87 return ERR_PTR(err);
88 }
Michal Kazior35f2fce2012-06-26 14:37:20 +020089 }
90
Johannes Berg3448c002012-09-11 17:57:42 +020091 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +020092
93 return ctx;
94}
95
96static void ieee80211_free_chanctx(struct ieee80211_local *local,
97 struct ieee80211_chanctx *ctx)
98{
99 lockdep_assert_held(&local->chanctx_mtx);
100
101 WARN_ON_ONCE(ctx->refcount != 0);
102
Johannes Berg55de9082012-07-26 17:24:39 +0200103 if (!local->use_chanctx) {
104 local->_oper_channel_type = NL80211_CHAN_NO_HT;
105 ieee80211_hw_config(local, 0);
106 } else {
107 drv_remove_chanctx(local, ctx);
108 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200109
Johannes Berg3448c002012-09-11 17:57:42 +0200110 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200111 kfree_rcu(ctx, rcu_head);
112}
113
114static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
115 struct ieee80211_chanctx *ctx)
116{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200117 struct ieee80211_local *local = sdata->local;
118 int ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200119
120 lockdep_assert_held(&local->chanctx_mtx);
121
Michal Kazior35f2fce2012-06-26 14:37:20 +0200122 ret = drv_assign_vif_chanctx(local, sdata, ctx);
123 if (ret)
124 return ret;
125
Michal Kaziord01a1e62012-06-26 14:37:16 +0200126 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
127 ctx->refcount++;
128
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200129 ieee80211_recalc_txpower(sdata);
130
Michal Kaziord01a1e62012-06-26 14:37:16 +0200131 return 0;
132}
133
Johannes Berg4bf88532012-11-09 11:39:59 +0100134static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
135 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200136{
137 struct ieee80211_chanctx_conf *conf = &ctx->conf;
138 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100139 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200140
141 lockdep_assert_held(&local->chanctx_mtx);
142
143 rcu_read_lock();
144 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100145
Michal Kaziore89a96f2012-06-26 14:37:22 +0200146 if (!ieee80211_sdata_running(sdata))
147 continue;
148 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
149 continue;
150
Johannes Berg4bf88532012-11-09 11:39:59 +0100151 if (!compat)
152 compat = &sdata->vif.bss_conf.chandef;
153
154 compat = cfg80211_chandef_compatible(
155 &sdata->vif.bss_conf.chandef, compat);
156 if (!compat)
157 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200158 }
159 rcu_read_unlock();
160
Johannes Berg4bf88532012-11-09 11:39:59 +0100161 if (WARN_ON_ONCE(!compat))
162 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200163
Johannes Berg4bf88532012-11-09 11:39:59 +0100164 ieee80211_change_chandef(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200165}
166
Michal Kaziord01a1e62012-06-26 14:37:16 +0200167static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
168 struct ieee80211_chanctx *ctx)
169{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200170 struct ieee80211_local *local = sdata->local;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200171
172 lockdep_assert_held(&local->chanctx_mtx);
173
174 ctx->refcount--;
175 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200176
177 drv_unassign_vif_chanctx(local, sdata, ctx);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200178
Johannes Berg04ecd252012-09-11 14:34:12 +0200179 if (ctx->refcount > 0) {
Michal Kaziore89a96f2012-06-26 14:37:22 +0200180 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
Johannes Berg04ecd252012-09-11 14:34:12 +0200181 ieee80211_recalc_smps_chanctx(local, ctx);
182 }
Michal Kaziord01a1e62012-06-26 14:37:16 +0200183}
184
185static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
186{
187 struct ieee80211_local *local = sdata->local;
188 struct ieee80211_chanctx_conf *conf;
189 struct ieee80211_chanctx *ctx;
190
191 lockdep_assert_held(&local->chanctx_mtx);
192
193 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
194 lockdep_is_held(&local->chanctx_mtx));
195 if (!conf)
196 return;
197
198 ctx = container_of(conf, struct ieee80211_chanctx, conf);
199
200 ieee80211_unassign_vif_chanctx(sdata, ctx);
201 if (ctx->refcount == 0)
202 ieee80211_free_chanctx(local, ctx);
203}
204
Johannes Berg04ecd252012-09-11 14:34:12 +0200205void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
206 struct ieee80211_chanctx *chanctx)
207{
208 struct ieee80211_sub_if_data *sdata;
209 u8 rx_chains_static, rx_chains_dynamic;
210
211 lockdep_assert_held(&local->chanctx_mtx);
212
213 rx_chains_static = 1;
214 rx_chains_dynamic = 1;
215
216 rcu_read_lock();
217 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
218 u8 needed_static, needed_dynamic;
219
220 if (!ieee80211_sdata_running(sdata))
221 continue;
222
223 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
224 &chanctx->conf)
225 continue;
226
227 switch (sdata->vif.type) {
228 case NL80211_IFTYPE_P2P_DEVICE:
229 continue;
230 case NL80211_IFTYPE_STATION:
231 if (!sdata->u.mgd.associated)
232 continue;
233 break;
234 case NL80211_IFTYPE_AP_VLAN:
235 continue;
236 case NL80211_IFTYPE_AP:
237 case NL80211_IFTYPE_ADHOC:
238 case NL80211_IFTYPE_WDS:
239 case NL80211_IFTYPE_MESH_POINT:
240 break;
241 default:
242 WARN_ON_ONCE(1);
243 }
244
245 switch (sdata->smps_mode) {
246 default:
247 WARN_ONCE(1, "Invalid SMPS mode %d\n",
248 sdata->smps_mode);
249 /* fall through */
250 case IEEE80211_SMPS_OFF:
251 needed_static = sdata->needed_rx_chains;
252 needed_dynamic = sdata->needed_rx_chains;
253 break;
254 case IEEE80211_SMPS_DYNAMIC:
255 needed_static = 1;
256 needed_dynamic = sdata->needed_rx_chains;
257 break;
258 case IEEE80211_SMPS_STATIC:
259 needed_static = 1;
260 needed_dynamic = 1;
261 break;
262 }
263
264 rx_chains_static = max(rx_chains_static, needed_static);
265 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
266 }
267 rcu_read_unlock();
268
269 if (!local->use_chanctx) {
270 if (rx_chains_static > 1)
271 local->smps_mode = IEEE80211_SMPS_OFF;
272 else if (rx_chains_dynamic > 1)
273 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
274 else
275 local->smps_mode = IEEE80211_SMPS_STATIC;
276 ieee80211_hw_config(local, 0);
277 }
278
279 if (rx_chains_static == chanctx->conf.rx_chains_static &&
280 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
281 return;
282
283 chanctx->conf.rx_chains_static = rx_chains_static;
284 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
285 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
286}
287
Michal Kaziord01a1e62012-06-26 14:37:16 +0200288int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100289 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200290 enum ieee80211_chanctx_mode mode)
291{
292 struct ieee80211_local *local = sdata->local;
293 struct ieee80211_chanctx *ctx;
294 int ret;
295
Johannes Berg55de9082012-07-26 17:24:39 +0200296 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
297
Michal Kaziord01a1e62012-06-26 14:37:16 +0200298 mutex_lock(&local->chanctx_mtx);
299 __ieee80211_vif_release_channel(sdata);
300
Johannes Berg4bf88532012-11-09 11:39:59 +0100301 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200302 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100303 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200304 if (IS_ERR(ctx)) {
305 ret = PTR_ERR(ctx);
306 goto out;
307 }
308
Johannes Berg4bf88532012-11-09 11:39:59 +0100309 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200310
Michal Kaziord01a1e62012-06-26 14:37:16 +0200311 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
312 if (ret) {
313 /* if assign fails refcount stays the same */
314 if (ctx->refcount == 0)
315 ieee80211_free_chanctx(local, ctx);
316 goto out;
317 }
318
Johannes Berg04ecd252012-09-11 14:34:12 +0200319 ieee80211_recalc_smps_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200320 out:
321 mutex_unlock(&local->chanctx_mtx);
322 return ret;
323}
324
325void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
326{
Johannes Berg55de9082012-07-26 17:24:39 +0200327 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
328
Michal Kaziord01a1e62012-06-26 14:37:16 +0200329 mutex_lock(&sdata->local->chanctx_mtx);
330 __ieee80211_vif_release_channel(sdata);
331 mutex_unlock(&sdata->local->chanctx_mtx);
332}
Johannes Berg3448c002012-09-11 17:57:42 +0200333
334void ieee80211_iter_chan_contexts_atomic(
335 struct ieee80211_hw *hw,
336 void (*iter)(struct ieee80211_hw *hw,
337 struct ieee80211_chanctx_conf *chanctx_conf,
338 void *data),
339 void *iter_data)
340{
341 struct ieee80211_local *local = hw_to_local(hw);
342 struct ieee80211_chanctx *ctx;
343
344 rcu_read_lock();
345 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
346 iter(hw, &ctx->conf, iter_data);
347 rcu_read_unlock();
348}
349EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);