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