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