1 /******************************************************************************
3 * Copyright(c) 2009-2012 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
14 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
22 * Larry Finger <Larry.Finger@lwfinger.net>
24 *****************************************************************************/
27 #include <linux/export.h>
29 void rtl_cam_reset_sec_info(struct ieee80211_hw
*hw
)
31 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
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
;
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
)
44 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
47 u32 target_content
= 0;
50 RT_PRINT_DATA(rtlpriv
, COMP_SEC
, DBG_DMESG
, "Key content :",
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);
58 target_content
= (u32
) (*(mac_addr
+ 0)) << 16 |
59 (u32
) (*(mac_addr
+ 1)) << 24 | (u32
) us_config
;
61 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
],
63 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
],
66 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_LOUD
,
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
,
73 rtlpriv
->cfg
->maps
[RWCAM
], target_command
);
75 } else if (entry_i
== 1) {
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));
82 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
],
84 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
],
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
);
95 (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 3)) <<
96 24 | (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 2))
98 (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 1)) << 8
99 | (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 0));
101 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
],
103 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
],
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
);
114 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_LOUD
,
115 "after set key, usconfig:%x\n", us_config
);
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
)
123 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
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
);
130 if (ul_key_id
== TOTAL_CAM_ENTRY
) {
131 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
132 "ulKeyId exceed!\n");
136 if (ul_default_key
== 1)
137 us_config
= CFG_VALID
| ((u16
) (ul_enc_alg
) << 2);
139 us_config
= CFG_VALID
| ((ul_enc_alg
) << 2) | ul_key_id
;
141 rtl_cam_program_entry(hw
, ul_entry_idx
, mac_addr
,
142 (u8
*)key_content
, us_config
);
144 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_DMESG
, "end\n");
149 EXPORT_SYMBOL(rtl_cam_add_one_entry
);
151 int rtl_cam_delete_one_entry(struct ieee80211_hw
*hw
,
152 u8
*mac_addr
, u32 ul_key_id
)
155 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
157 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_DMESG
, "key_idx:%d\n", ul_key_id
);
159 ul_command
= ul_key_id
* CAM_CONTENT_COUNT
;
160 ul_command
= ul_command
| BIT(31) | BIT(16);
162 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
], 0);
163 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
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
);
173 EXPORT_SYMBOL(rtl_cam_delete_one_entry
);
175 void rtl_cam_reset_all_entry(struct ieee80211_hw
*hw
)
178 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
180 ul_command
= BIT(31) | BIT(30);
181 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
183 EXPORT_SYMBOL(rtl_cam_reset_all_entry
);
185 void rtl_cam_mark_invalid(struct ieee80211_hw
*hw
, u8 uc_index
)
187 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
191 u32 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
193 switch (rtlpriv
->sec
.pairwise_enc_algorithm
) {
194 case WEP40_ENCRYPTION
:
195 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP40
];
197 case WEP104_ENCRYPTION
:
198 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP104
];
200 case TKIP_ENCRYPTION
:
201 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_TKIP
];
203 case AESCCMP_ENCRYPTION
:
204 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
207 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
210 ul_content
= (uc_index
& 3) | ((u16
) (ul_enc_algo
) << 2);
212 ul_content
|= BIT(15);
213 ul_command
= CAM_CONTENT_COUNT
* uc_index
;
214 ul_command
= ul_command
| BIT(31) | BIT(16);
216 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
], ul_content
);
217 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
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
);
224 EXPORT_SYMBOL(rtl_cam_mark_invalid
);
226 void rtl_cam_empty_entry(struct ieee80211_hw
*hw
, u8 uc_index
)
228 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
232 u32 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
235 switch (rtlpriv
->sec
.pairwise_enc_algorithm
) {
236 case WEP40_ENCRYPTION
:
237 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP40
];
239 case WEP104_ENCRYPTION
:
240 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP104
];
242 case TKIP_ENCRYPTION
:
243 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_TKIP
];
245 case AESCCMP_ENCRYPTION
:
246 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
249 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
252 for (entry_i
= 0; entry_i
< CAM_CONTENT_COUNT
; entry_i
++) {
256 (uc_index
& 0x03) | ((u16
) (ul_encalgo
) << 2);
257 ul_content
|= BIT(15);
263 ul_command
= CAM_CONTENT_COUNT
* uc_index
+ entry_i
;
264 ul_command
= ul_command
| BIT(31) | BIT(16);
266 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
], ul_content
);
267 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
269 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_LOUD
,
270 "rtl_cam_empty_entry(): WRITE A4: %x\n",
272 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_LOUD
,
273 "rtl_cam_empty_entry(): WRITE A0: %x\n",
278 EXPORT_SYMBOL(rtl_cam_empty_entry
);
280 u8
rtl_cam_get_free_entry(struct ieee80211_hw
*hw
, u8
*sta_addr
)
282 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
283 u32 bitmap
= (rtlpriv
->sec
.hwsec_cam_bitmap
) >> 4;
287 if (NULL
== sta_addr
) {
288 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_EMERG
,
289 "sta_addr is NULL.\n");
290 return TOTAL_CAM_ENTRY
;
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
))
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
],
309 bitmap
= bitmap
>> 1;
311 return TOTAL_CAM_ENTRY
;
313 EXPORT_SYMBOL(rtl_cam_get_free_entry
);
315 void rtl_cam_del_entry(struct ieee80211_hw
*hw
, u8
*sta_addr
)
317 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
321 if (NULL
== sta_addr
) {
322 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_EMERG
,
323 "sta_addr is NULL.\n");
327 if (is_zero_ether_addr(sta_addr
)) {
328 RT_TRACE(rtlpriv
, COMP_SEC
, DBG_EMERG
,
329 "sta_addr is %pM\n", sta_addr
);
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
);
347 EXPORT_SYMBOL(rtl_cam_del_entry
);