Linux Audio

Check our new training course

Yocto distribution development and maintenance

Need a Yocto distribution for your embedded project?
Loading...
v3.1
  1/******************************************************************************
  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 *
 26 * Larry Finger <Larry.Finger@lwfinger.net>
 27 *
 28 *****************************************************************************/
 29
 30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 31
 32#include "wifi.h"
 33#include "cam.h"
 34
 35void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
 36{
 37	struct rtl_priv *rtlpriv = rtl_priv(hw);
 38
 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;
 45}
 46
 47static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
 48			   u8 *mac_addr, u8 *key_cont_128, u16 us_config)
 49{
 50	struct rtl_priv *rtlpriv = rtl_priv(hw);
 51
 52	u32 target_command;
 53	u32 target_content = 0;
 54	u8 entry_i;
 55
 56	RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 57		 ("key_cont_128:\n %x:%x:%x:%x:%x:%x\n",
 58		  key_cont_128[0], key_cont_128[1],
 59		  key_cont_128[2], key_cont_128[3],
 60		  key_cont_128[4], key_cont_128[5]));
 61
 62	for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
 63		target_command = entry_i + CAM_CONTENT_COUNT * entry_no;
 64		target_command = target_command | BIT(31) | BIT(16);
 65
 66		if (entry_i == 0) {
 67			target_content = (u32) (*(mac_addr + 0)) << 16 |
 68			    (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
 69
 70			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
 71					target_content);
 72			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
 73					target_command);
 74
 
 
 75			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 76				 ("WRITE %x: %x\n",
 77				  rtlpriv->cfg->maps[WCAMI], target_content));
 78			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 79				 ("The Key ID is %d\n", entry_no));
 80			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 81				 ("WRITE %x: %x\n",
 82				  rtlpriv->cfg->maps[RWCAM], target_command));
 83
 84		} else if (entry_i == 1) {
 85
 86			target_content = (u32) (*(mac_addr + 5)) << 24 |
 87			    (u32) (*(mac_addr + 4)) << 16 |
 88			    (u32) (*(mac_addr + 3)) << 8 |
 89			    (u32) (*(mac_addr + 2));
 90
 91			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
 92					target_content);
 93			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
 94					target_command);
 95
 96			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 97				 ("WRITE A4: %x\n", target_content));
 98			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 99				 ("WRITE A0: %x\n", target_command));
