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