c01da7b636fd3fa1b3fca8d5917308a77e3b1dbb
[dpdk.git] / drivers / net / ixgbe / ixgbe_rxtx_vec.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <rte_ethdev.h>
36 #include <rte_malloc.h>
37
38 #include "ixgbe_ethdev.h"
39 #include "ixgbe_rxtx.h"
40
41 #include <tmmintrin.h>
42
43 #ifndef __INTEL_COMPILER
44 #pragma GCC diagnostic ignored "-Wcast-qual"
45 #endif
46
47 static inline void
48 ixgbe_rxq_rearm(struct ixgbe_rx_queue *rxq)
49 {
50         int i;
51         uint16_t rx_id;
52         volatile union ixgbe_adv_rx_desc *rxdp;
53         struct ixgbe_rx_entry *rxep = &rxq->sw_ring[rxq->rxrearm_start];
54         struct rte_mbuf *mb0, *mb1;
55         __m128i hdr_room = _mm_set_epi64x(RTE_PKTMBUF_HEADROOM,
56                         RTE_PKTMBUF_HEADROOM);
57         __m128i dma_addr0, dma_addr1;
58
59         rxdp = rxq->rx_ring + rxq->rxrearm_start;
60
61         /* Pull 'n' more MBUFs into the software ring */
62         if (rte_mempool_get_bulk(rxq->mb_pool,
63                                  (void *)rxep,
64                                  RTE_IXGBE_RXQ_REARM_THRESH) < 0) {
65                 if (rxq->rxrearm_nb + RTE_IXGBE_RXQ_REARM_THRESH >=
66                     rxq->nb_rx_desc) {
67                         dma_addr0 = _mm_setzero_si128();
68                         for (i = 0; i < RTE_IXGBE_DESCS_PER_LOOP; i++) {
69                                 rxep[i].mbuf = &rxq->fake_mbuf;
70                                 _mm_store_si128((__m128i *)&rxdp[i].read,
71                                                 dma_addr0);
72                         }
73                 }
74                 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed +=
75                         RTE_IXGBE_RXQ_REARM_THRESH;
76                 return;
77         }
78
79         /* Initialize the mbufs in vector, process 2 mbufs in one loop */
80         for (i = 0; i < RTE_IXGBE_RXQ_REARM_THRESH; i += 2, rxep += 2) {
81                 __m128i vaddr0, vaddr1;
82                 uintptr_t p0, p1;
83
84                 mb0 = rxep[0].mbuf;
85                 mb1 = rxep[1].mbuf;
86
87                 /*
88                  * Flush mbuf with pkt template.
89                  * Data to be rearmed is 6 bytes long.
90                  * Though, RX will overwrite ol_flags that are coming next
91                  * anyway. So overwrite whole 8 bytes with one load:
92                  * 6 bytes of rearm_data plus first 2 bytes of ol_flags.
93                  */
94                 p0 = (uintptr_t)&mb0->rearm_data;
95                 *(uint64_t *)p0 = rxq->mbuf_initializer;
96                 p1 = (uintptr_t)&mb1->rearm_data;
97                 *(uint64_t *)p1 = rxq->mbuf_initializer;
98
99                 /* load buf_addr(lo 64bit) and buf_physaddr(hi 64bit) */
100                 vaddr0 = _mm_loadu_si128((__m128i *)&(mb0->buf_addr));
101                 vaddr1 = _mm_loadu_si128((__m128i *)&(mb1->buf_addr));
102
103                 /* convert pa to dma_addr hdr/data */
104                 dma_addr0 = _mm_unpackhi_epi64(vaddr0, vaddr0);
105                 dma_addr1 = _mm_unpackhi_epi64(vaddr1, vaddr1);
106
107                 /* add headroom to pa values */
108                 dma_addr0 = _mm_add_epi64(dma_addr0, hdr_room);
109                 dma_addr1 = _mm_add_epi64(dma_addr1, hdr_room);
110
111                 /* flush desc with pa dma_addr */
112                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr0);
113                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr1);
114         }
115
116         rxq->rxrearm_start += RTE_IXGBE_RXQ_REARM_THRESH;
117         if (rxq->rxrearm_start >= rxq->nb_rx_desc)
118                 rxq->rxrearm_start = 0;
119
120         rxq->rxrearm_nb -= RTE_IXGBE_RXQ_REARM_THRESH;
121
122         rx_id = (uint16_t) ((rxq->rxrearm_start == 0) ?
123                              (rxq->nb_rx_desc - 1) : (rxq->rxrearm_start - 1));
124
125         /* Update the tail pointer on the NIC */
126         IXGBE_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
127 }
128
129 /* Handling the offload flags (olflags) field takes computation
130  * time when receiving packets. Therefore we provide a flag to disable
131  * the processing of the olflags field when they are not needed. This
132  * gives improved performance, at the cost of losing the offload info
133  * in the received packet
134  */
135 #ifdef RTE_IXGBE_RX_OLFLAGS_ENABLE
136
137 #ifdef RTE_NEXT_ABI
138 #define OLFLAGS_MASK_V  (((uint64_t)PKT_RX_VLAN_PKT << 48) | \
139                         ((uint64_t)PKT_RX_VLAN_PKT << 32) | \
140                         ((uint64_t)PKT_RX_VLAN_PKT << 16) | \
141                         ((uint64_t)PKT_RX_VLAN_PKT))
142 #else
143 #define OLFLAGS_MASK     ((uint16_t)(PKT_RX_VLAN_PKT | PKT_RX_IPV4_HDR |\
144                                      PKT_RX_IPV4_HDR_EXT | PKT_RX_IPV6_HDR |\
145                                      PKT_RX_IPV6_HDR_EXT))
146 #define OLFLAGS_MASK_V   (((uint64_t)OLFLAGS_MASK << 48) | \
147                           ((uint64_t)OLFLAGS_MASK << 32) | \
148                           ((uint64_t)OLFLAGS_MASK << 16) | \
149                           ((uint64_t)OLFLAGS_MASK))
150 #define PTYPE_SHIFT    (1)
151 #endif /* RTE_NEXT_ABI */
152
153 #define VTAG_SHIFT     (3)
154
155 static inline void
156 desc_to_olflags_v(__m128i descs[4], struct rte_mbuf **rx_pkts)
157 {
158 #ifdef RTE_NEXT_ABI
159         __m128i vtag0, vtag1;
160         union {
161                 uint16_t e[4];
162                 uint64_t dword;
163         } vol;
164
165         vtag0 = _mm_unpackhi_epi16(descs[0], descs[1]);
166         vtag1 = _mm_unpackhi_epi16(descs[2], descs[3]);
167         vtag1 = _mm_unpacklo_epi32(vtag0, vtag1);
168         vtag1 = _mm_srli_epi16(vtag1, VTAG_SHIFT);
169         vol.dword = _mm_cvtsi128_si64(vtag1) & OLFLAGS_MASK_V;
170 #else
171         __m128i ptype0, ptype1, vtag0, vtag1;
172         union {
173                 uint16_t e[4];
174                 uint64_t dword;
175         } vol;
176
177         ptype0 = _mm_unpacklo_epi16(descs[0], descs[1]);
178         ptype1 = _mm_unpacklo_epi16(descs[2], descs[3]);
179         vtag0 = _mm_unpackhi_epi16(descs[0], descs[1]);
180         vtag1 = _mm_unpackhi_epi16(descs[2], descs[3]);
181
182         ptype1 = _mm_unpacklo_epi32(ptype0, ptype1);
183         vtag1 = _mm_unpacklo_epi32(vtag0, vtag1);
184
185         ptype1 = _mm_slli_epi16(ptype1, PTYPE_SHIFT);
186         vtag1 = _mm_srli_epi16(vtag1, VTAG_SHIFT);
187
188         ptype1 = _mm_or_si128(ptype1, vtag1);
189         vol.dword = _mm_cvtsi128_si64(ptype1) & OLFLAGS_MASK_V;
190 #endif /* RTE_NEXT_ABI */
191
192         rx_pkts[0]->ol_flags = vol.e[0];
193         rx_pkts[1]->ol_flags = vol.e[1];
194         rx_pkts[2]->ol_flags = vol.e[2];
195         rx_pkts[3]->ol_flags = vol.e[3];
196 }
197 #else
198 #define desc_to_olflags_v(desc, rx_pkts) do {} while (0)
199 #endif
200
201 /*
202  * vPMD receive routine, now only accept (nb_pkts == RTE_IXGBE_VPMD_RX_BURST)
203  * in one loop
204  *
205  * Notice:
206  * - nb_pkts < RTE_IXGBE_VPMD_RX_BURST, just return no packet
207  * - nb_pkts > RTE_IXGBE_VPMD_RX_BURST, only scan RTE_IXGBE_VPMD_RX_BURST
208  *   numbers of DD bit
209  * - don't support ol_flags for rss and csum err
210  */
211 static inline uint16_t
212 _recv_raw_pkts_vec(struct ixgbe_rx_queue *rxq, struct rte_mbuf **rx_pkts,
213                 uint16_t nb_pkts, uint8_t *split_packet)
214 {
215         volatile union ixgbe_adv_rx_desc *rxdp;
216         struct ixgbe_rx_entry *sw_ring;
217         uint16_t nb_pkts_recd;
218         int pos;
219         uint64_t var;
220         __m128i shuf_msk;
221 #ifdef RTE_NEXT_ABI
222         __m128i crc_adjust = _mm_set_epi16(
223                                 0, 0, 0,    /* ignore non-length fields */
224                                 -rxq->crc_len, /* sub crc on data_len */
225                                 0,          /* ignore high-16bits of pkt_len */
226                                 -rxq->crc_len, /* sub crc on pkt_len */
227                                 0, 0            /* ignore pkt_type field */
228                         );
229         __m128i dd_check, eop_check;
230         __m128i desc_mask = _mm_set_epi32(0xFFFFFFFF, 0xFFFFFFFF,
231                                           0xFFFFFFFF, 0xFFFF07F0);
232 #else
233         __m128i crc_adjust = _mm_set_epi16(
234                                 0, 0, 0, 0, /* ignore non-length fields */
235                                 0,          /* ignore high-16bits of pkt_len */
236                                 -rxq->crc_len, /* sub crc on pkt_len */
237                                 -rxq->crc_len, /* sub crc on data_len */
238                                 0            /* ignore pkt_type field */
239                         );
240         __m128i dd_check, eop_check;
241 #endif /* RTE_NEXT_ABI */
242
243         if (unlikely(nb_pkts < RTE_IXGBE_VPMD_RX_BURST))
244                 return 0;
245
246         /* Just the act of getting into the function from the application is
247          * going to cost about 7 cycles */
248         rxdp = rxq->rx_ring + rxq->rx_tail;
249
250         _mm_prefetch((const void *)rxdp, _MM_HINT_T0);
251
252         /* See if we need to rearm the RX queue - gives the prefetch a bit
253          * of time to act */
254         if (rxq->rxrearm_nb > RTE_IXGBE_RXQ_REARM_THRESH)
255                 ixgbe_rxq_rearm(rxq);
256
257         /* Before we start moving massive data around, check to see if
258          * there is actually a packet available */
259         if (!(rxdp->wb.upper.status_error &
260                                 rte_cpu_to_le_32(IXGBE_RXDADV_STAT_DD)))
261                 return 0;
262
263         /* 4 packets DD mask */
264         dd_check = _mm_set_epi64x(0x0000000100000001LL, 0x0000000100000001LL);
265
266         /* 4 packets EOP mask */
267         eop_check = _mm_set_epi64x(0x0000000200000002LL, 0x0000000200000002LL);
268
269         /* mask to shuffle from desc. to mbuf */
270 #ifdef RTE_NEXT_ABI
271         shuf_msk = _mm_set_epi8(
272                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
273                 15, 14,      /* octet 14~15, low 16 bits vlan_macip */
274                 13, 12,      /* octet 12~13, 16 bits data_len */
275                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
276                 13, 12,      /* octet 12~13, low 16 bits pkt_len */
277                 0xFF, 0xFF,  /* skip high 16 bits pkt_type */
278                 1,           /* octet 1, 8 bits pkt_type field */
279                 0            /* octet 0, 4 bits offset 4 pkt_type field */
280                 );
281 #else
282         shuf_msk = _mm_set_epi8(
283                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
284                 0xFF, 0xFF,  /* skip high 16 bits vlan_macip, zero out */
285                 15, 14,      /* octet 14~15, low 16 bits vlan_macip */
286                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
287                 13, 12,      /* octet 12~13, low 16 bits pkt_len */
288                 13, 12,      /* octet 12~13, 16 bits data_len */
289                 0xFF, 0xFF   /* skip pkt_type field */
290                 );
291 #endif /* RTE_NEXT_ABI */
292
293         /* Cache is empty -> need to scan the buffer rings, but first move
294          * the next 'n' mbufs into the cache */
295         sw_ring = &rxq->sw_ring[rxq->rx_tail];
296
297 #ifdef RTE_NEXT_ABI
298         /* A. load 4 packet in one loop
299          * [A*. mask out 4 unused dirty field in desc]
300          * B. copy 4 mbuf point from swring to rx_pkts
301          * C. calc the number of DD bits among the 4 packets
302          * [C*. extract the end-of-packet bit, if requested]
303          * D. fill info. from desc to mbuf
304          */
305 #else
306         /* A. load 4 packet in one loop
307          * B. copy 4 mbuf point from swring to rx_pkts
308          * C. calc the number of DD bits among the 4 packets
309          * [C*. extract the end-of-packet bit, if requested]
310          * D. fill info. from desc to mbuf
311          */
312 #endif /* RTE_NEXT_ABI */
313         for (pos = 0, nb_pkts_recd = 0; pos < RTE_IXGBE_VPMD_RX_BURST;
314                         pos += RTE_IXGBE_DESCS_PER_LOOP,
315                         rxdp += RTE_IXGBE_DESCS_PER_LOOP) {
316                 __m128i descs[RTE_IXGBE_DESCS_PER_LOOP];
317                 __m128i pkt_mb1, pkt_mb2, pkt_mb3, pkt_mb4;
318                 __m128i zero, staterr, sterr_tmp1, sterr_tmp2;
319                 __m128i mbp1, mbp2; /* two mbuf pointer in one XMM reg. */
320
321                 if (split_packet) {
322                         rte_prefetch0(&rx_pkts[pos]->cacheline1);
323                         rte_prefetch0(&rx_pkts[pos + 1]->cacheline1);
324                         rte_prefetch0(&rx_pkts[pos + 2]->cacheline1);
325                         rte_prefetch0(&rx_pkts[pos + 3]->cacheline1);
326                 }
327
328                 /* B.1 load 1 mbuf point */
329                 mbp1 = _mm_loadu_si128((__m128i *)&sw_ring[pos]);
330
331                 /* Read desc statuses backwards to avoid race condition */
332                 /* A.1 load 4 pkts desc */
333                 descs[3] = _mm_loadu_si128((__m128i *)(rxdp + 3));
334
335                 /* B.2 copy 2 mbuf point into rx_pkts  */
336                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
337
338                 /* B.1 load 1 mbuf point */
339                 mbp2 = _mm_loadu_si128((__m128i *)&sw_ring[pos+2]);
340
341                 descs[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
342                 /* B.1 load 2 mbuf point */
343                 descs[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
344                 descs[0] = _mm_loadu_si128((__m128i *)(rxdp));
345
346                 /* B.2 copy 2 mbuf point into rx_pkts  */
347                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
348
349 #ifdef RTE_NEXT_ABI
350                 /* A* mask out 0~3 bits RSS type */
351                 descs[3] = _mm_and_si128(descs[3], desc_mask);
352                 descs[2] = _mm_and_si128(descs[2], desc_mask);
353
354                 /* A* mask out 0~3 bits RSS type */
355                 descs[1] = _mm_and_si128(descs[1], desc_mask);
356                 descs[0] = _mm_and_si128(descs[0], desc_mask);
357 #endif /* RTE_NEXT_ABI */
358
359                 /* avoid compiler reorder optimization */
360                 rte_compiler_barrier();
361
362                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
363                 pkt_mb4 = _mm_shuffle_epi8(descs[3], shuf_msk);
364                 pkt_mb3 = _mm_shuffle_epi8(descs[2], shuf_msk);
365
366                 /* C.1 4=>2 filter staterr info only */
367                 sterr_tmp2 = _mm_unpackhi_epi32(descs[3], descs[2]);
368                 /* C.1 4=>2 filter staterr info only */
369                 sterr_tmp1 = _mm_unpackhi_epi32(descs[1], descs[0]);
370
371 #ifdef RTE_NEXT_ABI
372                 /* set ol_flags with vlan packet type */
373 #else
374                 /* set ol_flags with packet type and vlan tag */
375 #endif /* RTE_NEXT_ABI */
376                 desc_to_olflags_v(descs, &rx_pkts[pos]);
377
378                 /* D.2 pkt 3,4 set in_port/nb_seg and remove crc */
379                 pkt_mb4 = _mm_add_epi16(pkt_mb4, crc_adjust);
380                 pkt_mb3 = _mm_add_epi16(pkt_mb3, crc_adjust);
381
382                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
383                 pkt_mb2 = _mm_shuffle_epi8(descs[1], shuf_msk);
384                 pkt_mb1 = _mm_shuffle_epi8(descs[0], shuf_msk);
385
386                 /* C.2 get 4 pkts staterr value  */
387                 zero = _mm_xor_si128(dd_check, dd_check);
388                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
389
390                 /* D.3 copy final 3,4 data to rx_pkts */
391                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
392                                 pkt_mb4);
393                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
394                                 pkt_mb3);
395
396                 /* D.2 pkt 1,2 set in_port/nb_seg and remove crc */
397                 pkt_mb2 = _mm_add_epi16(pkt_mb2, crc_adjust);
398                 pkt_mb1 = _mm_add_epi16(pkt_mb1, crc_adjust);
399
400                 /* C* extract and record EOP bit */
401                 if (split_packet) {
402                         __m128i eop_shuf_mask = _mm_set_epi8(
403                                         0xFF, 0xFF, 0xFF, 0xFF,
404                                         0xFF, 0xFF, 0xFF, 0xFF,
405                                         0xFF, 0xFF, 0xFF, 0xFF,
406                                         0x04, 0x0C, 0x00, 0x08
407                                         );
408
409                         /* and with mask to extract bits, flipping 1-0 */
410                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
411                         /* the staterr values are not in order, as the count
412                          * count of dd bits doesn't care. However, for end of
413                          * packet tracking, we do care, so shuffle. This also
414                          * compresses the 32-bit values to 8-bit */
415                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
416                         /* store the resulting 32-bit value */
417                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
418                         split_packet += RTE_IXGBE_DESCS_PER_LOOP;
419
420                         /* zero-out next pointers */
421                         rx_pkts[pos]->next = NULL;
422                         rx_pkts[pos + 1]->next = NULL;
423                         rx_pkts[pos + 2]->next = NULL;
424                         rx_pkts[pos + 3]->next = NULL;
425                 }
426
427                 /* C.3 calc available number of desc */
428                 staterr = _mm_and_si128(staterr, dd_check);
429                 staterr = _mm_packs_epi32(staterr, zero);
430
431                 /* D.3 copy final 1,2 data to rx_pkts */
432                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
433                                 pkt_mb2);
434                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
435                                 pkt_mb1);
436
437                 /* C.4 calc avaialbe number of desc */
438                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
439                 nb_pkts_recd += var;
440                 if (likely(var != RTE_IXGBE_DESCS_PER_LOOP))
441                         break;
442         }
443
444         /* Update our internal tail pointer */
445         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_pkts_recd);
446         rxq->rx_tail = (uint16_t)(rxq->rx_tail & (rxq->nb_rx_desc - 1));
447         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
448
449         return nb_pkts_recd;
450 }
451
452 /*
453  * vPMD receive routine, now only accept (nb_pkts == RTE_IXGBE_VPMD_RX_BURST)
454  * in one loop
455  *
456  * Notice:
457  * - nb_pkts < RTE_IXGBE_VPMD_RX_BURST, just return no packet
458  * - nb_pkts > RTE_IXGBE_VPMD_RX_BURST, only scan RTE_IXGBE_VPMD_RX_BURST
459  *   numbers of DD bit
460  * - don't support ol_flags for rss and csum err
461  */
462 uint16_t
463 ixgbe_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
464                 uint16_t nb_pkts)
465 {
466         return _recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
467 }
468
469 static inline uint16_t
470 reassemble_packets(struct ixgbe_rx_queue *rxq, struct rte_mbuf **rx_bufs,
471                 uint16_t nb_bufs, uint8_t *split_flags)
472 {
473         struct rte_mbuf *pkts[RTE_IXGBE_VPMD_RX_BURST]; /*finished pkts*/
474         struct rte_mbuf *start = rxq->pkt_first_seg;
475         struct rte_mbuf *end =  rxq->pkt_last_seg;
476         unsigned pkt_idx, buf_idx;
477
478
479         for (buf_idx = 0, pkt_idx = 0; buf_idx < nb_bufs; buf_idx++) {
480                 if (end != NULL) {
481                         /* processing a split packet */
482                         end->next = rx_bufs[buf_idx];
483                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
484
485                         start->nb_segs++;
486                         start->pkt_len += rx_bufs[buf_idx]->data_len;
487                         end = end->next;
488
489                         if (!split_flags[buf_idx]) {
490                                 /* it's the last packet of the set */
491                                 start->hash = end->hash;
492                                 start->ol_flags = end->ol_flags;
493                                 /* we need to strip crc for the whole packet */
494                                 start->pkt_len -= rxq->crc_len;
495                                 if (end->data_len > rxq->crc_len)
496                                         end->data_len -= rxq->crc_len;
497                                 else {
498                                         /* free up last mbuf */
499                                         struct rte_mbuf *secondlast = start;
500                                         while (secondlast->next != end)
501                                                 secondlast = secondlast->next;
502                                         secondlast->data_len -= (rxq->crc_len -
503                                                         end->data_len);
504                                         secondlast->next = NULL;
505                                         rte_pktmbuf_free_seg(end);
506                                         end = secondlast;
507                                 }
508                                 pkts[pkt_idx++] = start;
509                                 start = end = NULL;
510                         }
511                 } else {
512                         /* not processing a split packet */
513                         if (!split_flags[buf_idx]) {
514                                 /* not a split packet, save and skip */
515                                 pkts[pkt_idx++] = rx_bufs[buf_idx];
516                                 continue;
517                         }
518                         end = start = rx_bufs[buf_idx];
519                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
520                         rx_bufs[buf_idx]->pkt_len += rxq->crc_len;
521                 }
522         }
523
524         /* save the partial packet for next time */
525         rxq->pkt_first_seg = start;
526         rxq->pkt_last_seg = end;
527         memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
528         return pkt_idx;
529 }
530
531 /*
532  * vPMD receive routine that reassembles scattered packets
533  *
534  * Notice:
535  * - don't support ol_flags for rss and csum err
536  * - now only accept (nb_pkts == RTE_IXGBE_VPMD_RX_BURST)
537  */
538 uint16_t
539 ixgbe_recv_scattered_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
540                 uint16_t nb_pkts)
541 {
542         struct ixgbe_rx_queue *rxq = rx_queue;
543         uint8_t split_flags[RTE_IXGBE_VPMD_RX_BURST] = {0};
544
545         /* get some new buffers */
546         uint16_t nb_bufs = _recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
547                         split_flags);
548         if (nb_bufs == 0)
549                 return 0;
550
551         /* happy day case, full burst + no packets to be joined */
552         const uint32_t *split_fl32 = (uint32_t *)split_flags;
553         if (rxq->pkt_first_seg == NULL &&
554                         split_fl32[0] == 0 && split_fl32[1] == 0 &&
555                         split_fl32[2] == 0 && split_fl32[3] == 0)
556                 return nb_bufs;
557
558         /* reassemble any packets that need reassembly*/
559         unsigned i = 0;
560         if (rxq->pkt_first_seg == NULL) {
561                 /* find the first split flag, and only reassemble then*/
562                 while (i < nb_bufs && !split_flags[i])
563                         i++;
564                 if (i == nb_bufs)
565                         return nb_bufs;
566         }
567         return i + reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
568                 &split_flags[i]);
569 }
570
571 static inline void
572 vtx1(volatile union ixgbe_adv_tx_desc *txdp,
573                 struct rte_mbuf *pkt, uint64_t flags)
574 {
575         __m128i descriptor = _mm_set_epi64x((uint64_t)pkt->pkt_len << 46 |
576                         flags | pkt->data_len,
577                         pkt->buf_physaddr + pkt->data_off);
578         _mm_store_si128((__m128i *)&txdp->read, descriptor);
579 }
580
581 static inline void
582 vtx(volatile union ixgbe_adv_tx_desc *txdp,
583                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
584 {
585         int i;
586         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
587                 vtx1(txdp, *pkt, flags);
588 }
589
590 static inline int __attribute__((always_inline))
591 ixgbe_tx_free_bufs(struct ixgbe_tx_queue *txq)
592 {
593         struct ixgbe_tx_entry_v *txep;
594         uint32_t status;
595         uint32_t n;
596         uint32_t i;
597         int nb_free = 0;
598         struct rte_mbuf *m, *free[RTE_IXGBE_TX_MAX_FREE_BUF_SZ];
599
600         /* check DD bit on threshold descriptor */
601         status = txq->tx_ring[txq->tx_next_dd].wb.status;
602         if (!(status & IXGBE_ADVTXD_STAT_DD))
603                 return 0;
604
605         n = txq->tx_rs_thresh;
606
607         /*
608          * first buffer to free from S/W ring is at index
609          * tx_next_dd - (tx_rs_thresh-1)
610          */
611         txep = &((struct ixgbe_tx_entry_v *)txq->sw_ring)[txq->tx_next_dd -
612                         (n - 1)];
613         m = __rte_pktmbuf_prefree_seg(txep[0].mbuf);
614         if (likely(m != NULL)) {
615                 free[0] = m;
616                 nb_free = 1;
617                 for (i = 1; i < n; i++) {
618                         m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
619                         if (likely(m != NULL)) {
620                                 if (likely(m->pool == free[0]->pool))
621                                         free[nb_free++] = m;
622                                 else {
623                                         rte_mempool_put_bulk(free[0]->pool,
624                                                         (void *)free, nb_free);
625                                         free[0] = m;
626                                         nb_free = 1;
627                                 }
628                         }
629                 }
630                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
631         } else {
632                 for (i = 1; i < n; i++) {
633                         m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
634                         if (m != NULL)
635                                 rte_mempool_put(m->pool, m);
636                 }
637         }
638
639         /* buffers were freed, update counters */
640         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_rs_thresh);
641         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_rs_thresh);
642         if (txq->tx_next_dd >= txq->nb_tx_desc)
643                 txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
644
645         return txq->tx_rs_thresh;
646 }
647
648 static inline void __attribute__((always_inline))
649 tx_backlog_entry(struct ixgbe_tx_entry_v *txep,
650                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
651 {
652         int i;
653         for (i = 0; i < (int)nb_pkts; ++i)
654                 txep[i].mbuf = tx_pkts[i];
655 }
656
657 uint16_t
658 ixgbe_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
659                        uint16_t nb_pkts)
660 {
661         struct ixgbe_tx_queue *txq = (struct ixgbe_tx_queue *)tx_queue;
662         volatile union ixgbe_adv_tx_desc *txdp;
663         struct ixgbe_tx_entry_v *txep;
664         uint16_t n, nb_commit, tx_id;
665         uint64_t flags = DCMD_DTYP_FLAGS;
666         uint64_t rs = IXGBE_ADVTXD_DCMD_RS|DCMD_DTYP_FLAGS;
667         int i;
668
669         if (unlikely(nb_pkts > RTE_IXGBE_VPMD_TX_BURST))
670                 nb_pkts = RTE_IXGBE_VPMD_TX_BURST;
671
672         if (txq->nb_tx_free < txq->tx_free_thresh)
673                 ixgbe_tx_free_bufs(txq);
674
675         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
676         if (unlikely(nb_pkts == 0))
677                 return 0;
678
679         tx_id = txq->tx_tail;
680         txdp = &txq->tx_ring[tx_id];
681         txep = &((struct ixgbe_tx_entry_v *)txq->sw_ring)[tx_id];
682
683         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
684
685         n = (uint16_t)(txq->nb_tx_desc - tx_id);
686         if (nb_commit >= n) {
687
688                 tx_backlog_entry(txep, tx_pkts, n);
689
690                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
691                         vtx1(txdp, *tx_pkts, flags);
692
693                 vtx1(txdp, *tx_pkts++, rs);
694
695                 nb_commit = (uint16_t)(nb_commit - n);
696
697                 tx_id = 0;
698                 txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
699
700                 /* avoid reach the end of ring */
701                 txdp = &(txq->tx_ring[tx_id]);
702                 txep = &(((struct ixgbe_tx_entry_v *)txq->sw_ring)[tx_id]);
703         }
704
705         tx_backlog_entry(txep, tx_pkts, nb_commit);
706
707         vtx(txdp, tx_pkts, nb_commit, flags);
708
709         tx_id = (uint16_t)(tx_id + nb_commit);
710         if (tx_id > txq->tx_next_rs) {
711                 txq->tx_ring[txq->tx_next_rs].read.cmd_type_len |=
712                         rte_cpu_to_le_32(IXGBE_ADVTXD_DCMD_RS);
713                 txq->tx_next_rs = (uint16_t)(txq->tx_next_rs +
714                         txq->tx_rs_thresh);
715         }
716
717         txq->tx_tail = tx_id;
718
719         IXGBE_PCI_REG_WRITE(txq->tdt_reg_addr, txq->tx_tail);
720
721         return nb_pkts;
722 }
723
724 static void __attribute__((cold))
725 ixgbe_tx_queue_release_mbufs(struct ixgbe_tx_queue *txq)
726 {
727         unsigned i;
728         struct ixgbe_tx_entry_v *txe;
729         const uint16_t max_desc = (uint16_t)(txq->nb_tx_desc - 1);
730
731         if (txq->sw_ring == NULL || txq->nb_tx_free == max_desc)
732                 return;
733
734         /* release the used mbufs in sw_ring */
735         for (i = txq->tx_next_dd - (txq->tx_rs_thresh - 1);
736              i != txq->tx_tail;
737              i = (i + 1) & max_desc) {
738                 txe = &((struct ixgbe_tx_entry_v *)txq->sw_ring)[i];
739                 rte_pktmbuf_free_seg(txe->mbuf);
740         }
741         txq->nb_tx_free = max_desc;
742
743         /* reset tx_entry */
744         for (i = 0; i < txq->nb_tx_desc; i++) {
745                 txe = (struct ixgbe_tx_entry_v *)&txq->sw_ring[i];
746                 txe->mbuf = NULL;
747         }
748 }
749
750 void __attribute__((cold))
751 ixgbe_rx_queue_release_mbufs_vec(struct ixgbe_rx_queue *rxq)
752 {
753         const unsigned mask = rxq->nb_rx_desc - 1;
754         unsigned i;
755
756         if (rxq->sw_ring == NULL || rxq->rxrearm_nb >= rxq->nb_rx_desc)
757                 return;
758
759         /* free all mbufs that are valid in the ring */
760         for (i = rxq->rx_tail; i != rxq->rxrearm_start; i = (i + 1) & mask)
761                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
762         rxq->rxrearm_nb = rxq->nb_rx_desc;
763
764         /* set all entries to NULL */
765         memset(rxq->sw_ring, 0, sizeof(rxq->sw_ring[0]) * rxq->nb_rx_desc);
766 }
767
768 static void __attribute__((cold))
769 ixgbe_tx_free_swring(struct ixgbe_tx_queue *txq)
770 {
771         if (txq == NULL)
772                 return;
773
774         if (txq->sw_ring != NULL) {
775                 rte_free((struct ixgbe_rx_entry *)txq->sw_ring - 1);
776                 txq->sw_ring = NULL;
777         }
778 }
779
780 static void __attribute__((cold))
781 ixgbe_reset_tx_queue(struct ixgbe_tx_queue *txq)
782 {
783         static const union ixgbe_adv_tx_desc zeroed_desc = {{0}};
784         struct ixgbe_tx_entry_v *txe = (struct ixgbe_tx_entry_v *)txq->sw_ring;
785         uint16_t i;
786
787         /* Zero out HW ring memory */
788         for (i = 0; i < txq->nb_tx_desc; i++)
789                 txq->tx_ring[i] = zeroed_desc;
790
791         /* Initialize SW ring entries */
792         for (i = 0; i < txq->nb_tx_desc; i++) {
793                 volatile union ixgbe_adv_tx_desc *txd = &txq->tx_ring[i];
794                 txd->wb.status = IXGBE_TXD_STAT_DD;
795                 txe[i].mbuf = NULL;
796         }
797
798         txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
799         txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
800
801         txq->tx_tail = 0;
802         txq->nb_tx_used = 0;
803         /*
804          * Always allow 1 descriptor to be un-allocated to avoid
805          * a H/W race condition
806          */
807         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
808         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
809         txq->ctx_curr = 0;
810         memset((void *)&txq->ctx_cache, 0,
811                 IXGBE_CTX_NUM * sizeof(struct ixgbe_advctx_info));
812 }
813
814 static const struct ixgbe_txq_ops vec_txq_ops = {
815         .release_mbufs = ixgbe_tx_queue_release_mbufs,
816         .free_swring = ixgbe_tx_free_swring,
817         .reset = ixgbe_reset_tx_queue,
818 };
819
820 int __attribute__((cold))
821 ixgbe_rxq_vec_setup(struct ixgbe_rx_queue *rxq)
822 {
823         uintptr_t p;
824         struct rte_mbuf mb_def = { .buf_addr = 0 }; /* zeroed mbuf */
825
826         mb_def.nb_segs = 1;
827         mb_def.data_off = RTE_PKTMBUF_HEADROOM;
828         mb_def.port = rxq->port_id;
829         rte_mbuf_refcnt_set(&mb_def, 1);
830
831         /* prevent compiler reordering: rearm_data covers previous fields */
832         rte_compiler_barrier();
833         p = (uintptr_t)&mb_def.rearm_data;
834         rxq->mbuf_initializer = *(uint64_t *)p;
835         return 0;
836 }
837
838 int __attribute__((cold))
839 ixgbe_txq_vec_setup(struct ixgbe_tx_queue *txq)
840 {
841         if (txq->sw_ring == NULL)
842                 return -1;
843
844         /* leave the first one for overflow */
845         txq->sw_ring = (struct ixgbe_tx_entry *)
846                 ((struct ixgbe_tx_entry_v *)txq->sw_ring + 1);
847         txq->ops = &vec_txq_ops;
848
849         return 0;
850 }
851
852 int __attribute__((cold))
853 ixgbe_rx_vec_dev_conf_condition_check(struct rte_eth_dev *dev)
854 {
855 #ifndef RTE_LIBRTE_IEEE1588
856         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
857         struct rte_fdir_conf *fconf = &dev->data->dev_conf.fdir_conf;
858
859 #ifndef RTE_IXGBE_RX_OLFLAGS_ENABLE
860         /* whithout rx ol_flags, no VP flag report */
861         if (rxmode->hw_vlan_strip != 0 ||
862             rxmode->hw_vlan_extend != 0)
863                 return -1;
864 #endif
865
866         /* no fdir support */
867         if (fconf->mode != RTE_FDIR_MODE_NONE)
868                 return -1;
869
870         /*
871          * - no csum error report support
872          * - no header split support
873          */
874         if (rxmode->hw_ip_checksum == 1 ||
875             rxmode->header_split == 1)
876                 return -1;
877
878         return 0;
879 #else
880         RTE_SET_USED(dev);
881         return -1;
882 #endif
883 }