100
101		} else {
102
103			target_content =
104			    (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
105			    24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
106			    << 16 |
107			    (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
108			    | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
109
110			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
111					target_content);
112			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
113					target_command);
114			udelay(100);
115
116			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
117				 ("WRITE A4: %x\n", target_content));
118			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
119				 ("WRITE A0: %x\n", target_command));
120		}
121	}
122
123	RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
124		 ("after set key, usconfig:%x\n", us_config));
125}
126
127u8 rtl_cam_add_one_entry(struct ieee80211_hw *hw, u8 *mac_addr,
128			 u32 ul_key_id, u32 ul_entry_idx, u32 ul_enc_alg,
129			 u32 ul_default_key, u8 *key_content)
130{
131	u32 us_config;
132	struct rtl_priv *rtlpriv = rtl_priv(hw);
133
134	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
135		 ("EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, "
136		  "ulUseDK=%x MacAddr %pM\n",
137		  ul_entry_idx, ul_key_id, ul_enc_alg,
138		  ul_default_key, mac_addr));
139
140	if (ul_key_id == TOTAL_CAM_ENTRY) {
141		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
142			 ("<=== ulKeyId exceed!\n"));
143		return 0;
144	}
145
146	if (ul_default_key == 1) {
147		us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
148	} else {
149		us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
150	}
151
152	rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
153			      (u8 *) key_content, us_config);
154
155	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("<===\n"));
156
157	return 1;
158
159}
160EXPORT_SYMBOL(rtl_cam_add_one_entry);
161
162int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
163			     u8 *mac_addr, u32 ul_key_id)
164{
165	u32 ul_command;
166	struct rtl_priv *rtlpriv = rtl_priv(hw);
167
168	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, ("key_idx:%d\n", ul_key_id));
169
170	ul_command = ul_key_id * CAM_CONTENT_COUNT;
171	ul_command = ul_command | BIT(31) | BIT(16);
172
173	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
174	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
175
176	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
177		 ("rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0));
178	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
179		 ("rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command));
180
181	return 0;
182
183}
184EXPORT_SYMBOL(rtl_cam_delete_one_entry);
185
186void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
187{
188	u32 ul_command;
189	struct rtl_priv *rtlpriv = rtl_priv(hw);
190
191	ul_command = BIT(31) | BIT(30);
192	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
193}
194EXPORT_SYMBOL(rtl_cam_reset_all_entry);
195
196void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
197{
198	struct rtl_priv *rtlpriv = rtl_priv(hw);
199
200	u32 ul_command;
201	u32 ul_content;
202	u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
203
204	switch (rtlpriv->sec.pairwise_enc_algorithm) {
205	case WEP40_ENCRYPTION:
206		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
207		break;
208	case WEP104_ENCRYPTION:
209		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
210		break;
211	case TKIP_ENCRYPTION:
212		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
213		break;
214	case AESCCMP_ENCRYPTION:
215		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
216		break;
217	default:
218		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
219	}
220
221	ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
222
223	ul_content |= BIT(15);
224	ul_command = CAM_CONTENT_COUNT * uc_index;
225	ul_command = ul_command | BIT(31) | BIT(16);
226
227	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
228	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
229
230	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
231		 ("rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content));
232	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
233		 ("rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command));
234}
235EXPORT_SYMBOL(rtl_cam_mark_invalid);
236
237void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
238{
239	struct rtl_priv *rtlpriv = rtl_priv(hw);
240
241	u32 ul_command;
242	u32 ul_content;
243	u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
244	u8 entry_i;
245
246	switch (rtlpriv->sec.pairwise_enc_algorithm) {
247	case WEP40_ENCRYPTION:
248		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
249		break;
250	case WEP104_ENCRYPTION:
251		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
252		break;
253	case TKIP_ENCRYPTION:
254		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
255		break;
256	case AESCCMP_ENCRYPTION:
257		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
258		break;
259	default:
260		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
261	}
262
263	for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
264
265		if (entry_i == 0) {
266			ul_content =
267			    (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
268			ul_content |= BIT(15);
269
270		} else {
271			ul_content = 0;
272		}
273
274		ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
275		ul_command = ul_command | BIT(31) | BIT(16);
276
277		rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
278		rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
279
280		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
281			 ("rtl_cam_empty_entry(): WRITE A4: %x\n",
282			  ul_content));
283		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
284			 ("rtl_cam_empty_entry(): WRITE A0: %x\n",
285			  ul_command));
286	}
287
288}
289EXPORT_SYMBOL(rtl_cam_empty_entry);
290
291u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
292{
293	struct rtl_priv *rtlpriv = rtl_priv(hw);
294	u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
295	u8 entry_idx = 0;
296	u8 i, *addr;
297
298	if (NULL == sta_addr) {
299		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
300			("sta_addr is NULL.\n"));
301		return TOTAL_CAM_ENTRY;
302	}
303	/* Does STA already exist? */
304	for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
305		addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
306		if (memcmp(addr, sta_addr, ETH_ALEN) == 0)
307			return i;
308	}
309	/* Get a free CAM entry. */
310	for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
311		if ((bitmap & BIT(0)) == 0) {
312			RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
313				("-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
314				 rtlpriv->sec.hwsec_cam_bitmap, entry_idx));
315			rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
316			memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
317			       sta_addr, ETH_ALEN);
318			return entry_idx;
319		}
320		bitmap = bitmap >> 1;
321	}
322	return TOTAL_CAM_ENTRY;
323}
324EXPORT_SYMBOL(rtl_cam_get_free_entry);
325
326void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
327{
328	struct rtl_priv *rtlpriv = rtl_priv(hw);
329	u32 bitmap;
330	u8 i, *addr;
331
332	if (NULL == sta_addr) {
333		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
334			("sta_addr is NULL.\n"));
335	}
336
337	if ((sta_addr[0]|sta_addr[1]|sta_addr[2]|sta_addr[3]|\
338				sta_addr[4]|sta_addr[5]) == 0) {
339		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
340			("sta_addr is 00:00:00:00:00:00.\n"));
341		return;
342	}
343	/* Does STA already exist? */
344	for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
345		addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
346		bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
347		if (((bitmap & BIT(0)) == BIT(0)) &&
348		    (memcmp(addr, sta_addr, ETH_ALEN) == 0)) {
349			/* Remove from HW Security CAM */
350			memset(rtlpriv->sec.hwsec_cam_sta_addr[i], 0, ETH_ALEN);
351			rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
352			pr_info("&&&&&&&&&del entry %d\n", i);
353		}
354	}
355	return;
356}
357EXPORT_SYMBOL(rtl_cam_del_entry);
v3.5.6
  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 * 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 *
 26 * Larry Finger <Larry.Finger@lwfinger.net>
 27 *
 28 *****************************************************************************/
 29
 30#include <linux/export.h>
 
 31#include "wifi.h"
 32#include "cam.h"
 33
 34void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
 35{
 36	struct rtl_priv *rtlpriv = rtl_priv(hw);
 37
 38	rtlpriv->sec.use_defaultkey = false;
 39	rtlpriv->sec.pairwise_enc_algorithm = NO_ENCRYPTION;
 40	rtlpriv->sec.group_enc_algorithm = NO_ENCRYPTION;
 41	memset(rtlpriv->sec.key_buf, 0, KEY_BUF_SIZE * MAX_KEY_LEN);
 42	memset(rtlpriv->sec.key_len, 0, KEY_BUF_SIZE);
 43	rtlpriv->sec.pairwise_key = NULL;
 44}
 45
 46static void rtl_cam_program_entry(struct ieee80211_hw *hw, u32 entry_no,
 47			   u8 *mac_addr, u8 *key_cont_128, u16 us_config)
 48{
 49	struct rtl_priv *rtlpriv = rtl_priv(hw);
 50
 51	u32 target_command;
 52	u32 target_content = 0;
 53	u8 entry_i;
 54
 55	RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 56		 "key_cont_128:\n %x:%x:%x:%x:%x:%x\n",
 57		 key_cont_128[0], key_cont_128[1],
 58		 key_cont_128[2], key_cont_128[3],
 59		 key_cont_128[4], key_cont_128[5]);
 60
 61	for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
 62		target_command = entry_i + CAM_CONTENT_COUNT * entry_no;
 63		target_command = target_command | BIT(31) | BIT(16);
 64
 65		if (entry_i == 0) {
 66			target_content = (u32) (*(mac_addr + 0)) << 16 |
 67			    (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
 68
 69			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
 70					target_content);
 71			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
 72					target_command);
 73
 74			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE %x: %x\n",
 75				 rtlpriv->cfg->maps[WCAMI], target_content);
 76			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
 77				 "The Key ID is %d\n", entry_no);
 78			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE %x: %x\n",
 79				 rtlpriv->cfg->maps[RWCAM], target_command);
 
 
 
 
 80
 81		} else if (entry_i == 1) {
 82
 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));
 87
 88			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
 89					target_content);
 90			rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
 91					target_command);
 92
 93			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A4: %x\n",
 94				 target_content);
 95			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A0: %x\n",
 96				 target_command);
 97
 98		} else {
 99
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));
106
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);
112
113			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A4: %x\n",
114				 target_content);
115			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "WRITE A0: %x\n",
116				 target_command);
117		}
118	}
119
120	RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD, "after set key, usconfig:%x\n",
121		 us_config);
122}
123
124u8 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)
127{
128	u32 us_config;
129	struct rtl_priv *rtlpriv = rtl_priv(hw);
130
131	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
132		 "EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, ulUseDK=%x MacAddr %pM\n",
133		 ul_entry_idx, ul_key_id, ul_enc_alg,
134		 ul_default_key, mac_addr);
 
