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