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