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 uint16_t tso_segsz = 0;
537 #ifdef MLX5_PMD_SOFT_COUNTERS
538 uint32_t total_length = 0;
543 segs_n = buf->nb_segs;
545 * Make sure there is enough room to store this packet and
546 * that one ring entry remains unused.
549 if (max < segs_n + 1)
553 if (unlikely(--max_wqe == 0))
555 wqe = (volatile struct mlx5_wqe_v *)
556 tx_mlx5_wqe(txq, txq->wqe_ci);
557 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
559 rte_prefetch0(*(pkts + 1));
560 addr = rte_pktmbuf_mtod(buf, uintptr_t);
561 length = DATA_LEN(buf);
562 ehdr = (((uint8_t *)addr)[1] << 8) |
563 ((uint8_t *)addr)[0];
564 #ifdef MLX5_PMD_SOFT_COUNTERS
565 total_length = length;
567 if (length < (MLX5_WQE_DWORD_SIZE + 2))
569 /* Update element. */
570 (*txq->elts)[elts_head] = buf;
571 /* Prefetch next buffer data. */
574 rte_pktmbuf_mtod(*(pkts + 1), volatile void *));
575 /* Should we enable HW CKSUM offload */
577 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
578 const uint64_t is_tunneled = buf->ol_flags &
580 PKT_TX_TUNNEL_VXLAN);
582 if (is_tunneled && txq->tunnel_en) {
583 cs_flags = MLX5_ETH_WQE_L3_INNER_CSUM |
584 MLX5_ETH_WQE_L4_INNER_CSUM;
585 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
586 cs_flags |= MLX5_ETH_WQE_L3_CSUM;
588 cs_flags = MLX5_ETH_WQE_L3_CSUM |
589 MLX5_ETH_WQE_L4_CSUM;
592 raw = ((uint8_t *)(uintptr_t)wqe) + 2 * MLX5_WQE_DWORD_SIZE;
593 /* Replace the Ethernet type by the VLAN if necessary. */
594 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
595 uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
596 unsigned int len = 2 * ETHER_ADDR_LEN - 2;
600 /* Copy Destination and source mac address. */
601 memcpy((uint8_t *)raw, ((uint8_t *)addr), len);
603 memcpy((uint8_t *)raw + len, &vlan, sizeof(vlan));
604 /* Copy missing two bytes to end the DSeg. */
605 memcpy((uint8_t *)raw + len + sizeof(vlan),
606 ((uint8_t *)addr) + len, 2);
610 memcpy((uint8_t *)raw, ((uint8_t *)addr) + 2,
611 MLX5_WQE_DWORD_SIZE);
612 length -= pkt_inline_sz;
613 addr += pkt_inline_sz;
616 tso = buf->ol_flags & PKT_TX_TCP_SEG;
618 uintptr_t end = (uintptr_t)
619 (((uintptr_t)txq->wqes) +
623 uint8_t vlan_sz = (buf->ol_flags &
624 PKT_TX_VLAN_PKT) ? 4 : 0;
625 const uint64_t is_tunneled =
628 PKT_TX_TUNNEL_VXLAN);
630 tso_header_sz = buf->l2_len + vlan_sz +
631 buf->l3_len + buf->l4_len;
632 tso_segsz = buf->tso_segsz;
634 if (is_tunneled && txq->tunnel_en) {
635 tso_header_sz += buf->outer_l2_len +
637 cs_flags |= MLX5_ETH_WQE_L4_INNER_CSUM;
639 cs_flags |= MLX5_ETH_WQE_L4_CSUM;
641 if (unlikely(tso_header_sz >
642 MLX5_MAX_TSO_HEADER))
644 copy_b = tso_header_sz - pkt_inline_sz;
645 /* First seg must contain all headers. */
646 assert(copy_b <= length);
647 raw += MLX5_WQE_DWORD_SIZE;
649 ((end - (uintptr_t)raw) > copy_b)) {
650 uint16_t n = (MLX5_WQE_DS(copy_b) -
653 if (unlikely(max_wqe < n))
656 rte_memcpy((void *)raw,
657 (void *)addr, copy_b);
660 pkt_inline_sz += copy_b;
662 * Another DWORD will be added
663 * in the inline part.
665 raw += MLX5_WQE_DS(copy_b) *
666 MLX5_WQE_DWORD_SIZE -
670 wqe->ctrl = (rte_v128u32_t){
671 htonl(txq->wqe_ci << 8),
672 htonl(txq->qp_num_8s | 1),
683 /* Inline if enough room. */
684 if (inline_en || tso) {
685 uintptr_t end = (uintptr_t)
686 (((uintptr_t)txq->wqes) +
687 (1 << txq->wqe_n) * MLX5_WQE_SIZE);
688 unsigned int inline_room = max_inline *
689 RTE_CACHE_LINE_SIZE -
691 uintptr_t addr_end = (addr + inline_room) &
692 ~(RTE_CACHE_LINE_SIZE - 1);
693 unsigned int copy_b = (addr_end > addr) ?
694 RTE_MIN((addr_end - addr), length) :
697 raw += MLX5_WQE_DWORD_SIZE;
698 if (copy_b && ((end - (uintptr_t)raw) > copy_b)) {
700 * One Dseg remains in the current WQE. To
701 * keep the computation positive, it is
702 * removed after the bytes to Dseg conversion.
704 uint16_t n = (MLX5_WQE_DS(copy_b) - 1 + 3) / 4;
706 if (unlikely(max_wqe < n))
711 htonl(copy_b | MLX5_INLINE_SEG);
714 MLX5_WQE_DS(tso_header_sz) *
716 rte_memcpy((void *)raw,
717 (void *)&inl, sizeof(inl));
719 pkt_inline_sz += sizeof(inl);
721 rte_memcpy((void *)raw, (void *)addr, copy_b);
724 pkt_inline_sz += copy_b;
727 * 2 DWORDs consumed by the WQE header + ETH segment +
728 * the size of the inline part of the packet.
730 ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
732 if (ds % (MLX5_WQE_SIZE /
733 MLX5_WQE_DWORD_SIZE) == 0) {
734 if (unlikely(--max_wqe == 0))
736 dseg = (volatile rte_v128u32_t *)
737 tx_mlx5_wqe(txq, txq->wqe_ci +
740 dseg = (volatile rte_v128u32_t *)
742 (ds * MLX5_WQE_DWORD_SIZE));
745 } else if (!segs_n) {
748 /* dseg will be advance as part of next_seg */
749 dseg = (volatile rte_v128u32_t *)
751 ((ds - 1) * MLX5_WQE_DWORD_SIZE));
756 * No inline has been done in the packet, only the
757 * Ethernet Header as been stored.
759 dseg = (volatile rte_v128u32_t *)
760 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
763 /* Add the remaining packet as a simple ds. */
764 naddr = htonll(addr);
765 *dseg = (rte_v128u32_t){
767 txq_mp2mr(txq, txq_mb2mp(buf)),
780 * Spill on next WQE when the current one does not have
781 * enough room left. Size of WQE must a be a multiple
782 * of data segment size.
784 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
785 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
786 if (unlikely(--max_wqe == 0))
788 dseg = (volatile rte_v128u32_t *)
789 tx_mlx5_wqe(txq, txq->wqe_ci + ds / 4);
790 rte_prefetch0(tx_mlx5_wqe(txq,
791 txq->wqe_ci + ds / 4 + 1));
798 length = DATA_LEN(buf);
799 #ifdef MLX5_PMD_SOFT_COUNTERS
800 total_length += length;
802 /* Store segment information. */
803 naddr = htonll(rte_pktmbuf_mtod(buf, uintptr_t));
804 *dseg = (rte_v128u32_t){
806 txq_mp2mr(txq, txq_mb2mp(buf)),
810 elts_head = (elts_head + 1) & (elts_n - 1);
811 (*txq->elts)[elts_head] = buf;
813 /* Advance counter only if all segs are successfully posted. */
819 elts_head = (elts_head + 1) & (elts_n - 1);
822 /* Initialize known and common part of the WQE structure. */
824 wqe->ctrl = (rte_v128u32_t){
825 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_TSO),
826 htonl(txq->qp_num_8s | ds),
830 wqe->eseg = (rte_v128u32_t){
832 cs_flags | (htons(tso_segsz) << 16),
834 (ehdr << 16) | htons(tso_header_sz),
837 wqe->ctrl = (rte_v128u32_t){
838 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
839 htonl(txq->qp_num_8s | ds),
843 wqe->eseg = (rte_v128u32_t){
847 (ehdr << 16) | htons(pkt_inline_sz),
851 txq->wqe_ci += (ds + 3) / 4;
852 /* Save the last successful WQE for completion request */
853 last_wqe = (volatile struct mlx5_wqe_ctrl *)wqe;
854 #ifdef MLX5_PMD_SOFT_COUNTERS
855 /* Increment sent bytes counter. */
856 txq->stats.obytes += total_length;
858 } while (i < pkts_n);
859 /* Take a shortcut if nothing must be sent. */
860 if (unlikely((i + k) == 0))
862 txq->elts_head = (txq->elts_head + i + j) & (elts_n - 1);
863 /* Check whether completion threshold has been reached. */
864 comp = txq->elts_comp + i + j + k;
865 if (comp >= MLX5_TX_COMP_THRESH) {
866 /* Request completion on last WQE. */
867 last_wqe->ctrl2 = htonl(8);
868 /* Save elts_head in unused "immediate" field of WQE. */
869 last_wqe->ctrl3 = txq->elts_head;
872 txq->elts_comp = comp;
874 #ifdef MLX5_PMD_SOFT_COUNTERS
875 /* Increment sent packets counter. */
876 txq->stats.opackets += i;
878 /* Ring QP doorbell. */
879 mlx5_tx_dbrec(txq, (volatile struct mlx5_wqe *)last_wqe);
884 * Open a MPW session.
887 * Pointer to TX queue structure.
889 * Pointer to MPW session structure.
894 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
896 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
897 volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
898 (volatile struct mlx5_wqe_data_seg (*)[])
899 tx_mlx5_wqe(txq, idx + 1);
901 mpw->state = MLX5_MPW_STATE_OPENED;
905 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
906 mpw->wqe->eseg.mss = htons(length);
907 mpw->wqe->eseg.inline_hdr_sz = 0;
908 mpw->wqe->eseg.rsvd0 = 0;
909 mpw->wqe->eseg.rsvd1 = 0;
910 mpw->wqe->eseg.rsvd2 = 0;
911 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
912 (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
913 mpw->wqe->ctrl[2] = 0;
914 mpw->wqe->ctrl[3] = 0;
915 mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
916 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
917 mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
918 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
919 mpw->data.dseg[2] = &(*dseg)[0];
920 mpw->data.dseg[3] = &(*dseg)[1];
921 mpw->data.dseg[4] = &(*dseg)[2];
925 * Close a MPW session.
928 * Pointer to TX queue structure.
930 * Pointer to MPW session structure.
933 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
935 unsigned int num = mpw->pkts_n;
938 * Store size in multiple of 16 bytes. Control and Ethernet segments
941 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
942 mpw->state = MLX5_MPW_STATE_CLOSED;
947 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
948 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
952 * DPDK callback for TX with MPW support.
955 * Generic pointer to TX queue structure.
957 * Packets to transmit.
959 * Number of packets in array.
962 * Number of packets successfully transmitted (<= pkts_n).
965 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
967 struct txq *txq = (struct txq *)dpdk_txq;
968 uint16_t elts_head = txq->elts_head;
969 const unsigned int elts_n = 1 << txq->elts_n;
975 struct mlx5_mpw mpw = {
976 .state = MLX5_MPW_STATE_CLOSED,
979 if (unlikely(!pkts_n))
981 /* Prefetch first packet cacheline. */
982 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
983 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
984 /* Start processing. */
986 max = (elts_n - (elts_head - txq->elts_tail));
989 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
990 if (unlikely(!max_wqe))
993 struct rte_mbuf *buf = *(pkts++);
994 unsigned int elts_head_next;
996 unsigned int segs_n = buf->nb_segs;
997 uint32_t cs_flags = 0;
1000 * Make sure there is enough room to store this packet and
1001 * that one ring entry remains unused.
1004 if (max < segs_n + 1)
1006 /* Do not bother with large packets MPW cannot handle. */
1007 if (segs_n > MLX5_MPW_DSEG_MAX)
1011 /* Should we enable HW CKSUM offload */
1013 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1014 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1015 /* Retrieve packet information. */
1016 length = PKT_LEN(buf);
1018 /* Start new session if packet differs. */
1019 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
1020 ((mpw.len != length) ||
1022 (mpw.wqe->eseg.cs_flags != cs_flags)))
1023 mlx5_mpw_close(txq, &mpw);
1024 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1026 * Multi-Packet WQE consumes at most two WQE.
1027 * mlx5_mpw_new() expects to be able to use such
1030 if (unlikely(max_wqe < 2))
1033 mlx5_mpw_new(txq, &mpw, length);
1034 mpw.wqe->eseg.cs_flags = cs_flags;
1036 /* Multi-segment packets must be alone in their MPW. */
1037 assert((segs_n == 1) || (mpw.pkts_n == 0));
1038 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1042 volatile struct mlx5_wqe_data_seg *dseg;
1045 elts_head_next = (elts_head + 1) & (elts_n - 1);
1047 (*txq->elts)[elts_head] = buf;
1048 dseg = mpw.data.dseg[mpw.pkts_n];
1049 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1050 *dseg = (struct mlx5_wqe_data_seg){
1051 .byte_count = htonl(DATA_LEN(buf)),
1052 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1053 .addr = htonll(addr),
1055 elts_head = elts_head_next;
1056 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1057 length += DATA_LEN(buf);
1063 assert(length == mpw.len);
1064 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1065 mlx5_mpw_close(txq, &mpw);
1066 elts_head = elts_head_next;
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 unsigned int elts_n = 1 << txq->elts_n;
1190 unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
1191 struct mlx5_mpw mpw = {
1192 .state = MLX5_MPW_STATE_CLOSED,
1195 * Compute the maximum number of WQE which can be consumed by inline
1198 * - 1 control segment,
1199 * - 1 Ethernet segment,
1200 * - N Dseg from the inline request.
1202 const unsigned int wqe_inl_n =
1203 ((2 * MLX5_WQE_DWORD_SIZE +
1204 txq->max_inline * RTE_CACHE_LINE_SIZE) +
1205 RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
1207 if (unlikely(!pkts_n))
1209 /* Prefetch first packet cacheline. */
1210 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
1211 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
1212 /* Start processing. */
1214 max = (elts_n - (elts_head - txq->elts_tail));
1218 struct rte_mbuf *buf = *(pkts++);
1219 unsigned int elts_head_next;
1222 unsigned int segs_n = buf->nb_segs;
1223 uint32_t cs_flags = 0;
1226 * Make sure there is enough room to store this packet and
1227 * that one ring entry remains unused.
1230 if (max < segs_n + 1)
1232 /* Do not bother with large packets MPW cannot handle. */
1233 if (segs_n > MLX5_MPW_DSEG_MAX)
1238 * Compute max_wqe in case less WQE were consumed in previous
1241 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1242 /* Should we enable HW CKSUM offload */
1244 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1245 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1246 /* Retrieve packet information. */
1247 length = PKT_LEN(buf);
1248 /* Start new session if packet differs. */
1249 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1250 if ((mpw.len != length) ||
1252 (mpw.wqe->eseg.cs_flags != cs_flags))
1253 mlx5_mpw_close(txq, &mpw);
1254 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
1255 if ((mpw.len != length) ||
1257 (length > inline_room) ||
1258 (mpw.wqe->eseg.cs_flags != cs_flags)) {
1259 mlx5_mpw_inline_close(txq, &mpw);
1261 txq->max_inline * RTE_CACHE_LINE_SIZE;
1264 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1265 if ((segs_n != 1) ||
1266 (length > inline_room)) {
1268 * Multi-Packet WQE consumes at most two WQE.
1269 * mlx5_mpw_new() expects to be able to use
1272 if (unlikely(max_wqe < 2))
1275 mlx5_mpw_new(txq, &mpw, length);
1276 mpw.wqe->eseg.cs_flags = cs_flags;
1278 if (unlikely(max_wqe < wqe_inl_n))
1280 max_wqe -= wqe_inl_n;
1281 mlx5_mpw_inline_new(txq, &mpw, length);
1282 mpw.wqe->eseg.cs_flags = cs_flags;
1285 /* Multi-segment packets must be alone in their MPW. */
1286 assert((segs_n == 1) || (mpw.pkts_n == 0));
1287 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1288 assert(inline_room ==
1289 txq->max_inline * RTE_CACHE_LINE_SIZE);
1290 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1294 volatile struct mlx5_wqe_data_seg *dseg;
1297 (elts_head + 1) & (elts_n - 1);
1299 (*txq->elts)[elts_head] = buf;
1300 dseg = mpw.data.dseg[mpw.pkts_n];
1301 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1302 *dseg = (struct mlx5_wqe_data_seg){
1303 .byte_count = htonl(DATA_LEN(buf)),
1304 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1305 .addr = htonll(addr),
1307 elts_head = elts_head_next;
1308 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1309 length += DATA_LEN(buf);
1315 assert(length == mpw.len);
1316 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1317 mlx5_mpw_close(txq, &mpw);
1321 assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1322 assert(length <= inline_room);
1323 assert(length == DATA_LEN(buf));
1324 elts_head_next = (elts_head + 1) & (elts_n - 1);
1325 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1326 (*txq->elts)[elts_head] = buf;
1327 /* Maximum number of bytes before wrapping. */
1328 max = ((((uintptr_t)(txq->wqes)) +
1331 (uintptr_t)mpw.data.raw);
1333 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1336 mpw.data.raw = (volatile void *)txq->wqes;
1337 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1338 (void *)(addr + max),
1340 mpw.data.raw += length - max;
1342 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1348 (volatile void *)txq->wqes;
1350 mpw.data.raw += length;
1353 mpw.total_len += length;
1355 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1356 mlx5_mpw_inline_close(txq, &mpw);
1358 txq->max_inline * RTE_CACHE_LINE_SIZE;
1360 inline_room -= length;
1363 elts_head = elts_head_next;
1364 #ifdef MLX5_PMD_SOFT_COUNTERS
1365 /* Increment sent bytes counter. */
1366 txq->stats.obytes += length;
1370 /* Take a shortcut if nothing must be sent. */
1371 if (unlikely(i == 0))
1373 /* Check whether completion threshold has been reached. */
1374 /* "j" includes both packets and segments. */
1375 comp = txq->elts_comp + j;
1376 if (comp >= MLX5_TX_COMP_THRESH) {
1377 volatile struct mlx5_wqe *wqe = mpw.wqe;
1379 /* Request completion on last WQE. */
1380 wqe->ctrl[2] = htonl(8);
1381 /* Save elts_head in unused "immediate" field of WQE. */
1382 wqe->ctrl[3] = elts_head;
1385 txq->elts_comp = comp;
1387 #ifdef MLX5_PMD_SOFT_COUNTERS
1388 /* Increment sent packets counter. */
1389 txq->stats.opackets += i;
1391 /* Ring QP doorbell. */
1392 if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1393 mlx5_mpw_inline_close(txq, &mpw);
1394 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1395 mlx5_mpw_close(txq, &mpw);
1396 mlx5_tx_dbrec(txq, mpw.wqe);
1397 txq->elts_head = elts_head;
1402 * Open an Enhanced MPW session.
1405 * Pointer to TX queue structure.
1407 * Pointer to MPW session structure.
1412 mlx5_empw_new(struct txq *txq, struct mlx5_mpw *mpw, int padding)
1414 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
1416 mpw->state = MLX5_MPW_ENHANCED_STATE_OPENED;
1418 mpw->total_len = sizeof(struct mlx5_wqe);
1419 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
1420 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_ENHANCED_MPSW << 24) |
1421 (txq->wqe_ci << 8) |
1422 MLX5_OPCODE_ENHANCED_MPSW);
1423 mpw->wqe->ctrl[2] = 0;
1424 mpw->wqe->ctrl[3] = 0;
1425 memset((void *)(uintptr_t)&mpw->wqe->eseg, 0, MLX5_WQE_DWORD_SIZE);
1426 if (unlikely(padding)) {
1427 uintptr_t addr = (uintptr_t)(mpw->wqe + 1);
1429 /* Pad the first 2 DWORDs with zero-length inline header. */
1430 *(volatile uint32_t *)addr = htonl(MLX5_INLINE_SEG);
1431 *(volatile uint32_t *)(addr + MLX5_WQE_DWORD_SIZE) =
1432 htonl(MLX5_INLINE_SEG);
1433 mpw->total_len += 2 * MLX5_WQE_DWORD_SIZE;
1434 /* Start from the next WQEBB. */
1435 mpw->data.raw = (volatile void *)(tx_mlx5_wqe(txq, idx + 1));
1437 mpw->data.raw = (volatile void *)(mpw->wqe + 1);
1442 * Close an Enhanced MPW session.
1445 * Pointer to TX queue structure.
1447 * Pointer to MPW session structure.
1450 * Number of consumed WQEs.
1452 static inline uint16_t
1453 mlx5_empw_close(struct txq *txq, struct mlx5_mpw *mpw)
1457 /* Store size in multiple of 16 bytes. Control and Ethernet segments
1460 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(mpw->total_len));
1461 mpw->state = MLX5_MPW_STATE_CLOSED;
1462 ret = (mpw->total_len + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
1468 * DPDK callback for TX with Enhanced MPW support.
1471 * Generic pointer to TX queue structure.
1473 * Packets to transmit.
1475 * Number of packets in array.
1478 * Number of packets successfully transmitted (<= pkts_n).
1481 mlx5_tx_burst_empw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1483 struct txq *txq = (struct txq *)dpdk_txq;
1484 uint16_t elts_head = txq->elts_head;
1485 const unsigned int elts_n = 1 << txq->elts_n;
1488 unsigned int max_elts;
1490 unsigned int max_inline = txq->max_inline * RTE_CACHE_LINE_SIZE;
1491 unsigned int mpw_room = 0;
1492 unsigned int inl_pad = 0;
1494 struct mlx5_mpw mpw = {
1495 .state = MLX5_MPW_STATE_CLOSED,
1498 if (unlikely(!pkts_n))
1500 /* Start processing. */
1502 max_elts = (elts_n - (elts_head - txq->elts_tail));
1503 if (max_elts > elts_n)
1505 /* A CQE slot must always be available. */
1506 assert((1u << txq->cqe_n) - (txq->cq_pi - txq->cq_ci));
1507 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1508 if (unlikely(!max_wqe))
1511 struct rte_mbuf *buf = *(pkts++);
1512 unsigned int elts_head_next;
1516 unsigned int do_inline = 0; /* Whether inline is possible. */
1518 unsigned int segs_n = buf->nb_segs;
1519 uint32_t cs_flags = 0;
1522 * Make sure there is enough room to store this packet and
1523 * that one ring entry remains unused.
1526 if (max_elts - j < segs_n + 1)
1528 /* Do not bother with large packets MPW cannot handle. */
1529 if (segs_n > MLX5_MPW_DSEG_MAX)
1531 /* Should we enable HW CKSUM offload. */
1533 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1534 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1535 /* Retrieve packet information. */
1536 length = PKT_LEN(buf);
1537 /* Start new session if:
1538 * - multi-segment packet
1539 * - no space left even for a dseg
1540 * - next packet can be inlined with a new WQE
1542 * It can't be MLX5_MPW_STATE_OPENED as always have a single
1545 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED) {
1546 if ((segs_n != 1) ||
1547 (inl_pad + sizeof(struct mlx5_wqe_data_seg) >
1549 (length <= txq->inline_max_packet_sz &&
1550 inl_pad + sizeof(inl_hdr) + length >
1552 (mpw.wqe->eseg.cs_flags != cs_flags))
1553 max_wqe -= mlx5_empw_close(txq, &mpw);
1555 if (unlikely(mpw.state == MLX5_MPW_STATE_CLOSED)) {
1556 if (unlikely(segs_n != 1)) {
1557 /* Fall back to legacy MPW.
1558 * A MPW session consumes 2 WQEs at most to
1559 * include MLX5_MPW_DSEG_MAX pointers.
1561 if (unlikely(max_wqe < 2))
1563 mlx5_mpw_new(txq, &mpw, length);
1565 /* In Enhanced MPW, inline as much as the budget
1566 * is allowed. The remaining space is to be
1567 * filled with dsegs. If the title WQEBB isn't
1568 * padded, it will have 2 dsegs there.
1570 mpw_room = RTE_MIN(MLX5_WQE_SIZE_MAX,
1571 (max_inline ? max_inline :
1572 pkts_n * MLX5_WQE_DWORD_SIZE) +
1574 if (unlikely(max_wqe * MLX5_WQE_SIZE <
1577 /* Don't pad the title WQEBB to not waste WQ. */
1578 mlx5_empw_new(txq, &mpw, 0);
1579 mpw_room -= mpw.total_len;
1582 length <= txq->inline_max_packet_sz &&
1583 sizeof(inl_hdr) + length <= mpw_room &&
1586 mpw.wqe->eseg.cs_flags = cs_flags;
1588 /* Evaluate whether the next packet can be inlined.
1589 * Inlininig is possible when:
1590 * - length is less than configured value
1591 * - length fits for remaining space
1592 * - not required to fill the title WQEBB with dsegs
1595 length <= txq->inline_max_packet_sz &&
1596 inl_pad + sizeof(inl_hdr) + length <=
1598 (!txq->mpw_hdr_dseg ||
1599 mpw.total_len >= MLX5_WQE_SIZE);
1601 /* Multi-segment packets must be alone in their MPW. */
1602 assert((segs_n == 1) || (mpw.pkts_n == 0));
1603 if (unlikely(mpw.state == MLX5_MPW_STATE_OPENED)) {
1604 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1608 volatile struct mlx5_wqe_data_seg *dseg;
1611 (elts_head + 1) & (elts_n - 1);
1613 (*txq->elts)[elts_head] = buf;
1614 dseg = mpw.data.dseg[mpw.pkts_n];
1615 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1616 *dseg = (struct mlx5_wqe_data_seg){
1617 .byte_count = htonl(DATA_LEN(buf)),
1618 .lkey = txq_mp2mr(txq, txq_mb2mp(buf)),
1619 .addr = htonll(addr),
1621 elts_head = elts_head_next;
1622 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1623 length += DATA_LEN(buf);
1629 /* A multi-segmented packet takes one MPW session.
1630 * TODO: Pack more multi-segmented packets if possible.
1632 mlx5_mpw_close(txq, &mpw);
1637 } else if (do_inline) {
1638 /* Inline packet into WQE. */
1641 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1642 assert(length == DATA_LEN(buf));
1643 inl_hdr = htonl(length | MLX5_INLINE_SEG);
1644 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1645 mpw.data.raw = (volatile void *)
1646 ((uintptr_t)mpw.data.raw + inl_pad);
1647 max = tx_mlx5_wq_tailroom(txq,
1648 (void *)(uintptr_t)mpw.data.raw);
1649 /* Copy inline header. */
1650 mpw.data.raw = (volatile void *)
1652 (void *)(uintptr_t)mpw.data.raw,
1655 (void *)(uintptr_t)txq->wqes,
1657 max = tx_mlx5_wq_tailroom(txq,
1658 (void *)(uintptr_t)mpw.data.raw);
1659 /* Copy packet data. */
1660 mpw.data.raw = (volatile void *)
1662 (void *)(uintptr_t)mpw.data.raw,
1665 (void *)(uintptr_t)txq->wqes,
1668 mpw.total_len += (inl_pad + sizeof(inl_hdr) + length);
1669 /* No need to get completion as the entire packet is
1670 * copied to WQ. Free the buf right away.
1672 elts_head_next = elts_head;
1673 rte_pktmbuf_free_seg(buf);
1674 mpw_room -= (inl_pad + sizeof(inl_hdr) + length);
1675 /* Add pad in the next packet if any. */
1676 inl_pad = (((uintptr_t)mpw.data.raw +
1677 (MLX5_WQE_DWORD_SIZE - 1)) &
1678 ~(MLX5_WQE_DWORD_SIZE - 1)) -
1679 (uintptr_t)mpw.data.raw;
1681 /* No inline. Load a dseg of packet pointer. */
1682 volatile rte_v128u32_t *dseg;
1684 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1685 assert((inl_pad + sizeof(*dseg)) <= mpw_room);
1686 assert(length == DATA_LEN(buf));
1687 if (!tx_mlx5_wq_tailroom(txq,
1688 (void *)((uintptr_t)mpw.data.raw
1690 dseg = (volatile void *)txq->wqes;
1692 dseg = (volatile void *)
1693 ((uintptr_t)mpw.data.raw +
1695 elts_head_next = (elts_head + 1) & (elts_n - 1);
1696 (*txq->elts)[elts_head] = buf;
1697 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1698 for (n = 0; n * RTE_CACHE_LINE_SIZE < length; n++)
1699 rte_prefetch2((void *)(addr +
1700 n * RTE_CACHE_LINE_SIZE));
1701 naddr = htonll(addr);
1702 *dseg = (rte_v128u32_t) {
1704 txq_mp2mr(txq, txq_mb2mp(buf)),
1708 mpw.data.raw = (volatile void *)(dseg + 1);
1709 mpw.total_len += (inl_pad + sizeof(*dseg));
1712 mpw_room -= (inl_pad + sizeof(*dseg));
1715 elts_head = elts_head_next;
1716 #ifdef MLX5_PMD_SOFT_COUNTERS
1717 /* Increment sent bytes counter. */
1718 txq->stats.obytes += length;
1721 } while (i < pkts_n);
1722 /* Take a shortcut if nothing must be sent. */
1723 if (unlikely(i == 0))
1725 /* Check whether completion threshold has been reached. */
1726 if (txq->elts_comp + j >= MLX5_TX_COMP_THRESH ||
1727 (uint16_t)(txq->wqe_ci - txq->mpw_comp) >=
1728 (1 << txq->wqe_n) / MLX5_TX_COMP_THRESH_INLINE_DIV) {
1729 volatile struct mlx5_wqe *wqe = mpw.wqe;
1731 /* Request completion on last WQE. */
1732 wqe->ctrl[2] = htonl(8);
1733 /* Save elts_head in unused "immediate" field of WQE. */
1734 wqe->ctrl[3] = elts_head;
1736 txq->mpw_comp = txq->wqe_ci;
1739 txq->elts_comp += j;
1741 #ifdef MLX5_PMD_SOFT_COUNTERS
1742 /* Increment sent packets counter. */
1743 txq->stats.opackets += i;
1745 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED)
1746 mlx5_empw_close(txq, &mpw);
1747 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1748 mlx5_mpw_close(txq, &mpw);
1749 /* Ring QP doorbell. */
1750 mlx5_tx_dbrec(txq, mpw.wqe);
1751 txq->elts_head = elts_head;
1756 * Translate RX completion flags to packet type.
1761 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1764 * Packet type for struct rte_mbuf.
1766 static inline uint32_t
1767 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1770 uint16_t flags = ntohs(cqe->hdr_type_etc);
1772 if (cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) {
1775 MLX5_CQE_RX_IPV4_PACKET,
1776 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
1778 MLX5_CQE_RX_IPV6_PACKET,
1779 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
1780 pkt_type |= ((cqe->pkt_info & MLX5_CQE_RX_OUTER_PACKET) ?
1781 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN :
1782 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1786 MLX5_CQE_L3_HDR_TYPE_IPV6,
1787 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
1789 MLX5_CQE_L3_HDR_TYPE_IPV4,
1790 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
1796 * Get size of the next packet for a given CQE. For compressed CQEs, the
1797 * consumer index is updated only once all packets of the current one have
1801 * Pointer to RX queue.
1804 * @param[out] rss_hash
1805 * Packet RSS Hash result.
1808 * Packet size in bytes (0 if there is none), -1 in case of completion
1812 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1813 uint16_t cqe_cnt, uint32_t *rss_hash)
1815 struct rxq_zip *zip = &rxq->zip;
1816 uint16_t cqe_n = cqe_cnt + 1;
1820 /* Process compressed data in the CQE and mini arrays. */
1822 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1823 (volatile struct mlx5_mini_cqe8 (*)[8])
1824 (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt]);
1826 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1827 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1828 if ((++zip->ai & 7) == 0) {
1829 /* Invalidate consumed CQEs */
1832 while (idx != end) {
1833 (*rxq->cqes)[idx & cqe_cnt].op_own =
1834 MLX5_CQE_INVALIDATE;
1838 * Increment consumer index to skip the number of
1839 * CQEs consumed. Hardware leaves holes in the CQ
1840 * ring for software use.
1845 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1846 /* Invalidate the rest */
1850 while (idx != end) {
1851 (*rxq->cqes)[idx & cqe_cnt].op_own =
1852 MLX5_CQE_INVALIDATE;
1855 rxq->cq_ci = zip->cq_ci;
1858 /* No compressed data, get next CQE and verify if it is compressed. */
1863 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1864 if (unlikely(ret == 1))
1867 op_own = cqe->op_own;
1868 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1869 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1870 (volatile struct mlx5_mini_cqe8 (*)[8])
1871 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1874 /* Fix endianness. */
1875 zip->cqe_cnt = ntohl(cqe->byte_cnt);
1877 * Current mini array position is the one returned by
1880 * If completion comprises several mini arrays, as a
1881 * special case the second one is located 7 CQEs after
1882 * the initial CQE instead of 8 for subsequent ones.
1884 zip->ca = rxq->cq_ci;
1885 zip->na = zip->ca + 7;
1886 /* Compute the next non compressed CQE. */
1888 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1889 /* Get packet size to return. */
1890 len = ntohl((*mc)[0].byte_cnt);
1891 *rss_hash = ntohl((*mc)[0].rx_hash_result);
1893 /* Prefetch all the entries to be invalidated */
1896 while (idx != end) {
1897 rte_prefetch0(&(*rxq->cqes)[(idx) & cqe_cnt]);
1901 len = ntohl(cqe->byte_cnt);
1902 *rss_hash = ntohl(cqe->rx_hash_res);
1904 /* Error while receiving packet. */
1905 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1912 * Translate RX completion flags to offload flags.
1915 * Pointer to RX queue structure.
1920 * Offload flags (ol_flags) for struct rte_mbuf.
1922 static inline uint32_t
1923 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1925 uint32_t ol_flags = 0;
1926 uint16_t flags = ntohs(cqe->hdr_type_etc);
1930 MLX5_CQE_RX_L3_HDR_VALID,
1931 PKT_RX_IP_CKSUM_GOOD) |
1933 MLX5_CQE_RX_L4_HDR_VALID,
1934 PKT_RX_L4_CKSUM_GOOD);
1935 if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1938 MLX5_CQE_RX_L3_HDR_VALID,
1939 PKT_RX_IP_CKSUM_GOOD) |
1941 MLX5_CQE_RX_L4_HDR_VALID,
1942 PKT_RX_L4_CKSUM_GOOD);
1947 * DPDK callback for RX.
1950 * Generic pointer to RX queue structure.
1952 * Array to store received packets.
1954 * Maximum number of packets in array.
1957 * Number of packets successfully received (<= pkts_n).
1960 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1962 struct rxq *rxq = dpdk_rxq;
1963 const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1964 const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1965 const unsigned int sges_n = rxq->sges_n;
1966 struct rte_mbuf *pkt = NULL;
1967 struct rte_mbuf *seg = NULL;
1968 volatile struct mlx5_cqe *cqe =
1969 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1971 unsigned int rq_ci = rxq->rq_ci << sges_n;
1972 int len = 0; /* keep its value across iterations. */
1975 unsigned int idx = rq_ci & wqe_cnt;
1976 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1977 struct rte_mbuf *rep = (*rxq->elts)[idx];
1978 uint32_t rss_hash_res = 0;
1986 rep = rte_mbuf_raw_alloc(rxq->mp);
1987 if (unlikely(rep == NULL)) {
1988 ++rxq->stats.rx_nombuf;
1991 * no buffers before we even started,
1992 * bail out silently.
1996 while (pkt != seg) {
1997 assert(pkt != (*rxq->elts)[idx]);
2001 rte_mbuf_raw_free(pkt);
2007 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
2008 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
2011 rte_mbuf_raw_free(rep);
2014 if (unlikely(len == -1)) {
2015 /* RX error, packet is likely too large. */
2016 rte_mbuf_raw_free(rep);
2017 ++rxq->stats.idropped;
2021 assert(len >= (rxq->crc_present << 2));
2022 /* Update packet information. */
2023 pkt->packet_type = rxq_cq_to_pkt_type(cqe);
2025 if (rss_hash_res && rxq->rss_hash) {
2026 pkt->hash.rss = rss_hash_res;
2027 pkt->ol_flags = PKT_RX_RSS_HASH;
2030 MLX5_FLOW_MARK_IS_VALID(cqe->sop_drop_qpn)) {
2031 pkt->ol_flags |= PKT_RX_FDIR;
2032 if (cqe->sop_drop_qpn !=
2033 htonl(MLX5_FLOW_MARK_DEFAULT)) {
2034 uint32_t mark = cqe->sop_drop_qpn;
2036 pkt->ol_flags |= PKT_RX_FDIR_ID;
2038 mlx5_flow_mark_get(mark);
2041 if (rxq->csum | rxq->csum_l2tun)
2042 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)