4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
40 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #pragma GCC diagnostic ignored "-pedantic"
44 #include <infiniband/verbs.h>
45 #include <infiniband/mlx5_hw.h>
46 #include <infiniband/arch.h>
48 #pragma GCC diagnostic error "-pedantic"
51 /* DPDK headers don't like -pedantic. */
53 #pragma GCC diagnostic ignored "-pedantic"
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>
62 #pragma GCC diagnostic error "-pedantic"
66 #include "mlx5_utils.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_autoconf.h"
69 #include "mlx5_defs.h"
75 * Verify or set magic value in CQE.
84 check_cqe64_seen(volatile struct mlx5_cqe64 *cqe)
86 static const uint8_t magic[] = "seen";
87 volatile uint8_t (*buf)[sizeof(cqe->rsvd40)] = &cqe->rsvd40;
91 for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
92 if (!ret || (*buf)[i] != magic[i]) {
102 check_cqe64(volatile struct mlx5_cqe64 *cqe,
103 unsigned int cqes_n, const uint16_t ci)
104 __attribute__((always_inline));
107 * Check whether CQE is valid.
112 * Size of completion queue.
117 * 0 on success, 1 on failure.
120 check_cqe64(volatile struct mlx5_cqe64 *cqe,
121 unsigned int cqes_n, const uint16_t ci)
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);
128 if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
129 return 1; /* No CQE. */
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;
136 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
137 (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
139 if (!check_cqe64_seen(cqe))
140 ERROR("unexpected CQE error %u (0x%02x)"
142 op_code, op_code, syndrome);
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)",
156 txq_complete(struct txq *txq) __attribute__((always_inline));
159 * Manage TX completions.
161 * When sending a burst, mlx5_tx_burst() posts several WRs.
164 * Pointer to TX queue structure.
167 txq_complete(struct txq *txq)
169 const unsigned int elts_n = txq->elts_n;
170 const unsigned int cqe_n = txq->cqe_n;
171 const unsigned int cqe_cnt = cqe_n - 1;
172 uint16_t elts_free = txq->elts_tail;
174 uint16_t cq_ci = txq->cq_ci;
175 volatile struct mlx5_cqe64 *cqe = NULL;
176 volatile union mlx5_wqe *wqe;
179 volatile struct mlx5_cqe64 *tmp;
181 tmp = &(*txq->cqes)[cq_ci & cqe_cnt].cqe64;
182 if (check_cqe64(tmp, cqe_n, cq_ci))
186 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
187 if (!check_cqe64_seen(cqe))
188 ERROR("unexpected compressed CQE, TX stopped");
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");
200 if (unlikely(cqe == NULL))
202 wqe = &(*txq->wqes)[htons(cqe->wqe_counter) & (txq->wqe_n - 1)];
203 elts_tail = wqe->wqe.ctrl.data[3];
204 assert(elts_tail < txq->wqe_n);
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];
214 memset(&(*txq->elts)[elts_free],
216 sizeof((*txq->elts)[elts_free]));
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;
224 txq->elts_tail = elts_tail;
225 /* Update the consumer index. */
227 *txq->cq_db = htonl(cq_ci);
231 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
232 * the cloned mbuf is allocated is returned instead.
238 * Memory pool where data is located for given mbuf.
240 static struct rte_mempool *
241 txq_mb2mp(struct rte_mbuf *buf)
243 if (unlikely(RTE_MBUF_INDIRECT(buf)))
244 return rte_mbuf_from_indirect(buf)->pool;
248 static inline uint32_t
249 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
250 __attribute__((always_inline));
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.
258 * Pointer to TX queue structure.
260 * Memory Pool for which a Memory Region lkey must be returned.
263 * mr->lkey on success, (uint32_t)-1 on failure.
265 static inline uint32_t
266 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
269 uint32_t lkey = (uint32_t)-1;
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. */
276 if (txq->mp2mr[i].mp == mp) {
277 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
278 assert(htonl(txq->mp2mr[i].mr->lkey) ==
280 lkey = txq->mp2mr[i].lkey;
284 if (unlikely(lkey == (uint32_t)-1))
285 lkey = txq_mp2mr_reg(txq, mp, i);
290 * Write a regular WQE.
293 * Pointer to TX queue structure.
295 * Pointer to the WQE to fill.
302 * Number of DS elements consumed.
304 static inline unsigned int
305 mlx5_wqe_write(struct txq *txq, volatile union mlx5_wqe *wqe,
306 struct rte_mbuf *buf, uint32_t length)
308 uintptr_t raw = (uintptr_t)&wqe->wqe.eseg.inline_hdr_start;
310 uint16_t pkt_inline_sz = 16;
311 uintptr_t addr = rte_pktmbuf_mtod(buf, uintptr_t);
312 struct mlx5_wqe_data_seg *dseg = NULL;
314 assert(length >= 16);
315 /* Start the know and common part of the WQE structure. */
316 wqe->wqe.ctrl.data[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
317 wqe->wqe.ctrl.data[2] = 0;
318 wqe->wqe.ctrl.data[3] = 0;
319 wqe->wqe.eseg.rsvd0 = 0;
320 wqe->wqe.eseg.rsvd1 = 0;
321 wqe->wqe.eseg.mss = 0;
322 wqe->wqe.eseg.rsvd2 = 0;
323 /* Start by copying the Ethernet Header. */
324 rte_mov16((uint8_t *)raw, (uint8_t *)addr);
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);
331 memcpy((uint8_t *)(raw + 16 - sizeof(vlan)),
332 &vlan, sizeof(vlan));
333 addr -= sizeof(vlan);
334 length += sizeof(vlan);
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;
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) ?
351 rte_memcpy((void *)raw, (void *)addr, copy_b);
354 pkt_inline_sz += copy_b;
356 assert(addr <= addr_end);
358 /* Store the inlined packet size in the WQE. */
359 wqe->wqe.eseg.inline_hdr_sz = htons(pkt_inline_sz);
361 * 2 DWORDs consumed by the WQE header + 1 DSEG +
362 * the size of the inline part of the packet.
364 ds = 2 + ((pkt_inline_sz - 2 + 15) / 16);
366 dseg = (struct mlx5_wqe_data_seg *)
367 ((uintptr_t)wqe + (ds * 16));
368 if ((uintptr_t)dseg >= end)
369 dseg = (struct mlx5_wqe_data_seg *)
370 ((uintptr_t)&(*txq->wqes)[0]);
374 /* Add the remaining packet as a simple ds. */
377 * No inline has been done in the packet, only the Ethernet
378 * Header as been stored.
380 wqe->wqe.eseg.inline_hdr_sz = htons(16);
381 dseg = (struct mlx5_wqe_data_seg *)
382 ((uintptr_t)wqe + (ds * 16));
384 *dseg = (struct mlx5_wqe_data_seg) {
385 .addr = htonll(addr),
386 .byte_count = htonl(length),
387 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
391 wqe->wqe.ctrl.data[1] = htonl(txq->qp_num_8s | ds);
396 * Ring TX queue doorbell.
399 * Pointer to TX queue structure.
402 mlx5_tx_dbrec(struct txq *txq)
404 uint8_t *dst = (uint8_t *)((uintptr_t)txq->bf_reg + txq->bf_offset);
406 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
407 htonl(txq->qp_num_8s),
412 *txq->qp_db = htonl(txq->wqe_ci);
413 /* Ensure ordering between DB record and BF copy. */
415 rte_mov16(dst, (uint8_t *)data);
416 txq->bf_offset ^= txq->bf_buf_size;
423 * Pointer to TX queue structure.
425 * CQE consumer index.
428 tx_prefetch_cqe(struct txq *txq, uint16_t ci)
430 volatile struct mlx5_cqe64 *cqe;
432 cqe = &(*txq->cqes)[ci & (txq->cqe_n - 1)].cqe64;
440 * Pointer to TX queue structure.
442 * WQE consumer index.
445 tx_prefetch_wqe(struct txq *txq, uint16_t ci)
447 volatile union mlx5_wqe *wqe;
449 wqe = &(*txq->wqes)[ci & (txq->wqe_n - 1)];
454 * DPDK callback for TX.
457 * Generic pointer to TX queue structure.
459 * Packets to transmit.
461 * Number of packets in array.
464 * Number of packets successfully transmitted (<= pkts_n).
467 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
469 struct txq *txq = (struct txq *)dpdk_txq;
470 uint16_t elts_head = txq->elts_head;
471 const unsigned int elts_n = txq->elts_n;
476 volatile union mlx5_wqe *wqe = NULL;
478 if (unlikely(!pkts_n))
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. */
486 max = (elts_n - (elts_head - txq->elts_tail));
490 struct rte_mbuf *buf = *(pkts++);
491 unsigned int elts_head_next;
493 unsigned int segs_n = buf->nb_segs;
494 volatile struct mlx5_wqe_data_seg *dseg;
495 unsigned int ds = sizeof(*wqe) / 16;
498 * Make sure there is enough room to store this packet and
499 * that one ring entry remains unused.
502 if (max < segs_n + 1)
506 elts_head_next = (elts_head + 1) & (elts_n - 1);
507 wqe = &(*txq->wqes)[txq->wqe_ci & (txq->wqe_n - 1)];
508 tx_prefetch_wqe(txq, txq->wqe_ci);
509 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
511 rte_prefetch0(*pkts);
512 length = DATA_LEN(buf);
513 /* Update element. */
514 (*txq->elts)[elts_head] = buf;
515 /* Prefetch next buffer data. */
517 rte_prefetch0(rte_pktmbuf_mtod(*pkts,
519 /* Should we enable HW CKSUM offload */
521 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
522 wqe->wqe.eseg.cs_flags =
523 MLX5_ETH_WQE_L3_CSUM |
524 MLX5_ETH_WQE_L4_CSUM;
526 wqe->wqe.eseg.cs_flags = 0;
528 ds = mlx5_wqe_write(txq, wqe, buf, length);
531 dseg = (volatile struct mlx5_wqe_data_seg *)
532 (((uintptr_t)wqe) + ds * 16);
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.
539 assert(!(sizeof(*wqe) % sizeof(*dseg)));
540 if (!(ds % (sizeof(*wqe) / 16)))
541 dseg = (volatile void *)
542 &(*txq->wqes)[txq->wqe_ci++ &
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);
560 /* Update DS field in WQE. */
561 wqe->wqe.ctrl.data[1] &= htonl(0xffffffc0);
562 wqe->wqe.ctrl.data[1] |= htonl(ds & 0x3f);
564 #ifdef MLX5_PMD_SOFT_COUNTERS
565 /* Increment sent bytes counter. */
566 txq->stats.obytes += length;
568 /* Increment consumer index. */
569 txq->wqe_ci += (ds + 3) / 4;
570 elts_head = elts_head_next;
573 /* Take a shortcut if nothing must be sent. */
574 if (unlikely(i == 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->wqe.ctrl.data[2] = htonl(8);
581 /* Save elts_head in unused "immediate" field of WQE. */
582 wqe->wqe.ctrl.data[3] = elts_head;
585 txq->elts_comp = comp;
587 #ifdef MLX5_PMD_SOFT_COUNTERS
588 /* Increment sent packets counter. */
589 txq->stats.opackets += i;
591 /* Ring QP doorbell. */
593 txq->elts_head = elts_head;
598 * Open a MPW session.
601 * Pointer to TX queue structure.
603 * Pointer to MPW session structure.
608 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
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)];
615 mpw->state = MLX5_MPW_STATE_OPENED;
619 mpw->wqe = &(*txq->wqes)[idx];
620 mpw->wqe->mpw.eseg.mss = htons(length);
621 mpw->wqe->mpw.eseg.inline_hdr_sz = 0;
622 mpw->wqe->mpw.eseg.rsvd0 = 0;
623 mpw->wqe->mpw.eseg.rsvd1 = 0;
624 mpw->wqe->mpw.eseg.rsvd2 = 0;
625 mpw->wqe->mpw.ctrl.data[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
627 MLX5_OPCODE_LSO_MPW);
628 mpw->wqe->mpw.ctrl.data[2] = 0;
629 mpw->wqe->mpw.ctrl.data[3] = 0;
630 mpw->data.dseg[0] = &mpw->wqe->mpw.dseg[0];
631 mpw->data.dseg[1] = &mpw->wqe->mpw.dseg[1];
632 mpw->data.dseg[2] = &(*dseg)[0];
633 mpw->data.dseg[3] = &(*dseg)[1];
634 mpw->data.dseg[4] = &(*dseg)[2];
638 * Close a MPW session.
641 * Pointer to TX queue structure.
643 * Pointer to MPW session structure.
646 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
648 unsigned int num = mpw->pkts_n;
651 * Store size in multiple of 16 bytes. Control and Ethernet segments
654 mpw->wqe->mpw.ctrl.data[1] = htonl(txq->qp_num_8s | (2 + num));
655 mpw->state = MLX5_MPW_STATE_CLOSED;
660 tx_prefetch_wqe(txq, txq->wqe_ci);
661 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
665 * DPDK callback for TX with MPW support.
668 * Generic pointer to TX queue structure.
670 * Packets to transmit.
672 * Number of packets in array.
675 * Number of packets successfully transmitted (<= pkts_n).
678 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
680 struct txq *txq = (struct txq *)dpdk_txq;
681 uint16_t elts_head = txq->elts_head;
682 const unsigned int elts_n = txq->elts_n;
687 struct mlx5_mpw mpw = {
688 .state = MLX5_MPW_STATE_CLOSED,
691 if (unlikely(!pkts_n))
693 /* Prefetch first packet cacheline. */
694 tx_prefetch_cqe(txq, txq->cq_ci);
695 tx_prefetch_wqe(txq, txq->wqe_ci);
696 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
697 /* Start processing. */
699 max = (elts_n - (elts_head - txq->elts_tail));
703 struct rte_mbuf *buf = *(pkts++);
704 unsigned int elts_head_next;
706 unsigned int segs_n = buf->nb_segs;
707 uint32_t cs_flags = 0;
710 * Make sure there is enough room to store this packet and
711 * that one ring entry remains unused.
714 if (max < segs_n + 1)
716 /* Do not bother with large packets MPW cannot handle. */
717 if (segs_n > MLX5_MPW_DSEG_MAX)
721 /* Should we enable HW CKSUM offload */
723 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
724 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
725 /* Retrieve packet information. */
726 length = PKT_LEN(buf);
728 /* Start new session if packet differs. */
729 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
730 ((mpw.len != length) ||
732 (mpw.wqe->mpw.eseg.cs_flags != cs_flags)))
733 mlx5_mpw_close(txq, &mpw);
734 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
735 mlx5_mpw_new(txq, &mpw, length);
736 mpw.wqe->mpw.eseg.cs_flags = cs_flags;
738 /* Multi-segment packets must be alone in their MPW. */
739 assert((segs_n == 1) || (mpw.pkts_n == 0));
740 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
744 volatile struct mlx5_wqe_data_seg *dseg;
747 elts_head_next = (elts_head + 1) & (elts_n - 1);
749 (*txq->elts)[elts_head] = buf;
750 dseg = mpw.data.dseg[mpw.pkts_n];
751 addr = rte_pktmbuf_mtod(buf, uintptr_t);
752 *dseg = (struct mlx5_wqe_data_seg){
753 .byte_count = htonl(DATA_LEN(buf)),
754 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
755 .addr = htonll(addr),
757 elts_head = elts_head_next;
758 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
759 length += DATA_LEN(buf);
765 assert(length == mpw.len);
766 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
767 mlx5_mpw_close(txq, &mpw);
768 elts_head = elts_head_next;
769 #ifdef MLX5_PMD_SOFT_COUNTERS
770 /* Increment sent bytes counter. */
771 txq->stats.obytes += length;
775 /* Take a shortcut if nothing must be sent. */
776 if (unlikely(i == 0))
778 /* Check whether completion threshold has been reached. */
779 /* "j" includes both packets and segments. */
780 comp = txq->elts_comp + j;
781 if (comp >= MLX5_TX_COMP_THRESH) {
782 volatile union mlx5_wqe *wqe = mpw.wqe;
784 /* Request completion on last WQE. */
785 wqe->mpw.ctrl.data[2] = htonl(8);
786 /* Save elts_head in unused "immediate" field of WQE. */
787 wqe->mpw.ctrl.data[3] = elts_head;
790 txq->elts_comp = comp;
792 #ifdef MLX5_PMD_SOFT_COUNTERS
793 /* Increment sent packets counter. */
794 txq->stats.opackets += i;
796 /* Ring QP doorbell. */
797 if (mpw.state == MLX5_MPW_STATE_OPENED)
798 mlx5_mpw_close(txq, &mpw);
800 txq->elts_head = elts_head;
805 * Open a MPW inline session.
808 * Pointer to TX queue structure.
810 * Pointer to MPW session structure.
815 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
817 uint16_t idx = txq->wqe_ci & (txq->wqe_n - 1);
819 mpw->state = MLX5_MPW_INL_STATE_OPENED;
823 mpw->wqe = &(*txq->wqes)[idx];
824 mpw->wqe->mpw_inl.ctrl.data[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
826 MLX5_OPCODE_LSO_MPW);
827 mpw->wqe->mpw_inl.ctrl.data[2] = 0;
828 mpw->wqe->mpw_inl.ctrl.data[3] = 0;
829 mpw->wqe->mpw_inl.eseg.mss = htons(length);
830 mpw->wqe->mpw_inl.eseg.inline_hdr_sz = 0;
831 mpw->wqe->mpw_inl.eseg.cs_flags = 0;
832 mpw->wqe->mpw_inl.eseg.rsvd0 = 0;
833 mpw->wqe->mpw_inl.eseg.rsvd1 = 0;
834 mpw->wqe->mpw_inl.eseg.rsvd2 = 0;
835 mpw->data.raw = &mpw->wqe->mpw_inl.data[0];
839 * Close a MPW inline session.
842 * Pointer to TX queue structure.
844 * Pointer to MPW session structure.
847 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
851 size = sizeof(*mpw->wqe) - MLX5_MWQE64_INL_DATA + mpw->total_len;
853 * Store size in multiple of 16 bytes. Control and Ethernet segments
856 mpw->wqe->mpw_inl.ctrl.data[1] =
857 htonl(txq->qp_num_8s | ((size + 15) / 16));
858 mpw->state = MLX5_MPW_STATE_CLOSED;
859 mpw->wqe->mpw_inl.byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
860 txq->wqe_ci += (size + (sizeof(*mpw->wqe) - 1)) / sizeof(*mpw->wqe);
864 * DPDK callback for TX with MPW inline support.
867 * Generic pointer to TX queue structure.
869 * Packets to transmit.
871 * Number of packets in array.
874 * Number of packets successfully transmitted (<= pkts_n).
877 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
880 struct txq *txq = (struct txq *)dpdk_txq;
881 uint16_t elts_head = txq->elts_head;
882 const unsigned int elts_n = txq->elts_n;
887 unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
888 struct mlx5_mpw mpw = {
889 .state = MLX5_MPW_STATE_CLOSED,
892 if (unlikely(!pkts_n))
894 /* Prefetch first packet cacheline. */
895 tx_prefetch_cqe(txq, txq->cq_ci);
896 tx_prefetch_wqe(txq, txq->wqe_ci);
897 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
898 /* Start processing. */
900 max = (elts_n - (elts_head - txq->elts_tail));
904 struct rte_mbuf *buf = *(pkts++);
905 unsigned int elts_head_next;
908 unsigned int segs_n = buf->nb_segs;
909 uint32_t cs_flags = 0;
912 * Make sure there is enough room to store this packet and
913 * that one ring entry remains unused.
916 if (max < segs_n + 1)
918 /* Do not bother with large packets MPW cannot handle. */
919 if (segs_n > MLX5_MPW_DSEG_MAX)
923 /* Should we enable HW CKSUM offload */
925 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
926 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
927 /* Retrieve packet information. */
928 length = PKT_LEN(buf);
929 /* Start new session if packet differs. */
930 if (mpw.state == MLX5_MPW_STATE_OPENED) {
931 if ((mpw.len != length) ||
933 (mpw.wqe->mpw.eseg.cs_flags != cs_flags))
934 mlx5_mpw_close(txq, &mpw);
935 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
936 if ((mpw.len != length) ||
938 (length > inline_room) ||
939 (mpw.wqe->mpw_inl.eseg.cs_flags != cs_flags)) {
940 mlx5_mpw_inline_close(txq, &mpw);
942 txq->max_inline * RTE_CACHE_LINE_SIZE;
945 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
947 (length > inline_room)) {
948 mlx5_mpw_new(txq, &mpw, length);
949 mpw.wqe->mpw.eseg.cs_flags = cs_flags;
951 mlx5_mpw_inline_new(txq, &mpw, length);
952 mpw.wqe->mpw_inl.eseg.cs_flags = cs_flags;
955 /* Multi-segment packets must be alone in their MPW. */
956 assert((segs_n == 1) || (mpw.pkts_n == 0));
957 if (mpw.state == MLX5_MPW_STATE_OPENED) {
958 assert(inline_room ==
959 txq->max_inline * RTE_CACHE_LINE_SIZE);
960 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
964 volatile struct mlx5_wqe_data_seg *dseg;
967 (elts_head + 1) & (elts_n - 1);
969 (*txq->elts)[elts_head] = buf;
970 dseg = mpw.data.dseg[mpw.pkts_n];
971 addr = rte_pktmbuf_mtod(buf, uintptr_t);
972 *dseg = (struct mlx5_wqe_data_seg){
973 .byte_count = htonl(DATA_LEN(buf)),
974 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
975 .addr = htonll(addr),
977 elts_head = elts_head_next;
978 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
979 length += DATA_LEN(buf);
985 assert(length == mpw.len);
986 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
987 mlx5_mpw_close(txq, &mpw);
991 assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
992 assert(length <= inline_room);
993 assert(length == DATA_LEN(buf));
994 elts_head_next = (elts_head + 1) & (elts_n - 1);
995 addr = rte_pktmbuf_mtod(buf, uintptr_t);
996 (*txq->elts)[elts_head] = buf;
997 /* Maximum number of bytes before wrapping. */
998 max = ((uintptr_t)&(*txq->wqes)[txq->wqe_n] -
999 (uintptr_t)mpw.data.raw);
1001 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1005 (volatile void *)&(*txq->wqes)[0];
1006 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1007 (void *)(addr + max),
1009 mpw.data.raw += length - max;
1011 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1014 mpw.data.raw += length;
1016 if ((uintptr_t)mpw.data.raw ==
1017 (uintptr_t)&(*txq->wqes)[txq->wqe_n])
1019 (volatile void *)&(*txq->wqes)[0];
1022 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1023 mlx5_mpw_inline_close(txq, &mpw);
1025 txq->max_inline * RTE_CACHE_LINE_SIZE;
1027 inline_room -= length;
1030 mpw.total_len += length;
1031 elts_head = elts_head_next;
1032 #ifdef MLX5_PMD_SOFT_COUNTERS
1033 /* Increment sent bytes counter. */
1034 txq->stats.obytes += length;
1038 /* Take a shortcut if nothing must be sent. */
1039 if (unlikely(i == 0))
1041 /* Check whether completion threshold has been reached. */
1042 /* "j" includes both packets and segments. */
1043 comp = txq->elts_comp + j;
1044 if (comp >= MLX5_TX_COMP_THRESH) {
1045 volatile union mlx5_wqe *wqe = mpw.wqe;
1047 /* Request completion on last WQE. */
1048 wqe->mpw_inl.ctrl.data[2] = htonl(8);
1049 /* Save elts_head in unused "immediate" field of WQE. */
1050 wqe->mpw_inl.ctrl.data[3] = elts_head;
1053 txq->elts_comp = comp;
1055 #ifdef MLX5_PMD_SOFT_COUNTERS
1056 /* Increment sent packets counter. */
1057 txq->stats.opackets += i;
1059 /* Ring QP doorbell. */
1060 if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1061 mlx5_mpw_inline_close(txq, &mpw);
1062 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1063 mlx5_mpw_close(txq, &mpw);
1065 txq->elts_head = elts_head;
1070 * Translate RX completion flags to packet type.
1075 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1078 * Packet type for struct rte_mbuf.
1080 static inline uint32_t
1081 rxq_cq_to_pkt_type(volatile struct mlx5_cqe64 *cqe)
1084 uint8_t flags = cqe->l4_hdr_type_etc;
1085 uint8_t info = cqe->rsvd0[0];
1087 if (info & IBV_EXP_CQ_RX_TUNNEL_PACKET)
1090 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
1091 RTE_PTYPE_L3_IPV4) |
1093 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
1094 RTE_PTYPE_L3_IPV6) |
1096 IBV_EXP_CQ_RX_IPV4_PACKET,
1097 RTE_PTYPE_INNER_L3_IPV4) |
1099 IBV_EXP_CQ_RX_IPV6_PACKET,
1100 RTE_PTYPE_INNER_L3_IPV6);
1104 MLX5_CQE_L3_HDR_TYPE_IPV6,
1105 RTE_PTYPE_L3_IPV6) |
1107 MLX5_CQE_L3_HDR_TYPE_IPV4,
1113 * Get size of the next packet for a given CQE. For compressed CQEs, the
1114 * consumer index is updated only once all packets of the current one have
1118 * Pointer to RX queue.
1123 * Packet size in bytes (0 if there is none), -1 in case of completion
1127 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe,
1130 struct rxq_zip *zip = &rxq->zip;
1131 uint16_t cqe_n = cqe_cnt + 1;
1134 /* Process compressed data in the CQE and mini arrays. */
1136 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1137 (volatile struct mlx5_mini_cqe8 (*)[8])
1138 (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt].cqe64);
1140 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1141 if ((++zip->ai & 7) == 0) {
1143 * Increment consumer index to skip the number of
1144 * CQEs consumed. Hardware leaves holes in the CQ
1145 * ring for software use.
1150 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1151 uint16_t idx = rxq->cq_ci;
1152 uint16_t end = zip->cq_ci;
1154 while (idx != end) {
1155 (*rxq->cqes)[idx & cqe_cnt].cqe64.op_own =
1156 MLX5_CQE_INVALIDATE;
1159 rxq->cq_ci = zip->cq_ci;
1162 /* No compressed data, get next CQE and verify if it is compressed. */
1167 ret = check_cqe64(cqe, cqe_n, rxq->cq_ci);
1168 if (unlikely(ret == 1))
1171 op_own = cqe->op_own;
1172 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1173 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1174 (volatile struct mlx5_mini_cqe8 (*)[8])
1175 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1178 /* Fix endianness. */
1179 zip->cqe_cnt = ntohl(cqe->byte_cnt);
1181 * Current mini array position is the one returned by
1184 * If completion comprises several mini arrays, as a
1185 * special case the second one is located 7 CQEs after
1186 * the initial CQE instead of 8 for subsequent ones.
1188 zip->ca = rxq->cq_ci & cqe_cnt;
1189 zip->na = zip->ca + 7;
1190 /* Compute the next non compressed CQE. */
1192 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1193 /* Get packet size to return. */
1194 len = ntohl((*mc)[0].byte_cnt);
1197 len = ntohl(cqe->byte_cnt);
1199 /* Error while receiving packet. */
1200 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1207 * Translate RX completion flags to offload flags.
1210 * Pointer to RX queue structure.
1215 * Offload flags (ol_flags) for struct rte_mbuf.
1217 static inline uint32_t
1218 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe)
1220 uint32_t ol_flags = 0;
1221 uint8_t l3_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L3_HDR_TYPE_MASK;
1222 uint8_t l4_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L4_HDR_TYPE_MASK;
1223 uint8_t info = cqe->rsvd0[0];
1225 if ((l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV4) ||
1226 (l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV6))
1228 (!(cqe->hds_ip_ext & MLX5_CQE_L3_OK) *
1229 PKT_RX_IP_CKSUM_BAD);
1230 if ((l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP) ||
1231 (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_EMP_ACK) ||
1232 (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_ACK) ||
1233 (l4_hdr == MLX5_CQE_L4_HDR_TYPE_UDP))
1235 (!(cqe->hds_ip_ext & MLX5_CQE_L4_OK) *
1236 PKT_RX_L4_CKSUM_BAD);
1238 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
1239 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
1242 if ((info & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1244 TRANSPOSE(~cqe->l4_hdr_type_etc,
1245 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
1246 PKT_RX_IP_CKSUM_BAD) |
1247 TRANSPOSE(~cqe->l4_hdr_type_etc,
1248 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
1249 PKT_RX_L4_CKSUM_BAD);
1254 * DPDK callback for RX.
1257 * Generic pointer to RX queue structure.
1259 * Array to store received packets.
1261 * Maximum number of packets in array.
1264 * Number of packets successfully received (<= pkts_n).
1267 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1269 struct rxq *rxq = dpdk_rxq;
1270 const unsigned int wqe_cnt = rxq->elts_n - 1;
1271 const unsigned int cqe_cnt = rxq->cqe_n - 1;
1272 const unsigned int sges_n = rxq->sges_n;
1273 struct rte_mbuf *pkt = NULL;
1274 struct rte_mbuf *seg = NULL;
1275 volatile struct mlx5_cqe64 *cqe =
1276 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt].cqe64;
1278 unsigned int rq_ci = rxq->rq_ci << sges_n;
1282 unsigned int idx = rq_ci & wqe_cnt;
1283 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1284 struct rte_mbuf *rep = (*rxq->elts)[idx];
1292 rep = rte_mbuf_raw_alloc(rxq->mp);
1293 if (unlikely(rep == NULL)) {
1294 ++rxq->stats.rx_nombuf;
1297 * no buffers before we even started,
1298 * bail out silently.
1302 while (pkt != seg) {
1303 assert(pkt != (*rxq->elts)[idx]);
1305 rte_mbuf_refcnt_set(pkt, 0);
1306 __rte_mbuf_raw_free(pkt);
1312 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt].cqe64;
1313 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt);
1315 rte_mbuf_refcnt_set(rep, 0);
1316 __rte_mbuf_raw_free(rep);
1319 if (unlikely(len == -1)) {
1320 /* RX error, packet is likely too large. */
1321 rte_mbuf_refcnt_set(rep, 0);
1322 __rte_mbuf_raw_free(rep);
1323 ++rxq->stats.idropped;
1327 assert(len >= (rxq->crc_present << 2));
1328 /* Update packet information. */
1329 pkt->packet_type = 0;
1331 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
1335 rxq_cq_to_pkt_type(cqe);
1337 rxq_cq_to_ol_flags(rxq, cqe);
1339 if (cqe->l4_hdr_type_etc &
1340 MLX5_CQE_VLAN_STRIPPED) {
1341 pkt->ol_flags |= PKT_RX_VLAN_PKT |
1342 PKT_RX_VLAN_STRIPPED;
1343 pkt->vlan_tci = ntohs(cqe->vlan_info);
1345 if (rxq->crc_present)
1346 len -= ETHER_CRC_LEN;
1350 DATA_LEN(rep) = DATA_LEN(seg);
1351 PKT_LEN(rep) = PKT_LEN(seg);
1352 SET_DATA_OFF(rep, DATA_OFF(seg));
1353 NB_SEGS(rep) = NB_SEGS(seg);
1354 PORT(rep) = PORT(seg);
1356 (*rxq->elts)[idx] = rep;
1358 * Fill NIC descriptor with the new buffer. The lkey and size
1359 * of the buffers are already known, only the buffer address
1362 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
1363 if (len > DATA_LEN(seg)) {
1364 len -= DATA_LEN(seg);
1369 DATA_LEN(seg) = len;
1370 #ifdef MLX5_PMD_SOFT_COUNTERS
1371 /* Increment bytes counter. */
1372 rxq->stats.ibytes += PKT_LEN(pkt);
1374 /* Return packet. */
1380 /* Align consumer index to the next stride. */
1385 if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1387 /* Update the consumer index. */
1388 rxq->rq_ci = rq_ci >> sges_n;
1390 *rxq->cq_db = htonl(rxq->cq_ci);
1392 *rxq->rq_db = htonl(rxq->rq_ci);
1393 #ifdef MLX5_PMD_SOFT_COUNTERS
1394 /* Increment packets counter. */
1395 rxq->stats.ipackets += i;
1401 * Dummy DPDK callback for TX.
1403 * This function is used to temporarily replace the real callback during
1404 * unsafe control operations on the queue, or in case of error.
1407 * Generic pointer to TX queue structure.
1409 * Packets to transmit.
1411 * Number of packets in array.
1414 * Number of packets successfully transmitted (<= pkts_n).
1417 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1426 * Dummy DPDK callback for RX.
1428 * This function is used to temporarily replace the real callback during
1429 * unsafe control operations on the queue, or in case of error.
1432 * Generic pointer to RX queue structure.
1434 * Array to store received packets.
1436 * Maximum number of packets in array.
1439 * Number of packets successfully received (<= pkts_n).
1442 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)