39c732515c5eb4bdd58b06e9bfe4e2e02086e1c9
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx_vec_sse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
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 6WIND S.A. 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 <assert.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <smmintrin.h>
39
40 /* Verbs header. */
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #ifdef PEDANTIC
43 #pragma GCC diagnostic ignored "-Wpedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #include <infiniband/mlx5_hw.h>
47 #include <infiniband/arch.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-Wpedantic"
50 #endif
51
52 #include <rte_mbuf.h>
53 #include <rte_mempool.h>
54 #include <rte_prefetch.h>
55
56 #include "mlx5.h"
57 #include "mlx5_utils.h"
58 #include "mlx5_rxtx.h"
59 #include "mlx5_autoconf.h"
60 #include "mlx5_defs.h"
61 #include "mlx5_prm.h"
62
63 #ifndef __INTEL_COMPILER
64 #pragma GCC diagnostic ignored "-Wcast-qual"
65 #endif
66
67 /**
68  * Fill in buffer descriptors in a multi-packet send descriptor.
69  *
70  * @param txq
71  *   Pointer to TX queue structure.
72  * @param dseg
73  *   Pointer to buffer descriptor to be writen.
74  * @param pkts
75  *   Pointer to array of packets to be sent.
76  * @param n
77  *   Number of packets to be filled.
78  */
79 static inline void
80 txq_wr_dseg_v(struct txq *txq, __m128i *dseg,
81               struct rte_mbuf **pkts, unsigned int n)
82 {
83         unsigned int pos;
84         uintptr_t addr;
85         const __m128i shuf_mask_dseg =
86                 _mm_set_epi8(8,  9, 10, 11, /* addr, bswap64 */
87                             12, 13, 14, 15,
88                              7,  6,  5,  4, /* lkey */
89                              0,  1,  2,  3  /* length, bswap32 */);
90 #ifdef MLX5_PMD_SOFT_COUNTERS
91         uint32_t tx_byte = 0;
92 #endif
93
94         for (pos = 0; pos < n; ++pos, ++dseg) {
95                 __m128i desc;
96                 struct rte_mbuf *pkt = pkts[pos];
97
98                 addr = rte_pktmbuf_mtod(pkt, uintptr_t);
99                 desc = _mm_set_epi32(addr >> 32,
100                                      addr,
101                                      mlx5_tx_mb2mr(txq, pkt),
102                                      DATA_LEN(pkt));
103                 desc = _mm_shuffle_epi8(desc, shuf_mask_dseg);
104                 _mm_store_si128(dseg, desc);
105 #ifdef MLX5_PMD_SOFT_COUNTERS
106                 tx_byte += DATA_LEN(pkt);
107 #endif
108         }
109 #ifdef MLX5_PMD_SOFT_COUNTERS
110         txq->stats.obytes += tx_byte;
111 #endif
112 }
113
114 /**
115  * Count the number of continuous single segment packets.
116  *
117  * @param pkts
118  *   Pointer to array of packets.
119  * @param pkts_n
120  *   Number of packets.
121  *
122  * @return
123  *   Number of continuous single segment packets.
124  */
125 static inline unsigned int
126 txq_check_multiseg(struct rte_mbuf **pkts, uint16_t pkts_n)
127 {
128         unsigned int pos;
129
130         if (!pkts_n)
131                 return 0;
132         /* Count the number of continuous single segment packets. */
133         for (pos = 0; pos < pkts_n; ++pos)
134                 if (NB_SEGS(pkts[pos]) > 1)
135                         break;
136         return pos;
137 }
138
139 /**
140  * Count the number of packets having same ol_flags and calculate cs_flags.
141  *
142  * @param txq
143  *   Pointer to TX queue structure.
144  * @param pkts
145  *   Pointer to array of packets.
146  * @param pkts_n
147  *   Number of packets.
148  * @param cs_flags
149  *   Pointer of flags to be returned.
150  *
151  * @return
152  *   Number of packets having same ol_flags.
153  */
154 static inline unsigned int
155 txq_calc_offload(struct txq *txq, struct rte_mbuf **pkts, uint16_t pkts_n,
156                  uint8_t *cs_flags)
157 {
158         unsigned int pos;
159         const uint64_t ol_mask =
160                 PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM |
161                 PKT_TX_UDP_CKSUM | PKT_TX_TUNNEL_GRE |
162                 PKT_TX_TUNNEL_VXLAN | PKT_TX_OUTER_IP_CKSUM;
163
164         if (!pkts_n)
165                 return 0;
166         /* Count the number of packets having same ol_flags. */
167         for (pos = 1; pos < pkts_n; ++pos)
168                 if ((pkts[pos]->ol_flags ^ pkts[0]->ol_flags) & ol_mask)
169                         break;
170         /* Should open another MPW session for the rest. */
171         if (pkts[0]->ol_flags &
172             (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
173                 const uint64_t is_tunneled =
174                         pkts[0]->ol_flags &
175                         (PKT_TX_TUNNEL_GRE |
176                          PKT_TX_TUNNEL_VXLAN);
177
178                 if (is_tunneled && txq->tunnel_en) {
179                         *cs_flags = MLX5_ETH_WQE_L3_INNER_CSUM |
180                                     MLX5_ETH_WQE_L4_INNER_CSUM;
181                         if (pkts[0]->ol_flags & PKT_TX_OUTER_IP_CKSUM)
182                                 *cs_flags |= MLX5_ETH_WQE_L3_CSUM;
183                 } else {
184                         *cs_flags = MLX5_ETH_WQE_L3_CSUM |
185                                     MLX5_ETH_WQE_L4_CSUM;
186                 }
187         }
188         return pos;
189 }
190
191 /**
192  * Send multi-segmented packets until it encounters a single segment packet in
193  * the pkts list.
194  *
195  * @param txq
196  *   Pointer to TX queue structure.
197  * @param pkts
198  *   Pointer to array of packets to be sent.
199  * @param pkts_n
200  *   Number of packets to be sent.
201  *
202  * @return
203  *   Number of packets successfully transmitted (<= pkts_n).
204  */
205 static uint16_t
206 txq_scatter_v(struct txq *txq, struct rte_mbuf **pkts, uint16_t pkts_n)
207 {
208         uint16_t elts_head = txq->elts_head;
209         const uint16_t elts_n = 1 << txq->elts_n;
210         const uint16_t elts_m = elts_n - 1;
211         const uint16_t wq_n = 1 << txq->wqe_n;
212         const uint16_t wq_mask = wq_n - 1;
213         const unsigned int nb_dword_per_wqebb =
214                 MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE;
215         const unsigned int nb_dword_in_hdr =
216                 sizeof(struct mlx5_wqe) / MLX5_WQE_DWORD_SIZE;
217         unsigned int n;
218         volatile struct mlx5_wqe *wqe = NULL;
219
220         assert(elts_n > pkts_n);
221         mlx5_tx_complete(txq);
222         if (unlikely(!pkts_n))
223                 return 0;
224         for (n = 0; n < pkts_n; ++n) {
225                 struct rte_mbuf *buf = pkts[n];
226                 unsigned int segs_n = buf->nb_segs;
227                 unsigned int ds = nb_dword_in_hdr;
228                 unsigned int len = PKT_LEN(buf);
229                 uint16_t wqe_ci = txq->wqe_ci;
230                 const __m128i shuf_mask_ctrl =
231                         _mm_set_epi8(15, 14, 13, 12,
232                                       8,  9, 10, 11, /* bswap32 */
233                                       4,  5,  6,  7, /* bswap32 */
234                                       0,  1,  2,  3  /* bswap32 */);
235                 uint8_t cs_flags = 0;
236                 uint16_t max_elts;
237                 uint16_t max_wqe;
238                 __m128i *t_wqe, *dseg;
239                 __m128i ctrl;
240
241                 assert(segs_n);
242                 max_elts = elts_n - (elts_head - txq->elts_tail);
243                 max_wqe = wq_n - (txq->wqe_ci - txq->wqe_pi);
244                 /*
245                  * A MPW session consumes 2 WQEs at most to
246                  * include MLX5_MPW_DSEG_MAX pointers.
247                  */
248                 if (segs_n == 1 ||
249                     max_elts < segs_n || max_wqe < 2)
250                         break;
251                 if (segs_n > MLX5_MPW_DSEG_MAX) {
252                         txq->stats.oerrors++;
253                         break;
254                 }
255                 wqe = &((volatile struct mlx5_wqe64 *)
256                          txq->wqes)[wqe_ci & wq_mask].hdr;
257                 if (buf->ol_flags &
258                      (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
259                         const uint64_t is_tunneled = buf->ol_flags &
260                                                       (PKT_TX_TUNNEL_GRE |
261                                                        PKT_TX_TUNNEL_VXLAN);
262
263                         if (is_tunneled && txq->tunnel_en) {
264                                 cs_flags = MLX5_ETH_WQE_L3_INNER_CSUM |
265                                            MLX5_ETH_WQE_L4_INNER_CSUM;
266                                 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
267                                         cs_flags |= MLX5_ETH_WQE_L3_CSUM;
268                         } else {
269                                 cs_flags = MLX5_ETH_WQE_L3_CSUM |
270                                            MLX5_ETH_WQE_L4_CSUM;
271                         }
272                 }
273                 /* Title WQEBB pointer. */
274                 t_wqe = (__m128i *)wqe;
275                 dseg = (__m128i *)(wqe + 1);
276                 do {
277                         if (!(ds++ % nb_dword_per_wqebb)) {
278                                 dseg = (__m128i *)
279                                         &((volatile struct mlx5_wqe64 *)
280                                            txq->wqes)[++wqe_ci & wq_mask];
281                         }
282                         txq_wr_dseg_v(txq, dseg++, &buf, 1);
283                         (*txq->elts)[elts_head++ & elts_m] = buf;
284                         buf = buf->next;
285                 } while (--segs_n);
286                 ++wqe_ci;
287                 /* Fill CTRL in the header. */
288                 ctrl = _mm_set_epi32(0, 0, txq->qp_num_8s | ds,
289                                      MLX5_OPC_MOD_MPW << 24 |
290                                      txq->wqe_ci << 8 | MLX5_OPCODE_TSO);
291                 ctrl = _mm_shuffle_epi8(ctrl, shuf_mask_ctrl);
292                 _mm_store_si128(t_wqe, ctrl);
293                 /* Fill ESEG in the header. */
294                 _mm_store_si128(t_wqe + 1,
295                                 _mm_set_epi16(0, 0, 0, 0,
296                                               htons(len), cs_flags,
297                                               0, 0));
298                 txq->wqe_ci = wqe_ci;
299         }
300         if (!n)
301                 return 0;
302         txq->elts_comp += (uint16_t)(elts_head - txq->elts_head);
303         txq->elts_head = elts_head;
304         if (txq->elts_comp >= MLX5_TX_COMP_THRESH) {
305                 wqe->ctrl[2] = htonl(8);
306                 wqe->ctrl[3] = txq->elts_head;
307                 txq->elts_comp = 0;
308                 ++txq->cq_pi;
309         }
310 #ifdef MLX5_PMD_SOFT_COUNTERS
311         txq->stats.opackets += n;
312 #endif
313         mlx5_tx_dbrec(txq, wqe);
314         return n;
315 }
316
317 /**
318  * Send burst of packets with Enhanced MPW. If it encounters a multi-seg packet,
319  * it returns to make it processed by txq_scatter_v(). All the packets in
320  * the pkts list should be single segment packets having same offload flags.
321  * This must be checked by txq_check_multiseg() and txq_calc_offload().
322  *
323  * @param txq
324  *   Pointer to TX queue structure.
325  * @param pkts
326  *   Pointer to array of packets to be sent.
327  * @param pkts_n
328  *   Number of packets to be sent (<= MLX5_VPMD_TX_MAX_BURST).
329  * @param cs_flags
330  *   Checksum offload flags to be written in the descriptor.
331  *
332  * @return
333  *   Number of packets successfully transmitted (<= pkts_n).
334  */
335 static inline uint16_t
336 txq_burst_v(struct txq *txq, struct rte_mbuf **pkts, uint16_t pkts_n,
337             uint8_t cs_flags)
338 {
339         struct rte_mbuf **elts;
340         uint16_t elts_head = txq->elts_head;
341         const uint16_t elts_n = 1 << txq->elts_n;
342         const uint16_t elts_m = elts_n - 1;
343         const unsigned int nb_dword_per_wqebb =
344                 MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE;
345         const unsigned int nb_dword_in_hdr =
346                 sizeof(struct mlx5_wqe) / MLX5_WQE_DWORD_SIZE;
347         unsigned int n = 0;
348         unsigned int pos;
349         uint16_t max_elts;
350         uint16_t max_wqe;
351         uint32_t comp_req = 0;
352         const uint16_t wq_n = 1 << txq->wqe_n;
353         const uint16_t wq_mask = wq_n - 1;
354         uint16_t wq_idx = txq->wqe_ci & wq_mask;
355         volatile struct mlx5_wqe64 *wq =
356                 &((volatile struct mlx5_wqe64 *)txq->wqes)[wq_idx];
357         volatile struct mlx5_wqe *wqe = (volatile struct mlx5_wqe *)wq;
358         const __m128i shuf_mask_ctrl =
359                 _mm_set_epi8(15, 14, 13, 12,
360                               8,  9, 10, 11, /* bswap32 */
361                               4,  5,  6,  7, /* bswap32 */
362                               0,  1,  2,  3  /* bswap32 */);
363         __m128i *t_wqe, *dseg;
364         __m128i ctrl;
365
366         /* Make sure all packets can fit into a single WQE. */
367         assert(elts_n > pkts_n);
368         mlx5_tx_complete(txq);
369         max_elts = (elts_n - (elts_head - txq->elts_tail));
370         max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
371         pkts_n = RTE_MIN((unsigned int)RTE_MIN(pkts_n, max_wqe), max_elts);
372         assert(pkts_n <= MLX5_DSEG_MAX - nb_dword_in_hdr);
373         if (unlikely(!pkts_n))
374                 return 0;
375         elts = &(*txq->elts)[elts_head & elts_m];
376         /* Loop for available tailroom first. */
377         n = RTE_MIN(elts_n - (elts_head & elts_m), pkts_n);
378         for (pos = 0; pos < (n & -2); pos += 2)
379                 _mm_storeu_si128((__m128i *)&elts[pos],
380                                  _mm_loadu_si128((__m128i *)&pkts[pos]));
381         if (n & 1)
382                 elts[pos] = pkts[pos];
383         /* Check if it crosses the end of the queue. */
384         if (unlikely(n < pkts_n)) {
385                 elts = &(*txq->elts)[0];
386                 for (pos = 0; pos < pkts_n - n; ++pos)
387                         elts[pos] = pkts[n + pos];
388         }
389         txq->elts_head += pkts_n;
390         /* Save title WQEBB pointer. */
391         t_wqe = (__m128i *)wqe;
392         dseg = (__m128i *)(wqe + 1);
393         /* Calculate the number of entries to the end. */
394         n = RTE_MIN(
395                 (wq_n - wq_idx) * nb_dword_per_wqebb - nb_dword_in_hdr,
396                 pkts_n);
397         /* Fill DSEGs. */
398         txq_wr_dseg_v(txq, dseg, pkts, n);
399         /* Check if it crosses the end of the queue. */
400         if (n < pkts_n) {
401                 dseg = (__m128i *)txq->wqes;
402                 txq_wr_dseg_v(txq, dseg, &pkts[n], pkts_n - n);
403         }
404         if (txq->elts_comp + pkts_n < MLX5_TX_COMP_THRESH) {
405                 txq->elts_comp += pkts_n;
406         } else {
407                 /* Request a completion. */
408                 txq->elts_comp = 0;
409                 ++txq->cq_pi;
410                 comp_req = 8;
411         }
412         /* Fill CTRL in the header. */
413         ctrl = _mm_set_epi32(txq->elts_head, comp_req,
414                              txq->qp_num_8s | (pkts_n + 2),
415                              MLX5_OPC_MOD_ENHANCED_MPSW << 24 |
416                                 txq->wqe_ci << 8 | MLX5_OPCODE_ENHANCED_MPSW);
417         ctrl = _mm_shuffle_epi8(ctrl, shuf_mask_ctrl);
418         _mm_store_si128(t_wqe, ctrl);
419         /* Fill ESEG in the header. */
420         _mm_store_si128(t_wqe + 1,
421                         _mm_set_epi8(0, 0, 0, 0,
422                                      0, 0, 0, 0,
423                                      0, 0, 0, cs_flags,
424                                      0, 0, 0, 0));
425 #ifdef MLX5_PMD_SOFT_COUNTERS
426         txq->stats.opackets += pkts_n;
427 #endif
428         txq->wqe_ci += (nb_dword_in_hdr + pkts_n + (nb_dword_per_wqebb - 1)) /
429                        nb_dword_per_wqebb;
430         /* Ring QP doorbell. */
431         mlx5_tx_dbrec(txq, wqe);
432         return pkts_n;
433 }
434
435 /**
436  * DPDK callback for vectorized TX.
437  *
438  * @param dpdk_txq
439  *   Generic pointer to TX queue structure.
440  * @param[in] pkts
441  *   Packets to transmit.
442  * @param pkts_n
443  *   Number of packets in array.
444  *
445  * @return
446  *   Number of packets successfully transmitted (<= pkts_n).
447  */
448 uint16_t
449 mlx5_tx_burst_raw_vec(void *dpdk_txq, struct rte_mbuf **pkts,
450                       uint16_t pkts_n)
451 {
452         struct txq *txq = (struct txq *)dpdk_txq;
453         uint16_t nb_tx = 0;
454
455         while (pkts_n > nb_tx) {
456                 uint16_t n;
457                 uint16_t ret;
458
459                 n = RTE_MIN((uint16_t)(pkts_n - nb_tx), MLX5_VPMD_TX_MAX_BURST);
460                 ret = txq_burst_v(txq, &pkts[nb_tx], n, 0);
461                 nb_tx += ret;
462                 if (!ret)
463                         break;
464         }
465         return nb_tx;
466 }
467
468 /**
469  * DPDK callback for vectorized TX with multi-seg packets and offload.
470  *
471  * @param dpdk_txq
472  *   Generic pointer to TX queue structure.
473  * @param[in] pkts
474  *   Packets to transmit.
475  * @param pkts_n
476  *   Number of packets in array.
477  *
478  * @return
479  *   Number of packets successfully transmitted (<= pkts_n).
480  */
481 uint16_t
482 mlx5_tx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
483 {
484         struct txq *txq = (struct txq *)dpdk_txq;
485         uint16_t nb_tx = 0;
486
487         while (pkts_n > nb_tx) {
488                 uint8_t cs_flags = 0;
489                 uint16_t n;
490                 uint16_t ret;
491
492                 /* Transmit multi-seg packets in the head of pkts list. */
493                 if (!(txq->flags & ETH_TXQ_FLAGS_NOMULTSEGS) &&
494                     NB_SEGS(pkts[nb_tx]) > 1)
495                         nb_tx += txq_scatter_v(txq,
496                                                &pkts[nb_tx],
497                                                pkts_n - nb_tx);
498                 n = RTE_MIN((uint16_t)(pkts_n - nb_tx), MLX5_VPMD_TX_MAX_BURST);
499                 if (!(txq->flags & ETH_TXQ_FLAGS_NOMULTSEGS))
500                         n = txq_check_multiseg(&pkts[nb_tx], n);
501                 if (!(txq->flags & ETH_TXQ_FLAGS_NOOFFLOADS))
502                         n = txq_calc_offload(txq, &pkts[nb_tx], n, &cs_flags);
503                 ret = txq_burst_v(txq, &pkts[nb_tx], n, cs_flags);
504                 nb_tx += ret;
505                 if (!ret)
506                         break;
507         }
508         return nb_tx;
509 }
510
511 /**
512  * Store free buffers to RX SW ring.
513  *
514  * @param rxq
515  *   Pointer to RX queue structure.
516  * @param pkts
517  *   Pointer to array of packets to be stored.
518  * @param pkts_n
519  *   Number of packets to be stored.
520  */
521 static inline void
522 rxq_copy_mbuf_v(struct rxq *rxq, struct rte_mbuf **pkts, uint16_t n)
523 {
524         const uint16_t q_mask = (1 << rxq->elts_n) - 1;
525         struct rte_mbuf **elts = &(*rxq->elts)[rxq->rq_pi & q_mask];
526         unsigned int pos;
527         uint16_t p = n & -2;
528
529         for (pos = 0; pos < p; pos += 2) {
530                 __m128i mbp;
531
532                 mbp = _mm_loadu_si128((__m128i *)&elts[pos]);
533                 _mm_storeu_si128((__m128i *)&pkts[pos], mbp);
534         }
535         if (n & 1)
536                 pkts[pos] = elts[pos];
537 }
538
539 /**
540  * Replenish buffers for RX in bulk.
541  *
542  * @param rxq
543  *   Pointer to RX queue structure.
544  * @param n
545  *   Number of buffers to be replenished.
546  */
547 static inline void
548 rxq_replenish_bulk_mbuf(struct rxq *rxq, uint16_t n)
549 {
550         const uint16_t q_n = 1 << rxq->elts_n;
551         const uint16_t q_mask = q_n - 1;
552         const uint16_t elts_idx = rxq->rq_ci & q_mask;
553         struct rte_mbuf **elts = &(*rxq->elts)[elts_idx];
554         volatile struct mlx5_wqe_data_seg *wq = &(*rxq->wqes)[elts_idx];
555         unsigned int i;
556
557         assert(n >= MLX5_VPMD_RXQ_RPLNSH_THRESH);
558         assert(n <= (uint16_t)(q_n - (rxq->rq_ci - rxq->rq_pi)));
559         assert(MLX5_VPMD_RXQ_RPLNSH_THRESH > MLX5_VPMD_DESCS_PER_LOOP);
560         /* Not to cross queue end. */
561         n = RTE_MIN(n - MLX5_VPMD_DESCS_PER_LOOP, q_n - elts_idx);
562         if (rte_mempool_get_bulk(rxq->mp, (void *)elts, n) < 0) {
563                 rxq->stats.rx_nombuf += n;
564                 return;
565         }
566         for (i = 0; i < n; ++i)
567                 wq[i].addr = htonll((uintptr_t)elts[i]->buf_addr +
568                                     RTE_PKTMBUF_HEADROOM);
569         rxq->rq_ci += n;
570         rte_wmb();
571         *rxq->rq_db = htonl(rxq->rq_ci);
572 }
573
574 /**
575  * Decompress a compressed completion and fill in mbufs in RX SW ring with data
576  * extracted from the title completion descriptor.
577  *
578  * @param rxq
579  *   Pointer to RX queue structure.
580  * @param cq
581  *   Pointer to completion array having a compressed completion at first.
582  * @param elts
583  *   Pointer to SW ring to be filled. The first mbuf has to be pre-built from
584  *   the title completion descriptor to be copied to the rest of mbufs.
585  */
586 static inline void
587 rxq_cq_decompress_v(struct rxq *rxq,
588                     volatile struct mlx5_cqe *cq,
589                     struct rte_mbuf **elts)
590 {
591         volatile struct mlx5_mini_cqe8 *mcq = (void *)(cq + 1);
592         struct rte_mbuf *t_pkt = elts[0]; /* Title packet is pre-built. */
593         unsigned int pos;
594         unsigned int i;
595         unsigned int inv = 0;
596         /* Mask to shuffle from extracted mini CQE to mbuf. */
597         const __m128i shuf_mask1 =
598                 _mm_set_epi8(0,  1,  2,  3, /* rss, bswap32 */
599                             -1, -1,         /* skip vlan_tci */
600                              6,  7,         /* data_len, bswap16 */
601                             -1, -1,  6,  7, /* pkt_len, bswap16 */
602                             -1, -1, -1, -1  /* skip packet_type */);
603         const __m128i shuf_mask2 =
604                 _mm_set_epi8(8,  9, 10, 11, /* rss, bswap32 */
605                             -1, -1,         /* skip vlan_tci */
606                             14, 15,         /* data_len, bswap16 */
607                             -1, -1, 14, 15, /* pkt_len, bswap16 */
608                             -1, -1, -1, -1  /* skip packet_type */);
609         /* Restore the compressed count. Must be 16 bits. */
610         const uint16_t mcqe_n = t_pkt->data_len +
611                                 (rxq->crc_present * ETHER_CRC_LEN);
612         const __m128i rearm =
613                 _mm_loadu_si128((__m128i *)&t_pkt->rearm_data);
614         const __m128i rxdf =
615                 _mm_loadu_si128((__m128i *)&t_pkt->rx_descriptor_fields1);
616         const __m128i crc_adj =
617                 _mm_set_epi16(0, 0, 0,
618                               rxq->crc_present * ETHER_CRC_LEN,
619                               0,
620                               rxq->crc_present * ETHER_CRC_LEN,
621                               0, 0);
622         const uint32_t flow_tag = t_pkt->hash.fdir.hi;
623 #ifdef MLX5_PMD_SOFT_COUNTERS
624         const __m128i zero = _mm_setzero_si128();
625         const __m128i ones = _mm_cmpeq_epi32(zero, zero);
626         uint32_t rcvd_byte = 0;
627         /* Mask to shuffle byte_cnt to add up stats. Do bswap16 for all. */
628         const __m128i len_shuf_mask =
629                 _mm_set_epi8(-1, -1, -1, -1,
630                              -1, -1, -1, -1,
631                              14, 15,  6,  7,
632                              10, 11,  2,  3);
633 #endif
634
635         /* Compile time sanity check for this function. */
636         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
637                          offsetof(struct rte_mbuf, rx_descriptor_fields1) + 4);
638         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
639                          offsetof(struct rte_mbuf, rx_descriptor_fields1) + 8);
640         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, hash) !=
641                          offsetof(struct rte_mbuf, rx_descriptor_fields1) + 12);
642         /*
643          * A. load mCQEs into a 128bit register.
644          * B. store rearm data to mbuf.
645          * C. combine data from mCQEs with rx_descriptor_fields1.
646          * D. store rx_descriptor_fields1.
647          * E. store flow tag (rte_flow mark).
648          */
649         for (pos = 0; pos < mcqe_n; ) {
650                 __m128i mcqe1, mcqe2;
651                 __m128i rxdf1, rxdf2;
652 #ifdef MLX5_PMD_SOFT_COUNTERS
653                 __m128i byte_cnt, invalid_mask;
654 #endif
655
656                 if (!(pos & 0x7) && pos + 8 < mcqe_n)
657                         rte_prefetch0((void *)(cq + pos + 8));
658                 /* A.1 load mCQEs into a 128bit register. */
659                 mcqe1 = _mm_loadu_si128((__m128i *)&mcq[pos % 8]);
660                 mcqe2 = _mm_loadu_si128((__m128i *)&mcq[pos % 8 + 2]);
661                 /* B.1 store rearm data to mbuf. */
662                 _mm_storeu_si128((__m128i *)&elts[pos]->rearm_data, rearm);
663                 _mm_storeu_si128((__m128i *)&elts[pos + 1]->rearm_data, rearm);
664                 /* C.1 combine data from mCQEs with rx_descriptor_fields1. */
665                 rxdf1 = _mm_shuffle_epi8(mcqe1, shuf_mask1);
666                 rxdf2 = _mm_shuffle_epi8(mcqe1, shuf_mask2);
667                 rxdf1 = _mm_sub_epi16(rxdf1, crc_adj);
668                 rxdf2 = _mm_sub_epi16(rxdf2, crc_adj);
669                 rxdf1 = _mm_blend_epi16(rxdf1, rxdf, 0x23);
670                 rxdf2 = _mm_blend_epi16(rxdf2, rxdf, 0x23);
671                 /* D.1 store rx_descriptor_fields1. */
672                 _mm_storeu_si128((__m128i *)
673                                   &elts[pos]->rx_descriptor_fields1,
674                                  rxdf1);
675                 _mm_storeu_si128((__m128i *)
676                                   &elts[pos + 1]->rx_descriptor_fields1,
677                                  rxdf2);
678                 /* B.1 store rearm data to mbuf. */
679                 _mm_storeu_si128((__m128i *)&elts[pos + 2]->rearm_data, rearm);
680                 _mm_storeu_si128((__m128i *)&elts[pos + 3]->rearm_data, rearm);
681                 /* C.1 combine data from mCQEs with rx_descriptor_fields1. */
682                 rxdf1 = _mm_shuffle_epi8(mcqe2, shuf_mask1);
683                 rxdf2 = _mm_shuffle_epi8(mcqe2, shuf_mask2);
684                 rxdf1 = _mm_sub_epi16(rxdf1, crc_adj);
685                 rxdf2 = _mm_sub_epi16(rxdf2, crc_adj);
686                 rxdf1 = _mm_blend_epi16(rxdf1, rxdf, 0x23);
687                 rxdf2 = _mm_blend_epi16(rxdf2, rxdf, 0x23);
688                 /* D.1 store rx_descriptor_fields1. */
689                 _mm_storeu_si128((__m128i *)
690                                   &elts[pos + 2]->rx_descriptor_fields1,
691                                  rxdf1);
692                 _mm_storeu_si128((__m128i *)
693                                   &elts[pos + 3]->rx_descriptor_fields1,
694                                  rxdf2);
695 #ifdef MLX5_PMD_SOFT_COUNTERS
696                 invalid_mask = _mm_set_epi64x(0,
697                                               (mcqe_n - pos) *
698                                               sizeof(uint16_t) * 8);
699                 invalid_mask = _mm_sll_epi64(ones, invalid_mask);
700                 mcqe1 = _mm_srli_si128(mcqe1, 4);
701                 byte_cnt = _mm_blend_epi16(mcqe1, mcqe2, 0xcc);
702                 byte_cnt = _mm_shuffle_epi8(byte_cnt, len_shuf_mask);
703                 byte_cnt = _mm_andnot_si128(invalid_mask, byte_cnt);
704                 byte_cnt = _mm_hadd_epi16(byte_cnt, zero);
705                 rcvd_byte += _mm_cvtsi128_si64(_mm_hadd_epi16(byte_cnt, zero));
706 #endif
707                 if (rxq->mark) {
708                         /* E.1 store flow tag (rte_flow mark). */
709                         elts[pos]->hash.fdir.hi = flow_tag;
710                         elts[pos + 1]->hash.fdir.hi = flow_tag;
711                         elts[pos + 2]->hash.fdir.hi = flow_tag;
712                         elts[pos + 3]->hash.fdir.hi = flow_tag;
713                 }
714                 pos += MLX5_VPMD_DESCS_PER_LOOP;
715                 /* Move to next CQE and invalidate consumed CQEs. */
716                 if (!(pos & 0x7) && pos < mcqe_n) {
717                         mcq = (void *)(cq + pos);
718                         for (i = 0; i < 8; ++i)
719                                 cq[inv++].op_own = MLX5_CQE_INVALIDATE;
720                 }
721         }
722         /* Invalidate the rest of CQEs. */
723         for (; inv < mcqe_n; ++inv)
724                 cq[inv].op_own = MLX5_CQE_INVALIDATE;
725 #ifdef MLX5_PMD_SOFT_COUNTERS
726         rxq->stats.ipackets += mcqe_n;
727         rxq->stats.ibytes += rcvd_byte;
728 #endif
729         rxq->cq_ci += mcqe_n;
730 }
731
732 /**
733  * Calculate packet type and offload flag for mbuf and store it.
734  *
735  * @param rxq
736  *   Pointer to RX queue structure.
737  * @param cqes[4]
738  *   Array of four 16bytes completions extracted from the original completion
739  *   descriptor.
740  * @param op_err
741  *   Opcode vector having responder error status. Each field is 4B.
742  * @param pkts
743  *   Pointer to array of packets to be filled.
744  */
745 static inline void
746 rxq_cq_to_ptype_oflags_v(struct rxq *rxq, __m128i cqes[4], __m128i op_err,
747                          struct rte_mbuf **pkts)
748 {
749         __m128i pinfo0, pinfo1;
750         __m128i pinfo, ptype;
751         __m128i ol_flags = _mm_set1_epi32(rxq->rss_hash * PKT_RX_RSS_HASH);
752         __m128i cv_flags;
753         const __m128i zero = _mm_setzero_si128();
754         const __m128i ptype_mask =
755                 _mm_set_epi32(0xfd06, 0xfd06, 0xfd06, 0xfd06);
756         const __m128i ptype_ol_mask =
757                 _mm_set_epi32(0x106, 0x106, 0x106, 0x106);
758         const __m128i pinfo_mask =
759                 _mm_set_epi32(0x3, 0x3, 0x3, 0x3);
760         const __m128i cv_flag_sel =
761                 _mm_set_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0,
762                              (uint8_t)((PKT_RX_IP_CKSUM_GOOD |
763                                         PKT_RX_L4_CKSUM_GOOD) >> 1),
764                              0,
765                              (uint8_t)(PKT_RX_L4_CKSUM_GOOD >> 1),
766                              0,
767                              (uint8_t)(PKT_RX_IP_CKSUM_GOOD >> 1),
768                              (uint8_t)(PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED),
769                              0);
770         const __m128i cv_mask =
771                 _mm_set_epi32(PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
772                               PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED,
773                               PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
774                               PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED,
775                               PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
776                               PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED,
777                               PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD |
778                               PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED);
779         const __m128i mbuf_init =
780                 _mm_loadl_epi64((__m128i *)&rxq->mbuf_initializer);
781         __m128i rearm0, rearm1, rearm2, rearm3;
782
783         /* Extract pkt_info field. */
784         pinfo0 = _mm_unpacklo_epi32(cqes[0], cqes[1]);
785         pinfo1 = _mm_unpacklo_epi32(cqes[2], cqes[3]);
786         pinfo = _mm_unpacklo_epi64(pinfo0, pinfo1);
787         /* Extract hdr_type_etc field. */
788         pinfo0 = _mm_unpackhi_epi32(cqes[0], cqes[1]);
789         pinfo1 = _mm_unpackhi_epi32(cqes[2], cqes[3]);
790         ptype = _mm_unpacklo_epi64(pinfo0, pinfo1);
791         if (rxq->mark) {
792                 const __m128i pinfo_ft_mask =
793                         _mm_set_epi32(0xffffff00, 0xffffff00,
794                                       0xffffff00, 0xffffff00);
795                 const __m128i fdir_flags = _mm_set1_epi32(PKT_RX_FDIR);
796                 const __m128i fdir_id_flags = _mm_set1_epi32(PKT_RX_FDIR_ID);
797                 __m128i flow_tag, invalid_mask;
798
799                 flow_tag = _mm_and_si128(pinfo, pinfo_ft_mask);
800                 /* Check if flow tag is non-zero then set PKT_RX_FDIR. */
801                 invalid_mask = _mm_cmpeq_epi32(flow_tag, zero);
802                 ol_flags = _mm_or_si128(ol_flags,
803                                         _mm_andnot_si128(invalid_mask,
804                                                          fdir_flags));
805                 /* Mask out invalid entries. */
806                 flow_tag = _mm_andnot_si128(invalid_mask, flow_tag);
807                 /* Check if flow tag MLX5_FLOW_MARK_DEFAULT. */
808                 ol_flags = _mm_or_si128(ol_flags,
809                                         _mm_andnot_si128(
810                                                 _mm_cmpeq_epi32(flow_tag,
811                                                                 pinfo_ft_mask),
812                                                 fdir_id_flags));
813         }
814         /*
815          * Merge the two fields to generate the following:
816          * bit[1]     = l3_ok
817          * bit[2]     = l4_ok
818          * bit[8]     = cv
819          * bit[11:10] = l3_hdr_type
820          * bit[14:12] = l4_hdr_type
821          * bit[15]    = ip_frag
822          * bit[16]    = tunneled
823          * bit[17]    = outer_l3_type
824          */
825         ptype = _mm_and_si128(ptype, ptype_mask);
826         pinfo = _mm_and_si128(pinfo, pinfo_mask);
827         pinfo = _mm_slli_epi32(pinfo, 16);
828         /* Make pinfo has merged fields for ol_flags calculation. */
829         pinfo = _mm_or_si128(ptype, pinfo);
830         ptype = _mm_srli_epi32(pinfo, 10);
831         ptype = _mm_packs_epi32(ptype, zero);
832         /* Errored packets will have RTE_PTYPE_ALL_MASK. */
833         op_err = _mm_srli_epi16(op_err, 8);
834         ptype = _mm_or_si128(ptype, op_err);
835         pkts[0]->packet_type = mlx5_ptype_table[_mm_extract_epi8(ptype, 0)];
836         pkts[1]->packet_type = mlx5_ptype_table[_mm_extract_epi8(ptype, 2)];
837         pkts[2]->packet_type = mlx5_ptype_table[_mm_extract_epi8(ptype, 4)];
838         pkts[3]->packet_type = mlx5_ptype_table[_mm_extract_epi8(ptype, 6)];
839         /* Fill flags for checksum and VLAN. */
840         pinfo = _mm_and_si128(pinfo, ptype_ol_mask);
841         pinfo = _mm_shuffle_epi8(cv_flag_sel, pinfo);
842         /* Locate checksum flags at byte[2:1] and merge with VLAN flags. */
843         cv_flags = _mm_slli_epi32(pinfo, 9);
844         cv_flags = _mm_or_si128(pinfo, cv_flags);
845         /* Move back flags to start from byte[0]. */
846         cv_flags = _mm_srli_epi32(cv_flags, 8);
847         /* Mask out garbage bits. */
848         cv_flags = _mm_and_si128(cv_flags, cv_mask);
849         /* Merge to ol_flags. */
850         ol_flags = _mm_or_si128(ol_flags, cv_flags);
851         /* Merge mbuf_init and ol_flags. */
852         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
853                          offsetof(struct rte_mbuf, rearm_data) + 8);
854         rearm0 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(ol_flags, 8), 0x30);
855         rearm1 = _mm_blend_epi16(mbuf_init, _mm_slli_si128(ol_flags, 4), 0x30);
856         rearm2 = _mm_blend_epi16(mbuf_init, ol_flags, 0x30);
857         rearm3 = _mm_blend_epi16(mbuf_init, _mm_srli_si128(ol_flags, 4), 0x30);
858         /* Write 8B rearm_data and 8B ol_flags. */
859         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, rearm_data) !=
860                          RTE_ALIGN(offsetof(struct rte_mbuf, rearm_data), 16));
861         _mm_store_si128((__m128i *)&pkts[0]->rearm_data, rearm0);
862         _mm_store_si128((__m128i *)&pkts[1]->rearm_data, rearm1);
863         _mm_store_si128((__m128i *)&pkts[2]->rearm_data, rearm2);
864         _mm_store_si128((__m128i *)&pkts[3]->rearm_data, rearm3);
865 }
866
867 /**
868  * Skip error packets.
869  *
870  * @param rxq
871  *   Pointer to RX queue structure.
872  * @param[out] pkts
873  *   Array to store received packets.
874  * @param pkts_n
875  *   Maximum number of packets in array.
876  *
877  * @return
878  *   Number of packets successfully received (<= pkts_n).
879  */
880 static uint16_t
881 rxq_handle_pending_error(struct rxq *rxq, struct rte_mbuf **pkts,
882                          uint16_t pkts_n)
883 {
884         uint16_t n = 0;
885         unsigned int i;
886 #ifdef MLX5_PMD_SOFT_COUNTERS
887         uint32_t err_bytes = 0;
888 #endif
889
890         for (i = 0; i < pkts_n; ++i) {
891                 struct rte_mbuf *pkt = pkts[i];
892
893                 if (pkt->packet_type == RTE_PTYPE_ALL_MASK) {
894 #ifdef MLX5_PMD_SOFT_COUNTERS
895                         err_bytes += PKT_LEN(pkt);
896 #endif
897                         rte_pktmbuf_free_seg(pkt);
898                 } else {
899                         pkts[n++] = pkt;
900                 }
901         }
902         rxq->stats.idropped += (pkts_n - n);
903 #ifdef MLX5_PMD_SOFT_COUNTERS
904         /* Correct counters of errored completions. */
905         rxq->stats.ipackets -= (pkts_n - n);
906         rxq->stats.ibytes -= err_bytes;
907 #endif
908         rxq->pending_err = 0;
909         return n;
910 }
911
912 /**
913  * Receive burst of packets. An errored completion also consumes a mbuf, but the
914  * packet_type is set to be RTE_PTYPE_ALL_MASK. Marked mbufs should be freed
915  * before returning to application.
916  *
917  * @param rxq
918  *   Pointer to RX queue structure.
919  * @param[out] pkts
920  *   Array to store received packets.
921  * @param pkts_n
922  *   Maximum number of packets in array.
923  *
924  * @return
925  *   Number of packets received including errors (<= pkts_n).
926  */
927 static inline uint16_t
928 rxq_burst_v(struct rxq *rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
929 {
930         const uint16_t q_n = 1 << rxq->cqe_n;
931         const uint16_t q_mask = q_n - 1;
932         volatile struct mlx5_cqe *cq;
933         struct rte_mbuf **elts;
934         unsigned int pos;
935         uint64_t n;
936         uint16_t repl_n;
937         uint64_t comp_idx = MLX5_VPMD_DESCS_PER_LOOP;
938         uint16_t nocmp_n = 0;
939         uint16_t rcvd_pkt = 0;
940         unsigned int cq_idx = rxq->cq_ci & q_mask;
941         unsigned int elts_idx;
942         unsigned int ownership = !!(rxq->cq_ci & (q_mask + 1));
943         const __m128i owner_check =
944                 _mm_set_epi64x(0x0100000001000000LL, 0x0100000001000000LL);
945         const __m128i opcode_check =
946                 _mm_set_epi64x(0xf0000000f0000000LL, 0xf0000000f0000000LL);
947         const __m128i format_check =
948                 _mm_set_epi64x(0x0c0000000c000000LL, 0x0c0000000c000000LL);
949         const __m128i resp_err_check =
950                 _mm_set_epi64x(0xe0000000e0000000LL, 0xe0000000e0000000LL);
951 #ifdef MLX5_PMD_SOFT_COUNTERS
952         uint32_t rcvd_byte = 0;
953         /* Mask to shuffle byte_cnt to add up stats. Do bswap16 for all. */
954         const __m128i len_shuf_mask =
955                 _mm_set_epi8(-1, -1, -1, -1,
956                              -1, -1, -1, -1,
957                              12, 13,  8,  9,
958                               4,  5,  0,  1);
959 #endif
960         /* Mask to shuffle from extracted CQE to mbuf. */
961         const __m128i shuf_mask =
962                 _mm_set_epi8(-1,  3,  2,  1, /* fdir.hi */
963                              12, 13, 14, 15, /* rss, bswap32 */
964                              10, 11,         /* vlan_tci, bswap16 */
965                               4,  5,         /* data_len, bswap16 */
966                              -1, -1,         /* zero out 2nd half of pkt_len */
967                               4,  5          /* pkt_len, bswap16 */);
968         /* Mask to blend from the last Qword to the first DQword. */
969         const __m128i blend_mask =
970                 _mm_set_epi8(-1, -1, -1, -1,
971                              -1, -1, -1, -1,
972                               0,  0,  0,  0,
973                               0,  0,  0, -1);
974         const __m128i zero = _mm_setzero_si128();
975         const __m128i ones = _mm_cmpeq_epi32(zero, zero);
976         const __m128i crc_adj =
977                 _mm_set_epi16(0, 0, 0, 0, 0,
978                               rxq->crc_present * ETHER_CRC_LEN,
979                               0,
980                               rxq->crc_present * ETHER_CRC_LEN);
981         const __m128i flow_mark_adj = _mm_set_epi32(rxq->mark * (-1), 0, 0, 0);
982
983         /* Compile time sanity check for this function. */
984         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
985                          offsetof(struct rte_mbuf, rx_descriptor_fields1) + 4);
986         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
987                          offsetof(struct rte_mbuf, rx_descriptor_fields1) + 8);
988         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, pkt_info) != 0);
989         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, rx_hash_res) !=
990                          offsetof(struct mlx5_cqe, pkt_info) + 12);
991         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, rsvd1) +
992                           sizeof(((struct mlx5_cqe *)0)->rsvd1) !=
993                          offsetof(struct mlx5_cqe, hdr_type_etc));
994         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, vlan_info) !=
995                          offsetof(struct mlx5_cqe, hdr_type_etc) + 2);
996         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, rsvd2) +
997                           sizeof(((struct mlx5_cqe *)0)->rsvd2) !=
998                          offsetof(struct mlx5_cqe, byte_cnt));
999         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, sop_drop_qpn) !=
1000                          RTE_ALIGN(offsetof(struct mlx5_cqe, sop_drop_qpn), 8));
1001         RTE_BUILD_BUG_ON(offsetof(struct mlx5_cqe, op_own) !=
1002                          offsetof(struct mlx5_cqe, sop_drop_qpn) + 7);
1003         assert(rxq->sges_n == 0);
1004         assert(rxq->cqe_n == rxq->elts_n);
1005         cq = &(*rxq->cqes)[cq_idx];
1006         rte_prefetch0(cq);
1007         rte_prefetch0(cq + 1);
1008         rte_prefetch0(cq + 2);
1009         rte_prefetch0(cq + 3);
1010         pkts_n = RTE_MIN(pkts_n, MLX5_VPMD_RX_MAX_BURST);
1011         /*
1012          * Order of indexes:
1013          *   rq_ci >= cq_ci >= rq_pi
1014          * Definition of indexes:
1015          *   rq_ci - cq_ci := # of buffers owned by HW (posted).
1016          *   cq_ci - rq_pi := # of buffers not returned to app (decompressed).
1017          *   N - (rq_ci - rq_pi) := # of buffers consumed (to be replenished).
1018          */
1019         repl_n = q_n - (rxq->rq_ci - rxq->rq_pi);
1020         if (repl_n >= MLX5_VPMD_RXQ_RPLNSH_THRESH)
1021                 rxq_replenish_bulk_mbuf(rxq, repl_n);
1022         /* See if there're unreturned mbufs from compressed CQE. */
1023         rcvd_pkt = rxq->cq_ci - rxq->rq_pi;
1024         if (rcvd_pkt > 0) {
1025                 rcvd_pkt = RTE_MIN(rcvd_pkt, pkts_n);
1026                 rxq_copy_mbuf_v(rxq, pkts, rcvd_pkt);
1027                 rxq->rq_pi += rcvd_pkt;
1028                 pkts += rcvd_pkt;
1029         }
1030         elts_idx = rxq->rq_pi & q_mask;
1031         elts = &(*rxq->elts)[elts_idx];
1032         /* Not to overflow pkts array. */
1033         pkts_n = RTE_ALIGN_FLOOR(pkts_n - rcvd_pkt, MLX5_VPMD_DESCS_PER_LOOP);
1034         /* Not to cross queue end. */
1035         pkts_n = RTE_MIN(pkts_n, q_n - elts_idx);
1036         if (!pkts_n)
1037                 return rcvd_pkt;
1038         /* At this point, there shouldn't be any remained packets. */
1039         assert(rxq->rq_pi == rxq->cq_ci);
1040         /*
1041          * A. load first Qword (8bytes) in one loop.
1042          * B. copy 4 mbuf pointers from elts ring to returing pkts.
1043          * C. load remained CQE data and extract necessary fields.
1044          *    Final 16bytes cqes[] extracted from original 64bytes CQE has the
1045          *    following structure:
1046          *        struct {
1047          *          uint8_t  pkt_info;
1048          *          uint8_t  flow_tag[3];
1049          *          uint16_t byte_cnt;
1050          *          uint8_t  rsvd4;
1051          *          uint8_t  op_own;
1052          *          uint16_t hdr_type_etc;
1053          *          uint16_t vlan_info;
1054          *          uint32_t rx_has_res;
1055          *        } c;
1056          * D. fill in mbuf.
1057          * E. get valid CQEs.
1058          * F. find compressed CQE.
1059          */
1060         for (pos = 0;
1061              pos < pkts_n;
1062              pos += MLX5_VPMD_DESCS_PER_LOOP) {
1063                 __m128i cqes[MLX5_VPMD_DESCS_PER_LOOP];
1064                 __m128i cqe_tmp1, cqe_tmp2;
1065                 __m128i pkt_mb0, pkt_mb1, pkt_mb2, pkt_mb3;
1066                 __m128i op_own, op_own_tmp1, op_own_tmp2;
1067                 __m128i opcode, owner_mask, invalid_mask;
1068                 __m128i comp_mask;
1069                 __m128i mask;
1070 #ifdef MLX5_PMD_SOFT_COUNTERS
1071                 __m128i byte_cnt;
1072 #endif
1073                 __m128i mbp1, mbp2;
1074                 __m128i p = _mm_set_epi16(0, 0, 0, 0, 3, 2, 1, 0);
1075                 unsigned int p1, p2, p3;
1076
1077                 /* Prefetch next 4 CQEs. */
1078                 if (pkts_n - pos >= 2 * MLX5_VPMD_DESCS_PER_LOOP) {
1079                         rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP]);
1080                         rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP + 1]);
1081                         rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP + 2]);
1082                         rte_prefetch0(&cq[pos + MLX5_VPMD_DESCS_PER_LOOP + 3]);
1083                 }
1084                 /* A.0 do not cross the end of CQ. */
1085                 mask = _mm_set_epi64x(0, (pkts_n - pos) * sizeof(uint16_t) * 8);
1086                 mask = _mm_sll_epi64(ones, mask);
1087                 p = _mm_andnot_si128(mask, p);
1088                 /* A.1 load cqes. */
1089                 p3 = _mm_extract_epi16(p, 3);
1090                 cqes[3] = _mm_loadl_epi64((__m128i *)
1091                                            &cq[pos + p3].sop_drop_qpn);
1092                 rte_compiler_barrier();
1093                 p2 = _mm_extract_epi16(p, 2);
1094                 cqes[2] = _mm_loadl_epi64((__m128i *)
1095                                            &cq[pos + p2].sop_drop_qpn);
1096                 rte_compiler_barrier();
1097                 /* B.1 load mbuf pointers. */
1098                 mbp1 = _mm_loadu_si128((__m128i *)&elts[pos]);
1099                 mbp2 = _mm_loadu_si128((__m128i *)&elts[pos + 2]);
1100                 /* A.1 load a block having op_own. */
1101                 p1 = _mm_extract_epi16(p, 1);
1102                 cqes[1] = _mm_loadl_epi64((__m128i *)
1103                                            &cq[pos + p1].sop_drop_qpn);
1104                 rte_compiler_barrier();
1105                 cqes[0] = _mm_loadl_epi64((__m128i *)
1106                                            &cq[pos].sop_drop_qpn);
1107                 /* B.2 copy mbuf pointers. */
1108                 _mm_storeu_si128((__m128i *)&pkts[pos], mbp1);
1109                 _mm_storeu_si128((__m128i *)&pkts[pos + 2], mbp2);
1110                 rte_compiler_barrier();
1111                 /* C.1 load remained CQE data and extract necessary fields. */
1112                 cqe_tmp2 = _mm_load_si128((__m128i *)&cq[pos + p3]);
1113                 cqe_tmp1 = _mm_load_si128((__m128i *)&cq[pos + p2]);
1114                 cqes[3] = _mm_blendv_epi8(cqes[3], cqe_tmp2, blend_mask);
1115                 cqes[2] = _mm_blendv_epi8(cqes[2], cqe_tmp1, blend_mask);
1116                 cqe_tmp2 = _mm_loadu_si128((__m128i *)&cq[pos + p3].rsvd1[3]);
1117                 cqe_tmp1 = _mm_loadu_si128((__m128i *)&cq[pos + p2].rsvd1[3]);
1118                 cqes[3] = _mm_blend_epi16(cqes[3], cqe_tmp2, 0x30);
1119                 cqes[2] = _mm_blend_epi16(cqes[2], cqe_tmp1, 0x30);
1120                 cqe_tmp2 = _mm_loadl_epi64((__m128i *)&cq[pos + p3].rsvd2[10]);
1121                 cqe_tmp1 = _mm_loadl_epi64((__m128i *)&cq[pos + p2].rsvd2[10]);
1122                 cqes[3] = _mm_blend_epi16(cqes[3], cqe_tmp2, 0x04);
1123                 cqes[2] = _mm_blend_epi16(cqes[2], cqe_tmp1, 0x04);
1124                 /* C.2 generate final structure for mbuf with swapping bytes. */
1125                 pkt_mb3 = _mm_shuffle_epi8(cqes[3], shuf_mask);
1126                 pkt_mb2 = _mm_shuffle_epi8(cqes[2], shuf_mask);
1127                 /* C.3 adjust CRC length. */
1128                 pkt_mb3 = _mm_sub_epi16(pkt_mb3, crc_adj);
1129                 pkt_mb2 = _mm_sub_epi16(pkt_mb2, crc_adj);
1130                 /* C.4 adjust flow mark. */
1131                 pkt_mb3 = _mm_add_epi32(pkt_mb3, flow_mark_adj);
1132                 pkt_mb2 = _mm_add_epi32(pkt_mb2, flow_mark_adj);
1133                 /* D.1 fill in mbuf - rx_descriptor_fields1. */
1134                 _mm_storeu_si128((void *)&pkts[pos + 3]->pkt_len, pkt_mb3);
1135                 _mm_storeu_si128((void *)&pkts[pos + 2]->pkt_len, pkt_mb2);
1136                 /* E.1 extract op_own field. */
1137                 op_own_tmp2 = _mm_unpacklo_epi32(cqes[2], cqes[3]);
1138                 /* C.1 load remained CQE data and extract necessary fields. */
1139                 cqe_tmp2 = _mm_load_si128((__m128i *)&cq[pos + p1]);
1140                 cqe_tmp1 = _mm_load_si128((__m128i *)&cq[pos]);
1141                 cqes[1] = _mm_blendv_epi8(cqes[1], cqe_tmp2, blend_mask);
1142                 cqes[0] = _mm_blendv_epi8(cqes[0], cqe_tmp1, blend_mask);
1143                 cqe_tmp2 = _mm_loadu_si128((__m128i *)&cq[pos + p1].rsvd1[3]);
1144                 cqe_tmp1 = _mm_loadu_si128((__m128i *)&cq[pos].rsvd1[3]);
1145                 cqes[1] = _mm_blend_epi16(cqes[1], cqe_tmp2, 0x30);
1146                 cqes[0] = _mm_blend_epi16(cqes[0], cqe_tmp1, 0x30);
1147                 cqe_tmp2 = _mm_loadl_epi64((__m128i *)&cq[pos + p1].rsvd2[10]);
1148                 cqe_tmp1 = _mm_loadl_epi64((__m128i *)&cq[pos].rsvd2[10]);
1149                 cqes[1] = _mm_blend_epi16(cqes[1], cqe_tmp2, 0x04);
1150                 cqes[0] = _mm_blend_epi16(cqes[0], cqe_tmp1, 0x04);
1151                 /* C.2 generate final structure for mbuf with swapping bytes. */
1152                 pkt_mb1 = _mm_shuffle_epi8(cqes[1], shuf_mask);
1153                 pkt_mb0 = _mm_shuffle_epi8(cqes[0], shuf_mask);
1154                 /* C.3 adjust CRC length. */
1155                 pkt_mb1 = _mm_sub_epi16(pkt_mb1, crc_adj);
1156                 pkt_mb0 = _mm_sub_epi16(pkt_mb0, crc_adj);
1157                 /* C.4 adjust flow mark. */
1158                 pkt_mb1 = _mm_add_epi32(pkt_mb1, flow_mark_adj);
1159                 pkt_mb0 = _mm_add_epi32(pkt_mb0, flow_mark_adj);
1160                 /* E.1 extract op_own byte. */
1161                 op_own_tmp1 = _mm_unpacklo_epi32(cqes[0], cqes[1]);
1162                 op_own = _mm_unpackhi_epi64(op_own_tmp1, op_own_tmp2);
1163                 /* D.1 fill in mbuf - rx_descriptor_fields1. */
1164                 _mm_storeu_si128((void *)&pkts[pos + 1]->pkt_len, pkt_mb1);
1165                 _mm_storeu_si128((void *)&pkts[pos]->pkt_len, pkt_mb0);
1166                 /* E.2 flip owner bit to mark CQEs from last round. */
1167                 owner_mask = _mm_and_si128(op_own, owner_check);
1168                 if (ownership)
1169                         owner_mask = _mm_xor_si128(owner_mask, owner_check);
1170                 owner_mask = _mm_cmpeq_epi32(owner_mask, owner_check);
1171                 owner_mask = _mm_packs_epi32(owner_mask, zero);
1172                 /* E.3 get mask for invalidated CQEs. */
1173                 opcode = _mm_and_si128(op_own, opcode_check);
1174                 invalid_mask = _mm_cmpeq_epi32(opcode_check, opcode);
1175                 invalid_mask = _mm_packs_epi32(invalid_mask, zero);
1176                 /* E.4 mask out beyond boundary. */
1177                 invalid_mask = _mm_or_si128(invalid_mask, mask);
1178                 /* E.5 merge invalid_mask with invalid owner. */
1179                 invalid_mask = _mm_or_si128(invalid_mask, owner_mask);
1180                 /* F.1 find compressed CQE format. */
1181                 comp_mask = _mm_and_si128(op_own, format_check);
1182                 comp_mask = _mm_cmpeq_epi32(comp_mask, format_check);
1183                 comp_mask = _mm_packs_epi32(comp_mask, zero);
1184                 /* F.2 mask out invalid entries. */
1185                 comp_mask = _mm_andnot_si128(invalid_mask, comp_mask);
1186                 comp_idx = _mm_cvtsi128_si64(comp_mask);
1187                 /* F.3 get the first compressed CQE. */
1188                 comp_idx = comp_idx ?
1189                                 __builtin_ctzll(comp_idx) /
1190                                         (sizeof(uint16_t) * 8) :
1191                                 MLX5_VPMD_DESCS_PER_LOOP;
1192                 /* E.6 mask out entries after the compressed CQE. */
1193                 mask = _mm_set_epi64x(0, comp_idx * sizeof(uint16_t) * 8);
1194                 mask = _mm_sll_epi64(ones, mask);
1195                 invalid_mask = _mm_or_si128(invalid_mask, mask);
1196                 /* E.7 count non-compressed valid CQEs. */
1197                 n = _mm_cvtsi128_si64(invalid_mask);
1198                 n = n ? __builtin_ctzll(n) / (sizeof(uint16_t) * 8) :
1199                         MLX5_VPMD_DESCS_PER_LOOP;
1200                 nocmp_n += n;
1201                 /* D.2 get the final invalid mask. */
1202                 mask = _mm_set_epi64x(0, n * sizeof(uint16_t) * 8);
1203                 mask = _mm_sll_epi64(ones, mask);
1204                 invalid_mask = _mm_or_si128(invalid_mask, mask);
1205                 /* D.3 check error in opcode. */
1206                 opcode = _mm_cmpeq_epi32(resp_err_check, opcode);
1207                 opcode = _mm_packs_epi32(opcode, zero);
1208                 opcode = _mm_andnot_si128(invalid_mask, opcode);
1209                 /* D.4 mark if any error is set */
1210                 rxq->pending_err |= !!_mm_cvtsi128_si64(opcode);
1211                 /* D.5 fill in mbuf - rearm_data and packet_type. */
1212                 rxq_cq_to_ptype_oflags_v(rxq, cqes, opcode, &pkts[pos]);
1213 #ifdef MLX5_PMD_SOFT_COUNTERS
1214                 /* Add up received bytes count. */
1215                 byte_cnt = _mm_shuffle_epi8(op_own, len_shuf_mask);
1216                 byte_cnt = _mm_andnot_si128(invalid_mask, byte_cnt);
1217                 byte_cnt = _mm_hadd_epi16(byte_cnt, zero);
1218                 rcvd_byte += _mm_cvtsi128_si64(_mm_hadd_epi16(byte_cnt, zero));
1219 #endif
1220                 /*
1221                  * Break the loop unless more valid CQE is expected, or if
1222                  * there's a compressed CQE.
1223                  */
1224                 if (n != MLX5_VPMD_DESCS_PER_LOOP)
1225                         break;
1226         }
1227         /* If no new CQE seen, return without updating cq_db. */
1228         if (unlikely(!nocmp_n && comp_idx == MLX5_VPMD_DESCS_PER_LOOP))
1229                 return rcvd_pkt;
1230         /* Update the consumer indexes for non-compressed CQEs. */
1231         assert(nocmp_n <= pkts_n);
1232         rxq->cq_ci += nocmp_n;
1233         rxq->rq_pi += nocmp_n;
1234         rcvd_pkt += nocmp_n;
1235 #ifdef MLX5_PMD_SOFT_COUNTERS
1236         rxq->stats.ipackets += nocmp_n;
1237         rxq->stats.ibytes += rcvd_byte;
1238 #endif
1239         /* Decompress the last CQE if compressed. */
1240         if (comp_idx < MLX5_VPMD_DESCS_PER_LOOP && comp_idx == n) {
1241                 assert(comp_idx == (nocmp_n % MLX5_VPMD_DESCS_PER_LOOP));
1242                 rxq_cq_decompress_v(rxq, &cq[nocmp_n], &elts[nocmp_n]);
1243                 /* Return more packets if needed. */
1244                 if (nocmp_n < pkts_n) {
1245                         uint16_t n = rxq->cq_ci - rxq->rq_pi;
1246
1247                         n = RTE_MIN(n, pkts_n - nocmp_n);
1248                         rxq_copy_mbuf_v(rxq, &pkts[nocmp_n], n);
1249                         rxq->rq_pi += n;
1250                         rcvd_pkt += n;
1251                 }
1252         }
1253         rte_wmb();
1254         *rxq->cq_db = htonl(rxq->cq_ci);
1255         return rcvd_pkt;
1256 }
1257
1258 /**
1259  * DPDK callback for vectorized RX.
1260  *
1261  * @param dpdk_rxq
1262  *   Generic pointer to RX queue structure.
1263  * @param[out] pkts
1264  *   Array to store received packets.
1265  * @param pkts_n
1266  *   Maximum number of packets in array.
1267  *
1268  * @return
1269  *   Number of packets successfully received (<= pkts_n).
1270  */
1271 uint16_t
1272 mlx5_rx_burst_vec(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1273 {
1274         struct rxq *rxq = dpdk_rxq;
1275         uint16_t nb_rx;
1276
1277         nb_rx = rxq_burst_v(rxq, pkts, pkts_n);
1278         if (unlikely(rxq->pending_err))
1279                 nb_rx = rxq_handle_pending_error(rxq, pkts, nb_rx);
1280         return nb_rx;
1281 }
1282
1283 /**
1284  * Check Tx queue flags are set for raw vectorized Tx.
1285  *
1286  * @param priv
1287  *   Pointer to private structure.
1288  *
1289  * @return
1290  *   1 if supported, negative errno value if not.
1291  */
1292 int __attribute__((cold))
1293 priv_check_raw_vec_tx_support(struct priv *priv)
1294 {
1295         uint16_t i;
1296
1297         /* All the configured queues should support. */
1298         for (i = 0; i < priv->txqs_n; ++i) {
1299                 struct txq *txq = (*priv->txqs)[i];
1300
1301                 if (!(txq->flags & ETH_TXQ_FLAGS_NOMULTSEGS) ||
1302                     !(txq->flags & ETH_TXQ_FLAGS_NOOFFLOADS))
1303                         break;
1304         }
1305         if (i != priv->txqs_n)
1306                 return -ENOTSUP;
1307         return 1;
1308 }
1309
1310 /**
1311  * Check a device can support vectorized TX.
1312  *
1313  * @param priv
1314  *   Pointer to private structure.
1315  *
1316  * @return
1317  *   1 if supported, negative errno value if not.
1318  */
1319 int __attribute__((cold))
1320 priv_check_vec_tx_support(struct priv *priv)
1321 {
1322         if (!priv->tx_vec_en ||
1323             priv->txqs_n > MLX5_VPMD_MIN_TXQS ||
1324             priv->mps != MLX5_MPW_ENHANCED ||
1325             priv->tso)
1326                 return -ENOTSUP;
1327         return 1;
1328 }
1329
1330 /**
1331  * Check a RX queue can support vectorized RX.
1332  *
1333  * @param rxq
1334  *   Pointer to RX queue.
1335  *
1336  * @return
1337  *   1 if supported, negative errno value if not.
1338  */
1339 int __attribute__((cold))
1340 rxq_check_vec_support(struct rxq *rxq)
1341 {
1342         struct rxq_ctrl *ctrl = container_of(rxq, struct rxq_ctrl, rxq);
1343
1344         if (!ctrl->priv->rx_vec_en || rxq->sges_n != 0)
1345                 return -ENOTSUP;
1346         return 1;
1347 }
1348
1349 /**
1350  * Check a device can support vectorized RX.
1351  *
1352  * @param priv
1353  *   Pointer to private structure.
1354  *
1355  * @return
1356  *   1 if supported, negative errno value if not.
1357  */
1358 int __attribute__((cold))
1359 priv_check_vec_rx_support(struct priv *priv)
1360 {
1361         uint16_t i;
1362
1363         if (!priv->rx_vec_en)
1364                 return -ENOTSUP;
1365         /* All the configured queues should support. */
1366         for (i = 0; i < priv->rxqs_n; ++i) {
1367                 struct rxq *rxq = (*priv->rxqs)[i];
1368
1369                 if (rxq_check_vec_support(rxq) < 0)
1370                         break;
1371         }
1372         if (i != priv->rxqs_n)
1373                 return -ENOTSUP;
1374         return 1;
1375 }