net/mlx5: check remaining space while processing Tx burst
[dpdk.git] / drivers / net / mlx5 / mlx5_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <assert.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 /* Verbs header. */
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
41 #ifdef PEDANTIC
42 #pragma GCC diagnostic ignored "-pedantic"
43 #endif
44 #include <infiniband/verbs.h>
45 #include <infiniband/mlx5_hw.h>
46 #include <infiniband/arch.h>
47 #ifdef PEDANTIC
48 #pragma GCC diagnostic error "-pedantic"
49 #endif
50
51 /* DPDK headers don't like -pedantic. */
52 #ifdef PEDANTIC
53 #pragma GCC diagnostic ignored "-pedantic"
54 #endif
55 #include <rte_mbuf.h>
56 #include <rte_mempool.h>
57 #include <rte_prefetch.h>
58 #include <rte_common.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_ether.h>
61 #ifdef PEDANTIC
62 #pragma GCC diagnostic error "-pedantic"
63 #endif
64
65 #include "mlx5.h"
66 #include "mlx5_utils.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_autoconf.h"
69 #include "mlx5_defs.h"
70 #include "mlx5_prm.h"
71
72 #ifndef NDEBUG
73
74 /**
75  * Verify or set magic value in CQE.
76  *
77  * @param cqe
78  *   Pointer to CQE.
79  *
80  * @return
81  *   0 the first time.
82  */
83 static inline int
84 check_cqe64_seen(volatile struct mlx5_cqe64 *cqe)
85 {
86         static const uint8_t magic[] = "seen";
87         volatile uint8_t (*buf)[sizeof(cqe->rsvd40)] = &cqe->rsvd40;
88         int ret = 1;
89         unsigned int i;
90
91         for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
92                 if (!ret || (*buf)[i] != magic[i]) {
93                         ret = 0;
94                         (*buf)[i] = magic[i];
95                 }
96         return ret;
97 }
98
99 #endif /* NDEBUG */
100
101 static inline int
102 check_cqe64(volatile struct mlx5_cqe64 *cqe,
103             unsigned int cqes_n, const uint16_t ci)
104             __attribute__((always_inline));
105
106 /**
107  * Check whether CQE is valid.
108  *
109  * @param cqe
110  *   Pointer to CQE.
111  * @param cqes_n
112  *   Size of completion queue.
113  * @param ci
114  *   Consumer index.
115  *
116  * @return
117  *   0 on success, 1 on failure.
118  */
119 static inline int
120 check_cqe64(volatile struct mlx5_cqe64 *cqe,
121                 unsigned int cqes_n, const uint16_t ci)
122 {
123         uint16_t idx = ci & cqes_n;
124         uint8_t op_own = cqe->op_own;
125         uint8_t op_owner = MLX5_CQE_OWNER(op_own);
126         uint8_t op_code = MLX5_CQE_OPCODE(op_own);
127
128         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
129                 return 1; /* No CQE. */
130 #ifndef NDEBUG
131         if ((op_code == MLX5_CQE_RESP_ERR) ||
132             (op_code == MLX5_CQE_REQ_ERR)) {
133                 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
134                 uint8_t syndrome = err_cqe->syndrome;
135
136                 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
137                     (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
138                         return 0;
139                 if (!check_cqe64_seen(cqe))
140                         ERROR("unexpected CQE error %u (0x%02x)"
141                               " syndrome 0x%02x",
142                               op_code, op_code, syndrome);
143                 return 1;
144         } else if ((op_code != MLX5_CQE_RESP_SEND) &&
145                    (op_code != MLX5_CQE_REQ)) {
146                 if (!check_cqe64_seen(cqe))
147                         ERROR("unexpected CQE opcode %u (0x%02x)",
148                               op_code, op_code);
149                 return 1;
150         }
151 #endif /* NDEBUG */
152         return 0;
153 }
154
155 /**
156  * Manage TX completions.
157  *
158  * When sending a burst, mlx5_tx_burst() posts several WRs.
159  *
160  * @param txq
161  *   Pointer to TX queue structure.
162  */
163 static void
164 txq_complete(struct txq *txq)
165 {
166         const unsigned int elts_n = txq->elts_n;
167         const unsigned int cqe_n = txq->cqe_n;
168         const unsigned int cqe_cnt = cqe_n - 1;
169         uint16_t elts_free = txq->elts_tail;
170         uint16_t elts_tail;
171         uint16_t cq_ci = txq->cq_ci;
172         volatile struct mlx5_cqe64 *cqe = NULL;
173         volatile union mlx5_wqe *wqe;
174
175         do {
176                 volatile struct mlx5_cqe64 *tmp;
177
178                 tmp = &(*txq->cqes)[cq_ci & cqe_cnt].cqe64;
179                 if (check_cqe64(tmp, cqe_n, cq_ci))
180                         break;
181                 cqe = tmp;
182 #ifndef NDEBUG
183                 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
184                         if (!check_cqe64_seen(cqe))
185                                 ERROR("unexpected compressed CQE, TX stopped");
186                         return;
187                 }
188                 if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
189                     (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
190                         if (!check_cqe64_seen(cqe))
191                                 ERROR("unexpected error CQE, TX stopped");
192                         return;
193                 }
194 #endif /* NDEBUG */
195                 ++cq_ci;
196         } while (1);
197         if (unlikely(cqe == NULL))
198                 return;
199         wqe = &(*txq->wqes)[htons(cqe->wqe_counter) & (txq->wqe_n - 1)];
200         elts_tail = wqe->wqe.ctrl.data[3];
201         assert(elts_tail < txq->wqe_n);
202         /* Free buffers. */
203         while (elts_free != elts_tail) {
204                 struct rte_mbuf *elt = (*txq->elts)[elts_free];
205                 unsigned int elts_free_next =
206                         (elts_free + 1) & (elts_n - 1);
207                 struct rte_mbuf *elt_next = (*txq->elts)[elts_free_next];
208
209 #ifndef NDEBUG
210                 /* Poisoning. */
211                 memset(&(*txq->elts)[elts_free],
212                        0x66,
213                        sizeof((*txq->elts)[elts_free]));
214 #endif
215                 RTE_MBUF_PREFETCH_TO_FREE(elt_next);
216                 /* Only one segment needs to be freed. */
217                 rte_pktmbuf_free_seg(elt);
218                 elts_free = elts_free_next;
219         }
220         txq->cq_ci = cq_ci;
221         txq->elts_tail = elts_tail;
222         /* Update the consumer index. */
223         rte_wmb();
224         *txq->cq_db = htonl(cq_ci);
225 }
226
227 /**
228  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
229  * the cloned mbuf is allocated is returned instead.
230  *
231  * @param buf
232  *   Pointer to mbuf.
233  *
234  * @return
235  *   Memory pool where data is located for given mbuf.
236  */
237 static struct rte_mempool *
238 txq_mb2mp(struct rte_mbuf *buf)
239 {
240         if (unlikely(RTE_MBUF_INDIRECT(buf)))
241                 return rte_mbuf_from_indirect(buf)->pool;
242         return buf->pool;
243 }
244
245 static inline uint32_t
246 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
247         __attribute__((always_inline));
248
249 /**
250  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
251  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
252  * remove an entry first.
253  *
254  * @param txq
255  *   Pointer to TX queue structure.
256  * @param[in] mp
257  *   Memory Pool for which a Memory Region lkey must be returned.
258  *
259  * @return
260  *   mr->lkey on success, (uint32_t)-1 on failure.
261  */
262 static inline uint32_t
263 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
264 {
265         unsigned int i;
266         uint32_t lkey = (uint32_t)-1;
267
268         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
269                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
270                         /* Unknown MP, add a new MR for it. */
271                         break;
272                 }
273                 if (txq->mp2mr[i].mp == mp) {
274                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
275                         assert(htonl(txq->mp2mr[i].mr->lkey) ==
276                                txq->mp2mr[i].lkey);
277                         lkey = txq->mp2mr[i].lkey;
278                         break;
279                 }
280         }
281         if (unlikely(lkey == (uint32_t)-1))
282                 lkey = txq_mp2mr_reg(txq, mp, i);
283         return lkey;
284 }
285
286 /**
287  * Write a regular WQE.
288  *
289  * @param txq
290  *   Pointer to TX queue structure.
291  * @param wqe
292  *   Pointer to the WQE to fill.
293  * @param addr
294  *   Buffer data address.
295  * @param length
296  *   Packet length.
297  * @param lkey
298  *   Memory region lkey.
299  */
300 static inline void
301 mlx5_wqe_write(struct txq *txq, volatile union mlx5_wqe *wqe,
302                uintptr_t addr, uint32_t length, uint32_t lkey)
303 {
304         wqe->wqe.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
305         wqe->wqe.ctrl.data[1] = htonl((txq->qp_num_8s) | 4);
306         wqe->wqe.ctrl.data[3] = 0;
307         wqe->inl.eseg.rsvd0 = 0;
308         wqe->inl.eseg.rsvd1 = 0;
309         wqe->inl.eseg.mss = 0;
310         wqe->inl.eseg.rsvd2 = 0;
311         wqe->wqe.eseg.inline_hdr_sz = htons(MLX5_ETH_INLINE_HEADER_SIZE);
312         /* Copy the first 16 bytes into inline header. */
313         rte_memcpy((uint8_t *)(uintptr_t)wqe->wqe.eseg.inline_hdr_start,
314                    (uint8_t *)(uintptr_t)addr,
315                    MLX5_ETH_INLINE_HEADER_SIZE);
316         addr += MLX5_ETH_INLINE_HEADER_SIZE;
317         length -= MLX5_ETH_INLINE_HEADER_SIZE;
318         /* Store remaining data in data segment. */
319         wqe->wqe.dseg.byte_count = htonl(length);
320         wqe->wqe.dseg.lkey = lkey;
321         wqe->wqe.dseg.addr = htonll(addr);
322         /* Increment consumer index. */
323         ++txq->wqe_ci;
324 }
325
326 /**
327  * Write a regular WQE with VLAN.
328  *
329  * @param txq
330  *   Pointer to TX queue structure.
331  * @param wqe
332  *   Pointer to the WQE to fill.
333  * @param addr
334  *   Buffer data address.
335  * @param length
336  *   Packet length.
337  * @param lkey
338  *   Memory region lkey.
339  * @param vlan_tci
340  *   VLAN field to insert in packet.
341  */
342 static inline void
343 mlx5_wqe_write_vlan(struct txq *txq, volatile union mlx5_wqe *wqe,
344                     uintptr_t addr, uint32_t length, uint32_t lkey,
345                     uint16_t vlan_tci)
346 {
347         uint32_t vlan = htonl(0x81000000 | vlan_tci);
348
349         wqe->wqe.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
350         wqe->wqe.ctrl.data[1] = htonl((txq->qp_num_8s) | 4);
351         wqe->wqe.ctrl.data[3] = 0;
352         wqe->inl.eseg.rsvd0 = 0;
353         wqe->inl.eseg.rsvd1 = 0;
354         wqe->inl.eseg.mss = 0;
355         wqe->inl.eseg.rsvd2 = 0;
356         wqe->wqe.eseg.inline_hdr_sz = htons(MLX5_ETH_VLAN_INLINE_HEADER_SIZE);
357         /*
358          * Copy 12 bytes of source & destination MAC address.
359          * Copy 4 bytes of VLAN.
360          * Copy 2 bytes of Ether type.
361          */
362         rte_memcpy((uint8_t *)(uintptr_t)wqe->wqe.eseg.inline_hdr_start,
363                    (uint8_t *)(uintptr_t)addr, 12);
364         rte_memcpy((uint8_t *)((uintptr_t)wqe->wqe.eseg.inline_hdr_start + 12),
365                    &vlan, sizeof(vlan));
366         rte_memcpy((uint8_t *)((uintptr_t)wqe->wqe.eseg.inline_hdr_start + 16),
367                    (uint8_t *)((uintptr_t)addr + 12), 2);
368         addr += MLX5_ETH_VLAN_INLINE_HEADER_SIZE - sizeof(vlan);
369         length -= MLX5_ETH_VLAN_INLINE_HEADER_SIZE - sizeof(vlan);
370         /* Store remaining data in data segment. */
371         wqe->wqe.dseg.byte_count = htonl(length);
372         wqe->wqe.dseg.lkey = lkey;
373         wqe->wqe.dseg.addr = htonll(addr);
374         /* Increment consumer index. */
375         ++txq->wqe_ci;
376 }
377
378 /**
379  * Write a inline WQE.
380  *
381  * @param txq
382  *   Pointer to TX queue structure.
383  * @param wqe
384  *   Pointer to the WQE to fill.
385  * @param addr
386  *   Buffer data address.
387  * @param length
388  *   Packet length.
389  * @param lkey
390  *   Memory region lkey.
391  */
392 static inline void
393 mlx5_wqe_write_inline(struct txq *txq, volatile union mlx5_wqe *wqe,
394                       uintptr_t addr, uint32_t length)
395 {
396         uint32_t size;
397         uint16_t wqe_cnt = txq->wqe_n - 1;
398         uint16_t wqe_ci = txq->wqe_ci + 1;
399
400         /* Copy the first 16 bytes into inline header. */
401         rte_memcpy((void *)(uintptr_t)wqe->inl.eseg.inline_hdr_start,
402                    (void *)(uintptr_t)addr,
403                    MLX5_ETH_INLINE_HEADER_SIZE);
404         addr += MLX5_ETH_INLINE_HEADER_SIZE;
405         length -= MLX5_ETH_INLINE_HEADER_SIZE;
406         size = 3 + ((4 + length + 15) / 16);
407         wqe->inl.byte_cnt = htonl(length | MLX5_INLINE_SEG);
408         rte_memcpy((void *)(uintptr_t)&wqe->inl.data[0],
409                    (void *)addr, MLX5_WQE64_INL_DATA);
410         addr += MLX5_WQE64_INL_DATA;
411         length -= MLX5_WQE64_INL_DATA;
412         while (length) {
413                 volatile union mlx5_wqe *wqe_next =
414                         &(*txq->wqes)[wqe_ci & wqe_cnt];
415                 uint32_t copy_bytes = (length > sizeof(*wqe)) ?
416                                       sizeof(*wqe) :
417                                       length;
418
419                 rte_mov64((uint8_t *)(uintptr_t)&wqe_next->data[0],
420                           (uint8_t *)addr);
421                 addr += copy_bytes;
422                 length -= copy_bytes;
423                 ++wqe_ci;
424         }
425         assert(size < 64);
426         wqe->inl.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
427         wqe->inl.ctrl.data[1] = htonl(txq->qp_num_8s | size);
428         wqe->inl.ctrl.data[3] = 0;
429         wqe->inl.eseg.rsvd0 = 0;
430         wqe->inl.eseg.rsvd1 = 0;
431         wqe->inl.eseg.mss = 0;
432         wqe->inl.eseg.rsvd2 = 0;
433         wqe->inl.eseg.inline_hdr_sz = htons(MLX5_ETH_INLINE_HEADER_SIZE);
434         /* Increment consumer index. */
435         txq->wqe_ci = wqe_ci;
436 }
437
438 /**
439  * Write a inline WQE with VLAN.
440  *
441  * @param txq
442  *   Pointer to TX queue structure.
443  * @param wqe
444  *   Pointer to the WQE to fill.
445  * @param addr
446  *   Buffer data address.
447  * @param length
448  *   Packet length.
449  * @param lkey
450  *   Memory region lkey.
451  * @param vlan_tci
452  *   VLAN field to insert in packet.
453  */
454 static inline void
455 mlx5_wqe_write_inline_vlan(struct txq *txq, volatile union mlx5_wqe *wqe,
456                            uintptr_t addr, uint32_t length, uint16_t vlan_tci)
457 {
458         uint32_t size;
459         uint32_t wqe_cnt = txq->wqe_n - 1;
460         uint16_t wqe_ci = txq->wqe_ci + 1;
461         uint32_t vlan = htonl(0x81000000 | vlan_tci);
462
463         /*
464          * Copy 12 bytes of source & destination MAC address.
465          * Copy 4 bytes of VLAN.
466          * Copy 2 bytes of Ether type.
467          */
468         rte_memcpy((uint8_t *)(uintptr_t)wqe->inl.eseg.inline_hdr_start,
469                    (uint8_t *)addr, 12);
470         rte_memcpy((uint8_t *)(uintptr_t)wqe->inl.eseg.inline_hdr_start + 12,
471                    &vlan, sizeof(vlan));
472         rte_memcpy((uint8_t *)(uintptr_t)wqe->inl.eseg.inline_hdr_start + 16,
473                    ((uint8_t *)addr + 12), 2);
474         addr += MLX5_ETH_VLAN_INLINE_HEADER_SIZE - sizeof(vlan);
475         length -= MLX5_ETH_VLAN_INLINE_HEADER_SIZE - sizeof(vlan);
476         size = (sizeof(wqe->inl.ctrl.ctrl) +
477                 sizeof(wqe->inl.eseg) +
478                 sizeof(wqe->inl.byte_cnt) +
479                 length + 15) / 16;
480         wqe->inl.byte_cnt = htonl(length | MLX5_INLINE_SEG);
481         rte_memcpy((void *)(uintptr_t)&wqe->inl.data[0],
482                    (void *)addr, MLX5_WQE64_INL_DATA);
483         addr += MLX5_WQE64_INL_DATA;
484         length -= MLX5_WQE64_INL_DATA;
485         while (length) {
486                 volatile union mlx5_wqe *wqe_next =
487                         &(*txq->wqes)[wqe_ci & wqe_cnt];
488                 uint32_t copy_bytes = (length > sizeof(*wqe)) ?
489                                       sizeof(*wqe) :
490                                       length;
491
492                 rte_mov64((uint8_t *)(uintptr_t)&wqe_next->data[0],
493                           (uint8_t *)addr);
494                 addr += copy_bytes;
495                 length -= copy_bytes;
496                 ++wqe_ci;
497         }
498         assert(size < 64);
499         wqe->inl.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
500         wqe->inl.ctrl.data[1] = htonl(txq->qp_num_8s | size);
501         wqe->inl.ctrl.data[3] = 0;
502         wqe->inl.eseg.rsvd0 = 0;
503         wqe->inl.eseg.rsvd1 = 0;
504         wqe->inl.eseg.mss = 0;
505         wqe->inl.eseg.rsvd2 = 0;
506         wqe->inl.eseg.inline_hdr_sz = htons(MLX5_ETH_VLAN_INLINE_HEADER_SIZE);
507         /* Increment consumer index. */
508         txq->wqe_ci = wqe_ci;
509 }
510
511 /**
512  * Ring TX queue doorbell.
513  *
514  * @param txq
515  *   Pointer to TX queue structure.
516  */
517 static inline void
518 mlx5_tx_dbrec(struct txq *txq)
519 {
520         uint8_t *dst = (uint8_t *)((uintptr_t)txq->bf_reg + txq->bf_offset);
521         uint32_t data[4] = {
522                 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
523                 htonl(txq->qp_num_8s),
524                 0,
525                 0,
526         };
527         rte_wmb();
528         *txq->qp_db = htonl(txq->wqe_ci);
529         /* Ensure ordering between DB record and BF copy. */
530         rte_wmb();
531         rte_mov16(dst, (uint8_t *)data);
532         txq->bf_offset ^= txq->bf_buf_size;
533 }
534
535 /**
536  * Prefetch a CQE.
537  *
538  * @param txq
539  *   Pointer to TX queue structure.
540  * @param cqe_ci
541  *   CQE consumer index.
542  */
543 static inline void
544 tx_prefetch_cqe(struct txq *txq, uint16_t ci)
545 {
546         volatile struct mlx5_cqe64 *cqe;
547
548         cqe = &(*txq->cqes)[ci & (txq->cqe_n - 1)].cqe64;
549         rte_prefetch0(cqe);
550 }
551
552 /**
553  * Prefetch a WQE.
554  *
555  * @param txq
556  *   Pointer to TX queue structure.
557  * @param  wqe_ci
558  *   WQE consumer index.
559  */
560 static inline void
561 tx_prefetch_wqe(struct txq *txq, uint16_t ci)
562 {
563         volatile union mlx5_wqe *wqe;
564
565         wqe = &(*txq->wqes)[ci & (txq->wqe_n - 1)];
566         rte_prefetch0(wqe);
567 }
568
569 /**
570  * DPDK callback for TX.
571  *
572  * @param dpdk_txq
573  *   Generic pointer to TX queue structure.
574  * @param[in] pkts
575  *   Packets to transmit.
576  * @param pkts_n
577  *   Number of packets in array.
578  *
579  * @return
580  *   Number of packets successfully transmitted (<= pkts_n).
581  */
582 uint16_t
583 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
584 {
585         struct txq *txq = (struct txq *)dpdk_txq;
586         uint16_t elts_head = txq->elts_head;
587         const unsigned int elts_n = txq->elts_n;
588         unsigned int i = 0;
589         unsigned int max;
590         unsigned int comp;
591         volatile union mlx5_wqe *wqe;
592
593         if (unlikely(!pkts_n))
594                 return 0;
595         /* Prefetch first packet cacheline. */
596         tx_prefetch_cqe(txq, txq->cq_ci);
597         tx_prefetch_cqe(txq, txq->cq_ci + 1);
598         rte_prefetch0(*pkts);
599         /* Start processing. */
600         txq_complete(txq);
601         max = (elts_n - (elts_head - txq->elts_tail));
602         if (max > elts_n)
603                 max -= elts_n;
604         do {
605                 struct rte_mbuf *buf;
606                 unsigned int elts_head_next;
607                 uintptr_t addr;
608                 uint32_t length;
609                 uint32_t lkey;
610
611                 /*
612                  * Make sure there is enough room to store this packet and
613                  * that one ring entry remains unused.
614                  */
615                 if (max < 1 + 1)
616                         break;
617                 --max;
618                 --pkts_n;
619                 buf = *(pkts++);
620                 elts_head_next = (elts_head + 1) & (elts_n - 1);
621                 wqe = &(*txq->wqes)[txq->wqe_ci & (txq->wqe_n - 1)];
622                 rte_prefetch0(wqe);
623                 if (pkts_n)
624                         rte_prefetch0(*pkts);
625                 /* Retrieve buffer information. */
626                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
627                 length = DATA_LEN(buf);
628                 /* Update element. */
629                 (*txq->elts)[elts_head] = buf;
630                 /* Prefetch next buffer data. */
631                 if (pkts_n)
632                         rte_prefetch0(rte_pktmbuf_mtod(*pkts,
633                                                        volatile void *));
634                 /* Retrieve Memory Region key for this memory pool. */
635                 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
636                 if (buf->ol_flags & PKT_TX_VLAN_PKT)
637                         mlx5_wqe_write_vlan(txq, wqe, addr, length, lkey,
638                                             buf->vlan_tci);
639                 else
640                         mlx5_wqe_write(txq, wqe, addr, length, lkey);
641                 wqe->wqe.ctrl.data[2] = 0;
642                 /* Should we enable HW CKSUM offload */
643                 if (buf->ol_flags &
644                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
645                         wqe->wqe.eseg.cs_flags =
646                                 MLX5_ETH_WQE_L3_CSUM |
647                                 MLX5_ETH_WQE_L4_CSUM;
648                 } else {
649                         wqe->wqe.eseg.cs_flags = 0;
650                 }
651 #ifdef MLX5_PMD_SOFT_COUNTERS
652                 /* Increment sent bytes counter. */
653                 txq->stats.obytes += length;
654 #endif
655                 elts_head = elts_head_next;
656                 ++i;
657         } while (pkts_n);
658         /* Take a shortcut if nothing must be sent. */
659         if (unlikely(i == 0))
660                 return 0;
661         /* Check whether completion threshold has been reached. */
662         comp = txq->elts_comp + i;
663         if (comp >= MLX5_TX_COMP_THRESH) {
664                 /* Request completion on last WQE. */
665                 wqe->wqe.ctrl.data[2] = htonl(8);
666                 /* Save elts_head in unused "immediate" field of WQE. */
667                 wqe->wqe.ctrl.data[3] = elts_head;
668                 txq->elts_comp = 0;
669         } else {
670                 txq->elts_comp = comp;
671         }
672 #ifdef MLX5_PMD_SOFT_COUNTERS
673         /* Increment sent packets counter. */
674         txq->stats.opackets += i;
675 #endif
676         /* Ring QP doorbell. */
677         mlx5_tx_dbrec(txq);
678         txq->elts_head = elts_head;
679         return i;
680 }
681
682 /**
683  * DPDK callback for TX with inline support.
684  *
685  * @param dpdk_txq
686  *   Generic pointer to TX queue structure.
687  * @param[in] pkts
688  *   Packets to transmit.
689  * @param pkts_n
690  *   Number of packets in array.
691  *
692  * @return
693  *   Number of packets successfully transmitted (<= pkts_n).
694  */
695 uint16_t
696 mlx5_tx_burst_inline(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
697 {
698         struct txq *txq = (struct txq *)dpdk_txq;
699         uint16_t elts_head = txq->elts_head;
700         const unsigned int elts_n = txq->elts_n;
701         unsigned int i = 0;
702         unsigned int max;
703         unsigned int comp;
704         volatile union mlx5_wqe *wqe;
705         unsigned int max_inline = txq->max_inline;
706
707         if (unlikely(!pkts_n))
708                 return 0;
709         /* Prefetch first packet cacheline. */
710         tx_prefetch_cqe(txq, txq->cq_ci);
711         tx_prefetch_cqe(txq, txq->cq_ci + 1);
712         rte_prefetch0(*pkts);
713         /* Start processing. */
714         txq_complete(txq);
715         max = (elts_n - (elts_head - txq->elts_tail));
716         if (max > elts_n)
717                 max -= elts_n;
718         do {
719                 struct rte_mbuf *buf;
720                 unsigned int elts_head_next;
721                 uintptr_t addr;
722                 uint32_t length;
723                 uint32_t lkey;
724
725                 /*
726                  * Make sure there is enough room to store this packet and
727                  * that one ring entry remains unused.
728                  */
729                 if (max < 1 + 1)
730                         break;
731                 --max;
732                 --pkts_n;
733                 buf = *(pkts++);
734                 elts_head_next = (elts_head + 1) & (elts_n - 1);
735                 wqe = &(*txq->wqes)[txq->wqe_ci & (txq->wqe_n - 1)];
736                 tx_prefetch_wqe(txq, txq->wqe_ci);
737                 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
738                 if (pkts_n)
739                         rte_prefetch0(*pkts);
740                 /* Should we enable HW CKSUM offload */
741                 if (buf->ol_flags &
742                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
743                         wqe->inl.eseg.cs_flags =
744                                 MLX5_ETH_WQE_L3_CSUM |
745                                 MLX5_ETH_WQE_L4_CSUM;
746                 } else {
747                         wqe->inl.eseg.cs_flags = 0;
748                 }
749                 /* Retrieve buffer information. */
750                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
751                 length = DATA_LEN(buf);
752                 /* Update element. */
753                 (*txq->elts)[elts_head] = buf;
754                 /* Prefetch next buffer data. */
755                 if (pkts_n)
756                         rte_prefetch0(rte_pktmbuf_mtod(*pkts,
757                                                        volatile void *));
758                 if (length <= max_inline) {
759                         if (buf->ol_flags & PKT_TX_VLAN_PKT)
760                                 mlx5_wqe_write_inline_vlan(txq, wqe,
761                                                            addr, length,
762                                                            buf->vlan_tci);
763                         else
764                                 mlx5_wqe_write_inline(txq, wqe, addr, length);
765                 } else {
766                         /* Retrieve Memory Region key for this memory pool. */
767                         lkey = txq_mp2mr(txq, txq_mb2mp(buf));
768                         if (buf->ol_flags & PKT_TX_VLAN_PKT)
769                                 mlx5_wqe_write_vlan(txq, wqe, addr, length,
770                                                     lkey, buf->vlan_tci);
771                         else
772                                 mlx5_wqe_write(txq, wqe, addr, length, lkey);
773                 }
774                 wqe->inl.ctrl.data[2] = 0;
775                 elts_head = elts_head_next;
776 #ifdef MLX5_PMD_SOFT_COUNTERS
777                 /* Increment sent bytes counter. */
778                 txq->stats.obytes += length;
779 #endif
780                 ++i;
781         } while (pkts_n);
782         /* Take a shortcut if nothing must be sent. */
783         if (unlikely(i == 0))
784                 return 0;
785         /* Check whether completion threshold has been reached. */
786         comp = txq->elts_comp + i;
787         if (comp >= MLX5_TX_COMP_THRESH) {
788                 /* Request completion on last WQE. */
789                 wqe->inl.ctrl.data[2] = htonl(8);
790                 /* Save elts_head in unused "immediate" field of WQE. */
791                 wqe->inl.ctrl.data[3] = elts_head;
792                 txq->elts_comp = 0;
793         } else {
794                 txq->elts_comp = comp;
795         }
796 #ifdef MLX5_PMD_SOFT_COUNTERS
797         /* Increment sent packets counter. */
798         txq->stats.opackets += i;
799 #endif
800         /* Ring QP doorbell. */
801         mlx5_tx_dbrec(txq);
802         txq->elts_head = elts_head;
803         return i;
804 }
805
806 /**
807  * Open a MPW session.
808  *
809  * @param txq
810  *   Pointer to TX queue structure.
811  * @param mpw
812  *   Pointer to MPW session structure.
813  * @param length
814  *   Packet length.
815  */
816 static inline void
817 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
818 {
819         uint16_t idx = txq->wqe_ci & (txq->wqe_n - 1);
820         volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
821                 (volatile struct mlx5_wqe_data_seg (*)[])
822                 (uintptr_t)&(*txq->wqes)[(idx + 1) & (txq->wqe_n - 1)];
823
824         mpw->state = MLX5_MPW_STATE_OPENED;
825         mpw->pkts_n = 0;
826         mpw->len = length;
827         mpw->total_len = 0;
828         mpw->wqe = &(*txq->wqes)[idx];
829         mpw->wqe->mpw.eseg.mss = htons(length);
830         mpw->wqe->mpw.eseg.inline_hdr_sz = 0;
831         mpw->wqe->mpw.eseg.rsvd0 = 0;
832         mpw->wqe->mpw.eseg.rsvd1 = 0;
833         mpw->wqe->mpw.eseg.rsvd2 = 0;
834         mpw->wqe->mpw.ctrl.data[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
835                                            (txq->wqe_ci << 8) |
836                                            MLX5_OPCODE_LSO_MPW);
837         mpw->wqe->mpw.ctrl.data[2] = 0;
838         mpw->wqe->mpw.ctrl.data[3] = 0;
839         mpw->data.dseg[0] = &mpw->wqe->mpw.dseg[0];
840         mpw->data.dseg[1] = &mpw->wqe->mpw.dseg[1];
841         mpw->data.dseg[2] = &(*dseg)[0];
842         mpw->data.dseg[3] = &(*dseg)[1];
843         mpw->data.dseg[4] = &(*dseg)[2];
844 }
845
846 /**
847  * Close a MPW session.
848  *
849  * @param txq
850  *   Pointer to TX queue structure.
851  * @param mpw
852  *   Pointer to MPW session structure.
853  */
854 static inline void
855 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
856 {
857         unsigned int num = mpw->pkts_n;
858
859         /*
860          * Store size in multiple of 16 bytes. Control and Ethernet segments
861          * count as 2.
862          */
863         mpw->wqe->mpw.ctrl.data[1] = htonl(txq->qp_num_8s | (2 + num));
864         mpw->state = MLX5_MPW_STATE_CLOSED;
865         if (num < 3)
866                 ++txq->wqe_ci;
867         else
868                 txq->wqe_ci += 2;
869         tx_prefetch_wqe(txq, txq->wqe_ci);
870         tx_prefetch_wqe(txq, txq->wqe_ci + 1);
871 }
872
873 /**
874  * DPDK callback for TX with MPW support.
875  *
876  * @param dpdk_txq
877  *   Generic pointer to TX queue structure.
878  * @param[in] pkts
879  *   Packets to transmit.
880  * @param pkts_n
881  *   Number of packets in array.
882  *
883  * @return
884  *   Number of packets successfully transmitted (<= pkts_n).
885  */
886 uint16_t
887 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
888 {
889         struct txq *txq = (struct txq *)dpdk_txq;
890         uint16_t elts_head = txq->elts_head;
891         const unsigned int elts_n = txq->elts_n;
892         unsigned int i = 0;
893         unsigned int max;
894         unsigned int comp;
895         struct mlx5_mpw mpw = {
896                 .state = MLX5_MPW_STATE_CLOSED,
897         };
898
899         if (unlikely(!pkts_n))
900                 return 0;
901         /* Prefetch first packet cacheline. */
902         tx_prefetch_cqe(txq, txq->cq_ci);
903         tx_prefetch_wqe(txq, txq->wqe_ci);
904         tx_prefetch_wqe(txq, txq->wqe_ci + 1);
905         /* Start processing. */
906         txq_complete(txq);
907         max = (elts_n - (elts_head - txq->elts_tail));
908         if (max > elts_n)
909                 max -= elts_n;
910         do {
911                 struct rte_mbuf *buf;
912                 volatile struct mlx5_wqe_data_seg *dseg;
913                 unsigned int elts_head_next;
914                 uintptr_t addr;
915                 uint32_t length;
916                 uint32_t cs_flags = 0;
917
918                 /*
919                  * Make sure there is enough room to store this packet and
920                  * that one ring entry remains unused.
921                  */
922                 if (max < 1 + 1)
923                         break;
924                 --max;
925                 --pkts_n;
926                 buf = *(pkts++);
927                 elts_head_next = (elts_head + 1) & (elts_n - 1);
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 buffer information. */
933                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
934                 length = DATA_LEN(buf);
935                 /* Update element. */
936                 (*txq->elts)[elts_head] = buf;
937                 /* Start new session if packet differs. */
938                 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
939                     ((mpw.len != length) ||
940                      (mpw.wqe->mpw.eseg.cs_flags != cs_flags)))
941                         mlx5_mpw_close(txq, &mpw);
942                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
943                         mlx5_mpw_new(txq, &mpw, length);
944                         mpw.wqe->mpw.eseg.cs_flags = cs_flags;
945                 }
946                 dseg = mpw.data.dseg[mpw.pkts_n];
947                 *dseg = (struct mlx5_wqe_data_seg){
948                         .byte_count = htonl(length),
949                         .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
950                         .addr = htonll(addr),
951                 };
952                 ++mpw.pkts_n;
953                 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
954                         mlx5_mpw_close(txq, &mpw);
955                 elts_head = elts_head_next;
956 #ifdef MLX5_PMD_SOFT_COUNTERS
957                 /* Increment sent bytes counter. */
958                 txq->stats.obytes += length;
959 #endif
960                 ++i;
961         } while (pkts_n);
962         /* Take a shortcut if nothing must be sent. */
963         if (unlikely(i == 0))
964                 return 0;
965         /* Check whether completion threshold has been reached. */
966         comp = txq->elts_comp + i;
967         if (comp >= MLX5_TX_COMP_THRESH) {
968                 volatile union mlx5_wqe *wqe = mpw.wqe;
969
970                 /* Request completion on last WQE. */
971                 wqe->mpw.ctrl.data[2] = htonl(8);
972                 /* Save elts_head in unused "immediate" field of WQE. */
973                 wqe->mpw.ctrl.data[3] = elts_head;
974                 txq->elts_comp = 0;
975         } else {
976                 txq->elts_comp = comp;
977         }
978 #ifdef MLX5_PMD_SOFT_COUNTERS
979         /* Increment sent packets counter. */
980         txq->stats.opackets += i;
981 #endif
982         /* Ring QP doorbell. */
983         if (mpw.state == MLX5_MPW_STATE_OPENED)
984                 mlx5_mpw_close(txq, &mpw);
985         mlx5_tx_dbrec(txq);
986         txq->elts_head = elts_head;
987         return i;
988 }
989
990 /**
991  * Open a MPW inline session.
992  *
993  * @param txq
994  *   Pointer to TX queue structure.
995  * @param mpw
996  *   Pointer to MPW session structure.
997  * @param length
998  *   Packet length.
999  */
1000 static inline void
1001 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
1002 {
1003         uint16_t idx = txq->wqe_ci & (txq->wqe_n - 1);
1004
1005         mpw->state = MLX5_MPW_INL_STATE_OPENED;
1006         mpw->pkts_n = 0;
1007         mpw->len = length;
1008         mpw->total_len = 0;
1009         mpw->wqe = &(*txq->wqes)[idx];
1010         mpw->wqe->mpw_inl.ctrl.data[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
1011                                                (txq->wqe_ci << 8) |
1012                                                MLX5_OPCODE_LSO_MPW);
1013         mpw->wqe->mpw_inl.ctrl.data[2] = 0;
1014         mpw->wqe->mpw_inl.ctrl.data[3] = 0;
1015         mpw->wqe->mpw_inl.eseg.mss = htons(length);
1016         mpw->wqe->mpw_inl.eseg.inline_hdr_sz = 0;
1017         mpw->wqe->mpw_inl.eseg.cs_flags = 0;
1018         mpw->wqe->mpw_inl.eseg.rsvd0 = 0;
1019         mpw->wqe->mpw_inl.eseg.rsvd1 = 0;
1020         mpw->wqe->mpw_inl.eseg.rsvd2 = 0;
1021         mpw->data.raw = &mpw->wqe->mpw_inl.data[0];
1022 }
1023
1024 /**
1025  * Close a MPW inline session.
1026  *
1027  * @param txq
1028  *   Pointer to TX queue structure.
1029  * @param mpw
1030  *   Pointer to MPW session structure.
1031  */
1032 static inline void
1033 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
1034 {
1035         unsigned int size;
1036
1037         size = sizeof(*mpw->wqe) - MLX5_MWQE64_INL_DATA + mpw->total_len;
1038         /*
1039          * Store size in multiple of 16 bytes. Control and Ethernet segments
1040          * count as 2.
1041          */
1042         mpw->wqe->mpw_inl.ctrl.data[1] =
1043                 htonl(txq->qp_num_8s | ((size + 15) / 16));
1044         mpw->state = MLX5_MPW_STATE_CLOSED;
1045         mpw->wqe->mpw_inl.byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
1046         txq->wqe_ci += (size + (sizeof(*mpw->wqe) - 1)) / sizeof(*mpw->wqe);
1047 }
1048
1049 /**
1050  * DPDK callback for TX with MPW inline support.
1051  *
1052  * @param dpdk_txq
1053  *   Generic pointer to TX queue structure.
1054  * @param[in] pkts
1055  *   Packets to transmit.
1056  * @param pkts_n
1057  *   Number of packets in array.
1058  *
1059  * @return
1060  *   Number of packets successfully transmitted (<= pkts_n).
1061  */
1062 uint16_t
1063 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
1064                          uint16_t pkts_n)
1065 {
1066         struct txq *txq = (struct txq *)dpdk_txq;
1067         uint16_t elts_head = txq->elts_head;
1068         const unsigned int elts_n = txq->elts_n;
1069         unsigned int i = 0;
1070         unsigned int max;
1071         unsigned int comp;
1072         unsigned int inline_room = txq->max_inline;
1073         struct mlx5_mpw mpw = {
1074                 .state = MLX5_MPW_STATE_CLOSED,
1075         };
1076
1077         if (unlikely(!pkts_n))
1078                 return 0;
1079         /* Prefetch first packet cacheline. */
1080         tx_prefetch_cqe(txq, txq->cq_ci);
1081         tx_prefetch_wqe(txq, txq->wqe_ci);
1082         tx_prefetch_wqe(txq, txq->wqe_ci + 1);
1083         /* Start processing. */
1084         txq_complete(txq);
1085         max = (elts_n - (elts_head - txq->elts_tail));
1086         if (max > elts_n)
1087                 max -= elts_n;
1088         do {
1089                 struct rte_mbuf *buf;
1090                 unsigned int elts_head_next;
1091                 uintptr_t addr;
1092                 uint32_t length;
1093                 uint32_t cs_flags = 0;
1094
1095                 /*
1096                  * Make sure there is enough room to store this packet and
1097                  * that one ring entry remains unused.
1098                  */
1099                 if (max < 1 + 1)
1100                         break;
1101                 --max;
1102                 --pkts_n;
1103                 buf = *(pkts++);
1104                 elts_head_next = (elts_head + 1) & (elts_n - 1);
1105                 /* Should we enable HW CKSUM offload */
1106                 if (buf->ol_flags &
1107                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1108                         cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1109                 /* Retrieve buffer information. */
1110                 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1111                 length = DATA_LEN(buf);
1112                 /* Update element. */
1113                 (*txq->elts)[elts_head] = buf;
1114                 /* Start new session if packet differs. */
1115                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1116                         if ((mpw.len != length) ||
1117                             (mpw.wqe->mpw.eseg.cs_flags != cs_flags))
1118                                 mlx5_mpw_close(txq, &mpw);
1119                 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
1120                         if ((mpw.len != length) ||
1121                             (length > inline_room) ||
1122                             (mpw.wqe->mpw_inl.eseg.cs_flags != cs_flags)) {
1123                                 mlx5_mpw_inline_close(txq, &mpw);
1124                                 inline_room = txq->max_inline;
1125                         }
1126                 }
1127                 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1128                         if (length > inline_room) {
1129                                 mlx5_mpw_new(txq, &mpw, length);
1130                                 mpw.wqe->mpw.eseg.cs_flags = cs_flags;
1131                         } else {
1132                                 mlx5_mpw_inline_new(txq, &mpw, length);
1133                                 mpw.wqe->mpw_inl.eseg.cs_flags = cs_flags;
1134                         }
1135                 }
1136                 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1137                         volatile struct mlx5_wqe_data_seg *dseg;
1138
1139                         assert(inline_room == txq->max_inline);
1140                         dseg = mpw.data.dseg[mpw.pkts_n];
1141                         *dseg = (struct mlx5_wqe_data_seg){
1142                                 .byte_count = htonl(length),
1143                                 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1144                                 .addr = htonll(addr),
1145                         };
1146                         ++mpw.pkts_n;
1147                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1148                                 mlx5_mpw_close(txq, &mpw);
1149                 } else {
1150                         unsigned int max;
1151
1152                         assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1153                         assert(length <= inline_room);
1154                         /* Maximum number of bytes before wrapping. */
1155                         max = ((uintptr_t)&(*txq->wqes)[txq->wqe_n] -
1156                                (uintptr_t)mpw.data.raw);
1157                         if (length > max) {
1158                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1159                                            (void *)addr,
1160                                            max);
1161                                 mpw.data.raw =
1162                                         (volatile void *)&(*txq->wqes)[0];
1163                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1164                                            (void *)(addr + max),
1165                                            length - max);
1166                                 mpw.data.raw += length - max;
1167                         } else {
1168                                 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1169                                            (void *)addr,
1170                                            length);
1171                                 mpw.data.raw += length;
1172                         }
1173                         if ((uintptr_t)mpw.data.raw ==
1174                             (uintptr_t)&(*txq->wqes)[txq->wqe_n])
1175                                 mpw.data.raw =
1176                                         (volatile void *)&(*txq->wqes)[0];
1177                         ++mpw.pkts_n;
1178                         if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1179                                 mlx5_mpw_inline_close(txq, &mpw);
1180                                 inline_room = txq->max_inline;
1181                         } else {
1182                                 inline_room -= length;
1183                         }
1184                 }
1185                 mpw.total_len += length;
1186                 elts_head = elts_head_next;
1187 #ifdef MLX5_PMD_SOFT_COUNTERS
1188                 /* Increment sent bytes counter. */
1189                 txq->stats.obytes += length;
1190 #endif
1191                 ++i;
1192         } while (pkts_n);
1193         /* Take a shortcut if nothing must be sent. */
1194         if (unlikely(i == 0))
1195                 return 0;
1196         /* Check whether completion threshold has been reached. */
1197         comp = txq->elts_comp + i;
1198         if (comp >= MLX5_TX_COMP_THRESH) {
1199                 volatile union mlx5_wqe *wqe = mpw.wqe;
1200
1201                 /* Request completion on last WQE. */
1202                 wqe->mpw_inl.ctrl.data[2] = htonl(8);
1203                 /* Save elts_head in unused "immediate" field of WQE. */
1204                 wqe->mpw_inl.ctrl.data[3] = elts_head;
1205                 txq->elts_comp = 0;
1206         } else {
1207                 txq->elts_comp = comp;
1208         }
1209 #ifdef MLX5_PMD_SOFT_COUNTERS
1210         /* Increment sent packets counter. */
1211         txq->stats.opackets += i;
1212 #endif
1213         /* Ring QP doorbell. */
1214         if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1215                 mlx5_mpw_inline_close(txq, &mpw);
1216         else if (mpw.state == MLX5_MPW_STATE_OPENED)
1217                 mlx5_mpw_close(txq, &mpw);
1218         mlx5_tx_dbrec(txq);
1219         txq->elts_head = elts_head;
1220         return i;
1221 }
1222
1223 /**
1224  * Translate RX completion flags to packet type.
1225  *
1226  * @param[in] cqe
1227  *   Pointer to CQE.
1228  *
1229  * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1230  *
1231  * @return
1232  *   Packet type for struct rte_mbuf.
1233  */
1234 static inline uint32_t
1235 rxq_cq_to_pkt_type(volatile struct mlx5_cqe64 *cqe)
1236 {
1237         uint32_t pkt_type;
1238         uint8_t flags = cqe->l4_hdr_type_etc;
1239         uint8_t info = cqe->rsvd0[0];
1240
1241         if (info & IBV_EXP_CQ_RX_TUNNEL_PACKET)
1242                 pkt_type =
1243                         TRANSPOSE(flags,
1244                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
1245                                   RTE_PTYPE_L3_IPV4) |
1246                         TRANSPOSE(flags,
1247                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
1248                                   RTE_PTYPE_L3_IPV6) |
1249                         TRANSPOSE(flags,
1250                                   IBV_EXP_CQ_RX_IPV4_PACKET,
1251                                   RTE_PTYPE_INNER_L3_IPV4) |
1252                         TRANSPOSE(flags,
1253                                   IBV_EXP_CQ_RX_IPV6_PACKET,
1254                                   RTE_PTYPE_INNER_L3_IPV6);
1255         else
1256                 pkt_type =
1257                         TRANSPOSE(flags,
1258                                   MLX5_CQE_L3_HDR_TYPE_IPV6,
1259                                   RTE_PTYPE_L3_IPV6) |
1260                         TRANSPOSE(flags,
1261                                   MLX5_CQE_L3_HDR_TYPE_IPV4,
1262                                   RTE_PTYPE_L3_IPV4);
1263         return pkt_type;
1264 }
1265
1266 /**
1267  * Get size of the next packet for a given CQE. For compressed CQEs, the
1268  * consumer index is updated only once all packets of the current one have
1269  * been processed.
1270  *
1271  * @param rxq
1272  *   Pointer to RX queue.
1273  * @param cqe
1274  *   CQE to process.
1275  *
1276  * @return
1277  *   Packet size in bytes (0 if there is none), -1 in case of completion
1278  *   with error.
1279  */
1280 static inline int
1281 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe,
1282                  uint16_t cqe_cnt)
1283 {
1284         struct rxq_zip *zip = &rxq->zip;
1285         uint16_t cqe_n = cqe_cnt + 1;
1286         int len = 0;
1287
1288         /* Process compressed data in the CQE and mini arrays. */
1289         if (zip->ai) {
1290                 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1291                         (volatile struct mlx5_mini_cqe8 (*)[8])
1292                         (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt].cqe64);
1293
1294                 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1295                 if ((++zip->ai & 7) == 0) {
1296                         /*
1297                          * Increment consumer index to skip the number of
1298                          * CQEs consumed. Hardware leaves holes in the CQ
1299                          * ring for software use.
1300                          */
1301                         zip->ca = zip->na;
1302                         zip->na += 8;
1303                 }
1304                 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1305                         uint16_t idx = rxq->cq_ci;
1306                         uint16_t end = zip->cq_ci;
1307
1308                         while (idx != end) {
1309                                 (*rxq->cqes)[idx & cqe_cnt].cqe64.op_own =
1310                                         MLX5_CQE_INVALIDATE;
1311                                 ++idx;
1312                         }
1313                         rxq->cq_ci = zip->cq_ci;
1314                         zip->ai = 0;
1315                 }
1316         /* No compressed data, get next CQE and verify if it is compressed. */
1317         } else {
1318                 int ret;
1319                 int8_t op_own;
1320
1321                 ret = check_cqe64(cqe, cqe_n, rxq->cq_ci);
1322                 if (unlikely(ret == 1))
1323                         return 0;
1324                 ++rxq->cq_ci;
1325                 op_own = cqe->op_own;
1326                 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1327                         volatile struct mlx5_mini_cqe8 (*mc)[8] =
1328                                 (volatile struct mlx5_mini_cqe8 (*)[8])
1329                                 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1330                                                           cqe_cnt].cqe64);
1331
1332                         /* Fix endianness. */
1333                         zip->cqe_cnt = ntohl(cqe->byte_cnt);
1334                         /*
1335                          * Current mini array position is the one returned by
1336                          * check_cqe64().
1337                          *
1338                          * If completion comprises several mini arrays, as a
1339                          * special case the second one is located 7 CQEs after
1340                          * the initial CQE instead of 8 for subsequent ones.
1341                          */
1342                         zip->ca = rxq->cq_ci & cqe_cnt;
1343                         zip->na = zip->ca + 7;
1344                         /* Compute the next non compressed CQE. */
1345                         --rxq->cq_ci;
1346                         zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1347                         /* Get packet size to return. */
1348                         len = ntohl((*mc)[0].byte_cnt);
1349                         zip->ai = 1;
1350                 } else {
1351                         len = ntohl(cqe->byte_cnt);
1352                 }
1353                 /* Error while receiving packet. */
1354                 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1355                         return -1;
1356         }
1357         return len;
1358 }
1359
1360 /**
1361  * Translate RX completion flags to offload flags.
1362  *
1363  * @param[in] rxq
1364  *   Pointer to RX queue structure.
1365  * @param[in] cqe
1366  *   Pointer to CQE.
1367  *
1368  * @return
1369  *   Offload flags (ol_flags) for struct rte_mbuf.
1370  */
1371 static inline uint32_t
1372 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe)
1373 {
1374         uint32_t ol_flags = 0;
1375         uint8_t l3_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L3_HDR_TYPE_MASK;
1376         uint8_t l4_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L4_HDR_TYPE_MASK;
1377         uint8_t info = cqe->rsvd0[0];
1378
1379         if ((l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV4) ||
1380             (l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV6))
1381                 ol_flags |=
1382                         (!(cqe->hds_ip_ext & MLX5_CQE_L3_OK) *
1383                          PKT_RX_IP_CKSUM_BAD);
1384         if ((l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP) ||
1385             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_EMP_ACK) ||
1386             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_ACK) ||
1387             (l4_hdr == MLX5_CQE_L4_HDR_TYPE_UDP))
1388                 ol_flags |=
1389                         (!(cqe->hds_ip_ext & MLX5_CQE_L4_OK) *
1390                          PKT_RX_L4_CKSUM_BAD);
1391         /*
1392          * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
1393          * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
1394          * (its value is 0).
1395          */
1396         if ((info & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1397                 ol_flags |=
1398                         TRANSPOSE(~cqe->l4_hdr_type_etc,
1399                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
1400                                   PKT_RX_IP_CKSUM_BAD) |
1401                         TRANSPOSE(~cqe->l4_hdr_type_etc,
1402                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
1403                                   PKT_RX_L4_CKSUM_BAD);
1404         return ol_flags;
1405 }
1406
1407 /**
1408  * DPDK callback for RX.
1409  *
1410  * @param dpdk_rxq
1411  *   Generic pointer to RX queue structure.
1412  * @param[out] pkts
1413  *   Array to store received packets.
1414  * @param pkts_n
1415  *   Maximum number of packets in array.
1416  *
1417  * @return
1418  *   Number of packets successfully received (<= pkts_n).
1419  */
1420 uint16_t
1421 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1422 {
1423         struct rxq *rxq = dpdk_rxq;
1424         unsigned int pkts_ret = 0;
1425         unsigned int i;
1426         unsigned int rq_ci = rxq->rq_ci;
1427         const unsigned int elts_n = rxq->elts_n;
1428         const unsigned int wqe_cnt = elts_n - 1;
1429         const unsigned int cqe_cnt = rxq->cqe_n - 1;
1430
1431         for (i = 0; (i != pkts_n); ++i) {
1432                 unsigned int idx = rq_ci & wqe_cnt;
1433                 int len;
1434                 struct rte_mbuf *rep;
1435                 struct rte_mbuf *pkt;
1436                 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1437                 volatile struct mlx5_cqe64 *cqe =
1438                         &(*rxq->cqes)[rxq->cq_ci & cqe_cnt].cqe64;
1439
1440                 pkt = (*rxq->elts)[idx];
1441                 rte_prefetch0(cqe);
1442                 rep = rte_mbuf_raw_alloc(rxq->mp);
1443                 if (unlikely(rep == NULL)) {
1444                         ++rxq->stats.rx_nombuf;
1445                         break;
1446                 }
1447                 SET_DATA_OFF(rep, RTE_PKTMBUF_HEADROOM);
1448                 NB_SEGS(rep) = 1;
1449                 PORT(rep) = rxq->port_id;
1450                 NEXT(rep) = NULL;
1451                 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt);
1452                 if (unlikely(len == 0)) {
1453                         rte_mbuf_refcnt_set(rep, 0);
1454                         __rte_mbuf_raw_free(rep);
1455                         break;
1456                 }
1457                 if (unlikely(len == -1)) {
1458                         /* RX error, packet is likely too large. */
1459                         rte_mbuf_refcnt_set(rep, 0);
1460                         __rte_mbuf_raw_free(rep);
1461                         ++rxq->stats.idropped;
1462                         --i;
1463                         goto skip;
1464                 }
1465                 /*
1466                  * Fill NIC descriptor with the new buffer.  The lkey and size
1467                  * of the buffers are already known, only the buffer address
1468                  * changes.
1469                  */
1470                 wqe->addr = htonll((uintptr_t)rep->buf_addr +
1471                                    RTE_PKTMBUF_HEADROOM);
1472                 (*rxq->elts)[idx] = rep;
1473                 /* Update pkt information. */
1474                 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
1475                     rxq->crc_present) {
1476                         if (rxq->csum) {
1477                                 pkt->packet_type = rxq_cq_to_pkt_type(cqe);
1478                                 pkt->ol_flags = rxq_cq_to_ol_flags(rxq, cqe);
1479                         }
1480                         if (cqe->l4_hdr_type_etc & MLX5_CQE_VLAN_STRIPPED) {
1481                                 pkt->ol_flags |= PKT_RX_VLAN_PKT |
1482                                         PKT_RX_VLAN_STRIPPED;
1483                                 pkt->vlan_tci = ntohs(cqe->vlan_info);
1484                         }
1485                         if (rxq->crc_present)
1486                                 len -= ETHER_CRC_LEN;
1487                 }
1488                 PKT_LEN(pkt) = len;
1489                 DATA_LEN(pkt) = len;
1490 #ifdef MLX5_PMD_SOFT_COUNTERS
1491                 /* Increment bytes counter. */
1492                 rxq->stats.ibytes += len;
1493 #endif
1494                 /* Return packet. */
1495                 *(pkts++) = pkt;
1496                 ++pkts_ret;
1497 skip:
1498                 ++rq_ci;
1499         }
1500         if (unlikely((i == 0) && (rq_ci == rxq->rq_ci)))
1501                 return 0;
1502         /* Repost WRs. */
1503 #ifdef DEBUG_RECV
1504         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
1505 #endif
1506         /* Update the consumer index. */
1507         rxq->rq_ci = rq_ci;
1508         rte_wmb();
1509         *rxq->cq_db = htonl(rxq->cq_ci);
1510         rte_wmb();
1511         *rxq->rq_db = htonl(rxq->rq_ci);
1512 #ifdef MLX5_PMD_SOFT_COUNTERS
1513         /* Increment packets counter. */
1514         rxq->stats.ipackets += pkts_ret;
1515 #endif
1516         return pkts_ret;
1517 }
1518
1519 /**
1520  * Dummy DPDK callback for TX.
1521  *
1522  * This function is used to temporarily replace the real callback during
1523  * unsafe control operations on the queue, or in case of error.
1524  *
1525  * @param dpdk_txq
1526  *   Generic pointer to TX queue structure.
1527  * @param[in] pkts
1528  *   Packets to transmit.
1529  * @param pkts_n
1530  *   Number of packets in array.
1531  *
1532  * @return
1533  *   Number of packets successfully transmitted (<= pkts_n).
1534  */
1535 uint16_t
1536 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1537 {
1538         (void)dpdk_txq;
1539         (void)pkts;
1540         (void)pkts_n;
1541         return 0;
1542 }
1543
1544 /**
1545  * Dummy DPDK callback for RX.
1546  *
1547  * This function is used to temporarily replace the real callback during
1548  * unsafe control operations on the queue, or in case of error.
1549  *
1550  * @param dpdk_rxq
1551  *   Generic pointer to RX queue structure.
1552  * @param[out] pkts
1553  *   Array to store received packets.
1554  * @param pkts_n
1555  *   Maximum number of packets in array.
1556  *
1557  * @return
1558  *   Number of packets successfully received (<= pkts_n).
1559  */
1560 uint16_t
1561 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1562 {
1563         (void)dpdk_rxq;
1564         (void)pkts;
1565         (void)pkts_n;
1566         return 0;
1567 }