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