blob: 8fe8b4cfae6c5aecb7ed58c1043ddc95f08d437f [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
Larry Fingera8d76062012-01-07 20:46:42 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Larry Finger0c817332010-12-08 11:12:31 -06004 *
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 *
Larry Finger0c817332010-12-08 11:12:31 -060014 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
16 *
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
21 *
Chaoming_Li46a62722011-04-25 13:23:05 -050022 * Larry Finger <Larry.Finger@lwfinger.net>
23 *
Larry Finger0c817332010-12-08 11:12:31 -060024 *****************************************************************************/
Larry Finger0c817332010-12-08 11:12:31 -060025#include "wifi.h"
26#include "cam.h"
Larry Finger3c67b8f2014-09-22 09:39:22 -050027#include <linux/export.h>
Larry Finger0c817332010-12-08 11:12:31 -060028
29void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
30{
31 struct rtl_priv *rtlpriv = rtl_priv(hw);
32
33 rtlpriv->sec.use_defaultkey = false;
34 rtlpriv->sec.pairwise_enc_algorithm = NO_ENCRYPTION;
35 rtlpriv->sec.group_enc_algorithm = NO_ENCRYPTION;
36 memset(rtlpriv->sec.key_buf, 0, KEY_BUF_SIZE * MAX_KEY_LEN);
37 memset(rtlpriv->sec.key_len, 0, KEY_BUF_SIZE);
38 rtlpriv->sec.pairwise_key = NULL;
39}
40
41static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
42 u8 *mac_addr, u8 *key_cont_128, u16 us_config)
43{
44 struct rtl_priv *rtlpriv = rtl_priv(hw);
45
46 u32 target_command;
47 u32 target_content = 0;
48 u8 entry_i;
49
Larry Finger3c67b8f2014-09-22 09:39:22 -050050 RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_DMESG, "Key content :",
51 key_cont_128, 16);
Larry Finger0c817332010-12-08 11:12:31 -060052
53 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
54 target_command = entry_i + CAM_CONTENT_COUNT * entry_no;
55 target_command = target_command | BIT(31) | BIT(16);
56
57 if (entry_i == 0) {
58 target_content = (u32) (*(mac_addr + 0)) << 16 |
59 (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
60
61 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
62 target_content);
63 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
64 target_command);
65
Larry Finger3c67b8f2014-09-22 09:39:22 -050066 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
67 "WRITE %x: %x\n",
Joe Perchesf30d7502012-01-04 19:40:41 -080068 rtlpriv->cfg->maps[WCAMI], target_content);
Chaoming_Li46a62722011-04-25 13:23:05 -050069 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -080070 "The Key ID is %d\n", entry_no);
Larry Finger3c67b8f2014-09-22 09:39:22 -050071 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
72 "WRITE %x: %x\n",
Joe Perchesf30d7502012-01-04 19:40:41 -080073 rtlpriv->cfg->maps[RWCAM], target_command);
Larry Finger0c817332010-12-08 11:12:31 -060074
75 } else if (entry_i == 1) {
76
77 target_content = (u32) (*(mac_addr + 5)) << 24 |
78 (u32) (*(mac_addr + 4)) << 16 |
79 (u32) (*(mac_addr + 3)) << 8 |
80 (u32) (*(mac_addr + 2));
81
82 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
83 target_content);
84 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
85 target_command);
86
Larry Finger3c67b8f2014-09-22 09:39:22 -050087 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
88 "WRITE A4: %x\n", target_content);
89 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
90 "WRITE A0: %x\n", target_command);
Larry Finger0c817332010-12-08 11:12:31 -060091
92 } else {
93
94 target_content =
95 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
96 24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
97 << 16 |
98 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
99 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
100
101 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
102 target_content);
103 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
104 target_command);
105 udelay(100);
106
Larry Finger3c67b8f2014-09-22 09:39:22 -0500107 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
108 "WRITE A4: %x\n", target_content);
109 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
110 "WRITE A0: %x\n", target_command);
Larry Finger0c817332010-12-08 11:12:31 -0600111 }
112 }
113
Larry Finger3c67b8f2014-09-22 09:39:22 -0500114 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
115 "after set key, usconfig:%x\n", us_config);
Larry Finger0c817332010-12-08 11:12:31 -0600116}
117
118u8 rtl_cam_add_one_entry(struct ieee80211_hw *hw, u8 *mac_addr,
119 u32 ul_key_id, u32 ul_entry_idx, u32 ul_enc_alg,
120 u32 ul_default_key, u8 *key_content)
121{
122 u32 us_config;
123 struct rtl_priv *rtlpriv = rtl_priv(hw);
124
Larry Finger3c67b8f2014-09-22 09:39:22 -0500125 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800126 "EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, ulUseDK=%x MacAddr %pM\n",
127 ul_entry_idx, ul_key_id, ul_enc_alg,
128 ul_default_key, mac_addr);
Larry Finger0c817332010-12-08 11:12:31 -0600129
130 if (ul_key_id == TOTAL_CAM_ENTRY) {
131 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
Larry Finger3c67b8f2014-09-22 09:39:22 -0500132 "ulKeyId exceed!\n");
Larry Finger0c817332010-12-08 11:12:31 -0600133 return 0;
134 }
135
Larry Finger3c67b8f2014-09-22 09:39:22 -0500136 if (ul_default_key == 1)
Larry Finger0c817332010-12-08 11:12:31 -0600137 us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
Larry Finger3c67b8f2014-09-22 09:39:22 -0500138 else
Larry Finger0c817332010-12-08 11:12:31 -0600139 us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
Larry Finger0c817332010-12-08 11:12:31 -0600140
141 rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
Larry Finger3c67b8f2014-09-22 09:39:22 -0500142 (u8 *)key_content, us_config);
Larry Finger0c817332010-12-08 11:12:31 -0600143
Larry Finger3c67b8f2014-09-22 09:39:22 -0500144 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "end\n");
Larry Finger0c817332010-12-08 11:12:31 -0600145
146 return 1;
147
148}
149EXPORT_SYMBOL(rtl_cam_add_one_entry);
150
151int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
152 u8 *mac_addr, u32 ul_key_id)
153{
154 u32 ul_command;
155 struct rtl_priv *rtlpriv = rtl_priv(hw);
156
Joe Perchesf30d7502012-01-04 19:40:41 -0800157 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "key_idx:%d\n", ul_key_id);
Larry Finger0c817332010-12-08 11:12:31 -0600158
159 ul_command = ul_key_id * CAM_CONTENT_COUNT;
160 ul_command = ul_command | BIT(31) | BIT(16);
161
162 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
163 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
164
165 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800166 "rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0);
Larry Finger0c817332010-12-08 11:12:31 -0600167 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800168 "rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command);
Larry Finger0c817332010-12-08 11:12:31 -0600169
170 return 0;
171
172}
173EXPORT_SYMBOL(rtl_cam_delete_one_entry);
174
175void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
176{
177 u32 ul_command;
178 struct rtl_priv *rtlpriv = rtl_priv(hw);
179
180 ul_command = BIT(31) | BIT(30);
181 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
182}
183EXPORT_SYMBOL(rtl_cam_reset_all_entry);
184
185void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
186{
187 struct rtl_priv *rtlpriv = rtl_priv(hw);
188
189 u32 ul_command;
190 u32 ul_content;
191 u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
192
193 switch (rtlpriv->sec.pairwise_enc_algorithm) {
194 case WEP40_ENCRYPTION:
195 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
196 break;
197 case WEP104_ENCRYPTION:
198 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
199 break;
200 case TKIP_ENCRYPTION:
201 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
202 break;
203 case AESCCMP_ENCRYPTION:
204 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
205 break;
206 default:
207 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
208 }
209
210 ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
211
212 ul_content |= BIT(15);
213 ul_command = CAM_CONTENT_COUNT * uc_index;
214 ul_command = ul_command | BIT(31) | BIT(16);
215
216 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
217 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
218
219 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800220 "rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content);
Larry Finger0c817332010-12-08 11:12:31 -0600221 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800222 "rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command);
Larry Finger0c817332010-12-08 11:12:31 -0600223}
224EXPORT_SYMBOL(rtl_cam_mark_invalid);
225
226void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
227{
228 struct rtl_priv *rtlpriv = rtl_priv(hw);
229
230 u32 ul_command;
231 u32 ul_content;
232 u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
233 u8 entry_i;
234
235 switch (rtlpriv->sec.pairwise_enc_algorithm) {
236 case WEP40_ENCRYPTION:
237 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
238 break;
239 case WEP104_ENCRYPTION:
240 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
241 break;
242 case TKIP_ENCRYPTION:
243 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
244 break;
245 case AESCCMP_ENCRYPTION:
246 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
247 break;
248 default:
249 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
250 }
251
252 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
253
254 if (entry_i == 0) {
255 ul_content =
256 (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
257 ul_content |= BIT(15);
258
259 } else {
260 ul_content = 0;
261 }
262
263 ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
264 ul_command = ul_command | BIT(31) | BIT(16);
265
266 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
267 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
268
269 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800270 "rtl_cam_empty_entry(): WRITE A4: %x\n",
271 ul_content);
Larry Finger0c817332010-12-08 11:12:31 -0600272 RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800273 "rtl_cam_empty_entry(): WRITE A0: %x\n",
274 ul_command);
Larry Finger0c817332010-12-08 11:12:31 -0600275 }
276
277}
278EXPORT_SYMBOL(rtl_cam_empty_entry);
Chaoming_Li46a62722011-04-25 13:23:05 -0500279
280u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
281{
282 struct rtl_priv *rtlpriv = rtl_priv(hw);
283 u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
284 u8 entry_idx = 0;
285 u8 i, *addr;
286
287 if (NULL == sta_addr) {
Larry Finger3c67b8f2014-09-22 09:39:22 -0500288 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
289 "sta_addr is NULL.\n");
Chaoming_Li46a62722011-04-25 13:23:05 -0500290 return TOTAL_CAM_ENTRY;
291 }
292 /* Does STA already exist? */
293 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
294 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
dingtianhong4f297392013-12-26 19:41:30 +0800295 if (ether_addr_equal_unaligned(addr, sta_addr))
Chaoming_Li46a62722011-04-25 13:23:05 -0500296 return i;
297 }
298 /* Get a free CAM entry. */
299 for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
300 if ((bitmap & BIT(0)) == 0) {
301 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800302 "-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
303 rtlpriv->sec.hwsec_cam_bitmap, entry_idx);
Chaoming_Li46a62722011-04-25 13:23:05 -0500304 rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
305 memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
306 sta_addr, ETH_ALEN);
307 return entry_idx;
308 }
309 bitmap = bitmap >> 1;
310 }
311 return TOTAL_CAM_ENTRY;
312}
313EXPORT_SYMBOL(rtl_cam_get_free_entry);
314
315void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
316{
317 struct rtl_priv *rtlpriv = rtl_priv(hw);
318 u32 bitmap;
319 u8 i, *addr;
320
321 if (NULL == sta_addr) {
Larry Finger3c67b8f2014-09-22 09:39:22 -0500322 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
323 "sta_addr is NULL.\n");
324 return;
Chaoming_Li46a62722011-04-25 13:23:05 -0500325 }
326
Joe Perches68e052d52012-03-17 12:13:30 -0700327 if (is_zero_ether_addr(sta_addr)) {
Chaoming_Li46a62722011-04-25 13:23:05 -0500328 RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
Joe Perches68e052d52012-03-17 12:13:30 -0700329 "sta_addr is %pM\n", sta_addr);
Chaoming_Li46a62722011-04-25 13:23:05 -0500330 return;
331 }
332 /* Does STA already exist? */
333 for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
334 addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
335 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
336 if (((bitmap & BIT(0)) == BIT(0)) &&
dingtianhong4f297392013-12-26 19:41:30 +0800337 (ether_addr_equal_unaligned(addr, sta_addr))) {
Chaoming_Li46a62722011-04-25 13:23:05 -0500338 /* Remove from HW Security CAM */
Wei Yongjunccd95362012-11-15 21:24:57 +0800339 eth_zero_addr(rtlpriv->sec.hwsec_cam_sta_addr[i]);
Chaoming_Li46a62722011-04-25 13:23:05 -0500340 rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
Larry Finger3c67b8f2014-09-22 09:39:22 -0500341 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
342 "&&&&&&&&&del entry %d\n", i);
Chaoming_Li46a62722011-04-25 13:23:05 -0500343 }
344 }
345 return;
346}
347EXPORT_SYMBOL(rtl_cam_del_entry);