4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 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.
36 * Data plane functions for mlx4 driver.
43 /* Verbs headers do not support -pedantic. */
45 #pragma GCC diagnostic ignored "-Wpedantic"
47 #include <infiniband/verbs.h>
49 #pragma GCC diagnostic error "-Wpedantic"
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
56 #include <rte_mempool.h>
57 #include <rte_prefetch.h>
61 #include "mlx4_rxtx.h"
62 #include "mlx4_utils.h"
65 * Pointer-value pair structure used in tx_post_send for saving the first
66 * DWORD (32 byte) of a TXBB.
69 struct mlx4_wqe_data_seg *dseg;
74 * Stamp a WQE so it won't be reused by the HW.
76 * Routine is used when freeing WQE used by the chip or when failing
77 * building an WQ entry has failed leaving partial information on the queue.
80 * Pointer to the SQ structure.
82 * Index of the freed WQE.
84 * Number of blocks to stamp.
85 * If < 0 the routine will use the size written in the WQ entry.
87 * The value of the WQE owner bit to use in the stamp.
90 * The number of Tx basic blocs (TXBB) the WQE contained.
93 mlx4_txq_stamp_freed_wqe(struct mlx4_sq *sq, uint16_t index, uint8_t owner)
95 uint32_t stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
96 (!!owner << MLX4_SQ_STAMP_SHIFT));
97 uint8_t *wqe = mlx4_get_send_wqe(sq, (index & sq->txbb_cnt_mask));
98 uint32_t *ptr = (uint32_t *)wqe;
103 /* Extract the size from the control segment of the WQE. */
104 num_txbbs = MLX4_SIZE_TO_TXBBS((((struct mlx4_wqe_ctrl_seg *)
105 wqe)->fence_size & 0x3f) << 4);
106 txbbs_size = num_txbbs * MLX4_TXBB_SIZE;
107 /* Optimize the common case when there is no wrap-around. */
108 if (wqe + txbbs_size <= sq->eob) {
109 /* Stamp the freed descriptor. */
110 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
112 ptr += MLX4_SQ_STAMP_DWORDS;
115 /* Stamp the freed descriptor. */
116 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
118 ptr += MLX4_SQ_STAMP_DWORDS;
119 if ((uint8_t *)ptr >= sq->eob) {
120 ptr = (uint32_t *)sq->buf;
121 stamp ^= RTE_BE32(0x80000000);
129 * Manage Tx completions.
131 * When sending a burst, mlx4_tx_burst() posts several WRs.
132 * To improve performance, a completion event is only required once every
133 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
134 * for other WRs, but this information would not be used anyway.
137 * Pointer to Tx queue structure.
140 * 0 on success, -1 on failure.
143 mlx4_txq_complete(struct txq *txq)
145 unsigned int elts_comp = txq->elts_comp;
146 unsigned int elts_tail = txq->elts_tail;
147 const unsigned int elts_n = txq->elts_n;
148 struct mlx4_cq *cq = &txq->mcq;
149 struct mlx4_sq *sq = &txq->msq;
150 struct mlx4_cqe *cqe;
151 uint32_t cons_index = cq->cons_index;
153 uint16_t nr_txbbs = 0;
156 if (unlikely(elts_comp == 0))
159 * Traverse over all CQ entries reported and handle each WQ entry
163 cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cons_index);
164 if (unlikely(!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
165 !!(cons_index & cq->cqe_cnt)))
168 * Make sure we read the CQE after we read the ownership bit.
172 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
173 MLX4_CQE_OPCODE_ERROR)) {
174 struct mlx4_err_cqe *cqe_err =
175 (struct mlx4_err_cqe *)cqe;
176 ERROR("%p CQE error - vendor syndrome: 0x%x"
178 (void *)txq, cqe_err->vendor_err,
182 /* Get WQE index reported in the CQE. */
184 rte_be_to_cpu_16(cqe->wqe_index) & sq->txbb_cnt_mask;
186 /* Free next descriptor. */
188 mlx4_txq_stamp_freed_wqe(sq,
189 (sq->tail + nr_txbbs) & sq->txbb_cnt_mask,
190 !!((sq->tail + nr_txbbs) & sq->txbb_cnt));
192 } while (((sq->tail + nr_txbbs) & sq->txbb_cnt_mask) !=
196 if (unlikely(pkts == 0))
200 * To prevent CQ overflow we first update CQ consumer and only then
203 cq->cons_index = cons_index;
204 *cq->set_ci_db = rte_cpu_to_be_32(cq->cons_index & MLX4_CQ_DB_CI_MASK);
206 sq->tail = sq->tail + nr_txbbs;
207 /* Update the list of packets posted for transmission. */
209 assert(elts_comp <= txq->elts_comp);
211 * Assume completion status is successful as nothing can be done about
215 if (elts_tail >= elts_n)
217 txq->elts_tail = elts_tail;
218 txq->elts_comp = elts_comp;
223 * Get memory pool (MP) from mbuf. If mbuf is indirect, the pool from which
224 * the cloned mbuf is allocated is returned instead.
230 * Memory pool where data is located for given mbuf.
232 static struct rte_mempool *
233 mlx4_txq_mb2mp(struct rte_mbuf *buf)
235 if (unlikely(RTE_MBUF_INDIRECT(buf)))
236 return rte_mbuf_from_indirect(buf)->pool;
241 * Posts a single work request to a send queue.
246 * Packet to transmit.
249 * 0 on success, negative errno value otherwise.
252 mlx4_post_send(struct txq *txq, struct rte_mbuf *pkt)
254 struct mlx4_wqe_ctrl_seg *ctrl;
255 struct mlx4_wqe_data_seg *dseg;
256 struct mlx4_sq *sq = &txq->msq;
257 struct rte_mbuf *buf;
262 uint32_t head_idx = sq->head & sq->txbb_cnt_mask;
265 uint32_t owner_opcode = MLX4_OPCODE_SEND;
269 struct pv *pv = (struct pv *)txq->bounce_buf;
272 /* Calculate the needed work queue entry size for this packet. */
273 wqe_real_size = sizeof(struct mlx4_wqe_ctrl_seg) +
274 pkt->nb_segs * sizeof(struct mlx4_wqe_data_seg);
275 nr_txbbs = MLX4_SIZE_TO_TXBBS(wqe_real_size);
277 * Check that there is room for this WQE in the send queue and that
278 * the WQE size is legal.
280 if (((sq->head - sq->tail) + nr_txbbs +
281 sq->headroom_txbbs) >= sq->txbb_cnt ||
282 nr_txbbs > MLX4_MAX_WQE_TXBBS) {
285 /* Get the control and data entries of the WQE. */
286 ctrl = (struct mlx4_wqe_ctrl_seg *)mlx4_get_send_wqe(sq, head_idx);
287 dseg = (struct mlx4_wqe_data_seg *)((uintptr_t)ctrl +
288 sizeof(struct mlx4_wqe_ctrl_seg));
289 /* Fill the data segments with buffer information. */
290 for (buf = pkt; buf != NULL; buf = buf->next, dseg++) {
291 addr = rte_pktmbuf_mtod(buf, uintptr_t);
292 rte_prefetch0((volatile void *)addr);
293 /* Handle WQE wraparound. */
294 if (unlikely(dseg >= (struct mlx4_wqe_data_seg *)sq->eob))
295 dseg = (struct mlx4_wqe_data_seg *)sq->buf;
296 dseg->addr = rte_cpu_to_be_64(addr);
297 /* Memory region key for this memory pool. */
298 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(buf));
300 if (unlikely(lkey == (uint32_t)-1)) {
301 /* MR does not exist. */
302 DEBUG("%p: unable to get MP <-> MR association",
305 * Restamp entry in case of failure.
306 * Make sure that size is written correctly
307 * Note that we give ownership to the SW, not the HW.
309 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
310 mlx4_txq_stamp_freed_wqe(sq, head_idx,
311 (sq->head & sq->txbb_cnt) ? 0 : 1);
315 dseg->lkey = rte_cpu_to_be_32(lkey);
316 if (likely(buf->data_len)) {
317 byte_count = rte_cpu_to_be_32(buf->data_len);
320 * Zero length segment is treated as inline segment
323 byte_count = RTE_BE32(0x80000000);
326 * If the data segment is not at the beginning of a
327 * Tx basic block (TXBB) then write the byte count,
328 * else postpone the writing to just before updating the
331 if ((uintptr_t)dseg & (uintptr_t)(MLX4_TXBB_SIZE - 1)) {
333 * Need a barrier here before writing the byte_count
334 * fields to make sure that all the data is visible
335 * before the byte_count field is set.
336 * Otherwise, if the segment begins a new cacheline,
337 * the HCA prefetcher could grab the 64-byte chunk and
338 * get a valid (!= 0xffffffff) byte count but stale
339 * data, and end up sending the wrong data.
342 dseg->byte_count = byte_count;
345 * This data segment starts at the beginning of a new
346 * TXBB, so we need to postpone its byte_count writing
349 pv[pv_counter].dseg = dseg;
350 pv[pv_counter++].val = byte_count;
353 /* Write the first DWORD of each TXBB save earlier. */
355 /* Need a barrier here before writing the byte_count. */
357 for (--pv_counter; pv_counter >= 0; pv_counter--)
358 pv[pv_counter].dseg->byte_count = pv[pv_counter].val;
360 /* Fill the control parameters for this packet. */
361 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
363 * For raw Ethernet, the SOLICIT flag is used to indicate that no ICRC
364 * should be calculated.
366 txq->elts_comp_cd -= nr_txbbs;
367 if (unlikely(txq->elts_comp_cd <= 0)) {
368 txq->elts_comp_cd = txq->elts_comp_cd_init;
369 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT |
370 MLX4_WQE_CTRL_CQ_UPDATE);
372 srcrb.flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
374 /* Enable HW checksum offload if requested */
377 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM))) {
378 const uint64_t is_tunneled = (pkt->ol_flags &
380 PKT_TX_TUNNEL_VXLAN));
382 if (is_tunneled && txq->csum_l2tun) {
383 owner_opcode |= MLX4_WQE_CTRL_IIP_HDR_CSUM |
384 MLX4_WQE_CTRL_IL4_HDR_CSUM;
385 if (pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM)
387 RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM);
389 srcrb.flags |= RTE_BE32(MLX4_WQE_CTRL_IP_HDR_CSUM |
390 MLX4_WQE_CTRL_TCP_UDP_CSUM);
395 * Copy destination MAC address to the WQE, this allows
396 * loopback in eSwitch, so that VFs and PF can communicate
399 srcrb.flags16[0] = *(rte_pktmbuf_mtod(pkt, uint16_t *));
400 ctrl->imm = *(rte_pktmbuf_mtod_offset(pkt, uint32_t *,
405 ctrl->srcrb_flags = srcrb.flags;
407 * Make sure descriptor is fully written before
408 * setting ownership bit (because HW can start
409 * executing as soon as we do).
412 ctrl->owner_opcode = rte_cpu_to_be_32(owner_opcode |
413 ((sq->head & sq->txbb_cnt) ?
414 MLX4_BIT_WQE_OWN : 0));
415 sq->head += nr_txbbs;
420 * DPDK callback for Tx.
423 * Generic pointer to Tx queue structure.
425 * Packets to transmit.
427 * Number of packets in array.
430 * Number of packets successfully transmitted (<= pkts_n).
433 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
435 struct txq *txq = (struct txq *)dpdk_txq;
436 unsigned int elts_head = txq->elts_head;
437 const unsigned int elts_n = txq->elts_n;
438 unsigned int elts_comp = 0;
439 unsigned int bytes_sent = 0;
444 assert(txq->elts_comp_cd != 0);
445 mlx4_txq_complete(txq);
446 max = (elts_n - (elts_head - txq->elts_tail));
450 assert(max <= elts_n);
451 /* Always leave one free entry in the ring. */
457 for (i = 0; (i != max); ++i) {
458 struct rte_mbuf *buf = pkts[i];
459 unsigned int elts_head_next =
460 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
461 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
462 struct txq_elt *elt = &(*txq->elts)[elts_head];
464 /* Clean up old buffer. */
465 if (likely(elt->buf != NULL)) {
466 struct rte_mbuf *tmp = elt->buf;
470 memset(elt, 0x66, sizeof(*elt));
472 /* Faster than rte_pktmbuf_free(). */
474 struct rte_mbuf *next = tmp->next;
476 rte_pktmbuf_free_seg(tmp);
478 } while (tmp != NULL);
480 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
481 /* Post the packet for sending. */
482 err = mlx4_post_send(txq, buf);
488 bytes_sent += buf->pkt_len;
490 elts_head = elts_head_next;
493 /* Take a shortcut if nothing must be sent. */
494 if (unlikely(i == 0))
496 /* Increment send statistics counters. */
497 txq->stats.opackets += i;
498 txq->stats.obytes += bytes_sent;
499 /* Make sure that descriptors are written before doorbell record. */
501 /* Ring QP doorbell. */
502 rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
503 txq->elts_head = elts_head;
504 txq->elts_comp += elts_comp;
509 * Translate Rx completion flags to packet type.
512 * Rx completion flags returned by mlx4_cqe_flags().
515 * Packet type in mbuf format.
517 static inline uint32_t
518 rxq_cq_to_pkt_type(uint32_t flags)
522 if (flags & MLX4_CQE_L2_TUNNEL)
524 mlx4_transpose(flags,
525 MLX4_CQE_L2_TUNNEL_IPV4,
526 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
527 mlx4_transpose(flags,
528 MLX4_CQE_STATUS_IPV4_PKT,
529 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN);
531 pkt_type = mlx4_transpose(flags,
532 MLX4_CQE_STATUS_IPV4_PKT,
533 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN);
538 * Translate Rx completion flags to offload flags.
541 * Rx completion flags returned by mlx4_cqe_flags().
543 * Whether Rx checksums are enabled.
545 * Whether Rx L2 tunnel checksums are enabled.
548 * Offload flags (ol_flags) in mbuf format.
550 static inline uint32_t
551 rxq_cq_to_ol_flags(uint32_t flags, int csum, int csum_l2tun)
553 uint32_t ol_flags = 0;
557 mlx4_transpose(flags,
558 MLX4_CQE_STATUS_IP_HDR_CSUM_OK,
559 PKT_RX_IP_CKSUM_GOOD) |
560 mlx4_transpose(flags,
561 MLX4_CQE_STATUS_TCP_UDP_CSUM_OK,
562 PKT_RX_L4_CKSUM_GOOD);
563 if ((flags & MLX4_CQE_L2_TUNNEL) && csum_l2tun)
565 mlx4_transpose(flags,
566 MLX4_CQE_L2_TUNNEL_IPOK,
567 PKT_RX_IP_CKSUM_GOOD) |
568 mlx4_transpose(flags,
569 MLX4_CQE_L2_TUNNEL_L4_CSUM,
570 PKT_RX_L4_CKSUM_GOOD);
575 * Extract checksum information from CQE flags.
578 * Pointer to CQE structure.
580 * Whether Rx checksums are enabled.
582 * Whether Rx L2 tunnel checksums are enabled.
585 * CQE checksum information.
587 static inline uint32_t
588 mlx4_cqe_flags(struct mlx4_cqe *cqe, int csum, int csum_l2tun)
593 * The relevant bits are in different locations on their
594 * CQE fields therefore we can join them in one 32bit
598 flags = (rte_be_to_cpu_32(cqe->status) &
599 MLX4_CQE_STATUS_IPV4_CSUM_OK);
601 flags |= (rte_be_to_cpu_32(cqe->vlan_my_qpn) &
602 (MLX4_CQE_L2_TUNNEL |
603 MLX4_CQE_L2_TUNNEL_IPOK |
604 MLX4_CQE_L2_TUNNEL_L4_CSUM |
605 MLX4_CQE_L2_TUNNEL_IPV4));
610 * Poll one CQE from CQ.
613 * Pointer to the receive queue structure.
618 * Number of bytes of the CQE, 0 in case there is no completion.
621 mlx4_cq_poll_one(struct rxq *rxq, struct mlx4_cqe **out)
624 struct mlx4_cqe *cqe = NULL;
625 struct mlx4_cq *cq = &rxq->mcq;
627 cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cq->cons_index);
628 if (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
629 !!(cq->cons_index & cq->cqe_cnt))
632 * Make sure we read CQ entry contents after we've checked the
636 assert(!(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK));
637 assert((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) !=
638 MLX4_CQE_OPCODE_ERROR);
639 ret = rte_be_to_cpu_32(cqe->byte_cnt);
647 * DPDK callback for Rx with scattered packets support.
650 * Generic pointer to Rx queue structure.
652 * Array to store received packets.
654 * Maximum number of packets in array.
657 * Number of packets successfully received (<= pkts_n).
660 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
662 struct rxq *rxq = dpdk_rxq;
663 const uint32_t wr_cnt = (1 << rxq->elts_n) - 1;
664 const uint16_t sges_n = rxq->sges_n;
665 struct rte_mbuf *pkt = NULL;
666 struct rte_mbuf *seg = NULL;
668 uint32_t rq_ci = rxq->rq_ci << sges_n;
672 struct mlx4_cqe *cqe;
673 uint32_t idx = rq_ci & wr_cnt;
674 struct rte_mbuf *rep = (*rxq->elts)[idx];
675 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[idx];
677 /* Update the 'next' pointer of the previous segment. */
683 rep = rte_mbuf_raw_alloc(rxq->mp);
684 if (unlikely(rep == NULL)) {
685 ++rxq->stats.rx_nombuf;
688 * No buffers before we even started,
694 assert(pkt != (*rxq->elts)[idx]);
698 rte_mbuf_raw_free(pkt);
704 /* Looking for the new packet. */
705 len = mlx4_cq_poll_one(rxq, &cqe);
707 rte_mbuf_raw_free(rep);
710 if (unlikely(len < 0)) {
711 /* Rx error, packet is likely too large. */
712 rte_mbuf_raw_free(rep);
713 ++rxq->stats.idropped;
717 if (rxq->csum | rxq->csum_l2tun) {
724 rxq_cq_to_ol_flags(flags,
727 pkt->packet_type = rxq_cq_to_pkt_type(flags);
729 pkt->packet_type = 0;
735 rep->port = rxq->port_id;
736 rep->data_len = seg->data_len;
737 rep->data_off = seg->data_off;
738 (*rxq->elts)[idx] = rep;
740 * Fill NIC descriptor with the new buffer. The lkey and size
741 * of the buffers are already known, only the buffer address
744 scat->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
745 if (len > seg->data_len) {
746 len -= seg->data_len;
751 /* The last segment. */
753 /* Increment bytes counter. */
754 rxq->stats.ibytes += pkt->pkt_len;
761 /* Align consumer index to the next stride. */
766 if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
768 /* Update the consumer index. */
769 rxq->rq_ci = rq_ci >> sges_n;
771 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
772 *rxq->mcq.set_ci_db =
773 rte_cpu_to_be_32(rxq->mcq.cons_index & MLX4_CQ_DB_CI_MASK);
774 /* Increment packets counter. */
775 rxq->stats.ipackets += i;
780 * Dummy DPDK callback for Tx.
782 * This function is used to temporarily replace the real callback during
783 * unsafe control operations on the queue, or in case of error.
786 * Generic pointer to Tx queue structure.
788 * Packets to transmit.
790 * Number of packets in array.
793 * Number of packets successfully transmitted (<= pkts_n).
796 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
805 * Dummy DPDK callback for Rx.
807 * This function is used to temporarily replace the real callback during
808 * unsafe control operations on the queue, or in case of error.
811 * Generic pointer to Rx queue structure.
813 * Array to store received packets.
815 * Maximum number of packets in array.
818 * Number of packets successfully received (<= pkts_n).
821 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)