net/ngbe: support jumbo frame
[dpdk.git] / drivers / net / ngbe / ngbe_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3  * Copyright(c) 2010-2017 Intel Corporation
4  */
5
6 #include <sys/queue.h>
7
8 #include <stdint.h>
9 #include <rte_ethdev.h>
10 #include <ethdev_driver.h>
11 #include <rte_malloc.h>
12 #include <rte_net.h>
13
14 #include "ngbe_logs.h"
15 #include "base/ngbe.h"
16 #include "ngbe_ethdev.h"
17 #include "ngbe_rxtx.h"
18
19 /* Bit Mask to indicate what bits required for building Tx context */
20 static const u64 NGBE_TX_OFFLOAD_MASK = (RTE_MBUF_F_TX_IP_CKSUM |
21                 RTE_MBUF_F_TX_OUTER_IPV6 |
22                 RTE_MBUF_F_TX_OUTER_IPV4 |
23                 RTE_MBUF_F_TX_IPV6 |
24                 RTE_MBUF_F_TX_IPV4 |
25                 RTE_MBUF_F_TX_L4_MASK |
26                 RTE_MBUF_F_TX_TCP_SEG |
27                 RTE_MBUF_F_TX_TUNNEL_MASK |
28                 RTE_MBUF_F_TX_OUTER_IP_CKSUM);
29 #define NGBE_TX_OFFLOAD_NOTSUP_MASK \
30                 (RTE_MBUF_F_TX_OFFLOAD_MASK ^ NGBE_TX_OFFLOAD_MASK)
31
32 /*
33  * Prefetch a cache line into all cache levels.
34  */
35 #define rte_ngbe_prefetch(p)   rte_prefetch0(p)
36
37 /*********************************************************************
38  *
39  *  Tx functions
40  *
41  **********************************************************************/
42
43 /*
44  * Check for descriptors with their DD bit set and free mbufs.
45  * Return the total number of buffers freed.
46  */
47 static __rte_always_inline int
48 ngbe_tx_free_bufs(struct ngbe_tx_queue *txq)
49 {
50         struct ngbe_tx_entry *txep;
51         uint32_t status;
52         int i, nb_free = 0;
53         struct rte_mbuf *m, *free[RTE_NGBE_TX_MAX_FREE_BUF_SZ];
54
55         /* check DD bit on threshold descriptor */
56         status = txq->tx_ring[txq->tx_next_dd].dw3;
57         if (!(status & rte_cpu_to_le_32(NGBE_TXD_DD))) {
58                 if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
59                         ngbe_set32_masked(txq->tdc_reg_addr,
60                                 NGBE_TXCFG_FLUSH, NGBE_TXCFG_FLUSH);
61                 return 0;
62         }
63
64         /*
65          * first buffer to free from S/W ring is at index
66          * tx_next_dd - (tx_free_thresh-1)
67          */
68         txep = &txq->sw_ring[txq->tx_next_dd - (txq->tx_free_thresh - 1)];
69         for (i = 0; i < txq->tx_free_thresh; ++i, ++txep) {
70                 /* free buffers one at a time */
71                 m = rte_pktmbuf_prefree_seg(txep->mbuf);
72                 txep->mbuf = NULL;
73
74                 if (unlikely(m == NULL))
75                         continue;
76
77                 if (nb_free >= RTE_NGBE_TX_MAX_FREE_BUF_SZ ||
78                     (nb_free > 0 && m->pool != free[0]->pool)) {
79                         rte_mempool_put_bulk(free[0]->pool,
80                                              (void **)free, nb_free);
81                         nb_free = 0;
82                 }
83
84                 free[nb_free++] = m;
85         }
86
87         if (nb_free > 0)
88                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
89
90         /* buffers were freed, update counters */
91         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_free_thresh);
92         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_free_thresh);
93         if (txq->tx_next_dd >= txq->nb_tx_desc)
94                 txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
95
96         return txq->tx_free_thresh;
97 }
98
99 /* Populate 4 descriptors with data from 4 mbufs */
100 static inline void
101 tx4(volatile struct ngbe_tx_desc *txdp, struct rte_mbuf **pkts)
102 {
103         uint64_t buf_dma_addr;
104         uint32_t pkt_len;
105         int i;
106
107         for (i = 0; i < 4; ++i, ++txdp, ++pkts) {
108                 buf_dma_addr = rte_mbuf_data_iova(*pkts);
109                 pkt_len = (*pkts)->data_len;
110
111                 /* write data to descriptor */
112                 txdp->qw0 = rte_cpu_to_le_64(buf_dma_addr);
113                 txdp->dw2 = cpu_to_le32(NGBE_TXD_FLAGS |
114                                         NGBE_TXD_DATLEN(pkt_len));
115                 txdp->dw3 = cpu_to_le32(NGBE_TXD_PAYLEN(pkt_len));
116
117                 rte_prefetch0(&(*pkts)->pool);
118         }
119 }
120
121 /* Populate 1 descriptor with data from 1 mbuf */
122 static inline void
123 tx1(volatile struct ngbe_tx_desc *txdp, struct rte_mbuf **pkts)
124 {
125         uint64_t buf_dma_addr;
126         uint32_t pkt_len;
127
128         buf_dma_addr = rte_mbuf_data_iova(*pkts);
129         pkt_len = (*pkts)->data_len;
130
131         /* write data to descriptor */
132         txdp->qw0 = cpu_to_le64(buf_dma_addr);
133         txdp->dw2 = cpu_to_le32(NGBE_TXD_FLAGS |
134                                 NGBE_TXD_DATLEN(pkt_len));
135         txdp->dw3 = cpu_to_le32(NGBE_TXD_PAYLEN(pkt_len));
136
137         rte_prefetch0(&(*pkts)->pool);
138 }
139
140 /*
141  * Fill H/W descriptor ring with mbuf data.
142  * Copy mbuf pointers to the S/W ring.
143  */
144 static inline void
145 ngbe_tx_fill_hw_ring(struct ngbe_tx_queue *txq, struct rte_mbuf **pkts,
146                       uint16_t nb_pkts)
147 {
148         volatile struct ngbe_tx_desc *txdp = &txq->tx_ring[txq->tx_tail];
149         struct ngbe_tx_entry *txep = &txq->sw_ring[txq->tx_tail];
150         const int N_PER_LOOP = 4;
151         const int N_PER_LOOP_MASK = N_PER_LOOP - 1;
152         int mainpart, leftover;
153         int i, j;
154
155         /*
156          * Process most of the packets in chunks of N pkts.  Any
157          * leftover packets will get processed one at a time.
158          */
159         mainpart = (nb_pkts & ((uint32_t)~N_PER_LOOP_MASK));
160         leftover = (nb_pkts & ((uint32_t)N_PER_LOOP_MASK));
161         for (i = 0; i < mainpart; i += N_PER_LOOP) {
162                 /* Copy N mbuf pointers to the S/W ring */
163                 for (j = 0; j < N_PER_LOOP; ++j)
164                         (txep + i + j)->mbuf = *(pkts + i + j);
165                 tx4(txdp + i, pkts + i);
166         }
167
168         if (unlikely(leftover > 0)) {
169                 for (i = 0; i < leftover; ++i) {
170                         (txep + mainpart + i)->mbuf = *(pkts + mainpart + i);
171                         tx1(txdp + mainpart + i, pkts + mainpart + i);
172                 }
173         }
174 }
175
176 static inline uint16_t
177 tx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
178              uint16_t nb_pkts)
179 {
180         struct ngbe_tx_queue *txq = (struct ngbe_tx_queue *)tx_queue;
181         uint16_t n = 0;
182
183         /*
184          * Begin scanning the H/W ring for done descriptors when the
185          * number of available descriptors drops below tx_free_thresh.
186          * For each done descriptor, free the associated buffer.
187          */
188         if (txq->nb_tx_free < txq->tx_free_thresh)
189                 ngbe_tx_free_bufs(txq);
190
191         /* Only use descriptors that are available */
192         nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
193         if (unlikely(nb_pkts == 0))
194                 return 0;
195
196         /* Use exactly nb_pkts descriptors */
197         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
198
199         /*
200          * At this point, we know there are enough descriptors in the
201          * ring to transmit all the packets.  This assumes that each
202          * mbuf contains a single segment, and that no new offloads
203          * are expected, which would require a new context descriptor.
204          */
205
206         /*
207          * See if we're going to wrap-around. If so, handle the top
208          * of the descriptor ring first, then do the bottom.  If not,
209          * the processing looks just like the "bottom" part anyway...
210          */
211         if ((txq->tx_tail + nb_pkts) > txq->nb_tx_desc) {
212                 n = (uint16_t)(txq->nb_tx_desc - txq->tx_tail);
213                 ngbe_tx_fill_hw_ring(txq, tx_pkts, n);
214                 txq->tx_tail = 0;
215         }
216
217         /* Fill H/W descriptor ring with mbuf data */
218         ngbe_tx_fill_hw_ring(txq, tx_pkts + n, (uint16_t)(nb_pkts - n));
219         txq->tx_tail = (uint16_t)(txq->tx_tail + (nb_pkts - n));
220
221         /*
222          * Check for wrap-around. This would only happen if we used
223          * up to the last descriptor in the ring, no more, no less.
224          */
225         if (txq->tx_tail >= txq->nb_tx_desc)
226                 txq->tx_tail = 0;
227
228         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
229                    (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
230                    (uint16_t)txq->tx_tail, (uint16_t)nb_pkts);
231
232         /* update tail pointer */
233         rte_wmb();
234         ngbe_set32_relaxed(txq->tdt_reg_addr, txq->tx_tail);
235
236         return nb_pkts;
237 }
238
239 uint16_t
240 ngbe_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
241                        uint16_t nb_pkts)
242 {
243         uint16_t nb_tx;
244
245         /* Try to transmit at least chunks of TX_MAX_BURST pkts */
246         if (likely(nb_pkts <= RTE_PMD_NGBE_TX_MAX_BURST))
247                 return tx_xmit_pkts(tx_queue, tx_pkts, nb_pkts);
248
249         /* transmit more than the max burst, in chunks of TX_MAX_BURST */
250         nb_tx = 0;
251         while (nb_pkts != 0) {
252                 uint16_t ret, n;
253
254                 n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_NGBE_TX_MAX_BURST);
255                 ret = tx_xmit_pkts(tx_queue, &tx_pkts[nb_tx], n);
256                 nb_tx = (uint16_t)(nb_tx + ret);
257                 nb_pkts = (uint16_t)(nb_pkts - ret);
258                 if (ret < n)
259                         break;
260         }
261
262         return nb_tx;
263 }
264
265 static inline void
266 ngbe_set_xmit_ctx(struct ngbe_tx_queue *txq,
267                 volatile struct ngbe_tx_ctx_desc *ctx_txd,
268                 uint64_t ol_flags, union ngbe_tx_offload tx_offload)
269 {
270         union ngbe_tx_offload tx_offload_mask;
271         uint32_t type_tucmd_mlhl;
272         uint32_t mss_l4len_idx;
273         uint32_t ctx_idx;
274         uint32_t vlan_macip_lens;
275         uint32_t tunnel_seed;
276
277         ctx_idx = txq->ctx_curr;
278         tx_offload_mask.data[0] = 0;
279         tx_offload_mask.data[1] = 0;
280
281         /* Specify which HW CTX to upload. */
282         mss_l4len_idx = NGBE_TXD_IDX(ctx_idx);
283         type_tucmd_mlhl = NGBE_TXD_CTXT;
284
285         tx_offload_mask.ptid |= ~0;
286         type_tucmd_mlhl |= NGBE_TXD_PTID(tx_offload.ptid);
287
288         /* check if TCP segmentation required for this packet */
289         if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
290                 tx_offload_mask.l2_len |= ~0;
291                 tx_offload_mask.l3_len |= ~0;
292                 tx_offload_mask.l4_len |= ~0;
293                 tx_offload_mask.tso_segsz |= ~0;
294                 mss_l4len_idx |= NGBE_TXD_MSS(tx_offload.tso_segsz);
295                 mss_l4len_idx |= NGBE_TXD_L4LEN(tx_offload.l4_len);
296         } else { /* no TSO, check if hardware checksum is needed */
297                 if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
298                         tx_offload_mask.l2_len |= ~0;
299                         tx_offload_mask.l3_len |= ~0;
300                 }
301
302                 switch (ol_flags & RTE_MBUF_F_TX_L4_MASK) {
303                 case RTE_MBUF_F_TX_UDP_CKSUM:
304                         mss_l4len_idx |=
305                                 NGBE_TXD_L4LEN(sizeof(struct rte_udp_hdr));
306                         tx_offload_mask.l2_len |= ~0;
307                         tx_offload_mask.l3_len |= ~0;
308                         break;
309                 case RTE_MBUF_F_TX_TCP_CKSUM:
310                         mss_l4len_idx |=
311                                 NGBE_TXD_L4LEN(sizeof(struct rte_tcp_hdr));
312                         tx_offload_mask.l2_len |= ~0;
313                         tx_offload_mask.l3_len |= ~0;
314                         break;
315                 case RTE_MBUF_F_TX_SCTP_CKSUM:
316                         mss_l4len_idx |=
317                                 NGBE_TXD_L4LEN(sizeof(struct rte_sctp_hdr));
318                         tx_offload_mask.l2_len |= ~0;
319                         tx_offload_mask.l3_len |= ~0;
320                         break;
321                 default:
322                         break;
323                 }
324         }
325
326         vlan_macip_lens = NGBE_TXD_IPLEN(tx_offload.l3_len >> 1);
327
328         if (ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) {
329                 tx_offload_mask.outer_tun_len |= ~0;
330                 tx_offload_mask.outer_l2_len |= ~0;
331                 tx_offload_mask.outer_l3_len |= ~0;
332                 tx_offload_mask.l2_len |= ~0;
333                 tunnel_seed = NGBE_TXD_ETUNLEN(tx_offload.outer_tun_len >> 1);
334                 tunnel_seed |= NGBE_TXD_EIPLEN(tx_offload.outer_l3_len >> 2);
335
336                 switch (ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK) {
337                 case RTE_MBUF_F_TX_TUNNEL_IPIP:
338                         /* for non UDP / GRE tunneling, set to 0b */
339                         break;
340                 default:
341                         PMD_TX_LOG(ERR, "Tunnel type not supported");
342                         return;
343                 }
344                 vlan_macip_lens |= NGBE_TXD_MACLEN(tx_offload.outer_l2_len);
345         } else {
346                 tunnel_seed = 0;
347                 vlan_macip_lens |= NGBE_TXD_MACLEN(tx_offload.l2_len);
348         }
349
350         txq->ctx_cache[ctx_idx].flags = ol_flags;
351         txq->ctx_cache[ctx_idx].tx_offload.data[0] =
352                 tx_offload_mask.data[0] & tx_offload.data[0];
353         txq->ctx_cache[ctx_idx].tx_offload.data[1] =
354                 tx_offload_mask.data[1] & tx_offload.data[1];
355         txq->ctx_cache[ctx_idx].tx_offload_mask = tx_offload_mask;
356
357         ctx_txd->dw0 = rte_cpu_to_le_32(vlan_macip_lens);
358         ctx_txd->dw1 = rte_cpu_to_le_32(tunnel_seed);
359         ctx_txd->dw2 = rte_cpu_to_le_32(type_tucmd_mlhl);
360         ctx_txd->dw3 = rte_cpu_to_le_32(mss_l4len_idx);
361 }
362
363 /*
364  * Check which hardware context can be used. Use the existing match
365  * or create a new context descriptor.
366  */
367 static inline uint32_t
368 what_ctx_update(struct ngbe_tx_queue *txq, uint64_t flags,
369                    union ngbe_tx_offload tx_offload)
370 {
371         /* If match with the current used context */
372         if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
373                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
374                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
375                      & tx_offload.data[0])) &&
376                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
377                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
378                      & tx_offload.data[1]))))
379                 return txq->ctx_curr;
380
381         /* What if match with the next context  */
382         txq->ctx_curr ^= 1;
383         if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
384                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
385                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
386                      & tx_offload.data[0])) &&
387                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
388                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
389                      & tx_offload.data[1]))))
390                 return txq->ctx_curr;
391
392         /* Mismatch, use the previous context */
393         return NGBE_CTX_NUM;
394 }
395
396 static inline uint32_t
397 tx_desc_cksum_flags_to_olinfo(uint64_t ol_flags)
398 {
399         uint32_t tmp = 0;
400
401         if ((ol_flags & RTE_MBUF_F_TX_L4_MASK) != RTE_MBUF_F_TX_L4_NO_CKSUM) {
402                 tmp |= NGBE_TXD_CC;
403                 tmp |= NGBE_TXD_L4CS;
404         }
405         if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
406                 tmp |= NGBE_TXD_CC;
407                 tmp |= NGBE_TXD_IPCS;
408         }
409         if (ol_flags & RTE_MBUF_F_TX_OUTER_IP_CKSUM) {
410                 tmp |= NGBE_TXD_CC;
411                 tmp |= NGBE_TXD_EIPCS;
412         }
413         if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
414                 tmp |= NGBE_TXD_CC;
415                 /* implies IPv4 cksum */
416                 if (ol_flags & RTE_MBUF_F_TX_IPV4)
417                         tmp |= NGBE_TXD_IPCS;
418                 tmp |= NGBE_TXD_L4CS;
419         }
420
421         return tmp;
422 }
423
424 static inline uint32_t
425 tx_desc_ol_flags_to_cmdtype(uint64_t ol_flags)
426 {
427         uint32_t cmdtype = 0;
428
429         if (ol_flags & RTE_MBUF_F_TX_TCP_SEG)
430                 cmdtype |= NGBE_TXD_TSE;
431         return cmdtype;
432 }
433
434 static inline uint8_t
435 tx_desc_ol_flags_to_ptid(uint64_t oflags, uint32_t ptype)
436 {
437         bool tun;
438
439         if (ptype)
440                 return ngbe_encode_ptype(ptype);
441
442         /* Only support flags in NGBE_TX_OFFLOAD_MASK */
443         tun = !!(oflags & RTE_MBUF_F_TX_TUNNEL_MASK);
444
445         /* L2 level */
446         ptype = RTE_PTYPE_L2_ETHER;
447
448         /* L3 level */
449         if (oflags & (RTE_MBUF_F_TX_OUTER_IPV4 | RTE_MBUF_F_TX_OUTER_IP_CKSUM))
450                 ptype |= RTE_PTYPE_L3_IPV4;
451         else if (oflags & (RTE_MBUF_F_TX_OUTER_IPV6))
452                 ptype |= RTE_PTYPE_L3_IPV6;
453
454         if (oflags & (RTE_MBUF_F_TX_IPV4 | RTE_MBUF_F_TX_IP_CKSUM))
455                 ptype |= (tun ? RTE_PTYPE_INNER_L3_IPV4 : RTE_PTYPE_L3_IPV4);
456         else if (oflags & (RTE_MBUF_F_TX_IPV6))
457                 ptype |= (tun ? RTE_PTYPE_INNER_L3_IPV6 : RTE_PTYPE_L3_IPV6);
458
459         /* L4 level */
460         switch (oflags & (RTE_MBUF_F_TX_L4_MASK)) {
461         case RTE_MBUF_F_TX_TCP_CKSUM:
462                 ptype |= (tun ? RTE_PTYPE_INNER_L4_TCP : RTE_PTYPE_L4_TCP);
463                 break;
464         case RTE_MBUF_F_TX_UDP_CKSUM:
465                 ptype |= (tun ? RTE_PTYPE_INNER_L4_UDP : RTE_PTYPE_L4_UDP);
466                 break;
467         case RTE_MBUF_F_TX_SCTP_CKSUM:
468                 ptype |= (tun ? RTE_PTYPE_INNER_L4_SCTP : RTE_PTYPE_L4_SCTP);
469                 break;
470         }
471
472         if (oflags & RTE_MBUF_F_TX_TCP_SEG)
473                 ptype |= (tun ? RTE_PTYPE_INNER_L4_TCP : RTE_PTYPE_L4_TCP);
474
475         /* Tunnel */
476         switch (oflags & RTE_MBUF_F_TX_TUNNEL_MASK) {
477         case RTE_MBUF_F_TX_TUNNEL_IPIP:
478         case RTE_MBUF_F_TX_TUNNEL_IP:
479                 ptype |= RTE_PTYPE_L2_ETHER |
480                          RTE_PTYPE_L3_IPV4 |
481                          RTE_PTYPE_TUNNEL_IP;
482                 break;
483         }
484
485         return ngbe_encode_ptype(ptype);
486 }
487
488 /* Reset transmit descriptors after they have been used */
489 static inline int
490 ngbe_xmit_cleanup(struct ngbe_tx_queue *txq)
491 {
492         struct ngbe_tx_entry *sw_ring = txq->sw_ring;
493         volatile struct ngbe_tx_desc *txr = txq->tx_ring;
494         uint16_t last_desc_cleaned = txq->last_desc_cleaned;
495         uint16_t nb_tx_desc = txq->nb_tx_desc;
496         uint16_t desc_to_clean_to;
497         uint16_t nb_tx_to_clean;
498         uint32_t status;
499
500         /* Determine the last descriptor needing to be cleaned */
501         desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_free_thresh);
502         if (desc_to_clean_to >= nb_tx_desc)
503                 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
504
505         /* Check to make sure the last descriptor to clean is done */
506         desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
507         status = txr[desc_to_clean_to].dw3;
508         if (!(status & rte_cpu_to_le_32(NGBE_TXD_DD))) {
509                 PMD_TX_LOG(DEBUG,
510                         "Tx descriptor %4u is not done"
511                         "(port=%d queue=%d)",
512                         desc_to_clean_to,
513                         txq->port_id, txq->queue_id);
514                 if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
515                         ngbe_set32_masked(txq->tdc_reg_addr,
516                                 NGBE_TXCFG_FLUSH, NGBE_TXCFG_FLUSH);
517                 /* Failed to clean any descriptors, better luck next time */
518                 return -(1);
519         }
520
521         /* Figure out how many descriptors will be cleaned */
522         if (last_desc_cleaned > desc_to_clean_to)
523                 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
524                                                         desc_to_clean_to);
525         else
526                 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
527                                                 last_desc_cleaned);
528
529         PMD_TX_LOG(DEBUG,
530                 "Cleaning %4u Tx descriptors: %4u to %4u (port=%d queue=%d)",
531                 nb_tx_to_clean, last_desc_cleaned, desc_to_clean_to,
532                 txq->port_id, txq->queue_id);
533
534         /*
535          * The last descriptor to clean is done, so that means all the
536          * descriptors from the last descriptor that was cleaned
537          * up to the last descriptor with the RS bit set
538          * are done. Only reset the threshold descriptor.
539          */
540         txr[desc_to_clean_to].dw3 = 0;
541
542         /* Update the txq to reflect the last descriptor that was cleaned */
543         txq->last_desc_cleaned = desc_to_clean_to;
544         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
545
546         /* No Error */
547         return 0;
548 }
549
550 uint16_t
551 ngbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
552                 uint16_t nb_pkts)
553 {
554         struct ngbe_tx_queue *txq;
555         struct ngbe_tx_entry *sw_ring;
556         struct ngbe_tx_entry *txe, *txn;
557         volatile struct ngbe_tx_desc *txr;
558         volatile struct ngbe_tx_desc *txd;
559         struct rte_mbuf     *tx_pkt;
560         struct rte_mbuf     *m_seg;
561         uint64_t buf_dma_addr;
562         uint32_t olinfo_status;
563         uint32_t cmd_type_len;
564         uint32_t pkt_len;
565         uint16_t slen;
566         uint64_t ol_flags;
567         uint16_t tx_id;
568         uint16_t tx_last;
569         uint16_t nb_tx;
570         uint16_t nb_used;
571         uint64_t tx_ol_req;
572         uint32_t ctx = 0;
573         uint32_t new_ctx;
574         union ngbe_tx_offload tx_offload;
575
576         tx_offload.data[0] = 0;
577         tx_offload.data[1] = 0;
578         txq = tx_queue;
579         sw_ring = txq->sw_ring;
580         txr     = txq->tx_ring;
581         tx_id   = txq->tx_tail;
582         txe = &sw_ring[tx_id];
583
584         /* Determine if the descriptor ring needs to be cleaned. */
585         if (txq->nb_tx_free < txq->tx_free_thresh)
586                 ngbe_xmit_cleanup(txq);
587
588         rte_prefetch0(&txe->mbuf->pool);
589
590         /* Tx loop */
591         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
592                 new_ctx = 0;
593                 tx_pkt = *tx_pkts++;
594                 pkt_len = tx_pkt->pkt_len;
595
596                 /*
597                  * Determine how many (if any) context descriptors
598                  * are needed for offload functionality.
599                  */
600                 ol_flags = tx_pkt->ol_flags;
601
602                 /* If hardware offload required */
603                 tx_ol_req = ol_flags & NGBE_TX_OFFLOAD_MASK;
604                 if (tx_ol_req) {
605                         tx_offload.ptid = tx_desc_ol_flags_to_ptid(tx_ol_req,
606                                         tx_pkt->packet_type);
607                         tx_offload.l2_len = tx_pkt->l2_len;
608                         tx_offload.l3_len = tx_pkt->l3_len;
609                         tx_offload.l4_len = tx_pkt->l4_len;
610                         tx_offload.tso_segsz = tx_pkt->tso_segsz;
611                         tx_offload.outer_l2_len = tx_pkt->outer_l2_len;
612                         tx_offload.outer_l3_len = tx_pkt->outer_l3_len;
613                         tx_offload.outer_tun_len = 0;
614
615                         /* If new context need be built or reuse the exist ctx*/
616                         ctx = what_ctx_update(txq, tx_ol_req, tx_offload);
617                         /* Only allocate context descriptor if required */
618                         new_ctx = (ctx == NGBE_CTX_NUM);
619                         ctx = txq->ctx_curr;
620                 }
621
622                 /*
623                  * Keep track of how many descriptors are used this loop
624                  * This will always be the number of segments + the number of
625                  * Context descriptors required to transmit the packet
626                  */
627                 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
628
629                 /*
630                  * The number of descriptors that must be allocated for a
631                  * packet is the number of segments of that packet, plus 1
632                  * Context Descriptor for the hardware offload, if any.
633                  * Determine the last Tx descriptor to allocate in the Tx ring
634                  * for the packet, starting from the current position (tx_id)
635                  * in the ring.
636                  */
637                 tx_last = (uint16_t)(tx_id + nb_used - 1);
638
639                 /* Circular ring */
640                 if (tx_last >= txq->nb_tx_desc)
641                         tx_last = (uint16_t)(tx_last - txq->nb_tx_desc);
642
643                 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
644                            " tx_first=%u tx_last=%u",
645                            (uint16_t)txq->port_id,
646                            (uint16_t)txq->queue_id,
647                            (uint32_t)pkt_len,
648                            (uint16_t)tx_id,
649                            (uint16_t)tx_last);
650
651                 /*
652                  * Make sure there are enough Tx descriptors available to
653                  * transmit the entire packet.
654                  * nb_used better be less than or equal to txq->tx_free_thresh
655                  */
656                 if (nb_used > txq->nb_tx_free) {
657                         PMD_TX_LOG(DEBUG,
658                                 "Not enough free Tx descriptors "
659                                 "nb_used=%4u nb_free=%4u "
660                                 "(port=%d queue=%d)",
661                                 nb_used, txq->nb_tx_free,
662                                 txq->port_id, txq->queue_id);
663
664                         if (ngbe_xmit_cleanup(txq) != 0) {
665                                 /* Could not clean any descriptors */
666                                 if (nb_tx == 0)
667                                         return 0;
668                                 goto end_of_tx;
669                         }
670
671                         /* nb_used better be <= txq->tx_free_thresh */
672                         if (unlikely(nb_used > txq->tx_free_thresh)) {
673                                 PMD_TX_LOG(DEBUG,
674                                         "The number of descriptors needed to "
675                                         "transmit the packet exceeds the "
676                                         "RS bit threshold. This will impact "
677                                         "performance."
678                                         "nb_used=%4u nb_free=%4u "
679                                         "tx_free_thresh=%4u. "
680                                         "(port=%d queue=%d)",
681                                         nb_used, txq->nb_tx_free,
682                                         txq->tx_free_thresh,
683                                         txq->port_id, txq->queue_id);
684                                 /*
685                                  * Loop here until there are enough Tx
686                                  * descriptors or until the ring cannot be
687                                  * cleaned.
688                                  */
689                                 while (nb_used > txq->nb_tx_free) {
690                                         if (ngbe_xmit_cleanup(txq) != 0) {
691                                                 /*
692                                                  * Could not clean any
693                                                  * descriptors
694                                                  */
695                                                 if (nb_tx == 0)
696                                                         return 0;
697                                                 goto end_of_tx;
698                                         }
699                                 }
700                         }
701                 }
702
703                 /*
704                  * By now there are enough free Tx descriptors to transmit
705                  * the packet.
706                  */
707
708                 /*
709                  * Set common flags of all Tx Data Descriptors.
710                  *
711                  * The following bits must be set in the first Data Descriptor
712                  * and are ignored in the other ones:
713                  *   - NGBE_TXD_FCS
714                  *
715                  * The following bits must only be set in the last Data
716                  * Descriptor:
717                  *   - NGBE_TXD_EOP
718                  */
719                 cmd_type_len = NGBE_TXD_FCS;
720
721                 olinfo_status = 0;
722                 if (tx_ol_req) {
723                         if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
724                                 /* when TSO is on, paylen in descriptor is the
725                                  * not the packet len but the tcp payload len
726                                  */
727                                 pkt_len -= (tx_offload.l2_len +
728                                         tx_offload.l3_len + tx_offload.l4_len);
729                                 pkt_len -=
730                                         (tx_pkt->ol_flags & RTE_MBUF_F_TX_TUNNEL_MASK)
731                                         ? tx_offload.outer_l2_len +
732                                           tx_offload.outer_l3_len : 0;
733                         }
734
735                         /*
736                          * Setup the Tx Context Descriptor if required
737                          */
738                         if (new_ctx) {
739                                 volatile struct ngbe_tx_ctx_desc *ctx_txd;
740
741                                 ctx_txd = (volatile struct ngbe_tx_ctx_desc *)
742                                     &txr[tx_id];
743
744                                 txn = &sw_ring[txe->next_id];
745                                 rte_prefetch0(&txn->mbuf->pool);
746
747                                 if (txe->mbuf != NULL) {
748                                         rte_pktmbuf_free_seg(txe->mbuf);
749                                         txe->mbuf = NULL;
750                                 }
751
752                                 ngbe_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
753                                         tx_offload);
754
755                                 txe->last_id = tx_last;
756                                 tx_id = txe->next_id;
757                                 txe = txn;
758                         }
759
760                         /*
761                          * Setup the Tx Data Descriptor,
762                          * This path will go through
763                          * whatever new/reuse the context descriptor
764                          */
765                         cmd_type_len  |= tx_desc_ol_flags_to_cmdtype(ol_flags);
766                         olinfo_status |=
767                                 tx_desc_cksum_flags_to_olinfo(ol_flags);
768                         olinfo_status |= NGBE_TXD_IDX(ctx);
769                 }
770
771                 olinfo_status |= NGBE_TXD_PAYLEN(pkt_len);
772
773                 m_seg = tx_pkt;
774                 do {
775                         txd = &txr[tx_id];
776                         txn = &sw_ring[txe->next_id];
777                         rte_prefetch0(&txn->mbuf->pool);
778
779                         if (txe->mbuf != NULL)
780                                 rte_pktmbuf_free_seg(txe->mbuf);
781                         txe->mbuf = m_seg;
782
783                         /*
784                          * Set up Transmit Data Descriptor.
785                          */
786                         slen = m_seg->data_len;
787                         buf_dma_addr = rte_mbuf_data_iova(m_seg);
788                         txd->qw0 = rte_cpu_to_le_64(buf_dma_addr);
789                         txd->dw2 = rte_cpu_to_le_32(cmd_type_len | slen);
790                         txd->dw3 = rte_cpu_to_le_32(olinfo_status);
791                         txe->last_id = tx_last;
792                         tx_id = txe->next_id;
793                         txe = txn;
794                         m_seg = m_seg->next;
795                 } while (m_seg != NULL);
796
797                 /*
798                  * The last packet data descriptor needs End Of Packet (EOP)
799                  */
800                 cmd_type_len |= NGBE_TXD_EOP;
801                 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
802
803                 txd->dw2 |= rte_cpu_to_le_32(cmd_type_len);
804         }
805
806 end_of_tx:
807
808         rte_wmb();
809
810         /*
811          * Set the Transmit Descriptor Tail (TDT)
812          */
813         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
814                    (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
815                    (uint16_t)tx_id, (uint16_t)nb_tx);
816         ngbe_set32_relaxed(txq->tdt_reg_addr, tx_id);
817         txq->tx_tail = tx_id;
818
819         return nb_tx;
820 }
821
822 /*********************************************************************
823  *
824  *  Tx prep functions
825  *
826  **********************************************************************/
827 uint16_t
828 ngbe_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
829 {
830         int i, ret;
831         uint64_t ol_flags;
832         struct rte_mbuf *m;
833         struct ngbe_tx_queue *txq = (struct ngbe_tx_queue *)tx_queue;
834
835         for (i = 0; i < nb_pkts; i++) {
836                 m = tx_pkts[i];
837                 ol_flags = m->ol_flags;
838
839                 /**
840                  * Check if packet meets requirements for number of segments
841                  *
842                  * NOTE: for ngbe it's always (40 - WTHRESH) for both TSO and
843                  *       non-TSO
844                  */
845
846                 if (m->nb_segs > NGBE_TX_MAX_SEG - txq->wthresh) {
847                         rte_errno = -EINVAL;
848                         return i;
849                 }
850
851                 if (ol_flags & NGBE_TX_OFFLOAD_NOTSUP_MASK) {
852                         rte_errno = -ENOTSUP;
853                         return i;
854                 }
855
856 #ifdef RTE_ETHDEV_DEBUG_TX
857                 ret = rte_validate_tx_offload(m);
858                 if (ret != 0) {
859                         rte_errno = ret;
860                         return i;
861                 }
862 #endif
863                 ret = rte_net_intel_cksum_prepare(m);
864                 if (ret != 0) {
865                         rte_errno = ret;
866                         return i;
867                 }
868         }
869
870         return i;
871 }
872
873 /*********************************************************************
874  *
875  *  Rx functions
876  *
877  **********************************************************************/
878 static inline uint32_t
879 ngbe_rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint16_t ptid_mask)
880 {
881         uint16_t ptid = NGBE_RXD_PTID(pkt_info);
882
883         ptid &= ptid_mask;
884
885         return ngbe_decode_ptype(ptid);
886 }
887
888 static inline uint64_t
889 rx_desc_error_to_pkt_flags(uint32_t rx_status)
890 {
891         uint64_t pkt_flags = 0;
892
893         /* checksum offload can't be disabled */
894         if (rx_status & NGBE_RXD_STAT_IPCS)
895                 pkt_flags |= (rx_status & NGBE_RXD_ERR_IPCS
896                                 ? RTE_MBUF_F_RX_IP_CKSUM_BAD : RTE_MBUF_F_RX_IP_CKSUM_GOOD);
897
898         if (rx_status & NGBE_RXD_STAT_L4CS)
899                 pkt_flags |= (rx_status & NGBE_RXD_ERR_L4CS
900                                 ? RTE_MBUF_F_RX_L4_CKSUM_BAD : RTE_MBUF_F_RX_L4_CKSUM_GOOD);
901
902         if (rx_status & NGBE_RXD_STAT_EIPCS &&
903             rx_status & NGBE_RXD_ERR_EIPCS)
904                 pkt_flags |= RTE_MBUF_F_RX_OUTER_IP_CKSUM_BAD;
905
906         return pkt_flags;
907 }
908
909 /*
910  * LOOK_AHEAD defines how many desc statuses to check beyond the
911  * current descriptor.
912  * It must be a pound define for optimal performance.
913  * Do not change the value of LOOK_AHEAD, as the ngbe_rx_scan_hw_ring
914  * function only works with LOOK_AHEAD=8.
915  */
916 #define LOOK_AHEAD 8
917 #if (LOOK_AHEAD != 8)
918 #error "PMD NGBE: LOOK_AHEAD must be 8\n"
919 #endif
920 static inline int
921 ngbe_rx_scan_hw_ring(struct ngbe_rx_queue *rxq)
922 {
923         volatile struct ngbe_rx_desc *rxdp;
924         struct ngbe_rx_entry *rxep;
925         struct rte_mbuf *mb;
926         uint16_t pkt_len;
927         uint64_t pkt_flags;
928         int nb_dd;
929         uint32_t s[LOOK_AHEAD];
930         uint32_t pkt_info[LOOK_AHEAD];
931         int i, j, nb_rx = 0;
932         uint32_t status;
933
934         /* get references to current descriptor and S/W ring entry */
935         rxdp = &rxq->rx_ring[rxq->rx_tail];
936         rxep = &rxq->sw_ring[rxq->rx_tail];
937
938         status = rxdp->qw1.lo.status;
939         /* check to make sure there is at least 1 packet to receive */
940         if (!(status & rte_cpu_to_le_32(NGBE_RXD_STAT_DD)))
941                 return 0;
942
943         /*
944          * Scan LOOK_AHEAD descriptors at a time to determine which descriptors
945          * reference packets that are ready to be received.
946          */
947         for (i = 0; i < RTE_PMD_NGBE_RX_MAX_BURST;
948              i += LOOK_AHEAD, rxdp += LOOK_AHEAD, rxep += LOOK_AHEAD) {
949                 /* Read desc statuses backwards to avoid race condition */
950                 for (j = 0; j < LOOK_AHEAD; j++)
951                         s[j] = rte_le_to_cpu_32(rxdp[j].qw1.lo.status);
952
953                 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
954
955                 /* Compute how many status bits were set */
956                 for (nb_dd = 0; nb_dd < LOOK_AHEAD &&
957                                 (s[nb_dd] & NGBE_RXD_STAT_DD); nb_dd++)
958                         ;
959
960                 for (j = 0; j < nb_dd; j++)
961                         pkt_info[j] = rte_le_to_cpu_32(rxdp[j].qw0.dw0);
962
963                 nb_rx += nb_dd;
964
965                 /* Translate descriptor info to mbuf format */
966                 for (j = 0; j < nb_dd; ++j) {
967                         mb = rxep[j].mbuf;
968                         pkt_len = rte_le_to_cpu_16(rxdp[j].qw1.hi.len) -
969                                   rxq->crc_len;
970                         mb->data_len = pkt_len;
971                         mb->pkt_len = pkt_len;
972
973                         /* convert descriptor fields to rte mbuf flags */
974                         pkt_flags = rx_desc_error_to_pkt_flags(s[j]);
975                         mb->ol_flags = pkt_flags;
976                         mb->packet_type =
977                                 ngbe_rxd_pkt_info_to_pkt_type(pkt_info[j],
978                                 NGBE_PTID_MASK);
979                 }
980
981                 /* Move mbuf pointers from the S/W ring to the stage */
982                 for (j = 0; j < LOOK_AHEAD; ++j)
983                         rxq->rx_stage[i + j] = rxep[j].mbuf;
984
985                 /* stop if all requested packets could not be received */
986                 if (nb_dd != LOOK_AHEAD)
987                         break;
988         }
989
990         /* clear software ring entries so we can cleanup correctly */
991         for (i = 0; i < nb_rx; ++i)
992                 rxq->sw_ring[rxq->rx_tail + i].mbuf = NULL;
993
994         return nb_rx;
995 }
996
997 static inline int
998 ngbe_rx_alloc_bufs(struct ngbe_rx_queue *rxq, bool reset_mbuf)
999 {
1000         volatile struct ngbe_rx_desc *rxdp;
1001         struct ngbe_rx_entry *rxep;
1002         struct rte_mbuf *mb;
1003         uint16_t alloc_idx;
1004         __le64 dma_addr;
1005         int diag, i;
1006
1007         /* allocate buffers in bulk directly into the S/W ring */
1008         alloc_idx = rxq->rx_free_trigger - (rxq->rx_free_thresh - 1);
1009         rxep = &rxq->sw_ring[alloc_idx];
1010         diag = rte_mempool_get_bulk(rxq->mb_pool, (void *)rxep,
1011                                     rxq->rx_free_thresh);
1012         if (unlikely(diag != 0))
1013                 return -ENOMEM;
1014
1015         rxdp = &rxq->rx_ring[alloc_idx];
1016         for (i = 0; i < rxq->rx_free_thresh; ++i) {
1017                 /* populate the static rte mbuf fields */
1018                 mb = rxep[i].mbuf;
1019                 if (reset_mbuf)
1020                         mb->port = rxq->port_id;
1021
1022                 rte_mbuf_refcnt_set(mb, 1);
1023                 mb->data_off = RTE_PKTMBUF_HEADROOM;
1024
1025                 /* populate the descriptors */
1026                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mb));
1027                 NGBE_RXD_HDRADDR(&rxdp[i], 0);
1028                 NGBE_RXD_PKTADDR(&rxdp[i], dma_addr);
1029         }
1030
1031         /* update state of internal queue structure */
1032         rxq->rx_free_trigger = rxq->rx_free_trigger + rxq->rx_free_thresh;
1033         if (rxq->rx_free_trigger >= rxq->nb_rx_desc)
1034                 rxq->rx_free_trigger = rxq->rx_free_thresh - 1;
1035
1036         /* no errors */
1037         return 0;
1038 }
1039
1040 static inline uint16_t
1041 ngbe_rx_fill_from_stage(struct ngbe_rx_queue *rxq, struct rte_mbuf **rx_pkts,
1042                          uint16_t nb_pkts)
1043 {
1044         struct rte_mbuf **stage = &rxq->rx_stage[rxq->rx_next_avail];
1045         int i;
1046
1047         /* how many packets are ready to return? */
1048         nb_pkts = (uint16_t)RTE_MIN(nb_pkts, rxq->rx_nb_avail);
1049
1050         /* copy mbuf pointers to the application's packet list */
1051         for (i = 0; i < nb_pkts; ++i)
1052                 rx_pkts[i] = stage[i];
1053
1054         /* update internal queue state */
1055         rxq->rx_nb_avail = (uint16_t)(rxq->rx_nb_avail - nb_pkts);
1056         rxq->rx_next_avail = (uint16_t)(rxq->rx_next_avail + nb_pkts);
1057
1058         return nb_pkts;
1059 }
1060
1061 static inline uint16_t
1062 ngbe_rx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1063              uint16_t nb_pkts)
1064 {
1065         struct ngbe_rx_queue *rxq = (struct ngbe_rx_queue *)rx_queue;
1066         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1067         uint16_t nb_rx = 0;
1068
1069         /* Any previously recv'd pkts will be returned from the Rx stage */
1070         if (rxq->rx_nb_avail)
1071                 return ngbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1072
1073         /* Scan the H/W ring for packets to receive */
1074         nb_rx = (uint16_t)ngbe_rx_scan_hw_ring(rxq);
1075
1076         /* update internal queue state */
1077         rxq->rx_next_avail = 0;
1078         rxq->rx_nb_avail = nb_rx;
1079         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_rx);
1080
1081         /* if required, allocate new buffers to replenish descriptors */
1082         if (rxq->rx_tail > rxq->rx_free_trigger) {
1083                 uint16_t cur_free_trigger = rxq->rx_free_trigger;
1084
1085                 if (ngbe_rx_alloc_bufs(rxq, true) != 0) {
1086                         int i, j;
1087
1088                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1089                                    "queue_id=%u", (uint16_t)rxq->port_id,
1090                                    (uint16_t)rxq->queue_id);
1091
1092                         dev->data->rx_mbuf_alloc_failed +=
1093                                 rxq->rx_free_thresh;
1094
1095                         /*
1096                          * Need to rewind any previous receives if we cannot
1097                          * allocate new buffers to replenish the old ones.
1098                          */
1099                         rxq->rx_nb_avail = 0;
1100                         rxq->rx_tail = (uint16_t)(rxq->rx_tail - nb_rx);
1101                         for (i = 0, j = rxq->rx_tail; i < nb_rx; ++i, ++j)
1102                                 rxq->sw_ring[j].mbuf = rxq->rx_stage[i];
1103
1104                         return 0;
1105                 }
1106
1107                 /* update tail pointer */
1108                 rte_wmb();
1109                 ngbe_set32_relaxed(rxq->rdt_reg_addr, cur_free_trigger);
1110         }
1111
1112         if (rxq->rx_tail >= rxq->nb_rx_desc)
1113                 rxq->rx_tail = 0;
1114
1115         /* received any packets this loop? */
1116         if (rxq->rx_nb_avail)
1117                 return ngbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1118
1119         return 0;
1120 }
1121
1122 /* split requests into chunks of size RTE_PMD_NGBE_RX_MAX_BURST */
1123 uint16_t
1124 ngbe_recv_pkts_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1125                            uint16_t nb_pkts)
1126 {
1127         uint16_t nb_rx;
1128
1129         if (unlikely(nb_pkts == 0))
1130                 return 0;
1131
1132         if (likely(nb_pkts <= RTE_PMD_NGBE_RX_MAX_BURST))
1133                 return ngbe_rx_recv_pkts(rx_queue, rx_pkts, nb_pkts);
1134
1135         /* request is relatively large, chunk it up */
1136         nb_rx = 0;
1137         while (nb_pkts) {
1138                 uint16_t ret, n;
1139
1140                 n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_NGBE_RX_MAX_BURST);
1141                 ret = ngbe_rx_recv_pkts(rx_queue, &rx_pkts[nb_rx], n);
1142                 nb_rx = (uint16_t)(nb_rx + ret);
1143                 nb_pkts = (uint16_t)(nb_pkts - ret);
1144                 if (ret < n)
1145                         break;
1146         }
1147
1148         return nb_rx;
1149 }
1150
1151 uint16_t
1152 ngbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1153                 uint16_t nb_pkts)
1154 {
1155         struct ngbe_rx_queue *rxq;
1156         volatile struct ngbe_rx_desc *rx_ring;
1157         volatile struct ngbe_rx_desc *rxdp;
1158         struct ngbe_rx_entry *sw_ring;
1159         struct ngbe_rx_entry *rxe;
1160         struct rte_mbuf *rxm;
1161         struct rte_mbuf *nmb;
1162         struct ngbe_rx_desc rxd;
1163         uint64_t dma_addr;
1164         uint32_t staterr;
1165         uint32_t pkt_info;
1166         uint16_t pkt_len;
1167         uint16_t rx_id;
1168         uint16_t nb_rx;
1169         uint16_t nb_hold;
1170         uint64_t pkt_flags;
1171
1172         nb_rx = 0;
1173         nb_hold = 0;
1174         rxq = rx_queue;
1175         rx_id = rxq->rx_tail;
1176         rx_ring = rxq->rx_ring;
1177         sw_ring = rxq->sw_ring;
1178         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1179         while (nb_rx < nb_pkts) {
1180                 /*
1181                  * The order of operations here is important as the DD status
1182                  * bit must not be read after any other descriptor fields.
1183                  * rx_ring and rxdp are pointing to volatile data so the order
1184                  * of accesses cannot be reordered by the compiler. If they were
1185                  * not volatile, they could be reordered which could lead to
1186                  * using invalid descriptor fields when read from rxd.
1187                  */
1188                 rxdp = &rx_ring[rx_id];
1189                 staterr = rxdp->qw1.lo.status;
1190                 if (!(staterr & rte_cpu_to_le_32(NGBE_RXD_STAT_DD)))
1191                         break;
1192                 rxd = *rxdp;
1193
1194                 /*
1195                  * End of packet.
1196                  *
1197                  * If the NGBE_RXD_STAT_EOP flag is not set, the Rx packet
1198                  * is likely to be invalid and to be dropped by the various
1199                  * validation checks performed by the network stack.
1200                  *
1201                  * Allocate a new mbuf to replenish the RX ring descriptor.
1202                  * If the allocation fails:
1203                  *    - arrange for that Rx descriptor to be the first one
1204                  *      being parsed the next time the receive function is
1205                  *      invoked [on the same queue].
1206                  *
1207                  *    - Stop parsing the Rx ring and return immediately.
1208                  *
1209                  * This policy do not drop the packet received in the Rx
1210                  * descriptor for which the allocation of a new mbuf failed.
1211                  * Thus, it allows that packet to be later retrieved if
1212                  * mbuf have been freed in the mean time.
1213                  * As a side effect, holding Rx descriptors instead of
1214                  * systematically giving them back to the NIC may lead to
1215                  * Rx ring exhaustion situations.
1216                  * However, the NIC can gracefully prevent such situations
1217                  * to happen by sending specific "back-pressure" flow control
1218                  * frames to its peer(s).
1219                  */
1220                 PMD_RX_LOG(DEBUG,
1221                            "port_id=%u queue_id=%u rx_id=%u ext_err_stat=0x%08x pkt_len=%u",
1222                            (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1223                            (uint16_t)rx_id, (uint32_t)staterr,
1224                            (uint16_t)rte_le_to_cpu_16(rxd.qw1.hi.len));
1225
1226                 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1227                 if (nmb == NULL) {
1228                         PMD_RX_LOG(DEBUG,
1229                                    "Rx mbuf alloc failed port_id=%u queue_id=%u",
1230                                    (uint16_t)rxq->port_id,
1231                                    (uint16_t)rxq->queue_id);
1232                         dev->data->rx_mbuf_alloc_failed++;
1233                         break;
1234                 }
1235
1236                 nb_hold++;
1237                 rxe = &sw_ring[rx_id];
1238                 rx_id++;
1239                 if (rx_id == rxq->nb_rx_desc)
1240                         rx_id = 0;
1241
1242                 /* Prefetch next mbuf while processing current one. */
1243                 rte_ngbe_prefetch(sw_ring[rx_id].mbuf);
1244
1245                 /*
1246                  * When next Rx descriptor is on a cache-line boundary,
1247                  * prefetch the next 4 Rx descriptors and the next 8 pointers
1248                  * to mbufs.
1249                  */
1250                 if ((rx_id & 0x3) == 0) {
1251                         rte_ngbe_prefetch(&rx_ring[rx_id]);
1252                         rte_ngbe_prefetch(&sw_ring[rx_id]);
1253                 }
1254
1255                 rxm = rxe->mbuf;
1256                 rxe->mbuf = nmb;
1257                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1258                 NGBE_RXD_HDRADDR(rxdp, 0);
1259                 NGBE_RXD_PKTADDR(rxdp, dma_addr);
1260
1261                 /*
1262                  * Initialize the returned mbuf.
1263                  * 1) setup generic mbuf fields:
1264                  *    - number of segments,
1265                  *    - next segment,
1266                  *    - packet length,
1267                  *    - Rx port identifier.
1268                  * 2) integrate hardware offload data, if any:
1269                  *    - IP checksum flag,
1270                  *    - error flags.
1271                  */
1272                 pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.qw1.hi.len) -
1273                                       rxq->crc_len);
1274                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1275                 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
1276                 rxm->nb_segs = 1;
1277                 rxm->next = NULL;
1278                 rxm->pkt_len = pkt_len;
1279                 rxm->data_len = pkt_len;
1280                 rxm->port = rxq->port_id;
1281
1282                 pkt_info = rte_le_to_cpu_32(rxd.qw0.dw0);
1283                 pkt_flags = rx_desc_error_to_pkt_flags(staterr);
1284                 rxm->ol_flags = pkt_flags;
1285                 rxm->packet_type = ngbe_rxd_pkt_info_to_pkt_type(pkt_info,
1286                                                        NGBE_PTID_MASK);
1287
1288                 /*
1289                  * Store the mbuf address into the next entry of the array
1290                  * of returned packets.
1291                  */
1292                 rx_pkts[nb_rx++] = rxm;
1293         }
1294         rxq->rx_tail = rx_id;
1295
1296         /*
1297          * If the number of free Rx descriptors is greater than the Rx free
1298          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1299          * register.
1300          * Update the RDT with the value of the last processed Rx descriptor
1301          * minus 1, to guarantee that the RDT register is never equal to the
1302          * RDH register, which creates a "full" ring situation from the
1303          * hardware point of view...
1304          */
1305         nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
1306         if (nb_hold > rxq->rx_free_thresh) {
1307                 PMD_RX_LOG(DEBUG,
1308                            "port_id=%u queue_id=%u rx_tail=%u nb_hold=%u nb_rx=%u",
1309                            (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1310                            (uint16_t)rx_id, (uint16_t)nb_hold,
1311                            (uint16_t)nb_rx);
1312                 rx_id = (uint16_t)((rx_id == 0) ?
1313                                 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1314                 ngbe_set32(rxq->rdt_reg_addr, rx_id);
1315                 nb_hold = 0;
1316         }
1317         rxq->nb_rx_hold = nb_hold;
1318         return nb_rx;
1319 }
1320
1321 /**
1322  * ngbe_fill_cluster_head_buf - fill the first mbuf of the returned packet
1323  *
1324  * Fill the following info in the HEAD buffer of the Rx cluster:
1325  *    - RX port identifier
1326  *    - hardware offload data, if any:
1327  *      - IP checksum flag
1328  *      - error flags
1329  * @head HEAD of the packet cluster
1330  * @desc HW descriptor to get data from
1331  * @rxq Pointer to the Rx queue
1332  */
1333 static inline void
1334 ngbe_fill_cluster_head_buf(struct rte_mbuf *head, struct ngbe_rx_desc *desc,
1335                 struct ngbe_rx_queue *rxq, uint32_t staterr)
1336 {
1337         uint32_t pkt_info;
1338         uint64_t pkt_flags;
1339
1340         head->port = rxq->port_id;
1341
1342         pkt_info = rte_le_to_cpu_32(desc->qw0.dw0);
1343         pkt_flags = rx_desc_error_to_pkt_flags(staterr);
1344         head->ol_flags = pkt_flags;
1345         head->packet_type = ngbe_rxd_pkt_info_to_pkt_type(pkt_info,
1346                                                 NGBE_PTID_MASK);
1347 }
1348
1349 /**
1350  * ngbe_recv_pkts_sc - receive handler for scatter case.
1351  *
1352  * @rx_queue Rx queue handle
1353  * @rx_pkts table of received packets
1354  * @nb_pkts size of rx_pkts table
1355  * @bulk_alloc if TRUE bulk allocation is used for a HW ring refilling
1356  *
1357  * Returns the number of received packets/clusters (according to the "bulk
1358  * receive" interface).
1359  */
1360 static inline uint16_t
1361 ngbe_recv_pkts_sc(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts,
1362                     bool bulk_alloc)
1363 {
1364         struct ngbe_rx_queue *rxq = rx_queue;
1365         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1366         volatile struct ngbe_rx_desc *rx_ring = rxq->rx_ring;
1367         struct ngbe_rx_entry *sw_ring = rxq->sw_ring;
1368         struct ngbe_scattered_rx_entry *sw_sc_ring = rxq->sw_sc_ring;
1369         uint16_t rx_id = rxq->rx_tail;
1370         uint16_t nb_rx = 0;
1371         uint16_t nb_hold = rxq->nb_rx_hold;
1372         uint16_t prev_id = rxq->rx_tail;
1373
1374         while (nb_rx < nb_pkts) {
1375                 bool eop;
1376                 struct ngbe_rx_entry *rxe;
1377                 struct ngbe_scattered_rx_entry *sc_entry;
1378                 struct ngbe_scattered_rx_entry *next_sc_entry = NULL;
1379                 struct ngbe_rx_entry *next_rxe = NULL;
1380                 struct rte_mbuf *first_seg;
1381                 struct rte_mbuf *rxm;
1382                 struct rte_mbuf *nmb = NULL;
1383                 struct ngbe_rx_desc rxd;
1384                 uint16_t data_len;
1385                 uint16_t next_id;
1386                 volatile struct ngbe_rx_desc *rxdp;
1387                 uint32_t staterr;
1388
1389 next_desc:
1390                 rxdp = &rx_ring[rx_id];
1391                 staterr = rte_le_to_cpu_32(rxdp->qw1.lo.status);
1392
1393                 if (!(staterr & NGBE_RXD_STAT_DD))
1394                         break;
1395
1396                 rxd = *rxdp;
1397
1398                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1399                                   "staterr=0x%x data_len=%u",
1400                            rxq->port_id, rxq->queue_id, rx_id, staterr,
1401                            rte_le_to_cpu_16(rxd.qw1.hi.len));
1402
1403                 if (!bulk_alloc) {
1404                         nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1405                         if (nmb == NULL) {
1406                                 PMD_RX_LOG(DEBUG, "Rx mbuf alloc failed "
1407                                                   "port_id=%u queue_id=%u",
1408                                            rxq->port_id, rxq->queue_id);
1409
1410                                 dev->data->rx_mbuf_alloc_failed++;
1411                                 break;
1412                         }
1413                 } else if (nb_hold > rxq->rx_free_thresh) {
1414                         uint16_t next_rdt = rxq->rx_free_trigger;
1415
1416                         if (!ngbe_rx_alloc_bufs(rxq, false)) {
1417                                 rte_wmb();
1418                                 ngbe_set32_relaxed(rxq->rdt_reg_addr,
1419                                                             next_rdt);
1420                                 nb_hold -= rxq->rx_free_thresh;
1421                         } else {
1422                                 PMD_RX_LOG(DEBUG, "Rx bulk alloc failed "
1423                                                   "port_id=%u queue_id=%u",
1424                                            rxq->port_id, rxq->queue_id);
1425
1426                                 dev->data->rx_mbuf_alloc_failed++;
1427                                 break;
1428                         }
1429                 }
1430
1431                 nb_hold++;
1432                 rxe = &sw_ring[rx_id];
1433                 eop = staterr & NGBE_RXD_STAT_EOP;
1434
1435                 next_id = rx_id + 1;
1436                 if (next_id == rxq->nb_rx_desc)
1437                         next_id = 0;
1438
1439                 /* Prefetch next mbuf while processing current one. */
1440                 rte_ngbe_prefetch(sw_ring[next_id].mbuf);
1441
1442                 /*
1443                  * When next Rx descriptor is on a cache-line boundary,
1444                  * prefetch the next 4 RX descriptors and the next 4 pointers
1445                  * to mbufs.
1446                  */
1447                 if ((next_id & 0x3) == 0) {
1448                         rte_ngbe_prefetch(&rx_ring[next_id]);
1449                         rte_ngbe_prefetch(&sw_ring[next_id]);
1450                 }
1451
1452                 rxm = rxe->mbuf;
1453
1454                 if (!bulk_alloc) {
1455                         __le64 dma =
1456                           rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1457                         /*
1458                          * Update Rx descriptor with the physical address of the
1459                          * new data buffer of the new allocated mbuf.
1460                          */
1461                         rxe->mbuf = nmb;
1462
1463                         rxm->data_off = RTE_PKTMBUF_HEADROOM;
1464                         NGBE_RXD_HDRADDR(rxdp, 0);
1465                         NGBE_RXD_PKTADDR(rxdp, dma);
1466                 } else {
1467                         rxe->mbuf = NULL;
1468                 }
1469
1470                 /*
1471                  * Set data length & data buffer address of mbuf.
1472                  */
1473                 data_len = rte_le_to_cpu_16(rxd.qw1.hi.len);
1474                 rxm->data_len = data_len;
1475
1476                 if (!eop) {
1477                         uint16_t nextp_id;
1478
1479                         nextp_id = next_id;
1480                         next_sc_entry = &sw_sc_ring[nextp_id];
1481                         next_rxe = &sw_ring[nextp_id];
1482                         rte_ngbe_prefetch(next_rxe);
1483                 }
1484
1485                 sc_entry = &sw_sc_ring[rx_id];
1486                 first_seg = sc_entry->fbuf;
1487                 sc_entry->fbuf = NULL;
1488
1489                 /*
1490                  * If this is the first buffer of the received packet,
1491                  * set the pointer to the first mbuf of the packet and
1492                  * initialize its context.
1493                  * Otherwise, update the total length and the number of segments
1494                  * of the current scattered packet, and update the pointer to
1495                  * the last mbuf of the current packet.
1496                  */
1497                 if (first_seg == NULL) {
1498                         first_seg = rxm;
1499                         first_seg->pkt_len = data_len;
1500                         first_seg->nb_segs = 1;
1501                 } else {
1502                         first_seg->pkt_len += data_len;
1503                         first_seg->nb_segs++;
1504                 }
1505
1506                 prev_id = rx_id;
1507                 rx_id = next_id;
1508
1509                 /*
1510                  * If this is not the last buffer of the received packet, update
1511                  * the pointer to the first mbuf at the NEXTP entry in the
1512                  * sw_sc_ring and continue to parse the Rx ring.
1513                  */
1514                 if (!eop && next_rxe) {
1515                         rxm->next = next_rxe->mbuf;
1516                         next_sc_entry->fbuf = first_seg;
1517                         goto next_desc;
1518                 }
1519
1520                 /* Initialize the first mbuf of the returned packet */
1521                 ngbe_fill_cluster_head_buf(first_seg, &rxd, rxq, staterr);
1522
1523                 /* Deal with the case, when HW CRC srip is disabled. */
1524                 first_seg->pkt_len -= rxq->crc_len;
1525                 if (unlikely(rxm->data_len <= rxq->crc_len)) {
1526                         struct rte_mbuf *lp;
1527
1528                         for (lp = first_seg; lp->next != rxm; lp = lp->next)
1529                                 ;
1530
1531                         first_seg->nb_segs--;
1532                         lp->data_len -= rxq->crc_len - rxm->data_len;
1533                         lp->next = NULL;
1534                         rte_pktmbuf_free_seg(rxm);
1535                 } else {
1536                         rxm->data_len -= rxq->crc_len;
1537                 }
1538
1539                 /* Prefetch data of first segment, if configured to do so. */
1540                 rte_packet_prefetch((char *)first_seg->buf_addr +
1541                         first_seg->data_off);
1542
1543                 /*
1544                  * Store the mbuf address into the next entry of the array
1545                  * of returned packets.
1546                  */
1547                 rx_pkts[nb_rx++] = first_seg;
1548         }
1549
1550         /*
1551          * Record index of the next Rx descriptor to probe.
1552          */
1553         rxq->rx_tail = rx_id;
1554
1555         /*
1556          * If the number of free Rx descriptors is greater than the Rx free
1557          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1558          * register.
1559          * Update the RDT with the value of the last processed Rx descriptor
1560          * minus 1, to guarantee that the RDT register is never equal to the
1561          * RDH register, which creates a "full" ring situation from the
1562          * hardware point of view...
1563          */
1564         if (!bulk_alloc && nb_hold > rxq->rx_free_thresh) {
1565                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1566                            "nb_hold=%u nb_rx=%u",
1567                            rxq->port_id, rxq->queue_id, rx_id, nb_hold, nb_rx);
1568
1569                 rte_wmb();
1570                 ngbe_set32_relaxed(rxq->rdt_reg_addr, prev_id);
1571                 nb_hold = 0;
1572         }
1573
1574         rxq->nb_rx_hold = nb_hold;
1575         return nb_rx;
1576 }
1577
1578 uint16_t
1579 ngbe_recv_pkts_sc_single_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1580                                  uint16_t nb_pkts)
1581 {
1582         return ngbe_recv_pkts_sc(rx_queue, rx_pkts, nb_pkts, false);
1583 }
1584
1585 uint16_t
1586 ngbe_recv_pkts_sc_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1587                                uint16_t nb_pkts)
1588 {
1589         return ngbe_recv_pkts_sc(rx_queue, rx_pkts, nb_pkts, true);
1590 }
1591
1592 /*********************************************************************
1593  *
1594  *  Queue management functions
1595  *
1596  **********************************************************************/
1597
1598 static void
1599 ngbe_tx_queue_release_mbufs(struct ngbe_tx_queue *txq)
1600 {
1601         unsigned int i;
1602
1603         if (txq->sw_ring != NULL) {
1604                 for (i = 0; i < txq->nb_tx_desc; i++) {
1605                         if (txq->sw_ring[i].mbuf != NULL) {
1606                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1607                                 txq->sw_ring[i].mbuf = NULL;
1608                         }
1609                 }
1610         }
1611 }
1612
1613 static void
1614 ngbe_tx_free_swring(struct ngbe_tx_queue *txq)
1615 {
1616         if (txq != NULL)
1617                 rte_free(txq->sw_ring);
1618 }
1619
1620 static void
1621 ngbe_tx_queue_release(struct ngbe_tx_queue *txq)
1622 {
1623         if (txq != NULL) {
1624                 if (txq->ops != NULL) {
1625                         txq->ops->release_mbufs(txq);
1626                         txq->ops->free_swring(txq);
1627                 }
1628                 rte_free(txq);
1629         }
1630 }
1631
1632 void
1633 ngbe_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1634 {
1635         ngbe_tx_queue_release(dev->data->tx_queues[qid]);
1636 }
1637
1638 /* (Re)set dynamic ngbe_tx_queue fields to defaults */
1639 static void
1640 ngbe_reset_tx_queue(struct ngbe_tx_queue *txq)
1641 {
1642         static const struct ngbe_tx_desc zeroed_desc = {0};
1643         struct ngbe_tx_entry *txe = txq->sw_ring;
1644         uint16_t prev, i;
1645
1646         /* Zero out HW ring memory */
1647         for (i = 0; i < txq->nb_tx_desc; i++)
1648                 txq->tx_ring[i] = zeroed_desc;
1649
1650         /* Initialize SW ring entries */
1651         prev = (uint16_t)(txq->nb_tx_desc - 1);
1652         for (i = 0; i < txq->nb_tx_desc; i++) {
1653                 /* the ring can also be modified by hardware */
1654                 volatile struct ngbe_tx_desc *txd = &txq->tx_ring[i];
1655
1656                 txd->dw3 = rte_cpu_to_le_32(NGBE_TXD_DD);
1657                 txe[i].mbuf = NULL;
1658                 txe[i].last_id = i;
1659                 txe[prev].next_id = i;
1660                 prev = i;
1661         }
1662
1663         txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
1664         txq->tx_tail = 0;
1665
1666         /*
1667          * Always allow 1 descriptor to be un-allocated to avoid
1668          * a H/W race condition
1669          */
1670         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
1671         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
1672         txq->ctx_curr = 0;
1673         memset((void *)&txq->ctx_cache, 0,
1674                 NGBE_CTX_NUM * sizeof(struct ngbe_ctx_info));
1675 }
1676
1677 static const struct ngbe_txq_ops def_txq_ops = {
1678         .release_mbufs = ngbe_tx_queue_release_mbufs,
1679         .free_swring = ngbe_tx_free_swring,
1680         .reset = ngbe_reset_tx_queue,
1681 };
1682
1683 /* Takes an ethdev and a queue and sets up the tx function to be used based on
1684  * the queue parameters. Used in tx_queue_setup by primary process and then
1685  * in dev_init by secondary process when attaching to an existing ethdev.
1686  */
1687 void
1688 ngbe_set_tx_function(struct rte_eth_dev *dev, struct ngbe_tx_queue *txq)
1689 {
1690         /* Use a simple Tx queue (no offloads, no multi segs) if possible */
1691         if (txq->offloads == 0 &&
1692                         txq->tx_free_thresh >= RTE_PMD_NGBE_TX_MAX_BURST) {
1693                 PMD_INIT_LOG(DEBUG, "Using simple tx code path");
1694                 dev->tx_pkt_burst = ngbe_xmit_pkts_simple;
1695                 dev->tx_pkt_prepare = NULL;
1696         } else {
1697                 PMD_INIT_LOG(DEBUG, "Using full-featured tx code path");
1698                 PMD_INIT_LOG(DEBUG,
1699                                 " - offloads = 0x%" PRIx64,
1700                                 txq->offloads);
1701                 PMD_INIT_LOG(DEBUG,
1702                                 " - tx_free_thresh = %lu [RTE_PMD_NGBE_TX_MAX_BURST=%lu]",
1703                                 (unsigned long)txq->tx_free_thresh,
1704                                 (unsigned long)RTE_PMD_NGBE_TX_MAX_BURST);
1705                 dev->tx_pkt_burst = ngbe_xmit_pkts;
1706                 dev->tx_pkt_prepare = ngbe_prep_pkts;
1707         }
1708 }
1709
1710 static const struct {
1711         eth_tx_burst_t pkt_burst;
1712         const char *info;
1713 } ngbe_tx_burst_infos[] = {
1714         { ngbe_xmit_pkts_simple,   "Scalar Simple"},
1715         { ngbe_xmit_pkts,          "Scalar"},
1716 };
1717
1718 int
1719 ngbe_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
1720                       struct rte_eth_burst_mode *mode)
1721 {
1722         eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
1723         int ret = -EINVAL;
1724         unsigned int i;
1725
1726         for (i = 0; i < RTE_DIM(ngbe_tx_burst_infos); ++i) {
1727                 if (pkt_burst == ngbe_tx_burst_infos[i].pkt_burst) {
1728                         snprintf(mode->info, sizeof(mode->info), "%s",
1729                                  ngbe_tx_burst_infos[i].info);
1730                         ret = 0;
1731                         break;
1732                 }
1733         }
1734
1735         return ret;
1736 }
1737
1738 uint64_t
1739 ngbe_get_tx_port_offloads(struct rte_eth_dev *dev)
1740 {
1741         uint64_t tx_offload_capa;
1742
1743         RTE_SET_USED(dev);
1744
1745         tx_offload_capa =
1746                 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM  |
1747                 RTE_ETH_TX_OFFLOAD_UDP_CKSUM   |
1748                 RTE_ETH_TX_OFFLOAD_TCP_CKSUM   |
1749                 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM  |
1750                 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM |
1751                 RTE_ETH_TX_OFFLOAD_TCP_TSO     |
1752                 RTE_ETH_TX_OFFLOAD_UDP_TSO         |
1753                 RTE_ETH_TX_OFFLOAD_UDP_TNL_TSO  |
1754                 RTE_ETH_TX_OFFLOAD_IP_TNL_TSO   |
1755                 RTE_ETH_TX_OFFLOAD_IPIP_TNL_TSO |
1756                 RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
1757
1758         return tx_offload_capa;
1759 }
1760
1761 int
1762 ngbe_dev_tx_queue_setup(struct rte_eth_dev *dev,
1763                          uint16_t queue_idx,
1764                          uint16_t nb_desc,
1765                          unsigned int socket_id,
1766                          const struct rte_eth_txconf *tx_conf)
1767 {
1768         const struct rte_memzone *tz;
1769         struct ngbe_tx_queue *txq;
1770         struct ngbe_hw     *hw;
1771         uint16_t tx_free_thresh;
1772         uint64_t offloads;
1773
1774         PMD_INIT_FUNC_TRACE();
1775         hw = ngbe_dev_hw(dev);
1776
1777         offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1778
1779         /*
1780          * The Tx descriptor ring will be cleaned after txq->tx_free_thresh
1781          * descriptors are used or if the number of descriptors required
1782          * to transmit a packet is greater than the number of free Tx
1783          * descriptors.
1784          * One descriptor in the Tx ring is used as a sentinel to avoid a
1785          * H/W race condition, hence the maximum threshold constraints.
1786          * When set to zero use default values.
1787          */
1788         tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
1789                         tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH);
1790         if (tx_free_thresh >= (nb_desc - 3)) {
1791                 PMD_INIT_LOG(ERR,
1792                              "tx_free_thresh must be less than the number of TX descriptors minus 3. (tx_free_thresh=%u port=%d queue=%d)",
1793                              (unsigned int)tx_free_thresh,
1794                              (int)dev->data->port_id, (int)queue_idx);
1795                 return -(EINVAL);
1796         }
1797
1798         if (nb_desc % tx_free_thresh != 0) {
1799                 PMD_INIT_LOG(ERR,
1800                              "tx_free_thresh must be a divisor of the number of Tx descriptors. (tx_free_thresh=%u port=%d queue=%d)",
1801                              (unsigned int)tx_free_thresh,
1802                              (int)dev->data->port_id, (int)queue_idx);
1803                 return -(EINVAL);
1804         }
1805
1806         /* Free memory prior to re-allocation if needed... */
1807         if (dev->data->tx_queues[queue_idx] != NULL) {
1808                 ngbe_tx_queue_release(dev->data->tx_queues[queue_idx]);
1809                 dev->data->tx_queues[queue_idx] = NULL;
1810         }
1811
1812         /* First allocate the Tx queue data structure */
1813         txq = rte_zmalloc_socket("ethdev Tx queue",
1814                                  sizeof(struct ngbe_tx_queue),
1815                                  RTE_CACHE_LINE_SIZE, socket_id);
1816         if (txq == NULL)
1817                 return -ENOMEM;
1818
1819         /*
1820          * Allocate Tx ring hardware descriptors. A memzone large enough to
1821          * handle the maximum ring size is allocated in order to allow for
1822          * resizing in later calls to the queue setup function.
1823          */
1824         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
1825                         sizeof(struct ngbe_tx_desc) * NGBE_RING_DESC_MAX,
1826                         NGBE_ALIGN, socket_id);
1827         if (tz == NULL) {
1828                 ngbe_tx_queue_release(txq);
1829                 return -ENOMEM;
1830         }
1831
1832         txq->nb_tx_desc = nb_desc;
1833         txq->tx_free_thresh = tx_free_thresh;
1834         txq->pthresh = tx_conf->tx_thresh.pthresh;
1835         txq->hthresh = tx_conf->tx_thresh.hthresh;
1836         txq->wthresh = tx_conf->tx_thresh.wthresh;
1837         txq->queue_id = queue_idx;
1838         txq->reg_idx = queue_idx;
1839         txq->port_id = dev->data->port_id;
1840         txq->offloads = offloads;
1841         txq->ops = &def_txq_ops;
1842         txq->tx_deferred_start = tx_conf->tx_deferred_start;
1843
1844         txq->tdt_reg_addr = NGBE_REG_ADDR(hw, NGBE_TXWP(txq->reg_idx));
1845         txq->tdc_reg_addr = NGBE_REG_ADDR(hw, NGBE_TXCFG(txq->reg_idx));
1846
1847         txq->tx_ring_phys_addr = TMZ_PADDR(tz);
1848         txq->tx_ring = (struct ngbe_tx_desc *)TMZ_VADDR(tz);
1849
1850         /* Allocate software ring */
1851         txq->sw_ring = rte_zmalloc_socket("txq->sw_ring",
1852                                 sizeof(struct ngbe_tx_entry) * nb_desc,
1853                                 RTE_CACHE_LINE_SIZE, socket_id);
1854         if (txq->sw_ring == NULL) {
1855                 ngbe_tx_queue_release(txq);
1856                 return -ENOMEM;
1857         }
1858         PMD_INIT_LOG(DEBUG,
1859                      "sw_ring=%p hw_ring=%p dma_addr=0x%" PRIx64,
1860                      txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1861
1862         /* set up scalar Tx function as appropriate */
1863         ngbe_set_tx_function(dev, txq);
1864
1865         txq->ops->reset(txq);
1866
1867         dev->data->tx_queues[queue_idx] = txq;
1868
1869         return 0;
1870 }
1871
1872 /**
1873  * ngbe_free_sc_cluster - free the not-yet-completed scattered cluster
1874  *
1875  * The "next" pointer of the last segment of (not-yet-completed) RSC clusters
1876  * in the sw_sc_ring is not set to NULL but rather points to the next
1877  * mbuf of this RSC aggregation (that has not been completed yet and still
1878  * resides on the HW ring). So, instead of calling for rte_pktmbuf_free() we
1879  * will just free first "nb_segs" segments of the cluster explicitly by calling
1880  * an rte_pktmbuf_free_seg().
1881  *
1882  * @m scattered cluster head
1883  */
1884 static void
1885 ngbe_free_sc_cluster(struct rte_mbuf *m)
1886 {
1887         uint16_t i, nb_segs = m->nb_segs;
1888         struct rte_mbuf *next_seg;
1889
1890         for (i = 0; i < nb_segs; i++) {
1891                 next_seg = m->next;
1892                 rte_pktmbuf_free_seg(m);
1893                 m = next_seg;
1894         }
1895 }
1896
1897 static void
1898 ngbe_rx_queue_release_mbufs(struct ngbe_rx_queue *rxq)
1899 {
1900         unsigned int i;
1901
1902         if (rxq->sw_ring != NULL) {
1903                 for (i = 0; i < rxq->nb_rx_desc; i++) {
1904                         if (rxq->sw_ring[i].mbuf != NULL) {
1905                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1906                                 rxq->sw_ring[i].mbuf = NULL;
1907                         }
1908                 }
1909                 for (i = 0; i < rxq->rx_nb_avail; ++i) {
1910                         struct rte_mbuf *mb;
1911
1912                         mb = rxq->rx_stage[rxq->rx_next_avail + i];
1913                         rte_pktmbuf_free_seg(mb);
1914                 }
1915                 rxq->rx_nb_avail = 0;
1916         }
1917
1918         if (rxq->sw_sc_ring != NULL)
1919                 for (i = 0; i < rxq->nb_rx_desc; i++)
1920                         if (rxq->sw_sc_ring[i].fbuf != NULL) {
1921                                 ngbe_free_sc_cluster(rxq->sw_sc_ring[i].fbuf);
1922                                 rxq->sw_sc_ring[i].fbuf = NULL;
1923                         }
1924 }
1925
1926 static void
1927 ngbe_rx_queue_release(struct ngbe_rx_queue *rxq)
1928 {
1929         if (rxq != NULL) {
1930                 ngbe_rx_queue_release_mbufs(rxq);
1931                 rte_free(rxq->sw_ring);
1932                 rte_free(rxq->sw_sc_ring);
1933                 rte_free(rxq);
1934         }
1935 }
1936
1937 void
1938 ngbe_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1939 {
1940         ngbe_rx_queue_release(dev->data->rx_queues[qid]);
1941 }
1942
1943 /*
1944  * Check if Rx Burst Bulk Alloc function can be used.
1945  * Return
1946  *        0: the preconditions are satisfied and the bulk allocation function
1947  *           can be used.
1948  *  -EINVAL: the preconditions are NOT satisfied and the default Rx burst
1949  *           function must be used.
1950  */
1951 static inline int
1952 check_rx_burst_bulk_alloc_preconditions(struct ngbe_rx_queue *rxq)
1953 {
1954         int ret = 0;
1955
1956         /*
1957          * Make sure the following pre-conditions are satisfied:
1958          *   rxq->rx_free_thresh >= RTE_PMD_NGBE_RX_MAX_BURST
1959          *   rxq->rx_free_thresh < rxq->nb_rx_desc
1960          *   (rxq->nb_rx_desc % rxq->rx_free_thresh) == 0
1961          * Scattered packets are not supported.  This should be checked
1962          * outside of this function.
1963          */
1964         if (rxq->rx_free_thresh < RTE_PMD_NGBE_RX_MAX_BURST) {
1965                 PMD_INIT_LOG(DEBUG,
1966                              "Rx Burst Bulk Alloc Preconditions: rxq->rx_free_thresh=%d, RTE_PMD_NGBE_RX_MAX_BURST=%d",
1967                              rxq->rx_free_thresh, RTE_PMD_NGBE_RX_MAX_BURST);
1968                 ret = -EINVAL;
1969         } else if (rxq->rx_free_thresh >= rxq->nb_rx_desc) {
1970                 PMD_INIT_LOG(DEBUG,
1971                              "Rx Burst Bulk Alloc Preconditions: rxq->rx_free_thresh=%d, rxq->nb_rx_desc=%d",
1972                              rxq->rx_free_thresh, rxq->nb_rx_desc);
1973                 ret = -EINVAL;
1974         } else if ((rxq->nb_rx_desc % rxq->rx_free_thresh) != 0) {
1975                 PMD_INIT_LOG(DEBUG,
1976                              "Rx Burst Bulk Alloc Preconditions: rxq->nb_rx_desc=%d, rxq->rx_free_thresh=%d",
1977                              rxq->nb_rx_desc, rxq->rx_free_thresh);
1978                 ret = -EINVAL;
1979         }
1980
1981         return ret;
1982 }
1983
1984 /* Reset dynamic ngbe_rx_queue fields back to defaults */
1985 static void
1986 ngbe_reset_rx_queue(struct ngbe_adapter *adapter, struct ngbe_rx_queue *rxq)
1987 {
1988         static const struct ngbe_rx_desc zeroed_desc = {
1989                                                 {{0}, {0} }, {{0}, {0} } };
1990         unsigned int i;
1991         uint16_t len = rxq->nb_rx_desc;
1992
1993         /*
1994          * By default, the Rx queue setup function allocates enough memory for
1995          * NGBE_RING_DESC_MAX.  The Rx Burst bulk allocation function requires
1996          * extra memory at the end of the descriptor ring to be zero'd out.
1997          */
1998         if (adapter->rx_bulk_alloc_allowed)
1999                 /* zero out extra memory */
2000                 len += RTE_PMD_NGBE_RX_MAX_BURST;
2001
2002         /*
2003          * Zero out HW ring memory. Zero out extra memory at the end of
2004          * the H/W ring so look-ahead logic in Rx Burst bulk alloc function
2005          * reads extra memory as zeros.
2006          */
2007         for (i = 0; i < len; i++)
2008                 rxq->rx_ring[i] = zeroed_desc;
2009
2010         /*
2011          * initialize extra software ring entries. Space for these extra
2012          * entries is always allocated
2013          */
2014         memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
2015         for (i = rxq->nb_rx_desc; i < len; ++i)
2016                 rxq->sw_ring[i].mbuf = &rxq->fake_mbuf;
2017
2018         rxq->rx_nb_avail = 0;
2019         rxq->rx_next_avail = 0;
2020         rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
2021         rxq->rx_tail = 0;
2022         rxq->nb_rx_hold = 0;
2023         rxq->pkt_first_seg = NULL;
2024         rxq->pkt_last_seg = NULL;
2025 }
2026
2027 uint64_t
2028 ngbe_get_rx_port_offloads(struct rte_eth_dev *dev __rte_unused)
2029 {
2030         uint64_t offloads;
2031
2032         offloads = RTE_ETH_RX_OFFLOAD_IPV4_CKSUM  |
2033                    RTE_ETH_RX_OFFLOAD_UDP_CKSUM   |
2034                    RTE_ETH_RX_OFFLOAD_TCP_CKSUM   |
2035                    RTE_ETH_RX_OFFLOAD_KEEP_CRC    |
2036                    RTE_ETH_RX_OFFLOAD_SCATTER;
2037
2038         return offloads;
2039 }
2040
2041 int
2042 ngbe_dev_rx_queue_setup(struct rte_eth_dev *dev,
2043                          uint16_t queue_idx,
2044                          uint16_t nb_desc,
2045                          unsigned int socket_id,
2046                          const struct rte_eth_rxconf *rx_conf,
2047                          struct rte_mempool *mp)
2048 {
2049         const struct rte_memzone *rz;
2050         struct ngbe_rx_queue *rxq;
2051         struct ngbe_hw     *hw;
2052         uint16_t len;
2053         struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2054
2055         PMD_INIT_FUNC_TRACE();
2056         hw = ngbe_dev_hw(dev);
2057
2058         /* Free memory prior to re-allocation if needed... */
2059         if (dev->data->rx_queues[queue_idx] != NULL) {
2060                 ngbe_rx_queue_release(dev->data->rx_queues[queue_idx]);
2061                 dev->data->rx_queues[queue_idx] = NULL;
2062         }
2063
2064         /* First allocate the Rx queue data structure */
2065         rxq = rte_zmalloc_socket("ethdev RX queue",
2066                                  sizeof(struct ngbe_rx_queue),
2067                                  RTE_CACHE_LINE_SIZE, socket_id);
2068         if (rxq == NULL)
2069                 return -ENOMEM;
2070         rxq->mb_pool = mp;
2071         rxq->nb_rx_desc = nb_desc;
2072         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
2073         rxq->queue_id = queue_idx;
2074         rxq->reg_idx = queue_idx;
2075         rxq->port_id = dev->data->port_id;
2076         if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2077                 rxq->crc_len = RTE_ETHER_CRC_LEN;
2078         else
2079                 rxq->crc_len = 0;
2080         rxq->drop_en = rx_conf->rx_drop_en;
2081         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
2082
2083         /*
2084          * Allocate Rx ring hardware descriptors. A memzone large enough to
2085          * handle the maximum ring size is allocated in order to allow for
2086          * resizing in later calls to the queue setup function.
2087          */
2088         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
2089                                       RX_RING_SZ, NGBE_ALIGN, socket_id);
2090         if (rz == NULL) {
2091                 ngbe_rx_queue_release(rxq);
2092                 return -ENOMEM;
2093         }
2094
2095         /*
2096          * Zero init all the descriptors in the ring.
2097          */
2098         memset(rz->addr, 0, RX_RING_SZ);
2099
2100         rxq->rdt_reg_addr = NGBE_REG_ADDR(hw, NGBE_RXWP(rxq->reg_idx));
2101         rxq->rdh_reg_addr = NGBE_REG_ADDR(hw, NGBE_RXRP(rxq->reg_idx));
2102
2103         rxq->rx_ring_phys_addr = TMZ_PADDR(rz);
2104         rxq->rx_ring = (struct ngbe_rx_desc *)TMZ_VADDR(rz);
2105
2106         /*
2107          * Certain constraints must be met in order to use the bulk buffer
2108          * allocation Rx burst function. If any of Rx queues doesn't meet them
2109          * the feature should be disabled for the whole port.
2110          */
2111         if (check_rx_burst_bulk_alloc_preconditions(rxq)) {
2112                 PMD_INIT_LOG(DEBUG,
2113                              "queue[%d] doesn't meet Rx Bulk Alloc preconditions - canceling the feature for the whole port[%d]",
2114                              rxq->queue_id, rxq->port_id);
2115                 adapter->rx_bulk_alloc_allowed = false;
2116         }
2117
2118         /*
2119          * Allocate software ring. Allow for space at the end of the
2120          * S/W ring to make sure look-ahead logic in bulk alloc Rx burst
2121          * function does not access an invalid memory region.
2122          */
2123         len = nb_desc;
2124         if (adapter->rx_bulk_alloc_allowed)
2125                 len += RTE_PMD_NGBE_RX_MAX_BURST;
2126
2127         rxq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
2128                                           sizeof(struct ngbe_rx_entry) * len,
2129                                           RTE_CACHE_LINE_SIZE, socket_id);
2130         if (rxq->sw_ring == NULL) {
2131                 ngbe_rx_queue_release(rxq);
2132                 return -ENOMEM;
2133         }
2134
2135         /*
2136          * Always allocate even if it's not going to be needed in order to
2137          * simplify the code.
2138          *
2139          * This ring is used in Scattered Rx cases and Scattered Rx may
2140          * be requested in ngbe_dev_rx_init(), which is called later from
2141          * dev_start() flow.
2142          */
2143         rxq->sw_sc_ring =
2144                 rte_zmalloc_socket("rxq->sw_sc_ring",
2145                                   sizeof(struct ngbe_scattered_rx_entry) * len,
2146                                   RTE_CACHE_LINE_SIZE, socket_id);
2147         if (rxq->sw_sc_ring == NULL) {
2148                 ngbe_rx_queue_release(rxq);
2149                 return -ENOMEM;
2150         }
2151
2152         PMD_INIT_LOG(DEBUG,
2153                      "sw_ring=%p sw_sc_ring=%p hw_ring=%p dma_addr=0x%" PRIx64,
2154                      rxq->sw_ring, rxq->sw_sc_ring, rxq->rx_ring,
2155                      rxq->rx_ring_phys_addr);
2156
2157         dev->data->rx_queues[queue_idx] = rxq;
2158
2159         ngbe_reset_rx_queue(adapter, rxq);
2160
2161         return 0;
2162 }
2163
2164 void
2165 ngbe_dev_clear_queues(struct rte_eth_dev *dev)
2166 {
2167         unsigned int i;
2168         struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2169
2170         PMD_INIT_FUNC_TRACE();
2171
2172         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2173                 struct ngbe_tx_queue *txq = dev->data->tx_queues[i];
2174
2175                 if (txq != NULL) {
2176                         txq->ops->release_mbufs(txq);
2177                         txq->ops->reset(txq);
2178                 }
2179         }
2180
2181         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2182                 struct ngbe_rx_queue *rxq = dev->data->rx_queues[i];
2183
2184                 if (rxq != NULL) {
2185                         ngbe_rx_queue_release_mbufs(rxq);
2186                         ngbe_reset_rx_queue(adapter, rxq);
2187                 }
2188         }
2189 }
2190
2191 void
2192 ngbe_dev_free_queues(struct rte_eth_dev *dev)
2193 {
2194         unsigned int i;
2195
2196         PMD_INIT_FUNC_TRACE();
2197
2198         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2199                 ngbe_dev_rx_queue_release(dev, i);
2200                 dev->data->rx_queues[i] = NULL;
2201         }
2202         dev->data->nb_rx_queues = 0;
2203
2204         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2205                 ngbe_dev_tx_queue_release(dev, i);
2206                 dev->data->tx_queues[i] = NULL;
2207         }
2208         dev->data->nb_tx_queues = 0;
2209 }
2210
2211 static int
2212 ngbe_alloc_rx_queue_mbufs(struct ngbe_rx_queue *rxq)
2213 {
2214         struct ngbe_rx_entry *rxe = rxq->sw_ring;
2215         uint64_t dma_addr;
2216         unsigned int i;
2217
2218         /* Initialize software ring entries */
2219         for (i = 0; i < rxq->nb_rx_desc; i++) {
2220                 /* the ring can also be modified by hardware */
2221                 volatile struct ngbe_rx_desc *rxd;
2222                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
2223
2224                 if (mbuf == NULL) {
2225                         PMD_INIT_LOG(ERR, "Rx mbuf alloc failed queue_id=%u port_id=%u",
2226                                      (unsigned int)rxq->queue_id,
2227                                      (unsigned int)rxq->port_id);
2228                         return -ENOMEM;
2229                 }
2230
2231                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
2232                 mbuf->port = rxq->port_id;
2233
2234                 dma_addr =
2235                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
2236                 rxd = &rxq->rx_ring[i];
2237                 NGBE_RXD_HDRADDR(rxd, 0);
2238                 NGBE_RXD_PKTADDR(rxd, dma_addr);
2239                 rxe[i].mbuf = mbuf;
2240         }
2241
2242         return 0;
2243 }
2244
2245 void
2246 ngbe_set_rx_function(struct rte_eth_dev *dev)
2247 {
2248         struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2249
2250         if (dev->data->scattered_rx) {
2251                 /*
2252                  * Set the scattered callback: there are bulk and
2253                  * single allocation versions.
2254                  */
2255                 if (adapter->rx_bulk_alloc_allowed) {
2256                         PMD_INIT_LOG(DEBUG, "Using a Scattered with bulk "
2257                                            "allocation callback (port=%d).",
2258                                      dev->data->port_id);
2259                         dev->rx_pkt_burst = ngbe_recv_pkts_sc_bulk_alloc;
2260                 } else {
2261                         PMD_INIT_LOG(DEBUG, "Using Regular (non-vector, "
2262                                             "single allocation) "
2263                                             "Scattered Rx callback "
2264                                             "(port=%d).",
2265                                      dev->data->port_id);
2266
2267                         dev->rx_pkt_burst = ngbe_recv_pkts_sc_single_alloc;
2268                 }
2269         /*
2270          * Below we set "simple" callbacks according to port/queues parameters.
2271          * If parameters allow we are going to choose between the following
2272          * callbacks:
2273          *    - Bulk Allocation
2274          *    - Single buffer allocation (the simplest one)
2275          */
2276         } else if (adapter->rx_bulk_alloc_allowed) {
2277                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
2278                                     "satisfied. Rx Burst Bulk Alloc function "
2279                                     "will be used on port=%d.",
2280                              dev->data->port_id);
2281
2282                 dev->rx_pkt_burst = ngbe_recv_pkts_bulk_alloc;
2283         } else {
2284                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are not "
2285                                     "satisfied, or Scattered Rx is requested "
2286                                     "(port=%d).",
2287                              dev->data->port_id);
2288
2289                 dev->rx_pkt_burst = ngbe_recv_pkts;
2290         }
2291 }
2292
2293 static const struct {
2294         eth_rx_burst_t pkt_burst;
2295         const char *info;
2296 } ngbe_rx_burst_infos[] = {
2297         { ngbe_recv_pkts_sc_single_alloc,    "Scalar Scattered"},
2298         { ngbe_recv_pkts_sc_bulk_alloc,      "Scalar Scattered Bulk Alloc"},
2299         { ngbe_recv_pkts_bulk_alloc,         "Scalar Bulk Alloc"},
2300         { ngbe_recv_pkts,                    "Scalar"},
2301 };
2302
2303 int
2304 ngbe_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
2305                       struct rte_eth_burst_mode *mode)
2306 {
2307         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2308         int ret = -EINVAL;
2309         unsigned int i;
2310
2311         for (i = 0; i < RTE_DIM(ngbe_rx_burst_infos); ++i) {
2312                 if (pkt_burst == ngbe_rx_burst_infos[i].pkt_burst) {
2313                         snprintf(mode->info, sizeof(mode->info), "%s",
2314                                  ngbe_rx_burst_infos[i].info);
2315                         ret = 0;
2316                         break;
2317                 }
2318         }
2319
2320         return ret;
2321 }
2322
2323 /*
2324  * Initializes Receive Unit.
2325  */
2326 int
2327 ngbe_dev_rx_init(struct rte_eth_dev *dev)
2328 {
2329         struct ngbe_hw *hw;
2330         struct ngbe_rx_queue *rxq;
2331         uint64_t bus_addr;
2332         uint32_t fctrl;
2333         uint32_t hlreg0;
2334         uint32_t srrctl;
2335         uint32_t rdrxctl;
2336         uint32_t rxcsum;
2337         uint16_t buf_size;
2338         uint16_t i;
2339         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
2340
2341         PMD_INIT_FUNC_TRACE();
2342         hw = ngbe_dev_hw(dev);
2343
2344         /*
2345          * Make sure receives are disabled while setting
2346          * up the Rx context (registers, descriptor rings, etc.).
2347          */
2348         wr32m(hw, NGBE_MACRXCFG, NGBE_MACRXCFG_ENA, 0);
2349         wr32m(hw, NGBE_PBRXCTL, NGBE_PBRXCTL_ENA, 0);
2350
2351         /* Enable receipt of broadcasted frames */
2352         fctrl = rd32(hw, NGBE_PSRCTL);
2353         fctrl |= NGBE_PSRCTL_BCA;
2354         wr32(hw, NGBE_PSRCTL, fctrl);
2355
2356         /*
2357          * Configure CRC stripping, if any.
2358          */
2359         hlreg0 = rd32(hw, NGBE_SECRXCTL);
2360         if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2361                 hlreg0 &= ~NGBE_SECRXCTL_CRCSTRIP;
2362         else
2363                 hlreg0 |= NGBE_SECRXCTL_CRCSTRIP;
2364         hlreg0 &= ~NGBE_SECRXCTL_XDSA;
2365         wr32(hw, NGBE_SECRXCTL, hlreg0);
2366
2367         /*
2368          * Configure jumbo frame support, if any.
2369          */
2370         wr32m(hw, NGBE_FRMSZ, NGBE_FRMSZ_MAX_MASK,
2371                 NGBE_FRMSZ_MAX(dev->data->mtu + NGBE_ETH_OVERHEAD));
2372
2373         /* Setup Rx queues */
2374         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2375                 rxq = dev->data->rx_queues[i];
2376
2377                 /*
2378                  * Reset crc_len in case it was changed after queue setup by a
2379                  * call to configure.
2380                  */
2381                 if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2382                         rxq->crc_len = RTE_ETHER_CRC_LEN;
2383                 else
2384                         rxq->crc_len = 0;
2385
2386                 /* Setup the Base and Length of the Rx Descriptor Rings */
2387                 bus_addr = rxq->rx_ring_phys_addr;
2388                 wr32(hw, NGBE_RXBAL(rxq->reg_idx),
2389                                 (uint32_t)(bus_addr & BIT_MASK32));
2390                 wr32(hw, NGBE_RXBAH(rxq->reg_idx),
2391                                 (uint32_t)(bus_addr >> 32));
2392                 wr32(hw, NGBE_RXRP(rxq->reg_idx), 0);
2393                 wr32(hw, NGBE_RXWP(rxq->reg_idx), 0);
2394
2395                 srrctl = NGBE_RXCFG_RNGLEN(rxq->nb_rx_desc);
2396
2397                 /* Set if packets are dropped when no descriptors available */
2398                 if (rxq->drop_en)
2399                         srrctl |= NGBE_RXCFG_DROP;
2400
2401                 /*
2402                  * Configure the Rx buffer size in the PKTLEN field of
2403                  * the RXCFG register of the queue.
2404                  * The value is in 1 KB resolution. Valid values can be from
2405                  * 1 KB to 16 KB.
2406                  */
2407                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
2408                         RTE_PKTMBUF_HEADROOM);
2409                 buf_size = ROUND_DOWN(buf_size, 0x1 << 10);
2410                 srrctl |= NGBE_RXCFG_PKTLEN(buf_size);
2411
2412                 wr32(hw, NGBE_RXCFG(rxq->reg_idx), srrctl);
2413         }
2414
2415         if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_SCATTER)
2416                 dev->data->scattered_rx = 1;
2417         /*
2418          * Setup the Checksum Register.
2419          * Enable IP/L4 checksum computation by hardware if requested to do so.
2420          */
2421         rxcsum = rd32(hw, NGBE_PSRCTL);
2422         rxcsum |= NGBE_PSRCTL_PCSD;
2423         if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM)
2424                 rxcsum |= NGBE_PSRCTL_L4CSUM;
2425         else
2426                 rxcsum &= ~NGBE_PSRCTL_L4CSUM;
2427
2428         wr32(hw, NGBE_PSRCTL, rxcsum);
2429
2430         if (hw->is_pf) {
2431                 rdrxctl = rd32(hw, NGBE_SECRXCTL);
2432                 if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2433                         rdrxctl &= ~NGBE_SECRXCTL_CRCSTRIP;
2434                 else
2435                         rdrxctl |= NGBE_SECRXCTL_CRCSTRIP;
2436                 wr32(hw, NGBE_SECRXCTL, rdrxctl);
2437         }
2438
2439         ngbe_set_rx_function(dev);
2440
2441         return 0;
2442 }
2443
2444 /*
2445  * Initializes Transmit Unit.
2446  */
2447 void
2448 ngbe_dev_tx_init(struct rte_eth_dev *dev)
2449 {
2450         struct ngbe_hw     *hw;
2451         struct ngbe_tx_queue *txq;
2452         uint64_t bus_addr;
2453         uint16_t i;
2454
2455         PMD_INIT_FUNC_TRACE();
2456         hw = ngbe_dev_hw(dev);
2457
2458         wr32m(hw, NGBE_SECTXCTL, NGBE_SECTXCTL_ODSA, NGBE_SECTXCTL_ODSA);
2459         wr32m(hw, NGBE_SECTXCTL, NGBE_SECTXCTL_XDSA, 0);
2460
2461         /* Setup the Base and Length of the Tx Descriptor Rings */
2462         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2463                 txq = dev->data->tx_queues[i];
2464
2465                 bus_addr = txq->tx_ring_phys_addr;
2466                 wr32(hw, NGBE_TXBAL(txq->reg_idx),
2467                                 (uint32_t)(bus_addr & BIT_MASK32));
2468                 wr32(hw, NGBE_TXBAH(txq->reg_idx),
2469                                 (uint32_t)(bus_addr >> 32));
2470                 wr32m(hw, NGBE_TXCFG(txq->reg_idx), NGBE_TXCFG_BUFLEN_MASK,
2471                         NGBE_TXCFG_BUFLEN(txq->nb_tx_desc));
2472                 /* Setup the HW Tx Head and TX Tail descriptor pointers */
2473                 wr32(hw, NGBE_TXRP(txq->reg_idx), 0);
2474                 wr32(hw, NGBE_TXWP(txq->reg_idx), 0);
2475         }
2476 }
2477
2478 /*
2479  * Start Transmit and Receive Units.
2480  */
2481 int
2482 ngbe_dev_rxtx_start(struct rte_eth_dev *dev)
2483 {
2484         struct ngbe_hw     *hw;
2485         struct ngbe_tx_queue *txq;
2486         struct ngbe_rx_queue *rxq;
2487         uint32_t dmatxctl;
2488         uint32_t rxctrl;
2489         uint16_t i;
2490         int ret = 0;
2491
2492         PMD_INIT_FUNC_TRACE();
2493         hw = ngbe_dev_hw(dev);
2494
2495         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2496                 txq = dev->data->tx_queues[i];
2497                 /* Setup Transmit Threshold Registers */
2498                 wr32m(hw, NGBE_TXCFG(txq->reg_idx),
2499                       NGBE_TXCFG_HTHRESH_MASK |
2500                       NGBE_TXCFG_WTHRESH_MASK,
2501                       NGBE_TXCFG_HTHRESH(txq->hthresh) |
2502                       NGBE_TXCFG_WTHRESH(txq->wthresh));
2503         }
2504
2505         dmatxctl = rd32(hw, NGBE_DMATXCTRL);
2506         dmatxctl |= NGBE_DMATXCTRL_ENA;
2507         wr32(hw, NGBE_DMATXCTRL, dmatxctl);
2508
2509         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2510                 txq = dev->data->tx_queues[i];
2511                 if (txq->tx_deferred_start == 0) {
2512                         ret = ngbe_dev_tx_queue_start(dev, i);
2513                         if (ret < 0)
2514                                 return ret;
2515                 }
2516         }
2517
2518         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2519                 rxq = dev->data->rx_queues[i];
2520                 if (rxq->rx_deferred_start == 0) {
2521                         ret = ngbe_dev_rx_queue_start(dev, i);
2522                         if (ret < 0)
2523                                 return ret;
2524                 }
2525         }
2526
2527         /* Enable Receive engine */
2528         rxctrl = rd32(hw, NGBE_PBRXCTL);
2529         rxctrl |= NGBE_PBRXCTL_ENA;
2530         hw->mac.enable_rx_dma(hw, rxctrl);
2531
2532         return 0;
2533 }
2534
2535 void
2536 ngbe_dev_save_rx_queue(struct ngbe_hw *hw, uint16_t rx_queue_id)
2537 {
2538         u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
2539         *(reg++) = rd32(hw, NGBE_RXBAL(rx_queue_id));
2540         *(reg++) = rd32(hw, NGBE_RXBAH(rx_queue_id));
2541         *(reg++) = rd32(hw, NGBE_RXCFG(rx_queue_id));
2542 }
2543
2544 void
2545 ngbe_dev_store_rx_queue(struct ngbe_hw *hw, uint16_t rx_queue_id)
2546 {
2547         u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
2548         wr32(hw, NGBE_RXBAL(rx_queue_id), *(reg++));
2549         wr32(hw, NGBE_RXBAH(rx_queue_id), *(reg++));
2550         wr32(hw, NGBE_RXCFG(rx_queue_id), *(reg++) & ~NGBE_RXCFG_ENA);
2551 }
2552
2553 void
2554 ngbe_dev_save_tx_queue(struct ngbe_hw *hw, uint16_t tx_queue_id)
2555 {
2556         u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
2557         *(reg++) = rd32(hw, NGBE_TXBAL(tx_queue_id));
2558         *(reg++) = rd32(hw, NGBE_TXBAH(tx_queue_id));
2559         *(reg++) = rd32(hw, NGBE_TXCFG(tx_queue_id));
2560 }
2561
2562 void
2563 ngbe_dev_store_tx_queue(struct ngbe_hw *hw, uint16_t tx_queue_id)
2564 {
2565         u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
2566         wr32(hw, NGBE_TXBAL(tx_queue_id), *(reg++));
2567         wr32(hw, NGBE_TXBAH(tx_queue_id), *(reg++));
2568         wr32(hw, NGBE_TXCFG(tx_queue_id), *(reg++) & ~NGBE_TXCFG_ENA);
2569 }
2570
2571 /*
2572  * Start Receive Units for specified queue.
2573  */
2574 int
2575 ngbe_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
2576 {
2577         struct ngbe_hw *hw = ngbe_dev_hw(dev);
2578         struct ngbe_rx_queue *rxq;
2579         uint32_t rxdctl;
2580         int poll_ms;
2581
2582         PMD_INIT_FUNC_TRACE();
2583
2584         rxq = dev->data->rx_queues[rx_queue_id];
2585
2586         /* Allocate buffers for descriptor rings */
2587         if (ngbe_alloc_rx_queue_mbufs(rxq) != 0) {
2588                 PMD_INIT_LOG(ERR, "Could not alloc mbuf for queue:%d",
2589                              rx_queue_id);
2590                 return -1;
2591         }
2592         rxdctl = rd32(hw, NGBE_RXCFG(rxq->reg_idx));
2593         rxdctl |= NGBE_RXCFG_ENA;
2594         wr32(hw, NGBE_RXCFG(rxq->reg_idx), rxdctl);
2595
2596         /* Wait until Rx Enable ready */
2597         poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
2598         do {
2599                 rte_delay_ms(1);
2600                 rxdctl = rd32(hw, NGBE_RXCFG(rxq->reg_idx));
2601         } while (--poll_ms && !(rxdctl & NGBE_RXCFG_ENA));
2602         if (poll_ms == 0)
2603                 PMD_INIT_LOG(ERR, "Could not enable Rx Queue %d", rx_queue_id);
2604         rte_wmb();
2605         wr32(hw, NGBE_RXRP(rxq->reg_idx), 0);
2606         wr32(hw, NGBE_RXWP(rxq->reg_idx), rxq->nb_rx_desc - 1);
2607         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
2608
2609         return 0;
2610 }
2611
2612 /*
2613  * Stop Receive Units for specified queue.
2614  */
2615 int
2616 ngbe_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
2617 {
2618         struct ngbe_hw *hw = ngbe_dev_hw(dev);
2619         struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
2620         struct ngbe_rx_queue *rxq;
2621         uint32_t rxdctl;
2622         int poll_ms;
2623
2624         PMD_INIT_FUNC_TRACE();
2625
2626         rxq = dev->data->rx_queues[rx_queue_id];
2627
2628         ngbe_dev_save_rx_queue(hw, rxq->reg_idx);
2629         wr32m(hw, NGBE_RXCFG(rxq->reg_idx), NGBE_RXCFG_ENA, 0);
2630
2631         /* Wait until Rx Enable bit clear */
2632         poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
2633         do {
2634                 rte_delay_ms(1);
2635                 rxdctl = rd32(hw, NGBE_RXCFG(rxq->reg_idx));
2636         } while (--poll_ms && (rxdctl & NGBE_RXCFG_ENA));
2637         if (poll_ms == 0)
2638                 PMD_INIT_LOG(ERR, "Could not disable Rx Queue %d", rx_queue_id);
2639
2640         rte_delay_us(RTE_NGBE_WAIT_100_US);
2641         ngbe_dev_store_rx_queue(hw, rxq->reg_idx);
2642
2643         ngbe_rx_queue_release_mbufs(rxq);
2644         ngbe_reset_rx_queue(adapter, rxq);
2645         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
2646
2647         return 0;
2648 }
2649
2650 /*
2651  * Start Transmit Units for specified queue.
2652  */
2653 int
2654 ngbe_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
2655 {
2656         struct ngbe_hw *hw = ngbe_dev_hw(dev);
2657         struct ngbe_tx_queue *txq;
2658         uint32_t txdctl;
2659         int poll_ms;
2660
2661         PMD_INIT_FUNC_TRACE();
2662
2663         txq = dev->data->tx_queues[tx_queue_id];
2664         wr32m(hw, NGBE_TXCFG(txq->reg_idx), NGBE_TXCFG_ENA, NGBE_TXCFG_ENA);
2665
2666         /* Wait until Tx Enable ready */
2667         poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
2668         do {
2669                 rte_delay_ms(1);
2670                 txdctl = rd32(hw, NGBE_TXCFG(txq->reg_idx));
2671         } while (--poll_ms && !(txdctl & NGBE_TXCFG_ENA));
2672         if (poll_ms == 0)
2673                 PMD_INIT_LOG(ERR, "Could not enable Tx Queue %d",
2674                              tx_queue_id);
2675
2676         rte_wmb();
2677         wr32(hw, NGBE_TXWP(txq->reg_idx), txq->tx_tail);
2678         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
2679
2680         return 0;
2681 }
2682
2683 /*
2684  * Stop Transmit Units for specified queue.
2685  */
2686 int
2687 ngbe_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
2688 {
2689         struct ngbe_hw *hw = ngbe_dev_hw(dev);
2690         struct ngbe_tx_queue *txq;
2691         uint32_t txdctl;
2692         uint32_t txtdh, txtdt;
2693         int poll_ms;
2694
2695         PMD_INIT_FUNC_TRACE();
2696
2697         txq = dev->data->tx_queues[tx_queue_id];
2698
2699         /* Wait until Tx queue is empty */
2700         poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
2701         do {
2702                 rte_delay_us(RTE_NGBE_WAIT_100_US);
2703                 txtdh = rd32(hw, NGBE_TXRP(txq->reg_idx));
2704                 txtdt = rd32(hw, NGBE_TXWP(txq->reg_idx));
2705         } while (--poll_ms && (txtdh != txtdt));
2706         if (poll_ms == 0)
2707                 PMD_INIT_LOG(ERR, "Tx Queue %d is not empty when stopping.",
2708                              tx_queue_id);
2709
2710         ngbe_dev_save_tx_queue(hw, txq->reg_idx);
2711         wr32m(hw, NGBE_TXCFG(txq->reg_idx), NGBE_TXCFG_ENA, 0);
2712
2713         /* Wait until Tx Enable bit clear */
2714         poll_ms = RTE_NGBE_REGISTER_POLL_WAIT_10_MS;
2715         do {
2716                 rte_delay_ms(1);
2717                 txdctl = rd32(hw, NGBE_TXCFG(txq->reg_idx));
2718         } while (--poll_ms && (txdctl & NGBE_TXCFG_ENA));
2719         if (poll_ms == 0)
2720                 PMD_INIT_LOG(ERR, "Could not disable Tx Queue %d",
2721                              tx_queue_id);
2722
2723         rte_delay_us(RTE_NGBE_WAIT_100_US);
2724         ngbe_dev_store_tx_queue(hw, txq->reg_idx);
2725
2726         if (txq->ops != NULL) {
2727                 txq->ops->release_mbufs(txq);
2728                 txq->ops->reset(txq);
2729         }
2730         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
2731
2732         return 0;
2733 }