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 "-Wpedantic"
44 #include <infiniband/verbs.h>
45 #include <infiniband/mlx5_hw.h>
46 #include <infiniband/arch.h>
48 #pragma GCC diagnostic error "-Wpedantic"
51 /* DPDK headers don't like -pedantic. */
53 #pragma GCC diagnostic ignored "-Wpedantic"
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 "-Wpedantic"
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 = 1 << txq->elts_n;
170 const unsigned int cqe_n = 1 << txq->cqe_n;
171 const unsigned int cqe_cnt = cqe_n - 1;
172 uint16_t elts_free = txq->elts_tail;
174 uint16_t cq_ci = txq->cq_ci;
175 volatile struct mlx5_cqe64 *cqe = NULL;
176 volatile struct 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) &
203 ((1 << txq->wqe_n) - 1)].hdr;
204 elts_tail = wqe->ctrl[3];
205 assert(elts_tail < (1 << txq->wqe_n));
207 while (elts_free != elts_tail) {
208 struct rte_mbuf *elt = (*txq->elts)[elts_free];
209 unsigned int elts_free_next =
210 (elts_free + 1) & (elts_n - 1);
211 struct rte_mbuf *elt_next = (*txq->elts)[elts_free_next];
215 memset(&(*txq->elts)[elts_free],
217 sizeof((*txq->elts)[elts_free]));
219 RTE_MBUF_PREFETCH_TO_FREE(elt_next);
220 /* Only one segment needs to be freed. */
221 rte_pktmbuf_free_seg(elt);
222 elts_free = elts_free_next;
225 txq->elts_tail = elts_tail;
226 /* Update the consumer index. */
228 *txq->cq_db = htonl(cq_ci);
232 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
233 * the cloned mbuf is allocated is returned instead.
239 * Memory pool where data is located for given mbuf.
241 static struct rte_mempool *
242 txq_mb2mp(struct rte_mbuf *buf)
244 if (unlikely(RTE_MBUF_INDIRECT(buf)))
245 return rte_mbuf_from_indirect(buf)->pool;
249 static inline uint32_t
250 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
251 __attribute__((always_inline));
254 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
255 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
256 * remove an entry first.
259 * Pointer to TX queue structure.
261 * Memory Pool for which a Memory Region lkey must be returned.
264 * mr->lkey on success, (uint32_t)-1 on failure.
266 static inline uint32_t
267 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
270 uint32_t lkey = (uint32_t)-1;
272 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
273 if (unlikely(txq->mp2mr[i].mp == NULL)) {
274 /* Unknown MP, add a new MR for it. */
277 if (txq->mp2mr[i].mp == mp) {
278 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
279 assert(htonl(txq->mp2mr[i].mr->lkey) ==
281 lkey = txq->mp2mr[i].lkey;
285 if (unlikely(lkey == (uint32_t)-1))
286 lkey = txq_mp2mr_reg(txq, mp, i);
291 * Ring TX queue doorbell.
294 * Pointer to TX queue structure.
297 mlx5_tx_dbrec(struct txq *txq)
299 uint8_t *dst = (uint8_t *)((uintptr_t)txq->bf_reg + txq->bf_offset);
301 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
302 htonl(txq->qp_num_8s),
307 *txq->qp_db = htonl(txq->wqe_ci);
308 /* Ensure ordering between DB record and BF copy. */
310 rte_mov16(dst, (uint8_t *)data);
311 txq->bf_offset ^= (1 << txq->bf_buf_size);
318 * Pointer to TX queue structure.
320 * CQE consumer index.
323 tx_prefetch_cqe(struct txq *txq, uint16_t ci)
325 volatile struct mlx5_cqe *cqe;
327 cqe = &(*txq->cqes)[ci & ((1 << txq->cqe_n) - 1)];
335 * Pointer to TX queue structure.
337 * WQE consumer index.
340 tx_prefetch_wqe(struct txq *txq, uint16_t ci)
342 volatile struct mlx5_wqe64 *wqe;
344 wqe = &(*txq->wqes)[ci & ((1 << txq->wqe_n) - 1)];
349 * DPDK callback for TX.
352 * Generic pointer to TX queue structure.
354 * Packets to transmit.
356 * Number of packets in array.
359 * Number of packets successfully transmitted (<= pkts_n).
362 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
364 struct txq *txq = (struct txq *)dpdk_txq;
365 uint16_t elts_head = txq->elts_head;
366 const unsigned int elts_n = 1 << txq->elts_n;
371 volatile struct mlx5_wqe *wqe = NULL;
372 unsigned int segs_n = 0;
373 struct rte_mbuf *buf = NULL;
376 if (unlikely(!pkts_n))
378 /* Prefetch first packet cacheline. */
379 tx_prefetch_cqe(txq, txq->cq_ci);
380 tx_prefetch_cqe(txq, txq->cq_ci + 1);
381 rte_prefetch0(*pkts);
382 /* Start processing. */
384 max = (elts_n - (elts_head - txq->elts_tail));
388 volatile struct mlx5_wqe_data_seg *dseg = NULL;
392 #ifdef MLX5_PMD_SOFT_COUNTERS
393 uint32_t total_length = 0;
398 segs_n = buf->nb_segs;
400 * Make sure there is enough room to store this packet and
401 * that one ring entry remains unused.
404 if (max < segs_n + 1)
410 wqe = &(*txq->wqes)[txq->wqe_ci &
411 ((1 << txq->wqe_n) - 1)].hdr;
412 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
414 rte_prefetch0(*pkts);
415 addr = rte_pktmbuf_mtod(buf, uintptr_t);
416 length = DATA_LEN(buf);
417 #ifdef MLX5_PMD_SOFT_COUNTERS
418 total_length = length;
420 assert(length >= MLX5_WQE_DWORD_SIZE);
421 /* Update element. */
422 (*txq->elts)[elts_head] = buf;
423 elts_head = (elts_head + 1) & (elts_n - 1);
424 /* Prefetch next buffer data. */
426 volatile void *pkt_addr;
428 pkt_addr = rte_pktmbuf_mtod(*pkts, volatile void *);
429 rte_prefetch0(pkt_addr);
431 /* Should we enable HW CKSUM offload */
433 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
435 MLX5_ETH_WQE_L3_CSUM |
436 MLX5_ETH_WQE_L4_CSUM;
438 wqe->eseg.cs_flags = 0;
440 raw = (uint8_t *)(uintptr_t)&wqe->eseg.inline_hdr[0];
441 /* Start the know and common part of the WQE structure. */
442 wqe->ctrl[0] = htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND);
449 /* Start by copying the Ethernet Header. */
450 rte_mov16((uint8_t *)raw, (uint8_t *)addr);
451 length -= MLX5_WQE_DWORD_SIZE;
452 addr += MLX5_WQE_DWORD_SIZE;
453 /* Replace the Ethernet type by the VLAN if necessary. */
454 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
455 uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
457 memcpy((uint8_t *)(raw + MLX5_WQE_DWORD_SIZE -
459 &vlan, sizeof(vlan));
460 addr -= sizeof(vlan);
461 length += sizeof(vlan);
463 /* Inline if enough room. */
464 if (txq->max_inline != 0) {
466 (uintptr_t)&(*txq->wqes)[1 << txq->wqe_n];
467 uint16_t max_inline =
468 txq->max_inline * RTE_CACHE_LINE_SIZE;
469 uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE;
472 raw += MLX5_WQE_DWORD_SIZE;
473 room = end - (uintptr_t)raw;
474 if (room > max_inline) {
475 uintptr_t addr_end = (addr + max_inline) &
476 ~(RTE_CACHE_LINE_SIZE - 1);
477 uint16_t copy_b = ((addr_end - addr) > length) ?
481 rte_memcpy((void *)raw, (void *)addr, copy_b);
484 pkt_inline_sz += copy_b;
486 assert(addr <= addr_end);
488 /* Store the inlined packet size in the WQE. */
489 wqe->eseg.inline_hdr_sz = htons(pkt_inline_sz);
491 * 2 DWORDs consumed by the WQE header + 1 DSEG +
492 * the size of the inline part of the packet.
494 ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
496 dseg = (struct mlx5_wqe_data_seg *)
498 (ds * MLX5_WQE_DWORD_SIZE));
499 if ((uintptr_t)dseg >= end)
500 dseg = (struct mlx5_wqe_data_seg *)
501 ((uintptr_t)&(*txq->wqes)[0]);
503 } else if (!segs_n) {
510 * No inline has been done in the packet, only the
511 * Ethernet Header as been stored.
513 wqe->eseg.inline_hdr_sz = htons(MLX5_WQE_DWORD_SIZE);
514 dseg = (struct mlx5_wqe_data_seg *)
515 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
518 /* Add the remaining packet as a simple ds. */
519 *dseg = (struct mlx5_wqe_data_seg) {
520 .addr = htonll(addr),
521 .byte_count = htonl(length),
522 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
533 * Spill on next WQE when the current one does not have
534 * enough room left. Size of WQE must a be a multiple
535 * of data segment size.
537 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
538 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
539 unsigned int n = (txq->wqe_ci + ((ds + 3) / 4)) &
540 ((1 << txq->wqe_n) - 1);
542 dseg = (struct mlx5_wqe_data_seg *)
543 ((uintptr_t)&(*txq->wqes)[n]);
544 tx_prefetch_wqe(txq, n + 1);
551 length = DATA_LEN(buf);
552 #ifdef MLX5_PMD_SOFT_COUNTERS
553 total_length += length;
555 /* Store segment information. */
556 *dseg = (struct mlx5_wqe_data_seg) {
557 .addr = htonll(rte_pktmbuf_mtod(buf, uintptr_t)),
558 .byte_count = htonl(length),
559 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
561 (*txq->elts)[elts_head] = buf;
562 elts_head = (elts_head + 1) & (elts_n - 1);
571 wqe->ctrl[1] = htonl(txq->qp_num_8s | ds);
572 txq->wqe_ci += (ds + 3) / 4;
573 #ifdef MLX5_PMD_SOFT_COUNTERS
574 /* Increment sent bytes counter. */
575 txq->stats.obytes += total_length;
578 /* Take a shortcut if nothing must be sent. */
579 if (unlikely(i == 0))
581 /* Check whether completion threshold has been reached. */
582 comp = txq->elts_comp + i + j;
583 if (comp >= MLX5_TX_COMP_THRESH) {
584 /* Request completion on last WQE. */
585 wqe->ctrl[2] = htonl(8);
586 /* Save elts_head in unused "immediate" field of WQE. */
587 wqe->ctrl[3] = elts_head;
590 txq->elts_comp = comp;
592 #ifdef MLX5_PMD_SOFT_COUNTERS
593 /* Increment sent packets counter. */
594 txq->stats.opackets += i;
596 /* Ring QP doorbell. */
598 txq->elts_head = elts_head;
603 * Open a MPW session.
606 * Pointer to TX queue structure.
608 * Pointer to MPW session structure.
613 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
615 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
616 volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
617 (volatile struct mlx5_wqe_data_seg (*)[])
618 (uintptr_t)&(*txq->wqes)[(idx + 1) & ((1 << txq->wqe_n) - 1)];
620 mpw->state = MLX5_MPW_STATE_OPENED;
624 mpw->wqe = (volatile struct mlx5_wqe *)&(*txq->wqes)[idx].hdr;
625 mpw->wqe->eseg.mss = htons(length);
626 mpw->wqe->eseg.inline_hdr_sz = 0;
627 mpw->wqe->eseg.rsvd0 = 0;
628 mpw->wqe->eseg.rsvd1 = 0;
629 mpw->wqe->eseg.rsvd2 = 0;
630 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
631 (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
632 mpw->wqe->ctrl[2] = 0;
633 mpw->wqe->ctrl[3] = 0;
634 mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
635 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
636 mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
637 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
638 mpw->data.dseg[2] = &(*dseg)[0];
639 mpw->data.dseg[3] = &(*dseg)[1];
640 mpw->data.dseg[4] = &(*dseg)[2];
644 * Close a MPW session.
647 * Pointer to TX queue structure.
649 * Pointer to MPW session structure.
652 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
654 unsigned int num = mpw->pkts_n;
657 * Store size in multiple of 16 bytes. Control and Ethernet segments
660 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
661 mpw->state = MLX5_MPW_STATE_CLOSED;
666 tx_prefetch_wqe(txq, txq->wqe_ci);
667 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
671 * DPDK callback for TX with MPW support.
674 * Generic pointer to TX queue structure.
676 * Packets to transmit.
678 * Number of packets in array.
681 * Number of packets successfully transmitted (<= pkts_n).
684 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
686 struct txq *txq = (struct txq *)dpdk_txq;
687 uint16_t elts_head = txq->elts_head;
688 const unsigned int elts_n = 1 << txq->elts_n;
693 struct mlx5_mpw mpw = {
694 .state = MLX5_MPW_STATE_CLOSED,
697 if (unlikely(!pkts_n))
699 /* Prefetch first packet cacheline. */
700 tx_prefetch_cqe(txq, txq->cq_ci);
701 tx_prefetch_wqe(txq, txq->wqe_ci);
702 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
703 /* Start processing. */
705 max = (elts_n - (elts_head - txq->elts_tail));
709 struct rte_mbuf *buf = *(pkts++);
710 unsigned int elts_head_next;
712 unsigned int segs_n = buf->nb_segs;
713 uint32_t cs_flags = 0;
716 * Make sure there is enough room to store this packet and
717 * that one ring entry remains unused.
720 if (max < segs_n + 1)
722 /* Do not bother with large packets MPW cannot handle. */
723 if (segs_n > MLX5_MPW_DSEG_MAX)
727 /* Should we enable HW CKSUM offload */
729 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
730 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
731 /* Retrieve packet information. */
732 length = PKT_LEN(buf);
734 /* Start new session if packet differs. */
735 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
736 ((mpw.len != length) ||
738 (mpw.wqe->eseg.cs_flags != cs_flags)))
739 mlx5_mpw_close(txq, &mpw);
740 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
741 mlx5_mpw_new(txq, &mpw, length);
742 mpw.wqe->eseg.cs_flags = cs_flags;
744 /* Multi-segment packets must be alone in their MPW. */
745 assert((segs_n == 1) || (mpw.pkts_n == 0));
746 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
750 volatile struct mlx5_wqe_data_seg *dseg;
753 elts_head_next = (elts_head + 1) & (elts_n - 1);
755 (*txq->elts)[elts_head] = buf;
756 dseg = mpw.data.dseg[mpw.pkts_n];
757 addr = rte_pktmbuf_mtod(buf, uintptr_t);
758 *dseg = (struct mlx5_wqe_data_seg){
759 .byte_count = htonl(DATA_LEN(buf)),
760 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
761 .addr = htonll(addr),
763 elts_head = elts_head_next;
764 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
765 length += DATA_LEN(buf);
771 assert(length == mpw.len);
772 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
773 mlx5_mpw_close(txq, &mpw);
774 elts_head = elts_head_next;
775 #ifdef MLX5_PMD_SOFT_COUNTERS
776 /* Increment sent bytes counter. */
777 txq->stats.obytes += length;
781 /* Take a shortcut if nothing must be sent. */
782 if (unlikely(i == 0))
784 /* Check whether completion threshold has been reached. */
785 /* "j" includes both packets and segments. */
786 comp = txq->elts_comp + j;
787 if (comp >= MLX5_TX_COMP_THRESH) {
788 volatile struct mlx5_wqe *wqe = mpw.wqe;
790 /* Request completion on last WQE. */
791 wqe->ctrl[2] = htonl(8);
792 /* Save elts_head in unused "immediate" field of WQE. */
793 wqe->ctrl[3] = elts_head;
796 txq->elts_comp = comp;
798 #ifdef MLX5_PMD_SOFT_COUNTERS
799 /* Increment sent packets counter. */
800 txq->stats.opackets += i;
802 /* Ring QP doorbell. */
803 if (mpw.state == MLX5_MPW_STATE_OPENED)
804 mlx5_mpw_close(txq, &mpw);
806 txq->elts_head = elts_head;
811 * Open a MPW inline session.
814 * Pointer to TX queue structure.
816 * Pointer to MPW session structure.
821 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
823 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
824 struct mlx5_wqe_inl_small *inl;
826 mpw->state = MLX5_MPW_INL_STATE_OPENED;
830 mpw->wqe = (volatile struct mlx5_wqe *)&(*txq->wqes)[idx].hdr;
831 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
834 mpw->wqe->ctrl[2] = 0;
835 mpw->wqe->ctrl[3] = 0;
836 mpw->wqe->eseg.mss = htons(length);
837 mpw->wqe->eseg.inline_hdr_sz = 0;
838 mpw->wqe->eseg.cs_flags = 0;
839 mpw->wqe->eseg.rsvd0 = 0;
840 mpw->wqe->eseg.rsvd1 = 0;
841 mpw->wqe->eseg.rsvd2 = 0;
842 inl = (struct mlx5_wqe_inl_small *)
843 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
844 mpw->data.raw = (uint8_t *)&inl->raw;
848 * Close a MPW inline session.
851 * Pointer to TX queue structure.
853 * Pointer to MPW session structure.
856 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
859 struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
860 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
862 size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
864 * Store size in multiple of 16 bytes. Control and Ethernet segments
867 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
868 mpw->state = MLX5_MPW_STATE_CLOSED;
869 inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
870 txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
874 * DPDK callback for TX with MPW inline support.
877 * Generic pointer to TX queue structure.
879 * Packets to transmit.
881 * Number of packets in array.
884 * Number of packets successfully transmitted (<= pkts_n).
887 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
890 struct txq *txq = (struct txq *)dpdk_txq;
891 uint16_t elts_head = txq->elts_head;
892 const unsigned int elts_n = 1 << txq->elts_n;
897 unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
898 struct mlx5_mpw mpw = {
899 .state = MLX5_MPW_STATE_CLOSED,
902 if (unlikely(!pkts_n))
904 /* Prefetch first packet cacheline. */
905 tx_prefetch_cqe(txq, txq->cq_ci);
906 tx_prefetch_wqe(txq, txq->wqe_ci);
907 tx_prefetch_wqe(txq, txq->wqe_ci + 1);
908 /* Start processing. */
910 max = (elts_n - (elts_head - txq->elts_tail));
914 struct rte_mbuf *buf = *(pkts++);
915 unsigned int elts_head_next;
918 unsigned int segs_n = buf->nb_segs;
919 uint32_t cs_flags = 0;
922 * Make sure there is enough room to store this packet and
923 * that one ring entry remains unused.
926 if (max < segs_n + 1)
928 /* Do not bother with large packets MPW cannot handle. */
929 if (segs_n > MLX5_MPW_DSEG_MAX)
933 /* Should we enable HW CKSUM offload */
935 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
936 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
937 /* Retrieve packet information. */
938 length = PKT_LEN(buf);
939 /* Start new session if packet differs. */
940 if (mpw.state == MLX5_MPW_STATE_OPENED) {
941 if ((mpw.len != length) ||
943 (mpw.wqe->eseg.cs_flags != cs_flags))
944 mlx5_mpw_close(txq, &mpw);
945 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
946 if ((mpw.len != length) ||
948 (length > inline_room) ||
949 (mpw.wqe->eseg.cs_flags != cs_flags)) {
950 mlx5_mpw_inline_close(txq, &mpw);
952 txq->max_inline * RTE_CACHE_LINE_SIZE;
955 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
957 (length > inline_room)) {
958 mlx5_mpw_new(txq, &mpw, length);
959 mpw.wqe->eseg.cs_flags = cs_flags;
961 mlx5_mpw_inline_new(txq, &mpw, length);
962 mpw.wqe->eseg.cs_flags = cs_flags;
965 /* Multi-segment packets must be alone in their MPW. */
966 assert((segs_n == 1) || (mpw.pkts_n == 0));
967 if (mpw.state == MLX5_MPW_STATE_OPENED) {
968 assert(inline_room ==
969 txq->max_inline * RTE_CACHE_LINE_SIZE);
970 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
974 volatile struct mlx5_wqe_data_seg *dseg;
977 (elts_head + 1) & (elts_n - 1);
979 (*txq->elts)[elts_head] = buf;
980 dseg = mpw.data.dseg[mpw.pkts_n];
981 addr = rte_pktmbuf_mtod(buf, uintptr_t);
982 *dseg = (struct mlx5_wqe_data_seg){
983 .byte_count = htonl(DATA_LEN(buf)),
984 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
985 .addr = htonll(addr),
987 elts_head = elts_head_next;
988 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
989 length += DATA_LEN(buf);
995 assert(length == mpw.len);
996 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
997 mlx5_mpw_close(txq, &mpw);
1001 assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1002 assert(length <= inline_room);
1003 assert(length == DATA_LEN(buf));
1004 elts_head_next = (elts_head + 1) & (elts_n - 1);
1005 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1006 (*txq->elts)[elts_head] = buf;
1007 /* Maximum number of bytes before wrapping. */
1008 max = ((uintptr_t)&(*txq->wqes)[1 << txq->wqe_n] -
1009 (uintptr_t)mpw.data.raw);
1011 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1015 (volatile void *)&(*txq->wqes)[0];
1016 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1017 (void *)(addr + max),
1019 mpw.data.raw += length - max;
1021 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1024 mpw.data.raw += length;
1026 if ((uintptr_t)mpw.data.raw ==
1027 (uintptr_t)&(*txq->wqes)[1 << txq->wqe_n])
1029 (volatile void *)&(*txq->wqes)[0];
1032 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1033 mlx5_mpw_inline_close(txq, &mpw);
1035 txq->max_inline * RTE_CACHE_LINE_SIZE;
1037 inline_room -= length;
1040 mpw.total_len += length;
1041 elts_head = elts_head_next;
1042 #ifdef MLX5_PMD_SOFT_COUNTERS
1043 /* Increment sent bytes counter. */
1044 txq->stats.obytes += length;
1048 /* Take a shortcut if nothing must be sent. */
1049 if (unlikely(i == 0))
1051 /* Check whether completion threshold has been reached. */
1052 /* "j" includes both packets and segments. */
1053 comp = txq->elts_comp + j;
1054 if (comp >= MLX5_TX_COMP_THRESH) {
1055 volatile struct mlx5_wqe *wqe = mpw.wqe;
1057 /* Request completion on last WQE. */
1058 wqe->ctrl[2] = htonl(8);
1059 /* Save elts_head in unused "immediate" field of WQE. */
1060 wqe->ctrl[3] = elts_head;
1063 txq->elts_comp = comp;
1065 #ifdef MLX5_PMD_SOFT_COUNTERS
1066 /* Increment sent packets counter. */
1067 txq->stats.opackets += i;
1069 /* Ring QP doorbell. */
1070 if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1071 mlx5_mpw_inline_close(txq, &mpw);
1072 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1073 mlx5_mpw_close(txq, &mpw);
1075 txq->elts_head = elts_head;
1080 * Translate RX completion flags to packet type.
1085 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1088 * Packet type for struct rte_mbuf.
1090 static inline uint32_t
1091 rxq_cq_to_pkt_type(volatile struct mlx5_cqe64 *cqe)
1094 uint8_t flags = cqe->l4_hdr_type_etc;
1095 uint8_t info = cqe->rsvd0[0];
1097 if (info & IBV_EXP_CQ_RX_TUNNEL_PACKET)
1100 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
1101 RTE_PTYPE_L3_IPV4) |
1103 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
1104 RTE_PTYPE_L3_IPV6) |
1106 IBV_EXP_CQ_RX_IPV4_PACKET,
1107 RTE_PTYPE_INNER_L3_IPV4) |
1109 IBV_EXP_CQ_RX_IPV6_PACKET,
1110 RTE_PTYPE_INNER_L3_IPV6);
1114 MLX5_CQE_L3_HDR_TYPE_IPV6,
1115 RTE_PTYPE_L3_IPV6) |
1117 MLX5_CQE_L3_HDR_TYPE_IPV4,
1123 * Get size of the next packet for a given CQE. For compressed CQEs, the
1124 * consumer index is updated only once all packets of the current one have
1128 * Pointer to RX queue.
1131 * @param[out] rss_hash
1132 * Packet RSS Hash result.
1135 * Packet size in bytes (0 if there is none), -1 in case of completion
1139 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe,
1140 uint16_t cqe_cnt, uint32_t *rss_hash)
1142 struct rxq_zip *zip = &rxq->zip;
1143 uint16_t cqe_n = cqe_cnt + 1;
1146 /* Process compressed data in the CQE and mini arrays. */
1148 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1149 (volatile struct mlx5_mini_cqe8 (*)[8])
1150 (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt].cqe64);
1152 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1153 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1154 if ((++zip->ai & 7) == 0) {
1156 * Increment consumer index to skip the number of
1157 * CQEs consumed. Hardware leaves holes in the CQ
1158 * ring for software use.
1163 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1164 uint16_t idx = rxq->cq_ci;
1165 uint16_t end = zip->cq_ci;
1167 while (idx != end) {
1168 (*rxq->cqes)[idx & cqe_cnt].cqe64.op_own =
1169 MLX5_CQE_INVALIDATE;
1172 rxq->cq_ci = zip->cq_ci;
1175 /* No compressed data, get next CQE and verify if it is compressed. */
1180 ret = check_cqe64(cqe, cqe_n, rxq->cq_ci);
1181 if (unlikely(ret == 1))
1184 op_own = cqe->op_own;
1185 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1186 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1187 (volatile struct mlx5_mini_cqe8 (*)[8])
1188 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1191 /* Fix endianness. */
1192 zip->cqe_cnt = ntohl(cqe->byte_cnt);
1194 * Current mini array position is the one returned by
1197 * If completion comprises several mini arrays, as a
1198 * special case the second one is located 7 CQEs after
1199 * the initial CQE instead of 8 for subsequent ones.
1201 zip->ca = rxq->cq_ci & cqe_cnt;
1202 zip->na = zip->ca + 7;
1203 /* Compute the next non compressed CQE. */
1205 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1206 /* Get packet size to return. */
1207 len = ntohl((*mc)[0].byte_cnt);
1208 *rss_hash = ntohl((*mc)[0].rx_hash_result);
1211 len = ntohl(cqe->byte_cnt);
1212 *rss_hash = ntohl(cqe->rx_hash_res);
1214 /* Error while receiving packet. */
1215 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1222 * Translate RX completion flags to offload flags.
1225 * Pointer to RX queue structure.
1230 * Offload flags (ol_flags) for struct rte_mbuf.
1232 static inline uint32_t
1233 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe64 *cqe)
1235 uint32_t ol_flags = 0;
1236 uint8_t l3_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L3_HDR_TYPE_MASK;
1237 uint8_t l4_hdr = (cqe->l4_hdr_type_etc) & MLX5_CQE_L4_HDR_TYPE_MASK;
1238 uint8_t info = cqe->rsvd0[0];
1240 if ((l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV4) ||
1241 (l3_hdr == MLX5_CQE_L3_HDR_TYPE_IPV6))
1243 (!(cqe->hds_ip_ext & MLX5_CQE_L3_OK) *
1244 PKT_RX_IP_CKSUM_BAD);
1245 if ((l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP) ||
1246 (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_EMP_ACK) ||
1247 (l4_hdr == MLX5_CQE_L4_HDR_TYPE_TCP_ACK) ||
1248 (l4_hdr == MLX5_CQE_L4_HDR_TYPE_UDP))
1250 (!(cqe->hds_ip_ext & MLX5_CQE_L4_OK) *
1251 PKT_RX_L4_CKSUM_BAD);
1253 * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
1254 * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
1257 if ((info & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1259 TRANSPOSE(~cqe->l4_hdr_type_etc,
1260 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
1261 PKT_RX_IP_CKSUM_BAD) |
1262 TRANSPOSE(~cqe->l4_hdr_type_etc,
1263 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
1264 PKT_RX_L4_CKSUM_BAD);
1269 * DPDK callback for RX.
1272 * Generic pointer to RX queue structure.
1274 * Array to store received packets.
1276 * Maximum number of packets in array.
1279 * Number of packets successfully received (<= pkts_n).
1282 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1284 struct rxq *rxq = dpdk_rxq;
1285 const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1286 const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1287 const unsigned int sges_n = rxq->sges_n;
1288 struct rte_mbuf *pkt = NULL;
1289 struct rte_mbuf *seg = NULL;
1290 volatile struct mlx5_cqe64 *cqe =
1291 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt].cqe64;
1293 unsigned int rq_ci = rxq->rq_ci << sges_n;
1294 int len; /* keep its value across iterations. */
1297 unsigned int idx = rq_ci & wqe_cnt;
1298 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1299 struct rte_mbuf *rep = (*rxq->elts)[idx];
1300 uint32_t rss_hash_res = 0;
1308 rep = rte_mbuf_raw_alloc(rxq->mp);
1309 if (unlikely(rep == NULL)) {
1310 ++rxq->stats.rx_nombuf;
1313 * no buffers before we even started,
1314 * bail out silently.
1318 while (pkt != seg) {
1319 assert(pkt != (*rxq->elts)[idx]);
1321 rte_mbuf_refcnt_set(pkt, 0);
1322 __rte_mbuf_raw_free(pkt);
1328 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt].cqe64;
1329 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
1332 rte_mbuf_refcnt_set(rep, 0);
1333 __rte_mbuf_raw_free(rep);
1336 if (unlikely(len == -1)) {
1337 /* RX error, packet is likely too large. */
1338 rte_mbuf_refcnt_set(rep, 0);
1339 __rte_mbuf_raw_free(rep);
1340 ++rxq->stats.idropped;
1344 assert(len >= (rxq->crc_present << 2));
1345 /* Update packet information. */
1346 pkt->packet_type = 0;
1348 if (rxq->rss_hash) {
1349 pkt->hash.rss = rss_hash_res;
1350 pkt->ol_flags = PKT_RX_RSS_HASH;
1352 if (rxq->csum | rxq->csum_l2tun | rxq->vlan_strip |
1356 rxq_cq_to_pkt_type(cqe);
1358 rxq_cq_to_ol_flags(rxq, cqe);
1360 if (cqe->l4_hdr_type_etc &
1361 MLX5_CQE_VLAN_STRIPPED) {
1362 pkt->ol_flags |= PKT_RX_VLAN_PKT |
1363 PKT_RX_VLAN_STRIPPED;
1364 pkt->vlan_tci = ntohs(cqe->vlan_info);
1366 if (rxq->crc_present)
1367 len -= ETHER_CRC_LEN;
1371 DATA_LEN(rep) = DATA_LEN(seg);
1372 PKT_LEN(rep) = PKT_LEN(seg);
1373 SET_DATA_OFF(rep, DATA_OFF(seg));
1374 NB_SEGS(rep) = NB_SEGS(seg);
1375 PORT(rep) = PORT(seg);
1377 (*rxq->elts)[idx] = rep;
1379 * Fill NIC descriptor with the new buffer. The lkey and size
1380 * of the buffers are already known, only the buffer address
1383 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
1384 if (len > DATA_LEN(seg)) {
1385 len -= DATA_LEN(seg);
1390 DATA_LEN(seg) = len;
1391 #ifdef MLX5_PMD_SOFT_COUNTERS
1392 /* Increment bytes counter. */
1393 rxq->stats.ibytes += PKT_LEN(pkt);
1395 /* Return packet. */
1401 /* Align consumer index to the next stride. */
1406 if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1408 /* Update the consumer index. */
1409 rxq->rq_ci = rq_ci >> sges_n;
1411 *rxq->cq_db = htonl(rxq->cq_ci);
1413 *rxq->rq_db = htonl(rxq->rq_ci);
1414 #ifdef MLX5_PMD_SOFT_COUNTERS
1415 /* Increment packets counter. */
1416 rxq->stats.ipackets += i;
1422 * Dummy DPDK callback for TX.
1424 * This function is used to temporarily replace the real callback during
1425 * unsafe control operations on the queue, or in case of error.
1428 * Generic pointer to TX queue structure.
1430 * Packets to transmit.
1432 * Number of packets in array.
1435 * Number of packets successfully transmitted (<= pkts_n).
1438 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1447 * Dummy DPDK callback for RX.
1449 * This function is used to temporarily replace the real callback during
1450 * unsafe control operations on the queue, or in case of error.
1453 * Generic pointer to RX queue structure.
1455 * Array to store received packets.
1457 * Maximum number of packets in array.
1460 * Number of packets successfully received (<= pkts_n).
1463 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)