• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  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  * 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  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 #include "wifi.h"
26 #include "cam.h"
27 #include <linux/export.h>
28 
rtl_cam_reset_sec_info(struct ieee80211_hw * hw)29 void 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 
rtl_cam_program_entry(struct ieee80211_hw * hw,u32 entry_no,u8 * mac_addr,u8 * key_cont_128,u16 us_config)41 static 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 
50 	RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_DMESG, "Key content :",
51 		      key_cont_128, 16);
52 
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 
66 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
67 				 "WRITE %x: %x\n",
68 				 rtlpriv->cfg->maps[WCAMI], target_content);
69 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
70 				 "The Key ID is %d\n", entry_no);
71 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
72 				 "WRITE %x: %x\n",
73 				 rtlpriv->cfg->maps[RWCAM], target_command);
74 
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 
87 			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);
91 
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 
107 			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);
111 		}
112 	}
113 
114 	RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
115 		 "after set key, usconfig:%x\n", us_config);
116 }
117 
rtl_cam_add_one_entry(struct ieee80211_hw * hw,u8 * mac_addr,u32 ul_key_id,u32 ul_entry_idx,u32 ul_enc_alg,u32 ul_default_key,u8 * key_content)118 u8 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 
125 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
126 		 "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);
129 
130 	if (ul_key_id == TOTAL_CAM_ENTRY) {
131 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
132 			 "ulKeyId exceed!\n");
133 		return 0;
134 	}
135 
136 	if (ul_default_key == 1)
137 		us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
138 	else
139 		us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
140 
141 	rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
142 			      (u8 *)key_content, us_config);
143 
144 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "end\n");
145 
146 	return 1;
147 
148 }
149 EXPORT_SYMBOL(rtl_cam_add_one_entry);
150 
rtl_cam_delete_one_entry(struct ieee80211_hw * hw,u8 * mac_addr,u32 ul_key_id)151 int 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 
157 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "key_idx:%d\n", ul_key_id);
158 
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,
166 		 "rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0);
167 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
168 		 "rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command);
169 
170 	return 0;
171 
172 }
173 EXPORT_SYMBOL(rtl_cam_delete_one_entry);
174 
rtl_cam_reset_all_entry(struct ieee80211_hw * hw)175 void 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 }
183 EXPORT_SYMBOL(rtl_cam_reset_all_entry);
184 
rtl_cam_mark_invalid(struct ieee80211_hw * hw,u8 uc_index)185 void 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,
220 		 "rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content);
221 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
222 		 "rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command);
223 }
224 EXPORT_SYMBOL(rtl_cam_mark_invalid);
225 
rtl_cam_empty_entry(struct ieee80211_hw * hw,u8 uc_index)226 void 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,
270 			 "rtl_cam_empty_entry(): WRITE A4: %x\n",
271 			 ul_content);
272 		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
273 			 "rtl_cam_empty_entry(): WRITE A0: %x\n",
274 			 ul_command);
275 	}
276 
277 }
278 EXPORT_SYMBOL(rtl_cam_empty_entry);
279 
rtl_cam_get_free_entry(struct ieee80211_hw * hw,u8 * sta_addr)280 u8 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) {
288 		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
289 			 "sta_addr is NULL.\n");
290 		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];
295 		if (ether_addr_equal_unaligned(addr, sta_addr))
296 			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,
302 				 "-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
303 				 rtlpriv->sec.hwsec_cam_bitmap, entry_idx);
304 			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 }
313 EXPORT_SYMBOL(rtl_cam_get_free_entry);
314 
rtl_cam_del_entry(struct ieee80211_hw * hw,u8 * sta_addr)315 void 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) {
322 		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
323 			 "sta_addr is NULL.\n");
324 		return;
325 	}
326 
327 	if (is_zero_ether_addr(sta_addr)) {
328 		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
329 			 "sta_addr is %pM\n", sta_addr);
330 		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)) &&
337 		    (ether_addr_equal_unaligned(addr, sta_addr))) {
338 			/* Remove from HW Security CAM */
339 			eth_zero_addr(rtlpriv->sec.hwsec_cam_sta_addr[i]);
340 			rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
341 			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
342 				 "&&&&&&&&&del entry %d\n", i);
343 		}
344 	}
345 	return;
346 }
347 EXPORT_SYMBOL(rtl_cam_del_entry);
348