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"
52 #include <rte_mempool.h>
53 #include <rte_prefetch.h>
54 #include <rte_common.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ether.h>
59 #include "mlx5_utils.h"
60 #include "mlx5_rxtx.h"
61 #include "mlx5_autoconf.h"
62 #include "mlx5_defs.h"
65 static __rte_always_inline uint32_t
66 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe);
68 static __rte_always_inline int
69 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
70 uint16_t cqe_cnt, uint32_t *rss_hash);
72 static __rte_always_inline uint32_t
73 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe);
75 uint32_t mlx5_ptype_table[] __rte_cache_aligned = {
76 [0xff] = RTE_PTYPE_ALL_MASK, /* Last entry for errored packet. */
80 * Build a table to translate Rx completion flags to packet type.
82 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
85 mlx5_set_ptype_table(void)
88 uint32_t (*p)[RTE_DIM(mlx5_ptype_table)] = &mlx5_ptype_table;
90 /* Last entry must not be overwritten, reserved for errored packet. */
91 for (i = 0; i < RTE_DIM(mlx5_ptype_table) - 1; ++i)
92 (*p)[i] = RTE_PTYPE_UNKNOWN;
94 * The index to the array should have:
95 * bit[1:0] = l3_hdr_type
96 * bit[4:2] = l4_hdr_type
99 * bit[7] = outer_l3_type
102 (*p)[0x01] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
103 RTE_PTYPE_L4_NONFRAG;
104 (*p)[0x02] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
105 RTE_PTYPE_L4_NONFRAG;
107 (*p)[0x21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
109 (*p)[0x22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
112 (*p)[0x05] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
114 (*p)[0x06] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
117 (*p)[0x09] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
119 (*p)[0x0a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
121 /* Repeat with outer_l3_type being set. Just in case. */
122 (*p)[0x81] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
123 RTE_PTYPE_L4_NONFRAG;
124 (*p)[0x82] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
125 RTE_PTYPE_L4_NONFRAG;
126 (*p)[0xa1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
128 (*p)[0xa2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
130 (*p)[0x85] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
132 (*p)[0x86] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
134 (*p)[0x89] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
136 (*p)[0x8a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
139 (*p)[0x41] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
140 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
141 RTE_PTYPE_INNER_L4_NONFRAG;
142 (*p)[0x42] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
143 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
144 RTE_PTYPE_INNER_L4_NONFRAG;
145 (*p)[0xc1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
146 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
147 RTE_PTYPE_INNER_L4_NONFRAG;
148 (*p)[0xc2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
149 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
150 RTE_PTYPE_INNER_L4_NONFRAG;
151 /* Tunneled - Fragmented */
152 (*p)[0x61] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
153 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
154 RTE_PTYPE_INNER_L4_FRAG;
155 (*p)[0x62] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
156 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
157 RTE_PTYPE_INNER_L4_FRAG;
158 (*p)[0xe1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
159 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
160 RTE_PTYPE_INNER_L4_FRAG;
161 (*p)[0xe2] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
162 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
163 RTE_PTYPE_INNER_L4_FRAG;
165 (*p)[0x45] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
166 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
168 (*p)[0x46] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
169 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
171 (*p)[0xc5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
172 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
174 (*p)[0xc6] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
175 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
178 (*p)[0x49] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
179 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
181 (*p)[0x4a] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
182 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
184 (*p)[0xc9] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
185 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
187 (*p)[0xca] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
188 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
193 * Return the size of tailroom of WQ.
196 * Pointer to TX queue structure.
198 * Pointer to tail of WQ.
204 tx_mlx5_wq_tailroom(struct txq *txq, void *addr)
207 tailroom = (uintptr_t)(txq->wqes) +
208 (1 << txq->wqe_n) * MLX5_WQE_SIZE -
214 * Copy data to tailroom of circular queue.
217 * Pointer to destination.
221 * Number of bytes to copy.
223 * Pointer to head of queue.
225 * Size of tailroom from dst.
228 * Pointer after copied data.
231 mlx5_copy_to_wq(void *dst, const void *src, size_t n,
232 void *base, size_t tailroom)
237 rte_memcpy(dst, src, tailroom);
238 rte_memcpy(base, (void *)((uintptr_t)src + tailroom),
240 ret = (uint8_t *)base + n - tailroom;
242 rte_memcpy(dst, src, n);
243 ret = (n == tailroom) ? base : (uint8_t *)dst + n;
249 * DPDK callback to check the status of a tx descriptor.
254 * The index of the descriptor in the ring.
257 * The status of the tx descriptor.
260 mlx5_tx_descriptor_status(void *tx_queue, uint16_t offset)
262 struct txq *txq = tx_queue;
265 mlx5_tx_complete(txq);
266 used = txq->elts_head - txq->elts_tail;
268 return RTE_ETH_TX_DESC_FULL;
269 return RTE_ETH_TX_DESC_DONE;
273 * DPDK callback to check the status of a rx descriptor.
278 * The index of the descriptor in the ring.
281 * The status of the tx descriptor.
284 mlx5_rx_descriptor_status(void *rx_queue, uint16_t offset)
286 struct rxq *rxq = rx_queue;
287 struct rxq_zip *zip = &rxq->zip;
288 volatile struct mlx5_cqe *cqe;
289 const unsigned int cqe_n = (1 << rxq->cqe_n);
290 const unsigned int cqe_cnt = cqe_n - 1;
294 /* if we are processing a compressed cqe */
296 used = zip->cqe_cnt - zip->ca;
302 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
303 while (check_cqe(cqe, cqe_n, cq_ci) == 0) {
307 op_own = cqe->op_own;
308 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED)
309 n = ntohl(cqe->byte_cnt);
314 cqe = &(*rxq->cqes)[cq_ci & cqe_cnt];
316 used = RTE_MIN(used, (1U << rxq->elts_n) - 1);
318 return RTE_ETH_RX_DESC_DONE;
319 return RTE_ETH_RX_DESC_AVAIL;
323 * DPDK callback for TX.
326 * Generic pointer to TX queue structure.
328 * Packets to transmit.
330 * Number of packets in array.
333 * Number of packets successfully transmitted (<= pkts_n).
336 mlx5_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
338 struct txq *txq = (struct txq *)dpdk_txq;
339 uint16_t elts_head = txq->elts_head;
340 const uint16_t elts_n = 1 << txq->elts_n;
341 const uint16_t elts_m = elts_n - 1;
346 unsigned int max_inline = txq->max_inline;
347 const unsigned int inline_en = !!max_inline && txq->inline_en;
350 volatile struct mlx5_wqe_v *wqe = NULL;
351 volatile struct mlx5_wqe_ctrl *last_wqe = NULL;
352 unsigned int segs_n = 0;
353 struct rte_mbuf *buf = NULL;
356 if (unlikely(!pkts_n))
358 /* Prefetch first packet cacheline. */
359 rte_prefetch0(*pkts);
360 /* Start processing. */
361 mlx5_tx_complete(txq);
362 max_elts = (elts_n - (elts_head - txq->elts_tail));
363 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
364 if (unlikely(!max_wqe))
367 volatile rte_v128u32_t *dseg = NULL;
370 unsigned int sg = 0; /* counter of additional segs attached. */
373 uint16_t pkt_inline_sz = MLX5_WQE_DWORD_SIZE + 2;
374 uint16_t tso_header_sz = 0;
376 uint8_t cs_flags = 0;
378 uint16_t tso_segsz = 0;
379 #ifdef MLX5_PMD_SOFT_COUNTERS
380 uint32_t total_length = 0;
385 segs_n = buf->nb_segs;
387 * Make sure there is enough room to store this packet and
388 * that one ring entry remains unused.
391 if (max_elts < segs_n)
395 if (unlikely(--max_wqe == 0))
397 wqe = (volatile struct mlx5_wqe_v *)
398 tx_mlx5_wqe(txq, txq->wqe_ci);
399 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
401 rte_prefetch0(*(pkts + 1));
402 addr = rte_pktmbuf_mtod(buf, uintptr_t);
403 length = DATA_LEN(buf);
404 ehdr = (((uint8_t *)addr)[1] << 8) |
405 ((uint8_t *)addr)[0];
406 #ifdef MLX5_PMD_SOFT_COUNTERS
407 total_length = length;
409 if (length < (MLX5_WQE_DWORD_SIZE + 2))
411 /* Update element. */
412 (*txq->elts)[elts_head & elts_m] = buf;
413 /* Prefetch next buffer data. */
416 rte_pktmbuf_mtod(*(pkts + 1), volatile void *));
417 /* Should we enable HW CKSUM offload */
419 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
420 const uint64_t is_tunneled = buf->ol_flags &
422 PKT_TX_TUNNEL_VXLAN);
424 if (is_tunneled && txq->tunnel_en) {
425 cs_flags = MLX5_ETH_WQE_L3_INNER_CSUM |
426 MLX5_ETH_WQE_L4_INNER_CSUM;
427 if (buf->ol_flags & PKT_TX_OUTER_IP_CKSUM)
428 cs_flags |= MLX5_ETH_WQE_L3_CSUM;
430 cs_flags = MLX5_ETH_WQE_L3_CSUM |
431 MLX5_ETH_WQE_L4_CSUM;
434 raw = ((uint8_t *)(uintptr_t)wqe) + 2 * MLX5_WQE_DWORD_SIZE;
435 /* Replace the Ethernet type by the VLAN if necessary. */
436 if (buf->ol_flags & PKT_TX_VLAN_PKT) {
437 uint32_t vlan = htonl(0x81000000 | buf->vlan_tci);
438 unsigned int len = 2 * ETHER_ADDR_LEN - 2;
442 /* Copy Destination and source mac address. */
443 memcpy((uint8_t *)raw, ((uint8_t *)addr), len);
445 memcpy((uint8_t *)raw + len, &vlan, sizeof(vlan));
446 /* Copy missing two bytes to end the DSeg. */
447 memcpy((uint8_t *)raw + len + sizeof(vlan),
448 ((uint8_t *)addr) + len, 2);
452 memcpy((uint8_t *)raw, ((uint8_t *)addr) + 2,
453 MLX5_WQE_DWORD_SIZE);
454 length -= pkt_inline_sz;
455 addr += pkt_inline_sz;
457 raw += MLX5_WQE_DWORD_SIZE;
459 tso = buf->ol_flags & PKT_TX_TCP_SEG;
461 uintptr_t end = (uintptr_t)
462 (((uintptr_t)txq->wqes) +
466 uint8_t vlan_sz = (buf->ol_flags &
467 PKT_TX_VLAN_PKT) ? 4 : 0;
468 const uint64_t is_tunneled =
471 PKT_TX_TUNNEL_VXLAN);
473 tso_header_sz = buf->l2_len + vlan_sz +
474 buf->l3_len + buf->l4_len;
475 tso_segsz = buf->tso_segsz;
477 if (is_tunneled && txq->tunnel_en) {
478 tso_header_sz += buf->outer_l2_len +
480 cs_flags |= MLX5_ETH_WQE_L4_INNER_CSUM;
482 cs_flags |= MLX5_ETH_WQE_L4_CSUM;
484 if (unlikely(tso_header_sz >
485 MLX5_MAX_TSO_HEADER))
487 copy_b = tso_header_sz - pkt_inline_sz;
488 /* First seg must contain all headers. */
489 assert(copy_b <= length);
491 ((end - (uintptr_t)raw) > copy_b)) {
492 uint16_t n = (MLX5_WQE_DS(copy_b) -
495 if (unlikely(max_wqe < n))
498 rte_memcpy((void *)raw,
499 (void *)addr, copy_b);
502 /* Include padding for TSO header. */
503 copy_b = MLX5_WQE_DS(copy_b) *
505 pkt_inline_sz += copy_b;
509 wqe->ctrl = (rte_v128u32_t){
510 htonl(txq->wqe_ci << 8),
511 htonl(txq->qp_num_8s | 1),
522 /* Inline if enough room. */
523 if (inline_en || tso) {
525 uintptr_t end = (uintptr_t)
526 (((uintptr_t)txq->wqes) +
527 (1 << txq->wqe_n) * MLX5_WQE_SIZE);
528 unsigned int inline_room = max_inline *
529 RTE_CACHE_LINE_SIZE -
530 (pkt_inline_sz - 2) -
532 uintptr_t addr_end = (addr + inline_room) &
533 ~(RTE_CACHE_LINE_SIZE - 1);
534 unsigned int copy_b = (addr_end > addr) ?
535 RTE_MIN((addr_end - addr), length) :
538 if (copy_b && ((end - (uintptr_t)raw) > copy_b)) {
540 * One Dseg remains in the current WQE. To
541 * keep the computation positive, it is
542 * removed after the bytes to Dseg conversion.
544 uint16_t n = (MLX5_WQE_DS(copy_b) - 1 + 3) / 4;
546 if (unlikely(max_wqe < n))
550 inl = htonl(copy_b | MLX5_INLINE_SEG);
551 rte_memcpy((void *)raw,
552 (void *)&inl, sizeof(inl));
554 pkt_inline_sz += sizeof(inl);
556 rte_memcpy((void *)raw, (void *)addr, copy_b);
559 pkt_inline_sz += copy_b;
562 * 2 DWORDs consumed by the WQE header + ETH segment +
563 * the size of the inline part of the packet.
565 ds = 2 + MLX5_WQE_DS(pkt_inline_sz - 2);
567 if (ds % (MLX5_WQE_SIZE /
568 MLX5_WQE_DWORD_SIZE) == 0) {
569 if (unlikely(--max_wqe == 0))
571 dseg = (volatile rte_v128u32_t *)
572 tx_mlx5_wqe(txq, txq->wqe_ci +
575 dseg = (volatile rte_v128u32_t *)
577 (ds * MLX5_WQE_DWORD_SIZE));
580 } else if (!segs_n) {
583 /* dseg will be advance as part of next_seg */
584 dseg = (volatile rte_v128u32_t *)
586 ((ds - 1) * MLX5_WQE_DWORD_SIZE));
591 * No inline has been done in the packet, only the
592 * Ethernet Header as been stored.
594 dseg = (volatile rte_v128u32_t *)
595 ((uintptr_t)wqe + (3 * MLX5_WQE_DWORD_SIZE));
598 /* Add the remaining packet as a simple ds. */
599 naddr = htonll(addr);
600 *dseg = (rte_v128u32_t){
602 mlx5_tx_mb2mr(txq, buf),
615 * Spill on next WQE when the current one does not have
616 * enough room left. Size of WQE must a be a multiple
617 * of data segment size.
619 assert(!(MLX5_WQE_SIZE % MLX5_WQE_DWORD_SIZE));
620 if (!(ds % (MLX5_WQE_SIZE / MLX5_WQE_DWORD_SIZE))) {
621 if (unlikely(--max_wqe == 0))
623 dseg = (volatile rte_v128u32_t *)
624 tx_mlx5_wqe(txq, txq->wqe_ci + ds / 4);
625 rte_prefetch0(tx_mlx5_wqe(txq,
626 txq->wqe_ci + ds / 4 + 1));
633 length = DATA_LEN(buf);
634 #ifdef MLX5_PMD_SOFT_COUNTERS
635 total_length += length;
637 /* Store segment information. */
638 naddr = htonll(rte_pktmbuf_mtod(buf, uintptr_t));
639 *dseg = (rte_v128u32_t){
641 mlx5_tx_mb2mr(txq, buf),
645 (*txq->elts)[++elts_head & elts_m] = buf;
647 /* Advance counter only if all segs are successfully posted. */
656 /* Initialize known and common part of the WQE structure. */
658 wqe->ctrl = (rte_v128u32_t){
659 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_TSO),
660 htonl(txq->qp_num_8s | ds),
664 wqe->eseg = (rte_v128u32_t){
666 cs_flags | (htons(tso_segsz) << 16),
668 (ehdr << 16) | htons(tso_header_sz),
671 wqe->ctrl = (rte_v128u32_t){
672 htonl((txq->wqe_ci << 8) | MLX5_OPCODE_SEND),
673 htonl(txq->qp_num_8s | ds),
677 wqe->eseg = (rte_v128u32_t){
681 (ehdr << 16) | htons(pkt_inline_sz),
685 txq->wqe_ci += (ds + 3) / 4;
686 /* Save the last successful WQE for completion request */
687 last_wqe = (volatile struct mlx5_wqe_ctrl *)wqe;
688 #ifdef MLX5_PMD_SOFT_COUNTERS
689 /* Increment sent bytes counter. */
690 txq->stats.obytes += total_length;
692 } while (i < pkts_n);
693 /* Take a shortcut if nothing must be sent. */
694 if (unlikely((i + k) == 0))
696 txq->elts_head += (i + j);
697 /* Check whether completion threshold has been reached. */
698 comp = txq->elts_comp + i + j + k;
699 if (comp >= MLX5_TX_COMP_THRESH) {
700 /* Request completion on last WQE. */
701 last_wqe->ctrl2 = htonl(8);
702 /* Save elts_head in unused "immediate" field of WQE. */
703 last_wqe->ctrl3 = txq->elts_head;
706 txq->elts_comp = comp;
708 #ifdef MLX5_PMD_SOFT_COUNTERS
709 /* Increment sent packets counter. */
710 txq->stats.opackets += i;
712 /* Ring QP doorbell. */
713 mlx5_tx_dbrec(txq, (volatile struct mlx5_wqe *)last_wqe);
718 * Open a MPW session.
721 * Pointer to TX queue structure.
723 * Pointer to MPW session structure.
728 mlx5_mpw_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
730 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
731 volatile struct mlx5_wqe_data_seg (*dseg)[MLX5_MPW_DSEG_MAX] =
732 (volatile struct mlx5_wqe_data_seg (*)[])
733 tx_mlx5_wqe(txq, idx + 1);
735 mpw->state = MLX5_MPW_STATE_OPENED;
739 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
740 mpw->wqe->eseg.mss = htons(length);
741 mpw->wqe->eseg.inline_hdr_sz = 0;
742 mpw->wqe->eseg.rsvd0 = 0;
743 mpw->wqe->eseg.rsvd1 = 0;
744 mpw->wqe->eseg.rsvd2 = 0;
745 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
746 (txq->wqe_ci << 8) | MLX5_OPCODE_TSO);
747 mpw->wqe->ctrl[2] = 0;
748 mpw->wqe->ctrl[3] = 0;
749 mpw->data.dseg[0] = (volatile struct mlx5_wqe_data_seg *)
750 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
751 mpw->data.dseg[1] = (volatile struct mlx5_wqe_data_seg *)
752 (((uintptr_t)mpw->wqe) + (3 * MLX5_WQE_DWORD_SIZE));
753 mpw->data.dseg[2] = &(*dseg)[0];
754 mpw->data.dseg[3] = &(*dseg)[1];
755 mpw->data.dseg[4] = &(*dseg)[2];
759 * Close a MPW session.
762 * Pointer to TX queue structure.
764 * Pointer to MPW session structure.
767 mlx5_mpw_close(struct txq *txq, struct mlx5_mpw *mpw)
769 unsigned int num = mpw->pkts_n;
772 * Store size in multiple of 16 bytes. Control and Ethernet segments
775 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | (2 + num));
776 mpw->state = MLX5_MPW_STATE_CLOSED;
781 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
782 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
786 * DPDK callback for TX with MPW support.
789 * Generic pointer to TX queue structure.
791 * Packets to transmit.
793 * Number of packets in array.
796 * Number of packets successfully transmitted (<= pkts_n).
799 mlx5_tx_burst_mpw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
801 struct txq *txq = (struct txq *)dpdk_txq;
802 uint16_t elts_head = txq->elts_head;
803 const uint16_t elts_n = 1 << txq->elts_n;
804 const uint16_t elts_m = elts_n - 1;
810 struct mlx5_mpw mpw = {
811 .state = MLX5_MPW_STATE_CLOSED,
814 if (unlikely(!pkts_n))
816 /* Prefetch first packet cacheline. */
817 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
818 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
819 /* Start processing. */
820 mlx5_tx_complete(txq);
821 max_elts = (elts_n - (elts_head - txq->elts_tail));
822 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
823 if (unlikely(!max_wqe))
826 struct rte_mbuf *buf = *(pkts++);
828 unsigned int segs_n = buf->nb_segs;
829 uint32_t cs_flags = 0;
832 * Make sure there is enough room to store this packet and
833 * that one ring entry remains unused.
836 if (max_elts < segs_n)
838 /* Do not bother with large packets MPW cannot handle. */
839 if (segs_n > MLX5_MPW_DSEG_MAX)
843 /* Should we enable HW CKSUM offload */
845 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
846 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
847 /* Retrieve packet information. */
848 length = PKT_LEN(buf);
850 /* Start new session if packet differs. */
851 if ((mpw.state == MLX5_MPW_STATE_OPENED) &&
852 ((mpw.len != length) ||
854 (mpw.wqe->eseg.cs_flags != cs_flags)))
855 mlx5_mpw_close(txq, &mpw);
856 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
858 * Multi-Packet WQE consumes at most two WQE.
859 * mlx5_mpw_new() expects to be able to use such
862 if (unlikely(max_wqe < 2))
865 mlx5_mpw_new(txq, &mpw, length);
866 mpw.wqe->eseg.cs_flags = cs_flags;
868 /* Multi-segment packets must be alone in their MPW. */
869 assert((segs_n == 1) || (mpw.pkts_n == 0));
870 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
874 volatile struct mlx5_wqe_data_seg *dseg;
878 (*txq->elts)[elts_head++ & elts_m] = buf;
879 dseg = mpw.data.dseg[mpw.pkts_n];
880 addr = rte_pktmbuf_mtod(buf, uintptr_t);
881 *dseg = (struct mlx5_wqe_data_seg){
882 .byte_count = htonl(DATA_LEN(buf)),
883 .lkey = mlx5_tx_mb2mr(txq, buf),
884 .addr = htonll(addr),
886 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
887 length += DATA_LEN(buf);
893 assert(length == mpw.len);
894 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
895 mlx5_mpw_close(txq, &mpw);
896 #ifdef MLX5_PMD_SOFT_COUNTERS
897 /* Increment sent bytes counter. */
898 txq->stats.obytes += length;
902 /* Take a shortcut if nothing must be sent. */
903 if (unlikely(i == 0))
905 /* Check whether completion threshold has been reached. */
906 /* "j" includes both packets and segments. */
907 comp = txq->elts_comp + j;
908 if (comp >= MLX5_TX_COMP_THRESH) {
909 volatile struct mlx5_wqe *wqe = mpw.wqe;
911 /* Request completion on last WQE. */
912 wqe->ctrl[2] = htonl(8);
913 /* Save elts_head in unused "immediate" field of WQE. */
914 wqe->ctrl[3] = elts_head;
917 txq->elts_comp = comp;
919 #ifdef MLX5_PMD_SOFT_COUNTERS
920 /* Increment sent packets counter. */
921 txq->stats.opackets += i;
923 /* Ring QP doorbell. */
924 if (mpw.state == MLX5_MPW_STATE_OPENED)
925 mlx5_mpw_close(txq, &mpw);
926 mlx5_tx_dbrec(txq, mpw.wqe);
927 txq->elts_head = elts_head;
932 * Open a MPW inline session.
935 * Pointer to TX queue structure.
937 * Pointer to MPW session structure.
942 mlx5_mpw_inline_new(struct txq *txq, struct mlx5_mpw *mpw, uint32_t length)
944 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
945 struct mlx5_wqe_inl_small *inl;
947 mpw->state = MLX5_MPW_INL_STATE_OPENED;
951 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
952 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_MPW << 24) |
955 mpw->wqe->ctrl[2] = 0;
956 mpw->wqe->ctrl[3] = 0;
957 mpw->wqe->eseg.mss = htons(length);
958 mpw->wqe->eseg.inline_hdr_sz = 0;
959 mpw->wqe->eseg.cs_flags = 0;
960 mpw->wqe->eseg.rsvd0 = 0;
961 mpw->wqe->eseg.rsvd1 = 0;
962 mpw->wqe->eseg.rsvd2 = 0;
963 inl = (struct mlx5_wqe_inl_small *)
964 (((uintptr_t)mpw->wqe) + 2 * MLX5_WQE_DWORD_SIZE);
965 mpw->data.raw = (uint8_t *)&inl->raw;
969 * Close a MPW inline session.
972 * Pointer to TX queue structure.
974 * Pointer to MPW session structure.
977 mlx5_mpw_inline_close(struct txq *txq, struct mlx5_mpw *mpw)
980 struct mlx5_wqe_inl_small *inl = (struct mlx5_wqe_inl_small *)
981 (((uintptr_t)mpw->wqe) + (2 * MLX5_WQE_DWORD_SIZE));
983 size = MLX5_WQE_SIZE - MLX5_MWQE64_INL_DATA + mpw->total_len;
985 * Store size in multiple of 16 bytes. Control and Ethernet segments
988 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(size));
989 mpw->state = MLX5_MPW_STATE_CLOSED;
990 inl->byte_cnt = htonl(mpw->total_len | MLX5_INLINE_SEG);
991 txq->wqe_ci += (size + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
995 * DPDK callback for TX with MPW inline support.
998 * Generic pointer to TX queue structure.
1000 * Packets to transmit.
1002 * Number of packets in array.
1005 * Number of packets successfully transmitted (<= pkts_n).
1008 mlx5_tx_burst_mpw_inline(void *dpdk_txq, struct rte_mbuf **pkts,
1011 struct txq *txq = (struct txq *)dpdk_txq;
1012 uint16_t elts_head = txq->elts_head;
1013 const uint16_t elts_n = 1 << txq->elts_n;
1014 const uint16_t elts_m = elts_n - 1;
1020 unsigned int inline_room = txq->max_inline * RTE_CACHE_LINE_SIZE;
1021 struct mlx5_mpw mpw = {
1022 .state = MLX5_MPW_STATE_CLOSED,
1025 * Compute the maximum number of WQE which can be consumed by inline
1028 * - 1 control segment,
1029 * - 1 Ethernet segment,
1030 * - N Dseg from the inline request.
1032 const unsigned int wqe_inl_n =
1033 ((2 * MLX5_WQE_DWORD_SIZE +
1034 txq->max_inline * RTE_CACHE_LINE_SIZE) +
1035 RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
1037 if (unlikely(!pkts_n))
1039 /* Prefetch first packet cacheline. */
1040 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci));
1041 rte_prefetch0(tx_mlx5_wqe(txq, txq->wqe_ci + 1));
1042 /* Start processing. */
1043 mlx5_tx_complete(txq);
1044 max_elts = (elts_n - (elts_head - txq->elts_tail));
1046 struct rte_mbuf *buf = *(pkts++);
1049 unsigned int segs_n = buf->nb_segs;
1050 uint32_t cs_flags = 0;
1053 * Make sure there is enough room to store this packet and
1054 * that one ring entry remains unused.
1057 if (max_elts < segs_n)
1059 /* Do not bother with large packets MPW cannot handle. */
1060 if (segs_n > MLX5_MPW_DSEG_MAX)
1065 * Compute max_wqe in case less WQE were consumed in previous
1068 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1069 /* Should we enable HW CKSUM offload */
1071 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1072 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1073 /* Retrieve packet information. */
1074 length = PKT_LEN(buf);
1075 /* Start new session if packet differs. */
1076 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1077 if ((mpw.len != length) ||
1079 (mpw.wqe->eseg.cs_flags != cs_flags))
1080 mlx5_mpw_close(txq, &mpw);
1081 } else if (mpw.state == MLX5_MPW_INL_STATE_OPENED) {
1082 if ((mpw.len != length) ||
1084 (length > inline_room) ||
1085 (mpw.wqe->eseg.cs_flags != cs_flags)) {
1086 mlx5_mpw_inline_close(txq, &mpw);
1088 txq->max_inline * RTE_CACHE_LINE_SIZE;
1091 if (mpw.state == MLX5_MPW_STATE_CLOSED) {
1092 if ((segs_n != 1) ||
1093 (length > inline_room)) {
1095 * Multi-Packet WQE consumes at most two WQE.
1096 * mlx5_mpw_new() expects to be able to use
1099 if (unlikely(max_wqe < 2))
1102 mlx5_mpw_new(txq, &mpw, length);
1103 mpw.wqe->eseg.cs_flags = cs_flags;
1105 if (unlikely(max_wqe < wqe_inl_n))
1107 max_wqe -= wqe_inl_n;
1108 mlx5_mpw_inline_new(txq, &mpw, length);
1109 mpw.wqe->eseg.cs_flags = cs_flags;
1112 /* Multi-segment packets must be alone in their MPW. */
1113 assert((segs_n == 1) || (mpw.pkts_n == 0));
1114 if (mpw.state == MLX5_MPW_STATE_OPENED) {
1115 assert(inline_room ==
1116 txq->max_inline * RTE_CACHE_LINE_SIZE);
1117 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1121 volatile struct mlx5_wqe_data_seg *dseg;
1124 (*txq->elts)[elts_head++ & elts_m] = buf;
1125 dseg = mpw.data.dseg[mpw.pkts_n];
1126 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1127 *dseg = (struct mlx5_wqe_data_seg){
1128 .byte_count = htonl(DATA_LEN(buf)),
1129 .lkey = mlx5_tx_mb2mr(txq, buf),
1130 .addr = htonll(addr),
1132 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1133 length += DATA_LEN(buf);
1139 assert(length == mpw.len);
1140 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX)
1141 mlx5_mpw_close(txq, &mpw);
1145 assert(mpw.state == MLX5_MPW_INL_STATE_OPENED);
1146 assert(length <= inline_room);
1147 assert(length == DATA_LEN(buf));
1148 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1149 (*txq->elts)[elts_head++ & elts_m] = buf;
1150 /* Maximum number of bytes before wrapping. */
1151 max = ((((uintptr_t)(txq->wqes)) +
1154 (uintptr_t)mpw.data.raw);
1156 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1159 mpw.data.raw = (volatile void *)txq->wqes;
1160 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1161 (void *)(addr + max),
1163 mpw.data.raw += length - max;
1165 rte_memcpy((void *)(uintptr_t)mpw.data.raw,
1171 (volatile void *)txq->wqes;
1173 mpw.data.raw += length;
1176 mpw.total_len += length;
1178 if (mpw.pkts_n == MLX5_MPW_DSEG_MAX) {
1179 mlx5_mpw_inline_close(txq, &mpw);
1181 txq->max_inline * RTE_CACHE_LINE_SIZE;
1183 inline_room -= length;
1186 #ifdef MLX5_PMD_SOFT_COUNTERS
1187 /* Increment sent bytes counter. */
1188 txq->stats.obytes += length;
1192 /* Take a shortcut if nothing must be sent. */
1193 if (unlikely(i == 0))
1195 /* Check whether completion threshold has been reached. */
1196 /* "j" includes both packets and segments. */
1197 comp = txq->elts_comp + j;
1198 if (comp >= MLX5_TX_COMP_THRESH) {
1199 volatile struct mlx5_wqe *wqe = mpw.wqe;
1201 /* Request completion on last WQE. */
1202 wqe->ctrl[2] = htonl(8);
1203 /* Save elts_head in unused "immediate" field of WQE. */
1204 wqe->ctrl[3] = elts_head;
1207 txq->elts_comp = comp;
1209 #ifdef MLX5_PMD_SOFT_COUNTERS
1210 /* Increment sent packets counter. */
1211 txq->stats.opackets += i;
1213 /* Ring QP doorbell. */
1214 if (mpw.state == MLX5_MPW_INL_STATE_OPENED)
1215 mlx5_mpw_inline_close(txq, &mpw);
1216 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1217 mlx5_mpw_close(txq, &mpw);
1218 mlx5_tx_dbrec(txq, mpw.wqe);
1219 txq->elts_head = elts_head;
1224 * Open an Enhanced MPW session.
1227 * Pointer to TX queue structure.
1229 * Pointer to MPW session structure.
1234 mlx5_empw_new(struct txq *txq, struct mlx5_mpw *mpw, int padding)
1236 uint16_t idx = txq->wqe_ci & ((1 << txq->wqe_n) - 1);
1238 mpw->state = MLX5_MPW_ENHANCED_STATE_OPENED;
1240 mpw->total_len = sizeof(struct mlx5_wqe);
1241 mpw->wqe = (volatile struct mlx5_wqe *)tx_mlx5_wqe(txq, idx);
1242 mpw->wqe->ctrl[0] = htonl((MLX5_OPC_MOD_ENHANCED_MPSW << 24) |
1243 (txq->wqe_ci << 8) |
1244 MLX5_OPCODE_ENHANCED_MPSW);
1245 mpw->wqe->ctrl[2] = 0;
1246 mpw->wqe->ctrl[3] = 0;
1247 memset((void *)(uintptr_t)&mpw->wqe->eseg, 0, MLX5_WQE_DWORD_SIZE);
1248 if (unlikely(padding)) {
1249 uintptr_t addr = (uintptr_t)(mpw->wqe + 1);
1251 /* Pad the first 2 DWORDs with zero-length inline header. */
1252 *(volatile uint32_t *)addr = htonl(MLX5_INLINE_SEG);
1253 *(volatile uint32_t *)(addr + MLX5_WQE_DWORD_SIZE) =
1254 htonl(MLX5_INLINE_SEG);
1255 mpw->total_len += 2 * MLX5_WQE_DWORD_SIZE;
1256 /* Start from the next WQEBB. */
1257 mpw->data.raw = (volatile void *)(tx_mlx5_wqe(txq, idx + 1));
1259 mpw->data.raw = (volatile void *)(mpw->wqe + 1);
1264 * Close an Enhanced MPW session.
1267 * Pointer to TX queue structure.
1269 * Pointer to MPW session structure.
1272 * Number of consumed WQEs.
1274 static inline uint16_t
1275 mlx5_empw_close(struct txq *txq, struct mlx5_mpw *mpw)
1279 /* Store size in multiple of 16 bytes. Control and Ethernet segments
1282 mpw->wqe->ctrl[1] = htonl(txq->qp_num_8s | MLX5_WQE_DS(mpw->total_len));
1283 mpw->state = MLX5_MPW_STATE_CLOSED;
1284 ret = (mpw->total_len + (MLX5_WQE_SIZE - 1)) / MLX5_WQE_SIZE;
1290 * DPDK callback for TX with Enhanced MPW support.
1293 * Generic pointer to TX queue structure.
1295 * Packets to transmit.
1297 * Number of packets in array.
1300 * Number of packets successfully transmitted (<= pkts_n).
1303 mlx5_tx_burst_empw(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1305 struct txq *txq = (struct txq *)dpdk_txq;
1306 uint16_t elts_head = txq->elts_head;
1307 const uint16_t elts_n = 1 << txq->elts_n;
1308 const uint16_t elts_m = elts_n - 1;
1313 unsigned int max_inline = txq->max_inline * RTE_CACHE_LINE_SIZE;
1314 unsigned int mpw_room = 0;
1315 unsigned int inl_pad = 0;
1317 struct mlx5_mpw mpw = {
1318 .state = MLX5_MPW_STATE_CLOSED,
1321 if (unlikely(!pkts_n))
1323 /* Start processing. */
1324 mlx5_tx_complete(txq);
1325 max_elts = (elts_n - (elts_head - txq->elts_tail));
1326 /* A CQE slot must always be available. */
1327 assert((1u << txq->cqe_n) - (txq->cq_pi - txq->cq_ci));
1328 max_wqe = (1u << txq->wqe_n) - (txq->wqe_ci - txq->wqe_pi);
1329 if (unlikely(!max_wqe))
1332 struct rte_mbuf *buf = *(pkts++);
1336 unsigned int do_inline = 0; /* Whether inline is possible. */
1338 unsigned int segs_n = buf->nb_segs;
1339 uint32_t cs_flags = 0;
1342 * Make sure there is enough room to store this packet and
1343 * that one ring entry remains unused.
1346 if (max_elts - j < segs_n)
1348 /* Do not bother with large packets MPW cannot handle. */
1349 if (segs_n > MLX5_MPW_DSEG_MAX)
1351 /* Should we enable HW CKSUM offload. */
1353 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))
1354 cs_flags = MLX5_ETH_WQE_L3_CSUM | MLX5_ETH_WQE_L4_CSUM;
1355 /* Retrieve packet information. */
1356 length = PKT_LEN(buf);
1357 /* Start new session if:
1358 * - multi-segment packet
1359 * - no space left even for a dseg
1360 * - next packet can be inlined with a new WQE
1362 * It can't be MLX5_MPW_STATE_OPENED as always have a single
1365 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED) {
1366 if ((segs_n != 1) ||
1367 (inl_pad + sizeof(struct mlx5_wqe_data_seg) >
1369 (length <= txq->inline_max_packet_sz &&
1370 inl_pad + sizeof(inl_hdr) + length >
1372 (mpw.wqe->eseg.cs_flags != cs_flags))
1373 max_wqe -= mlx5_empw_close(txq, &mpw);
1375 if (unlikely(mpw.state == MLX5_MPW_STATE_CLOSED)) {
1376 if (unlikely(segs_n != 1)) {
1377 /* Fall back to legacy MPW.
1378 * A MPW session consumes 2 WQEs at most to
1379 * include MLX5_MPW_DSEG_MAX pointers.
1381 if (unlikely(max_wqe < 2))
1383 mlx5_mpw_new(txq, &mpw, length);
1385 /* In Enhanced MPW, inline as much as the budget
1386 * is allowed. The remaining space is to be
1387 * filled with dsegs. If the title WQEBB isn't
1388 * padded, it will have 2 dsegs there.
1390 mpw_room = RTE_MIN(MLX5_WQE_SIZE_MAX,
1391 (max_inline ? max_inline :
1392 pkts_n * MLX5_WQE_DWORD_SIZE) +
1394 if (unlikely(max_wqe * MLX5_WQE_SIZE <
1397 /* Don't pad the title WQEBB to not waste WQ. */
1398 mlx5_empw_new(txq, &mpw, 0);
1399 mpw_room -= mpw.total_len;
1402 length <= txq->inline_max_packet_sz &&
1403 sizeof(inl_hdr) + length <= mpw_room &&
1406 mpw.wqe->eseg.cs_flags = cs_flags;
1408 /* Evaluate whether the next packet can be inlined.
1409 * Inlininig is possible when:
1410 * - length is less than configured value
1411 * - length fits for remaining space
1412 * - not required to fill the title WQEBB with dsegs
1415 length <= txq->inline_max_packet_sz &&
1416 inl_pad + sizeof(inl_hdr) + length <=
1418 (!txq->mpw_hdr_dseg ||
1419 mpw.total_len >= MLX5_WQE_SIZE);
1421 /* Multi-segment packets must be alone in their MPW. */
1422 assert((segs_n == 1) || (mpw.pkts_n == 0));
1423 if (unlikely(mpw.state == MLX5_MPW_STATE_OPENED)) {
1424 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1428 volatile struct mlx5_wqe_data_seg *dseg;
1431 (*txq->elts)[elts_head++ & elts_m] = buf;
1432 dseg = mpw.data.dseg[mpw.pkts_n];
1433 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1434 *dseg = (struct mlx5_wqe_data_seg){
1435 .byte_count = htonl(DATA_LEN(buf)),
1436 .lkey = mlx5_tx_mb2mr(txq, buf),
1437 .addr = htonll(addr),
1439 #if defined(MLX5_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1440 length += DATA_LEN(buf);
1446 /* A multi-segmented packet takes one MPW session.
1447 * TODO: Pack more multi-segmented packets if possible.
1449 mlx5_mpw_close(txq, &mpw);
1454 } else if (do_inline) {
1455 /* Inline packet into WQE. */
1458 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1459 assert(length == DATA_LEN(buf));
1460 inl_hdr = htonl(length | MLX5_INLINE_SEG);
1461 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1462 mpw.data.raw = (volatile void *)
1463 ((uintptr_t)mpw.data.raw + inl_pad);
1464 max = tx_mlx5_wq_tailroom(txq,
1465 (void *)(uintptr_t)mpw.data.raw);
1466 /* Copy inline header. */
1467 mpw.data.raw = (volatile void *)
1469 (void *)(uintptr_t)mpw.data.raw,
1472 (void *)(uintptr_t)txq->wqes,
1474 max = tx_mlx5_wq_tailroom(txq,
1475 (void *)(uintptr_t)mpw.data.raw);
1476 /* Copy packet data. */
1477 mpw.data.raw = (volatile void *)
1479 (void *)(uintptr_t)mpw.data.raw,
1482 (void *)(uintptr_t)txq->wqes,
1485 mpw.total_len += (inl_pad + sizeof(inl_hdr) + length);
1486 /* No need to get completion as the entire packet is
1487 * copied to WQ. Free the buf right away.
1489 rte_pktmbuf_free_seg(buf);
1490 mpw_room -= (inl_pad + sizeof(inl_hdr) + length);
1491 /* Add pad in the next packet if any. */
1492 inl_pad = (((uintptr_t)mpw.data.raw +
1493 (MLX5_WQE_DWORD_SIZE - 1)) &
1494 ~(MLX5_WQE_DWORD_SIZE - 1)) -
1495 (uintptr_t)mpw.data.raw;
1497 /* No inline. Load a dseg of packet pointer. */
1498 volatile rte_v128u32_t *dseg;
1500 assert(mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED);
1501 assert((inl_pad + sizeof(*dseg)) <= mpw_room);
1502 assert(length == DATA_LEN(buf));
1503 if (!tx_mlx5_wq_tailroom(txq,
1504 (void *)((uintptr_t)mpw.data.raw
1506 dseg = (volatile void *)txq->wqes;
1508 dseg = (volatile void *)
1509 ((uintptr_t)mpw.data.raw +
1511 (*txq->elts)[elts_head++ & elts_m] = buf;
1512 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1513 for (n = 0; n * RTE_CACHE_LINE_SIZE < length; n++)
1514 rte_prefetch2((void *)(addr +
1515 n * RTE_CACHE_LINE_SIZE));
1516 naddr = htonll(addr);
1517 *dseg = (rte_v128u32_t) {
1519 mlx5_tx_mb2mr(txq, buf),
1523 mpw.data.raw = (volatile void *)(dseg + 1);
1524 mpw.total_len += (inl_pad + sizeof(*dseg));
1527 mpw_room -= (inl_pad + sizeof(*dseg));
1530 #ifdef MLX5_PMD_SOFT_COUNTERS
1531 /* Increment sent bytes counter. */
1532 txq->stats.obytes += length;
1535 } while (i < pkts_n);
1536 /* Take a shortcut if nothing must be sent. */
1537 if (unlikely(i == 0))
1539 /* Check whether completion threshold has been reached. */
1540 if (txq->elts_comp + j >= MLX5_TX_COMP_THRESH ||
1541 (uint16_t)(txq->wqe_ci - txq->mpw_comp) >=
1542 (1 << txq->wqe_n) / MLX5_TX_COMP_THRESH_INLINE_DIV) {
1543 volatile struct mlx5_wqe *wqe = mpw.wqe;
1545 /* Request completion on last WQE. */
1546 wqe->ctrl[2] = htonl(8);
1547 /* Save elts_head in unused "immediate" field of WQE. */
1548 wqe->ctrl[3] = elts_head;
1550 txq->mpw_comp = txq->wqe_ci;
1553 txq->elts_comp += j;
1555 #ifdef MLX5_PMD_SOFT_COUNTERS
1556 /* Increment sent packets counter. */
1557 txq->stats.opackets += i;
1559 if (mpw.state == MLX5_MPW_ENHANCED_STATE_OPENED)
1560 mlx5_empw_close(txq, &mpw);
1561 else if (mpw.state == MLX5_MPW_STATE_OPENED)
1562 mlx5_mpw_close(txq, &mpw);
1563 /* Ring QP doorbell. */
1564 mlx5_tx_dbrec(txq, mpw.wqe);
1565 txq->elts_head = elts_head;
1570 * Translate RX completion flags to packet type.
1575 * @note: fix mlx5_dev_supported_ptypes_get() if any change here.
1578 * Packet type for struct rte_mbuf.
1580 static inline uint32_t
1581 rxq_cq_to_pkt_type(volatile struct mlx5_cqe *cqe)
1584 uint8_t pinfo = cqe->pkt_info;
1585 uint16_t ptype = cqe->hdr_type_etc;
1588 * The index to the array should have:
1589 * bit[1:0] = l3_hdr_type
1590 * bit[4:2] = l4_hdr_type
1593 * bit[7] = outer_l3_type
1595 idx = ((pinfo & 0x3) << 6) | ((ptype & 0xfc00) >> 10);
1596 return mlx5_ptype_table[idx];
1600 * Get size of the next packet for a given CQE. For compressed CQEs, the
1601 * consumer index is updated only once all packets of the current one have
1605 * Pointer to RX queue.
1608 * @param[out] rss_hash
1609 * Packet RSS Hash result.
1612 * Packet size in bytes (0 if there is none), -1 in case of completion
1616 mlx5_rx_poll_len(struct rxq *rxq, volatile struct mlx5_cqe *cqe,
1617 uint16_t cqe_cnt, uint32_t *rss_hash)
1619 struct rxq_zip *zip = &rxq->zip;
1620 uint16_t cqe_n = cqe_cnt + 1;
1624 /* Process compressed data in the CQE and mini arrays. */
1626 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1627 (volatile struct mlx5_mini_cqe8 (*)[8])
1628 (uintptr_t)(&(*rxq->cqes)[zip->ca & cqe_cnt].pkt_info);
1630 len = ntohl((*mc)[zip->ai & 7].byte_cnt);
1631 *rss_hash = ntohl((*mc)[zip->ai & 7].rx_hash_result);
1632 if ((++zip->ai & 7) == 0) {
1633 /* Invalidate consumed CQEs */
1636 while (idx != end) {
1637 (*rxq->cqes)[idx & cqe_cnt].op_own =
1638 MLX5_CQE_INVALIDATE;
1642 * Increment consumer index to skip the number of
1643 * CQEs consumed. Hardware leaves holes in the CQ
1644 * ring for software use.
1649 if (unlikely(rxq->zip.ai == rxq->zip.cqe_cnt)) {
1650 /* Invalidate the rest */
1654 while (idx != end) {
1655 (*rxq->cqes)[idx & cqe_cnt].op_own =
1656 MLX5_CQE_INVALIDATE;
1659 rxq->cq_ci = zip->cq_ci;
1662 /* No compressed data, get next CQE and verify if it is compressed. */
1667 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
1668 if (unlikely(ret == 1))
1671 op_own = cqe->op_own;
1672 if (MLX5_CQE_FORMAT(op_own) == MLX5_COMPRESSED) {
1673 volatile struct mlx5_mini_cqe8 (*mc)[8] =
1674 (volatile struct mlx5_mini_cqe8 (*)[8])
1675 (uintptr_t)(&(*rxq->cqes)[rxq->cq_ci &
1678 /* Fix endianness. */
1679 zip->cqe_cnt = ntohl(cqe->byte_cnt);
1681 * Current mini array position is the one returned by
1684 * If completion comprises several mini arrays, as a
1685 * special case the second one is located 7 CQEs after
1686 * the initial CQE instead of 8 for subsequent ones.
1688 zip->ca = rxq->cq_ci;
1689 zip->na = zip->ca + 7;
1690 /* Compute the next non compressed CQE. */
1692 zip->cq_ci = rxq->cq_ci + zip->cqe_cnt;
1693 /* Get packet size to return. */
1694 len = ntohl((*mc)[0].byte_cnt);
1695 *rss_hash = ntohl((*mc)[0].rx_hash_result);
1697 /* Prefetch all the entries to be invalidated */
1700 while (idx != end) {
1701 rte_prefetch0(&(*rxq->cqes)[(idx) & cqe_cnt]);
1705 len = ntohl(cqe->byte_cnt);
1706 *rss_hash = ntohl(cqe->rx_hash_res);
1708 /* Error while receiving packet. */
1709 if (unlikely(MLX5_CQE_OPCODE(op_own) == MLX5_CQE_RESP_ERR))
1716 * Translate RX completion flags to offload flags.
1719 * Pointer to RX queue structure.
1724 * Offload flags (ol_flags) for struct rte_mbuf.
1726 static inline uint32_t
1727 rxq_cq_to_ol_flags(struct rxq *rxq, volatile struct mlx5_cqe *cqe)
1729 uint32_t ol_flags = 0;
1730 uint16_t flags = ntohs(cqe->hdr_type_etc);
1734 MLX5_CQE_RX_L3_HDR_VALID,
1735 PKT_RX_IP_CKSUM_GOOD) |
1737 MLX5_CQE_RX_L4_HDR_VALID,
1738 PKT_RX_L4_CKSUM_GOOD);
1739 if ((cqe->pkt_info & MLX5_CQE_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
1742 MLX5_CQE_RX_L3_HDR_VALID,
1743 PKT_RX_IP_CKSUM_GOOD) |
1745 MLX5_CQE_RX_L4_HDR_VALID,
1746 PKT_RX_L4_CKSUM_GOOD);
1751 * DPDK callback for RX.
1754 * Generic pointer to RX queue structure.
1756 * Array to store received packets.
1758 * Maximum number of packets in array.
1761 * Number of packets successfully received (<= pkts_n).
1764 mlx5_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1766 struct rxq *rxq = dpdk_rxq;
1767 const unsigned int wqe_cnt = (1 << rxq->elts_n) - 1;
1768 const unsigned int cqe_cnt = (1 << rxq->cqe_n) - 1;
1769 const unsigned int sges_n = rxq->sges_n;
1770 struct rte_mbuf *pkt = NULL;
1771 struct rte_mbuf *seg = NULL;
1772 volatile struct mlx5_cqe *cqe =
1773 &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1775 unsigned int rq_ci = rxq->rq_ci << sges_n;
1776 int len = 0; /* keep its value across iterations. */
1779 unsigned int idx = rq_ci & wqe_cnt;
1780 volatile struct mlx5_wqe_data_seg *wqe = &(*rxq->wqes)[idx];
1781 struct rte_mbuf *rep = (*rxq->elts)[idx];
1782 uint32_t rss_hash_res = 0;
1790 rep = rte_mbuf_raw_alloc(rxq->mp);
1791 if (unlikely(rep == NULL)) {
1792 ++rxq->stats.rx_nombuf;
1795 * no buffers before we even started,
1796 * bail out silently.
1800 while (pkt != seg) {
1801 assert(pkt != (*rxq->elts)[idx]);
1805 rte_mbuf_raw_free(pkt);
1811 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_cnt];
1812 len = mlx5_rx_poll_len(rxq, cqe, cqe_cnt,
1815 rte_mbuf_raw_free(rep);
1818 if (unlikely(len == -1)) {
1819 /* RX error, packet is likely too large. */
1820 rte_mbuf_raw_free(rep);
1821 ++rxq->stats.idropped;
1825 assert(len >= (rxq->crc_present << 2));
1826 /* Update packet information. */
1827 pkt->packet_type = rxq_cq_to_pkt_type(cqe);
1829 if (rss_hash_res && rxq->rss_hash) {
1830 pkt->hash.rss = rss_hash_res;
1831 pkt->ol_flags = PKT_RX_RSS_HASH;
1834 MLX5_FLOW_MARK_IS_VALID(cqe->sop_drop_qpn)) {
1835 pkt->ol_flags |= PKT_RX_FDIR;
1836 if (cqe->sop_drop_qpn !=
1837 htonl(MLX5_FLOW_MARK_DEFAULT)) {
1838 uint32_t mark = cqe->sop_drop_qpn;
1840 pkt->ol_flags |= PKT_RX_FDIR_ID;
1842 mlx5_flow_mark_get(mark);
1845 if (rxq->csum | rxq->csum_l2tun)
1846 pkt->ol_flags |= rxq_cq_to_ol_flags(rxq, cqe);
1847 if (rxq->vlan_strip &&
1848 (cqe->hdr_type_etc &
1849 htons(MLX5_CQE_VLAN_STRIPPED))) {
1850 pkt->ol_flags |= PKT_RX_VLAN_PKT |
1851 PKT_RX_VLAN_STRIPPED;
1852 pkt->vlan_tci = ntohs(cqe->vlan_info);
1854 if (rxq->crc_present)
1855 len -= ETHER_CRC_LEN;
1858 DATA_LEN(rep) = DATA_LEN(seg);
1859 PKT_LEN(rep) = PKT_LEN(seg);
1860 SET_DATA_OFF(rep, DATA_OFF(seg));
1861 PORT(rep) = PORT(seg);
1862 (*rxq->elts)[idx] = rep;
1864 * Fill NIC descriptor with the new buffer. The lkey and size
1865 * of the buffers are already known, only the buffer address
1868 wqe->addr = htonll(rte_pktmbuf_mtod(rep, uintptr_t));
1869 if (len > DATA_LEN(seg)) {
1870 len -= DATA_LEN(seg);
1875 DATA_LEN(seg) = len;
1876 #ifdef MLX5_PMD_SOFT_COUNTERS
1877 /* Increment bytes counter. */
1878 rxq->stats.ibytes += PKT_LEN(pkt);
1880 /* Return packet. */
1886 /* Align consumer index to the next stride. */
1891 if (unlikely((i == 0) && ((rq_ci >> sges_n) == rxq->rq_ci)))
1893 /* Update the consumer index. */
1894 rxq->rq_ci = rq_ci >> sges_n;
1896 *rxq->cq_db = htonl(rxq->cq_ci);
1898 *rxq->rq_db = htonl(rxq->rq_ci);
1899 #ifdef MLX5_PMD_SOFT_COUNTERS
1900 /* Increment packets counter. */
1901 rxq->stats.ipackets += i;
1907 * Dummy DPDK callback for TX.
1909 * This function is used to temporarily replace the real callback during
1910 * unsafe control operations on the queue, or in case of error.
1913 * Generic pointer to TX queue structure.
1915 * Packets to transmit.
1917 * Number of packets in array.
1920 * Number of packets successfully transmitted (<= pkts_n).
1923 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1932 * Dummy DPDK callback for RX.
1934 * This function is used to temporarily replace the real callback during
1935 * unsafe control operations on the queue, or in case of error.
1938 * Generic pointer to RX queue structure.
1940 * Array to store received packets.
1942 * Maximum number of packets in array.
1945 * Number of packets successfully received (<= pkts_n).
1948 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1957 * Vectorized Rx/Tx routines are not compiled in when required vector
1958 * instructions are not supported on a target architecture. The following null
1959 * stubs are needed for linkage when those are not included outside of this file
1960 * (e.g. mlx5_rxtx_vec_sse.c for x86).
1963 uint16_t __attribute__((weak))
1964 mlx5_tx_burst_raw_vec(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1972 uint16_t __attribute__((weak))
1973 mlx5_tx_burst_vec(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1981 uint16_t __attribute__((weak))
1982 mlx5_rx_burst_vec(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1990 int __attribute__((weak))
1991 priv_check_raw_vec_tx_support(struct priv *priv)
1997 int __attribute__((weak))
1998 priv_check_vec_tx_support(struct priv *priv)
2004 int __attribute__((weak))
2005 rxq_check_vec_support(struct rxq *rxq)
2011 int __attribute__((weak))
2012 priv_check_vec_rx_support(struct priv *priv)