sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / net / wireless / rtlwifi / cam.c
blob7295af0536b716553458902f3ef60d8832bd3faa
1 /******************************************************************************
3 * Copyright(c) 2009-2010 Realtek Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 * Larry Finger <Larry.Finger@lwfinger.net>
28 *****************************************************************************/
30 #include "wifi.h"
31 #include "cam.h"
33 void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
35 struct rtl_priv *rtlpriv = rtl_priv(hw);
37 rtlpriv->sec.use_defaultkey = false;
38 rtlpriv->sec.pairwise_enc_algorithm = NO_ENCRYPTION;
39 rtlpriv->sec.group_enc_algorithm = NO_ENCRYPTION;
40 memset(rtlpriv->sec.key_buf, 0, KEY_BUF_SIZE * MAX_KEY_LEN);
41 memset(rtlpriv->sec.key_len, 0, KEY_BUF_SIZE);
42 rtlpriv->sec.pairwise_key = NULL;
45 static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
46 u8 *mac_addr, u8 *key_cont_128, u16 us_config)
48 struct rtl_priv *rtlpriv = rtl_priv(hw);
50 u32 target_command;
51 u32 target_content = 0;
52 u8 entry_i;
54 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
55 ("key_cont_128:\n %x:%x:%x:%x:%x:%x\n",
56 key_cont_128[0], key_cont_128[1],
57 key_cont_128[2], key_cont_128[3],
58 key_cont_128[4], key_cont_128[5]));
60 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
61 target_command = entry_i + CAM_CONTENT_COUNT * entry_no;
62 target_command = target_command | BIT(31) | BIT(16);
64 if (entry_i == 0) {
65 target_content = (u32) (*(mac_addr + 0)) << 16 |
66 (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
68 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
69 target_content);
70 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
71 target_command);
73 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
74 ("WRITE %x: %x\n",
75 rtlpriv->cfg->maps[WCAMI], target_content));
76 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
77 ("The Key ID is %d\n", entry_no));
78 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
79 ("WRITE %x: %x\n",
80 rtlpriv->cfg->maps[RWCAM], target_command));
82 } else if (entry_i == 1) {
84 target_content = (u32) (*(mac_addr + 5)) << 24 |
85 (u32) (*(mac_addr + 4)) << 16 |
86 (u32) (*(mac_addr + 3)) << 8 |
87 (u32) (*(mac_addr + 2));
89 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
90 target_content);
91 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
92 target_command);
94 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
95 ("WRITE A4: %x\n", target_content));
96 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
97 ("WRITE A0: %x\n", target_command));
99 } else {
101 target_content =
102 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
103 24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
104 << 16 |
105 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
106 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
108 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
109 target_content);
110 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
111 target_command);
112 udelay(100);
114 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
115 ("WRITE A4: %x\n", target_content));
116 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
117 ("WRITE A0: %x\n", target_command));
121 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
122 ("after set key, usconfig:%x\n", us_config));
125 u8 rtl_cam_add_one_entry(struct ieee80211_hw *hw, u8 *mac_addr,
126 u32 ul_key_id, u32 ul_entry_idx, u32 ul_enc_alg,
127 u32 ul_default_key, u8 *key_content)
129 u32 us_config;
130 struct rtl_priv *rtlpriv = rtl_priv(hw);
132 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
133 ("EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, "
134 "ulUseDK=%x MacAddr" MAC_FMT "\n",
135 ul_entry_idx, ul_key_id, ul_enc_alg,
136 ul_default_key, MAC_ARG(mac_addr)));
138 if (ul_key_id == TOTAL_CAM_ENTRY) {
139 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
140 ("<=== ulKeyId exceed!\n"));
141 return 0;
144 if (ul_default_key == 1) {
145 us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
146 } else {
147 us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
150 rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
151 (u8 *) key_content, us_config);
153 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("<===\n"));
155 return 1;
158 EXPORT_SYMBOL(rtl_cam_add_one_entry);
160 int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
161 u8 *mac_addr, u32 ul_key_id)
163 u32 ul_command;
164 struct rtl_priv *rtlpriv = rtl_priv(hw);
166 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("key_idx:%d\n", ul_key_id));
168 ul_command = ul_key_id * CAM_CONTENT_COUNT;
169 ul_command = ul_command | BIT(31) | BIT(16);
171 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
172 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
174 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
175 ("rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0));
176 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
177 ("rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command));
179 return 0;
182 EXPORT_SYMBOL(rtl_cam_delete_one_entry);
184 void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
186 u32 ul_command;
187 struct rtl_priv *rtlpriv = rtl_priv(hw);
189 ul_command = BIT(31) | BIT(30);
190 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
192 EXPORT_SYMBOL(rtl_cam_reset_all_entry);
194 void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
196 struct rtl_priv *rtlpriv = rtl_priv(hw);
198 u32 ul_command;
199 u32 ul_content;
200 u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
202 switch (rtlpriv->sec.pairwise_enc_algorithm) {
203 case WEP40_ENCRYPTION:
204 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
205 break;
206 case WEP104_ENCRYPTION:
207 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
208 break;
209 case TKIP_ENCRYPTION:
210 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
211 break;
212 case AESCCMP_ENCRYPTION:
213 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
214 break;
215 default:
216 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
219 ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
221 ul_content |= BIT(15);
222 ul_command = CAM_CONTENT_COUNT * uc_index;
223 ul_command = ul_command | BIT(31) | BIT(16);
225 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
226 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
228 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
229 ("rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content));
230 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
231 ("rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command));
233 EXPORT_SYMBOL(rtl_cam_mark_invalid);
235 void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
237 struct rtl_priv *rtlpriv = rtl_priv(hw);
239 u32 ul_command;
240 u32 ul_content;
241 u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
242 u8 entry_i;
244 switch (rtlpriv->sec.pairwise_enc_algorithm) {
245 case WEP40_ENCRYPTION:
246 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
247 break;
248 case WEP104_ENCRYPTION:
249 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
250 break;
251 case TKIP_ENCRYPTION:
252 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
253 break;
254 case AESCCMP_ENCRYPTION:
255 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
256 break;
257 default:
258 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
261 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
263 if (entry_i == 0) {
264 ul_content =
265 (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
266 ul_content |= BIT(15);
268 } else {
269 ul_content = 0;
272 ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
273 ul_command = ul_command | BIT(31) | BIT(16);
275 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
276 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
278 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
279 ("rtl_cam_empty_entry(): WRITE A4: %x\n",
280 ul_content));
281 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
282 ("rtl_cam_empty_entry(): WRITE A0: %x\n",
283 ul_command));
287 EXPORT_SYMBOL(rtl_cam_empty_entry);
289 u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
291 struct rtl_priv *rtlpriv = rtl_priv(hw);
292 u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
293 u8 entry_idx = 0;
294 u8 i, *addr;
296 if (NULL == sta_addr) {
297 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
298 ("sta_addr is NULL.\n"));
299 return TOTAL_CAM_ENTRY;
301 /* Does STA already exist? */
302 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
303 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
304 if (memcmp(addr, sta_addr, ETH_ALEN) == 0)
305 return i;
307 /* Get a free CAM entry. */
308 for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
309 if ((bitmap & BIT(0)) == 0) {
310 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
311 ("-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
312 rtlpriv->sec.hwsec_cam_bitmap, entry_idx));
313 rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
314 memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
315 sta_addr, ETH_ALEN);
316 return entry_idx;
318 bitmap = bitmap >> 1;
320 return TOTAL_CAM_ENTRY;
322 EXPORT_SYMBOL(rtl_cam_get_free_entry);
324 void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
326 struct rtl_priv *rtlpriv = rtl_priv(hw);
327 u32 bitmap;
328 u8 i, *addr;
330 if (NULL == sta_addr) {
331 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
332 ("sta_addr is NULL.\n"));
335 if ((sta_addr[0]|sta_addr[1]|sta_addr[2]|sta_addr[3]|\
336 sta_addr[4]|sta_addr[5]) == 0) {
337 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
338 ("sta_addr is 00:00:00:00:00:00.\n"));
339 return;
341 /* Does STA already exist? */
342 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
343 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
344 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
345 if (((bitmap & BIT(0)) == BIT(0)) &&
346 (memcmp(addr, sta_addr, ETH_ALEN) == 0)) {
347 /* Remove from HW Security CAM */
348 memset(rtlpriv->sec.hwsec_cam_sta_addr[i], 0, ETH_ALEN);
349 rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
350 printk(KERN_INFO "&&&&&&&&&del entry %d\n", i);
353 return;
355 EXPORT_SYMBOL(rtl_cam_del_entry);