Imported Upstream version 16.07-rc1
[deb_dpdk.git] / drivers / net / fm10k / fm10k_rxtx_vec.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2013-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 <inttypes.h>
35
36 #include <rte_ethdev.h>
37 #include <rte_common.h>
38 #include "fm10k.h"
39 #include "base/fm10k_type.h"
40
41 #include <tmmintrin.h>
42
43 #ifndef __INTEL_COMPILER
44 #pragma GCC diagnostic ignored "-Wcast-qual"
45 #endif
46
47 static void
48 fm10k_reset_tx_queue(struct fm10k_tx_queue *txq);
49
50 /* Handling the offload flags (olflags) field takes computation
51  * time when receiving packets. Therefore we provide a flag to disable
52  * the processing of the olflags field when they are not needed. This
53  * gives improved performance, at the cost of losing the offload info
54  * in the received packet
55  */
56 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
57
58 /* Vlan present flag shift */
59 #define VP_SHIFT     (2)
60 /* L3 type shift */
61 #define L3TYPE_SHIFT     (4)
62 /* L4 type shift */
63 #define L4TYPE_SHIFT     (7)
64 /* HBO flag shift */
65 #define HBOFLAG_SHIFT     (10)
66 /* RXE flag shift */
67 #define RXEFLAG_SHIFT     (13)
68 /* IPE/L4E flag shift */
69 #define L3L4EFLAG_SHIFT     (14)
70
71 static inline void
72 fm10k_desc_to_olflags_v(__m128i descs[4], struct rte_mbuf **rx_pkts)
73 {
74         __m128i ptype0, ptype1, vtag0, vtag1, eflag0, eflag1, cksumflag;
75         union {
76                 uint16_t e[4];
77                 uint64_t dword;
78         } vol;
79
80         const __m128i pkttype_msk = _mm_set_epi16(
81                         0x0000, 0x0000, 0x0000, 0x0000,
82                         PKT_RX_VLAN_PKT, PKT_RX_VLAN_PKT,
83                         PKT_RX_VLAN_PKT, PKT_RX_VLAN_PKT);
84
85         /* mask everything except rss type */
86         const __m128i rsstype_msk = _mm_set_epi16(
87                         0x0000, 0x0000, 0x0000, 0x0000,
88                         0x000F, 0x000F, 0x000F, 0x000F);
89
90         /* mask for HBO and RXE flag flags */
91         const __m128i rxe_msk = _mm_set_epi16(
92                         0x0000, 0x0000, 0x0000, 0x0000,
93                         0x0001, 0x0001, 0x0001, 0x0001);
94
95         const __m128i l3l4cksum_flag = _mm_set_epi8(0, 0, 0, 0,
96                         0, 0, 0, 0,
97                         0, 0, 0, 0,
98                         PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD,
99                         PKT_RX_IP_CKSUM_BAD, PKT_RX_L4_CKSUM_BAD, 0);
100
101         const __m128i rxe_flag = _mm_set_epi8(0, 0, 0, 0,
102                         0, 0, 0, 0,
103                         0, 0, 0, 0,
104                         0, 0, 0, 0);
105
106         /* map rss type to rss hash flag */
107         const __m128i rss_flags = _mm_set_epi8(0, 0, 0, 0,
108                         0, 0, 0, PKT_RX_RSS_HASH,
109                         PKT_RX_RSS_HASH, 0, PKT_RX_RSS_HASH, 0,
110                         PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, 0);
111
112         /* Calculate RSS_hash and Vlan fields */
113         ptype0 = _mm_unpacklo_epi16(descs[0], descs[1]);
114         ptype1 = _mm_unpacklo_epi16(descs[2], descs[3]);
115         vtag0 = _mm_unpackhi_epi16(descs[0], descs[1]);
116         vtag1 = _mm_unpackhi_epi16(descs[2], descs[3]);
117
118         ptype0 = _mm_unpacklo_epi32(ptype0, ptype1);
119         ptype0 = _mm_and_si128(ptype0, rsstype_msk);
120         ptype0 = _mm_shuffle_epi8(rss_flags, ptype0);
121
122         vtag1 = _mm_unpacklo_epi32(vtag0, vtag1);
123         eflag0 = vtag1;
124         cksumflag = vtag1;
125         vtag1 = _mm_srli_epi16(vtag1, VP_SHIFT);
126         vtag1 = _mm_and_si128(vtag1, pkttype_msk);
127
128         vtag1 = _mm_or_si128(ptype0, vtag1);
129
130         /* Process err flags, simply set RECIP_ERR bit if HBO/IXE is set */
131         eflag1 = _mm_srli_epi16(eflag0, RXEFLAG_SHIFT);
132         eflag0 = _mm_srli_epi16(eflag0, HBOFLAG_SHIFT);
133         eflag0 = _mm_or_si128(eflag0, eflag1);
134         eflag0 = _mm_and_si128(eflag0, rxe_msk);
135         eflag0 = _mm_shuffle_epi8(rxe_flag, eflag0);
136
137         vtag1 = _mm_or_si128(eflag0, vtag1);
138
139         /* Process L4/L3 checksum error flags */
140         cksumflag = _mm_srli_epi16(cksumflag, L3L4EFLAG_SHIFT);
141         cksumflag = _mm_shuffle_epi8(l3l4cksum_flag, cksumflag);
142         vtag1 = _mm_or_si128(cksumflag, vtag1);
143
144         vol.dword = _mm_cvtsi128_si64(vtag1);
145
146         rx_pkts[0]->ol_flags = vol.e[0];
147         rx_pkts[1]->ol_flags = vol.e[1];
148         rx_pkts[2]->ol_flags = vol.e[2];
149         rx_pkts[3]->ol_flags = vol.e[3];
150 }
151
152 /* @note: When this function is changed, make corresponding change to
153  * fm10k_dev_supported_ptypes_get().
154  */
155 static inline void
156 fm10k_desc_to_pktype_v(__m128i descs[4], struct rte_mbuf **rx_pkts)
157 {
158         __m128i l3l4type0, l3l4type1, l3type, l4type;
159         union {
160                 uint16_t e[4];
161                 uint64_t dword;
162         } vol;
163
164         /* L3 pkt type mask  Bit4 to Bit6 */
165         const __m128i l3type_msk = _mm_set_epi16(
166                         0x0000, 0x0000, 0x0000, 0x0000,
167                         0x0070, 0x0070, 0x0070, 0x0070);
168
169         /* L4 pkt type mask  Bit7 to Bit9 */
170         const __m128i l4type_msk = _mm_set_epi16(
171                         0x0000, 0x0000, 0x0000, 0x0000,
172                         0x0380, 0x0380, 0x0380, 0x0380);
173
174         /* convert RRC l3 type to mbuf format */
175         const __m128i l3type_flags = _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0,
176                         0, 0, 0, RTE_PTYPE_L3_IPV6_EXT,
177                         RTE_PTYPE_L3_IPV6, RTE_PTYPE_L3_IPV4_EXT,
178                         RTE_PTYPE_L3_IPV4, 0);
179
180         /* Convert RRC l4 type to mbuf format l4type_flags shift-left 8 bits
181          * to fill into8 bits length.
182          */
183         const __m128i l4type_flags = _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0,
184                         RTE_PTYPE_TUNNEL_GENEVE >> 8,
185                         RTE_PTYPE_TUNNEL_NVGRE >> 8,
186                         RTE_PTYPE_TUNNEL_VXLAN >> 8,
187                         RTE_PTYPE_TUNNEL_GRE >> 8,
188                         RTE_PTYPE_L4_UDP >> 8,
189                         RTE_PTYPE_L4_TCP >> 8,
190                         0);
191
192         l3l4type0 = _mm_unpacklo_epi16(descs[0], descs[1]);
193         l3l4type1 = _mm_unpacklo_epi16(descs[2], descs[3]);
194         l3l4type0 = _mm_unpacklo_epi32(l3l4type0, l3l4type1);
195
196         l3type = _mm_and_si128(l3l4type0, l3type_msk);
197         l4type = _mm_and_si128(l3l4type0, l4type_msk);
198
199         l3type = _mm_srli_epi16(l3type, L3TYPE_SHIFT);
200         l4type = _mm_srli_epi16(l4type, L4TYPE_SHIFT);
201
202         l3type = _mm_shuffle_epi8(l3type_flags, l3type);
203         /* l4type_flags shift-left for 8 bits, need shift-right back */
204         l4type = _mm_shuffle_epi8(l4type_flags, l4type);
205
206         l4type = _mm_slli_epi16(l4type, 8);
207         l3l4type0 = _mm_or_si128(l3type, l4type);
208         vol.dword = _mm_cvtsi128_si64(l3l4type0);
209
210         rx_pkts[0]->packet_type = vol.e[0];
211         rx_pkts[1]->packet_type = vol.e[1];
212         rx_pkts[2]->packet_type = vol.e[2];
213         rx_pkts[3]->packet_type = vol.e[3];
214 }
215 #else
216 #define fm10k_desc_to_olflags_v(desc, rx_pkts) do {} while (0)
217 #define fm10k_desc_to_pktype_v(desc, rx_pkts) do {} while (0)
218 #endif
219
220 int __attribute__((cold))
221 fm10k_rx_vec_condition_check(struct rte_eth_dev *dev)
222 {
223 #ifndef RTE_LIBRTE_IEEE1588
224         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
225         struct rte_fdir_conf *fconf = &dev->data->dev_conf.fdir_conf;
226
227 #ifndef RTE_FM10K_RX_OLFLAGS_ENABLE
228         /* whithout rx ol_flags, no VP flag report */
229         if (rxmode->hw_vlan_extend != 0)
230                 return -1;
231 #endif
232
233         /* no fdir support */
234         if (fconf->mode != RTE_FDIR_MODE_NONE)
235                 return -1;
236
237         /* - no csum error report support
238          * - no header split support
239          */
240         if (rxmode->hw_ip_checksum == 1 ||
241             rxmode->header_split == 1)
242                 return -1;
243
244         return 0;
245 #else
246         RTE_SET_USED(dev);
247         return -1;
248 #endif
249 }
250
251 int __attribute__((cold))
252 fm10k_rxq_vec_setup(struct fm10k_rx_queue *rxq)
253 {
254         uintptr_t p;
255         struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
256
257         mb_def.nb_segs = 1;
258         /* data_off will be ajusted after new mbuf allocated for 512-byte
259          * alignment.
260          */
261         mb_def.data_off = RTE_PKTMBUF_HEADROOM;
262         mb_def.port = rxq->port_id;
263         rte_mbuf_refcnt_set(&mb_def, 1);
264
265         /* prevent compiler reordering: rearm_data covers previous fields */
266         rte_compiler_barrier();
267         p = (uintptr_t)&mb_def.rearm_data;
268         rxq->mbuf_initializer = *(uint64_t *)p;
269         return 0;
270 }
271
272 static inline void
273 fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
274 {
275         int i;
276         uint16_t rx_id;
277         volatile union fm10k_rx_desc *rxdp;
278         struct rte_mbuf **mb_alloc = &rxq->sw_ring[rxq->rxrearm_start];
279         struct rte_mbuf *mb0, *mb1;
280         __m128i head_off = _mm_set_epi64x(
281                         RTE_PKTMBUF_HEADROOM + FM10K_RX_DATABUF_ALIGN - 1,
282                         RTE_PKTMBUF_HEADROOM + FM10K_RX_DATABUF_ALIGN - 1);
283         __m128i dma_addr0, dma_addr1;
284         /* Rx buffer need to be aligned with 512 byte */
285         const __m128i hba_msk = _mm_set_epi64x(0,
286                                 UINT64_MAX - FM10K_RX_DATABUF_ALIGN + 1);
287
288         rxdp = rxq->hw_ring + rxq->rxrearm_start;
289
290         /* Pull 'n' more MBUFs into the software ring */
291         if (rte_mempool_get_bulk(rxq->mp,
292                                  (void *)mb_alloc,
293                                  RTE_FM10K_RXQ_REARM_THRESH) < 0) {
294                 dma_addr0 = _mm_setzero_si128();
295                 /* Clean up all the HW/SW ring content */
296                 for (i = 0; i < RTE_FM10K_RXQ_REARM_THRESH; i++) {
297                         mb_alloc[i] = &rxq->fake_mbuf;
298                         _mm_store_si128((__m128i *)&rxdp[i].q,
299                                                 dma_addr0);
300                 }
301
302                 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed +=
303                         RTE_FM10K_RXQ_REARM_THRESH;
304                 return;
305         }
306
307         /* Initialize the mbufs in vector, process 2 mbufs in one loop */
308         for (i = 0; i < RTE_FM10K_RXQ_REARM_THRESH; i += 2, mb_alloc += 2) {
309                 __m128i vaddr0, vaddr1;
310                 uintptr_t p0, p1;
311
312                 mb0 = mb_alloc[0];
313                 mb1 = mb_alloc[1];
314
315                 /* Flush mbuf with pkt template.
316                  * Data to be rearmed is 6 bytes long.
317                  * Though, RX will overwrite ol_flags that are coming next
318                  * anyway. So overwrite whole 8 bytes with one load:
319                  * 6 bytes of rearm_data plus first 2 bytes of ol_flags.
320                  */
321                 p0 = (uintptr_t)&mb0->rearm_data;
322                 *(uint64_t *)p0 = rxq->mbuf_initializer;
323                 p1 = (uintptr_t)&mb1->rearm_data;
324                 *(uint64_t *)p1 = rxq->mbuf_initializer;
325
326                 /* load buf_addr(lo 64bit) and buf_physaddr(hi 64bit) */
327                 vaddr0 = _mm_loadu_si128((__m128i *)&mb0->buf_addr);
328                 vaddr1 = _mm_loadu_si128((__m128i *)&mb1->buf_addr);
329
330                 /* convert pa to dma_addr hdr/data */
331                 dma_addr0 = _mm_unpackhi_epi64(vaddr0, vaddr0);
332                 dma_addr1 = _mm_unpackhi_epi64(vaddr1, vaddr1);
333
334                 /* add headroom to pa values */
335                 dma_addr0 = _mm_add_epi64(dma_addr0, head_off);
336                 dma_addr1 = _mm_add_epi64(dma_addr1, head_off);
337
338                 /* Do 512 byte alignment to satisfy HW requirement, in the
339                  * meanwhile, set Header Buffer Address to zero.
340                  */
341                 dma_addr0 = _mm_and_si128(dma_addr0, hba_msk);
342                 dma_addr1 = _mm_and_si128(dma_addr1, hba_msk);
343
344                 /* flush desc with pa dma_addr */
345                 _mm_store_si128((__m128i *)&rxdp++->q, dma_addr0);
346                 _mm_store_si128((__m128i *)&rxdp++->q, dma_addr1);
347
348                 /* enforce 512B alignment on default Rx virtual addresses */
349                 mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
350                                 + RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
351                                 - (char *)mb0->buf_addr);
352                 mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
353                                 + RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
354                                 - (char *)mb1->buf_addr);
355         }
356
357         rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
358         if (rxq->rxrearm_start >= rxq->nb_desc)
359                 rxq->rxrearm_start = 0;
360
361         rxq->rxrearm_nb -= RTE_FM10K_RXQ_REARM_THRESH;
362
363         rx_id = (uint16_t)((rxq->rxrearm_start == 0) ?
364                         (rxq->nb_desc - 1) : (rxq->rxrearm_start - 1));
365
366         /* Update the tail pointer on the NIC */
367         FM10K_PCI_REG_WRITE(rxq->tail_ptr, rx_id);
368 }
369
370 void __attribute__((cold))
371 fm10k_rx_queue_release_mbufs_vec(struct fm10k_rx_queue *rxq)
372 {
373         const unsigned mask = rxq->nb_desc - 1;
374         unsigned i;
375
376         if (rxq->sw_ring == NULL || rxq->rxrearm_nb >= rxq->nb_desc)
377                 return;
378
379         /* free all mbufs that are valid in the ring */
380         for (i = rxq->next_dd; i != rxq->rxrearm_start; i = (i + 1) & mask)
381                 rte_pktmbuf_free_seg(rxq->sw_ring[i]);
382         rxq->rxrearm_nb = rxq->nb_desc;
383
384         /* set all entries to NULL */
385         memset(rxq->sw_ring, 0, sizeof(rxq->sw_ring[0]) * rxq->nb_desc);
386 }
387
388 static inline uint16_t
389 fm10k_recv_raw_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
390                 uint16_t nb_pkts, uint8_t *split_packet)
391 {
392         volatile union fm10k_rx_desc *rxdp;
393         struct rte_mbuf **mbufp;
394         uint16_t nb_pkts_recd;
395         int pos;
396         struct fm10k_rx_queue *rxq = rx_queue;
397         uint64_t var;
398         __m128i shuf_msk;
399         __m128i dd_check, eop_check;
400         uint16_t next_dd;
401
402         next_dd = rxq->next_dd;
403
404         /* Just the act of getting into the function from the application is
405          * going to cost about 7 cycles
406          */
407         rxdp = rxq->hw_ring + next_dd;
408
409         _mm_prefetch((const void *)rxdp, _MM_HINT_T0);
410
411         /* See if we need to rearm the RX queue - gives the prefetch a bit
412          * of time to act
413          */
414         if (rxq->rxrearm_nb > RTE_FM10K_RXQ_REARM_THRESH)
415                 fm10k_rxq_rearm(rxq);
416
417         /* Before we start moving massive data around, check to see if
418          * there is actually a packet available
419          */
420         if (!(rxdp->d.staterr & FM10K_RXD_STATUS_DD))
421                 return 0;
422
423         /* Vecotr RX will process 4 packets at a time, strip the unaligned
424          * tails in case it's not multiple of 4.
425          */
426         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_FM10K_DESCS_PER_LOOP);
427
428         /* 4 packets DD mask */
429         dd_check = _mm_set_epi64x(0x0000000100000001LL, 0x0000000100000001LL);
430
431         /* 4 packets EOP mask */
432         eop_check = _mm_set_epi64x(0x0000000200000002LL, 0x0000000200000002LL);
433
434         /* mask to shuffle from desc. to mbuf */
435         shuf_msk = _mm_set_epi8(
436                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
437                 15, 14,      /* octet 14~15, low 16 bits vlan_macip */
438                 13, 12,      /* octet 12~13, 16 bits data_len */
439                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
440                 13, 12,      /* octet 12~13, low 16 bits pkt_len */
441                 0xFF, 0xFF,  /* skip high 16 bits pkt_type */
442                 0xFF, 0xFF   /* Skip pkt_type field in shuffle operation */
443                 );
444
445         /* Cache is empty -> need to scan the buffer rings, but first move
446          * the next 'n' mbufs into the cache
447          */
448         mbufp = &rxq->sw_ring[next_dd];
449
450         /* A. load 4 packet in one loop
451          * [A*. mask out 4 unused dirty field in desc]
452          * B. copy 4 mbuf point from swring to rx_pkts
453          * C. calc the number of DD bits among the 4 packets
454          * [C*. extract the end-of-packet bit, if requested]
455          * D. fill info. from desc to mbuf
456          */
457         for (pos = 0, nb_pkts_recd = 0; pos < nb_pkts;
458                         pos += RTE_FM10K_DESCS_PER_LOOP,
459                         rxdp += RTE_FM10K_DESCS_PER_LOOP) {
460                 __m128i descs0[RTE_FM10K_DESCS_PER_LOOP];
461                 __m128i pkt_mb1, pkt_mb2, pkt_mb3, pkt_mb4;
462                 __m128i zero, staterr, sterr_tmp1, sterr_tmp2;
463                 __m128i mbp1, mbp2; /* two mbuf pointer in one XMM reg. */
464
465                 /* B.1 load 1 mbuf point */
466                 mbp1 = _mm_loadu_si128((__m128i *)&mbufp[pos]);
467
468                 /* Read desc statuses backwards to avoid race condition */
469                 /* A.1 load 4 pkts desc */
470                 descs0[3] = _mm_loadu_si128((__m128i *)(rxdp + 3));
471
472                 /* B.2 copy 2 mbuf point into rx_pkts  */
473                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
474
475                 /* B.1 load 1 mbuf point */
476                 mbp2 = _mm_loadu_si128((__m128i *)&mbufp[pos+2]);
477
478                 descs0[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
479                 /* B.1 load 2 mbuf point */
480                 descs0[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
481                 descs0[0] = _mm_loadu_si128((__m128i *)(rxdp));
482
483                 /* B.2 copy 2 mbuf point into rx_pkts  */
484                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
485
486                 /* avoid compiler reorder optimization */
487                 rte_compiler_barrier();
488
489                 if (split_packet) {
490                         rte_mbuf_prefetch_part2(rx_pkts[pos]);
491                         rte_mbuf_prefetch_part2(rx_pkts[pos + 1]);
492                         rte_mbuf_prefetch_part2(rx_pkts[pos + 2]);
493                         rte_mbuf_prefetch_part2(rx_pkts[pos + 3]);
494                 }
495
496                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
497                 pkt_mb4 = _mm_shuffle_epi8(descs0[3], shuf_msk);
498                 pkt_mb3 = _mm_shuffle_epi8(descs0[2], shuf_msk);
499
500                 /* C.1 4=>2 filter staterr info only */
501                 sterr_tmp2 = _mm_unpackhi_epi32(descs0[3], descs0[2]);
502                 /* C.1 4=>2 filter staterr info only */
503                 sterr_tmp1 = _mm_unpackhi_epi32(descs0[1], descs0[0]);
504
505                 /* set ol_flags with vlan packet type */
506                 fm10k_desc_to_olflags_v(descs0, &rx_pkts[pos]);
507
508                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
509                 pkt_mb2 = _mm_shuffle_epi8(descs0[1], shuf_msk);
510                 pkt_mb1 = _mm_shuffle_epi8(descs0[0], shuf_msk);
511
512                 /* C.2 get 4 pkts staterr value  */
513                 zero = _mm_xor_si128(dd_check, dd_check);
514                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
515
516                 /* D.3 copy final 3,4 data to rx_pkts */
517                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
518                                 pkt_mb4);
519                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
520                                 pkt_mb3);
521
522                 /* C* extract and record EOP bit */
523                 if (split_packet) {
524                         __m128i eop_shuf_mask = _mm_set_epi8(
525                                         0xFF, 0xFF, 0xFF, 0xFF,
526                                         0xFF, 0xFF, 0xFF, 0xFF,
527                                         0xFF, 0xFF, 0xFF, 0xFF,
528                                         0x04, 0x0C, 0x00, 0x08
529                                         );
530
531                         /* and with mask to extract bits, flipping 1-0 */
532                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
533                         /* the staterr values are not in order, as the count
534                          * count of dd bits doesn't care. However, for end of
535                          * packet tracking, we do care, so shuffle. This also
536                          * compresses the 32-bit values to 8-bit
537                          */
538                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
539                         /* store the resulting 32-bit value */
540                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
541                         split_packet += RTE_FM10K_DESCS_PER_LOOP;
542
543                         /* zero-out next pointers */
544                         rx_pkts[pos]->next = NULL;
545                         rx_pkts[pos + 1]->next = NULL;
546                         rx_pkts[pos + 2]->next = NULL;
547                         rx_pkts[pos + 3]->next = NULL;
548                 }
549
550                 /* C.3 calc available number of desc */
551                 staterr = _mm_and_si128(staterr, dd_check);
552                 staterr = _mm_packs_epi32(staterr, zero);
553
554                 /* D.3 copy final 1,2 data to rx_pkts */
555                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
556                                 pkt_mb2);
557                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
558                                 pkt_mb1);
559
560                 fm10k_desc_to_pktype_v(descs0, &rx_pkts[pos]);
561
562                 /* C.4 calc avaialbe number of desc */
563                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
564                 nb_pkts_recd += var;
565                 if (likely(var != RTE_FM10K_DESCS_PER_LOOP))
566                         break;
567         }
568
569         /* Update our internal tail pointer */
570         rxq->next_dd = (uint16_t)(rxq->next_dd + nb_pkts_recd);
571         rxq->next_dd = (uint16_t)(rxq->next_dd & (rxq->nb_desc - 1));
572         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
573
574         return nb_pkts_recd;
575 }
576
577 /* vPMD receive routine
578  *
579  * Notice:
580  * - don't support ol_flags for rss and csum err
581  */
582 uint16_t
583 fm10k_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
584                 uint16_t nb_pkts)
585 {
586         return fm10k_recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
587 }
588
589 static inline uint16_t
590 fm10k_reassemble_packets(struct fm10k_rx_queue *rxq,
591                 struct rte_mbuf **rx_bufs,
592                 uint16_t nb_bufs, uint8_t *split_flags)
593 {
594         struct rte_mbuf *pkts[RTE_FM10K_MAX_RX_BURST]; /*finished pkts*/
595         struct rte_mbuf *start = rxq->pkt_first_seg;
596         struct rte_mbuf *end =  rxq->pkt_last_seg;
597         unsigned pkt_idx, buf_idx;
598
599         for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
600                 if (end != NULL) {
601                         /* processing a split packet */
602                         end->next = rx_bufs[buf_idx];
603                         start->nb_segs++;
604                         start->pkt_len += rx_bufs[buf_idx]->data_len;
605                         end = end->next;
606
607                         if (!split_flags[buf_idx]) {
608                                 /* it's the last packet of the set */
609 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
610                                 start->hash = end->hash;
611                                 start->ol_flags = end->ol_flags;
612                                 start->packet_type = end->packet_type;
613 #endif
614                                 pkts[pkt_idx++] = start;
615                                 start = end = NULL;
616                         }
617                 } else {
618                         /* not processing a split packet */
619                         if (!split_flags[buf_idx]) {
620                                 /* not a split packet, save and skip */
621                                 pkts[pkt_idx++] = rx_bufs[buf_idx];
622                                 continue;
623                         }
624                         end = start = rx_bufs[buf_idx];
625                 }
626         }
627
628         /* save the partial packet for next time */
629         rxq->pkt_first_seg = start;
630         rxq->pkt_last_seg = end;
631         memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
632         return pkt_idx;
633 }
634
635 /*
636  * vPMD receive routine that reassembles scattered packets
637  *
638  * Notice:
639  * - don't support ol_flags for rss and csum err
640  * - nb_pkts > RTE_FM10K_MAX_RX_BURST, only scan RTE_FM10K_MAX_RX_BURST
641  *   numbers of DD bit
642  */
643 uint16_t
644 fm10k_recv_scattered_pkts_vec(void *rx_queue,
645                                 struct rte_mbuf **rx_pkts,
646                                 uint16_t nb_pkts)
647 {
648         struct fm10k_rx_queue *rxq = rx_queue;
649         uint8_t split_flags[RTE_FM10K_MAX_RX_BURST] = {0};
650         unsigned i = 0;
651
652         /* Split_flags only can support max of RTE_FM10K_MAX_RX_BURST */
653         nb_pkts = RTE_MIN(nb_pkts, RTE_FM10K_MAX_RX_BURST);
654         /* get some new buffers */
655         uint16_t nb_bufs = fm10k_recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
656                         split_flags);
657         if (nb_bufs == 0)
658                 return 0;
659
660         /* happy day case, full burst + no packets to be joined */
661         const uint64_t *split_fl64 = (uint64_t *)split_flags;
662
663         if (rxq->pkt_first_seg == NULL &&
664                         split_fl64[0] == 0 && split_fl64[1] == 0 &&
665                         split_fl64[2] == 0 && split_fl64[3] == 0)
666                 return nb_bufs;
667
668         /* reassemble any packets that need reassembly*/
669         if (rxq->pkt_first_seg == NULL) {
670                 /* find the first split flag, and only reassemble then*/
671                 while (i < nb_bufs && !split_flags[i])
672                         i++;
673                 if (i == nb_bufs)
674                         return nb_bufs;
675         }
676         return i + fm10k_reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
677                 &split_flags[i]);
678 }
679
680 static const struct fm10k_txq_ops vec_txq_ops = {
681         .reset = fm10k_reset_tx_queue,
682 };
683
684 void __attribute__((cold))
685 fm10k_txq_vec_setup(struct fm10k_tx_queue *txq)
686 {
687         txq->ops = &vec_txq_ops;
688 }
689
690 int __attribute__((cold))
691 fm10k_tx_vec_condition_check(struct fm10k_tx_queue *txq)
692 {
693         /* Vector TX can't offload any features yet */
694         if ((txq->txq_flags & FM10K_SIMPLE_TX_FLAG) != FM10K_SIMPLE_TX_FLAG)
695                 return -1;
696
697         if (txq->tx_ftag_en)
698                 return -1;
699
700         return 0;
701 }
702
703 static inline void
704 vtx1(volatile struct fm10k_tx_desc *txdp,
705                 struct rte_mbuf *pkt, uint64_t flags)
706 {
707         __m128i descriptor = _mm_set_epi64x(flags << 56 |
708                         pkt->vlan_tci << 16 | pkt->data_len,
709                         MBUF_DMA_ADDR(pkt));
710         _mm_store_si128((__m128i *)txdp, descriptor);
711 }
712
713 static inline void
714 vtx(volatile struct fm10k_tx_desc *txdp,
715                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
716 {
717         int i;
718
719         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
720                 vtx1(txdp, *pkt, flags);
721 }
722
723 static inline int __attribute__((always_inline))
724 fm10k_tx_free_bufs(struct fm10k_tx_queue *txq)
725 {
726         struct rte_mbuf **txep;
727         uint8_t flags;
728         uint32_t n;
729         uint32_t i;
730         int nb_free = 0;
731         struct rte_mbuf *m, *free[RTE_FM10K_TX_MAX_FREE_BUF_SZ];
732
733         /* check DD bit on threshold descriptor */
734         flags = txq->hw_ring[txq->next_dd].flags;
735         if (!(flags & FM10K_TXD_FLAG_DONE))
736                 return 0;
737
738         n = txq->rs_thresh;
739
740         /* First buffer to free from S/W ring is at index
741          * next_dd - (rs_thresh-1)
742          */
743         txep = &txq->sw_ring[txq->next_dd - (n - 1)];
744         m = __rte_pktmbuf_prefree_seg(txep[0]);
745         if (likely(m != NULL)) {
746                 free[0] = m;
747                 nb_free = 1;
748                 for (i = 1; i < n; i++) {
749                         m = __rte_pktmbuf_prefree_seg(txep[i]);
750                         if (likely(m != NULL)) {
751                                 if (likely(m->pool == free[0]->pool))
752                                         free[nb_free++] = m;
753                                 else {
754                                         rte_mempool_put_bulk(free[0]->pool,
755                                                         (void *)free, nb_free);
756                                         free[0] = m;
757                                         nb_free = 1;
758                                 }
759                         }
760                 }
761                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
762         } else {
763                 for (i = 1; i < n; i++) {
764                         m = __rte_pktmbuf_prefree_seg(txep[i]);
765                         if (m != NULL)
766                                 rte_mempool_put(m->pool, m);
767                 }
768         }
769
770         /* buffers were freed, update counters */
771         txq->nb_free = (uint16_t)(txq->nb_free + txq->rs_thresh);
772         txq->next_dd = (uint16_t)(txq->next_dd + txq->rs_thresh);
773         if (txq->next_dd >= txq->nb_desc)
774                 txq->next_dd = (uint16_t)(txq->rs_thresh - 1);
775
776         return txq->rs_thresh;
777 }
778
779 static inline void __attribute__((always_inline))
780 tx_backlog_entry(struct rte_mbuf **txep,
781                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
782 {
783         int i;
784
785         for (i = 0; i < (int)nb_pkts; ++i)
786                 txep[i] = tx_pkts[i];
787 }
788
789 uint16_t
790 fm10k_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
791                         uint16_t nb_pkts)
792 {
793         struct fm10k_tx_queue *txq = (struct fm10k_tx_queue *)tx_queue;
794         volatile struct fm10k_tx_desc *txdp;
795         struct rte_mbuf **txep;
796         uint16_t n, nb_commit, tx_id;
797         uint64_t flags = FM10K_TXD_FLAG_LAST;
798         uint64_t rs = FM10K_TXD_FLAG_RS | FM10K_TXD_FLAG_LAST;
799         int i;
800
801         /* cross rx_thresh boundary is not allowed */
802         nb_pkts = RTE_MIN(nb_pkts, txq->rs_thresh);
803
804         if (txq->nb_free < txq->free_thresh)
805                 fm10k_tx_free_bufs(txq);
806
807         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_free, nb_pkts);
808         if (unlikely(nb_pkts == 0))
809                 return 0;
810
811         tx_id = txq->next_free;
812         txdp = &txq->hw_ring[tx_id];
813         txep = &txq->sw_ring[tx_id];
814
815         txq->nb_free = (uint16_t)(txq->nb_free - nb_pkts);
816
817         n = (uint16_t)(txq->nb_desc - tx_id);
818         if (nb_commit >= n) {
819                 tx_backlog_entry(txep, tx_pkts, n);
820
821                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
822                         vtx1(txdp, *tx_pkts, flags);
823
824                 vtx1(txdp, *tx_pkts++, rs);
825
826                 nb_commit = (uint16_t)(nb_commit - n);
827
828                 tx_id = 0;
829                 txq->next_rs = (uint16_t)(txq->rs_thresh - 1);
830
831                 /* avoid reach the end of ring */
832                 txdp = &(txq->hw_ring[tx_id]);
833                 txep = &txq->sw_ring[tx_id];
834         }
835
836         tx_backlog_entry(txep, tx_pkts, nb_commit);
837
838         vtx(txdp, tx_pkts, nb_commit, flags);
839
840         tx_id = (uint16_t)(tx_id + nb_commit);
841         if (tx_id > txq->next_rs) {
842                 txq->hw_ring[txq->next_rs].flags |= FM10K_TXD_FLAG_RS;
843                 txq->next_rs = (uint16_t)(txq->next_rs + txq->rs_thresh);
844         }
845
846         txq->next_free = tx_id;
847
848         FM10K_PCI_REG_WRITE(txq->tail_ptr, txq->next_free);
849
850         return nb_pkts;
851 }
852
853 static void __attribute__((cold))
854 fm10k_reset_tx_queue(struct fm10k_tx_queue *txq)
855 {
856         static const struct fm10k_tx_desc zeroed_desc = {0};
857         struct rte_mbuf **txe = txq->sw_ring;
858         uint16_t i;
859
860         /* Zero out HW ring memory */
861         for (i = 0; i < txq->nb_desc; i++)
862                 txq->hw_ring[i] = zeroed_desc;
863
864         /* Initialize SW ring entries */
865         for (i = 0; i < txq->nb_desc; i++)
866                 txe[i] = NULL;
867
868         txq->next_dd = (uint16_t)(txq->rs_thresh - 1);
869         txq->next_rs = (uint16_t)(txq->rs_thresh - 1);
870
871         txq->next_free = 0;
872         txq->nb_used = 0;
873         /* Always allow 1 descriptor to be un-allocated to avoid
874          * a H/W race condition
875          */
876         txq->nb_free = (uint16_t)(txq->nb_desc - 1);
877         FM10K_PCI_REG_WRITE(txq->tail_ptr, 0);
878 }