net/txgbe: support VXLAN-GPE
[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_byteorder.h>
17 #include <rte_common.h>
18 #include <rte_cycles.h>
19 #include <rte_log.h>
20 #include <rte_debug.h>
21 #include <rte_ethdev.h>
22 #include <ethdev_driver.h>
23 #include <rte_security_driver.h>
24 #include <rte_memzone.h>
25 #include <rte_atomic.h>
26 #include <rte_mempool.h>
27 #include <rte_malloc.h>
28 #include <rte_mbuf.h>
29 #include <rte_ether.h>
30 #include <rte_prefetch.h>
31 #include <rte_udp.h>
32 #include <rte_tcp.h>
33 #include <rte_sctp.h>
34 #include <rte_string_fns.h>
35 #include <rte_errno.h>
36 #include <rte_ip.h>
37 #include <rte_net.h>
38
39 #include "txgbe_logs.h"
40 #include "base/txgbe.h"
41 #include "txgbe_ethdev.h"
42 #include "txgbe_rxtx.h"
43
44 #ifdef RTE_LIBRTE_IEEE1588
45 #define TXGBE_TX_IEEE1588_TMST PKT_TX_IEEE1588_TMST
46 #else
47 #define TXGBE_TX_IEEE1588_TMST 0
48 #endif
49
50 /* Bit Mask to indicate what bits required for building TX context */
51 static const u64 TXGBE_TX_OFFLOAD_MASK = (PKT_TX_IP_CKSUM |
52                 PKT_TX_OUTER_IPV6 |
53                 PKT_TX_OUTER_IPV4 |
54                 PKT_TX_IPV6 |
55                 PKT_TX_IPV4 |
56                 PKT_TX_VLAN_PKT |
57                 PKT_TX_L4_MASK |
58                 PKT_TX_TCP_SEG |
59                 PKT_TX_TUNNEL_MASK |
60                 PKT_TX_OUTER_IP_CKSUM |
61                 PKT_TX_OUTER_UDP_CKSUM |
62 #ifdef RTE_LIB_SECURITY
63                 PKT_TX_SEC_OFFLOAD |
64 #endif
65                 TXGBE_TX_IEEE1588_TMST);
66
67 #define TXGBE_TX_OFFLOAD_NOTSUP_MASK \
68                 (PKT_TX_OFFLOAD_MASK ^ TXGBE_TX_OFFLOAD_MASK)
69
70 /*
71  * Prefetch a cache line into all cache levels.
72  */
73 #define rte_txgbe_prefetch(p)   rte_prefetch0(p)
74
75 static int
76 txgbe_is_vf(struct rte_eth_dev *dev)
77 {
78         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
79
80         switch (hw->mac.type) {
81         case txgbe_mac_raptor_vf:
82                 return 1;
83         default:
84                 return 0;
85         }
86 }
87
88 /*********************************************************************
89  *
90  *  TX functions
91  *
92  **********************************************************************/
93
94 /*
95  * Check for descriptors with their DD bit set and free mbufs.
96  * Return the total number of buffers freed.
97  */
98 static __rte_always_inline int
99 txgbe_tx_free_bufs(struct txgbe_tx_queue *txq)
100 {
101         struct txgbe_tx_entry *txep;
102         uint32_t status;
103         int i, nb_free = 0;
104         struct rte_mbuf *m, *free[RTE_TXGBE_TX_MAX_FREE_BUF_SZ];
105
106         /* check DD bit on threshold descriptor */
107         status = txq->tx_ring[txq->tx_next_dd].dw3;
108         if (!(status & rte_cpu_to_le_32(TXGBE_TXD_DD))) {
109                 if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
110                         txgbe_set32_masked(txq->tdc_reg_addr,
111                                 TXGBE_TXCFG_FLUSH, TXGBE_TXCFG_FLUSH);
112                 return 0;
113         }
114
115         /*
116          * first buffer to free from S/W ring is at index
117          * tx_next_dd - (tx_free_thresh-1)
118          */
119         txep = &txq->sw_ring[txq->tx_next_dd - (txq->tx_free_thresh - 1)];
120         for (i = 0; i < txq->tx_free_thresh; ++i, ++txep) {
121                 /* free buffers one at a time */
122                 m = rte_pktmbuf_prefree_seg(txep->mbuf);
123                 txep->mbuf = NULL;
124
125                 if (unlikely(m == NULL))
126                         continue;
127
128                 if (nb_free >= RTE_TXGBE_TX_MAX_FREE_BUF_SZ ||
129                     (nb_free > 0 && m->pool != free[0]->pool)) {
130                         rte_mempool_put_bulk(free[0]->pool,
131                                              (void **)free, nb_free);
132                         nb_free = 0;
133                 }
134
135                 free[nb_free++] = m;
136         }
137
138         if (nb_free > 0)
139                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
140
141         /* buffers were freed, update counters */
142         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_free_thresh);
143         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_free_thresh);
144         if (txq->tx_next_dd >= txq->nb_tx_desc)
145                 txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
146
147         return txq->tx_free_thresh;
148 }
149
150 /* Populate 4 descriptors with data from 4 mbufs */
151 static inline void
152 tx4(volatile struct txgbe_tx_desc *txdp, struct rte_mbuf **pkts)
153 {
154         uint64_t buf_dma_addr;
155         uint32_t pkt_len;
156         int i;
157
158         for (i = 0; i < 4; ++i, ++txdp, ++pkts) {
159                 buf_dma_addr = rte_mbuf_data_iova(*pkts);
160                 pkt_len = (*pkts)->data_len;
161
162                 /* write data to descriptor */
163                 txdp->qw0 = rte_cpu_to_le_64(buf_dma_addr);
164                 txdp->dw2 = cpu_to_le32(TXGBE_TXD_FLAGS |
165                                         TXGBE_TXD_DATLEN(pkt_len));
166                 txdp->dw3 = cpu_to_le32(TXGBE_TXD_PAYLEN(pkt_len));
167
168                 rte_prefetch0(&(*pkts)->pool);
169         }
170 }
171
172 /* Populate 1 descriptor with data from 1 mbuf */
173 static inline void
174 tx1(volatile struct txgbe_tx_desc *txdp, struct rte_mbuf **pkts)
175 {
176         uint64_t buf_dma_addr;
177         uint32_t pkt_len;
178
179         buf_dma_addr = rte_mbuf_data_iova(*pkts);
180         pkt_len = (*pkts)->data_len;
181
182         /* write data to descriptor */
183         txdp->qw0 = cpu_to_le64(buf_dma_addr);
184         txdp->dw2 = cpu_to_le32(TXGBE_TXD_FLAGS |
185                                 TXGBE_TXD_DATLEN(pkt_len));
186         txdp->dw3 = cpu_to_le32(TXGBE_TXD_PAYLEN(pkt_len));
187
188         rte_prefetch0(&(*pkts)->pool);
189 }
190
191 /*
192  * Fill H/W descriptor ring with mbuf data.
193  * Copy mbuf pointers to the S/W ring.
194  */
195 static inline void
196 txgbe_tx_fill_hw_ring(struct txgbe_tx_queue *txq, struct rte_mbuf **pkts,
197                       uint16_t nb_pkts)
198 {
199         volatile struct txgbe_tx_desc *txdp = &txq->tx_ring[txq->tx_tail];
200         struct txgbe_tx_entry *txep = &txq->sw_ring[txq->tx_tail];
201         const int N_PER_LOOP = 4;
202         const int N_PER_LOOP_MASK = N_PER_LOOP - 1;
203         int mainpart, leftover;
204         int i, j;
205
206         /*
207          * Process most of the packets in chunks of N pkts.  Any
208          * leftover packets will get processed one at a time.
209          */
210         mainpart = (nb_pkts & ((uint32_t)~N_PER_LOOP_MASK));
211         leftover = (nb_pkts & ((uint32_t)N_PER_LOOP_MASK));
212         for (i = 0; i < mainpart; i += N_PER_LOOP) {
213                 /* Copy N mbuf pointers to the S/W ring */
214                 for (j = 0; j < N_PER_LOOP; ++j)
215                         (txep + i + j)->mbuf = *(pkts + i + j);
216                 tx4(txdp + i, pkts + i);
217         }
218
219         if (unlikely(leftover > 0)) {
220                 for (i = 0; i < leftover; ++i) {
221                         (txep + mainpart + i)->mbuf = *(pkts + mainpart + i);
222                         tx1(txdp + mainpart + i, pkts + mainpart + i);
223                 }
224         }
225 }
226
227 static inline uint16_t
228 tx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
229              uint16_t nb_pkts)
230 {
231         struct txgbe_tx_queue *txq = (struct txgbe_tx_queue *)tx_queue;
232         uint16_t n = 0;
233
234         /*
235          * Begin scanning the H/W ring for done descriptors when the
236          * number of available descriptors drops below tx_free_thresh.  For
237          * each done descriptor, free the associated buffer.
238          */
239         if (txq->nb_tx_free < txq->tx_free_thresh)
240                 txgbe_tx_free_bufs(txq);
241
242         /* Only use descriptors that are available */
243         nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
244         if (unlikely(nb_pkts == 0))
245                 return 0;
246
247         /* Use exactly nb_pkts descriptors */
248         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
249
250         /*
251          * At this point, we know there are enough descriptors in the
252          * ring to transmit all the packets.  This assumes that each
253          * mbuf contains a single segment, and that no new offloads
254          * are expected, which would require a new context descriptor.
255          */
256
257         /*
258          * See if we're going to wrap-around. If so, handle the top
259          * of the descriptor ring first, then do the bottom.  If not,
260          * the processing looks just like the "bottom" part anyway...
261          */
262         if ((txq->tx_tail + nb_pkts) > txq->nb_tx_desc) {
263                 n = (uint16_t)(txq->nb_tx_desc - txq->tx_tail);
264                 txgbe_tx_fill_hw_ring(txq, tx_pkts, n);
265                 txq->tx_tail = 0;
266         }
267
268         /* Fill H/W descriptor ring with mbuf data */
269         txgbe_tx_fill_hw_ring(txq, tx_pkts + n, (uint16_t)(nb_pkts - n));
270         txq->tx_tail = (uint16_t)(txq->tx_tail + (nb_pkts - n));
271
272         /*
273          * Check for wrap-around. This would only happen if we used
274          * up to the last descriptor in the ring, no more, no less.
275          */
276         if (txq->tx_tail >= txq->nb_tx_desc)
277                 txq->tx_tail = 0;
278
279         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
280                    (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
281                    (uint16_t)txq->tx_tail, (uint16_t)nb_pkts);
282
283         /* update tail pointer */
284         rte_wmb();
285         txgbe_set32_relaxed(txq->tdt_reg_addr, txq->tx_tail);
286
287         return nb_pkts;
288 }
289
290 uint16_t
291 txgbe_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
292                        uint16_t nb_pkts)
293 {
294         uint16_t nb_tx;
295
296         /* Try to transmit at least chunks of TX_MAX_BURST pkts */
297         if (likely(nb_pkts <= RTE_PMD_TXGBE_TX_MAX_BURST))
298                 return tx_xmit_pkts(tx_queue, tx_pkts, nb_pkts);
299
300         /* transmit more than the max burst, in chunks of TX_MAX_BURST */
301         nb_tx = 0;
302         while (nb_pkts) {
303                 uint16_t ret, n;
304
305                 n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_TXGBE_TX_MAX_BURST);
306                 ret = tx_xmit_pkts(tx_queue, &tx_pkts[nb_tx], n);
307                 nb_tx = (uint16_t)(nb_tx + ret);
308                 nb_pkts = (uint16_t)(nb_pkts - ret);
309                 if (ret < n)
310                         break;
311         }
312
313         return nb_tx;
314 }
315
316 static inline void
317 txgbe_set_xmit_ctx(struct txgbe_tx_queue *txq,
318                 volatile struct txgbe_tx_ctx_desc *ctx_txd,
319                 uint64_t ol_flags, union txgbe_tx_offload tx_offload,
320                 __rte_unused uint64_t *mdata)
321 {
322         union txgbe_tx_offload tx_offload_mask;
323         uint32_t type_tucmd_mlhl;
324         uint32_t mss_l4len_idx;
325         uint32_t ctx_idx;
326         uint32_t vlan_macip_lens;
327         uint32_t tunnel_seed;
328
329         ctx_idx = txq->ctx_curr;
330         tx_offload_mask.data[0] = 0;
331         tx_offload_mask.data[1] = 0;
332
333         /* Specify which HW CTX to upload. */
334         mss_l4len_idx = TXGBE_TXD_IDX(ctx_idx);
335         type_tucmd_mlhl = TXGBE_TXD_CTXT;
336
337         tx_offload_mask.ptid |= ~0;
338         type_tucmd_mlhl |= TXGBE_TXD_PTID(tx_offload.ptid);
339
340         /* check if TCP segmentation required for this packet */
341         if (ol_flags & PKT_TX_TCP_SEG) {
342                 tx_offload_mask.l2_len |= ~0;
343                 tx_offload_mask.l3_len |= ~0;
344                 tx_offload_mask.l4_len |= ~0;
345                 tx_offload_mask.tso_segsz |= ~0;
346                 mss_l4len_idx |= TXGBE_TXD_MSS(tx_offload.tso_segsz);
347                 mss_l4len_idx |= TXGBE_TXD_L4LEN(tx_offload.l4_len);
348         } else { /* no TSO, check if hardware checksum is needed */
349                 if (ol_flags & PKT_TX_IP_CKSUM) {
350                         tx_offload_mask.l2_len |= ~0;
351                         tx_offload_mask.l3_len |= ~0;
352                 }
353
354                 switch (ol_flags & PKT_TX_L4_MASK) {
355                 case PKT_TX_UDP_CKSUM:
356                         mss_l4len_idx |=
357                                 TXGBE_TXD_L4LEN(sizeof(struct rte_udp_hdr));
358                         tx_offload_mask.l2_len |= ~0;
359                         tx_offload_mask.l3_len |= ~0;
360                         break;
361                 case PKT_TX_TCP_CKSUM:
362                         mss_l4len_idx |=
363                                 TXGBE_TXD_L4LEN(sizeof(struct rte_tcp_hdr));
364                         tx_offload_mask.l2_len |= ~0;
365                         tx_offload_mask.l3_len |= ~0;
366                         break;
367                 case PKT_TX_SCTP_CKSUM:
368                         mss_l4len_idx |=
369                                 TXGBE_TXD_L4LEN(sizeof(struct rte_sctp_hdr));
370                         tx_offload_mask.l2_len |= ~0;
371                         tx_offload_mask.l3_len |= ~0;
372                         break;
373                 default:
374                         break;
375                 }
376         }
377
378         vlan_macip_lens = TXGBE_TXD_IPLEN(tx_offload.l3_len >> 1);
379
380         if (ol_flags & PKT_TX_TUNNEL_MASK) {
381                 tx_offload_mask.outer_tun_len |= ~0;
382                 tx_offload_mask.outer_l2_len |= ~0;
383                 tx_offload_mask.outer_l3_len |= ~0;
384                 tx_offload_mask.l2_len |= ~0;
385                 tunnel_seed = TXGBE_TXD_ETUNLEN(tx_offload.outer_tun_len >> 1);
386                 tunnel_seed |= TXGBE_TXD_EIPLEN(tx_offload.outer_l3_len >> 2);
387
388                 switch (ol_flags & PKT_TX_TUNNEL_MASK) {
389                 case PKT_TX_TUNNEL_IPIP:
390                         /* for non UDP / GRE tunneling, set to 0b */
391                         break;
392                 case PKT_TX_TUNNEL_VXLAN:
393                 case PKT_TX_TUNNEL_VXLAN_GPE:
394                 case PKT_TX_TUNNEL_GENEVE:
395                         tunnel_seed |= TXGBE_TXD_ETYPE_UDP;
396                         break;
397                 case PKT_TX_TUNNEL_GRE:
398                         tunnel_seed |= TXGBE_TXD_ETYPE_GRE;
399                         break;
400                 default:
401                         PMD_TX_LOG(ERR, "Tunnel type not supported");
402                         return;
403                 }
404                 vlan_macip_lens |= TXGBE_TXD_MACLEN(tx_offload.outer_l2_len);
405         } else {
406                 tunnel_seed = 0;
407                 vlan_macip_lens |= TXGBE_TXD_MACLEN(tx_offload.l2_len);
408         }
409
410         if (ol_flags & PKT_TX_VLAN_PKT) {
411                 tx_offload_mask.vlan_tci |= ~0;
412                 vlan_macip_lens |= TXGBE_TXD_VLAN(tx_offload.vlan_tci);
413         }
414
415 #ifdef RTE_LIB_SECURITY
416         if (ol_flags & PKT_TX_SEC_OFFLOAD) {
417                 union txgbe_crypto_tx_desc_md *md =
418                                 (union txgbe_crypto_tx_desc_md *)mdata;
419                 tunnel_seed |= TXGBE_TXD_IPSEC_SAIDX(md->sa_idx);
420                 type_tucmd_mlhl |= md->enc ?
421                         (TXGBE_TXD_IPSEC_ESP | TXGBE_TXD_IPSEC_ESPENC) : 0;
422                 type_tucmd_mlhl |= TXGBE_TXD_IPSEC_ESPLEN(md->pad_len);
423                 tx_offload_mask.sa_idx |= ~0;
424                 tx_offload_mask.sec_pad_len |= ~0;
425         }
426 #endif
427
428         txq->ctx_cache[ctx_idx].flags = ol_flags;
429         txq->ctx_cache[ctx_idx].tx_offload.data[0] =
430                 tx_offload_mask.data[0] & tx_offload.data[0];
431         txq->ctx_cache[ctx_idx].tx_offload.data[1] =
432                 tx_offload_mask.data[1] & tx_offload.data[1];
433         txq->ctx_cache[ctx_idx].tx_offload_mask = tx_offload_mask;
434
435         ctx_txd->dw0 = rte_cpu_to_le_32(vlan_macip_lens);
436         ctx_txd->dw1 = rte_cpu_to_le_32(tunnel_seed);
437         ctx_txd->dw2 = rte_cpu_to_le_32(type_tucmd_mlhl);
438         ctx_txd->dw3 = rte_cpu_to_le_32(mss_l4len_idx);
439 }
440
441 /*
442  * Check which hardware context can be used. Use the existing match
443  * or create a new context descriptor.
444  */
445 static inline uint32_t
446 what_ctx_update(struct txgbe_tx_queue *txq, uint64_t flags,
447                    union txgbe_tx_offload tx_offload)
448 {
449         /* If match with the current used context */
450         if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
451                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
452                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
453                      & tx_offload.data[0])) &&
454                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
455                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
456                      & tx_offload.data[1]))))
457                 return txq->ctx_curr;
458
459         /* What if match with the next context  */
460         txq->ctx_curr ^= 1;
461         if (likely(txq->ctx_cache[txq->ctx_curr].flags == flags &&
462                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[0] ==
463                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[0]
464                      & tx_offload.data[0])) &&
465                    (txq->ctx_cache[txq->ctx_curr].tx_offload.data[1] ==
466                     (txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data[1]
467                      & tx_offload.data[1]))))
468                 return txq->ctx_curr;
469
470         /* Mismatch, use the previous context */
471         return TXGBE_CTX_NUM;
472 }
473
474 static inline uint32_t
475 tx_desc_cksum_flags_to_olinfo(uint64_t ol_flags)
476 {
477         uint32_t tmp = 0;
478
479         if ((ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM) {
480                 tmp |= TXGBE_TXD_CC;
481                 tmp |= TXGBE_TXD_L4CS;
482         }
483         if (ol_flags & PKT_TX_IP_CKSUM) {
484                 tmp |= TXGBE_TXD_CC;
485                 tmp |= TXGBE_TXD_IPCS;
486         }
487         if (ol_flags & PKT_TX_OUTER_IP_CKSUM) {
488                 tmp |= TXGBE_TXD_CC;
489                 tmp |= TXGBE_TXD_EIPCS;
490         }
491         if (ol_flags & PKT_TX_TCP_SEG) {
492                 tmp |= TXGBE_TXD_CC;
493                 /* implies IPv4 cksum */
494                 if (ol_flags & PKT_TX_IPV4)
495                         tmp |= TXGBE_TXD_IPCS;
496                 tmp |= TXGBE_TXD_L4CS;
497         }
498         if (ol_flags & PKT_TX_VLAN_PKT)
499                 tmp |= TXGBE_TXD_CC;
500
501         return tmp;
502 }
503
504 static inline uint32_t
505 tx_desc_ol_flags_to_cmdtype(uint64_t ol_flags)
506 {
507         uint32_t cmdtype = 0;
508
509         if (ol_flags & PKT_TX_VLAN_PKT)
510                 cmdtype |= TXGBE_TXD_VLE;
511         if (ol_flags & PKT_TX_TCP_SEG)
512                 cmdtype |= TXGBE_TXD_TSE;
513         if (ol_flags & PKT_TX_MACSEC)
514                 cmdtype |= TXGBE_TXD_LINKSEC;
515         return cmdtype;
516 }
517
518 static inline uint8_t
519 tx_desc_ol_flags_to_ptid(uint64_t oflags, uint32_t ptype)
520 {
521         bool tun;
522
523         if (ptype)
524                 return txgbe_encode_ptype(ptype);
525
526         /* Only support flags in TXGBE_TX_OFFLOAD_MASK */
527         tun = !!(oflags & PKT_TX_TUNNEL_MASK);
528
529         /* L2 level */
530         ptype = RTE_PTYPE_L2_ETHER;
531         if (oflags & PKT_TX_VLAN)
532                 ptype |= RTE_PTYPE_L2_ETHER_VLAN;
533
534         /* L3 level */
535         if (oflags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IP_CKSUM))
536                 ptype |= RTE_PTYPE_L3_IPV4;
537         else if (oflags & (PKT_TX_OUTER_IPV6))
538                 ptype |= RTE_PTYPE_L3_IPV6;
539
540         if (oflags & (PKT_TX_IPV4 | PKT_TX_IP_CKSUM))
541                 ptype |= (tun ? RTE_PTYPE_INNER_L3_IPV4 : RTE_PTYPE_L3_IPV4);
542         else if (oflags & (PKT_TX_IPV6))
543                 ptype |= (tun ? RTE_PTYPE_INNER_L3_IPV6 : RTE_PTYPE_L3_IPV6);
544
545         /* L4 level */
546         switch (oflags & (PKT_TX_L4_MASK)) {
547         case PKT_TX_TCP_CKSUM:
548                 ptype |= (tun ? RTE_PTYPE_INNER_L4_TCP : RTE_PTYPE_L4_TCP);
549                 break;
550         case PKT_TX_UDP_CKSUM:
551                 ptype |= (tun ? RTE_PTYPE_INNER_L4_UDP : RTE_PTYPE_L4_UDP);
552                 break;
553         case PKT_TX_SCTP_CKSUM:
554                 ptype |= (tun ? RTE_PTYPE_INNER_L4_SCTP : RTE_PTYPE_L4_SCTP);
555                 break;
556         }
557
558         if (oflags & PKT_TX_TCP_SEG)
559                 ptype |= (tun ? RTE_PTYPE_INNER_L4_TCP : RTE_PTYPE_L4_TCP);
560
561         /* Tunnel */
562         switch (oflags & PKT_TX_TUNNEL_MASK) {
563         case PKT_TX_TUNNEL_VXLAN:
564                 ptype |= RTE_PTYPE_L2_ETHER |
565                          RTE_PTYPE_L3_IPV4 |
566                          RTE_PTYPE_TUNNEL_VXLAN;
567                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
568                 break;
569         case PKT_TX_TUNNEL_GRE:
570                 ptype |= RTE_PTYPE_L2_ETHER |
571                          RTE_PTYPE_L3_IPV4 |
572                          RTE_PTYPE_TUNNEL_GRE;
573                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
574                 break;
575         case PKT_TX_TUNNEL_GENEVE:
576                 ptype |= RTE_PTYPE_L2_ETHER |
577                          RTE_PTYPE_L3_IPV4 |
578                          RTE_PTYPE_TUNNEL_GENEVE;
579                 ptype |= RTE_PTYPE_INNER_L2_ETHER;
580                 break;
581         case PKT_TX_TUNNEL_VXLAN_GPE:
582                 ptype |= RTE_PTYPE_L2_ETHER |
583                          RTE_PTYPE_L3_IPV4 |
584                          RTE_PTYPE_TUNNEL_VXLAN_GPE;
585                 break;
586         case PKT_TX_TUNNEL_IPIP:
587         case PKT_TX_TUNNEL_IP:
588                 ptype |= RTE_PTYPE_L2_ETHER |
589                          RTE_PTYPE_L3_IPV4 |
590                          RTE_PTYPE_TUNNEL_IP;
591                 break;
592         }
593
594         return txgbe_encode_ptype(ptype);
595 }
596
597 #ifndef DEFAULT_TX_FREE_THRESH
598 #define DEFAULT_TX_FREE_THRESH 32
599 #endif
600
601 /* Reset transmit descriptors after they have been used */
602 static inline int
603 txgbe_xmit_cleanup(struct txgbe_tx_queue *txq)
604 {
605         struct txgbe_tx_entry *sw_ring = txq->sw_ring;
606         volatile struct txgbe_tx_desc *txr = txq->tx_ring;
607         uint16_t last_desc_cleaned = txq->last_desc_cleaned;
608         uint16_t nb_tx_desc = txq->nb_tx_desc;
609         uint16_t desc_to_clean_to;
610         uint16_t nb_tx_to_clean;
611         uint32_t status;
612
613         /* Determine the last descriptor needing to be cleaned */
614         desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_free_thresh);
615         if (desc_to_clean_to >= nb_tx_desc)
616                 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
617
618         /* Check to make sure the last descriptor to clean is done */
619         desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
620         status = txr[desc_to_clean_to].dw3;
621         if (!(status & rte_cpu_to_le_32(TXGBE_TXD_DD))) {
622                 PMD_TX_FREE_LOG(DEBUG,
623                                 "TX descriptor %4u is not done"
624                                 "(port=%d queue=%d)",
625                                 desc_to_clean_to,
626                                 txq->port_id, txq->queue_id);
627                 if (txq->nb_tx_free >> 1 < txq->tx_free_thresh)
628                         txgbe_set32_masked(txq->tdc_reg_addr,
629                                 TXGBE_TXCFG_FLUSH, TXGBE_TXCFG_FLUSH);
630                 /* Failed to clean any descriptors, better luck next time */
631                 return -(1);
632         }
633
634         /* Figure out how many descriptors will be cleaned */
635         if (last_desc_cleaned > desc_to_clean_to)
636                 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
637                                                         desc_to_clean_to);
638         else
639                 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
640                                                 last_desc_cleaned);
641
642         PMD_TX_FREE_LOG(DEBUG,
643                         "Cleaning %4u TX descriptors: %4u to %4u "
644                         "(port=%d queue=%d)",
645                         nb_tx_to_clean, last_desc_cleaned, desc_to_clean_to,
646                         txq->port_id, txq->queue_id);
647
648         /*
649          * The last descriptor to clean is done, so that means all the
650          * descriptors from the last descriptor that was cleaned
651          * up to the last descriptor with the RS bit set
652          * are done. Only reset the threshold descriptor.
653          */
654         txr[desc_to_clean_to].dw3 = 0;
655
656         /* Update the txq to reflect the last descriptor that was cleaned */
657         txq->last_desc_cleaned = desc_to_clean_to;
658         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
659
660         /* No Error */
661         return 0;
662 }
663
664 static inline uint8_t
665 txgbe_get_tun_len(struct rte_mbuf *mbuf)
666 {
667         struct txgbe_genevehdr genevehdr;
668         const struct txgbe_genevehdr *gh;
669         uint8_t tun_len;
670
671         switch (mbuf->ol_flags & PKT_TX_TUNNEL_MASK) {
672         case PKT_TX_TUNNEL_IPIP:
673                 tun_len = 0;
674                 break;
675         case PKT_TX_TUNNEL_VXLAN:
676         case PKT_TX_TUNNEL_VXLAN_GPE:
677                 tun_len = sizeof(struct txgbe_udphdr)
678                         + sizeof(struct txgbe_vxlanhdr);
679                 break;
680         case PKT_TX_TUNNEL_GRE:
681                 tun_len = sizeof(struct txgbe_nvgrehdr);
682                 break;
683         case PKT_TX_TUNNEL_GENEVE:
684                 gh = rte_pktmbuf_read(mbuf,
685                         mbuf->outer_l2_len + mbuf->outer_l3_len,
686                         sizeof(genevehdr), &genevehdr);
687                 tun_len = sizeof(struct txgbe_udphdr)
688                         + sizeof(struct txgbe_genevehdr)
689                         + (gh->opt_len << 2);
690                 break;
691         default:
692                 tun_len = 0;
693         }
694
695         return tun_len;
696 }
697
698 uint16_t
699 txgbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
700                 uint16_t nb_pkts)
701 {
702         struct txgbe_tx_queue *txq;
703         struct txgbe_tx_entry *sw_ring;
704         struct txgbe_tx_entry *txe, *txn;
705         volatile struct txgbe_tx_desc *txr;
706         volatile struct txgbe_tx_desc *txd;
707         struct rte_mbuf     *tx_pkt;
708         struct rte_mbuf     *m_seg;
709         uint64_t buf_dma_addr;
710         uint32_t olinfo_status;
711         uint32_t cmd_type_len;
712         uint32_t pkt_len;
713         uint16_t slen;
714         uint64_t ol_flags;
715         uint16_t tx_id;
716         uint16_t tx_last;
717         uint16_t nb_tx;
718         uint16_t nb_used;
719         uint64_t tx_ol_req;
720         uint32_t ctx = 0;
721         uint32_t new_ctx;
722         union txgbe_tx_offload tx_offload;
723 #ifdef RTE_LIB_SECURITY
724         uint8_t use_ipsec;
725 #endif
726
727         tx_offload.data[0] = 0;
728         tx_offload.data[1] = 0;
729         txq = tx_queue;
730         sw_ring = txq->sw_ring;
731         txr     = txq->tx_ring;
732         tx_id   = txq->tx_tail;
733         txe = &sw_ring[tx_id];
734
735         /* Determine if the descriptor ring needs to be cleaned. */
736         if (txq->nb_tx_free < txq->tx_free_thresh)
737                 txgbe_xmit_cleanup(txq);
738
739         rte_prefetch0(&txe->mbuf->pool);
740
741         /* TX loop */
742         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
743                 new_ctx = 0;
744                 tx_pkt = *tx_pkts++;
745                 pkt_len = tx_pkt->pkt_len;
746
747                 /*
748                  * Determine how many (if any) context descriptors
749                  * are needed for offload functionality.
750                  */
751                 ol_flags = tx_pkt->ol_flags;
752 #ifdef RTE_LIB_SECURITY
753                 use_ipsec = txq->using_ipsec && (ol_flags & PKT_TX_SEC_OFFLOAD);
754 #endif
755
756                 /* If hardware offload required */
757                 tx_ol_req = ol_flags & TXGBE_TX_OFFLOAD_MASK;
758                 if (tx_ol_req) {
759                         tx_offload.ptid = tx_desc_ol_flags_to_ptid(tx_ol_req,
760                                         tx_pkt->packet_type);
761                         tx_offload.l2_len = tx_pkt->l2_len;
762                         tx_offload.l3_len = tx_pkt->l3_len;
763                         tx_offload.l4_len = tx_pkt->l4_len;
764                         tx_offload.vlan_tci = tx_pkt->vlan_tci;
765                         tx_offload.tso_segsz = tx_pkt->tso_segsz;
766                         tx_offload.outer_l2_len = tx_pkt->outer_l2_len;
767                         tx_offload.outer_l3_len = tx_pkt->outer_l3_len;
768                         tx_offload.outer_tun_len = txgbe_get_tun_len(tx_pkt);
769
770 #ifdef RTE_LIB_SECURITY
771                         if (use_ipsec) {
772                                 union txgbe_crypto_tx_desc_md *ipsec_mdata =
773                                         (union txgbe_crypto_tx_desc_md *)
774                                                 rte_security_dynfield(tx_pkt);
775                                 tx_offload.sa_idx = ipsec_mdata->sa_idx;
776                                 tx_offload.sec_pad_len = ipsec_mdata->pad_len;
777                         }
778 #endif
779
780                         /* If new context need be built or reuse the exist ctx*/
781                         ctx = what_ctx_update(txq, tx_ol_req, tx_offload);
782                         /* Only allocate context descriptor if required */
783                         new_ctx = (ctx == TXGBE_CTX_NUM);
784                         ctx = txq->ctx_curr;
785                 }
786
787                 /*
788                  * Keep track of how many descriptors are used this loop
789                  * This will always be the number of segments + the number of
790                  * Context descriptors required to transmit the packet
791                  */
792                 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
793
794                 /*
795                  * The number of descriptors that must be allocated for a
796                  * packet is the number of segments of that packet, plus 1
797                  * Context Descriptor for the hardware offload, if any.
798                  * Determine the last TX descriptor to allocate in the TX ring
799                  * for the packet, starting from the current position (tx_id)
800                  * in the ring.
801                  */
802                 tx_last = (uint16_t)(tx_id + nb_used - 1);
803
804                 /* Circular ring */
805                 if (tx_last >= txq->nb_tx_desc)
806                         tx_last = (uint16_t)(tx_last - txq->nb_tx_desc);
807
808                 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
809                            " tx_first=%u tx_last=%u",
810                            (uint16_t)txq->port_id,
811                            (uint16_t)txq->queue_id,
812                            (uint32_t)pkt_len,
813                            (uint16_t)tx_id,
814                            (uint16_t)tx_last);
815
816                 /*
817                  * Make sure there are enough TX descriptors available to
818                  * transmit the entire packet.
819                  * nb_used better be less than or equal to txq->tx_free_thresh
820                  */
821                 if (nb_used > txq->nb_tx_free) {
822                         PMD_TX_FREE_LOG(DEBUG,
823                                         "Not enough free TX descriptors "
824                                         "nb_used=%4u nb_free=%4u "
825                                         "(port=%d queue=%d)",
826                                         nb_used, txq->nb_tx_free,
827                                         txq->port_id, txq->queue_id);
828
829                         if (txgbe_xmit_cleanup(txq) != 0) {
830                                 /* Could not clean any descriptors */
831                                 if (nb_tx == 0)
832                                         return 0;
833                                 goto end_of_tx;
834                         }
835
836                         /* nb_used better be <= txq->tx_free_thresh */
837                         if (unlikely(nb_used > txq->tx_free_thresh)) {
838                                 PMD_TX_FREE_LOG(DEBUG,
839                                         "The number of descriptors needed to "
840                                         "transmit the packet exceeds the "
841                                         "RS bit threshold. This will impact "
842                                         "performance."
843                                         "nb_used=%4u nb_free=%4u "
844                                         "tx_free_thresh=%4u. "
845                                         "(port=%d queue=%d)",
846                                         nb_used, txq->nb_tx_free,
847                                         txq->tx_free_thresh,
848                                         txq->port_id, txq->queue_id);
849                                 /*
850                                  * Loop here until there are enough TX
851                                  * descriptors or until the ring cannot be
852                                  * cleaned.
853                                  */
854                                 while (nb_used > txq->nb_tx_free) {
855                                         if (txgbe_xmit_cleanup(txq) != 0) {
856                                                 /*
857                                                  * Could not clean any
858                                                  * descriptors
859                                                  */
860                                                 if (nb_tx == 0)
861                                                         return 0;
862                                                 goto end_of_tx;
863                                         }
864                                 }
865                         }
866                 }
867
868                 /*
869                  * By now there are enough free TX descriptors to transmit
870                  * the packet.
871                  */
872
873                 /*
874                  * Set common flags of all TX Data Descriptors.
875                  *
876                  * The following bits must be set in all Data Descriptors:
877                  *   - TXGBE_TXD_DTYP_DATA
878                  *   - TXGBE_TXD_DCMD_DEXT
879                  *
880                  * The following bits must be set in the first Data Descriptor
881                  * and are ignored in the other ones:
882                  *   - TXGBE_TXD_DCMD_IFCS
883                  *   - TXGBE_TXD_MAC_1588
884                  *   - TXGBE_TXD_DCMD_VLE
885                  *
886                  * The following bits must only be set in the last Data
887                  * Descriptor:
888                  *   - TXGBE_TXD_CMD_EOP
889                  *
890                  * The following bits can be set in any Data Descriptor, but
891                  * are only set in the last Data Descriptor:
892                  *   - TXGBE_TXD_CMD_RS
893                  */
894                 cmd_type_len = TXGBE_TXD_FCS;
895
896 #ifdef RTE_LIBRTE_IEEE1588
897                 if (ol_flags & PKT_TX_IEEE1588_TMST)
898                         cmd_type_len |= TXGBE_TXD_1588;
899 #endif
900
901                 olinfo_status = 0;
902                 if (tx_ol_req) {
903                         if (ol_flags & PKT_TX_TCP_SEG) {
904                                 /* when TSO is on, paylen in descriptor is the
905                                  * not the packet len but the tcp payload len
906                                  */
907                                 pkt_len -= (tx_offload.l2_len +
908                                         tx_offload.l3_len + tx_offload.l4_len);
909                                 pkt_len -=
910                                         (tx_pkt->ol_flags & PKT_TX_TUNNEL_MASK)
911                                         ? tx_offload.outer_l2_len +
912                                           tx_offload.outer_l3_len : 0;
913                         }
914
915                         /*
916                          * Setup the TX Advanced Context Descriptor if required
917                          */
918                         if (new_ctx) {
919                                 volatile struct txgbe_tx_ctx_desc *ctx_txd;
920
921                                 ctx_txd = (volatile struct txgbe_tx_ctx_desc *)
922                                     &txr[tx_id];
923
924                                 txn = &sw_ring[txe->next_id];
925                                 rte_prefetch0(&txn->mbuf->pool);
926
927                                 if (txe->mbuf != NULL) {
928                                         rte_pktmbuf_free_seg(txe->mbuf);
929                                         txe->mbuf = NULL;
930                                 }
931
932                                 txgbe_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
933                                         tx_offload,
934                                         rte_security_dynfield(tx_pkt));
935
936                                 txe->last_id = tx_last;
937                                 tx_id = txe->next_id;
938                                 txe = txn;
939                         }
940
941                         /*
942                          * Setup the TX Advanced Data Descriptor,
943                          * This path will go through
944                          * whatever new/reuse the context descriptor
945                          */
946                         cmd_type_len  |= tx_desc_ol_flags_to_cmdtype(ol_flags);
947                         olinfo_status |=
948                                 tx_desc_cksum_flags_to_olinfo(ol_flags);
949                         olinfo_status |= TXGBE_TXD_IDX(ctx);
950                 }
951
952                 olinfo_status |= TXGBE_TXD_PAYLEN(pkt_len);
953 #ifdef RTE_LIB_SECURITY
954                 if (use_ipsec)
955                         olinfo_status |= TXGBE_TXD_IPSEC;
956 #endif
957
958                 m_seg = tx_pkt;
959                 do {
960                         txd = &txr[tx_id];
961                         txn = &sw_ring[txe->next_id];
962                         rte_prefetch0(&txn->mbuf->pool);
963
964                         if (txe->mbuf != NULL)
965                                 rte_pktmbuf_free_seg(txe->mbuf);
966                         txe->mbuf = m_seg;
967
968                         /*
969                          * Set up Transmit Data Descriptor.
970                          */
971                         slen = m_seg->data_len;
972                         buf_dma_addr = rte_mbuf_data_iova(m_seg);
973                         txd->qw0 = rte_cpu_to_le_64(buf_dma_addr);
974                         txd->dw2 = rte_cpu_to_le_32(cmd_type_len | slen);
975                         txd->dw3 = rte_cpu_to_le_32(olinfo_status);
976                         txe->last_id = tx_last;
977                         tx_id = txe->next_id;
978                         txe = txn;
979                         m_seg = m_seg->next;
980                 } while (m_seg != NULL);
981
982                 /*
983                  * The last packet data descriptor needs End Of Packet (EOP)
984                  */
985                 cmd_type_len |= TXGBE_TXD_EOP;
986                 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
987
988                 txd->dw2 |= rte_cpu_to_le_32(cmd_type_len);
989         }
990
991 end_of_tx:
992
993         rte_wmb();
994
995         /*
996          * Set the Transmit Descriptor Tail (TDT)
997          */
998         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
999                    (uint16_t)txq->port_id, (uint16_t)txq->queue_id,
1000                    (uint16_t)tx_id, (uint16_t)nb_tx);
1001         txgbe_set32_relaxed(txq->tdt_reg_addr, tx_id);
1002         txq->tx_tail = tx_id;
1003
1004         return nb_tx;
1005 }
1006
1007 /*********************************************************************
1008  *
1009  *  TX prep functions
1010  *
1011  **********************************************************************/
1012 uint16_t
1013 txgbe_prep_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1014 {
1015         int i, ret;
1016         uint64_t ol_flags;
1017         struct rte_mbuf *m;
1018         struct txgbe_tx_queue *txq = (struct txgbe_tx_queue *)tx_queue;
1019
1020         for (i = 0; i < nb_pkts; i++) {
1021                 m = tx_pkts[i];
1022                 ol_flags = m->ol_flags;
1023
1024                 /**
1025                  * Check if packet meets requirements for number of segments
1026                  *
1027                  * NOTE: for txgbe it's always (40 - WTHRESH) for both TSO and
1028                  *       non-TSO
1029                  */
1030
1031                 if (m->nb_segs > TXGBE_TX_MAX_SEG - txq->wthresh) {
1032                         rte_errno = -EINVAL;
1033                         return i;
1034                 }
1035
1036                 if (ol_flags & TXGBE_TX_OFFLOAD_NOTSUP_MASK) {
1037                         rte_errno = -ENOTSUP;
1038                         return i;
1039                 }
1040
1041 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1042                 ret = rte_validate_tx_offload(m);
1043                 if (ret != 0) {
1044                         rte_errno = ret;
1045                         return i;
1046                 }
1047 #endif
1048                 ret = rte_net_intel_cksum_prepare(m);
1049                 if (ret != 0) {
1050                         rte_errno = ret;
1051                         return i;
1052                 }
1053         }
1054
1055         return i;
1056 }
1057
1058 /*********************************************************************
1059  *
1060  *  RX functions
1061  *
1062  **********************************************************************/
1063 /* @note: fix txgbe_dev_supported_ptypes_get() if any change here. */
1064 static inline uint32_t
1065 txgbe_rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint16_t ptid_mask)
1066 {
1067         uint16_t ptid = TXGBE_RXD_PTID(pkt_info);
1068
1069         ptid &= ptid_mask;
1070
1071         return txgbe_decode_ptype(ptid);
1072 }
1073
1074 static inline uint64_t
1075 txgbe_rxd_pkt_info_to_pkt_flags(uint32_t pkt_info)
1076 {
1077         static uint64_t ip_rss_types_map[16] __rte_cache_aligned = {
1078                 0, PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, PKT_RX_RSS_HASH,
1079                 0, PKT_RX_RSS_HASH, 0, PKT_RX_RSS_HASH,
1080                 PKT_RX_RSS_HASH, 0, 0, 0,
1081                 0, 0, 0,  PKT_RX_FDIR,
1082         };
1083 #ifdef RTE_LIBRTE_IEEE1588
1084         static uint64_t ip_pkt_etqf_map[8] = {
1085                 0, 0, 0, PKT_RX_IEEE1588_PTP,
1086                 0, 0, 0, 0,
1087         };
1088         int etfid = txgbe_etflt_id(TXGBE_RXD_PTID(pkt_info));
1089         if (likely(-1 != etfid))
1090                 return ip_pkt_etqf_map[etfid] |
1091                        ip_rss_types_map[TXGBE_RXD_RSSTYPE(pkt_info)];
1092         else
1093                 return ip_rss_types_map[TXGBE_RXD_RSSTYPE(pkt_info)];
1094 #else
1095         return ip_rss_types_map[TXGBE_RXD_RSSTYPE(pkt_info)];
1096 #endif
1097 }
1098
1099 static inline uint64_t
1100 rx_desc_status_to_pkt_flags(uint32_t rx_status, uint64_t vlan_flags)
1101 {
1102         uint64_t pkt_flags;
1103
1104         /*
1105          * Check if VLAN present only.
1106          * Do not check whether L3/L4 rx checksum done by NIC or not,
1107          * That can be found from rte_eth_rxmode.offloads flag
1108          */
1109         pkt_flags = (rx_status & TXGBE_RXD_STAT_VLAN &&
1110                      vlan_flags & PKT_RX_VLAN_STRIPPED)
1111                     ? vlan_flags : 0;
1112
1113 #ifdef RTE_LIBRTE_IEEE1588
1114         if (rx_status & TXGBE_RXD_STAT_1588)
1115                 pkt_flags = pkt_flags | PKT_RX_IEEE1588_TMST;
1116 #endif
1117         return pkt_flags;
1118 }
1119
1120 static inline uint64_t
1121 rx_desc_error_to_pkt_flags(uint32_t rx_status)
1122 {
1123         uint64_t pkt_flags = 0;
1124
1125         /* checksum offload can't be disabled */
1126         if (rx_status & TXGBE_RXD_STAT_IPCS) {
1127                 pkt_flags |= (rx_status & TXGBE_RXD_ERR_IPCS
1128                                 ? PKT_RX_IP_CKSUM_BAD : PKT_RX_IP_CKSUM_GOOD);
1129         }
1130
1131         if (rx_status & TXGBE_RXD_STAT_L4CS) {
1132                 pkt_flags |= (rx_status & TXGBE_RXD_ERR_L4CS
1133                                 ? PKT_RX_L4_CKSUM_BAD : PKT_RX_L4_CKSUM_GOOD);
1134         }
1135
1136         if (rx_status & TXGBE_RXD_STAT_EIPCS &&
1137             rx_status & TXGBE_RXD_ERR_EIPCS) {
1138                 pkt_flags |= PKT_RX_OUTER_IP_CKSUM_BAD;
1139         }
1140
1141 #ifdef RTE_LIB_SECURITY
1142         if (rx_status & TXGBE_RXD_STAT_SECP) {
1143                 pkt_flags |= PKT_RX_SEC_OFFLOAD;
1144                 if (rx_status & TXGBE_RXD_ERR_SECERR)
1145                         pkt_flags |= PKT_RX_SEC_OFFLOAD_FAILED;
1146         }
1147 #endif
1148
1149         return pkt_flags;
1150 }
1151
1152 /*
1153  * LOOK_AHEAD defines how many desc statuses to check beyond the
1154  * current descriptor.
1155  * It must be a pound define for optimal performance.
1156  * Do not change the value of LOOK_AHEAD, as the txgbe_rx_scan_hw_ring
1157  * function only works with LOOK_AHEAD=8.
1158  */
1159 #define LOOK_AHEAD 8
1160 #if (LOOK_AHEAD != 8)
1161 #error "PMD TXGBE: LOOK_AHEAD must be 8\n"
1162 #endif
1163 static inline int
1164 txgbe_rx_scan_hw_ring(struct txgbe_rx_queue *rxq)
1165 {
1166         volatile struct txgbe_rx_desc *rxdp;
1167         struct txgbe_rx_entry *rxep;
1168         struct rte_mbuf *mb;
1169         uint16_t pkt_len;
1170         uint64_t pkt_flags;
1171         int nb_dd;
1172         uint32_t s[LOOK_AHEAD];
1173         uint32_t pkt_info[LOOK_AHEAD];
1174         int i, j, nb_rx = 0;
1175         uint32_t status;
1176
1177         /* get references to current descriptor and S/W ring entry */
1178         rxdp = &rxq->rx_ring[rxq->rx_tail];
1179         rxep = &rxq->sw_ring[rxq->rx_tail];
1180
1181         status = rxdp->qw1.lo.status;
1182         /* check to make sure there is at least 1 packet to receive */
1183         if (!(status & rte_cpu_to_le_32(TXGBE_RXD_STAT_DD)))
1184                 return 0;
1185
1186         /*
1187          * Scan LOOK_AHEAD descriptors at a time to determine which descriptors
1188          * reference packets that are ready to be received.
1189          */
1190         for (i = 0; i < RTE_PMD_TXGBE_RX_MAX_BURST;
1191              i += LOOK_AHEAD, rxdp += LOOK_AHEAD, rxep += LOOK_AHEAD) {
1192                 /* Read desc statuses backwards to avoid race condition */
1193                 for (j = 0; j < LOOK_AHEAD; j++)
1194                         s[j] = rte_le_to_cpu_32(rxdp[j].qw1.lo.status);
1195
1196                 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
1197
1198                 /* Compute how many status bits were set */
1199                 for (nb_dd = 0; nb_dd < LOOK_AHEAD &&
1200                                 (s[nb_dd] & TXGBE_RXD_STAT_DD); nb_dd++)
1201                         ;
1202
1203                 for (j = 0; j < nb_dd; j++)
1204                         pkt_info[j] = rte_le_to_cpu_32(rxdp[j].qw0.dw0);
1205
1206                 nb_rx += nb_dd;
1207
1208                 /* Translate descriptor info to mbuf format */
1209                 for (j = 0; j < nb_dd; ++j) {
1210                         mb = rxep[j].mbuf;
1211                         pkt_len = rte_le_to_cpu_16(rxdp[j].qw1.hi.len) -
1212                                   rxq->crc_len;
1213                         mb->data_len = pkt_len;
1214                         mb->pkt_len = pkt_len;
1215                         mb->vlan_tci = rte_le_to_cpu_16(rxdp[j].qw1.hi.tag);
1216
1217                         /* convert descriptor fields to rte mbuf flags */
1218                         pkt_flags = rx_desc_status_to_pkt_flags(s[j],
1219                                         rxq->vlan_flags);
1220                         pkt_flags |= rx_desc_error_to_pkt_flags(s[j]);
1221                         pkt_flags |=
1222                                 txgbe_rxd_pkt_info_to_pkt_flags(pkt_info[j]);
1223                         mb->ol_flags = pkt_flags;
1224                         mb->packet_type =
1225                                 txgbe_rxd_pkt_info_to_pkt_type(pkt_info[j],
1226                                 rxq->pkt_type_mask);
1227
1228                         if (likely(pkt_flags & PKT_RX_RSS_HASH))
1229                                 mb->hash.rss =
1230                                         rte_le_to_cpu_32(rxdp[j].qw0.dw1);
1231                         else if (pkt_flags & PKT_RX_FDIR) {
1232                                 mb->hash.fdir.hash =
1233                                         rte_le_to_cpu_16(rxdp[j].qw0.hi.csum) &
1234                                         TXGBE_ATR_HASH_MASK;
1235                                 mb->hash.fdir.id =
1236                                         rte_le_to_cpu_16(rxdp[j].qw0.hi.ipid);
1237                         }
1238                 }
1239
1240                 /* Move mbuf pointers from the S/W ring to the stage */
1241                 for (j = 0; j < LOOK_AHEAD; ++j)
1242                         rxq->rx_stage[i + j] = rxep[j].mbuf;
1243
1244                 /* stop if all requested packets could not be received */
1245                 if (nb_dd != LOOK_AHEAD)
1246                         break;
1247         }
1248
1249         /* clear software ring entries so we can cleanup correctly */
1250         for (i = 0; i < nb_rx; ++i)
1251                 rxq->sw_ring[rxq->rx_tail + i].mbuf = NULL;
1252
1253         return nb_rx;
1254 }
1255
1256 static inline int
1257 txgbe_rx_alloc_bufs(struct txgbe_rx_queue *rxq, bool reset_mbuf)
1258 {
1259         volatile struct txgbe_rx_desc *rxdp;
1260         struct txgbe_rx_entry *rxep;
1261         struct rte_mbuf *mb;
1262         uint16_t alloc_idx;
1263         __le64 dma_addr;
1264         int diag, i;
1265
1266         /* allocate buffers in bulk directly into the S/W ring */
1267         alloc_idx = rxq->rx_free_trigger - (rxq->rx_free_thresh - 1);
1268         rxep = &rxq->sw_ring[alloc_idx];
1269         diag = rte_mempool_get_bulk(rxq->mb_pool, (void *)rxep,
1270                                     rxq->rx_free_thresh);
1271         if (unlikely(diag != 0))
1272                 return -ENOMEM;
1273
1274         rxdp = &rxq->rx_ring[alloc_idx];
1275         for (i = 0; i < rxq->rx_free_thresh; ++i) {
1276                 /* populate the static rte mbuf fields */
1277                 mb = rxep[i].mbuf;
1278                 if (reset_mbuf)
1279                         mb->port = rxq->port_id;
1280
1281                 rte_mbuf_refcnt_set(mb, 1);
1282                 mb->data_off = RTE_PKTMBUF_HEADROOM;
1283
1284                 /* populate the descriptors */
1285                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mb));
1286                 TXGBE_RXD_HDRADDR(&rxdp[i], 0);
1287                 TXGBE_RXD_PKTADDR(&rxdp[i], dma_addr);
1288         }
1289
1290         /* update state of internal queue structure */
1291         rxq->rx_free_trigger = rxq->rx_free_trigger + rxq->rx_free_thresh;
1292         if (rxq->rx_free_trigger >= rxq->nb_rx_desc)
1293                 rxq->rx_free_trigger = rxq->rx_free_thresh - 1;
1294
1295         /* no errors */
1296         return 0;
1297 }
1298
1299 static inline uint16_t
1300 txgbe_rx_fill_from_stage(struct txgbe_rx_queue *rxq, struct rte_mbuf **rx_pkts,
1301                          uint16_t nb_pkts)
1302 {
1303         struct rte_mbuf **stage = &rxq->rx_stage[rxq->rx_next_avail];
1304         int i;
1305
1306         /* how many packets are ready to return? */
1307         nb_pkts = (uint16_t)RTE_MIN(nb_pkts, rxq->rx_nb_avail);
1308
1309         /* copy mbuf pointers to the application's packet list */
1310         for (i = 0; i < nb_pkts; ++i)
1311                 rx_pkts[i] = stage[i];
1312
1313         /* update internal queue state */
1314         rxq->rx_nb_avail = (uint16_t)(rxq->rx_nb_avail - nb_pkts);
1315         rxq->rx_next_avail = (uint16_t)(rxq->rx_next_avail + nb_pkts);
1316
1317         return nb_pkts;
1318 }
1319
1320 static inline uint16_t
1321 txgbe_rx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1322              uint16_t nb_pkts)
1323 {
1324         struct txgbe_rx_queue *rxq = (struct txgbe_rx_queue *)rx_queue;
1325         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1326         uint16_t nb_rx = 0;
1327
1328         /* Any previously recv'd pkts will be returned from the Rx stage */
1329         if (rxq->rx_nb_avail)
1330                 return txgbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1331
1332         /* Scan the H/W ring for packets to receive */
1333         nb_rx = (uint16_t)txgbe_rx_scan_hw_ring(rxq);
1334
1335         /* update internal queue state */
1336         rxq->rx_next_avail = 0;
1337         rxq->rx_nb_avail = nb_rx;
1338         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_rx);
1339
1340         /* if required, allocate new buffers to replenish descriptors */
1341         if (rxq->rx_tail > rxq->rx_free_trigger) {
1342                 uint16_t cur_free_trigger = rxq->rx_free_trigger;
1343
1344                 if (txgbe_rx_alloc_bufs(rxq, true) != 0) {
1345                         int i, j;
1346
1347                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1348                                    "queue_id=%u", (uint16_t)rxq->port_id,
1349                                    (uint16_t)rxq->queue_id);
1350
1351                         dev->data->rx_mbuf_alloc_failed +=
1352                                 rxq->rx_free_thresh;
1353
1354                         /*
1355                          * Need to rewind any previous receives if we cannot
1356                          * allocate new buffers to replenish the old ones.
1357                          */
1358                         rxq->rx_nb_avail = 0;
1359                         rxq->rx_tail = (uint16_t)(rxq->rx_tail - nb_rx);
1360                         for (i = 0, j = rxq->rx_tail; i < nb_rx; ++i, ++j)
1361                                 rxq->sw_ring[j].mbuf = rxq->rx_stage[i];
1362
1363                         return 0;
1364                 }
1365
1366                 /* update tail pointer */
1367                 rte_wmb();
1368                 txgbe_set32_relaxed(rxq->rdt_reg_addr, cur_free_trigger);
1369         }
1370
1371         if (rxq->rx_tail >= rxq->nb_rx_desc)
1372                 rxq->rx_tail = 0;
1373
1374         /* received any packets this loop? */
1375         if (rxq->rx_nb_avail)
1376                 return txgbe_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1377
1378         return 0;
1379 }
1380
1381 /* split requests into chunks of size RTE_PMD_TXGBE_RX_MAX_BURST */
1382 uint16_t
1383 txgbe_recv_pkts_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1384                            uint16_t nb_pkts)
1385 {
1386         uint16_t nb_rx;
1387
1388         if (unlikely(nb_pkts == 0))
1389                 return 0;
1390
1391         if (likely(nb_pkts <= RTE_PMD_TXGBE_RX_MAX_BURST))
1392                 return txgbe_rx_recv_pkts(rx_queue, rx_pkts, nb_pkts);
1393
1394         /* request is relatively large, chunk it up */
1395         nb_rx = 0;
1396         while (nb_pkts) {
1397                 uint16_t ret, n;
1398
1399                 n = (uint16_t)RTE_MIN(nb_pkts, RTE_PMD_TXGBE_RX_MAX_BURST);
1400                 ret = txgbe_rx_recv_pkts(rx_queue, &rx_pkts[nb_rx], n);
1401                 nb_rx = (uint16_t)(nb_rx + ret);
1402                 nb_pkts = (uint16_t)(nb_pkts - ret);
1403                 if (ret < n)
1404                         break;
1405         }
1406
1407         return nb_rx;
1408 }
1409
1410 uint16_t
1411 txgbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1412                 uint16_t nb_pkts)
1413 {
1414         struct txgbe_rx_queue *rxq;
1415         volatile struct txgbe_rx_desc *rx_ring;
1416         volatile struct txgbe_rx_desc *rxdp;
1417         struct txgbe_rx_entry *sw_ring;
1418         struct txgbe_rx_entry *rxe;
1419         struct rte_mbuf *rxm;
1420         struct rte_mbuf *nmb;
1421         struct txgbe_rx_desc rxd;
1422         uint64_t dma_addr;
1423         uint32_t staterr;
1424         uint32_t pkt_info;
1425         uint16_t pkt_len;
1426         uint16_t rx_id;
1427         uint16_t nb_rx;
1428         uint16_t nb_hold;
1429         uint64_t pkt_flags;
1430
1431         nb_rx = 0;
1432         nb_hold = 0;
1433         rxq = rx_queue;
1434         rx_id = rxq->rx_tail;
1435         rx_ring = rxq->rx_ring;
1436         sw_ring = rxq->sw_ring;
1437         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1438         while (nb_rx < nb_pkts) {
1439                 /*
1440                  * The order of operations here is important as the DD status
1441                  * bit must not be read after any other descriptor fields.
1442                  * rx_ring and rxdp are pointing to volatile data so the order
1443                  * of accesses cannot be reordered by the compiler. If they were
1444                  * not volatile, they could be reordered which could lead to
1445                  * using invalid descriptor fields when read from rxd.
1446                  */
1447                 rxdp = &rx_ring[rx_id];
1448                 staterr = rxdp->qw1.lo.status;
1449                 if (!(staterr & rte_cpu_to_le_32(TXGBE_RXD_STAT_DD)))
1450                         break;
1451                 rxd = *rxdp;
1452
1453                 /*
1454                  * End of packet.
1455                  *
1456                  * If the TXGBE_RXD_STAT_EOP flag is not set, the RX packet
1457                  * is likely to be invalid and to be dropped by the various
1458                  * validation checks performed by the network stack.
1459                  *
1460                  * Allocate a new mbuf to replenish the RX ring descriptor.
1461                  * If the allocation fails:
1462                  *    - arrange for that RX descriptor to be the first one
1463                  *      being parsed the next time the receive function is
1464                  *      invoked [on the same queue].
1465                  *
1466                  *    - Stop parsing the RX ring and return immediately.
1467                  *
1468                  * This policy do not drop the packet received in the RX
1469                  * descriptor for which the allocation of a new mbuf failed.
1470                  * Thus, it allows that packet to be later retrieved if
1471                  * mbuf have been freed in the mean time.
1472                  * As a side effect, holding RX descriptors instead of
1473                  * systematically giving them back to the NIC may lead to
1474                  * RX ring exhaustion situations.
1475                  * However, the NIC can gracefully prevent such situations
1476                  * to happen by sending specific "back-pressure" flow control
1477                  * frames to its peer(s).
1478                  */
1479                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1480                            "ext_err_stat=0x%08x pkt_len=%u",
1481                            (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1482                            (uint16_t)rx_id, (uint32_t)staterr,
1483                            (uint16_t)rte_le_to_cpu_16(rxd.qw1.hi.len));
1484
1485                 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1486                 if (nmb == NULL) {
1487                         PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1488                                    "queue_id=%u", (uint16_t)rxq->port_id,
1489                                    (uint16_t)rxq->queue_id);
1490                         dev->data->rx_mbuf_alloc_failed++;
1491                         break;
1492                 }
1493
1494                 nb_hold++;
1495                 rxe = &sw_ring[rx_id];
1496                 rx_id++;
1497                 if (rx_id == rxq->nb_rx_desc)
1498                         rx_id = 0;
1499
1500                 /* Prefetch next mbuf while processing current one. */
1501                 rte_txgbe_prefetch(sw_ring[rx_id].mbuf);
1502
1503                 /*
1504                  * When next RX descriptor is on a cache-line boundary,
1505                  * prefetch the next 4 RX descriptors and the next 8 pointers
1506                  * to mbufs.
1507                  */
1508                 if ((rx_id & 0x3) == 0) {
1509                         rte_txgbe_prefetch(&rx_ring[rx_id]);
1510                         rte_txgbe_prefetch(&sw_ring[rx_id]);
1511                 }
1512
1513                 rxm = rxe->mbuf;
1514                 rxe->mbuf = nmb;
1515                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1516                 TXGBE_RXD_HDRADDR(rxdp, 0);
1517                 TXGBE_RXD_PKTADDR(rxdp, dma_addr);
1518
1519                 /*
1520                  * Initialize the returned mbuf.
1521                  * 1) setup generic mbuf fields:
1522                  *    - number of segments,
1523                  *    - next segment,
1524                  *    - packet length,
1525                  *    - RX port identifier.
1526                  * 2) integrate hardware offload data, if any:
1527                  *    - RSS flag & hash,
1528                  *    - IP checksum flag,
1529                  *    - VLAN TCI, if any,
1530                  *    - error flags.
1531                  */
1532                 pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.qw1.hi.len) -
1533                                       rxq->crc_len);
1534                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1535                 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
1536                 rxm->nb_segs = 1;
1537                 rxm->next = NULL;
1538                 rxm->pkt_len = pkt_len;
1539                 rxm->data_len = pkt_len;
1540                 rxm->port = rxq->port_id;
1541
1542                 pkt_info = rte_le_to_cpu_32(rxd.qw0.dw0);
1543                 /* Only valid if PKT_RX_VLAN set in pkt_flags */
1544                 rxm->vlan_tci = rte_le_to_cpu_16(rxd.qw1.hi.tag);
1545
1546                 pkt_flags = rx_desc_status_to_pkt_flags(staterr,
1547                                         rxq->vlan_flags);
1548                 pkt_flags |= rx_desc_error_to_pkt_flags(staterr);
1549                 pkt_flags |= txgbe_rxd_pkt_info_to_pkt_flags(pkt_info);
1550                 rxm->ol_flags = pkt_flags;
1551                 rxm->packet_type = txgbe_rxd_pkt_info_to_pkt_type(pkt_info,
1552                                                        rxq->pkt_type_mask);
1553
1554                 if (likely(pkt_flags & PKT_RX_RSS_HASH)) {
1555                         rxm->hash.rss = rte_le_to_cpu_32(rxd.qw0.dw1);
1556                 } else if (pkt_flags & PKT_RX_FDIR) {
1557                         rxm->hash.fdir.hash =
1558                                 rte_le_to_cpu_16(rxd.qw0.hi.csum) &
1559                                 TXGBE_ATR_HASH_MASK;
1560                         rxm->hash.fdir.id = rte_le_to_cpu_16(rxd.qw0.hi.ipid);
1561                 }
1562                 /*
1563                  * Store the mbuf address into the next entry of the array
1564                  * of returned packets.
1565                  */
1566                 rx_pkts[nb_rx++] = rxm;
1567         }
1568         rxq->rx_tail = rx_id;
1569
1570         /*
1571          * If the number of free RX descriptors is greater than the RX free
1572          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1573          * register.
1574          * Update the RDT with the value of the last processed RX descriptor
1575          * minus 1, to guarantee that the RDT register is never equal to the
1576          * RDH register, which creates a "full" ring situation from the
1577          * hardware point of view...
1578          */
1579         nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
1580         if (nb_hold > rxq->rx_free_thresh) {
1581                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1582                            "nb_hold=%u nb_rx=%u",
1583                            (uint16_t)rxq->port_id, (uint16_t)rxq->queue_id,
1584                            (uint16_t)rx_id, (uint16_t)nb_hold,
1585                            (uint16_t)nb_rx);
1586                 rx_id = (uint16_t)((rx_id == 0) ?
1587                                 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1588                 txgbe_set32(rxq->rdt_reg_addr, rx_id);
1589                 nb_hold = 0;
1590         }
1591         rxq->nb_rx_hold = nb_hold;
1592         return nb_rx;
1593 }
1594
1595 /**
1596  * txgbe_fill_cluster_head_buf - fill the first mbuf of the returned packet
1597  *
1598  * Fill the following info in the HEAD buffer of the Rx cluster:
1599  *    - RX port identifier
1600  *    - hardware offload data, if any:
1601  *      - RSS flag & hash
1602  *      - IP checksum flag
1603  *      - VLAN TCI, if any
1604  *      - error flags
1605  * @head HEAD of the packet cluster
1606  * @desc HW descriptor to get data from
1607  * @rxq Pointer to the Rx queue
1608  */
1609 static inline void
1610 txgbe_fill_cluster_head_buf(struct rte_mbuf *head, struct txgbe_rx_desc *desc,
1611                 struct txgbe_rx_queue *rxq, uint32_t staterr)
1612 {
1613         uint32_t pkt_info;
1614         uint64_t pkt_flags;
1615
1616         head->port = rxq->port_id;
1617
1618         /* The vlan_tci field is only valid when PKT_RX_VLAN is
1619          * set in the pkt_flags field.
1620          */
1621         head->vlan_tci = rte_le_to_cpu_16(desc->qw1.hi.tag);
1622         pkt_info = rte_le_to_cpu_32(desc->qw0.dw0);
1623         pkt_flags = rx_desc_status_to_pkt_flags(staterr, rxq->vlan_flags);
1624         pkt_flags |= rx_desc_error_to_pkt_flags(staterr);
1625         pkt_flags |= txgbe_rxd_pkt_info_to_pkt_flags(pkt_info);
1626         head->ol_flags = pkt_flags;
1627         head->packet_type = txgbe_rxd_pkt_info_to_pkt_type(pkt_info,
1628                                                 rxq->pkt_type_mask);
1629
1630         if (likely(pkt_flags & PKT_RX_RSS_HASH)) {
1631                 head->hash.rss = rte_le_to_cpu_32(desc->qw0.dw1);
1632         } else if (pkt_flags & PKT_RX_FDIR) {
1633                 head->hash.fdir.hash = rte_le_to_cpu_16(desc->qw0.hi.csum)
1634                                 & TXGBE_ATR_HASH_MASK;
1635                 head->hash.fdir.id = rte_le_to_cpu_16(desc->qw0.hi.ipid);
1636         }
1637 }
1638
1639 /**
1640  * txgbe_recv_pkts_lro - receive handler for and LRO case.
1641  *
1642  * @rx_queue Rx queue handle
1643  * @rx_pkts table of received packets
1644  * @nb_pkts size of rx_pkts table
1645  * @bulk_alloc if TRUE bulk allocation is used for a HW ring refilling
1646  *
1647  * Handles the Rx HW ring completions when RSC feature is configured. Uses an
1648  * additional ring of txgbe_rsc_entry's that will hold the relevant RSC info.
1649  *
1650  * We use the same logic as in Linux and in FreeBSD txgbe drivers:
1651  * 1) When non-EOP RSC completion arrives:
1652  *    a) Update the HEAD of the current RSC aggregation cluster with the new
1653  *       segment's data length.
1654  *    b) Set the "next" pointer of the current segment to point to the segment
1655  *       at the NEXTP index.
1656  *    c) Pass the HEAD of RSC aggregation cluster on to the next NEXTP entry
1657  *       in the sw_rsc_ring.
1658  * 2) When EOP arrives we just update the cluster's total length and offload
1659  *    flags and deliver the cluster up to the upper layers. In our case - put it
1660  *    in the rx_pkts table.
1661  *
1662  * Returns the number of received packets/clusters (according to the "bulk
1663  * receive" interface).
1664  */
1665 static inline uint16_t
1666 txgbe_recv_pkts_lro(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts,
1667                     bool bulk_alloc)
1668 {
1669         struct txgbe_rx_queue *rxq = rx_queue;
1670         struct rte_eth_dev *dev = &rte_eth_devices[rxq->port_id];
1671         volatile struct txgbe_rx_desc *rx_ring = rxq->rx_ring;
1672         struct txgbe_rx_entry *sw_ring = rxq->sw_ring;
1673         struct txgbe_scattered_rx_entry *sw_sc_ring = rxq->sw_sc_ring;
1674         uint16_t rx_id = rxq->rx_tail;
1675         uint16_t nb_rx = 0;
1676         uint16_t nb_hold = rxq->nb_rx_hold;
1677         uint16_t prev_id = rxq->rx_tail;
1678
1679         while (nb_rx < nb_pkts) {
1680                 bool eop;
1681                 struct txgbe_rx_entry *rxe;
1682                 struct txgbe_scattered_rx_entry *sc_entry;
1683                 struct txgbe_scattered_rx_entry *next_sc_entry = NULL;
1684                 struct txgbe_rx_entry *next_rxe = NULL;
1685                 struct rte_mbuf *first_seg;
1686                 struct rte_mbuf *rxm;
1687                 struct rte_mbuf *nmb = NULL;
1688                 struct txgbe_rx_desc rxd;
1689                 uint16_t data_len;
1690                 uint16_t next_id;
1691                 volatile struct txgbe_rx_desc *rxdp;
1692                 uint32_t staterr;
1693
1694 next_desc:
1695                 /*
1696                  * The code in this whole file uses the volatile pointer to
1697                  * ensure the read ordering of the status and the rest of the
1698                  * descriptor fields (on the compiler level only!!!). This is so
1699                  * UGLY - why not to just use the compiler barrier instead? DPDK
1700                  * even has the rte_compiler_barrier() for that.
1701                  *
1702                  * But most importantly this is just wrong because this doesn't
1703                  * ensure memory ordering in a general case at all. For
1704                  * instance, DPDK is supposed to work on Power CPUs where
1705                  * compiler barrier may just not be enough!
1706                  *
1707                  * I tried to write only this function properly to have a
1708                  * starting point (as a part of an LRO/RSC series) but the
1709                  * compiler cursed at me when I tried to cast away the
1710                  * "volatile" from rx_ring (yes, it's volatile too!!!). So, I'm
1711                  * keeping it the way it is for now.
1712                  *
1713                  * The code in this file is broken in so many other places and
1714                  * will just not work on a big endian CPU anyway therefore the
1715                  * lines below will have to be revisited together with the rest
1716                  * of the txgbe PMD.
1717                  *
1718                  * TODO:
1719                  *    - Get rid of "volatile" and let the compiler do its job.
1720                  *    - Use the proper memory barrier (rte_rmb()) to ensure the
1721                  *      memory ordering below.
1722                  */
1723                 rxdp = &rx_ring[rx_id];
1724                 staterr = rte_le_to_cpu_32(rxdp->qw1.lo.status);
1725
1726                 if (!(staterr & TXGBE_RXD_STAT_DD))
1727                         break;
1728
1729                 rxd = *rxdp;
1730
1731                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1732                                   "staterr=0x%x data_len=%u",
1733                            rxq->port_id, rxq->queue_id, rx_id, staterr,
1734                            rte_le_to_cpu_16(rxd.qw1.hi.len));
1735
1736                 if (!bulk_alloc) {
1737                         nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1738                         if (nmb == NULL) {
1739                                 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed "
1740                                                   "port_id=%u queue_id=%u",
1741                                            rxq->port_id, rxq->queue_id);
1742
1743                                 dev->data->rx_mbuf_alloc_failed++;
1744                                 break;
1745                         }
1746                 } else if (nb_hold > rxq->rx_free_thresh) {
1747                         uint16_t next_rdt = rxq->rx_free_trigger;
1748
1749                         if (!txgbe_rx_alloc_bufs(rxq, false)) {
1750                                 rte_wmb();
1751                                 txgbe_set32_relaxed(rxq->rdt_reg_addr,
1752                                                             next_rdt);
1753                                 nb_hold -= rxq->rx_free_thresh;
1754                         } else {
1755                                 PMD_RX_LOG(DEBUG, "RX bulk alloc failed "
1756                                                   "port_id=%u queue_id=%u",
1757                                            rxq->port_id, rxq->queue_id);
1758
1759                                 dev->data->rx_mbuf_alloc_failed++;
1760                                 break;
1761                         }
1762                 }
1763
1764                 nb_hold++;
1765                 rxe = &sw_ring[rx_id];
1766                 eop = staterr & TXGBE_RXD_STAT_EOP;
1767
1768                 next_id = rx_id + 1;
1769                 if (next_id == rxq->nb_rx_desc)
1770                         next_id = 0;
1771
1772                 /* Prefetch next mbuf while processing current one. */
1773                 rte_txgbe_prefetch(sw_ring[next_id].mbuf);
1774
1775                 /*
1776                  * When next RX descriptor is on a cache-line boundary,
1777                  * prefetch the next 4 RX descriptors and the next 4 pointers
1778                  * to mbufs.
1779                  */
1780                 if ((next_id & 0x3) == 0) {
1781                         rte_txgbe_prefetch(&rx_ring[next_id]);
1782                         rte_txgbe_prefetch(&sw_ring[next_id]);
1783                 }
1784
1785                 rxm = rxe->mbuf;
1786
1787                 if (!bulk_alloc) {
1788                         __le64 dma =
1789                           rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1790                         /*
1791                          * Update RX descriptor with the physical address of the
1792                          * new data buffer of the new allocated mbuf.
1793                          */
1794                         rxe->mbuf = nmb;
1795
1796                         rxm->data_off = RTE_PKTMBUF_HEADROOM;
1797                         TXGBE_RXD_HDRADDR(rxdp, 0);
1798                         TXGBE_RXD_PKTADDR(rxdp, dma);
1799                 } else {
1800                         rxe->mbuf = NULL;
1801                 }
1802
1803                 /*
1804                  * Set data length & data buffer address of mbuf.
1805                  */
1806                 data_len = rte_le_to_cpu_16(rxd.qw1.hi.len);
1807                 rxm->data_len = data_len;
1808
1809                 if (!eop) {
1810                         uint16_t nextp_id;
1811                         /*
1812                          * Get next descriptor index:
1813                          *  - For RSC it's in the NEXTP field.
1814                          *  - For a scattered packet - it's just a following
1815                          *    descriptor.
1816                          */
1817                         if (TXGBE_RXD_RSCCNT(rxd.qw0.dw0))
1818                                 nextp_id = TXGBE_RXD_NEXTP(staterr);
1819                         else
1820                                 nextp_id = next_id;
1821
1822                         next_sc_entry = &sw_sc_ring[nextp_id];
1823                         next_rxe = &sw_ring[nextp_id];
1824                         rte_txgbe_prefetch(next_rxe);
1825                 }
1826
1827                 sc_entry = &sw_sc_ring[rx_id];
1828                 first_seg = sc_entry->fbuf;
1829                 sc_entry->fbuf = NULL;
1830
1831                 /*
1832                  * If this is the first buffer of the received packet,
1833                  * set the pointer to the first mbuf of the packet and
1834                  * initialize its context.
1835                  * Otherwise, update the total length and the number of segments
1836                  * of the current scattered packet, and update the pointer to
1837                  * the last mbuf of the current packet.
1838                  */
1839                 if (first_seg == NULL) {
1840                         first_seg = rxm;
1841                         first_seg->pkt_len = data_len;
1842                         first_seg->nb_segs = 1;
1843                 } else {
1844                         first_seg->pkt_len += data_len;
1845                         first_seg->nb_segs++;
1846                 }
1847
1848                 prev_id = rx_id;
1849                 rx_id = next_id;
1850
1851                 /*
1852                  * If this is not the last buffer of the received packet, update
1853                  * the pointer to the first mbuf at the NEXTP entry in the
1854                  * sw_sc_ring and continue to parse the RX ring.
1855                  */
1856                 if (!eop && next_rxe) {
1857                         rxm->next = next_rxe->mbuf;
1858                         next_sc_entry->fbuf = first_seg;
1859                         goto next_desc;
1860                 }
1861
1862                 /* Initialize the first mbuf of the returned packet */
1863                 txgbe_fill_cluster_head_buf(first_seg, &rxd, rxq, staterr);
1864
1865                 /*
1866                  * Deal with the case, when HW CRC srip is disabled.
1867                  * That can't happen when LRO is enabled, but still could
1868                  * happen for scattered RX mode.
1869                  */
1870                 first_seg->pkt_len -= rxq->crc_len;
1871                 if (unlikely(rxm->data_len <= rxq->crc_len)) {
1872                         struct rte_mbuf *lp;
1873
1874                         for (lp = first_seg; lp->next != rxm; lp = lp->next)
1875                                 ;
1876
1877                         first_seg->nb_segs--;
1878                         lp->data_len -= rxq->crc_len - rxm->data_len;
1879                         lp->next = NULL;
1880                         rte_pktmbuf_free_seg(rxm);
1881                 } else {
1882                         rxm->data_len -= rxq->crc_len;
1883                 }
1884
1885                 /* Prefetch data of first segment, if configured to do so. */
1886                 rte_packet_prefetch((char *)first_seg->buf_addr +
1887                         first_seg->data_off);
1888
1889                 /*
1890                  * Store the mbuf address into the next entry of the array
1891                  * of returned packets.
1892                  */
1893                 rx_pkts[nb_rx++] = first_seg;
1894         }
1895
1896         /*
1897          * Record index of the next RX descriptor to probe.
1898          */
1899         rxq->rx_tail = rx_id;
1900
1901         /*
1902          * If the number of free RX descriptors is greater than the RX free
1903          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1904          * register.
1905          * Update the RDT with the value of the last processed RX descriptor
1906          * minus 1, to guarantee that the RDT register is never equal to the
1907          * RDH register, which creates a "full" ring situation from the
1908          * hardware point of view...
1909          */
1910         if (!bulk_alloc && nb_hold > rxq->rx_free_thresh) {
1911                 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1912                            "nb_hold=%u nb_rx=%u",
1913                            rxq->port_id, rxq->queue_id, rx_id, nb_hold, nb_rx);
1914
1915                 rte_wmb();
1916                 txgbe_set32_relaxed(rxq->rdt_reg_addr, prev_id);
1917                 nb_hold = 0;
1918         }
1919
1920         rxq->nb_rx_hold = nb_hold;
1921         return nb_rx;
1922 }
1923
1924 uint16_t
1925 txgbe_recv_pkts_lro_single_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1926                                  uint16_t nb_pkts)
1927 {
1928         return txgbe_recv_pkts_lro(rx_queue, rx_pkts, nb_pkts, false);
1929 }
1930
1931 uint16_t
1932 txgbe_recv_pkts_lro_bulk_alloc(void *rx_queue, struct rte_mbuf **rx_pkts,
1933                                uint16_t nb_pkts)
1934 {
1935         return txgbe_recv_pkts_lro(rx_queue, rx_pkts, nb_pkts, true);
1936 }
1937
1938 uint64_t
1939 txgbe_get_rx_queue_offloads(struct rte_eth_dev *dev __rte_unused)
1940 {
1941         return DEV_RX_OFFLOAD_VLAN_STRIP;
1942 }
1943
1944 uint64_t
1945 txgbe_get_rx_port_offloads(struct rte_eth_dev *dev)
1946 {
1947         uint64_t offloads;
1948         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1949         struct rte_eth_dev_sriov *sriov = &RTE_ETH_DEV_SRIOV(dev);
1950
1951         offloads = DEV_RX_OFFLOAD_IPV4_CKSUM  |
1952                    DEV_RX_OFFLOAD_UDP_CKSUM   |
1953                    DEV_RX_OFFLOAD_TCP_CKSUM   |
1954                    DEV_RX_OFFLOAD_KEEP_CRC    |
1955                    DEV_RX_OFFLOAD_JUMBO_FRAME |
1956                    DEV_RX_OFFLOAD_VLAN_FILTER |
1957                    DEV_RX_OFFLOAD_RSS_HASH |
1958                    DEV_RX_OFFLOAD_SCATTER;
1959
1960         if (!txgbe_is_vf(dev))
1961                 offloads |= (DEV_RX_OFFLOAD_VLAN_FILTER |
1962                              DEV_RX_OFFLOAD_QINQ_STRIP |
1963                              DEV_RX_OFFLOAD_VLAN_EXTEND);
1964
1965         /*
1966          * RSC is only supported by PF devices in a non-SR-IOV
1967          * mode.
1968          */
1969         if (hw->mac.type == txgbe_mac_raptor && !sriov->active)
1970                 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
1971
1972         if (hw->mac.type == txgbe_mac_raptor)
1973                 offloads |= DEV_RX_OFFLOAD_MACSEC_STRIP;
1974
1975         offloads |= DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM;
1976
1977 #ifdef RTE_LIB_SECURITY
1978         if (dev->security_ctx)
1979                 offloads |= DEV_RX_OFFLOAD_SECURITY;
1980 #endif
1981
1982         return offloads;
1983 }
1984
1985 static void __rte_cold
1986 txgbe_tx_queue_release_mbufs(struct txgbe_tx_queue *txq)
1987 {
1988         unsigned int i;
1989
1990         if (txq->sw_ring != NULL) {
1991                 for (i = 0; i < txq->nb_tx_desc; i++) {
1992                         if (txq->sw_ring[i].mbuf != NULL) {
1993                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1994                                 txq->sw_ring[i].mbuf = NULL;
1995                         }
1996                 }
1997         }
1998 }
1999
2000 static int
2001 txgbe_tx_done_cleanup_full(struct txgbe_tx_queue *txq, uint32_t free_cnt)
2002 {
2003         struct txgbe_tx_entry *swr_ring = txq->sw_ring;
2004         uint16_t i, tx_last, tx_id;
2005         uint16_t nb_tx_free_last;
2006         uint16_t nb_tx_to_clean;
2007         uint32_t pkt_cnt;
2008
2009         /* Start free mbuf from the next of tx_tail */
2010         tx_last = txq->tx_tail;
2011         tx_id  = swr_ring[tx_last].next_id;
2012
2013         if (txq->nb_tx_free == 0 && txgbe_xmit_cleanup(txq))
2014                 return 0;
2015
2016         nb_tx_to_clean = txq->nb_tx_free;
2017         nb_tx_free_last = txq->nb_tx_free;
2018         if (!free_cnt)
2019                 free_cnt = txq->nb_tx_desc;
2020
2021         /* Loop through swr_ring to count the amount of
2022          * freeable mubfs and packets.
2023          */
2024         for (pkt_cnt = 0; pkt_cnt < free_cnt; ) {
2025                 for (i = 0; i < nb_tx_to_clean &&
2026                         pkt_cnt < free_cnt &&
2027                         tx_id != tx_last; i++) {
2028                         if (swr_ring[tx_id].mbuf != NULL) {
2029                                 rte_pktmbuf_free_seg(swr_ring[tx_id].mbuf);
2030                                 swr_ring[tx_id].mbuf = NULL;
2031
2032                                 /*
2033                                  * last segment in the packet,
2034                                  * increment packet count
2035                                  */
2036                                 pkt_cnt += (swr_ring[tx_id].last_id == tx_id);
2037                         }
2038
2039                         tx_id = swr_ring[tx_id].next_id;
2040                 }
2041
2042                 if (pkt_cnt < free_cnt) {
2043                         if (txgbe_xmit_cleanup(txq))
2044                                 break;
2045
2046                         nb_tx_to_clean = txq->nb_tx_free - nb_tx_free_last;
2047                         nb_tx_free_last = txq->nb_tx_free;
2048                 }
2049         }
2050
2051         return (int)pkt_cnt;
2052 }
2053
2054 static int
2055 txgbe_tx_done_cleanup_simple(struct txgbe_tx_queue *txq,
2056                         uint32_t free_cnt)
2057 {
2058         int i, n, cnt;
2059
2060         if (free_cnt == 0 || free_cnt > txq->nb_tx_desc)
2061                 free_cnt = txq->nb_tx_desc;
2062
2063         cnt = free_cnt - free_cnt % txq->tx_free_thresh;
2064
2065         for (i = 0; i < cnt; i += n) {
2066                 if (txq->nb_tx_desc - txq->nb_tx_free < txq->tx_free_thresh)
2067                         break;
2068
2069                 n = txgbe_tx_free_bufs(txq);
2070
2071                 if (n == 0)
2072                         break;
2073         }
2074
2075         return i;
2076 }
2077
2078 int
2079 txgbe_dev_tx_done_cleanup(void *tx_queue, uint32_t free_cnt)
2080 {
2081         struct txgbe_tx_queue *txq = (struct txgbe_tx_queue *)tx_queue;
2082         if (txq->offloads == 0 &&
2083 #ifdef RTE_LIB_SECURITY
2084                 !(txq->using_ipsec) &&
2085 #endif
2086                 txq->tx_free_thresh >= RTE_PMD_TXGBE_TX_MAX_BURST)
2087                 return txgbe_tx_done_cleanup_simple(txq, free_cnt);
2088
2089         return txgbe_tx_done_cleanup_full(txq, free_cnt);
2090 }
2091
2092 static void __rte_cold
2093 txgbe_tx_free_swring(struct txgbe_tx_queue *txq)
2094 {
2095         if (txq != NULL &&
2096             txq->sw_ring != NULL)
2097                 rte_free(txq->sw_ring);
2098 }
2099
2100 static void __rte_cold
2101 txgbe_tx_queue_release(struct txgbe_tx_queue *txq)
2102 {
2103         if (txq != NULL && txq->ops != NULL) {
2104                 txq->ops->release_mbufs(txq);
2105                 txq->ops->free_swring(txq);
2106                 rte_free(txq);
2107         }
2108 }
2109
2110 void __rte_cold
2111 txgbe_dev_tx_queue_release(void *txq)
2112 {
2113         txgbe_tx_queue_release(txq);
2114 }
2115
2116 /* (Re)set dynamic txgbe_tx_queue fields to defaults */
2117 static void __rte_cold
2118 txgbe_reset_tx_queue(struct txgbe_tx_queue *txq)
2119 {
2120         static const struct txgbe_tx_desc zeroed_desc = {0};
2121         struct txgbe_tx_entry *txe = txq->sw_ring;
2122         uint16_t prev, i;
2123
2124         /* Zero out HW ring memory */
2125         for (i = 0; i < txq->nb_tx_desc; i++)
2126                 txq->tx_ring[i] = zeroed_desc;
2127
2128         /* Initialize SW ring entries */
2129         prev = (uint16_t)(txq->nb_tx_desc - 1);
2130         for (i = 0; i < txq->nb_tx_desc; i++) {
2131                 volatile struct txgbe_tx_desc *txd = &txq->tx_ring[i];
2132
2133                 txd->dw3 = rte_cpu_to_le_32(TXGBE_TXD_DD);
2134                 txe[i].mbuf = NULL;
2135                 txe[i].last_id = i;
2136                 txe[prev].next_id = i;
2137                 prev = i;
2138         }
2139
2140         txq->tx_next_dd = (uint16_t)(txq->tx_free_thresh - 1);
2141         txq->tx_tail = 0;
2142
2143         /*
2144          * Always allow 1 descriptor to be un-allocated to avoid
2145          * a H/W race condition
2146          */
2147         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
2148         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
2149         txq->ctx_curr = 0;
2150         memset((void *)&txq->ctx_cache, 0,
2151                 TXGBE_CTX_NUM * sizeof(struct txgbe_ctx_info));
2152 }
2153
2154 static const struct txgbe_txq_ops def_txq_ops = {
2155         .release_mbufs = txgbe_tx_queue_release_mbufs,
2156         .free_swring = txgbe_tx_free_swring,
2157         .reset = txgbe_reset_tx_queue,
2158 };
2159
2160 /* Takes an ethdev and a queue and sets up the tx function to be used based on
2161  * the queue parameters. Used in tx_queue_setup by primary process and then
2162  * in dev_init by secondary process when attaching to an existing ethdev.
2163  */
2164 void __rte_cold
2165 txgbe_set_tx_function(struct rte_eth_dev *dev, struct txgbe_tx_queue *txq)
2166 {
2167         /* Use a simple Tx queue (no offloads, no multi segs) if possible */
2168         if (txq->offloads == 0 &&
2169 #ifdef RTE_LIB_SECURITY
2170                         !(txq->using_ipsec) &&
2171 #endif
2172                         txq->tx_free_thresh >= RTE_PMD_TXGBE_TX_MAX_BURST) {
2173                 PMD_INIT_LOG(DEBUG, "Using simple tx code path");
2174                 dev->tx_pkt_burst = txgbe_xmit_pkts_simple;
2175                 dev->tx_pkt_prepare = NULL;
2176         } else {
2177                 PMD_INIT_LOG(DEBUG, "Using full-featured tx code path");
2178                 PMD_INIT_LOG(DEBUG,
2179                                 " - offloads = 0x%" PRIx64,
2180                                 txq->offloads);
2181                 PMD_INIT_LOG(DEBUG,
2182                                 " - tx_free_thresh = %lu [RTE_PMD_TXGBE_TX_MAX_BURST=%lu]",
2183                                 (unsigned long)txq->tx_free_thresh,
2184                                 (unsigned long)RTE_PMD_TXGBE_TX_MAX_BURST);
2185                 dev->tx_pkt_burst = txgbe_xmit_pkts;
2186                 dev->tx_pkt_prepare = txgbe_prep_pkts;
2187         }
2188 }
2189
2190 uint64_t
2191 txgbe_get_tx_queue_offloads(struct rte_eth_dev *dev)
2192 {
2193         RTE_SET_USED(dev);
2194
2195         return 0;
2196 }
2197
2198 uint64_t
2199 txgbe_get_tx_port_offloads(struct rte_eth_dev *dev)
2200 {
2201         uint64_t tx_offload_capa;
2202
2203         tx_offload_capa =
2204                 DEV_TX_OFFLOAD_VLAN_INSERT |
2205                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2206                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2207                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2208                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
2209                 DEV_TX_OFFLOAD_TCP_TSO     |
2210                 DEV_TX_OFFLOAD_UDP_TSO     |
2211                 DEV_TX_OFFLOAD_UDP_TNL_TSO      |
2212                 DEV_TX_OFFLOAD_IP_TNL_TSO       |
2213                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO    |
2214                 DEV_TX_OFFLOAD_GRE_TNL_TSO      |
2215                 DEV_TX_OFFLOAD_IPIP_TNL_TSO     |
2216                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO   |
2217                 DEV_TX_OFFLOAD_MULTI_SEGS;
2218
2219         if (!txgbe_is_vf(dev))
2220                 tx_offload_capa |= DEV_TX_OFFLOAD_QINQ_INSERT;
2221
2222         tx_offload_capa |= DEV_TX_OFFLOAD_MACSEC_INSERT;
2223
2224         tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2225                            DEV_TX_OFFLOAD_OUTER_UDP_CKSUM;
2226
2227 #ifdef RTE_LIB_SECURITY
2228         if (dev->security_ctx)
2229                 tx_offload_capa |= DEV_TX_OFFLOAD_SECURITY;
2230 #endif
2231         return tx_offload_capa;
2232 }
2233
2234 int __rte_cold
2235 txgbe_dev_tx_queue_setup(struct rte_eth_dev *dev,
2236                          uint16_t queue_idx,
2237                          uint16_t nb_desc,
2238                          unsigned int socket_id,
2239                          const struct rte_eth_txconf *tx_conf)
2240 {
2241         const struct rte_memzone *tz;
2242         struct txgbe_tx_queue *txq;
2243         struct txgbe_hw     *hw;
2244         uint16_t tx_free_thresh;
2245         uint64_t offloads;
2246
2247         PMD_INIT_FUNC_TRACE();
2248         hw = TXGBE_DEV_HW(dev);
2249
2250         offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
2251
2252         /*
2253          * Validate number of transmit descriptors.
2254          * It must not exceed hardware maximum, and must be multiple
2255          * of TXGBE_ALIGN.
2256          */
2257         if (nb_desc % TXGBE_TXD_ALIGN != 0 ||
2258             nb_desc > TXGBE_RING_DESC_MAX ||
2259             nb_desc < TXGBE_RING_DESC_MIN) {
2260                 return -EINVAL;
2261         }
2262
2263         /*
2264          * The TX descriptor ring will be cleaned after txq->tx_free_thresh
2265          * descriptors are used or if the number of descriptors required
2266          * to transmit a packet is greater than the number of free TX
2267          * descriptors.
2268          * One descriptor in the TX ring is used as a sentinel to avoid a
2269          * H/W race condition, hence the maximum threshold constraints.
2270          * When set to zero use default values.
2271          */
2272         tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
2273                         tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH);
2274         if (tx_free_thresh >= (nb_desc - 3)) {
2275                 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the number of "
2276                              "TX descriptors minus 3. (tx_free_thresh=%u "
2277                              "port=%d queue=%d)",
2278                              (unsigned int)tx_free_thresh,
2279                              (int)dev->data->port_id, (int)queue_idx);
2280                 return -(EINVAL);
2281         }
2282
2283         if ((nb_desc % tx_free_thresh) != 0) {
2284                 PMD_INIT_LOG(ERR, "tx_free_thresh must be a divisor of the "
2285                              "number of TX descriptors. (tx_free_thresh=%u "
2286                              "port=%d queue=%d)", (unsigned int)tx_free_thresh,
2287                              (int)dev->data->port_id, (int)queue_idx);
2288                 return -(EINVAL);
2289         }
2290
2291         /* Free memory prior to re-allocation if needed... */
2292         if (dev->data->tx_queues[queue_idx] != NULL) {
2293                 txgbe_tx_queue_release(dev->data->tx_queues[queue_idx]);
2294                 dev->data->tx_queues[queue_idx] = NULL;
2295         }
2296
2297         /* First allocate the tx queue data structure */
2298         txq = rte_zmalloc_socket("ethdev TX queue",
2299                                  sizeof(struct txgbe_tx_queue),
2300                                  RTE_CACHE_LINE_SIZE, socket_id);
2301         if (txq == NULL)
2302                 return -ENOMEM;
2303
2304         /*
2305          * Allocate TX ring hardware descriptors. A memzone large enough to
2306          * handle the maximum ring size is allocated in order to allow for
2307          * resizing in later calls to the queue setup function.
2308          */
2309         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
2310                         sizeof(struct txgbe_tx_desc) * TXGBE_RING_DESC_MAX,
2311                         TXGBE_ALIGN, socket_id);
2312         if (tz == NULL) {
2313                 txgbe_tx_queue_release(txq);
2314                 return -ENOMEM;
2315         }
2316
2317         txq->nb_tx_desc = nb_desc;
2318         txq->tx_free_thresh = tx_free_thresh;
2319         txq->pthresh = tx_conf->tx_thresh.pthresh;
2320         txq->hthresh = tx_conf->tx_thresh.hthresh;
2321         txq->wthresh = tx_conf->tx_thresh.wthresh;
2322         txq->queue_id = queue_idx;
2323         txq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
2324                 queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
2325         txq->port_id = dev->data->port_id;
2326         txq->offloads = offloads;
2327         txq->ops = &def_txq_ops;
2328         txq->tx_deferred_start = tx_conf->tx_deferred_start;
2329 #ifdef RTE_LIB_SECURITY
2330         txq->using_ipsec = !!(dev->data->dev_conf.txmode.offloads &
2331                         DEV_TX_OFFLOAD_SECURITY);
2332 #endif
2333
2334         /* Modification to set tail pointer for virtual function
2335          * if vf is detected.
2336          */
2337         if (hw->mac.type == txgbe_mac_raptor_vf) {
2338                 txq->tdt_reg_addr = TXGBE_REG_ADDR(hw, TXGBE_TXWP(queue_idx));
2339                 txq->tdc_reg_addr = TXGBE_REG_ADDR(hw, TXGBE_TXCFG(queue_idx));
2340         } else {
2341                 txq->tdt_reg_addr = TXGBE_REG_ADDR(hw,
2342                                                 TXGBE_TXWP(txq->reg_idx));
2343                 txq->tdc_reg_addr = TXGBE_REG_ADDR(hw,
2344                                                 TXGBE_TXCFG(txq->reg_idx));
2345         }
2346
2347         txq->tx_ring_phys_addr = TMZ_PADDR(tz);
2348         txq->tx_ring = (struct txgbe_tx_desc *)TMZ_VADDR(tz);
2349
2350         /* Allocate software ring */
2351         txq->sw_ring = rte_zmalloc_socket("txq->sw_ring",
2352                                 sizeof(struct txgbe_tx_entry) * nb_desc,
2353                                 RTE_CACHE_LINE_SIZE, socket_id);
2354         if (txq->sw_ring == NULL) {
2355                 txgbe_tx_queue_release(txq);
2356                 return -ENOMEM;
2357         }
2358         PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%" PRIx64,
2359                      txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
2360
2361         /* set up scalar TX function as appropriate */
2362         txgbe_set_tx_function(dev, txq);
2363
2364         txq->ops->reset(txq);
2365
2366         dev->data->tx_queues[queue_idx] = txq;
2367
2368         return 0;
2369 }
2370
2371 /**
2372  * txgbe_free_sc_cluster - free the not-yet-completed scattered cluster
2373  *
2374  * The "next" pointer of the last segment of (not-yet-completed) RSC clusters
2375  * in the sw_rsc_ring is not set to NULL but rather points to the next
2376  * mbuf of this RSC aggregation (that has not been completed yet and still
2377  * resides on the HW ring). So, instead of calling for rte_pktmbuf_free() we
2378  * will just free first "nb_segs" segments of the cluster explicitly by calling
2379  * an rte_pktmbuf_free_seg().
2380  *
2381  * @m scattered cluster head
2382  */
2383 static void __rte_cold
2384 txgbe_free_sc_cluster(struct rte_mbuf *m)
2385 {
2386         uint16_t i, nb_segs = m->nb_segs;
2387         struct rte_mbuf *next_seg;
2388
2389         for (i = 0; i < nb_segs; i++) {
2390                 next_seg = m->next;
2391                 rte_pktmbuf_free_seg(m);
2392                 m = next_seg;
2393         }
2394 }
2395
2396 static void __rte_cold
2397 txgbe_rx_queue_release_mbufs(struct txgbe_rx_queue *rxq)
2398 {
2399         unsigned int i;
2400
2401         if (rxq->sw_ring != NULL) {
2402                 for (i = 0; i < rxq->nb_rx_desc; i++) {
2403                         if (rxq->sw_ring[i].mbuf != NULL) {
2404                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
2405                                 rxq->sw_ring[i].mbuf = NULL;
2406                         }
2407                 }
2408                 if (rxq->rx_nb_avail) {
2409                         for (i = 0; i < rxq->rx_nb_avail; ++i) {
2410                                 struct rte_mbuf *mb;
2411
2412                                 mb = rxq->rx_stage[rxq->rx_next_avail + i];
2413                                 rte_pktmbuf_free_seg(mb);
2414                         }
2415                         rxq->rx_nb_avail = 0;
2416                 }
2417         }
2418
2419         if (rxq->sw_sc_ring)
2420                 for (i = 0; i < rxq->nb_rx_desc; i++)
2421                         if (rxq->sw_sc_ring[i].fbuf) {
2422                                 txgbe_free_sc_cluster(rxq->sw_sc_ring[i].fbuf);
2423                                 rxq->sw_sc_ring[i].fbuf = NULL;
2424                         }
2425 }
2426
2427 static void __rte_cold
2428 txgbe_rx_queue_release(struct txgbe_rx_queue *rxq)
2429 {
2430         if (rxq != NULL) {
2431                 txgbe_rx_queue_release_mbufs(rxq);
2432                 rte_free(rxq->sw_ring);
2433                 rte_free(rxq->sw_sc_ring);
2434                 rte_free(rxq);
2435         }
2436 }
2437
2438 void __rte_cold
2439 txgbe_dev_rx_queue_release(void *rxq)
2440 {
2441         txgbe_rx_queue_release(rxq);
2442 }
2443
2444 /*
2445  * Check if Rx Burst Bulk Alloc function can be used.
2446  * Return
2447  *        0: the preconditions are satisfied and the bulk allocation function
2448  *           can be used.
2449  *  -EINVAL: the preconditions are NOT satisfied and the default Rx burst
2450  *           function must be used.
2451  */
2452 static inline int __rte_cold
2453 check_rx_burst_bulk_alloc_preconditions(struct txgbe_rx_queue *rxq)
2454 {
2455         int ret = 0;
2456
2457         /*
2458          * Make sure the following pre-conditions are satisfied:
2459          *   rxq->rx_free_thresh >= RTE_PMD_TXGBE_RX_MAX_BURST
2460          *   rxq->rx_free_thresh < rxq->nb_rx_desc
2461          *   (rxq->nb_rx_desc % rxq->rx_free_thresh) == 0
2462          * Scattered packets are not supported.  This should be checked
2463          * outside of this function.
2464          */
2465         if (!(rxq->rx_free_thresh >= RTE_PMD_TXGBE_RX_MAX_BURST)) {
2466                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
2467                              "rxq->rx_free_thresh=%d, "
2468                              "RTE_PMD_TXGBE_RX_MAX_BURST=%d",
2469                              rxq->rx_free_thresh, RTE_PMD_TXGBE_RX_MAX_BURST);
2470                 ret = -EINVAL;
2471         } else if (!(rxq->rx_free_thresh < rxq->nb_rx_desc)) {
2472                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
2473                              "rxq->rx_free_thresh=%d, "
2474                              "rxq->nb_rx_desc=%d",
2475                              rxq->rx_free_thresh, rxq->nb_rx_desc);
2476                 ret = -EINVAL;
2477         } else if (!((rxq->nb_rx_desc % rxq->rx_free_thresh) == 0)) {
2478                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
2479                              "rxq->nb_rx_desc=%d, "
2480                              "rxq->rx_free_thresh=%d",
2481                              rxq->nb_rx_desc, rxq->rx_free_thresh);
2482                 ret = -EINVAL;
2483         }
2484
2485         return ret;
2486 }
2487
2488 /* Reset dynamic txgbe_rx_queue fields back to defaults */
2489 static void __rte_cold
2490 txgbe_reset_rx_queue(struct txgbe_adapter *adapter, struct txgbe_rx_queue *rxq)
2491 {
2492         static const struct txgbe_rx_desc zeroed_desc = {
2493                                                 {{0}, {0} }, {{0}, {0} } };
2494         unsigned int i;
2495         uint16_t len = rxq->nb_rx_desc;
2496
2497         /*
2498          * By default, the Rx queue setup function allocates enough memory for
2499          * TXGBE_RING_DESC_MAX.  The Rx Burst bulk allocation function requires
2500          * extra memory at the end of the descriptor ring to be zero'd out.
2501          */
2502         if (adapter->rx_bulk_alloc_allowed)
2503                 /* zero out extra memory */
2504                 len += RTE_PMD_TXGBE_RX_MAX_BURST;
2505
2506         /*
2507          * Zero out HW ring memory. Zero out extra memory at the end of
2508          * the H/W ring so look-ahead logic in Rx Burst bulk alloc function
2509          * reads extra memory as zeros.
2510          */
2511         for (i = 0; i < len; i++)
2512                 rxq->rx_ring[i] = zeroed_desc;
2513
2514         /*
2515          * initialize extra software ring entries. Space for these extra
2516          * entries is always allocated
2517          */
2518         memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
2519         for (i = rxq->nb_rx_desc; i < len; ++i)
2520                 rxq->sw_ring[i].mbuf = &rxq->fake_mbuf;
2521
2522         rxq->rx_nb_avail = 0;
2523         rxq->rx_next_avail = 0;
2524         rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
2525         rxq->rx_tail = 0;
2526         rxq->nb_rx_hold = 0;
2527         rxq->pkt_first_seg = NULL;
2528         rxq->pkt_last_seg = NULL;
2529 }
2530
2531 int __rte_cold
2532 txgbe_dev_rx_queue_setup(struct rte_eth_dev *dev,
2533                          uint16_t queue_idx,
2534                          uint16_t nb_desc,
2535                          unsigned int socket_id,
2536                          const struct rte_eth_rxconf *rx_conf,
2537                          struct rte_mempool *mp)
2538 {
2539         const struct rte_memzone *rz;
2540         struct txgbe_rx_queue *rxq;
2541         struct txgbe_hw     *hw;
2542         uint16_t len;
2543         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
2544         uint64_t offloads;
2545
2546         PMD_INIT_FUNC_TRACE();
2547         hw = TXGBE_DEV_HW(dev);
2548
2549         offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
2550
2551         /*
2552          * Validate number of receive descriptors.
2553          * It must not exceed hardware maximum, and must be multiple
2554          * of TXGBE_ALIGN.
2555          */
2556         if (nb_desc % TXGBE_RXD_ALIGN != 0 ||
2557                         nb_desc > TXGBE_RING_DESC_MAX ||
2558                         nb_desc < TXGBE_RING_DESC_MIN) {
2559                 return -EINVAL;
2560         }
2561
2562         /* Free memory prior to re-allocation if needed... */
2563         if (dev->data->rx_queues[queue_idx] != NULL) {
2564                 txgbe_rx_queue_release(dev->data->rx_queues[queue_idx]);
2565                 dev->data->rx_queues[queue_idx] = NULL;
2566         }
2567
2568         /* First allocate the rx queue data structure */
2569         rxq = rte_zmalloc_socket("ethdev RX queue",
2570                                  sizeof(struct txgbe_rx_queue),
2571                                  RTE_CACHE_LINE_SIZE, socket_id);
2572         if (rxq == NULL)
2573                 return -ENOMEM;
2574         rxq->mb_pool = mp;
2575         rxq->nb_rx_desc = nb_desc;
2576         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
2577         rxq->queue_id = queue_idx;
2578         rxq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
2579                 queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
2580         rxq->port_id = dev->data->port_id;
2581         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
2582                 rxq->crc_len = RTE_ETHER_CRC_LEN;
2583         else
2584                 rxq->crc_len = 0;
2585         rxq->drop_en = rx_conf->rx_drop_en;
2586         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
2587         rxq->offloads = offloads;
2588
2589         /*
2590          * The packet type in RX descriptor is different for different NICs.
2591          * So set different masks for different NICs.
2592          */
2593         rxq->pkt_type_mask = TXGBE_PTID_MASK;
2594
2595         /*
2596          * Allocate RX ring hardware descriptors. A memzone large enough to
2597          * handle the maximum ring size is allocated in order to allow for
2598          * resizing in later calls to the queue setup function.
2599          */
2600         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
2601                                       RX_RING_SZ, TXGBE_ALIGN, socket_id);
2602         if (rz == NULL) {
2603                 txgbe_rx_queue_release(rxq);
2604                 return -ENOMEM;
2605         }
2606
2607         /*
2608          * Zero init all the descriptors in the ring.
2609          */
2610         memset(rz->addr, 0, RX_RING_SZ);
2611
2612         /*
2613          * Modified to setup VFRDT for Virtual Function
2614          */
2615         if (hw->mac.type == txgbe_mac_raptor_vf) {
2616                 rxq->rdt_reg_addr =
2617                         TXGBE_REG_ADDR(hw, TXGBE_RXWP(queue_idx));
2618                 rxq->rdh_reg_addr =
2619                         TXGBE_REG_ADDR(hw, TXGBE_RXRP(queue_idx));
2620         } else {
2621                 rxq->rdt_reg_addr =
2622                         TXGBE_REG_ADDR(hw, TXGBE_RXWP(rxq->reg_idx));
2623                 rxq->rdh_reg_addr =
2624                         TXGBE_REG_ADDR(hw, TXGBE_RXRP(rxq->reg_idx));
2625         }
2626
2627         rxq->rx_ring_phys_addr = TMZ_PADDR(rz);
2628         rxq->rx_ring = (struct txgbe_rx_desc *)TMZ_VADDR(rz);
2629
2630         /*
2631          * Certain constraints must be met in order to use the bulk buffer
2632          * allocation Rx burst function. If any of Rx queues doesn't meet them
2633          * the feature should be disabled for the whole port.
2634          */
2635         if (check_rx_burst_bulk_alloc_preconditions(rxq)) {
2636                 PMD_INIT_LOG(DEBUG, "queue[%d] doesn't meet Rx Bulk Alloc "
2637                                     "preconditions - canceling the feature for "
2638                                     "the whole port[%d]",
2639                              rxq->queue_id, rxq->port_id);
2640                 adapter->rx_bulk_alloc_allowed = false;
2641         }
2642
2643         /*
2644          * Allocate software ring. Allow for space at the end of the
2645          * S/W ring to make sure look-ahead logic in bulk alloc Rx burst
2646          * function does not access an invalid memory region.
2647          */
2648         len = nb_desc;
2649         if (adapter->rx_bulk_alloc_allowed)
2650                 len += RTE_PMD_TXGBE_RX_MAX_BURST;
2651
2652         rxq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
2653                                           sizeof(struct txgbe_rx_entry) * len,
2654                                           RTE_CACHE_LINE_SIZE, socket_id);
2655         if (!rxq->sw_ring) {
2656                 txgbe_rx_queue_release(rxq);
2657                 return -ENOMEM;
2658         }
2659
2660         /*
2661          * Always allocate even if it's not going to be needed in order to
2662          * simplify the code.
2663          *
2664          * This ring is used in LRO and Scattered Rx cases and Scattered Rx may
2665          * be requested in txgbe_dev_rx_init(), which is called later from
2666          * dev_start() flow.
2667          */
2668         rxq->sw_sc_ring =
2669                 rte_zmalloc_socket("rxq->sw_sc_ring",
2670                                   sizeof(struct txgbe_scattered_rx_entry) * len,
2671                                   RTE_CACHE_LINE_SIZE, socket_id);
2672         if (!rxq->sw_sc_ring) {
2673                 txgbe_rx_queue_release(rxq);
2674                 return -ENOMEM;
2675         }
2676
2677         PMD_INIT_LOG(DEBUG, "sw_ring=%p sw_sc_ring=%p hw_ring=%p "
2678                             "dma_addr=0x%" PRIx64,
2679                      rxq->sw_ring, rxq->sw_sc_ring, rxq->rx_ring,
2680                      rxq->rx_ring_phys_addr);
2681
2682         dev->data->rx_queues[queue_idx] = rxq;
2683
2684         txgbe_reset_rx_queue(adapter, rxq);
2685
2686         return 0;
2687 }
2688
2689 uint32_t
2690 txgbe_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
2691 {
2692 #define TXGBE_RXQ_SCAN_INTERVAL 4
2693         volatile struct txgbe_rx_desc *rxdp;
2694         struct txgbe_rx_queue *rxq;
2695         uint32_t desc = 0;
2696
2697         rxq = dev->data->rx_queues[rx_queue_id];
2698         rxdp = &rxq->rx_ring[rxq->rx_tail];
2699
2700         while ((desc < rxq->nb_rx_desc) &&
2701                 (rxdp->qw1.lo.status &
2702                         rte_cpu_to_le_32(TXGBE_RXD_STAT_DD))) {
2703                 desc += TXGBE_RXQ_SCAN_INTERVAL;
2704                 rxdp += TXGBE_RXQ_SCAN_INTERVAL;
2705                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
2706                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
2707                                 desc - rxq->nb_rx_desc]);
2708         }
2709
2710         return desc;
2711 }
2712
2713 int
2714 txgbe_dev_rx_descriptor_status(void *rx_queue, uint16_t offset)
2715 {
2716         struct txgbe_rx_queue *rxq = rx_queue;
2717         volatile uint32_t *status;
2718         uint32_t nb_hold, desc;
2719
2720         if (unlikely(offset >= rxq->nb_rx_desc))
2721                 return -EINVAL;
2722
2723         nb_hold = rxq->nb_rx_hold;
2724         if (offset >= rxq->nb_rx_desc - nb_hold)
2725                 return RTE_ETH_RX_DESC_UNAVAIL;
2726
2727         desc = rxq->rx_tail + offset;
2728         if (desc >= rxq->nb_rx_desc)
2729                 desc -= rxq->nb_rx_desc;
2730
2731         status = &rxq->rx_ring[desc].qw1.lo.status;
2732         if (*status & rte_cpu_to_le_32(TXGBE_RXD_STAT_DD))
2733                 return RTE_ETH_RX_DESC_DONE;
2734
2735         return RTE_ETH_RX_DESC_AVAIL;
2736 }
2737
2738 int
2739 txgbe_dev_tx_descriptor_status(void *tx_queue, uint16_t offset)
2740 {
2741         struct txgbe_tx_queue *txq = tx_queue;
2742         volatile uint32_t *status;
2743         uint32_t desc;
2744
2745         if (unlikely(offset >= txq->nb_tx_desc))
2746                 return -EINVAL;
2747
2748         desc = txq->tx_tail + offset;
2749         if (desc >= txq->nb_tx_desc) {
2750                 desc -= txq->nb_tx_desc;
2751                 if (desc >= txq->nb_tx_desc)
2752                         desc -= txq->nb_tx_desc;
2753         }
2754
2755         status = &txq->tx_ring[desc].dw3;
2756         if (*status & rte_cpu_to_le_32(TXGBE_TXD_DD))
2757                 return RTE_ETH_TX_DESC_DONE;
2758
2759         return RTE_ETH_TX_DESC_FULL;
2760 }
2761
2762 void __rte_cold
2763 txgbe_dev_clear_queues(struct rte_eth_dev *dev)
2764 {
2765         unsigned int i;
2766         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
2767
2768         PMD_INIT_FUNC_TRACE();
2769
2770         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2771                 struct txgbe_tx_queue *txq = dev->data->tx_queues[i];
2772
2773                 if (txq != NULL) {
2774                         txq->ops->release_mbufs(txq);
2775                         txq->ops->reset(txq);
2776                 }
2777         }
2778
2779         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2780                 struct txgbe_rx_queue *rxq = dev->data->rx_queues[i];
2781
2782                 if (rxq != NULL) {
2783                         txgbe_rx_queue_release_mbufs(rxq);
2784                         txgbe_reset_rx_queue(adapter, rxq);
2785                 }
2786         }
2787 }
2788
2789 void
2790 txgbe_dev_free_queues(struct rte_eth_dev *dev)
2791 {
2792         unsigned int i;
2793
2794         PMD_INIT_FUNC_TRACE();
2795
2796         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2797                 txgbe_dev_rx_queue_release(dev->data->rx_queues[i]);
2798                 dev->data->rx_queues[i] = NULL;
2799         }
2800         dev->data->nb_rx_queues = 0;
2801
2802         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2803                 txgbe_dev_tx_queue_release(dev->data->tx_queues[i]);
2804                 dev->data->tx_queues[i] = NULL;
2805         }
2806         dev->data->nb_tx_queues = 0;
2807 }
2808
2809 /**
2810  * Receive Side Scaling (RSS)
2811  *
2812  * Principles:
2813  * The source and destination IP addresses of the IP header and the source
2814  * and destination ports of TCP/UDP headers, if any, of received packets are
2815  * hashed against a configurable random key to compute a 32-bit RSS hash result.
2816  * The seven (7) LSBs of the 32-bit hash result are used as an index into a
2817  * 128-entry redirection table (RETA).  Each entry of the RETA provides a 3-bit
2818  * RSS output index which is used as the RX queue index where to store the
2819  * received packets.
2820  * The following output is supplied in the RX write-back descriptor:
2821  *     - 32-bit result of the Microsoft RSS hash function,
2822  *     - 4-bit RSS type field.
2823  */
2824
2825 /*
2826  * Used as the default key.
2827  */
2828 static uint8_t rss_intel_key[40] = {
2829         0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
2830         0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
2831         0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
2832         0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
2833         0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA,
2834 };
2835
2836 static void
2837 txgbe_rss_disable(struct rte_eth_dev *dev)
2838 {
2839         struct txgbe_hw *hw;
2840
2841         hw = TXGBE_DEV_HW(dev);
2842         if (hw->mac.type == txgbe_mac_raptor_vf)
2843                 wr32m(hw, TXGBE_VFPLCFG, TXGBE_VFPLCFG_RSSENA, 0);
2844         else
2845                 wr32m(hw, TXGBE_RACTL, TXGBE_RACTL_RSSENA, 0);
2846 }
2847
2848 int
2849 txgbe_dev_rss_hash_update(struct rte_eth_dev *dev,
2850                           struct rte_eth_rss_conf *rss_conf)
2851 {
2852         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2853         uint8_t  *hash_key;
2854         uint32_t mrqc;
2855         uint32_t rss_key;
2856         uint64_t rss_hf;
2857         uint16_t i;
2858
2859         if (!txgbe_rss_update_sp(hw->mac.type)) {
2860                 PMD_DRV_LOG(ERR, "RSS hash update is not supported on this "
2861                         "NIC.");
2862                 return -ENOTSUP;
2863         }
2864
2865         hash_key = rss_conf->rss_key;
2866         if (hash_key) {
2867                 /* Fill in RSS hash key */
2868                 for (i = 0; i < 10; i++) {
2869                         rss_key  = LS32(hash_key[(i * 4) + 0], 0, 0xFF);
2870                         rss_key |= LS32(hash_key[(i * 4) + 1], 8, 0xFF);
2871                         rss_key |= LS32(hash_key[(i * 4) + 2], 16, 0xFF);
2872                         rss_key |= LS32(hash_key[(i * 4) + 3], 24, 0xFF);
2873                         wr32at(hw, TXGBE_REG_RSSKEY, i, rss_key);
2874                 }
2875         }
2876
2877         /* Set configured hashing protocols */
2878         rss_hf = rss_conf->rss_hf & TXGBE_RSS_OFFLOAD_ALL;
2879         if (hw->mac.type == txgbe_mac_raptor_vf) {
2880                 mrqc = rd32(hw, TXGBE_VFPLCFG);
2881                 mrqc &= ~TXGBE_VFPLCFG_RSSMASK;
2882                 if (rss_hf & ETH_RSS_IPV4)
2883                         mrqc |= TXGBE_VFPLCFG_RSSIPV4;
2884                 if (rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
2885                         mrqc |= TXGBE_VFPLCFG_RSSIPV4TCP;
2886                 if (rss_hf & ETH_RSS_IPV6 ||
2887                     rss_hf & ETH_RSS_IPV6_EX)
2888                         mrqc |= TXGBE_VFPLCFG_RSSIPV6;
2889                 if (rss_hf & ETH_RSS_NONFRAG_IPV6_TCP ||
2890                     rss_hf & ETH_RSS_IPV6_TCP_EX)
2891                         mrqc |= TXGBE_VFPLCFG_RSSIPV6TCP;
2892                 if (rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
2893                         mrqc |= TXGBE_VFPLCFG_RSSIPV4UDP;
2894                 if (rss_hf & ETH_RSS_NONFRAG_IPV6_UDP ||
2895                     rss_hf & ETH_RSS_IPV6_UDP_EX)
2896                         mrqc |= TXGBE_VFPLCFG_RSSIPV6UDP;
2897
2898                 if (rss_hf)
2899                         mrqc |= TXGBE_VFPLCFG_RSSENA;
2900                 else
2901                         mrqc &= ~TXGBE_VFPLCFG_RSSENA;
2902
2903                 if (dev->data->nb_rx_queues > 3)
2904                         mrqc |= TXGBE_VFPLCFG_RSSHASH(2);
2905                 else if (dev->data->nb_rx_queues > 1)
2906                         mrqc |= TXGBE_VFPLCFG_RSSHASH(1);
2907
2908                 wr32(hw, TXGBE_VFPLCFG, mrqc);
2909         } else {
2910                 mrqc = rd32(hw, TXGBE_RACTL);
2911                 mrqc &= ~TXGBE_RACTL_RSSMASK;
2912                 if (rss_hf & ETH_RSS_IPV4)
2913                         mrqc |= TXGBE_RACTL_RSSIPV4;
2914                 if (rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
2915                         mrqc |= TXGBE_RACTL_RSSIPV4TCP;
2916                 if (rss_hf & ETH_RSS_IPV6 ||
2917                     rss_hf & ETH_RSS_IPV6_EX)
2918                         mrqc |= TXGBE_RACTL_RSSIPV6;
2919                 if (rss_hf & ETH_RSS_NONFRAG_IPV6_TCP ||
2920                     rss_hf & ETH_RSS_IPV6_TCP_EX)
2921                         mrqc |= TXGBE_RACTL_RSSIPV6TCP;
2922                 if (rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
2923                         mrqc |= TXGBE_RACTL_RSSIPV4UDP;
2924                 if (rss_hf & ETH_RSS_NONFRAG_IPV6_UDP ||
2925                     rss_hf & ETH_RSS_IPV6_UDP_EX)
2926                         mrqc |= TXGBE_RACTL_RSSIPV6UDP;
2927
2928                 if (rss_hf)
2929                         mrqc |= TXGBE_RACTL_RSSENA;
2930                 else
2931                         mrqc &= ~TXGBE_RACTL_RSSENA;
2932
2933                 wr32(hw, TXGBE_RACTL, mrqc);
2934         }
2935
2936         return 0;
2937 }
2938
2939 int
2940 txgbe_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
2941                             struct rte_eth_rss_conf *rss_conf)
2942 {
2943         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2944         uint8_t *hash_key;
2945         uint32_t mrqc;
2946         uint32_t rss_key;
2947         uint64_t rss_hf;
2948         uint16_t i;
2949
2950         hash_key = rss_conf->rss_key;
2951         if (hash_key) {
2952                 /* Return RSS hash key */
2953                 for (i = 0; i < 10; i++) {
2954                         rss_key = rd32at(hw, TXGBE_REG_RSSKEY, i);
2955                         hash_key[(i * 4) + 0] = RS32(rss_key, 0, 0xFF);
2956                         hash_key[(i * 4) + 1] = RS32(rss_key, 8, 0xFF);
2957                         hash_key[(i * 4) + 2] = RS32(rss_key, 16, 0xFF);
2958                         hash_key[(i * 4) + 3] = RS32(rss_key, 24, 0xFF);
2959                 }
2960         }
2961
2962         rss_hf = 0;
2963         if (hw->mac.type == txgbe_mac_raptor_vf) {
2964                 mrqc = rd32(hw, TXGBE_VFPLCFG);
2965                 if (mrqc & TXGBE_VFPLCFG_RSSIPV4)
2966                         rss_hf |= ETH_RSS_IPV4;
2967                 if (mrqc & TXGBE_VFPLCFG_RSSIPV4TCP)
2968                         rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
2969                 if (mrqc & TXGBE_VFPLCFG_RSSIPV6)
2970                         rss_hf |= ETH_RSS_IPV6 |
2971                                   ETH_RSS_IPV6_EX;
2972                 if (mrqc & TXGBE_VFPLCFG_RSSIPV6TCP)
2973                         rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP |
2974                                   ETH_RSS_IPV6_TCP_EX;
2975                 if (mrqc & TXGBE_VFPLCFG_RSSIPV4UDP)
2976                         rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
2977                 if (mrqc & TXGBE_VFPLCFG_RSSIPV6UDP)
2978                         rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP |
2979                                   ETH_RSS_IPV6_UDP_EX;
2980                 if (!(mrqc & TXGBE_VFPLCFG_RSSENA))
2981                         rss_hf = 0;
2982         } else {
2983                 mrqc = rd32(hw, TXGBE_RACTL);
2984                 if (mrqc & TXGBE_RACTL_RSSIPV4)
2985                         rss_hf |= ETH_RSS_IPV4;
2986                 if (mrqc & TXGBE_RACTL_RSSIPV4TCP)
2987                         rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
2988                 if (mrqc & TXGBE_RACTL_RSSIPV6)
2989                         rss_hf |= ETH_RSS_IPV6 |
2990                                   ETH_RSS_IPV6_EX;
2991                 if (mrqc & TXGBE_RACTL_RSSIPV6TCP)
2992                         rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP |
2993                                   ETH_RSS_IPV6_TCP_EX;
2994                 if (mrqc & TXGBE_RACTL_RSSIPV4UDP)
2995                         rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
2996                 if (mrqc & TXGBE_RACTL_RSSIPV6UDP)
2997                         rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP |
2998                                   ETH_RSS_IPV6_UDP_EX;
2999                 if (!(mrqc & TXGBE_RACTL_RSSENA))
3000                         rss_hf = 0;
3001         }
3002
3003         rss_hf &= TXGBE_RSS_OFFLOAD_ALL;
3004
3005         rss_conf->rss_hf = rss_hf;
3006         return 0;
3007 }
3008
3009 static void
3010 txgbe_rss_configure(struct rte_eth_dev *dev)
3011 {
3012         struct rte_eth_rss_conf rss_conf;
3013         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
3014         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3015         uint32_t reta;
3016         uint16_t i;
3017         uint16_t j;
3018
3019         PMD_INIT_FUNC_TRACE();
3020
3021         /*
3022          * Fill in redirection table
3023          * The byte-swap is needed because NIC registers are in
3024          * little-endian order.
3025          */
3026         if (adapter->rss_reta_updated == 0) {
3027                 reta = 0;
3028                 for (i = 0, j = 0; i < ETH_RSS_RETA_SIZE_128; i++, j++) {
3029                         if (j == dev->data->nb_rx_queues)
3030                                 j = 0;
3031                         reta = (reta >> 8) | LS32(j, 24, 0xFF);
3032                         if ((i & 3) == 3)
3033                                 wr32at(hw, TXGBE_REG_RSSTBL, i >> 2, reta);
3034                 }
3035         }
3036         /*
3037          * Configure the RSS key and the RSS protocols used to compute
3038          * the RSS hash of input packets.
3039          */
3040         rss_conf = dev->data->dev_conf.rx_adv_conf.rss_conf;
3041         if (rss_conf.rss_key == NULL)
3042                 rss_conf.rss_key = rss_intel_key; /* Default hash key */
3043         txgbe_dev_rss_hash_update(dev, &rss_conf);
3044 }
3045
3046 #define NUM_VFTA_REGISTERS 128
3047 #define NIC_RX_BUFFER_SIZE 0x200
3048
3049 static void
3050 txgbe_vmdq_dcb_configure(struct rte_eth_dev *dev)
3051 {
3052         struct rte_eth_vmdq_dcb_conf *cfg;
3053         struct txgbe_hw *hw;
3054         enum rte_eth_nb_pools num_pools;
3055         uint32_t mrqc, vt_ctl, queue_mapping, vlanctrl;
3056         uint16_t pbsize;
3057         uint8_t nb_tcs; /* number of traffic classes */
3058         int i;
3059
3060         PMD_INIT_FUNC_TRACE();
3061         hw = TXGBE_DEV_HW(dev);
3062         cfg = &dev->data->dev_conf.rx_adv_conf.vmdq_dcb_conf;
3063         num_pools = cfg->nb_queue_pools;
3064         /* Check we have a valid number of pools */
3065         if (num_pools != ETH_16_POOLS && num_pools != ETH_32_POOLS) {
3066                 txgbe_rss_disable(dev);
3067                 return;
3068         }
3069         /* 16 pools -> 8 traffic classes, 32 pools -> 4 traffic classes */
3070         nb_tcs = (uint8_t)(ETH_VMDQ_DCB_NUM_QUEUES / (int)num_pools);
3071
3072         /*
3073          * split rx buffer up into sections, each for 1 traffic class
3074          */
3075         pbsize = (uint16_t)(NIC_RX_BUFFER_SIZE / nb_tcs);
3076         for (i = 0; i < nb_tcs; i++) {
3077                 uint32_t rxpbsize = rd32(hw, TXGBE_PBRXSIZE(i));
3078
3079                 rxpbsize &= (~(0x3FF << 10));
3080                 /* clear 10 bits. */
3081                 rxpbsize |= (pbsize << 10); /* set value */
3082                 wr32(hw, TXGBE_PBRXSIZE(i), rxpbsize);
3083         }
3084         /* zero alloc all unused TCs */
3085         for (i = nb_tcs; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3086                 uint32_t rxpbsize = rd32(hw, TXGBE_PBRXSIZE(i));
3087
3088                 rxpbsize &= (~(0x3FF << 10));
3089                 /* clear 10 bits. */
3090                 wr32(hw, TXGBE_PBRXSIZE(i), rxpbsize);
3091         }
3092
3093         if (num_pools == ETH_16_POOLS) {
3094                 mrqc = TXGBE_PORTCTL_NUMTC_8;
3095                 mrqc |= TXGBE_PORTCTL_NUMVT_16;
3096         } else {
3097                 mrqc = TXGBE_PORTCTL_NUMTC_4;
3098                 mrqc |= TXGBE_PORTCTL_NUMVT_32;
3099         }
3100         wr32m(hw, TXGBE_PORTCTL,
3101               TXGBE_PORTCTL_NUMTC_MASK | TXGBE_PORTCTL_NUMVT_MASK, mrqc);
3102
3103         vt_ctl = TXGBE_POOLCTL_RPLEN;
3104         if (cfg->enable_default_pool)
3105                 vt_ctl |= TXGBE_POOLCTL_DEFPL(cfg->default_pool);
3106         else
3107                 vt_ctl |= TXGBE_POOLCTL_DEFDSA;
3108
3109         wr32(hw, TXGBE_POOLCTL, vt_ctl);
3110
3111         queue_mapping = 0;
3112         for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++)
3113                 /*
3114                  * mapping is done with 3 bits per priority,
3115                  * so shift by i*3 each time
3116                  */
3117                 queue_mapping |= ((cfg->dcb_tc[i] & 0x07) << (i * 3));
3118
3119         wr32(hw, TXGBE_RPUP2TC, queue_mapping);
3120
3121         wr32(hw, TXGBE_ARBRXCTL, TXGBE_ARBRXCTL_RRM);
3122
3123         /* enable vlan filtering and allow all vlan tags through */
3124         vlanctrl = rd32(hw, TXGBE_VLANCTL);
3125         vlanctrl |= TXGBE_VLANCTL_VFE; /* enable vlan filters */
3126         wr32(hw, TXGBE_VLANCTL, vlanctrl);
3127
3128         /* enable all vlan filters */
3129         for (i = 0; i < NUM_VFTA_REGISTERS; i++)
3130                 wr32(hw, TXGBE_VLANTBL(i), 0xFFFFFFFF);
3131
3132         wr32(hw, TXGBE_POOLRXENA(0),
3133                         num_pools == ETH_16_POOLS ? 0xFFFF : 0xFFFFFFFF);
3134
3135         wr32(hw, TXGBE_ETHADDRIDX, 0);
3136         wr32(hw, TXGBE_ETHADDRASSL, 0xFFFFFFFF);
3137         wr32(hw, TXGBE_ETHADDRASSH, 0xFFFFFFFF);
3138
3139         /* set up filters for vlan tags as configured */
3140         for (i = 0; i < cfg->nb_pool_maps; i++) {
3141                 /* set vlan id in VF register and set the valid bit */
3142                 wr32(hw, TXGBE_PSRVLANIDX, i);
3143                 wr32(hw, TXGBE_PSRVLAN, (TXGBE_PSRVLAN_EA |
3144                                 (cfg->pool_map[i].vlan_id & 0xFFF)));
3145
3146                 wr32(hw, TXGBE_PSRVLANPLM(0), cfg->pool_map[i].pools);
3147         }
3148 }
3149
3150 /**
3151  * txgbe_dcb_config_tx_hw_config - Configure general DCB TX parameters
3152  * @dev: pointer to eth_dev structure
3153  * @dcb_config: pointer to txgbe_dcb_config structure
3154  */
3155 static void
3156 txgbe_dcb_tx_hw_config(struct rte_eth_dev *dev,
3157                        struct txgbe_dcb_config *dcb_config)
3158 {
3159         uint32_t reg;
3160         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3161
3162         PMD_INIT_FUNC_TRACE();
3163
3164         /* Disable the Tx desc arbiter */
3165         reg = rd32(hw, TXGBE_ARBTXCTL);
3166         reg |= TXGBE_ARBTXCTL_DIA;
3167         wr32(hw, TXGBE_ARBTXCTL, reg);
3168
3169         /* Enable DCB for Tx with 8 TCs */
3170         reg = rd32(hw, TXGBE_PORTCTL);
3171         reg &= TXGBE_PORTCTL_NUMTC_MASK;
3172         reg |= TXGBE_PORTCTL_DCB;
3173         if (dcb_config->num_tcs.pg_tcs == 8)
3174                 reg |= TXGBE_PORTCTL_NUMTC_8;
3175         else
3176                 reg |= TXGBE_PORTCTL_NUMTC_4;
3177
3178         wr32(hw, TXGBE_PORTCTL, reg);
3179
3180         /* Enable the Tx desc arbiter */
3181         reg = rd32(hw, TXGBE_ARBTXCTL);
3182         reg &= ~TXGBE_ARBTXCTL_DIA;
3183         wr32(hw, TXGBE_ARBTXCTL, reg);
3184 }
3185
3186 /**
3187  * txgbe_vmdq_dcb_hw_tx_config - Configure general VMDQ+DCB TX parameters
3188  * @dev: pointer to rte_eth_dev structure
3189  * @dcb_config: pointer to txgbe_dcb_config structure
3190  */
3191 static void
3192 txgbe_vmdq_dcb_hw_tx_config(struct rte_eth_dev *dev,
3193                         struct txgbe_dcb_config *dcb_config)
3194 {
3195         struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
3196                         &dev->data->dev_conf.tx_adv_conf.vmdq_dcb_tx_conf;
3197         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3198
3199         PMD_INIT_FUNC_TRACE();
3200         /*PF VF Transmit Enable*/
3201         wr32(hw, TXGBE_POOLTXENA(0),
3202                 vmdq_tx_conf->nb_queue_pools ==
3203                                 ETH_16_POOLS ? 0xFFFF : 0xFFFFFFFF);
3204
3205         /*Configure general DCB TX parameters*/
3206         txgbe_dcb_tx_hw_config(dev, dcb_config);
3207 }
3208
3209 static void
3210 txgbe_vmdq_dcb_rx_config(struct rte_eth_dev *dev,
3211                         struct txgbe_dcb_config *dcb_config)
3212 {
3213         struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
3214                         &dev->data->dev_conf.rx_adv_conf.vmdq_dcb_conf;
3215         struct txgbe_dcb_tc_config *tc;
3216         uint8_t i, j;
3217
3218         /* convert rte_eth_conf.rx_adv_conf to struct txgbe_dcb_config */
3219         if (vmdq_rx_conf->nb_queue_pools == ETH_16_POOLS) {
3220                 dcb_config->num_tcs.pg_tcs = ETH_8_TCS;
3221                 dcb_config->num_tcs.pfc_tcs = ETH_8_TCS;
3222         } else {
3223                 dcb_config->num_tcs.pg_tcs = ETH_4_TCS;
3224                 dcb_config->num_tcs.pfc_tcs = ETH_4_TCS;
3225         }
3226
3227         /* Initialize User Priority to Traffic Class mapping */
3228         for (j = 0; j < TXGBE_DCB_TC_MAX; j++) {
3229                 tc = &dcb_config->tc_config[j];
3230                 tc->path[TXGBE_DCB_RX_CONFIG].up_to_tc_bitmap = 0;
3231         }
3232
3233         /* User Priority to Traffic Class mapping */
3234         for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3235                 j = vmdq_rx_conf->dcb_tc[i];
3236                 tc = &dcb_config->tc_config[j];
3237                 tc->path[TXGBE_DCB_RX_CONFIG].up_to_tc_bitmap |=
3238                                                 (uint8_t)(1 << i);
3239         }
3240 }
3241
3242 static void
3243 txgbe_dcb_vt_tx_config(struct rte_eth_dev *dev,
3244                         struct txgbe_dcb_config *dcb_config)
3245 {
3246         struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
3247                         &dev->data->dev_conf.tx_adv_conf.vmdq_dcb_tx_conf;
3248         struct txgbe_dcb_tc_config *tc;
3249         uint8_t i, j;
3250
3251         /* convert rte_eth_conf.rx_adv_conf to struct txgbe_dcb_config */
3252         if (vmdq_tx_conf->nb_queue_pools == ETH_16_POOLS) {
3253                 dcb_config->num_tcs.pg_tcs = ETH_8_TCS;
3254                 dcb_config->num_tcs.pfc_tcs = ETH_8_TCS;
3255         } else {
3256                 dcb_config->num_tcs.pg_tcs = ETH_4_TCS;
3257                 dcb_config->num_tcs.pfc_tcs = ETH_4_TCS;
3258         }
3259
3260         /* Initialize User Priority to Traffic Class mapping */
3261         for (j = 0; j < TXGBE_DCB_TC_MAX; j++) {
3262                 tc = &dcb_config->tc_config[j];
3263                 tc->path[TXGBE_DCB_TX_CONFIG].up_to_tc_bitmap = 0;
3264         }
3265
3266         /* User Priority to Traffic Class mapping */
3267         for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3268                 j = vmdq_tx_conf->dcb_tc[i];
3269                 tc = &dcb_config->tc_config[j];
3270                 tc->path[TXGBE_DCB_TX_CONFIG].up_to_tc_bitmap |=
3271                                                 (uint8_t)(1 << i);
3272         }
3273 }
3274
3275 static void
3276 txgbe_dcb_rx_config(struct rte_eth_dev *dev,
3277                 struct txgbe_dcb_config *dcb_config)
3278 {
3279         struct rte_eth_dcb_rx_conf *rx_conf =
3280                         &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
3281         struct txgbe_dcb_tc_config *tc;
3282         uint8_t i, j;
3283
3284         dcb_config->num_tcs.pg_tcs = (uint8_t)rx_conf->nb_tcs;
3285         dcb_config->num_tcs.pfc_tcs = (uint8_t)rx_conf->nb_tcs;
3286
3287         /* Initialize User Priority to Traffic Class mapping */
3288         for (j = 0; j < TXGBE_DCB_TC_MAX; j++) {
3289                 tc = &dcb_config->tc_config[j];
3290                 tc->path[TXGBE_DCB_RX_CONFIG].up_to_tc_bitmap = 0;
3291         }
3292
3293         /* User Priority to Traffic Class mapping */
3294         for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3295                 j = rx_conf->dcb_tc[i];
3296                 tc = &dcb_config->tc_config[j];
3297                 tc->path[TXGBE_DCB_RX_CONFIG].up_to_tc_bitmap |=
3298                                                 (uint8_t)(1 << i);
3299         }
3300 }
3301
3302 static void
3303 txgbe_dcb_tx_config(struct rte_eth_dev *dev,
3304                 struct txgbe_dcb_config *dcb_config)
3305 {
3306         struct rte_eth_dcb_tx_conf *tx_conf =
3307                         &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
3308         struct txgbe_dcb_tc_config *tc;
3309         uint8_t i, j;
3310
3311         dcb_config->num_tcs.pg_tcs = (uint8_t)tx_conf->nb_tcs;
3312         dcb_config->num_tcs.pfc_tcs = (uint8_t)tx_conf->nb_tcs;
3313
3314         /* Initialize User Priority to Traffic Class mapping */
3315         for (j = 0; j < TXGBE_DCB_TC_MAX; j++) {
3316                 tc = &dcb_config->tc_config[j];
3317                 tc->path[TXGBE_DCB_TX_CONFIG].up_to_tc_bitmap = 0;
3318         }
3319
3320         /* User Priority to Traffic Class mapping */
3321         for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3322                 j = tx_conf->dcb_tc[i];
3323                 tc = &dcb_config->tc_config[j];
3324                 tc->path[TXGBE_DCB_TX_CONFIG].up_to_tc_bitmap |=
3325                                                 (uint8_t)(1 << i);
3326         }
3327 }
3328
3329 /**
3330  * txgbe_dcb_rx_hw_config - Configure general DCB RX HW parameters
3331  * @dev: pointer to eth_dev structure
3332  * @dcb_config: pointer to txgbe_dcb_config structure
3333  */
3334 static void
3335 txgbe_dcb_rx_hw_config(struct rte_eth_dev *dev,
3336                        struct txgbe_dcb_config *dcb_config)
3337 {
3338         uint32_t reg;
3339         uint32_t vlanctrl;
3340         uint8_t i;
3341         uint32_t q;
3342         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3343
3344         PMD_INIT_FUNC_TRACE();
3345         /*
3346          * Disable the arbiter before changing parameters
3347          * (always enable recycle mode; WSP)
3348          */
3349         reg = TXGBE_ARBRXCTL_RRM | TXGBE_ARBRXCTL_WSP | TXGBE_ARBRXCTL_DIA;
3350         wr32(hw, TXGBE_ARBRXCTL, reg);
3351
3352         reg = rd32(hw, TXGBE_PORTCTL);
3353         reg &= ~(TXGBE_PORTCTL_NUMTC_MASK | TXGBE_PORTCTL_NUMVT_MASK);
3354         if (dcb_config->num_tcs.pg_tcs == 4) {
3355                 reg |= TXGBE_PORTCTL_NUMTC_4;
3356                 if (dcb_config->vt_mode)
3357                         reg |= TXGBE_PORTCTL_NUMVT_32;
3358                 else
3359                         wr32(hw, TXGBE_POOLCTL, 0);
3360         }
3361
3362         if (dcb_config->num_tcs.pg_tcs == 8) {
3363                 reg |= TXGBE_PORTCTL_NUMTC_8;
3364                 if (dcb_config->vt_mode)
3365                         reg |= TXGBE_PORTCTL_NUMVT_16;
3366                 else
3367                         wr32(hw, TXGBE_POOLCTL, 0);
3368         }
3369
3370         wr32(hw, TXGBE_PORTCTL, reg);
3371
3372         if (RTE_ETH_DEV_SRIOV(dev).active == 0) {
3373                 /* Disable drop for all queues in VMDQ mode*/
3374                 for (q = 0; q < TXGBE_MAX_RX_QUEUE_NUM; q++) {
3375                         u32 val = 1 << (q % 32);
3376                         wr32m(hw, TXGBE_QPRXDROP(q / 32), val, val);
3377                 }
3378         } else {
3379                 /* Enable drop for all queues in SRIOV mode */
3380                 for (q = 0; q < TXGBE_MAX_RX_QUEUE_NUM; q++) {
3381                         u32 val = 1 << (q % 32);
3382                         wr32m(hw, TXGBE_QPRXDROP(q / 32), val, val);
3383                 }
3384         }
3385
3386         /* VLNCTL: enable vlan filtering and allow all vlan tags through */
3387         vlanctrl = rd32(hw, TXGBE_VLANCTL);
3388         vlanctrl |= TXGBE_VLANCTL_VFE; /* enable vlan filters */
3389         wr32(hw, TXGBE_VLANCTL, vlanctrl);
3390
3391         /* VLANTBL - enable all vlan filters */
3392         for (i = 0; i < NUM_VFTA_REGISTERS; i++)
3393                 wr32(hw, TXGBE_VLANTBL(i), 0xFFFFFFFF);
3394
3395         /*
3396          * Configure Rx packet plane (recycle mode; WSP) and
3397          * enable arbiter
3398          */
3399         reg = TXGBE_ARBRXCTL_RRM | TXGBE_ARBRXCTL_WSP;
3400         wr32(hw, TXGBE_ARBRXCTL, reg);
3401 }
3402
3403 static void
3404 txgbe_dcb_hw_arbite_rx_config(struct txgbe_hw *hw, uint16_t *refill,
3405                 uint16_t *max, uint8_t *bwg_id, uint8_t *tsa, uint8_t *map)
3406 {
3407         txgbe_dcb_config_rx_arbiter_raptor(hw, refill, max, bwg_id,
3408                                           tsa, map);
3409 }
3410
3411 static void
3412 txgbe_dcb_hw_arbite_tx_config(struct txgbe_hw *hw, uint16_t *refill,
3413                 uint16_t *max, uint8_t *bwg_id, uint8_t *tsa, uint8_t *map)
3414 {
3415         switch (hw->mac.type) {
3416         case txgbe_mac_raptor:
3417                 txgbe_dcb_config_tx_desc_arbiter_raptor(hw, refill,
3418                                                         max, bwg_id, tsa);
3419                 txgbe_dcb_config_tx_data_arbiter_raptor(hw, refill,
3420                                                         max, bwg_id, tsa, map);
3421                 break;
3422         default:
3423                 break;
3424         }
3425 }
3426
3427 #define DCB_RX_CONFIG  1
3428 #define DCB_TX_CONFIG  1
3429 #define DCB_TX_PB      1024
3430 /**
3431  * txgbe_dcb_hw_configure - Enable DCB and configure
3432  * general DCB in VT mode and non-VT mode parameters
3433  * @dev: pointer to rte_eth_dev structure
3434  * @dcb_config: pointer to txgbe_dcb_config structure
3435  */
3436 static int
3437 txgbe_dcb_hw_configure(struct rte_eth_dev *dev,
3438                         struct txgbe_dcb_config *dcb_config)
3439 {
3440         int     ret = 0;
3441         uint8_t i, pfc_en, nb_tcs;
3442         uint16_t pbsize, rx_buffer_size;
3443         uint8_t config_dcb_rx = 0;
3444         uint8_t config_dcb_tx = 0;
3445         uint8_t tsa[TXGBE_DCB_TC_MAX] = {0};
3446         uint8_t bwgid[TXGBE_DCB_TC_MAX] = {0};
3447         uint16_t refill[TXGBE_DCB_TC_MAX] = {0};
3448         uint16_t max[TXGBE_DCB_TC_MAX] = {0};
3449         uint8_t map[TXGBE_DCB_TC_MAX] = {0};
3450         struct txgbe_dcb_tc_config *tc;
3451         uint32_t max_frame = dev->data->mtu +
3452                         RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
3453         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3454         struct txgbe_bw_conf *bw_conf = TXGBE_DEV_BW_CONF(dev);
3455
3456         switch (dev->data->dev_conf.rxmode.mq_mode) {
3457         case ETH_MQ_RX_VMDQ_DCB:
3458                 dcb_config->vt_mode = true;
3459                 config_dcb_rx = DCB_RX_CONFIG;
3460                 /*
3461                  * get dcb and VT rx configuration parameters
3462                  * from rte_eth_conf
3463                  */
3464                 txgbe_vmdq_dcb_rx_config(dev, dcb_config);
3465                 /*Configure general VMDQ and DCB RX parameters*/
3466                 txgbe_vmdq_dcb_configure(dev);
3467                 break;
3468         case ETH_MQ_RX_DCB:
3469         case ETH_MQ_RX_DCB_RSS:
3470                 dcb_config->vt_mode = false;
3471                 config_dcb_rx = DCB_RX_CONFIG;
3472                 /* Get dcb TX configuration parameters from rte_eth_conf */
3473                 txgbe_dcb_rx_config(dev, dcb_config);
3474                 /*Configure general DCB RX parameters*/
3475                 txgbe_dcb_rx_hw_config(dev, dcb_config);
3476                 break;
3477         default:
3478                 PMD_INIT_LOG(ERR, "Incorrect DCB RX mode configuration");
3479                 break;
3480         }
3481         switch (dev->data->dev_conf.txmode.mq_mode) {
3482         case ETH_MQ_TX_VMDQ_DCB:
3483                 dcb_config->vt_mode = true;
3484                 config_dcb_tx = DCB_TX_CONFIG;
3485                 /* get DCB and VT TX configuration parameters
3486                  * from rte_eth_conf
3487                  */
3488                 txgbe_dcb_vt_tx_config(dev, dcb_config);
3489                 /* Configure general VMDQ and DCB TX parameters */
3490                 txgbe_vmdq_dcb_hw_tx_config(dev, dcb_config);
3491                 break;
3492
3493         case ETH_MQ_TX_DCB:
3494                 dcb_config->vt_mode = false;
3495                 config_dcb_tx = DCB_TX_CONFIG;
3496                 /* get DCB TX configuration parameters from rte_eth_conf */
3497                 txgbe_dcb_tx_config(dev, dcb_config);
3498                 /* Configure general DCB TX parameters */
3499                 txgbe_dcb_tx_hw_config(dev, dcb_config);
3500                 break;
3501         default:
3502                 PMD_INIT_LOG(ERR, "Incorrect DCB TX mode configuration");
3503                 break;
3504         }
3505
3506         nb_tcs = dcb_config->num_tcs.pfc_tcs;
3507         /* Unpack map */
3508         txgbe_dcb_unpack_map_cee(dcb_config, TXGBE_DCB_RX_CONFIG, map);
3509         if (nb_tcs == ETH_4_TCS) {
3510                 /* Avoid un-configured priority mapping to TC0 */
3511                 uint8_t j = 4;
3512                 uint8_t mask = 0xFF;
3513
3514                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES - 4; i++)
3515                         mask = (uint8_t)(mask & (~(1 << map[i])));
3516                 for (i = 0; mask && (i < TXGBE_DCB_TC_MAX); i++) {
3517                         if ((mask & 0x1) && j < ETH_DCB_NUM_USER_PRIORITIES)
3518                                 map[j++] = i;
3519                         mask >>= 1;
3520                 }
3521                 /* Re-configure 4 TCs BW */
3522                 for (i = 0; i < nb_tcs; i++) {
3523                         tc = &dcb_config->tc_config[i];
3524                         if (bw_conf->tc_num != nb_tcs)
3525                                 tc->path[TXGBE_DCB_TX_CONFIG].bwg_percent =
3526                                         (uint8_t)(100 / nb_tcs);
3527                         tc->path[TXGBE_DCB_RX_CONFIG].bwg_percent =
3528                                                 (uint8_t)(100 / nb_tcs);
3529                 }
3530                 for (; i < TXGBE_DCB_TC_MAX; i++) {
3531                         tc = &dcb_config->tc_config[i];
3532                         tc->path[TXGBE_DCB_TX_CONFIG].bwg_percent = 0;
3533                         tc->path[TXGBE_DCB_RX_CONFIG].bwg_percent = 0;
3534                 }
3535         } else {
3536                 /* Re-configure 8 TCs BW */
3537                 for (i = 0; i < nb_tcs; i++) {
3538                         tc = &dcb_config->tc_config[i];
3539                         if (bw_conf->tc_num != nb_tcs)
3540                                 tc->path[TXGBE_DCB_TX_CONFIG].bwg_percent =
3541                                         (uint8_t)(100 / nb_tcs + (i & 1));
3542                         tc->path[TXGBE_DCB_RX_CONFIG].bwg_percent =
3543                                 (uint8_t)(100 / nb_tcs + (i & 1));
3544                 }
3545         }
3546
3547         rx_buffer_size = NIC_RX_BUFFER_SIZE;
3548
3549         if (config_dcb_rx) {
3550                 /* Set RX buffer size */
3551                 pbsize = (uint16_t)(rx_buffer_size / nb_tcs);
3552                 uint32_t rxpbsize = pbsize << 10;
3553
3554                 for (i = 0; i < nb_tcs; i++)
3555                         wr32(hw, TXGBE_PBRXSIZE(i), rxpbsize);
3556
3557                 /* zero alloc all unused TCs */
3558                 for (; i < ETH_DCB_NUM_USER_PRIORITIES; i++)
3559                         wr32(hw, TXGBE_PBRXSIZE(i), 0);
3560         }
3561         if (config_dcb_tx) {
3562                 /* Only support an equally distributed
3563                  *  Tx packet buffer strategy.
3564                  */
3565                 uint32_t txpktsize = TXGBE_PBTXSIZE_MAX / nb_tcs;
3566                 uint32_t txpbthresh = (txpktsize / DCB_TX_PB) -
3567                                         TXGBE_TXPKT_SIZE_MAX;
3568
3569                 for (i = 0; i < nb_tcs; i++) {
3570                         wr32(hw, TXGBE_PBTXSIZE(i), txpktsize);
3571                         wr32(hw, TXGBE_PBTXDMATH(i), txpbthresh);
3572                 }
3573                 /* Clear unused TCs, if any, to zero buffer size*/
3574                 for (; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
3575                         wr32(hw, TXGBE_PBTXSIZE(i), 0);
3576                         wr32(hw, TXGBE_PBTXDMATH(i), 0);
3577                 }
3578         }
3579
3580         /*Calculates traffic class credits*/
3581         txgbe_dcb_calculate_tc_credits_cee(hw, dcb_config, max_frame,
3582                                 TXGBE_DCB_TX_CONFIG);
3583         txgbe_dcb_calculate_tc_credits_cee(hw, dcb_config, max_frame,
3584                                 TXGBE_DCB_RX_CONFIG);
3585
3586         if (config_dcb_rx) {
3587                 /* Unpack CEE standard containers */
3588                 txgbe_dcb_unpack_refill_cee(dcb_config,
3589                                 TXGBE_DCB_RX_CONFIG, refill);
3590                 txgbe_dcb_unpack_max_cee(dcb_config, max);
3591                 txgbe_dcb_unpack_bwgid_cee(dcb_config,
3592                                 TXGBE_DCB_RX_CONFIG, bwgid);
3593                 txgbe_dcb_unpack_tsa_cee(dcb_config,
3594                                 TXGBE_DCB_RX_CONFIG, tsa);
3595                 /* Configure PG(ETS) RX */
3596                 txgbe_dcb_hw_arbite_rx_config(hw, refill, max, bwgid, tsa, map);
3597         }
3598
3599         if (config_dcb_tx) {
3600                 /* Unpack CEE standard containers */
3601                 txgbe_dcb_unpack_refill_cee(dcb_config,
3602                                 TXGBE_DCB_TX_CONFIG, refill);
3603                 txgbe_dcb_unpack_max_cee(dcb_config, max);
3604                 txgbe_dcb_unpack_bwgid_cee(dcb_config,
3605                                 TXGBE_DCB_TX_CONFIG, bwgid);
3606                 txgbe_dcb_unpack_tsa_cee(dcb_config,
3607                                 TXGBE_DCB_TX_CONFIG, tsa);
3608                 /* Configure PG(ETS) TX */
3609                 txgbe_dcb_hw_arbite_tx_config(hw, refill, max, bwgid, tsa, map);
3610         }
3611
3612         /* Configure queue statistics registers */
3613         txgbe_dcb_config_tc_stats_raptor(hw, dcb_config);
3614
3615         /* Check if the PFC is supported */
3616         if (dev->data->dev_conf.dcb_capability_en & ETH_DCB_PFC_SUPPORT) {
3617                 pbsize = (uint16_t)(rx_buffer_size / nb_tcs);
3618                 for (i = 0; i < nb_tcs; i++) {
3619                         /* If the TC count is 8,
3620                          * and the default high_water is 48,
3621                          * the low_water is 16 as default.
3622                          */
3623                         hw->fc.high_water[i] = (pbsize * 3) / 4;
3624                         hw->fc.low_water[i] = pbsize / 4;
3625                         /* Enable pfc for this TC */
3626                         tc = &dcb_config->tc_config[i];
3627                         tc->pfc = txgbe_dcb_pfc_enabled;
3628                 }
3629                 txgbe_dcb_unpack_pfc_cee(dcb_config, map, &pfc_en);
3630                 if (dcb_config->num_tcs.pfc_tcs == ETH_4_TCS)
3631                         pfc_en &= 0x0F;
3632                 ret = txgbe_dcb_config_pfc(hw, pfc_en, map);
3633         }
3634
3635         return ret;
3636 }
3637
3638 void txgbe_configure_pb(struct rte_eth_dev *dev)
3639 {
3640         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
3641         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3642
3643         int hdrm;
3644         int tc = dev_conf->rx_adv_conf.dcb_rx_conf.nb_tcs;
3645
3646         /* Reserve 256KB(/512KB) rx buffer for fdir */
3647         hdrm = 256; /*KB*/
3648
3649         hw->mac.setup_pba(hw, tc, hdrm, PBA_STRATEGY_EQUAL);
3650 }
3651
3652 void txgbe_configure_port(struct rte_eth_dev *dev)
3653 {
3654         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3655         int i = 0;
3656         uint16_t tpids[8] = {RTE_ETHER_TYPE_VLAN, RTE_ETHER_TYPE_QINQ,
3657                                 0x9100, 0x9200,
3658                                 0x0000, 0x0000,
3659                                 0x0000, 0x0000};
3660
3661         PMD_INIT_FUNC_TRACE();
3662
3663         /* default outer vlan tpid */
3664         wr32(hw, TXGBE_EXTAG,
3665                 TXGBE_EXTAG_ETAG(RTE_ETHER_TYPE_ETAG) |
3666                 TXGBE_EXTAG_VLAN(RTE_ETHER_TYPE_QINQ));
3667
3668         /* default inner vlan tpid */
3669         wr32m(hw, TXGBE_VLANCTL,
3670                 TXGBE_VLANCTL_TPID_MASK,
3671                 TXGBE_VLANCTL_TPID(RTE_ETHER_TYPE_VLAN));
3672         wr32m(hw, TXGBE_DMATXCTRL,
3673                 TXGBE_DMATXCTRL_TPID_MASK,
3674                 TXGBE_DMATXCTRL_TPID(RTE_ETHER_TYPE_VLAN));
3675
3676         /* default vlan tpid filters */
3677         for (i = 0; i < 8; i++) {
3678                 wr32m(hw, TXGBE_TAGTPID(i / 2),
3679                         (i % 2 ? TXGBE_TAGTPID_MSB_MASK
3680                                : TXGBE_TAGTPID_LSB_MASK),
3681                         (i % 2 ? TXGBE_TAGTPID_MSB(tpids[i])
3682                                : TXGBE_TAGTPID_LSB(tpids[i])));
3683         }
3684
3685         /* default vxlan port */
3686         wr32(hw, TXGBE_VXLANPORT, 4789);
3687 }
3688
3689 /**
3690  * txgbe_configure_dcb - Configure DCB  Hardware
3691  * @dev: pointer to rte_eth_dev
3692  */
3693 void txgbe_configure_dcb(struct rte_eth_dev *dev)
3694 {
3695         struct txgbe_dcb_config *dcb_cfg = TXGBE_DEV_DCB_CONFIG(dev);
3696         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
3697
3698         PMD_INIT_FUNC_TRACE();
3699
3700         /* check support mq_mode for DCB */
3701         if (dev_conf->rxmode.mq_mode != ETH_MQ_RX_VMDQ_DCB &&
3702             dev_conf->rxmode.mq_mode != ETH_MQ_RX_DCB &&
3703             dev_conf->rxmode.mq_mode != ETH_MQ_RX_DCB_RSS)
3704                 return;
3705
3706         if (dev->data->nb_rx_queues > ETH_DCB_NUM_QUEUES)
3707                 return;
3708
3709         /** Configure DCB hardware **/
3710         txgbe_dcb_hw_configure(dev, dcb_cfg);
3711 }
3712
3713 /*
3714  * VMDq only support for 10 GbE NIC.
3715  */
3716 static void
3717 txgbe_vmdq_rx_hw_configure(struct rte_eth_dev *dev)
3718 {
3719         struct rte_eth_vmdq_rx_conf *cfg;
3720         struct txgbe_hw *hw;
3721         enum rte_eth_nb_pools num_pools;
3722         uint32_t mrqc, vt_ctl, vlanctrl;
3723         uint32_t vmolr = 0;
3724         int i;
3725
3726         PMD_INIT_FUNC_TRACE();
3727         hw = TXGBE_DEV_HW(dev);
3728         cfg = &dev->data->dev_conf.rx_adv_conf.vmdq_rx_conf;
3729         num_pools = cfg->nb_queue_pools;
3730
3731         txgbe_rss_disable(dev);
3732
3733         /* enable vmdq */
3734         mrqc = TXGBE_PORTCTL_NUMVT_64;
3735         wr32m(hw, TXGBE_PORTCTL, TXGBE_PORTCTL_NUMVT_MASK, mrqc);
3736
3737         /* turn on virtualisation and set the default pool */
3738         vt_ctl = TXGBE_POOLCTL_RPLEN;
3739         if (cfg->enable_default_pool)
3740                 vt_ctl |= TXGBE_POOLCTL_DEFPL(cfg->default_pool);
3741         else
3742                 vt_ctl |= TXGBE_POOLCTL_DEFDSA;
3743
3744         wr32(hw, TXGBE_POOLCTL, vt_ctl);
3745
3746         for (i = 0; i < (int)num_pools; i++) {
3747                 vmolr = txgbe_convert_vm_rx_mask_to_val(cfg->rx_mode, vmolr);
3748                 wr32(hw, TXGBE_POOLETHCTL(i), vmolr);
3749         }
3750
3751         /* enable vlan filtering and allow all vlan tags through */
3752         vlanctrl = rd32(hw, TXGBE_VLANCTL);
3753         vlanctrl |= TXGBE_VLANCTL_VFE; /* enable vlan filters */
3754         wr32(hw, TXGBE_VLANCTL, vlanctrl);
3755
3756         /* enable all vlan filters */
3757         for (i = 0; i < NUM_VFTA_REGISTERS; i++)
3758                 wr32(hw, TXGBE_VLANTBL(i), UINT32_MAX);
3759
3760         /* pool enabling for receive - 64 */
3761         wr32(hw, TXGBE_POOLRXENA(0), UINT32_MAX);
3762         if (num_pools == ETH_64_POOLS)
3763                 wr32(hw, TXGBE_POOLRXENA(1), UINT32_MAX);
3764
3765         /*
3766          * allow pools to read specific mac addresses
3767          * In this case, all pools should be able to read from mac addr 0
3768          */
3769         wr32(hw, TXGBE_ETHADDRIDX, 0);
3770         wr32(hw, TXGBE_ETHADDRASSL, 0xFFFFFFFF);
3771         wr32(hw, TXGBE_ETHADDRASSH, 0xFFFFFFFF);
3772
3773         /* set up filters for vlan tags as configured */
3774         for (i = 0; i < cfg->nb_pool_maps; i++) {
3775                 /* set vlan id in VF register and set the valid bit */
3776                 wr32(hw, TXGBE_PSRVLANIDX, i);
3777                 wr32(hw, TXGBE_PSRVLAN, (TXGBE_PSRVLAN_EA |
3778                                 TXGBE_PSRVLAN_VID(cfg->pool_map[i].vlan_id)));
3779                 /*
3780                  * Put the allowed pools in VFB reg. As we only have 16 or 64
3781                  * pools, we only need to use the first half of the register
3782                  * i.e. bits 0-31
3783                  */
3784                 if (((cfg->pool_map[i].pools >> 32) & UINT32_MAX) == 0)
3785                         wr32(hw, TXGBE_PSRVLANPLM(0),
3786                                 (cfg->pool_map[i].pools & UINT32_MAX));
3787                 else
3788                         wr32(hw, TXGBE_PSRVLANPLM(1),
3789                                 ((cfg->pool_map[i].pools >> 32) & UINT32_MAX));
3790         }
3791
3792         /* Tx General Switch Control Enables VMDQ loopback */
3793         if (cfg->enable_loop_back) {
3794                 wr32(hw, TXGBE_PSRCTL, TXGBE_PSRCTL_LBENA);
3795                 for (i = 0; i < 64; i++)
3796                         wr32m(hw, TXGBE_POOLETHCTL(i),
3797                                 TXGBE_POOLETHCTL_LLB, TXGBE_POOLETHCTL_LLB);
3798         }
3799
3800         txgbe_flush(hw);
3801 }
3802
3803 /*
3804  * txgbe_vmdq_tx_hw_configure - Configure general VMDq TX parameters
3805  * @hw: pointer to hardware structure
3806  */
3807 static void
3808 txgbe_vmdq_tx_hw_configure(struct txgbe_hw *hw)
3809 {
3810         uint32_t reg;
3811         uint32_t q;
3812
3813         PMD_INIT_FUNC_TRACE();
3814         /*PF VF Transmit Enable*/
3815         wr32(hw, TXGBE_POOLTXENA(0), UINT32_MAX);
3816         wr32(hw, TXGBE_POOLTXENA(1), UINT32_MAX);
3817
3818         /* Disable the Tx desc arbiter */
3819         reg = rd32(hw, TXGBE_ARBTXCTL);
3820         reg |= TXGBE_ARBTXCTL_DIA;
3821         wr32(hw, TXGBE_ARBTXCTL, reg);
3822
3823         wr32m(hw, TXGBE_PORTCTL, TXGBE_PORTCTL_NUMVT_MASK,
3824                 TXGBE_PORTCTL_NUMVT_64);
3825
3826         /* Disable drop for all queues */
3827         for (q = 0; q < 128; q++) {
3828                 u32 val = 1 << (q % 32);
3829                 wr32m(hw, TXGBE_QPRXDROP(q / 32), val, val);
3830         }
3831
3832         /* Enable the Tx desc arbiter */
3833         reg = rd32(hw, TXGBE_ARBTXCTL);
3834         reg &= ~TXGBE_ARBTXCTL_DIA;
3835         wr32(hw, TXGBE_ARBTXCTL, reg);
3836
3837         txgbe_flush(hw);
3838 }
3839
3840 static int __rte_cold
3841 txgbe_alloc_rx_queue_mbufs(struct txgbe_rx_queue *rxq)
3842 {
3843         struct txgbe_rx_entry *rxe = rxq->sw_ring;
3844         uint64_t dma_addr;
3845         unsigned int i;
3846
3847         /* Initialize software ring entries */
3848         for (i = 0; i < rxq->nb_rx_desc; i++) {
3849                 volatile struct txgbe_rx_desc *rxd;
3850                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
3851
3852                 if (mbuf == NULL) {
3853                         PMD_INIT_LOG(ERR, "RX mbuf alloc failed queue_id=%u",
3854                                      (unsigned int)rxq->queue_id);
3855                         return -ENOMEM;
3856                 }
3857
3858                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
3859                 mbuf->port = rxq->port_id;
3860
3861                 dma_addr =
3862                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
3863                 rxd = &rxq->rx_ring[i];
3864                 TXGBE_RXD_HDRADDR(rxd, 0);
3865                 TXGBE_RXD_PKTADDR(rxd, dma_addr);
3866                 rxe[i].mbuf = mbuf;
3867         }
3868
3869         return 0;
3870 }
3871
3872 static int
3873 txgbe_config_vf_rss(struct rte_eth_dev *dev)
3874 {
3875         struct txgbe_hw *hw;
3876         uint32_t mrqc;
3877
3878         txgbe_rss_configure(dev);
3879
3880         hw = TXGBE_DEV_HW(dev);
3881
3882         /* enable VF RSS */
3883         mrqc = rd32(hw, TXGBE_PORTCTL);
3884         mrqc &= ~(TXGBE_PORTCTL_NUMTC_MASK | TXGBE_PORTCTL_NUMVT_MASK);
3885         switch (RTE_ETH_DEV_SRIOV(dev).active) {
3886         case ETH_64_POOLS:
3887                 mrqc |= TXGBE_PORTCTL_NUMVT_64;
3888                 break;
3889
3890         case ETH_32_POOLS:
3891                 mrqc |= TXGBE_PORTCTL_NUMVT_32;
3892                 break;
3893
3894         default:
3895                 PMD_INIT_LOG(ERR, "Invalid pool number in IOV mode with VMDQ RSS");
3896                 return -EINVAL;
3897         }
3898
3899         wr32(hw, TXGBE_PORTCTL, mrqc);
3900
3901         return 0;
3902 }
3903
3904 static int
3905 txgbe_config_vf_default(struct rte_eth_dev *dev)
3906 {
3907         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3908         uint32_t mrqc;
3909
3910         mrqc = rd32(hw, TXGBE_PORTCTL);
3911         mrqc &= ~(TXGBE_PORTCTL_NUMTC_MASK | TXGBE_PORTCTL_NUMVT_MASK);
3912         switch (RTE_ETH_DEV_SRIOV(dev).active) {
3913         case ETH_64_POOLS:
3914                 mrqc |= TXGBE_PORTCTL_NUMVT_64;
3915                 break;
3916
3917         case ETH_32_POOLS:
3918                 mrqc |= TXGBE_PORTCTL_NUMVT_32;
3919                 break;
3920
3921         case ETH_16_POOLS:
3922                 mrqc |= TXGBE_PORTCTL_NUMVT_16;
3923                 break;
3924         default:
3925                 PMD_INIT_LOG(ERR,
3926                         "invalid pool number in IOV mode");
3927                 return 0;
3928         }
3929
3930         wr32(hw, TXGBE_PORTCTL, mrqc);
3931
3932         return 0;
3933 }
3934
3935 static int
3936 txgbe_dev_mq_rx_configure(struct rte_eth_dev *dev)
3937 {
3938         if (RTE_ETH_DEV_SRIOV(dev).active == 0) {
3939                 /*
3940                  * SRIOV inactive scheme
3941                  * any DCB/RSS w/o VMDq multi-queue setting
3942                  */
3943                 switch (dev->data->dev_conf.rxmode.mq_mode) {
3944                 case ETH_MQ_RX_RSS:
3945                 case ETH_MQ_RX_DCB_RSS:
3946                 case ETH_MQ_RX_VMDQ_RSS:
3947                         txgbe_rss_configure(dev);
3948                         break;
3949
3950                 case ETH_MQ_RX_VMDQ_DCB:
3951                         txgbe_vmdq_dcb_configure(dev);
3952                         break;
3953
3954                 case ETH_MQ_RX_VMDQ_ONLY:
3955                         txgbe_vmdq_rx_hw_configure(dev);
3956                         break;
3957
3958                 case ETH_MQ_RX_NONE:
3959                 default:
3960                         /* if mq_mode is none, disable rss mode.*/
3961                         txgbe_rss_disable(dev);
3962                         break;
3963                 }
3964         } else {
3965                 /* SRIOV active scheme
3966                  * Support RSS together with SRIOV.
3967                  */
3968                 switch (dev->data->dev_conf.rxmode.mq_mode) {
3969                 case ETH_MQ_RX_RSS:
3970                 case ETH_MQ_RX_VMDQ_RSS:
3971                         txgbe_config_vf_rss(dev);
3972                         break;
3973                 case ETH_MQ_RX_VMDQ_DCB:
3974                 case ETH_MQ_RX_DCB:
3975                 /* In SRIOV, the configuration is the same as VMDq case */
3976                         txgbe_vmdq_dcb_configure(dev);
3977                         break;
3978                 /* DCB/RSS together with SRIOV is not supported */
3979                 case ETH_MQ_RX_VMDQ_DCB_RSS:
3980                 case ETH_MQ_RX_DCB_RSS:
3981                         PMD_INIT_LOG(ERR,
3982                                 "Could not support DCB/RSS with VMDq & SRIOV");
3983                         return -1;
3984                 default:
3985                         txgbe_config_vf_default(dev);
3986                         break;
3987                 }
3988         }
3989
3990         return 0;
3991 }
3992
3993 static int
3994 txgbe_dev_mq_tx_configure(struct rte_eth_dev *dev)
3995 {
3996         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3997         uint32_t mtqc;
3998         uint32_t rttdcs;
3999
4000         /* disable arbiter */
4001         rttdcs = rd32(hw, TXGBE_ARBTXCTL);
4002         rttdcs |= TXGBE_ARBTXCTL_DIA;
4003         wr32(hw, TXGBE_ARBTXCTL, rttdcs);
4004
4005         if (RTE_ETH_DEV_SRIOV(dev).active == 0) {
4006                 /*
4007                  * SRIOV inactive scheme
4008                  * any DCB w/o VMDq multi-queue setting
4009                  */
4010                 if (dev->data->dev_conf.txmode.mq_mode == ETH_MQ_TX_VMDQ_ONLY)
4011                         txgbe_vmdq_tx_hw_configure(hw);
4012                 else
4013                         wr32m(hw, TXGBE_PORTCTL, TXGBE_PORTCTL_NUMVT_MASK, 0);
4014         } else {
4015                 switch (RTE_ETH_DEV_SRIOV(dev).active) {
4016                 /*
4017                  * SRIOV active scheme
4018                  * FIXME if support DCB together with VMDq & SRIOV
4019                  */
4020                 case ETH_64_POOLS:
4021                         mtqc = TXGBE_PORTCTL_NUMVT_64;
4022                         break;
4023                 case ETH_32_POOLS:
4024                         mtqc = TXGBE_PORTCTL_NUMVT_32;
4025                         break;
4026                 case ETH_16_POOLS:
4027                         mtqc = TXGBE_PORTCTL_NUMVT_16;
4028                         break;
4029                 default:
4030                         mtqc = 0;
4031                         PMD_INIT_LOG(ERR, "invalid pool number in IOV mode");
4032                 }
4033                 wr32m(hw, TXGBE_PORTCTL, TXGBE_PORTCTL_NUMVT_MASK, mtqc);
4034         }
4035
4036         /* re-enable arbiter */
4037         rttdcs &= ~TXGBE_ARBTXCTL_DIA;
4038         wr32(hw, TXGBE_ARBTXCTL, rttdcs);
4039
4040         return 0;
4041 }
4042
4043 /**
4044  * txgbe_get_rscctl_maxdesc
4045  *
4046  * @pool Memory pool of the Rx queue
4047  */
4048 static inline uint32_t
4049 txgbe_get_rscctl_maxdesc(struct rte_mempool *pool)
4050 {
4051         struct rte_pktmbuf_pool_private *mp_priv = rte_mempool_get_priv(pool);
4052
4053         uint16_t maxdesc =
4054                 RTE_IPV4_MAX_PKT_LEN /
4055                         (mp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM);
4056
4057         if (maxdesc >= 16)
4058                 return TXGBE_RXCFG_RSCMAX_16;
4059         else if (maxdesc >= 8)
4060                 return TXGBE_RXCFG_RSCMAX_8;
4061         else if (maxdesc >= 4)
4062                 return TXGBE_RXCFG_RSCMAX_4;
4063         else
4064                 return TXGBE_RXCFG_RSCMAX_1;
4065 }
4066
4067 /**
4068  * txgbe_set_rsc - configure RSC related port HW registers
4069  *
4070  * Configures the port's RSC related registers.
4071  *
4072  * @dev port handle
4073  *
4074  * Returns 0 in case of success or a non-zero error code
4075  */
4076 static int
4077 txgbe_set_rsc(struct rte_eth_dev *dev)
4078 {
4079         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
4080         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4081         struct rte_eth_dev_info dev_info = { 0 };
4082         bool rsc_capable = false;
4083         uint16_t i;
4084         uint32_t rdrxctl;
4085         uint32_t rfctl;
4086
4087         /* Sanity check */
4088         dev->dev_ops->dev_infos_get(dev, &dev_info);
4089         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO)
4090                 rsc_capable = true;
4091
4092         if (!rsc_capable && (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO)) {
4093                 PMD_INIT_LOG(CRIT, "LRO is requested on HW that doesn't "
4094                                    "support it");
4095                 return -EINVAL;
4096         }
4097
4098         /* RSC global configuration */
4099
4100         if ((rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC) &&
4101              (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO)) {
4102                 PMD_INIT_LOG(CRIT, "LRO can't be enabled when HW CRC "
4103                                     "is disabled");
4104                 return -EINVAL;
4105         }
4106
4107         rfctl = rd32(hw, TXGBE_PSRCTL);
4108         if (rsc_capable && (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO))
4109                 rfctl &= ~TXGBE_PSRCTL_RSCDIA;
4110         else
4111                 rfctl |= TXGBE_PSRCTL_RSCDIA;
4112         wr32(hw, TXGBE_PSRCTL, rfctl);
4113
4114         /* If LRO hasn't been requested - we are done here. */
4115         if (!(rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO))
4116                 return 0;
4117
4118         /* Set PSRCTL.RSCACK bit */
4119         rdrxctl = rd32(hw, TXGBE_PSRCTL);
4120         rdrxctl |= TXGBE_PSRCTL_RSCACK;
4121         wr32(hw, TXGBE_PSRCTL, rdrxctl);
4122
4123         /* Per-queue RSC configuration */
4124         for (i = 0; i < dev->data->nb_rx_queues; i++) {
4125                 struct txgbe_rx_queue *rxq = dev->data->rx_queues[i];
4126                 uint32_t srrctl =
4127                         rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
4128                 uint32_t psrtype =
4129                         rd32(hw, TXGBE_POOLRSS(rxq->reg_idx));
4130                 uint32_t eitr =
4131                         rd32(hw, TXGBE_ITR(rxq->reg_idx));
4132
4133                 /*
4134                  * txgbe PMD doesn't support header-split at the moment.
4135                  */
4136                 srrctl &= ~TXGBE_RXCFG_HDRLEN_MASK;
4137                 srrctl |= TXGBE_RXCFG_HDRLEN(128);
4138
4139                 /*
4140                  * TODO: Consider setting the Receive Descriptor Minimum
4141                  * Threshold Size for an RSC case. This is not an obviously
4142                  * beneficiary option but the one worth considering...
4143                  */
4144
4145                 srrctl |= TXGBE_RXCFG_RSCENA;
4146                 srrctl &= ~TXGBE_RXCFG_RSCMAX_MASK;
4147                 srrctl |= txgbe_get_rscctl_maxdesc(rxq->mb_pool);
4148                 psrtype |= TXGBE_POOLRSS_L4HDR;
4149
4150                 /*
4151                  * RSC: Set ITR interval corresponding to 2K ints/s.
4152                  *
4153                  * Full-sized RSC aggregations for a 10Gb/s link will
4154                  * arrive at about 20K aggregation/s rate.
4155                  *
4156                  * 2K inst/s rate will make only 10% of the
4157                  * aggregations to be closed due to the interrupt timer
4158                  * expiration for a streaming at wire-speed case.
4159                  *
4160                  * For a sparse streaming case this setting will yield
4161                  * at most 500us latency for a single RSC aggregation.
4162                  */
4163                 eitr &= ~TXGBE_ITR_IVAL_MASK;
4164                 eitr |= TXGBE_ITR_IVAL_10G(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT);
4165                 eitr |= TXGBE_ITR_WRDSA;
4166
4167                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), srrctl);
4168                 wr32(hw, TXGBE_POOLRSS(rxq->reg_idx), psrtype);
4169                 wr32(hw, TXGBE_ITR(rxq->reg_idx), eitr);
4170
4171                 /*
4172                  * RSC requires the mapping of the queue to the
4173                  * interrupt vector.
4174                  */
4175                 txgbe_set_ivar_map(hw, 0, rxq->reg_idx, i);
4176         }
4177
4178         dev->data->lro = 1;
4179
4180         PMD_INIT_LOG(DEBUG, "enabling LRO mode");
4181
4182         return 0;
4183 }
4184
4185 void __rte_cold
4186 txgbe_set_rx_function(struct rte_eth_dev *dev)
4187 {
4188         uint16_t i;
4189         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4190
4191         /*
4192          * Initialize the appropriate LRO callback.
4193          *
4194          * If all queues satisfy the bulk allocation preconditions
4195          * (adapter->rx_bulk_alloc_allowed is TRUE) then we may use
4196          * bulk allocation. Otherwise use a single allocation version.
4197          */
4198         if (dev->data->lro) {
4199                 if (adapter->rx_bulk_alloc_allowed) {
4200                         PMD_INIT_LOG(DEBUG, "LRO is requested. Using a bulk "
4201                                            "allocation version");
4202                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_bulk_alloc;
4203                 } else {
4204                         PMD_INIT_LOG(DEBUG, "LRO is requested. Using a single "
4205                                            "allocation version");
4206                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_single_alloc;
4207                 }
4208         } else if (dev->data->scattered_rx) {
4209                 /*
4210                  * Set the non-LRO scattered callback: there are bulk and
4211                  * single allocation versions.
4212                  */
4213                 if (adapter->rx_bulk_alloc_allowed) {
4214                         PMD_INIT_LOG(DEBUG, "Using a Scattered with bulk "
4215                                            "allocation callback (port=%d).",
4216                                      dev->data->port_id);
4217                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_bulk_alloc;
4218                 } else {
4219                         PMD_INIT_LOG(DEBUG, "Using Regular (non-vector, "
4220                                             "single allocation) "
4221                                             "Scattered Rx callback "
4222                                             "(port=%d).",
4223                                      dev->data->port_id);
4224
4225                         dev->rx_pkt_burst = txgbe_recv_pkts_lro_single_alloc;
4226                 }
4227         /*
4228          * Below we set "simple" callbacks according to port/queues parameters.
4229          * If parameters allow we are going to choose between the following
4230          * callbacks:
4231          *    - Bulk Allocation
4232          *    - Single buffer allocation (the simplest one)
4233          */
4234         } else if (adapter->rx_bulk_alloc_allowed) {
4235                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
4236                                     "satisfied. Rx Burst Bulk Alloc function "
4237                                     "will be used on port=%d.",
4238                              dev->data->port_id);
4239
4240                 dev->rx_pkt_burst = txgbe_recv_pkts_bulk_alloc;
4241         } else {
4242                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are not "
4243                                     "satisfied, or Scattered Rx is requested "
4244                                     "(port=%d).",
4245                              dev->data->port_id);
4246
4247                 dev->rx_pkt_burst = txgbe_recv_pkts;
4248         }
4249
4250 #ifdef RTE_LIB_SECURITY
4251         for (i = 0; i < dev->data->nb_rx_queues; i++) {
4252                 struct txgbe_rx_queue *rxq = dev->data->rx_queues[i];
4253
4254                 rxq->using_ipsec = !!(dev->data->dev_conf.rxmode.offloads &
4255                                 DEV_RX_OFFLOAD_SECURITY);
4256         }
4257 #endif
4258 }
4259
4260 /*
4261  * Initializes Receive Unit.
4262  */
4263 int __rte_cold
4264 txgbe_dev_rx_init(struct rte_eth_dev *dev)
4265 {
4266         struct txgbe_hw *hw;
4267         struct txgbe_rx_queue *rxq;
4268         uint64_t bus_addr;
4269         uint32_t fctrl;
4270         uint32_t hlreg0;
4271         uint32_t srrctl;
4272         uint32_t rdrxctl;
4273         uint32_t rxcsum;
4274         uint16_t buf_size;
4275         uint16_t i;
4276         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
4277         int rc;
4278
4279         PMD_INIT_FUNC_TRACE();
4280         hw = TXGBE_DEV_HW(dev);
4281
4282         /*
4283          * Make sure receives are disabled while setting
4284          * up the RX context (registers, descriptor rings, etc.).
4285          */
4286         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, 0);
4287         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, 0);
4288
4289         /* Enable receipt of broadcasted frames */
4290         fctrl = rd32(hw, TXGBE_PSRCTL);
4291         fctrl |= TXGBE_PSRCTL_BCA;
4292         wr32(hw, TXGBE_PSRCTL, fctrl);
4293
4294         /*
4295          * Configure CRC stripping, if any.
4296          */
4297         hlreg0 = rd32(hw, TXGBE_SECRXCTL);
4298         if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4299                 hlreg0 &= ~TXGBE_SECRXCTL_CRCSTRIP;
4300         else
4301                 hlreg0 |= TXGBE_SECRXCTL_CRCSTRIP;
4302         wr32(hw, TXGBE_SECRXCTL, hlreg0);
4303
4304         /*
4305          * Configure jumbo frame support, if any.
4306          */
4307         if (rx_conf->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
4308                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
4309                         TXGBE_FRMSZ_MAX(rx_conf->max_rx_pkt_len));
4310         } else {
4311                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
4312                         TXGBE_FRMSZ_MAX(TXGBE_FRAME_SIZE_DFT));
4313         }
4314
4315         /*
4316          * If loopback mode is configured, set LPBK bit.
4317          */
4318         hlreg0 = rd32(hw, TXGBE_PSRCTL);
4319         if (hw->mac.type == txgbe_mac_raptor &&
4320             dev->data->dev_conf.lpbk_mode)
4321                 hlreg0 |= TXGBE_PSRCTL_LBENA;
4322         else
4323                 hlreg0 &= ~TXGBE_PSRCTL_LBENA;
4324
4325         wr32(hw, TXGBE_PSRCTL, hlreg0);
4326
4327         /*
4328          * Assume no header split and no VLAN strip support
4329          * on any Rx queue first .
4330          */
4331         rx_conf->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
4332
4333         /* Setup RX queues */
4334         for (i = 0; i < dev->data->nb_rx_queues; i++) {
4335                 rxq = dev->data->rx_queues[i];
4336
4337                 /*
4338                  * Reset crc_len in case it was changed after queue setup by a
4339                  * call to configure.
4340                  */
4341                 if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4342                         rxq->crc_len = RTE_ETHER_CRC_LEN;
4343                 else
4344                         rxq->crc_len = 0;
4345
4346                 /* Setup the Base and Length of the Rx Descriptor Rings */
4347                 bus_addr = rxq->rx_ring_phys_addr;
4348                 wr32(hw, TXGBE_RXBAL(rxq->reg_idx),
4349                                 (uint32_t)(bus_addr & BIT_MASK32));
4350                 wr32(hw, TXGBE_RXBAH(rxq->reg_idx),
4351                                 (uint32_t)(bus_addr >> 32));
4352                 wr32(hw, TXGBE_RXRP(rxq->reg_idx), 0);
4353                 wr32(hw, TXGBE_RXWP(rxq->reg_idx), 0);
4354
4355                 srrctl = TXGBE_RXCFG_RNGLEN(rxq->nb_rx_desc);
4356
4357                 /* Set if packets are dropped when no descriptors available */
4358                 if (rxq->drop_en)
4359                         srrctl |= TXGBE_RXCFG_DROP;
4360
4361                 /*
4362                  * Configure the RX buffer size in the PKTLEN field of
4363                  * the RXCFG register of the queue.
4364                  * The value is in 1 KB resolution. Valid values can be from
4365                  * 1 KB to 16 KB.
4366                  */
4367                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
4368                         RTE_PKTMBUF_HEADROOM);
4369                 buf_size = ROUND_UP(buf_size, 0x1 << 10);
4370                 srrctl |= TXGBE_RXCFG_PKTLEN(buf_size);
4371
4372                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), srrctl);
4373
4374                 /* It adds dual VLAN length for supporting dual VLAN */
4375                 if (dev->data->dev_conf.rxmode.max_rx_pkt_len +
4376                                             2 * TXGBE_VLAN_TAG_SIZE > buf_size)
4377                         dev->data->scattered_rx = 1;
4378                 if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
4379                         rx_conf->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
4380         }
4381
4382         if (rx_conf->offloads & DEV_RX_OFFLOAD_SCATTER)
4383                 dev->data->scattered_rx = 1;
4384
4385         /*
4386          * Device configured with multiple RX queues.
4387          */
4388         txgbe_dev_mq_rx_configure(dev);
4389
4390         /*
4391          * Setup the Checksum Register.
4392          * Disable Full-Packet Checksum which is mutually exclusive with RSS.
4393          * Enable IP/L4 checksum computation by hardware if requested to do so.
4394          */
4395         rxcsum = rd32(hw, TXGBE_PSRCTL);
4396         rxcsum |= TXGBE_PSRCTL_PCSD;
4397         if (rx_conf->offloads & DEV_RX_OFFLOAD_CHECKSUM)
4398                 rxcsum |= TXGBE_PSRCTL_L4CSUM;
4399         else
4400                 rxcsum &= ~TXGBE_PSRCTL_L4CSUM;
4401
4402         wr32(hw, TXGBE_PSRCTL, rxcsum);
4403
4404         if (hw->mac.type == txgbe_mac_raptor) {
4405                 rdrxctl = rd32(hw, TXGBE_SECRXCTL);
4406                 if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4407                         rdrxctl &= ~TXGBE_SECRXCTL_CRCSTRIP;
4408                 else
4409                         rdrxctl |= TXGBE_SECRXCTL_CRCSTRIP;
4410                 wr32(hw, TXGBE_SECRXCTL, rdrxctl);
4411         }
4412
4413         rc = txgbe_set_rsc(dev);
4414         if (rc)
4415                 return rc;
4416
4417         txgbe_set_rx_function(dev);
4418
4419         return 0;
4420 }
4421
4422 /*
4423  * Initializes Transmit Unit.
4424  */
4425 void __rte_cold
4426 txgbe_dev_tx_init(struct rte_eth_dev *dev)
4427 {
4428         struct txgbe_hw     *hw;
4429         struct txgbe_tx_queue *txq;
4430         uint64_t bus_addr;
4431         uint16_t i;
4432
4433         PMD_INIT_FUNC_TRACE();
4434         hw = TXGBE_DEV_HW(dev);
4435
4436         /* Setup the Base and Length of the Tx Descriptor Rings */
4437         for (i = 0; i < dev->data->nb_tx_queues; i++) {
4438                 txq = dev->data->tx_queues[i];
4439
4440                 bus_addr = txq->tx_ring_phys_addr;
4441                 wr32(hw, TXGBE_TXBAL(txq->reg_idx),
4442                                 (uint32_t)(bus_addr & BIT_MASK32));
4443                 wr32(hw, TXGBE_TXBAH(txq->reg_idx),
4444                                 (uint32_t)(bus_addr >> 32));
4445                 wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_BUFLEN_MASK,
4446                         TXGBE_TXCFG_BUFLEN(txq->nb_tx_desc));
4447                 /* Setup the HW Tx Head and TX Tail descriptor pointers */
4448                 wr32(hw, TXGBE_TXRP(txq->reg_idx), 0);
4449                 wr32(hw, TXGBE_TXWP(txq->reg_idx), 0);
4450         }
4451
4452         /* Device configured with multiple TX queues. */
4453         txgbe_dev_mq_tx_configure(dev);
4454 }
4455
4456 /*
4457  * Set up link loopback mode Tx->Rx.
4458  */
4459 static inline void __rte_cold
4460 txgbe_setup_loopback_link_raptor(struct txgbe_hw *hw)
4461 {
4462         PMD_INIT_FUNC_TRACE();
4463
4464         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_LB, TXGBE_MACRXCFG_LB);
4465
4466         msec_delay(50);
4467 }
4468
4469 /*
4470  * Start Transmit and Receive Units.
4471  */
4472 int __rte_cold
4473 txgbe_dev_rxtx_start(struct rte_eth_dev *dev)
4474 {
4475         struct txgbe_hw     *hw;
4476         struct txgbe_tx_queue *txq;
4477         struct txgbe_rx_queue *rxq;
4478         uint32_t dmatxctl;
4479         uint32_t rxctrl;
4480         uint16_t i;
4481         int ret = 0;
4482
4483         PMD_INIT_FUNC_TRACE();
4484         hw = TXGBE_DEV_HW(dev);
4485
4486         for (i = 0; i < dev->data->nb_tx_queues; i++) {
4487                 txq = dev->data->tx_queues[i];
4488                 /* Setup Transmit Threshold Registers */
4489                 wr32m(hw, TXGBE_TXCFG(txq->reg_idx),
4490                       TXGBE_TXCFG_HTHRESH_MASK |
4491                       TXGBE_TXCFG_WTHRESH_MASK,
4492                       TXGBE_TXCFG_HTHRESH(txq->hthresh) |
4493                       TXGBE_TXCFG_WTHRESH(txq->wthresh));
4494         }
4495
4496         dmatxctl = rd32(hw, TXGBE_DMATXCTRL);
4497         dmatxctl |= TXGBE_DMATXCTRL_ENA;
4498         wr32(hw, TXGBE_DMATXCTRL, dmatxctl);
4499
4500         for (i = 0; i < dev->data->nb_tx_queues; i++) {
4501                 txq = dev->data->tx_queues[i];
4502                 if (!txq->tx_deferred_start) {
4503                         ret = txgbe_dev_tx_queue_start(dev, i);
4504                         if (ret < 0)
4505                                 return ret;
4506                 }
4507         }
4508
4509         for (i = 0; i < dev->data->nb_rx_queues; i++) {
4510                 rxq = dev->data->rx_queues[i];
4511                 if (!rxq->rx_deferred_start) {
4512                         ret = txgbe_dev_rx_queue_start(dev, i);
4513                         if (ret < 0)
4514                                 return ret;
4515                 }
4516         }
4517
4518         /* Enable Receive engine */
4519         rxctrl = rd32(hw, TXGBE_PBRXCTL);
4520         rxctrl |= TXGBE_PBRXCTL_ENA;
4521         hw->mac.enable_rx_dma(hw, rxctrl);
4522
4523         /* If loopback mode is enabled, set up the link accordingly */
4524         if (hw->mac.type == txgbe_mac_raptor &&
4525             dev->data->dev_conf.lpbk_mode)
4526                 txgbe_setup_loopback_link_raptor(hw);
4527
4528 #ifdef RTE_LIB_SECURITY
4529         if ((dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SECURITY) ||
4530             (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_SECURITY)) {
4531                 ret = txgbe_crypto_enable_ipsec(dev);
4532                 if (ret != 0) {
4533                         PMD_DRV_LOG(ERR,
4534                                     "txgbe_crypto_enable_ipsec fails with %d.",
4535                                     ret);
4536                         return ret;
4537                 }
4538         }
4539 #endif
4540
4541         return 0;
4542 }
4543
4544 void
4545 txgbe_dev_save_rx_queue(struct txgbe_hw *hw, uint16_t rx_queue_id)
4546 {
4547         u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
4548         *(reg++) = rd32(hw, TXGBE_RXBAL(rx_queue_id));
4549         *(reg++) = rd32(hw, TXGBE_RXBAH(rx_queue_id));
4550         *(reg++) = rd32(hw, TXGBE_RXCFG(rx_queue_id));
4551 }
4552
4553 void
4554 txgbe_dev_store_rx_queue(struct txgbe_hw *hw, uint16_t rx_queue_id)
4555 {
4556         u32 *reg = &hw->q_rx_regs[rx_queue_id * 8];
4557         wr32(hw, TXGBE_RXBAL(rx_queue_id), *(reg++));
4558         wr32(hw, TXGBE_RXBAH(rx_queue_id), *(reg++));
4559         wr32(hw, TXGBE_RXCFG(rx_queue_id), *(reg++) & ~TXGBE_RXCFG_ENA);
4560 }
4561
4562 void
4563 txgbe_dev_save_tx_queue(struct txgbe_hw *hw, uint16_t tx_queue_id)
4564 {
4565         u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
4566         *(reg++) = rd32(hw, TXGBE_TXBAL(tx_queue_id));
4567         *(reg++) = rd32(hw, TXGBE_TXBAH(tx_queue_id));
4568         *(reg++) = rd32(hw, TXGBE_TXCFG(tx_queue_id));
4569 }
4570
4571 void
4572 txgbe_dev_store_tx_queue(struct txgbe_hw *hw, uint16_t tx_queue_id)
4573 {
4574         u32 *reg = &hw->q_tx_regs[tx_queue_id * 8];
4575         wr32(hw, TXGBE_TXBAL(tx_queue_id), *(reg++));
4576         wr32(hw, TXGBE_TXBAH(tx_queue_id), *(reg++));
4577         wr32(hw, TXGBE_TXCFG(tx_queue_id), *(reg++) & ~TXGBE_TXCFG_ENA);
4578 }
4579
4580 /*
4581  * Start Receive Units for specified queue.
4582  */
4583 int __rte_cold
4584 txgbe_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4585 {
4586         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4587         struct txgbe_rx_queue *rxq;
4588         uint32_t rxdctl;
4589         int poll_ms;
4590
4591         PMD_INIT_FUNC_TRACE();
4592
4593         rxq = dev->data->rx_queues[rx_queue_id];
4594
4595         /* Allocate buffers for descriptor rings */
4596         if (txgbe_alloc_rx_queue_mbufs(rxq) != 0) {
4597                 PMD_INIT_LOG(ERR, "Could not alloc mbuf for queue:%d",
4598                              rx_queue_id);
4599                 return -1;
4600         }
4601         rxdctl = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
4602         rxdctl |= TXGBE_RXCFG_ENA;
4603         wr32(hw, TXGBE_RXCFG(rxq->reg_idx), rxdctl);
4604
4605         /* Wait until RX Enable ready */
4606         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
4607         do {
4608                 rte_delay_ms(1);
4609                 rxdctl = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
4610         } while (--poll_ms && !(rxdctl & TXGBE_RXCFG_ENA));
4611         if (!poll_ms)
4612                 PMD_INIT_LOG(ERR, "Could not enable Rx Queue %d", rx_queue_id);
4613         rte_wmb();
4614         wr32(hw, TXGBE_RXRP(rxq->reg_idx), 0);
4615         wr32(hw, TXGBE_RXWP(rxq->reg_idx), rxq->nb_rx_desc - 1);
4616         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4617
4618         return 0;
4619 }
4620
4621 /*
4622  * Stop Receive Units for specified queue.
4623  */
4624 int __rte_cold
4625 txgbe_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4626 {
4627         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4628         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4629         struct txgbe_rx_queue *rxq;
4630         uint32_t rxdctl;
4631         int poll_ms;
4632
4633         PMD_INIT_FUNC_TRACE();
4634
4635         rxq = dev->data->rx_queues[rx_queue_id];
4636
4637         txgbe_dev_save_rx_queue(hw, rxq->reg_idx);
4638         wr32m(hw, TXGBE_RXCFG(rxq->reg_idx), TXGBE_RXCFG_ENA, 0);
4639
4640         /* Wait until RX Enable bit clear */
4641         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
4642         do {
4643                 rte_delay_ms(1);
4644                 rxdctl = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
4645         } while (--poll_ms && (rxdctl & TXGBE_RXCFG_ENA));
4646         if (!poll_ms)
4647                 PMD_INIT_LOG(ERR, "Could not disable Rx Queue %d", rx_queue_id);
4648
4649         rte_delay_us(RTE_TXGBE_WAIT_100_US);
4650         txgbe_dev_store_rx_queue(hw, rxq->reg_idx);
4651
4652         txgbe_rx_queue_release_mbufs(rxq);
4653         txgbe_reset_rx_queue(adapter, rxq);
4654         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4655
4656         return 0;
4657 }
4658
4659 /*
4660  * Start Transmit Units for specified queue.
4661  */
4662 int __rte_cold
4663 txgbe_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
4664 {
4665         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4666         struct txgbe_tx_queue *txq;
4667         uint32_t txdctl;
4668         int poll_ms;
4669
4670         PMD_INIT_FUNC_TRACE();
4671
4672         txq = dev->data->tx_queues[tx_queue_id];
4673         wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_ENA, TXGBE_TXCFG_ENA);
4674
4675         /* Wait until TX Enable ready */
4676         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
4677         do {
4678                 rte_delay_ms(1);
4679                 txdctl = rd32(hw, TXGBE_TXCFG(txq->reg_idx));
4680         } while (--poll_ms && !(txdctl & TXGBE_TXCFG_ENA));
4681         if (!poll_ms)
4682                 PMD_INIT_LOG(ERR, "Could not enable "
4683                              "Tx Queue %d", tx_queue_id);
4684
4685         rte_wmb();
4686         wr32(hw, TXGBE_TXWP(txq->reg_idx), txq->tx_tail);
4687         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4688
4689         return 0;
4690 }
4691
4692 /*
4693  * Stop Transmit Units for specified queue.
4694  */
4695 int __rte_cold
4696 txgbe_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
4697 {
4698         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4699         struct txgbe_tx_queue *txq;
4700         uint32_t txdctl;
4701         uint32_t txtdh, txtdt;
4702         int poll_ms;
4703
4704         PMD_INIT_FUNC_TRACE();
4705
4706         txq = dev->data->tx_queues[tx_queue_id];
4707
4708         /* Wait until TX queue is empty */
4709         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
4710         do {
4711                 rte_delay_us(RTE_TXGBE_WAIT_100_US);
4712                 txtdh = rd32(hw, TXGBE_TXRP(txq->reg_idx));
4713                 txtdt = rd32(hw, TXGBE_TXWP(txq->reg_idx));
4714         } while (--poll_ms && (txtdh != txtdt));
4715         if (!poll_ms)
4716                 PMD_INIT_LOG(ERR,
4717                         "Tx Queue %d is not empty when stopping.",
4718                         tx_queue_id);
4719
4720         txgbe_dev_save_tx_queue(hw, txq->reg_idx);
4721         wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_ENA, 0);
4722
4723         /* Wait until TX Enable bit clear */
4724         poll_ms = RTE_TXGBE_REGISTER_POLL_WAIT_10_MS;
4725         do {
4726                 rte_delay_ms(1);
4727                 txdctl = rd32(hw, TXGBE_TXCFG(txq->reg_idx));
4728         } while (--poll_ms && (txdctl & TXGBE_TXCFG_ENA));
4729         if (!poll_ms)
4730                 PMD_INIT_LOG(ERR, "Could not disable Tx Queue %d",
4731                         tx_queue_id);
4732
4733         rte_delay_us(RTE_TXGBE_WAIT_100_US);
4734         txgbe_dev_store_tx_queue(hw, txq->reg_idx);
4735
4736         if (txq->ops != NULL) {
4737                 txq->ops->release_mbufs(txq);
4738                 txq->ops->reset(txq);
4739         }
4740         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4741
4742         return 0;
4743 }
4744
4745 void
4746 txgbe_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
4747         struct rte_eth_rxq_info *qinfo)
4748 {
4749         struct txgbe_rx_queue *rxq;
4750
4751         rxq = dev->data->rx_queues[queue_id];
4752
4753         qinfo->mp = rxq->mb_pool;
4754         qinfo->scattered_rx = dev->data->scattered_rx;
4755         qinfo->nb_desc = rxq->nb_rx_desc;
4756
4757         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
4758         qinfo->conf.rx_drop_en = rxq->drop_en;
4759         qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
4760         qinfo->conf.offloads = rxq->offloads;
4761 }
4762
4763 void
4764 txgbe_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
4765         struct rte_eth_txq_info *qinfo)
4766 {
4767         struct txgbe_tx_queue *txq;
4768
4769         txq = dev->data->tx_queues[queue_id];
4770
4771         qinfo->nb_desc = txq->nb_tx_desc;
4772
4773         qinfo->conf.tx_thresh.pthresh = txq->pthresh;
4774         qinfo->conf.tx_thresh.hthresh = txq->hthresh;
4775         qinfo->conf.tx_thresh.wthresh = txq->wthresh;
4776
4777         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
4778         qinfo->conf.offloads = txq->offloads;
4779         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
4780 }
4781
4782 /*
4783  * [VF] Initializes Receive Unit.
4784  */
4785 int __rte_cold
4786 txgbevf_dev_rx_init(struct rte_eth_dev *dev)
4787 {
4788         struct txgbe_hw     *hw;
4789         struct txgbe_rx_queue *rxq;
4790         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
4791         uint64_t bus_addr;
4792         uint32_t srrctl, psrtype;
4793         uint16_t buf_size;
4794         uint16_t i;
4795         int ret;
4796
4797         PMD_INIT_FUNC_TRACE();
4798         hw = TXGBE_DEV_HW(dev);
4799
4800         if (rte_is_power_of_2(dev->data->nb_rx_queues) == 0) {
4801                 PMD_INIT_LOG(ERR, "The number of Rx queue invalid, "
4802                         "it should be power of 2");
4803                 return -1;
4804         }
4805
4806         if (dev->data->nb_rx_queues > hw->mac.max_rx_queues) {
4807                 PMD_INIT_LOG(ERR, "The number of Rx queue invalid, "
4808                         "it should be equal to or less than %d",
4809                         hw->mac.max_rx_queues);
4810                 return -1;
4811         }
4812
4813         /*
4814          * When the VF driver issues a TXGBE_VF_RESET request, the PF driver
4815          * disables the VF receipt of packets if the PF MTU is > 1500.
4816          * This is done to deal with limitations that imposes
4817          * the PF and all VFs to share the same MTU.
4818          * Then, the PF driver enables again the VF receipt of packet when
4819          * the VF driver issues a TXGBE_VF_SET_LPE request.
4820          * In the meantime, the VF device cannot be used, even if the VF driver
4821          * and the Guest VM network stack are ready to accept packets with a
4822          * size up to the PF MTU.
4823          * As a work-around to this PF behaviour, force the call to
4824          * txgbevf_rlpml_set_vf even if jumbo frames are not used. This way,
4825          * VF packets received can work in all cases.
4826          */
4827         if (txgbevf_rlpml_set_vf(hw,
4828             (uint16_t)dev->data->dev_conf.rxmode.max_rx_pkt_len)) {
4829                 PMD_INIT_LOG(ERR, "Set max packet length to %d failed.",
4830                              dev->data->dev_conf.rxmode.max_rx_pkt_len);
4831                 return -EINVAL;
4832         }
4833
4834         /*
4835          * Assume no header split and no VLAN strip support
4836          * on any Rx queue first .
4837          */
4838         rxmode->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
4839
4840         /* Set PSR type for VF RSS according to max Rx queue */
4841         psrtype = TXGBE_VFPLCFG_PSRL4HDR |
4842                   TXGBE_VFPLCFG_PSRL4HDR |
4843                   TXGBE_VFPLCFG_PSRL2HDR |
4844                   TXGBE_VFPLCFG_PSRTUNHDR |
4845                   TXGBE_VFPLCFG_PSRTUNMAC;
4846         wr32(hw, TXGBE_VFPLCFG, TXGBE_VFPLCFG_PSR(psrtype));
4847
4848         /* Setup RX queues */
4849         for (i = 0; i < dev->data->nb_rx_queues; i++) {
4850                 rxq = dev->data->rx_queues[i];
4851
4852                 /* Allocate buffers for descriptor rings */
4853                 ret = txgbe_alloc_rx_queue_mbufs(rxq);
4854                 if (ret)
4855                         return ret;
4856
4857                 /* Setup the Base and Length of the Rx Descriptor Rings */
4858                 bus_addr = rxq->rx_ring_phys_addr;
4859
4860                 wr32(hw, TXGBE_RXBAL(i),
4861                                 (uint32_t)(bus_addr & BIT_MASK32));
4862                 wr32(hw, TXGBE_RXBAH(i),
4863                                 (uint32_t)(bus_addr >> 32));
4864                 wr32(hw, TXGBE_RXRP(i), 0);
4865                 wr32(hw, TXGBE_RXWP(i), 0);
4866
4867                 /* Configure the RXCFG register */
4868                 srrctl = TXGBE_RXCFG_RNGLEN(rxq->nb_rx_desc);
4869
4870                 /* Set if packets are dropped when no descriptors available */
4871                 if (rxq->drop_en)
4872                         srrctl |= TXGBE_RXCFG_DROP;
4873
4874                 /*
4875                  * Configure the RX buffer size in the PKTLEN field of
4876                  * the RXCFG register of the queue.
4877                  * The value is in 1 KB resolution. Valid values can be from
4878                  * 1 KB to 16 KB.
4879                  */
4880                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
4881                         RTE_PKTMBUF_HEADROOM);
4882                 buf_size = ROUND_UP(buf_size, 1 << 10);
4883                 srrctl |= TXGBE_RXCFG_PKTLEN(buf_size);
4884
4885                 /*
4886                  * VF modification to write virtual function RXCFG register
4887                  */
4888                 wr32(hw, TXGBE_RXCFG(i), srrctl);
4889
4890                 if (rxmode->offloads & DEV_RX_OFFLOAD_SCATTER ||
4891                     /* It adds dual VLAN length for supporting dual VLAN */
4892                     (rxmode->max_rx_pkt_len +
4893                                 2 * TXGBE_VLAN_TAG_SIZE) > buf_size) {
4894                         if (!dev->data->scattered_rx)
4895                                 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
4896                         dev->data->scattered_rx = 1;
4897                 }
4898
4899                 if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
4900                         rxmode->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
4901         }
4902
4903         /*
4904          * Device configured with multiple RX queues.
4905          */
4906         txgbe_dev_mq_rx_configure(dev);
4907
4908         txgbe_set_rx_function(dev);
4909
4910         return 0;
4911 }
4912
4913 /*
4914  * [VF] Initializes Transmit Unit.
4915  */
4916 void __rte_cold
4917 txgbevf_dev_tx_init(struct rte_eth_dev *dev)
4918 {
4919         struct txgbe_hw     *hw;
4920         struct txgbe_tx_queue *txq;
4921         uint64_t bus_addr;
4922         uint16_t i;
4923
4924         PMD_INIT_FUNC_TRACE();
4925         hw = TXGBE_DEV_HW(dev);
4926
4927         /* Setup the Base and Length of the Tx Descriptor Rings */
4928         for (i = 0; i < dev->data->nb_tx_queues; i++) {
4929                 txq = dev->data->tx_queues[i];
4930                 bus_addr = txq->tx_ring_phys_addr;
4931                 wr32(hw, TXGBE_TXBAL(i),
4932                                 (uint32_t)(bus_addr & BIT_MASK32));
4933                 wr32(hw, TXGBE_TXBAH(i),
4934                                 (uint32_t)(bus_addr >> 32));
4935                 wr32m(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_BUFLEN_MASK,
4936                         TXGBE_TXCFG_BUFLEN(txq->nb_tx_desc));
4937                 /* Setup the HW Tx Head and TX Tail descriptor pointers */
4938                 wr32(hw, TXGBE_TXRP(i), 0);
4939                 wr32(hw, TXGBE_TXWP(i), 0);
4940         }
4941 }
4942
4943 /*
4944  * [VF] Start Transmit and Receive Units.
4945  */
4946 void __rte_cold
4947 txgbevf_dev_rxtx_start(struct rte_eth_dev *dev)
4948 {
4949         struct txgbe_hw     *hw;
4950         struct txgbe_tx_queue *txq;
4951         struct txgbe_rx_queue *rxq;
4952         uint32_t txdctl;
4953         uint32_t rxdctl;
4954         uint16_t i;
4955         int poll_ms;
4956
4957         PMD_INIT_FUNC_TRACE();
4958         hw = TXGBE_DEV_HW(dev);
4959
4960         for (i = 0; i < dev->data->nb_tx_queues; i++) {
4961                 txq = dev->data->tx_queues[i];
4962                 /* Setup Transmit Threshold Registers */
4963                 wr32m(hw, TXGBE_TXCFG(txq->reg_idx),
4964                       TXGBE_TXCFG_HTHRESH_MASK |
4965                       TXGBE_TXCFG_WTHRESH_MASK,
4966                       TXGBE_TXCFG_HTHRESH(txq->hthresh) |
4967                       TXGBE_TXCFG_WTHRESH(txq->wthresh));
4968         }
4969
4970         for (i = 0; i < dev->data->nb_tx_queues; i++) {
4971                 wr32m(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_ENA, TXGBE_TXCFG_ENA);
4972
4973                 poll_ms = 10;
4974                 /* Wait until TX Enable ready */
4975                 do {
4976                         rte_delay_ms(1);
4977                         txdctl = rd32(hw, TXGBE_TXCFG(i));
4978                 } while (--poll_ms && !(txdctl & TXGBE_TXCFG_ENA));
4979                 if (!poll_ms)
4980                         PMD_INIT_LOG(ERR, "Could not enable Tx Queue %d", i);
4981         }
4982         for (i = 0; i < dev->data->nb_rx_queues; i++) {
4983                 rxq = dev->data->rx_queues[i];
4984
4985                 wr32m(hw, TXGBE_RXCFG(i), TXGBE_RXCFG_ENA, TXGBE_RXCFG_ENA);
4986
4987                 /* Wait until RX Enable ready */
4988                 poll_ms = 10;
4989                 do {
4990                         rte_delay_ms(1);
4991                         rxdctl = rd32(hw, TXGBE_RXCFG(i));
4992                 } while (--poll_ms && !(rxdctl & TXGBE_RXCFG_ENA));
4993                 if (!poll_ms)
4994                         PMD_INIT_LOG(ERR, "Could not enable Rx Queue %d", i);
4995                 rte_wmb();
4996                 wr32(hw, TXGBE_RXWP(i), rxq->nb_rx_desc - 1);
4997         }
4998 }
4999
5000 int
5001 txgbe_rss_conf_init(struct txgbe_rte_flow_rss_conf *out,
5002                     const struct rte_flow_action_rss *in)
5003 {
5004         if (in->key_len > RTE_DIM(out->key) ||
5005             in->queue_num > RTE_DIM(out->queue))
5006                 return -EINVAL;
5007         out->conf = (struct rte_flow_action_rss){
5008                 .func = in->func,
5009                 .level = in->level,
5010                 .types = in->types,
5011                 .key_len = in->key_len,
5012                 .queue_num = in->queue_num,
5013                 .key = memcpy(out->key, in->key, in->key_len),
5014                 .queue = memcpy(out->queue, in->queue,
5015                                 sizeof(*in->queue) * in->queue_num),
5016         };
5017         return 0;
5018 }
5019
5020 int
5021 txgbe_action_rss_same(const struct rte_flow_action_rss *comp,
5022                       const struct rte_flow_action_rss *with)
5023 {
5024         return (comp->func == with->func &&
5025                 comp->level == with->level &&
5026                 comp->types == with->types &&
5027                 comp->key_len == with->key_len &&
5028                 comp->queue_num == with->queue_num &&
5029                 !memcmp(comp->key, with->key, with->key_len) &&
5030                 !memcmp(comp->queue, with->queue,
5031                         sizeof(*with->queue) * with->queue_num));
5032 }
5033
5034 int
5035 txgbe_config_rss_filter(struct rte_eth_dev *dev,
5036                 struct txgbe_rte_flow_rss_conf *conf, bool add)
5037 {
5038         struct txgbe_hw *hw;
5039         uint32_t reta;
5040         uint16_t i;
5041         uint16_t j;
5042         struct rte_eth_rss_conf rss_conf = {
5043                 .rss_key = conf->conf.key_len ?
5044                         (void *)(uintptr_t)conf->conf.key : NULL,
5045                 .rss_key_len = conf->conf.key_len,
5046                 .rss_hf = conf->conf.types,
5047         };
5048         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
5049
5050         PMD_INIT_FUNC_TRACE();
5051         hw = TXGBE_DEV_HW(dev);
5052
5053         if (!add) {
5054                 if (txgbe_action_rss_same(&filter_info->rss_info.conf,
5055                                           &conf->conf)) {
5056                         txgbe_rss_disable(dev);
5057                         memset(&filter_info->rss_info, 0,
5058                                 sizeof(struct txgbe_rte_flow_rss_conf));
5059                         return 0;
5060                 }
5061                 return -EINVAL;
5062         }
5063
5064         if (filter_info->rss_info.conf.queue_num)
5065                 return -EINVAL;
5066         /* Fill in redirection table
5067          * The byte-swap is needed because NIC registers are in
5068          * little-endian order.
5069          */
5070         reta = 0;
5071         for (i = 0, j = 0; i < ETH_RSS_RETA_SIZE_128; i++, j++) {
5072                 if (j == conf->conf.queue_num)
5073                         j = 0;
5074                 reta = (reta >> 8) | LS32(conf->conf.queue[j], 24, 0xFF);
5075                 if ((i & 3) == 3)
5076                         wr32at(hw, TXGBE_REG_RSSTBL, i >> 2, reta);
5077         }
5078
5079         /* Configure the RSS key and the RSS protocols used to compute
5080          * the RSS hash of input packets.
5081          */
5082         if ((rss_conf.rss_hf & TXGBE_RSS_OFFLOAD_ALL) == 0) {
5083                 txgbe_rss_disable(dev);
5084                 return 0;
5085         }
5086         if (rss_conf.rss_key == NULL)
5087                 rss_conf.rss_key = rss_intel_key; /* Default hash key */
5088         txgbe_dev_rss_hash_update(dev, &rss_conf);
5089
5090         if (txgbe_rss_conf_init(&filter_info->rss_info, &conf->conf))
5091                 return -EINVAL;
5092
5093         return 0;
5094 }
5095