-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhnd_pktpool.c
2341 lines (1960 loc) · 58.2 KB
/
hnd_pktpool.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
/*
* HND generic packet pool operation primitives
*
* Copyright (C) 2022, Broadcom.
*
* Unless you and Broadcom execute a separate written software license
* agreement governing use of this software, this software is licensed to you
* under the terms of the GNU General Public License version 2 (the "GPL"),
* available at http://www.broadcom.com/licenses/GPLv2.php, with the
* following added to such license:
*
* As a special exception, the copyright holders of this software give you
* permission to link this software with independent modules, and to copy and
* distribute the resulting executable under terms of your choice, provided that
* you also meet, for each linked independent module, the terms and conditions of
* the license of that module. An independent module is a module which is not
* derived from this software. The special exception does not apply to any
* modifications of the software.
*
*
* <<Broadcom-WL-IPTag/Dual:>>
*/
#include <typedefs.h>
#include <osl.h>
#include <osl_ext.h>
#include <bcmutils.h>
#include <wlioctl.h>
#include <hnd_pktpool.h>
#ifdef BCMRESVFRAGPOOL
#include <hnd_resvpool.h>
#endif /* BCMRESVFRAGPOOL */
#ifdef BCMFRWDPOOLREORG
#include <hnd_poolreorg.h>
#endif /* BCMFRWDPOOLREORG */
#if defined(DONGLEBUILD) && defined(SRMEM)
#include <hndsrmem.h>
#endif /* DONGLEBUILD && SRMEM */
#if defined(DONGLEBUILD)
#include <d11_cfg.h>
#endif
/* mutex macros for thread safe */
#ifdef HND_PKTPOOL_THREAD_SAFE
#define HND_PKTPOOL_MUTEX_CREATE(name, mutex) osl_ext_mutex_create(name, mutex)
#define HND_PKTPOOL_MUTEX_DELETE(mutex) osl_ext_mutex_delete(mutex)
#define HND_PKTPOOL_MUTEX_ACQUIRE(mutex, msec) osl_ext_mutex_acquire(mutex, msec)
#define HND_PKTPOOL_MUTEX_RELEASE(mutex) osl_ext_mutex_release(mutex)
#else
#define HND_PKTPOOL_MUTEX_CREATE(name, mutex) OSL_EXT_SUCCESS
#define HND_PKTPOOL_MUTEX_DELETE(mutex) OSL_EXT_SUCCESS
#define HND_PKTPOOL_MUTEX_ACQUIRE(mutex, msec) OSL_EXT_SUCCESS
#define HND_PKTPOOL_MUTEX_RELEASE(mutex) OSL_EXT_SUCCESS
#endif
#define PKTPOOL_REGISTRY_SET(id, pp) (pktpool_registry_set((id), (pp)))
#define PKTPOOL_REGISTRY_CMP(id, pp) (pktpool_registry_cmp((id), (pp)))
/* Tag a registry entry as free for use */
#define PKTPOOL_REGISTRY_CLR(id) \
PKTPOOL_REGISTRY_SET((id), PKTPOOLREG_FREE_PTR)
#define PKTPOOL_REGISTRY_ISCLR(id) \
(PKTPOOL_REGISTRY_CMP((id), PKTPOOLREG_FREE_PTR))
/* Tag registry entry 0 as reserved */
#define PKTPOOL_REGISTRY_RSV() \
PKTPOOL_REGISTRY_SET(PKTPOOLREG_RSVD_ID, PKTPOOLREG_RSVD_PTR)
#define PKTPOOL_REGISTRY_ISRSVD() \
(PKTPOOL_REGISTRY_CMP(PKTPOOLREG_RSVD_ID, PKTPOOLREG_RSVD_PTR))
/* Walk all un-reserved entries in registry */
#define PKTPOOL_REGISTRY_FOREACH(id) \
for ((id) = 1U; (id) <= pktpools_max; (id)++)
enum pktpool_empty_cb_state {
EMPTYCB_ENABLED = 0, /* Enable callback when new packets are added to pool */
EMPTYCB_DISABLED, /* Disable callback when new packets are added to pool */
EMPTYCB_SKIPPED /* Packet was added to pool when callback was disabled */
};
uint32 pktpools_max = 0U; /* maximum number of pools that may be initialized */
pktpool_t *pktpools_registry[PKTPOOL_MAXIMUM_ID + 1]; /* Pktpool registry */
/* number of pktids that are reserved for pktpool usage at the moment
* initializing this with max pktids reserved for pktpool
* pktpool_init, pktpool_fill and pktpool_refill decrements this
* pktpool_reclaim, pktpool_empty and heap_pkt_release increments this
*/
#ifdef DONGLEBUILD
uint32 total_pool_pktid_count = PKTID_POOL;
#else
uint32 total_pool_pktid_count = 0U;
#endif /* DONGLEBUILD */
int last_alloc_err = 0; /* Error reported during last alloc failure */
#if defined(BCMPKTIDMAP) || defined(BCMPKTIDMAP_MIN)
#define TOTAL_POOL_PKTID_CNT_INC(pktp) \
do { \
if (pktp->type != lbuf_rxdata) { \
total_pool_pktid_count++; \
} \
} while (0)
#define TOTAL_POOL_PKTID_CNT_DEC(pktp) \
do { \
if (pktp->type != lbuf_rxdata) { \
ASSERT(total_pool_pktid_count > 0); \
total_pool_pktid_count--; \
} \
} while (0)
#else
#define TOTAL_POOL_PKTID_CNT_INC(pktp)
#define TOTAL_POOL_PKTID_CNT_DEC(pktp)
#endif /* BCMPKTIDMAP || BCMPKTIDMAP_MIN */
#ifdef POOL_HEAP_RECONFIG
static uint32 pktpool_add_fail_cnt = 0; /* Counter if pktpool_add fails */
typedef struct pktpool_heap_cb_reg {
pktpool_heap_cb_t fn;
void *ctxt;
uint32 flag;
} pktpool_heap_cb_reg_t;
#define PKTPOOL_MAX_HEAP_CB 2
pktpool_heap_cb_reg_t pktpool_heap_cb_reg[PKTPOOL_MAX_HEAP_CB];
uint32 pktpool_heap_rel_active = 0U;
static void hnd_pktpool_heap_pkt_release(osl_t *osh, pktpool_t *pktp, uint32 flag);
static void hnd_pktpool_heap_pkt_retrieve(pktpool_t *pktp, uint32 flag);
static int hnd_pktpool_heap_get_cb(uint8 handle, void *ctxt, void *pkt, uint pktsize);
static void hnd_pktpool_lbuf_free_cb(uint8 poolid);
static pktpool_heap_cb_reg_t *BCMRAMFN(hnd_pool_get_cb_registry)(void);
#endif /* POOL_HEAP_RECONFIG */
/* Register/Deregister a pktpool with registry during pktpool_init/deinit */
static int pktpool_register(pktpool_t * poolptr);
static int pktpool_deregister(pktpool_t * poolptr);
/** add declaration */
static void pktpool_avail_notify(pktpool_t *pktp);
#ifdef DONGLEBUILD
#define PKTPOOL_RXLFRAG(pktp) ((pktp)->type == lbuf_rxfrag)
#else
#define PKTPOOL_RXLFRAG(pktp) FALSE
#endif
#define RXLRAG_POOL_FREELIST_TAIL(pktp) ((pktp)->freelist_tail)
#ifdef RXLFRAGPOOL_SORT_RXFRAG_DBG
static const char BCMPOST_TRAP_RODATA(pktpool_deq_print_1)[] =
"\n%d removing buffer from the rxlfrag pool\n";
static const char BCMPOST_TRAP_RODATA(pktpool_enq_print_1)[] =
"\n%d adding non rxfrag buffer to the rxlfrag pool, %p, %p\n";
static const char BCMPOST_TRAP_RODATA(pktpool_enq_print_2)[] =
"\n%d adding rxfrag buffer to the rxlfrag pool, %p, %p\n";
uint32 g_rxlfrag_rxfrag_queued = 0;
uint32 g_rxlfrag_non_rxfrag_queued = 0;
#define RXLFRAG_SORT_DBG_PRINT(args) do { printf args;} while (0)
#define INCR_RXLFRAG_QUEUE_RXFRAG() do { g_rxlfrag_rxfrag_queued++; } while (0)
#define INCR_RXLFRAG_QUEUE_NONRXFRAG() do { g_rxlfrag_non_rxfrag_queued++; } while (0)
#else
#define RXLFRAG_SORT_DBG_PRINT(args) do { } while (0)
#define INCR_RXLFRAG_QUEUE_RXFRAG() do { } while (0)
#define INCR_RXLFRAG_QUEUE_NONRXFRAG() do { } while (0)
#endif /* RXLFRAGPOOL_SORT_RXFRAG_DBG */
/** accessor functions required when ROMming this file, forced into RAM */
pktpool_t *
BCMPOSTTRAPRAMFN(get_pktpools_registry)(int id)
{
return pktpools_registry[id];
}
static void
BCMRAMFN(pktpool_registry_set)(uint id, pktpool_t *pp)
{
pktpools_registry[id] = pp;
}
static bool
BCMRAMFN(pktpool_registry_cmp)(uint id, pktpool_t *pp)
{
return pktpools_registry[id] == pp;
}
/** Constructs a pool registry to serve a maximum of total_pools */
int
pktpool_attach(osl_t *osh, uint32 total_pools)
{
uint32 poolid;
BCM_REFERENCE(osh);
if (pktpools_max != 0U) {
return BCME_ERROR;
}
ASSERT(total_pools <= PKTPOOL_MAXIMUM_ID);
/* Initialize registry: reserve slot#0 and tag others as free */
PKTPOOL_REGISTRY_RSV(); /* reserve slot#0 */
PKTPOOL_REGISTRY_FOREACH(poolid) { /* tag all unreserved entries as free */
PKTPOOL_REGISTRY_CLR(poolid);
}
pktpools_max = total_pools;
return (int)pktpools_max;
}
/** Destructs the pool registry. Ascertain all pools were first de-inited */
int
pktpool_dettach(osl_t *osh)
{
uint32 poolid;
BCM_REFERENCE(osh);
if (pktpools_max == 0U) {
return BCME_OK;
}
/* Ascertain that no pools are still registered */
ASSERT(PKTPOOL_REGISTRY_ISRSVD()); /* assert reserved slot */
PKTPOOL_REGISTRY_FOREACH(poolid) { /* ascertain all others are free */
ASSERT(PKTPOOL_REGISTRY_ISCLR(poolid));
}
pktpools_max = 0U; /* restore boot state */
return BCME_OK;
}
/** Registers a pool in a free slot; returns the registry slot index */
static int
pktpool_register(pktpool_t * poolptr)
{
uint32 poolid;
if (pktpools_max == 0U) {
return PKTPOOL_INVALID_ID; /* registry has not yet been constructed */
}
ASSERT(pktpools_max != 0U);
/* find an empty slot in pktpools_registry */
PKTPOOL_REGISTRY_FOREACH(poolid) {
if (PKTPOOL_REGISTRY_ISCLR(poolid)) {
PKTPOOL_REGISTRY_SET(poolid, POOLPTR(poolptr)); /* register pool */
return (int)poolid; /* return pool ID */
}
} /* FOREACH */
return PKTPOOL_INVALID_ID; /* error: registry is full */
}
/** Deregisters a pktpool, given the pool pointer; tag slot as free */
static int
pktpool_deregister(pktpool_t * poolptr)
{
uint32 poolid;
ASSERT(POOLPTR(poolptr) != POOLPTR(NULL));
poolid = POOLID(poolptr);
ASSERT(poolid <= pktpools_max);
/* Asertain that a previously registered poolptr is being de-registered */
if (PKTPOOL_REGISTRY_CMP(poolid, POOLPTR(poolptr))) {
PKTPOOL_REGISTRY_CLR(poolid); /* mark as free */
} else {
ASSERT(0);
return BCME_ERROR; /* mismatch in registry */
}
return BCME_OK;
}
/**
* pktpool_init:
* User provides a pktpool_t structure and specifies the number of packets to
* be pre-filled into the pool (n_pkts).
* pktpool_init first attempts to register the pool and fetch a unique poolid.
* If registration fails, it is considered an BCME_ERR, caused by either the
* registry was not pre-created (pktpool_attach) or the registry is full.
* If registration succeeds, then the requested number of packets will be filled
* into the pool as part of initialization. In the event that there is no
* available memory to service the request, then BCME_NOMEM will be returned
* along with the count of how many packets were successfully allocated.
* In dongle builds, prior to memory reclaimation, one should limit the number
* of packets to be allocated during pktpool_init and fill the pool up after
* reclaim stage.
*
* @param n_pkts Number of packets to be pre-filled into the pool
* @param max_pkt_bytes The size of all packets in a pool must be the same. E.g. PKTBUFSZ.
* @param type e.g. 'lbuf_frag'
*/
int
pktpool_init(osl_t *osh,
pktpool_t *pktp,
int *n_pkts,
int max_pkt_bytes,
bool istx,
uint8 type,
bool is_heap_pool,
uint32 heap_pool_flag,
uint16 min_backup_buf)
{
int i, err = BCME_OK;
int pktplen;
uint8 pktp_id;
#ifdef BCMPKTPOOL
uint lbufsz;
uint32 flags;
#endif
ASSERT(pktp != NULL);
ASSERT(osh != NULL);
ASSERT(n_pkts != NULL);
pktplen = *n_pkts;
/* assign a unique pktpool id */
if ((pktp_id = (uint8) pktpool_register(pktp)) == PKTPOOL_INVALID_ID) {
return BCME_ERROR;
}
POOLSETID(pktp, pktp_id);
pktp->inited = TRUE;
pktp->istx = istx ? TRUE : FALSE;
pktp->max_pkt_bytes = (uint16)max_pkt_bytes;
pktp->type = type;
#ifdef BCMPKTPOOL
flags = 0;
lbufsz = 0;
set_lbuf_params(type, &flags, &lbufsz);
/* Size of tx/rx metadata */
pktp->metasz = lb_get_metasz(type);
/* Offset of tx/rx metadata from lbuf start */
pktp->metaoff = lbufsz - pktp->metasz;
#else
pktp->metasz = 0u;
pktp->metaoff = 0u;
#endif /* BCMPKTPOOL */
#ifdef POOL_HEAP_RECONFIG
pktp->poolheap_flag = heap_pool_flag;
pktp->poolheap_count = 0;
pktp->min_backup_buf = min_backup_buf;
if (is_heap_pool) {
if (rte_freelist_mgr_register(&pktp->mem_handle,
hnd_pktpool_heap_get_cb,
lb_get_pktalloclen(type, max_pkt_bytes),
pktp) != BCME_OK) {
return BCME_ERROR;
}
}
pktp->is_heap_pool = is_heap_pool;
#endif
if (HND_PKTPOOL_MUTEX_CREATE("pktpool", &pktp->mutex) != OSL_EXT_SUCCESS) {
return BCME_ERROR;
}
pktp->maxlen = PKTPOOL_LEN_MAX;
pktplen = LIMIT_TO_MAX(pktplen, pktp->maxlen);
for (i = 0; i < pktplen; i++) {
void *p;
#ifdef _RTE_
/* For rte builds, use PKTALLOC rather than PKTGET
* Avoid same pkts being dequed and enqued to pool
* when allocation fails.
*/
p = PKTALLOC(osh, max_pkt_bytes, type);
#else
p = PKTGET(osh, max_pkt_bytes, TRUE);
#endif
if (p == NULL) {
/* Not able to allocate all requested pkts
* so just return what was actually allocated
* We can add to the pool later
*/
if (pktp->freelist == NULL) /* pktpool free list is empty */
err = BCME_NOMEM;
goto exit;
}
PKTSETPOOL(osh, p, TRUE, pktp); /* Tag packet with pool ID */
PKTSETFREELIST(p, pktp->freelist); /* insert p at head of free list */
pktp->freelist = p;
if (PKTPOOL_RXLFRAG(pktp) && (pktp->pktpool_flags & PKTPOOL_RXLFRAG_SORTED_INSERT))
{
if (RXLRAG_POOL_FREELIST_TAIL(pktp) == NULL) {
RXLRAG_POOL_FREELIST_TAIL(pktp) = pktp->freelist;
}
}
pktp->avail++;
TOTAL_POOL_PKTID_CNT_DEC(pktp);
#ifdef BCMDBG_POOL
pktp->dbg_q[pktp->dbg_qlen++].p = p;
#endif
}
exit:
pktp->n_pkts = pktp->avail;
*n_pkts = pktp->n_pkts; /* number of packets managed by pool */
return err;
} /* pktpool_init */
/**
* pktpool_deinit:
* Prior to freeing a pktpool, all packets must be first freed into the pktpool.
* Upon pktpool_deinit, all packets in the free pool will be freed to the heap.
* An assert is in place to ensure that there are no packets still lingering
* around. Packets freed to a pool after the deinit will cause a memory
* corruption as the pktpool_t structure no longer exists.
*/
int
pktpool_deinit(osl_t *osh, pktpool_t *pktp)
{
uint16 freed = 0;
ASSERT(osh != NULL);
ASSERT(pktp != NULL);
#ifdef BCMDBG_POOL
{
int i;
for (i = 0; i <= pktp->n_pkts; i++) {
pktp->dbg_q[i].p = NULL;
}
}
#endif
while (pktp->freelist != NULL) {
void * p = pktp->freelist;
pktp->freelist = PKTFREELIST(p); /* unlink head packet from free list */
PKTSETFREELIST(p, NULL);
PKTSETPOOL(osh, p, FALSE, NULL); /* clear pool ID tag in pkt */
TOTAL_POOL_PKTID_CNT_INC(pktp);
PKTFREE(osh, p, pktp->istx); /* free the packet */
freed++;
ASSERT(freed <= pktp->n_pkts);
}
pktp->avail -= freed;
ASSERT(pktp->avail == 0);
pktp->n_pkts -= freed;
pktpool_deregister(pktp); /* release previously acquired unique pool id */
POOLSETID(pktp, PKTPOOL_INVALID_ID);
if (HND_PKTPOOL_MUTEX_DELETE(&pktp->mutex) != OSL_EXT_SUCCESS)
return BCME_ERROR;
pktp->inited = FALSE;
/* Are there still pending pkts? */
ASSERT(pktp->n_pkts == 0);
return 0;
}
int
pktpool_fill(osl_t *osh, pktpool_t *pktp, bool minimal)
{
void *p;
int err = 0;
int n_pkts, psize, maxlen;
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_ACQUIRE(&pktp->mutex, OSL_EXT_TIME_FOREVER) != OSL_EXT_SUCCESS)
return BCME_ERROR;
#if defined(BCMRXDATAPOOL) || defined(URB)
if (BCMRXDATAPOOL_ENAB() || URB_ENAB()) {
ASSERT((pktp->max_pkt_bytes != 0) || (pktp->type == lbuf_rxfrag));
} else
#endif /* BCMRXDATAPOOL || URB */
{
ASSERT(pktp->max_pkt_bytes != 0);
}
maxlen = pktp->maxlen;
psize = minimal ? (maxlen >> 2) : maxlen;
n_pkts = (int)pktp->n_pkts;
#ifdef POOL_HEAP_RECONFIG
/*
* Consider the packets released to freelist mgr also
* as part of pool size
*/
n_pkts += pktp->is_heap_pool ?
pktp->poolheap_count : 0;
#endif
for (; n_pkts < psize; n_pkts++) {
#ifdef _RTE_
/* For rte builds, use PKTALLOC rather than PKTGET
* Avoid same pkts being dequed and enqued to pool when allocation fails.
* All pkts in pool have same length.
*/
p = PKTALLOC(osh, pktp->max_pkt_bytes, pktp->type);
#else
p = PKTGET(osh, pktp->n_pkts, TRUE);
#endif
if (p == NULL) {
err = BCME_NOMEM;
break;
}
if (pktpool_add(pktp, p) != BCME_OK) {
PKTFREE(osh, p, FALSE);
err = BCME_ERROR;
break;
}
TOTAL_POOL_PKTID_CNT_DEC(pktp);
}
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_RELEASE(&pktp->mutex) != OSL_EXT_SUCCESS)
return BCME_ERROR;
if (pktp->cbcnt) {
if (pktp->empty == FALSE)
pktpool_avail_notify(pktp);
}
return err;
}
#ifdef BCMPOOLRECLAIM
/* New API to decrease the pkts from pool, but not deinit
*/
uint16
pktpool_reclaim(osl_t *osh, pktpool_t *pktp, uint16 free_cnt, uint8 action)
{
uint16 freed = 0;
pktpool_cb_extn_t cb = NULL;
void *arg = NULL;
void *rem_list_head = NULL;
void *rem_list_tail = NULL;
bool dont_free = FALSE;
ASSERT(osh != NULL);
ASSERT(pktp != NULL);
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_ACQUIRE(&pktp->mutex, OSL_EXT_TIME_FOREVER) != OSL_EXT_SUCCESS) {
return freed;
}
if (pktp->avail < free_cnt) {
free_cnt = pktp->avail;
}
if (BCMSPLITRX_ENAB() && (pktp->type == lbuf_rxfrag)) {
/* If pool is shared rx frag pool, use call back fn to reclaim host address
* and Rx cpl ID associated with the pkt.
*/
ASSERT(pktp->cbext.cb != NULL);
cb = pktp->cbext.cb;
arg = pktp->cbext.arg;
} else if ((pktp->type == lbuf_basic) && (pktp->rxcplidfn.cb != NULL)) {
/* If pool is shared rx pool, use call back fn to freeup Rx cpl ID
* associated with the pkt.
*/
cb = pktp->rxcplidfn.cb;
arg = pktp->rxcplidfn.arg;
}
while ((pktp->freelist != NULL) && (free_cnt)) {
void * p = pktp->freelist;
pktp->freelist = PKTFREELIST(p); /* unlink head packet from free list */
PKTSETFREELIST(p, NULL);
dont_free = FALSE;
if (action == FREE_ALL_FRAG_PKTS) {
/* Free lbufs which are marked as frag_free_mem */
if (!PKTISFRMFRAG(p)) {
dont_free = TRUE;
}
}
if (dont_free) {
if (rem_list_head == NULL) {
rem_list_head = p;
} else {
PKTSETFREELIST(rem_list_tail, p);
}
rem_list_tail = p;
continue;
}
if (cb != NULL) {
if (cb(pktp, arg, p, REMOVE_RXCPLID, NULL)) {
PKTSETFREELIST(p, pktp->freelist);
pktp->freelist = p;
break;
}
}
PKTSETPOOL(osh, p, FALSE, NULL); /* clear pool ID tag in pkt */
pktp->avail--;
pktp->n_pkts--;
TOTAL_POOL_PKTID_CNT_INC(pktp);
PKTFREE(osh, p, pktp->istx); /* free the packet */
freed++;
free_cnt--;
}
if (rem_list_head) {
PKTSETFREELIST(rem_list_tail, pktp->freelist);
pktp->freelist = rem_list_head;
}
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_RELEASE(&pktp->mutex) != OSL_EXT_SUCCESS) {
return freed;
}
return freed;
}
#endif /* #ifdef BCMPOOLRECLAIM */
/* New API to empty the pkts from pool, but not deinit
* NOTE: caller is responsible to ensure,
* all pkts are available in pool for free; else LEAK !
*/
int
pktpool_empty(osl_t *osh, pktpool_t *pktp)
{
uint16 freed = 0;
ASSERT(osh != NULL);
ASSERT(pktp != NULL);
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_ACQUIRE(&pktp->mutex, OSL_EXT_TIME_FOREVER) != OSL_EXT_SUCCESS)
return BCME_ERROR;
#ifdef BCMDBG_POOL
{
int i;
for (i = 0; i <= pktp->n_pkts; i++) {
pktp->dbg_q[i].p = NULL;
}
}
#endif
while (pktp->freelist != NULL) {
void * p = pktp->freelist;
pktp->freelist = PKTFREELIST(p); /* unlink head packet from free list */
PKTSETFREELIST(p, NULL);
PKTSETPOOL(osh, p, FALSE, NULL); /* clear pool ID tag in pkt */
TOTAL_POOL_PKTID_CNT_INC(pktp);
PKTFREE(osh, p, pktp->istx); /* free the packet */
freed++;
ASSERT(freed <= pktp->n_pkts);
}
pktp->avail -= freed;
ASSERT(pktp->avail == 0);
pktp->n_pkts -= freed;
ASSERT(pktp->n_pkts == 0);
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_RELEASE(&pktp->mutex) != OSL_EXT_SUCCESS)
return BCME_ERROR;
return 0;
}
int
BCMPOSTTRAPFN(pktpool_avail)(pktpool_t *pktpool)
{
int avail = pktpool->avail;
if (avail == 0) {
pktpool_emptycb_disable(pktpool, FALSE);
}
return avail;
}
static void *
BCMPOSTTRAPFASTPATH(pktpool_deq)(pktpool_t *pktp)
{
void *p = NULL;
if (pktp->avail == 0)
return NULL;
ASSERT_FP(pktp->freelist != NULL);
p = pktp->freelist; /* dequeue packet from head of pktpool free list */
pktp->freelist = PKTFREELIST(p); /* free list points to next packet */
/* rxlfrag list is ordered list rxfrag packets at head followed by non rxfrag packets */
/* rxfrag is packet which already has associated host buffer */
if (PKTPOOL_RXLFRAG(pktp) && (pktp->pktpool_flags & PKTPOOL_RXLFRAG_SORTED_INSERT)) {
RXLFRAG_SORT_DBG_PRINT((pktpool_deq_print_1, pktp->avail));
if (pktp->freelist == NULL) {
RXLRAG_POOL_FREELIST_TAIL(pktp) = NULL;
}
}
#if defined(DONGLEBUILD) && defined(SRMEM)
if (SRMEM_ENAB()) {
PKTSRMEM_INC_INUSE(p);
}
#endif /* DONGLEBUILD && SRMEM */
PKTSETFREELIST(p, NULL);
pktp->avail--;
PKTSETQCALLER(p, pktp, CALL_SITE);
return p;
}
static void
BCMPOSTTRAPFASTPATH(pktpool_enq)(pktpool_t *pktp, void *p)
{
ASSERT_FP(p != NULL);
PKTSETQCALLER(p, pktp, CALL_SITE);
if (PKTPOOL_RXLFRAG(pktp) && (pktp->pktpool_flags & PKTPOOL_RXLFRAG_SORTED_INSERT)) {
/* rxfrag pkt becomes head of freelist, non rxfrag becomes tail of the freelist */
/* rxfrag is packet which already has associated host buffer */
if (PKTISRXFRAG(OSH_NULL, p)) {
INCR_RXLFRAG_QUEUE_RXFRAG();
RXLFRAG_SORT_DBG_PRINT((pktpool_enq_print_2, pktp->avail,
pktp->freelist, RXLRAG_POOL_FREELIST_TAIL(pktp)));
PKTSETFREELIST(p, pktp->freelist); /* insert at head of pktpool free list */
if (RXLRAG_POOL_FREELIST_TAIL(pktp) == NULL) {
RXLRAG_POOL_FREELIST_TAIL(pktp) = p;
}
pktp->freelist = p; /* free list points to newly inserted packet */
} else {
INCR_RXLFRAG_QUEUE_NONRXFRAG();
RXLFRAG_SORT_DBG_PRINT((pktpool_enq_print_1, pktp->avail,
pktp->freelist, RXLRAG_POOL_FREELIST_TAIL(pktp)));
if (pktp->freelist == NULL) {
pktp->freelist = p;
RXLRAG_POOL_FREELIST_TAIL(pktp) = p;
}
else {
/* this pkt becomes tail */
PKTSETFREELIST(RXLRAG_POOL_FREELIST_TAIL(pktp), p);
RXLRAG_POOL_FREELIST_TAIL(pktp) = p;
PKTSETFREELIST(p, NULL);
}
}
}
else {
PKTSETFREELIST(p, pktp->freelist); /* insert at head of pktpool free list */
pktp->freelist = p; /* free list points to newly inserted packet */
}
#if defined(DONGLEBUILD) && defined(SRMEM)
if (SRMEM_ENAB()) {
PKTSRMEM_DEC_INUSE(p);
}
#endif /* DONGLEBUILD && SRMEM */
pktp->avail++;
ASSERT_FP(pktp->avail <= pktp->n_pkts);
}
/** utility for registering host addr fill function called from pciedev */
int
pktpool_hostaddr_fill_register(pktpool_t *pktp, pktpool_cb_extn_t cb, void *arg)
{
ASSERT(cb != NULL);
ASSERT(pktp->cbext.cb == NULL);
pktp->cbext.cb = cb;
pktp->cbext.arg = arg;
return 0;
}
int
pktpool_rxcplid_fill_register(pktpool_t *pktp, pktpool_cb_extn_t cb, void *arg)
{
ASSERT(cb != NULL);
if (pktp == NULL)
return BCME_ERROR;
ASSERT(pktp->rxcplidfn.cb == NULL);
pktp->rxcplidfn.cb = cb;
pktp->rxcplidfn.arg = arg;
return 0;
}
/** whenever host posts rxbuffer, invoke dma_rxfill from pciedev layer */
void
pktpool_invoke_dmarxfill(pktpool_t *pktp)
{
ASSERT(pktp->dmarxfill.cb);
ASSERT(pktp->dmarxfill.arg);
if (pktp->dmarxfill.cb)
pktp->dmarxfill.cb(pktp, pktp->dmarxfill.arg);
}
/** Registers callback functions for split rx mode */
int
pkpool_haddr_avail_register_cb(pktpool_t *pktp, pktpool_cb_t cb, void *arg)
{
ASSERT(cb != NULL);
pktp->dmarxfill.cb = cb;
pktp->dmarxfill.arg = arg;
return 0;
}
#ifdef URB
/**
* @brief API to register the callback which gets invoked when Rx data buffer
* which is part of URB memory is freed while rxfrag is being freed.
* It is used to increment rd_ptr of URB HW in mac.
*
* @param[in] pktp Pointer to rxfrag pool
* @param[in] cb Callback function that needs to get registered
* @param[in] arg Argument that is passed to this callback while invocation
*
* @return Returns error status
*/
int
pkpool_rxurb_register_cb(pktpool_t *pktp, pktpool_rxurb_cb_t cb, void *arg)
{
ASSERT(cb != NULL);
pktp->dmarxurb.cb = cb;
pktp->dmarxurb.arg = arg;
return BCME_OK;
}
/**
* @brief API to register the callback which gets invoked during rxfrag free
* to remove rxcplid and host buffer is not used up in rxpath and during rxfill
* to post host buffers to dma rx fifo0
*
* @param[in] pktp Pointer to rxfrag pool
* @param[in] cb Callback function that needs to get registered
* @param[in] arg Argument that is passed to this callback while invocation
*
* @return Returns error status
*/
int
pktpool_hostaddr_ext_fill_register(pktpool_t *pktp, pktpool_cb_extn_t cb, void *arg)
{
ASSERT(cb != NULL);
ASSERT(pktp->cb_haddr.cb == NULL);
pktp->cb_haddr.cb = cb;
pktp->cb_haddr.arg = arg;
return BCME_OK;
}
#endif /* URB */
/**
* Registers callback functions.
* No BCMATTACHFN as it is used in xdc_enable_ep which is not an attach function
*/
int
pktpool_avail_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg)
{
int err = 0;
int i;
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_ACQUIRE(&pktp->mutex, OSL_EXT_TIME_FOREVER) != OSL_EXT_SUCCESS)
return BCME_ERROR;
ASSERT(cb != NULL);
for (i = 0; i < pktp->cbcnt; i++) {
ASSERT(pktp->cbs[i].cb != NULL);
if ((cb == pktp->cbs[i].cb) && (arg == pktp->cbs[i].arg)) {
pktp->cbs[i].refcnt++;
goto done;
}
}
i = pktp->cbcnt;
if (i == PKTPOOL_CB_MAX_AVL) {
err = BCME_ERROR;
goto done;
}
ASSERT(pktp->cbs[i].cb == NULL);
pktp->cbs[i].cb = cb;
pktp->cbs[i].arg = arg;
pktp->cbs[i].refcnt++;
pktp->cbcnt++;
/* force enable empty callback */
pktpool_emptycb_disable(pktp, FALSE);
done:
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_RELEASE(&pktp->mutex) != OSL_EXT_SUCCESS)
return BCME_ERROR;
return err;
}
/* No BCMATTACHFN as it is used in a non-attach function */
int
pktpool_avail_deregister(pktpool_t *pktp, pktpool_cb_t cb, void *arg)
{
int err = 0;
int i, k;
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_ACQUIRE(&pktp->mutex, OSL_EXT_TIME_FOREVER) != OSL_EXT_SUCCESS) {
return BCME_ERROR;
}
ASSERT(cb != NULL);
for (i = 0; i < pktp->cbcnt; i++) {
ASSERT(pktp->cbs[i].cb != NULL);
if ((cb == pktp->cbs[i].cb) && (arg == pktp->cbs[i].arg)) {
pktp->cbs[i].refcnt--;
if (pktp->cbs[i].refcnt) {
/* Still there are references to this callback */
goto done;
}
/* Moving any more callbacks to fill the hole */
for (k = i+1; k < pktp->cbcnt; i++, k++) {
pktp->cbs[i].cb = pktp->cbs[k].cb;
pktp->cbs[i].arg = pktp->cbs[k].arg;
pktp->cbs[i].refcnt = pktp->cbs[k].refcnt;
}
/* reset the last callback */
pktp->cbs[i].cb = NULL;
pktp->cbs[i].arg = NULL;
pktp->cbs[i].refcnt = 0;
pktp->cbcnt--;
goto done;
}
}
done:
/* protect shared resource */
if (HND_PKTPOOL_MUTEX_RELEASE(&pktp->mutex) != OSL_EXT_SUCCESS) {
return BCME_ERROR;
}
return err;
}
/** Registers callback functions */
int
pktpool_empty_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg)
{
int err = 0;
int i;
/* protect shared resource */