b65220f1bb1cb49cecb9c05df8abf0a6e7c54b31
[dpdk.git] / drivers / net / ixgbe / ixgbe_rxtx_vec_sse.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 #include "ixgbe_rxtx_vec_common.h"
41
42 #include <tmmintrin.h>
43
44 #ifndef __INTEL_COMPILER
45 #pragma GCC diagnostic ignored "-Wcast-qual"
46 #endif
47
48 static inline void
49 ixgbe_rxq_rearm(struct ixgbe_rx_queue *rxq)
50 {
51         int i;
52         uint16_t rx_id;
53         volatile union ixgbe_adv_rx_desc *rxdp;
54         struct ixgbe_rx_entry *rxep = &rxq->sw_ring[rxq->rxrearm_start];
55         struct rte_mbuf *mb0, *mb1;
56         __m128i hdr_room = _mm_set_epi64x(RTE_PKTMBUF_HEADROOM,
57                         RTE_PKTMBUF_HEADROOM);
58         __m128i dma_addr0, dma_addr1;
59
60         const __m128i hba_msk = _mm_set_epi64x(0, UINT64_MAX);
61
62         rxdp = rxq->rx_ring + rxq->rxrearm_start;
63
64         /* Pull 'n' more MBUFs into the software ring */
65         if (rte_mempool_get_bulk(rxq->mb_pool,
66                                  (void *)rxep,
67                                  RTE_IXGBE_RXQ_REARM_THRESH) < 0) {
68                 if (rxq->rxrearm_nb + RTE_IXGBE_RXQ_REARM_THRESH >=
69                     rxq->nb_rx_desc) {
70                         dma_addr0 = _mm_setzero_si128();
71                         for (i = 0; i < RTE_IXGBE_DESCS_PER_LOOP; i++) {
72                                 rxep[i].mbuf = &rxq->fake_mbuf;
73                                 _mm_store_si128((__m128i *)&rxdp[i].read,
74                                                 dma_addr0);
75                         }
76                 }
77                 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed +=
78                         RTE_IXGBE_RXQ_REARM_THRESH;
79                 return;
80         }
81
82         /* Initialize the mbufs in vector, process 2 mbufs in one loop */
83         for (i = 0; i < RTE_IXGBE_RXQ_REARM_THRESH; i += 2, rxep += 2) {
84                 __m128i vaddr0, vaddr1;
85
86                 mb0 = rxep[0].mbuf;
87                 mb1 = rxep[1].mbuf;
88
89                 /* load buf_addr(lo 64bit) and buf_physaddr(hi 64bit) */
90                 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, buf_physaddr) !=
91                                 offsetof(struct rte_mbuf, buf_addr) + 8);
92                 vaddr0 = _mm_loadu_si128((__m128i *)&(mb0->buf_addr));
93                 vaddr1 = _mm_loadu_si128((__m128i *)&(mb1->buf_addr));
94
95                 /* convert pa to dma_addr hdr/data */
96                 dma_addr0 = _mm_unpackhi_epi64(vaddr0, vaddr0);
97                 dma_addr1 = _mm_unpackhi_epi64(vaddr1, vaddr1);
98
99                 /* add headroom to pa values */
100                 dma_addr0 = _mm_add_epi64(dma_addr0, hdr_room);
101                 dma_addr1 = _mm_add_epi64(dma_addr1, hdr_room);
102
103                 /* set Header Buffer Address to zero */
104                 dma_addr0 =  _mm_and_si128(dma_addr0, hba_msk);
105                 dma_addr1 =  _mm_and_si128(dma_addr1, hba_msk);
106
107                 /* flush desc with pa dma_addr */
108                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr0);
109                 _mm_store_si128((__m128i *)&rxdp++->read, dma_addr1);
110         }
111
112         rxq->rxrearm_start += RTE_IXGBE_RXQ_REARM_THRESH;
113         if (rxq->rxrearm_start >= rxq->nb_rx_desc)
114                 rxq->rxrearm_start = 0;
115
116         rxq->rxrearm_nb -= RTE_IXGBE_RXQ_REARM_THRESH;
117
118         rx_id = (uint16_t) ((rxq->rxrearm_start == 0) ?
119                              (rxq->nb_rx_desc - 1) : (rxq->rxrearm_start - 1));
120
121         /* Update the tail pointer on the NIC */
122         IXGBE_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
123 }
124
125 static inline void
126 desc_to_olflags_v_ipsec(__m128i descs[4], struct rte_mbuf **rx_pkts)
127 {
128         __m128i sterr0, sterr1, sterr2, sterr3;
129         __m128i tmp1, tmp2, tmp3, tmp4;
130         __m128i rearm0, rearm1, rearm2, rearm3;
131
132         const __m128i ipsec_sterr_msk = _mm_set_epi32(
133                 0, IXGBE_RXDADV_IPSEC_STATUS_SECP |
134                         IXGBE_RXDADV_IPSEC_ERROR_AUTH_FAILED,
135                 0, 0);
136         const __m128i ipsec_proc_msk  = _mm_set_epi32(
137                 0, IXGBE_RXDADV_IPSEC_STATUS_SECP, 0, 0);
138         const __m128i ipsec_err_flag  = _mm_set_epi32(
139                 0, PKT_RX_SEC_OFFLOAD_FAILED | PKT_RX_SEC_OFFLOAD,
140                 0, 0);
141         const __m128i ipsec_proc_flag = _mm_set_epi32(
142                 0, PKT_RX_SEC_OFFLOAD, 0, 0);
143
144         rearm0 = _mm_load_si128((__m128i *)&rx_pkts[0]->rearm_data);
145         rearm1 = _mm_load_si128((__m128i *)&rx_pkts[1]->rearm_data);
146         rearm2 = _mm_load_si128((__m128i *)&rx_pkts[2]->rearm_data);
147         rearm3 = _mm_load_si128((__m128i *)&rx_pkts[3]->rearm_data);
148         sterr0 = _mm_and_si128(descs[0], ipsec_sterr_msk);
149         sterr1 = _mm_and_si128(descs[1], ipsec_sterr_msk);
150         sterr2 = _mm_and_si128(descs[2], ipsec_sterr_msk);
151         sterr3 = _mm_and_si128(descs[3], ipsec_sterr_msk);
152         tmp1 = _mm_cmpeq_epi32(sterr0, ipsec_sterr_msk);
153         tmp2 = _mm_cmpeq_epi32(sterr0, ipsec_proc_msk);
154         tmp3 = _mm_cmpeq_epi32(sterr1, ipsec_sterr_msk);
155         tmp4 = _mm_cmpeq_epi32(sterr1, ipsec_proc_msk);
156         sterr0 = _mm_or_si128(_mm_and_si128(tmp1, ipsec_err_flag),
157                                 _mm_and_si128(tmp2, ipsec_proc_flag));
158         sterr1 = _mm_or_si128(_mm_and_si128(tmp3, ipsec_err_flag),
159                                 _mm_and_si128(tmp4, ipsec_proc_flag));
160         tmp1 = _mm_cmpeq_epi32(sterr2, ipsec_sterr_msk);
161         tmp2 = _mm_cmpeq_epi32(sterr2, ipsec_proc_msk);
162         tmp3 = _mm_cmpeq_epi32(sterr3, ipsec_sterr_msk);
163         tmp4 = _mm_cmpeq_epi32(sterr3, ipsec_proc_msk);
164         sterr2 = _mm_or_si128(_mm_and_si128(tmp1, ipsec_err_flag),
165                                 _mm_and_si128(tmp2, ipsec_proc_flag));
166         sterr3 = _mm_or_si128(_mm_and_si128(tmp3, ipsec_err_flag),
167                                 _mm_and_si128(tmp4, ipsec_proc_flag));
168         rearm0 = _mm_or_si128(rearm0, sterr0);
169         rearm1 = _mm_or_si128(rearm1, sterr1);
170         rearm2 = _mm_or_si128(rearm2, sterr2);
171         rearm3 = _mm_or_si128(rearm3, sterr3);
172         _mm_store_si128((__m128i *)&rx_pkts[0]->rearm_data, rearm0);
173         _mm_store_si128((__m128i *)&rx_pkts[1]->rearm_data, rearm1);
174         _mm_store_si128((__m128i *)&rx_pkts[2]->rearm_data, rearm2);
175         _mm_store_si128((__m128i *)&rx_pkts[3]->rearm_data, rearm3);
176 }
177
178 static inline void
179 desc_to_olflags_v(__m128i descs[4], __m128i mbuf_init, uint8_t vlan_flags,
180         struct rte_mbuf **rx_pkts)
181 {
182         __m128i ptype0, ptype1, vtag0, vtag1, csum;
183         __m128i rearm0, rearm1, rearm2, rearm3;
184
185         /* mask everything except rss type */
186         const __m128i rsstype_msk = _mm_set_epi16(
187                         0x0000, 0x0000, 0x0000, 0x0000,
188                         0x000F, 0x000F, 0x000F, 0x000F);
189
190         /* mask the lower byte of ol_flags */
191         const __m128i ol_flags_msk = _mm_set_epi16(
192                         0x0000, 0x0000, 0x0000, 0x0000,
193                         0x00FF, 0x00FF, 0x00FF, 0x00FF);
194
195         /* map rss type to rss hash flag */
196         const __m128i rss_flags = _mm_set_epi8(PKT_RX_FDIR, 0, 0, 0,
197                         0, 0, 0, PKT_RX_RSS_HASH,
198                         PKT_RX_RSS_HASH, 0, PKT_RX_RSS_HASH, 0,
199                         PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, PKT_RX_RSS_HASH, 0);
200
201         /* mask everything except vlan present and l4/ip csum error */
202         const __m128i vlan_csum_msk = _mm_set_epi16(
203                 (IXGBE_RXDADV_ERR_TCPE | IXGBE_RXDADV_ERR_IPE) >> 16,
204                 (IXGBE_RXDADV_ERR_TCPE | IXGBE_RXDADV_ERR_IPE) >> 16,
205                 (IXGBE_RXDADV_ERR_TCPE | IXGBE_RXDADV_ERR_IPE) >> 16,
206                 (IXGBE_RXDADV_ERR_TCPE | IXGBE_RXDADV_ERR_IPE) >> 16,
207                 IXGBE_RXD_STAT_VP, IXGBE_RXD_STAT_VP,
208                 IXGBE_RXD_STAT_VP, IXGBE_RXD_STAT_VP);
209         /* map vlan present (0x8), IPE (0x2), L4E (0x1) to ol_flags */
210         const __m128i vlan_csum_map_lo = _mm_set_epi8(
211                 0, 0, 0, 0,
212                 vlan_flags | PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD,
213                 vlan_flags | PKT_RX_IP_CKSUM_BAD,
214                 vlan_flags | PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD,
215                 vlan_flags | PKT_RX_IP_CKSUM_GOOD,
216                 0, 0, 0, 0,
217                 PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD,
218                 PKT_RX_IP_CKSUM_BAD,
219                 PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_BAD,
220                 PKT_RX_IP_CKSUM_GOOD);
221
222         const __m128i vlan_csum_map_hi = _mm_set_epi8(
223                 0, 0, 0, 0,
224                 0, PKT_RX_L4_CKSUM_GOOD >> sizeof(uint8_t), 0,
225                 PKT_RX_L4_CKSUM_GOOD >> sizeof(uint8_t),
226                 0, 0, 0, 0,
227                 0, PKT_RX_L4_CKSUM_GOOD >> sizeof(uint8_t), 0,
228                 PKT_RX_L4_CKSUM_GOOD >> sizeof(uint8_t));
229
230         ptype0 = _mm_unpacklo_epi16(descs[0], descs[1]);
231         ptype1 = _mm_unpacklo_epi16(descs[2], descs[3]);
232         vtag0 = _mm_unpackhi_epi16(descs[0], descs[1]);
233         vtag1 = _mm_unpackhi_epi16(descs[2], descs[3]);
234
235         ptype0 = _mm_unpacklo_epi32(ptype0, ptype1);
236         ptype0 = _mm_and_si128(ptype0, rsstype_msk);
237         ptype0 = _mm_shuffle_epi8(rss_flags, ptype0);
238
239         vtag1 = _mm_unpacklo_epi32(vtag0, vtag1);
240         vtag1 = _mm_and_si128(vtag1, vlan_csum_msk);
241
242         /* csum bits are in the most significant, to use shuffle we need to
243          * shift them. Change mask to 0xc000 to 0x0003.
244          */
245         csum = _mm_srli_epi16(vtag1, 14);
246
247         /* now or the most significant 64 bits containing the checksum
248          * flags with the vlan present flags.
249          */
250         csum = _mm_srli_si128(csum, 8);
251         vtag1 = _mm_or_si128(csum, vtag1);
252
253         /* convert VP, IPE, L4E to ol_flags */
254         vtag0 = _mm_shuffle_epi8(vlan_csum_map_hi, vtag1);
255         vtag0 = _mm_slli_epi16(vtag0, sizeof(uint8_t));
256
257         vtag1 = _mm_shuffle_epi8(vlan_csum_map_lo, vtag1);
258         vtag1 = _mm_and_si128(vtag1, ol_flags_msk);
259         vtag1 = _mm_or_si128(vtag0, vtag1);
260
261         vtag1 = _mm_or_si128(ptype0, vtag1);
262
263         /*
264          * At this point, we have the 4 sets of flags in the low 64-bits
265          * of vtag1 (4x16).
266          * We want to extract these, and merge them with the mbuf init data
267          * so we can do a single 16-byte write to the mbuf to set the flags
268          * and all the other initialization fields. Extracting the
269          * appropriate flags means that we have to do a shift and blend for
270          * each mbuf before we do the write.
271          */
272         rearm0 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(vtag1, 8), 0x10);
273         rearm1 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(vtag1, 6), 0x10);
274         rearm2 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(vtag1, 4), 0x10);
275         rearm3 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(vtag1, 2), 0x10);
276
277         /* write the rearm data and the olflags in one write */
278         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
279                         offsetof(struct rte_mbuf, rearm_data) + 8);
280         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, rearm_data) !=
281                         RTE_ALIGN(offsetof(struct rte_mbuf, rearm_data), 16));
282         _mm_store_si128((__m128i *)&rx_pkts[0]->rearm_data, rearm0);
283         _mm_store_si128((__m128i *)&rx_pkts[1]->rearm_data, rearm1);
284         _mm_store_si128((__m128i *)&rx_pkts[2]->rearm_data, rearm2);
285         _mm_store_si128((__m128i *)&rx_pkts[3]->rearm_data, rearm3);
286 }
287
288 static inline uint32_t get_packet_type(int index,
289                                        uint32_t pkt_info,
290                                        uint32_t etqf_check,
291                                        uint32_t tunnel_check)
292 {
293         if (etqf_check & (0x02 << (index * RTE_IXGBE_DESCS_PER_LOOP)))
294                 return RTE_PTYPE_UNKNOWN;
295
296         if (tunnel_check & (0x02 << (index * RTE_IXGBE_DESCS_PER_LOOP))) {
297                 pkt_info &= IXGBE_PACKET_TYPE_MASK_TUNNEL;
298                 return ptype_table_tn[pkt_info];
299         }
300
301         pkt_info &= IXGBE_PACKET_TYPE_MASK_82599;
302         return ptype_table[pkt_info];
303 }
304
305 static inline void
306 desc_to_ptype_v(__m128i descs[4], uint16_t pkt_type_mask,
307                 struct rte_mbuf **rx_pkts)
308 {
309         __m128i etqf_mask = _mm_set_epi64x(0x800000008000LL, 0x800000008000LL);
310         __m128i ptype_mask = _mm_set_epi32(
311                 pkt_type_mask, pkt_type_mask, pkt_type_mask, pkt_type_mask);
312         __m128i tunnel_mask =
313                 _mm_set_epi64x(0x100000001000LL, 0x100000001000LL);
314
315         uint32_t etqf_check, tunnel_check, pkt_info;
316
317         __m128i ptype0 = _mm_unpacklo_epi32(descs[0], descs[2]);
318         __m128i ptype1 = _mm_unpacklo_epi32(descs[1], descs[3]);
319
320         /* interleave low 32 bits,
321          * now we have 4 ptypes in a XMM register
322          */
323         ptype0 = _mm_unpacklo_epi32(ptype0, ptype1);
324
325         /* create a etqf bitmask based on the etqf bit. */
326         etqf_check = _mm_movemask_epi8(_mm_and_si128(ptype0, etqf_mask));
327
328         /* shift left by IXGBE_PACKET_TYPE_SHIFT, and apply ptype mask */
329         ptype0 = _mm_and_si128(_mm_srli_epi32(ptype0, IXGBE_PACKET_TYPE_SHIFT),
330                                ptype_mask);
331
332         /* create a tunnel bitmask based on the tunnel bit */
333         tunnel_check = _mm_movemask_epi8(
334                 _mm_slli_epi32(_mm_and_si128(ptype0, tunnel_mask), 0x3));
335
336         pkt_info = _mm_extract_epi32(ptype0, 0);
337         rx_pkts[0]->packet_type =
338                 get_packet_type(0, pkt_info, etqf_check, tunnel_check);
339         pkt_info = _mm_extract_epi32(ptype0, 1);
340         rx_pkts[1]->packet_type =
341                 get_packet_type(1, pkt_info, etqf_check, tunnel_check);
342         pkt_info = _mm_extract_epi32(ptype0, 2);
343         rx_pkts[2]->packet_type =
344                 get_packet_type(2, pkt_info, etqf_check, tunnel_check);
345         pkt_info = _mm_extract_epi32(ptype0, 3);
346         rx_pkts[3]->packet_type =
347                 get_packet_type(3, pkt_info, etqf_check, tunnel_check);
348 }
349
350 /*
351  * vPMD raw receive routine, only accept(nb_pkts >= RTE_IXGBE_DESCS_PER_LOOP)
352  *
353  * Notice:
354  * - nb_pkts < RTE_IXGBE_DESCS_PER_LOOP, just return no packet
355  * - nb_pkts > RTE_IXGBE_MAX_RX_BURST, only scan RTE_IXGBE_MAX_RX_BURST
356  *   numbers of DD bit
357  * - floor align nb_pkts to a RTE_IXGBE_DESC_PER_LOOP power-of-two
358  */
359 static inline uint16_t
360 _recv_raw_pkts_vec(struct ixgbe_rx_queue *rxq, struct rte_mbuf **rx_pkts,
361                 uint16_t nb_pkts, uint8_t *split_packet)
362 {
363         volatile union ixgbe_adv_rx_desc *rxdp;
364         struct ixgbe_rx_entry *sw_ring;
365         uint16_t nb_pkts_recd;
366         uint8_t use_ipsec = rxq->using_ipsec;
367         int pos;
368         uint64_t var;
369         __m128i shuf_msk;
370         __m128i crc_adjust = _mm_set_epi16(
371                                 0, 0, 0,    /* ignore non-length fields */
372                                 -rxq->crc_len, /* sub crc on data_len */
373                                 0,          /* ignore high-16bits of pkt_len */
374                                 -rxq->crc_len, /* sub crc on pkt_len */
375                                 0, 0            /* ignore pkt_type field */
376                         );
377         /*
378          * compile-time check the above crc_adjust layout is correct.
379          * NOTE: the first field (lowest address) is given last in set_epi16
380          * call above.
381          */
382         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
383                         offsetof(struct rte_mbuf, rx_descriptor_fields1) + 4);
384         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
385                         offsetof(struct rte_mbuf, rx_descriptor_fields1) + 8);
386         __m128i dd_check, eop_check;
387         __m128i mbuf_init;
388         uint8_t vlan_flags;
389
390         /* nb_pkts shall be less equal than RTE_IXGBE_MAX_RX_BURST */
391         nb_pkts = RTE_MIN(nb_pkts, RTE_IXGBE_MAX_RX_BURST);
392
393         /* nb_pkts has to be floor-aligned to RTE_IXGBE_DESCS_PER_LOOP */
394         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_IXGBE_DESCS_PER_LOOP);
395
396         /* Just the act of getting into the function from the application is
397          * going to cost about 7 cycles
398          */
399         rxdp = rxq->rx_ring + rxq->rx_tail;
400
401         rte_prefetch0(rxdp);
402
403         /* See if we need to rearm the RX queue - gives the prefetch a bit
404          * of time to act
405          */
406         if (rxq->rxrearm_nb > RTE_IXGBE_RXQ_REARM_THRESH)
407                 ixgbe_rxq_rearm(rxq);
408
409         /* Before we start moving massive data around, check to see if
410          * there is actually a packet available
411          */
412         if (!(rxdp->wb.upper.status_error &
413                                 rte_cpu_to_le_32(IXGBE_RXDADV_STAT_DD)))
414                 return 0;
415
416         /* 4 packets DD mask */
417         dd_check = _mm_set_epi64x(0x0000000100000001LL, 0x0000000100000001LL);
418
419         /* 4 packets EOP mask */
420         eop_check = _mm_set_epi64x(0x0000000200000002LL, 0x0000000200000002LL);
421
422         /* mask to shuffle from desc. to mbuf */
423         shuf_msk = _mm_set_epi8(
424                 7, 6, 5, 4,  /* octet 4~7, 32bits rss */
425                 15, 14,      /* octet 14~15, low 16 bits vlan_macip */
426                 13, 12,      /* octet 12~13, 16 bits data_len */
427                 0xFF, 0xFF,  /* skip high 16 bits pkt_len, zero out */
428                 13, 12,      /* octet 12~13, low 16 bits pkt_len */
429                 0xFF, 0xFF,  /* skip 32 bit pkt_type */
430                 0xFF, 0xFF
431                 );
432         /*
433          * Compile-time verify the shuffle mask
434          * NOTE: some field positions already verified above, but duplicated
435          * here for completeness in case of future modifications.
436          */
437         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
438                         offsetof(struct rte_mbuf, rx_descriptor_fields1) + 4);
439         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
440                         offsetof(struct rte_mbuf, rx_descriptor_fields1) + 8);
441         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, vlan_tci) !=
442                         offsetof(struct rte_mbuf, rx_descriptor_fields1) + 10);
443         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, hash) !=
444                         offsetof(struct rte_mbuf, rx_descriptor_fields1) + 12);
445
446         mbuf_init = _mm_set_epi64x(0, rxq->mbuf_initializer);
447
448         /* Cache is empty -> need to scan the buffer rings, but first move
449          * the next 'n' mbufs into the cache
450          */
451         sw_ring = &rxq->sw_ring[rxq->rx_tail];
452
453         /* ensure these 2 flags are in the lower 8 bits */
454         RTE_BUILD_BUG_ON((PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED) > UINT8_MAX);
455         vlan_flags = rxq->vlan_flags & UINT8_MAX;
456
457         /* A. load 4 packet in one loop
458          * [A*. mask out 4 unused dirty field in desc]
459          * B. copy 4 mbuf point from swring to rx_pkts
460          * C. calc the number of DD bits among the 4 packets
461          * [C*. extract the end-of-packet bit, if requested]
462          * D. fill info. from desc to mbuf
463          */
464         for (pos = 0, nb_pkts_recd = 0; pos < nb_pkts;
465                         pos += RTE_IXGBE_DESCS_PER_LOOP,
466                         rxdp += RTE_IXGBE_DESCS_PER_LOOP) {
467                 __m128i descs[RTE_IXGBE_DESCS_PER_LOOP];
468                 __m128i pkt_mb1, pkt_mb2, pkt_mb3, pkt_mb4;
469                 __m128i zero, staterr, sterr_tmp1, sterr_tmp2;
470                 /* 2 64 bit or 4 32 bit mbuf pointers in one XMM reg. */
471                 __m128i mbp1;
472 #if defined(RTE_ARCH_X86_64)
473                 __m128i mbp2;
474 #endif
475
476                 /* B.1 load 2 (64 bit) or 4 (32 bit) mbuf points */
477                 mbp1 = _mm_loadu_si128((__m128i *)&sw_ring[pos]);
478
479                 /* Read desc statuses backwards to avoid race condition */
480                 /* A.1 load 4 pkts desc */
481                 descs[3] = _mm_loadu_si128((__m128i *)(rxdp + 3));
482                 rte_compiler_barrier();
483
484                 /* B.2 copy 2 64 bit or 4 32 bit mbuf point into rx_pkts */
485                 _mm_storeu_si128((__m128i *)&rx_pkts[pos], mbp1);
486
487 #if defined(RTE_ARCH_X86_64)
488                 /* B.1 load 2 64 bit mbuf points */
489                 mbp2 = _mm_loadu_si128((__m128i *)&sw_ring[pos+2]);
490 #endif
491
492                 descs[2] = _mm_loadu_si128((__m128i *)(rxdp + 2));
493                 rte_compiler_barrier();
494                 /* B.1 load 2 mbuf point */
495                 descs[1] = _mm_loadu_si128((__m128i *)(rxdp + 1));
496                 rte_compiler_barrier();
497                 descs[0] = _mm_loadu_si128((__m128i *)(rxdp));
498
499 #if defined(RTE_ARCH_X86_64)
500                 /* B.2 copy 2 mbuf point into rx_pkts  */
501                 _mm_storeu_si128((__m128i *)&rx_pkts[pos+2], mbp2);
502 #endif
503
504                 if (split_packet) {
505                         rte_mbuf_prefetch_part2(rx_pkts[pos]);
506                         rte_mbuf_prefetch_part2(rx_pkts[pos + 1]);
507                         rte_mbuf_prefetch_part2(rx_pkts[pos + 2]);
508                         rte_mbuf_prefetch_part2(rx_pkts[pos + 3]);
509                 }
510
511                 /* avoid compiler reorder optimization */
512                 rte_compiler_barrier();
513
514                 /* D.1 pkt 3,4 convert format from desc to pktmbuf */
515                 pkt_mb4 = _mm_shuffle_epi8(descs[3], shuf_msk);
516                 pkt_mb3 = _mm_shuffle_epi8(descs[2], shuf_msk);
517
518                 /* D.1 pkt 1,2 convert format from desc to pktmbuf */
519                 pkt_mb2 = _mm_shuffle_epi8(descs[1], shuf_msk);
520                 pkt_mb1 = _mm_shuffle_epi8(descs[0], shuf_msk);
521
522                 /* C.1 4=>2 filter staterr info only */
523                 sterr_tmp2 = _mm_unpackhi_epi32(descs[3], descs[2]);
524                 /* C.1 4=>2 filter staterr info only */
525                 sterr_tmp1 = _mm_unpackhi_epi32(descs[1], descs[0]);
526
527                 /* set ol_flags with vlan packet type */
528                 desc_to_olflags_v(descs, mbuf_init, vlan_flags, &rx_pkts[pos]);
529
530                 if (unlikely(use_ipsec))
531                         desc_to_olflags_v_ipsec(descs, rx_pkts);
532
533                 /* D.2 pkt 3,4 set in_port/nb_seg and remove crc */
534                 pkt_mb4 = _mm_add_epi16(pkt_mb4, crc_adjust);
535                 pkt_mb3 = _mm_add_epi16(pkt_mb3, crc_adjust);
536
537                 /* C.2 get 4 pkts staterr value  */
538                 zero = _mm_xor_si128(dd_check, dd_check);
539                 staterr = _mm_unpacklo_epi32(sterr_tmp1, sterr_tmp2);
540
541                 /* D.3 copy final 3,4 data to rx_pkts */
542                 _mm_storeu_si128((void *)&rx_pkts[pos+3]->rx_descriptor_fields1,
543                                 pkt_mb4);
544                 _mm_storeu_si128((void *)&rx_pkts[pos+2]->rx_descriptor_fields1,
545                                 pkt_mb3);
546
547                 /* D.2 pkt 1,2 set in_port/nb_seg and remove crc */
548                 pkt_mb2 = _mm_add_epi16(pkt_mb2, crc_adjust);
549                 pkt_mb1 = _mm_add_epi16(pkt_mb1, crc_adjust);
550
551                 /* C* extract and record EOP bit */
552                 if (split_packet) {
553                         __m128i eop_shuf_mask = _mm_set_epi8(
554                                         0xFF, 0xFF, 0xFF, 0xFF,
555                                         0xFF, 0xFF, 0xFF, 0xFF,
556                                         0xFF, 0xFF, 0xFF, 0xFF,
557                                         0x04, 0x0C, 0x00, 0x08
558                                         );
559
560                         /* and with mask to extract bits, flipping 1-0 */
561                         __m128i eop_bits = _mm_andnot_si128(staterr, eop_check);
562                         /* the staterr values are not in order, as the count
563                          * count of dd bits doesn't care. However, for end of
564                          * packet tracking, we do care, so shuffle. This also
565                          * compresses the 32-bit values to 8-bit
566                          */
567                         eop_bits = _mm_shuffle_epi8(eop_bits, eop_shuf_mask);
568                         /* store the resulting 32-bit value */
569                         *(int *)split_packet = _mm_cvtsi128_si32(eop_bits);
570                         split_packet += RTE_IXGBE_DESCS_PER_LOOP;
571                 }
572
573                 /* C.3 calc available number of desc */
574                 staterr = _mm_and_si128(staterr, dd_check);
575                 staterr = _mm_packs_epi32(staterr, zero);
576
577                 /* D.3 copy final 1,2 data to rx_pkts */
578                 _mm_storeu_si128((void *)&rx_pkts[pos+1]->rx_descriptor_fields1,
579                                 pkt_mb2);
580                 _mm_storeu_si128((void *)&rx_pkts[pos]->rx_descriptor_fields1,
581                                 pkt_mb1);
582
583                 desc_to_ptype_v(descs, rxq->pkt_type_mask, &rx_pkts[pos]);
584
585                 /* C.4 calc avaialbe number of desc */
586                 var = __builtin_popcountll(_mm_cvtsi128_si64(staterr));
587                 nb_pkts_recd += var;
588                 if (likely(var != RTE_IXGBE_DESCS_PER_LOOP))
589                         break;
590         }
591
592         /* Update our internal tail pointer */
593         rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_pkts_recd);
594         rxq->rx_tail = (uint16_t)(rxq->rx_tail & (rxq->nb_rx_desc - 1));
595         rxq->rxrearm_nb = (uint16_t)(rxq->rxrearm_nb + nb_pkts_recd);
596
597         return nb_pkts_recd;
598 }
599
600 /*
601  * vPMD receive routine, only accept(nb_pkts >= RTE_IXGBE_DESCS_PER_LOOP)
602  *
603  * Notice:
604  * - nb_pkts < RTE_IXGBE_DESCS_PER_LOOP, just return no packet
605  * - nb_pkts > RTE_IXGBE_MAX_RX_BURST, only scan RTE_IXGBE_MAX_RX_BURST
606  *   numbers of DD bit
607  * - floor align nb_pkts to a RTE_IXGBE_DESC_PER_LOOP power-of-two
608  */
609 uint16_t
610 ixgbe_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
611                 uint16_t nb_pkts)
612 {
613         return _recv_raw_pkts_vec(rx_queue, rx_pkts, nb_pkts, NULL);
614 }
615
616 /*
617  * vPMD receive routine that reassembles scattered packets
618  *
619  * Notice:
620  * - nb_pkts < RTE_IXGBE_DESCS_PER_LOOP, just return no packet
621  * - nb_pkts > RTE_IXGBE_MAX_RX_BURST, only scan RTE_IXGBE_MAX_RX_BURST
622  *   numbers of DD bit
623  * - floor align nb_pkts to a RTE_IXGBE_DESC_PER_LOOP power-of-two
624  */
625 uint16_t
626 ixgbe_recv_scattered_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
627                 uint16_t nb_pkts)
628 {
629         struct ixgbe_rx_queue *rxq = rx_queue;
630         uint8_t split_flags[RTE_IXGBE_MAX_RX_BURST] = {0};
631
632         /* get some new buffers */
633         uint16_t nb_bufs = _recv_raw_pkts_vec(rxq, rx_pkts, nb_pkts,
634                         split_flags);
635         if (nb_bufs == 0)
636                 return 0;
637
638         /* happy day case, full burst + no packets to be joined */
639         const uint64_t *split_fl64 = (uint64_t *)split_flags;
640         if (rxq->pkt_first_seg == NULL &&
641                         split_fl64[0] == 0 && split_fl64[1] == 0 &&
642                         split_fl64[2] == 0 && split_fl64[3] == 0)
643                 return nb_bufs;
644
645         /* reassemble any packets that need reassembly*/
646         unsigned i = 0;
647         if (rxq->pkt_first_seg == NULL) {
648                 /* find the first split flag, and only reassemble then*/
649                 while (i < nb_bufs && !split_flags[i])
650                         i++;
651                 if (i == nb_bufs)
652                         return nb_bufs;
653         }
654         return i + reassemble_packets(rxq, &rx_pkts[i], nb_bufs - i,
655                 &split_flags[i]);
656 }
657
658 static inline void
659 vtx1(volatile union ixgbe_adv_tx_desc *txdp,
660                 struct rte_mbuf *pkt, uint64_t flags)
661 {
662         __m128i descriptor = _mm_set_epi64x((uint64_t)pkt->pkt_len << 46 |
663                         flags | pkt->data_len,
664                         pkt->buf_physaddr + pkt->data_off);
665         _mm_store_si128((__m128i *)&txdp->read, descriptor);
666 }
667
668 static inline void
669 vtx(volatile union ixgbe_adv_tx_desc *txdp,
670                 struct rte_mbuf **pkt, uint16_t nb_pkts,  uint64_t flags)
671 {
672         int i;
673
674         for (i = 0; i < nb_pkts; ++i, ++txdp, ++pkt)
675                 vtx1(txdp, *pkt, flags);
676 }
677
678 uint16_t
679 ixgbe_xmit_fixed_burst_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
680                            uint16_t nb_pkts)
681 {
682         struct ixgbe_tx_queue *txq = (struct ixgbe_tx_queue *)tx_queue;
683         volatile union ixgbe_adv_tx_desc *txdp;
684         struct ixgbe_tx_entry_v *txep;
685         uint16_t n, nb_commit, tx_id;
686         uint64_t flags = DCMD_DTYP_FLAGS;
687         uint64_t rs = IXGBE_ADVTXD_DCMD_RS|DCMD_DTYP_FLAGS;
688         int i;
689
690         /* cross rx_thresh boundary is not allowed */
691         nb_pkts = RTE_MIN(nb_pkts, txq->tx_rs_thresh);
692
693         if (txq->nb_tx_free < txq->tx_free_thresh)
694                 ixgbe_tx_free_bufs(txq);
695
696         nb_commit = nb_pkts = (uint16_t)RTE_MIN(txq->nb_tx_free, nb_pkts);
697         if (unlikely(nb_pkts == 0))
698                 return 0;
699
700         tx_id = txq->tx_tail;
701         txdp = &txq->tx_ring[tx_id];
702         txep = &txq->sw_ring_v[tx_id];
703
704         txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_pkts);
705
706         n = (uint16_t)(txq->nb_tx_desc - tx_id);
707         if (nb_commit >= n) {
708
709                 tx_backlog_entry(txep, tx_pkts, n);
710
711                 for (i = 0; i < n - 1; ++i, ++tx_pkts, ++txdp)
712                         vtx1(txdp, *tx_pkts, flags);
713
714                 vtx1(txdp, *tx_pkts++, rs);
715
716                 nb_commit = (uint16_t)(nb_commit - n);
717
718                 tx_id = 0;
719                 txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
720
721                 /* avoid reach the end of ring */
722                 txdp = &(txq->tx_ring[tx_id]);
723                 txep = &txq->sw_ring_v[tx_id];
724         }
725
726         tx_backlog_entry(txep, tx_pkts, nb_commit);
727
728         vtx(txdp, tx_pkts, nb_commit, flags);
729
730         tx_id = (uint16_t)(tx_id + nb_commit);
731         if (tx_id > txq->tx_next_rs) {
732                 txq->tx_ring[txq->tx_next_rs].read.cmd_type_len |=
733                         rte_cpu_to_le_32(IXGBE_ADVTXD_DCMD_RS);
734                 txq->tx_next_rs = (uint16_t)(txq->tx_next_rs +
735                         txq->tx_rs_thresh);
736         }
737
738         txq->tx_tail = tx_id;
739
740         IXGBE_PCI_REG_WRITE(txq->tdt_reg_addr, txq->tx_tail);
741
742         return nb_pkts;
743 }
744
745 static void __attribute__((cold))
746 ixgbe_tx_queue_release_mbufs_vec(struct ixgbe_tx_queue *txq)
747 {
748         _ixgbe_tx_queue_release_mbufs_vec(txq);
749 }
750
751 void __attribute__((cold))
752 ixgbe_rx_queue_release_mbufs_vec(struct ixgbe_rx_queue *rxq)
753 {
754         _ixgbe_rx_queue_release_mbufs_vec(rxq);
755 }
756
757 static void __attribute__((cold))
758 ixgbe_tx_free_swring(struct ixgbe_tx_queue *txq)
759 {
760         _ixgbe_tx_free_swring_vec(txq);
761 }
762
763 static void __attribute__((cold))
764 ixgbe_reset_tx_queue(struct ixgbe_tx_queue *txq)
765 {
766         _ixgbe_reset_tx_queue_vec(txq);
767 }
768
769 static const struct ixgbe_txq_ops vec_txq_ops = {
770         .release_mbufs = ixgbe_tx_queue_release_mbufs_vec,
771         .free_swring = ixgbe_tx_free_swring,
772         .reset = ixgbe_reset_tx_queue,
773 };
774
775 int __attribute__((cold))
776 ixgbe_rxq_vec_setup(struct ixgbe_rx_queue *rxq)
777 {
778         return ixgbe_rxq_vec_setup_default(rxq);
779 }
780
781 int __attribute__((cold))
782 ixgbe_txq_vec_setup(struct ixgbe_tx_queue *txq)
783 {
784         return ixgbe_txq_vec_setup_default(txq, &vec_txq_ops);
785 }
786
787 int __attribute__((cold))
788 ixgbe_rx_vec_dev_conf_condition_check(struct rte_eth_dev *dev)
789 {
790         return ixgbe_rx_vec_dev_conf_condition_check_default(dev);
791 }