-
Notifications
You must be signed in to change notification settings - Fork 159
/
chan.c
2470 lines (2051 loc) · 64.7 KB
/
chan.c
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/* Copyright(c) 2020-2022 Realtek Corporation
*/
#include "chan.h"
#include "coex.h"
#include "debug.h"
#include "fw.h"
#include "mac.h"
#include "ps.h"
#include "util.h"
static enum rtw89_subband rtw89_get_subband_type(enum rtw89_band band,
u8 center_chan)
{
switch (band) {
default:
case RTW89_BAND_2G:
switch (center_chan) {
default:
case 1 ... 14:
return RTW89_CH_2G;
}
case RTW89_BAND_5G:
switch (center_chan) {
default:
case 36 ... 64:
return RTW89_CH_5G_BAND_1;
case 100 ... 144:
return RTW89_CH_5G_BAND_3;
case 149 ... 177:
return RTW89_CH_5G_BAND_4;
}
case RTW89_BAND_6G:
switch (center_chan) {
default:
case 1 ... 29:
return RTW89_CH_6G_BAND_IDX0;
case 33 ... 61:
return RTW89_CH_6G_BAND_IDX1;
case 65 ... 93:
return RTW89_CH_6G_BAND_IDX2;
case 97 ... 125:
return RTW89_CH_6G_BAND_IDX3;
case 129 ... 157:
return RTW89_CH_6G_BAND_IDX4;
case 161 ... 189:
return RTW89_CH_6G_BAND_IDX5;
case 193 ... 221:
return RTW89_CH_6G_BAND_IDX6;
case 225 ... 253:
return RTW89_CH_6G_BAND_IDX7;
}
}
}
static enum rtw89_sc_offset rtw89_get_primary_chan_idx(enum rtw89_bandwidth bw,
u32 center_freq,
u32 primary_freq)
{
u8 primary_chan_idx;
u32 offset;
switch (bw) {
default:
case RTW89_CHANNEL_WIDTH_20:
primary_chan_idx = RTW89_SC_DONT_CARE;
break;
case RTW89_CHANNEL_WIDTH_40:
if (primary_freq > center_freq)
primary_chan_idx = RTW89_SC_20_UPPER;
else
primary_chan_idx = RTW89_SC_20_LOWER;
break;
case RTW89_CHANNEL_WIDTH_80:
case RTW89_CHANNEL_WIDTH_160:
if (primary_freq > center_freq) {
offset = (primary_freq - center_freq - 10) / 20;
primary_chan_idx = RTW89_SC_20_UPPER + offset * 2;
} else {
offset = (center_freq - primary_freq - 10) / 20;
primary_chan_idx = RTW89_SC_20_LOWER + offset * 2;
}
break;
}
return primary_chan_idx;
}
static u8 rtw89_get_primary_sb_idx(u8 central_ch, u8 pri_ch,
enum rtw89_bandwidth bw)
{
static const u8 prisb_cal_ofst[RTW89_CHANNEL_WIDTH_ORDINARY_NUM] = {
0, 2, 6, 14, 30
};
if (bw >= RTW89_CHANNEL_WIDTH_ORDINARY_NUM)
return 0;
return (prisb_cal_ofst[bw] + pri_ch - central_ch) / 4;
}
void rtw89_chan_create(struct rtw89_chan *chan, u8 center_chan, u8 primary_chan,
enum rtw89_band band, enum rtw89_bandwidth bandwidth)
{
enum nl80211_band nl_band = rtw89_hw_to_nl80211_band(band);
u32 center_freq, primary_freq;
memset(chan, 0, sizeof(*chan));
chan->channel = center_chan;
chan->primary_channel = primary_chan;
chan->band_type = band;
chan->band_width = bandwidth;
center_freq = ieee80211_channel_to_frequency(center_chan, nl_band);
primary_freq = ieee80211_channel_to_frequency(primary_chan, nl_band);
chan->freq = center_freq;
chan->subband_type = rtw89_get_subband_type(band, center_chan);
chan->pri_ch_idx = rtw89_get_primary_chan_idx(bandwidth, center_freq,
primary_freq);
chan->pri_sb_idx = rtw89_get_primary_sb_idx(center_chan, primary_chan,
bandwidth);
}
bool rtw89_assign_entity_chan(struct rtw89_dev *rtwdev,
enum rtw89_sub_entity_idx idx,
const struct rtw89_chan *new)
{
struct rtw89_hal *hal = &rtwdev->hal;
struct rtw89_chan *chan = &hal->sub[idx].chan;
struct rtw89_chan_rcd *rcd = &hal->sub[idx].rcd;
bool band_changed;
rcd->prev_primary_channel = chan->primary_channel;
rcd->prev_band_type = chan->band_type;
band_changed = new->band_type != chan->band_type;
rcd->band_changed = band_changed;
*chan = *new;
return band_changed;
}
static void __rtw89_config_entity_chandef(struct rtw89_dev *rtwdev,
enum rtw89_sub_entity_idx idx,
const struct cfg80211_chan_def *chandef,
bool from_stack)
{
struct rtw89_hal *hal = &rtwdev->hal;
hal->sub[idx].chandef = *chandef;
if (from_stack)
set_bit(idx, hal->entity_map);
}
void rtw89_config_entity_chandef(struct rtw89_dev *rtwdev,
enum rtw89_sub_entity_idx idx,
const struct cfg80211_chan_def *chandef)
{
__rtw89_config_entity_chandef(rtwdev, idx, chandef, true);
}
void rtw89_config_roc_chandef(struct rtw89_dev *rtwdev,
enum rtw89_sub_entity_idx idx,
const struct cfg80211_chan_def *chandef)
{
struct rtw89_hal *hal = &rtwdev->hal;
enum rtw89_sub_entity_idx cur;
if (chandef) {
cur = atomic_cmpxchg(&hal->roc_entity_idx,
RTW89_SUB_ENTITY_IDLE, idx);
if (cur != RTW89_SUB_ENTITY_IDLE) {
rtw89_debug(rtwdev, RTW89_DBG_TXRX,
"ROC still processing on entity %d\n", idx);
return;
}
hal->roc_chandef = *chandef;
} else {
cur = atomic_cmpxchg(&hal->roc_entity_idx, idx,
RTW89_SUB_ENTITY_IDLE);
if (cur == idx)
return;
if (cur == RTW89_SUB_ENTITY_IDLE)
rtw89_debug(rtwdev, RTW89_DBG_TXRX,
"ROC already finished on entity %d\n", idx);
else
rtw89_debug(rtwdev, RTW89_DBG_TXRX,
"ROC is processing on entity %d\n", cur);
}
}
static void rtw89_config_default_chandef(struct rtw89_dev *rtwdev)
{
struct cfg80211_chan_def chandef = {0};
rtw89_get_default_chandef(&chandef);
__rtw89_config_entity_chandef(rtwdev, RTW89_SUB_ENTITY_0, &chandef, false);
}
void rtw89_entity_init(struct rtw89_dev *rtwdev)
{
struct rtw89_hal *hal = &rtwdev->hal;
hal->entity_pause = false;
bitmap_zero(hal->entity_map, NUM_OF_RTW89_SUB_ENTITY);
bitmap_zero(hal->changes, NUM_OF_RTW89_CHANCTX_CHANGES);
atomic_set(&hal->roc_entity_idx, RTW89_SUB_ENTITY_IDLE);
rtw89_config_default_chandef(rtwdev);
}
static void rtw89_entity_calculate_weight(struct rtw89_dev *rtwdev,
struct rtw89_entity_weight *w)
{
struct rtw89_hal *hal = &rtwdev->hal;
const struct rtw89_chanctx_cfg *cfg;
struct rtw89_vif *rtwvif;
int idx;
for_each_set_bit(idx, hal->entity_map, NUM_OF_RTW89_SUB_ENTITY) {
cfg = hal->sub[idx].cfg;
if (!cfg) {
/* doesn't run with chanctx ops; one channel at most */
w->active_chanctxs = 1;
break;
}
if (cfg->ref_count > 0)
w->active_chanctxs++;
}
rtw89_for_each_rtwvif(rtwdev, rtwvif) {
if (rtwvif->chanctx_assigned)
w->active_roles++;
}
}
enum rtw89_entity_mode rtw89_entity_recalc(struct rtw89_dev *rtwdev)
{
DECLARE_BITMAP(recalc_map, NUM_OF_RTW89_SUB_ENTITY) = {};
struct rtw89_hal *hal = &rtwdev->hal;
const struct cfg80211_chan_def *chandef;
struct rtw89_entity_weight w = {};
enum rtw89_entity_mode mode;
struct rtw89_chan chan;
u8 idx;
lockdep_assert_held(&rtwdev->mutex);
bitmap_copy(recalc_map, hal->entity_map, NUM_OF_RTW89_SUB_ENTITY);
rtw89_entity_calculate_weight(rtwdev, &w);
switch (w.active_chanctxs) {
default:
rtw89_warn(rtwdev, "unknown ent chanctxs weight: %d\n",
w.active_chanctxs);
bitmap_zero(recalc_map, NUM_OF_RTW89_SUB_ENTITY);
fallthrough;
case 0:
rtw89_config_default_chandef(rtwdev);
set_bit(RTW89_SUB_ENTITY_0, recalc_map);
fallthrough;
case 1:
mode = RTW89_ENTITY_MODE_SCC;
break;
case 2 ... NUM_OF_RTW89_SUB_ENTITY:
if (w.active_roles != NUM_OF_RTW89_MCC_ROLES) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"unhandled ent: %d chanctxs %d roles\n",
w.active_chanctxs, w.active_roles);
return RTW89_ENTITY_MODE_UNHANDLED;
}
mode = rtw89_get_entity_mode(rtwdev);
if (mode == RTW89_ENTITY_MODE_MCC)
break;
mode = RTW89_ENTITY_MODE_MCC_PREPARE;
break;
}
for_each_set_bit(idx, recalc_map, NUM_OF_RTW89_SUB_ENTITY) {
chandef = rtw89_chandef_get(rtwdev, idx);
rtw89_get_channel_params(chandef, &chan);
if (chan.channel == 0) {
WARN(1, "Invalid channel on chanctx %d\n", idx);
return RTW89_ENTITY_MODE_INVALID;
}
rtw89_assign_entity_chan(rtwdev, idx, &chan);
}
if (hal->entity_pause)
return rtw89_get_entity_mode(rtwdev);
rtw89_set_entity_mode(rtwdev, mode);
return mode;
}
static void rtw89_chanctx_notify(struct rtw89_dev *rtwdev,
enum rtw89_chanctx_state state)
{
const struct rtw89_chip_info *chip = rtwdev->chip;
const struct rtw89_chanctx_listener *listener = chip->chanctx_listener;
int i;
if (!listener)
return;
for (i = 0; i < NUM_OF_RTW89_CHANCTX_CALLBACKS; i++) {
if (!listener->callbacks[i])
continue;
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"chanctx notify listener: cb %d, state %d\n",
i, state);
listener->callbacks[i](rtwdev, state);
}
}
static bool rtw89_concurrent_via_mrc(struct rtw89_dev *rtwdev)
{
enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen;
return chip_gen == RTW89_CHIP_BE;
}
/* This function centrally manages how MCC roles are sorted and iterated.
* And, it guarantees that ordered_idx is less than NUM_OF_RTW89_MCC_ROLES.
* So, if data needs to pass an array for ordered_idx, the array can declare
* with NUM_OF_RTW89_MCC_ROLES. Besides, the entire iteration will stop
* immediately as long as iterator returns a non-zero value.
*/
static
int rtw89_iterate_mcc_roles(struct rtw89_dev *rtwdev,
int (*iterator)(struct rtw89_dev *rtwdev,
struct rtw89_mcc_role *mcc_role,
unsigned int ordered_idx,
void *data),
void *data)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role * const roles[] = {
&mcc->role_ref,
&mcc->role_aux,
};
unsigned int idx;
int ret;
BUILD_BUG_ON(ARRAY_SIZE(roles) != NUM_OF_RTW89_MCC_ROLES);
for (idx = 0; idx < NUM_OF_RTW89_MCC_ROLES; idx++) {
ret = iterator(rtwdev, roles[idx], idx, data);
if (ret)
return ret;
}
return 0;
}
static u32 rtw89_mcc_get_tbtt_ofst(struct rtw89_dev *rtwdev,
struct rtw89_mcc_role *role, u64 tsf)
{
struct rtw89_vif *rtwvif = role->rtwvif;
u32 bcn_intvl_us = ieee80211_tu_to_usec(role->beacon_interval);
u64 sync_tsf = READ_ONCE(rtwvif->sync_bcn_tsf);
u32 remainder;
if (tsf < sync_tsf) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC get tbtt ofst: tsf might not update yet\n");
sync_tsf = 0;
}
div_u64_rem(tsf - sync_tsf, bcn_intvl_us, &remainder);
return remainder;
}
static int __mcc_fw_req_tsf(struct rtw89_dev *rtwdev, u64 *tsf_ref, u64 *tsf_aux)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
struct rtw89_mac_mcc_tsf_rpt rpt = {};
struct rtw89_fw_mcc_tsf_req req = {};
int ret;
req.group = mcc->group;
req.macid_x = ref->rtwvif->mac_id;
req.macid_y = aux->rtwvif->mac_id;
ret = rtw89_fw_h2c_mcc_req_tsf(rtwdev, &req, &rpt);
if (ret) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC h2c failed to request tsf: %d\n", ret);
return ret;
}
*tsf_ref = (u64)rpt.tsf_x_high << 32 | rpt.tsf_x_low;
*tsf_aux = (u64)rpt.tsf_y_high << 32 | rpt.tsf_y_low;
return 0;
}
static int __mrc_fw_req_tsf(struct rtw89_dev *rtwdev, u64 *tsf_ref, u64 *tsf_aux)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
struct rtw89_fw_mrc_req_tsf_arg arg = {};
struct rtw89_mac_mrc_tsf_rpt rpt = {};
int ret;
BUILD_BUG_ON(RTW89_MAC_MRC_MAX_REQ_TSF_NUM < NUM_OF_RTW89_MCC_ROLES);
arg.num = 2;
arg.infos[0].band = ref->rtwvif->mac_idx;
arg.infos[0].port = ref->rtwvif->port;
arg.infos[1].band = aux->rtwvif->mac_idx;
arg.infos[1].port = aux->rtwvif->port;
ret = rtw89_fw_h2c_mrc_req_tsf(rtwdev, &arg, &rpt);
if (ret) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MRC h2c failed to request tsf: %d\n", ret);
return ret;
}
*tsf_ref = rpt.tsfs[0];
*tsf_aux = rpt.tsfs[1];
return 0;
}
static u16 rtw89_mcc_get_bcn_ofst(struct rtw89_dev *rtwdev)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
u32 bcn_intvl_ref_us = ieee80211_tu_to_usec(ref->beacon_interval);
u32 tbtt_ofst_ref, tbtt_ofst_aux;
u64 tsf_ref, tsf_aux;
int ret;
if (rtw89_concurrent_via_mrc(rtwdev))
ret = __mrc_fw_req_tsf(rtwdev, &tsf_ref, &tsf_aux);
else
ret = __mcc_fw_req_tsf(rtwdev, &tsf_ref, &tsf_aux);
if (ret)
return RTW89_MCC_DFLT_BCN_OFST_TIME;
tbtt_ofst_ref = rtw89_mcc_get_tbtt_ofst(rtwdev, ref, tsf_ref);
tbtt_ofst_aux = rtw89_mcc_get_tbtt_ofst(rtwdev, aux, tsf_aux);
while (tbtt_ofst_ref < tbtt_ofst_aux)
tbtt_ofst_ref += bcn_intvl_ref_us;
return (tbtt_ofst_ref - tbtt_ofst_aux) / 1024;
}
static
void rtw89_mcc_role_fw_macid_bitmap_set_bit(struct rtw89_mcc_role *mcc_role,
unsigned int bit)
{
unsigned int idx = bit / 8;
unsigned int pos = bit % 8;
if (idx >= ARRAY_SIZE(mcc_role->macid_bitmap))
return;
mcc_role->macid_bitmap[idx] |= BIT(pos);
}
static
u32 rtw89_mcc_role_fw_macid_bitmap_to_u32(struct rtw89_mcc_role *mcc_role)
{
unsigned int macid;
unsigned int i, j;
u32 bitmap = 0;
for (i = 0; i < ARRAY_SIZE(mcc_role->macid_bitmap); i++) {
for (j = 0; j < 8; j++) {
macid = i * 8 + j;
if (macid >= 32)
goto out;
if (mcc_role->macid_bitmap[i] & BIT(j))
bitmap |= BIT(macid);
}
}
out:
return bitmap;
}
static void rtw89_mcc_role_macid_sta_iter(void *data, struct ieee80211_sta *sta)
{
struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
struct rtw89_vif *rtwvif = rtwsta->rtwvif;
struct rtw89_mcc_role *mcc_role = data;
struct rtw89_vif *target = mcc_role->rtwvif;
if (rtwvif != target)
return;
rtw89_mcc_role_fw_macid_bitmap_set_bit(mcc_role, rtwsta->mac_id);
}
static void rtw89_mcc_fill_role_macid_bitmap(struct rtw89_dev *rtwdev,
struct rtw89_mcc_role *mcc_role)
{
struct rtw89_vif *rtwvif = mcc_role->rtwvif;
rtw89_mcc_role_fw_macid_bitmap_set_bit(mcc_role, rtwvif->mac_id);
ieee80211_iterate_stations_atomic(rtwdev->hw,
rtw89_mcc_role_macid_sta_iter,
mcc_role);
}
static void rtw89_mcc_fill_role_policy(struct rtw89_dev *rtwdev,
struct rtw89_mcc_role *mcc_role)
{
struct rtw89_mcc_policy *policy = &mcc_role->policy;
policy->c2h_rpt = RTW89_FW_MCC_C2H_RPT_ALL;
policy->tx_null_early = RTW89_MCC_DFLT_TX_NULL_EARLY;
policy->in_curr_ch = false;
policy->dis_sw_retry = true;
policy->sw_retry_count = false;
if (mcc_role->is_go)
policy->dis_tx_null = true;
else
policy->dis_tx_null = false;
}
static void rtw89_mcc_fill_role_limit(struct rtw89_dev *rtwdev,
struct rtw89_mcc_role *mcc_role)
{
struct ieee80211_vif *vif = rtwvif_to_vif(mcc_role->rtwvif);
struct ieee80211_p2p_noa_desc *noa_desc;
u32 bcn_intvl_us = ieee80211_tu_to_usec(mcc_role->beacon_interval);
u32 max_toa_us, max_tob_us, max_dur_us;
u32 start_time, interval, duration;
u64 tsf, tsf_lmt;
int ret;
int i;
if (!mcc_role->is_go && !mcc_role->is_gc)
return;
/* find the first periodic NoA */
for (i = 0; i < RTW89_P2P_MAX_NOA_NUM; i++) {
noa_desc = &vif->bss_conf.p2p_noa_attr.desc[i];
if (noa_desc->count == 255)
goto fill;
}
return;
fill:
start_time = le32_to_cpu(noa_desc->start_time);
interval = le32_to_cpu(noa_desc->interval);
duration = le32_to_cpu(noa_desc->duration);
if (interval != bcn_intvl_us) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC role limit: mismatch interval: %d vs. %d\n",
interval, bcn_intvl_us);
return;
}
ret = rtw89_mac_port_get_tsf(rtwdev, mcc_role->rtwvif, &tsf);
if (ret) {
rtw89_warn(rtwdev, "MCC failed to get port tsf: %d\n", ret);
return;
}
tsf_lmt = (tsf & GENMASK_ULL(63, 32)) | start_time;
max_toa_us = rtw89_mcc_get_tbtt_ofst(rtwdev, mcc_role, tsf_lmt);
max_dur_us = interval - duration;
max_tob_us = max_dur_us - max_toa_us;
if (!max_toa_us || !max_tob_us) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC role limit: hit boundary\n");
return;
}
if (max_dur_us < max_toa_us) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC role limit: insufficient duration\n");
return;
}
mcc_role->limit.max_toa = max_toa_us / 1024;
mcc_role->limit.max_tob = max_tob_us / 1024;
mcc_role->limit.max_dur = max_dur_us / 1024;
mcc_role->limit.enable = true;
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC role limit: max_toa %d, max_tob %d, max_dur %d\n",
mcc_role->limit.max_toa, mcc_role->limit.max_tob,
mcc_role->limit.max_dur);
}
static int rtw89_mcc_fill_role(struct rtw89_dev *rtwdev,
struct rtw89_vif *rtwvif,
struct rtw89_mcc_role *role)
{
struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
const struct rtw89_chan *chan;
memset(role, 0, sizeof(*role));
role->rtwvif = rtwvif;
role->beacon_interval = vif->bss_conf.beacon_int;
if (!role->beacon_interval) {
rtw89_warn(rtwdev,
"cannot handle MCC role without beacon interval\n");
return -EINVAL;
}
role->duration = role->beacon_interval / 2;
chan = rtw89_chan_get(rtwdev, rtwvif->sub_entity_idx);
role->is_2ghz = chan->band_type == RTW89_BAND_2G;
role->is_go = rtwvif->wifi_role == RTW89_WIFI_ROLE_P2P_GO;
role->is_gc = rtwvif->wifi_role == RTW89_WIFI_ROLE_P2P_CLIENT;
rtw89_mcc_fill_role_macid_bitmap(rtwdev, role);
rtw89_mcc_fill_role_policy(rtwdev, role);
rtw89_mcc_fill_role_limit(rtwdev, role);
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC role: bcn_intvl %d, is_2ghz %d, is_go %d, is_gc %d\n",
role->beacon_interval, role->is_2ghz, role->is_go, role->is_gc);
return 0;
}
static void rtw89_mcc_fill_bt_role(struct rtw89_dev *rtwdev)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_bt_role *bt_role = &mcc->bt_role;
memset(bt_role, 0, sizeof(*bt_role));
bt_role->duration = rtw89_coex_query_bt_req_len(rtwdev, RTW89_PHY_0);
rtw89_debug(rtwdev, RTW89_DBG_CHAN, "MCC bt role: dur %d\n",
bt_role->duration);
}
struct rtw89_mcc_fill_role_selector {
struct rtw89_vif *bind_vif[NUM_OF_RTW89_SUB_ENTITY];
};
static_assert((u8)NUM_OF_RTW89_SUB_ENTITY >= NUM_OF_RTW89_MCC_ROLES);
static int rtw89_mcc_fill_role_iterator(struct rtw89_dev *rtwdev,
struct rtw89_mcc_role *mcc_role,
unsigned int ordered_idx,
void *data)
{
struct rtw89_mcc_fill_role_selector *sel = data;
struct rtw89_vif *role_vif = sel->bind_vif[ordered_idx];
int ret;
if (!role_vif) {
rtw89_warn(rtwdev, "cannot handle MCC without role[%d]\n",
ordered_idx);
return -EINVAL;
}
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC fill role[%d] with vif <macid %d>\n",
ordered_idx, role_vif->mac_id);
ret = rtw89_mcc_fill_role(rtwdev, role_vif, mcc_role);
if (ret)
return ret;
return 0;
}
static int rtw89_mcc_fill_all_roles(struct rtw89_dev *rtwdev)
{
struct rtw89_mcc_fill_role_selector sel = {};
struct rtw89_vif *rtwvif;
int ret;
rtw89_for_each_rtwvif(rtwdev, rtwvif) {
if (!rtwvif->chanctx_assigned)
continue;
if (sel.bind_vif[rtwvif->sub_entity_idx]) {
rtw89_warn(rtwdev,
"MCC skip extra vif <macid %d> on chanctx[%d]\n",
rtwvif->mac_id, rtwvif->sub_entity_idx);
continue;
}
sel.bind_vif[rtwvif->sub_entity_idx] = rtwvif;
}
ret = rtw89_iterate_mcc_roles(rtwdev, rtw89_mcc_fill_role_iterator, &sel);
if (ret)
return ret;
rtw89_mcc_fill_bt_role(rtwdev);
return 0;
}
static void rtw89_mcc_assign_pattern(struct rtw89_dev *rtwdev,
const struct rtw89_mcc_pattern *new)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
struct rtw89_mcc_config *config = &mcc->config;
struct rtw89_mcc_pattern *pattern = &config->pattern;
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC assign pattern: ref {%d | %d}, aux {%d | %d}\n",
new->tob_ref, new->toa_ref, new->tob_aux, new->toa_aux);
*pattern = *new;
memset(&pattern->courtesy, 0, sizeof(pattern->courtesy));
if (pattern->tob_aux <= 0 || pattern->toa_aux <= 0) {
pattern->courtesy.macid_tgt = aux->rtwvif->mac_id;
pattern->courtesy.macid_src = ref->rtwvif->mac_id;
pattern->courtesy.slot_num = RTW89_MCC_DFLT_COURTESY_SLOT;
pattern->courtesy.enable = true;
} else if (pattern->tob_ref <= 0 || pattern->toa_ref <= 0) {
pattern->courtesy.macid_tgt = ref->rtwvif->mac_id;
pattern->courtesy.macid_src = aux->rtwvif->mac_id;
pattern->courtesy.slot_num = RTW89_MCC_DFLT_COURTESY_SLOT;
pattern->courtesy.enable = true;
}
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC pattern flags: plan %d, courtesy_en %d\n",
pattern->plan, pattern->courtesy.enable);
if (!pattern->courtesy.enable)
return;
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC pattern courtesy: tgt %d, src %d, slot %d\n",
pattern->courtesy.macid_tgt, pattern->courtesy.macid_src,
pattern->courtesy.slot_num);
}
/* The follow-up roughly shows the relationship between the parameters
* for pattern calculation.
*
* |< duration ref >| (if mid bt) |< duration aux >|
* |< tob ref >|< toa ref >| ... |< tob aux >|< toa aux >|
* V V
* tbtt ref tbtt aux
* |< beacon offset >|
*
* In loose pattern calculation, we only ensure at least tob_ref and
* toa_ref have positive results. If tob_aux or toa_aux is negative
* unfortunately, FW will be notified to handle it with courtesy
* mechanism.
*/
static void __rtw89_mcc_calc_pattern_loose(struct rtw89_dev *rtwdev,
struct rtw89_mcc_pattern *ptrn,
bool hdl_bt)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
struct rtw89_mcc_config *config = &mcc->config;
u16 bcn_ofst = config->beacon_offset;
u16 bt_dur_in_mid = 0;
u16 max_bcn_ofst;
s16 upper, lower;
u16 res;
*ptrn = (typeof(*ptrn)){
.plan = hdl_bt ? RTW89_MCC_PLAN_TAIL_BT : RTW89_MCC_PLAN_NO_BT,
};
if (!hdl_bt)
goto calc;
max_bcn_ofst = ref->duration + aux->duration;
if (ref->limit.enable)
max_bcn_ofst = min_t(u16, max_bcn_ofst,
ref->limit.max_toa + aux->duration);
else if (aux->limit.enable)
max_bcn_ofst = min_t(u16, max_bcn_ofst,
ref->duration + aux->limit.max_tob);
if (bcn_ofst > max_bcn_ofst && bcn_ofst >= mcc->bt_role.duration) {
bt_dur_in_mid = mcc->bt_role.duration;
ptrn->plan = RTW89_MCC_PLAN_MID_BT;
}
calc:
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_ls: plan %d, bcn_ofst %d\n",
ptrn->plan, bcn_ofst);
res = bcn_ofst - bt_dur_in_mid;
upper = min_t(s16, ref->duration, res);
lower = 0;
if (ref->limit.enable) {
upper = min_t(s16, upper, ref->limit.max_toa);
lower = max_t(s16, lower, ref->duration - ref->limit.max_tob);
} else if (aux->limit.enable) {
upper = min_t(s16, upper,
res - (aux->duration - aux->limit.max_toa));
lower = max_t(s16, lower, res - aux->limit.max_tob);
}
if (lower < upper)
ptrn->toa_ref = (upper + lower) / 2;
else
ptrn->toa_ref = lower;
ptrn->tob_ref = ref->duration - ptrn->toa_ref;
ptrn->tob_aux = res - ptrn->toa_ref;
ptrn->toa_aux = aux->duration - ptrn->tob_aux;
}
/* In strict pattern calculation, we consider timing that might need
* for HW stuffs, i.e. min_tob and min_toa.
*/
static int __rtw89_mcc_calc_pattern_strict(struct rtw89_dev *rtwdev,
struct rtw89_mcc_pattern *ptrn)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
struct rtw89_mcc_config *config = &mcc->config;
u16 min_tob = RTW89_MCC_EARLY_RX_BCN_TIME;
u16 min_toa = RTW89_MCC_MIN_RX_BCN_TIME;
u16 bcn_ofst = config->beacon_offset;
s16 upper_toa_ref, lower_toa_ref;
s16 upper_tob_aux, lower_tob_aux;
u16 bt_dur_in_mid;
s16 res;
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: plan %d, bcn_ofst %d\n",
ptrn->plan, bcn_ofst);
if (ptrn->plan == RTW89_MCC_PLAN_MID_BT)
bt_dur_in_mid = mcc->bt_role.duration;
else
bt_dur_in_mid = 0;
if (ref->duration < min_tob + min_toa) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: not meet ref dur cond\n");
return -EINVAL;
}
if (aux->duration < min_tob + min_toa) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: not meet aux dur cond\n");
return -EINVAL;
}
res = bcn_ofst - min_toa - min_tob - bt_dur_in_mid;
if (res < 0) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: not meet bcn_ofst cond\n");
return -EINVAL;
}
upper_toa_ref = min_t(s16, min_toa + res, ref->duration - min_tob);
lower_toa_ref = min_toa;
upper_tob_aux = min_t(s16, min_tob + res, aux->duration - min_toa);
lower_tob_aux = min_tob;
if (ref->limit.enable) {
if (min_tob > ref->limit.max_tob || min_toa > ref->limit.max_toa) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: conflict ref limit\n");
return -EINVAL;
}
upper_toa_ref = min_t(s16, upper_toa_ref, ref->limit.max_toa);
lower_toa_ref = max_t(s16, lower_toa_ref,
ref->duration - ref->limit.max_tob);
} else if (aux->limit.enable) {
if (min_tob > aux->limit.max_tob || min_toa > aux->limit.max_toa) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: conflict aux limit\n");
return -EINVAL;
}
upper_tob_aux = min_t(s16, upper_tob_aux, aux->limit.max_tob);
lower_tob_aux = max_t(s16, lower_tob_aux,
aux->duration - aux->limit.max_toa);
}
upper_toa_ref = min_t(s16, upper_toa_ref,
bcn_ofst - bt_dur_in_mid - lower_tob_aux);
lower_toa_ref = max_t(s16, lower_toa_ref,
bcn_ofst - bt_dur_in_mid - upper_tob_aux);
if (lower_toa_ref > upper_toa_ref) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st: conflict boundary\n");
return -EINVAL;
}
ptrn->toa_ref = (upper_toa_ref + lower_toa_ref) / 2;
ptrn->tob_ref = ref->duration - ptrn->toa_ref;
ptrn->tob_aux = bcn_ofst - ptrn->toa_ref - bt_dur_in_mid;
ptrn->toa_aux = aux->duration - ptrn->tob_aux;
return 0;
}
static int rtw89_mcc_calc_pattern(struct rtw89_dev *rtwdev, bool hdl_bt)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
bool sel_plan[NUM_OF_RTW89_MCC_PLAN] = {};
struct rtw89_mcc_pattern ptrn;
int ret;
int i;
if (ref->limit.enable && aux->limit.enable) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn: not support dual limited roles\n");
return -EINVAL;
}
if (ref->limit.enable &&
ref->duration > ref->limit.max_tob + ref->limit.max_toa) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn: not fit ref limit\n");
return -EINVAL;
}
if (aux->limit.enable &&
aux->duration > aux->limit.max_tob + aux->limit.max_toa) {
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn: not fit aux limit\n");
return -EINVAL;
}
if (hdl_bt) {
sel_plan[RTW89_MCC_PLAN_TAIL_BT] = true;
sel_plan[RTW89_MCC_PLAN_MID_BT] = true;
} else {
sel_plan[RTW89_MCC_PLAN_NO_BT] = true;
}
for (i = 0; i < NUM_OF_RTW89_MCC_PLAN; i++) {
if (!sel_plan[i])
continue;
ptrn = (typeof(ptrn)){
.plan = i,
};
ret = __rtw89_mcc_calc_pattern_strict(rtwdev, &ptrn);
if (ret)
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC calc ptrn_st with plan %d: fail\n", i);
else
goto done;
}
__rtw89_mcc_calc_pattern_loose(rtwdev, &ptrn, hdl_bt);
done:
rtw89_mcc_assign_pattern(rtwdev, &ptrn);
return 0;
}
static void rtw89_mcc_set_default_pattern(struct rtw89_dev *rtwdev)
{
struct rtw89_mcc_info *mcc = &rtwdev->mcc;
struct rtw89_mcc_role *ref = &mcc->role_ref;
struct rtw89_mcc_role *aux = &mcc->role_aux;
struct rtw89_mcc_pattern tmp = {};
rtw89_debug(rtwdev, RTW89_DBG_CHAN,
"MCC use default pattern unexpectedly\n");
tmp.plan = RTW89_MCC_PLAN_NO_BT;
tmp.tob_ref = ref->duration / 2;
tmp.toa_ref = ref->duration - tmp.tob_ref;
tmp.tob_aux = aux->duration / 2;
tmp.toa_aux = aux->duration - tmp.tob_aux;