2 * Copyright (c) 2009 Atheros Communications Inc.
3 * Copyright (c) 2010 Bruno Randolf <br1@einfach.org>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <asm/unaligned.h>
19 #include <net/mac80211.h>
24 #define REG_READ (common->ops->read)
25 #define REG_WRITE(_ah, _reg, _val) (common->ops->write)(_ah, _val, _reg)
27 #define IEEE80211_WEP_NKID 4 /* number of key ids */
29 /************************/
30 /* Key Cache Management */
31 /************************/
33 bool ath_hw_keyreset(struct ath_common
*common
, u16 entry
)
36 void *ah
= common
->ah
;
38 if (entry
>= common
->keymax
) {
39 ath_err(common
, "keycache entry %u out of range\n", entry
);
43 keyType
= REG_READ(ah
, AR_KEYTABLE_TYPE(entry
));
45 REG_WRITE(ah
, AR_KEYTABLE_KEY0(entry
), 0);
46 REG_WRITE(ah
, AR_KEYTABLE_KEY1(entry
), 0);
47 REG_WRITE(ah
, AR_KEYTABLE_KEY2(entry
), 0);
48 REG_WRITE(ah
, AR_KEYTABLE_KEY3(entry
), 0);
49 REG_WRITE(ah
, AR_KEYTABLE_KEY4(entry
), 0);
50 REG_WRITE(ah
, AR_KEYTABLE_TYPE(entry
), AR_KEYTABLE_TYPE_CLR
);
51 REG_WRITE(ah
, AR_KEYTABLE_MAC0(entry
), 0);
52 REG_WRITE(ah
, AR_KEYTABLE_MAC1(entry
), 0);
54 if (keyType
== AR_KEYTABLE_TYPE_TKIP
) {
55 u16 micentry
= entry
+ 64;
57 REG_WRITE(ah
, AR_KEYTABLE_KEY0(micentry
), 0);
58 REG_WRITE(ah
, AR_KEYTABLE_KEY1(micentry
), 0);
59 REG_WRITE(ah
, AR_KEYTABLE_KEY2(micentry
), 0);
60 REG_WRITE(ah
, AR_KEYTABLE_KEY3(micentry
), 0);
61 if (common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
) {
62 REG_WRITE(ah
, AR_KEYTABLE_KEY4(micentry
), 0);
63 REG_WRITE(ah
, AR_KEYTABLE_TYPE(micentry
),
64 AR_KEYTABLE_TYPE_CLR
);
71 EXPORT_SYMBOL(ath_hw_keyreset
);
73 static bool ath_hw_keysetmac(struct ath_common
*common
,
74 u16 entry
, const u8
*mac
)
77 u32 unicast_flag
= AR_KEYTABLE_VALID
;
78 void *ah
= common
->ah
;
80 if (entry
>= common
->keymax
) {
81 ath_err(common
, "keycache entry %u out of range\n", entry
);
87 * AR_KEYTABLE_VALID indicates that the address is a unicast
88 * address, which must match the transmitter address for
90 * Not setting this bit allows the hardware to use the key
91 * for multicast frame decryption.
96 macHi
= (mac
[5] << 8) | mac
[4];
97 macLo
= (mac
[3] << 24) |
102 macLo
|= (macHi
& 1) << 31;
107 REG_WRITE(ah
, AR_KEYTABLE_MAC0(entry
), macLo
);
108 REG_WRITE(ah
, AR_KEYTABLE_MAC1(entry
), macHi
| unicast_flag
);
113 static bool ath_hw_set_keycache_entry(struct ath_common
*common
, u16 entry
,
114 const struct ath_keyval
*k
,
117 void *ah
= common
->ah
;
118 u32 key0
, key1
, key2
, key3
, key4
;
121 if (entry
>= common
->keymax
) {
122 ath_err(common
, "keycache entry %u out of range\n", entry
);
126 switch (k
->kv_type
) {
127 case ATH_CIPHER_AES_OCB
:
128 keyType
= AR_KEYTABLE_TYPE_AES
;
130 case ATH_CIPHER_AES_CCM
:
131 if (!(common
->crypt_caps
& ATH_CRYPT_CAP_CIPHER_AESCCM
)) {
132 ath_dbg(common
, ATH_DBG_ANY
,
133 "AES-CCM not supported by this mac rev\n");
136 keyType
= AR_KEYTABLE_TYPE_CCM
;
138 case ATH_CIPHER_TKIP
:
139 keyType
= AR_KEYTABLE_TYPE_TKIP
;
140 if (entry
+ 64 >= common
->keymax
) {
141 ath_dbg(common
, ATH_DBG_ANY
,
142 "entry %u inappropriate for TKIP\n", entry
);
147 if (k
->kv_len
< WLAN_KEY_LEN_WEP40
) {
148 ath_dbg(common
, ATH_DBG_ANY
,
149 "WEP key length %u too small\n", k
->kv_len
);
152 if (k
->kv_len
<= WLAN_KEY_LEN_WEP40
)
153 keyType
= AR_KEYTABLE_TYPE_40
;
154 else if (k
->kv_len
<= WLAN_KEY_LEN_WEP104
)
155 keyType
= AR_KEYTABLE_TYPE_104
;
157 keyType
= AR_KEYTABLE_TYPE_128
;
160 keyType
= AR_KEYTABLE_TYPE_CLR
;
163 ath_err(common
, "cipher %u not supported\n", k
->kv_type
);
167 key0
= get_unaligned_le32(k
->kv_val
+ 0);
168 key1
= get_unaligned_le16(k
->kv_val
+ 4);
169 key2
= get_unaligned_le32(k
->kv_val
+ 6);
170 key3
= get_unaligned_le16(k
->kv_val
+ 10);
171 key4
= get_unaligned_le32(k
->kv_val
+ 12);
172 if (k
->kv_len
<= WLAN_KEY_LEN_WEP104
)
176 * Note: Key cache registers access special memory area that requires
177 * two 32-bit writes to actually update the values in the internal
178 * memory. Consequently, the exact order and pairs used here must be
182 if (keyType
== AR_KEYTABLE_TYPE_TKIP
) {
183 u16 micentry
= entry
+ 64;
186 * Write inverted key[47:0] first to avoid Michael MIC errors
187 * on frames that could be sent or received at the same time.
188 * The correct key will be written in the end once everything
191 REG_WRITE(ah
, AR_KEYTABLE_KEY0(entry
), ~key0
);
192 REG_WRITE(ah
, AR_KEYTABLE_KEY1(entry
), ~key1
);
194 /* Write key[95:48] */
195 REG_WRITE(ah
, AR_KEYTABLE_KEY2(entry
), key2
);
196 REG_WRITE(ah
, AR_KEYTABLE_KEY3(entry
), key3
);
198 /* Write key[127:96] and key type */
199 REG_WRITE(ah
, AR_KEYTABLE_KEY4(entry
), key4
);
200 REG_WRITE(ah
, AR_KEYTABLE_TYPE(entry
), keyType
);
202 /* Write MAC address for the entry */
203 (void) ath_hw_keysetmac(common
, entry
, mac
);
205 if (common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
) {
207 * TKIP uses two key cache entries:
208 * Michael MIC TX/RX keys in the same key cache entry
209 * (idx = main index + 64):
210 * key0 [31:0] = RX key [31:0]
211 * key1 [15:0] = TX key [31:16]
212 * key1 [31:16] = reserved
213 * key2 [31:0] = RX key [63:32]
214 * key3 [15:0] = TX key [15:0]
215 * key3 [31:16] = reserved
216 * key4 [31:0] = TX key [63:32]
218 u32 mic0
, mic1
, mic2
, mic3
, mic4
;
220 mic0
= get_unaligned_le32(k
->kv_mic
+ 0);
221 mic2
= get_unaligned_le32(k
->kv_mic
+ 4);
222 mic1
= get_unaligned_le16(k
->kv_txmic
+ 2) & 0xffff;
223 mic3
= get_unaligned_le16(k
->kv_txmic
+ 0) & 0xffff;
224 mic4
= get_unaligned_le32(k
->kv_txmic
+ 4);
226 /* Write RX[31:0] and TX[31:16] */
227 REG_WRITE(ah
, AR_KEYTABLE_KEY0(micentry
), mic0
);
228 REG_WRITE(ah
, AR_KEYTABLE_KEY1(micentry
), mic1
);
230 /* Write RX[63:32] and TX[15:0] */
231 REG_WRITE(ah
, AR_KEYTABLE_KEY2(micentry
), mic2
);
232 REG_WRITE(ah
, AR_KEYTABLE_KEY3(micentry
), mic3
);
234 /* Write TX[63:32] and keyType(reserved) */
235 REG_WRITE(ah
, AR_KEYTABLE_KEY4(micentry
), mic4
);
236 REG_WRITE(ah
, AR_KEYTABLE_TYPE(micentry
),
237 AR_KEYTABLE_TYPE_CLR
);
241 * TKIP uses four key cache entries (two for group
243 * Michael MIC TX/RX keys are in different key cache
244 * entries (idx = main index + 64 for TX and
245 * main index + 32 + 96 for RX):
246 * key0 [31:0] = TX/RX MIC key [31:0]
247 * key1 [31:0] = reserved
248 * key2 [31:0] = TX/RX MIC key [63:32]
249 * key3 [31:0] = reserved
250 * key4 [31:0] = reserved
252 * Upper layer code will call this function separately
253 * for TX and RX keys when these registers offsets are
258 mic0
= get_unaligned_le32(k
->kv_mic
+ 0);
259 mic2
= get_unaligned_le32(k
->kv_mic
+ 4);
261 /* Write MIC key[31:0] */
262 REG_WRITE(ah
, AR_KEYTABLE_KEY0(micentry
), mic0
);
263 REG_WRITE(ah
, AR_KEYTABLE_KEY1(micentry
), 0);
265 /* Write MIC key[63:32] */
266 REG_WRITE(ah
, AR_KEYTABLE_KEY2(micentry
), mic2
);
267 REG_WRITE(ah
, AR_KEYTABLE_KEY3(micentry
), 0);
269 /* Write TX[63:32] and keyType(reserved) */
270 REG_WRITE(ah
, AR_KEYTABLE_KEY4(micentry
), 0);
271 REG_WRITE(ah
, AR_KEYTABLE_TYPE(micentry
),
272 AR_KEYTABLE_TYPE_CLR
);
275 /* MAC address registers are reserved for the MIC entry */
276 REG_WRITE(ah
, AR_KEYTABLE_MAC0(micentry
), 0);
277 REG_WRITE(ah
, AR_KEYTABLE_MAC1(micentry
), 0);
280 * Write the correct (un-inverted) key[47:0] last to enable
281 * TKIP now that all other registers are set with correct
284 REG_WRITE(ah
, AR_KEYTABLE_KEY0(entry
), key0
);
285 REG_WRITE(ah
, AR_KEYTABLE_KEY1(entry
), key1
);
287 /* Write key[47:0] */
288 REG_WRITE(ah
, AR_KEYTABLE_KEY0(entry
), key0
);
289 REG_WRITE(ah
, AR_KEYTABLE_KEY1(entry
), key1
);
291 /* Write key[95:48] */
292 REG_WRITE(ah
, AR_KEYTABLE_KEY2(entry
), key2
);
293 REG_WRITE(ah
, AR_KEYTABLE_KEY3(entry
), key3
);
295 /* Write key[127:96] and key type */
296 REG_WRITE(ah
, AR_KEYTABLE_KEY4(entry
), key4
);
297 REG_WRITE(ah
, AR_KEYTABLE_TYPE(entry
), keyType
);
299 /* Write MAC address for the entry */
300 (void) ath_hw_keysetmac(common
, entry
, mac
);
306 static int ath_setkey_tkip(struct ath_common
*common
, u16 keyix
, const u8
*key
,
307 struct ath_keyval
*hk
, const u8
*addr
,
313 key_txmic
= key
+ NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY
;
314 key_rxmic
= key
+ NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY
;
318 * Group key installation - only two key cache entries are used
319 * regardless of splitmic capability since group key is only
320 * used either for TX or RX.
323 memcpy(hk
->kv_mic
, key_txmic
, sizeof(hk
->kv_mic
));
324 memcpy(hk
->kv_txmic
, key_txmic
, sizeof(hk
->kv_mic
));
326 memcpy(hk
->kv_mic
, key_rxmic
, sizeof(hk
->kv_mic
));
327 memcpy(hk
->kv_txmic
, key_rxmic
, sizeof(hk
->kv_mic
));
329 return ath_hw_set_keycache_entry(common
, keyix
, hk
, addr
);
331 if (common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
) {
332 /* TX and RX keys share the same key cache entry. */
333 memcpy(hk
->kv_mic
, key_rxmic
, sizeof(hk
->kv_mic
));
334 memcpy(hk
->kv_txmic
, key_txmic
, sizeof(hk
->kv_txmic
));
335 return ath_hw_set_keycache_entry(common
, keyix
, hk
, addr
);
338 /* Separate key cache entries for TX and RX */
340 /* TX key goes at first index, RX key at +32. */
341 memcpy(hk
->kv_mic
, key_txmic
, sizeof(hk
->kv_mic
));
342 if (!ath_hw_set_keycache_entry(common
, keyix
, hk
, NULL
)) {
343 /* TX MIC entry failed. No need to proceed further */
344 ath_err(common
, "Setting TX MIC Key Failed\n");
348 memcpy(hk
->kv_mic
, key_rxmic
, sizeof(hk
->kv_mic
));
349 /* XXX delete tx key on failure? */
350 return ath_hw_set_keycache_entry(common
, keyix
+ 32, hk
, addr
);
353 static int ath_reserve_key_cache_slot_tkip(struct ath_common
*common
)
357 for (i
= IEEE80211_WEP_NKID
; i
< common
->keymax
/ 2; i
++) {
358 if (test_bit(i
, common
->keymap
) ||
359 test_bit(i
+ 64, common
->keymap
))
360 continue; /* At least one part of TKIP key allocated */
361 if (!(common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
) &&
362 (test_bit(i
+ 32, common
->keymap
) ||
363 test_bit(i
+ 64 + 32, common
->keymap
)))
364 continue; /* At least one part of TKIP key allocated */
366 /* Found a free slot for a TKIP key */
372 static int ath_reserve_key_cache_slot(struct ath_common
*common
,
377 if (cipher
== WLAN_CIPHER_SUITE_TKIP
)
378 return ath_reserve_key_cache_slot_tkip(common
);
380 /* First, try to find slots that would not be available for TKIP. */
381 if (!(common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
)) {
382 for (i
= IEEE80211_WEP_NKID
; i
< common
->keymax
/ 4; i
++) {
383 if (!test_bit(i
, common
->keymap
) &&
384 (test_bit(i
+ 32, common
->keymap
) ||
385 test_bit(i
+ 64, common
->keymap
) ||
386 test_bit(i
+ 64 + 32, common
->keymap
)))
388 if (!test_bit(i
+ 32, common
->keymap
) &&
389 (test_bit(i
, common
->keymap
) ||
390 test_bit(i
+ 64, common
->keymap
) ||
391 test_bit(i
+ 64 + 32, common
->keymap
)))
393 if (!test_bit(i
+ 64, common
->keymap
) &&
394 (test_bit(i
, common
->keymap
) ||
395 test_bit(i
+ 32, common
->keymap
) ||
396 test_bit(i
+ 64 + 32, common
->keymap
)))
398 if (!test_bit(i
+ 64 + 32, common
->keymap
) &&
399 (test_bit(i
, common
->keymap
) ||
400 test_bit(i
+ 32, common
->keymap
) ||
401 test_bit(i
+ 64, common
->keymap
)))
405 for (i
= IEEE80211_WEP_NKID
; i
< common
->keymax
/ 2; i
++) {
406 if (!test_bit(i
, common
->keymap
) &&
407 test_bit(i
+ 64, common
->keymap
))
409 if (test_bit(i
, common
->keymap
) &&
410 !test_bit(i
+ 64, common
->keymap
))
415 /* No partially used TKIP slots, pick any available slot */
416 for (i
= IEEE80211_WEP_NKID
; i
< common
->keymax
; i
++) {
417 /* Do not allow slots that could be needed for TKIP group keys
418 * to be used. This limitation could be removed if we know that
419 * TKIP will not be used. */
420 if (i
>= 64 && i
< 64 + IEEE80211_WEP_NKID
)
422 if (!(common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
)) {
423 if (i
>= 32 && i
< 32 + IEEE80211_WEP_NKID
)
425 if (i
>= 64 + 32 && i
< 64 + 32 + IEEE80211_WEP_NKID
)
429 if (!test_bit(i
, common
->keymap
))
430 return i
; /* Found a free slot for a key */
433 /* No free slot found */
438 * Configure encryption in the HW.
440 int ath_key_config(struct ath_common
*common
,
441 struct ieee80211_vif
*vif
,
442 struct ieee80211_sta
*sta
,
443 struct ieee80211_key_conf
*key
)
445 struct ath_keyval hk
;
446 const u8
*mac
= NULL
;
451 memset(&hk
, 0, sizeof(hk
));
453 switch (key
->cipher
) {
454 case WLAN_CIPHER_SUITE_WEP40
:
455 case WLAN_CIPHER_SUITE_WEP104
:
456 hk
.kv_type
= ATH_CIPHER_WEP
;
458 case WLAN_CIPHER_SUITE_TKIP
:
459 hk
.kv_type
= ATH_CIPHER_TKIP
;
461 case WLAN_CIPHER_SUITE_CCMP
:
462 hk
.kv_type
= ATH_CIPHER_AES_CCM
;
468 hk
.kv_len
= key
->keylen
;
469 memcpy(hk
.kv_val
, key
->key
, key
->keylen
);
471 if (!(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
)) {
473 case NL80211_IFTYPE_AP
:
474 memcpy(gmac
, vif
->addr
, ETH_ALEN
);
477 idx
= ath_reserve_key_cache_slot(common
, key
->cipher
);
479 case NL80211_IFTYPE_ADHOC
:
484 memcpy(gmac
, sta
->addr
, ETH_ALEN
);
487 idx
= ath_reserve_key_cache_slot(common
, key
->cipher
);
493 } else if (key
->keyidx
) {
498 if (vif
->type
!= NL80211_IFTYPE_AP
) {
499 /* Only keyidx 0 should be used with unicast key, but
500 * allow this for client mode for now. */
509 idx
= ath_reserve_key_cache_slot(common
, key
->cipher
);
513 return -ENOSPC
; /* no free key cache entries */
515 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
)
516 ret
= ath_setkey_tkip(common
, idx
, key
->key
, &hk
, mac
,
517 vif
->type
== NL80211_IFTYPE_AP
);
519 ret
= ath_hw_set_keycache_entry(common
, idx
, &hk
, mac
);
524 set_bit(idx
, common
->keymap
);
525 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
526 set_bit(idx
+ 64, common
->keymap
);
527 set_bit(idx
, common
->tkip_keymap
);
528 set_bit(idx
+ 64, common
->tkip_keymap
);
529 if (!(common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
)) {
530 set_bit(idx
+ 32, common
->keymap
);
531 set_bit(idx
+ 64 + 32, common
->keymap
);
532 set_bit(idx
+ 32, common
->tkip_keymap
);
533 set_bit(idx
+ 64 + 32, common
->tkip_keymap
);
539 EXPORT_SYMBOL(ath_key_config
);
544 void ath_key_delete(struct ath_common
*common
, struct ieee80211_key_conf
*key
)
546 ath_hw_keyreset(common
, key
->hw_key_idx
);
547 if (key
->hw_key_idx
< IEEE80211_WEP_NKID
)
550 clear_bit(key
->hw_key_idx
, common
->keymap
);
551 if (key
->cipher
!= WLAN_CIPHER_SUITE_TKIP
)
554 clear_bit(key
->hw_key_idx
+ 64, common
->keymap
);
556 clear_bit(key
->hw_key_idx
, common
->tkip_keymap
);
557 clear_bit(key
->hw_key_idx
+ 64, common
->tkip_keymap
);
559 if (!(common
->crypt_caps
& ATH_CRYPT_CAP_MIC_COMBINED
)) {
560 ath_hw_keyreset(common
, key
->hw_key_idx
+ 32);
561 clear_bit(key
->hw_key_idx
+ 32, common
->keymap
);
562 clear_bit(key
->hw_key_idx
+ 64 + 32, common
->keymap
);
564 clear_bit(key
->hw_key_idx
+ 32, common
->tkip_keymap
);
565 clear_bit(key
->hw_key_idx
+ 64 + 32, common
->tkip_keymap
);
568 EXPORT_SYMBOL(ath_key_delete
);