forked from lwfinger/rtw88
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coex.c
4175 lines (3512 loc) · 113 KB
/
coex.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) 2018-2019 Realtek Corporation
*/
#include "main.h"
#include "coex.h"
#include "fw.h"
#include "ps.h"
#include "debug.h"
#include "reg.h"
#include "phy.h"
static u8 rtw_coex_next_rssi_state(struct rtw_dev *rtwdev, u8 pre_state,
u8 rssi, u8 rssi_thresh)
{
const struct rtw_chip_info *chip = rtwdev->chip;
u8 tol = chip->rssi_tolerance;
u8 next_state;
if (pre_state == COEX_RSSI_STATE_LOW ||
pre_state == COEX_RSSI_STATE_STAY_LOW) {
if (rssi >= (rssi_thresh + tol))
next_state = COEX_RSSI_STATE_HIGH;
else
next_state = COEX_RSSI_STATE_STAY_LOW;
} else {
if (rssi < rssi_thresh)
next_state = COEX_RSSI_STATE_LOW;
else
next_state = COEX_RSSI_STATE_STAY_HIGH;
}
return next_state;
}
static void rtw_coex_limited_tx(struct rtw_dev *rtwdev,
bool tx_limit_en, bool ampdu_limit_en)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
u8 num_of_active_port = 1;
if (!chip->scbd_support)
return;
/* force max tx retry limit = 8 */
if (coex_stat->wl_tx_limit_en == tx_limit_en &&
coex_stat->wl_ampdu_limit_en == ampdu_limit_en)
return;
if (!coex_stat->wl_tx_limit_en) {
coex_stat->darfrc = rtw_read32(rtwdev, REG_DARFRC);
coex_stat->darfrch = rtw_read32(rtwdev, REG_DARFRCH);
coex_stat->retry_limit = rtw_read16(rtwdev, REG_RETRY_LIMIT);
}
if (!coex_stat->wl_ampdu_limit_en)
coex_stat->ampdu_max_time =
rtw_read8(rtwdev, REG_AMPDU_MAX_TIME_V1);
coex_stat->wl_tx_limit_en = tx_limit_en;
coex_stat->wl_ampdu_limit_en = ampdu_limit_en;
if (tx_limit_en) {
/* set BT polluted packet on for tx rate adaptive,
* not including tx retry broken by PTA
*/
rtw_write8_set(rtwdev, REG_TX_HANG_CTRL, BIT_EN_GNT_BT_AWAKE);
/* set queue life time to avoid can't reach tx retry limit
* if tx is always broken by GNT_BT
*/
if (num_of_active_port <= 1)
rtw_write8_set(rtwdev, REG_LIFETIME_EN, 0xf);
rtw_write16(rtwdev, REG_RETRY_LIMIT, 0x0808);
/* auto rate fallback step within 8 retries */
rtw_write32(rtwdev, REG_DARFRC, 0x1000000);
rtw_write32(rtwdev, REG_DARFRCH, 0x4030201);
} else {
rtw_write8_clr(rtwdev, REG_TX_HANG_CTRL, BIT_EN_GNT_BT_AWAKE);
rtw_write8_clr(rtwdev, REG_LIFETIME_EN, 0xf);
rtw_write16(rtwdev, REG_RETRY_LIMIT, coex_stat->retry_limit);
rtw_write32(rtwdev, REG_DARFRC, coex_stat->darfrc);
rtw_write32(rtwdev, REG_DARFRCH, coex_stat->darfrch);
}
if (ampdu_limit_en)
rtw_write8(rtwdev, REG_AMPDU_MAX_TIME_V1, 0x20);
else
rtw_write8(rtwdev, REG_AMPDU_MAX_TIME_V1,
coex_stat->ampdu_max_time);
}
static void rtw_coex_limited_wl(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_dm *coex_dm = &coex->dm;
bool tx_limit = false;
bool tx_agg_ctrl = false;
if (!coex->under_5g && coex_dm->bt_status != COEX_BTSTATUS_NCON_IDLE) {
tx_limit = true;
tx_agg_ctrl = true;
}
rtw_coex_limited_tx(rtwdev, tx_limit, tx_agg_ctrl);
}
static bool rtw_coex_freerun_check(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_dm *coex_dm = &coex->dm;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_efuse *efuse = &rtwdev->efuse;
u8 bt_rssi;
u8 ant_distance = 10;
if (coex_stat->bt_disabled)
return false;
if (efuse->share_ant || ant_distance <= 5 || !coex_stat->wl_gl_busy)
return false;
if (ant_distance >= 40 || coex_stat->bt_hid_pair_num >= 2)
return true;
/* ant_distance = 5 ~ 40 */
if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[1]) &&
COEX_RSSI_HIGH(coex_dm->bt_rssi_state[0]))
return true;
if (coex_stat->wl_tput_dir == COEX_WL_TPUT_TX)
bt_rssi = coex_dm->bt_rssi_state[0];
else
bt_rssi = coex_dm->bt_rssi_state[1];
if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[3]) &&
COEX_RSSI_HIGH(bt_rssi) &&
coex_stat->cnt_wl[COEX_CNT_WL_SCANAP] <= 5)
return true;
return false;
}
static void rtw_coex_wl_slot_extend(struct rtw_dev *rtwdev, bool enable)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
u8 para[6] = {0};
para[0] = COEX_H2C69_WL_LEAKAP;
para[1] = PARA1_H2C69_DIS_5MS;
if (enable)
para[1] = PARA1_H2C69_EN_5MS;
else
coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] = 0;
coex_stat->wl_slot_extend = enable;
rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
}
static void rtw_coex_wl_ccklock_action(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
if (coex->manual_control || coex->stop_dm)
return;
if (coex_stat->tdma_timer_base == 3 && coex_stat->wl_slot_extend) {
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], set h2c 0x69 opcode 12 to turn off 5ms WL slot extend!!\n");
rtw_coex_wl_slot_extend(rtwdev, false);
return;
}
if (coex_stat->wl_slot_extend && coex_stat->wl_force_lps_ctrl &&
!coex_stat->wl_cck_lock_ever) {
if (coex_stat->wl_fw_dbg_info[7] <= 5)
coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND]++;
else
coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] = 0;
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], 5ms WL slot extend cnt = %d!!\n",
coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND]);
if (coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] == 7) {
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], set h2c 0x69 opcode 12 to turn off 5ms WL slot extend!!\n");
rtw_coex_wl_slot_extend(rtwdev, false);
}
} else if (!coex_stat->wl_slot_extend && coex_stat->wl_cck_lock) {
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], set h2c 0x69 opcode 12 to turn on 5ms WL slot extend!!\n");
rtw_coex_wl_slot_extend(rtwdev, true);
}
}
static void rtw_coex_wl_ccklock_detect(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_coex_dm *coex_dm = &coex->dm;
bool is_cck_lock_rate = false;
if (coex_stat->wl_coex_mode != COEX_WLINK_2G1PORT &&
coex_stat->wl_coex_mode != COEX_WLINK_2GFREE)
return;
if (coex_dm->bt_status == COEX_BTSTATUS_INQ_PAGE ||
coex_stat->bt_setup_link) {
coex_stat->wl_cck_lock = false;
coex_stat->wl_cck_lock_pre = false;
return;
}
if (coex_stat->wl_rx_rate <= COEX_CCK_2 ||
coex_stat->wl_rts_rx_rate <= COEX_CCK_2)
is_cck_lock_rate = true;
if (coex_stat->wl_connected && coex_stat->wl_gl_busy &&
COEX_RSSI_HIGH(coex_dm->wl_rssi_state[3]) &&
(coex_dm->bt_status == COEX_BTSTATUS_ACL_BUSY ||
coex_dm->bt_status == COEX_BTSTATUS_ACL_SCO_BUSY ||
coex_dm->bt_status == COEX_BTSTATUS_SCO_BUSY)) {
if (is_cck_lock_rate) {
coex_stat->wl_cck_lock = true;
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], cck locking...\n");
} else {
coex_stat->wl_cck_lock = false;
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], cck unlock...\n");
}
} else {
coex_stat->wl_cck_lock = false;
}
/* CCK lock identification */
if (coex_stat->wl_cck_lock && !coex_stat->wl_cck_lock_pre)
ieee80211_queue_delayed_work(rtwdev->hw, &coex->wl_ccklock_work,
3 * HZ);
coex_stat->wl_cck_lock_pre = coex_stat->wl_cck_lock;
}
static void rtw_coex_wl_noisy_detect(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_dm_info *dm_info = &rtwdev->dm_info;
u32 cnt_cck;
bool wl_cck_lock = false;
/* wifi noisy environment identification */
cnt_cck = dm_info->cck_ok_cnt + dm_info->cck_err_cnt;
if (!coex_stat->wl_gl_busy && !wl_cck_lock) {
if (cnt_cck > 250) {
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] < 5)
coex_stat->cnt_wl[COEX_CNT_WL_NOISY2]++;
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] == 5) {
coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] = 0;
coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] = 0;
}
} else if (cnt_cck < 100) {
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] < 5)
coex_stat->cnt_wl[COEX_CNT_WL_NOISY0]++;
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] == 5) {
coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] = 0;
coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] = 0;
}
} else {
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] < 5)
coex_stat->cnt_wl[COEX_CNT_WL_NOISY1]++;
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] == 5) {
coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] = 0;
coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] = 0;
}
}
if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] == 5)
coex_stat->wl_noisy_level = 2;
else if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] == 5)
coex_stat->wl_noisy_level = 1;
else
coex_stat->wl_noisy_level = 0;
rtw_dbg(rtwdev, RTW_DBG_COEX, "[BTCoex], wl_noisy_level = %d\n",
coex_stat->wl_noisy_level);
}
}
static void rtw_coex_tdma_timer_base(struct rtw_dev *rtwdev, u8 type)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
u8 para[2] = {0};
u8 times;
u16 tbtt_interval = coex_stat->wl_beacon_interval;
if (coex_stat->tdma_timer_base == type)
return;
coex_stat->tdma_timer_base = type;
para[0] = COEX_H2C69_TDMA_SLOT;
rtw_dbg(rtwdev, RTW_DBG_COEX, "[BTCoex], tbtt_interval = %d\n",
tbtt_interval);
if (type == TDMA_TIMER_TYPE_4SLOT && tbtt_interval < 120) {
para[1] = PARA1_H2C69_TDMA_4SLOT; /* 4-slot */
} else if (tbtt_interval < 80 && tbtt_interval > 0) {
times = 100 / tbtt_interval;
if (100 % tbtt_interval != 0)
times++;
para[1] = FIELD_PREP(PARA1_H2C69_TBTT_TIMES, times);
} else if (tbtt_interval >= 180) {
times = tbtt_interval / 100;
if (tbtt_interval % 100 <= 80)
times--;
para[1] = FIELD_PREP(PARA1_H2C69_TBTT_TIMES, times) |
FIELD_PREP(PARA1_H2C69_TBTT_DIV100, 1);
} else {
para[1] = PARA1_H2C69_TDMA_2SLOT;
}
rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
rtw_dbg(rtwdev, RTW_DBG_COEX, "[BTCoex], %s(): h2c_0x69 = 0x%x\n",
__func__, para[1]);
/* no 5ms_wl_slot_extend for 4-slot mode */
if (coex_stat->tdma_timer_base == 3)
rtw_coex_wl_ccklock_action(rtwdev);
}
static void rtw_coex_set_wl_pri_mask(struct rtw_dev *rtwdev, u8 bitmap,
u8 data)
{
u32 addr;
addr = REG_BT_COEX_TABLE_H + (bitmap / 8);
bitmap = bitmap % 8;
rtw_write8_mask(rtwdev, addr, BIT(bitmap), data);
}
void rtw_coex_write_scbd(struct rtw_dev *rtwdev, u16 bitpos, bool set)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
u16 val = 0x2;
if (!chip->scbd_support)
return;
val |= coex_stat->score_board;
/* for 8822b, scbd[10] is CQDDR on
* for 8822c, scbd[10] is no fix 2M
*/
if (!chip->new_scbd10_def && (bitpos & COEX_SCBD_FIX2M)) {
if (set)
val &= ~COEX_SCBD_FIX2M;
else
val |= COEX_SCBD_FIX2M;
} else {
if (set)
val |= bitpos;
else
val &= ~bitpos;
}
if (val != coex_stat->score_board) {
coex_stat->score_board = val;
val |= BIT_BT_INT_EN;
rtw_write16(rtwdev, REG_WIFI_BT_INFO, val);
}
}
EXPORT_SYMBOL(rtw_coex_write_scbd);
static u16 rtw_coex_read_scbd(struct rtw_dev *rtwdev)
{
const struct rtw_chip_info *chip = rtwdev->chip;
if (!chip->scbd_support)
return 0;
return (rtw_read16(rtwdev, REG_WIFI_BT_INFO)) & ~(BIT_BT_INT_EN);
}
static void rtw_coex_check_rfk(struct rtw_dev *rtwdev)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_coex_rfe *coex_rfe = &coex->rfe;
u8 cnt = 0;
u32 wait_cnt;
bool btk, wlk;
if (coex_rfe->wlg_at_btg && chip->scbd_support &&
coex_stat->bt_iqk_state != 0xff) {
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], (Before Ant Setup) Delay by IQK\n");
wait_cnt = COEX_RFK_TIMEOUT / COEX_MIN_DELAY;
do {
/* BT RFK */
btk = !!(rtw_coex_read_scbd(rtwdev) & COEX_SCBD_BT_RFK);
/* WL RFK */
wlk = !!(rtw_read8(rtwdev, REG_ARFR4) & BIT_WL_RFK);
if (!btk && !wlk)
break;
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], (Before Ant Setup) wlk = %d, btk = %d\n",
wlk, btk);
mdelay(COEX_MIN_DELAY);
} while (++cnt < wait_cnt);
if (cnt >= wait_cnt)
coex_stat->bt_iqk_state = 0xff;
}
}
static void rtw_coex_query_bt_info(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
if (coex_stat->bt_disabled)
return;
rtw_dbg(rtwdev, RTW_DBG_COEX, "[BTCoex], %s()\n", __func__);
rtw_fw_query_bt_info(rtwdev);
}
static void rtw_coex_gnt_workaround(struct rtw_dev *rtwdev, bool force, u8 mode)
{
rtw_coex_set_gnt_fix(rtwdev);
}
static void rtw_coex_monitor_bt_ctr(struct rtw_dev *rtwdev)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
u32 tmp;
tmp = rtw_read32(rtwdev, REG_BT_ACT_STATISTICS);
coex_stat->hi_pri_tx = FIELD_GET(MASKLWORD, tmp);
coex_stat->hi_pri_rx = FIELD_GET(MASKHWORD, tmp);
tmp = rtw_read32(rtwdev, REG_BT_ACT_STATISTICS_1);
coex_stat->lo_pri_tx = FIELD_GET(MASKLWORD, tmp);
coex_stat->lo_pri_rx = FIELD_GET(MASKHWORD, tmp);
rtw_write8(rtwdev, REG_BT_COEX_ENH_INTR_CTRL,
BIT_R_GRANTALL_WLMASK | BIT_STATIS_BT_EN);
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
coex_stat->hi_pri_rx, coex_stat->hi_pri_tx,
coex_stat->lo_pri_rx, coex_stat->lo_pri_tx);
}
static void rtw_coex_monitor_bt_enable(struct rtw_dev *rtwdev)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_coex_dm *coex_dm = &coex->dm;
bool bt_disabled = false;
u16 score_board;
if (chip->scbd_support) {
score_board = rtw_coex_read_scbd(rtwdev);
bt_disabled = !(score_board & COEX_SCBD_ONOFF);
}
if (coex_stat->bt_disabled != bt_disabled) {
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], BT state changed (%d) -> (%d)\n",
coex_stat->bt_disabled, bt_disabled);
coex_stat->bt_disabled = bt_disabled;
coex_stat->bt_ble_scan_type = 0;
coex_dm->cur_bt_lna_lvl = 0;
if (!coex_stat->bt_disabled) {
coex_stat->bt_reenable = true;
ieee80211_queue_delayed_work(rtwdev->hw,
&coex->bt_reenable_work,
15 * HZ);
} else {
coex_stat->bt_mailbox_reply = false;
coex_stat->bt_reenable = false;
}
}
}
static void rtw_coex_update_wl_link_info(struct rtw_dev *rtwdev, u8 reason)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_coex_dm *coex_dm = &coex->dm;
struct rtw_traffic_stats *stats = &rtwdev->stats;
bool is_5G = false;
bool wl_busy = false;
bool scan = false, link = false;
int i;
u8 rssi_state;
u8 rssi_step;
u8 rssi;
scan = test_bit(RTW_FLAG_SCANNING, rtwdev->flags);
coex_stat->wl_connected = !!rtwdev->sta_cnt;
wl_busy = test_bit(RTW_FLAG_BUSY_TRAFFIC, rtwdev->flags);
if (wl_busy != coex_stat->wl_gl_busy) {
if (wl_busy)
coex_stat->wl_gl_busy = true;
else
ieee80211_queue_delayed_work(rtwdev->hw,
&coex->wl_remain_work,
12 * HZ);
}
if (stats->tx_throughput > stats->rx_throughput)
coex_stat->wl_tput_dir = COEX_WL_TPUT_TX;
else
coex_stat->wl_tput_dir = COEX_WL_TPUT_RX;
if (scan || link || reason == COEX_RSN_2GCONSTART ||
reason == COEX_RSN_2GSCANSTART || reason == COEX_RSN_2GSWITCHBAND)
coex_stat->wl_linkscan_proc = true;
else
coex_stat->wl_linkscan_proc = false;
rtw_coex_wl_noisy_detect(rtwdev);
for (i = 0; i < 4; i++) {
rssi_state = coex_dm->wl_rssi_state[i];
rssi_step = chip->wl_rssi_step[i];
rssi = rtwdev->dm_info.min_rssi;
rssi_state = rtw_coex_next_rssi_state(rtwdev, rssi_state,
rssi, rssi_step);
coex_dm->wl_rssi_state[i] = rssi_state;
}
if (coex_stat->wl_linkscan_proc || coex_stat->wl_hi_pri_task1 ||
coex_stat->wl_hi_pri_task2 || coex_stat->wl_gl_busy)
rtw_coex_write_scbd(rtwdev, COEX_SCBD_SCAN, true);
else
rtw_coex_write_scbd(rtwdev, COEX_SCBD_SCAN, false);
switch (reason) {
case COEX_RSN_5GSCANSTART:
case COEX_RSN_5GSWITCHBAND:
case COEX_RSN_5GCONSTART:
is_5G = true;
break;
case COEX_RSN_2GSCANSTART:
case COEX_RSN_2GSWITCHBAND:
case COEX_RSN_2GCONSTART:
is_5G = false;
break;
default:
if (rtwdev->hal.current_band_type == RTW_BAND_5G)
is_5G = true;
else
is_5G = false;
break;
}
coex->under_5g = is_5G;
}
static inline u8 *get_payload_from_coex_resp(struct sk_buff *resp)
{
struct rtw_c2h_cmd *c2h;
u32 pkt_offset;
pkt_offset = *((u32 *)resp->cb);
c2h = (struct rtw_c2h_cmd *)(resp->data + pkt_offset);
return c2h->payload;
}
void rtw_coex_info_response(struct rtw_dev *rtwdev, struct sk_buff *skb)
{
struct rtw_coex *coex = &rtwdev->coex;
u8 *payload = get_payload_from_coex_resp(skb);
if (payload[0] != COEX_RESP_ACK_BY_WL_FW) {
dev_kfree_skb_any(skb);
return;
}
skb_queue_tail(&coex->queue, skb);
wake_up(&coex->wait);
}
static struct sk_buff *rtw_coex_info_request(struct rtw_dev *rtwdev,
struct rtw_coex_info_req *req)
{
struct rtw_coex *coex = &rtwdev->coex;
struct sk_buff *skb_resp = NULL;
lockdep_assert_held(&rtwdev->mutex);
rtw_fw_query_bt_mp_info(rtwdev, req);
if (!wait_event_timeout(coex->wait, !skb_queue_empty(&coex->queue),
COEX_REQUEST_TIMEOUT)) {
rtw_err(rtwdev, "coex request time out\n");
goto out;
}
skb_resp = skb_dequeue(&coex->queue);
if (!skb_resp) {
rtw_err(rtwdev, "failed to get coex info response\n");
goto out;
}
out:
return skb_resp;
}
static bool rtw_coex_get_bt_scan_type(struct rtw_dev *rtwdev, u8 *scan_type)
{
struct rtw_coex_info_req req = {0};
struct sk_buff *skb;
u8 *payload;
req.op_code = BT_MP_INFO_OP_SCAN_TYPE;
skb = rtw_coex_info_request(rtwdev, &req);
if (!skb)
return false;
payload = get_payload_from_coex_resp(skb);
*scan_type = GET_COEX_RESP_BT_SCAN_TYPE(payload);
dev_kfree_skb_any(skb);
return true;
}
static bool rtw_coex_set_lna_constrain_level(struct rtw_dev *rtwdev,
u8 lna_constrain_level)
{
struct rtw_coex_info_req req = {0};
struct sk_buff *skb;
req.op_code = BT_MP_INFO_OP_LNA_CONSTRAINT;
req.para1 = lna_constrain_level;
skb = rtw_coex_info_request(rtwdev, &req);
if (!skb)
return false;
dev_kfree_skb_any(skb);
return true;
}
#define case_BTSTATUS(src) \
case COEX_BTSTATUS_##src: return #src
static const char *rtw_coex_get_bt_status_string(u8 bt_status)
{
switch (bt_status) {
case_BTSTATUS(NCON_IDLE);
case_BTSTATUS(CON_IDLE);
case_BTSTATUS(INQ_PAGE);
case_BTSTATUS(ACL_BUSY);
case_BTSTATUS(SCO_BUSY);
case_BTSTATUS(ACL_SCO_BUSY);
default:
return "Unknown";
}
}
static void rtw_coex_update_bt_link_info(struct rtw_dev *rtwdev)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
struct rtw_coex_dm *coex_dm = &coex->dm;
u8 i;
u8 rssi_state;
u8 rssi_step;
u8 rssi;
/* update wl/bt rssi by btinfo */
for (i = 0; i < COEX_RSSI_STEP; i++) {
rssi_state = coex_dm->bt_rssi_state[i];
rssi_step = chip->bt_rssi_step[i];
rssi = coex_stat->bt_rssi;
rssi_state = rtw_coex_next_rssi_state(rtwdev, rssi_state, rssi,
rssi_step);
coex_dm->bt_rssi_state[i] = rssi_state;
}
if (coex_stat->bt_ble_scan_en &&
coex_stat->cnt_bt[COEX_CNT_BT_INFOUPDATE] % 3 == 0) {
u8 scan_type;
if (rtw_coex_get_bt_scan_type(rtwdev, &scan_type)) {
coex_stat->bt_ble_scan_type = scan_type;
if ((coex_stat->bt_ble_scan_type & 0x1) == 0x1)
coex_stat->bt_init_scan = true;
else
coex_stat->bt_init_scan = false;
}
}
coex_stat->bt_profile_num = 0;
/* set link exist status */
if (!(coex_stat->bt_info_lb2 & COEX_INFO_CONNECTION)) {
coex_stat->bt_link_exist = false;
coex_stat->bt_pan_exist = false;
coex_stat->bt_a2dp_exist = false;
coex_stat->bt_hid_exist = false;
coex_stat->bt_hfp_exist = false;
} else {
/* connection exists */
coex_stat->bt_link_exist = true;
if (coex_stat->bt_info_lb2 & COEX_INFO_FTP) {
coex_stat->bt_pan_exist = true;
coex_stat->bt_profile_num++;
} else {
coex_stat->bt_pan_exist = false;
}
if (coex_stat->bt_info_lb2 & COEX_INFO_A2DP) {
coex_stat->bt_a2dp_exist = true;
coex_stat->bt_profile_num++;
} else {
coex_stat->bt_a2dp_exist = false;
}
if (coex_stat->bt_info_lb2 & COEX_INFO_HID) {
coex_stat->bt_hid_exist = true;
coex_stat->bt_profile_num++;
} else {
coex_stat->bt_hid_exist = false;
}
if (coex_stat->bt_info_lb2 & COEX_INFO_SCO_ESCO) {
coex_stat->bt_hfp_exist = true;
coex_stat->bt_profile_num++;
} else {
coex_stat->bt_hfp_exist = false;
}
}
if (coex_stat->bt_info_lb2 & COEX_INFO_INQ_PAGE) {
coex_dm->bt_status = COEX_BTSTATUS_INQ_PAGE;
} else if (!(coex_stat->bt_info_lb2 & COEX_INFO_CONNECTION)) {
coex_dm->bt_status = COEX_BTSTATUS_NCON_IDLE;
coex_stat->bt_multi_link_remain = false;
} else if (coex_stat->bt_info_lb2 == COEX_INFO_CONNECTION) {
coex_dm->bt_status = COEX_BTSTATUS_CON_IDLE;
} else if ((coex_stat->bt_info_lb2 & COEX_INFO_SCO_ESCO) ||
(coex_stat->bt_info_lb2 & COEX_INFO_SCO_BUSY)) {
if (coex_stat->bt_info_lb2 & COEX_INFO_ACL_BUSY)
coex_dm->bt_status = COEX_BTSTATUS_ACL_SCO_BUSY;
else
coex_dm->bt_status = COEX_BTSTATUS_SCO_BUSY;
} else if (coex_stat->bt_info_lb2 & COEX_INFO_ACL_BUSY) {
coex_dm->bt_status = COEX_BTSTATUS_ACL_BUSY;
} else {
coex_dm->bt_status = COEX_BTSTATUS_MAX;
}
coex_stat->cnt_bt[COEX_CNT_BT_INFOUPDATE]++;
rtw_dbg(rtwdev, RTW_DBG_COEX, "[BTCoex], %s(), %s!!!\n", __func__,
rtw_coex_get_bt_status_string(coex_dm->bt_status));
}
static void rtw_coex_update_wl_ch_info(struct rtw_dev *rtwdev, u8 type)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_efuse *efuse = &rtwdev->efuse;
struct rtw_coex_dm *coex_dm = &rtwdev->coex.dm;
struct rtw_coex_stat *coex_stat = &rtwdev->coex.stat;
u8 link = 0;
u8 center_chan = 0;
u8 bw;
int i;
bw = rtwdev->hal.current_band_width;
if (type != COEX_MEDIA_DISCONNECT)
center_chan = rtwdev->hal.current_channel;
if (center_chan == 0 ||
(efuse->share_ant && center_chan <= 14 &&
coex_stat->wl_coex_mode != COEX_WLINK_2GFREE)) {
link = 0;
center_chan = 0;
bw = 0;
} else if (center_chan <= 14) {
link = 0x1;
if (bw == RTW_CHANNEL_WIDTH_40)
bw = chip->bt_afh_span_bw40;
else
bw = chip->bt_afh_span_bw20;
} else if (chip->afh_5g_num > 1) {
for (i = 0; i < chip->afh_5g_num; i++) {
if (center_chan == chip->afh_5g[i].wl_5g_ch) {
link = 0x3;
center_chan = chip->afh_5g[i].bt_skip_ch;
bw = chip->afh_5g[i].bt_skip_span;
break;
}
}
}
coex_dm->wl_ch_info[0] = link;
coex_dm->wl_ch_info[1] = center_chan;
coex_dm->wl_ch_info[2] = bw;
rtw_fw_wl_ch_info(rtwdev, link, center_chan, bw);
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], %s: para[0:2] = 0x%x 0x%x 0x%x\n", __func__, link,
center_chan, bw);
}
static void rtw_coex_set_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_dm *coex_dm = &coex->dm;
if (bt_pwr_dec_lvl == coex_dm->cur_bt_pwr_lvl)
return;
coex_dm->cur_bt_pwr_lvl = bt_pwr_dec_lvl;
rtw_fw_force_bt_tx_power(rtwdev, bt_pwr_dec_lvl);
}
static void rtw_coex_set_bt_rx_gain(struct rtw_dev *rtwdev, u8 bt_lna_lvl)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_dm *coex_dm = &coex->dm;
if (bt_lna_lvl == coex_dm->cur_bt_lna_lvl)
return;
coex_dm->cur_bt_lna_lvl = bt_lna_lvl;
/* notify BT rx gain table changed */
if (bt_lna_lvl < 7) {
rtw_coex_set_lna_constrain_level(rtwdev, bt_lna_lvl);
rtw_coex_write_scbd(rtwdev, COEX_SCBD_RXGAIN, true);
} else {
rtw_coex_write_scbd(rtwdev, COEX_SCBD_RXGAIN, false);
}
rtw_dbg(rtwdev, RTW_DBG_COEX, "[BTCoex], %s(): bt_rx_LNA_level = %d\n",
__func__, bt_lna_lvl);
}
static void rtw_coex_set_rf_para(struct rtw_dev *rtwdev,
struct coex_rf_para para)
{
struct rtw_coex *coex = &rtwdev->coex;
struct rtw_coex_stat *coex_stat = &coex->stat;
u8 offset = 0;
if (coex->freerun && coex_stat->cnt_wl[COEX_CNT_WL_SCANAP] <= 5)
offset = 3;
rtw_coex_set_wl_tx_power(rtwdev, para.wl_pwr_dec_lvl);
rtw_coex_set_bt_tx_power(rtwdev, para.bt_pwr_dec_lvl + offset);
rtw_coex_set_wl_rx_gain(rtwdev, para.wl_low_gain_en);
rtw_coex_set_bt_rx_gain(rtwdev, para.bt_lna_lvl);
}
u32 rtw_coex_read_indirect_reg(struct rtw_dev *rtwdev, u16 addr)
{
u32 val;
if (!ltecoex_read_reg(rtwdev, addr, &val)) {
rtw_err(rtwdev, "failed to read indirect register\n");
return 0;
}
return val;
}
EXPORT_SYMBOL(rtw_coex_read_indirect_reg);
void rtw_coex_write_indirect_reg(struct rtw_dev *rtwdev, u16 addr,
u32 mask, u32 val)
{
u32 shift = __ffs(mask);
u32 tmp;
tmp = rtw_coex_read_indirect_reg(rtwdev, addr);
tmp = (tmp & (~mask)) | ((val << shift) & mask);
if (!ltecoex_reg_write(rtwdev, addr, tmp))
rtw_err(rtwdev, "failed to write indirect register\n");
}
EXPORT_SYMBOL(rtw_coex_write_indirect_reg);
static void rtw_coex_coex_ctrl_owner(struct rtw_dev *rtwdev, bool wifi_control)
{
const struct rtw_chip_info *chip = rtwdev->chip;
const struct rtw_hw_reg *btg_reg = chip->btg_reg;
if (wifi_control) {
rtw_write8_set(rtwdev, REG_SYS_SDIO_CTRL + 3,
BIT_LTE_MUX_CTRL_PATH >> 24);
if (btg_reg)
rtw_write8_set(rtwdev, btg_reg->addr, btg_reg->mask);
} else {
rtw_write8_clr(rtwdev, REG_SYS_SDIO_CTRL + 3,
BIT_LTE_MUX_CTRL_PATH >> 24);
if (btg_reg)
rtw_write8_clr(rtwdev, btg_reg->addr, btg_reg->mask);
}
}
static void rtw_coex_set_gnt_bt(struct rtw_dev *rtwdev, u8 state)
{
rtw_coex_write_indirect_reg(rtwdev, LTE_COEX_CTRL, 0xc000, state);
rtw_coex_write_indirect_reg(rtwdev, LTE_COEX_CTRL, 0x0c00, state);
}
static void rtw_coex_set_gnt_wl(struct rtw_dev *rtwdev, u8 state)
{
rtw_coex_write_indirect_reg(rtwdev, LTE_COEX_CTRL, 0x3000, state);
rtw_coex_write_indirect_reg(rtwdev, LTE_COEX_CTRL, 0x0300, state);
}
static void rtw_coex_mimo_ps(struct rtw_dev *rtwdev, bool force, bool state)
{
struct rtw_coex_stat *coex_stat = &rtwdev->coex.stat;
if (!force && state == coex_stat->wl_mimo_ps)
return;
coex_stat->wl_mimo_ps = state;
rtw_set_txrx_1ss(rtwdev, state);
rtw_coex_update_wl_ch_info(rtwdev, (u8)coex_stat->wl_connected);
rtw_dbg(rtwdev, RTW_DBG_COEX,
"[BTCoex], %s(): state = %d\n", __func__, state);
}
static void rtw_btc_wltoggle_table_a(struct rtw_dev *rtwdev, bool force,
u8 table_case)
{
const struct rtw_chip_info *chip = rtwdev->chip;
struct rtw_efuse *efuse = &rtwdev->efuse;
u8 h2c_para[6] = {0};
u32 table_wl = 0x5a5a5a5a;
h2c_para[0] = COEX_H2C69_TOGGLE_TABLE_A;
/* no definition */
h2c_para[1] = 0x1;
if (efuse->share_ant) {
if (table_case < chip->table_sant_num)
table_wl = chip->table_sant[table_case].wl;
} else {
if (table_case < chip->table_nsant_num)
table_wl = chip->table_nsant[table_case].wl;
}
/* tell WL FW WL slot toggle table-A*/