-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpageset.c
More file actions
1132 lines (939 loc) · 32.6 KB
/
pageset.c
File metadata and controls
1132 lines (939 loc) · 32.6 KB
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
/*
* Copyright 2021-2023 Morse Micro
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "morse.h"
#include "debug.h"
#include "pageset.h"
#include "pager_if.h"
#include "skb_header.h"
#include "ps.h"
#include "hw.h"
#include "bus.h"
#include "ipmon.h"
#include <linux/gpio.h>
#include "pager_if_hw.h"
#include "pager_if_sw.h"
/* Defined as the most number of MPDUs per AMPDU */
#ifndef MAX_PAGES_PER_TX_TXN
#define MAX_PAGES_PER_TX_TXN 16
#endif
/* 2 full AMPDUs (and also more than the number of RX pages in chip) */
#ifndef MAX_PAGES_PER_RX_TXN
#define MAX_PAGES_PER_RX_TXN 32
#endif
/* How frequently to notify the chip when RX pages are returned */
#ifndef PAGE_RETURN_NOTIFY_INT
#define PAGE_RETURN_NOTIFY_INT 4
#endif
/* Time in milliseconds to wait for the beacon tasklet to queue the beacon to skbq */
#define BEACON_TASKLET_WAITQ_TIMEOUT 1
static int is_pageset_locked(struct morse_pageset *pageset)
{
return test_bit(0, &pageset->access_lock);
}
static int pageset_lock(struct morse_pageset *pageset)
{
if (test_and_set_bit_lock(0, &pageset->access_lock))
return -1;
return 0;
}
static void pageset_unlock(struct morse_pageset *pageset)
{
clear_bit_unlock(0, &pageset->access_lock);
}
/* Mappings between sk_buff, skbq and pageset */
static inline struct morse_skbq *skbq_pageset_tc_q_from_aci(struct morse *mors, int aci)
{
struct morse_pageset *pageset = mors->chip_if->to_chip_pageset;
if (!pageset)
return NULL;
if (aci >= ARRAY_SIZE(pageset->data_qs))
return NULL;
return &pageset->data_qs[aci];
}
static inline struct morse_skbq *pageset2cmdq(struct morse_pageset *pageset)
{
return &pageset->cmd_q;
}
static struct morse_skbq *skbq_pageset_cmd_tc_q(struct morse *mors)
{
return (mors->chip_if->to_chip_pageset) ? &mors->chip_if->to_chip_pageset->cmd_q : NULL;
}
static struct morse_skbq *skbq_pageset_bcn_tc_q(struct morse *mors)
{
return (mors->chip_if->to_chip_pageset) ? &mors->chip_if->to_chip_pageset->beacon_q : NULL;
}
static struct morse_skbq *skbq_pageset_mgmt_tc_q(struct morse *mors)
{
return (mors->chip_if->to_chip_pageset) ? &mors->chip_if->to_chip_pageset->mgmt_q : NULL;
}
static void skbq_pageset_get_tx_qs(struct morse *mors, struct morse_skbq **qs, int *num_qs)
{
struct morse_pageset *pageset = mors->chip_if->to_chip_pageset;
*qs = pageset->data_qs;
*num_qs = PAGESET_TX_SKBQ_MAX;
}
static struct morse_skbq *skbq_pageset_get_rx_data_q(struct morse *mors)
{
/* On rx, all data frames go through data_q[0] */
const int rx_data_queue = 0;
return (mors->chip_if && mors->chip_if->from_chip_pageset) ?
&mors->chip_if->from_chip_pageset->data_qs[rx_data_queue] : NULL;
}
static int morse_pageset_get_rx_buffered_count(struct morse *mors)
{
struct morse_skbq *skbq = skbq_pageset_get_rx_data_q(mors);
if (!skbq)
return 0;
return skbq->skbq.qlen;
}
const struct chip_if_ops morse_pageset_hw_ops = {
.init = morse_pager_hw_pagesets_init,
.hw_restarted = morse_pager_hw_pagesets_init,
.flush_tx_data = morse_pager_hw_pagesets_flush_tx_data,
.skbq_get_tx_status_pending_count = morse_pagesets_get_tx_status_pending_count,
.skbq_get_tx_buffered_count = morse_pagesets_get_tx_buffered_count,
.finish = morse_pager_hw_pagesets_finish,
.skbq_get_tx_qs = skbq_pageset_get_tx_qs,
.skbq_bcn_tc_q = skbq_pageset_bcn_tc_q,
.skbq_mgmt_tc_q = skbq_pageset_mgmt_tc_q,
.skbq_cmd_tc_q = skbq_pageset_cmd_tc_q,
.skbq_tc_q_from_aci = skbq_pageset_tc_q_from_aci,
.chip_if_handle_irq = morse_pager_irq_handler
};
const struct chip_if_ops morse_pageset_sw_ops = {
.init = morse_pager_sw_pagesets_init,
.flush_tx_data = morse_pager_sw_pagesets_flush_tx_data,
.skbq_get_tx_status_pending_count = morse_pagesets_get_tx_status_pending_count,
.skbq_get_tx_buffered_count = morse_pagesets_get_tx_buffered_count,
.finish = morse_pager_sw_pagesets_finish,
.skbq_get_tx_qs = skbq_pageset_get_tx_qs,
.skbq_bcn_tc_q = skbq_pageset_bcn_tc_q,
.skbq_mgmt_tc_q = skbq_pageset_mgmt_tc_q,
.skbq_cmd_tc_q = skbq_pageset_cmd_tc_q,
.skbq_tc_q_from_aci = skbq_pageset_tc_q_from_aci,
.chip_if_handle_irq = morse_pager_irq_handler
};
static bool morse_pageset_page_is_cached(struct morse_pageset *pageset, struct morse_page *page)
{
int i;
int n_pages;
struct morse_page pages[CACHED_PAGES_MAX];
BUILD_BUG_ON(CMD_RSVED_PAGES_MAX > CACHED_PAGES_MAX);
MORSE_WARN_ON(FEATURE_ID_PAGER, !pageset || !page);
if (!pageset || !page)
return false;
n_pages = kfifo_out_peek(&pageset->reserved_pages, pages, CACHED_PAGES_MAX);
for (i = 0; i < n_pages; i++) {
if (pages[i].addr == page->addr)
return true;
}
n_pages = kfifo_out_peek(&pageset->cached_pages, pages, CACHED_PAGES_MAX);
for (i = 0; i < n_pages; i++) {
if (pages[i].addr == page->addr)
return true;
}
return false;
}
static void morse_pageset_to_chip_return_handler_no_lock(struct morse *mors)
{
struct morse_pageset *pageset = mors->chip_if->to_chip_pageset;
struct morse_pager *pager = pageset->return_pager;
struct morse_page page;
int ret;
bool pager_empty = false;
unsigned int popped = 0;
/* Continue to pop until either the pager is exhausted or more than
* double the amount of possible cache entries have been popped.
* (to max bound this loop in the event of hardware failure)
*/
unsigned int max_expected_pops = kfifo_size(&pageset->cached_pages) * 2;
MORSE_WARN_ON(FEATURE_ID_PAGER, !is_pageset_locked(pageset));
while (kfifo_len(&pageset->reserved_pages) < CMD_RSVED_PAGES_MAX) {
if (pager->ops->pop(pager, &page)) {
pager_empty = true;
break;
}
popped++;
if (morse_pageset_page_is_cached(pageset, &page))
continue;
ret = kfifo_put(&pageset->reserved_pages, page);
MORSE_WARN_ON(FEATURE_ID_PAGER, !ret);
}
if (pager_empty)
goto exit;
while (popped < max_expected_pops) {
if (pager->ops->pop(pager, &page))
break;
popped++;
if (morse_pageset_page_is_cached(pageset, &page))
continue;
ret = kfifo_put(&pageset->cached_pages, page);
MORSE_WARN_ON(FEATURE_ID_PAGER, !ret);
}
MORSE_WARN_ON(FEATURE_ID_PAGER, popped >= max_expected_pops);
exit:
if (popped)
pager->ops->notify(pager);
}
static void morse_pageset_to_chip_return_handler(struct morse *mors, bool have_lock)
{
struct morse_pageset *pageset = mors->chip_if->to_chip_pageset;
if (!have_lock) {
int ret = 0;
ret = pageset_lock(pageset);
if (ret) {
MORSE_DBG(mors, "%s pageset lock failed %d\n", __func__, ret);
return;
}
}
morse_pageset_to_chip_return_handler_no_lock(mors);
if (!have_lock)
pageset_unlock(pageset);
}
#define BCN_LOSS_CHECK (500)
#define BCN_LOSS_THRESHOLD (50)
static unsigned int bcn_page_get;
static unsigned int bcn_page_fail;
/*
* Some beacons may be lost by design. Report excessive beacon loss.
*/
static void morse_pageset_bcn_loss_monitor(struct morse *mors)
{
if (bcn_page_fail > BCN_LOSS_THRESHOLD) {
mors->debug.page_stats.excessive_bcn_loss++;
MORSE_WARN(mors, "%s failed to send %d of %d beacons\n",
__func__, bcn_page_fail, bcn_page_get);
}
bcn_page_get = 0;
bcn_page_fail = 0;
}
static bool morse_pageset_rsved_page_is_avail(struct morse_pageset *pageset, u8 channel,
bool have_lock)
{
struct morse *mors = pageset->mors;
switch (channel) {
case MORSE_SKB_CHAN_BEACON:
bcn_page_get++;
if (bcn_page_get == BCN_LOSS_CHECK)
morse_pageset_bcn_loss_monitor(mors);
/* Always hold at least one reserved page for commands */
if (kfifo_len(&pageset->reserved_pages) <= 1) {
bcn_page_fail++;
mors->debug.page_stats.bcn_no_page++;
MORSE_DBG(mors, "%s no page available for beacon\n", __func__);
return false;
}
return true;
case MORSE_SKB_CHAN_COMMAND:
/*
* Always try to write a command. There should only ever be one and there should
* always be a reserved page available. It may have been returned after the command
* response, so check again if it's not already available.
*/
if (kfifo_is_empty(&pageset->reserved_pages)) {
morse_pageset_to_chip_return_handler(mors, have_lock);
if (kfifo_is_empty(&pageset->reserved_pages)) {
mors->debug.page_stats.cmd_no_page++;
MORSE_ERR(mors, "%s unexpected command page exhaustion\n",
__func__);
} else {
mors->debug.page_stats.cmd_rsv_page_retry++;
MORSE_DBG(mors, "%s got command page on second attempt\n",
__func__);
}
}
return (kfifo_len(&pageset->reserved_pages) > 0);
}
return 0;
}
static int morse_pageset_write(struct morse_pageset *pageset, struct sk_buff *skb)
{
int ret = 0;
bool from_rsvd = false;
struct morse *mors = pageset->mors;
struct morse_pager *populated_pager = pageset->populated_pager;
struct morse_page page;
struct morse_buff_skb_header *hdr = (struct morse_buff_skb_header *)skb->data;
size_t end_of_skb_pad = (skb->len & 0x03) ? (4 - (unsigned long)(skb->len & 3)) : 0;
size_t write_len = skb->len + end_of_skb_pad;
ret = pageset_lock(pageset);
if (ret) {
MORSE_DBG(mors, "%s pageset lock failed %d\n", __func__, ret);
return ret;
}
if (morse_pageset_rsved_page_is_avail(pageset, hdr->channel, true)) {
ret = kfifo_get(&pageset->reserved_pages, &page);
from_rsvd = true;
} else {
ret = kfifo_get(&pageset->cached_pages, &page);
}
if (ret <= 0) {
MORSE_ERR(mors, "%s no pages available\n", __func__);
ret = -ENOSPC;
goto exit;
}
if (write_len > page.size_bytes) {
MORSE_ERR(mors, "%s Data larger than pagesize: [%d:%d]\n",
__func__, skb->len, page.size_bytes);
ret = -ENOSPC;
goto exit;
}
if (write_len > (skb->len + skb_tailroom(skb))) {
/* SKB should be big enough to copy from */
MORSE_WARN_ON(FEATURE_ID_SKB, 1);
ret = -ENOSPC;
goto exit;
}
morse_debug_fw_hostif_log_record(mors, true, skb, hdr);
ret = populated_pager->ops->write_page(populated_pager, &page, 0, skb->data, write_len);
if (ret) {
MORSE_ERR(mors, "%s failed to write page: %d\n", __func__, ret);
/* Put the page back into the cache */
if (from_rsvd)
kfifo_put(&pageset->reserved_pages, page);
else
kfifo_put(&pageset->cached_pages, page);
goto exit;
}
/* Put the filled page to send it to the chip */
ret = populated_pager->ops->put(populated_pager, &page);
if (ret) {
MORSE_ERR(mors, "%s failed to return page: %d\n", __func__, ret);
/* Return page to avoid page leak.
* Write sync word as 0 so the chip discards it.
* Don't not putting this in the return pager to avoid
* reading and writing from the same pager, as this would require
* additional synchronisation.
*/
hdr->sync = 0;
populated_pager->ops->write_page(populated_pager, &page, 0,
(const char *)hdr, sizeof(*hdr));
populated_pager->ops->put(populated_pager, &page);
goto exit;
}
exit:
pageset_unlock(pageset);
return ret;
}
static int morse_pageset_read(struct morse_pageset *pageset)
{
int ret = 0;
struct morse *mors = pageset->mors;
struct sk_buff *skb = NULL;
struct morse_skbq *mq = NULL;
struct morse_pager *return_pager = pageset->return_pager;
struct morse_pager *populated_pager = pageset->populated_pager;
struct morse_chip_if_state *chip_if = mors->chip_if;
struct morse_page page = { .addr = 0, .size_bytes = 0 };
struct morse_buff_skb_header *hdr;
int skb_len;
int max_checksum_rounds = 2;
int count = 0;
bool checksum_valid = !(mors->chip_if->validate_skb_checksum);
/* Preference CMD responses, then TX statuses from the pager bypass
* locations before processing other RX. We treat these page types as
* 'first-class' citizens.
*/
if (kfifo_len(&chip_if->bypass.cmd_resp.to_process) > 0) {
ret = kfifo_get(&chip_if->bypass.cmd_resp.to_process, &page.addr);
WARN_ON(ret == 0);
page.size_bytes = populated_pager->page_size_bytes;
} else if (kfifo_len(&chip_if->bypass.tx_sts.to_process) > 0) {
ret = kfifo_get(&chip_if->bypass.tx_sts.to_process, &page.addr);
WARN_ON(ret == 0);
page.size_bytes = populated_pager->page_size_bytes;
} else {
/* Pop one page from pager */
ret = populated_pager->ops->pop(populated_pager, &page);
if (ret) {
/* No pages left */
page.addr = 0;
goto exit;
}
}
skb_len = round_up(page.addr >> 20, 4);
page.addr = ((page.addr & 0xFFFFF) | mors->cfg->regs->pager_base_address);
/* Allocate an skb for the page data, copy header to it */
skb = dev_alloc_skb(skb_len);
if (!skb) {
ret = -ENOMEM;
goto exit;
}
skb_put(skb, skb_len);
/* Read page data */
ret = populated_pager->ops->read_page(populated_pager, &page, 0, skb->data, skb_len);
if (ret) {
MORSE_ERR(mors, "%s failed to read page: %d\n", __func__, ret);
/* Error is considered catastrophic, pass up error to stop
* more page pops.
*/
goto exit;
}
hdr = (struct morse_buff_skb_header *)skb->data;
morse_debug_fw_hostif_log_record(mors, false, skb, hdr);
/* Validate header */
if (hdr->sync != MORSE_SKB_HEADER_SYNC) {
bool chip_owned = (hdr->sync == MORSE_SKB_HEADER_CHIP_OWNED_SYNC);
MORSE_DBG(mors, "%s sync error:0x%02X page[addr:0x%08x len:%d]\n",
__func__, hdr->sync, page.addr, hdr->len);
if (chip_owned) {
/* Chip already owns the page, clear page address
* to indicate that it should not be returned
*/
mors->debug.page_stats.page_owned_by_chip++;
page.addr = 0;
}
/* Not considered catastrophic, continue to read pages out of the
* pager.
*/
ret = 0;
goto exit;
}
while (!checksum_valid && count < max_checksum_rounds) {
checksum_valid = morse_validate_skb_checksum(skb->data);
if (checksum_valid)
break;
mors->debug.page_stats.invalid_checksum++;
/* Read tx status again if the first read is corrupted. There is a tput degradation
* if continue to read pages from the pager.
*/
if (hdr->channel != MORSE_SKB_CHAN_TX_STATUS)
break;
ret =
populated_pager->ops->read_page(populated_pager, &page, 0, skb->data, skb_len);
if (ret)
break;
count++;
}
if (!checksum_valid) {
MORSE_DBG(mors,
"%s: SKB checksum is invalid, page:[a:0x%08x len:%d] hdr:[c:%02X s:%02X]",
__func__, page.addr, skb_len, hdr->channel, hdr->sync);
if (hdr->channel == MORSE_SKB_CHAN_TX_STATUS)
mors->debug.page_stats.invalid_tx_status_checksum++;
goto exit;
}
/* SW-3875: seems like sdio read can sometimes go wrong and read first 4-bytes word twice,
* overwriting second word (hence, offset will be overwritten with 'sync' byte). Anyway, we
* should not expect offset value to be larger than word alignment (max 3 bytes)
*/
if (hdr->offset > 3) {
MORSE_ERR(mors,
"%s: corrupted skb header offset [offset=%u], hdr.len %d, page addr: 0x%08x\n",
__func__, hdr->offset, hdr->len, page.addr);
/* Should we actually do that, or just fail the page and go to exit_return_page? */
hdr->offset = (le16_to_cpu(hdr->len) & 0x03) ?
(4 - (unsigned long)(le16_to_cpu(hdr->len) & 3)) : 0;
}
/* Get correct skbq for the data based on the declared channel */
switch (hdr->channel) {
case MORSE_SKB_CHAN_DATA:
case MORSE_SKB_CHAN_NDP_FRAMES:
case MORSE_SKB_CHAN_TX_STATUS:
case MORSE_SKB_CHAN_DATA_NOACK:
case MORSE_SKB_CHAN_BEACON:
case MORSE_SKB_CHAN_MGMT:
case MORSE_SKB_CHAN_LOOPBACK:
mq = skbq_pageset_get_rx_data_q(mors);
break;
case MORSE_SKB_CHAN_COMMAND:
mq = &pageset->cmd_q;
break;
default:
MORSE_ERR(mors, "%s: unknown channel %d\n", __func__, hdr->channel);
/* Not considered catastrophic, continue to read pages out of the
* pager.
*/
ret = 0;
goto exit;
}
if (!mq) {
MORSE_WARN_ON_ONCE(FEATURE_ID_PAGER, 1);
goto exit;
}
/* Read of page can be greater than actual size of data - so trim */
skb_len = sizeof(*hdr) + hdr->offset + le16_to_cpu(hdr->len);
skb_trim(skb, skb_len);
#ifdef CONFIG_MORSE_IPMON
if (hdr->channel == MORSE_SKB_CHAN_DATA) {
static u64 time_start;
morse_ipmon(&time_start, skb, skb->data + sizeof(*hdr),
le16_to_cpu(hdr->len), IPMON_LOC_SERVER_DRV, 0);
}
#endif
ret = morse_skbq_put(mq, skb);
/* Unconditionally queue network work to process RX page. Either
* insertion into the mq was successful, or the mq is currently full
* and requires processing anyway.
*/
queue_work(mors->net_wq, &mq->dispatch_work);
if (ret) {
MORSE_ERR(mors, "%s: Failed to insert skb into mq[channel:%d]\n", __func__,
hdr->channel);
/* Considered catastrophic, return error code to stop page pop
* operations and more data getting lost.
*/
ret = -ENOMEM;
goto exit;
}
/* Successful in receiving page/skb. Do not free the page as it now
* is the responsibility of mq.
*/
skb = NULL;
exit:
/* If the SKB did not successfully make it into an MQ, it must be freed */
dev_kfree_skb(skb);
if (page.addr) {
/* Put the emptied page to send it back to the chip */
ret = return_pager->ops->put(return_pager, &page);
if (ret)
MORSE_ERR(mors, "%s: return page failed: %d\n", __func__, ret);
}
return ret;
}
/**
* Determine how many pages are available for sending packets to the firmware.
* - Always use 1 for commands. There should only ever be one command in progress at a
* time and there is a reserved page for it. If anything goes wrong the command will
* be dropped.
*/
static int morse_pageset_num_pages(struct morse_pageset *pageset, struct sk_buff *skb)
{
struct morse_buff_skb_header *hdr = (struct morse_buff_skb_header *)skb->data;
int num_pages = 0;
if (hdr->channel == MORSE_SKB_CHAN_COMMAND) {
num_pages = min(CMD_RSVED_CMD_PAGES_MAX,
(int)kfifo_len(&pageset->reserved_pages) +
(int)kfifo_len(&pageset->cached_pages));
} else {
if (morse_pageset_rsved_page_is_avail(pageset, hdr->channel, false))
num_pages = (CMD_RSVED_PAGES_MAX - CMD_RSVED_CMD_PAGES_MAX);
num_pages = min(MAX_PAGES_PER_TX_TXN,
num_pages + (int)kfifo_len(&pageset->cached_pages));
}
return num_pages;
}
static void morse_pageset_tx(struct morse_pageset *pageset, struct morse_skbq *mq)
{
int ret = 0;
int num_pages;
int num_items = 0;
struct sk_buff *skb;
struct sk_buff_head skbq_to_send;
struct sk_buff_head skbq_sent;
struct sk_buff_head skbq_failed;
struct sk_buff *pfirst, *pnext;
struct morse *mors = pageset->mors;
struct morse_buff_skb_header *hdr;
spin_lock_bh(&mq->lock);
skb = skb_peek(&mq->skbq);
if (skb)
num_pages = morse_pageset_num_pages(pageset, skb);
spin_unlock_bh(&mq->lock);
if (!skb)
return;
__skb_queue_head_init(&skbq_to_send);
__skb_queue_head_init(&skbq_sent);
__skb_queue_head_init(&skbq_failed);
if (mq == &pageset->cmd_q)
/* Purge timed-out commands (this should not happen) */
morse_skbq_purge(mq, &mq->pending);
else if (mq == &pageset->mgmt_q && mq->skbq.qlen > 0)
/* Purge old mgmt frames that have not been sent due to congestion */
morse_skbq_purge_aged(mors, mq);
if (num_pages > 0)
num_items = morse_skbq_deq_num_items(mq, &skbq_to_send, num_pages);
skb_queue_walk_safe(&skbq_to_send, pfirst, pnext) {
if (num_pages) {
ret = morse_pageset_write(pageset, pfirst);
} else {
mors->debug.page_stats.no_page++;
MORSE_ERR(mors, "%s no pages available\n", __func__);
ret = -ENOSPC;
}
if (ret == 0) {
hdr = (struct morse_buff_skb_header *)pfirst->data;
switch (hdr->channel) {
case MORSE_SKB_CHAN_COMMAND:
mors->debug.page_stats.cmd_tx++;
break;
case MORSE_SKB_CHAN_BEACON:
mors->debug.page_stats.bcn_tx++;
break;
case MORSE_SKB_CHAN_MGMT:
mors->debug.page_stats.mgmt_tx++;
break;
default:
mors->debug.page_stats.data_tx++;
break;
}
num_pages--;
__skb_unlink(pfirst, &skbq_to_send);
__skb_queue_tail(&skbq_sent, pfirst);
} else {
__skb_unlink(pfirst, &skbq_to_send);
__skb_queue_tail(&skbq_failed, pfirst);
}
}
if (skbq_failed.qlen > 0) {
mors->debug.page_stats.write_fail += skbq_failed.qlen;
MORSE_ERR(mors, "%s could not write %d pkts - rc=%d items=%d pages=%d",
__func__, skbq_failed.qlen, ret, num_items, num_pages);
morse_skbq_purge(NULL, &skbq_failed);
}
if (skbq_sent.qlen > 0) {
morse_skbq_tx_complete(mq, &skbq_sent);
pageset->populated_pager->ops->notify(pageset->populated_pager);
}
}
/* Returns true if there are TX data pages waiting to be sent */
static bool morse_pageset_tx_data_handler(struct morse_pageset *pageset)
{
s16 aci;
u32 count = 0;
struct morse *mors = pageset->mors;
for (aci = MORSE_ACI_VO; aci >= 0; aci--) {
struct morse_skbq *data_q = skbq_pageset_tc_q_from_aci(mors, aci);
if (!morse_is_data_tx_allowed(mors))
break;
morse_pageset_tx(pageset, data_q);
count += morse_skbq_count(data_q);
if (aci == MORSE_ACI_BE)
break;
}
/* Data has potentially been transmitted from the data SKBQs.
* If the mac80211 TX data Qs were previously stopped,
* now would be a good time to check if they can be started again.
*/
morse_skbq_may_wake_tx_queues(mors);
if (mors->custom_configs.enable_airtime_fairness &&
!test_bit(MORSE_STATE_FLAG_DATA_QS_STOPPED, &mors->state_flags))
tasklet_schedule(&mors->tasklet_txq);
return (count > 0) && morse_is_data_tx_allowed(mors);
}
/* Returns true if there are commands waiting to be sent */
static bool morse_pageset_tx_cmd_handler(struct morse_pageset *pageset)
{
struct morse_skbq *cmd_q = pageset2cmdq(pageset);
morse_pageset_tx(pageset, cmd_q);
return (morse_skbq_count(cmd_q) > 0);
}
static bool morse_pageset_tx_beacon_handler(struct morse_pageset *pageset)
{
struct morse_skbq *beacon_q = &pageset->beacon_q;
morse_pageset_tx(pageset, beacon_q);
return (morse_skbq_count(beacon_q) > 0);
}
static bool morse_pageset_tx_mgmt_handler(struct morse_pageset *pageset)
{
struct morse_skbq *mgmt_q = &pageset->mgmt_q;
morse_pageset_tx(pageset, mgmt_q);
return (morse_skbq_count(mgmt_q) > 0);
}
/**
* morse_is_beacon_request_interrupt_set() - Checks if beacon request interrupt
* bit is set.
*
* @mors: morse chip struct
*
* Return: true if beacon request interrupt bit is set.
*/
static inline bool morse_is_beacon_request_interrupt_set(struct morse *mors)
{
u32 status1 = 0;
morse_reg32_read(mors, MORSE_REG_INT1_STS(mors), &status1);
if (status1 & MORSE_INT_BEACON_VIF_MASK_ALL)
return true;
return false;
}
/* Returns true if there are populated RX pages left in the device */
static bool morse_pageset_rx_handler(struct morse_pageset *pageset,
bool *is_beacon_pending)
{
int ret = 0;
int count = 0;
bool return_notify_req = false;
bool do_beacon_irq_check = is_beacon_pending;
MORSE_WARN_ON(FEATURE_ID_PAGER, is_pageset_locked(pageset));
/* Read as many pages out as are available up to the RX limit */
do {
ret = morse_pageset_read(pageset);
count++;
return_notify_req = true;
if ((count % PAGE_RETURN_NOTIFY_INT) == 0) {
pageset->return_pager->ops->notify(pageset->return_pager);
return_notify_req = false;
if (do_beacon_irq_check &&
(morse_is_beacon_request_interrupt_set(pageset->mors))) {
*is_beacon_pending = true;
break;
}
}
} while ((count < MAX_PAGES_PER_RX_TXN) && (ret == 0));
MORSE_WARN_ON(FEATURE_ID_PAGER,
kfifo_len(&pageset->mors->chip_if->bypass.tx_sts.to_process) > 0);
MORSE_WARN_ON(FEATURE_ID_PAGER,
kfifo_len(&pageset->mors->chip_if->bypass.cmd_resp.to_process) > 0);
if (return_notify_req)
pageset->return_pager->ops->notify(pageset->return_pager);
pageset->populated_pager->ops->notify(pageset->populated_pager);
if (ret == -ENOMEM || count == MAX_PAGES_PER_RX_TXN ||
(do_beacon_irq_check && *is_beacon_pending))
return true;
else
return false;
}
void morse_pagesets_stale_tx_work(struct work_struct *work)
{
int i;
int flushed = 0;
struct morse *mors = container_of(work, struct morse, tx_stale_work);
struct morse_pageset *tx_pageset;
if (!mors->chip_if || !mors->chip_if->to_chip_pageset || !mors->stale_status.enabled)
return;
tx_pageset = mors->chip_if->to_chip_pageset;
flushed += morse_skbq_check_for_stale_tx(mors, &tx_pageset->beacon_q);
flushed += morse_skbq_check_for_stale_tx(mors, &tx_pageset->mgmt_q);
for (i = 0; i < ARRAY_SIZE(tx_pageset->data_qs); i++)
flushed += morse_skbq_check_for_stale_tx(mors, &tx_pageset->data_qs[i]);
if (flushed) {
MORSE_DBG(mors, "%s: Flushed %d stale TX SKBs\n", __func__, flushed);
if (mors->ps.enable &&
!mors->ps.suspended && (morse_pagesets_get_tx_buffered_count(mors) == 0)) {
/* Evaluate ps to check if it was gated on a stale tx status */
queue_delayed_work(mors->chip_wq, &mors->ps.delayed_eval_work, 0);
}
}
}
void morse_pagesets_work(struct work_struct *work)
{
struct morse *mors = container_of(work,
struct morse, chip_if_work);
int ps_bus_timeout_ms = 0;
unsigned long flags_on_entry = mors->chip_if->event_flags;
unsigned long *flags = &mors->chip_if->event_flags;
int rx_buffered_on_entry = morse_pageset_get_rx_buffered_count(mors);
bool is_beacon_pending = false;
if (!flags_on_entry)
return;
/* Don't attempt to interact with device once it becomes unresponsive */
if (test_bit(MORSE_STATE_FLAG_CHIP_UNRESPONSIVE, &mors->state_flags))
return;
/* Disable power save in case it is running */
morse_ps_disable(mors);
morse_claim_bus(mors);
/* Tx beacons first */
if (test_and_clear_bit(MORSE_TX_BEACON_PEND, flags)) {
if (morse_pageset_tx_beacon_handler(mors->chip_if->to_chip_pageset))
set_bit(MORSE_TX_BEACON_PEND, flags);
}
/* Handle any populated RX pages from chip first to
* avoid dropping pkts due to full on-chip buffers.
* Check if all pages were removed, set event flags if not.
*/
if (test_and_clear_bit(MORSE_RX_PEND, flags)) {
/* Check for beacon requests from target if AP interface exists */
if (morse_pageset_rx_handler(mors->chip_if->from_chip_pageset,
mors->num_of_ap_interfaces > 0 ? &is_beacon_pending : NULL))
set_bit(MORSE_RX_PEND, flags);
if (is_beacon_pending) {
mors->beacon_queued = false;
morse_release_bus(mors);
wait_event_interruptible_timeout(mors->beacon_tasklet_waitq,
mors->beacon_queued,
msecs_to_jiffies(BEACON_TASKLET_WAITQ_TIMEOUT));
morse_claim_bus(mors);
}
}
/* Handle any free TX pages being returned so caches are refilled */
if (test_and_clear_bit(MORSE_PAGE_RETURN_PEND, flags))
morse_pageset_to_chip_return_handler(mors, false);
/* TX any commands before anything else */
if (test_and_clear_bit(MORSE_TX_COMMAND_PEND, flags)) {
if (morse_pageset_tx_cmd_handler(mors->chip_if->to_chip_pageset))
set_bit(MORSE_TX_COMMAND_PEND, flags);
}
/* TX beacons before considering mgmt/data */
if (test_and_clear_bit(MORSE_TX_BEACON_PEND, flags)) {
if (morse_pageset_tx_beacon_handler(mors->chip_if->to_chip_pageset))
set_bit(MORSE_TX_BEACON_PEND, flags);
}
/* Process Rx buffers again if rx pages processing is stopped for any pending beacon */
if (test_and_clear_bit(MORSE_RX_PEND, flags) && is_beacon_pending) {
if (morse_pageset_rx_handler(mors->chip_if->from_chip_pageset, NULL))
set_bit(MORSE_RX_PEND, flags);
}
/* TX mgmt before considering data */
if (test_and_clear_bit(MORSE_TX_MGMT_PEND, flags)) {
if (morse_pageset_tx_mgmt_handler(mors->chip_if->to_chip_pageset))
set_bit(MORSE_TX_MGMT_PEND, flags);
}
/* Pause TX data Qs */
if (test_and_clear_bit(MORSE_DATA_TRAFFIC_PAUSE_PEND, flags)) {
if (test_and_clear_bit(MORSE_DATA_TRAFFIC_RESUME_PEND, flags))
MORSE_ERR_RATELIMITED(mors,
"Latency to handle traffic pause is too great\n");
else
morse_skbq_data_traffic_pause(mors);
}
/* Resume TX data Qs */
if (test_and_clear_bit(MORSE_DATA_TRAFFIC_RESUME_PEND, flags)) {
if (test_bit(MORSE_DATA_TRAFFIC_PAUSE_PEND, flags))
MORSE_ERR_RATELIMITED(mors,
"Latency to handle traffic resume is too great\n");
morse_skbq_data_traffic_resume(mors);
}
/* Finally TX any data */
if (test_and_clear_bit(MORSE_TX_DATA_PEND, flags)) {
if (morse_pageset_tx_data_handler(mors->chip_if->to_chip_pageset))
set_bit(MORSE_TX_DATA_PEND, flags);
}
/* Calculate timeout to disable the shared bus with the chip. For any TX that was
* pushed down, or non-CMD RX that came up from the chip - increase the timeout as
* 'network activity' was seen.
*/
if (test_bit(MORSE_TX_DATA_PEND, &flags_on_entry) ||
test_bit(MORSE_TX_MGMT_PEND, &flags_on_entry) ||
morse_pageset_get_rx_buffered_count(mors) > rx_buffered_on_entry)
ps_bus_timeout_ms = max(ps_bus_timeout_ms, morse_network_bus_timeout(mors));
if (test_and_clear_bit(MORSE_UPDATE_HW_CLOCK_REFERENCE, flags))
morse_hw_clock_update(mors);
if (ps_bus_timeout_ms)
morse_ps_bus_activity(mors, ps_bus_timeout_ms);
/* Disable power save in case it is running */
morse_release_bus(mors);
morse_ps_enable(mors);
/* A single RX event may represent the reception
* of many pages. We might not be able to process all these pages
* immediately. As such, manually requeue a chip work item - the firmware
* will not do this again.
*
* This is not required for TX events, as each single transmission will
* schedule a work event.
*/
if (test_bit(MORSE_RX_PEND, flags))
queue_work(mors->chip_wq, &mors->chip_if_work);
}
void morse_pageset_show(struct morse *mors, struct morse_pageset *pageset, struct seq_file *file)
{
int i;
seq_printf(file, "flags:0x%01x reserved=%d cached=%d\n",
pageset->flags,
kfifo_len(&pageset->reserved_pages), kfifo_len(&pageset->cached_pages));
morse_pager_show(pageset->mors, pageset->populated_pager, file);
morse_pager_show(pageset->mors, pageset->return_pager, file);
for (i = 0; i < ARRAY_SIZE(pageset->data_qs); i++)
morse_skbq_show(&pageset->data_qs[i], file);
morse_skbq_show(&pageset->mgmt_q, file);
morse_skbq_show(&pageset->beacon_q, file);
morse_skbq_show(&pageset->cmd_q, file);
}
int morse_pageset_init(struct morse *mors, struct morse_pageset *pageset,