1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2020  Realtek Corporation
3  */
4 
5 #include "cam.h"
6 #include "debug.h"
7 #include "fw.h"
8 #include "mac.h"
9 
10 static struct sk_buff *
rtw89_cam_get_sec_key_cmd(struct rtw89_dev * rtwdev,struct rtw89_sec_cam_entry * sec_cam,bool ext_key)11 rtw89_cam_get_sec_key_cmd(struct rtw89_dev *rtwdev,
12 			  struct rtw89_sec_cam_entry *sec_cam,
13 			  bool ext_key)
14 {
15 	struct sk_buff *skb;
16 	u32 cmd_len = H2C_SEC_CAM_LEN;
17 	u32 key32[4];
18 	u8 *cmd;
19 	int i, j;
20 
21 	skb = rtw89_fw_h2c_alloc_skb_with_hdr(rtwdev, cmd_len);
22 	if (!skb)
23 		return NULL;
24 
25 	skb_put_zero(skb, cmd_len);
26 
27 	for (i = 0; i < 4; i++) {
28 		j = i * 4;
29 		j += ext_key ? 16 : 0;
30 		key32[i] = FIELD_PREP(GENMASK(7, 0), sec_cam->key[j + 0]) |
31 			   FIELD_PREP(GENMASK(15, 8), sec_cam->key[j + 1]) |
32 			   FIELD_PREP(GENMASK(23, 16), sec_cam->key[j + 2]) |
33 			   FIELD_PREP(GENMASK(31, 24), sec_cam->key[j + 3]);
34 	}
35 
36 	cmd = skb->data;
37 	RTW89_SET_FWCMD_SEC_IDX(cmd, sec_cam->sec_cam_idx + (ext_key ? 1 : 0));
38 	RTW89_SET_FWCMD_SEC_OFFSET(cmd, sec_cam->offset);
39 	RTW89_SET_FWCMD_SEC_LEN(cmd, sec_cam->len);
40 	RTW89_SET_FWCMD_SEC_TYPE(cmd, sec_cam->type);
41 	RTW89_SET_FWCMD_SEC_EXT_KEY(cmd, ext_key);
42 	RTW89_SET_FWCMD_SEC_SPP_MODE(cmd, sec_cam->spp_mode);
43 	RTW89_SET_FWCMD_SEC_KEY0(cmd, key32[0]);
44 	RTW89_SET_FWCMD_SEC_KEY1(cmd, key32[1]);
45 	RTW89_SET_FWCMD_SEC_KEY2(cmd, key32[2]);
46 	RTW89_SET_FWCMD_SEC_KEY3(cmd, key32[3]);
47 
48 	return skb;
49 }
50 
rtw89_cam_send_sec_key_cmd(struct rtw89_dev * rtwdev,struct rtw89_sec_cam_entry * sec_cam)51 static int rtw89_cam_send_sec_key_cmd(struct rtw89_dev *rtwdev,
52 				      struct rtw89_sec_cam_entry *sec_cam)
53 {
54 	struct sk_buff *skb, *ext_skb;
55 	int ret;
56 
57 	skb = rtw89_cam_get_sec_key_cmd(rtwdev, sec_cam, false);
58 	if (!skb) {
59 		rtw89_err(rtwdev, "failed to get sec key command\n");
60 		return -ENOMEM;
61 	}
62 
63 	rtw89_h2c_pkt_set_hdr(rtwdev, skb,
64 			      FWCMD_TYPE_H2C,
65 			      H2C_CAT_MAC,
66 			      H2C_CL_MAC_SEC_CAM,
67 			      H2C_FUNC_MAC_SEC_UPD, 1, 0,
68 			      H2C_SEC_CAM_LEN);
69 	ret = rtw89_h2c_tx(rtwdev, skb, false);
70 	if (ret) {
71 		rtw89_err(rtwdev, "failed to send sec key h2c: %d\n", ret);
72 		dev_kfree_skb(skb);
73 		return ret;
74 	}
75 
76 	if (!sec_cam->ext_key)
77 		return 0;
78 
79 	ext_skb = rtw89_cam_get_sec_key_cmd(rtwdev, sec_cam, true);
80 	if (!ext_skb) {
81 		rtw89_err(rtwdev, "failed to get ext sec key command\n");
82 		return -ENOMEM;
83 	}
84 
85 	rtw89_h2c_pkt_set_hdr(rtwdev, ext_skb,
86 			      FWCMD_TYPE_H2C,
87 			      H2C_CAT_MAC,
88 			      H2C_CL_MAC_SEC_CAM,
89 			      H2C_FUNC_MAC_SEC_UPD,
90 			      1, 0, H2C_SEC_CAM_LEN);
91 	ret = rtw89_h2c_tx(rtwdev, ext_skb, false);
92 	if (ret) {
93 		rtw89_err(rtwdev, "failed to send ext sec key h2c: %d\n", ret);
94 		dev_kfree_skb(ext_skb);
95 		return ret;
96 	}
97 
98 	return 0;
99 }
100 
rtw89_cam_get_avail_sec_cam(struct rtw89_dev * rtwdev,u8 * sec_cam_idx,bool ext_key)101 static int rtw89_cam_get_avail_sec_cam(struct rtw89_dev *rtwdev,
102 				       u8 *sec_cam_idx, bool ext_key)
103 {
104 	const struct rtw89_chip_info *chip = rtwdev->chip;
105 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
106 	u8 sec_cam_num = chip->scam_num;
107 	u8 idx = 0;
108 
109 	if (!ext_key) {
110 		idx = find_first_zero_bit(cam_info->sec_cam_map, sec_cam_num);
111 		if (idx >= sec_cam_num)
112 			return -EBUSY;
113 
114 		set_bit(idx, cam_info->sec_cam_map);
115 		*sec_cam_idx = idx;
116 
117 		return 0;
118 	}
119 
120 again:
121 	idx = find_next_zero_bit(cam_info->sec_cam_map, sec_cam_num, idx);
122 	if (idx >= sec_cam_num - 1)
123 		return -EBUSY;
124 	/* ext keys need two cam entries for 256-bit key */
125 	if (test_bit(idx + 1, cam_info->sec_cam_map)) {
126 		idx++;
127 		goto again;
128 	}
129 
130 	set_bit(idx, cam_info->sec_cam_map);
131 	set_bit(idx + 1, cam_info->sec_cam_map);
132 	*sec_cam_idx = idx;
133 
134 	return 0;
135 }
136 
rtw89_cam_get_addr_cam_key_idx(struct rtw89_addr_cam_entry * addr_cam,struct rtw89_sec_cam_entry * sec_cam,struct ieee80211_key_conf * key,u8 * key_idx)137 static int rtw89_cam_get_addr_cam_key_idx(struct rtw89_addr_cam_entry *addr_cam,
138 					  struct rtw89_sec_cam_entry *sec_cam,
139 					  struct ieee80211_key_conf *key,
140 					  u8 *key_idx)
141 {
142 	u8 idx;
143 
144 	/* RTW89_ADDR_CAM_SEC_NONE	: not enabled
145 	 * RTW89_ADDR_CAM_SEC_ALL_UNI	: 0 - 6 unicast
146 	 * RTW89_ADDR_CAM_SEC_NORMAL	: 0 - 1 unicast, 2 - 4 group, 5 - 6 BIP
147 	 * RTW89_ADDR_CAM_SEC_4GROUP	: 0 - 1 unicast, 2 - 5 group, 6 BIP
148 	 */
149 	switch (addr_cam->sec_ent_mode) {
150 	case RTW89_ADDR_CAM_SEC_NONE:
151 		return -EINVAL;
152 	case RTW89_ADDR_CAM_SEC_ALL_UNI:
153 		idx = find_first_zero_bit(addr_cam->sec_cam_map,
154 					  RTW89_SEC_CAM_IN_ADDR_CAM);
155 		if (idx >= RTW89_SEC_CAM_IN_ADDR_CAM)
156 			return -EBUSY;
157 		*key_idx = idx;
158 		break;
159 	case RTW89_ADDR_CAM_SEC_NORMAL:
160 		if (sec_cam->type == RTW89_SEC_KEY_TYPE_BIP_CCMP128) {
161 			idx = find_next_zero_bit(addr_cam->sec_cam_map,
162 						 RTW89_SEC_CAM_IN_ADDR_CAM, 5);
163 			if (idx > 6)
164 				return -EBUSY;
165 			*key_idx = idx;
166 			break;
167 		}
168 
169 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
170 			idx = find_next_zero_bit(addr_cam->sec_cam_map,
171 						 RTW89_SEC_CAM_IN_ADDR_CAM, 0);
172 			if (idx > 1)
173 				return -EBUSY;
174 			*key_idx = idx;
175 			break;
176 		}
177 
178 		/* Group keys */
179 		idx = find_next_zero_bit(addr_cam->sec_cam_map,
180 					 RTW89_SEC_CAM_IN_ADDR_CAM, 2);
181 		if (idx > 4)
182 			return -EBUSY;
183 		*key_idx = idx;
184 		break;
185 	case RTW89_ADDR_CAM_SEC_4GROUP:
186 		if (sec_cam->type == RTW89_SEC_KEY_TYPE_BIP_CCMP128) {
187 			if (test_bit(6, addr_cam->sec_cam_map))
188 				return -EINVAL;
189 			*key_idx = 6;
190 			break;
191 		}
192 
193 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
194 			idx = find_next_zero_bit(addr_cam->sec_cam_map,
195 						 RTW89_SEC_CAM_IN_ADDR_CAM, 0);
196 			if (idx > 1)
197 				return -EBUSY;
198 			*key_idx = idx;
199 			break;
200 		}
201 
202 		/* Group keys */
203 		idx = find_next_zero_bit(addr_cam->sec_cam_map,
204 					 RTW89_SEC_CAM_IN_ADDR_CAM, 2);
205 		if (idx > 5)
206 			return -EBUSY;
207 		*key_idx = idx;
208 		break;
209 	}
210 
211 	return 0;
212 }
213 
rtw89_cam_detach_sec_cam(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct rtw89_sec_cam_entry * sec_cam,bool inform_fw)214 static int rtw89_cam_detach_sec_cam(struct rtw89_dev *rtwdev,
215 				    struct ieee80211_vif *vif,
216 				    struct ieee80211_sta *sta,
217 				    const struct rtw89_sec_cam_entry *sec_cam,
218 				    bool inform_fw)
219 {
220 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
221 	struct rtw89_vif *rtwvif;
222 	struct rtw89_addr_cam_entry *addr_cam;
223 	unsigned int i;
224 	int ret = 0;
225 
226 	if (!vif) {
227 		rtw89_err(rtwdev, "No iface for deleting sec cam\n");
228 		return -EINVAL;
229 	}
230 
231 	rtwvif = (struct rtw89_vif *)vif->drv_priv;
232 	addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
233 
234 	for_each_set_bit(i, addr_cam->sec_cam_map, RTW89_SEC_CAM_IN_ADDR_CAM) {
235 		if (addr_cam->sec_ent[i] != sec_cam->sec_cam_idx)
236 			continue;
237 
238 		clear_bit(i, addr_cam->sec_cam_map);
239 	}
240 
241 	if (inform_fw) {
242 		ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif, rtwsta);
243 		if (ret)
244 			rtw89_err(rtwdev,
245 				  "failed to update dctl cam del key: %d\n", ret);
246 		ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
247 		if (ret)
248 			rtw89_err(rtwdev, "failed to update cam del key: %d\n", ret);
249 	}
250 
251 	return ret;
252 }
253 
rtw89_cam_attach_sec_cam(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,struct rtw89_sec_cam_entry * sec_cam)254 static int rtw89_cam_attach_sec_cam(struct rtw89_dev *rtwdev,
255 				    struct ieee80211_vif *vif,
256 				    struct ieee80211_sta *sta,
257 				    struct ieee80211_key_conf *key,
258 				    struct rtw89_sec_cam_entry *sec_cam)
259 {
260 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
261 	struct rtw89_vif *rtwvif;
262 	struct rtw89_addr_cam_entry *addr_cam;
263 	u8 key_idx = 0;
264 	int ret;
265 
266 	if (!vif) {
267 		rtw89_err(rtwdev, "No iface for adding sec cam\n");
268 		return -EINVAL;
269 	}
270 
271 	rtwvif = (struct rtw89_vif *)vif->drv_priv;
272 	addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
273 
274 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
275 	    key->cipher == WLAN_CIPHER_SUITE_WEP104)
276 		addr_cam->sec_ent_mode = RTW89_ADDR_CAM_SEC_ALL_UNI;
277 
278 	ret = rtw89_cam_get_addr_cam_key_idx(addr_cam, sec_cam, key, &key_idx);
279 	if (ret) {
280 		rtw89_err(rtwdev, "failed to get addr cam key idx %d, %d\n",
281 			  addr_cam->sec_ent_mode, sec_cam->type);
282 		return ret;
283 	}
284 
285 	addr_cam->sec_ent_keyid[key_idx] = key->keyidx;
286 	addr_cam->sec_ent[key_idx] = sec_cam->sec_cam_idx;
287 	set_bit(key_idx, addr_cam->sec_cam_map);
288 	ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif, rtwsta);
289 	if (ret) {
290 		rtw89_err(rtwdev, "failed to update dctl cam sec entry: %d\n",
291 			  ret);
292 		return ret;
293 	}
294 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
295 	if (ret) {
296 		rtw89_err(rtwdev, "failed to update addr cam sec entry: %d\n",
297 			  ret);
298 		clear_bit(key_idx, addr_cam->sec_cam_map);
299 		return ret;
300 	}
301 
302 	return 0;
303 }
304 
rtw89_cam_sec_key_install(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,u8 hw_key_type,bool ext_key)305 static int rtw89_cam_sec_key_install(struct rtw89_dev *rtwdev,
306 				     struct ieee80211_vif *vif,
307 				     struct ieee80211_sta *sta,
308 				     struct ieee80211_key_conf *key,
309 				     u8 hw_key_type, bool ext_key)
310 {
311 	struct rtw89_sec_cam_entry *sec_cam = NULL;
312 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
313 	u8 sec_cam_idx;
314 	int ret;
315 
316 	/* maximum key length 256-bit */
317 	if (key->keylen > 32) {
318 		rtw89_err(rtwdev, "invalid sec key length %d\n", key->keylen);
319 		return -EINVAL;
320 	}
321 
322 	ret = rtw89_cam_get_avail_sec_cam(rtwdev, &sec_cam_idx, ext_key);
323 	if (ret) {
324 		rtw89_warn(rtwdev, "no available sec cam: %d ext: %d\n",
325 			   ret, ext_key);
326 		return ret;
327 	}
328 
329 	sec_cam = kzalloc(sizeof(*sec_cam), GFP_KERNEL);
330 	if (!sec_cam) {
331 		ret = -ENOMEM;
332 		goto err_release_cam;
333 	}
334 
335 	key->hw_key_idx = sec_cam_idx;
336 	cam_info->sec_entries[sec_cam_idx] = sec_cam;
337 
338 	sec_cam->sec_cam_idx = sec_cam_idx;
339 	sec_cam->type = hw_key_type;
340 	sec_cam->len = RTW89_SEC_CAM_LEN;
341 	sec_cam->ext_key = ext_key;
342 	memcpy(sec_cam->key, key->key, key->keylen);
343 	ret = rtw89_cam_send_sec_key_cmd(rtwdev, sec_cam);
344 	if (ret) {
345 		rtw89_err(rtwdev, "failed to send sec key cmd: %d\n", ret);
346 		goto err_release_cam;
347 	}
348 
349 	/* associate with addr cam */
350 	ret = rtw89_cam_attach_sec_cam(rtwdev, vif, sta, key, sec_cam);
351 	if (ret) {
352 		rtw89_err(rtwdev, "failed to attach sec cam: %d\n", ret);
353 		goto err_release_cam;
354 	}
355 
356 	return 0;
357 
358 err_release_cam:
359 	cam_info->sec_entries[sec_cam_idx] = NULL;
360 	kfree(sec_cam);
361 	clear_bit(sec_cam_idx, cam_info->sec_cam_map);
362 	if (ext_key)
363 		clear_bit(sec_cam_idx + 1, cam_info->sec_cam_map);
364 
365 	return ret;
366 }
367 
rtw89_cam_sec_key_add(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)368 int rtw89_cam_sec_key_add(struct rtw89_dev *rtwdev,
369 			  struct ieee80211_vif *vif,
370 			  struct ieee80211_sta *sta,
371 			  struct ieee80211_key_conf *key)
372 {
373 	const struct rtw89_chip_info *chip = rtwdev->chip;
374 	u8 hw_key_type;
375 	bool ext_key = false;
376 	int ret;
377 
378 	switch (key->cipher) {
379 	case WLAN_CIPHER_SUITE_WEP40:
380 		hw_key_type = RTW89_SEC_KEY_TYPE_WEP40;
381 		break;
382 	case WLAN_CIPHER_SUITE_WEP104:
383 		hw_key_type = RTW89_SEC_KEY_TYPE_WEP104;
384 		break;
385 	case WLAN_CIPHER_SUITE_CCMP:
386 		hw_key_type = RTW89_SEC_KEY_TYPE_CCMP128;
387 		if (!chip->hw_mgmt_tx_encrypt)
388 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
389 		break;
390 	case WLAN_CIPHER_SUITE_CCMP_256:
391 		hw_key_type = RTW89_SEC_KEY_TYPE_CCMP256;
392 		if (!chip->hw_mgmt_tx_encrypt)
393 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
394 		ext_key = true;
395 		break;
396 	case WLAN_CIPHER_SUITE_GCMP:
397 		hw_key_type = RTW89_SEC_KEY_TYPE_GCMP128;
398 		if (!chip->hw_mgmt_tx_encrypt)
399 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
400 		break;
401 	case WLAN_CIPHER_SUITE_GCMP_256:
402 		hw_key_type = RTW89_SEC_KEY_TYPE_GCMP256;
403 		if (!chip->hw_mgmt_tx_encrypt)
404 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
405 		ext_key = true;
406 		break;
407 	case WLAN_CIPHER_SUITE_AES_CMAC:
408 		hw_key_type = RTW89_SEC_KEY_TYPE_BIP_CCMP128;
409 		break;
410 	default:
411 		return -EOPNOTSUPP;
412 	}
413 
414 	if (!chip->hw_sec_hdr)
415 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
416 
417 	ret = rtw89_cam_sec_key_install(rtwdev, vif, sta, key, hw_key_type,
418 					ext_key);
419 	if (ret) {
420 		rtw89_err(rtwdev, "failed to install key type %d ext %d: %d\n",
421 			  hw_key_type, ext_key, ret);
422 		return ret;
423 	}
424 
425 	return 0;
426 }
427 
rtw89_cam_sec_key_del(struct rtw89_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,bool inform_fw)428 int rtw89_cam_sec_key_del(struct rtw89_dev *rtwdev,
429 			  struct ieee80211_vif *vif,
430 			  struct ieee80211_sta *sta,
431 			  struct ieee80211_key_conf *key,
432 			  bool inform_fw)
433 {
434 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
435 	const struct rtw89_sec_cam_entry *sec_cam;
436 	u8 sec_cam_idx;
437 	int ret;
438 
439 	sec_cam_idx = key->hw_key_idx;
440 	sec_cam = cam_info->sec_entries[sec_cam_idx];
441 	if (!sec_cam)
442 		return -EINVAL;
443 
444 	ret = rtw89_cam_detach_sec_cam(rtwdev, vif, sta, sec_cam, inform_fw);
445 
446 	/* clear valid bit in addr cam will disable sec cam,
447 	 * so we don't need to send H2C command again
448 	 */
449 	cam_info->sec_entries[sec_cam_idx] = NULL;
450 	clear_bit(sec_cam_idx, cam_info->sec_cam_map);
451 	if (sec_cam->ext_key)
452 		clear_bit(sec_cam_idx + 1, cam_info->sec_cam_map);
453 
454 	kfree(sec_cam);
455 
456 	return ret;
457 }
458 
rtw89_cam_reset_key_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)459 static void rtw89_cam_reset_key_iter(struct ieee80211_hw *hw,
460 				     struct ieee80211_vif *vif,
461 				     struct ieee80211_sta *sta,
462 				     struct ieee80211_key_conf *key,
463 				     void *data)
464 {
465 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
466 
467 	rtw89_cam_sec_key_del(rtwdev, vif, sta, key, false);
468 }
469 
rtw89_cam_deinit_addr_cam(struct rtw89_dev * rtwdev,struct rtw89_addr_cam_entry * addr_cam)470 void rtw89_cam_deinit_addr_cam(struct rtw89_dev *rtwdev,
471 			       struct rtw89_addr_cam_entry *addr_cam)
472 {
473 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
474 
475 	addr_cam->valid = false;
476 	clear_bit(addr_cam->addr_cam_idx, cam_info->addr_cam_map);
477 }
478 
rtw89_cam_deinit_bssid_cam(struct rtw89_dev * rtwdev,struct rtw89_bssid_cam_entry * bssid_cam)479 void rtw89_cam_deinit_bssid_cam(struct rtw89_dev *rtwdev,
480 				struct rtw89_bssid_cam_entry *bssid_cam)
481 {
482 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
483 
484 	bssid_cam->valid = false;
485 	clear_bit(bssid_cam->bssid_cam_idx, cam_info->bssid_cam_map);
486 }
487 
rtw89_cam_deinit(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)488 void rtw89_cam_deinit(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
489 {
490 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
491 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
492 
493 	rtw89_cam_deinit_addr_cam(rtwdev, addr_cam);
494 	rtw89_cam_deinit_bssid_cam(rtwdev, bssid_cam);
495 }
496 
rtw89_cam_reset_keys(struct rtw89_dev * rtwdev)497 void rtw89_cam_reset_keys(struct rtw89_dev *rtwdev)
498 {
499 	rcu_read_lock();
500 	ieee80211_iter_keys_rcu(rtwdev->hw, NULL, rtw89_cam_reset_key_iter, rtwdev);
501 	rcu_read_unlock();
502 }
503 
rtw89_cam_get_avail_addr_cam(struct rtw89_dev * rtwdev,u8 * addr_cam_idx)504 static int rtw89_cam_get_avail_addr_cam(struct rtw89_dev *rtwdev,
505 					u8 *addr_cam_idx)
506 {
507 	const struct rtw89_chip_info *chip = rtwdev->chip;
508 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
509 	u8 addr_cam_num = chip->acam_num;
510 	u8 idx;
511 
512 	idx = find_first_zero_bit(cam_info->addr_cam_map, addr_cam_num);
513 	if (idx >= addr_cam_num)
514 		return -EBUSY;
515 
516 	set_bit(idx, cam_info->addr_cam_map);
517 	*addr_cam_idx = idx;
518 
519 	return 0;
520 }
521 
rtw89_get_addr_cam_entry_size(struct rtw89_dev * rtwdev)522 static u8 rtw89_get_addr_cam_entry_size(struct rtw89_dev *rtwdev)
523 {
524 	const struct rtw89_chip_info *chip = rtwdev->chip;
525 
526 	switch (chip->chip_id) {
527 	case RTL8852A:
528 	case RTL8852B:
529 	case RTL8851B:
530 	case RTL8852BT:
531 		return ADDR_CAM_ENT_SIZE;
532 	default:
533 		return ADDR_CAM_ENT_SHORT_SIZE;
534 	}
535 }
536 
rtw89_cam_init_addr_cam(struct rtw89_dev * rtwdev,struct rtw89_addr_cam_entry * addr_cam,const struct rtw89_bssid_cam_entry * bssid_cam)537 int rtw89_cam_init_addr_cam(struct rtw89_dev *rtwdev,
538 			    struct rtw89_addr_cam_entry *addr_cam,
539 			    const struct rtw89_bssid_cam_entry *bssid_cam)
540 {
541 	u8 addr_cam_idx;
542 	int i;
543 	int ret;
544 
545 	if (unlikely(addr_cam->valid)) {
546 		rtw89_debug(rtwdev, RTW89_DBG_FW,
547 			    "addr cam is already valid; skip init\n");
548 		return 0;
549 	}
550 
551 	ret = rtw89_cam_get_avail_addr_cam(rtwdev, &addr_cam_idx);
552 	if (ret) {
553 		rtw89_err(rtwdev, "failed to get available addr cam\n");
554 		return ret;
555 	}
556 
557 	addr_cam->addr_cam_idx = addr_cam_idx;
558 	addr_cam->len = rtw89_get_addr_cam_entry_size(rtwdev);
559 	addr_cam->offset = 0;
560 	addr_cam->valid = true;
561 	addr_cam->addr_mask = 0;
562 	addr_cam->mask_sel = RTW89_NO_MSK;
563 	addr_cam->sec_ent_mode = RTW89_ADDR_CAM_SEC_NORMAL;
564 	bitmap_zero(addr_cam->sec_cam_map, RTW89_SEC_CAM_IN_ADDR_CAM);
565 
566 	for (i = 0; i < RTW89_SEC_CAM_IN_ADDR_CAM; i++) {
567 		addr_cam->sec_ent_keyid[i] = 0;
568 		addr_cam->sec_ent[i] = 0;
569 	}
570 
571 	/* associate addr cam with bssid cam */
572 	addr_cam->bssid_cam_idx = bssid_cam->bssid_cam_idx;
573 
574 	return 0;
575 }
576 
rtw89_cam_get_avail_bssid_cam(struct rtw89_dev * rtwdev,u8 * bssid_cam_idx)577 static int rtw89_cam_get_avail_bssid_cam(struct rtw89_dev *rtwdev,
578 					 u8 *bssid_cam_idx)
579 {
580 	const struct rtw89_chip_info *chip = rtwdev->chip;
581 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
582 	u8 bssid_cam_num = chip->bcam_num;
583 	u8 idx;
584 
585 	idx = find_first_zero_bit(cam_info->bssid_cam_map, bssid_cam_num);
586 	if (idx >= bssid_cam_num)
587 		return -EBUSY;
588 
589 	set_bit(idx, cam_info->bssid_cam_map);
590 	*bssid_cam_idx = idx;
591 
592 	return 0;
593 }
594 
rtw89_cam_init_bssid_cam(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_bssid_cam_entry * bssid_cam,const u8 * bssid)595 int rtw89_cam_init_bssid_cam(struct rtw89_dev *rtwdev,
596 			     struct rtw89_vif *rtwvif,
597 			     struct rtw89_bssid_cam_entry *bssid_cam,
598 			     const u8 *bssid)
599 {
600 	u8 bssid_cam_idx;
601 	int ret;
602 
603 	if (unlikely(bssid_cam->valid)) {
604 		rtw89_debug(rtwdev, RTW89_DBG_FW,
605 			    "bssid cam is already valid; skip init\n");
606 		return 0;
607 	}
608 
609 	ret = rtw89_cam_get_avail_bssid_cam(rtwdev, &bssid_cam_idx);
610 	if (ret) {
611 		rtw89_err(rtwdev, "failed to get available bssid cam\n");
612 		return ret;
613 	}
614 
615 	bssid_cam->bssid_cam_idx = bssid_cam_idx;
616 	bssid_cam->phy_idx = rtwvif->phy_idx;
617 	bssid_cam->len = BSSID_CAM_ENT_SIZE;
618 	bssid_cam->offset = 0;
619 	bssid_cam->valid = true;
620 	ether_addr_copy(bssid_cam->bssid, bssid);
621 
622 	return 0;
623 }
624 
rtw89_cam_bssid_changed(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)625 void rtw89_cam_bssid_changed(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
626 {
627 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
628 
629 	ether_addr_copy(bssid_cam->bssid, rtwvif->bssid);
630 }
631 
rtw89_cam_init(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)632 int rtw89_cam_init(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
633 {
634 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
635 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
636 	int ret;
637 
638 	ret = rtw89_cam_init_bssid_cam(rtwdev, rtwvif, bssid_cam, rtwvif->bssid);
639 	if (ret) {
640 		rtw89_err(rtwdev, "failed to init bssid cam\n");
641 		return ret;
642 	}
643 
644 	ret = rtw89_cam_init_addr_cam(rtwdev, addr_cam, bssid_cam);
645 	if (ret) {
646 		rtw89_err(rtwdev, "failed to init addr cam\n");
647 		return ret;
648 	}
649 
650 	return 0;
651 }
652 
rtw89_cam_fill_bssid_cam_info(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_sta * rtwsta,u8 * cmd)653 int rtw89_cam_fill_bssid_cam_info(struct rtw89_dev *rtwdev,
654 				  struct rtw89_vif *rtwvif,
655 				  struct rtw89_sta *rtwsta, u8 *cmd)
656 {
657 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
658 	struct rtw89_bssid_cam_entry *bssid_cam = rtw89_get_bssid_cam_of(rtwvif, rtwsta);
659 	u8 bss_color = vif->bss_conf.he_bss_color.color;
660 	u8 bss_mask;
661 
662 	if (vif->bss_conf.nontransmitted)
663 		bss_mask = RTW89_BSSID_MATCH_5_BYTES;
664 	else
665 		bss_mask = RTW89_BSSID_MATCH_ALL;
666 
667 	FWCMD_SET_ADDR_BSSID_IDX(cmd, bssid_cam->bssid_cam_idx);
668 	FWCMD_SET_ADDR_BSSID_OFFSET(cmd, bssid_cam->offset);
669 	FWCMD_SET_ADDR_BSSID_LEN(cmd, bssid_cam->len);
670 	FWCMD_SET_ADDR_BSSID_VALID(cmd, bssid_cam->valid);
671 	FWCMD_SET_ADDR_BSSID_MASK(cmd, bss_mask);
672 	FWCMD_SET_ADDR_BSSID_BB_SEL(cmd, bssid_cam->phy_idx);
673 	FWCMD_SET_ADDR_BSSID_BSS_COLOR(cmd, bss_color);
674 
675 	FWCMD_SET_ADDR_BSSID_BSSID0(cmd, bssid_cam->bssid[0]);
676 	FWCMD_SET_ADDR_BSSID_BSSID1(cmd, bssid_cam->bssid[1]);
677 	FWCMD_SET_ADDR_BSSID_BSSID2(cmd, bssid_cam->bssid[2]);
678 	FWCMD_SET_ADDR_BSSID_BSSID3(cmd, bssid_cam->bssid[3]);
679 	FWCMD_SET_ADDR_BSSID_BSSID4(cmd, bssid_cam->bssid[4]);
680 	FWCMD_SET_ADDR_BSSID_BSSID5(cmd, bssid_cam->bssid[5]);
681 
682 	return 0;
683 }
684 
rtw89_cam_addr_hash(u8 start,const u8 * addr)685 static u8 rtw89_cam_addr_hash(u8 start, const u8 *addr)
686 {
687 	u8 hash = 0;
688 	u8 i;
689 
690 	for (i = start; i < ETH_ALEN; i++)
691 		hash ^= addr[i];
692 
693 	return hash;
694 }
695 
rtw89_cam_fill_addr_cam_info(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_sta * rtwsta,const u8 * scan_mac_addr,u8 * cmd)696 void rtw89_cam_fill_addr_cam_info(struct rtw89_dev *rtwdev,
697 				  struct rtw89_vif *rtwvif,
698 				  struct rtw89_sta *rtwsta,
699 				  const u8 *scan_mac_addr,
700 				  u8 *cmd)
701 {
702 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
703 	struct rtw89_addr_cam_entry *addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
704 	struct ieee80211_sta *sta = rtwsta_to_sta_safe(rtwsta);
705 	const u8 *sma = scan_mac_addr ? scan_mac_addr : rtwvif->mac_addr;
706 	u8 sma_hash, tma_hash, addr_msk_start;
707 	u8 sma_start = 0;
708 	u8 tma_start = 0;
709 	u8 *tma = sta ? sta->addr : rtwvif->bssid;
710 
711 	if (addr_cam->addr_mask != 0) {
712 		addr_msk_start = __ffs(addr_cam->addr_mask);
713 		if (addr_cam->mask_sel == RTW89_SMA)
714 			sma_start = addr_msk_start;
715 		else if (addr_cam->mask_sel == RTW89_TMA)
716 			tma_start = addr_msk_start;
717 	}
718 	sma_hash = rtw89_cam_addr_hash(sma_start, sma);
719 	tma_hash = rtw89_cam_addr_hash(tma_start, tma);
720 
721 	FWCMD_SET_ADDR_IDX(cmd, addr_cam->addr_cam_idx);
722 	FWCMD_SET_ADDR_OFFSET(cmd, addr_cam->offset);
723 	FWCMD_SET_ADDR_LEN(cmd, addr_cam->len);
724 
725 	FWCMD_SET_ADDR_VALID(cmd, addr_cam->valid);
726 	FWCMD_SET_ADDR_NET_TYPE(cmd, rtwvif->net_type);
727 	FWCMD_SET_ADDR_BCN_HIT_COND(cmd, rtwvif->bcn_hit_cond);
728 	FWCMD_SET_ADDR_HIT_RULE(cmd, rtwvif->hit_rule);
729 	FWCMD_SET_ADDR_BB_SEL(cmd, rtwvif->phy_idx);
730 	FWCMD_SET_ADDR_ADDR_MASK(cmd, addr_cam->addr_mask);
731 	FWCMD_SET_ADDR_MASK_SEL(cmd, addr_cam->mask_sel);
732 	FWCMD_SET_ADDR_SMA_HASH(cmd, sma_hash);
733 	FWCMD_SET_ADDR_TMA_HASH(cmd, tma_hash);
734 
735 	FWCMD_SET_ADDR_BSSID_CAM_IDX(cmd, addr_cam->bssid_cam_idx);
736 
737 	FWCMD_SET_ADDR_SMA0(cmd, sma[0]);
738 	FWCMD_SET_ADDR_SMA1(cmd, sma[1]);
739 	FWCMD_SET_ADDR_SMA2(cmd, sma[2]);
740 	FWCMD_SET_ADDR_SMA3(cmd, sma[3]);
741 	FWCMD_SET_ADDR_SMA4(cmd, sma[4]);
742 	FWCMD_SET_ADDR_SMA5(cmd, sma[5]);
743 
744 	FWCMD_SET_ADDR_TMA0(cmd, tma[0]);
745 	FWCMD_SET_ADDR_TMA1(cmd, tma[1]);
746 	FWCMD_SET_ADDR_TMA2(cmd, tma[2]);
747 	FWCMD_SET_ADDR_TMA3(cmd, tma[3]);
748 	FWCMD_SET_ADDR_TMA4(cmd, tma[4]);
749 	FWCMD_SET_ADDR_TMA5(cmd, tma[5]);
750 
751 	FWCMD_SET_ADDR_PORT_INT(cmd, rtwvif->port);
752 	FWCMD_SET_ADDR_TSF_SYNC(cmd, rtwvif->port);
753 	FWCMD_SET_ADDR_TF_TRS(cmd, rtwvif->trigger);
754 	FWCMD_SET_ADDR_LSIG_TXOP(cmd, rtwvif->lsig_txop);
755 	FWCMD_SET_ADDR_TGT_IND(cmd, rtwvif->tgt_ind);
756 	FWCMD_SET_ADDR_FRM_TGT_IND(cmd, rtwvif->frm_tgt_ind);
757 	FWCMD_SET_ADDR_MACID(cmd, rtwsta ? rtwsta->mac_id : rtwvif->mac_id);
758 	if (rtwvif->net_type == RTW89_NET_TYPE_INFRA)
759 		FWCMD_SET_ADDR_AID12(cmd, vif->cfg.aid & 0xfff);
760 	else if (rtwvif->net_type == RTW89_NET_TYPE_AP_MODE)
761 		FWCMD_SET_ADDR_AID12(cmd, sta ? sta->aid & 0xfff : 0);
762 	FWCMD_SET_ADDR_WOL_PATTERN(cmd, rtwvif->wowlan_pattern);
763 	FWCMD_SET_ADDR_WOL_UC(cmd, rtwvif->wowlan_uc);
764 	FWCMD_SET_ADDR_WOL_MAGIC(cmd, rtwvif->wowlan_magic);
765 	FWCMD_SET_ADDR_WAPI(cmd, addr_cam->wapi);
766 	FWCMD_SET_ADDR_SEC_ENT_MODE(cmd, addr_cam->sec_ent_mode);
767 	FWCMD_SET_ADDR_SEC_ENT0_KEYID(cmd, addr_cam->sec_ent_keyid[0]);
768 	FWCMD_SET_ADDR_SEC_ENT1_KEYID(cmd, addr_cam->sec_ent_keyid[1]);
769 	FWCMD_SET_ADDR_SEC_ENT2_KEYID(cmd, addr_cam->sec_ent_keyid[2]);
770 	FWCMD_SET_ADDR_SEC_ENT3_KEYID(cmd, addr_cam->sec_ent_keyid[3]);
771 	FWCMD_SET_ADDR_SEC_ENT4_KEYID(cmd, addr_cam->sec_ent_keyid[4]);
772 	FWCMD_SET_ADDR_SEC_ENT5_KEYID(cmd, addr_cam->sec_ent_keyid[5]);
773 	FWCMD_SET_ADDR_SEC_ENT6_KEYID(cmd, addr_cam->sec_ent_keyid[6]);
774 
775 	FWCMD_SET_ADDR_SEC_ENT_VALID(cmd, addr_cam->sec_cam_map[0] & 0xff);
776 	FWCMD_SET_ADDR_SEC_ENT0(cmd, addr_cam->sec_ent[0]);
777 	FWCMD_SET_ADDR_SEC_ENT1(cmd, addr_cam->sec_ent[1]);
778 	FWCMD_SET_ADDR_SEC_ENT2(cmd, addr_cam->sec_ent[2]);
779 	FWCMD_SET_ADDR_SEC_ENT3(cmd, addr_cam->sec_ent[3]);
780 	FWCMD_SET_ADDR_SEC_ENT4(cmd, addr_cam->sec_ent[4]);
781 	FWCMD_SET_ADDR_SEC_ENT5(cmd, addr_cam->sec_ent[5]);
782 	FWCMD_SET_ADDR_SEC_ENT6(cmd, addr_cam->sec_ent[6]);
783 }
784 
rtw89_cam_fill_dctl_sec_cam_info_v1(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_sta * rtwsta,struct rtw89_h2c_dctlinfo_ud_v1 * h2c)785 void rtw89_cam_fill_dctl_sec_cam_info_v1(struct rtw89_dev *rtwdev,
786 					 struct rtw89_vif *rtwvif,
787 					 struct rtw89_sta *rtwsta,
788 					 struct rtw89_h2c_dctlinfo_ud_v1 *h2c)
789 {
790 	struct rtw89_addr_cam_entry *addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
791 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
792 	u8 *ptk_tx_iv = rtw_wow->key_info.ptk_tx_iv;
793 
794 	h2c->c0 = le32_encode_bits(rtwsta ? rtwsta->mac_id : rtwvif->mac_id,
795 				   DCTLINFO_V1_C0_MACID) |
796 		  le32_encode_bits(1, DCTLINFO_V1_C0_OP);
797 
798 	h2c->w4 = le32_encode_bits(addr_cam->sec_ent_keyid[0],
799 				   DCTLINFO_V1_W4_SEC_ENT0_KEYID) |
800 		  le32_encode_bits(addr_cam->sec_ent_keyid[1],
801 				   DCTLINFO_V1_W4_SEC_ENT1_KEYID) |
802 		  le32_encode_bits(addr_cam->sec_ent_keyid[2],
803 				   DCTLINFO_V1_W4_SEC_ENT2_KEYID) |
804 		  le32_encode_bits(addr_cam->sec_ent_keyid[3],
805 				   DCTLINFO_V1_W4_SEC_ENT3_KEYID) |
806 		  le32_encode_bits(addr_cam->sec_ent_keyid[4],
807 				   DCTLINFO_V1_W4_SEC_ENT4_KEYID) |
808 		  le32_encode_bits(addr_cam->sec_ent_keyid[5],
809 				   DCTLINFO_V1_W4_SEC_ENT5_KEYID) |
810 		  le32_encode_bits(addr_cam->sec_ent_keyid[6],
811 				   DCTLINFO_V1_W4_SEC_ENT6_KEYID);
812 	h2c->m4 = cpu_to_le32(DCTLINFO_V1_W4_SEC_ENT0_KEYID |
813 			      DCTLINFO_V1_W4_SEC_ENT1_KEYID |
814 			      DCTLINFO_V1_W4_SEC_ENT2_KEYID |
815 			      DCTLINFO_V1_W4_SEC_ENT3_KEYID |
816 			      DCTLINFO_V1_W4_SEC_ENT4_KEYID |
817 			      DCTLINFO_V1_W4_SEC_ENT5_KEYID |
818 			      DCTLINFO_V1_W4_SEC_ENT6_KEYID);
819 
820 	h2c->w5 = le32_encode_bits(addr_cam->sec_cam_map[0] & 0xff,
821 				   DCTLINFO_V1_W5_SEC_ENT_VALID) |
822 		  le32_encode_bits(addr_cam->sec_ent[0],
823 				   DCTLINFO_V1_W5_SEC_ENT0) |
824 		  le32_encode_bits(addr_cam->sec_ent[1],
825 				   DCTLINFO_V1_W5_SEC_ENT1) |
826 		  le32_encode_bits(addr_cam->sec_ent[2],
827 				   DCTLINFO_V1_W5_SEC_ENT2);
828 	h2c->m5 = cpu_to_le32(DCTLINFO_V1_W5_SEC_ENT_VALID |
829 			      DCTLINFO_V1_W5_SEC_ENT0      |
830 			      DCTLINFO_V1_W5_SEC_ENT1      |
831 			      DCTLINFO_V1_W5_SEC_ENT2);
832 
833 	h2c->w6 = le32_encode_bits(addr_cam->sec_ent[3],
834 				   DCTLINFO_V1_W6_SEC_ENT3) |
835 		  le32_encode_bits(addr_cam->sec_ent[4],
836 				   DCTLINFO_V1_W6_SEC_ENT4) |
837 		  le32_encode_bits(addr_cam->sec_ent[5],
838 				   DCTLINFO_V1_W6_SEC_ENT5) |
839 		  le32_encode_bits(addr_cam->sec_ent[6],
840 				   DCTLINFO_V1_W6_SEC_ENT6);
841 	h2c->m6 = cpu_to_le32(DCTLINFO_V1_W6_SEC_ENT3 |
842 			      DCTLINFO_V1_W6_SEC_ENT4 |
843 			      DCTLINFO_V1_W6_SEC_ENT5 |
844 			      DCTLINFO_V1_W6_SEC_ENT6);
845 
846 	if (rtw_wow->ptk_alg) {
847 		h2c->w0 = le32_encode_bits(ptk_tx_iv[0] | ptk_tx_iv[1] << 8,
848 					   DCTLINFO_V1_W0_AES_IV_L);
849 		h2c->m0 = cpu_to_le32(DCTLINFO_V1_W0_AES_IV_L);
850 
851 		h2c->w1 = le32_encode_bits(ptk_tx_iv[4]       |
852 					   ptk_tx_iv[5] << 8  |
853 					   ptk_tx_iv[6] << 16 |
854 					   ptk_tx_iv[7] << 24,
855 					   DCTLINFO_V1_W1_AES_IV_H);
856 		h2c->m1 = cpu_to_le32(DCTLINFO_V1_W1_AES_IV_H);
857 
858 		h2c->w4 |= le32_encode_bits(rtw_wow->ptk_keyidx,
859 					    DCTLINFO_V1_W4_SEC_KEY_ID);
860 		h2c->m4 |= cpu_to_le32(DCTLINFO_V1_W4_SEC_KEY_ID);
861 	}
862 }
863 
rtw89_cam_fill_dctl_sec_cam_info_v2(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct rtw89_sta * rtwsta,struct rtw89_h2c_dctlinfo_ud_v2 * h2c)864 void rtw89_cam_fill_dctl_sec_cam_info_v2(struct rtw89_dev *rtwdev,
865 					 struct rtw89_vif *rtwvif,
866 					 struct rtw89_sta *rtwsta,
867 					 struct rtw89_h2c_dctlinfo_ud_v2 *h2c)
868 {
869 	struct rtw89_addr_cam_entry *addr_cam = rtw89_get_addr_cam_of(rtwvif, rtwsta);
870 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
871 	u8 *ptk_tx_iv = rtw_wow->key_info.ptk_tx_iv;
872 
873 	h2c->c0 = le32_encode_bits(rtwsta ? rtwsta->mac_id : rtwvif->mac_id,
874 				   DCTLINFO_V2_C0_MACID) |
875 		  le32_encode_bits(1, DCTLINFO_V2_C0_OP);
876 
877 	h2c->w4 = le32_encode_bits(addr_cam->sec_ent_keyid[0],
878 				   DCTLINFO_V2_W4_SEC_ENT0_KEYID) |
879 		  le32_encode_bits(addr_cam->sec_ent_keyid[1],
880 				   DCTLINFO_V2_W4_SEC_ENT1_KEYID) |
881 		  le32_encode_bits(addr_cam->sec_ent_keyid[2],
882 				   DCTLINFO_V2_W4_SEC_ENT2_KEYID) |
883 		  le32_encode_bits(addr_cam->sec_ent_keyid[3],
884 				   DCTLINFO_V2_W4_SEC_ENT3_KEYID) |
885 		  le32_encode_bits(addr_cam->sec_ent_keyid[4],
886 				   DCTLINFO_V2_W4_SEC_ENT4_KEYID) |
887 		  le32_encode_bits(addr_cam->sec_ent_keyid[5],
888 				   DCTLINFO_V2_W4_SEC_ENT5_KEYID) |
889 		  le32_encode_bits(addr_cam->sec_ent_keyid[6],
890 				   DCTLINFO_V2_W4_SEC_ENT6_KEYID);
891 	h2c->m4 = cpu_to_le32(DCTLINFO_V2_W4_SEC_ENT0_KEYID |
892 			      DCTLINFO_V2_W4_SEC_ENT1_KEYID |
893 			      DCTLINFO_V2_W4_SEC_ENT2_KEYID |
894 			      DCTLINFO_V2_W4_SEC_ENT3_KEYID |
895 			      DCTLINFO_V2_W4_SEC_ENT4_KEYID |
896 			      DCTLINFO_V2_W4_SEC_ENT5_KEYID |
897 			      DCTLINFO_V2_W4_SEC_ENT6_KEYID);
898 
899 	h2c->w5 = le32_encode_bits(addr_cam->sec_cam_map[0],
900 				   DCTLINFO_V2_W5_SEC_ENT_VALID_V1) |
901 		  le32_encode_bits(addr_cam->sec_ent[0],
902 				   DCTLINFO_V2_W5_SEC_ENT0_V1);
903 	h2c->m5 = cpu_to_le32(DCTLINFO_V2_W5_SEC_ENT_VALID_V1 |
904 			      DCTLINFO_V2_W5_SEC_ENT0_V1);
905 
906 	h2c->w6 = le32_encode_bits(addr_cam->sec_ent[1],
907 				   DCTLINFO_V2_W6_SEC_ENT1_V1) |
908 		  le32_encode_bits(addr_cam->sec_ent[2],
909 				   DCTLINFO_V2_W6_SEC_ENT2_V1) |
910 		  le32_encode_bits(addr_cam->sec_ent[3],
911 				   DCTLINFO_V2_W6_SEC_ENT3_V1) |
912 		  le32_encode_bits(addr_cam->sec_ent[4],
913 				   DCTLINFO_V2_W6_SEC_ENT4_V1);
914 	h2c->m6 = cpu_to_le32(DCTLINFO_V2_W6_SEC_ENT1_V1 |
915 			      DCTLINFO_V2_W6_SEC_ENT2_V1 |
916 			      DCTLINFO_V2_W6_SEC_ENT3_V1 |
917 			      DCTLINFO_V2_W6_SEC_ENT4_V1);
918 
919 	h2c->w7 = le32_encode_bits(addr_cam->sec_ent[5],
920 				   DCTLINFO_V2_W7_SEC_ENT5_V1) |
921 		  le32_encode_bits(addr_cam->sec_ent[6],
922 				   DCTLINFO_V2_W7_SEC_ENT6_V1);
923 	h2c->m7 = cpu_to_le32(DCTLINFO_V2_W7_SEC_ENT5_V1 |
924 			      DCTLINFO_V2_W7_SEC_ENT6_V1);
925 
926 	if (rtw_wow->ptk_alg) {
927 		h2c->w0 = le32_encode_bits(ptk_tx_iv[0] | ptk_tx_iv[1] << 8,
928 					   DCTLINFO_V2_W0_AES_IV_L);
929 		h2c->m0 = cpu_to_le32(DCTLINFO_V2_W0_AES_IV_L);
930 
931 		h2c->w1 = le32_encode_bits(ptk_tx_iv[4] |
932 					   ptk_tx_iv[5] << 8 |
933 					   ptk_tx_iv[6] << 16 |
934 					   ptk_tx_iv[7] << 24,
935 					   DCTLINFO_V2_W1_AES_IV_H);
936 		h2c->m1 = cpu_to_le32(DCTLINFO_V2_W1_AES_IV_H);
937 
938 		h2c->w4 |= le32_encode_bits(rtw_wow->ptk_keyidx,
939 					    DCTLINFO_V2_W4_SEC_KEY_ID);
940 		h2c->m4 |= cpu_to_le32(DCTLINFO_V2_W4_SEC_KEY_ID);
941 	}
942 }
943