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