Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5210 / ar5210_keycache.c
1 /*
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2004 Atheros Communications, Inc.
4  *
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.
8  *
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.
16  *
17  * $Id: ar5210_keycache.c,v 1.4 2008/11/10 04:08:02 sam Exp $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24
25 #include "ar5210/ar5210.h"
26 #include "ar5210/ar5210reg.h"
27
28 #define AR_KEYTABLE_SIZE        64
29 #define KEY_XOR                 0xaa
30
31 /*
32  * Return the size of the hardware key cache.
33  */
34 u_int
35 ar5210GetKeyCacheSize(struct ath_hal *ah)
36 {
37         return AR_KEYTABLE_SIZE;
38 }
39
40 /*
41  * Return the size of the hardware key cache.
42  */
43 HAL_BOOL
44 ar5210IsKeyCacheEntryValid(struct ath_hal *ah, uint16_t entry)
45 {
46         if (entry < AR_KEYTABLE_SIZE) {
47                 uint32_t val = OS_REG_READ(ah, AR_KEYTABLE_MAC1(entry));
48                 if (val & AR_KEYTABLE_VALID)
49                         return AH_TRUE;
50         }
51         return AH_FALSE;
52 }
53
54 /*
55  * Clear the specified key cache entry.
56  */
57 HAL_BOOL
58 ar5210ResetKeyCacheEntry(struct ath_hal *ah, uint16_t entry)
59 {
60         if (entry < AR_KEYTABLE_SIZE) {
61                 OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
62                 OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
63                 OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
64                 OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
65                 OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
66                 OS_REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), 0);
67                 OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
68                 OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
69                 return AH_TRUE;
70         }
71         return AH_FALSE;
72 }
73
74 /*
75  * Sets the mac part of the specified key cache entry and mark it valid.
76  */
77 HAL_BOOL
78 ar5210SetKeyCacheEntryMac(struct ath_hal *ah, uint16_t entry, const uint8_t *mac)
79 {
80         uint32_t macHi, macLo;
81
82         if (entry < AR_KEYTABLE_SIZE) {
83                 /*
84                  * Set MAC address -- shifted right by 1.  MacLo is
85                  * the 4 MSBs, and MacHi is the 2 LSBs.
86                  */
87                 if (mac != AH_NULL) {
88                         macHi = (mac[5] << 8) | mac[4];
89                         macLo = (mac[3] << 24)| (mac[2] << 16)
90                               | (mac[1] << 8) | mac[0];
91                         macLo >>= 1;
92                         macLo |= (macHi & 1) << 31;     /* carry */
93                         macHi >>= 1;
94                 } else {
95                         macLo = macHi = 0;
96                 }
97
98                 OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
99                 OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(entry),
100                         macHi | AR_KEYTABLE_VALID);
101                 return AH_TRUE;
102         }
103         return AH_FALSE;
104 }
105
106 /*
107  * Sets the contents of the specified key cache entry.
108  */
109 HAL_BOOL
110 ar5210SetKeyCacheEntry(struct ath_hal *ah, uint16_t entry,
111                        const HAL_KEYVAL *k, const uint8_t *mac, int xorKey)
112 {
113         uint32_t key0, key1, key2, key3, key4;
114         uint32_t keyType;
115         uint32_t xorMask= xorKey ?
116                 (KEY_XOR << 24 | KEY_XOR << 16 | KEY_XOR << 8 | KEY_XOR) : 0;
117
118         if (entry >= AR_KEYTABLE_SIZE)
119                 return AH_FALSE;
120         if (k->kv_type != HAL_CIPHER_WEP) {
121                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: cipher %u not supported\n",
122                     __func__, k->kv_type);
123                 return AH_FALSE;
124         }
125
126         /* NB: only WEP supported */
127         if (k->kv_len < 40 / NBBY)
128                 return AH_FALSE;
129         if (k->kv_len <= 40 / NBBY)
130                 keyType = AR_KEYTABLE_TYPE_40;
131         else if (k->kv_len <= 104 / NBBY)
132                 keyType = AR_KEYTABLE_TYPE_104;
133         else
134                 keyType = AR_KEYTABLE_TYPE_128;
135
136         key0 = LE_READ_4(k->kv_val+0) ^ xorMask;
137         key1 = (LE_READ_2(k->kv_val+4) ^ xorMask) & 0xffff;
138         key2 = LE_READ_4(k->kv_val+6) ^ xorMask;
139         key3 = (LE_READ_2(k->kv_val+10) ^ xorMask) & 0xffff;
140         key4 = LE_READ_4(k->kv_val+12) ^ xorMask;
141         if (k->kv_len <= 104 / NBBY)
142                 key4 &= 0xff;
143
144         /*
145          * Note: WEP key cache hardware requires that each double-word
146          * pair be written in even/odd order (since the destination is
147          * a 64-bit register).  Don't reorder these writes w/o
148          * understanding this!
149          */
150         OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
151         OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
152         OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
153         OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
154         OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
155         OS_REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
156         return ar5210SetKeyCacheEntryMac(ah, entry, mac);
157 }