1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
12 #include <sys/queue.h>
15 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
17 #pragma GCC diagnostic ignored "-Wpedantic"
19 #include <infiniband/verbs.h>
20 #include <infiniband/mlx5dv.h>
22 #pragma GCC diagnostic error "-Wpedantic"
26 #include <rte_malloc.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29 #include <rte_interrupts.h>
30 #include <rte_debug.h>
34 #include "mlx5_rxtx.h"
35 #include "mlx5_utils.h"
36 #include "mlx5_autoconf.h"
37 #include "mlx5_defs.h"
38 #include "mlx5_glue.h"
40 /* Default RSS hash key also used for ConnectX-3. */
41 uint8_t rss_hash_default_key[] = {
42 0x2c, 0xc6, 0x81, 0xd1,
43 0x5b, 0xdb, 0xf4, 0xf7,
44 0xfc, 0xa2, 0x83, 0x19,
45 0xdb, 0x1a, 0x3e, 0x94,
46 0x6b, 0x9e, 0x38, 0xd9,
47 0x2c, 0x9c, 0x03, 0xd1,
48 0xad, 0x99, 0x44, 0xa7,
49 0xd9, 0x56, 0x3d, 0x59,
50 0x06, 0x3c, 0x25, 0xf3,
51 0xfc, 0x1f, 0xdc, 0x2a,
54 /* Length of the default RSS hash key. */
55 static_assert(MLX5_RSS_HASH_KEY_LEN ==
56 (unsigned int)sizeof(rss_hash_default_key),
57 "wrong RSS default key size.");
60 * Check whether Multi-Packet RQ can be enabled for the device.
63 * Pointer to Ethernet device.
66 * 1 if supported, negative errno value if not.
69 mlx5_check_mprq_support(struct rte_eth_dev *dev)
71 struct mlx5_priv *priv = dev->data->dev_private;
73 if (priv->config.mprq.enabled &&
74 priv->rxqs_n >= priv->config.mprq.min_rxqs_num)
80 * Check whether Multi-Packet RQ is enabled for the Rx queue.
83 * Pointer to receive queue structure.
86 * 0 if disabled, otherwise enabled.
89 mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq)
91 return rxq->strd_num_n > 0;
95 * Check whether Multi-Packet RQ is enabled for the device.
96 * MPRQ can be enabled explicitly, or implicitly by enabling LRO.
99 * Pointer to Ethernet device.
102 * 0 if disabled, otherwise enabled.
105 mlx5_mprq_enabled(struct rte_eth_dev *dev)
107 struct mlx5_priv *priv = dev->data->dev_private;
111 if (mlx5_check_mprq_support(dev) < 0)
113 /* All the configured queues should be enabled. */
114 for (i = 0; i < priv->rxqs_n; ++i) {
115 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
119 if (mlx5_rxq_mprq_enabled(rxq))
122 /* Multi-Packet RQ can't be partially configured. */
123 assert(n == 0 || n == priv->rxqs_n);
124 return n == priv->rxqs_n;
128 * Check whether LRO is supported and enabled for the device.
131 * Pointer to Ethernet device.
134 * 0 if disabled, 1 if enabled.
137 mlx5_lro_on(struct rte_eth_dev *dev)
139 return (MLX5_LRO_SUPPORTED(dev) && MLX5_LRO_ENABLED(dev));
143 * Allocate RX queue elements for Multi-Packet RQ.
146 * Pointer to RX queue structure.
149 * 0 on success, a negative errno value otherwise and rte_errno is set.
152 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
154 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
155 unsigned int wqe_n = 1 << rxq->elts_n;
159 /* Iterate on segments. */
160 for (i = 0; i <= wqe_n; ++i) {
161 struct mlx5_mprq_buf *buf;
163 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
164 DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
169 (*rxq->mprq_bufs)[i] = buf;
171 rxq->mprq_repl = buf;
174 "port %u Rx queue %u allocated and configured %u segments",
175 rxq->port_id, rxq->idx, wqe_n);
178 err = rte_errno; /* Save rte_errno before cleanup. */
180 for (i = 0; (i != wqe_n); ++i) {
181 if ((*rxq->mprq_bufs)[i] != NULL)
182 rte_mempool_put(rxq->mprq_mp,
183 (*rxq->mprq_bufs)[i]);
184 (*rxq->mprq_bufs)[i] = NULL;
186 DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
187 rxq->port_id, rxq->idx);
188 rte_errno = err; /* Restore rte_errno. */
193 * Allocate RX queue elements for Single-Packet RQ.
196 * Pointer to RX queue structure.
199 * 0 on success, errno value on failure.
202 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
204 const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
205 unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
209 /* Iterate on segments. */
210 for (i = 0; (i != elts_n); ++i) {
211 struct rte_mbuf *buf;
213 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
215 DRV_LOG(ERR, "port %u empty mbuf pool",
216 PORT_ID(rxq_ctrl->priv));
220 /* Headroom is reserved by rte_pktmbuf_alloc(). */
221 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
222 /* Buffer is supposed to be empty. */
223 assert(rte_pktmbuf_data_len(buf) == 0);
224 assert(rte_pktmbuf_pkt_len(buf) == 0);
226 /* Only the first segment keeps headroom. */
228 SET_DATA_OFF(buf, 0);
229 PORT(buf) = rxq_ctrl->rxq.port_id;
230 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
231 PKT_LEN(buf) = DATA_LEN(buf);
233 (*rxq_ctrl->rxq.elts)[i] = buf;
235 /* If Rx vector is activated. */
236 if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
237 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
238 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
241 /* Initialize default rearm_data for vPMD. */
242 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
243 rte_mbuf_refcnt_set(mbuf_init, 1);
244 mbuf_init->nb_segs = 1;
245 mbuf_init->port = rxq->port_id;
247 * prevent compiler reordering:
248 * rearm_data covers previous fields.
250 rte_compiler_barrier();
251 rxq->mbuf_initializer =
252 *(uint64_t *)&mbuf_init->rearm_data;
253 /* Padding with a fake mbuf for vectorized Rx. */
254 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
255 (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
258 "port %u Rx queue %u allocated and configured %u segments"
260 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx, elts_n,
261 elts_n / (1 << rxq_ctrl->rxq.sges_n));
264 err = rte_errno; /* Save rte_errno before cleanup. */
266 for (i = 0; (i != elts_n); ++i) {
267 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
268 rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
269 (*rxq_ctrl->rxq.elts)[i] = NULL;
271 DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
272 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx);
273 rte_errno = err; /* Restore rte_errno. */
278 * Allocate RX queue elements.
281 * Pointer to RX queue structure.
284 * 0 on success, errno value on failure.
287 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
289 return mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
290 rxq_alloc_elts_mprq(rxq_ctrl) : rxq_alloc_elts_sprq(rxq_ctrl);
294 * Free RX queue elements for Multi-Packet RQ.
297 * Pointer to RX queue structure.
300 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
302 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
305 DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing WRs",
306 rxq->port_id, rxq->idx);
307 if (rxq->mprq_bufs == NULL)
309 assert(mlx5_rxq_check_vec_support(rxq) < 0);
310 for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
311 if ((*rxq->mprq_bufs)[i] != NULL)
312 mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
313 (*rxq->mprq_bufs)[i] = NULL;
315 if (rxq->mprq_repl != NULL) {
316 mlx5_mprq_buf_free(rxq->mprq_repl);
317 rxq->mprq_repl = NULL;
322 * Free RX queue elements for Single-Packet RQ.
325 * Pointer to RX queue structure.
328 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
330 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
331 const uint16_t q_n = (1 << rxq->elts_n);
332 const uint16_t q_mask = q_n - 1;
333 uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
336 DRV_LOG(DEBUG, "port %u Rx queue %u freeing WRs",
337 PORT_ID(rxq_ctrl->priv), rxq->idx);
338 if (rxq->elts == NULL)
341 * Some mbuf in the Ring belongs to the application. They cannot be
344 if (mlx5_rxq_check_vec_support(rxq) > 0) {
345 for (i = 0; i < used; ++i)
346 (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
347 rxq->rq_pi = rxq->rq_ci;
349 for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
350 if ((*rxq->elts)[i] != NULL)
351 rte_pktmbuf_free_seg((*rxq->elts)[i]);
352 (*rxq->elts)[i] = NULL;
357 * Free RX queue elements.
360 * Pointer to RX queue structure.
363 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
365 if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
366 rxq_free_elts_mprq(rxq_ctrl);
368 rxq_free_elts_sprq(rxq_ctrl);
372 * Returns the per-queue supported offloads.
375 * Pointer to Ethernet device.
378 * Supported Rx offloads.
381 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
383 struct mlx5_priv *priv = dev->data->dev_private;
384 struct mlx5_dev_config *config = &priv->config;
385 uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
386 DEV_RX_OFFLOAD_TIMESTAMP |
387 DEV_RX_OFFLOAD_JUMBO_FRAME);
389 if (config->hw_fcs_strip)
390 offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
393 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
394 DEV_RX_OFFLOAD_UDP_CKSUM |
395 DEV_RX_OFFLOAD_TCP_CKSUM);
396 if (config->hw_vlan_strip)
397 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
403 * Returns the per-port supported offloads.
406 * Pointer to Ethernet device.
409 * Supported Rx offloads.
412 mlx5_get_rx_port_offloads(struct rte_eth_dev *dev)
414 uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
416 if (MLX5_LRO_SUPPORTED(dev))
417 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
422 * Verify if the queue can be released.
425 * Pointer to Ethernet device.
430 * 1 if the queue can be released
431 * 0 if the queue can not be released, there are references to it.
432 * Negative errno and rte_errno is set if queue doesn't exist.
435 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
437 struct mlx5_priv *priv = dev->data->dev_private;
438 struct mlx5_rxq_ctrl *rxq_ctrl;
440 if (!(*priv->rxqs)[idx]) {
444 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
445 return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
451 * Pointer to Ethernet device structure.
455 * Number of descriptors to configure in queue.
457 * NUMA socket on which memory must be allocated.
459 * Thresholds parameters.
461 * Memory pool for buffer allocations.
464 * 0 on success, a negative errno value otherwise and rte_errno is set.
467 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
468 unsigned int socket, const struct rte_eth_rxconf *conf,
469 struct rte_mempool *mp)
471 struct mlx5_priv *priv = dev->data->dev_private;
472 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
473 struct mlx5_rxq_ctrl *rxq_ctrl =
474 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
476 if (!rte_is_power_of_2(desc)) {
477 desc = 1 << log2above(desc);
479 "port %u increased number of descriptors in Rx queue %u"
480 " to the next power of two (%d)",
481 dev->data->port_id, idx, desc);
483 DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
484 dev->data->port_id, idx, desc);
485 if (idx >= priv->rxqs_n) {
486 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
487 dev->data->port_id, idx, priv->rxqs_n);
488 rte_errno = EOVERFLOW;
491 if (!mlx5_rxq_releasable(dev, idx)) {
492 DRV_LOG(ERR, "port %u unable to release queue index %u",
493 dev->data->port_id, idx);
497 mlx5_rxq_release(dev, idx);
498 rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, conf, mp);
500 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
501 dev->data->port_id, idx);
505 DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
506 dev->data->port_id, idx);
507 (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
512 * DPDK callback to release a RX queue.
515 * Generic RX queue pointer.
518 mlx5_rx_queue_release(void *dpdk_rxq)
520 struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
521 struct mlx5_rxq_ctrl *rxq_ctrl;
522 struct mlx5_priv *priv;
526 rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
527 priv = rxq_ctrl->priv;
528 if (!mlx5_rxq_releasable(ETH_DEV(priv), rxq_ctrl->rxq.idx))
529 rte_panic("port %u Rx queue %u is still used by a flow and"
530 " cannot be removed\n",
531 PORT_ID(priv), rxq->idx);
532 mlx5_rxq_release(ETH_DEV(priv), rxq_ctrl->rxq.idx);
536 * Get an Rx queue Verbs/DevX object.
539 * Pointer to Ethernet device.
541 * Queue index in DPDK Rx queue array
544 * The Verbs/DevX object if it exists.
546 static struct mlx5_rxq_obj *
547 mlx5_rxq_obj_get(struct rte_eth_dev *dev, uint16_t idx)
549 struct mlx5_priv *priv = dev->data->dev_private;
550 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
551 struct mlx5_rxq_ctrl *rxq_ctrl;
553 if (idx >= priv->rxqs_n)
557 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
559 rte_atomic32_inc(&rxq_ctrl->obj->refcnt);
560 return rxq_ctrl->obj;
564 * Release the resources allocated for an RQ DevX object.
567 * DevX Rx queue object.
570 rxq_release_rq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
572 if (rxq_ctrl->rxq.wqes) {
573 rte_free((void *)(uintptr_t)rxq_ctrl->rxq.wqes);
574 rxq_ctrl->rxq.wqes = NULL;
576 if (rxq_ctrl->wq_umem) {
577 mlx5_glue->devx_umem_dereg(rxq_ctrl->wq_umem);
578 rxq_ctrl->wq_umem = NULL;
583 * Release an Rx verbs/DevX queue object.
586 * Verbs/DevX Rx queue object.
589 * 1 while a reference on it exists, 0 when freed.
592 mlx5_rxq_obj_release(struct mlx5_rxq_obj *rxq_obj)
595 if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_IBV)
598 if (rte_atomic32_dec_and_test(&rxq_obj->refcnt)) {
599 rxq_free_elts(rxq_obj->rxq_ctrl);
600 if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_IBV) {
601 claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
602 } else if (rxq_obj->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
603 claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
604 rxq_release_rq_resources(rxq_obj->rxq_ctrl);
606 claim_zero(mlx5_glue->destroy_cq(rxq_obj->cq));
607 if (rxq_obj->channel)
608 claim_zero(mlx5_glue->destroy_comp_channel
610 LIST_REMOVE(rxq_obj, next);
618 * Allocate queue vector and fill epoll fd list for Rx interrupts.
621 * Pointer to Ethernet device.
624 * 0 on success, a negative errno value otherwise and rte_errno is set.
627 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
629 struct mlx5_priv *priv = dev->data->dev_private;
631 unsigned int rxqs_n = priv->rxqs_n;
632 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
633 unsigned int count = 0;
634 struct rte_intr_handle *intr_handle = dev->intr_handle;
636 if (!dev->data->dev_conf.intr_conf.rxq)
638 mlx5_rx_intr_vec_disable(dev);
639 intr_handle->intr_vec = malloc(n * sizeof(intr_handle->intr_vec[0]));
640 if (intr_handle->intr_vec == NULL) {
642 "port %u failed to allocate memory for interrupt"
643 " vector, Rx interrupts will not be supported",
648 intr_handle->type = RTE_INTR_HANDLE_EXT;
649 for (i = 0; i != n; ++i) {
650 /* This rxq obj must not be released in this function. */
651 struct mlx5_rxq_obj *rxq_obj = mlx5_rxq_obj_get(dev, i);
656 /* Skip queues that cannot request interrupts. */
657 if (!rxq_obj || !rxq_obj->channel) {
658 /* Use invalid intr_vec[] index to disable entry. */
659 intr_handle->intr_vec[i] =
660 RTE_INTR_VEC_RXTX_OFFSET +
661 RTE_MAX_RXTX_INTR_VEC_ID;
664 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
666 "port %u too many Rx queues for interrupt"
667 " vector size (%d), Rx interrupts cannot be"
669 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
670 mlx5_rx_intr_vec_disable(dev);
674 fd = rxq_obj->channel->fd;
675 flags = fcntl(fd, F_GETFL);
676 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
680 "port %u failed to make Rx interrupt file"
681 " descriptor %d non-blocking for queue index"
683 dev->data->port_id, fd, i);
684 mlx5_rx_intr_vec_disable(dev);
687 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
688 intr_handle->efds[count] = fd;
692 mlx5_rx_intr_vec_disable(dev);
694 intr_handle->nb_efd = count;
699 * Clean up Rx interrupts handler.
702 * Pointer to Ethernet device.
705 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
707 struct mlx5_priv *priv = dev->data->dev_private;
708 struct rte_intr_handle *intr_handle = dev->intr_handle;
710 unsigned int rxqs_n = priv->rxqs_n;
711 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
713 if (!dev->data->dev_conf.intr_conf.rxq)
715 if (!intr_handle->intr_vec)
717 for (i = 0; i != n; ++i) {
718 struct mlx5_rxq_ctrl *rxq_ctrl;
719 struct mlx5_rxq_data *rxq_data;
721 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
722 RTE_MAX_RXTX_INTR_VEC_ID)
725 * Need to access directly the queue to release the reference
726 * kept in mlx5_rx_intr_vec_enable().
728 rxq_data = (*priv->rxqs)[i];
729 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
731 mlx5_rxq_obj_release(rxq_ctrl->obj);
734 rte_intr_free_epoll_fd(intr_handle);
735 if (intr_handle->intr_vec)
736 free(intr_handle->intr_vec);
737 intr_handle->nb_efd = 0;
738 intr_handle->intr_vec = NULL;
742 * MLX5 CQ notification .
745 * Pointer to receive queue structure.
747 * Sequence number per receive queue .
750 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
753 uint32_t doorbell_hi;
755 void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
757 sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
758 doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
759 doorbell = (uint64_t)doorbell_hi << 32;
760 doorbell |= rxq->cqn;
761 rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
762 mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
763 cq_db_reg, rxq->uar_lock_cq);
767 * DPDK callback for Rx queue interrupt enable.
770 * Pointer to Ethernet device structure.
775 * 0 on success, a negative errno value otherwise and rte_errno is set.
778 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
780 struct mlx5_priv *priv = dev->data->dev_private;
781 struct mlx5_rxq_data *rxq_data;
782 struct mlx5_rxq_ctrl *rxq_ctrl;
784 rxq_data = (*priv->rxqs)[rx_queue_id];
789 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
791 struct mlx5_rxq_obj *rxq_obj;
793 rxq_obj = mlx5_rxq_obj_get(dev, rx_queue_id);
798 mlx5_arm_cq(rxq_data, rxq_data->cq_arm_sn);
799 mlx5_rxq_obj_release(rxq_obj);
805 * DPDK callback for Rx queue interrupt disable.
808 * Pointer to Ethernet device structure.
813 * 0 on success, a negative errno value otherwise and rte_errno is set.
816 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
818 struct mlx5_priv *priv = dev->data->dev_private;
819 struct mlx5_rxq_data *rxq_data;
820 struct mlx5_rxq_ctrl *rxq_ctrl;
821 struct mlx5_rxq_obj *rxq_obj = NULL;
822 struct ibv_cq *ev_cq;
826 rxq_data = (*priv->rxqs)[rx_queue_id];
831 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
834 rxq_obj = mlx5_rxq_obj_get(dev, rx_queue_id);
839 ret = mlx5_glue->get_cq_event(rxq_obj->channel, &ev_cq, &ev_ctx);
840 if (ret || ev_cq != rxq_obj->cq) {
844 rxq_data->cq_arm_sn++;
845 mlx5_glue->ack_cq_events(rxq_obj->cq, 1);
846 mlx5_rxq_obj_release(rxq_obj);
849 ret = rte_errno; /* Save rte_errno before cleanup. */
851 mlx5_rxq_obj_release(rxq_obj);
852 DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
853 dev->data->port_id, rx_queue_id);
854 rte_errno = ret; /* Restore rte_errno. */
859 * Create a CQ Verbs object.
862 * Pointer to Ethernet device.
864 * Pointer to device private data.
866 * Pointer to Rx queue data.
868 * Number of CQEs in CQ.
870 * Pointer to Rx queue object data.
873 * The Verbs object initialised, NULL otherwise and rte_errno is set.
875 static struct ibv_cq *
876 mlx5_ibv_cq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
877 struct mlx5_rxq_data *rxq_data,
878 unsigned int cqe_n, struct mlx5_rxq_obj *rxq_obj)
881 struct ibv_cq_init_attr_ex ibv;
882 struct mlx5dv_cq_init_attr mlx5;
885 cq_attr.ibv = (struct ibv_cq_init_attr_ex){
887 .channel = rxq_obj->channel,
890 cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
893 if (priv->config.cqe_comp && !rxq_data->hw_timestamp) {
894 cq_attr.mlx5.comp_mask |=
895 MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
896 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
897 cq_attr.mlx5.cqe_comp_res_format =
898 mlx5_rxq_mprq_enabled(rxq_data) ?
899 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
900 MLX5DV_CQE_RES_FORMAT_HASH;
902 cq_attr.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
905 * For vectorized Rx, it must not be doubled in order to
906 * make cq_ci and rq_ci aligned.
908 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
909 cq_attr.ibv.cqe *= 2;
910 } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
912 "port %u Rx CQE compression is disabled for HW"
916 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
917 if (priv->config.cqe_pad) {
918 cq_attr.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
919 cq_attr.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
922 return mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq(priv->sh->ctx,
928 * Create a WQ Verbs object.
931 * Pointer to Ethernet device.
933 * Pointer to device private data.
935 * Pointer to Rx queue data.
937 * Queue index in DPDK Rx queue array
939 * Number of WQEs in WQ.
941 * Pointer to Rx queue object data.
944 * The Verbs object initialised, NULL otherwise and rte_errno is set.
946 static struct ibv_wq *
947 mlx5_ibv_wq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
948 struct mlx5_rxq_data *rxq_data, uint16_t idx,
949 unsigned int wqe_n, struct mlx5_rxq_obj *rxq_obj)
952 struct ibv_wq_init_attr ibv;
953 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
954 struct mlx5dv_wq_init_attr mlx5;
958 wq_attr.ibv = (struct ibv_wq_init_attr){
959 .wq_context = NULL, /* Could be useful in the future. */
960 .wq_type = IBV_WQT_RQ,
961 /* Max number of outstanding WRs. */
962 .max_wr = wqe_n >> rxq_data->sges_n,
963 /* Max number of scatter/gather elements in a WR. */
964 .max_sge = 1 << rxq_data->sges_n,
967 .comp_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING | 0,
968 .create_flags = (rxq_data->vlan_strip ?
969 IBV_WQ_FLAGS_CVLAN_STRIPPING : 0),
971 /* By default, FCS (CRC) is stripped by hardware. */
972 if (rxq_data->crc_present) {
973 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
974 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
976 if (priv->config.hw_padding) {
977 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
978 wq_attr.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
979 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
980 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
981 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_PCI_WRITE_END_PADDING;
982 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
985 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
986 wq_attr.mlx5 = (struct mlx5dv_wq_init_attr){
989 if (mlx5_rxq_mprq_enabled(rxq_data)) {
990 struct mlx5dv_striding_rq_init_attr *mprq_attr =
991 &wq_attr.mlx5.striding_rq_attrs;
993 wq_attr.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
994 *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
995 .single_stride_log_num_of_bytes = rxq_data->strd_sz_n,
996 .single_wqe_log_num_of_strides = rxq_data->strd_num_n,
997 .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
1000 rxq_obj->wq = mlx5_glue->dv_create_wq(priv->sh->ctx, &wq_attr.ibv,
1003 rxq_obj->wq = mlx5_glue->create_wq(priv->sh->ctx, &wq_attr.ibv);
1007 * Make sure number of WRs*SGEs match expectations since a queue
1008 * cannot allocate more than "desc" buffers.
1010 if (wq_attr.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
1011 wq_attr.ibv.max_sge != (1u << rxq_data->sges_n)) {
1013 "port %u Rx queue %u requested %u*%u but got"
1015 dev->data->port_id, idx,
1016 wqe_n >> rxq_data->sges_n,
1017 (1 << rxq_data->sges_n),
1018 wq_attr.ibv.max_wr, wq_attr.ibv.max_sge);
1019 claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
1028 * Fill common fields of create RQ attributes structure.
1031 * Pointer to Rx queue data.
1033 * CQ number to use with this RQ.
1035 * RQ attributes structure to fill..
1038 mlx5_devx_create_rq_attr_fill(struct mlx5_rxq_data *rxq_data, uint32_t cqn,
1039 struct mlx5_devx_create_rq_attr *rq_attr)
1041 rq_attr->state = MLX5_RQC_STATE_RST;
1042 rq_attr->vsd = (rxq_data->vlan_strip) ? 0 : 1;
1044 rq_attr->scatter_fcs = (rxq_data->crc_present) ? 1 : 0;
1048 * Fill common fields of DevX WQ attributes structure.
1051 * Pointer to device private data.
1053 * Pointer to Rx queue control structure.
1055 * WQ attributes structure to fill..
1058 mlx5_devx_wq_attr_fill(struct mlx5_priv *priv, struct mlx5_rxq_ctrl *rxq_ctrl,
1059 struct mlx5_devx_wq_attr *wq_attr)
1061 wq_attr->end_padding_mode = priv->config.cqe_pad ?
1062 MLX5_WQ_END_PAD_MODE_ALIGN :
1063 MLX5_WQ_END_PAD_MODE_NONE;
1064 wq_attr->pd = priv->sh->pdn;
1065 wq_attr->dbr_addr = rxq_ctrl->dbr_offset;
1066 wq_attr->dbr_umem_id = rxq_ctrl->dbr_umem_id;
1067 wq_attr->dbr_umem_valid = 1;
1068 wq_attr->wq_umem_id = rxq_ctrl->wq_umem->umem_id;
1069 wq_attr->wq_umem_valid = 1;
1073 * Create a RQ object using DevX.
1076 * Pointer to Ethernet device.
1078 * Queue index in DPDK Rx queue array
1080 * CQ number to use with this RQ.
1083 * The DevX object initialised, NULL otherwise and rte_errno is set.
1085 static struct mlx5_devx_obj *
1086 mlx5_devx_rq_new(struct rte_eth_dev *dev, uint16_t idx, uint32_t cqn)
1088 struct mlx5_priv *priv = dev->data->dev_private;
1089 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1090 struct mlx5_rxq_ctrl *rxq_ctrl =
1091 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1092 struct mlx5_devx_create_rq_attr rq_attr;
1093 uint32_t wqe_n = 1 << (rxq_data->elts_n - rxq_data->sges_n);
1094 uint32_t wq_size = 0;
1095 uint32_t wqe_size = 0;
1096 uint32_t log_wqe_size = 0;
1098 struct mlx5_devx_obj *rq;
1100 memset(&rq_attr, 0, sizeof(rq_attr));
1101 /* Fill RQ attributes. */
1102 rq_attr.mem_rq_type = MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE;
1103 rq_attr.flush_in_error_en = 1;
1104 mlx5_devx_create_rq_attr_fill(rxq_data, cqn, &rq_attr);
1105 /* Fill WQ attributes for this RQ. */
1106 if (mlx5_rxq_mprq_enabled(rxq_data)) {
1107 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ;
1109 * Number of strides in each WQE:
1110 * 512*2^single_wqe_log_num_of_strides.
1112 rq_attr.wq_attr.single_wqe_log_num_of_strides =
1113 rxq_data->strd_num_n -
1114 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1115 /* Stride size = (2^single_stride_log_num_of_bytes)*64B. */
1116 rq_attr.wq_attr.single_stride_log_num_of_bytes =
1117 rxq_data->strd_sz_n -
1118 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1119 wqe_size = sizeof(struct mlx5_wqe_mprq);
1121 rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1122 wqe_size = sizeof(struct mlx5_wqe_data_seg);
1124 log_wqe_size = log2above(wqe_size) + rxq_data->sges_n;
1125 rq_attr.wq_attr.log_wq_stride = log_wqe_size;
1126 rq_attr.wq_attr.log_wq_sz = rxq_data->elts_n - rxq_data->sges_n;
1127 /* Calculate and allocate WQ memory space. */
1128 wqe_size = 1 << log_wqe_size; /* round up power of two.*/
1129 wq_size = wqe_n * wqe_size;
1130 buf = rte_calloc_socket(__func__, 1, wq_size, RTE_CACHE_LINE_SIZE,
1134 rxq_data->wqes = buf;
1135 rxq_ctrl->wq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
1137 if (!rxq_ctrl->wq_umem) {
1141 mlx5_devx_wq_attr_fill(priv, rxq_ctrl, &rq_attr.wq_attr);
1142 rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &rq_attr, rxq_ctrl->socket);
1144 rxq_release_rq_resources(rxq_ctrl);
1149 * Create the Rx queue Verbs/DevX object.
1152 * Pointer to Ethernet device.
1154 * Queue index in DPDK Rx queue array
1156 * Type of Rx queue object to create.
1159 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
1161 struct mlx5_rxq_obj *
1162 mlx5_rxq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
1163 enum mlx5_rxq_obj_type type)
1165 struct mlx5_priv *priv = dev->data->dev_private;
1166 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1167 struct mlx5_rxq_ctrl *rxq_ctrl =
1168 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1169 struct ibv_wq_attr mod;
1171 unsigned int wqe_n = 1 << rxq_data->elts_n;
1172 struct mlx5_rxq_obj *tmpl = NULL;
1173 struct mlx5dv_cq cq_info;
1174 struct mlx5dv_rwq rwq;
1176 struct mlx5dv_obj obj;
1179 assert(!rxq_ctrl->obj);
1180 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
1181 priv->verbs_alloc_ctx.obj = rxq_ctrl;
1182 tmpl = rte_calloc_socket(__func__, 1, sizeof(*tmpl), 0,
1186 "port %u Rx queue %u cannot allocate verbs resources",
1187 dev->data->port_id, rxq_data->idx);
1192 tmpl->rxq_ctrl = rxq_ctrl;
1193 if (rxq_ctrl->irq) {
1194 tmpl->channel = mlx5_glue->create_comp_channel(priv->sh->ctx);
1195 if (!tmpl->channel) {
1196 DRV_LOG(ERR, "port %u: comp channel creation failure",
1197 dev->data->port_id);
1202 if (mlx5_rxq_mprq_enabled(rxq_data))
1203 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
1206 tmpl->cq = mlx5_ibv_cq_new(dev, priv, rxq_data, cqe_n, tmpl);
1208 DRV_LOG(ERR, "port %u Rx queue %u CQ creation failure",
1209 dev->data->port_id, idx);
1213 obj.cq.in = tmpl->cq;
1214 obj.cq.out = &cq_info;
1215 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ);
1220 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
1222 "port %u wrong MLX5_CQE_SIZE environment variable"
1223 " value: it should be set to %u",
1224 dev->data->port_id, RTE_CACHE_LINE_SIZE);
1228 DRV_LOG(DEBUG, "port %u device_attr.max_qp_wr is %d",
1229 dev->data->port_id, priv->sh->device_attr.orig_attr.max_qp_wr);
1230 DRV_LOG(DEBUG, "port %u device_attr.max_sge is %d",
1231 dev->data->port_id, priv->sh->device_attr.orig_attr.max_sge);
1232 /* Allocate door-bell for types created with DevX. */
1233 if (tmpl->type != MLX5_RXQ_OBJ_TYPE_IBV) {
1234 struct mlx5_devx_dbr_page *dbr_page;
1237 dbr_offset = mlx5_get_dbr(dev, &dbr_page);
1240 rxq_ctrl->dbr_offset = dbr_offset;
1241 rxq_ctrl->dbr_umem_id = dbr_page->umem->umem_id;
1242 rxq_ctrl->dbr_umem_id_valid = 1;
1243 rxq_data->rq_db = (uint32_t *)((uintptr_t)dbr_page->dbrs +
1244 (uintptr_t)rxq_ctrl->dbr_offset);
1246 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV) {
1247 tmpl->wq = mlx5_ibv_wq_new(dev, priv, rxq_data, idx, wqe_n,
1250 DRV_LOG(ERR, "port %u Rx queue %u WQ creation failure",
1251 dev->data->port_id, idx);
1255 /* Change queue state to ready. */
1256 mod = (struct ibv_wq_attr){
1257 .attr_mask = IBV_WQ_ATTR_STATE,
1258 .wq_state = IBV_WQS_RDY,
1260 ret = mlx5_glue->modify_wq(tmpl->wq, &mod);
1263 "port %u Rx queue %u WQ state to IBV_WQS_RDY"
1264 " failed", dev->data->port_id, idx);
1268 obj.rwq.in = tmpl->wq;
1270 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_RWQ);
1275 rxq_data->wqes = rwq.buf;
1276 rxq_data->rq_db = rwq.dbrec;
1277 } else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ) {
1278 struct mlx5_devx_modify_rq_attr rq_attr;
1280 memset(&rq_attr, 0, sizeof(rq_attr));
1281 tmpl->rq = mlx5_devx_rq_new(dev, idx, cq_info.cqn);
1283 DRV_LOG(ERR, "port %u Rx queue %u RQ creation failure",
1284 dev->data->port_id, idx);
1288 /* Change queue state to ready. */
1289 rq_attr.rq_state = MLX5_RQC_STATE_RST;
1290 rq_attr.state = MLX5_RQC_STATE_RDY;
1291 ret = mlx5_devx_cmd_modify_rq(tmpl->rq, &rq_attr);
1295 /* Fill the rings. */
1296 rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
1297 rxq_data->cq_db = cq_info.dbrec;
1298 rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
1299 rxq_data->cq_uar = cq_info.cq_uar;
1300 rxq_data->cqn = cq_info.cqn;
1301 rxq_data->cq_arm_sn = 0;
1302 mlx5_rxq_initialize(rxq_data);
1303 rxq_data->cq_ci = 0;
1304 DRV_LOG(DEBUG, "port %u rxq %u updated with %p", dev->data->port_id,
1305 idx, (void *)&tmpl);
1306 rte_atomic32_inc(&tmpl->refcnt);
1307 LIST_INSERT_HEAD(&priv->rxqsobj, tmpl, next);
1308 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1312 ret = rte_errno; /* Save rte_errno before cleanup. */
1313 if (tmpl->type == MLX5_RXQ_OBJ_TYPE_IBV && tmpl->wq)
1314 claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
1315 else if (tmpl->type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ && tmpl->rq)
1316 claim_zero(mlx5_devx_cmd_destroy(tmpl->rq));
1318 claim_zero(mlx5_glue->destroy_cq(tmpl->cq));
1320 claim_zero(mlx5_glue->destroy_comp_channel
1323 rte_errno = ret; /* Restore rte_errno. */
1325 if (type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ)
1326 rxq_release_rq_resources(rxq_ctrl);
1327 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1332 * Verify the Rx queue objects list is empty
1335 * Pointer to Ethernet device.
1338 * The number of objects not released.
1341 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1343 struct mlx5_priv *priv = dev->data->dev_private;
1345 struct mlx5_rxq_obj *rxq_obj;
1347 LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1348 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1349 dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1356 * Callback function to initialize mbufs for Multi-Packet RQ.
1359 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1360 void *_m, unsigned int i __rte_unused)
1362 struct mlx5_mprq_buf *buf = _m;
1363 struct rte_mbuf_ext_shared_info *shinfo;
1364 unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
1367 memset(_m, 0, sizeof(*buf));
1369 rte_atomic16_set(&buf->refcnt, 1);
1370 for (j = 0; j != strd_n; ++j) {
1371 shinfo = &buf->shinfos[j];
1372 shinfo->free_cb = mlx5_mprq_buf_free_cb;
1373 shinfo->fcb_opaque = buf;
1378 * Free mempool of Multi-Packet RQ.
1381 * Pointer to Ethernet device.
1384 * 0 on success, negative errno value on failure.
1387 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
1389 struct mlx5_priv *priv = dev->data->dev_private;
1390 struct rte_mempool *mp = priv->mprq_mp;
1395 DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
1396 dev->data->port_id, mp->name);
1398 * If a buffer in the pool has been externally attached to a mbuf and it
1399 * is still in use by application, destroying the Rx queue can spoil
1400 * the packet. It is unlikely to happen but if application dynamically
1401 * creates and destroys with holding Rx packets, this can happen.
1403 * TODO: It is unavoidable for now because the mempool for Multi-Packet
1404 * RQ isn't provided by application but managed by PMD.
1406 if (!rte_mempool_full(mp)) {
1408 "port %u mempool for Multi-Packet RQ is still in use",
1409 dev->data->port_id);
1413 rte_mempool_free(mp);
1414 /* Unset mempool for each Rx queue. */
1415 for (i = 0; i != priv->rxqs_n; ++i) {
1416 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1420 rxq->mprq_mp = NULL;
1422 priv->mprq_mp = NULL;
1427 * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
1428 * mempool. If already allocated, reuse it if there're enough elements.
1429 * Otherwise, resize it.
1432 * Pointer to Ethernet device.
1435 * 0 on success, negative errno value on failure.
1438 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
1440 struct mlx5_priv *priv = dev->data->dev_private;
1441 struct rte_mempool *mp = priv->mprq_mp;
1442 char name[RTE_MEMPOOL_NAMESIZE];
1443 unsigned int desc = 0;
1444 unsigned int buf_len;
1445 unsigned int obj_num;
1446 unsigned int obj_size;
1447 unsigned int strd_num_n = 0;
1448 unsigned int strd_sz_n = 0;
1451 if (!mlx5_mprq_enabled(dev))
1453 /* Count the total number of descriptors configured. */
1454 for (i = 0; i != priv->rxqs_n; ++i) {
1455 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1459 desc += 1 << rxq->elts_n;
1460 /* Get the max number of strides. */
1461 if (strd_num_n < rxq->strd_num_n)
1462 strd_num_n = rxq->strd_num_n;
1463 /* Get the max size of a stride. */
1464 if (strd_sz_n < rxq->strd_sz_n)
1465 strd_sz_n = rxq->strd_sz_n;
1467 assert(strd_num_n && strd_sz_n);
1468 buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
1469 obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
1470 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
1472 * Received packets can be either memcpy'd or externally referenced. In
1473 * case that the packet is attached to an mbuf as an external buffer, as
1474 * it isn't possible to predict how the buffers will be queued by
1475 * application, there's no option to exactly pre-allocate needed buffers
1476 * in advance but to speculatively prepares enough buffers.
1478 * In the data path, if this Mempool is depleted, PMD will try to memcpy
1479 * received packets to buffers provided by application (rxq->mp) until
1480 * this Mempool gets available again.
1483 obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * priv->rxqs_n;
1485 * rte_mempool_create_empty() has sanity check to refuse large cache
1486 * size compared to the number of elements.
1487 * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
1488 * constant number 2 instead.
1490 obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
1491 /* Check a mempool is already allocated and if it can be resued. */
1492 if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
1493 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
1494 dev->data->port_id, mp->name);
1497 } else if (mp != NULL) {
1498 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
1499 dev->data->port_id, mp->name);
1501 * If failed to free, which means it may be still in use, no way
1502 * but to keep using the existing one. On buffer underrun,
1503 * packets will be memcpy'd instead of external buffer
1506 if (mlx5_mprq_free_mp(dev)) {
1507 if (mp->elt_size >= obj_size)
1513 snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
1514 mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
1515 0, NULL, NULL, mlx5_mprq_buf_init,
1516 (void *)(uintptr_t)(1 << strd_num_n),
1517 dev->device->numa_node, 0);
1520 "port %u failed to allocate a mempool for"
1521 " Multi-Packet RQ, count=%u, size=%u",
1522 dev->data->port_id, obj_num, obj_size);
1528 /* Set mempool for each Rx queue. */
1529 for (i = 0; i != priv->rxqs_n; ++i) {
1530 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1536 DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
1537 dev->data->port_id);
1541 #define MLX5_MAX_LRO_SIZE (UINT8_MAX * 256u)
1542 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
1543 sizeof(struct rte_vlan_hdr) * 2 + \
1544 sizeof(struct rte_ipv6_hdr)))
1546 * Adjust the maximum LRO massage size.
1549 * Pointer to Ethernet device.
1550 * @param max_lro_size
1551 * The maximum size for LRO packet.
1554 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint32_t max_lro_size)
1556 struct mlx5_priv *priv = dev->data->dev_private;
1558 if (priv->config.hca_attr.lro_max_msg_sz_mode ==
1559 MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
1560 MLX5_MAX_TCP_HDR_OFFSET)
1561 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
1562 max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
1563 assert(max_lro_size >= 256u);
1564 max_lro_size /= 256u;
1565 if (priv->max_lro_msg_size)
1566 priv->max_lro_msg_size =
1567 RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
1569 priv->max_lro_msg_size = max_lro_size;
1573 * Create a DPDK Rx queue.
1576 * Pointer to Ethernet device.
1580 * Number of descriptors to configure in queue.
1582 * NUMA socket on which memory must be allocated.
1585 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1587 struct mlx5_rxq_ctrl *
1588 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1589 unsigned int socket, const struct rte_eth_rxconf *conf,
1590 struct rte_mempool *mp)
1592 struct mlx5_priv *priv = dev->data->dev_private;
1593 struct mlx5_rxq_ctrl *tmpl;
1594 unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
1595 unsigned int mprq_stride_size;
1596 struct mlx5_dev_config *config = &priv->config;
1597 unsigned int strd_headroom_en;
1599 * Always allocate extra slots, even if eventually
1600 * the vector Rx will not be used.
1603 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
1604 uint64_t offloads = conf->offloads |
1605 dev->data->dev_conf.rxmode.offloads;
1606 const int mprq_en = mlx5_check_mprq_support(dev) > 0;
1607 unsigned int max_rx_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
1608 unsigned int non_scatter_min_mbuf_size = max_rx_pkt_len +
1609 RTE_PKTMBUF_HEADROOM;
1611 if (non_scatter_min_mbuf_size > mb_len && !(offloads &
1612 DEV_RX_OFFLOAD_SCATTER)) {
1613 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
1614 " configured and no enough mbuf space(%u) to contain "
1615 "the maximum RX packet length(%u) with head-room(%u)",
1616 dev->data->port_id, idx, mb_len, max_rx_pkt_len,
1617 RTE_PKTMBUF_HEADROOM);
1621 tmpl = rte_calloc_socket("RXQ", 1,
1623 desc_n * sizeof(struct rte_mbuf *),
1629 if (mlx5_mr_btree_init(&tmpl->rxq.mr_ctrl.cache_bh,
1630 MLX5_MR_BTREE_CACHE_N, socket)) {
1631 /* rte_errno is already set. */
1634 tmpl->socket = socket;
1635 if (dev->data->dev_conf.intr_conf.rxq)
1638 * LRO packet may consume all the stride memory, hence we cannot
1639 * guaranty head-room near the packet memory in the stride.
1640 * In this case scatter is, for sure, enabled and an empty mbuf may be
1641 * added in the start for the head-room.
1643 if (mlx5_lro_on(dev) && RTE_PKTMBUF_HEADROOM > 0 &&
1644 non_scatter_min_mbuf_size > mb_len) {
1645 strd_headroom_en = 0;
1646 mprq_stride_size = RTE_MIN(max_rx_pkt_len,
1647 1u << config->mprq.max_stride_size_n);
1649 strd_headroom_en = 1;
1650 mprq_stride_size = non_scatter_min_mbuf_size;
1653 * This Rx queue can be configured as a Multi-Packet RQ if all of the
1654 * following conditions are met:
1655 * - MPRQ is enabled.
1656 * - The number of descs is more than the number of strides.
1657 * - max_rx_pkt_len plus overhead is less than the max size of a
1659 * Otherwise, enable Rx scatter if necessary.
1662 desc > (1U << config->mprq.stride_num_n) &&
1663 mprq_stride_size <= (1U << config->mprq.max_stride_size_n)) {
1664 /* TODO: Rx scatter isn't supported yet. */
1665 tmpl->rxq.sges_n = 0;
1666 /* Trim the number of descs needed. */
1667 desc >>= config->mprq.stride_num_n;
1668 tmpl->rxq.strd_num_n = config->mprq.stride_num_n;
1669 tmpl->rxq.strd_sz_n = RTE_MAX(log2above(mprq_stride_size),
1670 config->mprq.min_stride_size_n);
1671 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
1672 tmpl->rxq.strd_headroom_en = strd_headroom_en;
1673 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(mb_len -
1674 RTE_PKTMBUF_HEADROOM, config->mprq.max_memcpy_len);
1675 mlx5_max_lro_msg_size_adjust(dev, RTE_MIN(max_rx_pkt_len,
1676 (1u << tmpl->rxq.strd_num_n) * (1u << tmpl->rxq.strd_sz_n)));
1678 "port %u Rx queue %u: Multi-Packet RQ is enabled"
1679 " strd_num_n = %u, strd_sz_n = %u",
1680 dev->data->port_id, idx,
1681 tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
1682 } else if (max_rx_pkt_len <= (mb_len - RTE_PKTMBUF_HEADROOM)) {
1683 tmpl->rxq.sges_n = 0;
1684 } else if (offloads & DEV_RX_OFFLOAD_SCATTER) {
1685 unsigned int size = non_scatter_min_mbuf_size;
1686 unsigned int sges_n;
1689 * Determine the number of SGEs needed for a full packet
1690 * and round it to the next power of two.
1692 sges_n = log2above((size / mb_len) + !!(size % mb_len));
1693 tmpl->rxq.sges_n = sges_n;
1694 /* Make sure rxq.sges_n did not overflow. */
1695 size = mb_len * (1 << tmpl->rxq.sges_n);
1696 size -= RTE_PKTMBUF_HEADROOM;
1697 if (size < max_rx_pkt_len) {
1699 "port %u too many SGEs (%u) needed to handle"
1700 " requested maximum packet size %u",
1704 rte_errno = EOVERFLOW;
1708 if (mprq_en && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
1710 "port %u MPRQ is requested but cannot be enabled"
1711 " (requested: desc = %u, stride_sz = %u,"
1712 " supported: min_stride_num = %u, max_stride_sz = %u).",
1713 dev->data->port_id, desc, mprq_stride_size,
1714 (1 << config->mprq.stride_num_n),
1715 (1 << config->mprq.max_stride_size_n));
1716 DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
1717 dev->data->port_id, 1 << tmpl->rxq.sges_n);
1718 if (desc % (1 << tmpl->rxq.sges_n)) {
1720 "port %u number of Rx queue descriptors (%u) is not a"
1721 " multiple of SGEs per packet (%u)",
1724 1 << tmpl->rxq.sges_n);
1728 /* Toggle RX checksum offload if hardware supports it. */
1729 tmpl->rxq.csum = !!(offloads & DEV_RX_OFFLOAD_CHECKSUM);
1730 tmpl->rxq.hw_timestamp = !!(offloads & DEV_RX_OFFLOAD_TIMESTAMP);
1731 /* Configure VLAN stripping. */
1732 tmpl->rxq.vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1733 /* By default, FCS (CRC) is stripped by hardware. */
1734 tmpl->rxq.crc_present = 0;
1735 if (offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
1736 if (config->hw_fcs_strip) {
1738 * RQs used for LRO-enabled TIRs should not be
1739 * configured to scatter the FCS.
1741 if (mlx5_lro_on(dev))
1743 "port %u CRC stripping has been "
1744 "disabled but will still be performed "
1745 "by hardware, because LRO is enabled",
1746 dev->data->port_id);
1748 tmpl->rxq.crc_present = 1;
1751 "port %u CRC stripping has been disabled but will"
1752 " still be performed by hardware, make sure MLNX_OFED"
1753 " and firmware are up to date",
1754 dev->data->port_id);
1758 "port %u CRC stripping is %s, %u bytes will be subtracted from"
1759 " incoming frames to hide it",
1761 tmpl->rxq.crc_present ? "disabled" : "enabled",
1762 tmpl->rxq.crc_present << 2);
1764 tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
1765 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
1766 tmpl->rxq.port_id = dev->data->port_id;
1769 tmpl->rxq.elts_n = log2above(desc);
1770 tmpl->rxq.rq_repl_thresh =
1771 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
1773 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1775 tmpl->rxq.uar_lock_cq = &priv->uar_lock_cq;
1777 tmpl->rxq.idx = idx;
1778 rte_atomic32_inc(&tmpl->refcnt);
1779 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1790 * Pointer to Ethernet device.
1795 * A pointer to the queue if it exists, NULL otherwise.
1797 struct mlx5_rxq_ctrl *
1798 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
1800 struct mlx5_priv *priv = dev->data->dev_private;
1801 struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1803 if ((*priv->rxqs)[idx]) {
1804 rxq_ctrl = container_of((*priv->rxqs)[idx],
1805 struct mlx5_rxq_ctrl,
1807 mlx5_rxq_obj_get(dev, idx);
1808 rte_atomic32_inc(&rxq_ctrl->refcnt);
1814 * Release a Rx queue.
1817 * Pointer to Ethernet device.
1822 * 1 while a reference on it exists, 0 when freed.
1825 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
1827 struct mlx5_priv *priv = dev->data->dev_private;
1828 struct mlx5_rxq_ctrl *rxq_ctrl;
1830 if (!(*priv->rxqs)[idx])
1832 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1833 assert(rxq_ctrl->priv);
1834 if (rxq_ctrl->obj && !mlx5_rxq_obj_release(rxq_ctrl->obj))
1835 rxq_ctrl->obj = NULL;
1836 if (rte_atomic32_dec_and_test(&rxq_ctrl->refcnt)) {
1837 if (rxq_ctrl->dbr_umem_id_valid)
1838 claim_zero(mlx5_release_dbr(dev, rxq_ctrl->dbr_umem_id,
1839 rxq_ctrl->dbr_offset));
1840 mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
1841 LIST_REMOVE(rxq_ctrl, next);
1843 (*priv->rxqs)[idx] = NULL;
1850 * Verify the Rx Queue list is empty
1853 * Pointer to Ethernet device.
1856 * The number of object not released.
1859 mlx5_rxq_verify(struct rte_eth_dev *dev)
1861 struct mlx5_priv *priv = dev->data->dev_private;
1862 struct mlx5_rxq_ctrl *rxq_ctrl;
1865 LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1866 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
1867 dev->data->port_id, rxq_ctrl->rxq.idx);
1874 * Create an indirection table.
1877 * Pointer to Ethernet device.
1879 * Queues entering in the indirection table.
1881 * Number of queues in the array.
1884 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
1886 static struct mlx5_ind_table_obj *
1887 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
1888 uint32_t queues_n, enum mlx5_ind_tbl_type type)
1890 struct mlx5_priv *priv = dev->data->dev_private;
1891 struct mlx5_ind_table_obj *ind_tbl;
1892 unsigned int i = 0, j = 0, k = 0;
1894 ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl) +
1895 queues_n * sizeof(uint16_t), 0);
1900 ind_tbl->type = type;
1901 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
1902 const unsigned int wq_n = rte_is_power_of_2(queues_n) ?
1903 log2above(queues_n) :
1904 log2above(priv->config.ind_table_max_size);
1905 struct ibv_wq *wq[1 << wq_n];
1907 for (i = 0; i != queues_n; ++i) {
1908 struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
1912 wq[i] = rxq->obj->wq;
1913 ind_tbl->queues[i] = queues[i];
1915 ind_tbl->queues_n = queues_n;
1916 /* Finalise indirection table. */
1917 k = i; /* Retain value of i for use in error case. */
1918 for (j = 0; k != (unsigned int)(1 << wq_n); ++k, ++j)
1920 ind_tbl->ind_table = mlx5_glue->create_rwq_ind_table
1922 &(struct ibv_rwq_ind_table_init_attr){
1923 .log_ind_tbl_size = wq_n,
1927 if (!ind_tbl->ind_table) {
1931 } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
1932 struct mlx5_devx_rqt_attr *rqt_attr = NULL;
1934 rqt_attr = rte_calloc(__func__, 1, sizeof(*rqt_attr) +
1935 queues_n * sizeof(uint16_t), 0);
1937 DRV_LOG(ERR, "port %u cannot allocate RQT resources",
1938 dev->data->port_id);
1942 rqt_attr->rqt_max_size = priv->config.ind_table_max_size;
1943 rqt_attr->rqt_actual_size = queues_n;
1944 for (i = 0; i != queues_n; ++i) {
1945 struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev,
1949 rqt_attr->rq_list[i] = rxq->obj->rq->id;
1950 ind_tbl->queues[i] = queues[i];
1952 ind_tbl->rqt = mlx5_devx_cmd_create_rqt(priv->sh->ctx,
1955 if (!ind_tbl->rqt) {
1956 DRV_LOG(ERR, "port %u cannot create DevX RQT",
1957 dev->data->port_id);
1961 ind_tbl->queues_n = queues_n;
1963 rte_atomic32_inc(&ind_tbl->refcnt);
1964 LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1967 for (j = 0; j < i; j++)
1968 mlx5_rxq_release(dev, ind_tbl->queues[j]);
1970 DEBUG("port %u cannot create indirection table", dev->data->port_id);
1975 * Get an indirection table.
1978 * Pointer to Ethernet device.
1980 * Queues entering in the indirection table.
1982 * Number of queues in the array.
1985 * An indirection table if found.
1987 static struct mlx5_ind_table_obj *
1988 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
1991 struct mlx5_priv *priv = dev->data->dev_private;
1992 struct mlx5_ind_table_obj *ind_tbl;
1994 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1995 if ((ind_tbl->queues_n == queues_n) &&
1996 (memcmp(ind_tbl->queues, queues,
1997 ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
2004 rte_atomic32_inc(&ind_tbl->refcnt);
2005 for (i = 0; i != ind_tbl->queues_n; ++i)
2006 mlx5_rxq_get(dev, ind_tbl->queues[i]);
2012 * Release an indirection table.
2015 * Pointer to Ethernet device.
2017 * Indirection table to release.
2020 * 1 while a reference on it exists, 0 when freed.
2023 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
2024 struct mlx5_ind_table_obj *ind_tbl)
2028 if (rte_atomic32_dec_and_test(&ind_tbl->refcnt)) {
2029 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV)
2030 claim_zero(mlx5_glue->destroy_rwq_ind_table
2031 (ind_tbl->ind_table));
2032 else if (ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX)
2033 claim_zero(mlx5_devx_cmd_destroy(ind_tbl->rqt));
2035 for (i = 0; i != ind_tbl->queues_n; ++i)
2036 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
2037 if (!rte_atomic32_read(&ind_tbl->refcnt)) {
2038 LIST_REMOVE(ind_tbl, next);
2046 * Verify the Rx Queue list is empty
2049 * Pointer to Ethernet device.
2052 * The number of object not released.
2055 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
2057 struct mlx5_priv *priv = dev->data->dev_private;
2058 struct mlx5_ind_table_obj *ind_tbl;
2061 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2063 "port %u indirection table obj %p still referenced",
2064 dev->data->port_id, (void *)ind_tbl);
2071 * Create an Rx Hash queue.
2074 * Pointer to Ethernet device.
2076 * RSS key for the Rx hash queue.
2077 * @param rss_key_len
2079 * @param hash_fields
2080 * Verbs protocol hash field to make the RSS on.
2082 * Queues entering in hash queue. In case of empty hash_fields only the
2083 * first queue index will be taken for the indirection table.
2089 * Flow rule is relevant for LRO, i.e. contains IPv4/IPv6 and TCP.
2092 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2095 mlx5_hrxq_new(struct rte_eth_dev *dev,
2096 const uint8_t *rss_key, uint32_t rss_key_len,
2097 uint64_t hash_fields,
2098 const uint16_t *queues, uint32_t queues_n,
2099 int tunnel __rte_unused, int lro)
2101 struct mlx5_priv *priv = dev->data->dev_private;
2102 struct mlx5_hrxq *hrxq;
2103 struct ibv_qp *qp = NULL;
2104 struct mlx5_ind_table_obj *ind_tbl;
2106 struct mlx5_devx_obj *tir = NULL;
2108 queues_n = hash_fields ? queues_n : 1;
2109 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2111 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[queues[0]];
2112 struct mlx5_rxq_ctrl *rxq_ctrl =
2113 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
2114 enum mlx5_ind_tbl_type type;
2116 type = rxq_ctrl->obj->type == MLX5_RXQ_OBJ_TYPE_IBV ?
2117 MLX5_IND_TBL_TYPE_IBV : MLX5_IND_TBL_TYPE_DEVX;
2118 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n, type);
2124 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2125 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
2126 struct mlx5dv_qp_init_attr qp_init_attr;
2128 memset(&qp_init_attr, 0, sizeof(qp_init_attr));
2130 qp_init_attr.comp_mask =
2131 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2132 qp_init_attr.create_flags =
2133 MLX5DV_QP_CREATE_TUNNEL_OFFLOADS;
2135 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2136 if (dev->data->dev_conf.lpbk_mode) {
2138 * Allow packet sent from NIC loop back
2139 * w/o source MAC check.
2141 qp_init_attr.comp_mask |=
2142 MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
2143 qp_init_attr.create_flags |=
2144 MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
2147 qp = mlx5_glue->dv_create_qp
2149 &(struct ibv_qp_init_attr_ex){
2150 .qp_type = IBV_QPT_RAW_PACKET,
2152 IBV_QP_INIT_ATTR_PD |
2153 IBV_QP_INIT_ATTR_IND_TABLE |
2154 IBV_QP_INIT_ATTR_RX_HASH,
2155 .rx_hash_conf = (struct ibv_rx_hash_conf){
2157 IBV_RX_HASH_FUNC_TOEPLITZ,
2158 .rx_hash_key_len = rss_key_len,
2160 (void *)(uintptr_t)rss_key,
2161 .rx_hash_fields_mask = hash_fields,
2163 .rwq_ind_tbl = ind_tbl->ind_table,
2168 qp = mlx5_glue->create_qp_ex
2170 &(struct ibv_qp_init_attr_ex){
2171 .qp_type = IBV_QPT_RAW_PACKET,
2173 IBV_QP_INIT_ATTR_PD |
2174 IBV_QP_INIT_ATTR_IND_TABLE |
2175 IBV_QP_INIT_ATTR_RX_HASH,
2176 .rx_hash_conf = (struct ibv_rx_hash_conf){
2178 IBV_RX_HASH_FUNC_TOEPLITZ,
2179 .rx_hash_key_len = rss_key_len,
2181 (void *)(uintptr_t)rss_key,
2182 .rx_hash_fields_mask = hash_fields,
2184 .rwq_ind_tbl = ind_tbl->ind_table,
2192 } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2193 struct mlx5_devx_tir_attr tir_attr;
2195 memset(&tir_attr, 0, sizeof(tir_attr));
2196 tir_attr.disp_type = MLX5_TIRC_DISP_TYPE_INDIRECT;
2197 tir_attr.rx_hash_fn = MLX5_RX_HASH_FN_TOEPLITZ;
2198 memcpy(&tir_attr.rx_hash_field_selector_outer, &hash_fields,
2200 tir_attr.transport_domain = priv->sh->tdn;
2201 memcpy(tir_attr.rx_hash_toeplitz_key, rss_key, rss_key_len);
2202 tir_attr.indirect_table = ind_tbl->rqt->id;
2203 if (dev->data->dev_conf.lpbk_mode)
2204 tir_attr.self_lb_block =
2205 MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
2207 tir_attr.lro_timeout_period_usecs =
2208 priv->config.lro.timeout;
2209 tir_attr.lro_max_msg_sz = priv->max_lro_msg_size;
2210 tir_attr.lro_enable_mask = lro;
2212 tir = mlx5_devx_cmd_create_tir(priv->sh->ctx, &tir_attr);
2214 DRV_LOG(ERR, "port %u cannot create DevX TIR",
2215 dev->data->port_id);
2220 hrxq = rte_calloc(__func__, 1, sizeof(*hrxq) + rss_key_len, 0);
2223 hrxq->ind_table = ind_tbl;
2224 if (ind_tbl->type == MLX5_IND_TBL_TYPE_IBV) {
2226 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2228 mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
2229 if (!hrxq->action) {
2234 } else { /* ind_tbl->type == MLX5_IND_TBL_TYPE_DEVX */
2236 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2237 hrxq->action = mlx5_glue->dv_create_flow_action_dest_devx_tir
2239 if (!hrxq->action) {
2245 hrxq->rss_key_len = rss_key_len;
2246 hrxq->hash_fields = hash_fields;
2247 memcpy(hrxq->rss_key, rss_key, rss_key_len);
2248 rte_atomic32_inc(&hrxq->refcnt);
2249 LIST_INSERT_HEAD(&priv->hrxqs, hrxq, next);
2252 err = rte_errno; /* Save rte_errno before cleanup. */
2253 mlx5_ind_table_obj_release(dev, ind_tbl);
2255 claim_zero(mlx5_glue->destroy_qp(qp));
2257 claim_zero(mlx5_devx_cmd_destroy(tir));
2258 rte_errno = err; /* Restore rte_errno. */
2263 * Get an Rx Hash queue.
2266 * Pointer to Ethernet device.
2268 * RSS configuration for the Rx hash queue.
2270 * Queues entering in hash queue. In case of empty hash_fields only the
2271 * first queue index will be taken for the indirection table.
2276 * An hash Rx queue on success.
2279 mlx5_hrxq_get(struct rte_eth_dev *dev,
2280 const uint8_t *rss_key, uint32_t rss_key_len,
2281 uint64_t hash_fields,
2282 const uint16_t *queues, uint32_t queues_n)
2284 struct mlx5_priv *priv = dev->data->dev_private;
2285 struct mlx5_hrxq *hrxq;
2287 queues_n = hash_fields ? queues_n : 1;
2288 LIST_FOREACH(hrxq, &priv->hrxqs, next) {
2289 struct mlx5_ind_table_obj *ind_tbl;
2291 if (hrxq->rss_key_len != rss_key_len)
2293 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
2295 if (hrxq->hash_fields != hash_fields)
2297 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2300 if (ind_tbl != hrxq->ind_table) {
2301 mlx5_ind_table_obj_release(dev, ind_tbl);
2304 rte_atomic32_inc(&hrxq->refcnt);
2311 * Release the hash Rx queue.
2314 * Pointer to Ethernet device.
2316 * Pointer to Hash Rx queue to release.
2319 * 1 while a reference on it exists, 0 when freed.
2322 mlx5_hrxq_release(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq)
2324 if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
2325 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2326 mlx5_glue->destroy_flow_action(hrxq->action);
2328 if (hrxq->ind_table->type == MLX5_IND_TBL_TYPE_IBV)
2329 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
2330 else /* hrxq->ind_table->type == MLX5_IND_TBL_TYPE_DEVX */
2331 claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
2332 mlx5_ind_table_obj_release(dev, hrxq->ind_table);
2333 LIST_REMOVE(hrxq, next);
2337 claim_nonzero(mlx5_ind_table_obj_release(dev, hrxq->ind_table));
2342 * Verify the Rx Queue list is empty
2345 * Pointer to Ethernet device.
2348 * The number of object not released.
2351 mlx5_hrxq_verify(struct rte_eth_dev *dev)
2353 struct mlx5_priv *priv = dev->data->dev_private;
2354 struct mlx5_hrxq *hrxq;
2357 LIST_FOREACH(hrxq, &priv->hrxqs, next) {
2359 "port %u hash Rx queue %p still referenced",
2360 dev->data->port_id, (void *)hrxq);
2367 * Create a drop Rx queue Verbs/DevX object.
2370 * Pointer to Ethernet device.
2373 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2375 static struct mlx5_rxq_obj *
2376 mlx5_rxq_obj_drop_new(struct rte_eth_dev *dev)
2378 struct mlx5_priv *priv = dev->data->dev_private;
2379 struct ibv_context *ctx = priv->sh->ctx;
2381 struct ibv_wq *wq = NULL;
2382 struct mlx5_rxq_obj *rxq;
2384 if (priv->drop_queue.rxq)
2385 return priv->drop_queue.rxq;
2386 cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
2388 DEBUG("port %u cannot allocate CQ for drop queue",
2389 dev->data->port_id);
2393 wq = mlx5_glue->create_wq(ctx,
2394 &(struct ibv_wq_init_attr){
2395 .wq_type = IBV_WQT_RQ,
2402 DEBUG("port %u cannot allocate WQ for drop queue",
2403 dev->data->port_id);
2407 rxq = rte_calloc(__func__, 1, sizeof(*rxq), 0);
2409 DEBUG("port %u cannot allocate drop Rx queue memory",
2410 dev->data->port_id);
2416 priv->drop_queue.rxq = rxq;
2420 claim_zero(mlx5_glue->destroy_wq(wq));
2422 claim_zero(mlx5_glue->destroy_cq(cq));
2427 * Release a drop Rx queue Verbs/DevX object.
2430 * Pointer to Ethernet device.
2433 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2436 mlx5_rxq_obj_drop_release(struct rte_eth_dev *dev)
2438 struct mlx5_priv *priv = dev->data->dev_private;
2439 struct mlx5_rxq_obj *rxq = priv->drop_queue.rxq;
2442 claim_zero(mlx5_glue->destroy_wq(rxq->wq));
2444 claim_zero(mlx5_glue->destroy_cq(rxq->cq));
2446 priv->drop_queue.rxq = NULL;
2450 * Create a drop indirection table.
2453 * Pointer to Ethernet device.
2456 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2458 static struct mlx5_ind_table_obj *
2459 mlx5_ind_table_obj_drop_new(struct rte_eth_dev *dev)
2461 struct mlx5_priv *priv = dev->data->dev_private;
2462 struct mlx5_ind_table_obj *ind_tbl;
2463 struct mlx5_rxq_obj *rxq;
2464 struct mlx5_ind_table_obj tmpl;
2466 rxq = mlx5_rxq_obj_drop_new(dev);
2469 tmpl.ind_table = mlx5_glue->create_rwq_ind_table
2471 &(struct ibv_rwq_ind_table_init_attr){
2472 .log_ind_tbl_size = 0,
2473 .ind_tbl = &rxq->wq,
2476 if (!tmpl.ind_table) {
2477 DEBUG("port %u cannot allocate indirection table for drop"
2479 dev->data->port_id);
2483 ind_tbl = rte_calloc(__func__, 1, sizeof(*ind_tbl), 0);
2488 ind_tbl->ind_table = tmpl.ind_table;
2491 mlx5_rxq_obj_drop_release(dev);
2496 * Release a drop indirection table.
2499 * Pointer to Ethernet device.
2502 mlx5_ind_table_obj_drop_release(struct rte_eth_dev *dev)
2504 struct mlx5_priv *priv = dev->data->dev_private;
2505 struct mlx5_ind_table_obj *ind_tbl = priv->drop_queue.hrxq->ind_table;
2507 claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl->ind_table));
2508 mlx5_rxq_obj_drop_release(dev);
2510 priv->drop_queue.hrxq->ind_table = NULL;
2514 * Create a drop Rx Hash queue.
2517 * Pointer to Ethernet device.
2520 * The Verbs/DevX object initialised, NULL otherwise and rte_errno is set.
2523 mlx5_hrxq_drop_new(struct rte_eth_dev *dev)
2525 struct mlx5_priv *priv = dev->data->dev_private;
2526 struct mlx5_ind_table_obj *ind_tbl;
2528 struct mlx5_hrxq *hrxq;
2530 if (priv->drop_queue.hrxq) {
2531 rte_atomic32_inc(&priv->drop_queue.hrxq->refcnt);
2532 return priv->drop_queue.hrxq;
2534 ind_tbl = mlx5_ind_table_obj_drop_new(dev);
2537 qp = mlx5_glue->create_qp_ex(priv->sh->ctx,
2538 &(struct ibv_qp_init_attr_ex){
2539 .qp_type = IBV_QPT_RAW_PACKET,
2541 IBV_QP_INIT_ATTR_PD |
2542 IBV_QP_INIT_ATTR_IND_TABLE |
2543 IBV_QP_INIT_ATTR_RX_HASH,
2544 .rx_hash_conf = (struct ibv_rx_hash_conf){
2546 IBV_RX_HASH_FUNC_TOEPLITZ,
2547 .rx_hash_key_len = MLX5_RSS_HASH_KEY_LEN,
2548 .rx_hash_key = rss_hash_default_key,
2549 .rx_hash_fields_mask = 0,
2551 .rwq_ind_tbl = ind_tbl->ind_table,
2555 DEBUG("port %u cannot allocate QP for drop queue",
2556 dev->data->port_id);
2560 hrxq = rte_calloc(__func__, 1, sizeof(*hrxq), 0);
2563 "port %u cannot allocate memory for drop queue",
2564 dev->data->port_id);
2568 hrxq->ind_table = ind_tbl;
2570 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2571 hrxq->action = mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
2572 if (!hrxq->action) {
2577 priv->drop_queue.hrxq = hrxq;
2578 rte_atomic32_set(&hrxq->refcnt, 1);
2582 mlx5_ind_table_obj_drop_release(dev);
2587 * Release a drop hash Rx queue.
2590 * Pointer to Ethernet device.
2593 mlx5_hrxq_drop_release(struct rte_eth_dev *dev)
2595 struct mlx5_priv *priv = dev->data->dev_private;
2596 struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
2598 if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
2599 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2600 mlx5_glue->destroy_flow_action(hrxq->action);
2602 claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
2603 mlx5_ind_table_obj_drop_release(dev);
2605 priv->drop_queue.hrxq = NULL;