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