net/mlx5: use vector types to speed up processing
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-Wpedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #include <infiniband/mlx5_hw.h>
46 #include <infiniband/arch.h>
47 #ifdef PEDANTIC
48 #pragma GCC diagnostic error "-Wpedantic"
49 #endif
50
51 /* DPDK headers don't like -pedantic. */
52 #ifdef PEDANTIC
53 #pragma GCC diagnostic ignored "-Wpedantic"
54 #endif
55 #include <rte_mbuf.h>
56 #include <rte_mempool.h>
57 #include <rte_prefetch.h>
58 #include <rte_common.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_ether.h>
61 #ifdef PEDANTIC
62 #pragma GCC diagnostic error "-Wpedantic"
63 #endif
64
65 #include "mlx5.h"
66 #include "mlx5_utils.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_autoconf.h"
69 #include "mlx5_defs.h"
70 #include "mlx5_prm.h"
71
72 #ifndef NDEBUG
73
74 /**
75  * Verify or set magic value in CQE.
76  *
77  * @param cqe
78  *   Pointer to CQE.
79  *
80  * @return
81  *   0 the first time.
82  */
83 static inline int
84 check_cqe_seen(volatile struct mlx5_cqe *cqe)
85 {
86         static const uint8_t magic[] = "seen";
87         volatile uint8_t (*buf)[sizeof(cqe->rsvd0)] = &cqe->rsvd0;
88         int ret = 1;
89         unsigned int i;
90
91         for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
92                 if (!ret || (*buf)[i] != magic[i]) {
93                         ret = 0;
94                         (*buf)[i] = magic[i];
95                 }
96         return ret;
97 }
98
99 #endif /* NDEBUG */
100
101 static inline int
102 check_cqe(volatile struct mlx5_cqe *cqe,
103           unsigned int cqes_n, const uint16_t ci)
104           __attribute__((always_inline));
105
106 /**
107  * Check whether CQE is valid.
108  *
109  * @param cqe
110  *   Pointer to CQE.
111  * @param cqes_n
112  *   Size of completion queue.
113  * @param ci
114  *   Consumer index.
115  *
116  * @return
117  *   0 on success, 1 on failure.
118  */
119 static inline int
120 check_cqe(volatile struct mlx5_cqe *cqe,
121           unsigned int cqes_n, const uint16_t ci)
122 {
123         uint16_t idx = ci & cqes_n;
124         uint8_t op_own = cqe->op_own;
125         uint8_t op_owner = MLX5_CQE_OWNER(op_own);
126         uint8_t op_code = MLX5_CQE_OPCODE(op_own);
127
128         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
129                 return 1; /* No CQE. */
130 #ifndef NDEBUG
131         if ((op_code == MLX5_CQE_RESP_ERR) ||
132             (op_code == MLX5_CQE_REQ_ERR)) {
133                 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
134                 uint8_t syndrome = err_cqe->syndrome;
135
136                 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
137                     (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
138                         return 0;
139                 if (!check_cqe_seen(cqe))
140                         ERROR("unexpected CQE error %u (0x%02x)"
141                               " syndrome 0x%02x",
142                               op_code, op_code, syndrome);
143                 return 1;
144         } else if ((op_code != MLX5_CQE_RESP_SEND) &&
145                    (op_code != MLX5_CQE_REQ)) {
146                 if (!check_cqe_seen(cqe))
147                         ERROR("unexpected CQE opcode %u (0x%02x)",
148                               op_code, op_code);
149                 return 1;
150         }
151 #endif /* NDEBUG */
152         return 0;
153 }
154
155 /**
156  * Return the address of the WQE.
157  *
158  * @param txq
159  *   Pointer to TX queue structure.
160  * @param  wqe_ci
161  *   WQE consumer index.
162  *
163  * @return
164  *   WQE address.
165  */
166 static inline uintptr_t *
167 tx_mlx5_wqe(struct txq *txq, uint16_t ci)
168 {
169         ci &= ((1 << txq->wqe_n) - 1);
170         return (uintptr_t *)((uintptr_t)txq->wqes + ci * MLX5_WQE_SIZE);
171 }
172
173 static inline void
174 txq_complete(struct txq *txq) __attribute__((always_inline));
175
176 /**
177  * Manage TX completions.
178  *
179  * When sending a burst, mlx5_tx_burst() posts several WRs.
180  *
181  * @param txq
182  *   Pointer to TX queue structure.
183  */
184 static inline void
185 txq_complete(struct txq *txq)
186 {
187         const unsigned int elts_n = 1 << txq->elts_n;
188         const unsigned int cqe_n = 1 << txq->cqe_n;
189         const unsigned int cqe_cnt = cqe_n - 1;
190         uint16_t elts_free = txq->elts_tail;
191         uint16_t elts_tail;
192         uint16_t cq_ci = txq->cq_ci;
193         volatile struct mlx5_cqe *cqe = NULL;
194         volatile struct mlx5_wqe_ctrl *ctrl;
195
196         do {
197                 volatile struct mlx5_cqe *tmp;
198
199                 tmp = &(*txq->cqes)[cq_ci & cqe_cnt];
200                 if (check_cqe(tmp, cqe_n, cq_ci))
201                         break;
202                 cqe = tmp;
203 #ifndef NDEBUG
204                 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
205                         if (!check_cqe_seen(cqe))
206                                 ERROR("unexpected compressed CQE, TX stopped");
207                         return;
208                 }
209                 if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
210                     (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
211                         if (!check_cqe_seen(cqe))
212                                 ERROR("unexpected error CQE, TX stopped");
213                         return;
214                 }
215 #endif /* NDEBUG */
216                 ++cq_ci;
217         } while (1);
218         if (unlikely(cqe == NULL))
219                 return;
220         ctrl = (volatile struct mlx5_wqe_ctrl *)
221                 tx_mlx5_wqe(txq, ntohs(cqe->wqe_counter));
222         elts_tail = ctrl->ctrl3;
223         assert(elts_tail < (1 << txq->wqe_n));
224         /* Free buffers. */
225         while (elts_free != elts_tail) {
226                 struct rte_mbuf *elt = (*txq->elts)[elts_free];
227                 unsigned int elts_free_next =
228                         (elts_free + 1) & (elts_n - 1);
229                 struct rte_mbuf *elt_next = (*txq->elts)[elts_free_next];
230
231 #ifndef NDEBUG
232                 /* Poisoning. */
233                 memset(&(*txq->elts)[elts_free],
234                        0x66,
235                        sizeof((*txq->elts)[elts_free]));
236 #endif
237                 RTE_MBUF_PREFETCH_TO_FREE(elt_next);
238                 /* Only one segment needs to be freed. */
239                 rte_pktmbuf_free_seg(elt);
240                 elts_free = elts_free_next;
241         }
242         txq->cq_ci = cq_ci;
243         txq->elts_tail = elts_tail;
244         /* Update the consumer index. */
245         rte_wmb();
246         *txq->cq_db = htonl(cq_ci);
247 }
248
249 /**
250  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
251  * the cloned mbuf is allocated is returned instead.
252  *
253  * @param buf
254  *   Pointer to mbuf.
255  *
256  * @return
257  *   Memory pool where data is located for given mbuf.
258  */
259 static struct rte_mempool *
260 txq_mb2mp(struct rte_mbuf *buf)
261 {
262         if (unlikely(RTE_MBUF_INDIRECT(buf)))
263                 return rte_mbuf_from_indirect(buf)->pool;
264         return buf->pool;
265 }
266
267 static inline uint32_t
268 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
269         __attribute__((always_inline));
270
271 /**
272  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
273  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
274  * remove an entry first.
275  *
276  * @param txq
277  *   Pointer to TX queue structure.
278  * @param[in] mp
279  *   Memory Pool for which a Memory Region lkey must be returned.
280  *
281  * @return
282  *   mr->lkey on success, (uint32_t)-1 on failure.
283  */
284 static inline uint32_t
285 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
286 {
287         unsigned int i;
288         uint32_t lkey = (uint32_t)-1;
289
290         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
291                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
292                         /* Unknown MP, add a new MR for it. */
293                         break;
294                 }
295                 if (txq->mp2mr[i].mp == mp) {
296                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
297                         assert(htonl(txq->mp2mr[i].mr->lkey) ==
298                                txq->mp2mr[i].lkey);
299                         lkey = txq->mp2mr[i].lkey;
300                         break;
301                 }
302         }
303         if (unlikely(lkey == (uint32_t)-1))
304                 lkey = txq_mp2mr_reg(txq, mp, i);
305         return lkey;
306 }
307
308 /**
309  * Ring TX queue doorbell.
310  *
311  * @param txq
312  *   Pointer to TX queue structure.
313  */
314 static inline void
315 mlx5_tx_dbrec(struct txq *txq)
316 {
317         uint8_t *dst = (uint8_t *)((uintptr_t)txq->bf_reg + txq->bf_offset);
318         uint32_t data[4] = {
319                 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
320                 htonl(txq->qp_num_8s),
321                 0,
322                 0,
323         };
324         rte_wmb();
325         *txq->qp_db = htonl(txq->wqe_ci);
326         /* Ensure ordering between DB record and BF copy. */
327         rte_wmb();
328         memcpy(dst, (uint8_t *)data, 16);
329         txq->bf_offset ^= (1 << txq->bf_buf_size);
330 }
331
332 /**
333  * Prefetch a CQE.
334  *
335  * @param txq
336  *   Pointer to TX queue structure.
337  * @param cqe_ci
338  *   CQE consumer index.
339  */
340 static inline void
341 tx_prefetch_cqe(struct txq *txq, uint16_t ci)
342 {
343         volatile struct mlx5_cqe *cqe;
344
345         cqe = &(*txq->cqes)[ci & ((1 << txq->cqe_n) - 1)];
346         rte_prefetch0(cqe);
347 }
348
349 /**
350  * DPDK callback for TX.
351  *
352  * @param dpdk_txq
353  *   Generic pointer to TX queue structure.
354  * @param[in] pkts
355  *   Packets to transmit.
356  * @param pkts_n
357  *   Number of packets in array.
358  *
359  * @return
360  *   Number of packets successfully transmitted (<= pkts_n).
361  */
362 uint16_t
363 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
364 {
365         struct txq *txq = (struct txq *)dpdk_txq;
366         uint16_t elts_head = txq->elts_head;
367         const unsigned int elts_n = 1 << txq->elts_n;
368         unsigned int i = 0;
369         unsigned int j = 0;
370         unsigned int max;
371         unsigned int comp;
372         volatile struct mlx5_wqe_v *wqe = NULL;
373         unsigned int segs_n = 0;
374         struct rte_mbuf *buf = NULL;
375         uint8_t *raw;
376
377         if (unlikely(!pkts_n))
378                 return 0;
379         /* Prefetch first packet cacheline. */
380         tx_prefetch_cqe(txq, txq->cq_ci);
381         tx_prefetch_cqe(txq, txq->cq_ci + 1);
382         rte_prefetch0(*pkts);
383         /* Start processing. */
384         txq_complete(txq);
385         max = (elts_n - (elts_head - txq->elts_tail));
386         if (max > elts_n)
387                 max -= elts_n;
388         do {
389                 volatile rte_v128u32_t *dseg = NULL;
390                 uint32_t length;
391                 unsigned int ds = 0;
392                 uintptr_t addr;
393                 uint64_t naddr;
394                 uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE;
395                 uint8_t ehdr[2];
396                 uint8_t cs_flags = 0;
397 #ifdef MLX5_PMD_SOFT_COUNTERS
398                 uint32_t total_length = 0;
399 #endif
400
401                 /* first_seg */
402                 buf = *(pkts++);
403                 segs_n = buf->nb_segs;
404                 /*
405                  * Make sure there is enough room to store this packet and
406                  * that one ring entry remains unused.
407                  */
408                 assert(segs_n);
409                 if (max < segs_n + 1)
410                         break;
411                 max -= segs_n;
412                 --segs_n;
413                 if (!segs_n)
414                         --pkts_n;
415                 wqe = (volatile struct mlx5_wqe_v *)
416                         tx_mlx5_wqe(txq, txq->wqe_ci);
417                 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
418                 if (pkts_n > 1)
419                         rte_prefetch0(*pkts);
420                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
421                 length = DATA_LEN(buf);
422                 ehdr[0] = ((uint8_t *)addr)[0];
423                 ehdr[1] = ((uint8_t *)addr)[1];
424 #ifdef MLX5_PMD_SOFT_COUNTERS
425                 total_length = length;
426 #endif
427                 assert(length >= MLX5_WQE_DWORD_SIZE);
428                 /* Update element. */
429                 (*txq->elts)[elts_head] = buf;
430                 elts_head = (elts_head + 1) & (elts_n - 1);
431                 /* Prefetch next buffer data. */
432                 if (pkts_n > 1) {
433                         volatile void *pkt_addr;
434
435                         pkt_addr = rte_pktmbuf_mtod(*pkts, volatile void *);
436                         rte_prefetch0(pkt_addr);
437                 }
438                 /* Should we enable HW CKSUM offload */
439                 if (buf->ol_flags &
440                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
441                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
442                 }
443                 raw = ((uint8_t *)(uintptr_t)wqe) + 2 * MLX5_WQE_DWORD_SIZE;
444                 /*
445                  * Start by copying the Ethernet header minus the first two
446                  * bytes which will be appended at the end of the Ethernet
447                  * segment.
448                  */
449                 memcpy((uint8_t *)raw, ((uint8_t *)addr) + 2, 16);
450                 length -= MLX5_WQE_DWORD_SIZE;
451                 addr += MLX5_WQE_DWORD_SIZE;
452                 /* Replace the Ethernet type by the VLAN if necessary. */
453                 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
454                         uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
455
456                         memcpy((uint8_t *)(raw + MLX5_WQE_DWORD_SIZE - 2 -
457                                            sizeof(vlan)),
458                                &vlan, sizeof(vlan));
459                         addr -= sizeof(vlan);
460                         length += sizeof(vlan);
461                 }
462                 /* Inline if enough room. */
463                 if (txq->max_inline != 0) {
464                         uintptr_t end = (uintptr_t)
465                                 (((uintptr_t)txq->wqes) +
466                                  (1 << txq->wqe_n) * MLX5_WQE_SIZE);
467                         uint16_t max_inline =
468                                 txq->max_inline * RTE_CACHE_LINE_SIZE;
469                         uint16_t room;
470
471                         /*
472                          * raw starts two bytes before the boundary to
473                          * continue the above copy of packet data.
474                          */
475                         raw += MLX5_WQE_DWORD_SIZE - 2;
476                         room = end - (uintptr_t)raw;
477                         if (room > max_inline) {
478                                 uintptr_t addr_end = (addr + max_inline) &
479                                         ~(RTE_CACHE_LINE_SIZE - 1);
480                                 uint16_t copy_b = ((addr_end - addr) > length) ?
481                                                   length :
482                                                   (addr_end - addr);
483
484                                 rte_memcpy((void *)raw, (void *)addr, copy_b);
485                                 addr += copy_b;
486                                 length -= copy_b;
487                                 pkt_inline_sz += copy_b;
488                                 /* Sanity check. */
489                                 assert(addr <= addr_end);
490                         }
491                         /*
492                          * 2 DWORDs consumed by the WQE header + 1 DSEG +
493                          * the size of the inline part of the packet.
494                          */
495                         ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
496                         if (length > 0) {
497                                 dseg = (volatile rte_v128u32_t *)
498                                         ((uintptr_t)wqe +
499                                          (ds * MLX5_WQE_DWORD_SIZE));
500                                 if ((uintptr_t)dseg >= end)
501                                         dseg = (volatile rte_v128u32_t *)
502                                                txq->wqes;
503                                 goto use_dseg;
504                         } else if (!segs_n) {
505                                 goto next_pkt;
506                         } else {
507                                 goto next_seg;
508                         }
509                 } else {
510                         /*
511                          * No inline has been done in the packet, only the
512                          * Ethernet Header as been stored.
513                          */
514                         dseg = (volatile rte_v128u32_t *)
515                                 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
516                         ds = 3;
517 use_dseg:
518                         /* Add the remaining packet as a simple ds. */
519                         naddr = htonll(addr);
520                         *dseg = (rte_v128u32_t){
521                                 htonl(length),
522                                 txq_mp2mr(txq, txq_mb2mp(buf)),
523                                 naddr,
524                                 naddr >> 32,
525                         };
526                         ++ds;
527                         if (!segs_n)
528                                 goto next_pkt;
529                 }
530 next_seg:
531                 assert(buf);
532                 assert(ds);
533                 assert(wqe);
534                 /*
535                  * Spill on next WQE when the current one does not have
536                  * enough room left. Size of WQE must a be a multiple
537                  * of data segment size.
538                  */
539                 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
540                 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
541                         unsigned int n = (txq->wqe_ci + ((ds + 3) / 4)) &
542                                 ((1 << txq->wqe_n) - 1);
543
544                         dseg = (volatile rte_v128u32_t *)
545                                tx_mlx5_wqe(txq, n);
546                         rte_prefetch0(tx_mlx5_wqe(txq, n + 1));
547                 } else {
548                         ++dseg;
549                 }
550                 ++ds;
551                 buf = buf->next;
552                 assert(buf);
553                 length = DATA_LEN(buf);
554 #ifdef MLX5_PMD_SOFT_COUNTERS
555                 total_length += length;
556 #endif
557                 /* Store segment information. */
558                 naddr = htonll(rte_pktmbuf_mtod(buf, uintptr_t));
559                 *dseg = (rte_v128u32_t){
560                         htonl(length),
561                         txq_mp2mr(txq, txq_mb2mp(buf)),
562                         naddr,
563                         naddr >> 32,
564                 };
565                 (*txq->elts)[elts_head] = buf;
566                 elts_head = (elts_head + 1) & (elts_n - 1);
567                 ++j;
568                 --segs_n;
569                 if (segs_n)
570                         goto next_seg;
571                 else
572                         --pkts_n;
573 next_pkt:
574                 ++i;
575                 /* Initialize known and common part of the WQE structure. */
576                 wqe->ctrl = (rte_v128u32_t){
577                         htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
578                         htonl(txq->qp_num_8s | ds),
579                         0,
580                         0,
581                 };
582                 wqe->eseg = (rte_v128u32_t){
583                         0,
584                         cs_flags,
585                         0,
586                         (ehdr[1] << 24) | (ehdr[0] << 16) |
587                         htons(pkt_inline_sz),
588                 };
589                 txq->wqe_ci += (ds + 3) / 4;
590 #ifdef MLX5_PMD_SOFT_COUNTERS
591                 /* Increment sent bytes counter. */
592                 txq->stats.obytes += total_length;
593 #endif
594         } while (pkts_n);
595         /* Take a shortcut if nothing must be sent. */
596         if (unlikely(i == 0))
597                 return 0;
598         /* Check whether completion threshold has been reached. */
599         comp = txq->elts_comp + i + j;
600         if (comp >= MLX5_TX_COMP_THRESH) {
601                 volatile struct mlx5_wqe_ctrl *w =
602                         (volatile struct mlx5_wqe_ctrl *)wqe;
603
604                 /* Request completion on last WQE. */
605                 w->ctrl2 = htonl(8);
606                 /* Save elts_head in unused "immediate" field of WQE. */
607                 w->ctrl3 = elts_head;
608                 txq->elts_comp = 0;
609         } else {
610                 txq->elts_comp = comp;
611         }
612 #ifdef MLX5_PMD_SOFT_COUNTERS
613         /* Increment sent packets counter. */
614         txq->stats.opackets += i;
615 #endif
616         /* Ring QP doorbell. */
617         mlx5_tx_dbrec(txq);
618         txq->elts_head = elts_head;
619         return i;
620 }
621
622 /**
623  * Open a MPW session.
624  *
625  * @param txq
626  *   Pointer to TX queue structure.
627  * @param mpw
628  *   Pointer to MPW session structure.
629  * @param length
630  *   Packet length.
631  */
632 static inline void
633 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
634 {
635         uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
636         volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
637                 (volatile struct mlx5_wqe_data_seg (*)[])
638                 tx_mlx5_wqe(txq, idx + 1);
639
640         mpw->state = MLX5_MPW_STATE_OPENED;
641         mpw->pkts_n = 0;
642         mpw->len = length;
643         mpw->total_len = 0;
644         mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
645         mpw->wqe->eseg.mss = htons(length);
646         mpw->wqe->eseg.inline_hdr_sz = 0;
647         mpw->wqe->eseg.rsvd0 = 0;
648         mpw->wqe->eseg.rsvd1 = 0;
649         mpw->wqe->eseg.rsvd2 = 0;
650         mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
651                                   (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
652         mpw->wqe->ctrl[2] = 0;
653         mpw->wqe->ctrl[3] = 0;
654         mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
655                 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
656         mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
657                 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
658         mpw->data.dseg[2] = &(*dseg)[0];
659         mpw->data.dseg[3] = &(*dseg)[1];
660         mpw->data.dseg[4] = &(*dseg)[2];
661 }
662
663 /**
664  * Close a MPW session.
665  *
666  * @param txq
667  *   Pointer to TX queue structure.
668  * @param mpw
669  *   Pointer to MPW session structure.
670  */
671 static inline void
672 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
673 {
674         unsigned int num = mpw->pkts_n;
675
676         /*
677          * Store size in multiple of 16 bytes. Control and Ethernet segments
678          * count as 2.
679          */
680         mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
681         mpw->state = MLX5_MPW_STATE_CLOSED;
682         if (num < 3)
683                 ++txq->wqe_ci;
684         else
685                 txq->wqe_ci += 2;
686         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
687         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
688 }
689
690 /**
691  * DPDK callback for TX with MPW support.
692  *
693  * @param dpdk_txq
694  *   Generic pointer to TX queue structure.
695  * @param[in] pkts
696  *   Packets to transmit.
697  * @param pkts_n
698  *   Number of packets in array.
699  *
700  * @return
701  *   Number of packets successfully transmitted (<= pkts_n).
702  */
703 uint16_t
704 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
705 {
706         struct txq *txq = (struct txq *)dpdk_txq;
707         uint16_t elts_head = txq->elts_head;
708         const unsigned int elts_n = 1 << txq->elts_n;
709         unsigned int i = 0;
710         unsigned int j = 0;
711         unsigned int max;
712         unsigned int comp;
713         struct mlx5_mpw mpw = {
714                 .state = MLX5_MPW_STATE_CLOSED,
715         };
716
717         if (unlikely(!pkts_n))
718                 return 0;
719         /* Prefetch first packet cacheline. */
720         tx_prefetch_cqe(txq, txq->cq_ci);
721         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
722         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
723         /* Start processing. */
724         txq_complete(txq);
725         max = (elts_n - (elts_head - txq->elts_tail));
726         if (max > elts_n)
727                 max -= elts_n;
728         do {
729                 struct rte_mbuf *buf = *(pkts++);
730                 unsigned int elts_head_next;
731                 uint32_t length;
732                 unsigned int segs_n = buf->nb_segs;
733                 uint32_t cs_flags = 0;
734
735                 /*
736                  * Make sure there is enough room to store this packet and
737                  * that one ring entry remains unused.
738                  */
739                 assert(segs_n);
740                 if (max < segs_n + 1)
741                         break;
742                 /* Do not bother with large packets MPW cannot handle. */
743                 if (segs_n > MLX5_MPW_DSEG_MAX)
744                         break;
745                 max -= segs_n;
746                 --pkts_n;
747                 /* Should we enable HW CKSUM offload */
748                 if (buf->ol_flags &
749                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
750                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
751                 /* Retrieve packet information. */
752                 length = PKT_LEN(buf);
753                 assert(length);
754                 /* Start new session if packet differs. */
755                 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
756                     ((mpw.len != length) ||
757                      (segs_n != 1) ||
758                      (mpw.wqe->eseg.cs_flags != cs_flags)))
759                         mlx5_mpw_close(txq, &mpw);
760                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
761                         mlx5_mpw_new(txq, &mpw, length);
762                         mpw.wqe->eseg.cs_flags = cs_flags;
763                 }
764                 /* Multi-segment packets must be alone in their MPW. */
765                 assert((segs_n == 1) || (mpw.pkts_n == 0));
766 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
767                 length = 0;
768 #endif
769                 do {
770                         volatile struct mlx5_wqe_data_seg *dseg;
771                         uintptr_t addr;
772
773                         elts_head_next = (elts_head + 1) & (elts_n - 1);
774                         assert(buf);
775                         (*txq->elts)[elts_head] = buf;
776                         dseg = mpw.data.dseg[mpw.pkts_n];
777                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
778                         *dseg = (struct mlx5_wqe_data_seg){
779                                 .byte_count = htonl(DATA_LEN(buf)),
780                                 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
781                                 .addr = htonll(addr),
782                         };
783                         elts_head = elts_head_next;
784 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
785                         length += DATA_LEN(buf);
786 #endif
787                         buf = buf->next;
788                         ++mpw.pkts_n;
789                         ++j;
790                 } while (--segs_n);
791                 assert(length == mpw.len);
792                 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
793                         mlx5_mpw_close(txq, &mpw);
794                 elts_head = elts_head_next;
795 #ifdef MLX5_PMD_SOFT_COUNTERS
796                 /* Increment sent bytes counter. */
797                 txq->stats.obytes += length;
798 #endif
799                 ++i;
800         } while (pkts_n);
801         /* Take a shortcut if nothing must be sent. */
802         if (unlikely(i == 0))
803                 return 0;
804         /* Check whether completion threshold has been reached. */
805         /* "j" includes both packets and segments. */
806         comp = txq->elts_comp + j;
807         if (comp >= MLX5_TX_COMP_THRESH) {
808                 volatile struct mlx5_wqe *wqe = mpw.wqe;
809
810                 /* Request completion on last WQE. */
811                 wqe->ctrl[2] = htonl(8);
812                 /* Save elts_head in unused "immediate" field of WQE. */
813                 wqe->ctrl[3] = elts_head;
814                 txq->elts_comp = 0;
815         } else {
816                 txq->elts_comp = comp;
817         }
818 #ifdef MLX5_PMD_SOFT_COUNTERS
819         /* Increment sent packets counter. */
820         txq->stats.opackets += i;
821 #endif
822         /* Ring QP doorbell. */
823         if (mpw.state == MLX5_MPW_STATE_OPENED)
824                 mlx5_mpw_close(txq, &mpw);
825         mlx5_tx_dbrec(txq);
826         txq->elts_head = elts_head;
827         return i;
828 }
829
830 /**
831  * Open a MPW inline session.
832  *
833  * @param txq
834  *   Pointer to TX queue structure.
835  * @param mpw
836  *   Pointer to MPW session structure.
837  * @param length
838  *   Packet length.
839  */
840 static inline void
841 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
842 {
843         uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
844         struct mlx5_wqe_inl_small *inl;
845
846         mpw->state = MLX5_MPW_INL_STATE_OPENED;
847         mpw->pkts_n = 0;
848         mpw->len = length;
849         mpw->total_len = 0;
850         mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
851         mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
852                                   (txq->wqe_ci << 8) |
853                                   MLX5_OPCODE_TSO);
854         mpw->wqe->ctrl[2] = 0;
855         mpw->wqe->ctrl[3] = 0;
856         mpw->wqe->eseg.mss = htons(length);
857         mpw->wqe->eseg.inline_hdr_sz = 0;
858         mpw->wqe->eseg.cs_flags = 0;
859         mpw->wqe->eseg.rsvd0 = 0;
860         mpw->wqe->eseg.rsvd1 = 0;
861         mpw->wqe->eseg.rsvd2 = 0;
862         inl = (struct mlx5_wqe_inl_small *)
863                 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
864         mpw->data.raw = (uint8_t *)&inl->raw;
865 }
866
867 /**
868  * Close a MPW inline session.
869  *
870  * @param txq
871  *   Pointer to TX queue structure.
872  * @param mpw
873  *   Pointer to MPW session structure.
874  */
875 static inline void
876 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
877 {
878         unsigned int size;
879         struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
880                 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
881
882         size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
883         /*
884          * Store size in multiple of 16 bytes. Control and Ethernet segments
885          * count as 2.
886          */
887         mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
888         mpw->state = MLX5_MPW_STATE_CLOSED;
889         inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
890         txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
891 }
892
893 /**
894  * DPDK callback for TX with MPW inline support.
895  *
896  * @param dpdk_txq
897  *   Generic pointer to TX queue structure.
898  * @param[in] pkts
899  *   Packets to transmit.
900  * @param pkts_n
901  *   Number of packets in array.
902  *
903  * @return
904  *   Number of packets successfully transmitted (<= pkts_n).
905  */
906 uint16_t
907 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
908                          uint16_t pkts_n)
909 {
910         struct txq *txq = (struct txq *)dpdk_txq;
911         uint16_t elts_head = txq->elts_head;
912         const unsigned int elts_n = 1 << txq->elts_n;
913         unsigned int i = 0;
914         unsigned int j = 0;
915         unsigned int max;
916         unsigned int comp;
917         unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
918         struct mlx5_mpw mpw = {
919                 .state = MLX5_MPW_STATE_CLOSED,
920         };
921
922         if (unlikely(!pkts_n))
923                 return 0;
924         /* Prefetch first packet cacheline. */
925         tx_prefetch_cqe(txq, txq->cq_ci);
926         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
927         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
928         /* Start processing. */
929         txq_complete(txq);
930         max = (elts_n - (elts_head - txq->elts_tail));
931         if (max > elts_n)
932                 max -= elts_n;
933         do {
934                 struct rte_mbuf *buf = *(pkts++);
935                 unsigned int elts_head_next;
936                 uintptr_t addr;
937                 uint32_t length;
938                 unsigned int segs_n = buf->nb_segs;
939                 uint32_t cs_flags = 0;
940
941                 /*
942                  * Make sure there is enough room to store this packet and
943                  * that one ring entry remains unused.
944                  */
945                 assert(segs_n);
946                 if (max < segs_n + 1)
947                         break;
948                 /* Do not bother with large packets MPW cannot handle. */
949                 if (segs_n > MLX5_MPW_DSEG_MAX)
950                         break;
951                 max -= segs_n;
952                 --pkts_n;
953                 /* Should we enable HW CKSUM offload */
954                 if (buf->ol_flags &
955                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
956                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
957                 /* Retrieve packet information. */
958                 length = PKT_LEN(buf);
959                 /* Start new session if packet differs. */
960                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
961                         if ((mpw.len != length) ||
962                             (segs_n != 1) ||
963                             (mpw.wqe->eseg.cs_flags != cs_flags))
964                                 mlx5_mpw_close(txq, &mpw);
965                 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
966                         if ((mpw.len != length) ||
967                             (segs_n != 1) ||
968                             (length > inline_room) ||
969                             (mpw.wqe->eseg.cs_flags != cs_flags)) {
970                                 mlx5_mpw_inline_close(txq, &mpw);
971                                 inline_room =
972                                         txq->max_inline * RTE_CACHE_LINE_SIZE;
973                         }
974                 }
975                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
976                         if ((segs_n != 1) ||
977                             (length > inline_room)) {
978                                 mlx5_mpw_new(txq, &mpw, length);
979                                 mpw.wqe->eseg.cs_flags = cs_flags;
980                         } else {
981                                 mlx5_mpw_inline_new(txq, &mpw, length);
982                                 mpw.wqe->eseg.cs_flags = cs_flags;
983                         }
984                 }
985                 /* Multi-segment packets must be alone in their MPW. */
986                 assert((segs_n == 1) || (mpw.pkts_n == 0));
987                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
988                         assert(inline_room ==
989                                txq->max_inline * RTE_CACHE_LINE_SIZE);
990 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
991                         length = 0;
992 #endif
993                         do {
994                                 volatile struct mlx5_wqe_data_seg *dseg;
995
996                                 elts_head_next =
997                                         (elts_head + 1) & (elts_n - 1);
998                                 assert(buf);
999                                 (*txq->elts)[elts_head] = buf;
1000                                 dseg = mpw.data.dseg[mpw.pkts_n];
1001                                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1002                                 *dseg = (struct mlx5_wqe_data_seg){
1003                                         .byte_count = htonl(DATA_LEN(buf)),
1004                                         .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1005                                         .addr = htonll(addr),
1006                                 };
1007                                 elts_head = elts_head_next;
1008 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1009                                 length += DATA_LEN(buf);
1010 #endif
1011                                 buf = buf->next;
1012                                 ++mpw.pkts_n;
1013                                 ++j;
1014                         } while (--segs_n);
1015                         assert(length == mpw.len);
1016                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1017                                 mlx5_mpw_close(txq, &mpw);
1018                 } else {
1019                         unsigned int max;
1020
1021                         assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1022                         assert(length <= inline_room);
1023                         assert(length == DATA_LEN(buf));
1024                         elts_head_next = (elts_head + 1) & (elts_n - 1);
1025                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
1026                         (*txq->elts)[elts_head] = buf;
1027                         /* Maximum number of bytes before wrapping. */
1028                         max = ((((uintptr_t)(txq->wqes)) +
1029                                 (1 << txq->wqe_n) *
1030                                 MLX5_WQE_SIZE) -
1031                                (uintptr_t)mpw.data.raw);
1032                         if (length > max) {
1033                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1034                                            (void *)addr,
1035                                            max);
1036                                 mpw.data.raw = (volatile void *)txq->wqes;
1037                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1038                                            (void *)(addr + max),
1039                                            length - max);
1040                                 mpw.data.raw += length - max;
1041                         } else {
1042                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1043                                            (void *)addr,
1044                                            length);
1045                                 mpw.data.raw += length;
1046                         }
1047                         if ((uintptr_t)mpw.data.raw ==
1048                             (uintptr_t)tx_mlx5_wqe(txq, 1 << txq->wqe_n))
1049                                 mpw.data.raw = (volatile void *)txq->wqes;
1050                         ++mpw.pkts_n;
1051                         ++j;
1052                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1053                                 mlx5_mpw_inline_close(txq, &mpw);
1054                                 inline_room =
1055                                         txq->max_inline * RTE_CACHE_LINE_SIZE;
1056                         } else {
1057                                 inline_room -= length;
1058                         }
1059                 }
1060                 mpw.total_len += length;
1061                 elts_head = elts_head_next;
1062 #ifdef MLX5_PMD_SOFT_COUNTERS
1063                 /* Increment sent bytes counter. */
1064                 txq->stats.obytes += length;
1065 #endif
1066                 ++i;
1067         } while (pkts_n);
1068         /* Take a shortcut if nothing must be sent. */
1069         if (unlikely(i == 0))
1070                 return 0;
1071         /* Check whether completion threshold has been reached. */
1072         /* "j" includes both packets and segments. */
1073         comp = txq->elts_comp + j;
1074         if (comp >= MLX5_TX_COMP_THRESH) {
1075                 volatile struct mlx5_wqe *wqe = mpw.wqe;
1076
1077                 /* Request completion on last WQE. */
1078                 wqe->ctrl[2] = htonl(8);
1079                 /* Save elts_head in unused "immediate" field of WQE. */
1080                 wqe->ctrl[3] = elts_head;
1081                 txq->elts_comp = 0;
1082         } else {
1083                 txq->elts_comp = comp;
1084         }
1085 #ifdef MLX5_PMD_SOFT_COUNTERS
1086         /* Increment sent packets counter. */
1087         txq->stats.opackets += i;
1088 #endif
1089         /* Ring QP doorbell. */
1090         if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1091                 mlx5_mpw_inline_close(txq, &mpw);
1092         else if (mpw.state == MLX5_MPW_STATE_OPENED)
1093                 mlx5_mpw_close(txq, &mpw);
1094         mlx5_tx_dbrec(txq);
1095         txq->elts_head = elts_head;
1096         return i;
1097 }
1098
1099 /**
1100  * Translate RX completion flags to packet type.
1101  *
1102  * @param[in] cqe
1103  *   Pointer to CQE.
1104  *
1105  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1106  *
1107  * @return
1108  *   Packet type for struct rte_mbuf.
1109  */
1110 static inline uint32_t
1111 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1112 {
1113         uint32_t pkt_type;
1114         uint8_t flags = cqe->l4_hdr_type_etc;
1115
1116         if (cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET)
1117                 pkt_type =
1118                         TRANSPOSE(flags,
1119                                   MLX5_CQE_RX_OUTER_IPV4_PACKET,
1120                                   RTE_PTYPE_L3_IPV4) |
1121                         TRANSPOSE(flags,
1122                                   MLX5_CQE_RX_OUTER_IPV6_PACKET,
1123                                   RTE_PTYPE_L3_IPV6) |
1124                         TRANSPOSE(flags,
1125                                   MLX5_CQE_RX_IPV4_PACKET,
1126                                   RTE_PTYPE_INNER_L3_IPV4) |
1127                         TRANSPOSE(flags,
1128                                   MLX5_CQE_RX_IPV6_PACKET,
1129                                   RTE_PTYPE_INNER_L3_IPV6);
1130         else
1131                 pkt_type =
1132                         TRANSPOSE(flags,
1133                                   MLX5_CQE_L3_HDR_TYPE_IPV6,
1134                                   RTE_PTYPE_L3_IPV6) |
1135                         TRANSPOSE(flags,
1136                                   MLX5_CQE_L3_HDR_TYPE_IPV4,
1137                                   RTE_PTYPE_L3_IPV4);
1138         return pkt_type;
1139 }
1140
1141 /**
1142  * Get size of the next packet for a given CQE. For compressed CQEs, the
1143  * consumer index is updated only once all packets of the current one have
1144  * been processed.
1145  *
1146  * @param rxq
1147  *   Pointer to RX queue.
1148  * @param cqe
1149  *   CQE to process.
1150  * @param[out] rss_hash
1151  *   Packet RSS Hash result.
1152  *
1153  * @return
1154  *   Packet size in bytes (0 if there is none), -1 in case of completion
1155  *   with error.
1156  */
1157 static inline int
1158 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1159                  uint16_t cqe_cnt, uint32_t *rss_hash)
1160 {
1161         struct rxq_zip *zip = &rxq->zip;
1162         uint16_t cqe_n = cqe_cnt + 1;
1163         int len = 0;
1164
1165         /* Process compressed data in the CQE and mini arrays. */
1166         if (zip->ai) {
1167                 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1168                         (volatile struct mlx5_mini_cqe8 (*)[8])
1169                         (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt]);
1170
1171                 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1172                 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1173                 if ((++zip->ai & 7) == 0) {
1174                         /*
1175                          * Increment consumer index to skip the number of
1176                          * CQEs consumed. Hardware leaves holes in the CQ
1177                          * ring for software use.
1178                          */
1179                         zip->ca = zip->na;
1180                         zip->na += 8;
1181                 }
1182                 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1183                         uint16_t idx = rxq->cq_ci + 1;
1184                         uint16_t end = zip->cq_ci;
1185
1186                         while (idx != end) {
1187                                 (*rxq->cqes)[idx & cqe_cnt].op_own =
1188                                         MLX5_CQE_INVALIDATE;
1189                                 ++idx;
1190                         }
1191                         rxq->cq_ci = zip->cq_ci;
1192                         zip->ai = 0;
1193                 }
1194         /* No compressed data, get next CQE and verify if it is compressed. */
1195         } else {
1196                 int ret;
1197                 int8_t op_own;
1198
1199                 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1200                 if (unlikely(ret == 1))
1201                         return 0;
1202                 ++rxq->cq_ci;
1203                 op_own = cqe->op_own;
1204                 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1205                         volatile struct mlx5_mini_cqe8 (*mc)[8] =
1206                                 (volatile struct mlx5_mini_cqe8 (*)[8])
1207                                 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1208                                                           cqe_cnt]);
1209
1210                         /* Fix endianness. */
1211                         zip->cqe_cnt = ntohl(cqe->byte_cnt);
1212                         /*
1213                          * Current mini array position is the one returned by
1214                          * check_cqe64().
1215                          *
1216                          * If completion comprises several mini arrays, as a
1217                          * special case the second one is located 7 CQEs after
1218                          * the initial CQE instead of 8 for subsequent ones.
1219                          */
1220                         zip->ca = rxq->cq_ci & cqe_cnt;
1221                         zip->na = zip->ca + 7;
1222                         /* Compute the next non compressed CQE. */
1223                         --rxq->cq_ci;
1224                         zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1225                         /* Get packet size to return. */
1226                         len = ntohl((*mc)[0].byte_cnt);
1227                         *rss_hash = ntohl((*mc)[0].rx_hash_result);
1228                         zip->ai = 1;
1229                 } else {
1230                         len = ntohl(cqe->byte_cnt);
1231                         *rss_hash = ntohl(cqe->rx_hash_res);
1232                 }
1233                 /* Error while receiving packet. */
1234                 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1235                         return -1;
1236         }
1237         return len;
1238 }
1239
1240 /**
1241  * Translate RX completion flags to offload flags.
1242  *
1243  * @param[in] rxq
1244  *   Pointer to RX queue structure.
1245  * @param[in] cqe
1246  *   Pointer to CQE.
1247  *
1248  * @return
1249  *   Offload flags (ol_flags) for struct rte_mbuf.
1250  */
1251 static inline uint32_t
1252 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1253 {
1254         uint32_t ol_flags = 0;
1255         uint8_t l3_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L3_HDR_TYPE_MASK;
1256         uint8_t l4_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L4_HDR_TYPE_MASK;
1257
1258         if ((l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV4) ||
1259             (l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV6))
1260                 ol_flags |= TRANSPOSE(cqe->hds_ip_ext,
1261                                       MLX5_CQE_L3_OK,
1262                                       PKT_RX_IP_CKSUM_GOOD);
1263         if ((l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP) ||
1264             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_EMP_ACK) ||
1265             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_ACK) ||
1266             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_UDP))
1267                 ol_flags |= TRANSPOSE(cqe->hds_ip_ext,
1268                                       MLX5_CQE_L4_OK,
1269                                       PKT_RX_L4_CKSUM_GOOD);
1270         if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1271                 ol_flags |=
1272                         TRANSPOSE(cqe->l4_hdr_type_etc,
1273                                   MLX5_CQE_RX_OUTER_IP_CSUM_OK,
1274                                   PKT_RX_IP_CKSUM_GOOD) |
1275                         TRANSPOSE(cqe->l4_hdr_type_etc,
1276                                   MLX5_CQE_RX_OUTER_TCP_UDP_CSUM_OK,
1277                                   PKT_RX_L4_CKSUM_GOOD);
1278         return ol_flags;
1279 }
1280
1281 /**
1282  * DPDK callback for RX.
1283  *
1284  * @param dpdk_rxq
1285  *   Generic pointer to RX queue structure.
1286  * @param[out] pkts
1287  *   Array to store received packets.
1288  * @param pkts_n
1289  *   Maximum number of packets in array.
1290  *
1291  * @return
1292  *   Number of packets successfully received (<= pkts_n).
1293  */
1294 uint16_t
1295 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1296 {
1297         struct rxq *rxq = dpdk_rxq;
1298         const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1299         const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1300         const unsigned int sges_n = rxq->sges_n;
1301         struct rte_mbuf *pkt = NULL;
1302         struct rte_mbuf *seg = NULL;
1303         volatile struct mlx5_cqe *cqe =
1304                 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1305         unsigned int i = 0;
1306         unsigned int rq_ci = rxq->rq_ci << sges_n;
1307         int len; /* keep its value across iterations. */
1308
1309         while (pkts_n) {
1310                 unsigned int idx = rq_ci & wqe_cnt;
1311                 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1312                 struct rte_mbuf *rep = (*rxq->elts)[idx];
1313                 uint32_t rss_hash_res = 0;
1314
1315                 if (pkt)
1316                         NEXT(seg) = rep;
1317                 seg = rep;
1318                 rte_prefetch0(seg);
1319                 rte_prefetch0(cqe);
1320                 rte_prefetch0(wqe);
1321                 rep = rte_mbuf_raw_alloc(rxq->mp);
1322                 if (unlikely(rep == NULL)) {
1323                         ++rxq->stats.rx_nombuf;
1324                         if (!pkt) {
1325                                 /*
1326                                  * no buffers before we even started,
1327                                  * bail out silently.
1328                                  */
1329                                 break;
1330                         }
1331                         while (pkt != seg) {
1332                                 assert(pkt != (*rxq->elts)[idx]);
1333                                 rep = NEXT(pkt);
1334                                 rte_mbuf_refcnt_set(pkt, 0);
1335                                 __rte_mbuf_raw_free(pkt);
1336                                 pkt = rep;
1337                         }
1338                         break;
1339                 }
1340                 if (!pkt) {
1341                         cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1342                         len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
1343                                                &rss_hash_res);
1344                         if (!len) {
1345                                 rte_mbuf_refcnt_set(rep, 0);
1346                                 __rte_mbuf_raw_free(rep);
1347                                 break;
1348                         }
1349                         if (unlikely(len == -1)) {
1350                                 /* RX error, packet is likely too large. */
1351                                 rte_mbuf_refcnt_set(rep, 0);
1352                                 __rte_mbuf_raw_free(rep);
1353                                 ++rxq->stats.idropped;
1354                                 goto skip;
1355                         }
1356                         pkt = seg;
1357                         assert(len >= (rxq->crc_present << 2));
1358                         /* Update packet information. */
1359                         pkt->packet_type = 0;
1360                         pkt->ol_flags = 0;
1361                         if (rss_hash_res && rxq->rss_hash) {
1362                                 pkt->hash.rss = rss_hash_res;
1363                                 pkt->ol_flags = PKT_RX_RSS_HASH;
1364                         }
1365                         if (rxq->mark &&
1366                             ((cqe->sop_drop_qpn !=
1367                               htonl(MLX5_FLOW_MARK_INVALID)) ||
1368                              (cqe->sop_drop_qpn !=
1369                               htonl(MLX5_FLOW_MARK_DEFAULT)))) {
1370                                 pkt->hash.fdir.hi =
1371                                         mlx5_flow_mark_get(cqe->sop_drop_qpn);
1372                                 pkt->ol_flags &= ~PKT_RX_RSS_HASH;
1373                                 pkt->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1374                         }
1375                         if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
1376                             rxq->crc_present) {
1377                                 if (rxq->csum) {
1378                                         pkt->packet_type =
1379                                                 rxq_cq_to_pkt_type(cqe);
1380                                         pkt->ol_flags |=
1381                                                 rxq_cq_to_ol_flags(rxq, cqe);
1382                                 }
1383                                 if (cqe->l4_hdr_type_etc &
1384                                     MLX5_CQE_VLAN_STRIPPED) {
1385                                         pkt->ol_flags |= PKT_RX_VLAN_PKT |
1386                                                 PKT_RX_VLAN_STRIPPED;
1387                                         pkt->vlan_tci = ntohs(cqe->vlan_info);
1388                                 }
1389                                 if (rxq->crc_present)
1390                                         len -= ETHER_CRC_LEN;
1391                         }
1392                         PKT_LEN(pkt) = len;
1393                 }
1394                 DATA_LEN(rep) = DATA_LEN(seg);
1395                 PKT_LEN(rep) = PKT_LEN(seg);
1396                 SET_DATA_OFF(rep, DATA_OFF(seg));
1397                 NB_SEGS(rep) = NB_SEGS(seg);
1398                 PORT(rep) = PORT(seg);
1399                 NEXT(rep) = NULL;
1400                 (*rxq->elts)[idx] = rep;
1401                 /*
1402                  * Fill NIC descriptor with the new buffer.  The lkey and size
1403                  * of the buffers are already known, only the buffer address
1404                  * changes.
1405                  */
1406                 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
1407                 if (len > DATA_LEN(seg)) {
1408                         len -= DATA_LEN(seg);
1409                         ++NB_SEGS(pkt);
1410                         ++rq_ci;
1411                         continue;
1412                 }
1413                 DATA_LEN(seg) = len;
1414 #ifdef MLX5_PMD_SOFT_COUNTERS
1415                 /* Increment bytes counter. */
1416                 rxq->stats.ibytes += PKT_LEN(pkt);
1417 #endif
1418                 /* Return packet. */
1419                 *(pkts++) = pkt;
1420                 pkt = NULL;
1421                 --pkts_n;
1422                 ++i;
1423 skip:
1424                 /* Align consumer index to the next stride. */
1425                 rq_ci >>= sges_n;
1426                 ++rq_ci;
1427                 rq_ci <<= sges_n;
1428         }
1429         if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1430                 return 0;
1431         /* Update the consumer index. */
1432         rxq->rq_ci = rq_ci >> sges_n;
1433         rte_wmb();
1434         *rxq->cq_db = htonl(rxq->cq_ci);
1435         rte_wmb();
1436         *rxq->rq_db = htonl(rxq->rq_ci);
1437 #ifdef MLX5_PMD_SOFT_COUNTERS
1438         /* Increment packets counter. */
1439         rxq->stats.ipackets += i;
1440 #endif
1441         return i;
1442 }
1443
1444 /**
1445  * Dummy DPDK callback for TX.
1446  *
1447  * This function is used to temporarily replace the real callback during
1448  * unsafe control operations on the queue, or in case of error.
1449  *
1450  * @param dpdk_txq
1451  *   Generic pointer to TX queue structure.
1452  * @param[in] pkts
1453  *   Packets to transmit.
1454  * @param pkts_n
1455  *   Number of packets in array.
1456  *
1457  * @return
1458  *   Number of packets successfully transmitted (<= pkts_n).
1459  */
1460 uint16_t
1461 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1462 {
1463         (void)dpdk_txq;
1464         (void)pkts;
1465         (void)pkts_n;
1466         return 0;
1467 }
1468
1469 /**
1470  * Dummy DPDK callback for RX.
1471  *
1472  * This function is used to temporarily replace the real callback during
1473  * unsafe control operations on the queue, or in case of error.
1474  *
1475  * @param dpdk_rxq
1476  *   Generic pointer to RX queue structure.
1477  * @param[out] pkts
1478  *   Array to store received packets.
1479  * @param pkts_n
1480  *   Maximum number of packets in array.
1481  *
1482  * @return
1483  *   Number of packets successfully received (<= pkts_n).
1484  */
1485 uint16_t
1486 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1487 {
1488         (void)dpdk_rxq;
1489         (void)pkts;
1490         (void)pkts_n;
1491         return 0;
1492 }