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));
216 * Returns the per-queue supported offloads.
219 * Pointer to private structure.
222 * Supported Rx offloads.
225 mlx5_priv_get_rx_queue_offloads(struct priv *priv)
227 struct mlx5_dev_config *config = &priv->config;
228 uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
229 DEV_RX_OFFLOAD_TIMESTAMP |
230 DEV_RX_OFFLOAD_JUMBO_FRAME);
232 if (config->hw_fcs_strip)
233 offloads |= DEV_RX_OFFLOAD_CRC_STRIP;
235 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
236 DEV_RX_OFFLOAD_UDP_CKSUM |
237 DEV_RX_OFFLOAD_TCP_CKSUM);
238 if (config->hw_vlan_strip)
239 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
245 * Returns the per-port supported offloads.
248 * Pointer to private structure.
250 * Supported Rx offloads.
253 mlx5_priv_get_rx_port_offloads(struct priv *priv __rte_unused)
255 uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
261 * Checks if the per-queue offload configuration is valid.
264 * Pointer to private structure.
266 * Per-queue offloads configuration.
269 * 1 if the configuration is valid, 0 otherwise.
272 priv_is_rx_queue_offloads_allowed(struct priv *priv, uint64_t offloads)
274 uint64_t port_offloads = priv->dev->data->dev_conf.rxmode.offloads;
275 uint64_t queue_supp_offloads =
276 mlx5_priv_get_rx_queue_offloads(priv);
277 uint64_t port_supp_offloads = mlx5_priv_get_rx_port_offloads(priv);
279 if ((offloads & (queue_supp_offloads | port_supp_offloads)) !=
282 if (((port_offloads ^ offloads) & port_supp_offloads))
290 * Pointer to Ethernet device structure.
294 * Number of descriptors to configure in queue.
296 * NUMA socket on which memory must be allocated.
298 * Thresholds parameters.
300 * Memory pool for buffer allocations.
303 * 0 on success, negative errno value on failure.
306 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
307 unsigned int socket, const struct rte_eth_rxconf *conf,
308 struct rte_mempool *mp)
310 struct priv *priv = dev->data->dev_private;
311 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
312 struct mlx5_rxq_ctrl *rxq_ctrl =
313 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
317 if (!rte_is_power_of_2(desc)) {
318 desc = 1 << log2above(desc);
319 WARN("%p: increased number of descriptors in RX queue %u"
320 " to the next power of two (%d)",
321 (void *)dev, idx, desc);
323 DEBUG("%p: configuring queue %u for %u descriptors",
324 (void *)dev, idx, desc);
325 if (idx >= priv->rxqs_n) {
326 ERROR("%p: queue index out of range (%u >= %u)",
327 (void *)dev, idx, priv->rxqs_n);
331 if (!priv_is_rx_queue_offloads_allowed(priv, conf->offloads)) {
333 ERROR("%p: Rx queue offloads 0x%" PRIx64 " don't match port "
334 "offloads 0x%" PRIx64 " or supported offloads 0x%" PRIx64,
335 (void *)dev, conf->offloads,
336 dev->data->dev_conf.rxmode.offloads,
337 (mlx5_priv_get_rx_port_offloads(priv) |
338 mlx5_priv_get_rx_queue_offloads(priv)));
341 if (!mlx5_priv_rxq_releasable(priv, idx)) {
343 ERROR("%p: unable to release queue index %u",
347 mlx5_priv_rxq_release(priv, idx);
348 rxq_ctrl = mlx5_priv_rxq_new(priv, idx, desc, socket, conf, mp);
350 ERROR("%p: unable to allocate queue index %u",
355 DEBUG("%p: adding RX queue %p to list",
356 (void *)dev, (void *)rxq_ctrl);
357 (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
364 * DPDK callback to release a RX queue.
367 * Generic RX queue pointer.
370 mlx5_rx_queue_release(void *dpdk_rxq)
372 struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
373 struct mlx5_rxq_ctrl *rxq_ctrl;
378 rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
379 priv = rxq_ctrl->priv;
381 if (!mlx5_priv_rxq_releasable(priv, rxq_ctrl->rxq.stats.idx))
382 rte_panic("Rx queue %p is still used by a flow and cannot be"
383 " removed\n", (void *)rxq_ctrl);
384 mlx5_priv_rxq_release(priv, rxq_ctrl->rxq.stats.idx);
389 * Allocate queue vector and fill epoll fd list for Rx interrupts.
392 * Pointer to private structure.
395 * 0 on success, negative on failure.
398 priv_rx_intr_vec_enable(struct priv *priv)
401 unsigned int rxqs_n = priv->rxqs_n;
402 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
403 unsigned int count = 0;
404 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
406 if (!priv->dev->data->dev_conf.intr_conf.rxq)
408 priv_rx_intr_vec_disable(priv);
409 intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
410 if (intr_handle->intr_vec == NULL) {
411 ERROR("failed to allocate memory for interrupt vector,"
412 " Rx interrupts will not be supported");
415 intr_handle->type = RTE_INTR_HANDLE_EXT;
416 for (i = 0; i != n; ++i) {
417 /* This rxq ibv must not be released in this function. */
418 struct mlx5_rxq_ibv *rxq_ibv = mlx5_priv_rxq_ibv_get(priv, i);
423 /* Skip queues that cannot request interrupts. */
424 if (!rxq_ibv || !rxq_ibv->channel) {
425 /* Use invalid intr_vec[] index to disable entry. */
426 intr_handle->intr_vec[i] =
427 RTE_INTR_VEC_RXTX_OFFSET +
428 RTE_MAX_RXTX_INTR_VEC_ID;
431 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
432 ERROR("too many Rx queues for interrupt vector size"
433 " (%d), Rx interrupts cannot be enabled",
434 RTE_MAX_RXTX_INTR_VEC_ID);
435 priv_rx_intr_vec_disable(priv);
438 fd = rxq_ibv->channel->fd;
439 flags = fcntl(fd, F_GETFL);
440 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
442 ERROR("failed to make Rx interrupt file descriptor"
443 " %d non-blocking for queue index %d", fd, i);
444 priv_rx_intr_vec_disable(priv);
447 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
448 intr_handle->efds[count] = fd;
452 priv_rx_intr_vec_disable(priv);
454 intr_handle->nb_efd = count;
459 * Clean up Rx interrupts handler.
462 * Pointer to private structure.
465 priv_rx_intr_vec_disable(struct priv *priv)
467 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
469 unsigned int rxqs_n = priv->rxqs_n;
470 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
472 if (!priv->dev->data->dev_conf.intr_conf.rxq)
474 if (!intr_handle->intr_vec)
476 for (i = 0; i != n; ++i) {
477 struct mlx5_rxq_ctrl *rxq_ctrl;
478 struct mlx5_rxq_data *rxq_data;
480 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
481 RTE_MAX_RXTX_INTR_VEC_ID)
484 * Need to access directly the queue to release the reference
485 * kept in priv_rx_intr_vec_enable().
487 rxq_data = (*priv->rxqs)[i];
488 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
489 mlx5_priv_rxq_ibv_release(priv, rxq_ctrl->ibv);
492 rte_intr_free_epoll_fd(intr_handle);
493 if (intr_handle->intr_vec)
494 free(intr_handle->intr_vec);
495 intr_handle->nb_efd = 0;
496 intr_handle->intr_vec = NULL;
500 * MLX5 CQ notification .
503 * Pointer to receive queue structure.
505 * Sequence number per receive queue .
508 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
511 uint32_t doorbell_hi;
513 void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
515 sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
516 doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
517 doorbell = (uint64_t)doorbell_hi << 32;
518 doorbell |= rxq->cqn;
519 rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
521 rte_write64(rte_cpu_to_be_64(doorbell), cq_db_reg);
525 * DPDK callback for Rx queue interrupt enable.
528 * Pointer to Ethernet device structure.
533 * 0 on success, negative on failure.
536 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
538 struct priv *priv = dev->data->dev_private;
539 struct mlx5_rxq_data *rxq_data;
540 struct mlx5_rxq_ctrl *rxq_ctrl;
544 rxq_data = (*priv->rxqs)[rx_queue_id];
549 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
551 struct mlx5_rxq_ibv *rxq_ibv;
553 rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
558 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
559 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
564 WARN("unable to arm interrupt on rx queue %d", rx_queue_id);
569 * DPDK callback for Rx queue interrupt disable.
572 * Pointer to Ethernet device structure.
577 * 0 on success, negative on failure.
580 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
582 struct priv *priv = dev->data->dev_private;
583 struct mlx5_rxq_data *rxq_data;
584 struct mlx5_rxq_ctrl *rxq_ctrl;
585 struct mlx5_rxq_ibv *rxq_ibv = NULL;
586 struct ibv_cq *ev_cq;
591 rxq_data = (*priv->rxqs)[rx_queue_id];
596 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
599 rxq_ibv = mlx5_priv_rxq_ibv_get(priv, rx_queue_id);
604 ret = ibv_get_cq_event(rxq_ibv->channel, &ev_cq, &ev_ctx);
605 if (ret || ev_cq != rxq_ibv->cq) {
609 rxq_data->cq_arm_sn++;
610 ibv_ack_cq_events(rxq_ibv->cq, 1);
613 mlx5_priv_rxq_ibv_release(priv, rxq_ibv);
616 WARN("unable to disable interrupt on rx queue %d",
622 * Create the Rx queue Verbs object.
625 * Pointer to private structure.
627 * Queue index in DPDK Rx queue array
630 * The Verbs object initialised if it can be created.
633 mlx5_priv_rxq_ibv_new(struct priv *priv, uint16_t idx)
635 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
636 struct mlx5_rxq_ctrl *rxq_ctrl =
637 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
638 struct ibv_wq_attr mod;
641 struct ibv_cq_init_attr_ex ibv;
642 struct mlx5dv_cq_init_attr mlx5;
644 struct ibv_wq_init_attr wq;
645 struct ibv_cq_ex cq_attr;
647 unsigned int cqe_n = (1 << rxq_data->elts_n) - 1;
648 struct mlx5_rxq_ibv *tmpl;
649 struct mlx5dv_cq cq_info;
650 struct mlx5dv_rwq rwq;
653 struct mlx5dv_obj obj;
654 struct mlx5_dev_config *config = &priv->config;
657 assert(!rxq_ctrl->ibv);
658 tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
661 ERROR("%p: cannot allocate verbs resources",
665 tmpl->rxq_ctrl = rxq_ctrl;
666 /* Use the entire RX mempool as the memory region. */
667 tmpl->mr = priv_mr_get(priv, rxq_data->mp);
669 tmpl->mr = priv_mr_new(priv, rxq_data->mp);
671 ERROR("%p: MR creation failure", (void *)rxq_ctrl);
676 tmpl->channel = ibv_create_comp_channel(priv->ctx);
677 if (!tmpl->channel) {
678 ERROR("%p: Comp Channel creation failure",
683 attr.cq.ibv = (struct ibv_cq_init_attr_ex){
685 .channel = tmpl->channel,
688 attr.cq.mlx5 = (struct mlx5dv_cq_init_attr){
691 if (config->cqe_comp && !rxq_data->hw_timestamp) {
692 attr.cq.mlx5.comp_mask |=
693 MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
694 attr.cq.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
696 * For vectorized Rx, it must not be doubled in order to
697 * make cq_ci and rq_ci aligned.
699 if (rxq_check_vec_support(rxq_data) < 0)
700 attr.cq.ibv.cqe *= 2;
701 } else if (config->cqe_comp && rxq_data->hw_timestamp) {
702 DEBUG("Rx CQE compression is disabled for HW timestamp");
704 tmpl->cq = ibv_cq_ex_to_cq(mlx5dv_create_cq(priv->ctx, &attr.cq.ibv,
706 if (tmpl->cq == NULL) {
707 ERROR("%p: CQ creation failure", (void *)rxq_ctrl);
710 DEBUG("priv->device_attr.max_qp_wr is %d",
711 priv->device_attr.orig_attr.max_qp_wr);
712 DEBUG("priv->device_attr.max_sge is %d",
713 priv->device_attr.orig_attr.max_sge);
714 attr.wq = (struct ibv_wq_init_attr){
715 .wq_context = NULL, /* Could be useful in the future. */
716 .wq_type = IBV_WQT_RQ,
717 /* Max number of outstanding WRs. */
718 .max_wr = (1 << rxq_data->elts_n) >> rxq_data->sges_n,
719 /* Max number of scatter/gather elements in a WR. */
720 .max_sge = 1 << rxq_data->sges_n,
724 IBV_WQ_FLAGS_CVLAN_STRIPPING |
726 .create_flags = (rxq_data->vlan_strip ?
727 IBV_WQ_FLAGS_CVLAN_STRIPPING :
730 /* By default, FCS (CRC) is stripped by hardware. */
731 if (rxq_data->crc_present) {
732 attr.wq.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
733 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
735 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
736 if (config->hw_padding) {
737 attr.wq.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
738 attr.wq.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
741 tmpl->wq = ibv_create_wq(priv->ctx, &attr.wq);
742 if (tmpl->wq == NULL) {
743 ERROR("%p: WQ creation failure", (void *)rxq_ctrl);
747 * Make sure number of WRs*SGEs match expectations since a queue
748 * cannot allocate more than "desc" buffers.
750 if (((int)attr.wq.max_wr !=
751 ((1 << rxq_data->elts_n) >> rxq_data->sges_n)) ||
752 ((int)attr.wq.max_sge != (1 << rxq_data->sges_n))) {
753 ERROR("%p: requested %u*%u but got %u*%u WRs*SGEs",
755 ((1 << rxq_data->elts_n) >> rxq_data->sges_n),
756 (1 << rxq_data->sges_n),
757 attr.wq.max_wr, attr.wq.max_sge);
760 /* Change queue state to ready. */
761 mod = (struct ibv_wq_attr){
762 .attr_mask = IBV_WQ_ATTR_STATE,
763 .wq_state = IBV_WQS_RDY,
765 ret = ibv_modify_wq(tmpl->wq, &mod);
767 ERROR("%p: WQ state to IBV_WQS_RDY failed",
771 obj.cq.in = tmpl->cq;
772 obj.cq.out = &cq_info;
773 obj.rwq.in = tmpl->wq;
775 ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_RWQ);
778 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
779 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
780 "it should be set to %u", RTE_CACHE_LINE_SIZE);
783 /* Fill the rings. */
784 rxq_data->wqes = (volatile struct mlx5_wqe_data_seg (*)[])
786 for (i = 0; (i != (unsigned int)(1 << rxq_data->elts_n)); ++i) {
787 struct rte_mbuf *buf = (*rxq_data->elts)[i];
788 volatile struct mlx5_wqe_data_seg *scat = &(*rxq_data->wqes)[i];
790 /* scat->addr must be able to store a pointer. */
791 assert(sizeof(scat->addr) >= sizeof(uintptr_t));
792 *scat = (struct mlx5_wqe_data_seg){
793 .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
795 .byte_count = rte_cpu_to_be_32(DATA_LEN(buf)),
796 .lkey = tmpl->mr->lkey,
799 rxq_data->rq_db = rwq.dbrec;
800 rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
804 rxq_data->zip = (struct rxq_zip){
807 rxq_data->cq_db = cq_info.dbrec;
808 rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
809 rxq_data->cq_uar = cq_info.cq_uar;
810 rxq_data->cqn = cq_info.cqn;
811 rxq_data->cq_arm_sn = 0;
812 /* Update doorbell counter. */
813 rxq_data->rq_ci = (1 << rxq_data->elts_n) >> rxq_data->sges_n;
815 *rxq_data->rq_db = rte_cpu_to_be_32(rxq_data->rq_ci);
816 DEBUG("%p: rxq updated with %p", (void *)rxq_ctrl, (void *)&tmpl);
817 rte_atomic32_inc(&tmpl->refcnt);
818 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
819 (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
820 LIST_INSERT_HEAD(&priv->rxqsibv, tmpl, next);
824 claim_zero(ibv_destroy_wq(tmpl->wq));
826 claim_zero(ibv_destroy_cq(tmpl->cq));
828 claim_zero(ibv_destroy_comp_channel(tmpl->channel));
830 priv_mr_release(priv, tmpl->mr);
835 * Get an Rx queue Verbs object.
838 * Pointer to private structure.
840 * Queue index in DPDK Rx queue array
843 * The Verbs object if it exists.
846 mlx5_priv_rxq_ibv_get(struct priv *priv, uint16_t idx)
848 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
849 struct mlx5_rxq_ctrl *rxq_ctrl;
851 if (idx >= priv->rxqs_n)
855 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
857 priv_mr_get(priv, rxq_data->mp);
858 rte_atomic32_inc(&rxq_ctrl->ibv->refcnt);
859 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
860 (void *)rxq_ctrl->ibv,
861 rte_atomic32_read(&rxq_ctrl->ibv->refcnt));
863 return rxq_ctrl->ibv;
867 * Release an Rx verbs queue object.
870 * Pointer to private structure.
872 * Verbs Rx queue object.
875 * 0 on success, errno value on failure.
878 mlx5_priv_rxq_ibv_release(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
886 ret = priv_mr_release(priv, rxq_ibv->mr);
889 DEBUG("%p: Verbs Rx queue %p: refcnt %d", (void *)priv,
890 (void *)rxq_ibv, rte_atomic32_read(&rxq_ibv->refcnt));
891 if (rte_atomic32_dec_and_test(&rxq_ibv->refcnt)) {
892 rxq_free_elts(rxq_ibv->rxq_ctrl);
893 claim_zero(ibv_destroy_wq(rxq_ibv->wq));
894 claim_zero(ibv_destroy_cq(rxq_ibv->cq));
895 if (rxq_ibv->channel)
896 claim_zero(ibv_destroy_comp_channel(rxq_ibv->channel));
897 LIST_REMOVE(rxq_ibv, next);
905 * Verify the Verbs Rx queue list is empty
908 * Pointer to private structure.
910 * @return the number of object not released.
913 mlx5_priv_rxq_ibv_verify(struct priv *priv)
916 struct mlx5_rxq_ibv *rxq_ibv;
918 LIST_FOREACH(rxq_ibv, &priv->rxqsibv, next) {
919 DEBUG("%p: Verbs Rx queue %p still referenced", (void *)priv,
927 * Return true if a single reference exists on the object.
930 * Pointer to private structure.
932 * Verbs Rx queue object.
935 mlx5_priv_rxq_ibv_releasable(struct priv *priv, struct mlx5_rxq_ibv *rxq_ibv)
939 return (rte_atomic32_read(&rxq_ibv->refcnt) == 1);
943 * Create a DPDK Rx queue.
946 * Pointer to private structure.
950 * Number of descriptors to configure in queue.
952 * NUMA socket on which memory must be allocated.
955 * A DPDK queue object on success.
957 struct mlx5_rxq_ctrl*
958 mlx5_priv_rxq_new(struct priv *priv, uint16_t idx, uint16_t desc,
959 unsigned int socket, const struct rte_eth_rxconf *conf,
960 struct rte_mempool *mp)
962 struct rte_eth_dev *dev = priv->dev;
963 struct mlx5_rxq_ctrl *tmpl;
964 unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
965 struct mlx5_dev_config *config = &priv->config;
967 * Always allocate extra slots, even if eventually
968 * the vector Rx will not be used.
970 const uint16_t desc_n =
971 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
973 tmpl = rte_calloc_socket("RXQ", 1,
975 desc_n * sizeof(struct rte_mbuf *),
979 tmpl->socket = socket;
980 if (priv->dev->data->dev_conf.intr_conf.rxq)
982 /* Enable scattered packets support for this queue if necessary. */
983 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
984 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
985 (mb_len - RTE_PKTMBUF_HEADROOM)) {
986 tmpl->rxq.sges_n = 0;
987 } else if (conf->offloads & DEV_RX_OFFLOAD_SCATTER) {
989 RTE_PKTMBUF_HEADROOM +
990 dev->data->dev_conf.rxmode.max_rx_pkt_len;
994 * Determine the number of SGEs needed for a full packet
995 * and round it to the next power of two.
997 sges_n = log2above((size / mb_len) + !!(size % mb_len));
998 tmpl->rxq.sges_n = sges_n;
999 /* Make sure rxq.sges_n did not overflow. */
1000 size = mb_len * (1 << tmpl->rxq.sges_n);
1001 size -= RTE_PKTMBUF_HEADROOM;
1002 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
1003 ERROR("%p: too many SGEs (%u) needed to handle"
1004 " requested maximum packet size %u",
1007 dev->data->dev_conf.rxmode.max_rx_pkt_len);
1011 WARN("%p: the requested maximum Rx packet size (%u) is"
1012 " larger than a single mbuf (%u) and scattered"
1013 " mode has not been requested",
1015 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1016 mb_len - RTE_PKTMBUF_HEADROOM);
1018 DEBUG("%p: maximum number of segments per packet: %u",
1019 (void *)dev, 1 << tmpl->rxq.sges_n);
1020 if (desc % (1 << tmpl->rxq.sges_n)) {
1021 ERROR("%p: number of RX queue descriptors (%u) is not a"
1022 " multiple of SGEs per packet (%u)",
1025 1 << tmpl->rxq.sges_n);
1028 /* Toggle RX checksum offload if hardware supports it. */
1029 tmpl->rxq.csum = !!(conf->offloads & DEV_RX_OFFLOAD_CHECKSUM);
1030 tmpl->rxq.csum_l2tun = (!!(conf->offloads & DEV_RX_OFFLOAD_CHECKSUM) &&
1031 priv->config.hw_csum_l2tun);
1032 tmpl->rxq.hw_timestamp = !!(conf->offloads & DEV_RX_OFFLOAD_TIMESTAMP);
1033 /* Configure VLAN stripping. */
1034 tmpl->rxq.vlan_strip = !!(conf->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1035 /* By default, FCS (CRC) is stripped by hardware. */
1036 if (conf->offloads & DEV_RX_OFFLOAD_CRC_STRIP) {
1037 tmpl->rxq.crc_present = 0;
1038 } else if (config->hw_fcs_strip) {
1039 tmpl->rxq.crc_present = 1;
1041 WARN("%p: CRC stripping has been disabled but will still"
1042 " be performed by hardware, make sure MLNX_OFED and"
1043 " firmware are up to date",
1045 tmpl->rxq.crc_present = 0;
1047 DEBUG("%p: CRC stripping is %s, %u bytes will be subtracted from"
1048 " incoming frames to hide it",
1050 tmpl->rxq.crc_present ? "disabled" : "enabled",
1051 tmpl->rxq.crc_present << 2);
1053 tmpl->rxq.rss_hash = priv->rxqs_n > 1;
1054 tmpl->rxq.port_id = dev->data->port_id;
1057 tmpl->rxq.stats.idx = idx;
1058 tmpl->rxq.elts_n = log2above(desc);
1060 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1061 rte_atomic32_inc(&tmpl->refcnt);
1062 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1063 (void *)tmpl, rte_atomic32_read(&tmpl->refcnt));
1064 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1075 * Pointer to private structure.
1080 * A pointer to the queue if it exists.
1082 struct mlx5_rxq_ctrl*
1083 mlx5_priv_rxq_get(struct priv *priv, uint16_t idx)
1085 struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1087 if ((*priv->rxqs)[idx]) {
1088 rxq_ctrl = container_of((*priv->rxqs)[idx],
1089 struct mlx5_rxq_ctrl,
1092 mlx5_priv_rxq_ibv_get(priv, idx);
1093 rte_atomic32_inc(&rxq_ctrl->refcnt);
1094 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1095 (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1101 * Release a Rx queue.
1104 * Pointer to private structure.
1109 * 0 on success, errno value on failure.
1112 mlx5_priv_rxq_release(struct priv *priv, uint16_t idx)
1114 struct mlx5_rxq_ctrl *rxq_ctrl;
1116 if (!(*priv->rxqs)[idx])
1118 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1119 assert(rxq_ctrl->priv);
1120 if (rxq_ctrl->ibv) {
1123 ret = mlx5_priv_rxq_ibv_release(rxq_ctrl->priv, rxq_ctrl->ibv);
1125 rxq_ctrl->ibv = NULL;
1127 DEBUG("%p: Rx queue %p: refcnt %d", (void *)priv,
1128 (void *)rxq_ctrl, rte_atomic32_read(&rxq_ctrl->refcnt));
1129 if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1130 LIST_REMOVE(rxq_ctrl, next);
1132 (*priv->rxqs)[idx] = NULL;
1139 * Verify if the queue can be released.
1142 * Pointer to private structure.
1147 * 1 if the queue can be released.
1150 mlx5_priv_rxq_releasable(struct priv *priv, uint16_t idx)
1152 struct mlx5_rxq_ctrl *rxq_ctrl;
1154 if (!(*priv->rxqs)[idx])
1156 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1157 return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
1161 * Verify the Rx Queue list is empty
1164 * Pointer to private structure.
1166 * @return the number of object not released.
1169 mlx5_priv_rxq_verify(struct priv *priv)
1171 struct mlx5_rxq_ctrl *rxq_ctrl;
1174 LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1175 DEBUG("%p: Rx Queue %p still referenced", (void *)priv,
1183 * Create an indirection table.
1186 * Pointer to private structure.
1188 * Queues entering in the indirection table.
1190 * Number of queues in the array.
1193 * A new indirection table.
1195 struct mlx5_ind_table_ibv*
1196 mlx5_priv_ind_table_ibv_new(struct priv *priv, uint16_t queues[],
1199 struct mlx5_ind_table_ibv *ind_tbl;
1200 const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1201 log2above(queues_n) :
1202 log2above(priv->ind_table_max_size);
1203 struct ibv_wq *wq[1 << wq_n];
1207 ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1208 queues_n * sizeof(uint16_t), 0);
1211 for (i = 0; i != queues_n; ++i) {
1212 struct mlx5_rxq_ctrl *rxq =
1213 mlx5_priv_rxq_get(priv, queues[i]);
1217 wq[i] = rxq->ibv->wq;
1218 ind_tbl->queues[i] = queues[i];
1220 ind_tbl->queues_n = queues_n;
1221 /* Finalise indirection table. */
1222 for (j = 0; i != (unsigned int)(1 << wq_n); ++i, ++j)
1224 ind_tbl->ind_table = ibv_create_rwq_ind_table(
1226 &(struct ibv_rwq_ind_table_init_attr){
1227 .log_ind_tbl_size = wq_n,
1231 if (!ind_tbl->ind_table)
1233 rte_atomic32_inc(&ind_tbl->refcnt);
1234 LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1235 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1236 (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1240 DEBUG("%p cannot create indirection table", (void *)priv);
1245 * Get an indirection table.
1248 * Pointer to private structure.
1250 * Queues entering in the indirection table.
1252 * Number of queues in the array.
1255 * An indirection table if found.
1257 struct mlx5_ind_table_ibv*
1258 mlx5_priv_ind_table_ibv_get(struct priv *priv, uint16_t queues[],
1261 struct mlx5_ind_table_ibv *ind_tbl;
1263 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1264 if ((ind_tbl->queues_n == queues_n) &&
1265 (memcmp(ind_tbl->queues, queues,
1266 ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1273 rte_atomic32_inc(&ind_tbl->refcnt);
1274 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1275 (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1276 for (i = 0; i != ind_tbl->queues_n; ++i)
1277 mlx5_priv_rxq_get(priv, ind_tbl->queues[i]);
1283 * Release an indirection table.
1286 * Pointer to private structure.
1288 * Indirection table to release.
1291 * 0 on success, errno value on failure.
1294 mlx5_priv_ind_table_ibv_release(struct priv *priv,
1295 struct mlx5_ind_table_ibv *ind_tbl)
1299 DEBUG("%p: Indirection table %p: refcnt %d", (void *)priv,
1300 (void *)ind_tbl, rte_atomic32_read(&ind_tbl->refcnt));
1301 if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1302 claim_zero(ibv_destroy_rwq_ind_table(ind_tbl->ind_table));
1303 for (i = 0; i != ind_tbl->queues_n; ++i)
1304 claim_nonzero(mlx5_priv_rxq_release(priv, ind_tbl->queues[i]));
1305 if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1306 LIST_REMOVE(ind_tbl, next);
1314 * Verify the Rx Queue list is empty
1317 * Pointer to private structure.
1319 * @return the number of object not released.
1322 mlx5_priv_ind_table_ibv_verify(struct priv *priv)
1324 struct mlx5_ind_table_ibv *ind_tbl;
1327 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1328 DEBUG("%p: Verbs indirection table %p still referenced",
1329 (void *)priv, (void *)ind_tbl);
1336 * Create an Rx Hash queue.
1339 * Pointer to private structure.
1341 * RSS key for the Rx hash queue.
1342 * @param rss_key_len
1344 * @param hash_fields
1345 * Verbs protocol hash field to make the RSS on.
1347 * Queues entering in hash queue. In case of empty hash_fields only the
1348 * first queue index will be taken for the indirection table.
1353 * An hash Rx queue on success.
1356 mlx5_priv_hrxq_new(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1357 uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1359 struct mlx5_hrxq *hrxq;
1360 struct mlx5_ind_table_ibv *ind_tbl;
1363 queues_n = hash_fields ? queues_n : 1;
1364 ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1366 ind_tbl = mlx5_priv_ind_table_ibv_new(priv, queues, queues_n);
1369 qp = ibv_create_qp_ex(
1371 &(struct ibv_qp_init_attr_ex){
1372 .qp_type = IBV_QPT_RAW_PACKET,
1374 IBV_QP_INIT_ATTR_PD |
1375 IBV_QP_INIT_ATTR_IND_TABLE |
1376 IBV_QP_INIT_ATTR_RX_HASH,
1377 .rx_hash_conf = (struct ibv_rx_hash_conf){
1378 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
1379 .rx_hash_key_len = rss_key_len,
1380 .rx_hash_key = rss_key,
1381 .rx_hash_fields_mask = hash_fields,
1383 .rwq_ind_tbl = ind_tbl->ind_table,
1388 hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
1391 hrxq->ind_table = ind_tbl;
1393 hrxq->rss_key_len = rss_key_len;
1394 hrxq->hash_fields = hash_fields;
1395 memcpy(hrxq->rss_key, rss_key, rss_key_len);
1396 rte_atomic32_inc(&hrxq->refcnt);
1397 LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
1398 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1399 (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1402 mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1404 claim_zero(ibv_destroy_qp(qp));
1409 * Get an Rx Hash queue.
1412 * Pointer to private structure.
1414 * RSS configuration for the Rx hash queue.
1416 * Queues entering in hash queue. In case of empty hash_fields only the
1417 * first queue index will be taken for the indirection table.
1422 * An hash Rx queue on success.
1425 mlx5_priv_hrxq_get(struct priv *priv, uint8_t *rss_key, uint8_t rss_key_len,
1426 uint64_t hash_fields, uint16_t queues[], uint16_t queues_n)
1428 struct mlx5_hrxq *hrxq;
1430 queues_n = hash_fields ? queues_n : 1;
1431 LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1432 struct mlx5_ind_table_ibv *ind_tbl;
1434 if (hrxq->rss_key_len != rss_key_len)
1436 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1438 if (hrxq->hash_fields != hash_fields)
1440 ind_tbl = mlx5_priv_ind_table_ibv_get(priv, queues, queues_n);
1443 if (ind_tbl != hrxq->ind_table) {
1444 mlx5_priv_ind_table_ibv_release(priv, ind_tbl);
1447 rte_atomic32_inc(&hrxq->refcnt);
1448 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1449 (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1456 * Release the hash Rx queue.
1459 * Pointer to private structure.
1461 * Pointer to Hash Rx queue to release.
1464 * 0 on success, errno value on failure.
1467 mlx5_priv_hrxq_release(struct priv *priv, struct mlx5_hrxq *hrxq)
1469 DEBUG("%p: Hash Rx queue %p: refcnt %d", (void *)priv,
1470 (void *)hrxq, rte_atomic32_read(&hrxq->refcnt));
1471 if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1472 claim_zero(ibv_destroy_qp(hrxq->qp));
1473 mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table);
1474 LIST_REMOVE(hrxq, next);
1478 claim_nonzero(mlx5_priv_ind_table_ibv_release(priv, hrxq->ind_table));
1483 * Verify the Rx Queue list is empty
1486 * Pointer to private structure.
1488 * @return the number of object not released.
1491 mlx5_priv_hrxq_ibv_verify(struct priv *priv)
1493 struct mlx5_hrxq *hrxq;
1496 LIST_FOREACH(hrxq, &priv->hrxqs, next) {
1497 DEBUG("%p: Verbs Hash Rx queue %p still referenced",
1498 (void *)priv, (void *)hrxq);