1af70e7329ae31bfca63b7ba4838a141bc63629b
[dpdk.git] / drivers / net / txgbe / txgbe_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include <sys/queue.h>
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <stdint.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14 #include <inttypes.h>
15
16 #include <rte_common.h>
17 #include <rte_cycles.h>
18 #include <rte_log.h>
19 #include <rte_debug.h>
20 #include <rte_ethdev.h>
21 #include <rte_ethdev_driver.h>
22 #include <rte_memzone.h>
23 #include <rte_atomic.h>
24 #include <rte_mempool.h>
25 #include <rte_malloc.h>
26 #include <rte_mbuf.h>
27 #include <rte_ether.h>
28 #include <rte_prefetch.h>
29 #include <rte_udp.h>
30 #include <rte_tcp.h>
31 #include <rte_sctp.h>
32 #include <rte_string_fns.h>
33 #include <rte_errno.h>
34 #include <rte_ip.h>
35 #include <rte_net.h>
36
37 #include "txgbe_logs.h"
38 #include "base/txgbe.h"
39 #include "txgbe_ethdev.h"
40 #include "txgbe_rxtx.h"
41
42 /* Bit Mask to indicate what bits required for building TX context */
43 static const u64 TXGBE_TX_OFFLOAD_MASK = (PKT_TX_IP_CKSUM |
44                 PKT_TX_OUTER_IPV6 |
45                 PKT_TX_OUTER_IPV4 |
46                 PKT_TX_IPV6 |
47                 PKT_TX_IPV4 |
48                 PKT_TX_VLAN_PKT |
49                 PKT_TX_L4_MASK |
50                 PKT_TX_TCP_SEG |
51                 PKT_TX_TUNNEL_MASK |
52                 PKT_TX_OUTER_IP_CKSUM);
53
54 #define TXGBE_TX_OFFLOAD_NOTSUP_MASK \
55                 (PKT_TX_OFFLOAD_MASK ^ TXGBE_TX_OFFLOAD_MASK)
56
57 /*
58  * Prefetch a cache line into all cache levels.
59  */
60 #define rte_txgbe_prefetch(p)   rte_prefetch0(p)
61
62 static int
63 txgbe_is_vf(struct rte_eth_dev *dev)
64 {
65         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
66
67         switch (hw->mac.type) {
68         case txgbe_mac_raptor_vf:
69                 return 1;
70         default:
71                 return 0;
72         }
73 }
74
75 /*********************************************************************
76  *
77  *  TX functions
78  *
79  **********************************************************************/
80
81 /*
82  * Check for descriptors with their DD bit set and free mbufs.
83  * Return the total number of buffers freed.
84  */
85 static __rte_always_inline int
86 txgbe_tx_free_bufs(struct txgbe_tx_queue *txq)
87 {
88         struct txgbe_tx_entry *txep;
89         uint32_t status;
90         int i, nb_free = 0;
91         struct rte_mbuf *m, *free[RTE_TXGBE_TX_MAX_FREE_BUF_SZ];
92
93         /* check DD bit on threshold descriptor */
94         status = txq->tx_ring[txq->tx_next_dd].dw3;
95         if (!(status & rte_cpu_to_le_32(TXGBE_TXD_DD))) {
96                 if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
97                         txgbe_set32_masked(txq->tdc_reg_addr,
98                                 TXGBE_TXCFG_FLUSH, TXGBE_TXCFG_FLUSH);
99                 return 0;
100         }
101
102         /*
103          * first buffer to free from S/W ring is at index
104          * tx_next_dd - (tx_free_thresh-1)
105          */
106         txep = &txq->sw_ring[txq->tx_next_dd - (txq->tx_free_thresh - 1)];
107         for (i = 0; i < txq->tx_free_thresh; ++i, ++txep) {
108                 /* free buffers one at a time */
109                 m = rte_pktmbuf_prefree_seg(txep->mbuf);
110                 txep->mbuf = NULL;
111
112                 if (unlikely(m == NULL))
113                         continue;
114
115                 if (nb_free >= RTE_TXGBE_TX_MAX_FREE_BUF_SZ ||
116                     (nb_free > 0 && m->pool != free[0]->pool)) {
117                         rte_mempool_put_bulk(free[0]->pool,
118                                              (void **)free, nb_free);
119                         nb_free = 0;
120                 }
121
122                 free[nb_free++] = m;
123         }
124
125         if (nb_free > 0)
126                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
127
128         /* buffers were freed, update counters */
129         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_free_thresh);
130         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_free_thresh);
131         if (txq->tx_next_dd >= txq->nb_tx_desc)
132                 txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
133
134         return txq->tx_free_thresh;
135 }
136
137 /* Populate 4 descriptors with data from 4 mbufs */
138 static inline void
139 tx4(volatile struct txgbe_tx_desc *txdp, struct rte_mbuf **pkts)
140 {
141         uint64_t buf_dma_addr;
142         uint32_t pkt_len;
143         int i;
144
145         for (i = 0; i < 4; ++i, ++txdp, ++pkts) {
146                 buf_dma_addr = rte_mbuf_data_iova(*pkts);
147                 pkt_len = (*pkts)->data_len;
148
149                 /* write data to descriptor */
150                 txdp->qw0 = rte_cpu_to_le_64(buf_dma_addr);
151                 txdp->dw2 = cpu_to_le32(TXGBE_TXD_FLAGS |
152                                         TXGBE_TXD_DATLEN(pkt_len));
153                 txdp->dw3 = cpu_to_le32(TXGBE_TXD_PAYLEN(pkt_len));
154
155                 rte_prefetch0(&(*pkts)->pool);
156         }
157 }
158
159 /* Populate 1 descriptor with data from 1 mbuf */
160 static inline void
161 tx1(volatile struct txgbe_tx_desc *txdp, struct rte_mbuf **pkts)
162 {
163         uint64_t buf_dma_addr;
164         uint32_t pkt_len;
165
166         buf_dma_addr = rte_mbuf_data_iova(*pkts);
167         pkt_len = (*pkts)->data_len;
168
169         /* write data to descriptor */
170         txdp->qw0 = cpu_to_le64(buf_dma_addr);
171         txdp->dw2 = cpu_to_le32(TXGBE_TXD_FLAGS |
172                                 TXGBE_TXD_DATLEN(pkt_len));
173         txdp->dw3 = cpu_to_le32(TXGBE_TXD_PAYLEN(pkt_len));
174
175         rte_prefetch0(&(*pkts)->pool);
176 }
177
178 /*
179  * Fill H/W descriptor ring with mbuf data.
180  * Copy mbuf pointers to the S/W ring.
181  */
182 static inline void
183 txgbe_tx_fill_hw_ring(struct txgbe_tx_queue *txq, struct rte_mbuf **pkts,
184                       uint16_t nb_pkts)
185 {
186         volatile struct txgbe_tx_desc *txdp = &txq->tx_ring[txq->tx_tail];
187         struct txgbe_tx_entry *txep = &txq->sw_ring[txq->tx_tail];
188         const int N_PER_LOOP = 4;
189         const int N_PER_LOOP_MASK = N_PER_LOOP - 1;
190         int mainpart, leftover;
191         int i, j;
192
193         /*
194          * Process most of the packets in chunks of N pkts.  Any
195          * leftover packets will get processed one at a time.
196          */
197         mainpart = (nb_pkts & ((uint32_t)~N_PER_LOOP_MASK));
198         leftover = (nb_pkts & ((uint32_t)N_PER_LOOP_MASK));
199         for (i = 0; i < mainpart; i += N_PER_LOOP) {
200                 /* Copy N mbuf pointers to the S/W ring */
201                 for (j = 0; j < N_PER_LOOP; ++j)
202                         (txep + i + j)->mbuf = *(pkts + i + j);
203                 tx4(txdp + i, pkts + i);
204         }
205
206         if (unlikely(leftover > 0)) {
207                 for (i = 0; i < leftover; ++i) {
208                         (txep + mainpart + i)->mbuf = *(pkts + mainpart + i);
209                         tx1(txdp + mainpart + i, pkts + mainpart + i);
210                 }
211         }
212 }
213
214 static inline uint16_t
215 tx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
216              uint16_t nb_pkts)
217 {
218         struct txgbe_tx_queue *txq = (struct txgbe_tx_queue *)tx_queue;
219         uint16_t n = 0;
220
221         /*
222          * Begin scanning the H/W ring for done descriptors when the
223          * number of available descriptors drops below tx_free_thresh.  For
224          * each done descriptor, free the associated buffer.
225          */
226         if (txq->nb_tx_free < txq->tx_free_thresh)
227                 txgbe_tx_free_bufs(txq);
228
229         /* Only use descriptors that are available */
230         nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
231         if (unlikely(nb_pkts == 0))
232                 return 0;
233
234         /* Use exactly nb_pkts descriptors */
235         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
236
237         /*
238          * At this point, we know there are enough descriptors in the
239          * ring to transmit all the packets.  This assumes that each
240          * mbuf contains a single segment, and that no new offloads
241          * are expected, which would require a new context descriptor.
242          */
243
244         /*
245          * See if we're going to wrap-around. If so, handle the top
246          * of the descriptor ring first, then do the bottom.  If not,
247          * the processing looks just like the "bottom" part anyway...
248          */
249         if ((txq->tx_tail + nb_pkts) > txq->nb_tx_desc) {
250                 n = (uint16_t)(txq->nb_tx_desc - txq->tx_tail);
251                 txgbe_tx_fill_hw_ring(txq, tx_pkts, n);
252                 txq->tx_tail = 0;
253         }
254
255         /* Fill H/W descriptor ring with mbuf data */
256         txgbe_tx_fill_hw_ring(txq, tx_pkts + n, (uint16_t)(nb_pkts - n));
257         txq->tx_tail = (uint16_t)(txq->tx_tail + (nb_pkts - n));
258
259         /*
260          * Check for wrap-around. This would only happen if we used
261          * up to the last descriptor in the ring, no more, no less.
262          */
263         if (txq->tx_tail >= txq->nb_tx_desc)
264                 txq->tx_tail = 0;
265
266         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
267                    (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
268                    (uint16_t)txq->tx_tail, (uint16_t)nb_pkts);
269
270         /* update tail pointer */
271         rte_wmb();
272         txgbe_set32_relaxed(txq->tdt_reg_addr, txq->tx_tail);
273
274         return nb_pkts;
275 }
276
277 uint16_t
278 txgbe_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
279                        uint16_t nb_pkts)
280 {
281         uint16_t nb_tx;
282
283         /* Try to transmit at least chunks of TX_MAX_BURST pkts */
284         if (likely(nb_pkts <= RTE_PMD_TXGBE_TX_MAX_BURST))
285                 return tx_xmit_pkts(tx_queue, tx_pkts, nb_pkts);
286
287         /* transmit more than the max burst, in chunks of TX_MAX_BURST */
288         nb_tx = 0;
289         while (nb_pkts) {
290                 uint16_t ret, n;
291
292                 n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_TXGBE_TX_MAX_BURST);
293                 ret = tx_xmit_pkts(tx_queue, &tx_pkts[nb_tx], n);
294                 nb_tx = (uint16_t)(nb_tx + ret);
295                 nb_pkts = (uint16_t)(nb_pkts - ret);
296                 if (ret < n)
297                         break;
298         }
299
300         return nb_tx;
301 }
302
303 static inline void
304 txgbe_set_xmit_ctx(struct txgbe_tx_queue *txq,
305                 volatile struct txgbe_tx_ctx_desc *ctx_txd,
306                 uint64_t ol_flags, union txgbe_tx_offload tx_offload)
307 {
308         union txgbe_tx_offload tx_offload_mask;
309         uint32_t type_tucmd_mlhl;
310         uint32_t mss_l4len_idx;
311         uint32_t ctx_idx;
312         uint32_t vlan_macip_lens;
313         uint32_t tunnel_seed;
314
315         ctx_idx = txq->ctx_curr;
316         tx_offload_mask.data[0] = 0;
317         tx_offload_mask.data[1] = 0;
318
319         /* Specify which HW CTX to upload. */
320         mss_l4len_idx = TXGBE_TXD_IDX(ctx_idx);
321         type_tucmd_mlhl = TXGBE_TXD_CTXT;
322
323         tx_offload_mask.ptid |= ~0;
324         type_tucmd_mlhl |= TXGBE_TXD_PTID(tx_offload.ptid);
325
326         /* check if TCP segmentation required for this packet */
327         if (ol_flags & PKT_TX_TCP_SEG) {
328                 tx_offload_mask.l2_len |= ~0;
329                 tx_offload_mask.l3_len |= ~0;
330                 tx_offload_mask.l4_len |= ~0;
331                 tx_offload_mask.tso_segsz |= ~0;
332                 mss_l4len_idx |= TXGBE_TXD_MSS(tx_offload.tso_segsz);
333                 mss_l4len_idx |= TXGBE_TXD_L4LEN(tx_offload.l4_len);
334         } else { /* no TSO, check if hardware checksum is needed */
335                 if (ol_flags & PKT_TX_IP_CKSUM) {
336                         tx_offload_mask.l2_len |= ~0;
337                         tx_offload_mask.l3_len |= ~0;
338                 }
339
340                 switch (ol_flags & PKT_TX_L4_MASK) {
341                 case PKT_TX_UDP_CKSUM:
342                         mss_l4len_idx |=
343                                 TXGBE_TXD_L4LEN(sizeof(struct rte_udp_hdr));
344                         tx_offload_mask.l2_len |= ~0;
345                         tx_offload_mask.l3_len |= ~0;
346                         break;
347                 case PKT_TX_TCP_CKSUM:
348                         mss_l4len_idx |=
349                                 TXGBE_TXD_L4LEN(sizeof(struct rte_tcp_hdr));
350                         tx_offload_mask.l2_len |= ~0;
351                         tx_offload_mask.l3_len |= ~0;
352                         break;
353                 case PKT_TX_SCTP_CKSUM:
354                         mss_l4len_idx |=
355                                 TXGBE_TXD_L4LEN(sizeof(struct rte_sctp_hdr));
356                         tx_offload_mask.l2_len |= ~0;
357                         tx_offload_mask.l3_len |= ~0;
358                         break;
359                 default:
360                         break;
361                 }
362         }
363
364         vlan_macip_lens = TXGBE_TXD_IPLEN(tx_offload.l3_len >> 1);
365
366         if (ol_flags & PKT_TX_TUNNEL_MASK) {
367                 tx_offload_mask.outer_tun_len |= ~0;
368                 tx_offload_mask.outer_l2_len |= ~0;
369                 tx_offload_mask.outer_l3_len |= ~0;
370                 tx_offload_mask.l2_len |= ~0;
371                 tunnel_seed = TXGBE_TXD_ETUNLEN(tx_offload.outer_tun_len >> 1);
372                 tunnel_seed |= TXGBE_TXD_EIPLEN(tx_offload.outer_l3_len >> 2);
373
374                 switch (ol_flags & PKT_TX_TUNNEL_MASK) {
375                 case PKT_TX_TUNNEL_IPIP:
376                         /* for non UDP / GRE tunneling, set to 0b */
377                         break;
378                 case PKT_TX_TUNNEL_VXLAN:
379                 case PKT_TX_TUNNEL_GENEVE:
380                         tunnel_seed |= TXGBE_TXD_ETYPE_UDP;
381                         break;
382                 case PKT_TX_TUNNEL_GRE:
383                         tunnel_seed |= TXGBE_TXD_ETYPE_GRE;
384                         break;
385                 default:
386                         PMD_TX_LOG(ERR, "Tunnel type not supported");
387                         return;
388                 }
389                 vlan_macip_lens |= TXGBE_TXD_MACLEN(tx_offload.outer_l2_len);
390         } else {
391                 tunnel_seed = 0;
392                 vlan_macip_lens |= TXGBE_TXD_MACLEN(tx_offload.l2_len);
393         }
394
395         if (ol_flags & PKT_TX_VLAN_PKT) {
396                 tx_offload_mask.vlan_tci |= ~0;
397                 vlan_macip_lens |= TXGBE_TXD_VLAN(tx_offload.vlan_tci);
398         }
399
400         txq->ctx_cache[ctx_idx].flags = ol_flags;
401         txq->ctx_cache[ctx_idx].tx_offload.data[0] =
402                 tx_offload_mask.data[0] & tx_offload.data[0];
403         txq->ctx_cache[ctx_idx].tx_offload.data[1] =
404                 tx_offload_mask.data[1] & tx_offload.data[1];
405         txq->ctx_cache[ctx_idx].tx_offload_mask = tx_offload_mask;
406
407         ctx_txd->dw0 = rte_cpu_to_le_32(vlan_macip_lens);
408         ctx_txd->dw1 = rte_cpu_to_le_32(tunnel_seed);
409         ctx_txd->dw2 = rte_cpu_to_le_32(type_tucmd_mlhl);
410         ctx_txd->dw3 = rte_cpu_to_le_32(mss_l4len_idx);
411 }
412
413 /*
414  * Check which hardware context can be used. Use the existing match
415  * or create a new context descriptor.
416  */
417 static inline uint32_t
418 what_ctx_update(struct txgbe_tx_queue *txq, uint64_t flags,
419                    union txgbe_tx_offload tx_offload)
420 {
421         /* If match with the current used context */
422         if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
423                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
424                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
425                      & tx_offload.data[0])) &&
426                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
427                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
428                      & tx_offload.data[1]))))
429                 return txq->ctx_curr;
430
431         /* What if match with the next context  */
432         txq->ctx_curr ^= 1;
433         if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
434                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
435                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
436                      & tx_offload.data[0])) &&
437                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
438                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
439                      & tx_offload.data[1]))))
440                 return txq->ctx_curr;
441
442         /* Mismatch, use the previous context */
443         return TXGBE_CTX_NUM;
444 }
445
446 static inline uint32_t
447 tx_desc_cksum_flags_to_olinfo(uint64_t ol_flags)
448 {
449         uint32_t tmp = 0;
450
451         if ((ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM) {
452                 tmp |= TXGBE_TXD_CC;
453                 tmp |= TXGBE_TXD_L4CS;
454         }
455         if (ol_flags & PKT_TX_IP_CKSUM) {
456                 tmp |= TXGBE_TXD_CC;
457                 tmp |= TXGBE_TXD_IPCS;
458         }
459         if (ol_flags & PKT_TX_OUTER_IP_CKSUM) {
460                 tmp |= TXGBE_TXD_CC;
461                 tmp |= TXGBE_TXD_EIPCS;
462         }
463         if (ol_flags & PKT_TX_TCP_SEG) {
464                 tmp |= TXGBE_TXD_CC;
465                 /* implies IPv4 cksum */
466                 if (ol_flags & PKT_TX_IPV4)
467                         tmp |= TXGBE_TXD_IPCS;
468                 tmp |= TXGBE_TXD_L4CS;
469         }
470         if (ol_flags & PKT_TX_VLAN_PKT)
471                 tmp |= TXGBE_TXD_CC;
472
473         return tmp;
474 }
475
476 static inline uint32_t
477 tx_desc_ol_flags_to_cmdtype(uint64_t ol_flags)
478 {
479         uint32_t cmdtype = 0;
480
481         if (ol_flags & PKT_TX_VLAN_PKT)
482                 cmdtype |= TXGBE_TXD_VLE;
483         if (ol_flags & PKT_TX_TCP_SEG)
484                 cmdtype |= TXGBE_TXD_TSE;
485         if (ol_flags & PKT_TX_MACSEC)
486                 cmdtype |= TXGBE_TXD_LINKSEC;
487         return cmdtype;
488 }
489
490 static inline uint8_t
491 tx_desc_ol_flags_to_ptid(uint64_t oflags, uint32_t ptype)
492 {
493         bool tun;
494
495         if (ptype)
496                 return txgbe_encode_ptype(ptype);
497
498         /* Only support flags in TXGBE_TX_OFFLOAD_MASK */
499         tun = !!(oflags & PKT_TX_TUNNEL_MASK);
500
501         /* L2 level */
502         ptype = RTE_PTYPE_L2_ETHER;
503         if (oflags & PKT_TX_VLAN)
504                 ptype |= RTE_PTYPE_L2_ETHER_VLAN;
505
506         /* L3 level */
507         if (oflags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IP_CKSUM))
508                 ptype |= RTE_PTYPE_L3_IPV4;
509         else if (oflags & (PKT_TX_OUTER_IPV6))
510                 ptype |= RTE_PTYPE_L3_IPV6;
511
512         if (oflags & (PKT_TX_IPV4 | PKT_TX_IP_CKSUM))
513                 ptype |= (tun ? RTE_PTYPE_INNER_L3_IPV4 : RTE_PTYPE_L3_IPV4);
514         else if (oflags & (PKT_TX_IPV6))
515                 ptype |= (tun ? RTE_PTYPE_INNER_L3_IPV6 : RTE_PTYPE_L3_IPV6);
516
517         /* L4 level */
518         switch (oflags & (PKT_TX_L4_MASK)) {
519         case PKT_TX_TCP_CKSUM:
520                 ptype |= (tun ? RTE_PTYPE_INNER_L4_TCP : RTE_PTYPE_L4_TCP);
521                 break;
522         case PKT_TX_UDP_CKSUM:
523                 ptype |= (tun ? RTE_PTYPE_INNER_L4_UDP : RTE_PTYPE_L4_UDP);
524                 break;
525         case PKT_TX_SCTP_CKSUM:
526                 ptype |= (tun ? RTE_PTYPE_INNER_L4_SCTP : RTE_PTYPE_L4_SCTP);
527                 break;
528         }
529
530         if (oflags & PKT_TX_TCP_SEG)
531                 ptype |= (tun ? RTE_PTYPE_INNER_L4_TCP : RTE_PTYPE_L4_TCP);
532
533         /* Tunnel */
534         switch (oflags & PKT_TX_TUNNEL_MASK) {
535         case PKT_TX_TUNNEL_VXLAN:
536                 ptype |= RTE_PTYPE_L2_ETHER |
537                          RTE_PTYPE_L3_IPV4 |
538                          RTE_PTYPE_TUNNEL_VXLAN;
539                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
540                 break;
541         case PKT_TX_TUNNEL_GRE:
542                 ptype |= RTE_PTYPE_L2_ETHER |
543                          RTE_PTYPE_L3_IPV4 |
544                          RTE_PTYPE_TUNNEL_GRE;
545                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
546                 break;
547         case PKT_TX_TUNNEL_GENEVE:
548                 ptype |= RTE_PTYPE_L2_ETHER |
549                          RTE_PTYPE_L3_IPV4 |
550                          RTE_PTYPE_TUNNEL_GENEVE;
551                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
552                 break;
553         case PKT_TX_TUNNEL_VXLAN_GPE:
554                 ptype |= RTE_PTYPE_L2_ETHER |
555                          RTE_PTYPE_L3_IPV4 |
556                          RTE_PTYPE_TUNNEL_VXLAN_GPE;
557                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
558                 break;
559         case PKT_TX_TUNNEL_IPIP:
560         case PKT_TX_TUNNEL_IP:
561                 ptype |= RTE_PTYPE_L2_ETHER |
562                          RTE_PTYPE_L3_IPV4 |
563                          RTE_PTYPE_TUNNEL_IP;
564                 break;
565         }
566
567         return txgbe_encode_ptype(ptype);
568 }
569
570 #ifndef DEFAULT_TX_FREE_THRESH
571 #define DEFAULT_TX_FREE_THRESH 32
572 #endif
573
574 /* Reset transmit descriptors after they have been used */
575 static inline int
576 txgbe_xmit_cleanup(struct txgbe_tx_queue *txq)
577 {
578         struct txgbe_tx_entry *sw_ring = txq->sw_ring;
579         volatile struct txgbe_tx_desc *txr = txq->tx_ring;
580         uint16_t last_desc_cleaned = txq->last_desc_cleaned;
581         uint16_t nb_tx_desc = txq->nb_tx_desc;
582         uint16_t desc_to_clean_to;
583         uint16_t nb_tx_to_clean;
584         uint32_t status;
585
586         /* Determine the last descriptor needing to be cleaned */
587         desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_free_thresh);
588         if (desc_to_clean_to >= nb_tx_desc)
589                 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
590
591         /* Check to make sure the last descriptor to clean is done */
592         desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
593         status = txr[desc_to_clean_to].dw3;
594         if (!(status & rte_cpu_to_le_32(TXGBE_TXD_DD))) {
595                 PMD_TX_FREE_LOG(DEBUG,
596                                 "TX descriptor %4u is not done"
597                                 "(port=%d queue=%d)",
598                                 desc_to_clean_to,
599                                 txq->port_id, txq->queue_id);
600                 if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
601                         txgbe_set32_masked(txq->tdc_reg_addr,
602                                 TXGBE_TXCFG_FLUSH, TXGBE_TXCFG_FLUSH);
603                 /* Failed to clean any descriptors, better luck next time */
604                 return -(1);
605         }
606
607         /* Figure out how many descriptors will be cleaned */
608         if (last_desc_cleaned > desc_to_clean_to)
609                 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
610                                                         desc_to_clean_to);
611         else
612                 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
613                                                 last_desc_cleaned);
614
615         PMD_TX_FREE_LOG(DEBUG,
616                         "Cleaning %4u TX descriptors: %4u to %4u "
617                         "(port=%d queue=%d)",
618                         nb_tx_to_clean, last_desc_cleaned, desc_to_clean_to,
619                         txq->port_id, txq->queue_id);
620
621         /*
622          * The last descriptor to clean is done, so that means all the
623          * descriptors from the last descriptor that was cleaned
624          * up to the last descriptor with the RS bit set
625          * are done. Only reset the threshold descriptor.
626          */
627         txr[desc_to_clean_to].dw3 = 0;
628
629         /* Update the txq to reflect the last descriptor that was cleaned */
630         txq->last_desc_cleaned = desc_to_clean_to;
631         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
632
633         /* No Error */
634         return 0;
635 }
636
637 static inline uint8_t
638 txgbe_get_tun_len(struct rte_mbuf *mbuf)
639 {
640         struct txgbe_genevehdr genevehdr;
641         const struct txgbe_genevehdr *gh;
642         uint8_t tun_len;
643
644         switch (mbuf->ol_flags & PKT_TX_TUNNEL_MASK) {
645         case PKT_TX_TUNNEL_IPIP:
646                 tun_len = 0;
647                 break;
648         case PKT_TX_TUNNEL_VXLAN:
649         case PKT_TX_TUNNEL_VXLAN_GPE:
650                 tun_len = sizeof(struct txgbe_udphdr)
651                         + sizeof(struct txgbe_vxlanhdr);
652                 break;
653         case PKT_TX_TUNNEL_GRE:
654                 tun_len = sizeof(struct txgbe_nvgrehdr);
655                 break;
656         case PKT_TX_TUNNEL_GENEVE:
657                 gh = rte_pktmbuf_read(mbuf,
658                         mbuf->outer_l2_len + mbuf->outer_l3_len,
659                         sizeof(genevehdr), &genevehdr);
660                 tun_len = sizeof(struct txgbe_udphdr)
661                         + sizeof(struct txgbe_genevehdr)
662                         + (gh->opt_len << 2);
663                 break;
664         default:
665                 tun_len = 0;
666         }
667
668         return tun_len;
669 }
670
671 uint16_t
672 txgbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
673                 uint16_t nb_pkts)
674 {
675         struct txgbe_tx_queue *txq;
676         struct txgbe_tx_entry *sw_ring;
677         struct txgbe_tx_entry *txe, *txn;
678         volatile struct txgbe_tx_desc *txr;
679         volatile struct txgbe_tx_desc *txd;
680         struct rte_mbuf     *tx_pkt;
681         struct rte_mbuf     *m_seg;
682         uint64_t buf_dma_addr;
683         uint32_t olinfo_status;
684         uint32_t cmd_type_len;
685         uint32_t pkt_len;
686         uint16_t slen;
687         uint64_t ol_flags;
688         uint16_t tx_id;
689         uint16_t tx_last;
690         uint16_t nb_tx;
691         uint16_t nb_used;
692         uint64_t tx_ol_req;
693         uint32_t ctx = 0;
694         uint32_t new_ctx;
695         union txgbe_tx_offload tx_offload;
696
697         tx_offload.data[0] = 0;
698         tx_offload.data[1] = 0;
699         txq = tx_queue;
700         sw_ring = txq->sw_ring;
701         txr     = txq->tx_ring;
702         tx_id   = txq->tx_tail;
703         txe = &sw_ring[tx_id];
704
705         /* Determine if the descriptor ring needs to be cleaned. */
706         if (txq->nb_tx_free < txq->tx_free_thresh)
707                 txgbe_xmit_cleanup(txq);
708
709         rte_prefetch0(&txe->mbuf->pool);
710
711         /* TX loop */
712         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
713                 new_ctx = 0;
714                 tx_pkt = *tx_pkts++;
715                 pkt_len = tx_pkt->pkt_len;
716
717                 /*
718                  * Determine how many (if any) context descriptors
719                  * are needed for offload functionality.
720                  */
721                 ol_flags = tx_pkt->ol_flags;
722
723                 /* If hardware offload required */
724                 tx_ol_req = ol_flags & TXGBE_TX_OFFLOAD_MASK;
725                 if (tx_ol_req) {
726                         tx_offload.ptid = tx_desc_ol_flags_to_ptid(tx_ol_req,
727                                         tx_pkt->packet_type);
728                         tx_offload.l2_len = tx_pkt->l2_len;
729                         tx_offload.l3_len = tx_pkt->l3_len;
730                         tx_offload.l4_len = tx_pkt->l4_len;
731                         tx_offload.vlan_tci = tx_pkt->vlan_tci;
732                         tx_offload.tso_segsz = tx_pkt->tso_segsz;
733                         tx_offload.outer_l2_len = tx_pkt->outer_l2_len;
734                         tx_offload.outer_l3_len = tx_pkt->outer_l3_len;
735                         tx_offload.outer_tun_len = txgbe_get_tun_len(tx_pkt);
736
737                         /* If new context need be built or reuse the exist ctx*/
738                         ctx = what_ctx_update(txq, tx_ol_req, tx_offload);
739                         /* Only allocate context descriptor if required */
740                         new_ctx = (ctx == TXGBE_CTX_NUM);
741                         ctx = txq->ctx_curr;
742                 }
743
744                 /*
745                  * Keep track of how many descriptors are used this loop
746                  * This will always be the number of segments + the number of
747                  * Context descriptors required to transmit the packet
748                  */
749                 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
750
751                 /*
752                  * The number of descriptors that must be allocated for a
753                  * packet is the number of segments of that packet, plus 1
754                  * Context Descriptor for the hardware offload, if any.
755                  * Determine the last TX descriptor to allocate in the TX ring
756                  * for the packet, starting from the current position (tx_id)
757                  * in the ring.
758                  */
759                 tx_last = (uint16_t)(tx_id + nb_used - 1);
760
761                 /* Circular ring */
762                 if (tx_last >= txq->nb_tx_desc)
763                         tx_last = (uint16_t)(tx_last - txq->nb_tx_desc);
764
765                 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
766                            " tx_first=%u tx_last=%u",
767                            (uint16_t)txq->port_id,
768                            (uint16_t)txq->queue_id,
769                            (uint32_t)pkt_len,
770                            (uint16_t)tx_id,
771                            (uint16_t)tx_last);
772
773                 /*
774                  * Make sure there are enough TX descriptors available to
775                  * transmit the entire packet.
776                  * nb_used better be less than or equal to txq->tx_free_thresh
777                  */
778                 if (nb_used > txq->nb_tx_free) {
779                         PMD_TX_FREE_LOG(DEBUG,
780                                         "Not enough free TX descriptors "
781                                         "nb_used=%4u nb_free=%4u "
782                                         "(port=%d queue=%d)",
783                                         nb_used, txq->nb_tx_free,
784                                         txq->port_id, txq->queue_id);
785
786                         if (txgbe_xmit_cleanup(txq) != 0) {
787                                 /* Could not clean any descriptors */
788                                 if (nb_tx == 0)
789                                         return 0;
790                                 goto end_of_tx;
791                         }
792
793                         /* nb_used better be <= txq->tx_free_thresh */
794                         if (unlikely(nb_used > txq->tx_free_thresh)) {
795                                 PMD_TX_FREE_LOG(DEBUG,
796                                         "The number of descriptors needed to "
797                                         "transmit the packet exceeds the "
798                                         "RS bit threshold. This will impact "
799                                         "performance."
800                                         "nb_used=%4u nb_free=%4u "
801                                         "tx_free_thresh=%4u. "
802                                         "(port=%d queue=%d)",
803                                         nb_used, txq->nb_tx_free,
804                                         txq->tx_free_thresh,
805                                         txq->port_id, txq->queue_id);
806                                 /*
807                                  * Loop here until there are enough TX
808                                  * descriptors or until the ring cannot be
809                                  * cleaned.
810                                  */
811                                 while (nb_used > txq->nb_tx_free) {
812                                         if (txgbe_xmit_cleanup(txq) != 0) {
813                                                 /*
814                                                  * Could not clean any
815                                                  * descriptors
816                                                  */
817                                                 if (nb_tx == 0)
818                                                         return 0;
819                                                 goto end_of_tx;
820                                         }
821                                 }
822                         }
823                 }
824
825                 /*
826                  * By now there are enough free TX descriptors to transmit
827                  * the packet.
828                  */
829
830                 /*
831                  * Set common flags of all TX Data Descriptors.
832                  *
833                  * The following bits must be set in all Data Descriptors:
834                  *   - TXGBE_TXD_DTYP_DATA
835                  *   - TXGBE_TXD_DCMD_DEXT
836                  *
837                  * The following bits must be set in the first Data Descriptor
838                  * and are ignored in the other ones:
839                  *   - TXGBE_TXD_DCMD_IFCS
840                  *   - TXGBE_TXD_MAC_1588
841                  *   - TXGBE_TXD_DCMD_VLE
842                  *
843                  * The following bits must only be set in the last Data
844                  * Descriptor:
845                  *   - TXGBE_TXD_CMD_EOP
846                  *
847                  * The following bits can be set in any Data Descriptor, but
848                  * are only set in the last Data Descriptor:
849                  *   - TXGBE_TXD_CMD_RS
850                  */
851                 cmd_type_len = TXGBE_TXD_FCS;
852
853                 olinfo_status = 0;
854                 if (tx_ol_req) {
855                         if (ol_flags & PKT_TX_TCP_SEG) {
856                                 /* when TSO is on, paylen in descriptor is the
857                                  * not the packet len but the tcp payload len
858                                  */
859                                 pkt_len -= (tx_offload.l2_len +
860                                         tx_offload.l3_len + tx_offload.l4_len);
861                                 pkt_len -=
862                                         (tx_pkt->ol_flags & PKT_TX_TUNNEL_MASK)
863                                         ? tx_offload.outer_l2_len +
864                                           tx_offload.outer_l3_len : 0;
865                         }
866
867                         /*
868                          * Setup the TX Advanced Context Descriptor if required
869                          */
870                         if (new_ctx) {
871                                 volatile struct txgbe_tx_ctx_desc *ctx_txd;
872
873                                 ctx_txd = (volatile struct txgbe_tx_ctx_desc *)
874                                     &txr[tx_id];
875
876                                 txn = &sw_ring[txe->next_id];
877                                 rte_prefetch0(&txn->mbuf->pool);
878
879                                 if (txe->mbuf != NULL) {
880                                         rte_pktmbuf_free_seg(txe->mbuf);
881                                         txe->mbuf = NULL;
882                                 }
883
884                                 txgbe_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
885                                         tx_offload);
886
887                                 txe->last_id = tx_last;
888                                 tx_id = txe->next_id;
889                                 txe = txn;
890                         }
891
892                         /*
893                          * Setup the TX Advanced Data Descriptor,
894                          * This path will go through
895                          * whatever new/reuse the context descriptor
896                          */
897                         cmd_type_len  |= tx_desc_ol_flags_to_cmdtype(ol_flags);
898                         olinfo_status |=
899                                 tx_desc_cksum_flags_to_olinfo(ol_flags);
900                         olinfo_status |= TXGBE_TXD_IDX(ctx);
901                 }
902
903                 olinfo_status |= TXGBE_TXD_PAYLEN(pkt_len);
904
905                 m_seg = tx_pkt;
906                 do {
907                         txd = &txr[tx_id];
908                         txn = &sw_ring[txe->next_id];
909                         rte_prefetch0(&txn->mbuf->pool);
910
911                         if (txe->mbuf != NULL)
912                                 rte_pktmbuf_free_seg(txe->mbuf);
913                         txe->mbuf = m_seg;
914
915                         /*
916                          * Set up Transmit Data Descriptor.
917                          */
918                         slen = m_seg->data_len;
919                         buf_dma_addr = rte_mbuf_data_iova(m_seg);
920                         txd->qw0 = rte_cpu_to_le_64(buf_dma_addr);
921                         txd->dw2 = rte_cpu_to_le_32(cmd_type_len | slen);
922                         txd->dw3 = rte_cpu_to_le_32(olinfo_status);
923                         txe->last_id = tx_last;
924                         tx_id = txe->next_id;
925                         txe = txn;
926                         m_seg = m_seg->next;
927                 } while (m_seg != NULL);
928
929                 /*
930                  * The last packet data descriptor needs End Of Packet (EOP)
931                  */
932                 cmd_type_len |= TXGBE_TXD_EOP;
933                 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
934
935                 txd->dw2 |= rte_cpu_to_le_32(cmd_type_len);
936         }
937
938 end_of_tx:
939
940         rte_wmb();
941
942         /*
943          * Set the Transmit Descriptor Tail (TDT)
944          */
945         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
946                    (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
947                    (uint16_t)tx_id, (uint16_t)nb_tx);
948         txgbe_set32_relaxed(txq->tdt_reg_addr, tx_id);
949         txq->tx_tail = tx_id;
950
951         return nb_tx;
952 }
953
954 /*********************************************************************
955  *
956  *  TX prep functions
957  *
958  **********************************************************************/
959 uint16_t
960 txgbe_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
961 {
962         int i, ret;
963         uint64_t ol_flags;
964         struct rte_mbuf *m;
965         struct txgbe_tx_queue *txq = (struct txgbe_tx_queue *)tx_queue;
966
967         for (i = 0; i < nb_pkts; i++) {
968                 m = tx_pkts[i];
969                 ol_flags = m->ol_flags;
970
971                 /**
972                  * Check if packet meets requirements for number of segments
973                  *
974                  * NOTE: for txgbe it's always (40 - WTHRESH) for both TSO and
975                  *       non-TSO
976                  */
977
978                 if (m->nb_segs > TXGBE_TX_MAX_SEG - txq->wthresh) {
979                         rte_errno = -EINVAL;
980                         return i;
981                 }
982
983                 if (ol_flags & TXGBE_TX_OFFLOAD_NOTSUP_MASK) {
984                         rte_errno = -ENOTSUP;
985                         return i;
986                 }
987
988 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
989                 ret = rte_validate_tx_offload(m);
990                 if (ret != 0) {
991                         rte_errno = ret;
992                         return i;
993                 }
994 #endif
995                 ret = rte_net_intel_cksum_prepare(m);
996                 if (ret != 0) {
997                         rte_errno = ret;
998                         return i;
999                 }
1000         }
1001
1002         return i;
1003 }
1004
1005 /*********************************************************************
1006  *
1007  *  RX functions
1008  *
1009  **********************************************************************/
1010 /* @note: fix txgbe_dev_supported_ptypes_get() if any change here. */
1011 static inline uint32_t
1012 txgbe_rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint16_t ptid_mask)
1013 {
1014         uint16_t ptid = TXGBE_RXD_PTID(pkt_info);
1015
1016         ptid &= ptid_mask;
1017
1018         return txgbe_decode_ptype(ptid);
1019 }
1020
1021 static inline uint64_t
1022 txgbe_rxd_pkt_info_to_pkt_flags(uint32_t pkt_info)
1023 {
1024         static uint64_t ip_rss_types_map[16] __rte_cache_aligned = {
1025                 0, PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, PKT_RX_RSS_HASH,
1026                 0, PKT_RX_RSS_HASH, 0, PKT_RX_RSS_HASH,
1027                 PKT_RX_RSS_HASH, 0, 0, 0,
1028                 0, 0, 0,  PKT_RX_FDIR,
1029         };
1030
1031         return ip_rss_types_map[TXGBE_RXD_RSSTYPE(pkt_info)];
1032 }
1033
1034 static inline uint64_t
1035 rx_desc_status_to_pkt_flags(uint32_t rx_status, uint64_t vlan_flags)
1036 {
1037         uint64_t pkt_flags;
1038
1039         /*
1040          * Check if VLAN present only.
1041          * Do not check whether L3/L4 rx checksum done by NIC or not,
1042          * That can be found from rte_eth_rxmode.offloads flag
1043          */
1044         pkt_flags = (rx_status & TXGBE_RXD_STAT_VLAN &&
1045                      vlan_flags & PKT_RX_VLAN_STRIPPED)
1046                     ? vlan_flags : 0;
1047
1048         return pkt_flags;
1049 }
1050
1051 static inline uint64_t
1052 rx_desc_error_to_pkt_flags(uint32_t rx_status)
1053 {
1054         uint64_t pkt_flags = 0;
1055
1056         /* checksum offload can't be disabled */
1057         if (rx_status & TXGBE_RXD_STAT_IPCS) {
1058                 pkt_flags |= (rx_status & TXGBE_RXD_ERR_IPCS
1059                                 ? PKT_RX_IP_CKSUM_BAD : PKT_RX_IP_CKSUM_GOOD);
1060         }
1061
1062         if (rx_status & TXGBE_RXD_STAT_L4CS) {
1063                 pkt_flags |= (rx_status & TXGBE_RXD_ERR_L4CS
1064                                 ? PKT_RX_L4_CKSUM_BAD : PKT_RX_L4_CKSUM_GOOD);
1065         }
1066
1067         if (rx_status & TXGBE_RXD_STAT_EIPCS &&
1068             rx_status & TXGBE_RXD_ERR_EIPCS) {
1069                 pkt_flags |= PKT_RX_EIP_CKSUM_BAD;
1070         }
1071
1072         return pkt_flags;
1073 }
1074
1075 /*
1076  * LOOK_AHEAD defines how many desc statuses to check beyond the
1077  * current descriptor.
1078  * It must be a pound define for optimal performance.
1079  * Do not change the value of LOOK_AHEAD, as the txgbe_rx_scan_hw_ring
1080  * function only works with LOOK_AHEAD=8.
1081  */
1082 #define LOOK_AHEAD 8
1083 #if (LOOK_AHEAD != 8)
1084 #error "PMD TXGBE: LOOK_AHEAD must be 8\n"
1085 #endif
1086 static inline int
1087 txgbe_rx_scan_hw_ring(struct txgbe_rx_queue *rxq)
1088 {
1089         volatile struct txgbe_rx_desc *rxdp;
1090         struct txgbe_rx_entry *rxep;
1091         struct rte_mbuf *mb;
1092         uint16_t pkt_len;
1093         uint64_t pkt_flags;
1094         int nb_dd;
1095         uint32_t s[LOOK_AHEAD];
1096         uint32_t pkt_info[LOOK_AHEAD];
1097         int i, j, nb_rx = 0;
1098         uint32_t status;
1099
1100         /* get references to current descriptor and S/W ring entry */
1101         rxdp = &rxq->rx_ring[rxq->rx_tail];
1102         rxep = &rxq->sw_ring[rxq->rx_tail];
1103
1104         status = rxdp->qw1.lo.status;
1105         /* check to make sure there is at least 1 packet to receive */
1106         if (!(status & rte_cpu_to_le_32(TXGBE_RXD_STAT_DD)))
1107                 return 0;
1108
1109         /*
1110          * Scan LOOK_AHEAD descriptors at a time to determine which descriptors
1111          * reference packets that are ready to be received.
1112          */
1113         for (i = 0; i < RTE_PMD_TXGBE_RX_MAX_BURST;
1114              i += LOOK_AHEAD, rxdp += LOOK_AHEAD, rxep += LOOK_AHEAD) {
1115                 /* Read desc statuses backwards to avoid race condition */
1116                 for (j = 0; j < LOOK_AHEAD; j++)
1117                         s[j] = rte_le_to_cpu_32(rxdp[j].qw1.lo.status);
1118
1119                 rte_smp_rmb();
1120
1121                 /* Compute how many status bits were set */
1122                 for (nb_dd = 0; nb_dd < LOOK_AHEAD &&
1123                                 (s[nb_dd] & TXGBE_RXD_STAT_DD); nb_dd++)
1124                         ;
1125
1126                 for (j = 0; j < nb_dd; j++)
1127                         pkt_info[j] = rte_le_to_cpu_32(rxdp[j].qw0.dw0);
1128
1129                 nb_rx += nb_dd;
1130
1131                 /* Translate descriptor info to mbuf format */
1132                 for (j = 0; j < nb_dd; ++j) {
1133                         mb = rxep[j].mbuf;
1134                         pkt_len = rte_le_to_cpu_16(rxdp[j].qw1.hi.len) -
1135                                   rxq->crc_len;
1136                         mb->data_len = pkt_len;
1137                         mb->pkt_len = pkt_len;
1138                         mb->vlan_tci = rte_le_to_cpu_16(rxdp[j].qw1.hi.tag);
1139
1140                         /* convert descriptor fields to rte mbuf flags */
1141                         pkt_flags = rx_desc_status_to_pkt_flags(s[j],
1142                                         rxq->vlan_flags);
1143                         pkt_flags |= rx_desc_error_to_pkt_flags(s[j]);
1144                         pkt_flags |=
1145                                 txgbe_rxd_pkt_info_to_pkt_flags(pkt_info[j]);
1146                         mb->ol_flags = pkt_flags;
1147                         mb->packet_type =
1148                                 txgbe_rxd_pkt_info_to_pkt_type(pkt_info[j],
1149                                 rxq->pkt_type_mask);
1150
1151                         if (likely(pkt_flags & PKT_RX_RSS_HASH))
1152                                 mb->hash.rss =
1153                                         rte_le_to_cpu_32(rxdp[j].qw0.dw1);
1154                         else if (pkt_flags & PKT_RX_FDIR) {
1155                                 mb->hash.fdir.hash =
1156                                         rte_le_to_cpu_16(rxdp[j].qw0.hi.csum) &
1157                                         TXGBE_ATR_HASH_MASK;
1158                                 mb->hash.fdir.id =
1159                                         rte_le_to_cpu_16(rxdp[j].qw0.hi.ipid);
1160                         }
1161                 }
1162
1163                 /* Move mbuf pointers from the S/W ring to the stage */
1164                 for (j = 0; j < LOOK_AHEAD; ++j)
1165                         rxq->rx_stage[i + j] = rxep[j].mbuf;
1166
1167                 /* stop if all requested packets could not be received */
1168                 if (nb_dd != LOOK_AHEAD)
1169                         break;
1170         }
1171
1172         /* clear software ring entries so we can cleanup correctly */
1173         for (i = 0; i < nb_rx; ++i)
1174                 rxq->sw_ring[rxq->rx_tail + i].mbuf = NULL;
1175
1176         return nb_rx;
1177 }
1178
1179 static inline int
1180 txgbe_rx_alloc_bufs(struct txgbe_rx_queue *rxq, bool reset_mbuf)
1181 {
1182         volatile struct txgbe_rx_desc *rxdp;
1183         struct txgbe_rx_entry *rxep;
1184         struct rte_mbuf *mb;
1185         uint16_t alloc_idx;
1186         __le64 dma_addr;
1187         int diag, i;
1188
1189         /* allocate buffers in bulk directly into the S/W ring */
1190         alloc_idx = rxq->rx_free_trigger - (rxq->rx_free_thresh - 1);
1191         rxep = &rxq->sw_ring[alloc_idx];
1192         diag = rte_mempool_get_bulk(rxq->mb_pool, (void *)rxep,
1193                                     rxq->rx_free_thresh);
1194         if (unlikely(diag != 0))
1195                 return -ENOMEM;
1196
1197         rxdp = &rxq->rx_ring[alloc_idx];
1198         for (i = 0; i < rxq->rx_free_thresh; ++i) {
1199                 /* populate the static rte mbuf fields */
1200                 mb = rxep[i].mbuf;
1201                 if (reset_mbuf)
1202                         mb->port = rxq->port_id;
1203
1204                 rte_mbuf_refcnt_set(mb, 1);
1205                 mb->data_off = RTE_PKTMBUF_HEADROOM;
1206
1207                 /* populate the descriptors */
1208                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mb));
1209                 TXGBE_RXD_HDRADDR(&rxdp[i], 0);
1210                 TXGBE_RXD_PKTADDR(&rxdp[i], dma_addr);
1211         }
1212
1213         /* update state of internal queue structure */
1214         rxq->rx_free_trigger = rxq->rx_free_trigger + rxq->rx_free_thresh;
1215         if (rxq->rx_free_trigger >= rxq->nb_rx_desc)
1216                 rxq->rx_free_trigger = rxq->rx_free_thresh - 1;
1217
1218         /* no errors */
1219         return 0;
1220 }
1221
1222 static inline uint16_t
1223 txgbe_rx_fill_from_stage(struct txgbe_rx_queue *rxq, struct rte_mbuf **rx_pkts,
1224                          uint16_t nb_pkts)
1225 {
1226         struct rte_mbuf **stage = &rxq->rx_stage[rxq->rx_next_avail];
1227         int i;
1228
1229         /* how many packets are ready to return? */
1230         nb_pkts = (uint16_t)RTE_MIN(nb_pkts, rxq->rx_nb_avail);
1231
1232         /* copy mbuf pointers to the application's packet list */
1233         for (i = 0; i < nb_pkts; ++i)
1234                 rx_pkts[i] = stage[i];
1235
1236         /* update internal queue state */
1237         rxq->rx_nb_avail = (uint16_t)(rxq->rx_nb_avail - nb_pkts);
1238         rxq->rx_next_avail = (uint16_t)(rxq->rx_next_avail + nb_pkts);
1239
1240         return nb_pkts;
1241 }
1242
1243 static inline uint16_t
1244 txgbe_rx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1245              uint16_t nb_pkts)
1246 {
1247         struct txgbe_rx_queue *rxq = (struct txgbe_rx_queue *)rx_queue;
1248         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1249         uint16_t nb_rx = 0;
1250
1251         /* Any previously recv'd pkts will be returned from the Rx stage */
1252         if (rxq->rx_nb_avail)
1253                 return txgbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1254
1255         /* Scan the H/W ring for packets to receive */
1256         nb_rx = (uint16_t)txgbe_rx_scan_hw_ring(rxq);
1257
1258         /* update internal queue state */
1259         rxq->rx_next_avail = 0;
1260         rxq->rx_nb_avail = nb_rx;
1261         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_rx);
1262
1263         /* if required, allocate new buffers to replenish descriptors */
1264         if (rxq->rx_tail > rxq->rx_free_trigger) {
1265                 uint16_t cur_free_trigger = rxq->rx_free_trigger;
1266
1267                 if (txgbe_rx_alloc_bufs(rxq, true) != 0) {
1268                         int i, j;
1269
1270                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1271                                    "queue_id=%u", (uint16_t)rxq->port_id,
1272                                    (uint16_t)rxq->queue_id);
1273
1274                         dev->data->rx_mbuf_alloc_failed +=
1275                                 rxq->rx_free_thresh;
1276
1277                         /*
1278                          * Need to rewind any previous receives if we cannot
1279                          * allocate new buffers to replenish the old ones.
1280                          */
1281                         rxq->rx_nb_avail = 0;
1282                         rxq->rx_tail = (uint16_t)(rxq->rx_tail - nb_rx);
1283                         for (i = 0, j = rxq->rx_tail; i < nb_rx; ++i, ++j)
1284                                 rxq->sw_ring[j].mbuf = rxq->rx_stage[i];
1285
1286                         return 0;
1287                 }
1288
1289                 /* update tail pointer */
1290                 rte_wmb();
1291                 txgbe_set32_relaxed(rxq->rdt_reg_addr, cur_free_trigger);
1292         }
1293
1294         if (rxq->rx_tail >= rxq->nb_rx_desc)
1295                 rxq->rx_tail = 0;
1296
1297         /* received any packets this loop? */
1298         if (rxq->rx_nb_avail)
1299                 return txgbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1300
1301         return 0;
1302 }
1303
1304 /* split requests into chunks of size RTE_PMD_TXGBE_RX_MAX_BURST */
1305 uint16_t
1306 txgbe_recv_pkts_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1307                            uint16_t nb_pkts)
1308 {
1309         uint16_t nb_rx;
1310
1311         if (unlikely(nb_pkts == 0))
1312                 return 0;
1313
1314         if (likely(nb_pkts <= RTE_PMD_TXGBE_RX_MAX_BURST))
1315                 return txgbe_rx_recv_pkts(rx_queue, rx_pkts, nb_pkts);
1316
1317         /* request is relatively large, chunk it up */
1318         nb_rx = 0;
1319         while (nb_pkts) {
1320                 uint16_t ret, n;
1321
1322                 n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_TXGBE_RX_MAX_BURST);
1323                 ret = txgbe_rx_recv_pkts(rx_queue, &rx_pkts[nb_rx], n);
1324                 nb_rx = (uint16_t)(nb_rx + ret);
1325                 nb_pkts = (uint16_t)(nb_pkts - ret);
1326                 if (ret < n)
1327                         break;
1328         }
1329
1330         return nb_rx;
1331 }
1332
1333 uint16_t
1334 txgbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1335                 uint16_t nb_pkts)
1336 {
1337         struct txgbe_rx_queue *rxq;
1338         volatile struct txgbe_rx_desc *rx_ring;
1339         volatile struct txgbe_rx_desc *rxdp;
1340         struct txgbe_rx_entry *sw_ring;
1341         struct txgbe_rx_entry *rxe;
1342         struct rte_mbuf *rxm;
1343         struct rte_mbuf *nmb;
1344         struct txgbe_rx_desc rxd;
1345         uint64_t dma_addr;
1346         uint32_t staterr;
1347         uint32_t pkt_info;
1348         uint16_t pkt_len;
1349         uint16_t rx_id;
1350         uint16_t nb_rx;
1351         uint16_t nb_hold;
1352         uint64_t pkt_flags;
1353
1354         nb_rx = 0;
1355         nb_hold = 0;
1356         rxq = rx_queue;
1357         rx_id = rxq->rx_tail;
1358         rx_ring = rxq->rx_ring;
1359         sw_ring = rxq->sw_ring;
1360         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1361         while (nb_rx < nb_pkts) {
1362                 /*
1363                  * The order of operations here is important as the DD status
1364                  * bit must not be read after any other descriptor fields.
1365                  * rx_ring and rxdp are pointing to volatile data so the order
1366                  * of accesses cannot be reordered by the compiler. If they were
1367                  * not volatile, they could be reordered which could lead to
1368                  * using invalid descriptor fields when read from rxd.
1369                  */
1370                 rxdp = &rx_ring[rx_id];
1371                 staterr = rxdp->qw1.lo.status;
1372                 if (!(staterr & rte_cpu_to_le_32(TXGBE_RXD_STAT_DD)))
1373                         break;
1374                 rxd = *rxdp;
1375
1376                 /*
1377                  * End of packet.
1378                  *
1379                  * If the TXGBE_RXD_STAT_EOP flag is not set, the RX packet
1380                  * is likely to be invalid and to be dropped by the various
1381                  * validation checks performed by the network stack.
1382                  *
1383                  * Allocate a new mbuf to replenish the RX ring descriptor.
1384                  * If the allocation fails:
1385                  *    - arrange for that RX descriptor to be the first one
1386                  *      being parsed the next time the receive function is
1387                  *      invoked [on the same queue].
1388                  *
1389                  *    - Stop parsing the RX ring and return immediately.
1390                  *
1391                  * This policy do not drop the packet received in the RX
1392                  * descriptor for which the allocation of a new mbuf failed.
1393                  * Thus, it allows that packet to be later retrieved if
1394                  * mbuf have been freed in the mean time.
1395                  * As a side effect, holding RX descriptors instead of
1396                  * systematically giving them back to the NIC may lead to
1397                  * RX ring exhaustion situations.
1398                  * However, the NIC can gracefully prevent such situations
1399                  * to happen by sending specific "back-pressure" flow control
1400                  * frames to its peer(s).
1401                  */
1402                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1403                            "ext_err_stat=0x%08x pkt_len=%u",
1404                            (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1405                            (uint16_t)rx_id, (uint32_t)staterr,
1406                            (uint16_t)rte_le_to_cpu_16(rxd.qw1.hi.len));
1407
1408                 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1409                 if (nmb == NULL) {
1410                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1411                                    "queue_id=%u", (uint16_t)rxq->port_id,
1412                                    (uint16_t)rxq->queue_id);
1413                         dev->data->rx_mbuf_alloc_failed++;
1414                         break;
1415                 }
1416
1417                 nb_hold++;
1418                 rxe = &sw_ring[rx_id];
1419                 rx_id++;
1420                 if (rx_id == rxq->nb_rx_desc)
1421                         rx_id = 0;
1422
1423                 /* Prefetch next mbuf while processing current one. */
1424                 rte_txgbe_prefetch(sw_ring[rx_id].mbuf);
1425
1426                 /*
1427                  * When next RX descriptor is on a cache-line boundary,
1428                  * prefetch the next 4 RX descriptors and the next 8 pointers
1429                  * to mbufs.
1430                  */
1431                 if ((rx_id & 0x3) == 0) {
1432                         rte_txgbe_prefetch(&rx_ring[rx_id]);
1433                         rte_txgbe_prefetch(&sw_ring[rx_id]);
1434                 }
1435
1436                 rxm = rxe->mbuf;
1437                 rxe->mbuf = nmb;
1438                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1439                 TXGBE_RXD_HDRADDR(rxdp, 0);
1440                 TXGBE_RXD_PKTADDR(rxdp, dma_addr);
1441
1442                 /*
1443                  * Initialize the returned mbuf.
1444                  * 1) setup generic mbuf fields:
1445                  *    - number of segments,
1446                  *    - next segment,
1447                  *    - packet length,
1448                  *    - RX port identifier.
1449                  * 2) integrate hardware offload data, if any:
1450                  *    - RSS flag & hash,
1451                  *    - IP checksum flag,
1452                  *    - VLAN TCI, if any,
1453                  *    - error flags.
1454                  */
1455                 pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.qw1.hi.len) -
1456                                       rxq->crc_len);
1457                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1458                 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
1459                 rxm->nb_segs = 1;
1460                 rxm->next = NULL;
1461                 rxm->pkt_len = pkt_len;
1462                 rxm->data_len = pkt_len;
1463                 rxm->port = rxq->port_id;
1464
1465                 pkt_info = rte_le_to_cpu_32(rxd.qw0.dw0);
1466                 /* Only valid if PKT_RX_VLAN set in pkt_flags */
1467                 rxm->vlan_tci = rte_le_to_cpu_16(rxd.qw1.hi.tag);
1468
1469                 pkt_flags = rx_desc_status_to_pkt_flags(staterr,
1470                                         rxq->vlan_flags);
1471                 pkt_flags |= rx_desc_error_to_pkt_flags(staterr);
1472                 pkt_flags |= txgbe_rxd_pkt_info_to_pkt_flags(pkt_info);
1473                 rxm->ol_flags = pkt_flags;
1474                 rxm->packet_type = txgbe_rxd_pkt_info_to_pkt_type(pkt_info,
1475                                                        rxq->pkt_type_mask);
1476
1477                 if (likely(pkt_flags & PKT_RX_RSS_HASH)) {
1478                         rxm->hash.rss = rte_le_to_cpu_32(rxd.qw0.dw1);
1479                 } else if (pkt_flags & PKT_RX_FDIR) {
1480                         rxm->hash.fdir.hash =
1481                                 rte_le_to_cpu_16(rxd.qw0.hi.csum) &
1482                                 TXGBE_ATR_HASH_MASK;
1483                         rxm->hash.fdir.id = rte_le_to_cpu_16(rxd.qw0.hi.ipid);
1484                 }
1485                 /*
1486                  * Store the mbuf address into the next entry of the array
1487                  * of returned packets.
1488                  */
1489                 rx_pkts[nb_rx++] = rxm;
1490         }
1491         rxq->rx_tail = rx_id;
1492
1493         /*
1494          * If the number of free RX descriptors is greater than the RX free
1495          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1496          * register.
1497          * Update the RDT with the value of the last processed RX descriptor
1498          * minus 1, to guarantee that the RDT register is never equal to the
1499          * RDH register, which creates a "full" ring situation from the
1500          * hardware point of view...
1501          */
1502         nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
1503         if (nb_hold > rxq->rx_free_thresh) {
1504                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1505                            "nb_hold=%u nb_rx=%u",
1506                            (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1507                            (uint16_t)rx_id, (uint16_t)nb_hold,
1508                            (uint16_t)nb_rx);
1509                 rx_id = (uint16_t)((rx_id == 0) ?
1510                                 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1511                 txgbe_set32(rxq->rdt_reg_addr, rx_id);
1512                 nb_hold = 0;
1513         }
1514         rxq->nb_rx_hold = nb_hold;
1515         return nb_rx;
1516 }
1517
1518 /**
1519  * txgbe_fill_cluster_head_buf - fill the first mbuf of the returned packet
1520  *
1521  * Fill the following info in the HEAD buffer of the Rx cluster:
1522  *    - RX port identifier
1523  *    - hardware offload data, if any:
1524  *      - RSS flag & hash
1525  *      - IP checksum flag
1526  *      - VLAN TCI, if any
1527  *      - error flags
1528  * @head HEAD of the packet cluster
1529  * @desc HW descriptor to get data from
1530  * @rxq Pointer to the Rx queue
1531  */
1532 static inline void
1533 txgbe_fill_cluster_head_buf(struct rte_mbuf *head, struct txgbe_rx_desc *desc,
1534                 struct txgbe_rx_queue *rxq, uint32_t staterr)
1535 {
1536         uint32_t pkt_info;
1537         uint64_t pkt_flags;
1538
1539         head->port = rxq->port_id;
1540
1541         /* The vlan_tci field is only valid when PKT_RX_VLAN is
1542          * set in the pkt_flags field.
1543          */
1544         head->vlan_tci = rte_le_to_cpu_16(desc->qw1.hi.tag);
1545         pkt_info = rte_le_to_cpu_32(desc->qw0.dw0);
1546         pkt_flags = rx_desc_status_to_pkt_flags(staterr, rxq->vlan_flags);
1547         pkt_flags |= rx_desc_error_to_pkt_flags(staterr);
1548         pkt_flags |= txgbe_rxd_pkt_info_to_pkt_flags(pkt_info);
1549         head->ol_flags = pkt_flags;
1550         head->packet_type = txgbe_rxd_pkt_info_to_pkt_type(pkt_info,
1551                                                 rxq->pkt_type_mask);
1552
1553         if (likely(pkt_flags & PKT_RX_RSS_HASH)) {
1554                 head->hash.rss = rte_le_to_cpu_32(desc->qw0.dw1);
1555         } else if (pkt_flags & PKT_RX_FDIR) {
1556                 head->hash.fdir.hash = rte_le_to_cpu_16(desc->qw0.hi.csum)
1557                                 & TXGBE_ATR_HASH_MASK;
1558                 head->hash.fdir.id = rte_le_to_cpu_16(desc->qw0.hi.ipid);
1559         }
1560 }
1561
1562 /**
1563  * txgbe_recv_pkts_lro - receive handler for and LRO case.
1564  *
1565  * @rx_queue Rx queue handle
1566  * @rx_pkts table of received packets
1567  * @nb_pkts size of rx_pkts table
1568  * @bulk_alloc if TRUE bulk allocation is used for a HW ring refilling
1569  *
1570  * Handles the Rx HW ring completions when RSC feature is configured. Uses an
1571  * additional ring of txgbe_rsc_entry's that will hold the relevant RSC info.
1572  *
1573  * We use the same logic as in Linux and in FreeBSD txgbe drivers:
1574  * 1) When non-EOP RSC completion arrives:
1575  *    a) Update the HEAD of the current RSC aggregation cluster with the new
1576  *       segment's data length.
1577  *    b) Set the "next" pointer of the current segment to point to the segment
1578  *       at the NEXTP index.
1579  *    c) Pass the HEAD of RSC aggregation cluster on to the next NEXTP entry
1580  *       in the sw_rsc_ring.
1581  * 2) When EOP arrives we just update the cluster's total length and offload
1582  *    flags and deliver the cluster up to the upper layers. In our case - put it
1583  *    in the rx_pkts table.
1584  *
1585  * Returns the number of received packets/clusters (according to the "bulk
1586  * receive" interface).
1587  */
1588 static inline uint16_t
1589 txgbe_recv_pkts_lro(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts,
1590                     bool bulk_alloc)
1591 {
1592         struct txgbe_rx_queue *rxq = rx_queue;
1593         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1594         volatile struct txgbe_rx_desc *rx_ring = rxq->rx_ring;
1595         struct txgbe_rx_entry *sw_ring = rxq->sw_ring;
1596         struct txgbe_scattered_rx_entry *sw_sc_ring = rxq->sw_sc_ring;
1597         uint16_t rx_id = rxq->rx_tail;
1598         uint16_t nb_rx = 0;
1599         uint16_t nb_hold = rxq->nb_rx_hold;
1600         uint16_t prev_id = rxq->rx_tail;
1601
1602         while (nb_rx < nb_pkts) {
1603                 bool eop;
1604                 struct txgbe_rx_entry *rxe;
1605                 struct txgbe_scattered_rx_entry *sc_entry;
1606                 struct txgbe_scattered_rx_entry *next_sc_entry = NULL;
1607                 struct txgbe_rx_entry *next_rxe = NULL;
1608                 struct rte_mbuf *first_seg;
1609                 struct rte_mbuf *rxm;
1610                 struct rte_mbuf *nmb = NULL;
1611                 struct txgbe_rx_desc rxd;
1612                 uint16_t data_len;
1613                 uint16_t next_id;
1614                 volatile struct txgbe_rx_desc *rxdp;
1615                 uint32_t staterr;
1616
1617 next_desc:
1618                 /*
1619                  * The code in this whole file uses the volatile pointer to
1620                  * ensure the read ordering of the status and the rest of the
1621                  * descriptor fields (on the compiler level only!!!). This is so
1622                  * UGLY - why not to just use the compiler barrier instead? DPDK
1623                  * even has the rte_compiler_barrier() for that.
1624                  *
1625                  * But most importantly this is just wrong because this doesn't
1626                  * ensure memory ordering in a general case at all. For
1627                  * instance, DPDK is supposed to work on Power CPUs where
1628                  * compiler barrier may just not be enough!
1629                  *
1630                  * I tried to write only this function properly to have a
1631                  * starting point (as a part of an LRO/RSC series) but the
1632                  * compiler cursed at me when I tried to cast away the
1633                  * "volatile" from rx_ring (yes, it's volatile too!!!). So, I'm
1634                  * keeping it the way it is for now.
1635                  *
1636                  * The code in this file is broken in so many other places and
1637                  * will just not work on a big endian CPU anyway therefore the
1638                  * lines below will have to be revisited together with the rest
1639                  * of the txgbe PMD.
1640                  *
1641                  * TODO:
1642                  *    - Get rid of "volatile" and let the compiler do its job.
1643                  *    - Use the proper memory barrier (rte_rmb()) to ensure the
1644                  *      memory ordering below.
1645                  */
1646                 rxdp = &rx_ring[rx_id];
1647                 staterr = rte_le_to_cpu_32(rxdp->qw1.lo.status);
1648
1649                 if (!(staterr & TXGBE_RXD_STAT_DD))
1650                         break;
1651
1652                 rxd = *rxdp;
1653
1654                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1655                                   "staterr=0x%x data_len=%u",
1656                            rxq->port_id, rxq->queue_id, rx_id, staterr,
1657                            rte_le_to_cpu_16(rxd.qw1.hi.len));
1658
1659                 if (!bulk_alloc) {
1660                         nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1661                         if (nmb == NULL) {
1662                                 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed "
1663                                                   "port_id=%u queue_id=%u",
1664                                            rxq->port_id, rxq->queue_id);
1665
1666                                 dev->data->rx_mbuf_alloc_failed++;
1667                                 break;
1668                         }
1669                 } else if (nb_hold > rxq->rx_free_thresh) {
1670                         uint16_t next_rdt = rxq->rx_free_trigger;
1671
1672                         if (!txgbe_rx_alloc_bufs(rxq, false)) {
1673                                 rte_wmb();
1674                                 txgbe_set32_relaxed(rxq->rdt_reg_addr,
1675                                                             next_rdt);
1676                                 nb_hold -= rxq->rx_free_thresh;
1677                         } else {
1678                                 PMD_RX_LOG(DEBUG, "RX bulk alloc failed "
1679                                                   "port_id=%u queue_id=%u",
1680                                            rxq->port_id, rxq->queue_id);
1681
1682                                 dev->data->rx_mbuf_alloc_failed++;
1683                                 break;
1684                         }
1685                 }
1686
1687                 nb_hold++;
1688                 rxe = &sw_ring[rx_id];
1689                 eop = staterr & TXGBE_RXD_STAT_EOP;
1690
1691                 next_id = rx_id + 1;
1692                 if (next_id == rxq->nb_rx_desc)
1693                         next_id = 0;
1694
1695                 /* Prefetch next mbuf while processing current one. */
1696                 rte_txgbe_prefetch(sw_ring[next_id].mbuf);
1697
1698                 /*
1699                  * When next RX descriptor is on a cache-line boundary,
1700                  * prefetch the next 4 RX descriptors and the next 4 pointers
1701                  * to mbufs.
1702                  */
1703                 if ((next_id & 0x3) == 0) {
1704                         rte_txgbe_prefetch(&rx_ring[next_id]);
1705                         rte_txgbe_prefetch(&sw_ring[next_id]);
1706                 }
1707
1708                 rxm = rxe->mbuf;
1709
1710                 if (!bulk_alloc) {
1711                         __le64 dma =
1712                           rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1713                         /*
1714                          * Update RX descriptor with the physical address of the
1715                          * new data buffer of the new allocated mbuf.
1716                          */
1717                         rxe->mbuf = nmb;
1718
1719                         rxm->data_off = RTE_PKTMBUF_HEADROOM;
1720                         TXGBE_RXD_HDRADDR(rxdp, 0);
1721                         TXGBE_RXD_PKTADDR(rxdp, dma);
1722                 } else {
1723                         rxe->mbuf = NULL;
1724                 }
1725
1726                 /*
1727                  * Set data length & data buffer address of mbuf.
1728                  */
1729                 data_len = rte_le_to_cpu_16(rxd.qw1.hi.len);
1730                 rxm->data_len = data_len;
1731
1732                 if (!eop) {
1733                         uint16_t nextp_id;
1734                         /*
1735                          * Get next descriptor index:
1736                          *  - For RSC it's in the NEXTP field.
1737                          *  - For a scattered packet - it's just a following
1738                          *    descriptor.
1739                          */
1740                         if (TXGBE_RXD_RSCCNT(rxd.qw0.dw0))
1741                                 nextp_id = TXGBE_RXD_NEXTP(staterr);
1742                         else
1743                                 nextp_id = next_id;
1744
1745                         next_sc_entry = &sw_sc_ring[nextp_id];
1746                         next_rxe = &sw_ring[nextp_id];
1747                         rte_txgbe_prefetch(next_rxe);
1748                 }
1749
1750                 sc_entry = &sw_sc_ring[rx_id];
1751                 first_seg = sc_entry->fbuf;
1752                 sc_entry->fbuf = NULL;
1753
1754                 /*
1755                  * If this is the first buffer of the received packet,
1756                  * set the pointer to the first mbuf of the packet and
1757                  * initialize its context.
1758                  * Otherwise, update the total length and the number of segments
1759                  * of the current scattered packet, and update the pointer to
1760                  * the last mbuf of the current packet.
1761                  */
1762                 if (first_seg == NULL) {
1763                         first_seg = rxm;
1764                         first_seg->pkt_len = data_len;
1765                         first_seg->nb_segs = 1;
1766                 } else {
1767                         first_seg->pkt_len += data_len;
1768                         first_seg->nb_segs++;
1769                 }
1770
1771                 prev_id = rx_id;
1772                 rx_id = next_id;
1773
1774                 /*
1775                  * If this is not the last buffer of the received packet, update
1776                  * the pointer to the first mbuf at the NEXTP entry in the
1777                  * sw_sc_ring and continue to parse the RX ring.
1778                  */
1779                 if (!eop && next_rxe) {
1780                         rxm->next = next_rxe->mbuf;
1781                         next_sc_entry->fbuf = first_seg;
1782                         goto next_desc;
1783                 }
1784
1785                 /* Initialize the first mbuf of the returned packet */
1786                 txgbe_fill_cluster_head_buf(first_seg, &rxd, rxq, staterr);
1787
1788                 /*
1789                  * Deal with the case, when HW CRC srip is disabled.
1790                  * That can't happen when LRO is enabled, but still could
1791                  * happen for scattered RX mode.
1792                  */
1793                 first_seg->pkt_len -= rxq->crc_len;
1794                 if (unlikely(rxm->data_len <= rxq->crc_len)) {
1795                         struct rte_mbuf *lp;
1796
1797                         for (lp = first_seg; lp->next != rxm; lp = lp->next)
1798                                 ;
1799
1800                         first_seg->nb_segs--;
1801                         lp->data_len -= rxq->crc_len - rxm->data_len;
1802                         lp->next = NULL;
1803                         rte_pktmbuf_free_seg(rxm);
1804                 } else {
1805                         rxm->data_len -= rxq->crc_len;
1806                 }
1807
1808                 /* Prefetch data of first segment, if configured to do so. */
1809                 rte_packet_prefetch((char *)first_seg->buf_addr +
1810                         first_seg->data_off);
1811
1812                 /*
1813                  * Store the mbuf address into the next entry of the array
1814                  * of returned packets.
1815                  */
1816                 rx_pkts[nb_rx++] = first_seg;
1817         }
1818
1819         /*
1820          * Record index of the next RX descriptor to probe.
1821          */
1822         rxq->rx_tail = rx_id;
1823
1824         /*
1825          * If the number of free RX descriptors is greater than the RX free
1826          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1827          * register.
1828          * Update the RDT with the value of the last processed RX descriptor
1829          * minus 1, to guarantee that the RDT register is never equal to the
1830          * RDH register, which creates a "full" ring situation from the
1831          * hardware point of view...
1832          */
1833         if (!bulk_alloc && nb_hold > rxq->rx_free_thresh) {
1834                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1835                            "nb_hold=%u nb_rx=%u",
1836                            rxq->port_id, rxq->queue_id, rx_id, nb_hold, nb_rx);
1837
1838                 rte_wmb();
1839                 txgbe_set32_relaxed(rxq->rdt_reg_addr, prev_id);
1840                 nb_hold = 0;
1841         }
1842
1843         rxq->nb_rx_hold = nb_hold;
1844         return nb_rx;
1845 }
1846
1847 uint16_t
1848 txgbe_recv_pkts_lro_single_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1849                                  uint16_t nb_pkts)
1850 {
1851         return txgbe_recv_pkts_lro(rx_queue, rx_pkts, nb_pkts, false);
1852 }
1853
1854 uint16_t
1855 txgbe_recv_pkts_lro_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1856                                uint16_t nb_pkts)
1857 {
1858         return txgbe_recv_pkts_lro(rx_queue, rx_pkts, nb_pkts, true);
1859 }
1860
1861 uint64_t
1862 txgbe_get_rx_queue_offloads(struct rte_eth_dev *dev __rte_unused)
1863 {
1864         return DEV_RX_OFFLOAD_VLAN_STRIP;
1865 }
1866
1867 uint64_t
1868 txgbe_get_rx_port_offloads(struct rte_eth_dev *dev)
1869 {
1870         uint64_t offloads;
1871         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1872         struct rte_eth_dev_sriov *sriov = &RTE_ETH_DEV_SRIOV(dev);
1873
1874         offloads = DEV_RX_OFFLOAD_IPV4_CKSUM  |
1875                    DEV_RX_OFFLOAD_UDP_CKSUM   |
1876                    DEV_RX_OFFLOAD_TCP_CKSUM   |
1877                    DEV_RX_OFFLOAD_KEEP_CRC    |
1878                    DEV_RX_OFFLOAD_JUMBO_FRAME |
1879                    DEV_RX_OFFLOAD_VLAN_FILTER |
1880                    DEV_RX_OFFLOAD_RSS_HASH |
1881                    DEV_RX_OFFLOAD_SCATTER;
1882
1883         if (!txgbe_is_vf(dev))
1884                 offloads |= (DEV_RX_OFFLOAD_VLAN_FILTER |
1885                              DEV_RX_OFFLOAD_QINQ_STRIP |
1886                              DEV_RX_OFFLOAD_VLAN_EXTEND);
1887
1888         /*
1889          * RSC is only supported by PF devices in a non-SR-IOV
1890          * mode.
1891          */
1892         if (hw->mac.type == txgbe_mac_raptor && !sriov->active)
1893                 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
1894
1895         if (hw->mac.type == txgbe_mac_raptor)
1896                 offloads |= DEV_RX_OFFLOAD_MACSEC_STRIP;
1897
1898         offloads |= DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM;
1899
1900         return offloads;
1901 }
1902
1903 static void __rte_cold
1904 txgbe_tx_queue_release_mbufs(struct txgbe_tx_queue *txq)
1905 {
1906         unsigned int i;
1907
1908         if (txq->sw_ring != NULL) {
1909                 for (i = 0; i < txq->nb_tx_desc; i++) {
1910                         if (txq->sw_ring[i].mbuf != NULL) {
1911                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1912                                 txq->sw_ring[i].mbuf = NULL;
1913                         }
1914                 }
1915         }
1916 }
1917
1918 static void __rte_cold
1919 txgbe_tx_free_swring(struct txgbe_tx_queue *txq)
1920 {
1921         if (txq != NULL &&
1922             txq->sw_ring != NULL)
1923                 rte_free(txq->sw_ring);
1924 }
1925
1926 static void __rte_cold
1927 txgbe_tx_queue_release(struct txgbe_tx_queue *txq)
1928 {
1929         if (txq != NULL && txq->ops != NULL) {
1930                 txq->ops->release_mbufs(txq);
1931                 txq->ops->free_swring(txq);
1932                 rte_free(txq);
1933         }
1934 }
1935
1936 void __rte_cold
1937 txgbe_dev_tx_queue_release(void *txq)
1938 {
1939         txgbe_tx_queue_release(txq);
1940 }
1941
1942 static const struct txgbe_txq_ops def_txq_ops = {
1943         .release_mbufs = txgbe_tx_queue_release_mbufs,
1944         .free_swring = txgbe_tx_free_swring,
1945 };
1946
1947 /* Takes an ethdev and a queue and sets up the tx function to be used based on
1948  * the queue parameters. Used in tx_queue_setup by primary process and then
1949  * in dev_init by secondary process when attaching to an existing ethdev.
1950  */
1951 void __rte_cold
1952 txgbe_set_tx_function(struct rte_eth_dev *dev, struct txgbe_tx_queue *txq)
1953 {
1954         /* Use a simple Tx queue (no offloads, no multi segs) if possible */
1955         if (txq->offloads == 0 &&
1956                         txq->tx_free_thresh >= RTE_PMD_TXGBE_TX_MAX_BURST) {
1957                 PMD_INIT_LOG(DEBUG, "Using simple tx code path");
1958                 dev->tx_pkt_burst = txgbe_xmit_pkts_simple;
1959                 dev->tx_pkt_prepare = NULL;
1960         } else {
1961                 PMD_INIT_LOG(DEBUG, "Using full-featured tx code path");
1962                 PMD_INIT_LOG(DEBUG,
1963                                 " - offloads = 0x%" PRIx64,
1964                                 txq->offloads);
1965                 PMD_INIT_LOG(DEBUG,
1966                                 " - tx_free_thresh = %lu [RTE_PMD_TXGBE_TX_MAX_BURST=%lu]",
1967                                 (unsigned long)txq->tx_free_thresh,
1968                                 (unsigned long)RTE_PMD_TXGBE_TX_MAX_BURST);
1969                 dev->tx_pkt_burst = txgbe_xmit_pkts;
1970                 dev->tx_pkt_prepare = txgbe_prep_pkts;
1971         }
1972 }
1973
1974 uint64_t
1975 txgbe_get_tx_queue_offloads(struct rte_eth_dev *dev)
1976 {
1977         RTE_SET_USED(dev);
1978
1979         return 0;
1980 }
1981
1982 uint64_t
1983 txgbe_get_tx_port_offloads(struct rte_eth_dev *dev)
1984 {
1985         uint64_t tx_offload_capa;
1986
1987         tx_offload_capa =
1988                 DEV_TX_OFFLOAD_VLAN_INSERT |
1989                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
1990                 DEV_TX_OFFLOAD_UDP_CKSUM   |
1991                 DEV_TX_OFFLOAD_TCP_CKSUM   |
1992                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
1993                 DEV_TX_OFFLOAD_TCP_TSO     |
1994                 DEV_TX_OFFLOAD_UDP_TSO     |
1995                 DEV_TX_OFFLOAD_UDP_TNL_TSO      |
1996                 DEV_TX_OFFLOAD_IP_TNL_TSO       |
1997                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO    |
1998                 DEV_TX_OFFLOAD_GRE_TNL_TSO      |
1999                 DEV_TX_OFFLOAD_IPIP_TNL_TSO     |
2000                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO   |
2001                 DEV_TX_OFFLOAD_MULTI_SEGS;
2002
2003         if (!txgbe_is_vf(dev))
2004                 tx_offload_capa |= DEV_TX_OFFLOAD_QINQ_INSERT;
2005
2006         tx_offload_capa |= DEV_TX_OFFLOAD_MACSEC_INSERT;
2007
2008         tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
2009
2010         return tx_offload_capa;
2011 }
2012
2013 int __rte_cold
2014 txgbe_dev_tx_queue_setup(struct rte_eth_dev *dev,
2015                          uint16_t queue_idx,
2016                          uint16_t nb_desc,
2017                          unsigned int socket_id,
2018                          const struct rte_eth_txconf *tx_conf)
2019 {
2020         const struct rte_memzone *tz;
2021         struct txgbe_tx_queue *txq;
2022         struct txgbe_hw     *hw;
2023         uint16_t tx_free_thresh;
2024         uint64_t offloads;
2025
2026         PMD_INIT_FUNC_TRACE();
2027         hw = TXGBE_DEV_HW(dev);
2028
2029         offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
2030
2031         /*
2032          * Validate number of transmit descriptors.
2033          * It must not exceed hardware maximum, and must be multiple
2034          * of TXGBE_ALIGN.
2035          */
2036         if (nb_desc % TXGBE_TXD_ALIGN != 0 ||
2037             nb_desc > TXGBE_RING_DESC_MAX ||
2038             nb_desc < TXGBE_RING_DESC_MIN) {
2039                 return -EINVAL;
2040         }
2041
2042         /*
2043          * The TX descriptor ring will be cleaned after txq->tx_free_thresh
2044          * descriptors are used or if the number of descriptors required
2045          * to transmit a packet is greater than the number of free TX
2046          * descriptors.
2047          * One descriptor in the TX ring is used as a sentinel to avoid a
2048          * H/W race condition, hence the maximum threshold constraints.
2049          * When set to zero use default values.
2050          */
2051         tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
2052                         tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH);
2053         if (tx_free_thresh >= (nb_desc - 3)) {
2054                 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the number of "
2055                              "TX descriptors minus 3. (tx_free_thresh=%u "
2056                              "port=%d queue=%d)",
2057                              (unsigned int)tx_free_thresh,
2058                              (int)dev->data->port_id, (int)queue_idx);
2059                 return -(EINVAL);
2060         }
2061
2062         if ((nb_desc % tx_free_thresh) != 0) {
2063                 PMD_INIT_LOG(ERR, "tx_free_thresh must be a divisor of the "
2064                              "number of TX descriptors. (tx_free_thresh=%u "
2065                              "port=%d queue=%d)", (unsigned int)tx_free_thresh,
2066                              (int)dev->data->port_id, (int)queue_idx);
2067                 return -(EINVAL);
2068         }
2069
2070         /* Free memory prior to re-allocation if needed... */
2071         if (dev->data->tx_queues[queue_idx] != NULL) {
2072                 txgbe_tx_queue_release(dev->data->tx_queues[queue_idx]);
2073                 dev->data->tx_queues[queue_idx] = NULL;
2074         }
2075
2076         /* First allocate the tx queue data structure */
2077         txq = rte_zmalloc_socket("ethdev TX queue",
2078                                  sizeof(struct txgbe_tx_queue),
2079                                  RTE_CACHE_LINE_SIZE, socket_id);
2080         if (txq == NULL)
2081                 return -ENOMEM;
2082
2083         /*
2084          * Allocate TX 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         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
2089                         sizeof(struct txgbe_tx_desc) * TXGBE_RING_DESC_MAX,
2090                         TXGBE_ALIGN, socket_id);
2091         if (tz == NULL) {
2092                 txgbe_tx_queue_release(txq);
2093                 return -ENOMEM;
2094         }
2095
2096         txq->nb_tx_desc = nb_desc;
2097         txq->tx_free_thresh = tx_free_thresh;
2098         txq->pthresh = tx_conf->tx_thresh.pthresh;
2099         txq->hthresh = tx_conf->tx_thresh.hthresh;
2100         txq->wthresh = tx_conf->tx_thresh.wthresh;
2101         txq->queue_id = queue_idx;
2102         txq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
2103                 queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
2104         txq->port_id = dev->data->port_id;
2105         txq->offloads = offloads;
2106         txq->ops = &def_txq_ops;
2107         txq->tx_deferred_start = tx_conf->tx_deferred_start;
2108
2109         /* Modification to set tail pointer for virtual function
2110          * if vf is detected.
2111          */
2112         if (hw->mac.type == txgbe_mac_raptor_vf) {
2113                 txq->tdt_reg_addr = TXGBE_REG_ADDR(hw, TXGBE_TXWP(queue_idx));
2114                 txq->tdc_reg_addr = TXGBE_REG_ADDR(hw, TXGBE_TXCFG(queue_idx));
2115         } else {
2116                 txq->tdt_reg_addr = TXGBE_REG_ADDR(hw,
2117                                                 TXGBE_TXWP(txq->reg_idx));
2118                 txq->tdc_reg_addr = TXGBE_REG_ADDR(hw,
2119                                                 TXGBE_TXCFG(txq->reg_idx));
2120         }
2121
2122         txq->tx_ring_phys_addr = TMZ_PADDR(tz);
2123         txq->tx_ring = (struct txgbe_tx_desc *)TMZ_VADDR(tz);
2124
2125         /* Allocate software ring */
2126         txq->sw_ring = rte_zmalloc_socket("txq->sw_ring",
2127                                 sizeof(struct txgbe_tx_entry) * nb_desc,
2128                                 RTE_CACHE_LINE_SIZE, socket_id);
2129         if (txq->sw_ring == NULL) {
2130                 txgbe_tx_queue_release(txq);
2131                 return -ENOMEM;
2132         }
2133         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%" PRIx64,
2134                      txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
2135
2136         /* set up scalar TX function as appropriate */
2137         txgbe_set_tx_function(dev, txq);
2138
2139         txq->ops->reset(txq);
2140
2141         dev->data->tx_queues[queue_idx] = txq;
2142
2143         return 0;
2144 }
2145
2146 /**
2147  * txgbe_free_sc_cluster - free the not-yet-completed scattered cluster
2148  *
2149  * The "next" pointer of the last segment of (not-yet-completed) RSC clusters
2150  * in the sw_rsc_ring is not set to NULL but rather points to the next
2151  * mbuf of this RSC aggregation (that has not been completed yet and still
2152  * resides on the HW ring). So, instead of calling for rte_pktmbuf_free() we
2153  * will just free first "nb_segs" segments of the cluster explicitly by calling
2154  * an rte_pktmbuf_free_seg().
2155  *
2156  * @m scattered cluster head
2157  */
2158 static void __rte_cold
2159 txgbe_free_sc_cluster(struct rte_mbuf *m)
2160 {
2161         uint16_t i, nb_segs = m->nb_segs;
2162         struct rte_mbuf *next_seg;
2163
2164         for (i = 0; i < nb_segs; i++) {
2165                 next_seg = m->next;
2166                 rte_pktmbuf_free_seg(m);
2167                 m = next_seg;
2168         }
2169 }
2170
2171 static void __rte_cold
2172 txgbe_rx_queue_release_mbufs(struct txgbe_rx_queue *rxq)
2173 {
2174         unsigned int i;
2175
2176         if (rxq->sw_ring != NULL) {
2177                 for (i = 0; i < rxq->nb_rx_desc; i++) {
2178                         if (rxq->sw_ring[i].mbuf != NULL) {
2179                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
2180                                 rxq->sw_ring[i].mbuf = NULL;
2181                         }
2182                 }
2183                 if (rxq->rx_nb_avail) {
2184                         for (i = 0; i < rxq->rx_nb_avail; ++i) {
2185                                 struct rte_mbuf *mb;
2186
2187                                 mb = rxq->rx_stage[rxq->rx_next_avail + i];
2188                                 rte_pktmbuf_free_seg(mb);
2189                         }
2190                         rxq->rx_nb_avail = 0;
2191                 }
2192         }
2193
2194         if (rxq->sw_sc_ring)
2195                 for (i = 0; i < rxq->nb_rx_desc; i++)
2196                         if (rxq->sw_sc_ring[i].fbuf) {
2197                                 txgbe_free_sc_cluster(rxq->sw_sc_ring[i].fbuf);
2198                                 rxq->sw_sc_ring[i].fbuf = NULL;
2199                         }
2200 }
2201
2202 static void __rte_cold
2203 txgbe_rx_queue_release(struct txgbe_rx_queue *rxq)
2204 {
2205         if (rxq != NULL) {
2206                 txgbe_rx_queue_release_mbufs(rxq);
2207                 rte_free(rxq->sw_ring);
2208                 rte_free(rxq->sw_sc_ring);
2209                 rte_free(rxq);
2210         }
2211 }
2212
2213 void __rte_cold
2214 txgbe_dev_rx_queue_release(void *rxq)
2215 {
2216         txgbe_rx_queue_release(rxq);
2217 }
2218
2219 /*
2220  * Check if Rx Burst Bulk Alloc function can be used.
2221  * Return
2222  *        0: the preconditions are satisfied and the bulk allocation function
2223  *           can be used.
2224  *  -EINVAL: the preconditions are NOT satisfied and the default Rx burst
2225  *           function must be used.
2226  */
2227 static inline int __rte_cold
2228 check_rx_burst_bulk_alloc_preconditions(struct txgbe_rx_queue *rxq)
2229 {
2230         int ret = 0;
2231
2232         /*
2233          * Make sure the following pre-conditions are satisfied:
2234          *   rxq->rx_free_thresh >= RTE_PMD_TXGBE_RX_MAX_BURST
2235          *   rxq->rx_free_thresh < rxq->nb_rx_desc
2236          *   (rxq->nb_rx_desc % rxq->rx_free_thresh) == 0
2237          * Scattered packets are not supported.  This should be checked
2238          * outside of this function.
2239          */
2240         if (!(rxq->rx_free_thresh >= RTE_PMD_TXGBE_RX_MAX_BURST)) {
2241                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
2242                              "rxq->rx_free_thresh=%d, "
2243                              "RTE_PMD_TXGBE_RX_MAX_BURST=%d",
2244                              rxq->rx_free_thresh, RTE_PMD_TXGBE_RX_MAX_BURST);
2245                 ret = -EINVAL;
2246         } else if (!(rxq->rx_free_thresh < rxq->nb_rx_desc)) {
2247                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
2248                              "rxq->rx_free_thresh=%d, "
2249                              "rxq->nb_rx_desc=%d",
2250                              rxq->rx_free_thresh, rxq->nb_rx_desc);
2251                 ret = -EINVAL;
2252         } else if (!((rxq->nb_rx_desc % rxq->rx_free_thresh) == 0)) {
2253                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
2254                              "rxq->nb_rx_desc=%d, "
2255                              "rxq->rx_free_thresh=%d",
2256                              rxq->nb_rx_desc, rxq->rx_free_thresh);
2257                 ret = -EINVAL;
2258         }
2259
2260         return ret;
2261 }
2262
2263 /* Reset dynamic txgbe_rx_queue fields back to defaults */
2264 static void __rte_cold
2265 txgbe_reset_rx_queue(struct txgbe_adapter *adapter, struct txgbe_rx_queue *rxq)
2266 {
2267         static const struct txgbe_rx_desc zeroed_desc = {
2268                                                 {{0}, {0} }, {{0}, {0} } };
2269         unsigned int i;
2270         uint16_t len = rxq->nb_rx_desc;
2271
2272         /*
2273          * By default, the Rx queue setup function allocates enough memory for
2274          * TXGBE_RING_DESC_MAX.  The Rx Burst bulk allocation function requires
2275          * extra memory at the end of the descriptor ring to be zero'd out.
2276          */
2277         if (adapter->rx_bulk_alloc_allowed)
2278                 /* zero out extra memory */
2279                 len += RTE_PMD_TXGBE_RX_MAX_BURST;
2280
2281         /*
2282          * Zero out HW ring memory. Zero out extra memory at the end of
2283          * the H/W ring so look-ahead logic in Rx Burst bulk alloc function
2284          * reads extra memory as zeros.
2285          */
2286         for (i = 0; i < len; i++)
2287                 rxq->rx_ring[i] = zeroed_desc;
2288
2289         /*
2290          * initialize extra software ring entries. Space for these extra
2291          * entries is always allocated
2292          */
2293         memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
2294         for (i = rxq->nb_rx_desc; i < len; ++i)
2295                 rxq->sw_ring[i].mbuf = &rxq->fake_mbuf;
2296
2297         rxq->rx_nb_avail = 0;
2298         rxq->rx_next_avail = 0;
2299         rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
2300         rxq->rx_tail = 0;
2301         rxq->nb_rx_hold = 0;
2302         rxq->pkt_first_seg = NULL;
2303         rxq->pkt_last_seg = NULL;
2304 }
2305
2306 int __rte_cold
2307 txgbe_dev_rx_queue_setup(struct rte_eth_dev *dev,
2308                          uint16_t queue_idx,
2309                          uint16_t nb_desc,
2310                          unsigned int socket_id,
2311                          const struct rte_eth_rxconf *rx_conf,
2312                          struct rte_mempool *mp)
2313 {
2314         const struct rte_memzone *rz;
2315         struct txgbe_rx_queue *rxq;
2316         struct txgbe_hw     *hw;
2317         uint16_t len;
2318         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
2319         uint64_t offloads;
2320
2321         PMD_INIT_FUNC_TRACE();
2322         hw = TXGBE_DEV_HW(dev);
2323
2324         offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
2325
2326         /*
2327          * Validate number of receive descriptors.
2328          * It must not exceed hardware maximum, and must be multiple
2329          * of TXGBE_ALIGN.
2330          */
2331         if (nb_desc % TXGBE_RXD_ALIGN != 0 ||
2332                         nb_desc > TXGBE_RING_DESC_MAX ||
2333                         nb_desc < TXGBE_RING_DESC_MIN) {
2334                 return -EINVAL;
2335         }
2336
2337         /* Free memory prior to re-allocation if needed... */
2338         if (dev->data->rx_queues[queue_idx] != NULL) {
2339                 txgbe_rx_queue_release(dev->data->rx_queues[queue_idx]);
2340                 dev->data->rx_queues[queue_idx] = NULL;
2341         }
2342
2343         /* First allocate the rx queue data structure */
2344         rxq = rte_zmalloc_socket("ethdev RX queue",
2345                                  sizeof(struct txgbe_rx_queue),
2346                                  RTE_CACHE_LINE_SIZE, socket_id);
2347         if (rxq == NULL)
2348                 return -ENOMEM;
2349         rxq->mb_pool = mp;
2350         rxq->nb_rx_desc = nb_desc;
2351         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
2352         rxq->queue_id = queue_idx;
2353         rxq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
2354                 queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
2355         rxq->port_id = dev->data->port_id;
2356         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
2357                 rxq->crc_len = RTE_ETHER_CRC_LEN;
2358         else
2359                 rxq->crc_len = 0;
2360         rxq->drop_en = rx_conf->rx_drop_en;
2361         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
2362         rxq->offloads = offloads;
2363
2364         /*
2365          * The packet type in RX descriptor is different for different NICs.
2366          * So set different masks for different NICs.
2367          */
2368         rxq->pkt_type_mask = TXGBE_PTID_MASK;
2369
2370         /*
2371          * Allocate RX ring hardware descriptors. A memzone large enough to
2372          * handle the maximum ring size is allocated in order to allow for
2373          * resizing in later calls to the queue setup function.
2374          */
2375         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
2376                                       RX_RING_SZ, TXGBE_ALIGN, socket_id);
2377         if (rz == NULL) {
2378                 txgbe_rx_queue_release(rxq);
2379                 return -ENOMEM;
2380         }
2381
2382         /*
2383          * Zero init all the descriptors in the ring.
2384          */
2385         memset(rz->addr, 0, RX_RING_SZ);
2386
2387         /*
2388          * Modified to setup VFRDT for Virtual Function
2389          */
2390         if (hw->mac.type == txgbe_mac_raptor_vf) {
2391                 rxq->rdt_reg_addr =
2392                         TXGBE_REG_ADDR(hw, TXGBE_RXWP(queue_idx));
2393                 rxq->rdh_reg_addr =
2394                         TXGBE_REG_ADDR(hw, TXGBE_RXRP(queue_idx));
2395         } else {
2396                 rxq->rdt_reg_addr =
2397                         TXGBE_REG_ADDR(hw, TXGBE_RXWP(rxq->reg_idx));
2398                 rxq->rdh_reg_addr =
2399                         TXGBE_REG_ADDR(hw, TXGBE_RXRP(rxq->reg_idx));
2400         }
2401
2402         rxq->rx_ring_phys_addr = TMZ_PADDR(rz);
2403         rxq->rx_ring = (struct txgbe_rx_desc *)TMZ_VADDR(rz);
2404
2405         /*
2406          * Certain constraints must be met in order to use the bulk buffer
2407          * allocation Rx burst function. If any of Rx queues doesn't meet them
2408          * the feature should be disabled for the whole port.
2409          */
2410         if (check_rx_burst_bulk_alloc_preconditions(rxq)) {
2411                 PMD_INIT_LOG(DEBUG, "queue[%d] doesn't meet Rx Bulk Alloc "
2412                                     "preconditions - canceling the feature for "
2413                                     "the whole port[%d]",
2414                              rxq->queue_id, rxq->port_id);
2415                 adapter->rx_bulk_alloc_allowed = false;
2416         }
2417
2418         /*
2419          * Allocate software ring. Allow for space at the end of the
2420          * S/W ring to make sure look-ahead logic in bulk alloc Rx burst
2421          * function does not access an invalid memory region.
2422          */
2423         len = nb_desc;
2424         if (adapter->rx_bulk_alloc_allowed)
2425                 len += RTE_PMD_TXGBE_RX_MAX_BURST;
2426
2427         rxq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
2428                                           sizeof(struct txgbe_rx_entry) * len,
2429                                           RTE_CACHE_LINE_SIZE, socket_id);
2430         if (!rxq->sw_ring) {
2431                 txgbe_rx_queue_release(rxq);
2432                 return -ENOMEM;
2433         }
2434
2435         /*
2436          * Always allocate even if it's not going to be needed in order to
2437          * simplify the code.
2438          *
2439          * This ring is used in LRO and Scattered Rx cases and Scattered Rx may
2440          * be requested in txgbe_dev_rx_init(), which is called later from
2441          * dev_start() flow.
2442          */
2443         rxq->sw_sc_ring =
2444                 rte_zmalloc_socket("rxq->sw_sc_ring",
2445                                   sizeof(struct txgbe_scattered_rx_entry) * len,
2446                                   RTE_CACHE_LINE_SIZE, socket_id);
2447         if (!rxq->sw_sc_ring) {
2448                 txgbe_rx_queue_release(rxq);
2449                 return -ENOMEM;
2450         }
2451
2452         PMD_INIT_LOG(DEBUG, "sw_ring=%p sw_sc_ring=%p hw_ring=%p "
2453                             "dma_addr=0x%" PRIx64,
2454                      rxq->sw_ring, rxq->sw_sc_ring, rxq->rx_ring,
2455                      rxq->rx_ring_phys_addr);
2456
2457         dev->data->rx_queues[queue_idx] = rxq;
2458
2459         txgbe_reset_rx_queue(adapter, rxq);
2460
2461         return 0;
2462 }
2463
2464 void __rte_cold
2465 txgbe_dev_clear_queues(struct rte_eth_dev *dev)
2466 {
2467         unsigned int i;
2468         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
2469
2470         PMD_INIT_FUNC_TRACE();
2471
2472         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2473                 struct txgbe_tx_queue *txq = dev->data->tx_queues[i];
2474
2475                 if (txq != NULL) {
2476                         txq->ops->release_mbufs(txq);
2477                         txq->ops->reset(txq);
2478                 }
2479         }
2480
2481         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2482                 struct txgbe_rx_queue *rxq = dev->data->rx_queues[i];
2483
2484                 if (rxq != NULL) {
2485                         txgbe_rx_queue_release_mbufs(rxq);
2486                         txgbe_reset_rx_queue(adapter, rxq);
2487                 }
2488         }
2489 }
2490
2491 void
2492 txgbe_dev_free_queues(struct rte_eth_dev *dev)
2493 {
2494         unsigned int i;
2495
2496         PMD_INIT_FUNC_TRACE();
2497
2498         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2499                 txgbe_dev_rx_queue_release(dev->data->rx_queues[i]);
2500                 dev->data->rx_queues[i] = NULL;
2501         }
2502         dev->data->nb_rx_queues = 0;
2503
2504         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2505                 txgbe_dev_tx_queue_release(dev->data->tx_queues[i]);
2506                 dev->data->tx_queues[i] = NULL;
2507         }
2508         dev->data->nb_tx_queues = 0;
2509 }
2510
2511 static int __rte_cold
2512 txgbe_alloc_rx_queue_mbufs(struct txgbe_rx_queue *rxq)
2513 {
2514         struct txgbe_rx_entry *rxe = rxq->sw_ring;
2515         uint64_t dma_addr;
2516         unsigned int i;
2517
2518         /* Initialize software ring entries */
2519         for (i = 0; i < rxq->nb_rx_desc; i++) {
2520                 volatile struct txgbe_rx_desc *rxd;
2521                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
2522
2523                 if (mbuf == NULL) {
2524                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed queue_id=%u",
2525                                      (unsigned int)rxq->queue_id);
2526                         return -ENOMEM;
2527                 }
2528
2529                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
2530                 mbuf->port = rxq->port_id;
2531
2532                 dma_addr =
2533                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
2534                 rxd = &rxq->rx_ring[i];
2535                 TXGBE_RXD_HDRADDR(rxd, 0);
2536                 TXGBE_RXD_PKTADDR(rxd, dma_addr);
2537                 rxe[i].mbuf = mbuf;
2538         }
2539
2540         return 0;
2541 }
2542
2543 /**
2544  * txgbe_get_rscctl_maxdesc
2545  *
2546  * @pool Memory pool of the Rx queue
2547  */
2548 static inline uint32_t
2549 txgbe_get_rscctl_maxdesc(struct rte_mempool *pool)
2550 {
2551         struct rte_pktmbuf_pool_private *mp_priv = rte_mempool_get_priv(pool);
2552
2553         uint16_t maxdesc =
2554                 RTE_IPV4_MAX_PKT_LEN /
2555                         (mp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM);
2556
2557         if (maxdesc >= 16)
2558                 return TXGBE_RXCFG_RSCMAX_16;
2559         else if (maxdesc >= 8)
2560                 return TXGBE_RXCFG_RSCMAX_8;
2561         else if (maxdesc >= 4)
2562                 return TXGBE_RXCFG_RSCMAX_4;
2563         else
2564                 return TXGBE_RXCFG_RSCMAX_1;
2565 }
2566
2567 /**
2568  * txgbe_set_rsc - configure RSC related port HW registers
2569  *
2570  * Configures the port's RSC related registers.
2571  *
2572  * @dev port handle
2573  *
2574  * Returns 0 in case of success or a non-zero error code
2575  */
2576 static int
2577 txgbe_set_rsc(struct rte_eth_dev *dev)
2578 {
2579         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
2580         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2581         struct rte_eth_dev_info dev_info = { 0 };
2582         bool rsc_capable = false;
2583         uint16_t i;
2584         uint32_t rdrxctl;
2585         uint32_t rfctl;
2586
2587         /* Sanity check */
2588         dev->dev_ops->dev_infos_get(dev, &dev_info);
2589         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO)
2590                 rsc_capable = true;
2591
2592         if (!rsc_capable && (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO)) {
2593                 PMD_INIT_LOG(CRIT, "LRO is requested on HW that doesn't "
2594                                    "support it");
2595                 return -EINVAL;
2596         }
2597
2598         /* RSC global configuration */
2599
2600         if ((rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC) &&
2601              (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO)) {
2602                 PMD_INIT_LOG(CRIT, "LRO can't be enabled when HW CRC "
2603                                     "is disabled");
2604                 return -EINVAL;
2605         }
2606
2607         rfctl = rd32(hw, TXGBE_PSRCTL);
2608         if (rsc_capable && (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO))
2609                 rfctl &= ~TXGBE_PSRCTL_RSCDIA;
2610         else
2611                 rfctl |= TXGBE_PSRCTL_RSCDIA;
2612         wr32(hw, TXGBE_PSRCTL, rfctl);
2613
2614         /* If LRO hasn't been requested - we are done here. */
2615         if (!(rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO))
2616                 return 0;
2617
2618         /* Set PSRCTL.RSCACK bit */
2619         rdrxctl = rd32(hw, TXGBE_PSRCTL);
2620         rdrxctl |= TXGBE_PSRCTL_RSCACK;
2621         wr32(hw, TXGBE_PSRCTL, rdrxctl);
2622
2623         /* Per-queue RSC configuration */
2624         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2625                 struct txgbe_rx_queue *rxq = dev->data->rx_queues[i];
2626                 uint32_t srrctl =
2627                         rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
2628                 uint32_t psrtype =
2629                         rd32(hw, TXGBE_POOLRSS(rxq->reg_idx));
2630                 uint32_t eitr =
2631                         rd32(hw, TXGBE_ITR(rxq->reg_idx));
2632
2633                 /*
2634                  * txgbe PMD doesn't support header-split at the moment.
2635                  */
2636                 srrctl &= ~TXGBE_RXCFG_HDRLEN_MASK;
2637                 srrctl |= TXGBE_RXCFG_HDRLEN(128);
2638
2639                 /*
2640                  * TODO: Consider setting the Receive Descriptor Minimum
2641                  * Threshold Size for an RSC case. This is not an obviously
2642                  * beneficiary option but the one worth considering...
2643                  */
2644
2645                 srrctl |= TXGBE_RXCFG_RSCENA;
2646                 srrctl &= ~TXGBE_RXCFG_RSCMAX_MASK;
2647                 srrctl |= txgbe_get_rscctl_maxdesc(rxq->mb_pool);
2648                 psrtype |= TXGBE_POOLRSS_L4HDR;
2649
2650                 /*
2651                  * RSC: Set ITR interval corresponding to 2K ints/s.
2652                  *
2653                  * Full-sized RSC aggregations for a 10Gb/s link will
2654                  * arrive at about 20K aggregation/s rate.
2655                  *
2656                  * 2K inst/s rate will make only 10% of the
2657                  * aggregations to be closed due to the interrupt timer
2658                  * expiration for a streaming at wire-speed case.
2659                  *
2660                  * For a sparse streaming case this setting will yield
2661                  * at most 500us latency for a single RSC aggregation.
2662                  */
2663                 eitr &= ~TXGBE_ITR_IVAL_MASK;
2664                 eitr |= TXGBE_ITR_IVAL_10G(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT);
2665                 eitr |= TXGBE_ITR_WRDSA;
2666
2667                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), srrctl);
2668                 wr32(hw, TXGBE_POOLRSS(rxq->reg_idx), psrtype);
2669                 wr32(hw, TXGBE_ITR(rxq->reg_idx), eitr);
2670
2671                 /*
2672                  * RSC requires the mapping of the queue to the
2673                  * interrupt vector.
2674                  */
2675                 txgbe_set_ivar_map(hw, 0, rxq->reg_idx, i);
2676         }
2677
2678         dev->data->lro = 1;
2679
2680         PMD_INIT_LOG(DEBUG, "enabling LRO mode");
2681
2682         return 0;
2683 }
2684
2685 void __rte_cold
2686 txgbe_set_rx_function(struct rte_eth_dev *dev)
2687 {
2688         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
2689
2690         /*
2691          * Initialize the appropriate LRO callback.
2692          *
2693          * If all queues satisfy the bulk allocation preconditions
2694          * (adapter->rx_bulk_alloc_allowed is TRUE) then we may use
2695          * bulk allocation. Otherwise use a single allocation version.
2696          */
2697         if (dev->data->lro) {
2698                 if (adapter->rx_bulk_alloc_allowed) {
2699                         PMD_INIT_LOG(DEBUG, "LRO is requested. Using a bulk "
2700                                            "allocation version");
2701                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_bulk_alloc;
2702                 } else {
2703                         PMD_INIT_LOG(DEBUG, "LRO is requested. Using a single "
2704                                            "allocation version");
2705                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_single_alloc;
2706                 }
2707         } else if (dev->data->scattered_rx) {
2708                 /*
2709                  * Set the non-LRO scattered callback: there are bulk and
2710                  * single allocation versions.
2711                  */
2712                 if (adapter->rx_bulk_alloc_allowed) {
2713                         PMD_INIT_LOG(DEBUG, "Using a Scattered with bulk "
2714                                            "allocation callback (port=%d).",
2715                                      dev->data->port_id);
2716                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_bulk_alloc;
2717                 } else {
2718                         PMD_INIT_LOG(DEBUG, "Using Regular (non-vector, "
2719                                             "single allocation) "
2720                                             "Scattered Rx callback "
2721                                             "(port=%d).",
2722                                      dev->data->port_id);
2723
2724                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_single_alloc;
2725                 }
2726         /*
2727          * Below we set "simple" callbacks according to port/queues parameters.
2728          * If parameters allow we are going to choose between the following
2729          * callbacks:
2730          *    - Bulk Allocation
2731          *    - Single buffer allocation (the simplest one)
2732          */
2733         } else if (adapter->rx_bulk_alloc_allowed) {
2734                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
2735                                     "satisfied. Rx Burst Bulk Alloc function "
2736                                     "will be used on port=%d.",
2737                              dev->data->port_id);
2738
2739                 dev->rx_pkt_burst = txgbe_recv_pkts_bulk_alloc;
2740         } else {
2741                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are not "
2742                                     "satisfied, or Scattered Rx is requested "
2743                                     "(port=%d).",
2744                              dev->data->port_id);
2745
2746                 dev->rx_pkt_burst = txgbe_recv_pkts;
2747         }
2748 }
2749
2750 /*
2751  * Initializes Receive Unit.
2752  */
2753 int __rte_cold
2754 txgbe_dev_rx_init(struct rte_eth_dev *dev)
2755 {
2756         struct txgbe_hw *hw;
2757         struct txgbe_rx_queue *rxq;
2758         uint64_t bus_addr;
2759         uint32_t fctrl;
2760         uint32_t hlreg0;
2761         uint32_t srrctl;
2762         uint32_t rdrxctl;
2763         uint32_t rxcsum;
2764         uint16_t buf_size;
2765         uint16_t i;
2766         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
2767         int rc;
2768
2769         PMD_INIT_FUNC_TRACE();
2770         hw = TXGBE_DEV_HW(dev);
2771
2772         /*
2773          * Make sure receives are disabled while setting
2774          * up the RX context (registers, descriptor rings, etc.).
2775          */
2776         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, 0);
2777         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, 0);
2778
2779         /* Enable receipt of broadcasted frames */
2780         fctrl = rd32(hw, TXGBE_PSRCTL);
2781         fctrl |= TXGBE_PSRCTL_BCA;
2782         wr32(hw, TXGBE_PSRCTL, fctrl);
2783
2784         /*
2785          * Configure CRC stripping, if any.
2786          */
2787         hlreg0 = rd32(hw, TXGBE_SECRXCTL);
2788         if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
2789                 hlreg0 &= ~TXGBE_SECRXCTL_CRCSTRIP;
2790         else
2791                 hlreg0 |= TXGBE_SECRXCTL_CRCSTRIP;
2792         wr32(hw, TXGBE_SECRXCTL, hlreg0);
2793
2794         /*
2795          * Configure jumbo frame support, if any.
2796          */
2797         if (rx_conf->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
2798                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
2799                         TXGBE_FRMSZ_MAX(rx_conf->max_rx_pkt_len));
2800         } else {
2801                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
2802                         TXGBE_FRMSZ_MAX(TXGBE_FRAME_SIZE_DFT));
2803         }
2804
2805         /*
2806          * If loopback mode is configured, set LPBK bit.
2807          */
2808         hlreg0 = rd32(hw, TXGBE_PSRCTL);
2809         if (hw->mac.type == txgbe_mac_raptor &&
2810             dev->data->dev_conf.lpbk_mode)
2811                 hlreg0 |= TXGBE_PSRCTL_LBENA;
2812         else
2813                 hlreg0 &= ~TXGBE_PSRCTL_LBENA;
2814
2815         wr32(hw, TXGBE_PSRCTL, hlreg0);
2816
2817         /*
2818          * Assume no header split and no VLAN strip support
2819          * on any Rx queue first .
2820          */
2821         rx_conf->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
2822
2823         /* Setup RX queues */
2824         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2825                 rxq = dev->data->rx_queues[i];
2826
2827                 /*
2828                  * Reset crc_len in case it was changed after queue setup by a
2829                  * call to configure.
2830                  */
2831                 if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
2832                         rxq->crc_len = RTE_ETHER_CRC_LEN;
2833                 else
2834                         rxq->crc_len = 0;
2835
2836                 /* Setup the Base and Length of the Rx Descriptor Rings */
2837                 bus_addr = rxq->rx_ring_phys_addr;
2838                 wr32(hw, TXGBE_RXBAL(rxq->reg_idx),
2839                                 (uint32_t)(bus_addr & BIT_MASK32));
2840                 wr32(hw, TXGBE_RXBAH(rxq->reg_idx),
2841                                 (uint32_t)(bus_addr >> 32));
2842                 wr32(hw, TXGBE_RXRP(rxq->reg_idx), 0);
2843                 wr32(hw, TXGBE_RXWP(rxq->reg_idx), 0);
2844
2845                 srrctl = TXGBE_RXCFG_RNGLEN(rxq->nb_rx_desc);
2846
2847                 /* Set if packets are dropped when no descriptors available */
2848                 if (rxq->drop_en)
2849                         srrctl |= TXGBE_RXCFG_DROP;
2850
2851                 /*
2852                  * Configure the RX buffer size in the PKTLEN field of
2853                  * the RXCFG register of the queue.
2854                  * The value is in 1 KB resolution. Valid values can be from
2855                  * 1 KB to 16 KB.
2856                  */
2857                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
2858                         RTE_PKTMBUF_HEADROOM);
2859                 buf_size = ROUND_UP(buf_size, 0x1 << 10);
2860                 srrctl |= TXGBE_RXCFG_PKTLEN(buf_size);
2861
2862                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), srrctl);
2863
2864                 /* It adds dual VLAN length for supporting dual VLAN */
2865                 if (dev->data->dev_conf.rxmode.max_rx_pkt_len +
2866                                             2 * TXGBE_VLAN_TAG_SIZE > buf_size)
2867                         dev->data->scattered_rx = 1;
2868                 if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2869                         rx_conf->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
2870         }
2871
2872         if (rx_conf->offloads & DEV_RX_OFFLOAD_SCATTER)
2873                 dev->data->scattered_rx = 1;
2874
2875         /*
2876          * Setup the Checksum Register.
2877          * Disable Full-Packet Checksum which is mutually exclusive with RSS.
2878          * Enable IP/L4 checksum computation by hardware if requested to do so.
2879          */
2880         rxcsum = rd32(hw, TXGBE_PSRCTL);
2881         rxcsum |= TXGBE_PSRCTL_PCSD;
2882         if (rx_conf->offloads & DEV_RX_OFFLOAD_CHECKSUM)
2883                 rxcsum |= TXGBE_PSRCTL_L4CSUM;
2884         else
2885                 rxcsum &= ~TXGBE_PSRCTL_L4CSUM;
2886
2887         wr32(hw, TXGBE_PSRCTL, rxcsum);
2888
2889         if (hw->mac.type == txgbe_mac_raptor) {
2890                 rdrxctl = rd32(hw, TXGBE_SECRXCTL);
2891                 if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
2892                         rdrxctl &= ~TXGBE_SECRXCTL_CRCSTRIP;
2893                 else
2894                         rdrxctl |= TXGBE_SECRXCTL_CRCSTRIP;
2895                 wr32(hw, TXGBE_SECRXCTL, rdrxctl);
2896         }
2897
2898         rc = txgbe_set_rsc(dev);
2899         if (rc)
2900                 return rc;
2901
2902         txgbe_set_rx_function(dev);
2903
2904         return 0;
2905 }
2906
2907 /*
2908  * Initializes Transmit Unit.
2909  */
2910 void __rte_cold
2911 txgbe_dev_tx_init(struct rte_eth_dev *dev)
2912 {
2913         struct txgbe_hw     *hw;
2914         struct txgbe_tx_queue *txq;
2915         uint64_t bus_addr;
2916         uint16_t i;
2917
2918         PMD_INIT_FUNC_TRACE();
2919         hw = TXGBE_DEV_HW(dev);
2920
2921         /* Setup the Base and Length of the Tx Descriptor Rings */
2922         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2923                 txq = dev->data->tx_queues[i];
2924
2925                 bus_addr = txq->tx_ring_phys_addr;
2926                 wr32(hw, TXGBE_TXBAL(txq->reg_idx),
2927                                 (uint32_t)(bus_addr & BIT_MASK32));
2928                 wr32(hw, TXGBE_TXBAH(txq->reg_idx),
2929                                 (uint32_t)(bus_addr >> 32));
2930                 wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_BUFLEN_MASK,
2931                         TXGBE_TXCFG_BUFLEN(txq->nb_tx_desc));
2932                 /* Setup the HW Tx Head and TX Tail descriptor pointers */
2933                 wr32(hw, TXGBE_TXRP(txq->reg_idx), 0);
2934                 wr32(hw, TXGBE_TXWP(txq->reg_idx), 0);
2935         }
2936 }
2937
2938 /*
2939  * Set up link loopback mode Tx->Rx.
2940  */
2941 static inline void __rte_cold
2942 txgbe_setup_loopback_link_raptor(struct txgbe_hw *hw)
2943 {
2944         PMD_INIT_FUNC_TRACE();
2945
2946         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_LB, TXGBE_MACRXCFG_LB);
2947
2948         msec_delay(50);
2949 }
2950
2951 /*
2952  * Start Transmit and Receive Units.
2953  */
2954 int __rte_cold
2955 txgbe_dev_rxtx_start(struct rte_eth_dev *dev)
2956 {
2957         struct txgbe_hw     *hw;
2958         struct txgbe_tx_queue *txq;
2959         struct txgbe_rx_queue *rxq;
2960         uint32_t dmatxctl;
2961         uint32_t rxctrl;
2962         uint16_t i;
2963         int ret = 0;
2964
2965         PMD_INIT_FUNC_TRACE();
2966         hw = TXGBE_DEV_HW(dev);
2967
2968         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2969                 txq = dev->data->tx_queues[i];
2970                 /* Setup Transmit Threshold Registers */
2971                 wr32m(hw, TXGBE_TXCFG(txq->reg_idx),
2972                       TXGBE_TXCFG_HTHRESH_MASK |
2973                       TXGBE_TXCFG_WTHRESH_MASK,
2974                       TXGBE_TXCFG_HTHRESH(txq->hthresh) |
2975                       TXGBE_TXCFG_WTHRESH(txq->wthresh));
2976         }
2977
2978         dmatxctl = rd32(hw, TXGBE_DMATXCTRL);
2979         dmatxctl |= TXGBE_DMATXCTRL_ENA;
2980         wr32(hw, TXGBE_DMATXCTRL, dmatxctl);
2981
2982         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2983                 txq = dev->data->tx_queues[i];
2984                 if (!txq->tx_deferred_start) {
2985                         ret = txgbe_dev_tx_queue_start(dev, i);
2986                         if (ret < 0)
2987                                 return ret;
2988                 }
2989         }
2990
2991         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2992                 rxq = dev->data->rx_queues[i];
2993                 if (!rxq->rx_deferred_start) {
2994                         ret = txgbe_dev_rx_queue_start(dev, i);
2995                         if (ret < 0)
2996                                 return ret;
2997                 }
2998         }
2999
3000         /* Enable Receive engine */
3001         rxctrl = rd32(hw, TXGBE_PBRXCTL);
3002         rxctrl |= TXGBE_PBRXCTL_ENA;
3003         hw->mac.enable_rx_dma(hw, rxctrl);
3004
3005         /* If loopback mode is enabled, set up the link accordingly */
3006         if (hw->mac.type == txgbe_mac_raptor &&
3007             dev->data->dev_conf.lpbk_mode)
3008                 txgbe_setup_loopback_link_raptor(hw);
3009
3010         return 0;
3011 }
3012
3013 void
3014 txgbe_dev_save_rx_queue(struct txgbe_hw *hw, uint16_t rx_queue_id)
3015 {
3016         u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
3017         *(reg++) = rd32(hw, TXGBE_RXBAL(rx_queue_id));
3018         *(reg++) = rd32(hw, TXGBE_RXBAH(rx_queue_id));
3019         *(reg++) = rd32(hw, TXGBE_RXCFG(rx_queue_id));
3020 }
3021
3022 void
3023 txgbe_dev_store_rx_queue(struct txgbe_hw *hw, uint16_t rx_queue_id)
3024 {
3025         u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
3026         wr32(hw, TXGBE_RXBAL(rx_queue_id), *(reg++));
3027         wr32(hw, TXGBE_RXBAH(rx_queue_id), *(reg++));
3028         wr32(hw, TXGBE_RXCFG(rx_queue_id), *(reg++) & ~TXGBE_RXCFG_ENA);
3029 }
3030
3031 void
3032 txgbe_dev_save_tx_queue(struct txgbe_hw *hw, uint16_t tx_queue_id)
3033 {
3034         u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
3035         *(reg++) = rd32(hw, TXGBE_TXBAL(tx_queue_id));
3036         *(reg++) = rd32(hw, TXGBE_TXBAH(tx_queue_id));
3037         *(reg++) = rd32(hw, TXGBE_TXCFG(tx_queue_id));
3038 }
3039
3040 void
3041 txgbe_dev_store_tx_queue(struct txgbe_hw *hw, uint16_t tx_queue_id)
3042 {
3043         u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
3044         wr32(hw, TXGBE_TXBAL(tx_queue_id), *(reg++));
3045         wr32(hw, TXGBE_TXBAH(tx_queue_id), *(reg++));
3046         wr32(hw, TXGBE_TXCFG(tx_queue_id), *(reg++) & ~TXGBE_TXCFG_ENA);
3047 }
3048
3049 /*
3050  * Start Receive Units for specified queue.
3051  */
3052 int __rte_cold
3053 txgbe_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
3054 {
3055         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3056         struct txgbe_rx_queue *rxq;
3057         uint32_t rxdctl;
3058         int poll_ms;
3059
3060         PMD_INIT_FUNC_TRACE();
3061
3062         rxq = dev->data->rx_queues[rx_queue_id];
3063
3064         /* Allocate buffers for descriptor rings */
3065         if (txgbe_alloc_rx_queue_mbufs(rxq) != 0) {
3066                 PMD_INIT_LOG(ERR, "Could not alloc mbuf for queue:%d",
3067                              rx_queue_id);
3068                 return -1;
3069         }
3070         rxdctl = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
3071         rxdctl |= TXGBE_RXCFG_ENA;
3072         wr32(hw, TXGBE_RXCFG(rxq->reg_idx), rxdctl);
3073
3074         /* Wait until RX Enable ready */
3075         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
3076         do {
3077                 rte_delay_ms(1);
3078                 rxdctl = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
3079         } while (--poll_ms && !(rxdctl & TXGBE_RXCFG_ENA));
3080         if (!poll_ms)
3081                 PMD_INIT_LOG(ERR, "Could not enable Rx Queue %d", rx_queue_id);
3082         rte_wmb();
3083         wr32(hw, TXGBE_RXRP(rxq->reg_idx), 0);
3084         wr32(hw, TXGBE_RXWP(rxq->reg_idx), rxq->nb_rx_desc - 1);
3085         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
3086
3087         return 0;
3088 }
3089
3090 /*
3091  * Stop Receive Units for specified queue.
3092  */
3093 int __rte_cold
3094 txgbe_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
3095 {
3096         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3097         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
3098         struct txgbe_rx_queue *rxq;
3099         uint32_t rxdctl;
3100         int poll_ms;
3101
3102         PMD_INIT_FUNC_TRACE();
3103
3104         rxq = dev->data->rx_queues[rx_queue_id];
3105
3106         txgbe_dev_save_rx_queue(hw, rxq->reg_idx);
3107         wr32m(hw, TXGBE_RXCFG(rxq->reg_idx), TXGBE_RXCFG_ENA, 0);
3108
3109         /* Wait until RX Enable bit clear */
3110         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
3111         do {
3112                 rte_delay_ms(1);
3113                 rxdctl = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
3114         } while (--poll_ms && (rxdctl & TXGBE_RXCFG_ENA));
3115         if (!poll_ms)
3116                 PMD_INIT_LOG(ERR, "Could not disable Rx Queue %d", rx_queue_id);
3117
3118         rte_delay_us(RTE_TXGBE_WAIT_100_US);
3119         txgbe_dev_store_rx_queue(hw, rxq->reg_idx);
3120
3121         txgbe_rx_queue_release_mbufs(rxq);
3122         txgbe_reset_rx_queue(adapter, rxq);
3123         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
3124
3125         return 0;
3126 }
3127
3128 /*
3129  * Start Transmit Units for specified queue.
3130  */
3131 int __rte_cold
3132 txgbe_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
3133 {
3134         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3135         struct txgbe_tx_queue *txq;
3136         uint32_t txdctl;
3137         int poll_ms;
3138
3139         PMD_INIT_FUNC_TRACE();
3140
3141         txq = dev->data->tx_queues[tx_queue_id];
3142         wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_ENA, TXGBE_TXCFG_ENA);
3143
3144         /* Wait until TX Enable ready */
3145         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
3146         do {
3147                 rte_delay_ms(1);
3148                 txdctl = rd32(hw, TXGBE_TXCFG(txq->reg_idx));
3149         } while (--poll_ms && !(txdctl & TXGBE_TXCFG_ENA));
3150         if (!poll_ms)
3151                 PMD_INIT_LOG(ERR, "Could not enable "
3152                              "Tx Queue %d", tx_queue_id);
3153
3154         rte_wmb();
3155         wr32(hw, TXGBE_TXWP(txq->reg_idx), txq->tx_tail);
3156         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
3157
3158         return 0;
3159 }
3160
3161 /*
3162  * Stop Transmit Units for specified queue.
3163  */
3164 int __rte_cold
3165 txgbe_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
3166 {
3167         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3168         struct txgbe_tx_queue *txq;
3169         uint32_t txdctl;
3170         uint32_t txtdh, txtdt;
3171         int poll_ms;
3172
3173         PMD_INIT_FUNC_TRACE();
3174
3175         txq = dev->data->tx_queues[tx_queue_id];
3176
3177         /* Wait until TX queue is empty */
3178         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
3179         do {
3180                 rte_delay_us(RTE_TXGBE_WAIT_100_US);
3181                 txtdh = rd32(hw, TXGBE_TXRP(txq->reg_idx));
3182                 txtdt = rd32(hw, TXGBE_TXWP(txq->reg_idx));
3183         } while (--poll_ms && (txtdh != txtdt));
3184         if (!poll_ms)
3185                 PMD_INIT_LOG(ERR,
3186                         "Tx Queue %d is not empty when stopping.",
3187                         tx_queue_id);
3188
3189         txgbe_dev_save_tx_queue(hw, txq->reg_idx);
3190         wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_ENA, 0);
3191
3192         /* Wait until TX Enable bit clear */
3193         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
3194         do {
3195                 rte_delay_ms(1);
3196                 txdctl = rd32(hw, TXGBE_TXCFG(txq->reg_idx));
3197         } while (--poll_ms && (txdctl & TXGBE_TXCFG_ENA));
3198         if (!poll_ms)
3199                 PMD_INIT_LOG(ERR, "Could not disable Tx Queue %d",
3200                         tx_queue_id);
3201
3202         rte_delay_us(RTE_TXGBE_WAIT_100_US);
3203         txgbe_dev_store_tx_queue(hw, txq->reg_idx);
3204
3205         if (txq->ops != NULL) {
3206                 txq->ops->release_mbufs(txq);
3207                 txq->ops->reset(txq);
3208         }
3209         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
3210
3211         return 0;
3212 }
3213