blob: cbf5e3cdb44e0ad9af7d41d2f1431b90f0eda1be [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
3 * Copyright(c) 2009-2010 Realtek Corporation.
4 *
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.
8 *
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.
13 *
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
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
25 *
Chaoming_Li46a62722011-04-25 13:23:05 -050026 * Larry Finger <Larry.Finger@lwfinger.net>
27 *
Larry Finger0c817332010-12-08 11:12:31 -060028 *****************************************************************************/
29
Joe Perches292b1192011-07-20 08:51:35 -070030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Paul Gortmakeree40fa02011-05-27 16:14:23 -040032#include <linux/export.h>
Larry Finger0c817332010-12-08 11:12:31 -060033#include "wifi.h"
34#include "cam.h"
35
36void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
37{
38 struct rtl_priv *rtlpriv = rtl_priv(hw);
39
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;
46}
47
48static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
49 u8 *mac_addr, u8 *key_cont_128, u16 us_config)
50{
51 struct rtl_priv *rtlpriv = rtl_priv(hw);
52
53 u32 target_command;
54 u32 target_content = 0;
55 u8 entry_i;
56
Chaoming_Li46a62722011-04-25 13:23:05 -050057 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -080058 "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]);
Larry Finger0c817332010-12-08 11:12:31 -060062
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);
66
67 if (entry_i == 0) {
68 target_content = (u32) (*(mac_addr + 0)) << 16 |
69 (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
70
71 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
72 target_content);
73 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
74 target_command);
75
Joe Perchesf30d7502012-01-04 19:40:41 -080076 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE %x: %x\n",
77 rtlpriv->cfg->maps[WCAMI], target_content);
Chaoming_Li46a62722011-04-25 13:23:05 -050078 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -080079 "The Key ID is %d\n", entry_no);
80 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE %x: %x\n",
81 rtlpriv->cfg->maps[RWCAM], target_command);
Larry Finger0c817332010-12-08 11:12:31 -060082
83 } else if (entry_i == 1) {
84
85 target_content = (u32) (*(mac_addr + 5)) << 24 |
86 (u32) (*(mac_addr + 4)) << 16 |
87 (u32) (*(mac_addr + 3)) << 8 |
88 (u32) (*(mac_addr + 2));
89
90 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
91 target_content);
92 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
93 target_command);
94
Joe Perchesf30d7502012-01-04 19:40:41 -080095 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A4: %x\n",
96 target_content);
97 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A0: %x\n",
98 target_command);
Larry Finger0c817332010-12-08 11:12:31 -060099
100 } else {
101
102 target_content =
103 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
104 24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
105 << 16 |
106 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
107 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
108
109 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
110 target_content);
111 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
112 target_command);
113 udelay(100);
114
Joe Perchesf30d7502012-01-04 19:40:41 -0800115 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A4: %x\n",
116 target_content);
117 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A0: %x\n",
118 target_command);
Larry Finger0c817332010-12-08 11:12:31 -0600119 }
120 }
121
Joe Perchesf30d7502012-01-04 19:40:41 -0800122 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "after set key, usconfig:%x\n",
123 us_config);
Larry Finger0c817332010-12-08 11:12:31 -0600124}
125
126u8 rtl_cam_add_one_entry(struct ieee80211_hw *hw, u8 *mac_addr,
127 u32 ul_key_id, u32 ul_entry_idx, u32 ul_enc_alg,
128 u32 ul_default_key, u8 *key_content)
129{
130 u32 us_config;
131 struct rtl_priv *rtlpriv = rtl_priv(hw);
132
133 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800134 "EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, ulUseDK=%x MacAddr %pM\n",
135 ul_entry_idx, ul_key_id, ul_enc_alg,
136 ul_default_key, mac_addr);
Larry Finger0c817332010-12-08 11:12:31 -0600137
138 if (ul_key_id == TOTAL_CAM_ENTRY) {
139 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Joe Perchesf30d7502012-01-04 19:40:41 -0800140 "<=== ulKeyId exceed!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600141 return 0;
142 }
143
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;
148 }
149
150 rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
151 (u8 *) key_content, us_config);
152
Joe Perchesf30d7502012-01-04 19:40:41 -0800153 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "<===\n");
Larry Finger0c817332010-12-08 11:12:31 -0600154
155 return 1;
156
157}
158EXPORT_SYMBOL(rtl_cam_add_one_entry);
159
160int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
161 u8 *mac_addr, u32 ul_key_id)
162{
163 u32 ul_command;
164 struct rtl_priv *rtlpriv = rtl_priv(hw);
165
Joe Perchesf30d7502012-01-04 19:40:41 -0800166 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "key_idx:%d\n", ul_key_id);
Larry Finger0c817332010-12-08 11:12:31 -0600167
168 ul_command = ul_key_id * CAM_CONTENT_COUNT;
169 ul_command = ul_command | BIT(31) | BIT(16);
170
171 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
172 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
173
174 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800175 "rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0);
Larry Finger0c817332010-12-08 11:12:31 -0600176 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800177 "rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command);
Larry Finger0c817332010-12-08 11:12:31 -0600178
179 return 0;
180
181}
182EXPORT_SYMBOL(rtl_cam_delete_one_entry);
183
184void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
185{
186 u32 ul_command;
187 struct rtl_priv *rtlpriv = rtl_priv(hw);
188
189 ul_command = BIT(31) | BIT(30);
190 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
191}
192EXPORT_SYMBOL(rtl_cam_reset_all_entry);
193
194void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
195{
196 struct rtl_priv *rtlpriv = rtl_priv(hw);
197
198 u32 ul_command;
199 u32 ul_content;
200 u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
201
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];
217 }
218
219 ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
220
221 ul_content |= BIT(15);
222 ul_command = CAM_CONTENT_COUNT * uc_index;
223 ul_command = ul_command | BIT(31) | BIT(16);
224
225 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
226 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
227
228 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800229 "rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content);
Larry Finger0c817332010-12-08 11:12:31 -0600230 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800231 "rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command);
Larry Finger0c817332010-12-08 11:12:31 -0600232}
233EXPORT_SYMBOL(rtl_cam_mark_invalid);
234
235void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
236{
237 struct rtl_priv *rtlpriv = rtl_priv(hw);
238
239 u32 ul_command;
240 u32 ul_content;
241 u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
242 u8 entry_i;
243
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];
259 }
260
261 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
262
263 if (entry_i == 0) {
264 ul_content =
265 (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
266 ul_content |= BIT(15);
267
268 } else {
269 ul_content = 0;
270 }
271
272 ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
273 ul_command = ul_command | BIT(31) | BIT(16);
274
275 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
276 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
277
278 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800279 "rtl_cam_empty_entry(): WRITE A4: %x\n",
280 ul_content);
Larry Finger0c817332010-12-08 11:12:31 -0600281 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800282 "rtl_cam_empty_entry(): WRITE A0: %x\n",
283 ul_command);
Larry Finger0c817332010-12-08 11:12:31 -0600284 }
285
286}
287EXPORT_SYMBOL(rtl_cam_empty_entry);
Chaoming_Li46a62722011-04-25 13:23:05 -0500288
289u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
290{
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;
295
296 if (NULL == sta_addr) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800297 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG, "sta_addr is NULL\n");
Chaoming_Li46a62722011-04-25 13:23:05 -0500298 return TOTAL_CAM_ENTRY;
299 }
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;
305 }
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(rtlpriv, COMP_SEC, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800310 "-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
311 rtlpriv->sec.hwsec_cam_bitmap, entry_idx);
Chaoming_Li46a62722011-04-25 13:23:05 -0500312 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;
316 }
317 bitmap = bitmap >> 1;
318 }
319 return TOTAL_CAM_ENTRY;
320}
321EXPORT_SYMBOL(rtl_cam_get_free_entry);
322
323void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
324{
325 struct rtl_priv *rtlpriv = rtl_priv(hw);
326 u32 bitmap;
327 u8 i, *addr;
328
329 if (NULL == sta_addr) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800330 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG, "sta_addr is NULL\n");
Chaoming_Li46a62722011-04-25 13:23:05 -0500331 }
332
333 if ((sta_addr[0]|sta_addr[1]|sta_addr[2]|sta_addr[3]|\
334 sta_addr[4]|sta_addr[5]) == 0) {
335 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800336 "sta_addr is 00:00:00:00:00:00\n");
Chaoming_Li46a62722011-04-25 13:23:05 -0500337 return;
338 }
339 /* Does STA already exist? */
340 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
341 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
342 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
343 if (((bitmap & BIT(0)) == BIT(0)) &&
344 (memcmp(addr, sta_addr, ETH_ALEN) == 0)) {
345 /* Remove from HW Security CAM */
346 memset(rtlpriv->sec.hwsec_cam_sta_addr[i], 0, ETH_ALEN);
347 rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
Joe Perches292b1192011-07-20 08:51:35 -0700348 pr_info("&&&&&&&&&del entry %d\n", i);
Chaoming_Li46a62722011-04-25 13:23:05 -0500349 }
350 }
351 return;
352}
353EXPORT_SYMBOL(rtl_cam_del_entry);