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