Staging: unisys: Remove RETINT macro
[linux/fpc-iii.git] / drivers / staging / rtl8821ae / cam.c
blob72743e78954b8c7f6071d2d12797aa4988680108
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 *****************************************************************************/
29 #include "wifi.h"
30 #include "cam.h"
31 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0))
32 #include <linux/export.h>
33 #endif
35 void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
37 struct rtl_priv *rtlpriv = rtl_priv(hw);
39 rtlpriv->sec.use_defaultkey = false;
40 rtlpriv->sec.pairwise_enc_algorithm = NO_ENCRYPTION;
41 rtlpriv->sec.group_enc_algorithm = NO_ENCRYPTION;
42 memset(rtlpriv->sec.key_buf, 0, KEY_BUF_SIZE * MAX_KEY_LEN);
43 memset(rtlpriv->sec.key_len, 0, KEY_BUF_SIZE);
44 rtlpriv->sec.pairwise_key = NULL;
47 static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
48 u8 *mac_addr, u8 *key_cont_128, u16 us_config)
50 struct rtl_priv *rtlpriv = rtl_priv(hw);
52 u32 target_command;
53 u32 target_content = 0;
54 u8 entry_i;
56 RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_DMESG, "Key content :",
57 key_cont_128, 16);
59 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
60 target_command = entry_i + CAM_CONTENT_COUNT * entry_no;
61 target_command = target_command | BIT(31) | BIT(16);
63 if (entry_i == 0) {
64 target_content = (u32) (*(mac_addr + 0)) << 16 |
65 (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
67 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
68 target_content);
69 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
70 target_command);
72 RT_TRACE(COMP_SEC, DBG_LOUD,
73 ("WRITE %x: %x \n",
74 rtlpriv->cfg->maps[WCAMI], target_content));
75 RT_TRACE(COMP_SEC, DBG_LOUD,
76 ("The Key ID is %d\n", entry_no));
77 RT_TRACE(COMP_SEC, DBG_LOUD,
78 ("WRITE %x: %x \n",
79 rtlpriv->cfg->maps[RWCAM], target_command));
81 } else if (entry_i == 1) {
83 target_content = (u32) (*(mac_addr + 5)) << 24 |
84 (u32) (*(mac_addr + 4)) << 16 |
85 (u32) (*(mac_addr + 3)) << 8 |
86 (u32) (*(mac_addr + 2));
88 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
89 target_content);
90 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
91 target_command);
93 RT_TRACE(COMP_SEC, DBG_LOUD,
94 ("WRITE A4: %x \n", target_content));
95 RT_TRACE(COMP_SEC, DBG_LOUD,
96 ("WRITE A0: %x \n", target_command));
98 } else {
100 target_content =
101 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
102 24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
103 << 16 |
104 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
105 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
107 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
108 target_content);
109 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
110 target_command);
111 udelay(100);
113 RT_TRACE(COMP_SEC, DBG_LOUD,
114 ("WRITE A4: %x \n", target_content));
115 RT_TRACE(COMP_SEC, DBG_LOUD,
116 ("WRITE A0: %x \n", target_command));
120 RT_TRACE(COMP_SEC, DBG_LOUD,
121 ("after set key, usconfig:%x\n", us_config));
124 u8 rtl_cam_add_one_entry(struct ieee80211_hw *hw, u8 *mac_addr,
125 u32 ul_key_id, u32 ul_entry_idx, u32 ul_enc_alg,
126 u32 ul_default_key, u8 *key_content)
128 u32 us_config;
129 struct rtl_priv *rtlpriv = rtl_priv(hw);
131 RT_TRACE(COMP_SEC, DBG_DMESG,
132 ("EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, "
133 "ulUseDK=%x MacAddr %pM\n",
134 ul_entry_idx, ul_key_id, ul_enc_alg,
135 ul_default_key, mac_addr));
137 if (ul_key_id == TOTAL_CAM_ENTRY) {
138 RT_TRACE(COMP_ERR, DBG_WARNING,
139 ("ulKeyId exceed!\n"));
140 return 0;
143 if (ul_default_key == 1) {
144 us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
145 } else {
146 us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
149 rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
150 (u8 *) key_content, us_config);
152 RT_TRACE(COMP_SEC, DBG_DMESG, ("end \n"));
154 return 1;
157 //EXPORT_SYMBOL(rtl_cam_add_one_entry);
159 int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
160 u8 *mac_addr, u32 ul_key_id)
162 u32 ul_command;
163 struct rtl_priv *rtlpriv = rtl_priv(hw);
165 RT_TRACE(COMP_SEC, DBG_DMESG, ("key_idx:%d\n", ul_key_id));
167 ul_command = ul_key_id * CAM_CONTENT_COUNT;
168 ul_command = ul_command | BIT(31) | BIT(16);
170 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
171 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
173 RT_TRACE(COMP_SEC, DBG_DMESG,
174 ("rtl_cam_delete_one_entry(): WRITE A4: %x \n", 0));
175 RT_TRACE(COMP_SEC, DBG_DMESG,
176 ("rtl_cam_delete_one_entry(): WRITE A0: %x \n", ul_command));
178 return 0;
181 //EXPORT_SYMBOL(rtl_cam_delete_one_entry);
183 void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
185 u32 ul_command;
186 struct rtl_priv *rtlpriv = rtl_priv(hw);
188 ul_command = BIT(31) | BIT(30);
189 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
191 //EXPORT_SYMBOL(rtl_cam_reset_all_entry);
193 void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
195 struct rtl_priv *rtlpriv = rtl_priv(hw);
197 u32 ul_command;
198 u32 ul_content;
199 u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
201 switch (rtlpriv->sec.pairwise_enc_algorithm) {
202 case WEP40_ENCRYPTION:
203 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
204 break;
205 case WEP104_ENCRYPTION:
206 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
207 break;
208 case TKIP_ENCRYPTION:
209 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
210 break;
211 case AESCCMP_ENCRYPTION:
212 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
213 break;
214 default:
215 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
218 ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
220 ul_content |= BIT(15);
221 ul_command = CAM_CONTENT_COUNT * uc_index;
222 ul_command = ul_command | BIT(31) | BIT(16);
224 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
225 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
227 RT_TRACE(COMP_SEC, DBG_DMESG,
228 ("rtl_cam_mark_invalid(): WRITE A4: %x \n", ul_content));
229 RT_TRACE(COMP_SEC, DBG_DMESG,
230 ("rtl_cam_mark_invalid(): WRITE A0: %x \n", ul_command));
232 //EXPORT_SYMBOL(rtl_cam_mark_invalid);
234 void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
236 struct rtl_priv *rtlpriv = rtl_priv(hw);
238 u32 ul_command;
239 u32 ul_content;
240 u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
241 u8 entry_i;
243 switch (rtlpriv->sec.pairwise_enc_algorithm) {
244 case WEP40_ENCRYPTION:
245 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
246 break;
247 case WEP104_ENCRYPTION:
248 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
249 break;
250 case TKIP_ENCRYPTION:
251 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
252 break;
253 case AESCCMP_ENCRYPTION:
254 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
255 break;
256 default:
257 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
260 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
262 if (entry_i == 0) {
263 ul_content =
264 (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
265 ul_content |= BIT(15);
267 } else {
268 ul_content = 0;
271 ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
272 ul_command = ul_command | BIT(31) | BIT(16);
274 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
275 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
277 RT_TRACE(COMP_SEC, DBG_LOUD,
278 ("rtl_cam_empty_entry(): WRITE A4: %x \n",
279 ul_content));
280 RT_TRACE(COMP_SEC, DBG_LOUD,
281 ("rtl_cam_empty_entry(): WRITE A0: %x \n",
282 ul_command));
286 //EXPORT_SYMBOL(rtl_cam_empty_entry);
288 u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
290 struct rtl_priv *rtlpriv = rtl_priv(hw);
291 u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
292 u8 entry_idx = 0;
293 u8 i, *addr;
295 if (NULL == sta_addr) {
296 RT_TRACE(COMP_SEC, DBG_EMERG,
297 ("sta_addr is NULL.\n"));
298 return TOTAL_CAM_ENTRY;
300 /* Does STA already exist? */
301 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
302 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
303 if(memcmp(addr, sta_addr, ETH_ALEN) == 0)
304 return i;
306 /* Get a free CAM entry. */
307 for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
308 if ((bitmap & BIT(0)) == 0) {
309 RT_TRACE(COMP_SEC, DBG_EMERG,
310 ("-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
311 rtlpriv->sec.hwsec_cam_bitmap, entry_idx));
312 rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
313 memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
314 sta_addr, ETH_ALEN);
315 return entry_idx;
317 bitmap = bitmap >>1;
319 return TOTAL_CAM_ENTRY;
321 //EXPORT_SYMBOL(rtl_cam_get_free_entry);
323 void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
325 struct rtl_priv *rtlpriv = rtl_priv(hw);
326 u32 bitmap;
327 u8 i, *addr;
329 if (NULL == sta_addr) {
330 RT_TRACE(COMP_SEC, DBG_EMERG,
331 ("sta_addr is NULL.\n"));
334 if ((sta_addr[0]|sta_addr[1]|sta_addr[2]|sta_addr[3]|\
335 sta_addr[4]|sta_addr[5]) == 0) {
336 RT_TRACE(COMP_SEC, DBG_EMERG,
337 ("sta_addr is 00:00:00:00:00:00.\n"));
338 return;
340 /* Does STA already exist? */
341 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
342 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
343 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
344 if (((bitmap & BIT(0)) == BIT(0)) &&
345 (memcmp(addr, sta_addr, ETH_ALEN) == 0)) {
346 /* Remove from HW Security CAM */
347 memset(rtlpriv->sec.hwsec_cam_sta_addr[i], 0, ETH_ALEN);
348 rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
349 printk("&&&&&&&&&del entry %d\n",i);
352 return;
354 //EXPORT_SYMBOL(rtl_cam_del_entry);