net/mlx5: use work queue buffer as a raw buffer
[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 *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 struct mlx5_wqe_data_seg *dseg = NULL;
390                 uint32_t length;
391                 unsigned int ds = 0;
392                 uintptr_t addr;
393                 uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE;
394                 uint8_t ehdr[2];
395 #ifdef MLX5_PMD_SOFT_COUNTERS
396                 uint32_t total_length = 0;
397 #endif
398
399                 /* first_seg */
400                 buf = *(pkts++);
401                 segs_n = buf->nb_segs;
402                 /*
403                  * Make sure there is enough room to store this packet and
404                  * that one ring entry remains unused.
405                  */
406                 assert(segs_n);
407                 if (max < segs_n + 1)
408                         break;
409                 max -= segs_n;
410                 --segs_n;
411                 if (!segs_n)
412                         --pkts_n;
413                 wqe = (volatile struct mlx5_wqe *)
414                         tx_mlx5_wqe(txq, txq->wqe_ci);
415                 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
416                 if (pkts_n > 1)
417                         rte_prefetch0(*pkts);
418                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
419                 length = DATA_LEN(buf);
420                 ehdr[0] = ((uint8_t *)addr)[0];
421                 ehdr[1] = ((uint8_t *)addr)[1];
422 #ifdef MLX5_PMD_SOFT_COUNTERS
423                 total_length = length;
424 #endif
425                 assert(length >= MLX5_WQE_DWORD_SIZE);
426                 /* Update element. */
427                 (*txq->elts)[elts_head] = buf;
428                 elts_head = (elts_head + 1) & (elts_n - 1);
429                 /* Prefetch next buffer data. */
430                 if (pkts_n > 1) {
431                         volatile void *pkt_addr;
432
433                         pkt_addr = rte_pktmbuf_mtod(*pkts, volatile void *);
434                         rte_prefetch0(pkt_addr);
435                 }
436                 /* Should we enable HW CKSUM offload */
437                 if (buf->ol_flags &
438                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
439                         wqe->eseg.cs_flags =
440                                 MLX5_ETH_WQE_L3_CSUM |
441                                 MLX5_ETH_WQE_L4_CSUM;
442                 } else {
443                         wqe->eseg.cs_flags = 0;
444                 }
445                 raw = ((uint8_t *)(uintptr_t)wqe) + 2 * MLX5_WQE_DWORD_SIZE;
446                 /*
447                  * Start by copying the Ethernet header minus the first two
448                  * bytes which will be appended at the end of the Ethernet
449                  * segment.
450                  */
451                 memcpy((uint8_t *)raw, ((uint8_t *)addr) + 2, 16);
452                 length -= MLX5_WQE_DWORD_SIZE;
453                 addr += MLX5_WQE_DWORD_SIZE;
454                 /* Replace the Ethernet type by the VLAN if necessary. */
455                 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
456                         uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
457
458                         memcpy((uint8_t *)(raw + MLX5_WQE_DWORD_SIZE - 2 -
459                                            sizeof(vlan)),
460                                &vlan, sizeof(vlan));
461                         addr -= sizeof(vlan);
462                         length += sizeof(vlan);
463                 }
464                 /* Inline if enough room. */
465                 if (txq->max_inline != 0) {
466                         uintptr_t end = (uintptr_t)
467                                 (((uintptr_t)txq->wqes) +
468                                  (1 << txq->wqe_n) * MLX5_WQE_SIZE);
469                         uint16_t max_inline =
470                                 txq->max_inline * RTE_CACHE_LINE_SIZE;
471                         uint16_t room;
472
473                         /*
474                          * raw starts two bytes before the boundary to
475                          * continue the above copy of packet data.
476                          */
477                         raw += MLX5_WQE_DWORD_SIZE - 2;
478                         room = end - (uintptr_t)raw;
479                         if (room > max_inline) {
480                                 uintptr_t addr_end = (addr + max_inline) &
481                                         ~(RTE_CACHE_LINE_SIZE - 1);
482                                 uint16_t copy_b = ((addr_end - addr) > length) ?
483                                                   length :
484                                                   (addr_end - addr);
485
486                                 rte_memcpy((void *)raw, (void *)addr, copy_b);
487                                 addr += copy_b;
488                                 length -= copy_b;
489                                 pkt_inline_sz += copy_b;
490                                 /* Sanity check. */
491                                 assert(addr <= addr_end);
492                         }
493                         /*
494                          * 2 DWORDs consumed by the WQE header + 1 DSEG +
495                          * the size of the inline part of the packet.
496                          */
497                         ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
498                         if (length > 0) {
499                                 dseg = (volatile struct mlx5_wqe_data_seg *)
500                                         ((uintptr_t)wqe +
501                                          (ds * MLX5_WQE_DWORD_SIZE));
502                                 if ((uintptr_t)dseg >= end)
503                                         dseg = (volatile struct
504                                                 mlx5_wqe_data_seg *)
505                                                txq->wqes;
506                                 goto use_dseg;
507                         } else if (!segs_n) {
508                                 goto next_pkt;
509                         } else {
510                                 goto next_seg;
511                         }
512                 } else {
513                         /*
514                          * No inline has been done in the packet, only the
515                          * Ethernet Header as been stored.
516                          */
517                         wqe->eseg.inline_hdr_sz = htons(MLX5_WQE_DWORD_SIZE);
518                         dseg = (volatile struct mlx5_wqe_data_seg *)
519                                 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
520                         ds = 3;
521 use_dseg:
522                         /* Add the remaining packet as a simple ds. */
523                         *dseg = (volatile struct mlx5_wqe_data_seg) {
524                                 .addr = htonll(addr),
525                                 .byte_count = htonl(length),
526                                 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
527                         };
528                         ++ds;
529                         if (!segs_n)
530                                 goto next_pkt;
531                 }
532 next_seg:
533                 assert(buf);
534                 assert(ds);
535                 assert(wqe);
536                 /*
537                  * Spill on next WQE when the current one does not have
538                  * enough room left. Size of WQE must a be a multiple
539                  * of data segment size.
540                  */
541                 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
542                 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
543                         unsigned int n = (txq->wqe_ci + ((ds + 3) / 4)) &
544                                 ((1 << txq->wqe_n) - 1);
545
546                         dseg = (volatile struct mlx5_wqe_data_seg *)
547                                tx_mlx5_wqe(txq, n);
548                         rte_prefetch0(tx_mlx5_wqe(txq, n + 1));
549                 } else {
550                         ++dseg;
551                 }
552                 ++ds;
553                 buf = buf->next;
554                 assert(buf);
555                 length = DATA_LEN(buf);
556 #ifdef MLX5_PMD_SOFT_COUNTERS
557                 total_length += length;
558 #endif
559                 /* Store segment information. */
560                 *dseg = (volatile struct mlx5_wqe_data_seg) {
561                         .addr = htonll(rte_pktmbuf_mtod(buf, uintptr_t)),
562                         .byte_count = htonl(length),
563                         .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
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[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
577                 wqe->ctrl[1] = htonl(txq->qp_num_8s | ds);
578                 wqe->ctrl[2] = 0;
579                 wqe->ctrl[3] = 0;
580                 wqe->eseg.rsvd0 = 0;
581                 wqe->eseg.rsvd1 = 0;
582                 wqe->eseg.mss = 0;
583                 wqe->eseg.rsvd2 = 0;
584                 wqe->eseg.inline_hdr_sz = htons(pkt_inline_sz);
585                 wqe->eseg.inline_hdr[0] = ehdr[0];
586                 wqe->eseg.inline_hdr[1] = ehdr[1];
587                 txq->wqe_ci += (ds + 3) / 4;
588 #ifdef MLX5_PMD_SOFT_COUNTERS
589                 /* Increment sent bytes counter. */
590                 txq->stats.obytes += total_length;
591 #endif
592         } while (pkts_n);
593         /* Take a shortcut if nothing must be sent. */
594         if (unlikely(i == 0))
595                 return 0;
596         /* Check whether completion threshold has been reached. */
597         comp = txq->elts_comp + i + j;
598         if (comp >= MLX5_TX_COMP_THRESH) {
599                 /* Request completion on last WQE. */
600                 wqe->ctrl[2] = htonl(8);
601                 /* Save elts_head in unused "immediate" field of WQE. */
602                 wqe->ctrl[3] = elts_head;
603                 txq->elts_comp = 0;
604         } else {
605                 txq->elts_comp = comp;
606         }
607 #ifdef MLX5_PMD_SOFT_COUNTERS
608         /* Increment sent packets counter. */
609         txq->stats.opackets += i;
610 #endif
611         /* Ring QP doorbell. */
612         mlx5_tx_dbrec(txq);
613         txq->elts_head = elts_head;
614         return i;
615 }
616
617 /**
618  * Open a MPW session.
619  *
620  * @param txq
621  *   Pointer to TX queue structure.
622  * @param mpw
623  *   Pointer to MPW session structure.
624  * @param length
625  *   Packet length.
626  */
627 static inline void
628 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
629 {
630         uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
631         volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
632                 (volatile struct mlx5_wqe_data_seg (*)[])
633                 tx_mlx5_wqe(txq, idx + 1);
634
635         mpw->state = MLX5_MPW_STATE_OPENED;
636         mpw->pkts_n = 0;
637         mpw->len = length;
638         mpw->total_len = 0;
639         mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
640         mpw->wqe->eseg.mss = htons(length);
641         mpw->wqe->eseg.inline_hdr_sz = 0;
642         mpw->wqe->eseg.rsvd0 = 0;
643         mpw->wqe->eseg.rsvd1 = 0;
644         mpw->wqe->eseg.rsvd2 = 0;
645         mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
646                                   (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
647         mpw->wqe->ctrl[2] = 0;
648         mpw->wqe->ctrl[3] = 0;
649         mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
650                 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
651         mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
652                 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
653         mpw->data.dseg[2] = &(*dseg)[0];
654         mpw->data.dseg[3] = &(*dseg)[1];
655         mpw->data.dseg[4] = &(*dseg)[2];
656 }
657
658 /**
659  * Close a MPW session.
660  *
661  * @param txq
662  *   Pointer to TX queue structure.
663  * @param mpw
664  *   Pointer to MPW session structure.
665  */
666 static inline void
667 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
668 {
669         unsigned int num = mpw->pkts_n;
670
671         /*
672          * Store size in multiple of 16 bytes. Control and Ethernet segments
673          * count as 2.
674          */
675         mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
676         mpw->state = MLX5_MPW_STATE_CLOSED;
677         if (num < 3)
678                 ++txq->wqe_ci;
679         else
680                 txq->wqe_ci += 2;
681         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
682         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
683 }
684
685 /**
686  * DPDK callback for TX with MPW support.
687  *
688  * @param dpdk_txq
689  *   Generic pointer to TX queue structure.
690  * @param[in] pkts
691  *   Packets to transmit.
692  * @param pkts_n
693  *   Number of packets in array.
694  *
695  * @return
696  *   Number of packets successfully transmitted (<= pkts_n).
697  */
698 uint16_t
699 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
700 {
701         struct txq *txq = (struct txq *)dpdk_txq;
702         uint16_t elts_head = txq->elts_head;
703         const unsigned int elts_n = 1 << txq->elts_n;
704         unsigned int i = 0;
705         unsigned int j = 0;
706         unsigned int max;
707         unsigned int comp;
708         struct mlx5_mpw mpw = {
709                 .state = MLX5_MPW_STATE_CLOSED,
710         };
711
712         if (unlikely(!pkts_n))
713                 return 0;
714         /* Prefetch first packet cacheline. */
715         tx_prefetch_cqe(txq, txq->cq_ci);
716         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
717         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
718         /* Start processing. */
719         txq_complete(txq);
720         max = (elts_n - (elts_head - txq->elts_tail));
721         if (max > elts_n)
722                 max -= elts_n;
723         do {
724                 struct rte_mbuf *buf = *(pkts++);
725                 unsigned int elts_head_next;
726                 uint32_t length;
727                 unsigned int segs_n = buf->nb_segs;
728                 uint32_t cs_flags = 0;
729
730                 /*
731                  * Make sure there is enough room to store this packet and
732                  * that one ring entry remains unused.
733                  */
734                 assert(segs_n);
735                 if (max < segs_n + 1)
736                         break;
737                 /* Do not bother with large packets MPW cannot handle. */
738                 if (segs_n > MLX5_MPW_DSEG_MAX)
739                         break;
740                 max -= segs_n;
741                 --pkts_n;
742                 /* Should we enable HW CKSUM offload */
743                 if (buf->ol_flags &
744                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
745                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
746                 /* Retrieve packet information. */
747                 length = PKT_LEN(buf);
748                 assert(length);
749                 /* Start new session if packet differs. */
750                 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
751                     ((mpw.len != length) ||
752                      (segs_n != 1) ||
753                      (mpw.wqe->eseg.cs_flags != cs_flags)))
754                         mlx5_mpw_close(txq, &mpw);
755                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
756                         mlx5_mpw_new(txq, &mpw, length);
757                         mpw.wqe->eseg.cs_flags = cs_flags;
758                 }
759                 /* Multi-segment packets must be alone in their MPW. */
760                 assert((segs_n == 1) || (mpw.pkts_n == 0));
761 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
762                 length = 0;
763 #endif
764                 do {
765                         volatile struct mlx5_wqe_data_seg *dseg;
766                         uintptr_t addr;
767
768                         elts_head_next = (elts_head + 1) & (elts_n - 1);
769                         assert(buf);
770                         (*txq->elts)[elts_head] = buf;
771                         dseg = mpw.data.dseg[mpw.pkts_n];
772                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
773                         *dseg = (struct mlx5_wqe_data_seg){
774                                 .byte_count = htonl(DATA_LEN(buf)),
775                                 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
776                                 .addr = htonll(addr),
777                         };
778                         elts_head = elts_head_next;
779 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
780                         length += DATA_LEN(buf);
781 #endif
782                         buf = buf->next;
783                         ++mpw.pkts_n;
784                         ++j;
785                 } while (--segs_n);
786                 assert(length == mpw.len);
787                 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
788                         mlx5_mpw_close(txq, &mpw);
789                 elts_head = elts_head_next;
790 #ifdef MLX5_PMD_SOFT_COUNTERS
791                 /* Increment sent bytes counter. */
792                 txq->stats.obytes += length;
793 #endif
794                 ++i;
795         } while (pkts_n);
796         /* Take a shortcut if nothing must be sent. */
797         if (unlikely(i == 0))
798                 return 0;
799         /* Check whether completion threshold has been reached. */
800         /* "j" includes both packets and segments. */
801         comp = txq->elts_comp + j;
802         if (comp >= MLX5_TX_COMP_THRESH) {
803                 volatile struct mlx5_wqe *wqe = mpw.wqe;
804
805                 /* Request completion on last WQE. */
806                 wqe->ctrl[2] = htonl(8);
807                 /* Save elts_head in unused "immediate" field of WQE. */
808                 wqe->ctrl[3] = elts_head;
809                 txq->elts_comp = 0;
810         } else {
811                 txq->elts_comp = comp;
812         }
813 #ifdef MLX5_PMD_SOFT_COUNTERS
814         /* Increment sent packets counter. */
815         txq->stats.opackets += i;
816 #endif
817         /* Ring QP doorbell. */
818         if (mpw.state == MLX5_MPW_STATE_OPENED)
819                 mlx5_mpw_close(txq, &mpw);
820         mlx5_tx_dbrec(txq);
821         txq->elts_head = elts_head;
822         return i;
823 }
824
825 /**
826  * Open a MPW inline session.
827  *
828  * @param txq
829  *   Pointer to TX queue structure.
830  * @param mpw
831  *   Pointer to MPW session structure.
832  * @param length
833  *   Packet length.
834  */
835 static inline void
836 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
837 {
838         uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
839         struct mlx5_wqe_inl_small *inl;
840
841         mpw->state = MLX5_MPW_INL_STATE_OPENED;
842         mpw->pkts_n = 0;
843         mpw->len = length;
844         mpw->total_len = 0;
845         mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
846         mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
847                                   (txq->wqe_ci << 8) |
848                                   MLX5_OPCODE_TSO);
849         mpw->wqe->ctrl[2] = 0;
850         mpw->wqe->ctrl[3] = 0;
851         mpw->wqe->eseg.mss = htons(length);
852         mpw->wqe->eseg.inline_hdr_sz = 0;
853         mpw->wqe->eseg.cs_flags = 0;
854         mpw->wqe->eseg.rsvd0 = 0;
855         mpw->wqe->eseg.rsvd1 = 0;
856         mpw->wqe->eseg.rsvd2 = 0;
857         inl = (struct mlx5_wqe_inl_small *)
858                 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
859         mpw->data.raw = (uint8_t *)&inl->raw;
860 }
861
862 /**
863  * Close a MPW inline session.
864  *
865  * @param txq
866  *   Pointer to TX queue structure.
867  * @param mpw
868  *   Pointer to MPW session structure.
869  */
870 static inline void
871 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
872 {
873         unsigned int size;
874         struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
875                 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
876
877         size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
878         /*
879          * Store size in multiple of 16 bytes. Control and Ethernet segments
880          * count as 2.
881          */
882         mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
883         mpw->state = MLX5_MPW_STATE_CLOSED;
884         inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
885         txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
886 }
887
888 /**
889  * DPDK callback for TX with MPW inline support.
890  *
891  * @param dpdk_txq
892  *   Generic pointer to TX queue structure.
893  * @param[in] pkts
894  *   Packets to transmit.
895  * @param pkts_n
896  *   Number of packets in array.
897  *
898  * @return
899  *   Number of packets successfully transmitted (<= pkts_n).
900  */
901 uint16_t
902 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
903                          uint16_t pkts_n)
904 {
905         struct txq *txq = (struct txq *)dpdk_txq;
906         uint16_t elts_head = txq->elts_head;
907         const unsigned int elts_n = 1 << txq->elts_n;
908         unsigned int i = 0;
909         unsigned int j = 0;
910         unsigned int max;
911         unsigned int comp;
912         unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
913         struct mlx5_mpw mpw = {
914                 .state = MLX5_MPW_STATE_CLOSED,
915         };
916
917         if (unlikely(!pkts_n))
918                 return 0;
919         /* Prefetch first packet cacheline. */
920         tx_prefetch_cqe(txq, txq->cq_ci);
921         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
922         rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
923         /* Start processing. */
924         txq_complete(txq);
925         max = (elts_n - (elts_head - txq->elts_tail));
926         if (max > elts_n)
927                 max -= elts_n;
928         do {
929                 struct rte_mbuf *buf = *(pkts++);
930                 unsigned int elts_head_next;
931                 uintptr_t addr;
932                 uint32_t length;
933                 unsigned int segs_n = buf->nb_segs;
934                 uint32_t cs_flags = 0;
935
936                 /*
937                  * Make sure there is enough room to store this packet and
938                  * that one ring entry remains unused.
939                  */
940                 assert(segs_n);
941                 if (max < segs_n + 1)
942                         break;
943                 /* Do not bother with large packets MPW cannot handle. */
944                 if (segs_n > MLX5_MPW_DSEG_MAX)
945                         break;
946                 max -= segs_n;
947                 --pkts_n;
948                 /* Should we enable HW CKSUM offload */
949                 if (buf->ol_flags &
950                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
951                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
952                 /* Retrieve packet information. */
953                 length = PKT_LEN(buf);
954                 /* Start new session if packet differs. */
955                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
956                         if ((mpw.len != length) ||
957                             (segs_n != 1) ||
958                             (mpw.wqe->eseg.cs_flags != cs_flags))
959                                 mlx5_mpw_close(txq, &mpw);
960                 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
961                         if ((mpw.len != length) ||
962                             (segs_n != 1) ||
963                             (length > inline_room) ||
964                             (mpw.wqe->eseg.cs_flags != cs_flags)) {
965                                 mlx5_mpw_inline_close(txq, &mpw);
966                                 inline_room =
967                                         txq->max_inline * RTE_CACHE_LINE_SIZE;
968                         }
969                 }
970                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
971                         if ((segs_n != 1) ||
972                             (length > inline_room)) {
973                                 mlx5_mpw_new(txq, &mpw, length);
974                                 mpw.wqe->eseg.cs_flags = cs_flags;
975                         } else {
976                                 mlx5_mpw_inline_new(txq, &mpw, length);
977                                 mpw.wqe->eseg.cs_flags = cs_flags;
978                         }
979                 }
980                 /* Multi-segment packets must be alone in their MPW. */
981                 assert((segs_n == 1) || (mpw.pkts_n == 0));
982                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
983                         assert(inline_room ==
984                                txq->max_inline * RTE_CACHE_LINE_SIZE);
985 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
986                         length = 0;
987 #endif
988                         do {
989                                 volatile struct mlx5_wqe_data_seg *dseg;
990
991                                 elts_head_next =
992                                         (elts_head + 1) & (elts_n - 1);
993                                 assert(buf);
994                                 (*txq->elts)[elts_head] = buf;
995                                 dseg = mpw.data.dseg[mpw.pkts_n];
996                                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
997                                 *dseg = (struct mlx5_wqe_data_seg){
998                                         .byte_count = htonl(DATA_LEN(buf)),
999                                         .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1000                                         .addr = htonll(addr),
1001                                 };
1002                                 elts_head = elts_head_next;
1003 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1004                                 length += DATA_LEN(buf);
1005 #endif
1006                                 buf = buf->next;
1007                                 ++mpw.pkts_n;
1008                                 ++j;
1009                         } while (--segs_n);
1010                         assert(length == mpw.len);
1011                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1012                                 mlx5_mpw_close(txq, &mpw);
1013                 } else {
1014                         unsigned int max;
1015
1016                         assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1017                         assert(length <= inline_room);
1018                         assert(length == DATA_LEN(buf));
1019                         elts_head_next = (elts_head + 1) & (elts_n - 1);
1020                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
1021                         (*txq->elts)[elts_head] = buf;
1022                         /* Maximum number of bytes before wrapping. */
1023                         max = ((((uintptr_t)(txq->wqes)) +
1024                                 (1 << txq->wqe_n) *
1025                                 MLX5_WQE_SIZE) -
1026                                (uintptr_t)mpw.data.raw);
1027                         if (length > max) {
1028                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1029                                            (void *)addr,
1030                                            max);
1031                                 mpw.data.raw = (volatile void *)txq->wqes;
1032                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1033                                            (void *)(addr + max),
1034                                            length - max);
1035                                 mpw.data.raw += length - max;
1036                         } else {
1037                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1038                                            (void *)addr,
1039                                            length);
1040                                 mpw.data.raw += length;
1041                         }
1042                         if ((uintptr_t)mpw.data.raw ==
1043                             (uintptr_t)tx_mlx5_wqe(txq, 1 << txq->wqe_n))
1044                                 mpw.data.raw = (volatile void *)txq->wqes;
1045                         ++mpw.pkts_n;
1046                         ++j;
1047                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1048                                 mlx5_mpw_inline_close(txq, &mpw);
1049                                 inline_room =
1050                                         txq->max_inline * RTE_CACHE_LINE_SIZE;
1051                         } else {
1052                                 inline_room -= length;
1053                         }
1054                 }
1055                 mpw.total_len += length;
1056                 elts_head = elts_head_next;
1057 #ifdef MLX5_PMD_SOFT_COUNTERS
1058                 /* Increment sent bytes counter. */
1059                 txq->stats.obytes += length;
1060 #endif
1061                 ++i;
1062         } while (pkts_n);
1063         /* Take a shortcut if nothing must be sent. */
1064         if (unlikely(i == 0))
1065                 return 0;
1066         /* Check whether completion threshold has been reached. */
1067         /* "j" includes both packets and segments. */
1068         comp = txq->elts_comp + j;
1069         if (comp >= MLX5_TX_COMP_THRESH) {
1070                 volatile struct mlx5_wqe *wqe = mpw.wqe;
1071
1072                 /* Request completion on last WQE. */
1073                 wqe->ctrl[2] = htonl(8);
1074                 /* Save elts_head in unused "immediate" field of WQE. */
1075                 wqe->ctrl[3] = elts_head;
1076                 txq->elts_comp = 0;
1077         } else {
1078                 txq->elts_comp = comp;
1079         }
1080 #ifdef MLX5_PMD_SOFT_COUNTERS
1081         /* Increment sent packets counter. */
1082         txq->stats.opackets += i;
1083 #endif
1084         /* Ring QP doorbell. */
1085         if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1086                 mlx5_mpw_inline_close(txq, &mpw);
1087         else if (mpw.state == MLX5_MPW_STATE_OPENED)
1088                 mlx5_mpw_close(txq, &mpw);
1089         mlx5_tx_dbrec(txq);
1090         txq->elts_head = elts_head;
1091         return i;
1092 }
1093
1094 /**
1095  * Translate RX completion flags to packet type.
1096  *
1097  * @param[in] cqe
1098  *   Pointer to CQE.
1099  *
1100  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1101  *
1102  * @return
1103  *   Packet type for struct rte_mbuf.
1104  */
1105 static inline uint32_t
1106 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1107 {
1108         uint32_t pkt_type;
1109         uint8_t flags = cqe->l4_hdr_type_etc;
1110
1111         if (cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET)
1112                 pkt_type =
1113                         TRANSPOSE(flags,
1114                                   MLX5_CQE_RX_OUTER_IPV4_PACKET,
1115                                   RTE_PTYPE_L3_IPV4) |
1116                         TRANSPOSE(flags,
1117                                   MLX5_CQE_RX_OUTER_IPV6_PACKET,
1118                                   RTE_PTYPE_L3_IPV6) |
1119                         TRANSPOSE(flags,
1120                                   MLX5_CQE_RX_IPV4_PACKET,
1121                                   RTE_PTYPE_INNER_L3_IPV4) |
1122                         TRANSPOSE(flags,
1123                                   MLX5_CQE_RX_IPV6_PACKET,
1124                                   RTE_PTYPE_INNER_L3_IPV6);
1125         else
1126                 pkt_type =
1127                         TRANSPOSE(flags,
1128                                   MLX5_CQE_L3_HDR_TYPE_IPV6,
1129                                   RTE_PTYPE_L3_IPV6) |
1130                         TRANSPOSE(flags,
1131                                   MLX5_CQE_L3_HDR_TYPE_IPV4,
1132                                   RTE_PTYPE_L3_IPV4);
1133         return pkt_type;
1134 }
1135
1136 /**
1137  * Get size of the next packet for a given CQE. For compressed CQEs, the
1138  * consumer index is updated only once all packets of the current one have
1139  * been processed.
1140  *
1141  * @param rxq
1142  *   Pointer to RX queue.
1143  * @param cqe
1144  *   CQE to process.
1145  * @param[out] rss_hash
1146  *   Packet RSS Hash result.
1147  *
1148  * @return
1149  *   Packet size in bytes (0 if there is none), -1 in case of completion
1150  *   with error.
1151  */
1152 static inline int
1153 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1154                  uint16_t cqe_cnt, uint32_t *rss_hash)
1155 {
1156         struct rxq_zip *zip = &rxq->zip;
1157         uint16_t cqe_n = cqe_cnt + 1;
1158         int len = 0;
1159
1160         /* Process compressed data in the CQE and mini arrays. */
1161         if (zip->ai) {
1162                 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1163                         (volatile struct mlx5_mini_cqe8 (*)[8])
1164                         (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt]);
1165
1166                 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1167                 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1168                 if ((++zip->ai & 7) == 0) {
1169                         /*
1170                          * Increment consumer index to skip the number of
1171                          * CQEs consumed. Hardware leaves holes in the CQ
1172                          * ring for software use.
1173                          */
1174                         zip->ca = zip->na;
1175                         zip->na += 8;
1176                 }
1177                 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1178                         uint16_t idx = rxq->cq_ci + 1;
1179                         uint16_t end = zip->cq_ci;
1180
1181                         while (idx != end) {
1182                                 (*rxq->cqes)[idx & cqe_cnt].op_own =
1183                                         MLX5_CQE_INVALIDATE;
1184                                 ++idx;
1185                         }
1186                         rxq->cq_ci = zip->cq_ci;
1187                         zip->ai = 0;
1188                 }
1189         /* No compressed data, get next CQE and verify if it is compressed. */
1190         } else {
1191                 int ret;
1192                 int8_t op_own;
1193
1194                 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1195                 if (unlikely(ret == 1))
1196                         return 0;
1197                 ++rxq->cq_ci;
1198                 op_own = cqe->op_own;
1199                 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1200                         volatile struct mlx5_mini_cqe8 (*mc)[8] =
1201                                 (volatile struct mlx5_mini_cqe8 (*)[8])
1202                                 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1203                                                           cqe_cnt]);
1204
1205                         /* Fix endianness. */
1206                         zip->cqe_cnt = ntohl(cqe->byte_cnt);
1207                         /*
1208                          * Current mini array position is the one returned by
1209                          * check_cqe64().
1210                          *
1211                          * If completion comprises several mini arrays, as a
1212                          * special case the second one is located 7 CQEs after
1213                          * the initial CQE instead of 8 for subsequent ones.
1214                          */
1215                         zip->ca = rxq->cq_ci & cqe_cnt;
1216                         zip->na = zip->ca + 7;
1217                         /* Compute the next non compressed CQE. */
1218                         --rxq->cq_ci;
1219                         zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1220                         /* Get packet size to return. */
1221                         len = ntohl((*mc)[0].byte_cnt);
1222                         *rss_hash = ntohl((*mc)[0].rx_hash_result);
1223                         zip->ai = 1;
1224                 } else {
1225                         len = ntohl(cqe->byte_cnt);
1226                         *rss_hash = ntohl(cqe->rx_hash_res);
1227                 }
1228                 /* Error while receiving packet. */
1229                 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1230                         return -1;
1231         }
1232         return len;
1233 }
1234
1235 /**
1236  * Translate RX completion flags to offload flags.
1237  *
1238  * @param[in] rxq
1239  *   Pointer to RX queue structure.
1240  * @param[in] cqe
1241  *   Pointer to CQE.
1242  *
1243  * @return
1244  *   Offload flags (ol_flags) for struct rte_mbuf.
1245  */
1246 static inline uint32_t
1247 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1248 {
1249         uint32_t ol_flags = 0;
1250         uint8_t l3_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L3_HDR_TYPE_MASK;
1251         uint8_t l4_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L4_HDR_TYPE_MASK;
1252
1253         if ((l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV4) ||
1254             (l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV6))
1255                 ol_flags |= TRANSPOSE(cqe->hds_ip_ext,
1256                                       MLX5_CQE_L3_OK,
1257                                       PKT_RX_IP_CKSUM_GOOD);
1258         if ((l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP) ||
1259             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_EMP_ACK) ||
1260             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_ACK) ||
1261             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_UDP))
1262                 ol_flags |= TRANSPOSE(cqe->hds_ip_ext,
1263                                       MLX5_CQE_L4_OK,
1264                                       PKT_RX_L4_CKSUM_GOOD);
1265         if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1266                 ol_flags |=
1267                         TRANSPOSE(cqe->l4_hdr_type_etc,
1268                                   MLX5_CQE_RX_OUTER_IP_CSUM_OK,
1269                                   PKT_RX_IP_CKSUM_GOOD) |
1270                         TRANSPOSE(cqe->l4_hdr_type_etc,
1271                                   MLX5_CQE_RX_OUTER_TCP_UDP_CSUM_OK,
1272                                   PKT_RX_L4_CKSUM_GOOD);
1273         return ol_flags;
1274 }
1275
1276 /**
1277  * DPDK callback for RX.
1278  *
1279  * @param dpdk_rxq
1280  *   Generic pointer to RX queue structure.
1281  * @param[out] pkts
1282  *   Array to store received packets.
1283  * @param pkts_n
1284  *   Maximum number of packets in array.
1285  *
1286  * @return
1287  *   Number of packets successfully received (<= pkts_n).
1288  */
1289 uint16_t
1290 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1291 {
1292         struct rxq *rxq = dpdk_rxq;
1293         const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1294         const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1295         const unsigned int sges_n = rxq->sges_n;
1296         struct rte_mbuf *pkt = NULL;
1297         struct rte_mbuf *seg = NULL;
1298         volatile struct mlx5_cqe *cqe =
1299                 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1300         unsigned int i = 0;
1301         unsigned int rq_ci = rxq->rq_ci << sges_n;
1302         int len; /* keep its value across iterations. */
1303
1304         while (pkts_n) {
1305                 unsigned int idx = rq_ci & wqe_cnt;
1306                 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1307                 struct rte_mbuf *rep = (*rxq->elts)[idx];
1308                 uint32_t rss_hash_res = 0;
1309
1310                 if (pkt)
1311                         NEXT(seg) = rep;
1312                 seg = rep;
1313                 rte_prefetch0(seg);
1314                 rte_prefetch0(cqe);
1315                 rte_prefetch0(wqe);
1316                 rep = rte_mbuf_raw_alloc(rxq->mp);
1317                 if (unlikely(rep == NULL)) {
1318                         ++rxq->stats.rx_nombuf;
1319                         if (!pkt) {
1320                                 /*
1321                                  * no buffers before we even started,
1322                                  * bail out silently.
1323                                  */
1324                                 break;
1325                         }
1326                         while (pkt != seg) {
1327                                 assert(pkt != (*rxq->elts)[idx]);
1328                                 rep = NEXT(pkt);
1329                                 rte_mbuf_refcnt_set(pkt, 0);
1330                                 __rte_mbuf_raw_free(pkt);
1331                                 pkt = rep;
1332                         }
1333                         break;
1334                 }
1335                 if (!pkt) {
1336                         cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1337                         len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
1338                                                &rss_hash_res);
1339                         if (!len) {
1340                                 rte_mbuf_refcnt_set(rep, 0);
1341                                 __rte_mbuf_raw_free(rep);
1342                                 break;
1343                         }
1344                         if (unlikely(len == -1)) {
1345                                 /* RX error, packet is likely too large. */
1346                                 rte_mbuf_refcnt_set(rep, 0);
1347                                 __rte_mbuf_raw_free(rep);
1348                                 ++rxq->stats.idropped;
1349                                 goto skip;
1350                         }
1351                         pkt = seg;
1352                         assert(len >= (rxq->crc_present << 2));
1353                         /* Update packet information. */
1354                         pkt->packet_type = 0;
1355                         pkt->ol_flags = 0;
1356                         if (rss_hash_res && rxq->rss_hash) {
1357                                 pkt->hash.rss = rss_hash_res;
1358                                 pkt->ol_flags = PKT_RX_RSS_HASH;
1359                         }
1360                         if (rxq->mark &&
1361                             ((cqe->sop_drop_qpn !=
1362                               htonl(MLX5_FLOW_MARK_INVALID)) ||
1363                              (cqe->sop_drop_qpn !=
1364                               htonl(MLX5_FLOW_MARK_DEFAULT)))) {
1365                                 pkt->hash.fdir.hi =
1366                                         mlx5_flow_mark_get(cqe->sop_drop_qpn);
1367                                 pkt->ol_flags &= ~PKT_RX_RSS_HASH;
1368                                 pkt->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1369                         }
1370                         if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
1371                             rxq->crc_present) {
1372                                 if (rxq->csum) {
1373                                         pkt->packet_type =
1374                                                 rxq_cq_to_pkt_type(cqe);
1375                                         pkt->ol_flags |=
1376                                                 rxq_cq_to_ol_flags(rxq, cqe);
1377                                 }
1378                                 if (cqe->l4_hdr_type_etc &
1379                                     MLX5_CQE_VLAN_STRIPPED) {
1380                                         pkt->ol_flags |= PKT_RX_VLAN_PKT |
1381                                                 PKT_RX_VLAN_STRIPPED;
1382                                         pkt->vlan_tci = ntohs(cqe->vlan_info);
1383                                 }
1384                                 if (rxq->crc_present)
1385                                         len -= ETHER_CRC_LEN;
1386                         }
1387                         PKT_LEN(pkt) = len;
1388                 }
1389                 DATA_LEN(rep) = DATA_LEN(seg);
1390                 PKT_LEN(rep) = PKT_LEN(seg);
1391                 SET_DATA_OFF(rep, DATA_OFF(seg));
1392                 NB_SEGS(rep) = NB_SEGS(seg);
1393                 PORT(rep) = PORT(seg);
1394                 NEXT(rep) = NULL;
1395                 (*rxq->elts)[idx] = rep;
1396                 /*
1397                  * Fill NIC descriptor with the new buffer.  The lkey and size
1398                  * of the buffers are already known, only the buffer address
1399                  * changes.
1400                  */
1401                 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
1402                 if (len > DATA_LEN(seg)) {
1403                         len -= DATA_LEN(seg);
1404                         ++NB_SEGS(pkt);
1405                         ++rq_ci;
1406                         continue;
1407                 }
1408                 DATA_LEN(seg) = len;
1409 #ifdef MLX5_PMD_SOFT_COUNTERS
1410                 /* Increment bytes counter. */
1411                 rxq->stats.ibytes += PKT_LEN(pkt);
1412 #endif
1413                 /* Return packet. */
1414                 *(pkts++) = pkt;
1415                 pkt = NULL;
1416                 --pkts_n;
1417                 ++i;
1418 skip:
1419                 /* Align consumer index to the next stride. */
1420                 rq_ci >>= sges_n;
1421                 ++rq_ci;
1422                 rq_ci <<= sges_n;
1423         }
1424         if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1425                 return 0;
1426         /* Update the consumer index. */
1427         rxq->rq_ci = rq_ci >> sges_n;
1428         rte_wmb();
1429         *rxq->cq_db = htonl(rxq->cq_ci);
1430         rte_wmb();
1431         *rxq->rq_db = htonl(rxq->rq_ci);
1432 #ifdef MLX5_PMD_SOFT_COUNTERS
1433         /* Increment packets counter. */
1434         rxq->stats.ipackets += i;
1435 #endif
1436         return i;
1437 }
1438
1439 /**
1440  * Dummy DPDK callback for TX.
1441  *
1442  * This function is used to temporarily replace the real callback during
1443  * unsafe control operations on the queue, or in case of error.
1444  *
1445  * @param dpdk_txq
1446  *   Generic pointer to TX queue structure.
1447  * @param[in] pkts
1448  *   Packets to transmit.
1449  * @param pkts_n
1450  *   Number of packets in array.
1451  *
1452  * @return
1453  *   Number of packets successfully transmitted (<= pkts_n).
1454  */
1455 uint16_t
1456 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1457 {
1458         (void)dpdk_txq;
1459         (void)pkts;
1460         (void)pkts_n;
1461         return 0;
1462 }
1463
1464 /**
1465  * Dummy DPDK callback for RX.
1466  *
1467  * This function is used to temporarily replace the real callback during
1468  * unsafe control operations on the queue, or in case of error.
1469  *
1470  * @param dpdk_rxq
1471  *   Generic pointer to RX queue structure.
1472  * @param[out] pkts
1473  *   Array to store received packets.
1474  * @param pkts_n
1475  *   Maximum number of packets in array.
1476  *
1477  * @return
1478  *   Number of packets successfully received (<= pkts_n).
1479  */
1480 uint16_t
1481 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1482 {
1483         (void)dpdk_rxq;
1484         (void)pkts;
1485         (void)pkts_n;
1486         return 0;
1487 }