ixgbe: improve slow-path perf with vector scattered Rx
[dpdk.git] / lib / librte_pmd_ixgbe / ixgbe_rxtx_vec.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 igb_rx_queue *rxq)
49 {
50         int i;
51         uint16_t rx_id;
52         volatile union ixgbe_adv_rx_desc *rxdp;
53         struct igb_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
58         /* Pull 'n' more MBUFs into the software ring */
59         if (rte_mempool_get_bulk(rxq->mb_pool,
60                                  (void *)rxep, RTE_IXGBE_RXQ_REARM_THRESH) < 0)
61                 return;
62
63         rxdp = rxq->rx_ring + rxq->rxrearm_start;
64
65         /* Initialize the mbufs in vector, process 2 mbufs in one loop */
66         for (i = 0; i < RTE_IXGBE_RXQ_REARM_THRESH; i += 2, rxep += 2) {
67                 __m128i dma_addr0, dma_addr1;
68                 __m128i vaddr0, vaddr1;
69
70                 mb0 = rxep[0].mbuf;
71                 mb1 = rxep[1].mbuf;
72
73                 /* flush mbuf with pkt template */
74                 mb0->rearm_data[0] = rxq->mbuf_initializer;
75                 mb1->rearm_data[0] = rxq->mbuf_initializer;
76
77                 /* load buf_addr(lo 64bit) and buf_physaddr(hi 64bit) */
78                 vaddr0 = _mm_loadu_si128((__m128i *)&(mb0->buf_addr));
79                 vaddr1 = _mm_loadu_si128((__m128i *)&(mb1->buf_addr));
80
81                 /* convert pa to dma_addr hdr/data */
82                 dma_addr0 = _mm_unpackhi_epi64(vaddr0, vaddr0);
83                 dma_addr1 = _mm_unpackhi_epi64(vaddr1, vaddr1);
84
85                 /* add headroom to pa values */
86                 dma_addr0 = _mm_add_epi64(dma_addr0, hdr_room);
87                 dma_addr1 = _mm_add_epi64(dma_addr1, hdr_room);
88
89                 /* flush desc with pa dma_addr */
90                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr0);
91                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr1);
92         }
93
94         rxq->rxrearm_start += RTE_IXGBE_RXQ_REARM_THRESH;
95         if (rxq->rxrearm_start >= rxq->nb_rx_desc)
96                 rxq->rxrearm_start = 0;
97
98         rxq->rxrearm_nb -= RTE_IXGBE_RXQ_REARM_THRESH;
99
100         rx_id = (uint16_t) ((rxq->rxrearm_start == 0) ?
101                              (rxq->nb_rx_desc - 1) : (rxq->rxrearm_start - 1));
102
103         /* Update the tail pointer on the NIC */
104         IXGBE_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
105 }
106
107 /* Handling the offload flags (olflags) field takes computation
108  * time when receiving packets. Therefore we provide a flag to disable
109  * the processing of the olflags field when they are not needed. This
110  * gives improved performance, at the cost of losing the offload info
111  * in the received packet
112  */
113 #ifdef RTE_IXGBE_RX_OLFLAGS_ENABLE
114
115 #define OLFLAGS_MASK     ((uint16_t)(PKT_RX_VLAN_PKT | PKT_RX_IPV4_HDR |\
116                                      PKT_RX_IPV4_HDR_EXT | PKT_RX_IPV6_HDR |\
117                                      PKT_RX_IPV6_HDR_EXT))
118 #define OLFLAGS_MASK_V   (((uint64_t)OLFLAGS_MASK << 48) | \
119                           ((uint64_t)OLFLAGS_MASK << 32) | \
120                           ((uint64_t)OLFLAGS_MASK << 16) | \
121                           ((uint64_t)OLFLAGS_MASK))
122 #define PTYPE_SHIFT    (1)
123 #define VTAG_SHIFT     (3)
124
125 static inline void
126 desc_to_olflags_v(__m128i descs[4], struct rte_mbuf **rx_pkts)
127 {
128         __m128i ptype0, ptype1, vtag0, vtag1;
129         union {
130                 uint16_t e[4];
131                 uint64_t dword;
132         } vol;
133
134         ptype0 = _mm_unpacklo_epi16(descs[0], descs[1]);
135         ptype1 = _mm_unpacklo_epi16(descs[2], descs[3]);
136         vtag0 = _mm_unpackhi_epi16(descs[0], descs[1]);
137         vtag1 = _mm_unpackhi_epi16(descs[2], descs[3]);
138
139         ptype1 = _mm_unpacklo_epi32(ptype0, ptype1);
140         vtag1 = _mm_unpacklo_epi32(vtag0, vtag1);
141
142         ptype1 = _mm_slli_epi16(ptype1, PTYPE_SHIFT);
143         vtag1 = _mm_srli_epi16(vtag1, VTAG_SHIFT);
144
145         ptype1 = _mm_or_si128(ptype1, vtag1);
146         vol.dword = _mm_cvtsi128_si64(ptype1) & OLFLAGS_MASK_V;
147
148         rx_pkts[0]->ol_flags = vol.e[0];
149         rx_pkts[1]->ol_flags = vol.e[1];
150         rx_pkts[2]->ol_flags = vol.e[2];
151         rx_pkts[3]->ol_flags = vol.e[3];
152 }
153 #else
154 #define desc_to_olflags_v(desc, rx_pkts) do {} while (0)
155 #endif
156
157 /*
158  * vPMD receive routine, now only accept (nb_pkts == RTE_IXGBE_VPMD_RX_BURST)
159  * in one loop
160  *
161  * Notice:
162  * - nb_pkts < RTE_IXGBE_VPMD_RX_BURST, just return no packet
163  * - nb_pkts > RTE_IXGBE_VPMD_RX_BURST, only scan RTE_IXGBE_VPMD_RX_BURST
164  *   numbers of DD bit
165  * - don't support ol_flags for rss and csum err
166  */
167 static inline uint16_t
168 _recv_raw_pkts_vec(struct igb_rx_queue *rxq, struct rte_mbuf **rx_pkts,
169                 uint16_t nb_pkts, uint8_t *split_packet)
170 {
171         volatile union ixgbe_adv_rx_desc *rxdp;
172         struct igb_rx_entry *sw_ring;
173         uint16_t nb_pkts_recd;
174         int pos;
175         uint64_t var;
176         __m128i shuf_msk;
177         __m128i crc_adjust = _mm_set_epi16(
178                                 0, 0, 0, 0, /* ignore non-length fields */
179                                 0,          /* ignore high-16bits of pkt_len */
180                                 -rxq->crc_len, /* sub crc on pkt_len */
181                                 -rxq->crc_len, /* sub crc on data_len */
182                                 0            /* ignore pkt_type field */
183                         );
184         __m128i dd_check, eop_check;
185
186         if (unlikely(nb_pkts < RTE_IXGBE_VPMD_RX_BURST))
187                 return 0;
188
189         /* Just the act of getting into the function from the application is
190          * going to cost about 7 cycles */
191         rxdp = rxq->rx_ring + rxq->rx_tail;
192
193         _mm_prefetch((const void *)rxdp, _MM_HINT_T0);
194
195         /* See if we need to rearm the RX queue - gives the prefetch a bit
196          * of time to act */
197         if (rxq->rxrearm_nb > RTE_IXGBE_RXQ_REARM_THRESH)
198                 ixgbe_rxq_rearm(rxq);
199
200         /* Before we start moving massive data around, check to see if
201          * there is actually a packet available */
202         if (!(rxdp->wb.upper.status_error &
203                                 rte_cpu_to_le_32(IXGBE_RXDADV_STAT_DD)))
204                 return 0;
205
206         /* 4 packets DD mask */
207         dd_check = _mm_set_epi64x(0x0000000100000001LL, 0x0000000100000001LL);
208
209         /* 4 packets EOP mask */
210         eop_check = _mm_set_epi64x(0x0000000200000002LL, 0x0000000200000002LL);
211
212         /* mask to shuffle from desc. to mbuf */
213         shuf_msk = _mm_set_epi8(
214                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
215                 0xFF, 0xFF,  /* skip high 16 bits vlan_macip, zero out */
216                 15, 14,      /* octet 14~15, low 16 bits vlan_macip */
217                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
218                 13, 12,      /* octet 12~13, low 16 bits pkt_len */
219                 13, 12,      /* octet 12~13, 16 bits data_len */
220                 0xFF, 0xFF   /* skip pkt_type field */
221                 );
222
223         /* Cache is empty -> need to scan the buffer rings, but first move
224          * the next 'n' mbufs into the cache */
225         sw_ring = &rxq->sw_ring[rxq->rx_tail];
226
227         /*
228          * A. load 4 packet in one loop
229          * B. copy 4 mbuf point from swring to rx_pkts
230          * C. calc the number of DD bits among the 4 packets
231          * [C*. extract the end-of-packet bit, if requested]
232          * D. fill info. from desc to mbuf
233          */
234         for (pos = 0, nb_pkts_recd = 0; pos < RTE_IXGBE_VPMD_RX_BURST;
235                         pos += RTE_IXGBE_DESCS_PER_LOOP,
236                         rxdp += RTE_IXGBE_DESCS_PER_LOOP) {
237                 __m128i descs[RTE_IXGBE_DESCS_PER_LOOP];
238                 __m128i pkt_mb1, pkt_mb2, pkt_mb3, pkt_mb4;
239                 __m128i zero, staterr, sterr_tmp1, sterr_tmp2;
240                 __m128i mbp1, mbp2; /* two mbuf pointer in one XMM reg. */
241
242                 if (split_packet) {
243                         rte_prefetch0(&rx_pkts[pos]->cacheline1);
244                         rte_prefetch0(&rx_pkts[pos + 1]->cacheline1);
245                         rte_prefetch0(&rx_pkts[pos + 2]->cacheline1);
246                         rte_prefetch0(&rx_pkts[pos + 3]->cacheline1);
247                 }
248
249                 /* B.1 load 1 mbuf point */
250                 mbp1 = _mm_loadu_si128((__m128i *)&sw_ring[pos]);
251
252                 /* Read desc statuses backwards to avoid race condition */
253                 /* A.1 load 4 pkts desc */
254                 descs[3] = _mm_loadu_si128((__m128i *)(rxdp + 3));
255
256                 /* B.2 copy 2 mbuf point into rx_pkts  */
257                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
258
259                 /* B.1 load 1 mbuf point */
260                 mbp2 = _mm_loadu_si128((__m128i *)&sw_ring[pos+2]);
261
262                 descs[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
263                 /* B.1 load 2 mbuf point */
264                 descs[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
265                 descs[0] = _mm_loadu_si128((__m128i *)(rxdp));
266
267                 /* B.2 copy 2 mbuf point into rx_pkts  */
268                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
269
270                 /* avoid compiler reorder optimization */
271                 rte_compiler_barrier();
272
273                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
274                 pkt_mb4 = _mm_shuffle_epi8(descs[3], shuf_msk);
275                 pkt_mb3 = _mm_shuffle_epi8(descs[2], shuf_msk);
276
277                 /* C.1 4=>2 filter staterr info only */
278                 sterr_tmp2 = _mm_unpackhi_epi32(descs[3], descs[2]);
279                 /* C.1 4=>2 filter staterr info only */
280                 sterr_tmp1 = _mm_unpackhi_epi32(descs[1], descs[0]);
281
282                 /* set ol_flags with packet type and vlan tag */
283                 desc_to_olflags_v(descs, &rx_pkts[pos]);
284
285                 /* D.2 pkt 3,4 set in_port/nb_seg and remove crc */
286                 pkt_mb4 = _mm_add_epi16(pkt_mb4, crc_adjust);
287                 pkt_mb3 = _mm_add_epi16(pkt_mb3, crc_adjust);
288
289                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
290                 pkt_mb2 = _mm_shuffle_epi8(descs[1], shuf_msk);
291                 pkt_mb1 = _mm_shuffle_epi8(descs[0], shuf_msk);
292
293                 /* C.2 get 4 pkts staterr value  */
294                 zero = _mm_xor_si128(dd_check, dd_check);
295                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
296
297                 /* D.3 copy final 3,4 data to rx_pkts */
298                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
299                                 pkt_mb4);
300                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
301                                 pkt_mb3);
302
303                 /* D.2 pkt 1,2 set in_port/nb_seg and remove crc */
304                 pkt_mb2 = _mm_add_epi16(pkt_mb2, crc_adjust);
305                 pkt_mb1 = _mm_add_epi16(pkt_mb1, crc_adjust);
306
307                 /* C* extract and record EOP bit */
308                 if (split_packet) {
309                         __m128i eop_shuf_mask = _mm_set_epi8(
310                                         0xFF, 0xFF, 0xFF, 0xFF,
311                                         0xFF, 0xFF, 0xFF, 0xFF,
312                                         0xFF, 0xFF, 0xFF, 0xFF,
313                                         0x04, 0x0C, 0x00, 0x08
314                                         );
315
316                         /* and with mask to extract bits, flipping 1-0 */
317                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
318                         /* the staterr values are not in order, as the count
319                          * count of dd bits doesn't care. However, for end of
320                          * packet tracking, we do care, so shuffle. This also
321                          * compresses the 32-bit values to 8-bit */
322                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
323                         /* store the resulting 32-bit value */
324                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
325                         split_packet += RTE_IXGBE_DESCS_PER_LOOP;
326
327                         /* zero-out next pointers */
328                         rx_pkts[pos]->next = NULL;
329                         rx_pkts[pos + 1]->next = NULL;
330                         rx_pkts[pos + 2]->next = NULL;
331                         rx_pkts[pos + 3]->next = NULL;
332                 }
333
334                 /* C.3 calc available number of desc */
335                 staterr = _mm_and_si128(staterr, dd_check);
336                 staterr = _mm_packs_epi32(staterr, zero);
337
338                 /* D.3 copy final 1,2 data to rx_pkts */
339                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
340                                 pkt_mb2);
341                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
342                                 pkt_mb1);
343
344                 /* C.4 calc avaialbe number of desc */
345                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
346                 nb_pkts_recd += var;
347                 if (likely(var != RTE_IXGBE_DESCS_PER_LOOP))
348                         break;
349         }
350
351         /* Update our internal tail pointer */
352         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_pkts_recd);
353         rxq->rx_tail = (uint16_t)(rxq->rx_tail & (rxq->nb_rx_desc - 1));
354         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
355
356         return nb_pkts_recd;
357 }
358
359 /*
360  * vPMD receive routine, now only accept (nb_pkts == RTE_IXGBE_VPMD_RX_BURST)
361  * in one loop
362  *
363  * Notice:
364  * - nb_pkts < RTE_IXGBE_VPMD_RX_BURST, just return no packet
365  * - nb_pkts > RTE_IXGBE_VPMD_RX_BURST, only scan RTE_IXGBE_VPMD_RX_BURST
366  *   numbers of DD bit
367  * - don't support ol_flags for rss and csum err
368  */
369 uint16_t
370 ixgbe_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
371                 uint16_t nb_pkts)
372 {
373         return _recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
374 }
375
376 static inline uint16_t
377 reassemble_packets(struct igb_rx_queue *rxq, struct rte_mbuf **rx_bufs,
378                 uint16_t nb_bufs, uint8_t *split_flags)
379 {
380         struct rte_mbuf *pkts[RTE_IXGBE_VPMD_RX_BURST]; /*finished pkts*/
381         struct rte_mbuf *start = rxq->pkt_first_seg;
382         struct rte_mbuf *end =  rxq->pkt_last_seg;
383         unsigned pkt_idx = 0, buf_idx = 0;
384
385
386         while (buf_idx < nb_bufs) {
387                 if (end != NULL) {
388                         /* processing a split packet */
389                         end->next = rx_bufs[buf_idx];
390                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
391
392                         start->nb_segs++;
393                         start->pkt_len += rx_bufs[buf_idx]->data_len;
394                         end = end->next;
395
396                         if (!split_flags[buf_idx]) {
397                                 /* it's the last packet of the set */
398                                 start->hash = end->hash;
399                                 start->ol_flags = end->ol_flags;
400                                 /* we need to strip crc for the whole packet */
401                                 start->pkt_len -= rxq->crc_len;
402                                 if (end->data_len > rxq->crc_len)
403                                         end->data_len -= rxq->crc_len;
404                                 else {
405                                         /* free up last mbuf */
406                                         struct rte_mbuf *secondlast = start;
407                                         while (secondlast->next != end)
408                                                 secondlast = secondlast->next;
409                                         secondlast->data_len -= (rxq->crc_len -
410                                                         end->data_len);
411                                         secondlast->next = NULL;
412                                         rte_pktmbuf_free_seg(end);
413                                         end = secondlast;
414                                 }
415                                 pkts[pkt_idx++] = start;
416                                 start = end = NULL;
417                         }
418                 } else {
419                         /* not processing a split packet */
420                         if (!split_flags[buf_idx]) {
421                                 /* not a split packet, save and skip */
422                                 pkts[pkt_idx++] = rx_bufs[buf_idx];
423                                 continue;
424                         }
425                         end = start = rx_bufs[buf_idx];
426                         rx_bufs[buf_idx]->data_len += rxq->crc_len;
427                         rx_bufs[buf_idx]->pkt_len += rxq->crc_len;
428                 }
429                 buf_idx++;
430         }
431
432         /* save the partial packet for next time */
433         rxq->pkt_first_seg = start;
434         rxq->pkt_last_seg = end;
435         memcpy(rx_bufs, pkts, pkt_idx * (sizeof(*pkts)));
436         return pkt_idx;
437 }
438
439 /*
440  * vPMD receive routine that reassembles scattered packets
441  *
442  * Notice:
443  * - don't support ol_flags for rss and csum err
444  * - now only accept (nb_pkts == RTE_IXGBE_VPMD_RX_BURST)
445  */
446 uint16_t
447 ixgbe_recv_scattered_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
448                 uint16_t nb_pkts)
449 {
450         struct igb_rx_queue *rxq = rx_queue;
451         uint8_t split_flags[RTE_IXGBE_VPMD_RX_BURST] = {0};
452
453         /* get some new buffers */
454         uint16_t nb_bufs = _recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
455                         split_flags);
456         if (nb_bufs == 0)
457                 return 0;
458
459         /* happy day case, full burst + no packets to be joined */
460         const uint32_t *split_fl32 = (uint32_t *)split_flags;
461         if (rxq->pkt_first_seg == NULL &&
462                         split_fl32[0] == 0 && split_fl32[1] == 0 &&
463                         split_fl32[2] == 0 && split_fl32[3] == 0)
464                 return nb_bufs;
465
466         /* reassemble any packets that need reassembly*/
467         unsigned i = 0;
468         if (rxq->pkt_first_seg == NULL) {
469                 /* find the first split flag, and only reassemble then*/
470                 while (!split_flags[i] && i < nb_bufs)
471                         i++;
472                 if (i == nb_bufs)
473                         return nb_bufs;
474         }
475         return i + reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
476                 &split_flags[i]);
477 }
478
479 static inline void
480 vtx1(volatile union ixgbe_adv_tx_desc *txdp,
481                 struct rte_mbuf *pkt, uint64_t flags)
482 {
483         __m128i descriptor = _mm_set_epi64x((uint64_t)pkt->pkt_len << 46 |
484                         flags | pkt->data_len,
485                         pkt->buf_physaddr + pkt->data_off);
486         _mm_store_si128((__m128i *)&txdp->read, descriptor);
487 }
488
489 static inline void
490 vtx(volatile union ixgbe_adv_tx_desc *txdp,
491                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
492 {
493         int i;
494         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
495                 vtx1(txdp, *pkt, flags);
496 }
497
498 static inline int __attribute__((always_inline))
499 ixgbe_tx_free_bufs(struct igb_tx_queue *txq)
500 {
501         struct igb_tx_entry_v *txep;
502         uint32_t status;
503         uint32_t n;
504         uint32_t i;
505         int nb_free = 0;
506         struct rte_mbuf *m, *free[RTE_IXGBE_TX_MAX_FREE_BUF_SZ];
507
508         /* check DD bit on threshold descriptor */
509         status = txq->tx_ring[txq->tx_next_dd].wb.status;
510         if (!(status & IXGBE_ADVTXD_STAT_DD))
511                 return 0;
512
513         n = txq->tx_rs_thresh;
514
515         /*
516          * first buffer to free from S/W ring is at index
517          * tx_next_dd - (tx_rs_thresh-1)
518          */
519         txep = &((struct igb_tx_entry_v *)txq->sw_ring)[txq->tx_next_dd -
520                         (n - 1)];
521 #ifdef RTE_MBUF_REFCNT
522         m = __rte_pktmbuf_prefree_seg(txep[0].mbuf);
523 #else
524         m = txep[0].mbuf;
525 #endif
526         if (likely(m != NULL)) {
527                 free[0] = m;
528                 nb_free = 1;
529                 for (i = 1; i < n; i++) {
530 #ifdef RTE_MBUF_REFCNT
531                         m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
532 #else
533                         m = txep[i]->mbuf;
534 #endif
535                         if (likely(m != NULL)) {
536                                 if (likely(m->pool == free[0]->pool))
537                                         free[nb_free++] = m;
538                                 else {
539                                         rte_mempool_put_bulk(free[0]->pool,
540                                                         (void *)free, nb_free);
541                                         free[0] = m;
542                                         nb_free = 1;
543                                 }
544                         }
545                 }
546                 rte_mempool_put_bulk(free[0]->pool, (void **)free, nb_free);
547         } else {
548                 for (i = 1; i < n; i++) {
549                         m = __rte_pktmbuf_prefree_seg(txep[i].mbuf);
550                         if (m != NULL)
551                                 rte_mempool_put(m->pool, m);
552                 }
553         }
554
555         /* buffers were freed, update counters */
556         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + txq->tx_rs_thresh);
557         txq->tx_next_dd = (uint16_t)(txq->tx_next_dd + txq->tx_rs_thresh);
558         if (txq->tx_next_dd >= txq->nb_tx_desc)
559                 txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
560
561         return txq->tx_rs_thresh;
562 }
563
564 static inline void __attribute__((always_inline))
565 tx_backlog_entry(struct igb_tx_entry_v *txep,
566                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
567 {
568         int i;
569         for (i = 0; i < (int)nb_pkts; ++i)
570                 txep[i].mbuf = tx_pkts[i];
571 }
572
573 uint16_t
574 ixgbe_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
575                        uint16_t nb_pkts)
576 {
577         struct igb_tx_queue *txq = (struct igb_tx_queue *)tx_queue;
578         volatile union ixgbe_adv_tx_desc *txdp;
579         struct igb_tx_entry_v *txep;
580         uint16_t n, nb_commit, tx_id;
581         uint64_t flags = DCMD_DTYP_FLAGS;
582         uint64_t rs = IXGBE_ADVTXD_DCMD_RS|DCMD_DTYP_FLAGS;
583         int i;
584
585         if (unlikely(nb_pkts > RTE_IXGBE_VPMD_TX_BURST))
586                 nb_pkts = RTE_IXGBE_VPMD_TX_BURST;
587
588         if (txq->nb_tx_free < txq->tx_free_thresh)
589                 ixgbe_tx_free_bufs(txq);
590
591         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
592         if (unlikely(nb_pkts == 0))
593                 return 0;
594
595         tx_id = txq->tx_tail;
596         txdp = &txq->tx_ring[tx_id];
597         txep = &((struct igb_tx_entry_v *)txq->sw_ring)[tx_id];
598
599         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
600
601         n = (uint16_t)(txq->nb_tx_desc - tx_id);
602         if (nb_commit >= n) {
603
604                 tx_backlog_entry(txep, tx_pkts, n);
605
606                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
607                         vtx1(txdp, *tx_pkts, flags);
608
609                 vtx1(txdp, *tx_pkts++, rs);
610
611                 nb_commit = (uint16_t)(nb_commit - n);
612
613                 tx_id = 0;
614                 txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
615
616                 /* avoid reach the end of ring */
617                 txdp = &(txq->tx_ring[tx_id]);
618                 txep = &(((struct igb_tx_entry_v *)txq->sw_ring)[tx_id]);
619         }
620
621         tx_backlog_entry(txep, tx_pkts, nb_commit);
622
623         vtx(txdp, tx_pkts, nb_commit, flags);
624
625         tx_id = (uint16_t)(tx_id + nb_commit);
626         if (tx_id > txq->tx_next_rs) {
627                 txq->tx_ring[txq->tx_next_rs].read.cmd_type_len |=
628                         rte_cpu_to_le_32(IXGBE_ADVTXD_DCMD_RS);
629                 txq->tx_next_rs = (uint16_t)(txq->tx_next_rs +
630                         txq->tx_rs_thresh);
631         }
632
633         txq->tx_tail = tx_id;
634
635         IXGBE_PCI_REG_WRITE(txq->tdt_reg_addr, txq->tx_tail);
636
637         return nb_pkts;
638 }
639
640 static void
641 ixgbe_tx_queue_release_mbufs(struct igb_tx_queue *txq)
642 {
643         unsigned i;
644         struct igb_tx_entry_v *txe;
645         uint16_t nb_free, max_desc;
646
647         if (txq->sw_ring != NULL) {
648                 /* release the used mbufs in sw_ring */
649                 nb_free = txq->nb_tx_free;
650                 max_desc = (uint16_t)(txq->nb_tx_desc - 1);
651                 for (i = txq->tx_next_dd - (txq->tx_rs_thresh - 1);
652                      nb_free < max_desc && i != txq->tx_tail;
653                      i = (i + 1) & max_desc) {
654                         txe = (struct igb_tx_entry_v *)&txq->sw_ring[i];
655                         if (txe->mbuf != NULL)
656                                 rte_pktmbuf_free_seg(txe->mbuf);
657                 }
658                 /* reset tx_entry */
659                 for (i = 0; i < txq->nb_tx_desc; i++) {
660                         txe = (struct igb_tx_entry_v *)&txq->sw_ring[i];
661                         txe->mbuf = NULL;
662                 }
663         }
664 }
665
666 static void
667 ixgbe_tx_free_swring(struct igb_tx_queue *txq)
668 {
669         if (txq == NULL)
670                 return;
671
672         if (txq->sw_ring != NULL) {
673                 rte_free((struct igb_rx_entry *)txq->sw_ring - 1);
674                 txq->sw_ring = NULL;
675         }
676 }
677
678 static void
679 ixgbe_reset_tx_queue(struct igb_tx_queue *txq)
680 {
681         static const union ixgbe_adv_tx_desc zeroed_desc = { .read = {
682                         .buffer_addr = 0} };
683         struct igb_tx_entry_v *txe = (struct igb_tx_entry_v *)txq->sw_ring;
684         uint16_t i;
685
686         /* Zero out HW ring memory */
687         for (i = 0; i < txq->nb_tx_desc; i++)
688                 txq->tx_ring[i] = zeroed_desc;
689
690         /* Initialize SW ring entries */
691         for (i = 0; i < txq->nb_tx_desc; i++) {
692                 volatile union ixgbe_adv_tx_desc *txd = &txq->tx_ring[i];
693                 txd->wb.status = IXGBE_TXD_STAT_DD;
694                 txe[i].mbuf = NULL;
695         }
696
697         txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
698         txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
699
700         txq->tx_tail = 0;
701         txq->nb_tx_used = 0;
702         /*
703          * Always allow 1 descriptor to be un-allocated to avoid
704          * a H/W race condition
705          */
706         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
707         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
708         txq->ctx_curr = 0;
709         memset((void *)&txq->ctx_cache, 0,
710                 IXGBE_CTX_NUM * sizeof(struct ixgbe_advctx_info));
711 }
712
713 static struct ixgbe_txq_ops vec_txq_ops = {
714         .release_mbufs = ixgbe_tx_queue_release_mbufs,
715         .free_swring = ixgbe_tx_free_swring,
716         .reset = ixgbe_reset_tx_queue,
717 };
718
719 int
720 ixgbe_rxq_vec_setup(struct igb_rx_queue *rxq)
721 {
722         static struct rte_mbuf mb_def = {
723                 .nb_segs = 1,
724                 .data_off = RTE_PKTMBUF_HEADROOM,
725 #ifdef RTE_MBUF_REFCNT
726                 .refcnt = 1,
727 #endif
728         };
729
730         mb_def.buf_len = rxq->mb_pool->elt_size - sizeof(struct rte_mbuf);
731         mb_def.port = rxq->port_id;
732         rxq->mbuf_initializer = *((uint64_t *)&mb_def.rearm_data);
733         return 0;
734 }
735
736 int ixgbe_txq_vec_setup(struct igb_tx_queue *txq)
737 {
738         if (txq->sw_ring == NULL)
739                 return -1;
740
741         /* leave the first one for overflow */
742         txq->sw_ring = (struct igb_tx_entry *)
743                 ((struct igb_tx_entry_v *)txq->sw_ring + 1);
744         txq->ops = &vec_txq_ops;
745
746         return 0;
747 }
748
749 int ixgbe_rx_vec_condition_check(struct rte_eth_dev *dev)
750 {
751 #ifndef RTE_LIBRTE_IEEE1588
752         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
753         struct rte_fdir_conf *fconf = &dev->data->dev_conf.fdir_conf;
754
755 #ifndef RTE_IXGBE_RX_OLFLAGS_ENABLE
756         /* whithout rx ol_flags, no VP flag report */
757         if (rxmode->hw_vlan_strip != 0 ||
758             rxmode->hw_vlan_extend != 0)
759                 return -1;
760 #endif
761
762         /* no fdir support */
763         if (fconf->mode != RTE_FDIR_MODE_NONE)
764                 return -1;
765
766         /*
767          * - no csum error report support
768          * - no header split support
769          */
770         if (rxmode->hw_ip_checksum == 1 ||
771             rxmode->header_split == 1)
772                 return -1;
773
774         return 0;
775 #else
776         RTE_SET_USED(dev);
777         return -1;
778 #endif
779 }