3b4a352eb4cc70a4c81e37698e49a517b46b53b3
[deb_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
86                 mb0 = rxep[0].mbuf;
87                 mb1 = rxep[1].mbuf;
88
89                 /* load buf_addr(lo 64bit) and buf_physaddr(hi 64bit) */
90                 vaddr0 = _mm_loadu_si128((__m128i *)&mb0->buf_addr);
91                 vaddr1 = _mm_loadu_si128((__m128i *)&mb1->buf_addr);
92
93                 /* convert pa to dma_addr hdr/data */
94                 dma_addr0 = _mm_unpackhi_epi64(vaddr0, vaddr0);
95                 dma_addr1 = _mm_unpackhi_epi64(vaddr1, vaddr1);
96
97                 /* add headroom to pa values */
98                 dma_addr0 = _mm_add_epi64(dma_addr0, hdr_room);
99                 dma_addr1 = _mm_add_epi64(dma_addr1, hdr_room);
100
101                 /* flush desc with pa dma_addr */
102                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr0);
103                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr1);
104         }
105
106         rxq->rxrearm_start += RTE_I40E_RXQ_REARM_THRESH;
107         if (rxq->rxrearm_start >= rxq->nb_rx_desc)
108                 rxq->rxrearm_start = 0;
109
110         rxq->rxrearm_nb -= RTE_I40E_RXQ_REARM_THRESH;
111
112         rx_id = (uint16_t)((rxq->rxrearm_start == 0) ?
113                              (rxq->nb_rx_desc - 1) : (rxq->rxrearm_start - 1));
114
115         /* Update the tail pointer on the NIC */
116         I40E_PCI_REG_WRITE(rxq->qrx_tail, rx_id);
117 }
118
119 static inline void
120 desc_to_olflags_v(struct i40e_rx_queue *rxq, __m128i descs[4] __rte_unused,
121         struct rte_mbuf **rx_pkts)
122 {
123         const __m128i mbuf_init = _mm_set_epi64x(0, rxq->mbuf_initializer);
124         __m128i rearm0, rearm1, rearm2, rearm3;
125
126         __m128i vlan0, vlan1, rss, l3_l4e;
127
128         /* mask everything except RSS, flow director and VLAN flags
129          * bit2 is for VLAN tag, bit11 for flow director indication
130          * bit13:12 for RSS indication.
131          */
132         const __m128i rss_vlan_msk = _mm_set_epi32(
133                         0x1c03804, 0x1c03804, 0x1c03804, 0x1c03804);
134
135         const __m128i cksum_mask = _mm_set_epi32(
136                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
137                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
138                         PKT_RX_EIP_CKSUM_BAD,
139                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
140                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
141                         PKT_RX_EIP_CKSUM_BAD,
142                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
143                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
144                         PKT_RX_EIP_CKSUM_BAD,
145                         PKT_RX_IP_CKSUM_GOOD | PKT_RX_IP_CKSUM_BAD |
146                         PKT_RX_L4_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD |
147                         PKT_RX_EIP_CKSUM_BAD);
148
149         /* map rss and vlan type to rss hash and vlan flag */
150         const __m128i vlan_flags = _mm_set_epi8(0, 0, 0, 0,
151                         0, 0, 0, 0,
152                         0, 0, 0, PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED,
153                         0, 0, 0, 0);
154
155         const __m128i rss_flags = _mm_set_epi8(0, 0, 0, 0,
156                         0, 0, 0, 0,
157                         PKT_RX_RSS_HASH | PKT_RX_FDIR, PKT_RX_RSS_HASH, 0, 0,
158                         0, 0, PKT_RX_FDIR, 0);
159
160         const __m128i l3_l4e_flags = _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,
161                         /* shift right 1 bit to make sure it not exceed 255 */
162                         (PKT_RX_EIP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD |
163                          PKT_RX_IP_CKSUM_BAD) >> 1,
164                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_EIP_CKSUM_BAD |
165                          PKT_RX_L4_CKSUM_BAD) >> 1,
166                         (PKT_RX_EIP_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD) >> 1,
167                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_EIP_CKSUM_BAD) >> 1,
168                         (PKT_RX_L4_CKSUM_BAD | PKT_RX_IP_CKSUM_BAD) >> 1,
169                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD) >> 1,
170                         PKT_RX_IP_CKSUM_BAD >> 1,
171                         (PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD) >> 1);
172
173         vlan0 = _mm_unpackhi_epi32(descs[0], descs[1]);
174         vlan1 = _mm_unpackhi_epi32(descs[2], descs[3]);
175         vlan0 = _mm_unpacklo_epi64(vlan0, vlan1);
176
177         vlan1 = _mm_and_si128(vlan0, rss_vlan_msk);
178         vlan0 = _mm_shuffle_epi8(vlan_flags, vlan1);
179
180         rss = _mm_srli_epi32(vlan1, 11);
181         rss = _mm_shuffle_epi8(rss_flags, rss);
182
183         l3_l4e = _mm_srli_epi32(vlan1, 22);
184         l3_l4e = _mm_shuffle_epi8(l3_l4e_flags, l3_l4e);
185         /* then we shift left 1 bit */
186         l3_l4e = _mm_slli_epi32(l3_l4e, 1);
187         /* we need to mask out the reduntant bits */
188         l3_l4e = _mm_and_si128(l3_l4e, cksum_mask);
189
190         vlan0 = _mm_or_si128(vlan0, rss);
191         vlan0 = _mm_or_si128(vlan0, l3_l4e);
192
193         /*
194          * At this point, we have the 4 sets of flags in the low 16-bits
195          * of each 32-bit value in vlan0.
196          * We want to extract these, and merge them with the mbuf init data
197          * so we can do a single 16-byte write to the mbuf to set the flags
198          * and all the other initialization fields. Extracting the
199          * appropriate flags means that we have to do a shift and blend for
200          * each mbuf before we do the write.
201          */
202         rearm0 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(vlan0, 8), 0x10);
203         rearm1 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(vlan0, 4), 0x10);
204         rearm2 = _mm_blend_epi16(mbuf_init, vlan0, 0x10);
205         rearm3 = _mm_blend_epi16(mbuf_init, _mm_srli_si128(vlan0, 4), 0x10);
206         _mm_store_si128((__m128i *)&rx_pkts[0]->rearm_data, rearm0);
207         _mm_store_si128((__m128i *)&rx_pkts[1]->rearm_data, rearm1);
208         _mm_store_si128((__m128i *)&rx_pkts[2]->rearm_data, rearm2);
209         _mm_store_si128((__m128i *)&rx_pkts[3]->rearm_data, rearm3);
210 }
211
212 #define PKTLEN_SHIFT     10
213
214 static inline void
215 desc_to_ptype_v(__m128i descs[4], struct rte_mbuf **rx_pkts,
216                 uint32_t *ptype_tbl)
217 {
218         __m128i ptype0 = _mm_unpackhi_epi64(descs[0], descs[1]);
219         __m128i ptype1 = _mm_unpackhi_epi64(descs[2], descs[3]);
220
221         ptype0 = _mm_srli_epi64(ptype0, 30);
222         ptype1 = _mm_srli_epi64(ptype1, 30);
223
224         rx_pkts[0]->packet_type = ptype_tbl[_mm_extract_epi8(ptype0, 0)];
225         rx_pkts[1]->packet_type = ptype_tbl[_mm_extract_epi8(ptype0, 8)];
226         rx_pkts[2]->packet_type = ptype_tbl[_mm_extract_epi8(ptype1, 0)];
227         rx_pkts[3]->packet_type = ptype_tbl[_mm_extract_epi8(ptype1, 8)];
228 }
229
230  /*
231  * Notice:
232  * - nb_pkts < RTE_I40E_DESCS_PER_LOOP, just return no packet
233  * - nb_pkts > RTE_I40E_VPMD_RX_BURST, only scan RTE_I40E_VPMD_RX_BURST
234  *   numbers of DD bits
235  */
236 static inline uint16_t
237 _recv_raw_pkts_vec(struct i40e_rx_queue *rxq, struct rte_mbuf **rx_pkts,
238                    uint16_t nb_pkts, uint8_t *split_packet)
239 {
240         volatile union i40e_rx_desc *rxdp;
241         struct i40e_rx_entry *sw_ring;
242         uint16_t nb_pkts_recd;
243         int pos;
244         uint64_t var;
245         __m128i shuf_msk;
246         uint32_t *ptype_tbl = rxq->vsi->adapter->ptype_tbl;
247
248         __m128i crc_adjust = _mm_set_epi16(
249                                 0, 0, 0,    /* ignore non-length fields */
250                                 -rxq->crc_len, /* sub crc on data_len */
251                                 0,          /* ignore high-16bits of pkt_len */
252                                 -rxq->crc_len, /* sub crc on pkt_len */
253                                 0, 0            /* ignore pkt_type field */
254                         );
255         __m128i dd_check, eop_check;
256
257         /* nb_pkts shall be less equal than RTE_I40E_MAX_RX_BURST */
258         nb_pkts = RTE_MIN(nb_pkts, RTE_I40E_MAX_RX_BURST);
259
260         /* nb_pkts has to be floor-aligned to RTE_I40E_DESCS_PER_LOOP */
261         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_I40E_DESCS_PER_LOOP);
262
263         /* Just the act of getting into the function from the application is
264          * going to cost about 7 cycles
265          */
266         rxdp = rxq->rx_ring + rxq->rx_tail;
267
268         rte_prefetch0(rxdp);
269
270         /* See if we need to rearm the RX queue - gives the prefetch a bit
271          * of time to act
272          */
273         if (rxq->rxrearm_nb > RTE_I40E_RXQ_REARM_THRESH)
274                 i40e_rxq_rearm(rxq);
275
276         /* Before we start moving massive data around, check to see if
277          * there is actually a packet available
278          */
279         if (!(rxdp->wb.qword1.status_error_len &
280                         rte_cpu_to_le_32(1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
281                 return 0;
282
283         /* 4 packets DD mask */
284         dd_check = _mm_set_epi64x(0x0000000100000001LL, 0x0000000100000001LL);
285
286         /* 4 packets EOP mask */
287         eop_check = _mm_set_epi64x(0x0000000200000002LL, 0x0000000200000002LL);
288
289         /* mask to shuffle from desc. to mbuf */
290         shuf_msk = _mm_set_epi8(
291                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
292                 3, 2,        /* octet 2~3, low 16 bits vlan_macip */
293                 15, 14,      /* octet 15~14, 16 bits data_len */
294                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
295                 15, 14,      /* octet 15~14, low 16 bits pkt_len */
296                 0xFF, 0xFF,  /* pkt_type set as unknown */
297                 0xFF, 0xFF  /*pkt_type set as unknown */
298                 );
299
300         /* Cache is empty -> need to scan the buffer rings, but first move
301          * the next 'n' mbufs into the cache
302          */
303         sw_ring = &rxq->sw_ring[rxq->rx_tail];
304
305         /* A. load 4 packet in one loop
306          * [A*. mask out 4 unused dirty field in desc]
307          * B. copy 4 mbuf point from swring to rx_pkts
308          * C. calc the number of DD bits among the 4 packets
309          * [C*. extract the end-of-packet bit, if requested]
310          * D. fill info. from desc to mbuf
311          */
312
313         for (pos = 0, nb_pkts_recd = 0; pos < nb_pkts;
314                         pos += RTE_I40E_DESCS_PER_LOOP,
315                         rxdp += RTE_I40E_DESCS_PER_LOOP) {
316                 __m128i descs[RTE_I40E_DESCS_PER_LOOP];
317                 __m128i pkt_mb1, pkt_mb2, pkt_mb3, pkt_mb4;
318                 __m128i zero, staterr, sterr_tmp1, sterr_tmp2;
319                 /* 2 64 bit or 4 32 bit mbuf pointers in one XMM reg. */
320                 __m128i mbp1;
321 #if defined(RTE_ARCH_X86_64)
322                 __m128i mbp2;
323 #endif
324
325                 /* B.1 load 2 (64 bit) or 4 (32 bit) mbuf points */
326                 mbp1 = _mm_loadu_si128((__m128i *)&sw_ring[pos]);
327                 /* Read desc statuses backwards to avoid race condition */
328                 /* A.1 load 4 pkts desc */
329                 descs[3] = _mm_loadu_si128((__m128i *)(rxdp + 3));
330                 rte_compiler_barrier();
331
332                 /* B.2 copy 2 64 bit or 4 32 bit mbuf point into rx_pkts */
333                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
334
335 #if defined(RTE_ARCH_X86_64)
336                 /* B.1 load 2 64 bit mbuf points */
337                 mbp2 = _mm_loadu_si128((__m128i *)&sw_ring[pos+2]);
338 #endif
339
340                 descs[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
341                 rte_compiler_barrier();
342                 /* B.1 load 2 mbuf point */
343                 descs[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
344                 rte_compiler_barrier();
345                 descs[0] = _mm_loadu_si128((__m128i *)(rxdp));
346
347 #if defined(RTE_ARCH_X86_64)
348                 /* B.2 copy 2 mbuf point into rx_pkts  */
349                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
350 #endif
351
352                 if (split_packet) {
353                         rte_mbuf_prefetch_part2(rx_pkts[pos]);
354                         rte_mbuf_prefetch_part2(rx_pkts[pos + 1]);
355                         rte_mbuf_prefetch_part2(rx_pkts[pos + 2]);
356                         rte_mbuf_prefetch_part2(rx_pkts[pos + 3]);
357                 }
358
359                 /* avoid compiler reorder optimization */
360                 rte_compiler_barrier();
361
362                 /* pkt 3,4 shift the pktlen field to be 16-bit aligned*/
363                 const __m128i len3 = _mm_slli_epi32(descs[3], PKTLEN_SHIFT);
364                 const __m128i len2 = _mm_slli_epi32(descs[2], PKTLEN_SHIFT);
365
366                 /* merge the now-aligned packet length fields back in */
367                 descs[3] = _mm_blend_epi16(descs[3], len3, 0x80);
368                 descs[2] = _mm_blend_epi16(descs[2], len2, 0x80);
369
370                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
371                 pkt_mb4 = _mm_shuffle_epi8(descs[3], shuf_msk);
372                 pkt_mb3 = _mm_shuffle_epi8(descs[2], shuf_msk);
373
374                 /* C.1 4=>2 filter staterr info only */
375                 sterr_tmp2 = _mm_unpackhi_epi32(descs[3], descs[2]);
376                 /* C.1 4=>2 filter staterr info only */
377                 sterr_tmp1 = _mm_unpackhi_epi32(descs[1], descs[0]);
378
379                 desc_to_olflags_v(rxq, descs, &rx_pkts[pos]);
380
381                 /* D.2 pkt 3,4 set in_port/nb_seg and remove crc */
382                 pkt_mb4 = _mm_add_epi16(pkt_mb4, crc_adjust);
383                 pkt_mb3 = _mm_add_epi16(pkt_mb3, crc_adjust);
384
385                 /* pkt 1,2 shift the pktlen field to be 16-bit aligned*/
386                 const __m128i len1 = _mm_slli_epi32(descs[1], PKTLEN_SHIFT);
387                 const __m128i len0 = _mm_slli_epi32(descs[0], PKTLEN_SHIFT);
388
389                 /* merge the now-aligned packet length fields back in */
390                 descs[1] = _mm_blend_epi16(descs[1], len1, 0x80);
391                 descs[0] = _mm_blend_epi16(descs[0], len0, 0x80);
392
393                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
394                 pkt_mb2 = _mm_shuffle_epi8(descs[1], shuf_msk);
395                 pkt_mb1 = _mm_shuffle_epi8(descs[0], shuf_msk);
396
397                 /* C.2 get 4 pkts staterr value  */
398                 zero = _mm_xor_si128(dd_check, dd_check);
399                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
400
401                 /* D.3 copy final 3,4 data to rx_pkts */
402                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
403                                  pkt_mb4);
404                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
405                                  pkt_mb3);
406
407                 /* D.2 pkt 1,2 set in_port/nb_seg and remove crc */
408                 pkt_mb2 = _mm_add_epi16(pkt_mb2, crc_adjust);
409                 pkt_mb1 = _mm_add_epi16(pkt_mb1, crc_adjust);
410
411                 /* C* extract and record EOP bit */
412                 if (split_packet) {
413                         __m128i eop_shuf_mask = _mm_set_epi8(
414                                         0xFF, 0xFF, 0xFF, 0xFF,
415                                         0xFF, 0xFF, 0xFF, 0xFF,
416                                         0xFF, 0xFF, 0xFF, 0xFF,
417                                         0x04, 0x0C, 0x00, 0x08
418                                         );
419
420                         /* and with mask to extract bits, flipping 1-0 */
421                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
422                         /* the staterr values are not in order, as the count
423                          * count of dd bits doesn't care. However, for end of
424                          * packet tracking, we do care, so shuffle. This also
425                          * compresses the 32-bit values to 8-bit
426                          */
427                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
428                         /* store the resulting 32-bit value */
429                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
430                         split_packet += RTE_I40E_DESCS_PER_LOOP;
431                 }
432
433                 /* C.3 calc available number of desc */
434                 staterr = _mm_and_si128(staterr, dd_check);
435                 staterr = _mm_packs_epi32(staterr, zero);
436
437                 /* D.3 copy final 1,2 data to rx_pkts */
438                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
439                                  pkt_mb2);
440                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
441                                  pkt_mb1);
442                 desc_to_ptype_v(descs, &rx_pkts[pos], ptype_tbl);
443                 /* C.4 calc avaialbe number of desc */
444                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
445                 nb_pkts_recd += var;
446                 if (likely(var != RTE_I40E_DESCS_PER_LOOP))
447                         break;
448         }
449
450         /* Update our internal tail pointer */
451         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_pkts_recd);
452         rxq->rx_tail = (uint16_t)(rxq->rx_tail & (rxq->nb_rx_desc - 1));
453         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
454
455         return nb_pkts_recd;
456 }
457
458  /*
459  * Notice:
460  * - nb_pkts < RTE_I40E_DESCS_PER_LOOP, just return no packet
461  * - nb_pkts > RTE_I40E_VPMD_RX_BURST, only scan RTE_I40E_VPMD_RX_BURST
462  *   numbers of DD bits
463  */
464 uint16_t
465 i40e_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
466                    uint16_t nb_pkts)
467 {
468         return _recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
469 }
470
471  /* vPMD receive routine that reassembles scattered packets
472  * Notice:
473  * - nb_pkts < RTE_I40E_DESCS_PER_LOOP, just return no packet
474  * - nb_pkts > RTE_I40E_VPMD_RX_BURST, only scan RTE_I40E_VPMD_RX_BURST
475  *   numbers of DD bits
476  */
477 uint16_t
478 i40e_recv_scattered_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
479                              uint16_t nb_pkts)
480 {
481
482         struct i40e_rx_queue *rxq = rx_queue;
483         uint8_t split_flags[RTE_I40E_VPMD_RX_BURST] = {0};
484
485         /* get some new buffers */
486         uint16_t nb_bufs = _recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
487                         split_flags);
488         if (nb_bufs == 0)
489                 return 0;
490
491         /* happy day case, full burst + no packets to be joined */
492         const uint64_t *split_fl64 = (uint64_t *)split_flags;
493
494         if (rxq->pkt_first_seg == NULL &&
495                         split_fl64[0] == 0 && split_fl64[1] == 0 &&
496                         split_fl64[2] == 0 && split_fl64[3] == 0)
497                 return nb_bufs;
498
499         /* reassemble any packets that need reassembly*/
500         unsigned i = 0;
501
502         if (rxq->pkt_first_seg == NULL) {
503                 /* find the first split flag, and only reassemble then*/
504                 while (i < nb_bufs && !split_flags[i])
505                         i++;
506                 if (i == nb_bufs)
507                         return nb_bufs;
508         }
509         return i + reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
510                 &split_flags[i]);
511 }
512
513 static inline void
514 vtx1(volatile struct i40e_tx_desc *txdp,
515                 struct rte_mbuf *pkt, uint64_t flags)
516 {
517         uint64_t high_qw = (I40E_TX_DESC_DTYPE_DATA |
518                         ((uint64_t)flags  << I40E_TXD_QW1_CMD_SHIFT) |
519                         ((uint64_t)pkt->data_len << I40E_TXD_QW1_TX_BUF_SZ_SHIFT));
520
521         __m128i descriptor = _mm_set_epi64x(high_qw,
522                                 pkt->buf_physaddr + pkt->data_off);
523         _mm_store_si128((__m128i *)txdp, descriptor);
524 }
525
526 static inline void
527 vtx(volatile struct i40e_tx_desc *txdp,
528                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
529 {
530         int i;
531
532         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
533                 vtx1(txdp, *pkt, flags);
534 }
535
536 uint16_t
537 i40e_xmit_fixed_burst_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
538                           uint16_t nb_pkts)
539 {
540         struct i40e_tx_queue *txq = (struct i40e_tx_queue *)tx_queue;
541         volatile struct i40e_tx_desc *txdp;
542         struct i40e_tx_entry *txep;
543         uint16_t n, nb_commit, tx_id;
544         uint64_t flags = I40E_TD_CMD;
545         uint64_t rs = I40E_TX_DESC_CMD_RS | I40E_TD_CMD;
546         int i;
547
548         /* cross rx_thresh boundary is not allowed */
549         nb_pkts = RTE_MIN(nb_pkts, txq->tx_rs_thresh);
550
551         if (txq->nb_tx_free < txq->tx_free_thresh)
552                 i40e_tx_free_bufs(txq);
553
554         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
555         if (unlikely(nb_pkts == 0))
556                 return 0;
557
558         tx_id = txq->tx_tail;
559         txdp = &txq->tx_ring[tx_id];
560         txep = &txq->sw_ring[tx_id];
561
562         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
563
564         n = (uint16_t)(txq->nb_tx_desc - tx_id);
565         if (nb_commit >= n) {
566                 tx_backlog_entry(txep, tx_pkts, n);
567
568                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
569                         vtx1(txdp, *tx_pkts, flags);
570
571                 vtx1(txdp, *tx_pkts++, rs);
572
573                 nb_commit = (uint16_t)(nb_commit - n);
574
575                 tx_id = 0;
576                 txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
577
578                 /* avoid reach the end of ring */
579                 txdp = &txq->tx_ring[tx_id];
580                 txep = &txq->sw_ring[tx_id];
581         }
582
583         tx_backlog_entry(txep, tx_pkts, nb_commit);
584
585         vtx(txdp, tx_pkts, nb_commit, flags);
586
587         tx_id = (uint16_t)(tx_id + nb_commit);
588         if (tx_id > txq->tx_next_rs) {
589                 txq->tx_ring[txq->tx_next_rs].cmd_type_offset_bsz |=
590                         rte_cpu_to_le_64(((uint64_t)I40E_TX_DESC_CMD_RS) <<
591                                                 I40E_TXD_QW1_CMD_SHIFT);
592                 txq->tx_next_rs =
593                         (uint16_t)(txq->tx_next_rs + txq->tx_rs_thresh);
594         }
595
596         txq->tx_tail = tx_id;
597
598         I40E_PCI_REG_WRITE(txq->qtx_tail, txq->tx_tail);
599
600         return nb_pkts;
601 }
602
603 void __attribute__((cold))
604 i40e_rx_queue_release_mbufs_vec(struct i40e_rx_queue *rxq)
605 {
606         _i40e_rx_queue_release_mbufs_vec(rxq);
607 }
608
609 int __attribute__((cold))
610 i40e_rxq_vec_setup(struct i40e_rx_queue *rxq)
611 {
612         return i40e_rxq_vec_setup_default(rxq);
613 }
614
615 int __attribute__((cold))
616 i40e_txq_vec_setup(struct i40e_tx_queue __rte_unused *txq)
617 {
618         return 0;
619 }
620
621 int __attribute__((cold))
622 i40e_rx_vec_dev_conf_condition_check(struct rte_eth_dev *dev)
623 {
624 #ifndef RTE_LIBRTE_IEEE1588
625         /* need SSE4.1 support */
626         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
627                 return -1;
628 #endif
629
630         return i40e_rx_vec_dev_conf_condition_check_default(dev);
631 }