xref: /linux/drivers/net/wireless/realtek/rtw89/cam.c (revision cdd30ebb1b9f36159d66f088b61aee264e649d7a)
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 *
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 
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 
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 
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 
214 static int __rtw89_cam_detach_sec_cam(struct rtw89_dev *rtwdev,
215 				      struct rtw89_vif_link *rtwvif_link,
216 				      struct rtw89_sta_link *rtwsta_link,
217 				      const struct rtw89_sec_cam_entry *sec_cam,
218 				      bool inform_fw)
219 {
220 	struct rtw89_addr_cam_entry *addr_cam;
221 	unsigned int i;
222 	int ret = 0;
223 
224 	addr_cam = rtw89_get_addr_cam_of(rtwvif_link, rtwsta_link);
225 
226 	for_each_set_bit(i, addr_cam->sec_cam_map, RTW89_SEC_CAM_IN_ADDR_CAM) {
227 		if (addr_cam->sec_ent[i] != sec_cam->sec_cam_idx)
228 			continue;
229 
230 		clear_bit(i, addr_cam->sec_cam_map);
231 	}
232 
233 	if (inform_fw) {
234 		ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif_link, rtwsta_link);
235 		if (ret)
236 			rtw89_err(rtwdev,
237 				  "failed to update dctl cam del key: %d\n", ret);
238 		ret = rtw89_fw_h2c_cam(rtwdev, rtwvif_link, rtwsta_link, NULL);
239 		if (ret)
240 			rtw89_err(rtwdev, "failed to update cam del key: %d\n", ret);
241 	}
242 
243 	return ret;
244 }
245 
246 static int __rtw89_cam_attach_sec_cam(struct rtw89_dev *rtwdev,
247 				      struct rtw89_vif_link *rtwvif_link,
248 				      struct rtw89_sta_link *rtwsta_link,
249 				      struct ieee80211_key_conf *key,
250 				      struct rtw89_sec_cam_entry *sec_cam)
251 {
252 	struct rtw89_addr_cam_entry *addr_cam;
253 	u8 key_idx = 0;
254 	int ret;
255 
256 	addr_cam = rtw89_get_addr_cam_of(rtwvif_link, rtwsta_link);
257 
258 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
259 	    key->cipher == WLAN_CIPHER_SUITE_WEP104)
260 		addr_cam->sec_ent_mode = RTW89_ADDR_CAM_SEC_ALL_UNI;
261 
262 	ret = rtw89_cam_get_addr_cam_key_idx(addr_cam, sec_cam, key, &key_idx);
263 	if (ret) {
264 		rtw89_err(rtwdev, "failed to get addr cam key idx %d, %d\n",
265 			  addr_cam->sec_ent_mode, sec_cam->type);
266 		return ret;
267 	}
268 
269 	addr_cam->sec_ent_keyid[key_idx] = key->keyidx;
270 	addr_cam->sec_ent[key_idx] = sec_cam->sec_cam_idx;
271 	set_bit(key_idx, addr_cam->sec_cam_map);
272 	ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif_link, rtwsta_link);
273 	if (ret) {
274 		rtw89_err(rtwdev, "failed to update dctl cam sec entry: %d\n",
275 			  ret);
276 		return ret;
277 	}
278 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif_link, rtwsta_link, NULL);
279 	if (ret) {
280 		rtw89_err(rtwdev, "failed to update addr cam sec entry: %d\n",
281 			  ret);
282 		clear_bit(key_idx, addr_cam->sec_cam_map);
283 		return ret;
284 	}
285 
286 	return 0;
287 }
288 
289 static int rtw89_cam_detach_sec_cam(struct rtw89_dev *rtwdev,
290 				    struct ieee80211_vif *vif,
291 				    struct ieee80211_sta *sta,
292 				    const struct rtw89_sec_cam_entry *sec_cam,
293 				    bool inform_fw)
294 {
295 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
296 	struct rtw89_sta_link *rtwsta_link;
297 	struct rtw89_vif_link *rtwvif_link;
298 	struct rtw89_vif *rtwvif;
299 	unsigned int link_id;
300 	int ret;
301 
302 	if (!vif) {
303 		rtw89_err(rtwdev, "No iface for deleting sec cam\n");
304 		return -EINVAL;
305 	}
306 
307 	rtwvif = vif_to_rtwvif(vif);
308 
309 	rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) {
310 		rtwsta_link = rtwsta ? rtwsta->links[link_id] : NULL;
311 		if (rtwsta && !rtwsta_link)
312 			continue;
313 
314 		ret = __rtw89_cam_detach_sec_cam(rtwdev, rtwvif_link, rtwsta_link,
315 						 sec_cam, inform_fw);
316 		if (ret)
317 			return ret;
318 	}
319 
320 	return 0;
321 }
322 
323 static int rtw89_cam_attach_sec_cam(struct rtw89_dev *rtwdev,
324 				    struct ieee80211_vif *vif,
325 				    struct ieee80211_sta *sta,
326 				    struct ieee80211_key_conf *key,
327 				    struct rtw89_sec_cam_entry *sec_cam)
328 {
329 	struct rtw89_sta *rtwsta = sta_to_rtwsta_safe(sta);
330 	struct rtw89_sta_link *rtwsta_link;
331 	struct rtw89_vif_link *rtwvif_link;
332 	struct rtw89_vif *rtwvif;
333 	unsigned int link_id;
334 	int key_link_id;
335 	int ret;
336 
337 	if (!vif) {
338 		rtw89_err(rtwdev, "No iface for adding sec cam\n");
339 		return -EINVAL;
340 	}
341 
342 	rtwvif = vif_to_rtwvif(vif);
343 
344 	key_link_id = ieee80211_vif_is_mld(vif) ? key->link_id : 0;
345 	if (key_link_id >= 0) {
346 		rtwvif_link = rtwvif->links[key_link_id];
347 		rtwsta_link = rtwsta ? rtwsta->links[key_link_id] : NULL;
348 
349 		if (!rtwvif_link || (rtwsta && !rtwsta_link)) {
350 			rtw89_err(rtwdev, "No drv link for adding sec cam\n");
351 			return -ENOLINK;
352 		}
353 
354 		return __rtw89_cam_attach_sec_cam(rtwdev, rtwvif_link,
355 						  rtwsta_link, key, sec_cam);
356 	}
357 
358 	/* key_link_id < 0: MLD pairwise key */
359 	if (!rtwsta) {
360 		rtw89_err(rtwdev, "No sta for adding MLD pairwise sec cam\n");
361 		return -EINVAL;
362 	}
363 
364 	rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) {
365 		rtwvif_link = rtwsta_link->rtwvif_link;
366 		ret = __rtw89_cam_attach_sec_cam(rtwdev, rtwvif_link,
367 						 rtwsta_link, key, sec_cam);
368 		if (ret)
369 			return ret;
370 	}
371 
372 	return 0;
373 }
374 
375 static int rtw89_cam_sec_key_install(struct rtw89_dev *rtwdev,
376 				     struct ieee80211_vif *vif,
377 				     struct ieee80211_sta *sta,
378 				     struct ieee80211_key_conf *key,
379 				     u8 hw_key_type, bool ext_key)
380 {
381 	struct rtw89_sec_cam_entry *sec_cam = NULL;
382 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
383 	u8 sec_cam_idx;
384 	int ret;
385 
386 	/* maximum key length 256-bit */
387 	if (key->keylen > 32) {
388 		rtw89_err(rtwdev, "invalid sec key length %d\n", key->keylen);
389 		return -EINVAL;
390 	}
391 
392 	ret = rtw89_cam_get_avail_sec_cam(rtwdev, &sec_cam_idx, ext_key);
393 	if (ret) {
394 		rtw89_warn(rtwdev, "no available sec cam: %d ext: %d\n",
395 			   ret, ext_key);
396 		return ret;
397 	}
398 
399 	sec_cam = kzalloc(sizeof(*sec_cam), GFP_KERNEL);
400 	if (!sec_cam) {
401 		ret = -ENOMEM;
402 		goto err_release_cam;
403 	}
404 
405 	key->hw_key_idx = sec_cam_idx;
406 	cam_info->sec_entries[sec_cam_idx] = sec_cam;
407 
408 	sec_cam->sec_cam_idx = sec_cam_idx;
409 	sec_cam->type = hw_key_type;
410 	sec_cam->len = RTW89_SEC_CAM_LEN;
411 	sec_cam->ext_key = ext_key;
412 	memcpy(sec_cam->key, key->key, key->keylen);
413 	ret = rtw89_cam_send_sec_key_cmd(rtwdev, sec_cam);
414 	if (ret) {
415 		rtw89_err(rtwdev, "failed to send sec key cmd: %d\n", ret);
416 		goto err_release_cam;
417 	}
418 
419 	/* associate with addr cam */
420 	ret = rtw89_cam_attach_sec_cam(rtwdev, vif, sta, key, sec_cam);
421 	if (ret) {
422 		rtw89_err(rtwdev, "failed to attach sec cam: %d\n", ret);
423 		goto err_release_cam;
424 	}
425 
426 	return 0;
427 
428 err_release_cam:
429 	cam_info->sec_entries[sec_cam_idx] = NULL;
430 	kfree(sec_cam);
431 	clear_bit(sec_cam_idx, cam_info->sec_cam_map);
432 	if (ext_key)
433 		clear_bit(sec_cam_idx + 1, cam_info->sec_cam_map);
434 
435 	return ret;
436 }
437 
438 int rtw89_cam_sec_key_add(struct rtw89_dev *rtwdev,
439 			  struct ieee80211_vif *vif,
440 			  struct ieee80211_sta *sta,
441 			  struct ieee80211_key_conf *key)
442 {
443 	const struct rtw89_chip_info *chip = rtwdev->chip;
444 	u8 hw_key_type;
445 	bool ext_key = false;
446 	int ret;
447 
448 	switch (key->cipher) {
449 	case WLAN_CIPHER_SUITE_WEP40:
450 		hw_key_type = RTW89_SEC_KEY_TYPE_WEP40;
451 		break;
452 	case WLAN_CIPHER_SUITE_WEP104:
453 		hw_key_type = RTW89_SEC_KEY_TYPE_WEP104;
454 		break;
455 	case WLAN_CIPHER_SUITE_CCMP:
456 		hw_key_type = RTW89_SEC_KEY_TYPE_CCMP128;
457 		if (!chip->hw_mgmt_tx_encrypt)
458 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
459 		break;
460 	case WLAN_CIPHER_SUITE_CCMP_256:
461 		hw_key_type = RTW89_SEC_KEY_TYPE_CCMP256;
462 		if (!chip->hw_mgmt_tx_encrypt)
463 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
464 		ext_key = true;
465 		break;
466 	case WLAN_CIPHER_SUITE_GCMP:
467 		hw_key_type = RTW89_SEC_KEY_TYPE_GCMP128;
468 		if (!chip->hw_mgmt_tx_encrypt)
469 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
470 		break;
471 	case WLAN_CIPHER_SUITE_GCMP_256:
472 		hw_key_type = RTW89_SEC_KEY_TYPE_GCMP256;
473 		if (!chip->hw_mgmt_tx_encrypt)
474 			key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
475 		ext_key = true;
476 		break;
477 	case WLAN_CIPHER_SUITE_AES_CMAC:
478 		hw_key_type = RTW89_SEC_KEY_TYPE_BIP_CCMP128;
479 		break;
480 	default:
481 		return -EOPNOTSUPP;
482 	}
483 
484 	if (!chip->hw_sec_hdr)
485 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
486 
487 	ret = rtw89_cam_sec_key_install(rtwdev, vif, sta, key, hw_key_type,
488 					ext_key);
489 	if (ret) {
490 		rtw89_err(rtwdev, "failed to install key type %d ext %d: %d\n",
491 			  hw_key_type, ext_key, ret);
492 		return ret;
493 	}
494 
495 	return 0;
496 }
497 
498 int rtw89_cam_sec_key_del(struct rtw89_dev *rtwdev,
499 			  struct ieee80211_vif *vif,
500 			  struct ieee80211_sta *sta,
501 			  struct ieee80211_key_conf *key,
502 			  bool inform_fw)
503 {
504 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
505 	const struct rtw89_sec_cam_entry *sec_cam;
506 	u8 sec_cam_idx;
507 	int ret;
508 
509 	sec_cam_idx = key->hw_key_idx;
510 	sec_cam = cam_info->sec_entries[sec_cam_idx];
511 	if (!sec_cam)
512 		return -EINVAL;
513 
514 	ret = rtw89_cam_detach_sec_cam(rtwdev, vif, sta, sec_cam, inform_fw);
515 
516 	/* clear valid bit in addr cam will disable sec cam,
517 	 * so we don't need to send H2C command again
518 	 */
519 	cam_info->sec_entries[sec_cam_idx] = NULL;
520 	clear_bit(sec_cam_idx, cam_info->sec_cam_map);
521 	if (sec_cam->ext_key)
522 		clear_bit(sec_cam_idx + 1, cam_info->sec_cam_map);
523 
524 	kfree(sec_cam);
525 
526 	return ret;
527 }
528 
529 static void rtw89_cam_reset_key_iter(struct ieee80211_hw *hw,
530 				     struct ieee80211_vif *vif,
531 				     struct ieee80211_sta *sta,
532 				     struct ieee80211_key_conf *key,
533 				     void *data)
534 {
535 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
536 
537 	rtw89_cam_sec_key_del(rtwdev, vif, sta, key, false);
538 }
539 
540 void rtw89_cam_deinit_addr_cam(struct rtw89_dev *rtwdev,
541 			       struct rtw89_addr_cam_entry *addr_cam)
542 {
543 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
544 
545 	addr_cam->valid = false;
546 	clear_bit(addr_cam->addr_cam_idx, cam_info->addr_cam_map);
547 }
548 
549 void rtw89_cam_deinit_bssid_cam(struct rtw89_dev *rtwdev,
550 				struct rtw89_bssid_cam_entry *bssid_cam)
551 {
552 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
553 
554 	bssid_cam->valid = false;
555 	clear_bit(bssid_cam->bssid_cam_idx, cam_info->bssid_cam_map);
556 }
557 
558 void rtw89_cam_deinit(struct rtw89_dev *rtwdev, struct rtw89_vif_link *rtwvif_link)
559 {
560 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif_link->addr_cam;
561 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif_link->bssid_cam;
562 
563 	rtw89_cam_deinit_addr_cam(rtwdev, addr_cam);
564 	rtw89_cam_deinit_bssid_cam(rtwdev, bssid_cam);
565 }
566 
567 void rtw89_cam_reset_keys(struct rtw89_dev *rtwdev)
568 {
569 	rcu_read_lock();
570 	ieee80211_iter_keys_rcu(rtwdev->hw, NULL, rtw89_cam_reset_key_iter, rtwdev);
571 	rcu_read_unlock();
572 }
573 
574 static int rtw89_cam_get_avail_addr_cam(struct rtw89_dev *rtwdev,
575 					u8 *addr_cam_idx)
576 {
577 	const struct rtw89_chip_info *chip = rtwdev->chip;
578 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
579 	u8 addr_cam_num = chip->acam_num;
580 	u8 idx;
581 
582 	idx = find_first_zero_bit(cam_info->addr_cam_map, addr_cam_num);
583 	if (idx >= addr_cam_num)
584 		return -EBUSY;
585 
586 	set_bit(idx, cam_info->addr_cam_map);
587 	*addr_cam_idx = idx;
588 
589 	return 0;
590 }
591 
592 static u8 rtw89_get_addr_cam_entry_size(struct rtw89_dev *rtwdev)
593 {
594 	const struct rtw89_chip_info *chip = rtwdev->chip;
595 
596 	switch (chip->chip_id) {
597 	case RTL8852A:
598 	case RTL8852B:
599 	case RTL8851B:
600 	case RTL8852BT:
601 		return ADDR_CAM_ENT_SIZE;
602 	default:
603 		return ADDR_CAM_ENT_SHORT_SIZE;
604 	}
605 }
606 
607 int rtw89_cam_init_addr_cam(struct rtw89_dev *rtwdev,
608 			    struct rtw89_addr_cam_entry *addr_cam,
609 			    const struct rtw89_bssid_cam_entry *bssid_cam)
610 {
611 	u8 addr_cam_idx;
612 	int i;
613 	int ret;
614 
615 	if (unlikely(addr_cam->valid)) {
616 		rtw89_debug(rtwdev, RTW89_DBG_FW,
617 			    "addr cam is already valid; skip init\n");
618 		return 0;
619 	}
620 
621 	ret = rtw89_cam_get_avail_addr_cam(rtwdev, &addr_cam_idx);
622 	if (ret) {
623 		rtw89_err(rtwdev, "failed to get available addr cam\n");
624 		return ret;
625 	}
626 
627 	addr_cam->addr_cam_idx = addr_cam_idx;
628 	addr_cam->len = rtw89_get_addr_cam_entry_size(rtwdev);
629 	addr_cam->offset = 0;
630 	addr_cam->valid = true;
631 	addr_cam->addr_mask = 0;
632 	addr_cam->mask_sel = RTW89_NO_MSK;
633 	addr_cam->sec_ent_mode = RTW89_ADDR_CAM_SEC_NORMAL;
634 	bitmap_zero(addr_cam->sec_cam_map, RTW89_SEC_CAM_IN_ADDR_CAM);
635 
636 	for (i = 0; i < RTW89_SEC_CAM_IN_ADDR_CAM; i++) {
637 		addr_cam->sec_ent_keyid[i] = 0;
638 		addr_cam->sec_ent[i] = 0;
639 	}
640 
641 	/* associate addr cam with bssid cam */
642 	addr_cam->bssid_cam_idx = bssid_cam->bssid_cam_idx;
643 
644 	return 0;
645 }
646 
647 static int rtw89_cam_get_avail_bssid_cam(struct rtw89_dev *rtwdev,
648 					 u8 *bssid_cam_idx)
649 {
650 	const struct rtw89_chip_info *chip = rtwdev->chip;
651 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
652 	u8 bssid_cam_num = chip->bcam_num;
653 	u8 idx;
654 
655 	idx = find_first_zero_bit(cam_info->bssid_cam_map, bssid_cam_num);
656 	if (idx >= bssid_cam_num)
657 		return -EBUSY;
658 
659 	set_bit(idx, cam_info->bssid_cam_map);
660 	*bssid_cam_idx = idx;
661 
662 	return 0;
663 }
664 
665 int rtw89_cam_init_bssid_cam(struct rtw89_dev *rtwdev,
666 			     struct rtw89_vif_link *rtwvif_link,
667 			     struct rtw89_bssid_cam_entry *bssid_cam,
668 			     const u8 *bssid)
669 {
670 	u8 bssid_cam_idx;
671 	int ret;
672 
673 	if (unlikely(bssid_cam->valid)) {
674 		rtw89_debug(rtwdev, RTW89_DBG_FW,
675 			    "bssid cam is already valid; skip init\n");
676 		return 0;
677 	}
678 
679 	ret = rtw89_cam_get_avail_bssid_cam(rtwdev, &bssid_cam_idx);
680 	if (ret) {
681 		rtw89_err(rtwdev, "failed to get available bssid cam\n");
682 		return ret;
683 	}
684 
685 	bssid_cam->bssid_cam_idx = bssid_cam_idx;
686 	bssid_cam->phy_idx = rtwvif_link->phy_idx;
687 	bssid_cam->len = BSSID_CAM_ENT_SIZE;
688 	bssid_cam->offset = 0;
689 	bssid_cam->valid = true;
690 	ether_addr_copy(bssid_cam->bssid, bssid);
691 
692 	return 0;
693 }
694 
695 void rtw89_cam_bssid_changed(struct rtw89_dev *rtwdev, struct rtw89_vif_link *rtwvif_link)
696 {
697 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif_link->bssid_cam;
698 
699 	ether_addr_copy(bssid_cam->bssid, rtwvif_link->bssid);
700 }
701 
702 int rtw89_cam_init(struct rtw89_dev *rtwdev, struct rtw89_vif_link *rtwvif_link)
703 {
704 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif_link->addr_cam;
705 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif_link->bssid_cam;
706 	int ret;
707 
708 	ret = rtw89_cam_init_bssid_cam(rtwdev, rtwvif_link, bssid_cam,
709 				       rtwvif_link->bssid);
710 	if (ret) {
711 		rtw89_err(rtwdev, "failed to init bssid cam\n");
712 		return ret;
713 	}
714 
715 	ret = rtw89_cam_init_addr_cam(rtwdev, addr_cam, bssid_cam);
716 	if (ret) {
717 		rtw89_err(rtwdev, "failed to init addr cam\n");
718 		return ret;
719 	}
720 
721 	return 0;
722 }
723 
724 int rtw89_cam_fill_bssid_cam_info(struct rtw89_dev *rtwdev,
725 				  struct rtw89_vif_link *rtwvif_link,
726 				  struct rtw89_sta_link *rtwsta_link, u8 *cmd)
727 {
728 	struct rtw89_bssid_cam_entry *bssid_cam = rtw89_get_bssid_cam_of(rtwvif_link,
729 									 rtwsta_link);
730 	struct ieee80211_bss_conf *bss_conf;
731 	u8 bss_color;
732 	u8 bss_mask;
733 
734 	rcu_read_lock();
735 
736 	bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, false);
737 	bss_color = bss_conf->he_bss_color.color;
738 
739 	if (bss_conf->nontransmitted)
740 		bss_mask = RTW89_BSSID_MATCH_5_BYTES;
741 	else
742 		bss_mask = RTW89_BSSID_MATCH_ALL;
743 
744 	rcu_read_unlock();
745 
746 	FWCMD_SET_ADDR_BSSID_IDX(cmd, bssid_cam->bssid_cam_idx);
747 	FWCMD_SET_ADDR_BSSID_OFFSET(cmd, bssid_cam->offset);
748 	FWCMD_SET_ADDR_BSSID_LEN(cmd, bssid_cam->len);
749 	FWCMD_SET_ADDR_BSSID_VALID(cmd, bssid_cam->valid);
750 	FWCMD_SET_ADDR_BSSID_MASK(cmd, bss_mask);
751 	FWCMD_SET_ADDR_BSSID_BB_SEL(cmd, bssid_cam->phy_idx);
752 	FWCMD_SET_ADDR_BSSID_BSS_COLOR(cmd, bss_color);
753 
754 	FWCMD_SET_ADDR_BSSID_BSSID0(cmd, bssid_cam->bssid[0]);
755 	FWCMD_SET_ADDR_BSSID_BSSID1(cmd, bssid_cam->bssid[1]);
756 	FWCMD_SET_ADDR_BSSID_BSSID2(cmd, bssid_cam->bssid[2]);
757 	FWCMD_SET_ADDR_BSSID_BSSID3(cmd, bssid_cam->bssid[3]);
758 	FWCMD_SET_ADDR_BSSID_BSSID4(cmd, bssid_cam->bssid[4]);
759 	FWCMD_SET_ADDR_BSSID_BSSID5(cmd, bssid_cam->bssid[5]);
760 
761 	return 0;
762 }
763 
764 static u8 rtw89_cam_addr_hash(u8 start, const u8 *addr)
765 {
766 	u8 hash = 0;
767 	u8 i;
768 
769 	for (i = start; i < ETH_ALEN; i++)
770 		hash ^= addr[i];
771 
772 	return hash;
773 }
774 
775 void rtw89_cam_fill_addr_cam_info(struct rtw89_dev *rtwdev,
776 				  struct rtw89_vif_link *rtwvif_link,
777 				  struct rtw89_sta_link *rtwsta_link,
778 				  const u8 *scan_mac_addr,
779 				  u8 *cmd)
780 {
781 	struct ieee80211_vif *vif = rtwvif_link_to_vif(rtwvif_link);
782 	struct rtw89_addr_cam_entry *addr_cam =
783 		rtw89_get_addr_cam_of(rtwvif_link, rtwsta_link);
784 	struct ieee80211_sta *sta = rtwsta_link_to_sta_safe(rtwsta_link);
785 	struct ieee80211_link_sta *link_sta;
786 	const u8 *sma = scan_mac_addr ? scan_mac_addr : rtwvif_link->mac_addr;
787 	u8 sma_hash, tma_hash, addr_msk_start;
788 	u8 sma_start = 0;
789 	u8 tma_start = 0;
790 	const u8 *tma;
791 
792 	rcu_read_lock();
793 
794 	if (sta) {
795 		link_sta = rtw89_sta_rcu_dereference_link(rtwsta_link, true);
796 		tma = link_sta->addr;
797 	} else {
798 		tma = rtwvif_link->bssid;
799 	}
800 
801 	if (addr_cam->addr_mask != 0) {
802 		addr_msk_start = __ffs(addr_cam->addr_mask);
803 		if (addr_cam->mask_sel == RTW89_SMA)
804 			sma_start = addr_msk_start;
805 		else if (addr_cam->mask_sel == RTW89_TMA)
806 			tma_start = addr_msk_start;
807 	}
808 	sma_hash = rtw89_cam_addr_hash(sma_start, sma);
809 	tma_hash = rtw89_cam_addr_hash(tma_start, tma);
810 
811 	FWCMD_SET_ADDR_IDX(cmd, addr_cam->addr_cam_idx);
812 	FWCMD_SET_ADDR_OFFSET(cmd, addr_cam->offset);
813 	FWCMD_SET_ADDR_LEN(cmd, addr_cam->len);
814 
815 	FWCMD_SET_ADDR_VALID(cmd, addr_cam->valid);
816 	FWCMD_SET_ADDR_NET_TYPE(cmd, rtwvif_link->net_type);
817 	FWCMD_SET_ADDR_BCN_HIT_COND(cmd, rtwvif_link->bcn_hit_cond);
818 	FWCMD_SET_ADDR_HIT_RULE(cmd, rtwvif_link->hit_rule);
819 	FWCMD_SET_ADDR_BB_SEL(cmd, rtwvif_link->phy_idx);
820 	FWCMD_SET_ADDR_ADDR_MASK(cmd, addr_cam->addr_mask);
821 	FWCMD_SET_ADDR_MASK_SEL(cmd, addr_cam->mask_sel);
822 	FWCMD_SET_ADDR_SMA_HASH(cmd, sma_hash);
823 	FWCMD_SET_ADDR_TMA_HASH(cmd, tma_hash);
824 
825 	FWCMD_SET_ADDR_BSSID_CAM_IDX(cmd, addr_cam->bssid_cam_idx);
826 
827 	FWCMD_SET_ADDR_SMA0(cmd, sma[0]);
828 	FWCMD_SET_ADDR_SMA1(cmd, sma[1]);
829 	FWCMD_SET_ADDR_SMA2(cmd, sma[2]);
830 	FWCMD_SET_ADDR_SMA3(cmd, sma[3]);
831 	FWCMD_SET_ADDR_SMA4(cmd, sma[4]);
832 	FWCMD_SET_ADDR_SMA5(cmd, sma[5]);
833 
834 	FWCMD_SET_ADDR_TMA0(cmd, tma[0]);
835 	FWCMD_SET_ADDR_TMA1(cmd, tma[1]);
836 	FWCMD_SET_ADDR_TMA2(cmd, tma[2]);
837 	FWCMD_SET_ADDR_TMA3(cmd, tma[3]);
838 	FWCMD_SET_ADDR_TMA4(cmd, tma[4]);
839 	FWCMD_SET_ADDR_TMA5(cmd, tma[5]);
840 
841 	FWCMD_SET_ADDR_PORT_INT(cmd, rtwvif_link->port);
842 	FWCMD_SET_ADDR_TSF_SYNC(cmd, rtwvif_link->port);
843 	FWCMD_SET_ADDR_TF_TRS(cmd, rtwvif_link->trigger);
844 	FWCMD_SET_ADDR_LSIG_TXOP(cmd, rtwvif_link->lsig_txop);
845 	FWCMD_SET_ADDR_TGT_IND(cmd, rtwvif_link->tgt_ind);
846 	FWCMD_SET_ADDR_FRM_TGT_IND(cmd, rtwvif_link->frm_tgt_ind);
847 	FWCMD_SET_ADDR_MACID(cmd, rtwsta_link ? rtwsta_link->mac_id :
848 						rtwvif_link->mac_id);
849 	if (rtwvif_link->net_type == RTW89_NET_TYPE_INFRA)
850 		FWCMD_SET_ADDR_AID12(cmd, vif->cfg.aid & 0xfff);
851 	else if (rtwvif_link->net_type == RTW89_NET_TYPE_AP_MODE)
852 		FWCMD_SET_ADDR_AID12(cmd, sta ? sta->aid & 0xfff : 0);
853 	FWCMD_SET_ADDR_WOL_PATTERN(cmd, rtwvif_link->wowlan_pattern);
854 	FWCMD_SET_ADDR_WOL_UC(cmd, rtwvif_link->wowlan_uc);
855 	FWCMD_SET_ADDR_WOL_MAGIC(cmd, rtwvif_link->wowlan_magic);
856 	FWCMD_SET_ADDR_WAPI(cmd, addr_cam->wapi);
857 	FWCMD_SET_ADDR_SEC_ENT_MODE(cmd, addr_cam->sec_ent_mode);
858 	FWCMD_SET_ADDR_SEC_ENT0_KEYID(cmd, addr_cam->sec_ent_keyid[0]);
859 	FWCMD_SET_ADDR_SEC_ENT1_KEYID(cmd, addr_cam->sec_ent_keyid[1]);
860 	FWCMD_SET_ADDR_SEC_ENT2_KEYID(cmd, addr_cam->sec_ent_keyid[2]);
861 	FWCMD_SET_ADDR_SEC_ENT3_KEYID(cmd, addr_cam->sec_ent_keyid[3]);
862 	FWCMD_SET_ADDR_SEC_ENT4_KEYID(cmd, addr_cam->sec_ent_keyid[4]);
863 	FWCMD_SET_ADDR_SEC_ENT5_KEYID(cmd, addr_cam->sec_ent_keyid[5]);
864 	FWCMD_SET_ADDR_SEC_ENT6_KEYID(cmd, addr_cam->sec_ent_keyid[6]);
865 
866 	FWCMD_SET_ADDR_SEC_ENT_VALID(cmd, addr_cam->sec_cam_map[0] & 0xff);
867 	FWCMD_SET_ADDR_SEC_ENT0(cmd, addr_cam->sec_ent[0]);
868 	FWCMD_SET_ADDR_SEC_ENT1(cmd, addr_cam->sec_ent[1]);
869 	FWCMD_SET_ADDR_SEC_ENT2(cmd, addr_cam->sec_ent[2]);
870 	FWCMD_SET_ADDR_SEC_ENT3(cmd, addr_cam->sec_ent[3]);
871 	FWCMD_SET_ADDR_SEC_ENT4(cmd, addr_cam->sec_ent[4]);
872 	FWCMD_SET_ADDR_SEC_ENT5(cmd, addr_cam->sec_ent[5]);
873 	FWCMD_SET_ADDR_SEC_ENT6(cmd, addr_cam->sec_ent[6]);
874 
875 	rcu_read_unlock();
876 }
877 
878 void rtw89_cam_fill_dctl_sec_cam_info_v1(struct rtw89_dev *rtwdev,
879 					 struct rtw89_vif_link *rtwvif_link,
880 					 struct rtw89_sta_link *rtwsta_link,
881 					 struct rtw89_h2c_dctlinfo_ud_v1 *h2c)
882 {
883 	struct rtw89_addr_cam_entry *addr_cam =
884 		rtw89_get_addr_cam_of(rtwvif_link, rtwsta_link);
885 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
886 	u8 *ptk_tx_iv = rtw_wow->key_info.ptk_tx_iv;
887 
888 	h2c->c0 = le32_encode_bits(rtwsta_link ? rtwsta_link->mac_id :
889 						 rtwvif_link->mac_id,
890 				   DCTLINFO_V1_C0_MACID) |
891 		  le32_encode_bits(1, DCTLINFO_V1_C0_OP);
892 
893 	h2c->w4 = le32_encode_bits(addr_cam->sec_ent_keyid[0],
894 				   DCTLINFO_V1_W4_SEC_ENT0_KEYID) |
895 		  le32_encode_bits(addr_cam->sec_ent_keyid[1],
896 				   DCTLINFO_V1_W4_SEC_ENT1_KEYID) |
897 		  le32_encode_bits(addr_cam->sec_ent_keyid[2],
898 				   DCTLINFO_V1_W4_SEC_ENT2_KEYID) |
899 		  le32_encode_bits(addr_cam->sec_ent_keyid[3],
900 				   DCTLINFO_V1_W4_SEC_ENT3_KEYID) |
901 		  le32_encode_bits(addr_cam->sec_ent_keyid[4],
902 				   DCTLINFO_V1_W4_SEC_ENT4_KEYID) |
903 		  le32_encode_bits(addr_cam->sec_ent_keyid[5],
904 				   DCTLINFO_V1_W4_SEC_ENT5_KEYID) |
905 		  le32_encode_bits(addr_cam->sec_ent_keyid[6],
906 				   DCTLINFO_V1_W4_SEC_ENT6_KEYID);
907 	h2c->m4 = cpu_to_le32(DCTLINFO_V1_W4_SEC_ENT0_KEYID |
908 			      DCTLINFO_V1_W4_SEC_ENT1_KEYID |
909 			      DCTLINFO_V1_W4_SEC_ENT2_KEYID |
910 			      DCTLINFO_V1_W4_SEC_ENT3_KEYID |
911 			      DCTLINFO_V1_W4_SEC_ENT4_KEYID |
912 			      DCTLINFO_V1_W4_SEC_ENT5_KEYID |
913 			      DCTLINFO_V1_W4_SEC_ENT6_KEYID);
914 
915 	h2c->w5 = le32_encode_bits(addr_cam->sec_cam_map[0] & 0xff,
916 				   DCTLINFO_V1_W5_SEC_ENT_VALID) |
917 		  le32_encode_bits(addr_cam->sec_ent[0],
918 				   DCTLINFO_V1_W5_SEC_ENT0) |
919 		  le32_encode_bits(addr_cam->sec_ent[1],
920 				   DCTLINFO_V1_W5_SEC_ENT1) |
921 		  le32_encode_bits(addr_cam->sec_ent[2],
922 				   DCTLINFO_V1_W5_SEC_ENT2);
923 	h2c->m5 = cpu_to_le32(DCTLINFO_V1_W5_SEC_ENT_VALID |
924 			      DCTLINFO_V1_W5_SEC_ENT0      |
925 			      DCTLINFO_V1_W5_SEC_ENT1      |
926 			      DCTLINFO_V1_W5_SEC_ENT2);
927 
928 	h2c->w6 = le32_encode_bits(addr_cam->sec_ent[3],
929 				   DCTLINFO_V1_W6_SEC_ENT3) |
930 		  le32_encode_bits(addr_cam->sec_ent[4],
931 				   DCTLINFO_V1_W6_SEC_ENT4) |
932 		  le32_encode_bits(addr_cam->sec_ent[5],
933 				   DCTLINFO_V1_W6_SEC_ENT5) |
934 		  le32_encode_bits(addr_cam->sec_ent[6],
935 				   DCTLINFO_V1_W6_SEC_ENT6);
936 	h2c->m6 = cpu_to_le32(DCTLINFO_V1_W6_SEC_ENT3 |
937 			      DCTLINFO_V1_W6_SEC_ENT4 |
938 			      DCTLINFO_V1_W6_SEC_ENT5 |
939 			      DCTLINFO_V1_W6_SEC_ENT6);
940 
941 	if (rtw_wow->ptk_alg) {
942 		h2c->w0 = le32_encode_bits(ptk_tx_iv[0] | ptk_tx_iv[1] << 8,
943 					   DCTLINFO_V1_W0_AES_IV_L);
944 		h2c->m0 = cpu_to_le32(DCTLINFO_V1_W0_AES_IV_L);
945 
946 		h2c->w1 = le32_encode_bits(ptk_tx_iv[4]       |
947 					   ptk_tx_iv[5] << 8  |
948 					   ptk_tx_iv[6] << 16 |
949 					   ptk_tx_iv[7] << 24,
950 					   DCTLINFO_V1_W1_AES_IV_H);
951 		h2c->m1 = cpu_to_le32(DCTLINFO_V1_W1_AES_IV_H);
952 
953 		h2c->w4 |= le32_encode_bits(rtw_wow->ptk_keyidx,
954 					    DCTLINFO_V1_W4_SEC_KEY_ID);
955 		h2c->m4 |= cpu_to_le32(DCTLINFO_V1_W4_SEC_KEY_ID);
956 	}
957 }
958 
959 void rtw89_cam_fill_dctl_sec_cam_info_v2(struct rtw89_dev *rtwdev,
960 					 struct rtw89_vif_link *rtwvif_link,
961 					 struct rtw89_sta_link *rtwsta_link,
962 					 struct rtw89_h2c_dctlinfo_ud_v2 *h2c)
963 {
964 	struct ieee80211_sta *sta = rtwsta_link_to_sta_safe(rtwsta_link);
965 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif_link->rtwvif);
966 	struct rtw89_vif *rtwvif = rtwvif_link->rtwvif;
967 	struct rtw89_addr_cam_entry *addr_cam =
968 		rtw89_get_addr_cam_of(rtwvif_link, rtwsta_link);
969 	bool is_mld = sta ? sta->mlo : ieee80211_vif_is_mld(vif);
970 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
971 	u8 *ptk_tx_iv = rtw_wow->key_info.ptk_tx_iv;
972 	u8 *mld_sma, *mld_tma, *mld_bssid;
973 
974 	h2c->c0 = le32_encode_bits(rtwsta_link ? rtwsta_link->mac_id :
975 						 rtwvif_link->mac_id,
976 				   DCTLINFO_V2_C0_MACID) |
977 		  le32_encode_bits(1, DCTLINFO_V2_C0_OP);
978 
979 	h2c->w2 = le32_encode_bits(is_mld, DCTLINFO_V2_W2_IS_MLD);
980 	h2c->m2 = cpu_to_le32(DCTLINFO_V2_W2_IS_MLD);
981 
982 	h2c->w4 = le32_encode_bits(addr_cam->sec_ent_keyid[0],
983 				   DCTLINFO_V2_W4_SEC_ENT0_KEYID) |
984 		  le32_encode_bits(addr_cam->sec_ent_keyid[1],
985 				   DCTLINFO_V2_W4_SEC_ENT1_KEYID) |
986 		  le32_encode_bits(addr_cam->sec_ent_keyid[2],
987 				   DCTLINFO_V2_W4_SEC_ENT2_KEYID) |
988 		  le32_encode_bits(addr_cam->sec_ent_keyid[3],
989 				   DCTLINFO_V2_W4_SEC_ENT3_KEYID) |
990 		  le32_encode_bits(addr_cam->sec_ent_keyid[4],
991 				   DCTLINFO_V2_W4_SEC_ENT4_KEYID) |
992 		  le32_encode_bits(addr_cam->sec_ent_keyid[5],
993 				   DCTLINFO_V2_W4_SEC_ENT5_KEYID) |
994 		  le32_encode_bits(addr_cam->sec_ent_keyid[6],
995 				   DCTLINFO_V2_W4_SEC_ENT6_KEYID);
996 	h2c->m4 = cpu_to_le32(DCTLINFO_V2_W4_SEC_ENT0_KEYID |
997 			      DCTLINFO_V2_W4_SEC_ENT1_KEYID |
998 			      DCTLINFO_V2_W4_SEC_ENT2_KEYID |
999 			      DCTLINFO_V2_W4_SEC_ENT3_KEYID |
1000 			      DCTLINFO_V2_W4_SEC_ENT4_KEYID |
1001 			      DCTLINFO_V2_W4_SEC_ENT5_KEYID |
1002 			      DCTLINFO_V2_W4_SEC_ENT6_KEYID);
1003 
1004 	h2c->w5 = le32_encode_bits(addr_cam->sec_cam_map[0],
1005 				   DCTLINFO_V2_W5_SEC_ENT_VALID_V1) |
1006 		  le32_encode_bits(addr_cam->sec_ent[0],
1007 				   DCTLINFO_V2_W5_SEC_ENT0_V1);
1008 	h2c->m5 = cpu_to_le32(DCTLINFO_V2_W5_SEC_ENT_VALID_V1 |
1009 			      DCTLINFO_V2_W5_SEC_ENT0_V1);
1010 
1011 	h2c->w6 = le32_encode_bits(addr_cam->sec_ent[1],
1012 				   DCTLINFO_V2_W6_SEC_ENT1_V1) |
1013 		  le32_encode_bits(addr_cam->sec_ent[2],
1014 				   DCTLINFO_V2_W6_SEC_ENT2_V1) |
1015 		  le32_encode_bits(addr_cam->sec_ent[3],
1016 				   DCTLINFO_V2_W6_SEC_ENT3_V1) |
1017 		  le32_encode_bits(addr_cam->sec_ent[4],
1018 				   DCTLINFO_V2_W6_SEC_ENT4_V1);
1019 	h2c->m6 = cpu_to_le32(DCTLINFO_V2_W6_SEC_ENT1_V1 |
1020 			      DCTLINFO_V2_W6_SEC_ENT2_V1 |
1021 			      DCTLINFO_V2_W6_SEC_ENT3_V1 |
1022 			      DCTLINFO_V2_W6_SEC_ENT4_V1);
1023 
1024 	h2c->w7 = le32_encode_bits(addr_cam->sec_ent[5],
1025 				   DCTLINFO_V2_W7_SEC_ENT5_V1) |
1026 		  le32_encode_bits(addr_cam->sec_ent[6],
1027 				   DCTLINFO_V2_W7_SEC_ENT6_V1);
1028 	h2c->m7 = cpu_to_le32(DCTLINFO_V2_W7_SEC_ENT5_V1 |
1029 			      DCTLINFO_V2_W7_SEC_ENT6_V1);
1030 
1031 	if (rtw_wow->ptk_alg) {
1032 		h2c->w0 = le32_encode_bits(ptk_tx_iv[0] | ptk_tx_iv[1] << 8,
1033 					   DCTLINFO_V2_W0_AES_IV_L);
1034 		h2c->m0 = cpu_to_le32(DCTLINFO_V2_W0_AES_IV_L);
1035 
1036 		h2c->w1 = le32_encode_bits(ptk_tx_iv[4] |
1037 					   ptk_tx_iv[5] << 8 |
1038 					   ptk_tx_iv[6] << 16 |
1039 					   ptk_tx_iv[7] << 24,
1040 					   DCTLINFO_V2_W1_AES_IV_H);
1041 		h2c->m1 = cpu_to_le32(DCTLINFO_V2_W1_AES_IV_H);
1042 
1043 		h2c->w4 |= le32_encode_bits(rtw_wow->ptk_keyidx,
1044 					    DCTLINFO_V2_W4_SEC_KEY_ID);
1045 		h2c->m4 |= cpu_to_le32(DCTLINFO_V2_W4_SEC_KEY_ID);
1046 	}
1047 
1048 	if (!is_mld)
1049 		return;
1050 
1051 	if (rtwvif_link->net_type == RTW89_NET_TYPE_INFRA) {
1052 		mld_sma = rtwvif->mac_addr;
1053 		mld_tma = vif->cfg.ap_addr;
1054 		mld_bssid = vif->cfg.ap_addr;
1055 	} else if (rtwvif_link->net_type == RTW89_NET_TYPE_AP_MODE && sta) {
1056 		mld_sma = rtwvif->mac_addr;
1057 		mld_tma = sta->addr;
1058 		mld_bssid = rtwvif->mac_addr;
1059 	} else {
1060 		return;
1061 	}
1062 
1063 	h2c->w8 = le32_encode_bits(mld_sma[0], DCTLINFO_V2_W8_MLD_SMA_0) |
1064 		  le32_encode_bits(mld_sma[1], DCTLINFO_V2_W8_MLD_SMA_1) |
1065 		  le32_encode_bits(mld_sma[2], DCTLINFO_V2_W8_MLD_SMA_2) |
1066 		  le32_encode_bits(mld_sma[3], DCTLINFO_V2_W8_MLD_SMA_3);
1067 	h2c->m8 = cpu_to_le32(DCTLINFO_V2_W8_ALL);
1068 
1069 	h2c->w9 = le32_encode_bits(mld_sma[4], DCTLINFO_V2_W9_MLD_SMA_4) |
1070 		  le32_encode_bits(mld_sma[5], DCTLINFO_V2_W9_MLD_SMA_5) |
1071 		  le32_encode_bits(mld_tma[0], DCTLINFO_V2_W9_MLD_TMA_0) |
1072 		  le32_encode_bits(mld_tma[1], DCTLINFO_V2_W9_MLD_TMA_1);
1073 	h2c->m9 = cpu_to_le32(DCTLINFO_V2_W9_ALL);
1074 
1075 	h2c->w10 = le32_encode_bits(mld_tma[2], DCTLINFO_V2_W10_MLD_TMA_2) |
1076 		   le32_encode_bits(mld_tma[3], DCTLINFO_V2_W10_MLD_TMA_3) |
1077 		   le32_encode_bits(mld_tma[4], DCTLINFO_V2_W10_MLD_TMA_4) |
1078 		   le32_encode_bits(mld_tma[5], DCTLINFO_V2_W10_MLD_TMA_5);
1079 	h2c->m10 = cpu_to_le32(DCTLINFO_V2_W10_ALL);
1080 
1081 	h2c->w11 = le32_encode_bits(mld_bssid[0], DCTLINFO_V2_W11_MLD_BSSID_0) |
1082 		   le32_encode_bits(mld_bssid[1], DCTLINFO_V2_W11_MLD_BSSID_1) |
1083 		   le32_encode_bits(mld_bssid[2], DCTLINFO_V2_W11_MLD_BSSID_2) |
1084 		   le32_encode_bits(mld_bssid[3], DCTLINFO_V2_W11_MLD_BSSID_3);
1085 	h2c->m11 = cpu_to_le32(DCTLINFO_V2_W11_ALL);
1086 
1087 	h2c->w12 = le32_encode_bits(mld_bssid[4], DCTLINFO_V2_W12_MLD_BSSID_4) |
1088 		   le32_encode_bits(mld_bssid[5], DCTLINFO_V2_W12_MLD_BSSID_5);
1089 	h2c->m12 = cpu_to_le32(DCTLINFO_V2_W12_ALL);
1090 }
1091