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(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 		if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
154 			return -EINVAL;
155 		idx = find_first_zero_bit(addr_cam->sec_cam_map,
156 					  RTW89_SEC_CAM_IN_ADDR_CAM);
157 		if (idx >= RTW89_SEC_CAM_IN_ADDR_CAM)
158 			return -EBUSY;
159 		*key_idx = idx;
160 		break;
161 	case RTW89_ADDR_CAM_SEC_NORMAL:
162 		if (sec_cam->type == RTW89_SEC_KEY_TYPE_BIP_CCMP128) {
163 			idx = find_next_zero_bit(addr_cam->sec_cam_map,
164 						 RTW89_SEC_CAM_IN_ADDR_CAM, 5);
165 			if (idx > 6)
166 				return -EBUSY;
167 			*key_idx = idx;
168 			break;
169 		}
170 
171 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
172 			idx = find_next_zero_bit(addr_cam->sec_cam_map,
173 						 RTW89_SEC_CAM_IN_ADDR_CAM, 0);
174 			if (idx > 1)
175 				return -EBUSY;
176 			*key_idx = idx;
177 			break;
178 		}
179 
180 		/* Group keys */
181 		idx = find_next_zero_bit(addr_cam->sec_cam_map,
182 					 RTW89_SEC_CAM_IN_ADDR_CAM, 2);
183 		if (idx > 4)
184 			return -EBUSY;
185 		*key_idx = idx;
186 		break;
187 	case RTW89_ADDR_CAM_SEC_4GROUP:
188 		if (sec_cam->type == RTW89_SEC_KEY_TYPE_BIP_CCMP128) {
189 			if (test_bit(6, addr_cam->sec_cam_map))
190 				return -EINVAL;
191 			*key_idx = 6;
192 			break;
193 		}
194 
195 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
196 			idx = find_next_zero_bit(addr_cam->sec_cam_map,
197 						 RTW89_SEC_CAM_IN_ADDR_CAM, 0);
198 			if (idx > 1)
199 				return -EBUSY;
200 			*key_idx = idx;
201 			break;
202 		}
203 
204 		/* Group keys */
205 		idx = find_next_zero_bit(addr_cam->sec_cam_map,
206 					 RTW89_SEC_CAM_IN_ADDR_CAM, 2);
207 		if (idx > 5)
208 			return -EBUSY;
209 		*key_idx = idx;
210 		break;
211 	}
212 
213 	return 0;
214 }
215 
216 static int rtw89_cam_attach_sec_cam(struct rtw89_dev *rtwdev,
217 				    struct ieee80211_vif *vif,
218 				    struct ieee80211_sta *sta,
219 				    struct ieee80211_key_conf *key,
220 				    struct rtw89_sec_cam_entry *sec_cam)
221 {
222 	struct rtw89_vif *rtwvif;
223 	struct rtw89_addr_cam_entry *addr_cam;
224 	u8 key_idx = 0;
225 	int ret;
226 
227 	if (!vif) {
228 		rtw89_err(rtwdev, "No iface for adding sec cam\n");
229 		return -EINVAL;
230 	}
231 
232 	rtwvif = (struct rtw89_vif *)vif->drv_priv;
233 	addr_cam = &rtwvif->addr_cam;
234 	ret = rtw89_cam_get_addr_cam_key_idx(addr_cam, sec_cam, key, &key_idx);
235 	if (ret) {
236 		rtw89_err(rtwdev, "failed to get addr cam key idx %d, %d\n",
237 			  addr_cam->sec_ent_mode, sec_cam->type);
238 		return ret;
239 	}
240 
241 	key->hw_key_idx = key_idx;
242 	addr_cam->sec_ent_keyid[key_idx] = key->keyidx;
243 	addr_cam->sec_ent[key_idx] = sec_cam->sec_cam_idx;
244 	addr_cam->sec_entries[key_idx] = sec_cam;
245 	set_bit(key_idx, addr_cam->sec_cam_map);
246 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif);
247 	if (ret) {
248 		rtw89_err(rtwdev, "failed to update addr cam sec entry: %d\n",
249 			  ret);
250 		clear_bit(key_idx, addr_cam->sec_cam_map);
251 		addr_cam->sec_entries[key_idx] = NULL;
252 		return ret;
253 	}
254 
255 	return 0;
256 }
257 
258 static int rtw89_cam_sec_key_install(struct rtw89_dev *rtwdev,
259 				     struct ieee80211_vif *vif,
260 				     struct ieee80211_sta *sta,
261 				     struct ieee80211_key_conf *key,
262 				     u8 hw_key_type, bool ext_key)
263 {
264 	struct rtw89_sec_cam_entry *sec_cam = NULL;
265 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
266 	u8 sec_cam_idx;
267 	int ret;
268 
269 	/* maximum key length 256-bit */
270 	if (key->keylen > 32) {
271 		rtw89_err(rtwdev, "invalid sec key length %d\n", key->keylen);
272 		return -EINVAL;
273 	}
274 
275 	ret = rtw89_cam_get_avail_sec_cam(rtwdev, &sec_cam_idx, ext_key);
276 	if (ret) {
277 		rtw89_warn(rtwdev, "no available sec cam: %d ext: %d\n",
278 			   ret, ext_key);
279 		return ret;
280 	}
281 
282 	sec_cam = kzalloc(sizeof(*sec_cam), GFP_KERNEL);
283 	if (!sec_cam) {
284 		ret = -ENOMEM;
285 		goto err_release_cam;
286 	}
287 
288 	sec_cam->sec_cam_idx = sec_cam_idx;
289 	sec_cam->type = hw_key_type;
290 	sec_cam->len = RTW89_SEC_CAM_LEN;
291 	sec_cam->ext_key = ext_key;
292 	memcpy(sec_cam->key, key->key, key->keylen);
293 	ret = rtw89_cam_send_sec_key_cmd(rtwdev, sec_cam);
294 	if (ret) {
295 		rtw89_err(rtwdev, "failed to send sec key cmd: %d\n", ret);
296 		goto err_release_cam;
297 	}
298 
299 	/* associate with addr cam */
300 	ret = rtw89_cam_attach_sec_cam(rtwdev, vif, sta, key, sec_cam);
301 	if (ret) {
302 		rtw89_err(rtwdev, "failed to attach sec cam: %d\n", ret);
303 		goto err_release_cam;
304 	}
305 
306 	return 0;
307 
308 err_release_cam:
309 	kfree(sec_cam);
310 	clear_bit(sec_cam_idx, cam_info->sec_cam_map);
311 	if (ext_key)
312 		clear_bit(sec_cam_idx + 1, cam_info->sec_cam_map);
313 
314 	return ret;
315 }
316 
317 int rtw89_cam_sec_key_add(struct rtw89_dev *rtwdev,
318 			  struct ieee80211_vif *vif,
319 			  struct ieee80211_sta *sta,
320 			  struct ieee80211_key_conf *key)
321 {
322 	u8 hw_key_type;
323 	bool ext_key = false;
324 	int ret;
325 
326 	switch (key->cipher) {
327 	case WLAN_CIPHER_SUITE_WEP40:
328 		hw_key_type = RTW89_SEC_KEY_TYPE_WEP40;
329 		break;
330 	case WLAN_CIPHER_SUITE_WEP104:
331 		hw_key_type = RTW89_SEC_KEY_TYPE_WEP104;
332 		break;
333 	case WLAN_CIPHER_SUITE_CCMP:
334 		hw_key_type = RTW89_SEC_KEY_TYPE_CCMP128;
335 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
336 		break;
337 	case WLAN_CIPHER_SUITE_CCMP_256:
338 		hw_key_type = RTW89_SEC_KEY_TYPE_CCMP256;
339 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
340 		ext_key = true;
341 		break;
342 	case WLAN_CIPHER_SUITE_GCMP:
343 		hw_key_type = RTW89_SEC_KEY_TYPE_GCMP128;
344 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
345 		break;
346 	case WLAN_CIPHER_SUITE_GCMP_256:
347 		hw_key_type = RTW89_SEC_KEY_TYPE_GCMP256;
348 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
349 		ext_key = true;
350 		break;
351 	default:
352 		return -EOPNOTSUPP;
353 	}
354 
355 	key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
356 
357 	ret = rtw89_cam_sec_key_install(rtwdev, vif, sta, key, hw_key_type,
358 					ext_key);
359 	if (ret) {
360 		rtw89_err(rtwdev, "failed to install key type %d ext %d: %d\n",
361 			  hw_key_type, ext_key, ret);
362 		return ret;
363 	}
364 
365 	return 0;
366 }
367 
368 int rtw89_cam_sec_key_del(struct rtw89_dev *rtwdev,
369 			  struct ieee80211_vif *vif,
370 			  struct ieee80211_sta *sta,
371 			  struct ieee80211_key_conf *key,
372 			  bool inform_fw)
373 {
374 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
375 	struct rtw89_vif *rtwvif;
376 	struct rtw89_addr_cam_entry *addr_cam;
377 	struct rtw89_sec_cam_entry *sec_cam;
378 	u8 key_idx = key->hw_key_idx;
379 	u8 sec_cam_idx;
380 	int ret = 0;
381 
382 	if (!vif) {
383 		rtw89_err(rtwdev, "No iface for deleting sec cam\n");
384 		return -EINVAL;
385 	}
386 
387 	rtwvif = (struct rtw89_vif *)vif->drv_priv;
388 	addr_cam = &rtwvif->addr_cam;
389 	sec_cam = addr_cam->sec_entries[key_idx];
390 	if (!sec_cam)
391 		return -EINVAL;
392 
393 	/* detach sec cam from addr cam */
394 	clear_bit(key_idx, addr_cam->sec_cam_map);
395 	addr_cam->sec_entries[key_idx] = NULL;
396 	if (inform_fw) {
397 		ret = rtw89_fw_h2c_cam(rtwdev, rtwvif);
398 		if (ret)
399 			rtw89_err(rtwdev, "failed to update cam del key: %d\n", ret);
400 	}
401 
402 	/* clear valid bit in addr cam will disable sec cam,
403 	 * so we don't need to send H2C command again
404 	 */
405 	sec_cam_idx = sec_cam->sec_cam_idx;
406 	clear_bit(sec_cam_idx, cam_info->sec_cam_map);
407 	if (sec_cam->ext_key)
408 		clear_bit(sec_cam_idx + 1, cam_info->sec_cam_map);
409 
410 	kfree(sec_cam);
411 
412 	return ret;
413 }
414 
415 static void rtw89_cam_reset_key_iter(struct ieee80211_hw *hw,
416 				     struct ieee80211_vif *vif,
417 				     struct ieee80211_sta *sta,
418 				     struct ieee80211_key_conf *key,
419 				     void *data)
420 {
421 	struct rtw89_dev *rtwdev = (struct rtw89_dev *)data;
422 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
423 
424 	rtw89_cam_sec_key_del(rtwdev, vif, sta, key, false);
425 	rtw89_cam_deinit(rtwdev, rtwvif);
426 }
427 
428 void rtw89_cam_deinit(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
429 {
430 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
431 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
432 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
433 
434 	addr_cam->valid = false;
435 	bssid_cam->valid = false;
436 	clear_bit(addr_cam->addr_cam_idx, cam_info->addr_cam_map);
437 	clear_bit(bssid_cam->bssid_cam_idx, cam_info->bssid_cam_map);
438 }
439 
440 void rtw89_cam_reset_keys(struct rtw89_dev *rtwdev)
441 {
442 	rcu_read_lock();
443 	ieee80211_iter_keys_rcu(rtwdev->hw, NULL, rtw89_cam_reset_key_iter, rtwdev);
444 	rcu_read_unlock();
445 }
446 
447 static int rtw89_cam_get_avail_addr_cam(struct rtw89_dev *rtwdev,
448 					u8 *addr_cam_idx)
449 {
450 	const struct rtw89_chip_info *chip = rtwdev->chip;
451 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
452 	u8 addr_cam_num = chip->acam_num;
453 	u8 idx;
454 
455 	idx = find_first_zero_bit(cam_info->addr_cam_map, addr_cam_num);
456 	if (idx >= addr_cam_num)
457 		return -EBUSY;
458 
459 	set_bit(idx, cam_info->addr_cam_map);
460 	*addr_cam_idx = idx;
461 
462 	return 0;
463 }
464 
465 static int rtw89_cam_init_addr_cam(struct rtw89_dev *rtwdev,
466 				   struct rtw89_vif *rtwvif)
467 {
468 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
469 	u8 addr_cam_idx;
470 	int i;
471 	int ret;
472 
473 	ret = rtw89_cam_get_avail_addr_cam(rtwdev, &addr_cam_idx);
474 	if (ret) {
475 		rtw89_err(rtwdev, "failed to get available addr cam\n");
476 		return ret;
477 	}
478 
479 	addr_cam->addr_cam_idx = addr_cam_idx;
480 	addr_cam->len = ADDR_CAM_ENT_SIZE;
481 	addr_cam->offset = 0;
482 	addr_cam->valid = true;
483 	addr_cam->addr_mask = 0;
484 	addr_cam->mask_sel = RTW89_NO_MSK;
485 	bitmap_zero(addr_cam->sec_cam_map, RTW89_SEC_CAM_IN_ADDR_CAM);
486 	ether_addr_copy(addr_cam->sma, rtwvif->mac_addr);
487 
488 	for (i = 0; i < RTW89_SEC_CAM_IN_ADDR_CAM; i++) {
489 		addr_cam->sec_ent_keyid[i] = 0;
490 		addr_cam->sec_ent[i] = 0;
491 	}
492 
493 	return 0;
494 }
495 
496 static int rtw89_cam_get_avail_bssid_cam(struct rtw89_dev *rtwdev,
497 					 u8 *bssid_cam_idx)
498 {
499 	const struct rtw89_chip_info *chip = rtwdev->chip;
500 	struct rtw89_cam_info *cam_info = &rtwdev->cam_info;
501 	u8 bssid_cam_num = chip->bcam_num;
502 	u8 idx;
503 
504 	idx = find_first_zero_bit(cam_info->bssid_cam_map, bssid_cam_num);
505 	if (idx >= bssid_cam_num)
506 		return -EBUSY;
507 
508 	set_bit(idx, cam_info->bssid_cam_map);
509 	*bssid_cam_idx = idx;
510 
511 	return 0;
512 }
513 
514 static int rtw89_cam_init_bssid_cam(struct rtw89_dev *rtwdev,
515 				    struct rtw89_vif *rtwvif)
516 {
517 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
518 	u8 bssid_cam_idx;
519 	int ret;
520 
521 	ret = rtw89_cam_get_avail_bssid_cam(rtwdev, &bssid_cam_idx);
522 	if (ret) {
523 		rtw89_err(rtwdev, "failed to get available bssid cam\n");
524 		return ret;
525 	}
526 
527 	bssid_cam->bssid_cam_idx = bssid_cam_idx;
528 	bssid_cam->phy_idx = rtwvif->phy_idx;
529 	bssid_cam->len = BSSID_CAM_ENT_SIZE;
530 	bssid_cam->offset = 0;
531 	bssid_cam->valid = true;
532 	ether_addr_copy(bssid_cam->bssid, rtwvif->bssid);
533 
534 	return 0;
535 }
536 
537 void rtw89_cam_bssid_changed(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
538 {
539 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
540 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
541 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
542 
543 	if (vif->type == NL80211_IFTYPE_STATION)
544 		ether_addr_copy(addr_cam->tma, rtwvif->bssid);
545 	ether_addr_copy(bssid_cam->bssid, rtwvif->bssid);
546 }
547 
548 int rtw89_cam_init(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
549 {
550 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
551 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
552 	int ret;
553 
554 	ret = rtw89_cam_init_addr_cam(rtwdev, rtwvif);
555 	if (ret) {
556 		rtw89_err(rtwdev, "failed to init addr cam\n");
557 		return ret;
558 	}
559 
560 	ret = rtw89_cam_init_bssid_cam(rtwdev, rtwvif);
561 	if (ret) {
562 		rtw89_err(rtwdev, "failed to init bssid cam\n");
563 		return ret;
564 	}
565 
566 	/* associate addr cam with bssid cam */
567 	addr_cam->bssid_cam_idx = bssid_cam->bssid_cam_idx;
568 
569 	return 0;
570 }
571 
572 int rtw89_cam_fill_bssid_cam_info(struct rtw89_dev *rtwdev,
573 				  struct rtw89_vif *rtwvif, u8 *cmd)
574 {
575 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
576 	struct rtw89_bssid_cam_entry *bssid_cam = &rtwvif->bssid_cam;
577 	u8 bss_color = vif->bss_conf.he_bss_color.color;
578 
579 	FWCMD_SET_ADDR_BSSID_IDX(cmd, bssid_cam->bssid_cam_idx);
580 	FWCMD_SET_ADDR_BSSID_OFFSET(cmd, bssid_cam->offset);
581 	FWCMD_SET_ADDR_BSSID_LEN(cmd, bssid_cam->len);
582 	FWCMD_SET_ADDR_BSSID_VALID(cmd, bssid_cam->valid);
583 	FWCMD_SET_ADDR_BSSID_BB_SEL(cmd, bssid_cam->phy_idx);
584 	FWCMD_SET_ADDR_BSSID_BSS_COLOR(cmd, bss_color);
585 
586 	FWCMD_SET_ADDR_BSSID_BSSID0(cmd, bssid_cam->bssid[0]);
587 	FWCMD_SET_ADDR_BSSID_BSSID1(cmd, bssid_cam->bssid[1]);
588 	FWCMD_SET_ADDR_BSSID_BSSID2(cmd, bssid_cam->bssid[2]);
589 	FWCMD_SET_ADDR_BSSID_BSSID3(cmd, bssid_cam->bssid[3]);
590 	FWCMD_SET_ADDR_BSSID_BSSID4(cmd, bssid_cam->bssid[4]);
591 	FWCMD_SET_ADDR_BSSID_BSSID5(cmd, bssid_cam->bssid[5]);
592 
593 	return 0;
594 }
595 
596 static u8 rtw89_cam_addr_hash(u8 start, u8 *addr)
597 {
598 	u8 hash = 0;
599 	u8 i;
600 
601 	for (i = start; i < ETH_ALEN; i++)
602 		hash ^= addr[i];
603 
604 	return hash;
605 }
606 
607 void rtw89_cam_fill_addr_cam_info(struct rtw89_dev *rtwdev,
608 				  struct rtw89_vif *rtwvif,
609 				  u8 *cmd)
610 {
611 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
612 	struct ieee80211_sta *sta;
613 	struct rtw89_sta *rtwsta;
614 	struct rtw89_addr_cam_entry *addr_cam = &rtwvif->addr_cam;
615 	u8 sma_hash, tma_hash, addr_msk_start;
616 	u8 sma_start = 0;
617 	u8 tma_start = 0;
618 
619 	if (addr_cam->addr_mask != 0) {
620 		addr_msk_start = __ffs(addr_cam->addr_mask);
621 		if (addr_cam->mask_sel == RTW89_SMA)
622 			sma_start = addr_msk_start;
623 		else if (addr_cam->mask_sel == RTW89_TMA)
624 			tma_start = addr_msk_start;
625 	}
626 	sma_hash = rtw89_cam_addr_hash(sma_start, rtwvif->mac_addr);
627 	tma_hash = rtw89_cam_addr_hash(tma_start, addr_cam->tma);
628 
629 	FWCMD_SET_ADDR_IDX(cmd, addr_cam->addr_cam_idx);
630 	FWCMD_SET_ADDR_OFFSET(cmd, addr_cam->offset);
631 	FWCMD_SET_ADDR_LEN(cmd, addr_cam->len);
632 
633 	FWCMD_SET_ADDR_VALID(cmd, addr_cam->valid);
634 	FWCMD_SET_ADDR_NET_TYPE(cmd, rtwvif->net_type);
635 	FWCMD_SET_ADDR_BCN_HIT_COND(cmd, rtwvif->bcn_hit_cond);
636 	FWCMD_SET_ADDR_HIT_RULE(cmd, rtwvif->hit_rule);
637 	FWCMD_SET_ADDR_BB_SEL(cmd, rtwvif->phy_idx);
638 	FWCMD_SET_ADDR_ADDR_MASK(cmd, addr_cam->addr_mask);
639 	FWCMD_SET_ADDR_MASK_SEL(cmd, addr_cam->mask_sel);
640 	FWCMD_SET_ADDR_SMA_HASH(cmd, sma_hash);
641 	FWCMD_SET_ADDR_TMA_HASH(cmd, tma_hash);
642 
643 	FWCMD_SET_ADDR_BSSID_CAM_IDX(cmd, addr_cam->bssid_cam_idx);
644 
645 	FWCMD_SET_ADDR_SMA0(cmd, rtwvif->mac_addr[0]);
646 	FWCMD_SET_ADDR_SMA1(cmd, rtwvif->mac_addr[1]);
647 	FWCMD_SET_ADDR_SMA2(cmd, rtwvif->mac_addr[2]);
648 	FWCMD_SET_ADDR_SMA3(cmd, rtwvif->mac_addr[3]);
649 	FWCMD_SET_ADDR_SMA4(cmd, rtwvif->mac_addr[4]);
650 	FWCMD_SET_ADDR_SMA5(cmd, rtwvif->mac_addr[5]);
651 
652 	FWCMD_SET_ADDR_TMA0(cmd, addr_cam->tma[0]);
653 	FWCMD_SET_ADDR_TMA1(cmd, addr_cam->tma[1]);
654 	FWCMD_SET_ADDR_TMA2(cmd, addr_cam->tma[2]);
655 	FWCMD_SET_ADDR_TMA3(cmd, addr_cam->tma[3]);
656 	FWCMD_SET_ADDR_TMA4(cmd, addr_cam->tma[4]);
657 	FWCMD_SET_ADDR_TMA5(cmd, addr_cam->tma[5]);
658 
659 	FWCMD_SET_ADDR_PORT_INT(cmd, rtwvif->port);
660 	FWCMD_SET_ADDR_TSF_SYNC(cmd, rtwvif->port);
661 	FWCMD_SET_ADDR_TF_TRS(cmd, rtwvif->trigger);
662 	FWCMD_SET_ADDR_LSIG_TXOP(cmd, rtwvif->lsig_txop);
663 	FWCMD_SET_ADDR_TGT_IND(cmd, rtwvif->tgt_ind);
664 	FWCMD_SET_ADDR_FRM_TGT_IND(cmd, rtwvif->frm_tgt_ind);
665 
666 	if (vif->type == NL80211_IFTYPE_STATION) {
667 		sta = rtwvif->mgd.ap;
668 		if (sta) {
669 			rtwsta = (struct rtw89_sta *)sta->drv_priv;
670 			FWCMD_SET_ADDR_MACID(cmd, rtwsta->mac_id);
671 			FWCMD_SET_ADDR_AID12(cmd, vif->bss_conf.aid & 0xfff);
672 		}
673 	}
674 	FWCMD_SET_ADDR_WOL_PATTERN(cmd, rtwvif->wowlan_pattern);
675 	FWCMD_SET_ADDR_WOL_UC(cmd, rtwvif->wowlan_uc);
676 	FWCMD_SET_ADDR_WOL_MAGIC(cmd, rtwvif->wowlan_magic);
677 	FWCMD_SET_ADDR_WAPI(cmd, addr_cam->wapi);
678 	FWCMD_SET_ADDR_SEC_ENT_MODE(cmd, addr_cam->sec_ent_mode);
679 	FWCMD_SET_ADDR_SEC_ENT0_KEYID(cmd, addr_cam->sec_ent_keyid[0]);
680 	FWCMD_SET_ADDR_SEC_ENT1_KEYID(cmd, addr_cam->sec_ent_keyid[1]);
681 	FWCMD_SET_ADDR_SEC_ENT2_KEYID(cmd, addr_cam->sec_ent_keyid[2]);
682 	FWCMD_SET_ADDR_SEC_ENT3_KEYID(cmd, addr_cam->sec_ent_keyid[3]);
683 	FWCMD_SET_ADDR_SEC_ENT4_KEYID(cmd, addr_cam->sec_ent_keyid[4]);
684 	FWCMD_SET_ADDR_SEC_ENT5_KEYID(cmd, addr_cam->sec_ent_keyid[5]);
685 	FWCMD_SET_ADDR_SEC_ENT6_KEYID(cmd, addr_cam->sec_ent_keyid[6]);
686 
687 	FWCMD_SET_ADDR_SEC_ENT_VALID(cmd, addr_cam->sec_cam_map[0] & 0xff);
688 	FWCMD_SET_ADDR_SEC_ENT0(cmd, addr_cam->sec_ent[0]);
689 	FWCMD_SET_ADDR_SEC_ENT1(cmd, addr_cam->sec_ent[1]);
690 	FWCMD_SET_ADDR_SEC_ENT2(cmd, addr_cam->sec_ent[2]);
691 	FWCMD_SET_ADDR_SEC_ENT3(cmd, addr_cam->sec_ent[3]);
692 	FWCMD_SET_ADDR_SEC_ENT4(cmd, addr_cam->sec_ent[4]);
693 	FWCMD_SET_ADDR_SEC_ENT5(cmd, addr_cam->sec_ent[5]);
694 	FWCMD_SET_ADDR_SEC_ENT6(cmd, addr_cam->sec_ent[6]);
695 }
696