135
136	if (ul_key_id == TOTAL_CAM_ENTRY) {
137		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
138			 "<=== ulKeyId exceed!\n");
139		return 0;
140	}
141
142	if (ul_default_key == 1) {
143		us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
144	} else {
145		us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
146	}
147
148	rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
149			      (u8 *) key_content, us_config);
150
151	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "<===\n");
152
153	return 1;
154
155}
156EXPORT_SYMBOL(rtl_cam_add_one_entry);
157
158int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
159			     u8 *mac_addr, u32 ul_key_id)
160{
161	u32 ul_command;
162	struct rtl_priv *rtlpriv = rtl_priv(hw);
163
164	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "key_idx:%d\n", ul_key_id);
165
166	ul_command = ul_key_id * CAM_CONTENT_COUNT;
167	ul_command = ul_command | BIT(31) | BIT(16);
168
169	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
170	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
171
172	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
173		 "rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0);
174	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
175		 "rtl_cam_delete_one_entry(): WRITE A0: %x\n", ul_command);
176
177	return 0;
178
179}
180EXPORT_SYMBOL(rtl_cam_delete_one_entry);
181
182void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
183{
184	u32 ul_command;
185	struct rtl_priv *rtlpriv = rtl_priv(hw);
186
187	ul_command = BIT(31) | BIT(30);
188	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
189}
190EXPORT_SYMBOL(rtl_cam_reset_all_entry);
191
192void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
193{
194	struct rtl_priv *rtlpriv = rtl_priv(hw);
195
196	u32 ul_command;
197	u32 ul_content;
198	u32 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
199
200	switch (rtlpriv->sec.pairwise_enc_algorithm) {
201	case WEP40_ENCRYPTION:
202		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
203		break;
204	case WEP104_ENCRYPTION:
205		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
206		break;
207	case TKIP_ENCRYPTION:
208		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
209		break;
210	case AESCCMP_ENCRYPTION:
211		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
212		break;
213	default:
214		ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
215	}
216
217	ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
218
219	ul_content |= BIT(15);
220	ul_command = CAM_CONTENT_COUNT * uc_index;
221	ul_command = ul_command | BIT(31) | BIT(16);
222
223	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
224	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
225
226	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
227		 "rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content);
228	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
229		 "rtl_cam_mark_invalid(): WRITE A0: %x\n", ul_command);
230}
231EXPORT_SYMBOL(rtl_cam_mark_invalid);
232
233void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
234{
235	struct rtl_priv *rtlpriv = rtl_priv(hw);
236
237	u32 ul_command;
238	u32 ul_content;
239	u32 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
240	u8 entry_i;
241
242	switch (rtlpriv->sec.pairwise_enc_algorithm) {
243	case WEP40_ENCRYPTION:
244		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
245		break;
246	case WEP104_ENCRYPTION:
247		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
248		break;
249	case TKIP_ENCRYPTION:
250		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
251		break;
252	case AESCCMP_ENCRYPTION:
253		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
254		break;
255	default:
256		ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
257	}
258
259	for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
260
261		if (entry_i == 0) {
262			ul_content =
263			    (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
264			ul_content |= BIT(15);
265
266		} else {
267			ul_content = 0;
268		}
269
270		ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
271		ul_command = ul_command | BIT(31) | BIT(16);
272
273		rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
274		rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
275
276		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
277			 "rtl_cam_empty_entry(): WRITE A4: %x\n",
278			 ul_content);
279		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
280			 "rtl_cam_empty_entry(): WRITE A0: %x\n",
281			 ul_command);
282	}
283
284}
285EXPORT_SYMBOL(rtl_cam_empty_entry);
286
287u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
288{
289	struct rtl_priv *rtlpriv = rtl_priv(hw);
290	u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
291	u8 entry_idx = 0;
292	u8 i, *addr;
293
294	if (NULL == sta_addr) {
295		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG, "sta_addr is NULL\n");
 
296		return TOTAL_CAM_ENTRY;
297	}
298	/* Does STA already exist? */
299	for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
300		addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
301		if (memcmp(addr, sta_addr, ETH_ALEN) == 0)
302			return i;
303	}
304	/* Get a free CAM entry. */
305	for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
306		if ((bitmap & BIT(0)) == 0) {
307			RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
308				 "-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
309				 rtlpriv->sec.hwsec_cam_bitmap, entry_idx);
310			rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
311			memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
312			       sta_addr, ETH_ALEN);
313			return entry_idx;
314		}
315		bitmap = bitmap >> 1;
316	}
317	return TOTAL_CAM_ENTRY;
318}
319EXPORT_SYMBOL(rtl_cam_get_free_entry);
320
321void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
322{
323	struct rtl_priv *rtlpriv = rtl_priv(hw);
324	u32 bitmap;
325	u8 i, *addr;
326
327	if (NULL == sta_addr) {
328		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG, "sta_addr is NULL\n");
 
329	}
330
331	if (is_zero_ether_addr(sta_addr)) {
 
332		RT_TRACE(rtlpriv, COMP_SEC, DBG_EMERG,
333			 "sta_addr is %pM\n", sta_addr);
334		return;
335	}
336	/* Does STA already exist? */
337	for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
338		addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
339		bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
340		if (((bitmap & BIT(0)) == BIT(0)) &&
341		    (memcmp(addr, sta_addr, ETH_ALEN) == 0)) {
342			/* Remove from HW Security CAM */
343			memset(rtlpriv->sec.hwsec_cam_sta_addr[i], 0, ETH_ALEN);
344			rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
345			pr_info("&&&&&&&&&del entry %d\n", i);
346		}
347	}
348	return;
349}
350EXPORT_SYMBOL(rtl_cam_del_entry);