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