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