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.
44 /* Verbs headers do not support -pedantic. */
46 #pragma GCC diagnostic ignored "-Wpedantic"
48 #include <infiniband/verbs.h>
50 #pragma GCC diagnostic error "-Wpedantic"
53 #include <rte_branch_prediction.h>
54 #include <rte_common.h>
57 #include <rte_mempool.h>
58 #include <rte_prefetch.h>
62 #include "mlx4_rxtx.h"
63 #include "mlx4_utils.h"
66 * Pointer-value pair structure used in tx_post_send for saving the first
67 * DWORD (32 byte) of a TXBB.
70 struct mlx4_wqe_data_seg *dseg;
75 * Stamp a WQE so it won't be reused by the HW.
77 * Routine is used when freeing WQE used by the chip or when failing
78 * building an WQ entry has failed leaving partial information on the queue.
81 * Pointer to the SQ structure.
83 * Index of the freed WQE.
85 * Number of blocks to stamp.
86 * If < 0 the routine will use the size written in the WQ entry.
88 * The value of the WQE owner bit to use in the stamp.
91 * The number of Tx basic blocs (TXBB) the WQE contained.
94 mlx4_txq_stamp_freed_wqe(struct mlx4_sq *sq, uint16_t index, uint8_t owner)
96 uint32_t stamp = rte_cpu_to_be_32(MLX4_SQ_STAMP_VAL |
97 (!!owner << MLX4_SQ_STAMP_SHIFT));
98 uint8_t *wqe = mlx4_get_send_wqe(sq, (index & sq->txbb_cnt_mask));
99 uint32_t *ptr = (uint32_t *)wqe;
104 /* Extract the size from the control segment of the WQE. */
105 num_txbbs = MLX4_SIZE_TO_TXBBS((((struct mlx4_wqe_ctrl_seg *)
106 wqe)->fence_size & 0x3f) << 4);
107 txbbs_size = num_txbbs * MLX4_TXBB_SIZE;
108 /* Optimize the common case when there is no wrap-around. */
109 if (wqe + txbbs_size <= sq->eob) {
110 /* Stamp the freed descriptor. */
111 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
113 ptr += MLX4_SQ_STAMP_DWORDS;
116 /* Stamp the freed descriptor. */
117 for (i = 0; i < txbbs_size; i += MLX4_SQ_STAMP_STRIDE) {
119 ptr += MLX4_SQ_STAMP_DWORDS;
120 if ((uint8_t *)ptr >= sq->eob) {
121 ptr = (uint32_t *)sq->buf;
122 stamp ^= RTE_BE32(0x80000000);
130 * Manage Tx completions.
132 * When sending a burst, mlx4_tx_burst() posts several WRs.
133 * To improve performance, a completion event is only required once every
134 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
135 * for other WRs, but this information would not be used anyway.
138 * Pointer to Tx queue structure.
141 * 0 on success, -1 on failure.
144 mlx4_txq_complete(struct txq *txq)
146 unsigned int elts_comp = txq->elts_comp;
147 unsigned int elts_tail = txq->elts_tail;
148 const unsigned int elts_n = txq->elts_n;
149 struct mlx4_cq *cq = &txq->mcq;
150 struct mlx4_sq *sq = &txq->msq;
151 struct mlx4_cqe *cqe;
152 uint32_t cons_index = cq->cons_index;
154 uint16_t nr_txbbs = 0;
157 if (unlikely(elts_comp == 0))
160 * Traverse over all CQ entries reported and handle each WQ entry
164 cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cons_index);
165 if (unlikely(!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
166 !!(cons_index & cq->cqe_cnt)))
169 * 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,
181 /* Get WQE index reported in the CQE. */
183 rte_be_to_cpu_16(cqe->wqe_index) & sq->txbb_cnt_mask;
185 /* Free next descriptor. */
187 mlx4_txq_stamp_freed_wqe(sq,
188 (sq->tail + nr_txbbs) & sq->txbb_cnt_mask,
189 !!((sq->tail + nr_txbbs) & sq->txbb_cnt));
191 } while (((sq->tail + nr_txbbs) & sq->txbb_cnt_mask) !=
195 if (unlikely(pkts == 0))
199 * To prevent CQ overflow we first update CQ consumer and only then
202 cq->cons_index = cons_index;
203 *cq->set_ci_db = rte_cpu_to_be_32(cq->cons_index & 0xffffff);
205 sq->tail = sq->tail + nr_txbbs;
206 /* Update the list of packets posted for transmission. */
208 assert(elts_comp <= txq->elts_comp);
210 * Assume completion status is successful as nothing can be done about
214 if (elts_tail >= elts_n)
216 txq->elts_tail = elts_tail;
217 txq->elts_comp = elts_comp;
222 * Get memory pool (MP) from mbuf. If mbuf is indirect, the pool from which
223 * the cloned mbuf is allocated is returned instead.
229 * Memory pool where data is located for given mbuf.
231 static struct rte_mempool *
232 mlx4_txq_mb2mp(struct rte_mbuf *buf)
234 if (unlikely(RTE_MBUF_INDIRECT(buf)))
235 return rte_mbuf_from_indirect(buf)->pool;
240 * Get memory region (MR) <-> memory pool (MP) association from txq->mp2mr[].
241 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
242 * remove an entry first.
245 * Pointer to Tx queue structure.
247 * Memory pool for which a memory region lkey must be returned.
250 * mr->lkey on success, (uint32_t)-1 on failure.
253 mlx4_txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
258 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
259 if (unlikely(txq->mp2mr[i].mp == NULL)) {
260 /* Unknown MP, add a new MR for it. */
263 if (txq->mp2mr[i].mp == mp) {
264 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
265 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
266 return txq->mp2mr[i].lkey;
269 /* Add a new entry, register MR first. */
270 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
271 (void *)txq, mp->name, (void *)mp);
272 mr = mlx4_mp2mr(txq->priv->pd, mp);
273 if (unlikely(mr == NULL)) {
274 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
278 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
279 /* Table is full, remove oldest entry. */
280 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
283 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
284 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
285 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
287 /* Store the new entry. */
288 txq->mp2mr[i].mp = mp;
289 txq->mp2mr[i].mr = mr;
290 txq->mp2mr[i].lkey = mr->lkey;
291 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
292 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
293 return txq->mp2mr[i].lkey;
297 * Posts a single work request to a send queue.
302 * Packet to transmit.
305 * 0 on success, negative errno value otherwise and rte_errno is set.
308 mlx4_post_send(struct txq *txq, struct rte_mbuf *pkt)
310 struct mlx4_wqe_ctrl_seg *ctrl;
311 struct mlx4_wqe_data_seg *dseg;
312 struct mlx4_sq *sq = &txq->msq;
313 struct rte_mbuf *buf;
314 uint32_t head_idx = sq->head & sq->txbb_cnt_mask;
317 uint32_t srcrb_flags;
318 uint32_t owner_opcode = MLX4_OPCODE_SEND;
323 struct pv *pv = (struct pv *)txq->bounce_buf;
326 /* Calculate the needed work queue entry size for this packet. */
327 wqe_real_size = sizeof(struct mlx4_wqe_ctrl_seg) +
328 pkt->nb_segs * sizeof(struct mlx4_wqe_data_seg);
329 nr_txbbs = MLX4_SIZE_TO_TXBBS(wqe_real_size);
331 * Check that there is room for this WQE in the send queue and that
332 * the WQE size is legal.
334 if (((sq->head - sq->tail) + nr_txbbs +
335 sq->headroom_txbbs) >= sq->txbb_cnt ||
336 nr_txbbs > MLX4_MAX_WQE_TXBBS) {
340 /* Get the control and data entries of the WQE. */
341 ctrl = (struct mlx4_wqe_ctrl_seg *)mlx4_get_send_wqe(sq, head_idx);
342 dseg = (struct mlx4_wqe_data_seg *)((uintptr_t)ctrl +
343 sizeof(struct mlx4_wqe_ctrl_seg));
344 /* Fill the data segments with buffer information. */
345 for (buf = pkt; buf != NULL; buf = buf->next, dseg++) {
346 addr = rte_pktmbuf_mtod(buf, uintptr_t);
347 rte_prefetch0((volatile void *)addr);
348 /* Handle WQE wraparound. */
349 if (unlikely(dseg >= (struct mlx4_wqe_data_seg *)sq->eob))
350 dseg = (struct mlx4_wqe_data_seg *)sq->buf;
351 dseg->addr = rte_cpu_to_be_64(addr);
352 /* Memory region key for this memory pool. */
353 lkey = mlx4_txq_mp2mr(txq, mlx4_txq_mb2mp(buf));
354 if (unlikely(lkey == (uint32_t)-1)) {
355 /* MR does not exist. */
356 DEBUG("%p: unable to get MP <-> MR association",
359 * Restamp entry in case of failure.
360 * Make sure that size is written correctly
361 * Note that we give ownership to the SW, not the HW.
363 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
364 mlx4_txq_stamp_freed_wqe(sq, head_idx,
365 (sq->head & sq->txbb_cnt) ? 0 : 1);
369 dseg->lkey = rte_cpu_to_be_32(lkey);
370 if (likely(buf->data_len)) {
371 byte_count = rte_cpu_to_be_32(buf->data_len);
374 * Zero length segment is treated as inline segment
377 byte_count = RTE_BE32(0x80000000);
380 * If the data segment is not at the beginning of a
381 * Tx basic block (TXBB) then write the byte count,
382 * else postpone the writing to just before updating the
385 if ((uintptr_t)dseg & (uintptr_t)(MLX4_TXBB_SIZE - 1)) {
387 * Need a barrier here before writing the byte_count
388 * fields to make sure that all the data is visible
389 * before the byte_count field is set.
390 * Otherwise, if the segment begins a new cacheline,
391 * the HCA prefetcher could grab the 64-byte chunk and
392 * get a valid (!= 0xffffffff) byte count but stale
393 * data, and end up sending the wrong data.
396 dseg->byte_count = byte_count;
399 * This data segment starts at the beginning of a new
400 * TXBB, so we need to postpone its byte_count writing
403 pv[pv_counter].dseg = dseg;
404 pv[pv_counter++].val = byte_count;
407 /* Write the first DWORD of each TXBB save earlier. */
409 /* Need a barrier here before writing the byte_count. */
411 for (--pv_counter; pv_counter >= 0; pv_counter--)
412 pv[pv_counter].dseg->byte_count = pv[pv_counter].val;
414 /* Fill the control parameters for this packet. */
415 ctrl->fence_size = (wqe_real_size >> 4) & 0x3f;
417 * The caller should prepare "imm" in advance in order to support
418 * VF to VF communication (when the device is a virtual-function
423 * For raw Ethernet, the SOLICIT flag is used to indicate that no ICRC
424 * should be calculated.
426 txq->elts_comp_cd -= nr_txbbs;
427 if (unlikely(txq->elts_comp_cd <= 0)) {
428 txq->elts_comp_cd = txq->elts_comp_cd_init;
429 srcrb_flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT |
430 MLX4_WQE_CTRL_CQ_UPDATE);
432 srcrb_flags = RTE_BE32(MLX4_WQE_CTRL_SOLICIT);
434 ctrl->srcrb_flags = srcrb_flags;
436 * Make sure descriptor is fully written before
437 * setting ownership bit (because HW can start
438 * executing as soon as we do).
441 ctrl->owner_opcode = rte_cpu_to_be_32(owner_opcode |
442 ((sq->head & sq->txbb_cnt) ?
443 MLX4_BIT_WQE_OWN : 0));
444 sq->head += nr_txbbs;
452 * DPDK callback for Tx.
455 * Generic pointer to Tx queue structure.
457 * Packets to transmit.
459 * Number of packets in array.
462 * Number of packets successfully transmitted (<= pkts_n).
465 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
467 struct txq *txq = (struct txq *)dpdk_txq;
468 unsigned int elts_head = txq->elts_head;
469 const unsigned int elts_n = txq->elts_n;
470 unsigned int elts_comp = 0;
471 unsigned int bytes_sent = 0;
476 assert(txq->elts_comp_cd != 0);
477 mlx4_txq_complete(txq);
478 max = (elts_n - (elts_head - txq->elts_tail));
482 assert(max <= elts_n);
483 /* Always leave one free entry in the ring. */
489 for (i = 0; (i != max); ++i) {
490 struct rte_mbuf *buf = pkts[i];
491 unsigned int elts_head_next =
492 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
493 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
494 struct txq_elt *elt = &(*txq->elts)[elts_head];
496 /* Clean up old buffer. */
497 if (likely(elt->buf != NULL)) {
498 struct rte_mbuf *tmp = elt->buf;
502 memset(elt, 0x66, sizeof(*elt));
504 /* Faster than rte_pktmbuf_free(). */
506 struct rte_mbuf *next = tmp->next;
508 rte_pktmbuf_free_seg(tmp);
510 } while (tmp != NULL);
512 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
513 /* Post the packet for sending. */
514 err = mlx4_post_send(txq, buf);
520 bytes_sent += buf->pkt_len;
522 elts_head = elts_head_next;
525 /* Take a shortcut if nothing must be sent. */
526 if (unlikely(i == 0))
528 /* Increment send statistics counters. */
529 txq->stats.opackets += i;
530 txq->stats.obytes += bytes_sent;
531 /* Make sure that descriptors are written before doorbell record. */
533 /* Ring QP doorbell. */
534 rte_write32(txq->msq.doorbell_qpn, txq->msq.db);
535 txq->elts_head = elts_head;
536 txq->elts_comp += elts_comp;
541 * Poll one CQE from CQ.
544 * Pointer to the receive queue structure.
549 * Number of bytes of the CQE, 0 in case there is no completion.
552 mlx4_cq_poll_one(struct rxq *rxq, struct mlx4_cqe **out)
555 struct mlx4_cqe *cqe = NULL;
556 struct mlx4_cq *cq = &rxq->mcq;
558 cqe = (struct mlx4_cqe *)mlx4_get_cqe(cq, cq->cons_index);
559 if (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
560 !!(cq->cons_index & cq->cqe_cnt))
563 * Make sure we read CQ entry contents after we've checked the
567 assert(!(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK));
568 assert((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) !=
569 MLX4_CQE_OPCODE_ERROR);
570 ret = rte_be_to_cpu_32(cqe->byte_cnt);
578 * DPDK callback for Rx with scattered packets support.
581 * Generic pointer to Rx queue structure.
583 * Array to store received packets.
585 * Maximum number of packets in array.
588 * Number of packets successfully received (<= pkts_n).
591 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
593 struct rxq *rxq = dpdk_rxq;
594 const uint32_t wr_cnt = (1 << rxq->elts_n) - 1;
595 const uint16_t sges_n = rxq->sges_n;
596 struct rte_mbuf *pkt = NULL;
597 struct rte_mbuf *seg = NULL;
599 uint32_t rq_ci = rxq->rq_ci << sges_n;
603 struct mlx4_cqe *cqe;
604 uint32_t idx = rq_ci & wr_cnt;
605 struct rte_mbuf *rep = (*rxq->elts)[idx];
606 volatile struct mlx4_wqe_data_seg *scat = &(*rxq->wqes)[idx];
608 /* Update the 'next' pointer of the previous segment. */
614 rep = rte_mbuf_raw_alloc(rxq->mp);
615 if (unlikely(rep == NULL)) {
616 ++rxq->stats.rx_nombuf;
619 * No buffers before we even started,
625 assert(pkt != (*rxq->elts)[idx]);
629 rte_mbuf_raw_free(pkt);
635 /* Looking for the new packet. */
636 len = mlx4_cq_poll_one(rxq, &cqe);
638 rte_mbuf_raw_free(rep);
641 if (unlikely(len < 0)) {
642 /* Rx error, packet is likely too large. */
643 rte_mbuf_raw_free(rep);
644 ++rxq->stats.idropped;
648 pkt->packet_type = 0;
653 rep->port = rxq->port_id;
654 rep->data_len = seg->data_len;
655 rep->data_off = seg->data_off;
656 (*rxq->elts)[idx] = rep;
658 * Fill NIC descriptor with the new buffer. The lkey and size
659 * of the buffers are already known, only the buffer address
662 scat->addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(rep, uintptr_t));
663 if (len > seg->data_len) {
664 len -= seg->data_len;
669 /* The last segment. */
671 /* Increment bytes counter. */
672 rxq->stats.ibytes += pkt->pkt_len;
679 /* Align consumer index to the next stride. */
684 if (unlikely(i == 0 && (rq_ci >> sges_n) == rxq->rq_ci))
686 /* Update the consumer index. */
687 rxq->rq_ci = rq_ci >> sges_n;
689 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
690 *rxq->mcq.set_ci_db = rte_cpu_to_be_32(rxq->mcq.cons_index & 0xffffff);
691 /* Increment packets counter. */
692 rxq->stats.ipackets += i;
697 * Dummy DPDK callback for Tx.
699 * This function is used to temporarily replace the real callback during
700 * unsafe control operations on the queue, or in case of error.
703 * Generic pointer to Tx queue structure.
705 * Packets to transmit.
707 * Number of packets in array.
710 * Number of packets successfully transmitted (<= pkts_n).
713 mlx4_tx_burst_removed(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
722 * Dummy DPDK callback for Rx.
724 * This function is used to temporarily replace the real callback during
725 * unsafe control operations on the queue, or in case of error.
728 * Generic pointer to Rx queue structure.
730 * Array to store received packets.
732 * Maximum number of packets in array.
735 * Number of packets successfully received (<= pkts_n).
738 mlx4_rx_burst_removed(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)