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