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"
73 check_cqe(volatile struct mlx5_cqe *cqe,
74 unsigned int cqes_n, const uint16_t ci)
75 __attribute__((always_inline));
78 txq_complete(struct txq *txq) __attribute__((always_inline));
80 static inline uint32_t
81 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
82 __attribute__((always_inline));
85 mlx5_tx_dbrec(struct txq *txq, volatile struct mlx5_wqe *wqe)
86 __attribute__((always_inline));
88 static inline uint32_t
89 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
90 __attribute__((always_inline));
93 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
94 uint16_t cqe_cnt, uint32_t *rss_hash)
95 __attribute__((always_inline));
97 static inline uint32_t
98 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
99 __attribute__((always_inline));
104 * Verify or set magic value in CQE.
113 check_cqe_seen(volatile struct mlx5_cqe *cqe)
115 static const uint8_t magic[] = "seen";
116 volatile uint8_t (*buf)[sizeof(cqe->rsvd0)] = &cqe->rsvd0;
120 for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
121 if (!ret || (*buf)[i] != magic[i]) {
123 (*buf)[i] = magic[i];
131 * Check whether CQE is valid.
136 * Size of completion queue.
141 * 0 on success, 1 on failure.
144 check_cqe(volatile struct mlx5_cqe *cqe,
145 unsigned int cqes_n, const uint16_t ci)
147 uint16_t idx = ci & cqes_n;
148 uint8_t op_own = cqe->op_own;
149 uint8_t op_owner = MLX5_CQE_OWNER(op_own);
150 uint8_t op_code = MLX5_CQE_OPCODE(op_own);
152 if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
153 return 1; /* No CQE. */
155 if ((op_code == MLX5_CQE_RESP_ERR) ||
156 (op_code == MLX5_CQE_REQ_ERR)) {
157 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
158 uint8_t syndrome = err_cqe->syndrome;
160 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
161 (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
163 if (!check_cqe_seen(cqe))
164 ERROR("unexpected CQE error %u (0x%02x)"
166 op_code, op_code, syndrome);
168 } else if ((op_code != MLX5_CQE_RESP_SEND) &&
169 (op_code != MLX5_CQE_REQ)) {
170 if (!check_cqe_seen(cqe))
171 ERROR("unexpected CQE opcode %u (0x%02x)",
180 * Return the address of the WQE.
183 * Pointer to TX queue structure.
185 * WQE consumer index.
190 static inline uintptr_t *
191 tx_mlx5_wqe(struct txq *txq, uint16_t ci)
193 ci &= ((1 << txq->wqe_n) - 1);
194 return (uintptr_t *)((uintptr_t)txq->wqes + ci * MLX5_WQE_SIZE);
198 * Return the size of tailroom of WQ.
201 * Pointer to TX queue structure.
203 * Pointer to tail of WQ.
209 tx_mlx5_wq_tailroom(struct txq *txq, void *addr)
212 tailroom = (uintptr_t)(txq->wqes) +
213 (1 << txq->wqe_n) * MLX5_WQE_SIZE -
219 * Copy data to tailroom of circular queue.
222 * Pointer to destination.
226 * Number of bytes to copy.
228 * Pointer to head of queue.
230 * Size of tailroom from dst.
233 * Pointer after copied data.
236 mlx5_copy_to_wq(void *dst, const void *src, size_t n,
237 void *base, size_t tailroom)
242 rte_memcpy(dst, src, tailroom);
243 rte_memcpy(base, (void *)((uintptr_t)src + tailroom),
245 ret = (uint8_t *)base + n - tailroom;
247 rte_memcpy(dst, src, n);
248 ret = (n == tailroom) ? base : (uint8_t *)dst + n;
254 * Manage TX completions.
256 * When sending a burst, mlx5_tx_burst() posts several WRs.
259 * Pointer to TX queue structure.
262 txq_complete(struct txq *txq)
264 const unsigned int elts_n = 1 << txq->elts_n;
265 const unsigned int cqe_n = 1 << txq->cqe_n;
266 const unsigned int cqe_cnt = cqe_n - 1;
267 uint16_t elts_free = txq->elts_tail;
269 uint16_t cq_ci = txq->cq_ci;
270 volatile struct mlx5_cqe *cqe = NULL;
271 volatile struct mlx5_wqe_ctrl *ctrl;
274 volatile struct mlx5_cqe *tmp;
276 tmp = &(*txq->cqes)[cq_ci & cqe_cnt];
277 if (check_cqe(tmp, cqe_n, cq_ci))
281 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
282 if (!check_cqe_seen(cqe))
283 ERROR("unexpected compressed CQE, TX stopped");
286 if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
287 (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
288 if (!check_cqe_seen(cqe))
289 ERROR("unexpected error CQE, TX stopped");
295 if (unlikely(cqe == NULL))
297 txq->wqe_pi = ntohs(cqe->wqe_counter);
298 ctrl = (volatile struct mlx5_wqe_ctrl *)
299 tx_mlx5_wqe(txq, txq->wqe_pi);
300 elts_tail = ctrl->ctrl3;
301 assert(elts_tail < (1 << txq->wqe_n));
303 while (elts_free != elts_tail) {
304 struct rte_mbuf *elt = (*txq->elts)[elts_free];
305 unsigned int elts_free_next =
306 (elts_free + 1) & (elts_n - 1);
307 struct rte_mbuf *elt_next = (*txq->elts)[elts_free_next];
311 memset(&(*txq->elts)[elts_free],
313 sizeof((*txq->elts)[elts_free]));
315 RTE_MBUF_PREFETCH_TO_FREE(elt_next);
316 /* Only one segment needs to be freed. */
317 rte_pktmbuf_free_seg(elt);
318 elts_free = elts_free_next;
321 txq->elts_tail = elts_tail;
322 /* Update the consumer index. */
324 *txq->cq_db = htonl(cq_ci);
328 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
329 * the cloned mbuf is allocated is returned instead.
335 * Memory pool where data is located for given mbuf.
337 static struct rte_mempool *
338 txq_mb2mp(struct rte_mbuf *buf)
340 if (unlikely(RTE_MBUF_INDIRECT(buf)))
341 return rte_mbuf_from_indirect(buf)->pool;
346 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
347 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
348 * remove an entry first.
351 * Pointer to TX queue structure.
353 * Memory Pool for which a Memory Region lkey must be returned.
356 * mr->lkey on success, (uint32_t)-1 on failure.
358 static inline uint32_t
359 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
362 uint32_t lkey = (uint32_t)-1;
364 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
365 if (unlikely(txq->mp2mr[i].mp == NULL)) {
366 /* Unknown MP, add a new MR for it. */
369 if (txq->mp2mr[i].mp == mp) {
370 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
371 assert(htonl(txq->mp2mr[i].mr->lkey) ==
373 lkey = txq->mp2mr[i].lkey;
377 if (unlikely(lkey == (uint32_t)-1))
378 lkey = txq_mp2mr_reg(txq, mp, i);
383 * Ring TX queue doorbell.
386 * Pointer to TX queue structure.
388 * Pointer to the last WQE posted in the NIC.
391 mlx5_tx_dbrec(struct txq *txq, volatile struct mlx5_wqe *wqe)
393 uint64_t *dst = (uint64_t *)((uintptr_t)txq->bf_reg);
394 volatile uint64_t *src = ((volatile uint64_t *)wqe);
397 *txq->qp_db = htonl(txq->wqe_ci);
398 /* Ensure ordering between DB record and BF copy. */
404 * DPDK callback to check the status of a tx descriptor.
409 * The index of the descriptor in the ring.
412 * The status of the tx descriptor.
415 mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset)
417 struct txq *txq = tx_queue;
418 const unsigned int elts_n = 1 << txq->elts_n;
419 const unsigned int elts_cnt = elts_n - 1;
423 used = (txq->elts_head - txq->elts_tail) & elts_cnt;
425 return RTE_ETH_TX_DESC_FULL;
426 return RTE_ETH_TX_DESC_DONE;
430 * DPDK callback to check the status of a rx descriptor.
435 * The index of the descriptor in the ring.
438 * The status of the tx descriptor.
441 mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset)
443 struct rxq *rxq = rx_queue;
444 struct rxq_zip *zip = &rxq->zip;
445 volatile struct mlx5_cqe *cqe;
446 const unsigned int cqe_n = (1 << rxq->cqe_n);
447 const unsigned int cqe_cnt = cqe_n - 1;
451 /* if we are processing a compressed cqe */
453 used = zip->cqe_cnt - zip->ca;
459 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
460 while (check_cqe(cqe, cqe_n, cq_ci) == 0) {
464 op_own = cqe->op_own;
465 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED)
466 n = ntohl(cqe->byte_cnt);
471 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
473 used = RTE_MIN(used, (1U << rxq->elts_n) - 1);
475 return RTE_ETH_RX_DESC_DONE;
476 return RTE_ETH_RX_DESC_AVAIL;
480 * DPDK callback for TX.
483 * Generic pointer to TX queue structure.
485 * Packets to transmit.
487 * Number of packets in array.
490 * Number of packets successfully transmitted (<= pkts_n).
493 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
495 struct txq *txq = (struct txq *)dpdk_txq;
496 uint16_t elts_head = txq->elts_head;
497 const unsigned int elts_n = 1 << txq->elts_n;
502 unsigned int max_inline = txq->max_inline;
503 const unsigned int inline_en = !!max_inline && txq->inline_en;
506 volatile struct mlx5_wqe_v *wqe = NULL;
507 volatile struct mlx5_wqe_ctrl *last_wqe = NULL;
508 unsigned int segs_n = 0;
509 struct rte_mbuf *buf = NULL;
512 if (unlikely(!pkts_n))
514 /* Prefetch first packet cacheline. */
515 rte_prefetch0(*pkts);
516 /* Start processing. */
518 max = (elts_n - (elts_head - txq->elts_tail));
521 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
522 if (unlikely(!max_wqe))
525 volatile rte_v128u32_t *dseg = NULL;
528 unsigned int sg = 0; /* counter of additional segs attached. */
531 uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE + 2;
532 uint16_t tso_header_sz = 0;
534 uint8_t cs_flags = 0;
536 #ifdef MLX5_PMD_SOFT_COUNTERS
537 uint32_t total_length = 0;
542 segs_n = buf->nb_segs;
544 * Make sure there is enough room to store this packet and
545 * that one ring entry remains unused.
548 if (max < segs_n + 1)
552 if (unlikely(--max_wqe == 0))
554 wqe = (volatile struct mlx5_wqe_v *)
555 tx_mlx5_wqe(txq, txq->wqe_ci);
556 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
558 rte_prefetch0(*(pkts + 1));
559 addr = rte_pktmbuf_mtod(buf, uintptr_t);
560 length = DATA_LEN(buf);
561 ehdr = (((uint8_t *)addr)[1] << 8) |
562 ((uint8_t *)addr)[0];
563 #ifdef MLX5_PMD_SOFT_COUNTERS
564 total_length = length;
566 if (length < (MLX5_WQE_DWORD_SIZE + 2))
568 /* Update element. */
569 (*txq->elts)[elts_head] = buf;
570 /* Prefetch next buffer data. */
573 rte_pktmbuf_mtod(*(pkts + 1), volatile void *));
574 /* Should we enable HW CKSUM offload */
576 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
577 const uint64_t is_tunneled = buf->ol_flags &
579 PKT_TX_TUNNEL_VXLAN);
581 if (is_tunneled && txq->tunnel_en) {
582 cs_flags = MLX5_ETH_WQE_L3_INNER_CSUM |
583 MLX5_ETH_WQE_L4_INNER_CSUM;
584 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
585 cs_flags |= MLX5_ETH_WQE_L3_CSUM;
587 cs_flags = MLX5_ETH_WQE_L3_CSUM |
588 MLX5_ETH_WQE_L4_CSUM;
591 raw = ((uint8_t *)(uintptr_t)wqe) + 2 * MLX5_WQE_DWORD_SIZE;
592 /* Replace the Ethernet type by the VLAN if necessary. */
593 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
594 uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
595 unsigned int len = 2 * ETHER_ADDR_LEN - 2;
599 /* Copy Destination and source mac address. */
600 memcpy((uint8_t *)raw, ((uint8_t *)addr), len);
602 memcpy((uint8_t *)raw + len, &vlan, sizeof(vlan));
603 /* Copy missing two bytes to end the DSeg. */
604 memcpy((uint8_t *)raw + len + sizeof(vlan),
605 ((uint8_t *)addr) + len, 2);
609 memcpy((uint8_t *)raw, ((uint8_t *)addr) + 2,
610 MLX5_WQE_DWORD_SIZE);
611 length -= pkt_inline_sz;
612 addr += pkt_inline_sz;
615 tso = buf->ol_flags & PKT_TX_TCP_SEG;
617 uintptr_t end = (uintptr_t)
618 (((uintptr_t)txq->wqes) +
622 uint8_t vlan_sz = (buf->ol_flags &
623 PKT_TX_VLAN_PKT) ? 4 : 0;
624 const uint64_t is_tunneled =
627 PKT_TX_TUNNEL_VXLAN);
629 tso_header_sz = buf->l2_len + vlan_sz +
630 buf->l3_len + buf->l4_len;
632 if (is_tunneled && txq->tunnel_en) {
633 tso_header_sz += buf->outer_l2_len +
635 cs_flags |= MLX5_ETH_WQE_L4_INNER_CSUM;
637 cs_flags |= MLX5_ETH_WQE_L4_CSUM;
639 if (unlikely(tso_header_sz >
640 MLX5_MAX_TSO_HEADER))
642 copy_b = tso_header_sz - pkt_inline_sz;
643 /* First seg must contain all headers. */
644 assert(copy_b <= length);
645 raw += MLX5_WQE_DWORD_SIZE;
647 ((end - (uintptr_t)raw) > copy_b)) {
648 uint16_t n = (MLX5_WQE_DS(copy_b) -
651 if (unlikely(max_wqe < n))
654 rte_memcpy((void *)raw,
655 (void *)addr, copy_b);
658 pkt_inline_sz += copy_b;
660 * Another DWORD will be added
661 * in the inline part.
663 raw += MLX5_WQE_DS(copy_b) *
664 MLX5_WQE_DWORD_SIZE -
668 wqe->ctrl = (rte_v128u32_t){
669 htonl(txq->wqe_ci << 8),
670 htonl(txq->qp_num_8s | 1),
681 /* Inline if enough room. */
682 if (inline_en || tso) {
683 uintptr_t end = (uintptr_t)
684 (((uintptr_t)txq->wqes) +
685 (1 << txq->wqe_n) * MLX5_WQE_SIZE);
686 unsigned int inline_room = max_inline *
687 RTE_CACHE_LINE_SIZE -
689 uintptr_t addr_end = (addr + inline_room) &
690 ~(RTE_CACHE_LINE_SIZE - 1);
691 unsigned int copy_b = (addr_end > addr) ?
692 RTE_MIN((addr_end - addr), length) :
695 raw += MLX5_WQE_DWORD_SIZE;
696 if (copy_b && ((end - (uintptr_t)raw) > copy_b)) {
698 * One Dseg remains in the current WQE. To
699 * keep the computation positive, it is
700 * removed after the bytes to Dseg conversion.
702 uint16_t n = (MLX5_WQE_DS(copy_b) - 1 + 3) / 4;
704 if (unlikely(max_wqe < n))
709 htonl(copy_b | MLX5_INLINE_SEG);
712 MLX5_WQE_DS(tso_header_sz) *
714 rte_memcpy((void *)raw,
715 (void *)&inl, sizeof(inl));
717 pkt_inline_sz += sizeof(inl);
719 rte_memcpy((void *)raw, (void *)addr, copy_b);
722 pkt_inline_sz += copy_b;
725 * 2 DWORDs consumed by the WQE header + ETH segment +
726 * the size of the inline part of the packet.
728 ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
730 if (ds % (MLX5_WQE_SIZE /
731 MLX5_WQE_DWORD_SIZE) == 0) {
732 if (unlikely(--max_wqe == 0))
734 dseg = (volatile rte_v128u32_t *)
735 tx_mlx5_wqe(txq, txq->wqe_ci +
738 dseg = (volatile rte_v128u32_t *)
740 (ds * MLX5_WQE_DWORD_SIZE));
743 } else if (!segs_n) {
746 /* dseg will be advance as part of next_seg */
747 dseg = (volatile rte_v128u32_t *)
749 ((ds - 1) * MLX5_WQE_DWORD_SIZE));
754 * No inline has been done in the packet, only the
755 * Ethernet Header as been stored.
757 dseg = (volatile rte_v128u32_t *)
758 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
761 /* Add the remaining packet as a simple ds. */
762 naddr = htonll(addr);
763 *dseg = (rte_v128u32_t){
765 txq_mp2mr(txq, txq_mb2mp(buf)),
778 * Spill on next WQE when the current one does not have
779 * enough room left. Size of WQE must a be a multiple
780 * of data segment size.
782 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
783 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
784 if (unlikely(--max_wqe == 0))
786 dseg = (volatile rte_v128u32_t *)
787 tx_mlx5_wqe(txq, txq->wqe_ci + ds / 4);
788 rte_prefetch0(tx_mlx5_wqe(txq,
789 txq->wqe_ci + ds / 4 + 1));
796 length = DATA_LEN(buf);
797 #ifdef MLX5_PMD_SOFT_COUNTERS
798 total_length += length;
800 /* Store segment information. */
801 naddr = htonll(rte_pktmbuf_mtod(buf, uintptr_t));
802 *dseg = (rte_v128u32_t){
804 txq_mp2mr(txq, txq_mb2mp(buf)),
808 elts_head = (elts_head + 1) & (elts_n - 1);
809 (*txq->elts)[elts_head] = buf;
811 /* Advance counter only if all segs are successfully posted. */
817 elts_head = (elts_head + 1) & (elts_n - 1);
820 /* Initialize known and common part of the WQE structure. */
822 wqe->ctrl = (rte_v128u32_t){
823 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_TSO),
824 htonl(txq->qp_num_8s | ds),
828 wqe->eseg = (rte_v128u32_t){
830 cs_flags | (htons(buf->tso_segsz) << 16),
832 (ehdr << 16) | htons(tso_header_sz),
835 wqe->ctrl = (rte_v128u32_t){
836 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
837 htonl(txq->qp_num_8s | ds),
841 wqe->eseg = (rte_v128u32_t){
845 (ehdr << 16) | htons(pkt_inline_sz),
849 txq->wqe_ci += (ds + 3) / 4;
850 /* Save the last successful WQE for completion request */
851 last_wqe = (volatile struct mlx5_wqe_ctrl *)wqe;
852 #ifdef MLX5_PMD_SOFT_COUNTERS
853 /* Increment sent bytes counter. */
854 txq->stats.obytes += total_length;
856 } while (i < pkts_n);
857 /* Take a shortcut if nothing must be sent. */
858 if (unlikely((i + k) == 0))
860 txq->elts_head = (txq->elts_head + i + j) & (elts_n - 1);
861 /* Check whether completion threshold has been reached. */
862 comp = txq->elts_comp + i + j + k;
863 if (comp >= MLX5_TX_COMP_THRESH) {
864 /* Request completion on last WQE. */
865 last_wqe->ctrl2 = htonl(8);
866 /* Save elts_head in unused "immediate" field of WQE. */
867 last_wqe->ctrl3 = txq->elts_head;
870 txq->elts_comp = comp;
872 #ifdef MLX5_PMD_SOFT_COUNTERS
873 /* Increment sent packets counter. */
874 txq->stats.opackets += i;
876 /* Ring QP doorbell. */
877 mlx5_tx_dbrec(txq, (volatile struct mlx5_wqe *)last_wqe);
882 * Open a MPW session.
885 * Pointer to TX queue structure.
887 * Pointer to MPW session structure.
892 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
894 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
895 volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
896 (volatile struct mlx5_wqe_data_seg (*)[])
897 tx_mlx5_wqe(txq, idx + 1);
899 mpw->state = MLX5_MPW_STATE_OPENED;
903 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
904 mpw->wqe->eseg.mss = htons(length);
905 mpw->wqe->eseg.inline_hdr_sz = 0;
906 mpw->wqe->eseg.rsvd0 = 0;
907 mpw->wqe->eseg.rsvd1 = 0;
908 mpw->wqe->eseg.rsvd2 = 0;
909 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
910 (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
911 mpw->wqe->ctrl[2] = 0;
912 mpw->wqe->ctrl[3] = 0;
913 mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
914 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
915 mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
916 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
917 mpw->data.dseg[2] = &(*dseg)[0];
918 mpw->data.dseg[3] = &(*dseg)[1];
919 mpw->data.dseg[4] = &(*dseg)[2];
923 * Close a MPW session.
926 * Pointer to TX queue structure.
928 * Pointer to MPW session structure.
931 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
933 unsigned int num = mpw->pkts_n;
936 * Store size in multiple of 16 bytes. Control and Ethernet segments
939 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
940 mpw->state = MLX5_MPW_STATE_CLOSED;
945 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
946 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
950 * DPDK callback for TX with MPW support.
953 * Generic pointer to TX queue structure.
955 * Packets to transmit.
957 * Number of packets in array.
960 * Number of packets successfully transmitted (<= pkts_n).
963 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
965 struct txq *txq = (struct txq *)dpdk_txq;
966 uint16_t elts_head = txq->elts_head;
967 const unsigned int elts_n = 1 << txq->elts_n;
973 struct mlx5_mpw mpw = {
974 .state = MLX5_MPW_STATE_CLOSED,
977 if (unlikely(!pkts_n))
979 /* Prefetch first packet cacheline. */
980 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
981 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
982 /* Start processing. */
984 max = (elts_n - (elts_head - txq->elts_tail));
987 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
988 if (unlikely(!max_wqe))
991 struct rte_mbuf *buf = *(pkts++);
992 unsigned int elts_head_next;
994 unsigned int segs_n = buf->nb_segs;
995 uint32_t cs_flags = 0;
998 * Make sure there is enough room to store this packet and
999 * that one ring entry remains unused.
1002 if (max < segs_n + 1)
1004 /* Do not bother with large packets MPW cannot handle. */
1005 if (segs_n > MLX5_MPW_DSEG_MAX)
1009 /* Should we enable HW CKSUM offload */
1011 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1012 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1013 /* Retrieve packet information. */
1014 length = PKT_LEN(buf);
1016 /* Start new session if packet differs. */
1017 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
1018 ((mpw.len != length) ||
1020 (mpw.wqe->eseg.cs_flags != cs_flags)))
1021 mlx5_mpw_close(txq, &mpw);
1022 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1024 * Multi-Packet WQE consumes at most two WQE.
1025 * mlx5_mpw_new() expects to be able to use such
1028 if (unlikely(max_wqe < 2))
1031 mlx5_mpw_new(txq, &mpw, length);
1032 mpw.wqe->eseg.cs_flags = cs_flags;
1034 /* Multi-segment packets must be alone in their MPW. */
1035 assert((segs_n == 1) || (mpw.pkts_n == 0));
1036 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1040 volatile struct mlx5_wqe_data_seg *dseg;
1043 elts_head_next = (elts_head + 1) & (elts_n - 1);
1045 (*txq->elts)[elts_head] = buf;
1046 dseg = mpw.data.dseg[mpw.pkts_n];
1047 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1048 *dseg = (struct mlx5_wqe_data_seg){
1049 .byte_count = htonl(DATA_LEN(buf)),
1050 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1051 .addr = htonll(addr),
1053 elts_head = elts_head_next;
1054 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1055 length += DATA_LEN(buf);
1061 assert(length == mpw.len);
1062 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1063 mlx5_mpw_close(txq, &mpw);
1064 elts_head = elts_head_next;
1065 #ifdef MLX5_PMD_SOFT_COUNTERS
1066 /* Increment sent bytes counter. */
1067 txq->stats.obytes += length;
1071 /* Take a shortcut if nothing must be sent. */
1072 if (unlikely(i == 0))
1074 /* Check whether completion threshold has been reached. */
1075 /* "j" includes both packets and segments. */
1076 comp = txq->elts_comp + j;
1077 if (comp >= MLX5_TX_COMP_THRESH) {
1078 volatile struct mlx5_wqe *wqe = mpw.wqe;
1080 /* Request completion on last WQE. */
1081 wqe->ctrl[2] = htonl(8);
1082 /* Save elts_head in unused "immediate" field of WQE. */
1083 wqe->ctrl[3] = elts_head;
1086 txq->elts_comp = comp;
1088 #ifdef MLX5_PMD_SOFT_COUNTERS
1089 /* Increment sent packets counter. */
1090 txq->stats.opackets += i;
1092 /* Ring QP doorbell. */
1093 if (mpw.state == MLX5_MPW_STATE_OPENED)
1094 mlx5_mpw_close(txq, &mpw);
1095 mlx5_tx_dbrec(txq, mpw.wqe);
1096 txq->elts_head = elts_head;
1101 * Open a MPW inline session.
1104 * Pointer to TX queue structure.
1106 * Pointer to MPW session structure.
1111 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
1113 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
1114 struct mlx5_wqe_inl_small *inl;
1116 mpw->state = MLX5_MPW_INL_STATE_OPENED;
1120 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
1121 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
1122 (txq->wqe_ci << 8) |
1124 mpw->wqe->ctrl[2] = 0;
1125 mpw->wqe->ctrl[3] = 0;
1126 mpw->wqe->eseg.mss = htons(length);
1127 mpw->wqe->eseg.inline_hdr_sz = 0;
1128 mpw->wqe->eseg.cs_flags = 0;
1129 mpw->wqe->eseg.rsvd0 = 0;
1130 mpw->wqe->eseg.rsvd1 = 0;
1131 mpw->wqe->eseg.rsvd2 = 0;
1132 inl = (struct mlx5_wqe_inl_small *)
1133 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
1134 mpw->data.raw = (uint8_t *)&inl->raw;
1138 * Close a MPW inline session.
1141 * Pointer to TX queue structure.
1143 * Pointer to MPW session structure.
1146 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
1149 struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
1150 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
1152 size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
1154 * Store size in multiple of 16 bytes. Control and Ethernet segments
1157 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
1158 mpw->state = MLX5_MPW_STATE_CLOSED;
1159 inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
1160 txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
1164 * DPDK callback for TX with MPW inline support.
1167 * Generic pointer to TX queue structure.
1169 * Packets to transmit.
1171 * Number of packets in array.
1174 * Number of packets successfully transmitted (<= pkts_n).
1177 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
1180 struct txq *txq = (struct txq *)dpdk_txq;
1181 uint16_t elts_head = txq->elts_head;
1182 const unsigned int elts_n = 1 << txq->elts_n;
1188 unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
1189 struct mlx5_mpw mpw = {
1190 .state = MLX5_MPW_STATE_CLOSED,
1193 * Compute the maximum number of WQE which can be consumed by inline
1196 * - 1 control segment,
1197 * - 1 Ethernet segment,
1198 * - N Dseg from the inline request.
1200 const unsigned int wqe_inl_n =
1201 ((2 * MLX5_WQE_DWORD_SIZE +
1202 txq->max_inline * RTE_CACHE_LINE_SIZE) +
1203 RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
1205 if (unlikely(!pkts_n))
1207 /* Prefetch first packet cacheline. */
1208 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
1209 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
1210 /* Start processing. */
1212 max = (elts_n - (elts_head - txq->elts_tail));
1216 struct rte_mbuf *buf = *(pkts++);
1217 unsigned int elts_head_next;
1220 unsigned int segs_n = buf->nb_segs;
1221 uint32_t cs_flags = 0;
1224 * Make sure there is enough room to store this packet and
1225 * that one ring entry remains unused.
1228 if (max < segs_n + 1)
1230 /* Do not bother with large packets MPW cannot handle. */
1231 if (segs_n > MLX5_MPW_DSEG_MAX)
1236 * Compute max_wqe in case less WQE were consumed in previous
1239 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1240 /* Should we enable HW CKSUM offload */
1242 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1243 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1244 /* Retrieve packet information. */
1245 length = PKT_LEN(buf);
1246 /* Start new session if packet differs. */
1247 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1248 if ((mpw.len != length) ||
1250 (mpw.wqe->eseg.cs_flags != cs_flags))
1251 mlx5_mpw_close(txq, &mpw);
1252 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
1253 if ((mpw.len != length) ||
1255 (length > inline_room) ||
1256 (mpw.wqe->eseg.cs_flags != cs_flags)) {
1257 mlx5_mpw_inline_close(txq, &mpw);
1259 txq->max_inline * RTE_CACHE_LINE_SIZE;
1262 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1263 if ((segs_n != 1) ||
1264 (length > inline_room)) {
1266 * Multi-Packet WQE consumes at most two WQE.
1267 * mlx5_mpw_new() expects to be able to use
1270 if (unlikely(max_wqe < 2))
1273 mlx5_mpw_new(txq, &mpw, length);
1274 mpw.wqe->eseg.cs_flags = cs_flags;
1276 if (unlikely(max_wqe < wqe_inl_n))
1278 max_wqe -= wqe_inl_n;
1279 mlx5_mpw_inline_new(txq, &mpw, length);
1280 mpw.wqe->eseg.cs_flags = cs_flags;
1283 /* Multi-segment packets must be alone in their MPW. */
1284 assert((segs_n == 1) || (mpw.pkts_n == 0));
1285 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1286 assert(inline_room ==
1287 txq->max_inline * RTE_CACHE_LINE_SIZE);
1288 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1292 volatile struct mlx5_wqe_data_seg *dseg;
1295 (elts_head + 1) & (elts_n - 1);
1297 (*txq->elts)[elts_head] = buf;
1298 dseg = mpw.data.dseg[mpw.pkts_n];
1299 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1300 *dseg = (struct mlx5_wqe_data_seg){
1301 .byte_count = htonl(DATA_LEN(buf)),
1302 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1303 .addr = htonll(addr),
1305 elts_head = elts_head_next;
1306 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1307 length += DATA_LEN(buf);
1313 assert(length == mpw.len);
1314 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1315 mlx5_mpw_close(txq, &mpw);
1319 assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1320 assert(length <= inline_room);
1321 assert(length == DATA_LEN(buf));
1322 elts_head_next = (elts_head + 1) & (elts_n - 1);
1323 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1324 (*txq->elts)[elts_head] = buf;
1325 /* Maximum number of bytes before wrapping. */
1326 max = ((((uintptr_t)(txq->wqes)) +
1329 (uintptr_t)mpw.data.raw);
1331 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1334 mpw.data.raw = (volatile void *)txq->wqes;
1335 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1336 (void *)(addr + max),
1338 mpw.data.raw += length - max;
1340 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1346 (volatile void *)txq->wqes;
1348 mpw.data.raw += length;
1351 mpw.total_len += length;
1353 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1354 mlx5_mpw_inline_close(txq, &mpw);
1356 txq->max_inline * RTE_CACHE_LINE_SIZE;
1358 inline_room -= length;
1361 elts_head = elts_head_next;
1362 #ifdef MLX5_PMD_SOFT_COUNTERS
1363 /* Increment sent bytes counter. */
1364 txq->stats.obytes += length;
1368 /* Take a shortcut if nothing must be sent. */
1369 if (unlikely(i == 0))
1371 /* Check whether completion threshold has been reached. */
1372 /* "j" includes both packets and segments. */
1373 comp = txq->elts_comp + j;
1374 if (comp >= MLX5_TX_COMP_THRESH) {
1375 volatile struct mlx5_wqe *wqe = mpw.wqe;
1377 /* Request completion on last WQE. */
1378 wqe->ctrl[2] = htonl(8);
1379 /* Save elts_head in unused "immediate" field of WQE. */
1380 wqe->ctrl[3] = elts_head;
1383 txq->elts_comp = comp;
1385 #ifdef MLX5_PMD_SOFT_COUNTERS
1386 /* Increment sent packets counter. */
1387 txq->stats.opackets += i;
1389 /* Ring QP doorbell. */
1390 if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1391 mlx5_mpw_inline_close(txq, &mpw);
1392 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1393 mlx5_mpw_close(txq, &mpw);
1394 mlx5_tx_dbrec(txq, mpw.wqe);
1395 txq->elts_head = elts_head;
1400 * Open an Enhanced MPW session.
1403 * Pointer to TX queue structure.
1405 * Pointer to MPW session structure.
1410 mlx5_empw_new(struct txq *txq, struct mlx5_mpw *mpw, int padding)
1412 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
1414 mpw->state = MLX5_MPW_ENHANCED_STATE_OPENED;
1416 mpw->total_len = sizeof(struct mlx5_wqe);
1417 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
1418 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_ENHANCED_MPSW << 24) |
1419 (txq->wqe_ci << 8) |
1420 MLX5_OPCODE_ENHANCED_MPSW);
1421 mpw->wqe->ctrl[2] = 0;
1422 mpw->wqe->ctrl[3] = 0;
1423 memset((void *)(uintptr_t)&mpw->wqe->eseg, 0, MLX5_WQE_DWORD_SIZE);
1424 if (unlikely(padding)) {
1425 uintptr_t addr = (uintptr_t)(mpw->wqe + 1);
1427 /* Pad the first 2 DWORDs with zero-length inline header. */
1428 *(volatile uint32_t *)addr = htonl(MLX5_INLINE_SEG);
1429 *(volatile uint32_t *)(addr + MLX5_WQE_DWORD_SIZE) =
1430 htonl(MLX5_INLINE_SEG);
1431 mpw->total_len += 2 * MLX5_WQE_DWORD_SIZE;
1432 /* Start from the next WQEBB. */
1433 mpw->data.raw = (volatile void *)(tx_mlx5_wqe(txq, idx + 1));
1435 mpw->data.raw = (volatile void *)(mpw->wqe + 1);
1440 * Close an Enhanced MPW session.
1443 * Pointer to TX queue structure.
1445 * Pointer to MPW session structure.
1448 * Number of consumed WQEs.
1450 static inline uint16_t
1451 mlx5_empw_close(struct txq *txq, struct mlx5_mpw *mpw)
1455 /* Store size in multiple of 16 bytes. Control and Ethernet segments
1458 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(mpw->total_len));
1459 mpw->state = MLX5_MPW_STATE_CLOSED;
1460 ret = (mpw->total_len + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
1466 * DPDK callback for TX with Enhanced MPW support.
1469 * Generic pointer to TX queue structure.
1471 * Packets to transmit.
1473 * Number of packets in array.
1476 * Number of packets successfully transmitted (<= pkts_n).
1479 mlx5_tx_burst_empw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1481 struct txq *txq = (struct txq *)dpdk_txq;
1482 uint16_t elts_head = txq->elts_head;
1483 const unsigned int elts_n = 1 << txq->elts_n;
1486 unsigned int max_elts;
1488 unsigned int max_inline = txq->max_inline * RTE_CACHE_LINE_SIZE;
1489 unsigned int mpw_room = 0;
1490 unsigned int inl_pad = 0;
1492 struct mlx5_mpw mpw = {
1493 .state = MLX5_MPW_STATE_CLOSED,
1496 if (unlikely(!pkts_n))
1498 /* Start processing. */
1500 max_elts = (elts_n - (elts_head - txq->elts_tail));
1501 if (max_elts > elts_n)
1503 /* A CQE slot must always be available. */
1504 assert((1u << txq->cqe_n) - (txq->cq_pi - txq->cq_ci));
1505 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1506 if (unlikely(!max_wqe))
1509 struct rte_mbuf *buf = *(pkts++);
1510 unsigned int elts_head_next;
1514 unsigned int do_inline = 0; /* Whether inline is possible. */
1516 unsigned int segs_n = buf->nb_segs;
1517 uint32_t cs_flags = 0;
1520 * Make sure there is enough room to store this packet and
1521 * that one ring entry remains unused.
1524 if (max_elts - j < segs_n + 1)
1526 /* Do not bother with large packets MPW cannot handle. */
1527 if (segs_n > MLX5_MPW_DSEG_MAX)
1529 /* Should we enable HW CKSUM offload. */
1531 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1532 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1533 /* Retrieve packet information. */
1534 length = PKT_LEN(buf);
1535 /* Start new session if:
1536 * - multi-segment packet
1537 * - no space left even for a dseg
1538 * - next packet can be inlined with a new WQE
1540 * It can't be MLX5_MPW_STATE_OPENED as always have a single
1543 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED) {
1544 if ((segs_n != 1) ||
1545 (inl_pad + sizeof(struct mlx5_wqe_data_seg) >
1547 (length <= txq->inline_max_packet_sz &&
1548 inl_pad + sizeof(inl_hdr) + length >
1550 (mpw.wqe->eseg.cs_flags != cs_flags))
1551 max_wqe -= mlx5_empw_close(txq, &mpw);
1553 if (unlikely(mpw.state == MLX5_MPW_STATE_CLOSED)) {
1554 if (unlikely(segs_n != 1)) {
1555 /* Fall back to legacy MPW.
1556 * A MPW session consumes 2 WQEs at most to
1557 * include MLX5_MPW_DSEG_MAX pointers.
1559 if (unlikely(max_wqe < 2))
1561 mlx5_mpw_new(txq, &mpw, length);
1563 /* In Enhanced MPW, inline as much as the budget
1564 * is allowed. The remaining space is to be
1565 * filled with dsegs. If the title WQEBB isn't
1566 * padded, it will have 2 dsegs there.
1568 mpw_room = RTE_MIN(MLX5_WQE_SIZE_MAX,
1569 (max_inline ? max_inline :
1570 pkts_n * MLX5_WQE_DWORD_SIZE) +
1572 if (unlikely(max_wqe * MLX5_WQE_SIZE <
1575 /* Don't pad the title WQEBB to not waste WQ. */
1576 mlx5_empw_new(txq, &mpw, 0);
1577 mpw_room -= mpw.total_len;
1580 length <= txq->inline_max_packet_sz &&
1581 sizeof(inl_hdr) + length <= mpw_room &&
1584 mpw.wqe->eseg.cs_flags = cs_flags;
1586 /* Evaluate whether the next packet can be inlined.
1587 * Inlininig is possible when:
1588 * - length is less than configured value
1589 * - length fits for remaining space
1590 * - not required to fill the title WQEBB with dsegs
1593 length <= txq->inline_max_packet_sz &&
1594 inl_pad + sizeof(inl_hdr) + length <=
1596 (!txq->mpw_hdr_dseg ||
1597 mpw.total_len >= MLX5_WQE_SIZE);
1599 /* Multi-segment packets must be alone in their MPW. */
1600 assert((segs_n == 1) || (mpw.pkts_n == 0));
1601 if (unlikely(mpw.state == MLX5_MPW_STATE_OPENED)) {
1602 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1606 volatile struct mlx5_wqe_data_seg *dseg;
1609 (elts_head + 1) & (elts_n - 1);
1611 (*txq->elts)[elts_head] = buf;
1612 dseg = mpw.data.dseg[mpw.pkts_n];
1613 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1614 *dseg = (struct mlx5_wqe_data_seg){
1615 .byte_count = htonl(DATA_LEN(buf)),
1616 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1617 .addr = htonll(addr),
1619 elts_head = elts_head_next;
1620 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1621 length += DATA_LEN(buf);
1627 /* A multi-segmented packet takes one MPW session.
1628 * TODO: Pack more multi-segmented packets if possible.
1630 mlx5_mpw_close(txq, &mpw);
1635 } else if (do_inline) {
1636 /* Inline packet into WQE. */
1639 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1640 assert(length == DATA_LEN(buf));
1641 inl_hdr = htonl(length | MLX5_INLINE_SEG);
1642 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1643 mpw.data.raw = (volatile void *)
1644 ((uintptr_t)mpw.data.raw + inl_pad);
1645 max = tx_mlx5_wq_tailroom(txq,
1646 (void *)(uintptr_t)mpw.data.raw);
1647 /* Copy inline header. */
1648 mpw.data.raw = (volatile void *)
1650 (void *)(uintptr_t)mpw.data.raw,
1653 (void *)(uintptr_t)txq->wqes,
1655 max = tx_mlx5_wq_tailroom(txq,
1656 (void *)(uintptr_t)mpw.data.raw);
1657 /* Copy packet data. */
1658 mpw.data.raw = (volatile void *)
1660 (void *)(uintptr_t)mpw.data.raw,
1663 (void *)(uintptr_t)txq->wqes,
1666 mpw.total_len += (inl_pad + sizeof(inl_hdr) + length);
1667 /* No need to get completion as the entire packet is
1668 * copied to WQ. Free the buf right away.
1670 elts_head_next = elts_head;
1671 rte_pktmbuf_free_seg(buf);
1672 mpw_room -= (inl_pad + sizeof(inl_hdr) + length);
1673 /* Add pad in the next packet if any. */
1674 inl_pad = (((uintptr_t)mpw.data.raw +
1675 (MLX5_WQE_DWORD_SIZE - 1)) &
1676 ~(MLX5_WQE_DWORD_SIZE - 1)) -
1677 (uintptr_t)mpw.data.raw;
1679 /* No inline. Load a dseg of packet pointer. */
1680 volatile rte_v128u32_t *dseg;
1682 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1683 assert((inl_pad + sizeof(*dseg)) <= mpw_room);
1684 assert(length == DATA_LEN(buf));
1685 if (!tx_mlx5_wq_tailroom(txq,
1686 (void *)((uintptr_t)mpw.data.raw
1688 dseg = (volatile void *)txq->wqes;
1690 dseg = (volatile void *)
1691 ((uintptr_t)mpw.data.raw +
1693 elts_head_next = (elts_head + 1) & (elts_n - 1);
1694 (*txq->elts)[elts_head] = buf;
1695 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1696 for (n = 0; n * RTE_CACHE_LINE_SIZE < length; n++)
1697 rte_prefetch2((void *)(addr +
1698 n * RTE_CACHE_LINE_SIZE));
1699 naddr = htonll(addr);
1700 *dseg = (rte_v128u32_t) {
1702 txq_mp2mr(txq, txq_mb2mp(buf)),
1706 mpw.data.raw = (volatile void *)(dseg + 1);
1707 mpw.total_len += (inl_pad + sizeof(*dseg));
1710 mpw_room -= (inl_pad + sizeof(*dseg));
1713 elts_head = elts_head_next;
1714 #ifdef MLX5_PMD_SOFT_COUNTERS
1715 /* Increment sent bytes counter. */
1716 txq->stats.obytes += length;
1719 } while (i < pkts_n);
1720 /* Take a shortcut if nothing must be sent. */
1721 if (unlikely(i == 0))
1723 /* Check whether completion threshold has been reached. */
1724 if (txq->elts_comp + j >= MLX5_TX_COMP_THRESH ||
1725 (uint16_t)(txq->wqe_ci - txq->mpw_comp) >=
1726 (1 << txq->wqe_n) / MLX5_TX_COMP_THRESH_INLINE_DIV) {
1727 volatile struct mlx5_wqe *wqe = mpw.wqe;
1729 /* Request completion on last WQE. */
1730 wqe->ctrl[2] = htonl(8);
1731 /* Save elts_head in unused "immediate" field of WQE. */
1732 wqe->ctrl[3] = elts_head;
1734 txq->mpw_comp = txq->wqe_ci;
1737 txq->elts_comp += j;
1739 #ifdef MLX5_PMD_SOFT_COUNTERS
1740 /* Increment sent packets counter. */
1741 txq->stats.opackets += i;
1743 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED)
1744 mlx5_empw_close(txq, &mpw);
1745 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1746 mlx5_mpw_close(txq, &mpw);
1747 /* Ring QP doorbell. */
1748 mlx5_tx_dbrec(txq, mpw.wqe);
1749 txq->elts_head = elts_head;
1754 * Translate RX completion flags to packet type.
1759 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1762 * Packet type for struct rte_mbuf.
1764 static inline uint32_t
1765 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1768 uint16_t flags = ntohs(cqe->hdr_type_etc);
1770 if (cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) {
1773 MLX5_CQE_RX_IPV4_PACKET,
1774 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
1776 MLX5_CQE_RX_IPV6_PACKET,
1777 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
1778 pkt_type |= ((cqe->pkt_info & MLX5_CQE_RX_OUTER_PACKET) ?
1779 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN :
1780 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1784 MLX5_CQE_L3_HDR_TYPE_IPV6,
1785 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
1787 MLX5_CQE_L3_HDR_TYPE_IPV4,
1788 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1794 * Get size of the next packet for a given CQE. For compressed CQEs, the
1795 * consumer index is updated only once all packets of the current one have
1799 * Pointer to RX queue.
1802 * @param[out] rss_hash
1803 * Packet RSS Hash result.
1806 * Packet size in bytes (0 if there is none), -1 in case of completion
1810 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1811 uint16_t cqe_cnt, uint32_t *rss_hash)
1813 struct rxq_zip *zip = &rxq->zip;
1814 uint16_t cqe_n = cqe_cnt + 1;
1818 /* Process compressed data in the CQE and mini arrays. */
1820 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1821 (volatile struct mlx5_mini_cqe8 (*)[8])
1822 (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt]);
1824 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1825 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1826 if ((++zip->ai & 7) == 0) {
1827 /* Invalidate consumed CQEs */
1830 while (idx != end) {
1831 (*rxq->cqes)[idx & cqe_cnt].op_own =
1832 MLX5_CQE_INVALIDATE;
1836 * Increment consumer index to skip the number of
1837 * CQEs consumed. Hardware leaves holes in the CQ
1838 * ring for software use.
1843 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1844 /* Invalidate the rest */
1848 while (idx != end) {
1849 (*rxq->cqes)[idx & cqe_cnt].op_own =
1850 MLX5_CQE_INVALIDATE;
1853 rxq->cq_ci = zip->cq_ci;
1856 /* No compressed data, get next CQE and verify if it is compressed. */
1861 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1862 if (unlikely(ret == 1))
1865 op_own = cqe->op_own;
1866 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1867 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1868 (volatile struct mlx5_mini_cqe8 (*)[8])
1869 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1872 /* Fix endianness. */
1873 zip->cqe_cnt = ntohl(cqe->byte_cnt);
1875 * Current mini array position is the one returned by
1878 * If completion comprises several mini arrays, as a
1879 * special case the second one is located 7 CQEs after
1880 * the initial CQE instead of 8 for subsequent ones.
1882 zip->ca = rxq->cq_ci;
1883 zip->na = zip->ca + 7;
1884 /* Compute the next non compressed CQE. */
1886 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1887 /* Get packet size to return. */
1888 len = ntohl((*mc)[0].byte_cnt);
1889 *rss_hash = ntohl((*mc)[0].rx_hash_result);
1891 /* Prefetch all the entries to be invalidated */
1894 while (idx != end) {
1895 rte_prefetch0(&(*rxq->cqes)[(idx) & cqe_cnt]);
1899 len = ntohl(cqe->byte_cnt);
1900 *rss_hash = ntohl(cqe->rx_hash_res);
1902 /* Error while receiving packet. */
1903 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1910 * Translate RX completion flags to offload flags.
1913 * Pointer to RX queue structure.
1918 * Offload flags (ol_flags) for struct rte_mbuf.
1920 static inline uint32_t
1921 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1923 uint32_t ol_flags = 0;
1924 uint16_t flags = ntohs(cqe->hdr_type_etc);
1928 MLX5_CQE_RX_L3_HDR_VALID,
1929 PKT_RX_IP_CKSUM_GOOD) |
1931 MLX5_CQE_RX_L4_HDR_VALID,
1932 PKT_RX_L4_CKSUM_GOOD);
1933 if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1936 MLX5_CQE_RX_L3_HDR_VALID,
1937 PKT_RX_IP_CKSUM_GOOD) |
1939 MLX5_CQE_RX_L4_HDR_VALID,
1940 PKT_RX_L4_CKSUM_GOOD);
1945 * DPDK callback for RX.
1948 * Generic pointer to RX queue structure.
1950 * Array to store received packets.
1952 * Maximum number of packets in array.
1955 * Number of packets successfully received (<= pkts_n).
1958 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1960 struct rxq *rxq = dpdk_rxq;
1961 const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1962 const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1963 const unsigned int sges_n = rxq->sges_n;
1964 struct rte_mbuf *pkt = NULL;
1965 struct rte_mbuf *seg = NULL;
1966 volatile struct mlx5_cqe *cqe =
1967 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1969 unsigned int rq_ci = rxq->rq_ci << sges_n;
1970 int len = 0; /* keep its value across iterations. */
1973 unsigned int idx = rq_ci & wqe_cnt;
1974 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1975 struct rte_mbuf *rep = (*rxq->elts)[idx];
1976 uint32_t rss_hash_res = 0;
1984 rep = rte_mbuf_raw_alloc(rxq->mp);
1985 if (unlikely(rep == NULL)) {
1986 ++rxq->stats.rx_nombuf;
1989 * no buffers before we even started,
1990 * bail out silently.
1994 while (pkt != seg) {
1995 assert(pkt != (*rxq->elts)[idx]);
1999 rte_mbuf_raw_free(pkt);
2005 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
2006 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
2009 rte_mbuf_raw_free(rep);
2012 if (unlikely(len == -1)) {
2013 /* RX error, packet is likely too large. */
2014 rte_mbuf_raw_free(rep);
2015 ++rxq->stats.idropped;
2019 assert(len >= (rxq->crc_present << 2));
2020 /* Update packet information. */
2021 pkt->packet_type = 0;
2023 if (rss_hash_res && rxq->rss_hash) {
2024 pkt->hash.rss = rss_hash_res;
2025 pkt->ol_flags = PKT_RX_RSS_HASH;
2028 MLX5_FLOW_MARK_IS_VALID(cqe->sop_drop_qpn)) {
2029 pkt->ol_flags |= PKT_RX_FDIR;
2030 if (cqe->sop_drop_qpn !=
2031 htonl(MLX5_FLOW_MARK_DEFAULT)) {
2032 uint32_t mark = cqe->sop_drop_qpn;
2034 pkt->ol_flags |= PKT_RX_FDIR_ID;
2036 mlx5_flow_mark_get(mark);
2039 if (rxq->csum | rxq->csum_l2tun) {
2040 pkt->packet_type = rxq_cq_to_pkt_type(cqe);
2041 pkt->ol_flags |= rxq_cq_to_ol_flags(rxq, cqe);
2043 if (rxq->vlan_strip &&
2044 (cqe->hdr_type_etc &
2045 htons(MLX5_CQE_VLAN_STRIPPED))) {
2046 pkt->ol_flags |= PKT_RX_VLAN_PKT |
2047 PKT_RX_VLAN_STRIPPED;
2048 pkt->vlan_tci = ntohs(cqe->vlan_info);
2050 if (rxq->crc_present)
2051 len -= ETHER_CRC_LEN;
2054 DATA_LEN(rep) = DATA_LEN(seg);
2055 PKT_LEN(rep) = PKT_LEN(seg);
2056 SET_DATA_OFF(rep, DATA_OFF(seg));
2057 NB_SEGS(rep) = NB_SEGS(seg);
2058 PORT(rep) = PORT(seg);
2060 (*rxq->elts)[idx] = rep;
2062 * Fill NIC descriptor with the new buffer. The lkey and size
2063 * of the buffers are already known, only the buffer address
2066 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
2067 if (len > DATA_LEN(seg)) {
2068 len -= DATA_LEN(seg);
2073 DATA_LEN(seg) = len;
2074 #ifdef MLX5_PMD_SOFT_COUNTERS
2075 /* Increment bytes counter. */
2076 rxq->stats.ibytes += PKT_LEN(pkt);
2078 /* Return packet. */
2084 /* Align consumer index to the next stride. */
2089 if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
2091 /* Update the consumer index. */
2092 rxq->rq_ci = rq_ci >> sges_n;
2094 *rxq->cq_db = htonl(rxq->cq_ci);
2096 *rxq->rq_db = htonl(rxq->rq_ci);
2097 #ifdef MLX5_PMD_SOFT_COUNTERS
2098 /* Increment packets counter. */
2099 rxq->stats.ipackets += i;
2105 * Dummy DPDK callback for TX.
2107 * This function is used to temporarily replace the real callback during
2108 * unsafe control operations on the queue, or in case of error.
2111 * Generic pointer to TX queue structure.
2113 * Packets to transmit.
2115 * Number of packets in array.
2118 * Number of packets successfully transmitted (<= pkts_n).
2121 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2130 * Dummy DPDK callback for RX.
2132 * This function is used to temporarily replace the real callback during
2133 * unsafe control operations on the queue, or in case of error.
2136 * Generic pointer to RX queue structure.
2138 * Array to store received packets.
2140 * Maximum number of packets in array.
2143 * Number of packets successfully received (<= pkts_n).
2146 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2155 * DPDK callback for rx queue interrupt enable.
2158 * Pointer to Ethernet device structure.
2159 * @param rx_queue_id
2163 * 0 on success, negative on failure.
2166 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
2168 #ifdef HAVE_UPDATE_CQ_CI
2169 struct priv *priv = mlx5_get_priv(dev);
2170 struct rxq *rxq = (*priv->rxqs)[rx_queue_id];
2171 struct rxq_ctrl *rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
2172 struct ibv_cq *cq = rxq_ctrl->cq;
2173 uint16_t ci = rxq->cq_ci;
2176 ibv_mlx5_exp_update_cq_ci(cq, ci);
2177 ret = ibv_req_notify_cq(cq, 0);
2184 WARN("unable to arm interrupt on rx queue %d", rx_queue_id);
2189 * DPDK callback for rx queue interrupt disable.
2192 * Pointer to Ethernet device structure.
2193 * @param rx_queue_id
2197 * 0 on success, negative on failure.
2200 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
2202 #ifdef HAVE_UPDATE_CQ_CI
2203 struct priv *priv = mlx5_get_priv(dev);
2204 struct rxq *rxq = (*priv->rxqs)[rx_queue_id];
2205 struct rxq_ctrl *rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
2206 struct ibv_cq *cq = rxq_ctrl->cq;
2207 struct ibv_cq *ev_cq;
2211 ret = ibv_get_cq_event(cq->channel, &ev_cq, &ev_ctx);
2212 if (ret || ev_cq != cq)
2215 ibv_ack_cq_events(cq, 1);
2222 WARN("unable to disable interrupt on rx queue %d",