forked from lwfinger/rtw89
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pci.c
3043 lines (2506 loc) · 78.4 KB
/
pci.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 Realtek Corporation
*/
#include <linux/pci.h>
#include "mac.h"
#include "pci.h"
#include "reg.h"
#include "ser.h"
static bool rtw89_pci_disable_clkreq;
static bool rtw89_pci_disable_aspm_l1;
static bool rtw89_pci_disable_l1ss;
module_param_named(disable_clkreq, rtw89_pci_disable_clkreq, bool, 0644);
module_param_named(disable_aspm_l1, rtw89_pci_disable_aspm_l1, bool, 0644);
module_param_named(disable_aspm_l1ss, rtw89_pci_disable_l1ss, bool, 0644);
MODULE_PARM_DESC(disable_clkreq, "Set Y to disable PCI clkreq support");
MODULE_PARM_DESC(disable_aspm_l1, "Set Y to disable PCI ASPM L1 support");
MODULE_PARM_DESC(disable_aspm_l1ss, "Set Y to disable PCI L1SS support");
static int rtw89_pci_rst_bdram_pcie(struct rtw89_dev *rtwdev)
{
u32 val;
int ret;
rtw89_write32(rtwdev, R_AX_PCIE_INIT_CFG1,
rtw89_read32(rtwdev, R_AX_PCIE_INIT_CFG1) | B_AX_RST_BDRAM);
ret = read_poll_timeout_atomic(rtw89_read32, val, !(val & B_AX_RST_BDRAM),
1, RTW89_PCI_POLL_BDRAM_RST_CNT, false,
rtwdev, R_AX_PCIE_INIT_CFG1);
if (ret)
return -EBUSY;
return 0;
}
static u32 rtw89_pci_dma_recalc(struct rtw89_dev *rtwdev,
struct rtw89_pci_dma_ring *bd_ring,
u32 cur_idx, bool tx)
{
u32 cnt, cur_rp, wp, rp, len;
rp = bd_ring->rp;
wp = bd_ring->wp;
len = bd_ring->len;
cur_rp = FIELD_GET(TXBD_HW_IDX_MASK, cur_idx);
if (tx)
cnt = cur_rp >= rp ? cur_rp - rp : len - (rp - cur_rp);
else
cnt = cur_rp >= wp ? cur_rp - wp : len - (wp - cur_rp);
bd_ring->rp = cur_rp;
return cnt;
}
static u32 rtw89_pci_txbd_recalc(struct rtw89_dev *rtwdev,
struct rtw89_pci_tx_ring *tx_ring)
{
struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
u32 addr_idx = bd_ring->addr.idx;
u32 cnt, idx;
idx = rtw89_read32(rtwdev, addr_idx);
cnt = rtw89_pci_dma_recalc(rtwdev, bd_ring, idx, true);
return cnt;
}
static void rtw89_pci_release_fwcmd(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci,
u32 cnt, bool release_all)
{
struct rtw89_pci_tx_data *tx_data;
struct sk_buff *skb;
u32 qlen;
while (cnt--) {
skb = skb_dequeue(&rtwpci->h2c_queue);
if (!skb) {
rtw89_err(rtwdev, "failed to pre-release fwcmd\n");
return;
}
skb_queue_tail(&rtwpci->h2c_release_queue, skb);
}
qlen = skb_queue_len(&rtwpci->h2c_release_queue);
if (!release_all)
qlen = qlen > RTW89_PCI_MULTITAG ? qlen - RTW89_PCI_MULTITAG : 0;
while (qlen--) {
skb = skb_dequeue(&rtwpci->h2c_release_queue);
if (!skb) {
rtw89_err(rtwdev, "failed to release fwcmd\n");
return;
}
tx_data = RTW89_PCI_TX_SKB_CB(skb);
dma_unmap_single(&rtwpci->pdev->dev, tx_data->dma, skb->len,
DMA_TO_DEVICE);
dev_kfree_skb_any(skb);
}
}
static void rtw89_pci_reclaim_tx_fwcmd(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci)
{
struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[RTW89_TXCH_CH12];
u32 cnt;
cnt = rtw89_pci_txbd_recalc(rtwdev, tx_ring);
if (!cnt)
return;
rtw89_pci_release_fwcmd(rtwdev, rtwpci, cnt, false);
}
static u32 rtw89_pci_rxbd_recalc(struct rtw89_dev *rtwdev,
struct rtw89_pci_rx_ring *rx_ring)
{
struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
u32 addr_idx = bd_ring->addr.idx;
u32 cnt, idx;
idx = rtw89_read32(rtwdev, addr_idx);
cnt = rtw89_pci_dma_recalc(rtwdev, bd_ring, idx, false);
return cnt;
}
static void rtw89_pci_sync_skb_for_cpu(struct rtw89_dev *rtwdev,
struct sk_buff *skb)
{
struct rtw89_pci_rx_info *rx_info;
dma_addr_t dma;
rx_info = RTW89_PCI_RX_SKB_CB(skb);
dma = rx_info->dma;
dma_sync_single_for_cpu(rtwdev->dev, dma, RTW89_PCI_RX_BUF_SIZE,
DMA_FROM_DEVICE);
}
static void rtw89_pci_sync_skb_for_device(struct rtw89_dev *rtwdev,
struct sk_buff *skb)
{
struct rtw89_pci_rx_info *rx_info;
dma_addr_t dma;
rx_info = RTW89_PCI_RX_SKB_CB(skb);
dma = rx_info->dma;
dma_sync_single_for_device(rtwdev->dev, dma, RTW89_PCI_RX_BUF_SIZE,
DMA_FROM_DEVICE);
}
static int rtw89_pci_rxbd_info_update(struct rtw89_dev *rtwdev,
struct sk_buff *skb)
{
struct rtw89_pci_rxbd_info *rxbd_info;
struct rtw89_pci_rx_info *rx_info = RTW89_PCI_RX_SKB_CB(skb);
rxbd_info = (struct rtw89_pci_rxbd_info *)skb->data;
rx_info->fs = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_FS);
rx_info->ls = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_LS);
rx_info->len = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_WRITE_SIZE);
rx_info->tag = le32_get_bits(rxbd_info->dword, RTW89_PCI_RXBD_TAG);
return 0;
}
static bool
rtw89_skb_put_rx_data(struct rtw89_dev *rtwdev, bool fs, bool ls,
struct sk_buff *new,
const struct sk_buff *skb, u32 offset,
const struct rtw89_pci_rx_info *rx_info,
const struct rtw89_rx_desc_info *desc_info)
{
u32 copy_len = rx_info->len - offset;
if (unlikely(skb_tailroom(new) < copy_len)) {
rtw89_debug(rtwdev, RTW89_DBG_TXRX,
"invalid rx data length bd_len=%d desc_len=%d offset=%d (fs=%d ls=%d)\n",
rx_info->len, desc_info->pkt_size, offset, fs, ls);
rtw89_hex_dump(rtwdev, RTW89_DBG_TXRX, "rx_data: ",
skb->data, rx_info->len);
/* length of a single segment skb is desc_info->pkt_size */
if (fs && ls) {
copy_len = desc_info->pkt_size;
} else {
rtw89_info(rtwdev, "drop rx data due to invalid length\n");
return false;
}
}
skb_put_data(new, skb->data + offset, copy_len);
return true;
}
static u32 rtw89_pci_rxbd_deliver_skbs(struct rtw89_dev *rtwdev,
struct rtw89_pci_rx_ring *rx_ring)
{
struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
struct rtw89_pci_rx_info *rx_info;
struct rtw89_rx_desc_info *desc_info = &rx_ring->diliver_desc;
struct sk_buff *new = rx_ring->diliver_skb;
struct sk_buff *skb;
u32 rxinfo_size = sizeof(struct rtw89_pci_rxbd_info);
u32 offset;
u32 cnt = 1;
bool fs, ls;
int ret;
skb = rx_ring->buf[bd_ring->wp];
rtw89_pci_sync_skb_for_cpu(rtwdev, skb);
ret = rtw89_pci_rxbd_info_update(rtwdev, skb);
if (ret) {
rtw89_err(rtwdev, "failed to update %d RXBD info: %d\n",
bd_ring->wp, ret);
goto err_sync_device;
}
rx_info = RTW89_PCI_RX_SKB_CB(skb);
fs = rx_info->fs;
ls = rx_info->ls;
if (fs) {
if (new) {
rtw89_err(rtwdev, "skb should not be ready before first segment start\n");
goto err_sync_device;
}
if (desc_info->ready) {
rtw89_warn(rtwdev, "desc info should not be ready before first segment start\n");
goto err_sync_device;
}
rtw89_core_query_rxdesc(rtwdev, desc_info, skb->data, rxinfo_size);
new = dev_alloc_skb(desc_info->pkt_size);
if (!new)
goto err_sync_device;
rx_ring->diliver_skb = new;
/* first segment has RX desc */
offset = desc_info->offset;
offset += desc_info->long_rxdesc ? sizeof(struct rtw89_rxdesc_long) :
sizeof(struct rtw89_rxdesc_short);
} else {
offset = sizeof(struct rtw89_pci_rxbd_info);
if (!new) {
rtw89_warn(rtwdev, "no last skb\n");
goto err_sync_device;
}
}
if (!rtw89_skb_put_rx_data(rtwdev, fs, ls, new, skb, offset, rx_info, desc_info))
goto err_sync_device;
rtw89_pci_sync_skb_for_device(rtwdev, skb);
rtw89_pci_rxbd_increase(rx_ring, 1);
if (!desc_info->ready) {
rtw89_warn(rtwdev, "no rx desc information\n");
goto err_free_resource;
}
if (ls) {
rtw89_core_rx(rtwdev, desc_info, new);
rx_ring->diliver_skb = NULL;
desc_info->ready = false;
}
return cnt;
err_sync_device:
rtw89_pci_sync_skb_for_device(rtwdev, skb);
rtw89_pci_rxbd_increase(rx_ring, 1);
err_free_resource:
if (new)
dev_kfree_skb_any(new);
rx_ring->diliver_skb = NULL;
desc_info->ready = false;
return cnt;
}
static void rtw89_pci_rxbd_deliver(struct rtw89_dev *rtwdev,
struct rtw89_pci_rx_ring *rx_ring,
u32 cnt)
{
struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
u32 rx_cnt;
while (cnt && rtwdev->napi_budget_countdown > 0) {
rx_cnt = rtw89_pci_rxbd_deliver_skbs(rtwdev, rx_ring);
if (!rx_cnt) {
rtw89_err(rtwdev, "failed to deliver RXBD skb\n");
/* skip the rest RXBD bufs */
rtw89_pci_rxbd_increase(rx_ring, cnt);
break;
}
cnt -= rx_cnt;
}
rtw89_write16(rtwdev, bd_ring->addr.idx, bd_ring->wp);
}
static int rtw89_pci_poll_rxq_dma(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci, int budget)
{
struct rtw89_pci_rx_ring *rx_ring;
int countdown = rtwdev->napi_budget_countdown;
u32 cnt;
rx_ring = &rtwpci->rx_rings[RTW89_RXCH_RXQ];
cnt = rtw89_pci_rxbd_recalc(rtwdev, rx_ring);
if (!cnt)
return 0;
cnt = min_t(u32, budget, cnt);
rtw89_pci_rxbd_deliver(rtwdev, rx_ring, cnt);
/* In case of flushing pending SKBs, the countdown may exceed. */
if (rtwdev->napi_budget_countdown <= 0)
return budget;
return budget - countdown;
}
static void rtw89_pci_tx_status(struct rtw89_dev *rtwdev,
struct rtw89_pci_tx_ring *tx_ring,
struct sk_buff *skb, u8 tx_status)
{
struct ieee80211_tx_info *info;
info = IEEE80211_SKB_CB(skb);
ieee80211_tx_info_clear_status(info);
if (info->flags & IEEE80211_TX_CTL_NO_ACK)
info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
if (tx_status == RTW89_TX_DONE) {
info->flags |= IEEE80211_TX_STAT_ACK;
tx_ring->tx_acked++;
} else {
if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
rtw89_debug(rtwdev, RTW89_DBG_FW,
"failed to TX of status %x\n", tx_status);
switch (tx_status) {
case RTW89_TX_RETRY_LIMIT:
tx_ring->tx_retry_lmt++;
break;
case RTW89_TX_LIFE_TIME:
tx_ring->tx_life_time++;
break;
case RTW89_TX_MACID_DROP:
tx_ring->tx_mac_id_drop++;
break;
default:
rtw89_warn(rtwdev, "invalid TX status %x\n", tx_status);
break;
}
}
ieee80211_tx_status_ni(rtwdev->hw, skb);
}
static void rtw89_pci_reclaim_txbd(struct rtw89_dev *rtwdev, struct rtw89_pci_tx_ring *tx_ring)
{
struct rtw89_pci_tx_wd *txwd;
u32 cnt;
cnt = rtw89_pci_txbd_recalc(rtwdev, tx_ring);
while (cnt--) {
txwd = list_first_entry_or_null(&tx_ring->busy_pages, struct rtw89_pci_tx_wd, list);
if (!txwd) {
rtw89_warn(rtwdev, "No busy txwd pages available\n");
break;
}
list_del_init(&txwd->list);
}
}
static void rtw89_pci_release_busy_txwd(struct rtw89_dev *rtwdev,
struct rtw89_pci_tx_ring *tx_ring)
{
struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
struct rtw89_pci_tx_wd *txwd;
int i;
for (i = 0; i < wd_ring->page_num; i++) {
txwd = list_first_entry_or_null(&tx_ring->busy_pages, struct rtw89_pci_tx_wd, list);
if (!txwd)
break;
list_del_init(&txwd->list);
}
}
static void rtw89_pci_release_txwd_skb(struct rtw89_dev *rtwdev,
struct rtw89_pci_tx_ring *tx_ring,
struct rtw89_pci_tx_wd *txwd, u16 seq,
u8 tx_status)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_tx_data *tx_data;
struct sk_buff *skb, *tmp;
u8 txch = tx_ring->txch;
if (!list_empty(&txwd->list)) {
rtw89_warn(rtwdev, "queue %d txwd %d is not idle\n",
txch, seq);
return;
}
/* currently, support for only one frame */
if (skb_queue_len(&txwd->queue) != 1) {
rtw89_warn(rtwdev, "empty pending queue %d page %d\n",
txch, seq);
return;
}
skb_queue_walk_safe(&txwd->queue, skb, tmp) {
skb_unlink(skb, &txwd->queue);
tx_data = RTW89_PCI_TX_SKB_CB(skb);
dma_unmap_single(&rtwpci->pdev->dev, tx_data->dma, skb->len,
DMA_TO_DEVICE);
rtw89_pci_tx_status(rtwdev, tx_ring, skb, tx_status);
}
rtw89_pci_enqueue_txwd(tx_ring, txwd);
}
static void rtw89_pci_release_rpp(struct rtw89_dev *rtwdev,
struct rtw89_pci_rpp_fmt *rpp)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_tx_ring *tx_ring;
struct rtw89_pci_tx_wd_ring *wd_ring;
struct rtw89_pci_tx_wd *txwd;
u16 seq;
u8 qsel, tx_status, txch;
seq = le32_get_bits(rpp->dword, RTW89_PCI_RPP_SEQ);
qsel = le32_get_bits(rpp->dword, RTW89_PCI_RPP_QSEL);
tx_status = le32_get_bits(rpp->dword, RTW89_PCI_RPP_TX_STATUS);
txch = rtw89_core_get_ch_dma(rtwdev, qsel);
if (txch == RTW89_TXCH_CH12) {
rtw89_warn(rtwdev, "should no fwcmd release report\n");
return;
}
tx_ring = &rtwpci->tx_rings[txch];
rtw89_pci_reclaim_txbd(rtwdev, tx_ring);
wd_ring = &tx_ring->wd_ring;
txwd = &wd_ring->pages[seq];
rtw89_pci_release_txwd_skb(rtwdev, tx_ring, txwd, seq, tx_status);
}
static void rtw89_pci_release_pending_txwd_skb(struct rtw89_dev *rtwdev,
struct rtw89_pci_tx_ring *tx_ring)
{
struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
struct rtw89_pci_tx_wd *txwd;
int i;
for (i = 0; i < wd_ring->page_num; i++) {
txwd = &wd_ring->pages[i];
if (!list_empty(&txwd->list))
continue;
rtw89_pci_release_txwd_skb(rtwdev, tx_ring, txwd, i, RTW89_TX_MACID_DROP);
}
}
static u32 rtw89_pci_release_tx_skbs(struct rtw89_dev *rtwdev,
struct rtw89_pci_rx_ring *rx_ring,
u32 max_cnt)
{
struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
struct rtw89_pci_rx_info *rx_info;
struct rtw89_pci_rpp_fmt *rpp;
struct rtw89_rx_desc_info desc_info = {};
struct sk_buff *skb;
u32 cnt = 0;
u32 rpp_size = sizeof(struct rtw89_pci_rpp_fmt);
u32 rxinfo_size = sizeof(struct rtw89_pci_rxbd_info);
u32 offset;
int ret;
skb = rx_ring->buf[bd_ring->wp];
rtw89_pci_sync_skb_for_cpu(rtwdev, skb);
ret = rtw89_pci_rxbd_info_update(rtwdev, skb);
if (ret) {
rtw89_err(rtwdev, "failed to update %d RXBD info: %d\n",
bd_ring->wp, ret);
goto err_sync_device;
}
rx_info = RTW89_PCI_RX_SKB_CB(skb);
if (!rx_info->fs || !rx_info->ls) {
rtw89_err(rtwdev, "cannot process RP frame not set FS/LS\n");
return cnt;
}
rtw89_core_query_rxdesc(rtwdev, &desc_info, skb->data, rxinfo_size);
/* first segment has RX desc */
offset = desc_info.offset;
offset += desc_info.long_rxdesc ? sizeof(struct rtw89_rxdesc_long) :
sizeof(struct rtw89_rxdesc_short);
for (; offset + rpp_size <= rx_info->len; offset += rpp_size) {
rpp = (struct rtw89_pci_rpp_fmt *)(skb->data + offset);
rtw89_pci_release_rpp(rtwdev, rpp);
}
rtw89_pci_sync_skb_for_device(rtwdev, skb);
rtw89_pci_rxbd_increase(rx_ring, 1);
cnt++;
return cnt;
err_sync_device:
rtw89_pci_sync_skb_for_device(rtwdev, skb);
return 0;
}
static void rtw89_pci_release_tx(struct rtw89_dev *rtwdev,
struct rtw89_pci_rx_ring *rx_ring,
u32 cnt)
{
struct rtw89_pci_dma_ring *bd_ring = &rx_ring->bd_ring;
u32 release_cnt;
while (cnt) {
release_cnt = rtw89_pci_release_tx_skbs(rtwdev, rx_ring, cnt);
if (!release_cnt) {
rtw89_err(rtwdev, "failed to release TX skbs\n");
/* skip the rest RXBD bufs */
rtw89_pci_rxbd_increase(rx_ring, cnt);
break;
}
cnt -= release_cnt;
}
rtw89_write16(rtwdev, bd_ring->addr.idx, bd_ring->wp);
}
static int rtw89_pci_poll_rpq_dma(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci, int budget)
{
struct rtw89_pci_rx_ring *rx_ring;
u32 cnt;
int work_done;
rx_ring = &rtwpci->rx_rings[RTW89_RXCH_RPQ];
spin_lock_bh(&rtwpci->trx_lock);
cnt = rtw89_pci_rxbd_recalc(rtwdev, rx_ring);
if (cnt == 0)
goto out_unlock;
rtw89_pci_release_tx(rtwdev, rx_ring, cnt);
out_unlock:
spin_unlock_bh(&rtwpci->trx_lock);
/* always release all RPQ */
work_done = min_t(int, cnt, budget);
rtwdev->napi_budget_countdown -= work_done;
return work_done;
}
static void rtw89_pci_isr_rxd_unavail(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci)
{
struct rtw89_pci_rx_ring *rx_ring;
struct rtw89_pci_dma_ring *bd_ring;
u32 reg_idx;
u16 hw_idx, hw_idx_next, host_idx;
int i;
for (i = 0; i < RTW89_RXCH_NUM; i++) {
rx_ring = &rtwpci->rx_rings[i];
bd_ring = &rx_ring->bd_ring;
reg_idx = rtw89_read32(rtwdev, bd_ring->addr.idx);
hw_idx = FIELD_GET(TXBD_HW_IDX_MASK, reg_idx);
host_idx = FIELD_GET(TXBD_HOST_IDX_MASK, reg_idx);
hw_idx_next = (hw_idx + 1) % bd_ring->len;
if (hw_idx_next == host_idx)
rtw89_warn(rtwdev, "%d RXD unavailable\n", i);
rtw89_debug(rtwdev, RTW89_DBG_TXRX,
"%d RXD unavailable, idx=0x%08x, len=%d\n",
i, reg_idx, bd_ring->len);
}
}
static void rtw89_pci_recognize_intrs(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci,
struct rtw89_pci_isrs *isrs)
{
isrs->halt_c2h_isrs = rtw89_read32(rtwdev, R_AX_HISR0) & rtwpci->halt_c2h_intrs;
isrs->isrs[0] = rtw89_read32(rtwdev, R_AX_PCIE_HISR00) & rtwpci->intrs[0];
isrs->isrs[1] = rtw89_read32(rtwdev, R_AX_PCIE_HISR10) & rtwpci->intrs[1];
rtw89_write32(rtwdev, R_AX_HISR0, isrs->halt_c2h_isrs);
rtw89_write32(rtwdev, R_AX_PCIE_HISR00, isrs->isrs[0]);
rtw89_write32(rtwdev, R_AX_PCIE_HISR10, isrs->isrs[1]);
}
static void rtw89_pci_clear_isr0(struct rtw89_dev *rtwdev, u32 isr00)
{
/* write 1 clear */
rtw89_write32(rtwdev, R_AX_PCIE_HISR00, isr00);
}
static void rtw89_pci_enable_intr(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci)
{
rtw89_write32(rtwdev, R_AX_HIMR0, rtwpci->halt_c2h_intrs);
rtw89_write32(rtwdev, R_AX_PCIE_HIMR00, rtwpci->intrs[0]);
rtw89_write32(rtwdev, R_AX_PCIE_HIMR10, rtwpci->intrs[1]);
}
static void rtw89_pci_disable_intr(struct rtw89_dev *rtwdev,
struct rtw89_pci *rtwpci)
{
rtw89_write32(rtwdev, R_AX_HIMR0, 0);
rtw89_write32(rtwdev, R_AX_PCIE_HIMR00, 0);
rtw89_write32(rtwdev, R_AX_PCIE_HIMR10, 0);
}
static irqreturn_t rtw89_pci_interrupt_threadfn(int irq, void *dev)
{
struct rtw89_dev *rtwdev = dev;
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_isrs isrs;
unsigned long flags;
spin_lock_irqsave(&rtwpci->irq_lock, flags);
rtw89_pci_recognize_intrs(rtwdev, rtwpci, &isrs);
spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
if (unlikely(isrs.isrs[0] & B_AX_RDU_INT))
rtw89_pci_isr_rxd_unavail(rtwdev, rtwpci);
if (unlikely(isrs.halt_c2h_isrs & B_AX_HALT_C2H_INT_EN))
rtw89_ser_notify(rtwdev, rtw89_mac_get_err_status(rtwdev));
if (likely(rtwpci->running)) {
local_bh_disable();
napi_schedule(&rtwdev->napi);
local_bh_enable();
}
return IRQ_HANDLED;
}
static irqreturn_t rtw89_pci_interrupt_handler(int irq, void *dev)
{
struct rtw89_dev *rtwdev = dev;
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
unsigned long flags;
irqreturn_t irqret = IRQ_WAKE_THREAD;
spin_lock_irqsave(&rtwpci->irq_lock, flags);
/* If interrupt event is on the road, it is still trigger interrupt
* even we have done pci_stop() to turn off IMR.
*/
if (unlikely(!rtwpci->running)) {
irqret = IRQ_HANDLED;
goto exit;
}
rtw89_pci_disable_intr(rtwdev, rtwpci);
exit:
spin_unlock_irqrestore(&rtwpci->irq_lock, flags);
return irqret;
}
#define DEF_TXCHADDRS_TYPE1(info, txch, v...) \
[RTW89_TXCH_##txch] = { \
.num = R_AX_##txch##_TXBD_NUM ##v, \
.idx = R_AX_##txch##_TXBD_IDX ##v, \
.bdram = R_AX_##txch##_BDRAM_CTRL ##v, \
.desa_l = R_AX_##txch##_TXBD_DESA_L ##v, \
.desa_h = R_AX_##txch##_TXBD_DESA_H ##v, \
}
#define DEF_TXCHADDRS(info, txch, v...) \
[RTW89_TXCH_##txch] = { \
.num = R_AX_##txch##_TXBD_NUM, \
.idx = R_AX_##txch##_TXBD_IDX, \
.bdram = R_AX_##txch##_BDRAM_CTRL ##v, \
.desa_l = R_AX_##txch##_TXBD_DESA_L ##v, \
.desa_h = R_AX_##txch##_TXBD_DESA_H ##v, \
}
#define DEF_RXCHADDRS(info, rxch, v...) \
[RTW89_RXCH_##rxch] = { \
.num = R_AX_##rxch##_RXBD_NUM ##v, \
.idx = R_AX_##rxch##_RXBD_IDX ##v, \
.desa_l = R_AX_##rxch##_RXBD_DESA_L ##v, \
.desa_h = R_AX_##rxch##_RXBD_DESA_H ##v, \
}
const struct rtw89_pci_ch_dma_addr_set rtw89_pci_ch_dma_addr_set = {
.tx = {
DEF_TXCHADDRS(info, ACH0),
DEF_TXCHADDRS(info, ACH1),
DEF_TXCHADDRS(info, ACH2),
DEF_TXCHADDRS(info, ACH3),
DEF_TXCHADDRS(info, ACH4),
DEF_TXCHADDRS(info, ACH5),
DEF_TXCHADDRS(info, ACH6),
DEF_TXCHADDRS(info, ACH7),
DEF_TXCHADDRS(info, CH8),
DEF_TXCHADDRS(info, CH9),
DEF_TXCHADDRS_TYPE1(info, CH10),
DEF_TXCHADDRS_TYPE1(info, CH11),
DEF_TXCHADDRS(info, CH12),
},
.rx = {
DEF_RXCHADDRS(info, RXQ),
DEF_RXCHADDRS(info, RPQ),
},
};
EXPORT_SYMBOL(rtw89_pci_ch_dma_addr_set);
const struct rtw89_pci_ch_dma_addr_set rtw89_pci_ch_dma_addr_set_v1 = {
.tx = {
DEF_TXCHADDRS(info, ACH0, _V1),
DEF_TXCHADDRS(info, ACH1, _V1),
DEF_TXCHADDRS(info, ACH2, _V1),
DEF_TXCHADDRS(info, ACH3, _V1),
DEF_TXCHADDRS(info, ACH4, _V1),
DEF_TXCHADDRS(info, ACH5, _V1),
DEF_TXCHADDRS(info, ACH6, _V1),
DEF_TXCHADDRS(info, ACH7, _V1),
DEF_TXCHADDRS(info, CH8, _V1),
DEF_TXCHADDRS(info, CH9, _V1),
DEF_TXCHADDRS_TYPE1(info, CH10, _V1),
DEF_TXCHADDRS_TYPE1(info, CH11, _V1),
DEF_TXCHADDRS(info, CH12, _V1),
},
.rx = {
DEF_RXCHADDRS(info, RXQ, _V1),
DEF_RXCHADDRS(info, RPQ, _V1),
},
};
EXPORT_SYMBOL(rtw89_pci_ch_dma_addr_set_v1);
#undef DEF_TXCHADDRS_TYPE1
#undef DEF_TXCHADDRS
#undef DEF_RXCHADDRS
static int rtw89_pci_get_txch_addrs(struct rtw89_dev *rtwdev,
enum rtw89_tx_channel txch,
const struct rtw89_pci_ch_dma_addr **addr)
{
const struct rtw89_pci_info *info = rtwdev->pci_info;
if (txch >= RTW89_TXCH_NUM)
return -EINVAL;
*addr = &info->dma_addr_set->tx[txch];
return 0;
}
static int rtw89_pci_get_rxch_addrs(struct rtw89_dev *rtwdev,
enum rtw89_rx_channel rxch,
const struct rtw89_pci_ch_dma_addr **addr)
{
const struct rtw89_pci_info *info = rtwdev->pci_info;
if (rxch >= RTW89_RXCH_NUM)
return -EINVAL;
*addr = &info->dma_addr_set->rx[rxch];
return 0;
}
static u32 rtw89_pci_get_avail_txbd_num(struct rtw89_pci_tx_ring *ring)
{
struct rtw89_pci_dma_ring *bd_ring = &ring->bd_ring;
/* reserved 1 desc check ring is full or not */
if (bd_ring->rp > bd_ring->wp)
return bd_ring->rp - bd_ring->wp - 1;
return bd_ring->len - (bd_ring->wp - bd_ring->rp) - 1;
}
static
u32 __rtw89_pci_check_and_reclaim_tx_fwcmd_resource(struct rtw89_dev *rtwdev)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[RTW89_TXCH_CH12];
u32 cnt;
spin_lock_bh(&rtwpci->trx_lock);
rtw89_pci_reclaim_tx_fwcmd(rtwdev, rtwpci);
cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
spin_unlock_bh(&rtwpci->trx_lock);
return cnt;
}
static u32 __rtw89_pci_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev,
u8 txch)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
struct rtw89_pci_tx_wd_ring *wd_ring = &tx_ring->wd_ring;
u32 bd_cnt, wd_cnt, min_cnt = 0;
struct rtw89_pci_rx_ring *rx_ring;
u32 cnt;
rx_ring = &rtwpci->rx_rings[RTW89_RXCH_RPQ];
spin_lock_bh(&rtwpci->trx_lock);
bd_cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
wd_cnt = wd_ring->curr_num;
if (wd_cnt == 0 || bd_cnt == 0) {
cnt = rtw89_pci_rxbd_recalc(rtwdev, rx_ring);
if (!cnt)
goto out_unlock;
rtw89_pci_release_tx(rtwdev, rx_ring, cnt);
}
bd_cnt = rtw89_pci_get_avail_txbd_num(tx_ring);
wd_cnt = wd_ring->curr_num;
min_cnt = min(bd_cnt, wd_cnt);
if (min_cnt == 0)
rtw89_warn(rtwdev, "still no tx resource after reclaim\n");
out_unlock:
spin_unlock_bh(&rtwpci->trx_lock);
return min_cnt;
}
static u32 rtw89_pci_check_and_reclaim_tx_resource(struct rtw89_dev *rtwdev,
u8 txch)
{
if (txch == RTW89_TXCH_CH12)
return __rtw89_pci_check_and_reclaim_tx_fwcmd_resource(rtwdev);
return __rtw89_pci_check_and_reclaim_tx_resource(rtwdev, txch);
}
static void __rtw89_pci_tx_kick_off(struct rtw89_dev *rtwdev, struct rtw89_pci_tx_ring *tx_ring)
{
struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
u32 host_idx, addr;
addr = bd_ring->addr.idx;
host_idx = bd_ring->wp;
rtw89_write16(rtwdev, addr, host_idx);
}
static void rtw89_pci_tx_bd_ring_update(struct rtw89_dev *rtwdev, struct rtw89_pci_tx_ring *tx_ring,
int n_txbd)
{
struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
u32 host_idx, len;
len = bd_ring->len;
host_idx = bd_ring->wp + n_txbd;
host_idx = host_idx < len ? host_idx : host_idx - len;
bd_ring->wp = host_idx;
}
static void rtw89_pci_ops_tx_kick_off(struct rtw89_dev *rtwdev, u8 txch)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
spin_lock_bh(&rtwpci->trx_lock);
__rtw89_pci_tx_kick_off(rtwdev, tx_ring);
spin_unlock_bh(&rtwpci->trx_lock);
}
static void __pci_flush_txch(struct rtw89_dev *rtwdev, u8 txch, bool drop)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_pci_tx_ring *tx_ring = &rtwpci->tx_rings[txch];
struct rtw89_pci_dma_ring *bd_ring = &tx_ring->bd_ring;
u32 cur_idx, cur_rp;
u8 i;
/* Because the time taked by the I/O is a bit dynamic, it's hard to
* define a reasonable fixed total timeout to use read_poll_timeout*
* helper. Instead, we can ensure a reasonable polling times, so we
* just use for loop with udelay here.
*/
for (i = 0; i < 60; i++) {
cur_idx = rtw89_read32(rtwdev, bd_ring->addr.idx);
cur_rp = FIELD_GET(TXBD_HW_IDX_MASK, cur_idx);
if (cur_rp == bd_ring->wp)
return;
udelay(1);
}
if (!drop)
rtw89_info(rtwdev, "timed out to flush pci txch: %d\n", txch);
}
static void __rtw89_pci_ops_flush_txchs(struct rtw89_dev *rtwdev, u32 txchs,
bool drop)
{
u8 i;
for (i = 0; i < RTW89_TXCH_NUM; i++) {
/* It may be unnecessary to flush FWCMD queue. */
if (i == RTW89_TXCH_CH12)
continue;
if (txchs & BIT(i))
__pci_flush_txch(rtwdev, i, drop);
}
}
static void rtw89_pci_ops_flush_queues(struct rtw89_dev *rtwdev, u32 queues,
bool drop)
{
__rtw89_pci_ops_flush_txchs(rtwdev, BIT(RTW89_TXCH_NUM) - 1, drop);
}
static int rtw89_pci_txwd_submit(struct rtw89_dev *rtwdev,
struct rtw89_pci_tx_ring *tx_ring,
struct rtw89_pci_tx_wd *txwd,
struct rtw89_core_tx_request *tx_req)
{
struct rtw89_pci *rtwpci = (struct rtw89_pci *)rtwdev->priv;
struct rtw89_tx_desc_info *desc_info = &tx_req->desc_info;
struct rtw89_txwd_body *txwd_body;
struct rtw89_txwd_info *txwd_info;
struct rtw89_pci_tx_wp_info *txwp_info;
struct rtw89_pci_tx_addr_info_32 *txaddr_info;
struct pci_dev *pdev = rtwpci->pdev;
struct sk_buff *skb = tx_req->skb;
struct rtw89_pci_tx_data *tx_data = RTW89_PCI_TX_SKB_CB(skb);
bool en_wd_info = desc_info->en_wd_info;
u32 txwd_len;
u32 txwp_len;
u32 txaddr_info_len;
dma_addr_t dma;
int ret;
rtw89_core_fill_txdesc(rtwdev, desc_info, txwd->vaddr);
dma = dma_map_single(&pdev->dev, skb->data, skb->len, DMA_TO_DEVICE);
if (dma_mapping_error(&pdev->dev, dma)) {
rtw89_err(rtwdev, "failed to map skb dma data\n");
ret = -EBUSY;
goto err;
}
tx_data->dma = dma;
txaddr_info_len = sizeof(*txaddr_info);
txwp_len = sizeof(*txwp_info);
txwd_len = sizeof(*txwd_body);
txwd_len += en_wd_info ? sizeof(*txwd_info) : 0;
txwp_info = txwd->vaddr + txwd_len;
txwp_info->seq0 = cpu_to_le16(txwd->seq | RTW89_PCI_TXWP_VALID);
txwp_info->seq1 = 0;
txwp_info->seq2 = 0;
txwp_info->seq3 = 0;
tx_ring->tx_cnt++;
txaddr_info = txwd->vaddr + txwd_len + txwp_len;
txaddr_info->length = cpu_to_le16(skb->len);
txaddr_info->option = cpu_to_le16(RTW89_PCI_ADDR_MSDU_LS |