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"
72 static __rte_always_inline int
73 check_cqe(volatile struct mlx5_cqe *cqe,
74 unsigned int cqes_n, const uint16_t ci);
76 static __rte_always_inline void
77 txq_complete(struct txq *txq);
79 static __rte_always_inline uint32_t
80 txq_mp2mr(struct txq *txq, struct rte_mempool *mp);
82 static __rte_always_inline void
83 mlx5_tx_dbrec(struct txq *txq, volatile struct mlx5_wqe *wqe);
85 static __rte_always_inline uint32_t
86 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe);
88 static __rte_always_inline int
89 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
90 uint16_t cqe_cnt, uint32_t *rss_hash);
92 static __rte_always_inline uint32_t
93 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe);
98 * Verify or set magic value in CQE.
107 check_cqe_seen(volatile struct mlx5_cqe *cqe)
109 static const uint8_t magic[] = "seen";
110 volatile uint8_t (*buf)[sizeof(cqe->rsvd0)] = &cqe->rsvd0;
114 for (i = 0; i < sizeof(magic) && i < sizeof(*buf); ++i)
115 if (!ret || (*buf)[i] != magic[i]) {
117 (*buf)[i] = magic[i];
125 * Check whether CQE is valid.
130 * Size of completion queue.
135 * 0 on success, 1 on failure.
138 check_cqe(volatile struct mlx5_cqe *cqe,
139 unsigned int cqes_n, const uint16_t ci)
141 uint16_t idx = ci & cqes_n;
142 uint8_t op_own = cqe->op_own;
143 uint8_t op_owner = MLX5_CQE_OWNER(op_own);
144 uint8_t op_code = MLX5_CQE_OPCODE(op_own);
146 if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
147 return 1; /* No CQE. */
149 if ((op_code == MLX5_CQE_RESP_ERR) ||
150 (op_code == MLX5_CQE_REQ_ERR)) {
151 volatile struct mlx5_err_cqe *err_cqe = (volatile void *)cqe;
152 uint8_t syndrome = err_cqe->syndrome;
154 if ((syndrome == MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR) ||
155 (syndrome == MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR))
157 if (!check_cqe_seen(cqe))
158 ERROR("unexpected CQE error %u (0x%02x)"
160 op_code, op_code, syndrome);
162 } else if ((op_code != MLX5_CQE_RESP_SEND) &&
163 (op_code != MLX5_CQE_REQ)) {
164 if (!check_cqe_seen(cqe))
165 ERROR("unexpected CQE opcode %u (0x%02x)",
174 * Return the address of the WQE.
177 * Pointer to TX queue structure.
179 * WQE consumer index.
184 static inline uintptr_t *
185 tx_mlx5_wqe(struct txq *txq, uint16_t ci)
187 ci &= ((1 << txq->wqe_n) - 1);
188 return (uintptr_t *)((uintptr_t)txq->wqes + ci * MLX5_WQE_SIZE);
192 * Return the size of tailroom of WQ.
195 * Pointer to TX queue structure.
197 * Pointer to tail of WQ.
203 tx_mlx5_wq_tailroom(struct txq *txq, void *addr)
206 tailroom = (uintptr_t)(txq->wqes) +
207 (1 << txq->wqe_n) * MLX5_WQE_SIZE -
213 * Copy data to tailroom of circular queue.
216 * Pointer to destination.
220 * Number of bytes to copy.
222 * Pointer to head of queue.
224 * Size of tailroom from dst.
227 * Pointer after copied data.
230 mlx5_copy_to_wq(void *dst, const void *src, size_t n,
231 void *base, size_t tailroom)
236 rte_memcpy(dst, src, tailroom);
237 rte_memcpy(base, (void *)((uintptr_t)src + tailroom),
239 ret = (uint8_t *)base + n - tailroom;
241 rte_memcpy(dst, src, n);
242 ret = (n == tailroom) ? base : (uint8_t *)dst + n;
248 * Manage TX completions.
250 * When sending a burst, mlx5_tx_burst() posts several WRs.
253 * Pointer to TX queue structure.
256 txq_complete(struct txq *txq)
258 const uint16_t elts_n = 1 << txq->elts_n;
259 const uint16_t elts_m = elts_n - 1;
260 const unsigned int cqe_n = 1 << txq->cqe_n;
261 const unsigned int cqe_cnt = cqe_n - 1;
262 uint16_t elts_free = txq->elts_tail;
264 uint16_t cq_ci = txq->cq_ci;
265 volatile struct mlx5_cqe *cqe = NULL;
266 volatile struct mlx5_wqe_ctrl *ctrl;
267 struct rte_mbuf *m, *free[elts_n];
268 struct rte_mempool *pool = NULL;
269 unsigned int blk_n = 0;
272 volatile struct mlx5_cqe *tmp;
274 tmp = &(*txq->cqes)[cq_ci & cqe_cnt];
275 if (check_cqe(tmp, cqe_n, cq_ci))
279 if (MLX5_CQE_FORMAT(cqe->op_own) == MLX5_COMPRESSED) {
280 if (!check_cqe_seen(cqe))
281 ERROR("unexpected compressed CQE, TX stopped");
284 if ((MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_RESP_ERR) ||
285 (MLX5_CQE_OPCODE(cqe->op_own) == MLX5_CQE_REQ_ERR)) {
286 if (!check_cqe_seen(cqe))
287 ERROR("unexpected error CQE, TX stopped");
293 if (unlikely(cqe == NULL))
295 txq->wqe_pi = ntohs(cqe->wqe_counter);
296 ctrl = (volatile struct mlx5_wqe_ctrl *)
297 tx_mlx5_wqe(txq, txq->wqe_pi);
298 elts_tail = ctrl->ctrl3;
299 assert((elts_tail & elts_m) < (1 << txq->wqe_n));
301 while (elts_free != elts_tail) {
302 m = rte_pktmbuf_prefree_seg((*txq->elts)[elts_free++ & elts_m]);
303 if (likely(m != NULL)) {
304 if (likely(m->pool == pool)) {
307 if (likely(pool != NULL))
308 rte_mempool_put_bulk(pool,
318 rte_mempool_put_bulk(pool, (void *)free, blk_n);
320 elts_free = txq->elts_tail;
322 while (elts_free != elts_tail) {
323 memset(&(*txq->elts)[elts_free & elts_m],
325 sizeof((*txq->elts)[elts_free & elts_m]));
330 txq->elts_tail = elts_tail;
331 /* Update the consumer index. */
333 *txq->cq_db = htonl(cq_ci);
337 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
338 * the cloned mbuf is allocated is returned instead.
344 * Memory pool where data is located for given mbuf.
346 static struct rte_mempool *
347 txq_mb2mp(struct rte_mbuf *buf)
349 if (unlikely(RTE_MBUF_INDIRECT(buf)))
350 return rte_mbuf_from_indirect(buf)->pool;
355 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
356 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
357 * remove an entry first.
360 * Pointer to TX queue structure.
362 * Memory Pool for which a Memory Region lkey must be returned.
365 * mr->lkey on success, (uint32_t)-1 on failure.
367 static inline uint32_t
368 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
371 uint32_t lkey = (uint32_t)-1;
373 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
374 if (unlikely(txq->mp2mr[i].mp == NULL)) {
375 /* Unknown MP, add a new MR for it. */
378 if (txq->mp2mr[i].mp == mp) {
379 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
380 assert(htonl(txq->mp2mr[i].mr->lkey) ==
382 lkey = txq->mp2mr[i].lkey;
386 if (unlikely(lkey == (uint32_t)-1))
387 lkey = txq_mp2mr_reg(txq, mp, i);
392 * Ring TX queue doorbell.
395 * Pointer to TX queue structure.
397 * Pointer to the last WQE posted in the NIC.
400 mlx5_tx_dbrec(struct txq *txq, volatile struct mlx5_wqe *wqe)
402 uint64_t *dst = (uint64_t *)((uintptr_t)txq->bf_reg);
403 volatile uint64_t *src = ((volatile uint64_t *)wqe);
406 *txq->qp_db = htonl(txq->wqe_ci);
407 /* Ensure ordering between DB record and BF copy. */
413 * DPDK callback to check the status of a tx descriptor.
418 * The index of the descriptor in the ring.
421 * The status of the tx descriptor.
424 mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset)
426 struct txq *txq = tx_queue;
430 used = txq->elts_head - txq->elts_tail;
432 return RTE_ETH_TX_DESC_FULL;
433 return RTE_ETH_TX_DESC_DONE;
437 * DPDK callback to check the status of a rx descriptor.
442 * The index of the descriptor in the ring.
445 * The status of the tx descriptor.
448 mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset)
450 struct rxq *rxq = rx_queue;
451 struct rxq_zip *zip = &rxq->zip;
452 volatile struct mlx5_cqe *cqe;
453 const unsigned int cqe_n = (1 << rxq->cqe_n);
454 const unsigned int cqe_cnt = cqe_n - 1;
458 /* if we are processing a compressed cqe */
460 used = zip->cqe_cnt - zip->ca;
466 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
467 while (check_cqe(cqe, cqe_n, cq_ci) == 0) {
471 op_own = cqe->op_own;
472 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED)
473 n = ntohl(cqe->byte_cnt);
478 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
480 used = RTE_MIN(used, (1U << rxq->elts_n) - 1);
482 return RTE_ETH_RX_DESC_DONE;
483 return RTE_ETH_RX_DESC_AVAIL;
487 * DPDK callback for TX.
490 * Generic pointer to TX queue structure.
492 * Packets to transmit.
494 * Number of packets in array.
497 * Number of packets successfully transmitted (<= pkts_n).
500 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
502 struct txq *txq = (struct txq *)dpdk_txq;
503 uint16_t elts_head = txq->elts_head;
504 const uint16_t elts_n = 1 << txq->elts_n;
505 const uint16_t elts_m = elts_n - 1;
510 unsigned int max_inline = txq->max_inline;
511 const unsigned int inline_en = !!max_inline && txq->inline_en;
514 volatile struct mlx5_wqe_v *wqe = NULL;
515 volatile struct mlx5_wqe_ctrl *last_wqe = NULL;
516 unsigned int segs_n = 0;
517 struct rte_mbuf *buf = NULL;
520 if (unlikely(!pkts_n))
522 /* Prefetch first packet cacheline. */
523 rte_prefetch0(*pkts);
524 /* Start processing. */
526 max_elts = (elts_n - (elts_head - txq->elts_tail));
527 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
528 if (unlikely(!max_wqe))
531 volatile rte_v128u32_t *dseg = NULL;
534 unsigned int sg = 0; /* counter of additional segs attached. */
537 uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE + 2;
538 uint16_t tso_header_sz = 0;
540 uint8_t cs_flags = 0;
542 uint16_t tso_segsz = 0;
543 #ifdef MLX5_PMD_SOFT_COUNTERS
544 uint32_t total_length = 0;
549 segs_n = buf->nb_segs;
551 * Make sure there is enough room to store this packet and
552 * that one ring entry remains unused.
555 if (max_elts < segs_n)
559 if (unlikely(--max_wqe == 0))
561 wqe = (volatile struct mlx5_wqe_v *)
562 tx_mlx5_wqe(txq, txq->wqe_ci);
563 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
565 rte_prefetch0(*(pkts + 1));
566 addr = rte_pktmbuf_mtod(buf, uintptr_t);
567 length = DATA_LEN(buf);
568 ehdr = (((uint8_t *)addr)[1] << 8) |
569 ((uint8_t *)addr)[0];
570 #ifdef MLX5_PMD_SOFT_COUNTERS
571 total_length = length;
573 if (length < (MLX5_WQE_DWORD_SIZE + 2))
575 /* Update element. */
576 (*txq->elts)[elts_head & elts_m] = buf;
577 /* Prefetch next buffer data. */
580 rte_pktmbuf_mtod(*(pkts + 1), volatile void *));
581 /* Should we enable HW CKSUM offload */
583 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
584 const uint64_t is_tunneled = buf->ol_flags &
586 PKT_TX_TUNNEL_VXLAN);
588 if (is_tunneled && txq->tunnel_en) {
589 cs_flags = MLX5_ETH_WQE_L3_INNER_CSUM |
590 MLX5_ETH_WQE_L4_INNER_CSUM;
591 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
592 cs_flags |= MLX5_ETH_WQE_L3_CSUM;
594 cs_flags = MLX5_ETH_WQE_L3_CSUM |
595 MLX5_ETH_WQE_L4_CSUM;
598 raw = ((uint8_t *)(uintptr_t)wqe) + 2 * MLX5_WQE_DWORD_SIZE;
599 /* Replace the Ethernet type by the VLAN if necessary. */
600 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
601 uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
602 unsigned int len = 2 * ETHER_ADDR_LEN - 2;
606 /* Copy Destination and source mac address. */
607 memcpy((uint8_t *)raw, ((uint8_t *)addr), len);
609 memcpy((uint8_t *)raw + len, &vlan, sizeof(vlan));
610 /* Copy missing two bytes to end the DSeg. */
611 memcpy((uint8_t *)raw + len + sizeof(vlan),
612 ((uint8_t *)addr) + len, 2);
616 memcpy((uint8_t *)raw, ((uint8_t *)addr) + 2,
617 MLX5_WQE_DWORD_SIZE);
618 length -= pkt_inline_sz;
619 addr += pkt_inline_sz;
622 tso = buf->ol_flags & PKT_TX_TCP_SEG;
624 uintptr_t end = (uintptr_t)
625 (((uintptr_t)txq->wqes) +
629 uint8_t vlan_sz = (buf->ol_flags &
630 PKT_TX_VLAN_PKT) ? 4 : 0;
631 const uint64_t is_tunneled =
634 PKT_TX_TUNNEL_VXLAN);
636 tso_header_sz = buf->l2_len + vlan_sz +
637 buf->l3_len + buf->l4_len;
638 tso_segsz = buf->tso_segsz;
640 if (is_tunneled && txq->tunnel_en) {
641 tso_header_sz += buf->outer_l2_len +
643 cs_flags |= MLX5_ETH_WQE_L4_INNER_CSUM;
645 cs_flags |= MLX5_ETH_WQE_L4_CSUM;
647 if (unlikely(tso_header_sz >
648 MLX5_MAX_TSO_HEADER))
650 copy_b = tso_header_sz - pkt_inline_sz;
651 /* First seg must contain all headers. */
652 assert(copy_b <= length);
653 raw += MLX5_WQE_DWORD_SIZE;
655 ((end - (uintptr_t)raw) > copy_b)) {
656 uint16_t n = (MLX5_WQE_DS(copy_b) -
659 if (unlikely(max_wqe < n))
662 rte_memcpy((void *)raw,
663 (void *)addr, copy_b);
666 pkt_inline_sz += copy_b;
668 * Another DWORD will be added
669 * in the inline part.
671 raw += MLX5_WQE_DS(copy_b) *
672 MLX5_WQE_DWORD_SIZE -
676 wqe->ctrl = (rte_v128u32_t){
677 htonl(txq->wqe_ci << 8),
678 htonl(txq->qp_num_8s | 1),
689 /* Inline if enough room. */
690 if (inline_en || tso) {
691 uintptr_t end = (uintptr_t)
692 (((uintptr_t)txq->wqes) +
693 (1 << txq->wqe_n) * MLX5_WQE_SIZE);
694 unsigned int inline_room = max_inline *
695 RTE_CACHE_LINE_SIZE -
697 uintptr_t addr_end = (addr + inline_room) &
698 ~(RTE_CACHE_LINE_SIZE - 1);
699 unsigned int copy_b = (addr_end > addr) ?
700 RTE_MIN((addr_end - addr), length) :
703 raw += MLX5_WQE_DWORD_SIZE;
704 if (copy_b && ((end - (uintptr_t)raw) > copy_b)) {
706 * One Dseg remains in the current WQE. To
707 * keep the computation positive, it is
708 * removed after the bytes to Dseg conversion.
710 uint16_t n = (MLX5_WQE_DS(copy_b) - 1 + 3) / 4;
712 if (unlikely(max_wqe < n))
717 htonl(copy_b | MLX5_INLINE_SEG);
720 MLX5_WQE_DS(tso_header_sz) *
722 rte_memcpy((void *)raw,
723 (void *)&inl, sizeof(inl));
725 pkt_inline_sz += sizeof(inl);
727 rte_memcpy((void *)raw, (void *)addr, copy_b);
730 pkt_inline_sz += copy_b;
733 * 2 DWORDs consumed by the WQE header + ETH segment +
734 * the size of the inline part of the packet.
736 ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
738 if (ds % (MLX5_WQE_SIZE /
739 MLX5_WQE_DWORD_SIZE) == 0) {
740 if (unlikely(--max_wqe == 0))
742 dseg = (volatile rte_v128u32_t *)
743 tx_mlx5_wqe(txq, txq->wqe_ci +
746 dseg = (volatile rte_v128u32_t *)
748 (ds * MLX5_WQE_DWORD_SIZE));
751 } else if (!segs_n) {
754 /* dseg will be advance as part of next_seg */
755 dseg = (volatile rte_v128u32_t *)
757 ((ds - 1) * MLX5_WQE_DWORD_SIZE));
762 * No inline has been done in the packet, only the
763 * Ethernet Header as been stored.
765 dseg = (volatile rte_v128u32_t *)
766 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
769 /* Add the remaining packet as a simple ds. */
770 naddr = htonll(addr);
771 *dseg = (rte_v128u32_t){
773 txq_mp2mr(txq, txq_mb2mp(buf)),
786 * Spill on next WQE when the current one does not have
787 * enough room left. Size of WQE must a be a multiple
788 * of data segment size.
790 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
791 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
792 if (unlikely(--max_wqe == 0))
794 dseg = (volatile rte_v128u32_t *)
795 tx_mlx5_wqe(txq, txq->wqe_ci + ds / 4);
796 rte_prefetch0(tx_mlx5_wqe(txq,
797 txq->wqe_ci + ds / 4 + 1));
804 length = DATA_LEN(buf);
805 #ifdef MLX5_PMD_SOFT_COUNTERS
806 total_length += length;
808 /* Store segment information. */
809 naddr = htonll(rte_pktmbuf_mtod(buf, uintptr_t));
810 *dseg = (rte_v128u32_t){
812 txq_mp2mr(txq, txq_mb2mp(buf)),
816 (*txq->elts)[++elts_head & elts_m] = buf;
818 /* Advance counter only if all segs are successfully posted. */
827 /* Initialize known and common part of the WQE structure. */
829 wqe->ctrl = (rte_v128u32_t){
830 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_TSO),
831 htonl(txq->qp_num_8s | ds),
835 wqe->eseg = (rte_v128u32_t){
837 cs_flags | (htons(tso_segsz) << 16),
839 (ehdr << 16) | htons(tso_header_sz),
842 wqe->ctrl = (rte_v128u32_t){
843 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
844 htonl(txq->qp_num_8s | ds),
848 wqe->eseg = (rte_v128u32_t){
852 (ehdr << 16) | htons(pkt_inline_sz),
856 txq->wqe_ci += (ds + 3) / 4;
857 /* Save the last successful WQE for completion request */
858 last_wqe = (volatile struct mlx5_wqe_ctrl *)wqe;
859 #ifdef MLX5_PMD_SOFT_COUNTERS
860 /* Increment sent bytes counter. */
861 txq->stats.obytes += total_length;
863 } while (i < pkts_n);
864 /* Take a shortcut if nothing must be sent. */
865 if (unlikely((i + k) == 0))
867 txq->elts_head += (i + j);
868 /* Check whether completion threshold has been reached. */
869 comp = txq->elts_comp + i + j + k;
870 if (comp >= MLX5_TX_COMP_THRESH) {
871 /* Request completion on last WQE. */
872 last_wqe->ctrl2 = htonl(8);
873 /* Save elts_head in unused "immediate" field of WQE. */
874 last_wqe->ctrl3 = txq->elts_head;
877 txq->elts_comp = comp;
879 #ifdef MLX5_PMD_SOFT_COUNTERS
880 /* Increment sent packets counter. */
881 txq->stats.opackets += i;
883 /* Ring QP doorbell. */
884 mlx5_tx_dbrec(txq, (volatile struct mlx5_wqe *)last_wqe);
889 * Open a MPW session.
892 * Pointer to TX queue structure.
894 * Pointer to MPW session structure.
899 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
901 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
902 volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
903 (volatile struct mlx5_wqe_data_seg (*)[])
904 tx_mlx5_wqe(txq, idx + 1);
906 mpw->state = MLX5_MPW_STATE_OPENED;
910 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
911 mpw->wqe->eseg.mss = htons(length);
912 mpw->wqe->eseg.inline_hdr_sz = 0;
913 mpw->wqe->eseg.rsvd0 = 0;
914 mpw->wqe->eseg.rsvd1 = 0;
915 mpw->wqe->eseg.rsvd2 = 0;
916 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
917 (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
918 mpw->wqe->ctrl[2] = 0;
919 mpw->wqe->ctrl[3] = 0;
920 mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
921 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
922 mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
923 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
924 mpw->data.dseg[2] = &(*dseg)[0];
925 mpw->data.dseg[3] = &(*dseg)[1];
926 mpw->data.dseg[4] = &(*dseg)[2];
930 * Close a MPW session.
933 * Pointer to TX queue structure.
935 * Pointer to MPW session structure.
938 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
940 unsigned int num = mpw->pkts_n;
943 * Store size in multiple of 16 bytes. Control and Ethernet segments
946 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
947 mpw->state = MLX5_MPW_STATE_CLOSED;
952 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
953 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
957 * DPDK callback for TX with MPW support.
960 * Generic pointer to TX queue structure.
962 * Packets to transmit.
964 * Number of packets in array.
967 * Number of packets successfully transmitted (<= pkts_n).
970 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
972 struct txq *txq = (struct txq *)dpdk_txq;
973 uint16_t elts_head = txq->elts_head;
974 const uint16_t elts_n = 1 << txq->elts_n;
975 const uint16_t elts_m = elts_n - 1;
981 struct mlx5_mpw mpw = {
982 .state = MLX5_MPW_STATE_CLOSED,
985 if (unlikely(!pkts_n))
987 /* Prefetch first packet cacheline. */
988 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
989 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
990 /* Start processing. */
992 max_elts = (elts_n - (elts_head - txq->elts_tail));
993 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
994 if (unlikely(!max_wqe))
997 struct rte_mbuf *buf = *(pkts++);
999 unsigned int segs_n = buf->nb_segs;
1000 uint32_t cs_flags = 0;
1003 * Make sure there is enough room to store this packet and
1004 * that one ring entry remains unused.
1007 if (max_elts < segs_n)
1009 /* Do not bother with large packets MPW cannot handle. */
1010 if (segs_n > MLX5_MPW_DSEG_MAX)
1014 /* Should we enable HW CKSUM offload */
1016 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1017 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1018 /* Retrieve packet information. */
1019 length = PKT_LEN(buf);
1021 /* Start new session if packet differs. */
1022 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
1023 ((mpw.len != length) ||
1025 (mpw.wqe->eseg.cs_flags != cs_flags)))
1026 mlx5_mpw_close(txq, &mpw);
1027 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1029 * Multi-Packet WQE consumes at most two WQE.
1030 * mlx5_mpw_new() expects to be able to use such
1033 if (unlikely(max_wqe < 2))
1036 mlx5_mpw_new(txq, &mpw, length);
1037 mpw.wqe->eseg.cs_flags = cs_flags;
1039 /* Multi-segment packets must be alone in their MPW. */
1040 assert((segs_n == 1) || (mpw.pkts_n == 0));
1041 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1045 volatile struct mlx5_wqe_data_seg *dseg;
1049 (*txq->elts)[elts_head++ & elts_m] = buf;
1050 dseg = mpw.data.dseg[mpw.pkts_n];
1051 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1052 *dseg = (struct mlx5_wqe_data_seg){
1053 .byte_count = htonl(DATA_LEN(buf)),
1054 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1055 .addr = htonll(addr),
1057 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1058 length += DATA_LEN(buf);
1064 assert(length == mpw.len);
1065 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1066 mlx5_mpw_close(txq, &mpw);
1067 #ifdef MLX5_PMD_SOFT_COUNTERS
1068 /* Increment sent bytes counter. */
1069 txq->stats.obytes += length;
1073 /* Take a shortcut if nothing must be sent. */
1074 if (unlikely(i == 0))
1076 /* Check whether completion threshold has been reached. */
1077 /* "j" includes both packets and segments. */
1078 comp = txq->elts_comp + j;
1079 if (comp >= MLX5_TX_COMP_THRESH) {
1080 volatile struct mlx5_wqe *wqe = mpw.wqe;
1082 /* Request completion on last WQE. */
1083 wqe->ctrl[2] = htonl(8);
1084 /* Save elts_head in unused "immediate" field of WQE. */
1085 wqe->ctrl[3] = elts_head;
1088 txq->elts_comp = comp;
1090 #ifdef MLX5_PMD_SOFT_COUNTERS
1091 /* Increment sent packets counter. */
1092 txq->stats.opackets += i;
1094 /* Ring QP doorbell. */
1095 if (mpw.state == MLX5_MPW_STATE_OPENED)
1096 mlx5_mpw_close(txq, &mpw);
1097 mlx5_tx_dbrec(txq, mpw.wqe);
1098 txq->elts_head = elts_head;
1103 * Open a MPW inline session.
1106 * Pointer to TX queue structure.
1108 * Pointer to MPW session structure.
1113 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
1115 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
1116 struct mlx5_wqe_inl_small *inl;
1118 mpw->state = MLX5_MPW_INL_STATE_OPENED;
1122 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
1123 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
1124 (txq->wqe_ci << 8) |
1126 mpw->wqe->ctrl[2] = 0;
1127 mpw->wqe->ctrl[3] = 0;
1128 mpw->wqe->eseg.mss = htons(length);
1129 mpw->wqe->eseg.inline_hdr_sz = 0;
1130 mpw->wqe->eseg.cs_flags = 0;
1131 mpw->wqe->eseg.rsvd0 = 0;
1132 mpw->wqe->eseg.rsvd1 = 0;
1133 mpw->wqe->eseg.rsvd2 = 0;
1134 inl = (struct mlx5_wqe_inl_small *)
1135 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
1136 mpw->data.raw = (uint8_t *)&inl->raw;
1140 * Close a MPW inline session.
1143 * Pointer to TX queue structure.
1145 * Pointer to MPW session structure.
1148 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
1151 struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
1152 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
1154 size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
1156 * Store size in multiple of 16 bytes. Control and Ethernet segments
1159 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
1160 mpw->state = MLX5_MPW_STATE_CLOSED;
1161 inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
1162 txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
1166 * DPDK callback for TX with MPW inline support.
1169 * Generic pointer to TX queue structure.
1171 * Packets to transmit.
1173 * Number of packets in array.
1176 * Number of packets successfully transmitted (<= pkts_n).
1179 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
1182 struct txq *txq = (struct txq *)dpdk_txq;
1183 uint16_t elts_head = txq->elts_head;
1184 const uint16_t elts_n = 1 << txq->elts_n;
1185 const uint16_t elts_m = elts_n - 1;
1191 unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
1192 struct mlx5_mpw mpw = {
1193 .state = MLX5_MPW_STATE_CLOSED,
1196 * Compute the maximum number of WQE which can be consumed by inline
1199 * - 1 control segment,
1200 * - 1 Ethernet segment,
1201 * - N Dseg from the inline request.
1203 const unsigned int wqe_inl_n =
1204 ((2 * MLX5_WQE_DWORD_SIZE +
1205 txq->max_inline * RTE_CACHE_LINE_SIZE) +
1206 RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
1208 if (unlikely(!pkts_n))
1210 /* Prefetch first packet cacheline. */
1211 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
1212 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
1213 /* Start processing. */
1215 max_elts = (elts_n - (elts_head - txq->elts_tail));
1217 struct rte_mbuf *buf = *(pkts++);
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_elts < segs_n)
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 (*txq->elts)[elts_head++ & elts_m] = buf;
1296 dseg = mpw.data.dseg[mpw.pkts_n];
1297 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1298 *dseg = (struct mlx5_wqe_data_seg){
1299 .byte_count = htonl(DATA_LEN(buf)),
1300 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1301 .addr = htonll(addr),
1303 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1304 length += DATA_LEN(buf);
1310 assert(length == mpw.len);
1311 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1312 mlx5_mpw_close(txq, &mpw);
1316 assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1317 assert(length <= inline_room);
1318 assert(length == DATA_LEN(buf));
1319 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1320 (*txq->elts)[elts_head++ & elts_m] = buf;
1321 /* Maximum number of bytes before wrapping. */
1322 max = ((((uintptr_t)(txq->wqes)) +
1325 (uintptr_t)mpw.data.raw);
1327 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1330 mpw.data.raw = (volatile void *)txq->wqes;
1331 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1332 (void *)(addr + max),
1334 mpw.data.raw += length - max;
1336 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1342 (volatile void *)txq->wqes;
1344 mpw.data.raw += length;
1347 mpw.total_len += length;
1349 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1350 mlx5_mpw_inline_close(txq, &mpw);
1352 txq->max_inline * RTE_CACHE_LINE_SIZE;
1354 inline_room -= length;
1357 #ifdef MLX5_PMD_SOFT_COUNTERS
1358 /* Increment sent bytes counter. */
1359 txq->stats.obytes += length;
1363 /* Take a shortcut if nothing must be sent. */
1364 if (unlikely(i == 0))
1366 /* Check whether completion threshold has been reached. */
1367 /* "j" includes both packets and segments. */
1368 comp = txq->elts_comp + j;
1369 if (comp >= MLX5_TX_COMP_THRESH) {
1370 volatile struct mlx5_wqe *wqe = mpw.wqe;
1372 /* Request completion on last WQE. */
1373 wqe->ctrl[2] = htonl(8);
1374 /* Save elts_head in unused "immediate" field of WQE. */
1375 wqe->ctrl[3] = elts_head;
1378 txq->elts_comp = comp;
1380 #ifdef MLX5_PMD_SOFT_COUNTERS
1381 /* Increment sent packets counter. */
1382 txq->stats.opackets += i;
1384 /* Ring QP doorbell. */
1385 if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1386 mlx5_mpw_inline_close(txq, &mpw);
1387 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1388 mlx5_mpw_close(txq, &mpw);
1389 mlx5_tx_dbrec(txq, mpw.wqe);
1390 txq->elts_head = elts_head;
1395 * Open an Enhanced MPW session.
1398 * Pointer to TX queue structure.
1400 * Pointer to MPW session structure.
1405 mlx5_empw_new(struct txq *txq, struct mlx5_mpw *mpw, int padding)
1407 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
1409 mpw->state = MLX5_MPW_ENHANCED_STATE_OPENED;
1411 mpw->total_len = sizeof(struct mlx5_wqe);
1412 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
1413 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_ENHANCED_MPSW << 24) |
1414 (txq->wqe_ci << 8) |
1415 MLX5_OPCODE_ENHANCED_MPSW);
1416 mpw->wqe->ctrl[2] = 0;
1417 mpw->wqe->ctrl[3] = 0;
1418 memset((void *)(uintptr_t)&mpw->wqe->eseg, 0, MLX5_WQE_DWORD_SIZE);
1419 if (unlikely(padding)) {
1420 uintptr_t addr = (uintptr_t)(mpw->wqe + 1);
1422 /* Pad the first 2 DWORDs with zero-length inline header. */
1423 *(volatile uint32_t *)addr = htonl(MLX5_INLINE_SEG);
1424 *(volatile uint32_t *)(addr + MLX5_WQE_DWORD_SIZE) =
1425 htonl(MLX5_INLINE_SEG);
1426 mpw->total_len += 2 * MLX5_WQE_DWORD_SIZE;
1427 /* Start from the next WQEBB. */
1428 mpw->data.raw = (volatile void *)(tx_mlx5_wqe(txq, idx + 1));
1430 mpw->data.raw = (volatile void *)(mpw->wqe + 1);
1435 * Close an Enhanced MPW session.
1438 * Pointer to TX queue structure.
1440 * Pointer to MPW session structure.
1443 * Number of consumed WQEs.
1445 static inline uint16_t
1446 mlx5_empw_close(struct txq *txq, struct mlx5_mpw *mpw)
1450 /* Store size in multiple of 16 bytes. Control and Ethernet segments
1453 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(mpw->total_len));
1454 mpw->state = MLX5_MPW_STATE_CLOSED;
1455 ret = (mpw->total_len + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
1461 * DPDK callback for TX with Enhanced MPW support.
1464 * Generic pointer to TX queue structure.
1466 * Packets to transmit.
1468 * Number of packets in array.
1471 * Number of packets successfully transmitted (<= pkts_n).
1474 mlx5_tx_burst_empw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1476 struct txq *txq = (struct txq *)dpdk_txq;
1477 uint16_t elts_head = txq->elts_head;
1478 const uint16_t elts_n = 1 << txq->elts_n;
1479 const uint16_t elts_m = elts_n - 1;
1484 unsigned int max_inline = txq->max_inline * RTE_CACHE_LINE_SIZE;
1485 unsigned int mpw_room = 0;
1486 unsigned int inl_pad = 0;
1488 struct mlx5_mpw mpw = {
1489 .state = MLX5_MPW_STATE_CLOSED,
1492 if (unlikely(!pkts_n))
1494 /* Start processing. */
1496 max_elts = (elts_n - (elts_head - txq->elts_tail));
1497 /* A CQE slot must always be available. */
1498 assert((1u << txq->cqe_n) - (txq->cq_pi - txq->cq_ci));
1499 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1500 if (unlikely(!max_wqe))
1503 struct rte_mbuf *buf = *(pkts++);
1507 unsigned int do_inline = 0; /* Whether inline is possible. */
1509 unsigned int segs_n = buf->nb_segs;
1510 uint32_t cs_flags = 0;
1513 * Make sure there is enough room to store this packet and
1514 * that one ring entry remains unused.
1517 if (max_elts - j < segs_n)
1519 /* Do not bother with large packets MPW cannot handle. */
1520 if (segs_n > MLX5_MPW_DSEG_MAX)
1522 /* Should we enable HW CKSUM offload. */
1524 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1525 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1526 /* Retrieve packet information. */
1527 length = PKT_LEN(buf);
1528 /* Start new session if:
1529 * - multi-segment packet
1530 * - no space left even for a dseg
1531 * - next packet can be inlined with a new WQE
1533 * It can't be MLX5_MPW_STATE_OPENED as always have a single
1536 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED) {
1537 if ((segs_n != 1) ||
1538 (inl_pad + sizeof(struct mlx5_wqe_data_seg) >
1540 (length <= txq->inline_max_packet_sz &&
1541 inl_pad + sizeof(inl_hdr) + length >
1543 (mpw.wqe->eseg.cs_flags != cs_flags))
1544 max_wqe -= mlx5_empw_close(txq, &mpw);
1546 if (unlikely(mpw.state == MLX5_MPW_STATE_CLOSED)) {
1547 if (unlikely(segs_n != 1)) {
1548 /* Fall back to legacy MPW.
1549 * A MPW session consumes 2 WQEs at most to
1550 * include MLX5_MPW_DSEG_MAX pointers.
1552 if (unlikely(max_wqe < 2))
1554 mlx5_mpw_new(txq, &mpw, length);
1556 /* In Enhanced MPW, inline as much as the budget
1557 * is allowed. The remaining space is to be
1558 * filled with dsegs. If the title WQEBB isn't
1559 * padded, it will have 2 dsegs there.
1561 mpw_room = RTE_MIN(MLX5_WQE_SIZE_MAX,
1562 (max_inline ? max_inline :
1563 pkts_n * MLX5_WQE_DWORD_SIZE) +
1565 if (unlikely(max_wqe * MLX5_WQE_SIZE <
1568 /* Don't pad the title WQEBB to not waste WQ. */
1569 mlx5_empw_new(txq, &mpw, 0);
1570 mpw_room -= mpw.total_len;
1573 length <= txq->inline_max_packet_sz &&
1574 sizeof(inl_hdr) + length <= mpw_room &&
1577 mpw.wqe->eseg.cs_flags = cs_flags;
1579 /* Evaluate whether the next packet can be inlined.
1580 * Inlininig is possible when:
1581 * - length is less than configured value
1582 * - length fits for remaining space
1583 * - not required to fill the title WQEBB with dsegs
1586 length <= txq->inline_max_packet_sz &&
1587 inl_pad + sizeof(inl_hdr) + length <=
1589 (!txq->mpw_hdr_dseg ||
1590 mpw.total_len >= MLX5_WQE_SIZE);
1592 /* Multi-segment packets must be alone in their MPW. */
1593 assert((segs_n == 1) || (mpw.pkts_n == 0));
1594 if (unlikely(mpw.state == MLX5_MPW_STATE_OPENED)) {
1595 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1599 volatile struct mlx5_wqe_data_seg *dseg;
1602 (*txq->elts)[elts_head++ & elts_m] = buf;
1603 dseg = mpw.data.dseg[mpw.pkts_n];
1604 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1605 *dseg = (struct mlx5_wqe_data_seg){
1606 .byte_count = htonl(DATA_LEN(buf)),
1607 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1608 .addr = htonll(addr),
1610 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1611 length += DATA_LEN(buf);
1617 /* A multi-segmented packet takes one MPW session.
1618 * TODO: Pack more multi-segmented packets if possible.
1620 mlx5_mpw_close(txq, &mpw);
1625 } else if (do_inline) {
1626 /* Inline packet into WQE. */
1629 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1630 assert(length == DATA_LEN(buf));
1631 inl_hdr = htonl(length | MLX5_INLINE_SEG);
1632 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1633 mpw.data.raw = (volatile void *)
1634 ((uintptr_t)mpw.data.raw + inl_pad);
1635 max = tx_mlx5_wq_tailroom(txq,
1636 (void *)(uintptr_t)mpw.data.raw);
1637 /* Copy inline header. */
1638 mpw.data.raw = (volatile void *)
1640 (void *)(uintptr_t)mpw.data.raw,
1643 (void *)(uintptr_t)txq->wqes,
1645 max = tx_mlx5_wq_tailroom(txq,
1646 (void *)(uintptr_t)mpw.data.raw);
1647 /* Copy packet data. */
1648 mpw.data.raw = (volatile void *)
1650 (void *)(uintptr_t)mpw.data.raw,
1653 (void *)(uintptr_t)txq->wqes,
1656 mpw.total_len += (inl_pad + sizeof(inl_hdr) + length);
1657 /* No need to get completion as the entire packet is
1658 * copied to WQ. Free the buf right away.
1660 rte_pktmbuf_free_seg(buf);
1661 mpw_room -= (inl_pad + sizeof(inl_hdr) + length);
1662 /* Add pad in the next packet if any. */
1663 inl_pad = (((uintptr_t)mpw.data.raw +
1664 (MLX5_WQE_DWORD_SIZE - 1)) &
1665 ~(MLX5_WQE_DWORD_SIZE - 1)) -
1666 (uintptr_t)mpw.data.raw;
1668 /* No inline. Load a dseg of packet pointer. */
1669 volatile rte_v128u32_t *dseg;
1671 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1672 assert((inl_pad + sizeof(*dseg)) <= mpw_room);
1673 assert(length == DATA_LEN(buf));
1674 if (!tx_mlx5_wq_tailroom(txq,
1675 (void *)((uintptr_t)mpw.data.raw
1677 dseg = (volatile void *)txq->wqes;
1679 dseg = (volatile void *)
1680 ((uintptr_t)mpw.data.raw +
1682 (*txq->elts)[elts_head++ & elts_m] = buf;
1683 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1684 for (n = 0; n * RTE_CACHE_LINE_SIZE < length; n++)
1685 rte_prefetch2((void *)(addr +
1686 n * RTE_CACHE_LINE_SIZE));
1687 naddr = htonll(addr);
1688 *dseg = (rte_v128u32_t) {
1690 txq_mp2mr(txq, txq_mb2mp(buf)),
1694 mpw.data.raw = (volatile void *)(dseg + 1);
1695 mpw.total_len += (inl_pad + sizeof(*dseg));
1698 mpw_room -= (inl_pad + sizeof(*dseg));
1701 #ifdef MLX5_PMD_SOFT_COUNTERS
1702 /* Increment sent bytes counter. */
1703 txq->stats.obytes += length;
1706 } while (i < pkts_n);
1707 /* Take a shortcut if nothing must be sent. */
1708 if (unlikely(i == 0))
1710 /* Check whether completion threshold has been reached. */
1711 if (txq->elts_comp + j >= MLX5_TX_COMP_THRESH ||
1712 (uint16_t)(txq->wqe_ci - txq->mpw_comp) >=
1713 (1 << txq->wqe_n) / MLX5_TX_COMP_THRESH_INLINE_DIV) {
1714 volatile struct mlx5_wqe *wqe = mpw.wqe;
1716 /* Request completion on last WQE. */
1717 wqe->ctrl[2] = htonl(8);
1718 /* Save elts_head in unused "immediate" field of WQE. */
1719 wqe->ctrl[3] = elts_head;
1721 txq->mpw_comp = txq->wqe_ci;
1724 txq->elts_comp += j;
1726 #ifdef MLX5_PMD_SOFT_COUNTERS
1727 /* Increment sent packets counter. */
1728 txq->stats.opackets += i;
1730 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED)
1731 mlx5_empw_close(txq, &mpw);
1732 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1733 mlx5_mpw_close(txq, &mpw);
1734 /* Ring QP doorbell. */
1735 mlx5_tx_dbrec(txq, mpw.wqe);
1736 txq->elts_head = elts_head;
1741 * Translate RX completion flags to packet type.
1746 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1749 * Packet type for struct rte_mbuf.
1751 static inline uint32_t
1752 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1755 uint16_t flags = ntohs(cqe->hdr_type_etc);
1757 if (cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) {
1760 MLX5_CQE_RX_IPV4_PACKET,
1761 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
1763 MLX5_CQE_RX_IPV6_PACKET,
1764 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
1765 pkt_type |= ((cqe->pkt_info & MLX5_CQE_RX_OUTER_PACKET) ?
1766 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN :
1767 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1771 MLX5_CQE_L3_HDR_TYPE_IPV6,
1772 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
1774 MLX5_CQE_L3_HDR_TYPE_IPV4,
1775 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1781 * Get size of the next packet for a given CQE. For compressed CQEs, the
1782 * consumer index is updated only once all packets of the current one have
1786 * Pointer to RX queue.
1789 * @param[out] rss_hash
1790 * Packet RSS Hash result.
1793 * Packet size in bytes (0 if there is none), -1 in case of completion
1797 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1798 uint16_t cqe_cnt, uint32_t *rss_hash)
1800 struct rxq_zip *zip = &rxq->zip;
1801 uint16_t cqe_n = cqe_cnt + 1;
1805 /* Process compressed data in the CQE and mini arrays. */
1807 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1808 (volatile struct mlx5_mini_cqe8 (*)[8])
1809 (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt]);
1811 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1812 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1813 if ((++zip->ai & 7) == 0) {
1814 /* Invalidate consumed CQEs */
1817 while (idx != end) {
1818 (*rxq->cqes)[idx & cqe_cnt].op_own =
1819 MLX5_CQE_INVALIDATE;
1823 * Increment consumer index to skip the number of
1824 * CQEs consumed. Hardware leaves holes in the CQ
1825 * ring for software use.
1830 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1831 /* Invalidate the rest */
1835 while (idx != end) {
1836 (*rxq->cqes)[idx & cqe_cnt].op_own =
1837 MLX5_CQE_INVALIDATE;
1840 rxq->cq_ci = zip->cq_ci;
1843 /* No compressed data, get next CQE and verify if it is compressed. */
1848 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1849 if (unlikely(ret == 1))
1852 op_own = cqe->op_own;
1853 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1854 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1855 (volatile struct mlx5_mini_cqe8 (*)[8])
1856 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1859 /* Fix endianness. */
1860 zip->cqe_cnt = ntohl(cqe->byte_cnt);
1862 * Current mini array position is the one returned by
1865 * If completion comprises several mini arrays, as a
1866 * special case the second one is located 7 CQEs after
1867 * the initial CQE instead of 8 for subsequent ones.
1869 zip->ca = rxq->cq_ci;
1870 zip->na = zip->ca + 7;
1871 /* Compute the next non compressed CQE. */
1873 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1874 /* Get packet size to return. */
1875 len = ntohl((*mc)[0].byte_cnt);
1876 *rss_hash = ntohl((*mc)[0].rx_hash_result);
1878 /* Prefetch all the entries to be invalidated */
1881 while (idx != end) {
1882 rte_prefetch0(&(*rxq->cqes)[(idx) & cqe_cnt]);
1886 len = ntohl(cqe->byte_cnt);
1887 *rss_hash = ntohl(cqe->rx_hash_res);
1889 /* Error while receiving packet. */
1890 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1897 * Translate RX completion flags to offload flags.
1900 * Pointer to RX queue structure.
1905 * Offload flags (ol_flags) for struct rte_mbuf.
1907 static inline uint32_t
1908 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1910 uint32_t ol_flags = 0;
1911 uint16_t flags = ntohs(cqe->hdr_type_etc);
1915 MLX5_CQE_RX_L3_HDR_VALID,
1916 PKT_RX_IP_CKSUM_GOOD) |
1918 MLX5_CQE_RX_L4_HDR_VALID,
1919 PKT_RX_L4_CKSUM_GOOD);
1920 if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1923 MLX5_CQE_RX_L3_HDR_VALID,
1924 PKT_RX_IP_CKSUM_GOOD) |
1926 MLX5_CQE_RX_L4_HDR_VALID,
1927 PKT_RX_L4_CKSUM_GOOD);
1932 * DPDK callback for RX.
1935 * Generic pointer to RX queue structure.
1937 * Array to store received packets.
1939 * Maximum number of packets in array.
1942 * Number of packets successfully received (<= pkts_n).
1945 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1947 struct rxq *rxq = dpdk_rxq;
1948 const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1949 const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1950 const unsigned int sges_n = rxq->sges_n;
1951 struct rte_mbuf *pkt = NULL;
1952 struct rte_mbuf *seg = NULL;
1953 volatile struct mlx5_cqe *cqe =
1954 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1956 unsigned int rq_ci = rxq->rq_ci << sges_n;
1957 int len = 0; /* keep its value across iterations. */
1960 unsigned int idx = rq_ci & wqe_cnt;
1961 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1962 struct rte_mbuf *rep = (*rxq->elts)[idx];
1963 uint32_t rss_hash_res = 0;
1971 rep = rte_mbuf_raw_alloc(rxq->mp);
1972 if (unlikely(rep == NULL)) {
1973 ++rxq->stats.rx_nombuf;
1976 * no buffers before we even started,
1977 * bail out silently.
1981 while (pkt != seg) {
1982 assert(pkt != (*rxq->elts)[idx]);
1986 rte_mbuf_raw_free(pkt);
1992 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1993 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
1996 rte_mbuf_raw_free(rep);
1999 if (unlikely(len == -1)) {
2000 /* RX error, packet is likely too large. */
2001 rte_mbuf_raw_free(rep);
2002 ++rxq->stats.idropped;
2006 assert(len >= (rxq->crc_present << 2));
2007 /* Update packet information. */
2008 pkt->packet_type = 0;
2010 if (rss_hash_res && rxq->rss_hash) {
2011 pkt->hash.rss = rss_hash_res;
2012 pkt->ol_flags = PKT_RX_RSS_HASH;
2015 MLX5_FLOW_MARK_IS_VALID(cqe->sop_drop_qpn)) {
2016 pkt->ol_flags |= PKT_RX_FDIR;
2017 if (cqe->sop_drop_qpn !=
2018 htonl(MLX5_FLOW_MARK_DEFAULT)) {
2019 uint32_t mark = cqe->sop_drop_qpn;
2021 pkt->ol_flags |= PKT_RX_FDIR_ID;
2023 mlx5_flow_mark_get(mark);
2026 if (rxq->csum | rxq->csum_l2tun) {
2027 pkt->packet_type = rxq_cq_to_pkt_type(cqe);
2028 pkt->ol_flags |= rxq_cq_to_ol_flags(rxq, cqe);
2030 if (rxq->vlan_strip &&
2031 (cqe->hdr_type_etc &
2032 htons(MLX5_CQE_VLAN_STRIPPED))) {
2033 pkt->ol_flags |= PKT_RX_VLAN_PKT |
2034 PKT_RX_VLAN_STRIPPED;
2035 pkt->vlan_tci = ntohs(cqe->vlan_info);
2037 if (rxq->crc_present)
2038 len -= ETHER_CRC_LEN;
2041 DATA_LEN(rep) = DATA_LEN(seg);
2042 PKT_LEN(rep) = PKT_LEN(seg);
2043 SET_DATA_OFF(rep, DATA_OFF(seg));
2044 PORT(rep) = PORT(seg);
2045 (*rxq->elts)[idx] = rep;
2047 * Fill NIC descriptor with the new buffer. The lkey and size
2048 * of the buffers are already known, only the buffer address
2051 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
2052 if (len > DATA_LEN(seg)) {
2053 len -= DATA_LEN(seg);
2058 DATA_LEN(seg) = len;
2059 #ifdef MLX5_PMD_SOFT_COUNTERS
2060 /* Increment bytes counter. */
2061 rxq->stats.ibytes += PKT_LEN(pkt);
2063 /* Return packet. */
2069 /* Align consumer index to the next stride. */
2074 if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
2076 /* Update the consumer index. */
2077 rxq->rq_ci = rq_ci >> sges_n;
2079 *rxq->cq_db = htonl(rxq->cq_ci);
2081 *rxq->rq_db = htonl(rxq->rq_ci);
2082 #ifdef MLX5_PMD_SOFT_COUNTERS
2083 /* Increment packets counter. */
2084 rxq->stats.ipackets += i;
2090 * Dummy DPDK callback for TX.
2092 * This function is used to temporarily replace the real callback during
2093 * unsafe control operations on the queue, or in case of error.
2096 * Generic pointer to TX queue structure.
2098 * Packets to transmit.
2100 * Number of packets in array.
2103 * Number of packets successfully transmitted (<= pkts_n).
2106 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2115 * Dummy DPDK callback for RX.
2117 * This function is used to temporarily replace the real callback during
2118 * unsafe control operations on the queue, or in case of error.
2121 * Generic pointer to RX queue structure.
2123 * Array to store received packets.
2125 * Maximum number of packets in array.
2128 * Number of packets successfully received (<= pkts_n).
2131 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)