New upstream version 16.11.5
[deb_dpdk.git] / drivers / net / i40e / i40e_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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 <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <stdint.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42 #include <sys/queue.h>
43
44 #include <rte_string_fns.h>
45 #include <rte_memzone.h>
46 #include <rte_mbuf.h>
47 #include <rte_malloc.h>
48 #include <rte_ether.h>
49 #include <rte_ethdev.h>
50 #include <rte_tcp.h>
51 #include <rte_sctp.h>
52 #include <rte_udp.h>
53
54 #include "i40e_logs.h"
55 #include "base/i40e_prototype.h"
56 #include "base/i40e_type.h"
57 #include "i40e_ethdev.h"
58 #include "i40e_rxtx.h"
59
60 #define DEFAULT_TX_RS_THRESH   32
61 #define DEFAULT_TX_FREE_THRESH 32
62 #define I40E_MAX_PKT_TYPE      256
63
64 #define I40E_TX_MAX_BURST  32
65
66 #define I40E_DMA_MEM_ALIGN 4096
67
68 /* Base address of the HW descriptor ring should be 128B aligned. */
69 #define I40E_RING_BASE_ALIGN    128
70
71 #define I40E_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
72                                         ETH_TXQ_FLAGS_NOOFFLOADS)
73
74 #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
75
76 #define I40E_TX_CKSUM_OFFLOAD_MASK (             \
77                 PKT_TX_IP_CKSUM |                \
78                 PKT_TX_L4_MASK |                 \
79                 PKT_TX_TCP_SEG |                 \
80                 PKT_TX_OUTER_IP_CKSUM)
81
82 static uint16_t i40e_xmit_pkts_simple(void *tx_queue,
83                                       struct rte_mbuf **tx_pkts,
84                                       uint16_t nb_pkts);
85
86 static inline void
87 i40e_rxd_to_vlan_tci(struct rte_mbuf *mb, volatile union i40e_rx_desc *rxdp)
88 {
89         if (rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len) &
90                 (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) {
91                 mb->ol_flags |= PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED;
92                 mb->vlan_tci =
93                         rte_le_to_cpu_16(rxdp->wb.qword0.lo_dword.l2tag1);
94                 PMD_RX_LOG(DEBUG, "Descriptor l2tag1: %u",
95                            rte_le_to_cpu_16(rxdp->wb.qword0.lo_dword.l2tag1));
96         } else {
97                 mb->vlan_tci = 0;
98         }
99 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
100         if (rte_le_to_cpu_16(rxdp->wb.qword2.ext_status) &
101                 (1 << I40E_RX_DESC_EXT_STATUS_L2TAG2P_SHIFT)) {
102                 mb->ol_flags |= PKT_RX_QINQ_STRIPPED;
103                 mb->vlan_tci_outer = mb->vlan_tci;
104                 mb->vlan_tci = rte_le_to_cpu_16(rxdp->wb.qword2.l2tag2_2);
105                 PMD_RX_LOG(DEBUG, "Descriptor l2tag2_1: %u, l2tag2_2: %u",
106                            rte_le_to_cpu_16(rxdp->wb.qword2.l2tag2_1),
107                            rte_le_to_cpu_16(rxdp->wb.qword2.l2tag2_2));
108         } else {
109                 mb->vlan_tci_outer = 0;
110         }
111 #endif
112         PMD_RX_LOG(DEBUG, "Mbuf vlan_tci: %u, vlan_tci_outer: %u",
113                    mb->vlan_tci, mb->vlan_tci_outer);
114 }
115
116 /* Translate the rx descriptor status to pkt flags */
117 static inline uint64_t
118 i40e_rxd_status_to_pkt_flags(uint64_t qword)
119 {
120         uint64_t flags;
121
122         /* Check if RSS_HASH */
123         flags = (((qword >> I40E_RX_DESC_STATUS_FLTSTAT_SHIFT) &
124                                         I40E_RX_DESC_FLTSTAT_RSS_HASH) ==
125                         I40E_RX_DESC_FLTSTAT_RSS_HASH) ? PKT_RX_RSS_HASH : 0;
126
127         /* Check if FDIR Match */
128         flags |= (qword & (1 << I40E_RX_DESC_STATUS_FLM_SHIFT) ?
129                                                         PKT_RX_FDIR : 0);
130
131         return flags;
132 }
133
134 static inline uint64_t
135 i40e_rxd_error_to_pkt_flags(uint64_t qword)
136 {
137         uint64_t flags = 0;
138         uint64_t error_bits = (qword >> I40E_RXD_QW1_ERROR_SHIFT);
139
140 #define I40E_RX_ERR_BITS 0x3f
141         if (likely((error_bits & I40E_RX_ERR_BITS) == 0)) {
142                 flags |= (PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD);
143                 return flags;
144         }
145
146         if (unlikely(error_bits & (1 << I40E_RX_DESC_ERROR_IPE_SHIFT)))
147                 flags |= PKT_RX_IP_CKSUM_BAD;
148         else
149                 flags |= PKT_RX_IP_CKSUM_GOOD;
150
151         if (unlikely(error_bits & (1 << I40E_RX_DESC_ERROR_L4E_SHIFT)))
152                 flags |= PKT_RX_L4_CKSUM_BAD;
153         else
154                 flags |= PKT_RX_L4_CKSUM_GOOD;
155
156         if (unlikely(error_bits & (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT)))
157                 flags |= PKT_RX_EIP_CKSUM_BAD;
158
159         return flags;
160 }
161
162 /* Function to check and set the ieee1588 timesync index and get the
163  * appropriate flags.
164  */
165 #ifdef RTE_LIBRTE_IEEE1588
166 static inline uint64_t
167 i40e_get_iee15888_flags(struct rte_mbuf *mb, uint64_t qword)
168 {
169         uint64_t pkt_flags = 0;
170         uint16_t tsyn = (qword & (I40E_RXD_QW1_STATUS_TSYNVALID_MASK
171                                   | I40E_RXD_QW1_STATUS_TSYNINDX_MASK))
172                                     >> I40E_RX_DESC_STATUS_TSYNINDX_SHIFT;
173
174         if ((mb->packet_type & RTE_PTYPE_L2_MASK)
175                         == RTE_PTYPE_L2_ETHER_TIMESYNC)
176                 pkt_flags = PKT_RX_IEEE1588_PTP;
177         if (tsyn & 0x04) {
178                 pkt_flags |= PKT_RX_IEEE1588_TMST;
179                 mb->timesync = tsyn & 0x03;
180         }
181
182         return pkt_flags;
183 }
184 #endif
185
186 #define I40E_RX_DESC_EXT_STATUS_FLEXBH_MASK   0x03
187 #define I40E_RX_DESC_EXT_STATUS_FLEXBH_FD_ID  0x01
188 #define I40E_RX_DESC_EXT_STATUS_FLEXBH_FLEX   0x02
189 #define I40E_RX_DESC_EXT_STATUS_FLEXBL_MASK   0x03
190 #define I40E_RX_DESC_EXT_STATUS_FLEXBL_FLEX   0x01
191
192 static inline uint64_t
193 i40e_rxd_build_fdir(volatile union i40e_rx_desc *rxdp, struct rte_mbuf *mb)
194 {
195         uint64_t flags = 0;
196 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
197         uint16_t flexbh, flexbl;
198
199         flexbh = (rte_le_to_cpu_32(rxdp->wb.qword2.ext_status) >>
200                 I40E_RX_DESC_EXT_STATUS_FLEXBH_SHIFT) &
201                 I40E_RX_DESC_EXT_STATUS_FLEXBH_MASK;
202         flexbl = (rte_le_to_cpu_32(rxdp->wb.qword2.ext_status) >>
203                 I40E_RX_DESC_EXT_STATUS_FLEXBL_SHIFT) &
204                 I40E_RX_DESC_EXT_STATUS_FLEXBL_MASK;
205
206
207         if (flexbh == I40E_RX_DESC_EXT_STATUS_FLEXBH_FD_ID) {
208                 mb->hash.fdir.hi =
209                         rte_le_to_cpu_32(rxdp->wb.qword3.hi_dword.fd_id);
210                 flags |= PKT_RX_FDIR_ID;
211         } else if (flexbh == I40E_RX_DESC_EXT_STATUS_FLEXBH_FLEX) {
212                 mb->hash.fdir.hi =
213                         rte_le_to_cpu_32(rxdp->wb.qword3.hi_dword.flex_bytes_hi);
214                 flags |= PKT_RX_FDIR_FLX;
215         }
216         if (flexbl == I40E_RX_DESC_EXT_STATUS_FLEXBL_FLEX) {
217                 mb->hash.fdir.lo =
218                         rte_le_to_cpu_32(rxdp->wb.qword3.lo_dword.flex_bytes_lo);
219                 flags |= PKT_RX_FDIR_FLX;
220         }
221 #else
222         mb->hash.fdir.hi =
223                 rte_le_to_cpu_32(rxdp->wb.qword0.hi_dword.fd_id);
224         flags |= PKT_RX_FDIR_ID;
225 #endif
226         return flags;
227 }
228
229 static inline void
230 i40e_parse_tunneling_params(uint64_t ol_flags,
231                             union i40e_tx_offload tx_offload,
232                             uint32_t *cd_tunneling)
233 {
234         /* EIPT: External (outer) IP header type */
235         if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
236                 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
237         else if (ol_flags & PKT_TX_OUTER_IPV4)
238                 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
239         else if (ol_flags & PKT_TX_OUTER_IPV6)
240                 *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
241
242         /* EIPLEN: External (outer) IP header length, in DWords */
243         *cd_tunneling |= (tx_offload.outer_l3_len >> 2) <<
244                 I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
245
246         /* L4TUNT: L4 Tunneling Type */
247         switch (ol_flags & PKT_TX_TUNNEL_MASK) {
248         case PKT_TX_TUNNEL_IPIP:
249                 /* for non UDP / GRE tunneling, set to 00b */
250                 break;
251         case PKT_TX_TUNNEL_VXLAN:
252         case PKT_TX_TUNNEL_GENEVE:
253                 *cd_tunneling |= I40E_TXD_CTX_UDP_TUNNELING;
254                 break;
255         case PKT_TX_TUNNEL_GRE:
256                 *cd_tunneling |= I40E_TXD_CTX_GRE_TUNNELING;
257                 break;
258         default:
259                 PMD_TX_LOG(ERR, "Tunnel type not supported\n");
260                 return;
261         }
262
263         /* L4TUNLEN: L4 Tunneling Length, in Words
264          *
265          * We depend on app to set rte_mbuf.l2_len correctly.
266          * For IP in GRE it should be set to the length of the GRE
267          * header;
268          * for MAC in GRE or MAC in UDP it should be set to the length
269          * of the GRE or UDP headers plus the inner MAC up to including
270          * its last Ethertype.
271          */
272         *cd_tunneling |= (tx_offload.l2_len >> 1) <<
273                 I40E_TXD_CTX_QW0_NATLEN_SHIFT;
274 }
275
276 static inline void
277 i40e_txd_enable_checksum(uint64_t ol_flags,
278                         uint32_t *td_cmd,
279                         uint32_t *td_offset,
280                         union i40e_tx_offload tx_offload)
281 {
282         /* Set MACLEN */
283         if (ol_flags & PKT_TX_TUNNEL_MASK)
284                 *td_offset |= (tx_offload.outer_l2_len >> 1)
285                                 << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
286         else
287                 *td_offset |= (tx_offload.l2_len >> 1)
288                         << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
289
290         /* Enable L3 checksum offloads */
291         if (ol_flags & PKT_TX_IP_CKSUM) {
292                 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
293                 *td_offset |= (tx_offload.l3_len >> 2)
294                                 << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
295         } else if (ol_flags & PKT_TX_IPV4) {
296                 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
297                 *td_offset |= (tx_offload.l3_len >> 2)
298                                 << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
299         } else if (ol_flags & PKT_TX_IPV6) {
300                 *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
301                 *td_offset |= (tx_offload.l3_len >> 2)
302                                 << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
303         }
304
305         if (ol_flags & PKT_TX_TCP_SEG) {
306                 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
307                 *td_offset |= (tx_offload.l4_len >> 2)
308                         << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
309                 return;
310         }
311
312         /* Enable L4 checksum offloads */
313         switch (ol_flags & PKT_TX_L4_MASK) {
314         case PKT_TX_TCP_CKSUM:
315                 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
316                 *td_offset |= (sizeof(struct tcp_hdr) >> 2) <<
317                                 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
318                 break;
319         case PKT_TX_SCTP_CKSUM:
320                 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
321                 *td_offset |= (sizeof(struct sctp_hdr) >> 2) <<
322                                 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
323                 break;
324         case PKT_TX_UDP_CKSUM:
325                 *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
326                 *td_offset |= (sizeof(struct udp_hdr) >> 2) <<
327                                 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
328                 break;
329         default:
330                 break;
331         }
332 }
333
334 /* Construct the tx flags */
335 static inline uint64_t
336 i40e_build_ctob(uint32_t td_cmd,
337                 uint32_t td_offset,
338                 unsigned int size,
339                 uint32_t td_tag)
340 {
341         return rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DATA |
342                         ((uint64_t)td_cmd  << I40E_TXD_QW1_CMD_SHIFT) |
343                         ((uint64_t)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
344                         ((uint64_t)size  << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
345                         ((uint64_t)td_tag  << I40E_TXD_QW1_L2TAG1_SHIFT));
346 }
347
348 static inline int
349 i40e_xmit_cleanup(struct i40e_tx_queue *txq)
350 {
351         struct i40e_tx_entry *sw_ring = txq->sw_ring;
352         volatile struct i40e_tx_desc *txd = txq->tx_ring;
353         uint16_t last_desc_cleaned = txq->last_desc_cleaned;
354         uint16_t nb_tx_desc = txq->nb_tx_desc;
355         uint16_t desc_to_clean_to;
356         uint16_t nb_tx_to_clean;
357
358         desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_rs_thresh);
359         if (desc_to_clean_to >= nb_tx_desc)
360                 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
361
362         desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
363         if ((txd[desc_to_clean_to].cmd_type_offset_bsz &
364                         rte_cpu_to_le_64(I40E_TXD_QW1_DTYPE_MASK)) !=
365                         rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DESC_DONE)) {
366                 PMD_TX_FREE_LOG(DEBUG, "TX descriptor %4u is not done "
367                         "(port=%d queue=%d)", desc_to_clean_to,
368                                 txq->port_id, txq->queue_id);
369                 return -1;
370         }
371
372         if (last_desc_cleaned > desc_to_clean_to)
373                 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
374                                                         desc_to_clean_to);
375         else
376                 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
377                                         last_desc_cleaned);
378
379         txd[desc_to_clean_to].cmd_type_offset_bsz = 0;
380
381         txq->last_desc_cleaned = desc_to_clean_to;
382         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
383
384         return 0;
385 }
386
387 static inline int
388 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
389 check_rx_burst_bulk_alloc_preconditions(struct i40e_rx_queue *rxq)
390 #else
391 check_rx_burst_bulk_alloc_preconditions(__rte_unused struct i40e_rx_queue *rxq)
392 #endif
393 {
394         int ret = 0;
395
396 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
397         if (!(rxq->rx_free_thresh >= RTE_PMD_I40E_RX_MAX_BURST)) {
398                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
399                              "rxq->rx_free_thresh=%d, "
400                              "RTE_PMD_I40E_RX_MAX_BURST=%d",
401                              rxq->rx_free_thresh, RTE_PMD_I40E_RX_MAX_BURST);
402                 ret = -EINVAL;
403         } else if (!(rxq->rx_free_thresh < rxq->nb_rx_desc)) {
404                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
405                              "rxq->rx_free_thresh=%d, "
406                              "rxq->nb_rx_desc=%d",
407                              rxq->rx_free_thresh, rxq->nb_rx_desc);
408                 ret = -EINVAL;
409         } else if (rxq->nb_rx_desc % rxq->rx_free_thresh != 0) {
410                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
411                              "rxq->nb_rx_desc=%d, "
412                              "rxq->rx_free_thresh=%d",
413                              rxq->nb_rx_desc, rxq->rx_free_thresh);
414                 ret = -EINVAL;
415         } else if (!(rxq->nb_rx_desc < (I40E_MAX_RING_DESC -
416                                 RTE_PMD_I40E_RX_MAX_BURST))) {
417                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
418                              "rxq->nb_rx_desc=%d, "
419                              "I40E_MAX_RING_DESC=%d, "
420                              "RTE_PMD_I40E_RX_MAX_BURST=%d",
421                              rxq->nb_rx_desc, I40E_MAX_RING_DESC,
422                              RTE_PMD_I40E_RX_MAX_BURST);
423                 ret = -EINVAL;
424         }
425 #else
426         ret = -EINVAL;
427 #endif
428
429         return ret;
430 }
431
432 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
433 #define I40E_LOOK_AHEAD 8
434 #if (I40E_LOOK_AHEAD != 8)
435 #error "PMD I40E: I40E_LOOK_AHEAD must be 8\n"
436 #endif
437 static inline int
438 i40e_rx_scan_hw_ring(struct i40e_rx_queue *rxq)
439 {
440         volatile union i40e_rx_desc *rxdp;
441         struct i40e_rx_entry *rxep;
442         struct rte_mbuf *mb;
443         uint16_t pkt_len;
444         uint64_t qword1;
445         uint32_t rx_status;
446         int32_t s[I40E_LOOK_AHEAD], nb_dd;
447         int32_t i, j, nb_rx = 0;
448         uint64_t pkt_flags;
449
450         rxdp = &rxq->rx_ring[rxq->rx_tail];
451         rxep = &rxq->sw_ring[rxq->rx_tail];
452
453         qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
454         rx_status = (qword1 & I40E_RXD_QW1_STATUS_MASK) >>
455                                 I40E_RXD_QW1_STATUS_SHIFT;
456
457         /* Make sure there is at least 1 packet to receive */
458         if (!(rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
459                 return 0;
460
461         /**
462          * Scan LOOK_AHEAD descriptors at a time to determine which
463          * descriptors reference packets that are ready to be received.
464          */
465         for (i = 0; i < RTE_PMD_I40E_RX_MAX_BURST; i+=I40E_LOOK_AHEAD,
466                         rxdp += I40E_LOOK_AHEAD, rxep += I40E_LOOK_AHEAD) {
467                 /* Read desc statuses backwards to avoid race condition */
468                 for (j = I40E_LOOK_AHEAD - 1; j >= 0; j--) {
469                         qword1 = rte_le_to_cpu_64(\
470                                 rxdp[j].wb.qword1.status_error_len);
471                         s[j] = (qword1 & I40E_RXD_QW1_STATUS_MASK) >>
472                                         I40E_RXD_QW1_STATUS_SHIFT;
473                 }
474
475                 rte_smp_rmb();
476
477                 /* Compute how many status bits were set */
478                 for (j = 0, nb_dd = 0; j < I40E_LOOK_AHEAD; j++)
479                         nb_dd += s[j] & (1 << I40E_RX_DESC_STATUS_DD_SHIFT);
480
481                 nb_rx += nb_dd;
482
483                 /* Translate descriptor info to mbuf parameters */
484                 for (j = 0; j < nb_dd; j++) {
485                         mb = rxep[j].mbuf;
486                         qword1 = rte_le_to_cpu_64(\
487                                 rxdp[j].wb.qword1.status_error_len);
488                         pkt_len = ((qword1 & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
489                                 I40E_RXD_QW1_LENGTH_PBUF_SHIFT) - rxq->crc_len;
490                         mb->data_len = pkt_len;
491                         mb->pkt_len = pkt_len;
492                         mb->ol_flags = 0;
493                         i40e_rxd_to_vlan_tci(mb, &rxdp[j]);
494                         pkt_flags = i40e_rxd_status_to_pkt_flags(qword1);
495                         pkt_flags |= i40e_rxd_error_to_pkt_flags(qword1);
496                         mb->packet_type =
497                                 i40e_rxd_pkt_type_mapping((uint8_t)((qword1 &
498                                                 I40E_RXD_QW1_PTYPE_MASK) >>
499                                                 I40E_RXD_QW1_PTYPE_SHIFT));
500                         if (pkt_flags & PKT_RX_RSS_HASH)
501                                 mb->hash.rss = rte_le_to_cpu_32(\
502                                         rxdp[j].wb.qword0.hi_dword.rss);
503                         if (pkt_flags & PKT_RX_FDIR)
504                                 pkt_flags |= i40e_rxd_build_fdir(&rxdp[j], mb);
505
506 #ifdef RTE_LIBRTE_IEEE1588
507                         pkt_flags |= i40e_get_iee15888_flags(mb, qword1);
508 #endif
509                         mb->ol_flags |= pkt_flags;
510
511                 }
512
513                 for (j = 0; j < I40E_LOOK_AHEAD; j++)
514                         rxq->rx_stage[i + j] = rxep[j].mbuf;
515
516                 if (nb_dd != I40E_LOOK_AHEAD)
517                         break;
518         }
519
520         /* Clear software ring entries */
521         for (i = 0; i < nb_rx; i++)
522                 rxq->sw_ring[rxq->rx_tail + i].mbuf = NULL;
523
524         return nb_rx;
525 }
526
527 static inline uint16_t
528 i40e_rx_fill_from_stage(struct i40e_rx_queue *rxq,
529                         struct rte_mbuf **rx_pkts,
530                         uint16_t nb_pkts)
531 {
532         uint16_t i;
533         struct rte_mbuf **stage = &rxq->rx_stage[rxq->rx_next_avail];
534
535         nb_pkts = (uint16_t)RTE_MIN(nb_pkts, rxq->rx_nb_avail);
536
537         for (i = 0; i < nb_pkts; i++)
538                 rx_pkts[i] = stage[i];
539
540         rxq->rx_nb_avail = (uint16_t)(rxq->rx_nb_avail - nb_pkts);
541         rxq->rx_next_avail = (uint16_t)(rxq->rx_next_avail + nb_pkts);
542
543         return nb_pkts;
544 }
545
546 static inline int
547 i40e_rx_alloc_bufs(struct i40e_rx_queue *rxq)
548 {
549         volatile union i40e_rx_desc *rxdp;
550         struct i40e_rx_entry *rxep;
551         struct rte_mbuf *mb;
552         uint16_t alloc_idx, i;
553         uint64_t dma_addr;
554         int diag;
555
556         /* Allocate buffers in bulk */
557         alloc_idx = (uint16_t)(rxq->rx_free_trigger -
558                                 (rxq->rx_free_thresh - 1));
559         rxep = &(rxq->sw_ring[alloc_idx]);
560         diag = rte_mempool_get_bulk(rxq->mp, (void *)rxep,
561                                         rxq->rx_free_thresh);
562         if (unlikely(diag != 0)) {
563                 PMD_DRV_LOG(ERR, "Failed to get mbufs in bulk");
564                 return -ENOMEM;
565         }
566
567         rxdp = &rxq->rx_ring[alloc_idx];
568         for (i = 0; i < rxq->rx_free_thresh; i++) {
569                 if (likely(i < (rxq->rx_free_thresh - 1)))
570                         /* Prefetch next mbuf */
571                         rte_prefetch0(rxep[i + 1].mbuf);
572
573                 mb = rxep[i].mbuf;
574                 rte_mbuf_refcnt_set(mb, 1);
575                 mb->next = NULL;
576                 mb->data_off = RTE_PKTMBUF_HEADROOM;
577                 mb->nb_segs = 1;
578                 mb->port = rxq->port_id;
579                 dma_addr = rte_cpu_to_le_64(\
580                         rte_mbuf_data_dma_addr_default(mb));
581                 rxdp[i].read.hdr_addr = 0;
582                 rxdp[i].read.pkt_addr = dma_addr;
583         }
584
585         /* Update rx tail regsiter */
586         rte_wmb();
587         I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->rx_free_trigger);
588
589         rxq->rx_free_trigger =
590                 (uint16_t)(rxq->rx_free_trigger + rxq->rx_free_thresh);
591         if (rxq->rx_free_trigger >= rxq->nb_rx_desc)
592                 rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
593
594         return 0;
595 }
596
597 static inline uint16_t
598 rx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
599 {
600         struct i40e_rx_queue *rxq = (struct i40e_rx_queue *)rx_queue;
601         struct rte_eth_dev *dev;
602         uint16_t nb_rx = 0;
603
604         if (!nb_pkts)
605                 return 0;
606
607         if (rxq->rx_nb_avail)
608                 return i40e_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
609
610         nb_rx = (uint16_t)i40e_rx_scan_hw_ring(rxq);
611         rxq->rx_next_avail = 0;
612         rxq->rx_nb_avail = nb_rx;
613         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_rx);
614
615         if (rxq->rx_tail > rxq->rx_free_trigger) {
616                 if (i40e_rx_alloc_bufs(rxq) != 0) {
617                         uint16_t i, j;
618
619                         dev = I40E_VSI_TO_ETH_DEV(rxq->vsi);
620                         dev->data->rx_mbuf_alloc_failed +=
621                                 rxq->rx_free_thresh;
622
623                         rxq->rx_nb_avail = 0;
624                         rxq->rx_tail = (uint16_t)(rxq->rx_tail - nb_rx);
625                         for (i = 0, j = rxq->rx_tail; i < nb_rx; i++, j++)
626                                 rxq->sw_ring[j].mbuf = rxq->rx_stage[i];
627
628                         return 0;
629                 }
630         }
631
632         if (rxq->rx_tail >= rxq->nb_rx_desc)
633                 rxq->rx_tail = 0;
634
635         if (rxq->rx_nb_avail)
636                 return i40e_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
637
638         return 0;
639 }
640
641 static uint16_t
642 i40e_recv_pkts_bulk_alloc(void *rx_queue,
643                           struct rte_mbuf **rx_pkts,
644                           uint16_t nb_pkts)
645 {
646         uint16_t nb_rx = 0, n, count;
647
648         if (unlikely(nb_pkts == 0))
649                 return 0;
650
651         if (likely(nb_pkts <= RTE_PMD_I40E_RX_MAX_BURST))
652                 return rx_recv_pkts(rx_queue, rx_pkts, nb_pkts);
653
654         while (nb_pkts) {
655                 n = RTE_MIN(nb_pkts, RTE_PMD_I40E_RX_MAX_BURST);
656                 count = rx_recv_pkts(rx_queue, &rx_pkts[nb_rx], n);
657                 nb_rx = (uint16_t)(nb_rx + count);
658                 nb_pkts = (uint16_t)(nb_pkts - count);
659                 if (count < n)
660                         break;
661         }
662
663         return nb_rx;
664 }
665 #else
666 static uint16_t
667 i40e_recv_pkts_bulk_alloc(void __rte_unused *rx_queue,
668                           struct rte_mbuf __rte_unused **rx_pkts,
669                           uint16_t __rte_unused nb_pkts)
670 {
671         return 0;
672 }
673 #endif /* RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC */
674
675 uint16_t
676 i40e_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
677 {
678         struct i40e_rx_queue *rxq;
679         volatile union i40e_rx_desc *rx_ring;
680         volatile union i40e_rx_desc *rxdp;
681         union i40e_rx_desc rxd;
682         struct i40e_rx_entry *sw_ring;
683         struct i40e_rx_entry *rxe;
684         struct rte_eth_dev *dev;
685         struct rte_mbuf *rxm;
686         struct rte_mbuf *nmb;
687         uint16_t nb_rx;
688         uint32_t rx_status;
689         uint64_t qword1;
690         uint16_t rx_packet_len;
691         uint16_t rx_id, nb_hold;
692         uint64_t dma_addr;
693         uint64_t pkt_flags;
694
695         nb_rx = 0;
696         nb_hold = 0;
697         rxq = rx_queue;
698         rx_id = rxq->rx_tail;
699         rx_ring = rxq->rx_ring;
700         sw_ring = rxq->sw_ring;
701
702         while (nb_rx < nb_pkts) {
703                 rxdp = &rx_ring[rx_id];
704                 qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
705                 rx_status = (qword1 & I40E_RXD_QW1_STATUS_MASK)
706                                 >> I40E_RXD_QW1_STATUS_SHIFT;
707
708                 /* Check the DD bit first */
709                 if (!(rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
710                         break;
711
712                 nmb = rte_mbuf_raw_alloc(rxq->mp);
713                 if (unlikely(!nmb)) {
714                         dev = I40E_VSI_TO_ETH_DEV(rxq->vsi);
715                         dev->data->rx_mbuf_alloc_failed++;
716                         break;
717                 }
718
719                 rxd = *rxdp;
720                 nb_hold++;
721                 rxe = &sw_ring[rx_id];
722                 rx_id++;
723                 if (unlikely(rx_id == rxq->nb_rx_desc))
724                         rx_id = 0;
725
726                 /* Prefetch next mbuf */
727                 rte_prefetch0(sw_ring[rx_id].mbuf);
728
729                 /**
730                  * When next RX descriptor is on a cache line boundary,
731                  * prefetch the next 4 RX descriptors and next 8 pointers
732                  * to mbufs.
733                  */
734                 if ((rx_id & 0x3) == 0) {
735                         rte_prefetch0(&rx_ring[rx_id]);
736                         rte_prefetch0(&sw_ring[rx_id]);
737                 }
738                 rxm = rxe->mbuf;
739                 rxe->mbuf = nmb;
740                 dma_addr =
741                         rte_cpu_to_le_64(rte_mbuf_data_dma_addr_default(nmb));
742                 rxdp->read.hdr_addr = 0;
743                 rxdp->read.pkt_addr = dma_addr;
744
745                 rx_packet_len = ((qword1 & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
746                                 I40E_RXD_QW1_LENGTH_PBUF_SHIFT) - rxq->crc_len;
747
748                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
749                 rte_prefetch0(RTE_PTR_ADD(rxm->buf_addr, RTE_PKTMBUF_HEADROOM));
750                 rxm->nb_segs = 1;
751                 rxm->next = NULL;
752                 rxm->pkt_len = rx_packet_len;
753                 rxm->data_len = rx_packet_len;
754                 rxm->port = rxq->port_id;
755                 rxm->ol_flags = 0;
756                 i40e_rxd_to_vlan_tci(rxm, &rxd);
757                 pkt_flags = i40e_rxd_status_to_pkt_flags(qword1);
758                 pkt_flags |= i40e_rxd_error_to_pkt_flags(qword1);
759                 rxm->packet_type =
760                         i40e_rxd_pkt_type_mapping((uint8_t)((qword1 &
761                         I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT));
762                 if (pkt_flags & PKT_RX_RSS_HASH)
763                         rxm->hash.rss =
764                                 rte_le_to_cpu_32(rxd.wb.qword0.hi_dword.rss);
765                 if (pkt_flags & PKT_RX_FDIR)
766                         pkt_flags |= i40e_rxd_build_fdir(&rxd, rxm);
767
768 #ifdef RTE_LIBRTE_IEEE1588
769                 pkt_flags |= i40e_get_iee15888_flags(rxm, qword1);
770 #endif
771                 rxm->ol_flags |= pkt_flags;
772
773                 rx_pkts[nb_rx++] = rxm;
774         }
775         rxq->rx_tail = rx_id;
776
777         /**
778          * If the number of free RX descriptors is greater than the RX free
779          * threshold of the queue, advance the receive tail register of queue.
780          * Update that register with the value of the last processed RX
781          * descriptor minus 1.
782          */
783         nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
784         if (nb_hold > rxq->rx_free_thresh) {
785                 rx_id = (uint16_t) ((rx_id == 0) ?
786                         (rxq->nb_rx_desc - 1) : (rx_id - 1));
787                 I40E_PCI_REG_WRITE(rxq->qrx_tail, rx_id);
788                 nb_hold = 0;
789         }
790         rxq->nb_rx_hold = nb_hold;
791
792         return nb_rx;
793 }
794
795 uint16_t
796 i40e_recv_scattered_pkts(void *rx_queue,
797                          struct rte_mbuf **rx_pkts,
798                          uint16_t nb_pkts)
799 {
800         struct i40e_rx_queue *rxq = rx_queue;
801         volatile union i40e_rx_desc *rx_ring = rxq->rx_ring;
802         volatile union i40e_rx_desc *rxdp;
803         union i40e_rx_desc rxd;
804         struct i40e_rx_entry *sw_ring = rxq->sw_ring;
805         struct i40e_rx_entry *rxe;
806         struct rte_mbuf *first_seg = rxq->pkt_first_seg;
807         struct rte_mbuf *last_seg = rxq->pkt_last_seg;
808         struct rte_mbuf *nmb, *rxm;
809         uint16_t rx_id = rxq->rx_tail;
810         uint16_t nb_rx = 0, nb_hold = 0, rx_packet_len;
811         struct rte_eth_dev *dev;
812         uint32_t rx_status;
813         uint64_t qword1;
814         uint64_t dma_addr;
815         uint64_t pkt_flags;
816
817         while (nb_rx < nb_pkts) {
818                 rxdp = &rx_ring[rx_id];
819                 qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
820                 rx_status = (qword1 & I40E_RXD_QW1_STATUS_MASK) >>
821                                         I40E_RXD_QW1_STATUS_SHIFT;
822
823                 /* Check the DD bit */
824                 if (!(rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
825                         break;
826
827                 nmb = rte_mbuf_raw_alloc(rxq->mp);
828                 if (unlikely(!nmb)) {
829                         dev = I40E_VSI_TO_ETH_DEV(rxq->vsi);
830                         dev->data->rx_mbuf_alloc_failed++;
831                         break;
832                 }
833
834                 rxd = *rxdp;
835                 nb_hold++;
836                 rxe = &sw_ring[rx_id];
837                 rx_id++;
838                 if (rx_id == rxq->nb_rx_desc)
839                         rx_id = 0;
840
841                 /* Prefetch next mbuf */
842                 rte_prefetch0(sw_ring[rx_id].mbuf);
843
844                 /**
845                  * When next RX descriptor is on a cache line boundary,
846                  * prefetch the next 4 RX descriptors and next 8 pointers
847                  * to mbufs.
848                  */
849                 if ((rx_id & 0x3) == 0) {
850                         rte_prefetch0(&rx_ring[rx_id]);
851                         rte_prefetch0(&sw_ring[rx_id]);
852                 }
853
854                 rxm = rxe->mbuf;
855                 rxe->mbuf = nmb;
856                 dma_addr =
857                         rte_cpu_to_le_64(rte_mbuf_data_dma_addr_default(nmb));
858
859                 /* Set data buffer address and data length of the mbuf */
860                 rxdp->read.hdr_addr = 0;
861                 rxdp->read.pkt_addr = dma_addr;
862                 rx_packet_len = (qword1 & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
863                                         I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
864                 rxm->data_len = rx_packet_len;
865                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
866
867                 /**
868                  * If this is the first buffer of the received packet, set the
869                  * pointer to the first mbuf of the packet and initialize its
870                  * context. Otherwise, update the total length and the number
871                  * of segments of the current scattered packet, and update the
872                  * pointer to the last mbuf of the current packet.
873                  */
874                 if (!first_seg) {
875                         first_seg = rxm;
876                         first_seg->nb_segs = 1;
877                         first_seg->pkt_len = rx_packet_len;
878                 } else {
879                         first_seg->pkt_len =
880                                 (uint16_t)(first_seg->pkt_len +
881                                                 rx_packet_len);
882                         first_seg->nb_segs++;
883                         last_seg->next = rxm;
884                 }
885
886                 /**
887                  * If this is not the last buffer of the received packet,
888                  * update the pointer to the last mbuf of the current scattered
889                  * packet and continue to parse the RX ring.
890                  */
891                 if (!(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT))) {
892                         last_seg = rxm;
893                         continue;
894                 }
895
896                 /**
897                  * This is the last buffer of the received packet. If the CRC
898                  * is not stripped by the hardware:
899                  *  - Subtract the CRC length from the total packet length.
900                  *  - If the last buffer only contains the whole CRC or a part
901                  *  of it, free the mbuf associated to the last buffer. If part
902                  *  of the CRC is also contained in the previous mbuf, subtract
903                  *  the length of that CRC part from the data length of the
904                  *  previous mbuf.
905                  */
906                 rxm->next = NULL;
907                 if (unlikely(rxq->crc_len > 0)) {
908                         first_seg->pkt_len -= ETHER_CRC_LEN;
909                         if (rx_packet_len <= ETHER_CRC_LEN) {
910                                 rte_pktmbuf_free_seg(rxm);
911                                 first_seg->nb_segs--;
912                                 last_seg->data_len =
913                                         (uint16_t)(last_seg->data_len -
914                                         (ETHER_CRC_LEN - rx_packet_len));
915                                 last_seg->next = NULL;
916                         } else
917                                 rxm->data_len = (uint16_t)(rx_packet_len -
918                                                                 ETHER_CRC_LEN);
919                 }
920
921                 first_seg->port = rxq->port_id;
922                 first_seg->ol_flags = 0;
923                 i40e_rxd_to_vlan_tci(first_seg, &rxd);
924                 pkt_flags = i40e_rxd_status_to_pkt_flags(qword1);
925                 pkt_flags |= i40e_rxd_error_to_pkt_flags(qword1);
926                 first_seg->packet_type =
927                         i40e_rxd_pkt_type_mapping((uint8_t)((qword1 &
928                         I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT));
929                 if (pkt_flags & PKT_RX_RSS_HASH)
930                         first_seg->hash.rss =
931                                 rte_le_to_cpu_32(rxd.wb.qword0.hi_dword.rss);
932                 if (pkt_flags & PKT_RX_FDIR)
933                         pkt_flags |= i40e_rxd_build_fdir(&rxd, first_seg);
934
935 #ifdef RTE_LIBRTE_IEEE1588
936                 pkt_flags |= i40e_get_iee15888_flags(first_seg, qword1);
937 #endif
938                 first_seg->ol_flags |= pkt_flags;
939
940                 /* Prefetch data of first segment, if configured to do so. */
941                 rte_prefetch0(RTE_PTR_ADD(first_seg->buf_addr,
942                         first_seg->data_off));
943                 rx_pkts[nb_rx++] = first_seg;
944                 first_seg = NULL;
945         }
946
947         /* Record index of the next RX descriptor to probe. */
948         rxq->rx_tail = rx_id;
949         rxq->pkt_first_seg = first_seg;
950         rxq->pkt_last_seg = last_seg;
951
952         /**
953          * If the number of free RX descriptors is greater than the RX free
954          * threshold of the queue, advance the Receive Descriptor Tail (RDT)
955          * register. Update the RDT with the value of the last processed RX
956          * descriptor minus 1, to guarantee that the RDT register is never
957          * equal to the RDH register, which creates a "full" ring situtation
958          * from the hardware point of view.
959          */
960         nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
961         if (nb_hold > rxq->rx_free_thresh) {
962                 rx_id = (uint16_t)(rx_id == 0 ?
963                         (rxq->nb_rx_desc - 1) : (rx_id - 1));
964                 I40E_PCI_REG_WRITE(rxq->qrx_tail, rx_id);
965                 nb_hold = 0;
966         }
967         rxq->nb_rx_hold = nb_hold;
968
969         return nb_rx;
970 }
971
972 /* Check if the context descriptor is needed for TX offloading */
973 static inline uint16_t
974 i40e_calc_context_desc(uint64_t flags)
975 {
976         static uint64_t mask = PKT_TX_OUTER_IP_CKSUM |
977                 PKT_TX_TCP_SEG |
978                 PKT_TX_QINQ_PKT |
979                 PKT_TX_TUNNEL_MASK;
980
981 #ifdef RTE_LIBRTE_IEEE1588
982         mask |= PKT_TX_IEEE1588_TMST;
983 #endif
984
985         return (flags & mask) ? 1 : 0;
986 }
987
988 /* set i40e TSO context descriptor */
989 static inline uint64_t
990 i40e_set_tso_ctx(struct rte_mbuf *mbuf, union i40e_tx_offload tx_offload)
991 {
992         uint64_t ctx_desc = 0;
993         uint32_t cd_cmd, hdr_len, cd_tso_len;
994
995         if (!tx_offload.l4_len) {
996                 PMD_DRV_LOG(DEBUG, "L4 length set to 0");
997                 return ctx_desc;
998         }
999
1000         /**
1001          * in case of non tunneling packet, the outer_l2_len and
1002          * outer_l3_len must be 0.
1003          */
1004         hdr_len = tx_offload.outer_l2_len +
1005                 tx_offload.outer_l3_len +
1006                 tx_offload.l2_len +
1007                 tx_offload.l3_len +
1008                 tx_offload.l4_len;
1009
1010         cd_cmd = I40E_TX_CTX_DESC_TSO;
1011         cd_tso_len = mbuf->pkt_len - hdr_len;
1012         ctx_desc |= ((uint64_t)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
1013                 ((uint64_t)cd_tso_len <<
1014                  I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1015                 ((uint64_t)mbuf->tso_segsz <<
1016                  I40E_TXD_CTX_QW1_MSS_SHIFT);
1017
1018         return ctx_desc;
1019 }
1020
1021 uint16_t
1022 i40e_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1023 {
1024         struct i40e_tx_queue *txq;
1025         struct i40e_tx_entry *sw_ring;
1026         struct i40e_tx_entry *txe, *txn;
1027         volatile struct i40e_tx_desc *txd;
1028         volatile struct i40e_tx_desc *txr;
1029         struct rte_mbuf *tx_pkt;
1030         struct rte_mbuf *m_seg;
1031         uint32_t cd_tunneling_params;
1032         uint16_t tx_id;
1033         uint16_t nb_tx;
1034         uint32_t td_cmd;
1035         uint32_t td_offset;
1036         uint32_t tx_flags;
1037         uint32_t td_tag;
1038         uint64_t ol_flags;
1039         uint16_t nb_used;
1040         uint16_t nb_ctx;
1041         uint16_t tx_last;
1042         uint16_t slen;
1043         uint64_t buf_dma_addr;
1044         union i40e_tx_offload tx_offload = {0};
1045
1046         txq = tx_queue;
1047         sw_ring = txq->sw_ring;
1048         txr = txq->tx_ring;
1049         tx_id = txq->tx_tail;
1050         txe = &sw_ring[tx_id];
1051
1052         /* Check if the descriptor ring needs to be cleaned. */
1053         if (txq->nb_tx_free < txq->tx_free_thresh)
1054                 i40e_xmit_cleanup(txq);
1055
1056         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1057                 td_cmd = 0;
1058                 td_tag = 0;
1059                 td_offset = 0;
1060                 tx_flags = 0;
1061
1062                 tx_pkt = *tx_pkts++;
1063                 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
1064
1065                 ol_flags = tx_pkt->ol_flags;
1066                 tx_offload.l2_len = tx_pkt->l2_len;
1067                 tx_offload.l3_len = tx_pkt->l3_len;
1068                 tx_offload.outer_l2_len = tx_pkt->outer_l2_len;
1069                 tx_offload.outer_l3_len = tx_pkt->outer_l3_len;
1070                 tx_offload.l4_len = tx_pkt->l4_len;
1071                 tx_offload.tso_segsz = tx_pkt->tso_segsz;
1072
1073                 /* Calculate the number of context descriptors needed. */
1074                 nb_ctx = i40e_calc_context_desc(ol_flags);
1075
1076                 /**
1077                  * The number of descriptors that must be allocated for
1078                  * a packet equals to the number of the segments of that
1079                  * packet plus 1 context descriptor if needed.
1080                  */
1081                 nb_used = (uint16_t)(tx_pkt->nb_segs + nb_ctx);
1082                 tx_last = (uint16_t)(tx_id + nb_used - 1);
1083
1084                 /* Circular ring */
1085                 if (tx_last >= txq->nb_tx_desc)
1086                         tx_last = (uint16_t)(tx_last - txq->nb_tx_desc);
1087
1088                 if (nb_used > txq->nb_tx_free) {
1089                         if (i40e_xmit_cleanup(txq) != 0) {
1090                                 if (nb_tx == 0)
1091                                         return 0;
1092                                 goto end_of_tx;
1093                         }
1094                         if (unlikely(nb_used > txq->tx_rs_thresh)) {
1095                                 while (nb_used > txq->nb_tx_free) {
1096                                         if (i40e_xmit_cleanup(txq) != 0) {
1097                                                 if (nb_tx == 0)
1098                                                         return 0;
1099                                                 goto end_of_tx;
1100                                         }
1101                                 }
1102                         }
1103                 }
1104
1105                 /* Descriptor based VLAN insertion */
1106                 if (ol_flags & (PKT_TX_VLAN_PKT | PKT_TX_QINQ_PKT)) {
1107                         tx_flags |= tx_pkt->vlan_tci <<
1108                                 I40E_TX_FLAG_L2TAG1_SHIFT;
1109                         tx_flags |= I40E_TX_FLAG_INSERT_VLAN;
1110                         td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
1111                         td_tag = (tx_flags & I40E_TX_FLAG_L2TAG1_MASK) >>
1112                                                 I40E_TX_FLAG_L2TAG1_SHIFT;
1113                 }
1114
1115                 /* Always enable CRC offload insertion */
1116                 td_cmd |= I40E_TX_DESC_CMD_ICRC;
1117
1118                 /* Fill in tunneling parameters if necessary */
1119                 cd_tunneling_params = 0;
1120                 if (ol_flags & PKT_TX_TUNNEL_MASK)
1121                         i40e_parse_tunneling_params(ol_flags, tx_offload,
1122                                                     &cd_tunneling_params);
1123                 /* Enable checksum offloading */
1124                 if (ol_flags & I40E_TX_CKSUM_OFFLOAD_MASK)
1125                         i40e_txd_enable_checksum(ol_flags, &td_cmd,
1126                                                  &td_offset, tx_offload);
1127
1128                 if (nb_ctx) {
1129                         /* Setup TX context descriptor if required */
1130                         volatile struct i40e_tx_context_desc *ctx_txd =
1131                                 (volatile struct i40e_tx_context_desc *)\
1132                                                         &txr[tx_id];
1133                         uint16_t cd_l2tag2 = 0;
1134                         uint64_t cd_type_cmd_tso_mss =
1135                                 I40E_TX_DESC_DTYPE_CONTEXT;
1136
1137                         txn = &sw_ring[txe->next_id];
1138                         RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
1139                         if (txe->mbuf != NULL) {
1140                                 rte_pktmbuf_free_seg(txe->mbuf);
1141                                 txe->mbuf = NULL;
1142                         }
1143
1144                         /* TSO enabled means no timestamp */
1145                         if (ol_flags & PKT_TX_TCP_SEG)
1146                                 cd_type_cmd_tso_mss |=
1147                                         i40e_set_tso_ctx(tx_pkt, tx_offload);
1148                         else {
1149 #ifdef RTE_LIBRTE_IEEE1588
1150                                 if (ol_flags & PKT_TX_IEEE1588_TMST)
1151                                         cd_type_cmd_tso_mss |=
1152                                                 ((uint64_t)I40E_TX_CTX_DESC_TSYN <<
1153                                                  I40E_TXD_CTX_QW1_CMD_SHIFT);
1154 #endif
1155                         }
1156
1157                         ctx_txd->tunneling_params =
1158                                 rte_cpu_to_le_32(cd_tunneling_params);
1159                         if (ol_flags & PKT_TX_QINQ_PKT) {
1160                                 cd_l2tag2 = tx_pkt->vlan_tci_outer;
1161                                 cd_type_cmd_tso_mss |=
1162                                         ((uint64_t)I40E_TX_CTX_DESC_IL2TAG2 <<
1163                                                 I40E_TXD_CTX_QW1_CMD_SHIFT);
1164                         }
1165                         ctx_txd->l2tag2 = rte_cpu_to_le_16(cd_l2tag2);
1166                         ctx_txd->type_cmd_tso_mss =
1167                                 rte_cpu_to_le_64(cd_type_cmd_tso_mss);
1168
1169                         PMD_TX_LOG(DEBUG, "mbuf: %p, TCD[%u]:\n"
1170                                 "tunneling_params: %#x;\n"
1171                                 "l2tag2: %#hx;\n"
1172                                 "rsvd: %#hx;\n"
1173                                 "type_cmd_tso_mss: %#"PRIx64";\n",
1174                                 tx_pkt, tx_id,
1175                                 ctx_txd->tunneling_params,
1176                                 ctx_txd->l2tag2,
1177                                 ctx_txd->rsvd,
1178                                 ctx_txd->type_cmd_tso_mss);
1179
1180                         txe->last_id = tx_last;
1181                         tx_id = txe->next_id;
1182                         txe = txn;
1183                 }
1184
1185                 m_seg = tx_pkt;
1186                 do {
1187                         txd = &txr[tx_id];
1188                         txn = &sw_ring[txe->next_id];
1189
1190                         if (txe->mbuf)
1191                                 rte_pktmbuf_free_seg(txe->mbuf);
1192                         txe->mbuf = m_seg;
1193
1194                         /* Setup TX Descriptor */
1195                         slen = m_seg->data_len;
1196                         buf_dma_addr = rte_mbuf_data_dma_addr(m_seg);
1197
1198                         PMD_TX_LOG(DEBUG, "mbuf: %p, TDD[%u]:\n"
1199                                 "buf_dma_addr: %#"PRIx64";\n"
1200                                 "td_cmd: %#x;\n"
1201                                 "td_offset: %#x;\n"
1202                                 "td_len: %u;\n"
1203                                 "td_tag: %#x;\n",
1204                                 tx_pkt, tx_id, buf_dma_addr,
1205                                 td_cmd, td_offset, slen, td_tag);
1206
1207                         txd->buffer_addr = rte_cpu_to_le_64(buf_dma_addr);
1208                         txd->cmd_type_offset_bsz = i40e_build_ctob(td_cmd,
1209                                                 td_offset, slen, td_tag);
1210                         txe->last_id = tx_last;
1211                         tx_id = txe->next_id;
1212                         txe = txn;
1213                         m_seg = m_seg->next;
1214                 } while (m_seg != NULL);
1215
1216                 /* The last packet data descriptor needs End Of Packet (EOP) */
1217                 td_cmd |= I40E_TX_DESC_CMD_EOP;
1218                 txq->nb_tx_used = (uint16_t)(txq->nb_tx_used + nb_used);
1219                 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
1220
1221                 if (txq->nb_tx_used >= txq->tx_rs_thresh) {
1222                         PMD_TX_FREE_LOG(DEBUG,
1223                                         "Setting RS bit on TXD id="
1224                                         "%4u (port=%d queue=%d)",
1225                                         tx_last, txq->port_id, txq->queue_id);
1226
1227                         td_cmd |= I40E_TX_DESC_CMD_RS;
1228
1229                         /* Update txq RS bit counters */
1230                         txq->nb_tx_used = 0;
1231                 }
1232
1233                 txd->cmd_type_offset_bsz |=
1234                         rte_cpu_to_le_64(((uint64_t)td_cmd) <<
1235                                         I40E_TXD_QW1_CMD_SHIFT);
1236         }
1237
1238 end_of_tx:
1239         rte_wmb();
1240
1241         PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
1242                    (unsigned) txq->port_id, (unsigned) txq->queue_id,
1243                    (unsigned) tx_id, (unsigned) nb_tx);
1244
1245         I40E_PCI_REG_WRITE(txq->qtx_tail, tx_id);
1246         txq->tx_tail = tx_id;
1247
1248         return nb_tx;
1249 }
1250
1251 static inline int __attribute__((always_inline))
1252 i40e_tx_free_bufs(struct i40e_tx_queue *txq)
1253 {
1254         struct i40e_tx_entry *txep;
1255         uint16_t i;
1256
1257         if ((txq->tx_ring[txq->tx_next_dd].cmd_type_offset_bsz &
1258                         rte_cpu_to_le_64(I40E_TXD_QW1_DTYPE_MASK)) !=
1259                         rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DESC_DONE))
1260                 return 0;
1261
1262         txep = &(txq->sw_ring[txq->tx_next_dd - (txq->tx_rs_thresh - 1)]);
1263
1264         for (i = 0; i < txq->tx_rs_thresh; i++)
1265                 rte_prefetch0((txep + i)->mbuf);
1266
1267         if (txq->txq_flags & (uint32_t)ETH_TXQ_FLAGS_NOREFCOUNT) {
1268                 for (i = 0; i < txq->tx_rs_thresh; ++i, ++txep) {
1269                         rte_mempool_put(txep->mbuf->pool, txep->mbuf);
1270                         txep->mbuf = NULL;
1271                 }
1272         } else {
1273                 for (i = 0; i < txq->tx_rs_thresh; ++i, ++txep) {
1274                         rte_pktmbuf_free_seg(txep->mbuf);
1275                         txep->mbuf = NULL;
1276                 }
1277         }
1278
1279         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_rs_thresh);
1280         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_rs_thresh);
1281         if (txq->tx_next_dd >= txq->nb_tx_desc)
1282                 txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
1283
1284         return txq->tx_rs_thresh;
1285 }
1286
1287 /* Populate 4 descriptors with data from 4 mbufs */
1288 static inline void
1289 tx4(volatile struct i40e_tx_desc *txdp, struct rte_mbuf **pkts)
1290 {
1291         uint64_t dma_addr;
1292         uint32_t i;
1293
1294         for (i = 0; i < 4; i++, txdp++, pkts++) {
1295                 dma_addr = rte_mbuf_data_dma_addr(*pkts);
1296                 txdp->buffer_addr = rte_cpu_to_le_64(dma_addr);
1297                 txdp->cmd_type_offset_bsz =
1298                         i40e_build_ctob((uint32_t)I40E_TD_CMD, 0,
1299                                         (*pkts)->data_len, 0);
1300         }
1301 }
1302
1303 /* Populate 1 descriptor with data from 1 mbuf */
1304 static inline void
1305 tx1(volatile struct i40e_tx_desc *txdp, struct rte_mbuf **pkts)
1306 {
1307         uint64_t dma_addr;
1308
1309         dma_addr = rte_mbuf_data_dma_addr(*pkts);
1310         txdp->buffer_addr = rte_cpu_to_le_64(dma_addr);
1311         txdp->cmd_type_offset_bsz =
1312                 i40e_build_ctob((uint32_t)I40E_TD_CMD, 0,
1313                                 (*pkts)->data_len, 0);
1314 }
1315
1316 /* Fill hardware descriptor ring with mbuf data */
1317 static inline void
1318 i40e_tx_fill_hw_ring(struct i40e_tx_queue *txq,
1319                      struct rte_mbuf **pkts,
1320                      uint16_t nb_pkts)
1321 {
1322         volatile struct i40e_tx_desc *txdp = &(txq->tx_ring[txq->tx_tail]);
1323         struct i40e_tx_entry *txep = &(txq->sw_ring[txq->tx_tail]);
1324         const int N_PER_LOOP = 4;
1325         const int N_PER_LOOP_MASK = N_PER_LOOP - 1;
1326         int mainpart, leftover;
1327         int i, j;
1328
1329         mainpart = (nb_pkts & ((uint32_t) ~N_PER_LOOP_MASK));
1330         leftover = (nb_pkts & ((uint32_t)  N_PER_LOOP_MASK));
1331         for (i = 0; i < mainpart; i += N_PER_LOOP) {
1332                 for (j = 0; j < N_PER_LOOP; ++j) {
1333                         (txep + i + j)->mbuf = *(pkts + i + j);
1334                 }
1335                 tx4(txdp + i, pkts + i);
1336         }
1337         if (unlikely(leftover > 0)) {
1338                 for (i = 0; i < leftover; ++i) {
1339                         (txep + mainpart + i)->mbuf = *(pkts + mainpart + i);
1340                         tx1(txdp + mainpart + i, pkts + mainpart + i);
1341                 }
1342         }
1343 }
1344
1345 static inline uint16_t
1346 tx_xmit_pkts(struct i40e_tx_queue *txq,
1347              struct rte_mbuf **tx_pkts,
1348              uint16_t nb_pkts)
1349 {
1350         volatile struct i40e_tx_desc *txr = txq->tx_ring;
1351         uint16_t n = 0;
1352
1353         /**
1354          * Begin scanning the H/W ring for done descriptors when the number
1355          * of available descriptors drops below tx_free_thresh. For each done
1356          * descriptor, free the associated buffer.
1357          */
1358         if (txq->nb_tx_free < txq->tx_free_thresh)
1359                 i40e_tx_free_bufs(txq);
1360
1361         /* Use available descriptor only */
1362         nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
1363         if (unlikely(!nb_pkts))
1364                 return 0;
1365
1366         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
1367         if ((txq->tx_tail + nb_pkts) > txq->nb_tx_desc) {
1368                 n = (uint16_t)(txq->nb_tx_desc - txq->tx_tail);
1369                 i40e_tx_fill_hw_ring(txq, tx_pkts, n);
1370                 txr[txq->tx_next_rs].cmd_type_offset_bsz |=
1371                         rte_cpu_to_le_64(((uint64_t)I40E_TX_DESC_CMD_RS) <<
1372                                                 I40E_TXD_QW1_CMD_SHIFT);
1373                 txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
1374                 txq->tx_tail = 0;
1375         }
1376
1377         /* Fill hardware descriptor ring with mbuf data */
1378         i40e_tx_fill_hw_ring(txq, tx_pkts + n, (uint16_t)(nb_pkts - n));
1379         txq->tx_tail = (uint16_t)(txq->tx_tail + (nb_pkts - n));
1380
1381         /* Determin if RS bit needs to be set */
1382         if (txq->tx_tail > txq->tx_next_rs) {
1383                 txr[txq->tx_next_rs].cmd_type_offset_bsz |=
1384                         rte_cpu_to_le_64(((uint64_t)I40E_TX_DESC_CMD_RS) <<
1385                                                 I40E_TXD_QW1_CMD_SHIFT);
1386                 txq->tx_next_rs =
1387                         (uint16_t)(txq->tx_next_rs + txq->tx_rs_thresh);
1388                 if (txq->tx_next_rs >= txq->nb_tx_desc)
1389                         txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
1390         }
1391
1392         if (txq->tx_tail >= txq->nb_tx_desc)
1393                 txq->tx_tail = 0;
1394
1395         /* Update the tx tail register */
1396         rte_wmb();
1397         I40E_PCI_REG_WRITE(txq->qtx_tail, txq->tx_tail);
1398
1399         return nb_pkts;
1400 }
1401
1402 static uint16_t
1403 i40e_xmit_pkts_simple(void *tx_queue,
1404                       struct rte_mbuf **tx_pkts,
1405                       uint16_t nb_pkts)
1406 {
1407         uint16_t nb_tx = 0;
1408
1409         if (likely(nb_pkts <= I40E_TX_MAX_BURST))
1410                 return tx_xmit_pkts((struct i40e_tx_queue *)tx_queue,
1411                                                 tx_pkts, nb_pkts);
1412
1413         while (nb_pkts) {
1414                 uint16_t ret, num = (uint16_t)RTE_MIN(nb_pkts,
1415                                                 I40E_TX_MAX_BURST);
1416
1417                 ret = tx_xmit_pkts((struct i40e_tx_queue *)tx_queue,
1418                                                 &tx_pkts[nb_tx], num);
1419                 nb_tx = (uint16_t)(nb_tx + ret);
1420                 nb_pkts = (uint16_t)(nb_pkts - ret);
1421                 if (ret < num)
1422                         break;
1423         }
1424
1425         return nb_tx;
1426 }
1427
1428 /*
1429  * Find the VSI the queue belongs to. 'queue_idx' is the queue index
1430  * application used, which assume having sequential ones. But from driver's
1431  * perspective, it's different. For example, q0 belongs to FDIR VSI, q1-q64
1432  * to MAIN VSI, , q65-96 to SRIOV VSIs, q97-128 to VMDQ VSIs. For application
1433  * running on host, q1-64 and q97-128 can be used, total 96 queues. They can
1434  * use queue_idx from 0 to 95 to access queues, while real queue would be
1435  * different. This function will do a queue mapping to find VSI the queue
1436  * belongs to.
1437  */
1438 static struct i40e_vsi*
1439 i40e_pf_get_vsi_by_qindex(struct i40e_pf *pf, uint16_t queue_idx)
1440 {
1441         /* the queue in MAIN VSI range */
1442         if (queue_idx < pf->main_vsi->nb_qps)
1443                 return pf->main_vsi;
1444
1445         queue_idx -= pf->main_vsi->nb_qps;
1446
1447         /* queue_idx is greater than VMDQ VSIs range */
1448         if (queue_idx > pf->nb_cfg_vmdq_vsi * pf->vmdq_nb_qps - 1) {
1449                 PMD_INIT_LOG(ERR, "queue_idx out of range. VMDQ configured?");
1450                 return NULL;
1451         }
1452
1453         return pf->vmdq[queue_idx / pf->vmdq_nb_qps].vsi;
1454 }
1455
1456 static uint16_t
1457 i40e_get_queue_offset_by_qindex(struct i40e_pf *pf, uint16_t queue_idx)
1458 {
1459         /* the queue in MAIN VSI range */
1460         if (queue_idx < pf->main_vsi->nb_qps)
1461                 return queue_idx;
1462
1463         /* It's VMDQ queues */
1464         queue_idx -= pf->main_vsi->nb_qps;
1465
1466         if (pf->nb_cfg_vmdq_vsi)
1467                 return queue_idx % pf->vmdq_nb_qps;
1468         else {
1469                 PMD_INIT_LOG(ERR, "Fail to get queue offset");
1470                 return (uint16_t)(-1);
1471         }
1472 }
1473
1474 int
1475 i40e_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1476 {
1477         struct i40e_rx_queue *rxq;
1478         int err = -1;
1479         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1480
1481         PMD_INIT_FUNC_TRACE();
1482
1483         if (rx_queue_id < dev->data->nb_rx_queues) {
1484                 rxq = dev->data->rx_queues[rx_queue_id];
1485
1486                 err = i40e_alloc_rx_queue_mbufs(rxq);
1487                 if (err) {
1488                         PMD_DRV_LOG(ERR, "Failed to allocate RX queue mbuf");
1489                         return err;
1490                 }
1491
1492                 rte_wmb();
1493
1494                 /* Init the RX tail regieter. */
1495                 I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
1496
1497                 err = i40e_switch_rx_queue(hw, rxq->reg_idx, TRUE);
1498
1499                 if (err) {
1500                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u on",
1501                                     rx_queue_id);
1502
1503                         i40e_rx_queue_release_mbufs(rxq);
1504                         i40e_reset_rx_queue(rxq);
1505                 } else
1506                         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1507         }
1508
1509         return err;
1510 }
1511
1512 int
1513 i40e_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1514 {
1515         struct i40e_rx_queue *rxq;
1516         int err;
1517         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1518
1519         if (rx_queue_id < dev->data->nb_rx_queues) {
1520                 rxq = dev->data->rx_queues[rx_queue_id];
1521
1522                 /*
1523                 * rx_queue_id is queue id aplication refers to, while
1524                 * rxq->reg_idx is the real queue index.
1525                 */
1526                 err = i40e_switch_rx_queue(hw, rxq->reg_idx, FALSE);
1527
1528                 if (err) {
1529                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u off",
1530                                     rx_queue_id);
1531                         return err;
1532                 }
1533                 i40e_rx_queue_release_mbufs(rxq);
1534                 i40e_reset_rx_queue(rxq);
1535                 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1536         }
1537
1538         return 0;
1539 }
1540
1541 int
1542 i40e_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1543 {
1544         int err = -1;
1545         struct i40e_tx_queue *txq;
1546         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1547
1548         PMD_INIT_FUNC_TRACE();
1549
1550         if (tx_queue_id < dev->data->nb_tx_queues) {
1551                 txq = dev->data->tx_queues[tx_queue_id];
1552
1553                 /*
1554                 * tx_queue_id is queue id aplication refers to, while
1555                 * rxq->reg_idx is the real queue index.
1556                 */
1557                 err = i40e_switch_tx_queue(hw, txq->reg_idx, TRUE);
1558                 if (err)
1559                         PMD_DRV_LOG(ERR, "Failed to switch TX queue %u on",
1560                                     tx_queue_id);
1561                 else
1562                         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1563         }
1564
1565         return err;
1566 }
1567
1568 int
1569 i40e_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1570 {
1571         struct i40e_tx_queue *txq;
1572         int err;
1573         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1574
1575         if (tx_queue_id < dev->data->nb_tx_queues) {
1576                 txq = dev->data->tx_queues[tx_queue_id];
1577
1578                 /*
1579                 * tx_queue_id is queue id aplication refers to, while
1580                 * txq->reg_idx is the real queue index.
1581                 */
1582                 err = i40e_switch_tx_queue(hw, txq->reg_idx, FALSE);
1583
1584                 if (err) {
1585                         PMD_DRV_LOG(ERR, "Failed to switch TX queue %u of",
1586                                     tx_queue_id);
1587                         return err;
1588                 }
1589
1590                 i40e_tx_queue_release_mbufs(txq);
1591                 i40e_reset_tx_queue(txq);
1592                 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1593         }
1594
1595         return 0;
1596 }
1597
1598 const uint32_t *
1599 i40e_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1600 {
1601         static const uint32_t ptypes[] = {
1602                 /* refers to i40e_rxd_pkt_type_mapping() */
1603                 RTE_PTYPE_L2_ETHER,
1604                 RTE_PTYPE_L2_ETHER_TIMESYNC,
1605                 RTE_PTYPE_L2_ETHER_LLDP,
1606                 RTE_PTYPE_L2_ETHER_ARP,
1607                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
1608                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
1609                 RTE_PTYPE_L4_FRAG,
1610                 RTE_PTYPE_L4_ICMP,
1611                 RTE_PTYPE_L4_NONFRAG,
1612                 RTE_PTYPE_L4_SCTP,
1613                 RTE_PTYPE_L4_TCP,
1614                 RTE_PTYPE_L4_UDP,
1615                 RTE_PTYPE_TUNNEL_GRENAT,
1616                 RTE_PTYPE_TUNNEL_IP,
1617                 RTE_PTYPE_INNER_L2_ETHER,
1618                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1619                 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
1620                 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
1621                 RTE_PTYPE_INNER_L4_FRAG,
1622                 RTE_PTYPE_INNER_L4_ICMP,
1623                 RTE_PTYPE_INNER_L4_NONFRAG,
1624                 RTE_PTYPE_INNER_L4_SCTP,
1625                 RTE_PTYPE_INNER_L4_TCP,
1626                 RTE_PTYPE_INNER_L4_UDP,
1627                 RTE_PTYPE_UNKNOWN
1628         };
1629
1630         if (dev->rx_pkt_burst == i40e_recv_pkts ||
1631 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
1632             dev->rx_pkt_burst == i40e_recv_pkts_bulk_alloc ||
1633 #endif
1634             dev->rx_pkt_burst == i40e_recv_scattered_pkts ||
1635             dev->rx_pkt_burst == i40e_recv_scattered_pkts_vec ||
1636             dev->rx_pkt_burst == i40e_recv_pkts_vec)
1637                 return ptypes;
1638         return NULL;
1639 }
1640
1641 int
1642 i40e_dev_rx_queue_setup(struct rte_eth_dev *dev,
1643                         uint16_t queue_idx,
1644                         uint16_t nb_desc,
1645                         unsigned int socket_id,
1646                         const struct rte_eth_rxconf *rx_conf,
1647                         struct rte_mempool *mp)
1648 {
1649         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1650         struct i40e_adapter *ad =
1651                 I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1652         struct i40e_vsi *vsi;
1653         struct i40e_pf *pf = NULL;
1654         struct i40e_vf *vf = NULL;
1655         struct i40e_rx_queue *rxq;
1656         const struct rte_memzone *rz;
1657         uint32_t ring_size;
1658         uint16_t len, i;
1659         uint16_t reg_idx, base, bsf, tc_mapping;
1660         int q_offset, use_def_burst_func = 1;
1661
1662         if (hw->mac.type == I40E_MAC_VF || hw->mac.type == I40E_MAC_X722_VF) {
1663                 vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1664                 vsi = &vf->vsi;
1665                 if (!vsi)
1666                         return -EINVAL;
1667                 reg_idx = queue_idx;
1668         } else {
1669                 pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1670                 vsi = i40e_pf_get_vsi_by_qindex(pf, queue_idx);
1671                 if (!vsi)
1672                         return -EINVAL;
1673                 q_offset = i40e_get_queue_offset_by_qindex(pf, queue_idx);
1674                 if (q_offset < 0)
1675                         return -EINVAL;
1676                 reg_idx = vsi->base_queue + q_offset;
1677         }
1678
1679         if (nb_desc % I40E_ALIGN_RING_DESC != 0 ||
1680             (nb_desc > I40E_MAX_RING_DESC) ||
1681             (nb_desc < I40E_MIN_RING_DESC)) {
1682                 PMD_DRV_LOG(ERR, "Number (%u) of receive descriptors is "
1683                             "invalid", nb_desc);
1684                 return -EINVAL;
1685         }
1686
1687         /* Free memory if needed */
1688         if (dev->data->rx_queues[queue_idx]) {
1689                 i40e_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
1690                 dev->data->rx_queues[queue_idx] = NULL;
1691         }
1692
1693         /* Allocate the rx queue data structure */
1694         rxq = rte_zmalloc_socket("i40e rx queue",
1695                                  sizeof(struct i40e_rx_queue),
1696                                  RTE_CACHE_LINE_SIZE,
1697                                  socket_id);
1698         if (!rxq) {
1699                 PMD_DRV_LOG(ERR, "Failed to allocate memory for "
1700                             "rx queue data structure");
1701                 return -ENOMEM;
1702         }
1703         rxq->mp = mp;
1704         rxq->nb_rx_desc = nb_desc;
1705         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1706         rxq->queue_id = queue_idx;
1707         rxq->reg_idx = reg_idx;
1708         rxq->port_id = dev->data->port_id;
1709         rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ?
1710                                                         0 : ETHER_CRC_LEN);
1711         rxq->drop_en = rx_conf->rx_drop_en;
1712         rxq->vsi = vsi;
1713         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
1714
1715         /* Allocate the maximun number of RX ring hardware descriptor. */
1716         ring_size = sizeof(union i40e_rx_desc) * I40E_MAX_RING_DESC;
1717         ring_size = RTE_ALIGN(ring_size, I40E_DMA_MEM_ALIGN);
1718         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
1719                               ring_size, I40E_RING_BASE_ALIGN, socket_id);
1720         if (!rz) {
1721                 i40e_dev_rx_queue_release(rxq);
1722                 PMD_DRV_LOG(ERR, "Failed to reserve DMA memory for RX");
1723                 return -ENOMEM;
1724         }
1725
1726         /* Zero all the descriptors in the ring. */
1727         memset(rz->addr, 0, ring_size);
1728
1729         rxq->rx_ring_phys_addr = rte_mem_phy2mch(rz->memseg_id, rz->phys_addr);
1730         rxq->rx_ring = (union i40e_rx_desc *)rz->addr;
1731
1732         len = (uint16_t)(nb_desc + RTE_PMD_I40E_RX_MAX_BURST);
1733
1734         /* Allocate the software ring. */
1735         rxq->sw_ring =
1736                 rte_zmalloc_socket("i40e rx sw ring",
1737                                    sizeof(struct i40e_rx_entry) * len,
1738                                    RTE_CACHE_LINE_SIZE,
1739                                    socket_id);
1740         if (!rxq->sw_ring) {
1741                 i40e_dev_rx_queue_release(rxq);
1742                 PMD_DRV_LOG(ERR, "Failed to allocate memory for SW ring");
1743                 return -ENOMEM;
1744         }
1745
1746         i40e_reset_rx_queue(rxq);
1747         rxq->q_set = TRUE;
1748         dev->data->rx_queues[queue_idx] = rxq;
1749
1750         use_def_burst_func = check_rx_burst_bulk_alloc_preconditions(rxq);
1751
1752         if (!use_def_burst_func) {
1753 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
1754                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
1755                              "satisfied. Rx Burst Bulk Alloc function will be "
1756                              "used on port=%d, queue=%d.",
1757                              rxq->port_id, rxq->queue_id);
1758 #endif /* RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC */
1759         } else {
1760                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
1761                              "not satisfied, Scattered Rx is requested, "
1762                              "or RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC is "
1763                              "not enabled on port=%d, queue=%d.",
1764                              rxq->port_id, rxq->queue_id);
1765                 ad->rx_bulk_alloc_allowed = false;
1766         }
1767
1768         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1769                 if (!(vsi->enabled_tc & (1 << i)))
1770                         continue;
1771                 tc_mapping = rte_le_to_cpu_16(vsi->info.tc_mapping[i]);
1772                 base = (tc_mapping & I40E_AQ_VSI_TC_QUE_OFFSET_MASK) >>
1773                         I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT;
1774                 bsf = (tc_mapping & I40E_AQ_VSI_TC_QUE_NUMBER_MASK) >>
1775                         I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT;
1776
1777                 if (queue_idx >= base && queue_idx < (base + BIT(bsf)))
1778                         rxq->dcb_tc = i;
1779         }
1780
1781         return 0;
1782 }
1783
1784 void
1785 i40e_dev_rx_queue_release(void *rxq)
1786 {
1787         struct i40e_rx_queue *q = (struct i40e_rx_queue *)rxq;
1788
1789         if (!q) {
1790                 PMD_DRV_LOG(DEBUG, "Pointer to rxq is NULL");
1791                 return;
1792         }
1793
1794         i40e_rx_queue_release_mbufs(q);
1795         rte_free(q->sw_ring);
1796         rte_free(q);
1797 }
1798
1799 uint32_t
1800 i40e_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1801 {
1802 #define I40E_RXQ_SCAN_INTERVAL 4
1803         volatile union i40e_rx_desc *rxdp;
1804         struct i40e_rx_queue *rxq;
1805         uint16_t desc = 0;
1806
1807         if (unlikely(rx_queue_id >= dev->data->nb_rx_queues)) {
1808                 PMD_DRV_LOG(ERR, "Invalid RX queue id %u", rx_queue_id);
1809                 return 0;
1810         }
1811
1812         rxq = dev->data->rx_queues[rx_queue_id];
1813         rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1814         while ((desc < rxq->nb_rx_desc) &&
1815                 ((rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len) &
1816                 I40E_RXD_QW1_STATUS_MASK) >> I40E_RXD_QW1_STATUS_SHIFT) &
1817                                 (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
1818                 /**
1819                  * Check the DD bit of a rx descriptor of each 4 in a group,
1820                  * to avoid checking too frequently and downgrading performance
1821                  * too much.
1822                  */
1823                 desc += I40E_RXQ_SCAN_INTERVAL;
1824                 rxdp += I40E_RXQ_SCAN_INTERVAL;
1825                 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1826                         rxdp = &(rxq->rx_ring[rxq->rx_tail +
1827                                         desc - rxq->nb_rx_desc]);
1828         }
1829
1830         return desc;
1831 }
1832
1833 int
1834 i40e_dev_rx_descriptor_done(void *rx_queue, uint16_t offset)
1835 {
1836         volatile union i40e_rx_desc *rxdp;
1837         struct i40e_rx_queue *rxq = rx_queue;
1838         uint16_t desc;
1839         int ret;
1840
1841         if (unlikely(offset >= rxq->nb_rx_desc)) {
1842                 PMD_DRV_LOG(ERR, "Invalid RX queue id %u", offset);
1843                 return 0;
1844         }
1845
1846         desc = rxq->rx_tail + offset;
1847         if (desc >= rxq->nb_rx_desc)
1848                 desc -= rxq->nb_rx_desc;
1849
1850         rxdp = &(rxq->rx_ring[desc]);
1851
1852         ret = !!(((rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len) &
1853                 I40E_RXD_QW1_STATUS_MASK) >> I40E_RXD_QW1_STATUS_SHIFT) &
1854                                 (1 << I40E_RX_DESC_STATUS_DD_SHIFT));
1855
1856         return ret;
1857 }
1858
1859 int
1860 i40e_dev_tx_queue_setup(struct rte_eth_dev *dev,
1861                         uint16_t queue_idx,
1862                         uint16_t nb_desc,
1863                         unsigned int socket_id,
1864                         const struct rte_eth_txconf *tx_conf)
1865 {
1866         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1867         struct i40e_vsi *vsi;
1868         struct i40e_pf *pf = NULL;
1869         struct i40e_vf *vf = NULL;
1870         struct i40e_tx_queue *txq;
1871         const struct rte_memzone *tz;
1872         uint32_t ring_size;
1873         uint16_t tx_rs_thresh, tx_free_thresh;
1874         uint16_t reg_idx, i, base, bsf, tc_mapping;
1875         int q_offset;
1876
1877         if (hw->mac.type == I40E_MAC_VF || hw->mac.type == I40E_MAC_X722_VF) {
1878                 vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1879                 vsi = &vf->vsi;
1880                 if (!vsi)
1881                         return -EINVAL;
1882                 reg_idx = queue_idx;
1883         } else {
1884                 pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1885                 vsi = i40e_pf_get_vsi_by_qindex(pf, queue_idx);
1886                 if (!vsi)
1887                         return -EINVAL;
1888                 q_offset = i40e_get_queue_offset_by_qindex(pf, queue_idx);
1889                 if (q_offset < 0)
1890                         return -EINVAL;
1891                 reg_idx = vsi->base_queue + q_offset;
1892         }
1893
1894         if (nb_desc % I40E_ALIGN_RING_DESC != 0 ||
1895             (nb_desc > I40E_MAX_RING_DESC) ||
1896             (nb_desc < I40E_MIN_RING_DESC)) {
1897                 PMD_DRV_LOG(ERR, "Number (%u) of transmit descriptors is "
1898                             "invalid", nb_desc);
1899                 return -EINVAL;
1900         }
1901
1902         /**
1903          * The following two parameters control the setting of the RS bit on
1904          * transmit descriptors. TX descriptors will have their RS bit set
1905          * after txq->tx_rs_thresh descriptors have been used. The TX
1906          * descriptor ring will be cleaned after txq->tx_free_thresh
1907          * descriptors are used or if the number of descriptors required to
1908          * transmit a packet is greater than the number of free TX descriptors.
1909          *
1910          * The following constraints must be satisfied:
1911          *  - tx_rs_thresh must be greater than 0.
1912          *  - tx_rs_thresh must be less than the size of the ring minus 2.
1913          *  - tx_rs_thresh must be less than or equal to tx_free_thresh.
1914          *  - tx_rs_thresh must be a divisor of the ring size.
1915          *  - tx_free_thresh must be greater than 0.
1916          *  - tx_free_thresh must be less than the size of the ring minus 3.
1917          *
1918          * One descriptor in the TX ring is used as a sentinel to avoid a H/W
1919          * race condition, hence the maximum threshold constraints. When set
1920          * to zero use default values.
1921          */
1922         tx_rs_thresh = (uint16_t)((tx_conf->tx_rs_thresh) ?
1923                 tx_conf->tx_rs_thresh : DEFAULT_TX_RS_THRESH);
1924         tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
1925                 tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH);
1926         if (tx_rs_thresh >= (nb_desc - 2)) {
1927                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than the "
1928                              "number of TX descriptors minus 2. "
1929                              "(tx_rs_thresh=%u port=%d queue=%d)",
1930                              (unsigned int)tx_rs_thresh,
1931                              (int)dev->data->port_id,
1932                              (int)queue_idx);
1933                 return I40E_ERR_PARAM;
1934         }
1935         if (tx_free_thresh >= (nb_desc - 3)) {
1936                 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the "
1937                              "number of TX descriptors minus 3. "
1938                              "(tx_free_thresh=%u port=%d queue=%d)",
1939                              (unsigned int)tx_free_thresh,
1940                              (int)dev->data->port_id,
1941                              (int)queue_idx);
1942                 return I40E_ERR_PARAM;
1943         }
1944         if (tx_rs_thresh > tx_free_thresh) {
1945                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or "
1946                              "equal to tx_free_thresh. (tx_free_thresh=%u"
1947                              " tx_rs_thresh=%u port=%d queue=%d)",
1948                              (unsigned int)tx_free_thresh,
1949                              (unsigned int)tx_rs_thresh,
1950                              (int)dev->data->port_id,
1951                              (int)queue_idx);
1952                 return I40E_ERR_PARAM;
1953         }
1954         if ((nb_desc % tx_rs_thresh) != 0) {
1955                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be a divisor of the "
1956                              "number of TX descriptors. (tx_rs_thresh=%u"
1957                              " port=%d queue=%d)",
1958                              (unsigned int)tx_rs_thresh,
1959                              (int)dev->data->port_id,
1960                              (int)queue_idx);
1961                 return I40E_ERR_PARAM;
1962         }
1963         if ((tx_rs_thresh > 1) && (tx_conf->tx_thresh.wthresh != 0)) {
1964                 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
1965                              "tx_rs_thresh is greater than 1. "
1966                              "(tx_rs_thresh=%u port=%d queue=%d)",
1967                              (unsigned int)tx_rs_thresh,
1968                              (int)dev->data->port_id,
1969                              (int)queue_idx);
1970                 return I40E_ERR_PARAM;
1971         }
1972
1973         /* Free memory if needed. */
1974         if (dev->data->tx_queues[queue_idx]) {
1975                 i40e_dev_tx_queue_release(dev->data->tx_queues[queue_idx]);
1976                 dev->data->tx_queues[queue_idx] = NULL;
1977         }
1978
1979         /* Allocate the TX queue data structure. */
1980         txq = rte_zmalloc_socket("i40e tx queue",
1981                                   sizeof(struct i40e_tx_queue),
1982                                   RTE_CACHE_LINE_SIZE,
1983                                   socket_id);
1984         if (!txq) {
1985                 PMD_DRV_LOG(ERR, "Failed to allocate memory for "
1986                             "tx queue structure");
1987                 return -ENOMEM;
1988         }
1989
1990         /* Allocate TX hardware ring descriptors. */
1991         ring_size = sizeof(struct i40e_tx_desc) * I40E_MAX_RING_DESC;
1992         ring_size = RTE_ALIGN(ring_size, I40E_DMA_MEM_ALIGN);
1993         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
1994                               ring_size, I40E_RING_BASE_ALIGN, socket_id);
1995         if (!tz) {
1996                 i40e_dev_tx_queue_release(txq);
1997                 PMD_DRV_LOG(ERR, "Failed to reserve DMA memory for TX");
1998                 return -ENOMEM;
1999         }
2000
2001         txq->nb_tx_desc = nb_desc;
2002         txq->tx_rs_thresh = tx_rs_thresh;
2003         txq->tx_free_thresh = tx_free_thresh;
2004         txq->pthresh = tx_conf->tx_thresh.pthresh;
2005         txq->hthresh = tx_conf->tx_thresh.hthresh;
2006         txq->wthresh = tx_conf->tx_thresh.wthresh;
2007         txq->queue_id = queue_idx;
2008         txq->reg_idx = reg_idx;
2009         txq->port_id = dev->data->port_id;
2010         txq->txq_flags = tx_conf->txq_flags;
2011         txq->vsi = vsi;
2012         txq->tx_deferred_start = tx_conf->tx_deferred_start;
2013
2014         txq->tx_ring_phys_addr = rte_mem_phy2mch(tz->memseg_id, tz->phys_addr);
2015         txq->tx_ring = (struct i40e_tx_desc *)tz->addr;
2016
2017         /* Allocate software ring */
2018         txq->sw_ring =
2019                 rte_zmalloc_socket("i40e tx sw ring",
2020                                    sizeof(struct i40e_tx_entry) * nb_desc,
2021                                    RTE_CACHE_LINE_SIZE,
2022                                    socket_id);
2023         if (!txq->sw_ring) {
2024                 i40e_dev_tx_queue_release(txq);
2025                 PMD_DRV_LOG(ERR, "Failed to allocate memory for SW TX ring");
2026                 return -ENOMEM;
2027         }
2028
2029         i40e_reset_tx_queue(txq);
2030         txq->q_set = TRUE;
2031         dev->data->tx_queues[queue_idx] = txq;
2032
2033         /* Use a simple TX queue without offloads or multi segs if possible */
2034         i40e_set_tx_function_flag(dev, txq);
2035
2036         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2037                 if (!(vsi->enabled_tc & (1 << i)))
2038                         continue;
2039                 tc_mapping = rte_le_to_cpu_16(vsi->info.tc_mapping[i]);
2040                 base = (tc_mapping & I40E_AQ_VSI_TC_QUE_OFFSET_MASK) >>
2041                         I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT;
2042                 bsf = (tc_mapping & I40E_AQ_VSI_TC_QUE_NUMBER_MASK) >>
2043                         I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT;
2044
2045                 if (queue_idx >= base && queue_idx < (base + BIT(bsf)))
2046                         txq->dcb_tc = i;
2047         }
2048
2049         return 0;
2050 }
2051
2052 void
2053 i40e_dev_tx_queue_release(void *txq)
2054 {
2055         struct i40e_tx_queue *q = (struct i40e_tx_queue *)txq;
2056
2057         if (!q) {
2058                 PMD_DRV_LOG(DEBUG, "Pointer to TX queue is NULL");
2059                 return;
2060         }
2061
2062         i40e_tx_queue_release_mbufs(q);
2063         rte_free(q->sw_ring);
2064         rte_free(q);
2065 }
2066
2067 const struct rte_memzone *
2068 i40e_memzone_reserve(const char *name, uint32_t len, int socket_id)
2069 {
2070         const struct rte_memzone *mz;
2071
2072         mz = rte_memzone_lookup(name);
2073         if (mz)
2074                 return mz;
2075
2076         if (rte_xen_dom0_supported())
2077                 mz = rte_memzone_reserve_bounded(name, len,
2078                                 socket_id, 0, I40E_RING_BASE_ALIGN, RTE_PGSIZE_2M);
2079         else
2080                 mz = rte_memzone_reserve_aligned(name, len,
2081                                 socket_id, 0, I40E_RING_BASE_ALIGN);
2082         return mz;
2083 }
2084
2085 void
2086 i40e_rx_queue_release_mbufs(struct i40e_rx_queue *rxq)
2087 {
2088         uint16_t i;
2089
2090         /* SSE Vector driver has a different way of releasing mbufs. */
2091         if (rxq->rx_using_sse) {
2092                 i40e_rx_queue_release_mbufs_vec(rxq);
2093                 return;
2094         }
2095
2096         if (!rxq->sw_ring) {
2097                 PMD_DRV_LOG(DEBUG, "Pointer to sw_ring is NULL");
2098                 return;
2099         }
2100
2101         for (i = 0; i < rxq->nb_rx_desc; i++) {
2102                 if (rxq->sw_ring[i].mbuf) {
2103                         rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
2104                         rxq->sw_ring[i].mbuf = NULL;
2105                 }
2106         }
2107 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
2108         if (rxq->rx_nb_avail == 0)
2109                 return;
2110         for (i = 0; i < rxq->rx_nb_avail; i++) {
2111                 struct rte_mbuf *mbuf;
2112
2113                 mbuf = rxq->rx_stage[rxq->rx_next_avail + i];
2114                 rte_pktmbuf_free_seg(mbuf);
2115         }
2116         rxq->rx_nb_avail = 0;
2117 #endif /* RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC */
2118 }
2119
2120 void
2121 i40e_reset_rx_queue(struct i40e_rx_queue *rxq)
2122 {
2123         unsigned i;
2124         uint16_t len;
2125
2126         if (!rxq) {
2127                 PMD_DRV_LOG(DEBUG, "Pointer to rxq is NULL");
2128                 return;
2129         }
2130
2131 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
2132         if (check_rx_burst_bulk_alloc_preconditions(rxq) == 0)
2133                 len = (uint16_t)(rxq->nb_rx_desc + RTE_PMD_I40E_RX_MAX_BURST);
2134         else
2135 #endif /* RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC */
2136                 len = rxq->nb_rx_desc;
2137
2138         for (i = 0; i < len * sizeof(union i40e_rx_desc); i++)
2139                 ((volatile char *)rxq->rx_ring)[i] = 0;
2140
2141         memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
2142         for (i = 0; i < RTE_PMD_I40E_RX_MAX_BURST; ++i)
2143                 rxq->sw_ring[rxq->nb_rx_desc + i].mbuf = &rxq->fake_mbuf;
2144
2145 #ifdef RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC
2146         rxq->rx_nb_avail = 0;
2147         rxq->rx_next_avail = 0;
2148         rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
2149 #endif /* RTE_LIBRTE_I40E_RX_ALLOW_BULK_ALLOC */
2150         rxq->rx_tail = 0;
2151         rxq->nb_rx_hold = 0;
2152         rxq->pkt_first_seg = NULL;
2153         rxq->pkt_last_seg = NULL;
2154
2155         rxq->rxrearm_start = 0;
2156         rxq->rxrearm_nb = 0;
2157 }
2158
2159 void
2160 i40e_tx_queue_release_mbufs(struct i40e_tx_queue *txq)
2161 {
2162         struct rte_eth_dev *dev;
2163         uint16_t i;
2164
2165         dev = &rte_eth_devices[txq->port_id];
2166
2167         if (!txq || !txq->sw_ring) {
2168                 PMD_DRV_LOG(DEBUG, "Pointer to rxq or sw_ring is NULL");
2169                 return;
2170         }
2171
2172         /**
2173          *  vPMD tx will not set sw_ring's mbuf to NULL after free,
2174          *  so need to free remains more carefully.
2175          */
2176         if (dev->tx_pkt_burst == i40e_xmit_pkts_vec) {
2177                 i = txq->tx_next_dd - txq->tx_rs_thresh + 1;
2178                 if (txq->tx_tail < i) {
2179                         for (; i < txq->nb_tx_desc; i++) {
2180                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
2181                                 txq->sw_ring[i].mbuf = NULL;
2182                         }
2183                         i = 0;
2184                 }
2185                 for (; i < txq->tx_tail; i++) {
2186                         rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
2187                         txq->sw_ring[i].mbuf = NULL;
2188                 }
2189         } else {
2190                 for (i = 0; i < txq->nb_tx_desc; i++) {
2191                         if (txq->sw_ring[i].mbuf) {
2192                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
2193                                 txq->sw_ring[i].mbuf = NULL;
2194                         }
2195                 }
2196         }
2197 }
2198
2199 void
2200 i40e_reset_tx_queue(struct i40e_tx_queue *txq)
2201 {
2202         struct i40e_tx_entry *txe;
2203         uint16_t i, prev, size;
2204
2205         if (!txq) {
2206                 PMD_DRV_LOG(DEBUG, "Pointer to txq is NULL");
2207                 return;
2208         }
2209
2210         txe = txq->sw_ring;
2211         size = sizeof(struct i40e_tx_desc) * txq->nb_tx_desc;
2212         for (i = 0; i < size; i++)
2213                 ((volatile char *)txq->tx_ring)[i] = 0;
2214
2215         prev = (uint16_t)(txq->nb_tx_desc - 1);
2216         for (i = 0; i < txq->nb_tx_desc; i++) {
2217                 volatile struct i40e_tx_desc *txd = &txq->tx_ring[i];
2218
2219                 txd->cmd_type_offset_bsz =
2220                         rte_cpu_to_le_64(I40E_TX_DESC_DTYPE_DESC_DONE);
2221                 txe[i].mbuf =  NULL;
2222                 txe[i].last_id = i;
2223                 txe[prev].next_id = i;
2224                 prev = i;
2225         }
2226
2227         txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
2228         txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
2229
2230         txq->tx_tail = 0;
2231         txq->nb_tx_used = 0;
2232
2233         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
2234         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
2235 }
2236
2237 /* Init the TX queue in hardware */
2238 int
2239 i40e_tx_queue_init(struct i40e_tx_queue *txq)
2240 {
2241         enum i40e_status_code err = I40E_SUCCESS;
2242         struct i40e_vsi *vsi = txq->vsi;
2243         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
2244         uint16_t pf_q = txq->reg_idx;
2245         struct i40e_hmc_obj_txq tx_ctx;
2246         uint32_t qtx_ctl;
2247
2248         /* clear the context structure first */
2249         memset(&tx_ctx, 0, sizeof(tx_ctx));
2250         tx_ctx.new_context = 1;
2251         tx_ctx.base = txq->tx_ring_phys_addr / I40E_QUEUE_BASE_ADDR_UNIT;
2252         tx_ctx.qlen = txq->nb_tx_desc;
2253
2254 #ifdef RTE_LIBRTE_IEEE1588
2255         tx_ctx.timesync_ena = 1;
2256 #endif
2257         tx_ctx.rdylist = rte_le_to_cpu_16(vsi->info.qs_handle[txq->dcb_tc]);
2258         if (vsi->type == I40E_VSI_FDIR)
2259                 tx_ctx.fd_ena = TRUE;
2260
2261         err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2262         if (err != I40E_SUCCESS) {
2263                 PMD_DRV_LOG(ERR, "Failure of clean lan tx queue context");
2264                 return err;
2265         }
2266
2267         err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2268         if (err != I40E_SUCCESS) {
2269                 PMD_DRV_LOG(ERR, "Failure of set lan tx queue context");
2270                 return err;
2271         }
2272
2273         /* Now associate this queue with this PCI function */
2274         qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
2275         qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2276                                         I40E_QTX_CTL_PF_INDX_MASK);
2277         I40E_WRITE_REG(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2278         I40E_WRITE_FLUSH(hw);
2279
2280         txq->qtx_tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2281
2282         return err;
2283 }
2284
2285 int
2286 i40e_alloc_rx_queue_mbufs(struct i40e_rx_queue *rxq)
2287 {
2288         struct i40e_rx_entry *rxe = rxq->sw_ring;
2289         uint64_t dma_addr;
2290         uint16_t i;
2291
2292         for (i = 0; i < rxq->nb_rx_desc; i++) {
2293                 volatile union i40e_rx_desc *rxd;
2294                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mp);
2295
2296                 if (unlikely(!mbuf)) {
2297                         PMD_DRV_LOG(ERR, "Failed to allocate mbuf for RX");
2298                         return -ENOMEM;
2299                 }
2300
2301                 rte_mbuf_refcnt_set(mbuf, 1);
2302                 mbuf->next = NULL;
2303                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
2304                 mbuf->nb_segs = 1;
2305                 mbuf->port = rxq->port_id;
2306
2307                 dma_addr =
2308                         rte_cpu_to_le_64(rte_mbuf_data_dma_addr_default(mbuf));
2309
2310                 rxd = &rxq->rx_ring[i];
2311                 rxd->read.pkt_addr = dma_addr;
2312                 rxd->read.hdr_addr = 0;
2313 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
2314                 rxd->read.rsvd1 = 0;
2315                 rxd->read.rsvd2 = 0;
2316 #endif /* RTE_LIBRTE_I40E_16BYTE_RX_DESC */
2317
2318                 rxe[i].mbuf = mbuf;
2319         }
2320
2321         return 0;
2322 }
2323
2324 /*
2325  * Calculate the buffer length, and check the jumbo frame
2326  * and maximum packet length.
2327  */
2328 static int
2329 i40e_rx_queue_config(struct i40e_rx_queue *rxq)
2330 {
2331         struct i40e_pf *pf = I40E_VSI_TO_PF(rxq->vsi);
2332         struct i40e_hw *hw = I40E_VSI_TO_HW(rxq->vsi);
2333         struct rte_eth_dev_data *data = pf->dev_data;
2334         uint16_t buf_size, len;
2335
2336         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mp) -
2337                 RTE_PKTMBUF_HEADROOM);
2338
2339         switch (pf->flags & (I40E_FLAG_HEADER_SPLIT_DISABLED |
2340                         I40E_FLAG_HEADER_SPLIT_ENABLED)) {
2341         case I40E_FLAG_HEADER_SPLIT_ENABLED: /* Not supported */
2342                 rxq->rx_hdr_len = RTE_ALIGN(I40E_RXBUF_SZ_1024,
2343                                 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2344                 rxq->rx_buf_len = RTE_ALIGN(I40E_RXBUF_SZ_2048,
2345                                 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2346                 rxq->hs_mode = i40e_header_split_enabled;
2347                 break;
2348         case I40E_FLAG_HEADER_SPLIT_DISABLED:
2349         default:
2350                 rxq->rx_hdr_len = 0;
2351                 rxq->rx_buf_len = RTE_ALIGN_FLOOR(buf_size,
2352                         (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2353                 rxq->hs_mode = i40e_header_split_none;
2354                 break;
2355         }
2356
2357         len = hw->func_caps.rx_buf_chain_len * rxq->rx_buf_len;
2358         rxq->max_pkt_len = RTE_MIN(len, data->dev_conf.rxmode.max_rx_pkt_len);
2359         if (data->dev_conf.rxmode.jumbo_frame == 1) {
2360                 if (rxq->max_pkt_len <= ETHER_MAX_LEN ||
2361                         rxq->max_pkt_len > I40E_FRAME_SIZE_MAX) {
2362                         PMD_DRV_LOG(ERR, "maximum packet length must "
2363                                     "be larger than %u and smaller than %u,"
2364                                     "as jumbo frame is enabled",
2365                                     (uint32_t)ETHER_MAX_LEN,
2366                                     (uint32_t)I40E_FRAME_SIZE_MAX);
2367                         return I40E_ERR_CONFIG;
2368                 }
2369         } else {
2370                 if (rxq->max_pkt_len < ETHER_MIN_LEN ||
2371                         rxq->max_pkt_len > ETHER_MAX_LEN) {
2372                         PMD_DRV_LOG(ERR, "maximum packet length must be "
2373                                     "larger than %u and smaller than %u, "
2374                                     "as jumbo frame is disabled",
2375                                     (uint32_t)ETHER_MIN_LEN,
2376                                     (uint32_t)ETHER_MAX_LEN);
2377                         return I40E_ERR_CONFIG;
2378                 }
2379         }
2380
2381         return 0;
2382 }
2383
2384 /* Init the RX queue in hardware */
2385 int
2386 i40e_rx_queue_init(struct i40e_rx_queue *rxq)
2387 {
2388         int err = I40E_SUCCESS;
2389         struct i40e_hw *hw = I40E_VSI_TO_HW(rxq->vsi);
2390         struct rte_eth_dev_data *dev_data = I40E_VSI_TO_DEV_DATA(rxq->vsi);
2391         uint16_t pf_q = rxq->reg_idx;
2392         uint16_t buf_size;
2393         struct i40e_hmc_obj_rxq rx_ctx;
2394
2395         err = i40e_rx_queue_config(rxq);
2396         if (err < 0) {
2397                 PMD_DRV_LOG(ERR, "Failed to config RX queue");
2398                 return err;
2399         }
2400
2401         /* Clear the context structure first */
2402         memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
2403         rx_ctx.dbuff = rxq->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2404         rx_ctx.hbuff = rxq->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2405
2406         rx_ctx.base = rxq->rx_ring_phys_addr / I40E_QUEUE_BASE_ADDR_UNIT;
2407         rx_ctx.qlen = rxq->nb_rx_desc;
2408 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
2409         rx_ctx.dsize = 1;
2410 #endif
2411         rx_ctx.dtype = rxq->hs_mode;
2412         if (rxq->hs_mode)
2413                 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_ALL;
2414         else
2415                 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_NONE;
2416         rx_ctx.rxmax = rxq->max_pkt_len;
2417         rx_ctx.tphrdesc_ena = 1;
2418         rx_ctx.tphwdesc_ena = 1;
2419         rx_ctx.tphdata_ena = 1;
2420         rx_ctx.tphhead_ena = 1;
2421         rx_ctx.lrxqthresh = 2;
2422         rx_ctx.crcstrip = (rxq->crc_len == 0) ? 1 : 0;
2423         rx_ctx.l2tsel = 1;
2424         /* showiv indicates if inner VLAN is stripped inside of tunnel
2425          * packet. When set it to 1, vlan information is stripped from
2426          * the inner header, but the hardware does not put it in the
2427          * descriptor. So set it zero by default.
2428          */
2429         rx_ctx.showiv = 0;
2430         rx_ctx.prefena = 1;
2431
2432         err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2433         if (err != I40E_SUCCESS) {
2434                 PMD_DRV_LOG(ERR, "Failed to clear LAN RX queue context");
2435                 return err;
2436         }
2437         err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2438         if (err != I40E_SUCCESS) {
2439                 PMD_DRV_LOG(ERR, "Failed to set LAN RX queue context");
2440                 return err;
2441         }
2442
2443         rxq->qrx_tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2444
2445         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mp) -
2446                 RTE_PKTMBUF_HEADROOM);
2447
2448         /* Check if scattered RX needs to be used. */
2449         if ((rxq->max_pkt_len + 2 * I40E_VLAN_TAG_SIZE) > buf_size) {
2450                 dev_data->scattered_rx = 1;
2451         }
2452
2453         /* Init the RX tail regieter. */
2454         I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
2455
2456         return 0;
2457 }
2458
2459 void
2460 i40e_dev_clear_queues(struct rte_eth_dev *dev)
2461 {
2462         uint16_t i;
2463
2464         PMD_INIT_FUNC_TRACE();
2465
2466         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2467                 if (!dev->data->tx_queues[i])
2468                         continue;
2469                 i40e_tx_queue_release_mbufs(dev->data->tx_queues[i]);
2470                 i40e_reset_tx_queue(dev->data->tx_queues[i]);
2471         }
2472
2473         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2474                 if (!dev->data->rx_queues[i])
2475                         continue;
2476                 i40e_rx_queue_release_mbufs(dev->data->rx_queues[i]);
2477                 i40e_reset_rx_queue(dev->data->rx_queues[i]);
2478         }
2479 }
2480
2481 void
2482 i40e_dev_free_queues(struct rte_eth_dev *dev)
2483 {
2484         uint16_t i;
2485
2486         PMD_INIT_FUNC_TRACE();
2487
2488         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2489                 if (!dev->data->rx_queues[i])
2490                         continue;
2491                 i40e_dev_rx_queue_release(dev->data->rx_queues[i]);
2492                 dev->data->rx_queues[i] = NULL;
2493         }
2494         dev->data->nb_rx_queues = 0;
2495
2496         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2497                 if (!dev->data->tx_queues[i])
2498                         continue;
2499                 i40e_dev_tx_queue_release(dev->data->tx_queues[i]);
2500                 dev->data->tx_queues[i] = NULL;
2501         }
2502         dev->data->nb_tx_queues = 0;
2503 }
2504
2505 #define I40E_FDIR_NUM_TX_DESC  I40E_MIN_RING_DESC
2506 #define I40E_FDIR_NUM_RX_DESC  I40E_MIN_RING_DESC
2507
2508 enum i40e_status_code
2509 i40e_fdir_setup_tx_resources(struct i40e_pf *pf)
2510 {
2511         struct i40e_tx_queue *txq;
2512         const struct rte_memzone *tz = NULL;
2513         uint32_t ring_size;
2514         struct rte_eth_dev *dev;
2515
2516         if (!pf) {
2517                 PMD_DRV_LOG(ERR, "PF is not available");
2518                 return I40E_ERR_BAD_PTR;
2519         }
2520
2521         dev = pf->adapter->eth_dev;
2522
2523         /* Allocate the TX queue data structure. */
2524         txq = rte_zmalloc_socket("i40e fdir tx queue",
2525                                   sizeof(struct i40e_tx_queue),
2526                                   RTE_CACHE_LINE_SIZE,
2527                                   SOCKET_ID_ANY);
2528         if (!txq) {
2529                 PMD_DRV_LOG(ERR, "Failed to allocate memory for "
2530                                         "tx queue structure.");
2531                 return I40E_ERR_NO_MEMORY;
2532         }
2533
2534         /* Allocate TX hardware ring descriptors. */
2535         ring_size = sizeof(struct i40e_tx_desc) * I40E_FDIR_NUM_TX_DESC;
2536         ring_size = RTE_ALIGN(ring_size, I40E_DMA_MEM_ALIGN);
2537
2538         tz = rte_eth_dma_zone_reserve(dev, "fdir_tx_ring",
2539                                       I40E_FDIR_QUEUE_ID, ring_size,
2540                                       I40E_RING_BASE_ALIGN, SOCKET_ID_ANY);
2541         if (!tz) {
2542                 i40e_dev_tx_queue_release(txq);
2543                 PMD_DRV_LOG(ERR, "Failed to reserve DMA memory for TX.");
2544                 return I40E_ERR_NO_MEMORY;
2545         }
2546
2547         txq->nb_tx_desc = I40E_FDIR_NUM_TX_DESC;
2548         txq->queue_id = I40E_FDIR_QUEUE_ID;
2549         txq->reg_idx = pf->fdir.fdir_vsi->base_queue;
2550         txq->vsi = pf->fdir.fdir_vsi;
2551
2552         txq->tx_ring_phys_addr = rte_mem_phy2mch(tz->memseg_id, tz->phys_addr);
2553         txq->tx_ring = (struct i40e_tx_desc *)tz->addr;
2554         /*
2555          * don't need to allocate software ring and reset for the fdir
2556          * program queue just set the queue has been configured.
2557          */
2558         txq->q_set = TRUE;
2559         pf->fdir.txq = txq;
2560
2561         return I40E_SUCCESS;
2562 }
2563
2564 enum i40e_status_code
2565 i40e_fdir_setup_rx_resources(struct i40e_pf *pf)
2566 {
2567         struct i40e_rx_queue *rxq;
2568         const struct rte_memzone *rz = NULL;
2569         uint32_t ring_size;
2570         struct rte_eth_dev *dev;
2571
2572         if (!pf) {
2573                 PMD_DRV_LOG(ERR, "PF is not available");
2574                 return I40E_ERR_BAD_PTR;
2575         }
2576
2577         dev = pf->adapter->eth_dev;
2578
2579         /* Allocate the RX queue data structure. */
2580         rxq = rte_zmalloc_socket("i40e fdir rx queue",
2581                                   sizeof(struct i40e_rx_queue),
2582                                   RTE_CACHE_LINE_SIZE,
2583                                   SOCKET_ID_ANY);
2584         if (!rxq) {
2585                 PMD_DRV_LOG(ERR, "Failed to allocate memory for "
2586                                         "rx queue structure.");
2587                 return I40E_ERR_NO_MEMORY;
2588         }
2589
2590         /* Allocate RX hardware ring descriptors. */
2591         ring_size = sizeof(union i40e_rx_desc) * I40E_FDIR_NUM_RX_DESC;
2592         ring_size = RTE_ALIGN(ring_size, I40E_DMA_MEM_ALIGN);
2593
2594         rz = rte_eth_dma_zone_reserve(dev, "fdir_rx_ring",
2595                                       I40E_FDIR_QUEUE_ID, ring_size,
2596                                       I40E_RING_BASE_ALIGN, SOCKET_ID_ANY);
2597         if (!rz) {
2598                 i40e_dev_rx_queue_release(rxq);
2599                 PMD_DRV_LOG(ERR, "Failed to reserve DMA memory for RX.");
2600                 return I40E_ERR_NO_MEMORY;
2601         }
2602
2603         rxq->nb_rx_desc = I40E_FDIR_NUM_RX_DESC;
2604         rxq->queue_id = I40E_FDIR_QUEUE_ID;
2605         rxq->reg_idx = pf->fdir.fdir_vsi->base_queue;
2606         rxq->vsi = pf->fdir.fdir_vsi;
2607
2608         rxq->rx_ring_phys_addr = rte_mem_phy2mch(rz->memseg_id, rz->phys_addr);
2609         memset(rz->addr, 0, I40E_FDIR_NUM_RX_DESC * sizeof(union i40e_rx_desc));
2610         rxq->rx_ring = (union i40e_rx_desc *)rz->addr;
2611
2612         /*
2613          * Don't need to allocate software ring and reset for the fdir
2614          * rx queue, just set the queue has been configured.
2615          */
2616         rxq->q_set = TRUE;
2617         pf->fdir.rxq = rxq;
2618
2619         return I40E_SUCCESS;
2620 }
2621
2622 void
2623 i40e_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
2624         struct rte_eth_rxq_info *qinfo)
2625 {
2626         struct i40e_rx_queue *rxq;
2627
2628         rxq = dev->data->rx_queues[queue_id];
2629
2630         qinfo->mp = rxq->mp;
2631         qinfo->scattered_rx = dev->data->scattered_rx;
2632         qinfo->nb_desc = rxq->nb_rx_desc;
2633
2634         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
2635         qinfo->conf.rx_drop_en = rxq->drop_en;
2636         qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
2637 }
2638
2639 void
2640 i40e_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
2641         struct rte_eth_txq_info *qinfo)
2642 {
2643         struct i40e_tx_queue *txq;
2644
2645         txq = dev->data->tx_queues[queue_id];
2646
2647         qinfo->nb_desc = txq->nb_tx_desc;
2648
2649         qinfo->conf.tx_thresh.pthresh = txq->pthresh;
2650         qinfo->conf.tx_thresh.hthresh = txq->hthresh;
2651         qinfo->conf.tx_thresh.wthresh = txq->wthresh;
2652
2653         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
2654         qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
2655         qinfo->conf.txq_flags = txq->txq_flags;
2656         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
2657 }
2658
2659 void __attribute__((cold))
2660 i40e_set_rx_function(struct rte_eth_dev *dev)
2661 {
2662         struct i40e_adapter *ad =
2663                 I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2664         uint16_t rx_using_sse, i;
2665         /* In order to allow Vector Rx there are a few configuration
2666          * conditions to be met and Rx Bulk Allocation should be allowed.
2667          */
2668         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2669                 if (i40e_rx_vec_dev_conf_condition_check(dev) ||
2670                     !ad->rx_bulk_alloc_allowed) {
2671                         PMD_INIT_LOG(DEBUG, "Port[%d] doesn't meet"
2672                                      " Vector Rx preconditions",
2673                                      dev->data->port_id);
2674
2675                         ad->rx_vec_allowed = false;
2676                 }
2677                 if (ad->rx_vec_allowed) {
2678                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
2679                                 struct i40e_rx_queue *rxq =
2680                                         dev->data->rx_queues[i];
2681
2682                                 if (rxq && i40e_rxq_vec_setup(rxq)) {
2683                                         ad->rx_vec_allowed = false;
2684                                         break;
2685                                 }
2686                         }
2687                 }
2688         }
2689
2690         if (dev->data->scattered_rx) {
2691                 /* Set the non-LRO scattered callback: there are Vector and
2692                  * single allocation versions.
2693                  */
2694                 if (ad->rx_vec_allowed) {
2695                         PMD_INIT_LOG(DEBUG, "Using Vector Scattered Rx "
2696                                             "callback (port=%d).",
2697                                      dev->data->port_id);
2698
2699                         dev->rx_pkt_burst = i40e_recv_scattered_pkts_vec;
2700                 } else {
2701                         PMD_INIT_LOG(DEBUG, "Using a Scattered with bulk "
2702                                            "allocation callback (port=%d).",
2703                                      dev->data->port_id);
2704                         dev->rx_pkt_burst = i40e_recv_scattered_pkts;
2705                 }
2706         /* If parameters allow we are going to choose between the following
2707          * callbacks:
2708          *    - Vector
2709          *    - Bulk Allocation
2710          *    - Single buffer allocation (the simplest one)
2711          */
2712         } else if (ad->rx_vec_allowed) {
2713                 PMD_INIT_LOG(DEBUG, "Vector rx enabled, please make sure RX "
2714                                     "burst size no less than %d (port=%d).",
2715                              RTE_I40E_DESCS_PER_LOOP,
2716                              dev->data->port_id);
2717
2718                 dev->rx_pkt_burst = i40e_recv_pkts_vec;
2719         } else if (ad->rx_bulk_alloc_allowed) {
2720                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
2721                                     "satisfied. Rx Burst Bulk Alloc function "
2722                                     "will be used on port=%d.",
2723                              dev->data->port_id);
2724
2725                 dev->rx_pkt_burst = i40e_recv_pkts_bulk_alloc;
2726         } else {
2727                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are not "
2728                                     "satisfied, or Scattered Rx is requested "
2729                                     "(port=%d).",
2730                              dev->data->port_id);
2731
2732                 dev->rx_pkt_burst = i40e_recv_pkts;
2733         }
2734
2735         /* Propagate information about RX function choice through all queues. */
2736         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2737                 rx_using_sse =
2738                         (dev->rx_pkt_burst == i40e_recv_scattered_pkts_vec ||
2739                          dev->rx_pkt_burst == i40e_recv_pkts_vec);
2740
2741                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2742                         struct i40e_rx_queue *rxq = dev->data->rx_queues[i];
2743
2744                         if (rxq)
2745                                 rxq->rx_using_sse = rx_using_sse;
2746                 }
2747         }
2748 }
2749
2750 void __attribute__((cold))
2751 i40e_set_tx_function_flag(struct rte_eth_dev *dev, struct i40e_tx_queue *txq)
2752 {
2753         struct i40e_adapter *ad =
2754                 I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2755
2756         /* Use a simple Tx queue (no offloads, no multi segs) if possible */
2757         if (((txq->txq_flags & I40E_SIMPLE_FLAGS) == I40E_SIMPLE_FLAGS)
2758                         && (txq->tx_rs_thresh >= RTE_PMD_I40E_TX_MAX_BURST)) {
2759                 if (txq->tx_rs_thresh <= RTE_I40E_TX_MAX_FREE_BUF_SZ) {
2760                         PMD_INIT_LOG(DEBUG, "Vector tx"
2761                                      " can be enabled on this txq.");
2762
2763                 } else {
2764                         ad->tx_vec_allowed = false;
2765                 }
2766         } else {
2767                 ad->tx_simple_allowed = false;
2768         }
2769 }
2770
2771 void __attribute__((cold))
2772 i40e_set_tx_function(struct rte_eth_dev *dev)
2773 {
2774         struct i40e_adapter *ad =
2775                 I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2776         int i;
2777
2778         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2779                 if (ad->tx_vec_allowed) {
2780                         for (i = 0; i < dev->data->nb_tx_queues; i++) {
2781                                 struct i40e_tx_queue *txq =
2782                                         dev->data->tx_queues[i];
2783
2784                                 if (txq && i40e_txq_vec_setup(txq)) {
2785                                         ad->tx_vec_allowed = false;
2786                                         break;
2787                                 }
2788                         }
2789                 }
2790         }
2791
2792         if (ad->tx_simple_allowed) {
2793                 if (ad->tx_vec_allowed) {
2794                         PMD_INIT_LOG(DEBUG, "Vector tx finally be used.");
2795                         dev->tx_pkt_burst = i40e_xmit_pkts_vec;
2796                 } else {
2797                         PMD_INIT_LOG(DEBUG, "Simple tx finally be used.");
2798                         dev->tx_pkt_burst = i40e_xmit_pkts_simple;
2799                 }
2800         } else {
2801                 PMD_INIT_LOG(DEBUG, "Xmit tx finally be used.");
2802                 dev->tx_pkt_burst = i40e_xmit_pkts;
2803         }
2804 }
2805
2806 /* Stubs needed for linkage when CONFIG_RTE_I40E_INC_VECTOR is set to 'n' */
2807 int __attribute__((weak))
2808 i40e_rx_vec_dev_conf_condition_check(struct rte_eth_dev __rte_unused *dev)
2809 {
2810         return -1;
2811 }
2812
2813 uint16_t __attribute__((weak))
2814 i40e_recv_pkts_vec(
2815         void __rte_unused *rx_queue,
2816         struct rte_mbuf __rte_unused **rx_pkts,
2817         uint16_t __rte_unused nb_pkts)
2818 {
2819         return 0;
2820 }
2821
2822 uint16_t __attribute__((weak))
2823 i40e_recv_scattered_pkts_vec(
2824         void __rte_unused *rx_queue,
2825         struct rte_mbuf __rte_unused **rx_pkts,
2826         uint16_t __rte_unused nb_pkts)
2827 {
2828         return 0;
2829 }
2830
2831 int __attribute__((weak))
2832 i40e_rxq_vec_setup(struct i40e_rx_queue __rte_unused *rxq)
2833 {
2834         return -1;
2835 }
2836
2837 int __attribute__((weak))
2838 i40e_txq_vec_setup(struct i40e_tx_queue __rte_unused *txq)
2839 {
2840         return -1;
2841 }
2842
2843 void __attribute__((weak))
2844 i40e_rx_queue_release_mbufs_vec(struct i40e_rx_queue __rte_unused*rxq)
2845 {
2846         return;
2847 }
2848
2849 uint16_t __attribute__((weak))
2850 i40e_xmit_pkts_vec(void __rte_unused *tx_queue,
2851                    struct rte_mbuf __rte_unused **tx_pkts,
2852                    uint16_t __rte_unused nb_pkts)
2853 {
2854         return 0;
2855 }