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