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 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
47 				 "WRITE %x: %x\n",
48 				 rtlpriv->cfg->maps[WCAMI], target_content);
49 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
50 				 "The Key ID is %d\n", entry_no);
51 			RT_TRACE(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 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
68 				 "WRITE A4: %x\n", target_content);
69 			RT_TRACE(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 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
87 				 "WRITE A4: %x\n", target_content);
88 			RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
89 				 "WRITE A0: %x\n", target_command);
90 		}
91 	}
92 
93 	RT_TRACE(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 	RT_TRACE(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 		RT_TRACE(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 	RT_TRACE(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 	RT_TRACE(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 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
145 		 "rtl_cam_delete_one_entry(): WRITE A4: %x\n", 0);
146 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
147 		 "rtl_cam_delete_one_entry(): WRITE A0: %x\n", 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 = rtlpriv->cfg->maps[SEC_CAM_AES];
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 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
199 		 "rtl_cam_mark_invalid(): WRITE A4: %x\n", ul_content);
200 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
201 		 "rtl_cam_mark_invalid(): WRITE A0: %x\n", 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 = rtlpriv->cfg->maps[SEC_CAM_AES];
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 		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
249 			 "rtl_cam_empty_entry(): WRITE A4: %x\n",
250 			 ul_content);
251 		RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
252 			 "rtl_cam_empty_entry(): WRITE A0: %x\n",
253 			 ul_command);
254 	}
255 
256 }
257 EXPORT_SYMBOL(rtl_cam_empty_entry);
258 
rtl_cam_get_free_entry(struct ieee80211_hw * hw,u8 * sta_addr)259 u8 rtl_cam_get_free_entry(struct ieee80211_hw *hw, u8 *sta_addr)
260 {
261 	struct rtl_priv *rtlpriv = rtl_priv(hw);
262 	u32 bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> 4;
263 	u8 entry_idx = 0;
264 	u8 i, *addr;
265 
266 	if (NULL == sta_addr) {
267 		pr_err("sta_addr is NULL.\n");
268 		return TOTAL_CAM_ENTRY;
269 	}
270 	/* Does STA already exist? */
271 	for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
272 		addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
273 		if (ether_addr_equal_unaligned(addr, sta_addr))
274 			return i;
275 	}
276 	/* Get a free CAM entry. */
277 	for (entry_idx = 4; entry_idx < TOTAL_CAM_ENTRY; entry_idx++) {
278 		if ((bitmap & BIT(0)) == 0) {
279 			pr_err("-----hwsec_cam_bitmap: 0x%x entry_idx=%d\n",
280 			       rtlpriv->sec.hwsec_cam_bitmap, entry_idx);
281 			rtlpriv->sec.hwsec_cam_bitmap |= BIT(0) << entry_idx;
282 			memcpy(rtlpriv->sec.hwsec_cam_sta_addr[entry_idx],
283 			       sta_addr, ETH_ALEN);
284 			return entry_idx;
285 		}
286 		bitmap = bitmap >> 1;
287 	}
288 	return TOTAL_CAM_ENTRY;
289 }
290 EXPORT_SYMBOL(rtl_cam_get_free_entry);
291 
rtl_cam_del_entry(struct ieee80211_hw * hw,u8 * sta_addr)292 void rtl_cam_del_entry(struct ieee80211_hw *hw, u8 *sta_addr)
293 {
294 	struct rtl_priv *rtlpriv = rtl_priv(hw);
295 	u32 bitmap;
296 	u8 i, *addr;
297 
298 	if (NULL == sta_addr) {
299 		pr_err("sta_addr is NULL.\n");
300 		return;
301 	}
302 
303 	if (is_zero_ether_addr(sta_addr)) {
304 		pr_err("sta_addr is %pM\n", sta_addr);
305 		return;
306 	}
307 	/* Does STA already exist? */
308 	for (i = 4; i < TOTAL_CAM_ENTRY; i++) {
309 		addr = rtlpriv->sec.hwsec_cam_sta_addr[i];
310 		bitmap = (rtlpriv->sec.hwsec_cam_bitmap) >> i;
311 		if (((bitmap & BIT(0)) == BIT(0)) &&
312 		    (ether_addr_equal_unaligned(addr, sta_addr))) {
313 			/* Remove from HW Security CAM */
314 			eth_zero_addr(rtlpriv->sec.hwsec_cam_sta_addr[i]);
315 			rtlpriv->sec.hwsec_cam_bitmap &= ~(BIT(0) << i);
316 			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
317 				 "&&&&&&&&&del entry %d\n", i);
318 		}
319 	}
320 	return;
321 }
322 EXPORT_SYMBOL(rtl_cam_del_entry);
323