mbuf: make rearm data address naturally aligned
[dpdk.git] / drivers / net / i40e / i40e_rxtx_vec_sse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <rte_ethdev.h>
36 #include <rte_malloc.h>
37
38 #include "base/i40e_prototype.h"
39 #include "base/i40e_type.h"
40 #include "i40e_ethdev.h"
41 #include "i40e_rxtx.h"
42 #include "i40e_rxtx_vec_common.h"
43
44 #include <tmmintrin.h>
45
46 #ifndef __INTEL_COMPILER
47 #pragma GCC diagnostic ignored "-Wcast-qual"
48 #endif
49
50 static inline void
51 i40e_rxq_rearm(struct i40e_rx_queue *rxq)
52 {
53         int i;
54         uint16_t rx_id;
55         volatile union i40e_rx_desc *rxdp;
56         struct i40e_rx_entry *rxep = &rxq->sw_ring[rxq->rxrearm_start];
57         struct rte_mbuf *mb0, *mb1;
58         __m128i hdr_room = _mm_set_epi64x(RTE_PKTMBUF_HEADROOM,
59                         RTE_PKTMBUF_HEADROOM);
60         __m128i dma_addr0, dma_addr1;
61
62         rxdp = rxq->rx_ring + rxq->rxrearm_start;
63
64         /* Pull 'n' more MBUFs into the software ring */
65         if (rte_mempool_get_bulk(rxq->mp,
66                                  (void *)rxep,
67                                  RTE_I40E_RXQ_REARM_THRESH) < 0) {
68                 if (rxq->rxrearm_nb + RTE_I40E_RXQ_REARM_THRESH >=
69                     rxq->nb_rx_desc) {
70                         dma_addr0 = _mm_setzero_si128();
71                         for (i = 0; i < RTE_I40E_DESCS_PER_LOOP; i++) {
72                                 rxep[i].mbuf = &rxq->fake_mbuf;
73                                 _mm_store_si128((__m128i *)&rxdp[i].read,
74                                                 dma_addr0);
75                         }
76                 }
77                 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed +=
78                         RTE_I40E_RXQ_REARM_THRESH;
79                 return;
80         }
81
82         /* Initialize the mbufs in vector, process 2 mbufs in one loop */
83         for (i = 0; i < RTE_I40E_RXQ_REARM_THRESH; i += 2, rxep += 2) {
84                 __m128i vaddr0, vaddr1;
85                 uintptr_t p0, p1;
86
87                 mb0 = rxep[0].mbuf;
88                 mb1 = rxep[1].mbuf;
89
90                 /* Flush mbuf with pkt template.
91                  * Data to be rearmed is 6 bytes long.
92                  */
93                 p0 = (uintptr_t)&mb0->rearm_data;
94                 *(uint64_t *)p0 = rxq->mbuf_initializer;
95                 p1 = (uintptr_t)&mb1->rearm_data;
96                 *(uint64_t *)p1 = rxq->mbuf_initializer;
97
98                 /* load buf_addr(lo 64bit) and buf_physaddr(hi 64bit) */
99                 vaddr0 = _mm_loadu_si128((__m128i *)&mb0->buf_addr);
100                 vaddr1 = _mm_loadu_si128((__m128i *)&mb1->buf_addr);
101
102                 /* convert pa to dma_addr hdr/data */
103                 dma_addr0 = _mm_unpackhi_epi64(vaddr0, vaddr0);
104                 dma_addr1 = _mm_unpackhi_epi64(vaddr1, vaddr1);
105
106                 /* add headroom to pa values */
107                 dma_addr0 = _mm_add_epi64(dma_addr0, hdr_room);
108                 dma_addr1 = _mm_add_epi64(dma_addr1, hdr_room);
109
110                 /* flush desc with pa dma_addr */
111                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr0);
112                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr1);
113         }
114
115         rxq->rxrearm_start += RTE_I40E_RXQ_REARM_THRESH;
116         if (rxq->rxrearm_start >= rxq->nb_rx_desc)
117                 rxq->rxrearm_start = 0;
118
119         rxq->rxrearm_nb -= RTE_I40E_RXQ_REARM_THRESH;
120
121         rx_id = (uint16_t)((rxq->rxrearm_start == 0) ?
122                              (rxq->nb_rx_desc - 1) : (rxq->rxrearm_start - 1));
123
124         /* Update the tail pointer on the NIC */
125         I40E_PCI_REG_WRITE(rxq->qrx_tail, rx_id);
126 }
127
128 /* Handling the offload flags (olflags) field takes computation
129  * time when receiving packets. Therefore we provide a flag to disable
130  * the processing of the olflags field when they are not needed. This
131  * gives improved performance, at the cost of losing the offload info
132  * in the received packet
133  */
134 #ifdef RTE_LIBRTE_I40E_RX_OLFLAGS_ENABLE
135
136 static inline void
137 desc_to_olflags_v(__m128i descs[4], struct rte_mbuf **rx_pkts)
138 {
139         __m128i vlan0, vlan1, rss, l3_l4e;
140
141         /* mask everything except RSS, flow director and VLAN flags
142          * bit2 is for VLAN tag, bit11 for flow director indication
143          * bit13:12 for RSS indication.
144          */
145         const __m128i rss_vlan_msk = _mm_set_epi32(
146                         0x1c03804, 0x1c03804, 0x1c03804, 0x1c03804);
147
148         const __m128i cksum_mask = _mm_set_epi32(
149                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
150                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
151                         PKT_RX_EIP_CKSUM_BAD,
152                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
153                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
154                         PKT_RX_EIP_CKSUM_BAD,
155                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
156                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
157                         PKT_RX_EIP_CKSUM_BAD,
158                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
159                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
160                         PKT_RX_EIP_CKSUM_BAD);
161
162         /* map rss and vlan type to rss hash and vlan flag */
163         const __m128i vlan_flags = _mm_set_epi8(0, 0, 0, 0,
164                         0, 0, 0, 0,
165                         0, 0, 0, PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED,
166                         0, 0, 0, 0);
167
168         const __m128i rss_flags = _mm_set_epi8(0, 0, 0, 0,
169                         0, 0, 0, 0,
170                         PKT_RX_RSS_HASH | PKT_RX_FDIR, PKT_RX_RSS_HASH, 0, 0,
171                         0, 0, PKT_RX_FDIR, 0);
172
173         const __m128i l3_l4e_flags = _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,
174                         /* shift right 1 bit to make sure it not exceed 255 */
175                         (PKT_RX_EIP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD |
176                          PKT_RX_IP_CKSUM_BAD) >> 1,
177                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_EIP_CKSUM_BAD |
178                          PKT_RX_L4_CKSUM_BAD) >> 1,
179                         (PKT_RX_EIP_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD) >> 1,
180                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_EIP_CKSUM_BAD) >> 1,
181                         (PKT_RX_L4_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD) >> 1,
182                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD) >> 1,
183                         PKT_RX_IP_CKSUM_BAD >> 1,
184                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD) >> 1);
185
186         vlan0 = _mm_unpackhi_epi32(descs[0], descs[1]);
187         vlan1 = _mm_unpackhi_epi32(descs[2], descs[3]);
188         vlan0 = _mm_unpacklo_epi64(vlan0, vlan1);
189
190         vlan1 = _mm_and_si128(vlan0, rss_vlan_msk);
191         vlan0 = _mm_shuffle_epi8(vlan_flags, vlan1);
192
193         rss = _mm_srli_epi32(vlan1, 11);
194         rss = _mm_shuffle_epi8(rss_flags, rss);
195
196         l3_l4e = _mm_srli_epi32(vlan1, 22);
197         l3_l4e = _mm_shuffle_epi8(l3_l4e_flags, l3_l4e);
198         /* then we shift left 1 bit */
199         l3_l4e = _mm_slli_epi32(l3_l4e, 1);
200         /* we need to mask out the reduntant bits */
201         l3_l4e = _mm_and_si128(l3_l4e, cksum_mask);
202
203         vlan0 = _mm_or_si128(vlan0, rss);
204         vlan0 = _mm_or_si128(vlan0, l3_l4e);
205
206         rx_pkts[0]->ol_flags = _mm_extract_epi16(vlan0, 0);
207         rx_pkts[1]->ol_flags = _mm_extract_epi16(vlan0, 2);
208         rx_pkts[2]->ol_flags = _mm_extract_epi16(vlan0, 4);
209         rx_pkts[3]->ol_flags = _mm_extract_epi16(vlan0, 6);
210 }
211 #else
212 #define desc_to_olflags_v(desc, rx_pkts) do {} while (0)
213 #endif
214
215 #define PKTLEN_SHIFT     10
216
217 static inline void
218 desc_to_ptype_v(__m128i descs[4], struct rte_mbuf **rx_pkts)
219 {
220         __m128i ptype0 = _mm_unpackhi_epi64(descs[0], descs[1]);
221         __m128i ptype1 = _mm_unpackhi_epi64(descs[2], descs[3]);
222
223         ptype0 = _mm_srli_epi64(ptype0, 30);
224         ptype1 = _mm_srli_epi64(ptype1, 30);
225
226         rx_pkts[0]->packet_type = i40e_rxd_pkt_type_mapping(_mm_extract_epi8(ptype0, 0));
227         rx_pkts[1]->packet_type = i40e_rxd_pkt_type_mapping(_mm_extract_epi8(ptype0, 8));
228         rx_pkts[2]->packet_type = i40e_rxd_pkt_type_mapping(_mm_extract_epi8(ptype1, 0));
229         rx_pkts[3]->packet_type = i40e_rxd_pkt_type_mapping(_mm_extract_epi8(ptype1, 8));
230 }
231
232  /*
233  * Notice:
234  * - nb_pkts < RTE_I40E_DESCS_PER_LOOP, just return no packet
235  * - nb_pkts > RTE_I40E_VPMD_RX_BURST, only scan RTE_I40E_VPMD_RX_BURST
236  *   numbers of DD bits
237  */
238 static inline uint16_t
239 _recv_raw_pkts_vec(struct i40e_rx_queue *rxq, struct rte_mbuf **rx_pkts,
240                    uint16_t nb_pkts, uint8_t *split_packet)
241 {
242         volatile union i40e_rx_desc *rxdp;
243         struct i40e_rx_entry *sw_ring;
244         uint16_t nb_pkts_recd;
245         int pos;
246         uint64_t var;
247         __m128i shuf_msk;
248
249         __m128i crc_adjust = _mm_set_epi16(
250                                 0, 0, 0,    /* ignore non-length fields */
251                                 -rxq->crc_len, /* sub crc on data_len */
252                                 0,          /* ignore high-16bits of pkt_len */
253                                 -rxq->crc_len, /* sub crc on pkt_len */
254                                 0, 0            /* ignore pkt_type field */
255                         );
256         __m128i dd_check, eop_check;
257
258         /* nb_pkts shall be less equal than RTE_I40E_MAX_RX_BURST */
259         nb_pkts = RTE_MIN(nb_pkts, RTE_I40E_MAX_RX_BURST);
260
261         /* nb_pkts has to be floor-aligned to RTE_I40E_DESCS_PER_LOOP */
262         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_I40E_DESCS_PER_LOOP);
263
264         /* Just the act of getting into the function from the application is
265          * going to cost about 7 cycles
266          */
267         rxdp = rxq->rx_ring + rxq->rx_tail;
268
269         rte_prefetch0(rxdp);
270
271         /* See if we need to rearm the RX queue - gives the prefetch a bit
272          * of time to act
273          */
274         if (rxq->rxrearm_nb > RTE_I40E_RXQ_REARM_THRESH)
275                 i40e_rxq_rearm(rxq);
276
277         /* Before we start moving massive data around, check to see if
278          * there is actually a packet available
279          */
280         if (!(rxdp->wb.qword1.status_error_len &
281                         rte_cpu_to_le_32(1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
282                 return 0;
283
284         /* 4 packets DD mask */
285         dd_check = _mm_set_epi64x(0x0000000100000001LL, 0x0000000100000001LL);
286
287         /* 4 packets EOP mask */
288         eop_check = _mm_set_epi64x(0x0000000200000002LL, 0x0000000200000002LL);
289
290         /* mask to shuffle from desc. to mbuf */
291         shuf_msk = _mm_set_epi8(
292                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
293                 3, 2,        /* octet 2~3, low 16 bits vlan_macip */
294                 15, 14,      /* octet 15~14, 16 bits data_len */
295                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
296                 15, 14,      /* octet 15~14, low 16 bits pkt_len */
297                 0xFF, 0xFF,  /* pkt_type set as unknown */
298                 0xFF, 0xFF  /*pkt_type set as unknown */
299                 );
300
301         /* Cache is empty -> need to scan the buffer rings, but first move
302          * the next 'n' mbufs into the cache
303          */
304         sw_ring = &rxq->sw_ring[rxq->rx_tail];
305
306         /* A. load 4 packet in one loop
307          * [A*. mask out 4 unused dirty field in desc]
308          * B. copy 4 mbuf point from swring to rx_pkts
309          * C. calc the number of DD bits among the 4 packets
310          * [C*. extract the end-of-packet bit, if requested]
311          * D. fill info. from desc to mbuf
312          */
313
314         for (pos = 0, nb_pkts_recd = 0; pos < nb_pkts;
315                         pos += RTE_I40E_DESCS_PER_LOOP,
316                         rxdp += RTE_I40E_DESCS_PER_LOOP) {
317                 __m128i descs[RTE_I40E_DESCS_PER_LOOP];
318                 __m128i pkt_mb1, pkt_mb2, pkt_mb3, pkt_mb4;
319                 __m128i zero, staterr, sterr_tmp1, sterr_tmp2;
320                 __m128i mbp1, mbp2; /* two mbuf pointer in one XMM reg. */
321
322                 /* B.1 load 1 mbuf point */
323                 mbp1 = _mm_loadu_si128((__m128i *)&sw_ring[pos]);
324                 /* Read desc statuses backwards to avoid race condition */
325                 /* A.1 load 4 pkts desc */
326                 descs[3] = _mm_loadu_si128((__m128i *)(rxdp + 3));
327                 rte_compiler_barrier();
328
329                 /* B.2 copy 2 mbuf point into rx_pkts  */
330                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
331
332                 /* B.1 load 1 mbuf point */
333                 mbp2 = _mm_loadu_si128((__m128i *)&sw_ring[pos+2]);
334
335                 descs[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
336                 rte_compiler_barrier();
337                 /* B.1 load 2 mbuf point */
338                 descs[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
339                 rte_compiler_barrier();
340                 descs[0] = _mm_loadu_si128((__m128i *)(rxdp));
341
342                 /* B.2 copy 2 mbuf point into rx_pkts  */
343                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
344
345                 if (split_packet) {
346                         rte_mbuf_prefetch_part2(rx_pkts[pos]);
347                         rte_mbuf_prefetch_part2(rx_pkts[pos + 1]);
348                         rte_mbuf_prefetch_part2(rx_pkts[pos + 2]);
349                         rte_mbuf_prefetch_part2(rx_pkts[pos + 3]);
350                 }
351
352                 /* avoid compiler reorder optimization */
353                 rte_compiler_barrier();
354
355                 /* pkt 3,4 shift the pktlen field to be 16-bit aligned*/
356                 const __m128i len3 = _mm_slli_epi32(descs[3], PKTLEN_SHIFT);
357                 const __m128i len2 = _mm_slli_epi32(descs[2], PKTLEN_SHIFT);
358
359                 /* merge the now-aligned packet length fields back in */
360                 descs[3] = _mm_blend_epi16(descs[3], len3, 0x80);
361                 descs[2] = _mm_blend_epi16(descs[2], len2, 0x80);
362
363                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
364                 pkt_mb4 = _mm_shuffle_epi8(descs[3], shuf_msk);
365                 pkt_mb3 = _mm_shuffle_epi8(descs[2], shuf_msk);
366
367                 /* C.1 4=>2 filter staterr info only */
368                 sterr_tmp2 = _mm_unpackhi_epi32(descs[3], descs[2]);
369                 /* C.1 4=>2 filter staterr info only */
370                 sterr_tmp1 = _mm_unpackhi_epi32(descs[1], descs[0]);
371
372                 desc_to_olflags_v(descs, &rx_pkts[pos]);
373
374                 /* D.2 pkt 3,4 set in_port/nb_seg and remove crc */
375                 pkt_mb4 = _mm_add_epi16(pkt_mb4, crc_adjust);
376                 pkt_mb3 = _mm_add_epi16(pkt_mb3, crc_adjust);
377
378                 /* pkt 1,2 shift the pktlen field to be 16-bit aligned*/
379                 const __m128i len1 = _mm_slli_epi32(descs[1], PKTLEN_SHIFT);
380                 const __m128i len0 = _mm_slli_epi32(descs[0], PKTLEN_SHIFT);
381
382                 /* merge the now-aligned packet length fields back in */
383                 descs[1] = _mm_blend_epi16(descs[1], len1, 0x80);
384                 descs[0] = _mm_blend_epi16(descs[0], len0, 0x80);
385
386                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
387                 pkt_mb2 = _mm_shuffle_epi8(descs[1], shuf_msk);
388                 pkt_mb1 = _mm_shuffle_epi8(descs[0], shuf_msk);
389
390                 /* C.2 get 4 pkts staterr value  */
391                 zero = _mm_xor_si128(dd_check, dd_check);
392                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
393
394                 /* D.3 copy final 3,4 data to rx_pkts */
395                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
396                                  pkt_mb4);
397                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
398                                  pkt_mb3);
399
400                 /* D.2 pkt 1,2 set in_port/nb_seg and remove crc */
401                 pkt_mb2 = _mm_add_epi16(pkt_mb2, crc_adjust);
402                 pkt_mb1 = _mm_add_epi16(pkt_mb1, crc_adjust);
403
404                 /* C* extract and record EOP bit */
405                 if (split_packet) {
406                         __m128i eop_shuf_mask = _mm_set_epi8(
407                                         0xFF, 0xFF, 0xFF, 0xFF,
408                                         0xFF, 0xFF, 0xFF, 0xFF,
409                                         0xFF, 0xFF, 0xFF, 0xFF,
410                                         0x04, 0x0C, 0x00, 0x08
411                                         );
412
413                         /* and with mask to extract bits, flipping 1-0 */
414                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
415                         /* the staterr values are not in order, as the count
416                          * count of dd bits doesn't care. However, for end of
417                          * packet tracking, we do care, so shuffle. This also
418                          * compresses the 32-bit values to 8-bit
419                          */
420                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
421                         /* store the resulting 32-bit value */
422                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
423                         split_packet += RTE_I40E_DESCS_PER_LOOP;
424                 }
425
426                 /* C.3 calc available number of desc */
427                 staterr = _mm_and_si128(staterr, dd_check);
428                 staterr = _mm_packs_epi32(staterr, zero);
429
430                 /* D.3 copy final 1,2 data to rx_pkts */
431                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
432                                  pkt_mb2);
433                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
434                                  pkt_mb1);
435                 desc_to_ptype_v(descs, &rx_pkts[pos]);
436                 /* C.4 calc avaialbe number of desc */
437                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
438                 nb_pkts_recd += var;
439                 if (likely(var != RTE_I40E_DESCS_PER_LOOP))
440                         break;
441         }
442
443         /* Update our internal tail pointer */
444         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_pkts_recd);
445         rxq->rx_tail = (uint16_t)(rxq->rx_tail & (rxq->nb_rx_desc - 1));
446         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
447
448         return nb_pkts_recd;
449 }
450
451  /*
452  * Notice:
453  * - nb_pkts < RTE_I40E_DESCS_PER_LOOP, just return no packet
454  * - nb_pkts > RTE_I40E_VPMD_RX_BURST, only scan RTE_I40E_VPMD_RX_BURST
455  *   numbers of DD bits
456  */
457 uint16_t
458 i40e_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
459                    uint16_t nb_pkts)
460 {
461         return _recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
462 }
463
464  /* vPMD receive routine that reassembles scattered packets
465  * Notice:
466  * - nb_pkts < RTE_I40E_DESCS_PER_LOOP, just return no packet
467  * - nb_pkts > RTE_I40E_VPMD_RX_BURST, only scan RTE_I40E_VPMD_RX_BURST
468  *   numbers of DD bits
469  */
470 uint16_t
471 i40e_recv_scattered_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
472                              uint16_t nb_pkts)
473 {
474
475         struct i40e_rx_queue *rxq = rx_queue;
476         uint8_t split_flags[RTE_I40E_VPMD_RX_BURST] = {0};
477
478         /* get some new buffers */
479         uint16_t nb_bufs = _recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
480                         split_flags);
481         if (nb_bufs == 0)
482                 return 0;
483
484         /* happy day case, full burst + no packets to be joined */
485         const uint64_t *split_fl64 = (uint64_t *)split_flags;
486
487         if (rxq->pkt_first_seg == NULL &&
488                         split_fl64[0] == 0 && split_fl64[1] == 0 &&
489                         split_fl64[2] == 0 && split_fl64[3] == 0)
490                 return nb_bufs;
491
492         /* reassemble any packets that need reassembly*/
493         unsigned i = 0;
494
495         if (rxq->pkt_first_seg == NULL) {
496                 /* find the first split flag, and only reassemble then*/
497                 while (i < nb_bufs && !split_flags[i])
498                         i++;
499                 if (i == nb_bufs)
500                         return nb_bufs;
501         }
502         return i + reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
503                 &split_flags[i]);
504 }
505
506 static inline void
507 vtx1(volatile struct i40e_tx_desc *txdp,
508                 struct rte_mbuf *pkt, uint64_t flags)
509 {
510         uint64_t high_qw = (I40E_TX_DESC_DTYPE_DATA |
511                         ((uint64_t)flags  << I40E_TXD_QW1_CMD_SHIFT) |
512                         ((uint64_t)pkt->data_len << I40E_TXD_QW1_TX_BUF_SZ_SHIFT));
513
514         __m128i descriptor = _mm_set_epi64x(high_qw,
515                                 pkt->buf_physaddr + pkt->data_off);
516         _mm_store_si128((__m128i *)txdp, descriptor);
517 }
518
519 static inline void
520 vtx(volatile struct i40e_tx_desc *txdp,
521                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
522 {
523         int i;
524
525         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
526                 vtx1(txdp, *pkt, flags);
527 }
528
529 uint16_t
530 i40e_xmit_fixed_burst_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
531                           uint16_t nb_pkts)
532 {
533         struct i40e_tx_queue *txq = (struct i40e_tx_queue *)tx_queue;
534         volatile struct i40e_tx_desc *txdp;
535         struct i40e_tx_entry *txep;
536         uint16_t n, nb_commit, tx_id;
537         uint64_t flags = I40E_TD_CMD;
538         uint64_t rs = I40E_TX_DESC_CMD_RS | I40E_TD_CMD;
539         int i;
540
541         /* cross rx_thresh boundary is not allowed */
542         nb_pkts = RTE_MIN(nb_pkts, txq->tx_rs_thresh);
543
544         if (txq->nb_tx_free < txq->tx_free_thresh)
545                 i40e_tx_free_bufs(txq);
546
547         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
548         if (unlikely(nb_pkts == 0))
549                 return 0;
550
551         tx_id = txq->tx_tail;
552         txdp = &txq->tx_ring[tx_id];
553         txep = &txq->sw_ring[tx_id];
554
555         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
556
557         n = (uint16_t)(txq->nb_tx_desc - tx_id);
558         if (nb_commit >= n) {
559                 tx_backlog_entry(txep, tx_pkts, n);
560
561                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
562                         vtx1(txdp, *tx_pkts, flags);
563
564                 vtx1(txdp, *tx_pkts++, rs);
565
566                 nb_commit = (uint16_t)(nb_commit - n);
567
568                 tx_id = 0;
569                 txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
570
571                 /* avoid reach the end of ring */
572                 txdp = &txq->tx_ring[tx_id];
573                 txep = &txq->sw_ring[tx_id];
574         }
575
576         tx_backlog_entry(txep, tx_pkts, nb_commit);
577
578         vtx(txdp, tx_pkts, nb_commit, flags);
579
580         tx_id = (uint16_t)(tx_id + nb_commit);
581         if (tx_id > txq->tx_next_rs) {
582                 txq->tx_ring[txq->tx_next_rs].cmd_type_offset_bsz |=
583                         rte_cpu_to_le_64(((uint64_t)I40E_TX_DESC_CMD_RS) <<
584                                                 I40E_TXD_QW1_CMD_SHIFT);
585                 txq->tx_next_rs =
586                         (uint16_t)(txq->tx_next_rs + txq->tx_rs_thresh);
587         }
588
589         txq->tx_tail = tx_id;
590
591         I40E_PCI_REG_WRITE(txq->qtx_tail, txq->tx_tail);
592
593         return nb_pkts;
594 }
595
596 void __attribute__((cold))
597 i40e_rx_queue_release_mbufs_vec(struct i40e_rx_queue *rxq)
598 {
599         _i40e_rx_queue_release_mbufs_vec(rxq);
600 }
601
602 int __attribute__((cold))
603 i40e_rxq_vec_setup(struct i40e_rx_queue *rxq)
604 {
605         return i40e_rxq_vec_setup_default(rxq);
606 }
607
608 int __attribute__((cold))
609 i40e_txq_vec_setup(struct i40e_tx_queue __rte_unused *txq)
610 {
611         return 0;
612 }
613
614 int __attribute__((cold))
615 i40e_rx_vec_dev_conf_condition_check(struct rte_eth_dev *dev)
616 {
617 #ifndef RTE_LIBRTE_IEEE1588
618         /* need SSE4.1 support */
619         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
620                 return -1;
621 #endif
622
623         return i40e_rx_vec_dev_conf_condition_check_default(dev);
624 }