Linux 3.3-rc1
[zen-stable.git] / drivers / net / wireless / rtlwifi / cam.c
blobdc36d7461caaaecd07a8f60a93941b843c094ff2
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/export.h>
33 #include "wifi.h"
34 #include "cam.h"
36 void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
38 struct rtl_priv *rtlpriv = rtl_priv(hw);
40 rtlpriv->sec.use_defaultkey = false;
41 rtlpriv->sec.pairwise_enc_algorithm = NO_ENCRYPTION;
42 rtlpriv->sec.group_enc_algorithm = NO_ENCRYPTION;
43 memset(rtlpriv->sec.key_buf, 0, KEY_BUF_SIZE * MAX_KEY_LEN);
44 memset(rtlpriv->sec.key_len, 0, KEY_BUF_SIZE);
45 rtlpriv->sec.pairwise_key = NULL;
48 static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
49 u8 *mac_addr, u8 *key_cont_128, u16 us_config)
51 struct rtl_priv *rtlpriv = rtl_priv(hw);
53 u32 target_command;
54 u32 target_content = 0;
55 u8 entry_i;
57 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
58 ("key_cont_128:\n %x:%x:%x:%x:%x:%x\n",
59 key_cont_128[0], key_cont_128[1],
60 key_cont_128[2], key_cont_128[3],
61 key_cont_128[4], key_cont_128[5]));
63 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
64 target_command = entry_i + CAM_CONTENT_COUNT * entry_no;
65 target_command = target_command | BIT(31) | BIT(16);
67 if (entry_i == 0) {
68 target_content = (u32) (*(mac_addr + 0)) << 16 |
69 (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
71 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
72 target_content);
73 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
74 target_command);
76 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
77 ("WRITE %x: %x\n",
78 rtlpriv->cfg->maps[WCAMI], target_content));
79 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
80 ("The Key ID is %d\n", entry_no));
81 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
82 ("WRITE %x: %x\n",
83 rtlpriv->cfg->maps[RWCAM], target_command));
85 } else if (entry_i == 1) {
87 target_content = (u32) (*(mac_addr + 5)) << 24 |
88 (u32) (*(mac_addr + 4)) << 16 |
89 (u32) (*(mac_addr + 3)) << 8 |
90 (u32) (*(mac_addr + 2));
92 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
93 target_content);
94 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
95 target_command);
97 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
98 ("WRITE A4: %x\n", target_content));
99 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
100 ("WRITE A0: %x\n", target_command));
102 } else {
104 target_content =
105 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
106 24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
107 << 16 |
108 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
109 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
111 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
112 target_content);
113 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
114 target_command);
115 udelay(100);
117 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
118 ("WRITE A4: %x\n", target_content));
119 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
120 ("WRITE A0: %x\n", target_command));
124 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
125 ("after set key, usconfig:%x\n", us_config));
128 u8 rtl_cam_add_one_entry(struct ieee80211_hw *hw, u8 *mac_addr,
129 u32 ul_key_id, u32 ul_entry_idx, u32 ul_enc_alg,
130 u32 ul_default_key, u8 *key_content)
132 u32 us_config;
133 struct rtl_priv *rtlpriv = rtl_priv(hw);
135 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
136 ("EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, "
137 "ulUseDK=%x MacAddr %pM\n",
138 ul_entry_idx, ul_key_id, ul_enc_alg,
139 ul_default_key, mac_addr));
141 if (ul_key_id == TOTAL_CAM_ENTRY) {
142 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
143 ("<=== ulKeyId exceed!\n"));
144 return 0;
147 if (ul_default_key == 1) {
148 us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
149 } else {
150 us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
153 rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
154 (u8 *) key_content, us_config);
156 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("<===\n"));
158 return 1;
161 EXPORT_SYMBOL(rtl_cam_add_one_entry);
163 int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
164 u8 *mac_addr, u32 ul_key_id)
166 u32 ul_command;
167 struct rtl_priv *rtlpriv = rtl_priv(hw);
169 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("key_idx:%d\n", ul_key_id));
171 ul_command = ul_key_id * CAM_CONTENT_COUNT;
172 ul_command = ul_command | BIT(31) | BIT(16);
174 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
175 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
177 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
178 ("rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0));
179 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
180 ("rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command));
182 return 0;
185 EXPORT_SYMBOL(rtl_cam_delete_one_entry);
187 void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
189 u32 ul_command;
190 struct rtl_priv *rtlpriv = rtl_priv(hw);
192 ul_command = BIT(31) | BIT(30);
193 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
195 EXPORT_SYMBOL(rtl_cam_reset_all_entry);
197 void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
199 struct rtl_priv *rtlpriv = rtl_priv(hw);
201 u32 ul_command;
202 u32 ul_content;
203 u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
205 switch (rtlpriv->sec.pairwise_enc_algorithm) {
206 case WEP40_ENCRYPTION:
207 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
208 break;
209 case WEP104_ENCRYPTION:
210 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
211 break;
212 case TKIP_ENCRYPTION:
213 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
214 break;
215 case AESCCMP_ENCRYPTION:
216 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
217 break;
218 default:
219 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
222 ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
224 ul_content |= BIT(15);
225 ul_command = CAM_CONTENT_COUNT * uc_index;
226 ul_command = ul_command | BIT(31) | BIT(16);
228 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
229 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
231 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
232 ("rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content));
233 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
234 ("rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command));
236 EXPORT_SYMBOL(rtl_cam_mark_invalid);
238 void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
240 struct rtl_priv *rtlpriv = rtl_priv(hw);
242 u32 ul_command;
243 u32 ul_content;
244 u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
245 u8 entry_i;
247 switch (rtlpriv->sec.pairwise_enc_algorithm) {
248 case WEP40_ENCRYPTION:
249 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
250 break;
251 case WEP104_ENCRYPTION:
252 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
253 break;
254 case TKIP_ENCRYPTION:
255 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
256 break;
257 case AESCCMP_ENCRYPTION:
258 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
259 break;
260 default:
261 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
264 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
266 if (entry_i == 0) {
267 ul_content =
268 (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
269 ul_content |= BIT(15);
271 } else {
272 ul_content = 0;
275 ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
276 ul_command = ul_command | BIT(31) | BIT(16);
278 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
279 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
281 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
282 ("rtl_cam_empty_entry(): WRITE A4: %x\n",
283 ul_content));
284 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
285 ("rtl_cam_empty_entry(): WRITE A0: %x\n",
286 ul_command));
290 EXPORT_SYMBOL(rtl_cam_empty_entry);
292 u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
294 struct rtl_priv *rtlpriv = rtl_priv(hw);
295 u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
296 u8 entry_idx = 0;
297 u8 i, *addr;
299 if (NULL == sta_addr) {
300 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
301 ("sta_addr is NULL.\n"));
302 return TOTAL_CAM_ENTRY;
304 /* Does STA already exist? */
305 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
306 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
307 if (memcmp(addr, sta_addr, ETH_ALEN) == 0)
308 return i;
310 /* Get a free CAM entry. */
311 for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
312 if ((bitmap & BIT(0)) == 0) {
313 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
314 ("-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
315 rtlpriv->sec.hwsec_cam_bitmap, entry_idx));
316 rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
317 memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
318 sta_addr, ETH_ALEN);
319 return entry_idx;
321 bitmap = bitmap >> 1;
323 return TOTAL_CAM_ENTRY;
325 EXPORT_SYMBOL(rtl_cam_get_free_entry);
327 void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
329 struct rtl_priv *rtlpriv = rtl_priv(hw);
330 u32 bitmap;
331 u8 i, *addr;
333 if (NULL == sta_addr) {
334 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
335 ("sta_addr is NULL.\n"));
338 if ((sta_addr[0]|sta_addr[1]|sta_addr[2]|sta_addr[3]|\
339 sta_addr[4]|sta_addr[5]) == 0) {
340 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
341 ("sta_addr is 00:00:00:00:00:00.\n"));
342 return;
344 /* Does STA already exist? */
345 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
346 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
347 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
348 if (((bitmap & BIT(0)) == BIT(0)) &&
349 (memcmp(addr, sta_addr, ETH_ALEN) == 0)) {
350 /* Remove from HW Security CAM */
351 memset(rtlpriv->sec.hwsec_cam_sta_addr[i], 0, ETH_ALEN);
352 rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
353 pr_info("&&&&&&&&&del entry %d\n", i);
356 return;
358 EXPORT_SYMBOL(rtl_cam_del_entry);