net/bnxt: fix Rx handling and buffer allocation logic
[dpdk.git] / drivers / net / bnxt / bnxt_rxr.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35 #include <stdbool.h>
36
37 #include <rte_bitmap.h>
38 #include <rte_byteorder.h>
39 #include <rte_malloc.h>
40 #include <rte_memory.h>
41
42 #include "bnxt.h"
43 #include "bnxt_cpr.h"
44 #include "bnxt_ring.h"
45 #include "bnxt_rxr.h"
46 #include "bnxt_rxq.h"
47 #include "hsi_struct_def_dpdk.h"
48
49 /*
50  * RX Ring handling
51  */
52
53 static inline struct rte_mbuf *__bnxt_alloc_rx_data(struct rte_mempool *mb)
54 {
55         struct rte_mbuf *data;
56
57         data = rte_mbuf_raw_alloc(mb);
58
59         return data;
60 }
61
62 static inline int bnxt_alloc_rx_data(struct bnxt_rx_queue *rxq,
63                                      struct bnxt_rx_ring_info *rxr,
64                                      uint16_t prod)
65 {
66         struct rx_prod_pkt_bd *rxbd = &rxr->rx_desc_ring[prod];
67         struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
68         struct rte_mbuf *data;
69
70         data = __bnxt_alloc_rx_data(rxq->mb_pool);
71         if (!data) {
72                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
73                 return -ENOMEM;
74         }
75
76         rx_buf->mbuf = data;
77
78         rxbd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(rx_buf->mbuf));
79
80         return 0;
81 }
82
83 static inline int bnxt_alloc_ag_data(struct bnxt_rx_queue *rxq,
84                                      struct bnxt_rx_ring_info *rxr,
85                                      uint16_t prod)
86 {
87         struct rx_prod_pkt_bd *rxbd = &rxr->ag_desc_ring[prod];
88         struct bnxt_sw_rx_bd *rx_buf = &rxr->ag_buf_ring[prod];
89         struct rte_mbuf *data;
90
91         data = __bnxt_alloc_rx_data(rxq->mb_pool);
92         if (!data) {
93                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
94                 return -ENOMEM;
95         }
96
97         if (rxbd == NULL)
98                 RTE_LOG(ERR, PMD, "Jumbo Frame. rxbd is NULL\n");
99         if (rx_buf == NULL)
100                 RTE_LOG(ERR, PMD, "Jumbo Frame. rx_buf is NULL\n");
101
102
103         rx_buf->mbuf = data;
104
105         rxbd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(rx_buf->mbuf));
106
107         return 0;
108 }
109
110 static inline void bnxt_reuse_rx_mbuf(struct bnxt_rx_ring_info *rxr,
111                                struct rte_mbuf *mbuf)
112 {
113         uint16_t prod = RING_NEXT(rxr->rx_ring_struct, rxr->rx_prod);
114         struct bnxt_sw_rx_bd *prod_rx_buf;
115         struct rx_prod_pkt_bd *prod_bd;
116
117         prod_rx_buf = &rxr->rx_buf_ring[prod];
118
119         RTE_ASSERT(prod_rx_buf->mbuf == NULL);
120         RTE_ASSERT(mbuf != NULL);
121
122         prod_rx_buf->mbuf = mbuf;
123
124         prod_bd = &rxr->rx_desc_ring[prod];
125
126         prod_bd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(mbuf));
127
128         rxr->rx_prod = prod;
129 }
130
131 #ifdef BNXT_DEBUG
132 static void bnxt_reuse_ag_mbuf(struct bnxt_rx_ring_info *rxr, uint16_t cons,
133                                struct rte_mbuf *mbuf)
134 {
135         uint16_t prod = rxr->ag_prod;
136         struct bnxt_sw_rx_bd *prod_rx_buf;
137         struct rx_prod_pkt_bd *prod_bd, *cons_bd;
138
139         prod_rx_buf = &rxr->ag_buf_ring[prod];
140
141         prod_rx_buf->mbuf = mbuf;
142
143         prod_bd = &rxr->ag_desc_ring[prod];
144         cons_bd = &rxr->ag_desc_ring[cons];
145
146         prod_bd->addr = cons_bd->addr;
147 }
148 #endif
149
150 static inline
151 struct rte_mbuf *bnxt_consume_rx_buf(struct bnxt_rx_ring_info *rxr,
152                                      uint16_t cons)
153 {
154         struct bnxt_sw_rx_bd *cons_rx_buf;
155         struct rte_mbuf *mbuf;
156
157         cons_rx_buf = &rxr->rx_buf_ring[cons];
158         RTE_ASSERT(cons_rx_buf->mbuf != NULL);
159         mbuf = cons_rx_buf->mbuf;
160         cons_rx_buf->mbuf = NULL;
161         return mbuf;
162 }
163
164 static void bnxt_tpa_start(struct bnxt_rx_queue *rxq,
165                            struct rx_tpa_start_cmpl *tpa_start,
166                            struct rx_tpa_start_cmpl_hi *tpa_start1)
167 {
168         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
169         uint8_t agg_id = rte_le_to_cpu_32(tpa_start->agg_id &
170                 RX_TPA_START_CMPL_AGG_ID_MASK) >> RX_TPA_START_CMPL_AGG_ID_SFT;
171         uint16_t data_cons;
172         struct bnxt_tpa_info *tpa_info;
173         struct rte_mbuf *mbuf;
174
175         data_cons = tpa_start->opaque;
176         tpa_info = &rxr->tpa_info[agg_id];
177
178         mbuf = bnxt_consume_rx_buf(rxr, data_cons);
179
180         bnxt_reuse_rx_mbuf(rxr, tpa_info->mbuf);
181
182         tpa_info->mbuf = mbuf;
183         tpa_info->len = rte_le_to_cpu_32(tpa_start->len);
184
185         mbuf->nb_segs = 1;
186         mbuf->next = NULL;
187         mbuf->pkt_len = rte_le_to_cpu_32(tpa_start->len);
188         mbuf->data_len = mbuf->pkt_len;
189         mbuf->port = rxq->port_id;
190         mbuf->ol_flags = PKT_RX_LRO;
191         if (likely(tpa_start->flags_type &
192                    rte_cpu_to_le_32(RX_TPA_START_CMPL_FLAGS_RSS_VALID))) {
193                 mbuf->hash.rss = rte_le_to_cpu_32(tpa_start->rss_hash);
194                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
195         } else {
196                 mbuf->hash.fdir.id = rte_le_to_cpu_16(tpa_start1->cfa_code);
197                 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
198         }
199         if (tpa_start1->flags2 &
200             rte_cpu_to_le_32(RX_TPA_START_CMPL_FLAGS2_META_FORMAT_VLAN)) {
201                 mbuf->vlan_tci = rte_le_to_cpu_32(tpa_start1->metadata);
202                 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
203         }
204         if (likely(tpa_start1->flags2 &
205                    rte_cpu_to_le_32(RX_TPA_START_CMPL_FLAGS2_L4_CS_CALC)))
206                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
207
208         /* recycle next mbuf */
209         data_cons = RING_NEXT(rxr->rx_ring_struct, data_cons);
210         bnxt_reuse_rx_mbuf(rxr, bnxt_consume_rx_buf(rxr, data_cons));
211 }
212
213 static int bnxt_agg_bufs_valid(struct bnxt_cp_ring_info *cpr,
214                 uint8_t agg_bufs, uint32_t raw_cp_cons)
215 {
216         uint16_t last_cp_cons;
217         struct rx_pkt_cmpl *agg_cmpl;
218
219         raw_cp_cons = ADV_RAW_CMP(raw_cp_cons, agg_bufs);
220         last_cp_cons = RING_CMP(cpr->cp_ring_struct, raw_cp_cons);
221         agg_cmpl = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[last_cp_cons];
222         return CMP_VALID(agg_cmpl, raw_cp_cons, cpr->cp_ring_struct);
223 }
224
225 /* TPA consume agg buffer out of order, allocate connected data only */
226 static int bnxt_prod_ag_mbuf(struct bnxt_rx_queue *rxq)
227 {
228         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
229         uint16_t next = RING_NEXT(rxr->ag_ring_struct, rxr->ag_prod);
230
231         /* TODO batch allocation for better performance */
232         while (rte_bitmap_get(rxr->ag_bitmap, next)) {
233                 if (unlikely(bnxt_alloc_ag_data(rxq, rxr, next))) {
234                         RTE_LOG(ERR, PMD,
235                                 "agg mbuf alloc failed: prod=0x%x\n", next);
236                         break;
237                 }
238                 rte_bitmap_clear(rxr->ag_bitmap, next);
239                 rxr->ag_prod = next;
240                 next = RING_NEXT(rxr->ag_ring_struct, next);
241         }
242
243         return 0;
244 }
245
246 static int bnxt_rx_pages(struct bnxt_rx_queue *rxq,
247                          struct rte_mbuf *mbuf, uint32_t *tmp_raw_cons,
248                          uint8_t agg_buf)
249 {
250         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
251         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
252         int i;
253         uint16_t cp_cons, ag_cons;
254         struct rx_pkt_cmpl *rxcmp;
255         struct rte_mbuf *last = mbuf;
256
257         for (i = 0; i < agg_buf; i++) {
258                 struct bnxt_sw_rx_bd *ag_buf;
259                 struct rte_mbuf *ag_mbuf;
260                 *tmp_raw_cons = NEXT_RAW_CMP(*tmp_raw_cons);
261                 cp_cons = RING_CMP(cpr->cp_ring_struct, *tmp_raw_cons);
262                 rxcmp = (struct rx_pkt_cmpl *)
263                                         &cpr->cp_desc_ring[cp_cons];
264
265 #ifdef BNXT_DEBUG
266                 bnxt_dump_cmpl(cp_cons, rxcmp);
267 #endif
268
269                 ag_cons = rxcmp->opaque;
270                 RTE_ASSERT(ag_cons <= rxr->ag_ring_struct->ring_mask);
271                 ag_buf = &rxr->ag_buf_ring[ag_cons];
272                 ag_mbuf = ag_buf->mbuf;
273                 RTE_ASSERT(ag_mbuf != NULL);
274
275                 ag_mbuf->data_len = rte_le_to_cpu_16(rxcmp->len);
276
277                 mbuf->nb_segs++;
278                 mbuf->pkt_len += ag_mbuf->data_len;
279
280                 last->next = ag_mbuf;
281                 last = ag_mbuf;
282
283                 ag_buf->mbuf = NULL;
284
285                 /*
286                  * As aggregation buffer consumed out of order in TPA module,
287                  * use bitmap to track freed slots to be allocated and notified
288                  * to NIC
289                  */
290                 rte_bitmap_set(rxr->ag_bitmap, ag_cons);
291         }
292         bnxt_prod_ag_mbuf(rxq);
293         return 0;
294 }
295
296 static inline struct rte_mbuf *bnxt_tpa_end(
297                 struct bnxt_rx_queue *rxq,
298                 uint32_t *raw_cp_cons,
299                 struct rx_tpa_end_cmpl *tpa_end,
300                 struct rx_tpa_end_cmpl_hi *tpa_end1 __rte_unused)
301 {
302         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
303         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
304         uint8_t agg_id = (tpa_end->agg_id & RX_TPA_END_CMPL_AGG_ID_MASK)
305                         >> RX_TPA_END_CMPL_AGG_ID_SFT;
306         struct rte_mbuf *mbuf;
307         uint8_t agg_bufs;
308         struct bnxt_tpa_info *tpa_info;
309
310         tpa_info = &rxr->tpa_info[agg_id];
311         mbuf = tpa_info->mbuf;
312         RTE_ASSERT(mbuf != NULL);
313
314         rte_prefetch0(mbuf);
315         agg_bufs = (rte_le_to_cpu_32(tpa_end->agg_bufs_v1) &
316                 RX_TPA_END_CMPL_AGG_BUFS_MASK) >> RX_TPA_END_CMPL_AGG_BUFS_SFT;
317         if (agg_bufs) {
318                 if (!bnxt_agg_bufs_valid(cpr, agg_bufs, *raw_cp_cons))
319                         return NULL;
320                 bnxt_rx_pages(rxq, mbuf, raw_cp_cons, agg_bufs);
321         }
322         mbuf->l4_len = tpa_end->payload_offset;
323
324         struct rte_mbuf *new_data = __bnxt_alloc_rx_data(rxq->mb_pool);
325         RTE_ASSERT(new_data != NULL);
326         if (!new_data) {
327                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
328                 return NULL;
329         }
330         tpa_info->mbuf = new_data;
331
332         return mbuf;
333 }
334
335 static int bnxt_rx_pkt(struct rte_mbuf **rx_pkt,
336                             struct bnxt_rx_queue *rxq, uint32_t *raw_cons)
337 {
338         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
339         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
340         struct rx_pkt_cmpl *rxcmp;
341         struct rx_pkt_cmpl_hi *rxcmp1;
342         uint32_t tmp_raw_cons = *raw_cons;
343         uint16_t cons, prod, cp_cons =
344             RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
345 #ifdef BNXT_DEBUG
346         uint16_t ag_cons;
347 #endif
348         struct rte_mbuf *mbuf;
349         int rc = 0;
350         uint8_t agg_buf = 0;
351         uint16_t cmp_type;
352
353         rxcmp = (struct rx_pkt_cmpl *)
354             &cpr->cp_desc_ring[cp_cons];
355
356         tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
357         cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
358         rxcmp1 = (struct rx_pkt_cmpl_hi *)&cpr->cp_desc_ring[cp_cons];
359
360         if (!CMP_VALID(rxcmp1, tmp_raw_cons, cpr->cp_ring_struct))
361                 return -EBUSY;
362
363         cmp_type = CMP_TYPE(rxcmp);
364         if (cmp_type == RX_PKT_CMPL_TYPE_RX_L2_TPA_START) {
365                 bnxt_tpa_start(rxq, (struct rx_tpa_start_cmpl *)rxcmp,
366                                (struct rx_tpa_start_cmpl_hi *)rxcmp1);
367                 rc = -EINVAL; /* Continue w/o new mbuf */
368                 goto next_rx;
369         } else if (cmp_type == RX_PKT_CMPL_TYPE_RX_L2_TPA_END) {
370                 mbuf = bnxt_tpa_end(rxq, &tmp_raw_cons,
371                                    (struct rx_tpa_end_cmpl *)rxcmp,
372                                    (struct rx_tpa_end_cmpl_hi *)rxcmp1);
373                 if (unlikely(!mbuf))
374                         return -EBUSY;
375                 *rx_pkt = mbuf;
376                 goto next_rx;
377         } else if (cmp_type != 0x11) {
378                 rc = -EINVAL;
379                 goto next_rx;
380         }
381
382         agg_buf = (rxcmp->agg_bufs_v1 & RX_PKT_CMPL_AGG_BUFS_MASK)
383                         >> RX_PKT_CMPL_AGG_BUFS_SFT;
384         if (agg_buf && !bnxt_agg_bufs_valid(cpr, agg_buf, tmp_raw_cons))
385                 return -EBUSY;
386
387         prod = rxr->rx_prod;
388
389         cons = rxcmp->opaque;
390         mbuf = bnxt_consume_rx_buf(rxr, cons);
391         rte_prefetch0(mbuf);
392
393         if (mbuf == NULL)
394                 return -EBUSY;
395
396         mbuf->nb_segs = 1;
397         mbuf->next = NULL;
398         mbuf->pkt_len = rxcmp->len;
399         mbuf->data_len = mbuf->pkt_len;
400         mbuf->port = rxq->port_id;
401         mbuf->ol_flags = 0;
402         if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID) {
403                 mbuf->hash.rss = rxcmp->rss_hash;
404                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
405         } else {
406                 mbuf->hash.fdir.id = rxcmp1->cfa_code;
407                 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
408         }
409
410         if (agg_buf)
411                 bnxt_rx_pages(rxq, mbuf, &tmp_raw_cons, agg_buf);
412
413         if (rxcmp1->flags2 & RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
414                 mbuf->vlan_tci = rxcmp1->metadata &
415                         (RX_PKT_CMPL_METADATA_VID_MASK |
416                         RX_PKT_CMPL_METADATA_DE |
417                         RX_PKT_CMPL_METADATA_PRI_MASK);
418                 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
419         }
420
421 #ifdef BNXT_DEBUG
422         if (rxcmp1->errors_v2 & RX_CMP_L2_ERRORS) {
423                 /* Re-install the mbuf back to the rx ring */
424                 bnxt_reuse_rx_mbuf(rxr, cons, mbuf);
425                 if (agg_buf)
426                         bnxt_reuse_ag_mbuf(rxr, ag_cons, mbuf);
427
428                 rc = -EIO;
429                 goto next_rx;
430         }
431 #endif
432         /*
433          * TODO: Redesign this....
434          * If the allocation fails, the packet does not get received.
435          * Simply returning this will result in slowly falling behind
436          * on the producer ring buffers.
437          * Instead, "filling up" the producer just before ringing the
438          * doorbell could be a better solution since it will let the
439          * producer ring starve until memory is available again pushing
440          * the drops into hardware and getting them out of the driver
441          * allowing recovery to a full producer ring.
442          *
443          * This could also help with cache usage by preventing per-packet
444          * calls in favour of a tight loop with the same function being called
445          * in it.
446          */
447         prod = RING_NEXT(rxr->rx_ring_struct, prod);
448         if (bnxt_alloc_rx_data(rxq, rxr, prod)) {
449                 RTE_LOG(ERR, PMD, "mbuf alloc failed with prod=0x%x\n", prod);
450                 rc = -ENOMEM;
451                 goto rx;
452         }
453         rxr->rx_prod = prod;
454         /*
455          * All MBUFs are allocated with the same size under DPDK,
456          * no optimization for rx_copy_thresh
457          */
458 rx:
459         *rx_pkt = mbuf;
460
461 next_rx:
462
463         *raw_cons = tmp_raw_cons;
464
465         return rc;
466 }
467
468 uint16_t bnxt_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
469                                uint16_t nb_pkts)
470 {
471         struct bnxt_rx_queue *rxq = rx_queue;
472         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
473         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
474         uint32_t raw_cons = cpr->cp_raw_cons;
475         uint32_t cons;
476         int nb_rx_pkts = 0;
477         struct rx_pkt_cmpl *rxcmp;
478         uint16_t prod = rxr->rx_prod;
479         uint16_t ag_prod = rxr->ag_prod;
480         int rc = 0;
481
482         /* Handle RX burst request */
483         while (1) {
484                 int rc;
485
486                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
487                 rte_prefetch0(&cpr->cp_desc_ring[cons]);
488                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
489
490                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
491                         break;
492
493                 /* TODO: Avoid magic numbers... */
494                 if ((CMP_TYPE(rxcmp) & 0x30) == 0x10) {
495                         rc = bnxt_rx_pkt(&rx_pkts[nb_rx_pkts], rxq, &raw_cons);
496                         if (likely(!rc) || rc == -ENOMEM)
497                                 nb_rx_pkts++;
498                         if (rc == -EBUSY)       /* partial completion */
499                                 break;
500                 }
501                 raw_cons = NEXT_RAW_CMP(raw_cons);
502                 if (nb_rx_pkts == nb_pkts)
503                         break;
504         }
505
506         cpr->cp_raw_cons = raw_cons;
507         if (prod == rxr->rx_prod && ag_prod == rxr->ag_prod) {
508                 /*
509                  * For PMD, there is no need to keep on pushing to REARM
510                  * the doorbell if there are no new completions
511                  */
512                 return nb_rx_pkts;
513         }
514
515         B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
516         B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
517         /* Ring the AGG ring DB */
518         B_RX_DB(rxr->ag_doorbell, rxr->ag_prod);
519
520         /* Attempt to alloc Rx buf in case of a previous allocation failure. */
521         if (rc == -ENOMEM) {
522                 int i;
523
524                 for (i = prod; i <= nb_rx_pkts;
525                         i = RING_NEXT(rxr->rx_ring_struct, i)) {
526                         struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
527
528                         /* Buffer already allocated for this index. */
529                         if (rx_buf->mbuf != NULL)
530                                 continue;
531
532                         /* This slot is empty. Alloc buffer for Rx */
533                         if (!bnxt_alloc_rx_data(rxq, rxr, i)) {
534                                 rxr->rx_prod = i;
535                                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
536                         } else {
537                                 RTE_LOG(ERR, PMD, "Alloc  mbuf failed\n");
538                                 break;
539                         }
540                 }
541         }
542
543         return nb_rx_pkts;
544 }
545
546 void bnxt_free_rx_rings(struct bnxt *bp)
547 {
548         int i;
549
550         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
551                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
552
553                 if (!rxq)
554                         continue;
555
556                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
557                 rte_free(rxq->rx_ring->rx_ring_struct);
558
559                 /* Free the Aggregator ring */
560                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
561                 rte_free(rxq->rx_ring->ag_ring_struct);
562                 rxq->rx_ring->ag_ring_struct = NULL;
563
564                 rte_free(rxq->rx_ring);
565
566                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
567                 rte_free(rxq->cp_ring->cp_ring_struct);
568                 rte_free(rxq->cp_ring);
569
570                 rte_free(rxq);
571                 bp->rx_queues[i] = NULL;
572         }
573 }
574
575 int bnxt_init_rx_ring_struct(struct bnxt_rx_queue *rxq, unsigned int socket_id)
576 {
577         struct bnxt_cp_ring_info *cpr;
578         struct bnxt_rx_ring_info *rxr;
579         struct bnxt_ring *ring;
580
581         rxq->rx_buf_use_size = BNXT_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN +
582                                (2 * VLAN_TAG_SIZE);
583         rxq->rx_buf_size = rxq->rx_buf_use_size + sizeof(struct rte_mbuf);
584
585         rxr = rte_zmalloc_socket("bnxt_rx_ring",
586                                  sizeof(struct bnxt_rx_ring_info),
587                                  RTE_CACHE_LINE_SIZE, socket_id);
588         if (rxr == NULL)
589                 return -ENOMEM;
590         rxq->rx_ring = rxr;
591
592         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
593                                    sizeof(struct bnxt_ring),
594                                    RTE_CACHE_LINE_SIZE, socket_id);
595         if (ring == NULL)
596                 return -ENOMEM;
597         rxr->rx_ring_struct = ring;
598         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
599         ring->ring_mask = ring->ring_size - 1;
600         ring->bd = (void *)rxr->rx_desc_ring;
601         ring->bd_dma = rxr->rx_desc_mapping;
602         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
603         ring->vmem = (void **)&rxr->rx_buf_ring;
604
605         cpr = rte_zmalloc_socket("bnxt_rx_ring",
606                                  sizeof(struct bnxt_cp_ring_info),
607                                  RTE_CACHE_LINE_SIZE, socket_id);
608         if (cpr == NULL)
609                 return -ENOMEM;
610         rxq->cp_ring = cpr;
611
612         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
613                                    sizeof(struct bnxt_ring),
614                                    RTE_CACHE_LINE_SIZE, socket_id);
615         if (ring == NULL)
616                 return -ENOMEM;
617         cpr->cp_ring_struct = ring;
618         ring->ring_size = rte_align32pow2(rxr->rx_ring_struct->ring_size *
619                                           (2 + AGG_RING_SIZE_FACTOR));
620         ring->ring_mask = ring->ring_size - 1;
621         ring->bd = (void *)cpr->cp_desc_ring;
622         ring->bd_dma = cpr->cp_desc_mapping;
623         ring->vmem_size = 0;
624         ring->vmem = NULL;
625
626         /* Allocate Aggregator rings */
627         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
628                                    sizeof(struct bnxt_ring),
629                                    RTE_CACHE_LINE_SIZE, socket_id);
630         if (ring == NULL)
631                 return -ENOMEM;
632         rxr->ag_ring_struct = ring;
633         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc *
634                                           AGG_RING_SIZE_FACTOR);
635         ring->ring_mask = ring->ring_size - 1;
636         ring->bd = (void *)rxr->ag_desc_ring;
637         ring->bd_dma = rxr->ag_desc_mapping;
638         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
639         ring->vmem = (void **)&rxr->ag_buf_ring;
640
641         return 0;
642 }
643
644 static void bnxt_init_rxbds(struct bnxt_ring *ring, uint32_t type,
645                             uint16_t len)
646 {
647         uint32_t j;
648         struct rx_prod_pkt_bd *rx_bd_ring = (struct rx_prod_pkt_bd *)ring->bd;
649
650         if (!rx_bd_ring)
651                 return;
652         for (j = 0; j < ring->ring_size; j++) {
653                 rx_bd_ring[j].flags_type = rte_cpu_to_le_16(type);
654                 rx_bd_ring[j].len = rte_cpu_to_le_16(len);
655                 rx_bd_ring[j].opaque = j;
656         }
657 }
658
659 int bnxt_init_one_rx_ring(struct bnxt_rx_queue *rxq)
660 {
661         struct bnxt_rx_ring_info *rxr;
662         struct bnxt_ring *ring;
663         uint32_t prod, type;
664         unsigned int i;
665         uint16_t size;
666
667         size = rte_pktmbuf_data_room_size(rxq->mb_pool) - RTE_PKTMBUF_HEADROOM;
668         if (rxq->rx_buf_use_size <= size)
669                 size = rxq->rx_buf_use_size;
670
671         type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT;
672
673         rxr = rxq->rx_ring;
674         ring = rxr->rx_ring_struct;
675         bnxt_init_rxbds(ring, type, size);
676
677         prod = rxr->rx_prod;
678         for (i = 0; i < ring->ring_size; i++) {
679                 if (bnxt_alloc_rx_data(rxq, rxr, prod) != 0) {
680                         RTE_LOG(WARNING, PMD,
681                                 "init'ed rx ring %d with %d/%d mbufs only\n",
682                                 rxq->queue_id, i, ring->ring_size);
683                         break;
684                 }
685                 rxr->rx_prod = prod;
686                 prod = RING_NEXT(rxr->rx_ring_struct, prod);
687         }
688         RTE_LOG(DEBUG, PMD, "%s\n", __func__);
689
690         ring = rxr->ag_ring_struct;
691         type = RX_PROD_AGG_BD_TYPE_RX_PROD_AGG;
692         bnxt_init_rxbds(ring, type, size);
693         prod = rxr->ag_prod;
694
695         for (i = 0; i < ring->ring_size; i++) {
696                 if (bnxt_alloc_ag_data(rxq, rxr, prod) != 0) {
697                         RTE_LOG(WARNING, PMD,
698                         "init'ed AG ring %d with %d/%d mbufs only\n",
699                         rxq->queue_id, i, ring->ring_size);
700                         break;
701                 }
702                 rxr->ag_prod = prod;
703                 prod = RING_NEXT(rxr->ag_ring_struct, prod);
704         }
705         RTE_LOG(DEBUG, PMD, "%s AGG Done!\n", __func__);
706
707         if (rxr->tpa_info) {
708                 for (i = 0; i < BNXT_TPA_MAX; i++) {
709                         rxr->tpa_info[i].mbuf =
710                                 __bnxt_alloc_rx_data(rxq->mb_pool);
711                         if (!rxr->tpa_info[i].mbuf) {
712                                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
713                                 return -ENOMEM;
714                         }
715                 }
716         }
717         RTE_LOG(DEBUG, PMD, "%s TPA alloc Done!\n", __func__);
718
719         return 0;
720 }