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