1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012 Realtek Corporation.*/
6 #include <linux/export.h>
8 void rtl_cam_reset_sec_info(struct ieee80211_hw
*hw
)
10 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
12 rtlpriv
->sec
.use_defaultkey
= false;
13 rtlpriv
->sec
.pairwise_enc_algorithm
= NO_ENCRYPTION
;
14 rtlpriv
->sec
.group_enc_algorithm
= NO_ENCRYPTION
;
15 memset(rtlpriv
->sec
.key_buf
, 0, KEY_BUF_SIZE
* MAX_KEY_LEN
);
16 memset(rtlpriv
->sec
.key_len
, 0, KEY_BUF_SIZE
);
17 rtlpriv
->sec
.pairwise_key
= NULL
;
20 static void rtl_cam_program_entry(struct ieee80211_hw
*hw
, u32 entry_no
,
21 u8
*mac_addr
, u8
*key_cont_128
, u16 us_config
)
23 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
26 u32 target_content
= 0;
29 RT_PRINT_DATA(rtlpriv
, COMP_SEC
, DBG_DMESG
, "Key content :",
32 /* 0-1 config + mac, 2-5 fill 128key,6-7 are reserved */
33 for (entry_i
= CAM_CONTENT_COUNT
- 1; entry_i
>= 0; entry_i
--) {
34 target_command
= entry_i
+ CAM_CONTENT_COUNT
* entry_no
;
35 target_command
= target_command
| BIT(31) | BIT(16);
38 target_content
= (u32
) (*(mac_addr
+ 0)) << 16 |
39 (u32
) (*(mac_addr
+ 1)) << 24 | (u32
) us_config
;
41 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
],
43 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
],
46 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
48 rtlpriv
->cfg
->maps
[WCAMI
], target_content
);
49 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
50 "The Key ID is %d\n", entry_no
);
51 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
53 rtlpriv
->cfg
->maps
[RWCAM
], target_command
);
55 } else if (entry_i
== 1) {
57 target_content
= (u32
) (*(mac_addr
+ 5)) << 24 |
58 (u32
) (*(mac_addr
+ 4)) << 16 |
59 (u32
) (*(mac_addr
+ 3)) << 8 |
60 (u32
) (*(mac_addr
+ 2));
62 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
],
64 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
],
67 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
68 "WRITE A4: %x\n", target_content
);
69 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
70 "WRITE A0: %x\n", target_command
);
75 (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 3)) <<
76 24 | (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 2))
78 (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 1)) << 8
79 | (u32
) (*(key_cont_128
+ (entry_i
* 4 - 8) + 0));
81 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
],
83 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
],
86 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
87 "WRITE A4: %x\n", target_content
);
88 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
89 "WRITE A0: %x\n", target_command
);
93 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
94 "after set key, usconfig:%x\n", us_config
);
97 u8
rtl_cam_add_one_entry(struct ieee80211_hw
*hw
, u8
*mac_addr
,
98 u32 ul_key_id
, u32 ul_entry_idx
, u32 ul_enc_alg
,
99 u32 ul_default_key
, u8
*key_content
)
102 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
104 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
,
105 "EntryNo:%x, ulKeyId=%x, ulEncAlg=%x, ulUseDK=%x MacAddr %pM\n",
106 ul_entry_idx
, ul_key_id
, ul_enc_alg
,
107 ul_default_key
, mac_addr
);
109 if (ul_key_id
== TOTAL_CAM_ENTRY
) {
110 rtl_dbg(rtlpriv
, COMP_ERR
, DBG_WARNING
,
111 "ulKeyId exceed!\n");
115 if (ul_default_key
== 1)
116 us_config
= CFG_VALID
| ((u16
) (ul_enc_alg
) << 2);
118 us_config
= CFG_VALID
| ((ul_enc_alg
) << 2) | ul_key_id
;
120 rtl_cam_program_entry(hw
, ul_entry_idx
, mac_addr
,
121 (u8
*)key_content
, us_config
);
123 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
, "end\n");
128 EXPORT_SYMBOL(rtl_cam_add_one_entry
);
130 int rtl_cam_delete_one_entry(struct ieee80211_hw
*hw
,
131 u8
*mac_addr
, u32 ul_key_id
)
134 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
136 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
, "key_idx:%d\n", ul_key_id
);
138 ul_command
= ul_key_id
* CAM_CONTENT_COUNT
;
139 ul_command
= ul_command
| BIT(31) | BIT(16);
141 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
], 0);
142 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
144 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
,
145 "%s: WRITE A4: %x\n", __func__
, 0);
146 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
,
147 "%s: WRITE A0: %x\n", __func__
, ul_command
);
152 EXPORT_SYMBOL(rtl_cam_delete_one_entry
);
154 void rtl_cam_reset_all_entry(struct ieee80211_hw
*hw
)
157 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
159 ul_command
= BIT(31) | BIT(30);
160 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
162 EXPORT_SYMBOL(rtl_cam_reset_all_entry
);
164 void rtl_cam_mark_invalid(struct ieee80211_hw
*hw
, u8 uc_index
)
166 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
170 u32 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
172 switch (rtlpriv
->sec
.pairwise_enc_algorithm
) {
173 case WEP40_ENCRYPTION
:
174 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP40
];
176 case WEP104_ENCRYPTION
:
177 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP104
];
179 case TKIP_ENCRYPTION
:
180 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_TKIP
];
182 case AESCCMP_ENCRYPTION
:
183 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
186 ul_enc_algo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
189 ul_content
= (uc_index
& 3) | ((u16
) (ul_enc_algo
) << 2);
191 ul_content
|= BIT(15);
192 ul_command
= CAM_CONTENT_COUNT
* uc_index
;
193 ul_command
= ul_command
| BIT(31) | BIT(16);
195 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
], ul_content
);
196 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
198 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
,
199 "%s: WRITE A4: %x\n", __func__
, ul_content
);
200 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
,
201 "%s: WRITE A0: %x\n", __func__
, ul_command
);
203 EXPORT_SYMBOL(rtl_cam_mark_invalid
);
205 void rtl_cam_empty_entry(struct ieee80211_hw
*hw
, u8 uc_index
)
207 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
211 u32 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
214 switch (rtlpriv
->sec
.pairwise_enc_algorithm
) {
215 case WEP40_ENCRYPTION
:
216 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP40
];
218 case WEP104_ENCRYPTION
:
219 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_WEP104
];
221 case TKIP_ENCRYPTION
:
222 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_TKIP
];
224 case AESCCMP_ENCRYPTION
:
225 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
228 ul_encalgo
= rtlpriv
->cfg
->maps
[SEC_CAM_AES
];
231 for (entry_i
= 0; entry_i
< CAM_CONTENT_COUNT
; entry_i
++) {
235 (uc_index
& 0x03) | ((u16
) (ul_encalgo
) << 2);
236 ul_content
|= BIT(15);
242 ul_command
= CAM_CONTENT_COUNT
* uc_index
+ entry_i
;
243 ul_command
= ul_command
| BIT(31) | BIT(16);
245 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[WCAMI
], ul_content
);
246 rtl_write_dword(rtlpriv
, rtlpriv
->cfg
->maps
[RWCAM
], ul_command
);
248 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
249 "%s: WRITE A4: %x\n", __func__
, ul_content
);
250 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_LOUD
,
251 "%s: WRITE A0: %x\n", __func__
, ul_command
);
255 EXPORT_SYMBOL(rtl_cam_empty_entry
);
257 u8
rtl_cam_get_free_entry(struct ieee80211_hw
*hw
, u8
*sta_addr
)
259 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
260 u32 bitmap
= (rtlpriv
->sec
.hwsec_cam_bitmap
) >> 4;
264 if (NULL
== sta_addr
) {
265 pr_err("sta_addr is NULL.\n");
266 return TOTAL_CAM_ENTRY
;
268 /* Does STA already exist? */
269 for (i
= 4; i
< TOTAL_CAM_ENTRY
; i
++) {
270 addr
= rtlpriv
->sec
.hwsec_cam_sta_addr
[i
];
271 if (ether_addr_equal_unaligned(addr
, sta_addr
))
274 /* Get a free CAM entry. */
275 for (entry_idx
= 4; entry_idx
< TOTAL_CAM_ENTRY
; entry_idx
++) {
276 if ((bitmap
& BIT(0)) == 0) {
277 pr_err("-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
278 rtlpriv
->sec
.hwsec_cam_bitmap
, entry_idx
);
279 rtlpriv
->sec
.hwsec_cam_bitmap
|= BIT(0) << entry_idx
;
280 memcpy(rtlpriv
->sec
.hwsec_cam_sta_addr
[entry_idx
],
284 bitmap
= bitmap
>> 1;
286 return TOTAL_CAM_ENTRY
;
288 EXPORT_SYMBOL(rtl_cam_get_free_entry
);
290 void rtl_cam_del_entry(struct ieee80211_hw
*hw
, u8
*sta_addr
)
292 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
296 if (NULL
== sta_addr
) {
297 pr_err("sta_addr is NULL.\n");
301 if (is_zero_ether_addr(sta_addr
)) {
302 pr_err("sta_addr is %pM\n", sta_addr
);
305 /* Does STA already exist? */
306 for (i
= 4; i
< TOTAL_CAM_ENTRY
; i
++) {
307 addr
= rtlpriv
->sec
.hwsec_cam_sta_addr
[i
];
308 bitmap
= (rtlpriv
->sec
.hwsec_cam_bitmap
) >> i
;
309 if (((bitmap
& BIT(0)) == BIT(0)) &&
310 (ether_addr_equal_unaligned(addr
, sta_addr
))) {
311 /* Remove from HW Security CAM */
312 eth_zero_addr(rtlpriv
->sec
.hwsec_cam_sta_addr
[i
]);
313 rtlpriv
->sec
.hwsec_cam_bitmap
&= ~(BIT(0) << i
);
314 rtl_dbg(rtlpriv
, COMP_SEC
, DBG_DMESG
,
315 "&&&&&&&&&del entry %d\n", i
);
320 EXPORT_SYMBOL(rtl_cam_del_entry
);