Imported Upstream version 16.07.2
[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                 rte_compiler_barrier();
472
473                 /* B.2 copy 2 mbuf point into rx_pkts  */
474                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
475
476                 /* B.1 load 1 mbuf point */
477                 mbp2 = _mm_loadu_si128((__m128i *)&mbufp[pos+2]);
478
479                 descs0[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
480                 rte_compiler_barrier();
481                 /* B.1 load 2 mbuf point */
482                 descs0[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
483                 rte_compiler_barrier();
484                 descs0[0] = _mm_loadu_si128((__m128i *)(rxdp));
485
486                 /* B.2 copy 2 mbuf point into rx_pkts  */
487                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
488
489                 /* avoid compiler reorder optimization */
490                 rte_compiler_barrier();
491
492                 if (split_packet) {
493                         rte_mbuf_prefetch_part2(rx_pkts[pos]);
494                         rte_mbuf_prefetch_part2(rx_pkts[pos + 1]);
495                         rte_mbuf_prefetch_part2(rx_pkts[pos + 2]);
496                         rte_mbuf_prefetch_part2(rx_pkts[pos + 3]);
497                 }
498
499                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
500                 pkt_mb4 = _mm_shuffle_epi8(descs0[3], shuf_msk);
501                 pkt_mb3 = _mm_shuffle_epi8(descs0[2], shuf_msk);
502
503                 /* C.1 4=>2 filter staterr info only */
504                 sterr_tmp2 = _mm_unpackhi_epi32(descs0[3], descs0[2]);
505                 /* C.1 4=>2 filter staterr info only */
506                 sterr_tmp1 = _mm_unpackhi_epi32(descs0[1], descs0[0]);
507
508                 /* set ol_flags with vlan packet type */
509                 fm10k_desc_to_olflags_v(descs0, &rx_pkts[pos]);
510
511                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
512                 pkt_mb2 = _mm_shuffle_epi8(descs0[1], shuf_msk);
513                 pkt_mb1 = _mm_shuffle_epi8(descs0[0], shuf_msk);
514
515                 /* C.2 get 4 pkts staterr value  */
516                 zero = _mm_xor_si128(dd_check, dd_check);
517                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
518
519                 /* D.3 copy final 3,4 data to rx_pkts */
520                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
521                                 pkt_mb4);
522                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
523                                 pkt_mb3);
524
525                 /* C* extract and record EOP bit */
526                 if (split_packet) {
527                         __m128i eop_shuf_mask = _mm_set_epi8(
528                                         0xFF, 0xFF, 0xFF, 0xFF,
529                                         0xFF, 0xFF, 0xFF, 0xFF,
530                                         0xFF, 0xFF, 0xFF, 0xFF,
531                                         0x04, 0x0C, 0x00, 0x08
532                                         );
533
534                         /* and with mask to extract bits, flipping 1-0 */
535                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
536                         /* the staterr values are not in order, as the count
537                          * count of dd bits doesn't care. However, for end of
538                          * packet tracking, we do care, so shuffle. This also
539                          * compresses the 32-bit values to 8-bit
540                          */
541                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
542                         /* store the resulting 32-bit value */
543                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
544                         split_packet += RTE_FM10K_DESCS_PER_LOOP;
545
546                         /* zero-out next pointers */
547                         rx_pkts[pos]->next = NULL;
548                         rx_pkts[pos + 1]->next = NULL;
549                         rx_pkts[pos + 2]->next = NULL;
550                         rx_pkts[pos + 3]->next = NULL;
551                 }
552
553                 /* C.3 calc available number of desc */
554                 staterr = _mm_and_si128(staterr, dd_check);
555                 staterr = _mm_packs_epi32(staterr, zero);
556
557                 /* D.3 copy final 1,2 data to rx_pkts */
558                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
559                                 pkt_mb2);
560                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
561                                 pkt_mb1);
562
563                 fm10k_desc_to_pktype_v(descs0, &rx_pkts[pos]);
564
565                 /* C.4 calc avaialbe number of desc */
566                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
567                 nb_pkts_recd += var;
568                 if (likely(var != RTE_FM10K_DESCS_PER_LOOP))
569                         break;
570         }
571
572         /* Update our internal tail pointer */
573         rxq->next_dd = (uint16_t)(rxq->next_dd + nb_pkts_recd);
574         rxq->next_dd = (uint16_t)(rxq->next_dd & (rxq->nb_desc - 1));
575         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
576
577         return nb_pkts_recd;
578 }
579
580 /* vPMD receive routine
581  *
582  * Notice:
583  * - don't support ol_flags for rss and csum err
584  */
585 uint16_t
586 fm10k_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
587                 uint16_t nb_pkts)
588 {
589         return fm10k_recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
590 }
591
592 static inline uint16_t
593 fm10k_reassemble_packets(struct fm10k_rx_queue *rxq,
594                 struct rte_mbuf **rx_bufs,
595                 uint16_t nb_bufs, uint8_t *split_flags)
596 {
597         struct rte_mbuf *pkts[RTE_FM10K_MAX_RX_BURST]; /*finished pkts*/
598         struct rte_mbuf *start = rxq->pkt_first_seg;
599         struct rte_mbuf *end =  rxq->pkt_last_seg;
600         unsigned pkt_idx, buf_idx;
601
602         for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
603                 if (end != NULL) {
604                         /* processing a split packet */
605                         end->next = rx_bufs[buf_idx];
606                         start->nb_segs++;
607                         start->pkt_len += rx_bufs[buf_idx]->data_len;
608                         end = end->next;
609
610                         if (!split_flags[buf_idx]) {
611                                 /* it's the last packet of the set */
612 #ifdef RTE_LIBRTE_FM10K_RX_OLFLAGS_ENABLE
613                                 start->hash = end->hash;
614                                 start->ol_flags = end->ol_flags;
615                                 start->packet_type = end->packet_type;
616 #endif
617                                 pkts[pkt_idx++] = start;
618                                 start = end = NULL;
619                         }
620                 } else {
621                         /* not processing a split packet */
622                         if (!split_flags[buf_idx]) {
623                                 /* not a split packet, save and skip */
624                                 pkts[pkt_idx++] = rx_bufs[buf_idx];
625                                 continue;
626                         }
627                         end = start = rx_bufs[buf_idx];
628                 }
629         }
630
631         /* save the partial packet for next time */
632         rxq->pkt_first_seg = start;
633         rxq->pkt_last_seg = end;
634         memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
635         return pkt_idx;
636 }
637
638 /*
639  * vPMD receive routine that reassembles scattered packets
640  *
641  * Notice:
642  * - don't support ol_flags for rss and csum err
643  * - nb_pkts > RTE_FM10K_MAX_RX_BURST, only scan RTE_FM10K_MAX_RX_BURST
644  *   numbers of DD bit
645  */
646 uint16_t
647 fm10k_recv_scattered_pkts_vec(void *rx_queue,
648                                 struct rte_mbuf **rx_pkts,
649                                 uint16_t nb_pkts)
650 {
651         struct fm10k_rx_queue *rxq = rx_queue;
652         uint8_t split_flags[RTE_FM10K_MAX_RX_BURST] = {0};
653         unsigned i = 0;
654
655         /* Split_flags only can support max of RTE_FM10K_MAX_RX_BURST */
656         nb_pkts = RTE_MIN(nb_pkts, RTE_FM10K_MAX_RX_BURST);
657         /* get some new buffers */
658         uint16_t nb_bufs = fm10k_recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
659                         split_flags);
660         if (nb_bufs == 0)
661                 return 0;
662
663         /* happy day case, full burst + no packets to be joined */
664         const uint64_t *split_fl64 = (uint64_t *)split_flags;
665
666         if (rxq->pkt_first_seg == NULL &&
667                         split_fl64[0] == 0 && split_fl64[1] == 0 &&
668                         split_fl64[2] == 0 && split_fl64[3] == 0)
669                 return nb_bufs;
670
671         /* reassemble any packets that need reassembly*/
672         if (rxq->pkt_first_seg == NULL) {
673                 /* find the first split flag, and only reassemble then*/
674                 while (i < nb_bufs && !split_flags[i])
675                         i++;
676                 if (i == nb_bufs)
677                         return nb_bufs;
678         }
679         return i + fm10k_reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
680                 &split_flags[i]);
681 }
682
683 static const struct fm10k_txq_ops vec_txq_ops = {
684         .reset = fm10k_reset_tx_queue,
685 };
686
687 void __attribute__((cold))
688 fm10k_txq_vec_setup(struct fm10k_tx_queue *txq)
689 {
690         txq->ops = &vec_txq_ops;
691 }
692
693 int __attribute__((cold))
694 fm10k_tx_vec_condition_check(struct fm10k_tx_queue *txq)
695 {
696         /* Vector TX can't offload any features yet */
697         if ((txq->txq_flags & FM10K_SIMPLE_TX_FLAG) != FM10K_SIMPLE_TX_FLAG)
698                 return -1;
699
700         if (txq->tx_ftag_en)
701                 return -1;
702
703         return 0;
704 }
705
706 static inline void
707 vtx1(volatile struct fm10k_tx_desc *txdp,
708                 struct rte_mbuf *pkt, uint64_t flags)
709 {
710         __m128i descriptor = _mm_set_epi64x(flags << 56 |
711                         pkt->vlan_tci << 16 | pkt->data_len,
712                         MBUF_DMA_ADDR(pkt));
713         _mm_store_si128((__m128i *)txdp, descriptor);
714 }
715
716 static inline void
717 vtx(volatile struct fm10k_tx_desc *txdp,
718                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
719 {
720         int i;
721
722         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
723                 vtx1(txdp, *pkt, flags);
724 }
725
726 static inline int __attribute__((always_inline))
727 fm10k_tx_free_bufs(struct fm10k_tx_queue *txq)
728 {
729         struct rte_mbuf **txep;
730         uint8_t flags;
731         uint32_t n;
732         uint32_t i;
733         int nb_free = 0;
734         struct rte_mbuf *m, *free[RTE_FM10K_TX_MAX_FREE_BUF_SZ];
735
736         /* check DD bit on threshold descriptor */
737         flags = txq->hw_ring[txq->next_dd].flags;
738         if (!(flags & FM10K_TXD_FLAG_DONE))
739                 return 0;
740
741         n = txq->rs_thresh;
742
743         /* First buffer to free from S/W ring is at index
744          * next_dd - (rs_thresh-1)
745          */
746         txep = &txq->sw_ring[txq->next_dd - (n - 1)];
747         m = __rte_pktmbuf_prefree_seg(txep[0]);
748         if (likely(m != NULL)) {
749                 free[0] = m;
750                 nb_free = 1;
751                 for (i = 1; i < n; i++) {
752                         m = __rte_pktmbuf_prefree_seg(txep[i]);
753                         if (likely(m != NULL)) {
754                                 if (likely(m->pool == free[0]->pool))
755                                         free[nb_free++] = m;
756                                 else {
757                                         rte_mempool_put_bulk(free[0]->pool,
758                                                         (void *)free, nb_free);
759                                         free[0] = m;
760                                         nb_free = 1;
761                                 }
762                         }
763                 }
764                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
765         } else {
766                 for (i = 1; i < n; i++) {
767                         m = __rte_pktmbuf_prefree_seg(txep[i]);
768                         if (m != NULL)
769                                 rte_mempool_put(m->pool, m);
770                 }
771         }
772
773         /* buffers were freed, update counters */
774         txq->nb_free = (uint16_t)(txq->nb_free + txq->rs_thresh);
775         txq->next_dd = (uint16_t)(txq->next_dd + txq->rs_thresh);
776         if (txq->next_dd >= txq->nb_desc)
777                 txq->next_dd = (uint16_t)(txq->rs_thresh - 1);
778
779         return txq->rs_thresh;
780 }
781
782 static inline void __attribute__((always_inline))
783 tx_backlog_entry(struct rte_mbuf **txep,
784                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
785 {
786         int i;
787
788         for (i = 0; i < (int)nb_pkts; ++i)
789                 txep[i] = tx_pkts[i];
790 }
791
792 uint16_t
793 fm10k_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
794                         uint16_t nb_pkts)
795 {
796         struct fm10k_tx_queue *txq = (struct fm10k_tx_queue *)tx_queue;
797         volatile struct fm10k_tx_desc *txdp;
798         struct rte_mbuf **txep;
799         uint16_t n, nb_commit, tx_id;
800         uint64_t flags = FM10K_TXD_FLAG_LAST;
801         uint64_t rs = FM10K_TXD_FLAG_RS | FM10K_TXD_FLAG_LAST;
802         int i;
803
804         /* cross rx_thresh boundary is not allowed */
805         nb_pkts = RTE_MIN(nb_pkts, txq->rs_thresh);
806
807         if (txq->nb_free < txq->free_thresh)
808                 fm10k_tx_free_bufs(txq);
809
810         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_free, nb_pkts);
811         if (unlikely(nb_pkts == 0))
812                 return 0;
813
814         tx_id = txq->next_free;
815         txdp = &txq->hw_ring[tx_id];
816         txep = &txq->sw_ring[tx_id];
817
818         txq->nb_free = (uint16_t)(txq->nb_free - nb_pkts);
819
820         n = (uint16_t)(txq->nb_desc - tx_id);
821         if (nb_commit >= n) {
822                 tx_backlog_entry(txep, tx_pkts, n);
823
824                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
825                         vtx1(txdp, *tx_pkts, flags);
826
827                 vtx1(txdp, *tx_pkts++, rs);
828
829                 nb_commit = (uint16_t)(nb_commit - n);
830
831                 tx_id = 0;
832                 txq->next_rs = (uint16_t)(txq->rs_thresh - 1);
833
834                 /* avoid reach the end of ring */
835                 txdp = &(txq->hw_ring[tx_id]);
836                 txep = &txq->sw_ring[tx_id];
837         }
838
839         tx_backlog_entry(txep, tx_pkts, nb_commit);
840
841         vtx(txdp, tx_pkts, nb_commit, flags);
842
843         tx_id = (uint16_t)(tx_id + nb_commit);
844         if (tx_id > txq->next_rs) {
845                 txq->hw_ring[txq->next_rs].flags |= FM10K_TXD_FLAG_RS;
846                 txq->next_rs = (uint16_t)(txq->next_rs + txq->rs_thresh);
847         }
848
849         txq->next_free = tx_id;
850
851         FM10K_PCI_REG_WRITE(txq->tail_ptr, txq->next_free);
852
853         return nb_pkts;
854 }
855
856 static void __attribute__((cold))
857 fm10k_reset_tx_queue(struct fm10k_tx_queue *txq)
858 {
859         static const struct fm10k_tx_desc zeroed_desc = {0};
860         struct rte_mbuf **txe = txq->sw_ring;
861         uint16_t i;
862
863         /* Zero out HW ring memory */
864         for (i = 0; i < txq->nb_desc; i++)
865                 txq->hw_ring[i] = zeroed_desc;
866
867         /* Initialize SW ring entries */
868         for (i = 0; i < txq->nb_desc; i++)
869                 txe[i] = NULL;
870
871         txq->next_dd = (uint16_t)(txq->rs_thresh - 1);
872         txq->next_rs = (uint16_t)(txq->rs_thresh - 1);
873
874         txq->next_free = 0;
875         txq->nb_used = 0;
876         /* Always allow 1 descriptor to be un-allocated to avoid
877          * a H/W race condition
878          */
879         txq->nb_free = (uint16_t)(txq->nb_desc - 1);
880         FM10K_PCI_REG_WRITE(txq->tail_ptr, 0);
881 }