1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012 Realtek Corporation.*/
3
4 #include "wifi.h"
5 #include "cam.h"
6 #include <linux/export.h>
7
rtl_cam_reset_sec_info(struct ieee80211_hw * hw)8 void rtl_cam_reset_sec_info(struct ieee80211_hw *hw)
9 {
10 struct rtl_priv *rtlpriv = rtl_priv(hw);
11
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;
18 }
19
rtl_cam_program_entry(struct ieee80211_hw * hw,u32 entry_no,u8 * mac_addr,u8 * key_cont_128,u16 us_config)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)
22 {
23 struct rtl_priv *rtlpriv = rtl_priv(hw);
24
25 u32 target_command;
26 u32 target_content = 0;
27 int entry_i;
28
29 RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_DMESG, "Key content :",
30 key_cont_128, 16);
31
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);
36
37 if (entry_i == 0) {
38 target_content = (u32) (*(mac_addr + 0)) << 16 |
39 (u32) (*(mac_addr + 1)) << 24 | (u32) us_config;
40
41 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
42 target_content);
43 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
44 target_command);
45
46 rtl_dbg(rtlpriv, COMP_SEC, DBG_LOUD,
47 "WRITE %x: %x\n",
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,
52 "WRITE %x: %x\n",
53 rtlpriv->cfg->maps[RWCAM], target_command);
54
55 } else if (entry_i == 1) {
56
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));
61
62 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
63 target_content);
64 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
65 target_command);
66
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);
71
72 } else {
73
74 target_content =
75 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 3)) <<
76 24 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 2))
77 << 16 |
78 (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 1)) << 8
79 | (u32) (*(key_cont_128 + (entry_i * 4 - 8) + 0));
80
81 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI],
82 target_content);
83 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM],
84 target_command);
85
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);
90 }
91 }
92
93 rtl_dbg(rtlpriv, COMP_SEC, DBG_LOUD,
94 "after set key, usconfig:%x\n", us_config);
95 }
96
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)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)
100 {
101 u32 us_config;
102 struct rtl_priv *rtlpriv = rtl_priv(hw);
103
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);
108
109 if (ul_key_id == TOTAL_CAM_ENTRY) {
110 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
111 "ulKeyId exceed!\n");
112 return 0;
113 }
114
115 if (ul_default_key == 1)
116 us_config = CFG_VALID | ((u16) (ul_enc_alg) << 2);
117 else
118 us_config = CFG_VALID | ((ul_enc_alg) << 2) | ul_key_id;
119
120 rtl_cam_program_entry(hw, ul_entry_idx, mac_addr,
121 (u8 *)key_content, us_config);
122
123 rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "end\n");
124
125 return 1;
126
127 }
128 EXPORT_SYMBOL(rtl_cam_add_one_entry);
129
rtl_cam_delete_one_entry(struct ieee80211_hw * hw,u8 * mac_addr,u32 ul_key_id)130 int rtl_cam_delete_one_entry(struct ieee80211_hw *hw,
131 u8 *mac_addr, u32 ul_key_id)
132 {
133 u32 ul_command;
134 struct rtl_priv *rtlpriv = rtl_priv(hw);
135
136 rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "key_idx:%d\n", ul_key_id);
137
138 ul_command = ul_key_id * CAM_CONTENT_COUNT;
139 ul_command = ul_command | BIT(31) | BIT(16);
140
141 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], 0);
142 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
143
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);
148
149 return 0;
150
151 }
152 EXPORT_SYMBOL(rtl_cam_delete_one_entry);
153
rtl_cam_reset_all_entry(struct ieee80211_hw * hw)154 void rtl_cam_reset_all_entry(struct ieee80211_hw *hw)
155 {
156 u32 ul_command;
157 struct rtl_priv *rtlpriv = rtl_priv(hw);
158
159 ul_command = BIT(31) | BIT(30);
160 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
161 }
162 EXPORT_SYMBOL(rtl_cam_reset_all_entry);
163
rtl_cam_mark_invalid(struct ieee80211_hw * hw,u8 uc_index)164 void rtl_cam_mark_invalid(struct ieee80211_hw *hw, u8 uc_index)
165 {
166 struct rtl_priv *rtlpriv = rtl_priv(hw);
167
168 u32 ul_command;
169 u32 ul_content;
170 u32 ul_enc_algo;
171
172 switch (rtlpriv->sec.pairwise_enc_algorithm) {
173 case WEP40_ENCRYPTION:
174 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
175 break;
176 case WEP104_ENCRYPTION:
177 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
178 break;
179 case TKIP_ENCRYPTION:
180 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
181 break;
182 case AESCCMP_ENCRYPTION:
183 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
184 break;
185 default:
186 ul_enc_algo = rtlpriv->cfg->maps[SEC_CAM_AES];
187 }
188
189 ul_content = (uc_index & 3) | ((u16) (ul_enc_algo) << 2);
190
191 ul_content |= BIT(15);
192 ul_command = CAM_CONTENT_COUNT * uc_index;
193 ul_command = ul_command | BIT(31) | BIT(16);
194
195 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
196 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
197
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);
202 }
203 EXPORT_SYMBOL(rtl_cam_mark_invalid);
204
rtl_cam_empty_entry(struct ieee80211_hw * hw,u8 uc_index)205 void rtl_cam_empty_entry(struct ieee80211_hw *hw, u8 uc_index)
206 {
207 struct rtl_priv *rtlpriv = rtl_priv(hw);
208
209 u32 ul_command;
210 u32 ul_content;
211 u32 ul_encalgo;
212 u8 entry_i;
213
214 switch (rtlpriv->sec.pairwise_enc_algorithm) {
215 case WEP40_ENCRYPTION:
216 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP40];
217 break;
218 case WEP104_ENCRYPTION:
219 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_WEP104];
220 break;
221 case TKIP_ENCRYPTION:
222 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_TKIP];
223 break;
224 case AESCCMP_ENCRYPTION:
225 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
226 break;
227 default:
228 ul_encalgo = rtlpriv->cfg->maps[SEC_CAM_AES];
229 }
230
231 for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
232
233 if (entry_i == 0) {
234 ul_content =
235 (uc_index & 0x03) | ((u16) (ul_encalgo) << 2);
236 ul_content |= BIT(15);
237
238 } else {
239 ul_content = 0;
240 }
241
242 ul_command = CAM_CONTENT_COUNT * uc_index + entry_i;
243 ul_command = ul_command | BIT(31) | BIT(16);
244
245 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[WCAMI], ul_content);
246 rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[RWCAM], ul_command);
247
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);
252 }
253
254 }
255 EXPORT_SYMBOL(rtl_cam_empty_entry);
256
rtl_cam_get_free_entry(struct ieee80211_hw * hw,u8 * sta_addr)257 u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
258 {
259 struct rtl_priv *rtlpriv = rtl_priv(hw);
260 u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
261 u8 entry_idx = 0;
262 u8 i, *addr;
263
264 if (NULL == sta_addr) {
265 pr_err("sta_addr is NULL.\n");
266 return TOTAL_CAM_ENTRY;
267 }
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))
272 return i;
273 }
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],
281 sta_addr, ETH_ALEN);
282 return entry_idx;
283 }
284 bitmap = bitmap >> 1;
285 }
286 return TOTAL_CAM_ENTRY;
287 }
288 EXPORT_SYMBOL(rtl_cam_get_free_entry);
289
rtl_cam_del_entry(struct ieee80211_hw * hw,u8 * sta_addr)290 void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
291 {
292 struct rtl_priv *rtlpriv = rtl_priv(hw);
293 u32 bitmap;
294 u8 i, *addr;
295
296 if (NULL == sta_addr) {
297 pr_err("sta_addr is NULL.\n");
298 return;
299 }
300
301 if (is_zero_ether_addr(sta_addr)) {
302 pr_err("sta_addr is %pM\n", sta_addr);
303 return;
304 }
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);
316 }
317 }
318 return;
319 }
320 EXPORT_SYMBOL(rtl_cam_del_entry);
321