net/mlx5: replace countdown with threshold for Tx completions
[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 "-pedantic"
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 "-pedantic"
49 #endif
50
51 /* DPDK headers don't like -pedantic. */
52 #ifdef PEDANTIC
53 #pragma GCC diagnostic ignored "-pedantic"
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 "-pedantic"
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 /**
156  * Manage TX completions.
157  *
158  * When sending a burst, mlx5_tx_burst() posts several WRs.
159  *
160  * @param txq
161  *   Pointer to TX queue structure.
162  */
163 static void
164 txq_complete(struct txq *txq)
165 {
166         const unsigned int elts_n = txq->elts_n;
167         const unsigned int cqe_n = txq->cqe_n;
168         const unsigned int cqe_cnt = cqe_n - 1;
169         uint16_t elts_free = txq->elts_tail;
170         uint16_t elts_tail;
171         uint16_t cq_ci = txq->cq_ci;
172         volatile struct mlx5_cqe64 *cqe = NULL;
173         volatile union mlx5_wqe *wqe;
174
175         do {
176                 volatile struct mlx5_cqe64 *tmp;
177
178                 tmp = &(*txq->cqes)[cq_ci & cqe_cnt].cqe64;
179                 if (check_cqe64(tmp, cqe_n, cq_ci))
180                         break;
181                 cqe = tmp;
182 #ifndef NDEBUG
183                 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
184                         if (!check_cqe64_seen(cqe))
185                                 ERROR("unexpected compressed CQE, TX stopped");
186                         return;
187                 }
188                 if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
189                     (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
190                         if (!check_cqe64_seen(cqe))
191                                 ERROR("unexpected error CQE, TX stopped");
192                         return;
193                 }
194 #endif /* NDEBUG */
195                 ++cq_ci;
196         } while (1);
197         if (unlikely(cqe == NULL))
198                 return;
199         wqe = &(*txq->wqes)[htons(cqe->wqe_counter) & (txq->wqe_n - 1)];
200         elts_tail = wqe->wqe.ctrl.data[3];
201         assert(elts_tail < txq->wqe_n);
202         /* Free buffers. */
203         while (elts_free != elts_tail) {
204                 struct rte_mbuf *elt = (*txq->elts)[elts_free];
205                 unsigned int elts_free_next =
206                         (elts_free + 1) & (elts_n - 1);
207                 struct rte_mbuf *elt_next = (*txq->elts)[elts_free_next];
208
209 #ifndef NDEBUG
210                 /* Poisoning. */
211                 memset(&(*txq->elts)[elts_free],
212                        0x66,
213                        sizeof((*txq->elts)[elts_free]));
214 #endif
215                 RTE_MBUF_PREFETCH_TO_FREE(elt_next);
216                 /* Only one segment needs to be freed. */
217                 rte_pktmbuf_free_seg(elt);
218                 elts_free = elts_free_next;
219         }
220         txq->cq_ci = cq_ci;
221         txq->elts_tail = elts_tail;
222         /* Update the consumer index. */
223         rte_wmb();
224         *txq->cq_db = htonl(cq_ci);
225 }
226
227 /**
228  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
229  * the cloned mbuf is allocated is returned instead.
230  *
231  * @param buf
232  *   Pointer to mbuf.
233  *
234  * @return
235  *   Memory pool where data is located for given mbuf.
236  */
237 static struct rte_mempool *
238 txq_mb2mp(struct rte_mbuf *buf)
239 {
240         if (unlikely(RTE_MBUF_INDIRECT(buf)))
241                 return rte_mbuf_from_indirect(buf)->pool;
242         return buf->pool;
243 }
244
245 static inline uint32_t
246 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
247         __attribute__((always_inline));
248
249 /**
250  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
251  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
252  * remove an entry first.
253  *
254  * @param txq
255  *   Pointer to TX queue structure.
256  * @param[in] mp
257  *   Memory Pool for which a Memory Region lkey must be returned.
258  *
259  * @return
260  *   mr->lkey on success, (uint32_t)-1 on failure.
261  */
262 static inline uint32_t
263 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
264 {
265         unsigned int i;
266         uint32_t lkey = (uint32_t)-1;
267
268         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
269                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
270                         /* Unknown MP, add a new MR for it. */
271                         break;
272                 }
273                 if (txq->mp2mr[i].mp == mp) {
274                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
275                         assert(htonl(txq->mp2mr[i].mr->lkey) ==
276                                txq->mp2mr[i].lkey);
277                         lkey = txq->mp2mr[i].lkey;
278                         break;
279                 }
280         }
281         if (unlikely(lkey == (uint32_t)-1))
282                 lkey = txq_mp2mr_reg(txq, mp, i);
283         return lkey;
284 }
285
286 /**
287  * Write a regular WQE.
288  *
289  * @param txq
290  *   Pointer to TX queue structure.
291  * @param wqe
292  *   Pointer to the WQE to fill.
293  * @param addr
294  *   Buffer data address.
295  * @param length
296  *   Packet length.
297  * @param lkey
298  *   Memory region lkey.
299  */
300 static inline void
301 mlx5_wqe_write(struct txq *txq, volatile union mlx5_wqe *wqe,
302                uintptr_t addr, uint32_t length, uint32_t lkey)
303 {
304         wqe->wqe.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
305         wqe->wqe.ctrl.data[1] = htonl((txq->qp_num_8s) | 4);
306         wqe->wqe.ctrl.data[3] = 0;
307         wqe->inl.eseg.rsvd0 = 0;
308         wqe->inl.eseg.rsvd1 = 0;
309         wqe->inl.eseg.mss = 0;
310         wqe->inl.eseg.rsvd2 = 0;
311         wqe->wqe.eseg.inline_hdr_sz = htons(MLX5_ETH_INLINE_HEADER_SIZE);
312         /* Copy the first 16 bytes into inline header. */
313         rte_memcpy((uint8_t *)(uintptr_t)wqe->wqe.eseg.inline_hdr_start,
314                    (uint8_t *)(uintptr_t)addr,
315                    MLX5_ETH_INLINE_HEADER_SIZE);
316         addr += MLX5_ETH_INLINE_HEADER_SIZE;
317         length -= MLX5_ETH_INLINE_HEADER_SIZE;
318         /* Store remaining data in data segment. */
319         wqe->wqe.dseg.byte_count = htonl(length);
320         wqe->wqe.dseg.lkey = lkey;
321         wqe->wqe.dseg.addr = htonll(addr);
322         /* Increment consumer index. */
323         ++txq->wqe_ci;
324 }
325
326 /**
327  * Write a regular WQE with VLAN.
328  *
329  * @param txq
330  *   Pointer to TX queue structure.
331  * @param wqe
332  *   Pointer to the WQE to fill.
333  * @param addr
334  *   Buffer data address.
335  * @param length
336  *   Packet length.
337  * @param lkey
338  *   Memory region lkey.
339  * @param vlan_tci
340  *   VLAN field to insert in packet.
341  */
342 static inline void
343 mlx5_wqe_write_vlan(struct txq *txq, volatile union mlx5_wqe *wqe,
344                     uintptr_t addr, uint32_t length, uint32_t lkey,
345                     uint16_t vlan_tci)
346 {
347         uint32_t vlan = htonl(0x81000000 | vlan_tci);
348
349         wqe->wqe.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
350         wqe->wqe.ctrl.data[1] = htonl((txq->qp_num_8s) | 4);
351         wqe->wqe.ctrl.data[3] = 0;
352         wqe->inl.eseg.rsvd0 = 0;
353         wqe->inl.eseg.rsvd1 = 0;
354         wqe->inl.eseg.mss = 0;
355         wqe->inl.eseg.rsvd2 = 0;
356         wqe->wqe.eseg.inline_hdr_sz = htons(MLX5_ETH_VLAN_INLINE_HEADER_SIZE);
357         /*
358          * Copy 12 bytes of source & destination MAC address.
359          * Copy 4 bytes of VLAN.
360          * Copy 2 bytes of Ether type.
361          */
362         rte_memcpy((uint8_t *)(uintptr_t)wqe->wqe.eseg.inline_hdr_start,
363                    (uint8_t *)(uintptr_t)addr, 12);
364         rte_memcpy((uint8_t *)((uintptr_t)wqe->wqe.eseg.inline_hdr_start + 12),
365                    &vlan, sizeof(vlan));
366         rte_memcpy((uint8_t *)((uintptr_t)wqe->wqe.eseg.inline_hdr_start + 16),
367                    (uint8_t *)((uintptr_t)addr + 12), 2);
368         addr += MLX5_ETH_VLAN_INLINE_HEADER_SIZE - sizeof(vlan);
369         length -= MLX5_ETH_VLAN_INLINE_HEADER_SIZE - sizeof(vlan);
370         /* Store remaining data in data segment. */
371         wqe->wqe.dseg.byte_count = htonl(length);
372         wqe->wqe.dseg.lkey = lkey;
373         wqe->wqe.dseg.addr = htonll(addr);
374         /* Increment consumer index. */
375         ++txq->wqe_ci;
376 }
377
378 /**
379  * Ring TX queue doorbell.
380  *
381  * @param txq
382  *   Pointer to TX queue structure.
383  */
384 static inline void
385 mlx5_tx_dbrec(struct txq *txq)
386 {
387         uint8_t *dst = (uint8_t *)((uintptr_t)txq->bf_reg + txq->bf_offset);
388         uint32_t data[4] = {
389                 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
390                 htonl(txq->qp_num_8s),
391                 0,
392                 0,
393         };
394         rte_wmb();
395         *txq->qp_db = htonl(txq->wqe_ci);
396         /* Ensure ordering between DB record and BF copy. */
397         rte_wmb();
398         rte_mov16(dst, (uint8_t *)data);
399         txq->bf_offset ^= txq->bf_buf_size;
400 }
401
402 /**
403  * Prefetch a CQE.
404  *
405  * @param txq
406  *   Pointer to TX queue structure.
407  * @param cqe_ci
408  *   CQE consumer index.
409  */
410 static inline void
411 tx_prefetch_cqe(struct txq *txq, uint16_t ci)
412 {
413         volatile struct mlx5_cqe64 *cqe;
414
415         cqe = &(*txq->cqes)[ci & (txq->cqe_n - 1)].cqe64;
416         rte_prefetch0(cqe);
417 }
418
419 /**
420  * DPDK callback for TX.
421  *
422  * @param dpdk_txq
423  *   Generic pointer to TX queue structure.
424  * @param[in] pkts
425  *   Packets to transmit.
426  * @param pkts_n
427  *   Number of packets in array.
428  *
429  * @return
430  *   Number of packets successfully transmitted (<= pkts_n).
431  */
432 uint16_t
433 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
434 {
435         struct txq *txq = (struct txq *)dpdk_txq;
436         uint16_t elts_head = txq->elts_head;
437         const unsigned int elts_n = txq->elts_n;
438         unsigned int i;
439         unsigned int max;
440         unsigned int comp;
441         volatile union mlx5_wqe *wqe;
442         struct rte_mbuf *buf;
443
444         if (unlikely(!pkts_n))
445                 return 0;
446         buf = pkts[0];
447         /* Prefetch first packet cacheline. */
448         tx_prefetch_cqe(txq, txq->cq_ci);
449         tx_prefetch_cqe(txq, txq->cq_ci + 1);
450         rte_prefetch0(buf);
451         /* Start processing. */
452         txq_complete(txq);
453         max = (elts_n - (elts_head - txq->elts_tail));
454         if (max > elts_n)
455                 max -= elts_n;
456         assert(max >= 1);
457         assert(max <= elts_n);
458         /* Always leave one free entry in the ring. */
459         --max;
460         if (max == 0)
461                 return 0;
462         if (max > pkts_n)
463                 max = pkts_n;
464         for (i = 0; (i != max); ++i) {
465                 unsigned int elts_head_next = (elts_head + 1) & (elts_n - 1);
466                 uintptr_t addr;
467                 uint32_t length;
468                 uint32_t lkey;
469
470                 wqe = &(*txq->wqes)[txq->wqe_ci & (txq->wqe_n - 1)];
471                 rte_prefetch0(wqe);
472                 if (i + 1 < max)
473                         rte_prefetch0(pkts[i + 1]);
474                 /* Retrieve buffer information. */
475                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
476                 length = DATA_LEN(buf);
477                 /* Update element. */
478                 (*txq->elts)[elts_head] = buf;
479                 /* Prefetch next buffer data. */
480                 if (i + 1 < max)
481                         rte_prefetch0(rte_pktmbuf_mtod(pkts[i + 1],
482                                                        volatile void *));
483                 /* Retrieve Memory Region key for this memory pool. */
484                 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
485                 if (buf->ol_flags & PKT_TX_VLAN_PKT)
486                         mlx5_wqe_write_vlan(txq, wqe, addr, length, lkey,
487                                             buf->vlan_tci);
488                 else
489                         mlx5_wqe_write(txq, wqe, addr, length, lkey);
490                 wqe->wqe.ctrl.data[2] = 0;
491                 /* Should we enable HW CKSUM offload */
492                 if (buf->ol_flags &
493                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
494                         wqe->wqe.eseg.cs_flags =
495                                 MLX5_ETH_WQE_L3_CSUM |
496                                 MLX5_ETH_WQE_L4_CSUM;
497                 } else {
498                         wqe->wqe.eseg.cs_flags = 0;
499                 }
500 #ifdef MLX5_PMD_SOFT_COUNTERS
501                 /* Increment sent bytes counter. */
502                 txq->stats.obytes += length;
503 #endif
504                 elts_head = elts_head_next;
505                 buf = pkts[i + 1];
506         }
507         /* Take a shortcut if nothing must be sent. */
508         if (unlikely(i == 0))
509                 return 0;
510         /* Check whether completion threshold has been reached. */
511         comp = txq->elts_comp + i;
512         if (comp >= MLX5_TX_COMP_THRESH) {
513                 /* Request completion on last WQE. */
514                 wqe->wqe.ctrl.data[2] = htonl(8);
515                 /* Save elts_head in unused "immediate" field of WQE. */
516                 wqe->wqe.ctrl.data[3] = elts_head;
517                 txq->elts_comp = 0;
518         } else {
519                 txq->elts_comp = comp;
520         }
521 #ifdef MLX5_PMD_SOFT_COUNTERS
522         /* Increment sent packets counter. */
523         txq->stats.opackets += i;
524 #endif
525         /* Ring QP doorbell. */
526         mlx5_tx_dbrec(txq);
527         txq->elts_head = elts_head;
528         return i;
529 }
530
531 /**
532  * Translate RX completion flags to packet type.
533  *
534  * @param[in] cqe
535  *   Pointer to CQE.
536  *
537  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
538  *
539  * @return
540  *   Packet type for struct rte_mbuf.
541  */
542 static inline uint32_t
543 rxq_cq_to_pkt_type(volatile struct mlx5_cqe64 *cqe)
544 {
545         uint32_t pkt_type;
546         uint8_t flags = cqe->l4_hdr_type_etc;
547         uint8_t info = cqe->rsvd0[0];
548
549         if (info & IBV_EXP_CQ_RX_TUNNEL_PACKET)
550                 pkt_type =
551                         TRANSPOSE(flags,
552                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
553                                   RTE_PTYPE_L3_IPV4) |
554                         TRANSPOSE(flags,
555                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
556                                   RTE_PTYPE_L3_IPV6) |
557                         TRANSPOSE(flags,
558                                   IBV_EXP_CQ_RX_IPV4_PACKET,
559                                   RTE_PTYPE_INNER_L3_IPV4) |
560                         TRANSPOSE(flags,
561                                   IBV_EXP_CQ_RX_IPV6_PACKET,
562                                   RTE_PTYPE_INNER_L3_IPV6);
563         else
564                 pkt_type =
565                         TRANSPOSE(flags,
566                                   MLX5_CQE_L3_HDR_TYPE_IPV6,
567                                   RTE_PTYPE_L3_IPV6) |
568                         TRANSPOSE(flags,
569                                   MLX5_CQE_L3_HDR_TYPE_IPV4,
570                                   RTE_PTYPE_L3_IPV4);
571         return pkt_type;
572 }
573
574 /**
575  * Get size of the next packet for a given CQE. For compressed CQEs, the
576  * consumer index is updated only once all packets of the current one have
577  * been processed.
578  *
579  * @param rxq
580  *   Pointer to RX queue.
581  * @param cqe
582  *   CQE to process.
583  *
584  * @return
585  *   Packet size in bytes (0 if there is none), -1 in case of completion
586  *   with error.
587  */
588 static inline int
589 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe,
590                  uint16_t cqe_cnt)
591 {
592         struct rxq_zip *zip = &rxq->zip;
593         uint16_t cqe_n = cqe_cnt + 1;
594         int len = 0;
595
596         /* Process compressed data in the CQE and mini arrays. */
597         if (zip->ai) {
598                 volatile struct mlx5_mini_cqe8 (*mc)[8] =
599                         (volatile struct mlx5_mini_cqe8 (*)[8])
600                         (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt].cqe64);
601
602                 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
603                 if ((++zip->ai & 7) == 0) {
604                         /*
605                          * Increment consumer index to skip the number of
606                          * CQEs consumed. Hardware leaves holes in the CQ
607                          * ring for software use.
608                          */
609                         zip->ca = zip->na;
610                         zip->na += 8;
611                 }
612                 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
613                         uint16_t idx = rxq->cq_ci;
614                         uint16_t end = zip->cq_ci;
615
616                         while (idx != end) {
617                                 (*rxq->cqes)[idx & cqe_cnt].cqe64.op_own =
618                                         MLX5_CQE_INVALIDATE;
619                                 ++idx;
620                         }
621                         rxq->cq_ci = zip->cq_ci;
622                         zip->ai = 0;
623                 }
624         /* No compressed data, get next CQE and verify if it is compressed. */
625         } else {
626                 int ret;
627                 int8_t op_own;
628
629                 ret = check_cqe64(cqe, cqe_n, rxq->cq_ci);
630                 if (unlikely(ret == 1))
631                         return 0;
632                 ++rxq->cq_ci;
633                 op_own = cqe->op_own;
634                 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
635                         volatile struct mlx5_mini_cqe8 (*mc)[8] =
636                                 (volatile struct mlx5_mini_cqe8 (*)[8])
637                                 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
638                                                           cqe_cnt].cqe64);
639
640                         /* Fix endianness. */
641                         zip->cqe_cnt = ntohl(cqe->byte_cnt);
642                         /*
643                          * Current mini array position is the one returned by
644                          * check_cqe64().
645                          *
646                          * If completion comprises several mini arrays, as a
647                          * special case the second one is located 7 CQEs after
648                          * the initial CQE instead of 8 for subsequent ones.
649                          */
650                         zip->ca = rxq->cq_ci & cqe_cnt;
651                         zip->na = zip->ca + 7;
652                         /* Compute the next non compressed CQE. */
653                         --rxq->cq_ci;
654                         zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
655                         /* Get packet size to return. */
656                         len = ntohl((*mc)[0].byte_cnt);
657                         zip->ai = 1;
658                 } else {
659                         len = ntohl(cqe->byte_cnt);
660                 }
661                 /* Error while receiving packet. */
662                 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
663                         return -1;
664         }
665         return len;
666 }
667
668 /**
669  * Translate RX completion flags to offload flags.
670  *
671  * @param[in] rxq
672  *   Pointer to RX queue structure.
673  * @param[in] cqe
674  *   Pointer to CQE.
675  *
676  * @return
677  *   Offload flags (ol_flags) for struct rte_mbuf.
678  */
679 static inline uint32_t
680 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe)
681 {
682         uint32_t ol_flags = 0;
683         uint8_t l3_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L3_HDR_TYPE_MASK;
684         uint8_t l4_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L4_HDR_TYPE_MASK;
685         uint8_t info = cqe->rsvd0[0];
686
687         if ((l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV4) ||
688             (l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV6))
689                 ol_flags |=
690                         (!(cqe->hds_ip_ext & MLX5_CQE_L3_OK) *
691                          PKT_RX_IP_CKSUM_BAD);
692         if ((l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP) ||
693             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_EMP_ACK) ||
694             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_ACK) ||
695             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_UDP))
696                 ol_flags |=
697                         (!(cqe->hds_ip_ext & MLX5_CQE_L4_OK) *
698                          PKT_RX_L4_CKSUM_BAD);
699         /*
700          * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
701          * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
702          * (its value is 0).
703          */
704         if ((info & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
705                 ol_flags |=
706                         TRANSPOSE(~cqe->l4_hdr_type_etc,
707                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
708                                   PKT_RX_IP_CKSUM_BAD) |
709                         TRANSPOSE(~cqe->l4_hdr_type_etc,
710                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
711                                   PKT_RX_L4_CKSUM_BAD);
712         return ol_flags;
713 }
714
715 /**
716  * DPDK callback for RX.
717  *
718  * @param dpdk_rxq
719  *   Generic pointer to RX queue structure.
720  * @param[out] pkts
721  *   Array to store received packets.
722  * @param pkts_n
723  *   Maximum number of packets in array.
724  *
725  * @return
726  *   Number of packets successfully received (<= pkts_n).
727  */
728 uint16_t
729 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
730 {
731         struct rxq *rxq = dpdk_rxq;
732         unsigned int pkts_ret = 0;
733         unsigned int i;
734         unsigned int rq_ci = rxq->rq_ci;
735         const unsigned int elts_n = rxq->elts_n;
736         const unsigned int wqe_cnt = elts_n - 1;
737         const unsigned int cqe_cnt = rxq->cqe_n - 1;
738
739         for (i = 0; (i != pkts_n); ++i) {
740                 unsigned int idx = rq_ci & wqe_cnt;
741                 int len;
742                 struct rte_mbuf *rep;
743                 struct rte_mbuf *pkt;
744                 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
745                 volatile struct mlx5_cqe64 *cqe =
746                         &(*rxq->cqes)[rxq->cq_ci & cqe_cnt].cqe64;
747
748                 pkt = (*rxq->elts)[idx];
749                 rte_prefetch0(cqe);
750                 rep = rte_mbuf_raw_alloc(rxq->mp);
751                 if (unlikely(rep == NULL)) {
752                         ++rxq->stats.rx_nombuf;
753                         break;
754                 }
755                 SET_DATA_OFF(rep, RTE_PKTMBUF_HEADROOM);
756                 NB_SEGS(rep) = 1;
757                 PORT(rep) = rxq->port_id;
758                 NEXT(rep) = NULL;
759                 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt);
760                 if (unlikely(len == 0)) {
761                         rte_mbuf_refcnt_set(rep, 0);
762                         __rte_mbuf_raw_free(rep);
763                         break;
764                 }
765                 if (unlikely(len == -1)) {
766                         /* RX error, packet is likely too large. */
767                         rte_mbuf_refcnt_set(rep, 0);
768                         __rte_mbuf_raw_free(rep);
769                         ++rxq->stats.idropped;
770                         --i;
771                         goto skip;
772                 }
773                 /*
774                  * Fill NIC descriptor with the new buffer.  The lkey and size
775                  * of the buffers are already known, only the buffer address
776                  * changes.
777                  */
778                 wqe->addr = htonll((uintptr_t)rep->buf_addr +
779                                    RTE_PKTMBUF_HEADROOM);
780                 (*rxq->elts)[idx] = rep;
781                 /* Update pkt information. */
782                 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
783                     rxq->crc_present) {
784                         if (rxq->csum) {
785                                 pkt->packet_type = rxq_cq_to_pkt_type(cqe);
786                                 pkt->ol_flags = rxq_cq_to_ol_flags(rxq, cqe);
787                         }
788                         if (cqe->l4_hdr_type_etc & MLX5_CQE_VLAN_STRIPPED) {
789                                 pkt->ol_flags |= PKT_RX_VLAN_PKT |
790                                         PKT_RX_VLAN_STRIPPED;
791                                 pkt->vlan_tci = ntohs(cqe->vlan_info);
792                         }
793                         if (rxq->crc_present)
794                                 len -= ETHER_CRC_LEN;
795                 }
796                 PKT_LEN(pkt) = len;
797                 DATA_LEN(pkt) = len;
798 #ifdef MLX5_PMD_SOFT_COUNTERS
799                 /* Increment bytes counter. */
800                 rxq->stats.ibytes += len;
801 #endif
802                 /* Return packet. */
803                 *(pkts++) = pkt;
804                 ++pkts_ret;
805 skip:
806                 ++rq_ci;
807         }
808         if (unlikely((i == 0) && (rq_ci == rxq->rq_ci)))
809                 return 0;
810         /* Repost WRs. */
811 #ifdef DEBUG_RECV
812         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
813 #endif
814         /* Update the consumer index. */
815         rxq->rq_ci = rq_ci;
816         rte_wmb();
817         *rxq->cq_db = htonl(rxq->cq_ci);
818         rte_wmb();
819         *rxq->rq_db = htonl(rxq->rq_ci);
820 #ifdef MLX5_PMD_SOFT_COUNTERS
821         /* Increment packets counter. */
822         rxq->stats.ipackets += pkts_ret;
823 #endif
824         return pkts_ret;
825 }
826
827 /**
828  * Dummy DPDK callback for TX.
829  *
830  * This function is used to temporarily replace the real callback during
831  * unsafe control operations on the queue, or in case of error.
832  *
833  * @param dpdk_txq
834  *   Generic pointer to TX queue structure.
835  * @param[in] pkts
836  *   Packets to transmit.
837  * @param pkts_n
838  *   Number of packets in array.
839  *
840  * @return
841  *   Number of packets successfully transmitted (<= pkts_n).
842  */
843 uint16_t
844 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
845 {
846         (void)dpdk_txq;
847         (void)pkts;
848         (void)pkts_n;
849         return 0;
850 }
851
852 /**
853  * Dummy DPDK callback for RX.
854  *
855  * This function is used to temporarily replace the real callback during
856  * unsafe control operations on the queue, or in case of error.
857  *
858  * @param dpdk_rxq
859  *   Generic pointer to RX queue structure.
860  * @param[out] pkts
861  *   Array to store received packets.
862  * @param pkts_n
863  *   Maximum number of packets in array.
864  *
865  * @return
866  *   Number of packets successfully received (<= pkts_n).
867  */
868 uint16_t
869 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
870 {
871         (void)dpdk_rxq;
872         (void)pkts;
873         (void)pkts_n;
874         return 0;
875 }