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