net/bnxt: fix packet type
[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 *mbuf;
69
70         mbuf = __bnxt_alloc_rx_data(rxq->mb_pool);
71         if (!mbuf) {
72                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
73                 return -ENOMEM;
74         }
75
76         rx_buf->mbuf = mbuf;
77
78         rxbd->addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(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 *mbuf;
90
91         mbuf = __bnxt_alloc_rx_data(rxq->mb_pool);
92         if (!mbuf) {
93                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
94                 return -ENOMEM;
95         }
96
97         if (rxbd == NULL)
98                 PMD_DRV_LOG(ERR, "Jumbo Frame. rxbd is NULL\n");
99         if (rx_buf == NULL)
100                 PMD_DRV_LOG(ERR, "Jumbo Frame. rx_buf is NULL\n");
101
102
103         rx_buf->mbuf = mbuf;
104
105         rxbd->addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(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_iova_default(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;
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         cpr->valid = FLIP_VALID(raw_cp_cons,
223                                 cpr->cp_ring_struct->ring_mask,
224                                 cpr->valid);
225         return CMP_VALID(agg_cmpl, raw_cp_cons, cpr->cp_ring_struct);
226 }
227
228 /* TPA consume agg buffer out of order, allocate connected data only */
229 static int bnxt_prod_ag_mbuf(struct bnxt_rx_queue *rxq)
230 {
231         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
232         uint16_t next = RING_NEXT(rxr->ag_ring_struct, rxr->ag_prod);
233
234         /* TODO batch allocation for better performance */
235         while (rte_bitmap_get(rxr->ag_bitmap, next)) {
236                 if (unlikely(bnxt_alloc_ag_data(rxq, rxr, next))) {
237                         PMD_DRV_LOG(ERR,
238                                 "agg mbuf alloc failed: prod=0x%x\n", next);
239                         break;
240                 }
241                 rte_bitmap_clear(rxr->ag_bitmap, next);
242                 rxr->ag_prod = next;
243                 next = RING_NEXT(rxr->ag_ring_struct, next);
244         }
245
246         return 0;
247 }
248
249 static int bnxt_rx_pages(struct bnxt_rx_queue *rxq,
250                          struct rte_mbuf *mbuf, uint32_t *tmp_raw_cons,
251                          uint8_t agg_buf)
252 {
253         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
254         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
255         int i;
256         uint16_t cp_cons, ag_cons;
257         struct rx_pkt_cmpl *rxcmp;
258         struct rte_mbuf *last = mbuf;
259
260         for (i = 0; i < agg_buf; i++) {
261                 struct bnxt_sw_rx_bd *ag_buf;
262                 struct rte_mbuf *ag_mbuf;
263                 *tmp_raw_cons = NEXT_RAW_CMP(*tmp_raw_cons);
264                 cp_cons = RING_CMP(cpr->cp_ring_struct, *tmp_raw_cons);
265                 rxcmp = (struct rx_pkt_cmpl *)
266                                         &cpr->cp_desc_ring[cp_cons];
267
268 #ifdef BNXT_DEBUG
269                 bnxt_dump_cmpl(cp_cons, rxcmp);
270 #endif
271
272                 ag_cons = rxcmp->opaque;
273                 RTE_ASSERT(ag_cons <= rxr->ag_ring_struct->ring_mask);
274                 ag_buf = &rxr->ag_buf_ring[ag_cons];
275                 ag_mbuf = ag_buf->mbuf;
276                 RTE_ASSERT(ag_mbuf != NULL);
277
278                 ag_mbuf->data_len = rte_le_to_cpu_16(rxcmp->len);
279
280                 mbuf->nb_segs++;
281                 mbuf->pkt_len += ag_mbuf->data_len;
282
283                 last->next = ag_mbuf;
284                 last = ag_mbuf;
285
286                 ag_buf->mbuf = NULL;
287
288                 /*
289                  * As aggregation buffer consumed out of order in TPA module,
290                  * use bitmap to track freed slots to be allocated and notified
291                  * to NIC
292                  */
293                 rte_bitmap_set(rxr->ag_bitmap, ag_cons);
294         }
295         bnxt_prod_ag_mbuf(rxq);
296         return 0;
297 }
298
299 static inline struct rte_mbuf *bnxt_tpa_end(
300                 struct bnxt_rx_queue *rxq,
301                 uint32_t *raw_cp_cons,
302                 struct rx_tpa_end_cmpl *tpa_end,
303                 struct rx_tpa_end_cmpl_hi *tpa_end1 __rte_unused)
304 {
305         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
306         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
307         uint8_t agg_id = (tpa_end->agg_id & RX_TPA_END_CMPL_AGG_ID_MASK)
308                         >> RX_TPA_END_CMPL_AGG_ID_SFT;
309         struct rte_mbuf *mbuf;
310         uint8_t agg_bufs;
311         struct bnxt_tpa_info *tpa_info;
312
313         tpa_info = &rxr->tpa_info[agg_id];
314         mbuf = tpa_info->mbuf;
315         RTE_ASSERT(mbuf != NULL);
316
317         rte_prefetch0(mbuf);
318         agg_bufs = (rte_le_to_cpu_32(tpa_end->agg_bufs_v1) &
319                 RX_TPA_END_CMPL_AGG_BUFS_MASK) >> RX_TPA_END_CMPL_AGG_BUFS_SFT;
320         if (agg_bufs) {
321                 if (!bnxt_agg_bufs_valid(cpr, agg_bufs, *raw_cp_cons))
322                         return NULL;
323                 bnxt_rx_pages(rxq, mbuf, raw_cp_cons, agg_bufs);
324         }
325         mbuf->l4_len = tpa_end->payload_offset;
326
327         struct rte_mbuf *new_data = __bnxt_alloc_rx_data(rxq->mb_pool);
328         RTE_ASSERT(new_data != NULL);
329         if (!new_data) {
330                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
331                 return NULL;
332         }
333         tpa_info->mbuf = new_data;
334
335         return mbuf;
336 }
337
338 static uint32_t
339 bnxt_parse_pkt_type(struct rx_pkt_cmpl *rxcmp, struct rx_pkt_cmpl_hi *rxcmp1)
340 {
341         uint32_t l3, pkt_type = 0;
342         uint32_t t_ipcs = 0, ip6 = 0, vlan = 0;
343         uint32_t flags_type;
344
345         vlan = !!(rxcmp1->flags2 &
346                 rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN));
347         pkt_type |= vlan ? RTE_PTYPE_L2_ETHER_VLAN : RTE_PTYPE_L2_ETHER;
348
349         t_ipcs = !!(rxcmp1->flags2 &
350                 rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS2_T_IP_CS_CALC));
351         ip6 = !!(rxcmp1->flags2 &
352                  rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS2_IP_TYPE));
353
354         flags_type = rxcmp->flags_type &
355                 rte_cpu_to_le_32(RX_PKT_CMPL_FLAGS_ITYPE_MASK);
356
357         if (!t_ipcs && !ip6)
358                 l3 = RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
359         else if (!t_ipcs && ip6)
360                 l3 = RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
361         else if (t_ipcs && !ip6)
362                 l3 = RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
363         else
364                 l3 = RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
365
366         switch (flags_type) {
367         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_ICMP):
368                 if (!t_ipcs)
369                         pkt_type |= l3 | RTE_PTYPE_L4_ICMP;
370                 else
371                         pkt_type |= l3 | RTE_PTYPE_INNER_L4_ICMP;
372                 break;
373
374         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_TCP):
375                 if (!t_ipcs)
376                         pkt_type |= l3 | RTE_PTYPE_L4_TCP;
377                 else
378                         pkt_type |= l3 | RTE_PTYPE_INNER_L4_TCP;
379                 break;
380
381         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_UDP):
382                 if (!t_ipcs)
383                         pkt_type |= l3 | RTE_PTYPE_L4_UDP;
384                 else
385                         pkt_type |= l3 | RTE_PTYPE_INNER_L4_UDP;
386                 break;
387
388         case RTE_LE32(RX_PKT_CMPL_FLAGS_ITYPE_IP):
389                 pkt_type |= l3;
390                 break;
391         }
392
393         return pkt_type;
394 }
395
396 static int bnxt_rx_pkt(struct rte_mbuf **rx_pkt,
397                             struct bnxt_rx_queue *rxq, uint32_t *raw_cons)
398 {
399         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
400         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
401         struct rx_pkt_cmpl *rxcmp;
402         struct rx_pkt_cmpl_hi *rxcmp1;
403         uint32_t tmp_raw_cons = *raw_cons;
404         uint16_t cons, prod, cp_cons =
405             RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
406 #ifdef BNXT_DEBUG
407         uint16_t ag_cons;
408 #endif
409         struct rte_mbuf *mbuf;
410         int rc = 0;
411         uint8_t agg_buf = 0;
412         uint16_t cmp_type;
413
414         rxcmp = (struct rx_pkt_cmpl *)
415             &cpr->cp_desc_ring[cp_cons];
416
417         tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
418         cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
419         rxcmp1 = (struct rx_pkt_cmpl_hi *)&cpr->cp_desc_ring[cp_cons];
420
421         if (!CMP_VALID(rxcmp1, tmp_raw_cons, cpr->cp_ring_struct))
422                 return -EBUSY;
423
424         cpr->valid = FLIP_VALID(cp_cons,
425                                 cpr->cp_ring_struct->ring_mask,
426                                 cpr->valid);
427
428         cmp_type = CMP_TYPE(rxcmp);
429         if (cmp_type == RX_TPA_START_CMPL_TYPE_RX_TPA_START) {
430                 bnxt_tpa_start(rxq, (struct rx_tpa_start_cmpl *)rxcmp,
431                                (struct rx_tpa_start_cmpl_hi *)rxcmp1);
432                 rc = -EINVAL; /* Continue w/o new mbuf */
433                 goto next_rx;
434         } else if (cmp_type == RX_TPA_END_CMPL_TYPE_RX_TPA_END) {
435                 mbuf = bnxt_tpa_end(rxq, &tmp_raw_cons,
436                                    (struct rx_tpa_end_cmpl *)rxcmp,
437                                    (struct rx_tpa_end_cmpl_hi *)rxcmp1);
438                 if (unlikely(!mbuf))
439                         return -EBUSY;
440                 *rx_pkt = mbuf;
441                 goto next_rx;
442         } else if (cmp_type != 0x11) {
443                 rc = -EINVAL;
444                 goto next_rx;
445         }
446
447         agg_buf = (rxcmp->agg_bufs_v1 & RX_PKT_CMPL_AGG_BUFS_MASK)
448                         >> RX_PKT_CMPL_AGG_BUFS_SFT;
449         if (agg_buf && !bnxt_agg_bufs_valid(cpr, agg_buf, tmp_raw_cons))
450                 return -EBUSY;
451
452         prod = rxr->rx_prod;
453
454         cons = rxcmp->opaque;
455         mbuf = bnxt_consume_rx_buf(rxr, cons);
456         if (mbuf == NULL)
457                 return -EBUSY;
458
459         rte_prefetch0(mbuf);
460
461         mbuf->data_off = RTE_PKTMBUF_HEADROOM;
462         mbuf->nb_segs = 1;
463         mbuf->next = NULL;
464         mbuf->pkt_len = rxcmp->len;
465         mbuf->data_len = mbuf->pkt_len;
466         mbuf->port = rxq->port_id;
467         mbuf->ol_flags = 0;
468         if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID) {
469                 mbuf->hash.rss = rxcmp->rss_hash;
470                 mbuf->ol_flags |= PKT_RX_RSS_HASH;
471         } else {
472                 mbuf->hash.fdir.id = rxcmp1->cfa_code;
473                 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
474         }
475
476         if ((rxcmp->flags_type & rte_cpu_to_le_16(RX_PKT_CMPL_FLAGS_MASK)) ==
477              RX_PKT_CMPL_FLAGS_ITYPE_PTP_W_TIMESTAMP)
478                 mbuf->ol_flags |= PKT_RX_IEEE1588_PTP | PKT_RX_IEEE1588_TMST;
479
480         if (agg_buf)
481                 bnxt_rx_pages(rxq, mbuf, &tmp_raw_cons, agg_buf);
482
483         if (rxcmp1->flags2 & RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
484                 mbuf->vlan_tci = rxcmp1->metadata &
485                         (RX_PKT_CMPL_METADATA_VID_MASK |
486                         RX_PKT_CMPL_METADATA_DE |
487                         RX_PKT_CMPL_METADATA_PRI_MASK);
488                 mbuf->ol_flags |= PKT_RX_VLAN;
489         }
490
491         if (likely(RX_CMP_IP_CS_OK(rxcmp1)))
492                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
493         else
494                 mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
495
496         if (likely(RX_CMP_L4_CS_OK(rxcmp1)))
497                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
498         else
499                 mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
500
501         mbuf->packet_type = bnxt_parse_pkt_type(rxcmp, rxcmp1);
502
503 #ifdef BNXT_DEBUG
504         if (rxcmp1->errors_v2 & RX_CMP_L2_ERRORS) {
505                 /* Re-install the mbuf back to the rx ring */
506                 bnxt_reuse_rx_mbuf(rxr, cons, mbuf);
507                 if (agg_buf)
508                         bnxt_reuse_ag_mbuf(rxr, ag_cons, mbuf);
509
510                 rc = -EIO;
511                 goto next_rx;
512         }
513 #endif
514         /*
515          * TODO: Redesign this....
516          * If the allocation fails, the packet does not get received.
517          * Simply returning this will result in slowly falling behind
518          * on the producer ring buffers.
519          * Instead, "filling up" the producer just before ringing the
520          * doorbell could be a better solution since it will let the
521          * producer ring starve until memory is available again pushing
522          * the drops into hardware and getting them out of the driver
523          * allowing recovery to a full producer ring.
524          *
525          * This could also help with cache usage by preventing per-packet
526          * calls in favour of a tight loop with the same function being called
527          * in it.
528          */
529         prod = RING_NEXT(rxr->rx_ring_struct, prod);
530         if (bnxt_alloc_rx_data(rxq, rxr, prod)) {
531                 PMD_DRV_LOG(ERR, "mbuf alloc failed with prod=0x%x\n", prod);
532                 rc = -ENOMEM;
533                 goto rx;
534         }
535         rxr->rx_prod = prod;
536         /*
537          * All MBUFs are allocated with the same size under DPDK,
538          * no optimization for rx_copy_thresh
539          */
540 rx:
541         *rx_pkt = mbuf;
542
543 next_rx:
544
545         *raw_cons = tmp_raw_cons;
546
547         return rc;
548 }
549
550 uint16_t bnxt_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
551                                uint16_t nb_pkts)
552 {
553         struct bnxt_rx_queue *rxq = rx_queue;
554         struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
555         struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
556         uint32_t raw_cons = cpr->cp_raw_cons;
557         uint32_t cons;
558         int nb_rx_pkts = 0;
559         struct rx_pkt_cmpl *rxcmp;
560         uint16_t prod = rxr->rx_prod;
561         uint16_t ag_prod = rxr->ag_prod;
562         int rc = 0;
563
564         /* If Rx Q was stopped return */
565         if (rxq->rx_deferred_start)
566                 return 0;
567
568         /* Handle RX burst request */
569         while (1) {
570                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
571                 rte_prefetch0(&cpr->cp_desc_ring[cons]);
572                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
573
574                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
575                         break;
576                 cpr->valid = FLIP_VALID(cons,
577                                         cpr->cp_ring_struct->ring_mask,
578                                         cpr->valid);
579
580                 /* TODO: Avoid magic numbers... */
581                 if ((CMP_TYPE(rxcmp) & 0x30) == 0x10) {
582                         rc = bnxt_rx_pkt(&rx_pkts[nb_rx_pkts], rxq, &raw_cons);
583                         if (likely(!rc) || rc == -ENOMEM)
584                                 nb_rx_pkts++;
585                         if (rc == -EBUSY)       /* partial completion */
586                                 break;
587                 }
588                 raw_cons = NEXT_RAW_CMP(raw_cons);
589                 if (nb_rx_pkts == nb_pkts)
590                         break;
591         }
592
593         cpr->cp_raw_cons = raw_cons;
594         if (prod == rxr->rx_prod && ag_prod == rxr->ag_prod) {
595                 /*
596                  * For PMD, there is no need to keep on pushing to REARM
597                  * the doorbell if there are no new completions
598                  */
599                 return nb_rx_pkts;
600         }
601
602         B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
603         B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
604         /* Ring the AGG ring DB */
605         B_RX_DB(rxr->ag_doorbell, rxr->ag_prod);
606
607         /* Attempt to alloc Rx buf in case of a previous allocation failure. */
608         if (rc == -ENOMEM) {
609                 int i;
610
611                 for (i = prod; i <= nb_rx_pkts;
612                         i = RING_NEXT(rxr->rx_ring_struct, i)) {
613                         struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
614
615                         /* Buffer already allocated for this index. */
616                         if (rx_buf->mbuf != NULL)
617                                 continue;
618
619                         /* This slot is empty. Alloc buffer for Rx */
620                         if (!bnxt_alloc_rx_data(rxq, rxr, i)) {
621                                 rxr->rx_prod = i;
622                                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
623                         } else {
624                                 PMD_DRV_LOG(ERR, "Alloc  mbuf failed\n");
625                                 break;
626                         }
627                 }
628         }
629
630         return nb_rx_pkts;
631 }
632
633 void bnxt_free_rx_rings(struct bnxt *bp)
634 {
635         int i;
636
637         for (i = 0; i < (int)bp->rx_nr_rings; i++) {
638                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
639
640                 if (!rxq)
641                         continue;
642
643                 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
644                 rte_free(rxq->rx_ring->rx_ring_struct);
645
646                 /* Free the Aggregator ring */
647                 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
648                 rte_free(rxq->rx_ring->ag_ring_struct);
649                 rxq->rx_ring->ag_ring_struct = NULL;
650
651                 rte_free(rxq->rx_ring);
652
653                 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
654                 rte_free(rxq->cp_ring->cp_ring_struct);
655                 rte_free(rxq->cp_ring);
656
657                 rte_free(rxq);
658                 bp->rx_queues[i] = NULL;
659         }
660 }
661
662 int bnxt_init_rx_ring_struct(struct bnxt_rx_queue *rxq, unsigned int socket_id)
663 {
664         struct bnxt_cp_ring_info *cpr;
665         struct bnxt_rx_ring_info *rxr;
666         struct bnxt_ring *ring;
667
668         rxq->rx_buf_use_size = BNXT_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN +
669                                (2 * VLAN_TAG_SIZE);
670         rxq->rx_buf_size = rxq->rx_buf_use_size + sizeof(struct rte_mbuf);
671
672         rxr = rte_zmalloc_socket("bnxt_rx_ring",
673                                  sizeof(struct bnxt_rx_ring_info),
674                                  RTE_CACHE_LINE_SIZE, socket_id);
675         if (rxr == NULL)
676                 return -ENOMEM;
677         rxq->rx_ring = rxr;
678
679         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
680                                    sizeof(struct bnxt_ring),
681                                    RTE_CACHE_LINE_SIZE, socket_id);
682         if (ring == NULL)
683                 return -ENOMEM;
684         rxr->rx_ring_struct = ring;
685         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
686         ring->ring_mask = ring->ring_size - 1;
687         ring->bd = (void *)rxr->rx_desc_ring;
688         ring->bd_dma = rxr->rx_desc_mapping;
689         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
690         ring->vmem = (void **)&rxr->rx_buf_ring;
691
692         cpr = rte_zmalloc_socket("bnxt_rx_ring",
693                                  sizeof(struct bnxt_cp_ring_info),
694                                  RTE_CACHE_LINE_SIZE, socket_id);
695         if (cpr == NULL)
696                 return -ENOMEM;
697         rxq->cp_ring = cpr;
698
699         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
700                                    sizeof(struct bnxt_ring),
701                                    RTE_CACHE_LINE_SIZE, socket_id);
702         if (ring == NULL)
703                 return -ENOMEM;
704         cpr->cp_ring_struct = ring;
705         ring->ring_size = rte_align32pow2(rxr->rx_ring_struct->ring_size *
706                                           (2 + AGG_RING_SIZE_FACTOR));
707         ring->ring_mask = ring->ring_size - 1;
708         ring->bd = (void *)cpr->cp_desc_ring;
709         ring->bd_dma = cpr->cp_desc_mapping;
710         ring->vmem_size = 0;
711         ring->vmem = NULL;
712
713         /* Allocate Aggregator rings */
714         ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
715                                    sizeof(struct bnxt_ring),
716                                    RTE_CACHE_LINE_SIZE, socket_id);
717         if (ring == NULL)
718                 return -ENOMEM;
719         rxr->ag_ring_struct = ring;
720         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc *
721                                           AGG_RING_SIZE_FACTOR);
722         ring->ring_mask = ring->ring_size - 1;
723         ring->bd = (void *)rxr->ag_desc_ring;
724         ring->bd_dma = rxr->ag_desc_mapping;
725         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
726         ring->vmem = (void **)&rxr->ag_buf_ring;
727
728         return 0;
729 }
730
731 static void bnxt_init_rxbds(struct bnxt_ring *ring, uint32_t type,
732                             uint16_t len)
733 {
734         uint32_t j;
735         struct rx_prod_pkt_bd *rx_bd_ring = (struct rx_prod_pkt_bd *)ring->bd;
736
737         if (!rx_bd_ring)
738                 return;
739         for (j = 0; j < ring->ring_size; j++) {
740                 rx_bd_ring[j].flags_type = rte_cpu_to_le_16(type);
741                 rx_bd_ring[j].len = rte_cpu_to_le_16(len);
742                 rx_bd_ring[j].opaque = j;
743         }
744 }
745
746 int bnxt_init_one_rx_ring(struct bnxt_rx_queue *rxq)
747 {
748         struct bnxt_rx_ring_info *rxr;
749         struct bnxt_ring *ring;
750         uint32_t prod, type;
751         unsigned int i;
752         uint16_t size;
753
754         size = rte_pktmbuf_data_room_size(rxq->mb_pool) - RTE_PKTMBUF_HEADROOM;
755         if (rxq->rx_buf_use_size <= size)
756                 size = rxq->rx_buf_use_size;
757
758         type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT;
759
760         rxr = rxq->rx_ring;
761         ring = rxr->rx_ring_struct;
762         bnxt_init_rxbds(ring, type, size);
763
764         prod = rxr->rx_prod;
765         for (i = 0; i < ring->ring_size; i++) {
766                 if (bnxt_alloc_rx_data(rxq, rxr, prod) != 0) {
767                         PMD_DRV_LOG(WARNING,
768                                 "init'ed rx ring %d with %d/%d mbufs only\n",
769                                 rxq->queue_id, i, ring->ring_size);
770                         break;
771                 }
772                 rxr->rx_prod = prod;
773                 prod = RING_NEXT(rxr->rx_ring_struct, prod);
774         }
775
776         ring = rxr->ag_ring_struct;
777         type = RX_PROD_AGG_BD_TYPE_RX_PROD_AGG;
778         bnxt_init_rxbds(ring, type, size);
779         prod = rxr->ag_prod;
780
781         for (i = 0; i < ring->ring_size; i++) {
782                 if (bnxt_alloc_ag_data(rxq, rxr, prod) != 0) {
783                         PMD_DRV_LOG(WARNING,
784                         "init'ed AG ring %d with %d/%d mbufs only\n",
785                         rxq->queue_id, i, ring->ring_size);
786                         break;
787                 }
788                 rxr->ag_prod = prod;
789                 prod = RING_NEXT(rxr->ag_ring_struct, prod);
790         }
791         PMD_DRV_LOG(DEBUG, "AGG Done!\n");
792
793         if (rxr->tpa_info) {
794                 for (i = 0; i < BNXT_TPA_MAX; i++) {
795                         rxr->tpa_info[i].mbuf =
796                                 __bnxt_alloc_rx_data(rxq->mb_pool);
797                         if (!rxr->tpa_info[i].mbuf) {
798                                 rte_atomic64_inc(&rxq->bp->rx_mbuf_alloc_fail);
799                                 return -ENOMEM;
800                         }
801                 }
802         }
803         PMD_DRV_LOG(DEBUG, "TPA alloc Done!\n");
804
805         return 0;
806 }