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