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 #include <sys/queue.h>
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
45 #pragma GCC diagnostic ignored "-Wpedantic"
47 #include <infiniband/verbs.h>
48 #include <infiniband/mlx5dv.h>
50 #pragma GCC diagnostic error "-Wpedantic"
54 #include <rte_malloc.h>
55 #include <rte_ethdev.h>
56 #include <rte_common.h>
57 #include <rte_interrupts.h>
58 #include <rte_debug.h>
62 #include "mlx5_rxtx.h"
63 #include "mlx5_utils.h"
64 #include "mlx5_autoconf.h"
65 #include "mlx5_defs.h"
67 /* Default RSS hash key also used for ConnectX-3. */
68 uint8_t rss_hash_default_key[] = {
69 0x2c, 0xc6, 0x81, 0xd1,
70 0x5b, 0xdb, 0xf4, 0xf7,
71 0xfc, 0xa2, 0x83, 0x19,
72 0xdb, 0x1a, 0x3e, 0x94,
73 0x6b, 0x9e, 0x38, 0xd9,
74 0x2c, 0x9c, 0x03, 0xd1,
75 0xad, 0x99, 0x44, 0xa7,
76 0xd9, 0x56, 0x3d, 0x59,
77 0x06, 0x3c, 0x25, 0xf3,
78 0xfc, 0x1f, 0xdc, 0x2a,
81 /* Length of the default RSS hash key. */
82 const size_t rss_hash_default_key_len = sizeof(rss_hash_default_key);
85 * Allocate RX queue elements.
88 * Pointer to RX queue structure.
91 * 0 on success, errno value on failure.
94 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
96 const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
97 unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
101 /* Iterate on segments. */
102 for (i = 0; (i != elts_n); ++i) {
103 struct rte_mbuf *buf;
105 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
107 ERROR("%p: empty mbuf pool", (void *)rxq_ctrl);
111 /* Headroom is reserved by rte_pktmbuf_alloc(). */
112 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
113 /* Buffer is supposed to be empty. */
114 assert(rte_pktmbuf_data_len(buf) == 0);
115 assert(rte_pktmbuf_pkt_len(buf) == 0);
117 /* Only the first segment keeps headroom. */
119 SET_DATA_OFF(buf, 0);
120 PORT(buf) = rxq_ctrl->rxq.port_id;
121 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
122 PKT_LEN(buf) = DATA_LEN(buf);
124 (*rxq_ctrl->rxq.elts)[i] = buf;
126 /* If Rx vector is activated. */
127 if (rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
128 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
129 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
132 /* Initialize default rearm_data for vPMD. */
133 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
134 rte_mbuf_refcnt_set(mbuf_init, 1);
135 mbuf_init->nb_segs = 1;
136 mbuf_init->port = rxq->port_id;
138 * prevent compiler reordering:
139 * rearm_data covers previous fields.
141 rte_compiler_barrier();
142 rxq->mbuf_initializer =
143 *(uint64_t *)&mbuf_init->rearm_data;
144 /* Padding with a fake mbuf for vectorized Rx. */
145 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
146 (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
148 DEBUG("%p: allocated and configured %u segments (max %u packets)",
149 (void *)rxq_ctrl, elts_n, elts_n / (1 << rxq_ctrl->rxq.sges_n));
154 for (i = 0; (i != elts_n); ++i) {
155 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
156 rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
157 (*rxq_ctrl->rxq.elts)[i] = NULL;
159 DEBUG("%p: failed, freed everything", (void *)rxq_ctrl);
165 * Free RX queue elements.
168 * Pointer to RX queue structure.
171 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
173 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
174 const uint16_t q_n = (1 << rxq->elts_n);
175 const uint16_t q_mask = q_n - 1;
176 uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
179 DEBUG("%p: freeing WRs", (void *)rxq_ctrl);
180 if (rxq->elts == NULL)
183 * Some mbuf in the Ring belongs to the application. They cannot be
186 if (rxq_check_vec_support(rxq) > 0) {
187 for (i = 0; i < used; ++i)
188 (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
189 rxq->rq_pi = rxq->rq_ci;
191 for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
192 if ((*rxq->elts)[i] != NULL)
193 rte_pktmbuf_free_seg((*rxq->elts)[i]);
194 (*rxq->elts)[i] = NULL;
199 * Clean up a RX queue.
201 * Destroy objects, free allocated memory and reset the structure for reuse.
204 * Pointer to RX queue structure.
207 mlx5_rxq_cleanup(struct mlx5_rxq_ctrl *rxq_ctrl)
209 DEBUG("cleaning up %p", (void *)rxq_ctrl);
211 mlx5_priv_rxq_ibv_release(rxq_ctrl->priv, rxq_ctrl->ibv);
212 memset(rxq_ctrl, 0, sizeof(*rxq_ctrl));
218 * Pointer to Ethernet device structure.
222 * Number of descriptors to configure in queue.
224 * NUMA socket on which memory must be allocated.
226 * Thresholds parameters.
228 * Memory pool for buffer allocations.
231 * 0 on success, negative errno value on failure.
234 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
235 unsigned int socket, const struct rte_eth_rxconf *conf,
236 struct rte_mempool *mp)
238 struct priv *priv = dev->data->dev_private;
239 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
240 struct mlx5_rxq_ctrl *rxq_ctrl =
241 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
245 if (mlx5_is_secondary())
246 return -E_RTE_SECONDARY;
248 if (!rte_is_power_of_2(desc)) {
249 desc = 1 << log2above(desc);
250 WARN("%p: increased number of descriptors in RX queue %u"
251 " to the next power of two (%d)",
252 (void *)dev, idx, desc);
254 DEBUG("%p: configuring queue %u for %u descriptors",
255 (void *)dev, idx, desc);
256 if (idx >= priv->rxqs_n) {
257 ERROR("%p: queue index out of range (%u >= %u)",
258 (void *)dev, idx, priv->rxqs_n);
262 if (!mlx5_priv_rxq_releasable(priv, idx)) {
264 ERROR("%p: unable to release queue index %u",
268 mlx5_priv_rxq_release(priv, idx);
269 rxq_ctrl = mlx5_priv_rxq_new(priv, idx, desc, socket, mp);
271 ERROR("%p: unable to allocate queue index %u",
276 DEBUG("%p: adding RX queue %p to list",
277 (void *)dev, (void *)rxq_ctrl);
278 (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
285 * DPDK callback to release a RX queue.
288 * Generic RX queue pointer.
291 mlx5_rx_queue_release(void *dpdk_rxq)
293 struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
294 struct mlx5_rxq_ctrl *rxq_ctrl;
297 if (mlx5_is_secondary())
302 rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
303 priv = rxq_ctrl->priv;
305 if (!mlx5_priv_rxq_releasable(priv, rxq_ctrl->rxq.stats.idx))
306 rte_panic("Rx queue %p is still used by a flow and cannot be"
307 " removed\n", (void *)rxq_ctrl);
308 mlx5_priv_rxq_release(priv, rxq_ctrl->rxq.stats.idx);
313 * Allocate queue vector and fill epoll fd list for Rx interrupts.
316 * Pointer to private structure.
319 * 0 on success, negative on failure.
322 priv_rx_intr_vec_enable(struct priv *priv)
325 unsigned int rxqs_n = priv->rxqs_n;
326 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
327 unsigned int count = 0;
328 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
330 assert(!mlx5_is_secondary());
331 if (!priv->dev->data->dev_conf.intr_conf.rxq)
333 priv_rx_intr_vec_disable(priv);
334 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
335 if (intr_handle->intr_vec == NULL) {
336 ERROR("failed to allocate memory for interrupt vector,"
337 " Rx interrupts will not be supported");
340 intr_handle->type = RTE_INTR_HANDLE_EXT;
341 for (i = 0; i != n; ++i) {
342 /* This rxq ibv must not be released in this function. */
343 struct mlx5_rxq_ibv *rxq_ibv = mlx5_priv_rxq_ibv_get(priv, i);
348 /* Skip queues that cannot request interrupts. */
349 if (!rxq_ibv || !rxq_ibv->channel) {
350 /* Use invalid intr_vec[] index to disable entry. */
351 intr_handle->intr_vec[i] =
352 RTE_INTR_VEC_RXTX_OFFSET +
353 RTE_MAX_RXTX_INTR_VEC_ID;
356 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
357 ERROR("too many Rx queues for interrupt vector size"
358 " (%d), Rx interrupts cannot be enabled",
359 RTE_MAX_RXTX_INTR_VEC_ID);
360 priv_rx_intr_vec_disable(priv);
363 fd = rxq_ibv->channel->fd;
364 flags = fcntl(fd, F_GETFL);
365 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
367 ERROR("failed to make Rx interrupt file descriptor"
368 " %d non-blocking for queue index %d", fd, i);
369 priv_rx_intr_vec_disable(priv);
372 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
373 intr_handle->efds[count] = fd;
377 priv_rx_intr_vec_disable(priv);
379 intr_handle->nb_efd = count;
384 * Clean up Rx interrupts handler.
387 * Pointer to private structure.
390 priv_rx_intr_vec_disable(struct priv *priv)
392 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
394 unsigned int rxqs_n = priv->rxqs_n;
395 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
397 if (!priv->dev->data->dev_conf.intr_conf.rxq)
399 for (i = 0; i != n; ++i) {
400 struct mlx5_rxq_ctrl *rxq_ctrl;
401 struct mlx5_rxq_data *rxq_data;
403 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
404 RTE_MAX_RXTX_INTR_VEC_ID)
407 * Need to access directly the queue to release the reference
408 * kept in priv_rx_intr_vec_enable().
410 rxq_data = (*priv->rxqs)[i];
411 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
412 mlx5_priv_rxq_ibv_release(priv, rxq_ctrl->ibv);
414 rte_intr_free_epoll_fd(intr_handle);
415 free(intr_handle->intr_vec);
416 intr_handle->nb_efd = 0;
417 intr_handle->intr_vec = NULL;
421 * MLX5 CQ notification .
424 * Pointer to receive queue structure.
426 * Sequence number per receive queue .
429 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
432 uint32_t doorbell_hi;
434 void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
436 sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
437 doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
438 doorbell = (uint64_t)doorbell_hi << 32;
439 doorbell |= rxq->cqn;
440 rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
442 rte_write64(rte_cpu_to_be_64(doorbell), cq_db_reg);
446 * DPDK callback for Rx queue interrupt enable.
449 * Pointer to Ethernet device structure.
454 * 0 on success, negative on failure.
457 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
459 struct priv *priv = mlx5_get_priv(dev);
460 struct mlx5_rxq_data *rxq_data;
461 struct mlx5_rxq_ctrl *rxq_ctrl;
465 rxq_data = (*priv->rxqs)[rx_queue_id];
470 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
472 struct mlx5_rxq_ibv *rxq_ibv;
474 rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
479 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
480 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
485 WARN("unable to arm interrupt on rx queue %d", rx_queue_id);
490 * DPDK callback for Rx queue interrupt disable.
493 * Pointer to Ethernet device structure.
498 * 0 on success, negative on failure.
501 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
503 struct priv *priv = mlx5_get_priv(dev);
504 struct mlx5_rxq_data *rxq_data;
505 struct mlx5_rxq_ctrl *rxq_ctrl;
506 struct mlx5_rxq_ibv *rxq_ibv = NULL;
507 struct ibv_cq *ev_cq;
512 rxq_data = (*priv->rxqs)[rx_queue_id];
517 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
520 rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
525 ret = ibv_get_cq_event(rxq_ibv->channel, &ev_cq, &ev_ctx);
526 if (ret || ev_cq != rxq_ibv->cq) {
530 rxq_data->cq_arm_sn++;
531 ibv_ack_cq_events(rxq_ibv->cq, 1);
534 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
537 WARN("unable to disable interrupt on rx queue %d",
543 * Create the Rx queue Verbs object.
546 * Pointer to private structure.
548 * Queue index in DPDK Rx queue array
551 * The Verbs object initialised if it can be created.
554 mlx5_priv_rxq_ibv_new(struct priv *priv, uint16_t idx)
556 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
557 struct mlx5_rxq_ctrl *rxq_ctrl =
558 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
559 struct ibv_wq_attr mod;
561 struct ibv_cq_init_attr_ex cq;
562 struct ibv_wq_init_attr wq;
563 struct ibv_cq_ex cq_attr;
565 unsigned int cqe_n = (1 << rxq_data->elts_n) - 1;
566 struct mlx5_rxq_ibv *tmpl;
567 struct mlx5dv_cq cq_info;
568 struct mlx5dv_rwq rwq;
571 struct mlx5dv_obj obj;
574 assert(!rxq_ctrl->ibv);
575 tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
578 ERROR("%p: cannot allocate verbs resources",
582 tmpl->rxq_ctrl = rxq_ctrl;
583 /* Use the entire RX mempool as the memory region. */
584 tmpl->mr = priv_mr_get(priv, rxq_data->mp);
586 tmpl->mr = priv_mr_new(priv, rxq_data->mp);
588 ERROR("%p: MR creation failure", (void *)rxq_ctrl);
593 tmpl->channel = ibv_create_comp_channel(priv->ctx);
594 if (!tmpl->channel) {
595 ERROR("%p: Comp Channel creation failure",
600 attr.cq = (struct ibv_cq_init_attr_ex){
603 if (priv->cqe_comp) {
604 attr.cq.comp_mask |= IBV_CQ_INIT_ATTR_MASK_FLAGS;
605 attr.cq.flags |= MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
607 * For vectorized Rx, it must not be doubled in order to
608 * make cq_ci and rq_ci aligned.
610 if (rxq_check_vec_support(rxq_data) < 0)
613 tmpl->cq = ibv_create_cq(priv->ctx, cqe_n, NULL, tmpl->channel, 0);
614 if (tmpl->cq == NULL) {
615 ERROR("%p: CQ creation failure", (void *)rxq_ctrl);
618 DEBUG("priv->device_attr.max_qp_wr is %d",
619 priv->device_attr.orig_attr.max_qp_wr);
620 DEBUG("priv->device_attr.max_sge is %d",
621 priv->device_attr.orig_attr.max_sge);
622 attr.wq = (struct ibv_wq_init_attr){
623 .wq_context = NULL, /* Could be useful in the future. */
624 .wq_type = IBV_WQT_RQ,
625 /* Max number of outstanding WRs. */
626 .max_wr = (1 << rxq_data->elts_n) >> rxq_data->sges_n,
627 /* Max number of scatter/gather elements in a WR. */
628 .max_sge = 1 << rxq_data->sges_n,
632 IBV_WQ_FLAGS_CVLAN_STRIPPING |
634 .create_flags = (rxq_data->vlan_strip ?
635 IBV_WQ_FLAGS_CVLAN_STRIPPING :
638 /* By default, FCS (CRC) is stripped by hardware. */
639 if (rxq_data->crc_present) {
640 attr.wq.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
641 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
643 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
644 if (priv->hw_padding) {
645 attr.wq.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
646 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
649 tmpl->wq = ibv_create_wq(priv->ctx, &attr.wq);
650 if (tmpl->wq == NULL) {
651 ERROR("%p: WQ creation failure", (void *)rxq_ctrl);
655 * Make sure number of WRs*SGEs match expectations since a queue
656 * cannot allocate more than "desc" buffers.
658 if (((int)attr.wq.max_wr !=
659 ((1 << rxq_data->elts_n) >> rxq_data->sges_n)) ||
660 ((int)attr.wq.max_sge != (1 << rxq_data->sges_n))) {
661 ERROR("%p: requested %u*%u but got %u*%u WRs*SGEs",
663 ((1 << rxq_data->elts_n) >> rxq_data->sges_n),
664 (1 << rxq_data->sges_n),
665 attr.wq.max_wr, attr.wq.max_sge);
668 /* Change queue state to ready. */
669 mod = (struct ibv_wq_attr){
670 .attr_mask = IBV_WQ_ATTR_STATE,
671 .wq_state = IBV_WQS_RDY,
673 ret = ibv_modify_wq(tmpl->wq, &mod);
675 ERROR("%p: WQ state to IBV_WQS_RDY failed",
679 obj.cq.in = tmpl->cq;
680 obj.cq.out = &cq_info;
681 obj.rwq.in = tmpl->wq;
683 ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_RWQ);
686 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
687 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
688 "it should be set to %u", RTE_CACHE_LINE_SIZE);
691 /* Fill the rings. */
692 rxq_data->wqes = (volatile struct mlx5_wqe_data_seg (*)[])
694 for (i = 0; (i != (unsigned int)(1 << rxq_data->elts_n)); ++i) {
695 struct rte_mbuf *buf = (*rxq_data->elts)[i];
696 volatile struct mlx5_wqe_data_seg *scat = &(*rxq_data->wqes)[i];
698 /* scat->addr must be able to store a pointer. */
699 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
700 *scat = (struct mlx5_wqe_data_seg){
701 .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
703 .byte_count = rte_cpu_to_be_32(DATA_LEN(buf)),
704 .lkey = tmpl->mr->lkey,
707 rxq_data->rq_db = rwq.dbrec;
708 rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
712 rxq_data->zip = (struct rxq_zip){
715 rxq_data->cq_db = cq_info.dbrec;
716 rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
717 /* Update doorbell counter. */
718 rxq_data->rq_ci = (1 << rxq_data->elts_n) >> rxq_data->sges_n;
720 *rxq_data->rq_db = rte_cpu_to_be_32(rxq_data->rq_ci);
721 DEBUG("%p: rxq updated with %p", (void *)rxq_ctrl, (void *)&tmpl);
722 rte_atomic32_inc(&tmpl->refcnt);
723 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
724 (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
725 LIST_INSERT_HEAD(&priv->rxqsibv, tmpl, next);
729 claim_zero(ibv_destroy_wq(tmpl->wq));
731 claim_zero(ibv_destroy_cq(tmpl->cq));
733 claim_zero(ibv_destroy_comp_channel(tmpl->channel));
735 priv_mr_release(priv, tmpl->mr);
740 * Get an Rx queue Verbs object.
743 * Pointer to private structure.
745 * Queue index in DPDK Rx queue array
748 * The Verbs object if it exists.
751 mlx5_priv_rxq_ibv_get(struct priv *priv, uint16_t idx)
753 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
754 struct mlx5_rxq_ctrl *rxq_ctrl;
756 if (idx >= priv->rxqs_n)
760 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
762 priv_mr_get(priv, rxq_data->mp);
763 rte_atomic32_inc(&rxq_ctrl->ibv->refcnt);
764 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
765 (void *)rxq_ctrl->ibv,
766 rte_atomic32_read(&rxq_ctrl->ibv->refcnt));
768 return rxq_ctrl->ibv;
772 * Release an Rx verbs queue object.
775 * Pointer to private structure.
777 * Verbs Rx queue object.
780 * 0 on success, errno value on failure.
783 mlx5_priv_rxq_ibv_release(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
791 ret = priv_mr_release(priv, rxq_ibv->mr);
794 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
795 (void *)rxq_ibv, rte_atomic32_read(&rxq_ibv->refcnt));
796 if (rte_atomic32_dec_and_test(&rxq_ibv->refcnt)) {
797 rxq_free_elts(rxq_ibv->rxq_ctrl);
798 claim_zero(ibv_destroy_wq(rxq_ibv->wq));
799 claim_zero(ibv_destroy_cq(rxq_ibv->cq));
800 if (rxq_ibv->channel)
801 claim_zero(ibv_destroy_comp_channel(rxq_ibv->channel));
802 LIST_REMOVE(rxq_ibv, next);
810 * Verify the Verbs Rx queue list is empty
813 * Pointer to private structure.
815 * @return the number of object not released.
818 mlx5_priv_rxq_ibv_verify(struct priv *priv)
821 struct mlx5_rxq_ibv *rxq_ibv;
823 LIST_FOREACH(rxq_ibv, &priv->rxqsibv, next) {
824 DEBUG("%p: Verbs Rx queue %p still referenced", (void *)priv,
832 * Return true if a single reference exists on the object.
835 * Pointer to private structure.
837 * Verbs Rx queue object.
840 mlx5_priv_rxq_ibv_releasable(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
844 return (rte_atomic32_read(&rxq_ibv->refcnt) == 1);
848 * Create a DPDK Rx queue.
851 * Pointer to private structure.
855 * Number of descriptors to configure in queue.
857 * NUMA socket on which memory must be allocated.
860 * A DPDK queue object on success.
862 struct mlx5_rxq_ctrl*
863 mlx5_priv_rxq_new(struct priv *priv, uint16_t idx, uint16_t desc,
864 unsigned int socket, struct rte_mempool *mp)
866 struct rte_eth_dev *dev = priv->dev;
867 struct mlx5_rxq_ctrl *tmpl;
868 const uint16_t desc_n =
869 desc + priv->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
870 unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
872 tmpl = rte_calloc_socket("RXQ", 1,
874 desc_n * sizeof(struct rte_mbuf *),
878 if (priv->dev->data->dev_conf.intr_conf.rxq)
880 /* Enable scattered packets support for this queue if necessary. */
881 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
882 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
883 (mb_len - RTE_PKTMBUF_HEADROOM)) {
884 tmpl->rxq.sges_n = 0;
885 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
887 RTE_PKTMBUF_HEADROOM +
888 dev->data->dev_conf.rxmode.max_rx_pkt_len;
892 * Determine the number of SGEs needed for a full packet
893 * and round it to the next power of two.
895 sges_n = log2above((size / mb_len) + !!(size % mb_len));
896 tmpl->rxq.sges_n = sges_n;
897 /* Make sure rxq.sges_n did not overflow. */
898 size = mb_len * (1 << tmpl->rxq.sges_n);
899 size -= RTE_PKTMBUF_HEADROOM;
900 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
901 ERROR("%p: too many SGEs (%u) needed to handle"
902 " requested maximum packet size %u",
905 dev->data->dev_conf.rxmode.max_rx_pkt_len);
909 WARN("%p: the requested maximum Rx packet size (%u) is"
910 " larger than a single mbuf (%u) and scattered"
911 " mode has not been requested",
913 dev->data->dev_conf.rxmode.max_rx_pkt_len,
914 mb_len - RTE_PKTMBUF_HEADROOM);
916 DEBUG("%p: maximum number of segments per packet: %u",
917 (void *)dev, 1 << tmpl->rxq.sges_n);
918 if (desc % (1 << tmpl->rxq.sges_n)) {
919 ERROR("%p: number of RX queue descriptors (%u) is not a"
920 " multiple of SGEs per packet (%u)",
923 1 << tmpl->rxq.sges_n);
926 /* Toggle RX checksum offload if hardware supports it. */
928 tmpl->rxq.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
929 if (priv->hw_csum_l2tun)
930 tmpl->rxq.csum_l2tun =
931 !!dev->data->dev_conf.rxmode.hw_ip_checksum;
932 /* Configure VLAN stripping. */
933 tmpl->rxq.vlan_strip = (priv->hw_vlan_strip &&
934 !!dev->data->dev_conf.rxmode.hw_vlan_strip);
935 /* By default, FCS (CRC) is stripped by hardware. */
936 if (dev->data->dev_conf.rxmode.hw_strip_crc) {
937 tmpl->rxq.crc_present = 0;
938 } else if (priv->hw_fcs_strip) {
939 tmpl->rxq.crc_present = 1;
941 WARN("%p: CRC stripping has been disabled but will still"
942 " be performed by hardware, make sure MLNX_OFED and"
943 " firmware are up to date",
945 tmpl->rxq.crc_present = 0;
947 DEBUG("%p: CRC stripping is %s, %u bytes will be subtracted from"
948 " incoming frames to hide it",
950 tmpl->rxq.crc_present ? "disabled" : "enabled",
951 tmpl->rxq.crc_present << 2);
953 tmpl->rxq.rss_hash = priv->rxqs_n > 1;
954 tmpl->rxq.port_id = dev->data->port_id;
957 tmpl->rxq.stats.idx = idx;
958 tmpl->rxq.elts_n = log2above(desc);
960 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
961 rte_atomic32_inc(&tmpl->refcnt);
962 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
963 (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
964 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
975 * Pointer to private structure.
980 * A pointer to the queue if it exists.
982 struct mlx5_rxq_ctrl*
983 mlx5_priv_rxq_get(struct priv *priv, uint16_t idx)
985 struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
987 if ((*priv->rxqs)[idx]) {
988 rxq_ctrl = container_of((*priv->rxqs)[idx],
989 struct mlx5_rxq_ctrl,
992 mlx5_priv_rxq_ibv_get(priv, idx);
993 rte_atomic32_inc(&rxq_ctrl->refcnt);
994 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
995 (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1001 * Release a Rx queue.
1004 * Pointer to private structure.
1009 * 0 on success, errno value on failure.
1012 mlx5_priv_rxq_release(struct priv *priv, uint16_t idx)
1014 struct mlx5_rxq_ctrl *rxq_ctrl;
1016 if (!(*priv->rxqs)[idx])
1018 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1019 assert(rxq_ctrl->priv);
1020 if (rxq_ctrl->ibv) {
1023 ret = mlx5_priv_rxq_ibv_release(rxq_ctrl->priv, rxq_ctrl->ibv);
1025 rxq_ctrl->ibv = NULL;
1027 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1028 (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1029 if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1030 LIST_REMOVE(rxq_ctrl, next);
1032 (*priv->rxqs)[idx] = NULL;
1039 * Verify if the queue can be released.
1042 * Pointer to private structure.
1047 * 1 if the queue can be released.
1050 mlx5_priv_rxq_releasable(struct priv *priv, uint16_t idx)
1052 struct mlx5_rxq_ctrl *rxq_ctrl;
1054 if (!(*priv->rxqs)[idx])
1056 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1057 return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
1061 * Verify the Rx Queue list is empty
1064 * Pointer to private structure.
1066 * @return the number of object not released.
1069 mlx5_priv_rxq_verify(struct priv *priv)
1071 struct mlx5_rxq_ctrl *rxq_ctrl;
1074 LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1075 DEBUG("%p: Rx Queue %p still referenced", (void *)priv,
1083 * Create an indirection table.
1086 * Pointer to private structure.
1088 * Queues entering in the indirection table.
1090 * Number of queues in the array.
1093 * A new indirection table.
1095 struct mlx5_ind_table_ibv*
1096 mlx5_priv_ind_table_ibv_new(struct priv *priv, uint16_t queues[],
1099 struct mlx5_ind_table_ibv *ind_tbl;
1100 const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1101 log2above(queues_n) :
1102 priv->ind_table_max_size;
1103 struct ibv_wq *wq[1 << wq_n];
1107 ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1108 queues_n * sizeof(uint16_t), 0);
1111 for (i = 0; i != queues_n; ++i) {
1112 struct mlx5_rxq_ctrl *rxq =
1113 mlx5_priv_rxq_get(priv, queues[i]);
1117 wq[i] = rxq->ibv->wq;
1118 ind_tbl->queues[i] = queues[i];
1120 ind_tbl->queues_n = queues_n;
1121 /* Finalise indirection table. */
1122 for (j = 0; i != (unsigned int)(1 << wq_n); ++i, ++j)
1124 ind_tbl->ind_table = ibv_create_rwq_ind_table(
1126 &(struct ibv_rwq_ind_table_init_attr){
1127 .log_ind_tbl_size = wq_n,
1131 if (!ind_tbl->ind_table)
1133 rte_atomic32_inc(&ind_tbl->refcnt);
1134 LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1135 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1136 (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1140 DEBUG("%p cannot create indirection table", (void *)priv);
1145 * Get an indirection table.
1148 * Pointer to private structure.
1150 * Queues entering in the indirection table.
1152 * Number of queues in the array.
1155 * An indirection table if found.
1157 struct mlx5_ind_table_ibv*
1158 mlx5_priv_ind_table_ibv_get(struct priv *priv, uint16_t queues[],
1161 struct mlx5_ind_table_ibv *ind_tbl;
1163 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1164 if ((ind_tbl->queues_n == queues_n) &&
1165 (memcmp(ind_tbl->queues, queues,
1166 ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1173 rte_atomic32_inc(&ind_tbl->refcnt);
1174 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1175 (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1176 for (i = 0; i != ind_tbl->queues_n; ++i)
1177 mlx5_priv_rxq_get(priv, ind_tbl->queues[i]);
1183 * Release an indirection table.
1186 * Pointer to private structure.
1188 * Indirection table to release.
1191 * 0 on success, errno value on failure.
1194 mlx5_priv_ind_table_ibv_release(struct priv *priv,
1195 struct mlx5_ind_table_ibv *ind_tbl)
1199 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1200 (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1201 if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1202 claim_zero(ibv_destroy_rwq_ind_table(ind_tbl->ind_table));
1203 for (i = 0; i != ind_tbl->queues_n; ++i)
1204 claim_nonzero(mlx5_priv_rxq_release(priv, ind_tbl->queues[i]));
1205 if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1206 LIST_REMOVE(ind_tbl, next);
1214 * Verify the Rx Queue list is empty
1217 * Pointer to private structure.
1219 * @return the number of object not released.
1222 mlx5_priv_ind_table_ibv_verify(struct priv *priv)
1224 struct mlx5_ind_table_ibv *ind_tbl;
1227 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1228 DEBUG("%p: Verbs indirection table %p still referenced",
1229 (void *)priv, (void *)ind_tbl);
1236 * Create an Rx Hash queue.
1239 * Pointer to private structure.
1241 * RSS key for the Rx hash queue.
1242 * @param rss_key_len
1244 * @param hash_fields
1245 * Verbs protocol hash field to make the RSS on.
1247 * Queues entering in hash queue.
1252 * An hash Rx queue on success.
1255 mlx5_priv_hrxq_new(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1256 uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1258 struct mlx5_hrxq *hrxq;
1259 struct mlx5_ind_table_ibv *ind_tbl;
1262 ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1264 ind_tbl = mlx5_priv_ind_table_ibv_new(priv, queues, queues_n);
1267 qp = ibv_create_qp_ex(
1269 &(struct ibv_qp_init_attr_ex){
1270 .qp_type = IBV_QPT_RAW_PACKET,
1272 IBV_QP_INIT_ATTR_PD |
1273 IBV_QP_INIT_ATTR_IND_TABLE |
1274 IBV_QP_INIT_ATTR_RX_HASH,
1275 .rx_hash_conf = (struct ibv_rx_hash_conf){
1276 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1277 .rx_hash_key_len = rss_key_len,
1278 .rx_hash_key = rss_key,
1279 .rx_hash_fields_mask = hash_fields,
1281 .rwq_ind_tbl = ind_tbl->ind_table,
1286 hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
1289 hrxq->ind_table = ind_tbl;
1291 hrxq->rss_key_len = rss_key_len;
1292 hrxq->hash_fields = hash_fields;
1293 memcpy(hrxq->rss_key, rss_key, rss_key_len);
1294 rte_atomic32_inc(&hrxq->refcnt);
1295 LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
1296 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1297 (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1300 mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1302 claim_zero(ibv_destroy_qp(qp));
1307 * Get an Rx Hash queue.
1310 * Pointer to private structure.
1312 * RSS configuration for the Rx hash queue.
1314 * Queues entering in hash queue.
1319 * An hash Rx queue on success.
1322 mlx5_priv_hrxq_get(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1323 uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1325 struct mlx5_hrxq *hrxq;
1327 LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1328 struct mlx5_ind_table_ibv *ind_tbl;
1330 if (hrxq->rss_key_len != rss_key_len)
1332 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1334 if (hrxq->hash_fields != hash_fields)
1336 ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1339 if (ind_tbl != hrxq->ind_table) {
1340 mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1343 rte_atomic32_inc(&hrxq->refcnt);
1344 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1345 (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1352 * Release the hash Rx queue.
1355 * Pointer to private structure.
1357 * Pointer to Hash Rx queue to release.
1360 * 0 on success, errno value on failure.
1363 mlx5_priv_hrxq_release(struct priv *priv, struct mlx5_hrxq *hrxq)
1365 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1366 (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1367 if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1368 claim_zero(ibv_destroy_qp(hrxq->qp));
1369 mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table);
1370 LIST_REMOVE(hrxq, next);
1374 claim_nonzero(mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table));
1379 * Verify the Rx Queue list is empty
1382 * Pointer to private structure.
1384 * @return the number of object not released.
1387 mlx5_priv_hrxq_ibv_verify(struct priv *priv)
1389 struct mlx5_hrxq *hrxq;
1392 LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1393 DEBUG("%p: Verbs Hash Rx queue %p still referenced",
1394 (void *)priv, (void *)hrxq);