blob: 27f9ae56f96c53fae560d23a965d6c5365e4bae3 [file] [log] [blame]
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -08001/*
2 * Copyright (c) 2009 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/*
18 * Module for common driver code between ath9k and ath9k_htc
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23
24#include "common.h"
25
26MODULE_AUTHOR("Atheros Communications");
27MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards.");
28MODULE_LICENSE("Dual BSD/GPL");
29
Benoit Papillault1bc14882009-11-24 15:49:18 +010030int ath9k_cmn_padpos(__le16 frame_control)
31{
32 int padpos = 24;
33 if (ieee80211_has_a4(frame_control)) {
34 padpos += ETH_ALEN;
35 }
36 if (ieee80211_is_data_qos(frame_control)) {
37 padpos += IEEE80211_QOS_CTL_LEN;
38 }
39
40 return padpos;
41}
42EXPORT_SYMBOL(ath9k_cmn_padpos);
43
Sujithfb9987d2010-03-17 14:25:25 +053044int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
45{
46 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
47
48 if (tx_info->control.hw_key) {
49 if (tx_info->control.hw_key->alg == ALG_WEP)
50 return ATH9K_KEY_TYPE_WEP;
51 else if (tx_info->control.hw_key->alg == ALG_TKIP)
52 return ATH9K_KEY_TYPE_TKIP;
53 else if (tx_info->control.hw_key->alg == ALG_CCMP)
54 return ATH9K_KEY_TYPE_AES;
55 }
56
57 return ATH9K_KEY_TYPE_CLEAR;
58}
59EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
60
Sujithfb9987d2010-03-17 14:25:25 +053061static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
62 enum nl80211_channel_type channel_type)
63{
64 u32 chanmode = 0;
65
66 switch (chan->band) {
67 case IEEE80211_BAND_2GHZ:
68 switch (channel_type) {
69 case NL80211_CHAN_NO_HT:
70 case NL80211_CHAN_HT20:
71 chanmode = CHANNEL_G_HT20;
72 break;
73 case NL80211_CHAN_HT40PLUS:
74 chanmode = CHANNEL_G_HT40PLUS;
75 break;
76 case NL80211_CHAN_HT40MINUS:
77 chanmode = CHANNEL_G_HT40MINUS;
78 break;
79 }
80 break;
81 case IEEE80211_BAND_5GHZ:
82 switch (channel_type) {
83 case NL80211_CHAN_NO_HT:
84 case NL80211_CHAN_HT20:
85 chanmode = CHANNEL_A_HT20;
86 break;
87 case NL80211_CHAN_HT40PLUS:
88 chanmode = CHANNEL_A_HT40PLUS;
89 break;
90 case NL80211_CHAN_HT40MINUS:
91 chanmode = CHANNEL_A_HT40MINUS;
92 break;
93 }
94 break;
95 default:
96 break;
97 }
98
99 return chanmode;
100}
101
102/*
103 * Update internal channel flags.
104 */
105void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
106 struct ath9k_channel *ichan)
107{
108 struct ieee80211_channel *chan = hw->conf.channel;
109 struct ieee80211_conf *conf = &hw->conf;
110
111 ichan->channel = chan->center_freq;
112 ichan->chan = chan;
113
114 if (chan->band == IEEE80211_BAND_2GHZ) {
115 ichan->chanmode = CHANNEL_G;
116 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
117 } else {
118 ichan->chanmode = CHANNEL_A;
119 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
120 }
121
122 if (conf_is_ht(conf))
123 ichan->chanmode = ath9k_get_extchanmode(chan,
124 conf->channel_type);
125}
126EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
127
128/*
129 * Get the internal channel reference.
130 */
131struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
132 struct ath_hw *ah)
133{
134 struct ieee80211_channel *curchan = hw->conf.channel;
135 struct ath9k_channel *channel;
136 u8 chan_idx;
137
138 chan_idx = curchan->hw_value;
139 channel = &ah->channels[chan_idx];
140 ath9k_cmn_update_ichannel(hw, channel);
141
142 return channel;
143}
144EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
145
146static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
147 struct ath9k_keyval *hk, const u8 *addr,
148 bool authenticator)
149{
150 struct ath_hw *ah = common->ah;
151 const u8 *key_rxmic;
152 const u8 *key_txmic;
153
154 key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
155 key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
156
157 if (addr == NULL) {
158 /*
159 * Group key installation - only two key cache entries are used
160 * regardless of splitmic capability since group key is only
161 * used either for TX or RX.
162 */
163 if (authenticator) {
164 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
165 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
166 } else {
167 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
168 memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
169 }
170 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
171 }
172 if (!common->splitmic) {
173 /* TX and RX keys share the same key cache entry. */
174 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
175 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
176 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
177 }
178
179 /* Separate key cache entries for TX and RX */
180
181 /* TX key goes at first index, RX key at +32. */
182 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
183 if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
184 /* TX MIC entry failed. No need to proceed further */
185 ath_print(common, ATH_DBG_FATAL,
186 "Setting TX MIC Key Failed\n");
187 return 0;
188 }
189
190 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
191 /* XXX delete tx key on failure? */
192 return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
193}
194
195static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
196{
197 int i;
198
199 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
200 if (test_bit(i, common->keymap) ||
201 test_bit(i + 64, common->keymap))
202 continue; /* At least one part of TKIP key allocated */
203 if (common->splitmic &&
204 (test_bit(i + 32, common->keymap) ||
205 test_bit(i + 64 + 32, common->keymap)))
206 continue; /* At least one part of TKIP key allocated */
207
208 /* Found a free slot for a TKIP key */
209 return i;
210 }
211 return -1;
212}
213
214static int ath_reserve_key_cache_slot(struct ath_common *common)
215{
216 int i;
217
218 /* First, try to find slots that would not be available for TKIP. */
219 if (common->splitmic) {
220 for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
221 if (!test_bit(i, common->keymap) &&
222 (test_bit(i + 32, common->keymap) ||
223 test_bit(i + 64, common->keymap) ||
224 test_bit(i + 64 + 32, common->keymap)))
225 return i;
226 if (!test_bit(i + 32, common->keymap) &&
227 (test_bit(i, common->keymap) ||
228 test_bit(i + 64, common->keymap) ||
229 test_bit(i + 64 + 32, common->keymap)))
230 return i + 32;
231 if (!test_bit(i + 64, common->keymap) &&
232 (test_bit(i , common->keymap) ||
233 test_bit(i + 32, common->keymap) ||
234 test_bit(i + 64 + 32, common->keymap)))
235 return i + 64;
236 if (!test_bit(i + 64 + 32, common->keymap) &&
237 (test_bit(i, common->keymap) ||
238 test_bit(i + 32, common->keymap) ||
239 test_bit(i + 64, common->keymap)))
240 return i + 64 + 32;
241 }
242 } else {
243 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
244 if (!test_bit(i, common->keymap) &&
245 test_bit(i + 64, common->keymap))
246 return i;
247 if (test_bit(i, common->keymap) &&
248 !test_bit(i + 64, common->keymap))
249 return i + 64;
250 }
251 }
252
253 /* No partially used TKIP slots, pick any available slot */
254 for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
255 /* Do not allow slots that could be needed for TKIP group keys
256 * to be used. This limitation could be removed if we know that
257 * TKIP will not be used. */
258 if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
259 continue;
260 if (common->splitmic) {
261 if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
262 continue;
263 if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
264 continue;
265 }
266
267 if (!test_bit(i, common->keymap))
268 return i; /* Found a free slot for a key */
269 }
270
271 /* No free slot found */
272 return -1;
273}
274
275/*
276 * Configure encryption in the HW.
277 */
278int ath9k_cmn_key_config(struct ath_common *common,
279 struct ieee80211_vif *vif,
280 struct ieee80211_sta *sta,
281 struct ieee80211_key_conf *key)
282{
283 struct ath_hw *ah = common->ah;
284 struct ath9k_keyval hk;
285 const u8 *mac = NULL;
286 int ret = 0;
287 int idx;
288
289 memset(&hk, 0, sizeof(hk));
290
291 switch (key->alg) {
292 case ALG_WEP:
293 hk.kv_type = ATH9K_CIPHER_WEP;
294 break;
295 case ALG_TKIP:
296 hk.kv_type = ATH9K_CIPHER_TKIP;
297 break;
298 case ALG_CCMP:
299 hk.kv_type = ATH9K_CIPHER_AES_CCM;
300 break;
301 default:
302 return -EOPNOTSUPP;
303 }
304
305 hk.kv_len = key->keylen;
306 memcpy(hk.kv_val, key->key, key->keylen);
307
308 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
309 /* For now, use the default keys for broadcast keys. This may
310 * need to change with virtual interfaces. */
311 idx = key->keyidx;
312 } else if (key->keyidx) {
313 if (WARN_ON(!sta))
314 return -EOPNOTSUPP;
315 mac = sta->addr;
316
317 if (vif->type != NL80211_IFTYPE_AP) {
318 /* Only keyidx 0 should be used with unicast key, but
319 * allow this for client mode for now. */
320 idx = key->keyidx;
321 } else
322 return -EIO;
323 } else {
324 if (WARN_ON(!sta))
325 return -EOPNOTSUPP;
326 mac = sta->addr;
327
328 if (key->alg == ALG_TKIP)
329 idx = ath_reserve_key_cache_slot_tkip(common);
330 else
331 idx = ath_reserve_key_cache_slot(common);
332 if (idx < 0)
333 return -ENOSPC; /* no free key cache entries */
334 }
335
336 if (key->alg == ALG_TKIP)
337 ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
338 vif->type == NL80211_IFTYPE_AP);
339 else
340 ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
341
342 if (!ret)
343 return -EIO;
344
345 set_bit(idx, common->keymap);
346 if (key->alg == ALG_TKIP) {
347 set_bit(idx + 64, common->keymap);
348 if (common->splitmic) {
349 set_bit(idx + 32, common->keymap);
350 set_bit(idx + 64 + 32, common->keymap);
351 }
352 }
353
354 return idx;
355}
356EXPORT_SYMBOL(ath9k_cmn_key_config);
357
358/*
359 * Delete Key.
360 */
361void ath9k_cmn_key_delete(struct ath_common *common,
362 struct ieee80211_key_conf *key)
363{
364 struct ath_hw *ah = common->ah;
365
366 ath9k_hw_keyreset(ah, key->hw_key_idx);
367 if (key->hw_key_idx < IEEE80211_WEP_NKID)
368 return;
369
370 clear_bit(key->hw_key_idx, common->keymap);
371 if (key->alg != ALG_TKIP)
372 return;
373
374 clear_bit(key->hw_key_idx + 64, common->keymap);
375 if (common->splitmic) {
376 ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
377 clear_bit(key->hw_key_idx + 32, common->keymap);
378 clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
379 }
380}
381EXPORT_SYMBOL(ath9k_cmn_key_delete);
382
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -0800383static int __init ath9k_cmn_init(void)
384{
385 return 0;
386}
387module_init(ath9k_cmn_init);
388
389static void __exit ath9k_cmn_exit(void)
390{
391 return;
392}
393module_exit(ath9k_cmn_exit);