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