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