1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
11 #include <sys/queue.h>
14 #include <rte_malloc.h>
15 #include <ethdev_driver.h>
16 #include <rte_common.h>
17 #include <rte_interrupts.h>
18 #include <rte_debug.h>
20 #include <rte_eal_paging.h>
22 #include <mlx5_glue.h>
23 #include <mlx5_malloc.h>
24 #include <mlx5_common.h>
25 #include <mlx5_common_mr.h>
27 #include "mlx5_defs.h"
30 #include "mlx5_utils.h"
31 #include "mlx5_autoconf.h"
32 #include "mlx5_devx.h"
35 /* Default RSS hash key also used for ConnectX-3. */
36 uint8_t rss_hash_default_key[] = {
37 0x2c, 0xc6, 0x81, 0xd1,
38 0x5b, 0xdb, 0xf4, 0xf7,
39 0xfc, 0xa2, 0x83, 0x19,
40 0xdb, 0x1a, 0x3e, 0x94,
41 0x6b, 0x9e, 0x38, 0xd9,
42 0x2c, 0x9c, 0x03, 0xd1,
43 0xad, 0x99, 0x44, 0xa7,
44 0xd9, 0x56, 0x3d, 0x59,
45 0x06, 0x3c, 0x25, 0xf3,
46 0xfc, 0x1f, 0xdc, 0x2a,
49 /* Length of the default RSS hash key. */
50 static_assert(MLX5_RSS_HASH_KEY_LEN ==
51 (unsigned int)sizeof(rss_hash_default_key),
52 "wrong RSS default key size.");
55 * Calculate the number of CQEs in CQ for the Rx queue.
58 * Pointer to receive queue structure.
61 * Number of CQEs in CQ.
64 mlx5_rxq_cqe_num(struct mlx5_rxq_data *rxq_data)
67 unsigned int wqe_n = 1 << rxq_data->elts_n;
69 if (mlx5_rxq_mprq_enabled(rxq_data))
70 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
77 * Allocate RX queue elements for Multi-Packet RQ.
80 * Pointer to RX queue structure.
83 * 0 on success, a negative errno value otherwise and rte_errno is set.
86 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
88 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
89 unsigned int wqe_n = 1 << rxq->elts_n;
93 /* Iterate on segments. */
94 for (i = 0; i <= wqe_n; ++i) {
95 struct mlx5_mprq_buf *buf;
97 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
98 DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
103 (*rxq->mprq_bufs)[i] = buf;
105 rxq->mprq_repl = buf;
108 "port %u MPRQ queue %u allocated and configured %u segments",
109 rxq->port_id, rxq->idx, wqe_n);
112 err = rte_errno; /* Save rte_errno before cleanup. */
114 for (i = 0; (i != wqe_n); ++i) {
115 if ((*rxq->mprq_bufs)[i] != NULL)
116 rte_mempool_put(rxq->mprq_mp,
117 (*rxq->mprq_bufs)[i]);
118 (*rxq->mprq_bufs)[i] = NULL;
120 DRV_LOG(DEBUG, "port %u MPRQ queue %u failed, freed everything",
121 rxq->port_id, rxq->idx);
122 rte_errno = err; /* Restore rte_errno. */
127 * Allocate RX queue elements for Single-Packet RQ.
130 * Pointer to RX queue structure.
133 * 0 on success, negative errno value on failure.
136 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
138 const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
139 unsigned int elts_n = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
140 (1 << rxq_ctrl->rxq.elts_n) * (1 << rxq_ctrl->rxq.strd_num_n) :
141 (1 << rxq_ctrl->rxq.elts_n);
145 /* Iterate on segments. */
146 for (i = 0; (i != elts_n); ++i) {
147 struct mlx5_eth_rxseg *seg = &rxq_ctrl->rxq.rxseg[i % sges_n];
148 struct rte_mbuf *buf;
150 buf = rte_pktmbuf_alloc(seg->mp);
152 if (rxq_ctrl->share_group == 0)
153 DRV_LOG(ERR, "port %u queue %u empty mbuf pool",
154 RXQ_PORT_ID(rxq_ctrl),
157 DRV_LOG(ERR, "share group %u queue %u empty mbuf pool",
158 rxq_ctrl->share_group,
159 rxq_ctrl->share_qid);
163 /* Headroom is reserved by rte_pktmbuf_alloc(). */
164 MLX5_ASSERT(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
165 /* Buffer is supposed to be empty. */
166 MLX5_ASSERT(rte_pktmbuf_data_len(buf) == 0);
167 MLX5_ASSERT(rte_pktmbuf_pkt_len(buf) == 0);
168 MLX5_ASSERT(!buf->next);
169 SET_DATA_OFF(buf, seg->offset);
170 PORT(buf) = rxq_ctrl->rxq.port_id;
171 DATA_LEN(buf) = seg->length;
172 PKT_LEN(buf) = seg->length;
174 (*rxq_ctrl->rxq.elts)[i] = buf;
176 /* If Rx vector is activated. */
177 if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
178 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
179 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
180 struct rte_pktmbuf_pool_private *priv =
181 (struct rte_pktmbuf_pool_private *)
182 rte_mempool_get_priv(rxq_ctrl->rxq.mp);
185 /* Initialize default rearm_data for vPMD. */
186 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
187 rte_mbuf_refcnt_set(mbuf_init, 1);
188 mbuf_init->nb_segs = 1;
189 /* For shared queues port is provided in CQE */
190 mbuf_init->port = rxq->shared ? 0 : rxq->port_id;
191 if (priv->flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
192 mbuf_init->ol_flags = RTE_MBUF_F_EXTERNAL;
194 * prevent compiler reordering:
195 * rearm_data covers previous fields.
197 rte_compiler_barrier();
198 rxq->mbuf_initializer =
199 *(rte_xmm_t *)&mbuf_init->rearm_data;
200 /* Padding with a fake mbuf for vectorized Rx. */
201 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
202 (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
204 if (rxq_ctrl->share_group == 0)
206 "port %u SPRQ queue %u allocated and configured %u segments (max %u packets)",
207 RXQ_PORT_ID(rxq_ctrl), rxq_ctrl->rxq.idx, elts_n,
208 elts_n / (1 << rxq_ctrl->rxq.sges_n));
211 "share group %u SPRQ queue %u allocated and configured %u segments (max %u packets)",
212 rxq_ctrl->share_group, rxq_ctrl->share_qid, elts_n,
213 elts_n / (1 << rxq_ctrl->rxq.sges_n));
216 err = rte_errno; /* Save rte_errno before cleanup. */
218 for (i = 0; (i != elts_n); ++i) {
219 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
220 rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
221 (*rxq_ctrl->rxq.elts)[i] = NULL;
223 if (rxq_ctrl->share_group == 0)
224 DRV_LOG(DEBUG, "port %u SPRQ queue %u failed, freed everything",
225 RXQ_PORT_ID(rxq_ctrl), rxq_ctrl->rxq.idx);
227 DRV_LOG(DEBUG, "share group %u SPRQ queue %u failed, freed everything",
228 rxq_ctrl->share_group, rxq_ctrl->share_qid);
229 rte_errno = err; /* Restore rte_errno. */
234 * Allocate RX queue elements.
237 * Pointer to RX queue structure.
240 * 0 on success, negative errno value on failure.
243 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
248 * For MPRQ we need to allocate both MPRQ buffers
249 * for WQEs and simple mbufs for vector processing.
251 if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
252 ret = rxq_alloc_elts_mprq(rxq_ctrl);
254 ret = rxq_alloc_elts_sprq(rxq_ctrl);
259 * Free RX queue elements for Multi-Packet RQ.
262 * Pointer to RX queue structure.
265 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
267 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
270 DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing %d WRs",
271 rxq->port_id, rxq->idx, (1u << rxq->elts_n));
272 if (rxq->mprq_bufs == NULL)
274 for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
275 if ((*rxq->mprq_bufs)[i] != NULL)
276 mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
277 (*rxq->mprq_bufs)[i] = NULL;
279 if (rxq->mprq_repl != NULL) {
280 mlx5_mprq_buf_free(rxq->mprq_repl);
281 rxq->mprq_repl = NULL;
286 * Free RX queue elements for Single-Packet RQ.
289 * Pointer to RX queue structure.
292 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
294 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
295 const uint16_t q_n = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
296 (1 << rxq->elts_n) * (1 << rxq->strd_num_n) :
298 const uint16_t q_mask = q_n - 1;
299 uint16_t elts_ci = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
300 rxq->elts_ci : rxq->rq_ci;
301 uint16_t used = q_n - (elts_ci - rxq->rq_pi);
304 if (rxq_ctrl->share_group == 0)
305 DRV_LOG(DEBUG, "port %u Rx queue %u freeing %d WRs",
306 RXQ_PORT_ID(rxq_ctrl), rxq->idx, q_n);
308 DRV_LOG(DEBUG, "share group %u Rx queue %u freeing %d WRs",
309 rxq_ctrl->share_group, rxq_ctrl->share_qid, q_n);
310 if (rxq->elts == NULL)
313 * Some mbuf in the Ring belongs to the application.
314 * They cannot be freed.
316 if (mlx5_rxq_check_vec_support(rxq) > 0) {
317 for (i = 0; i < used; ++i)
318 (*rxq->elts)[(elts_ci + i) & q_mask] = NULL;
319 rxq->rq_pi = elts_ci;
321 for (i = 0; i != q_n; ++i) {
322 if ((*rxq->elts)[i] != NULL)
323 rte_pktmbuf_free_seg((*rxq->elts)[i]);
324 (*rxq->elts)[i] = NULL;
329 * Free RX queue elements.
332 * Pointer to RX queue structure.
335 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
338 * For MPRQ we need to allocate both MPRQ buffers
339 * for WQEs and simple mbufs for vector processing.
341 if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
342 rxq_free_elts_mprq(rxq_ctrl);
343 rxq_free_elts_sprq(rxq_ctrl);
347 * Returns the per-queue supported offloads.
350 * Pointer to Ethernet device.
353 * Supported Rx offloads.
356 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
358 struct mlx5_priv *priv = dev->data->dev_private;
359 struct mlx5_dev_config *config = &priv->config;
360 uint64_t offloads = (RTE_ETH_RX_OFFLOAD_SCATTER |
361 RTE_ETH_RX_OFFLOAD_TIMESTAMP |
362 RTE_ETH_RX_OFFLOAD_RSS_HASH);
364 if (!config->mprq.enabled)
365 offloads |= RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT;
366 if (config->hw_fcs_strip)
367 offloads |= RTE_ETH_RX_OFFLOAD_KEEP_CRC;
369 offloads |= (RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
370 RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
371 RTE_ETH_RX_OFFLOAD_TCP_CKSUM);
372 if (config->hw_vlan_strip)
373 offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
374 if (MLX5_LRO_SUPPORTED(dev))
375 offloads |= RTE_ETH_RX_OFFLOAD_TCP_LRO;
381 * Returns the per-port supported offloads.
384 * Supported Rx offloads.
387 mlx5_get_rx_port_offloads(void)
389 uint64_t offloads = RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
395 * Verify if the queue can be released.
398 * Pointer to Ethernet device.
403 * 1 if the queue can be released
404 * 0 if the queue can not be released, there are references to it.
405 * Negative errno and rte_errno is set if queue doesn't exist.
408 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
410 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
416 return (__atomic_load_n(&rxq->refcnt, __ATOMIC_RELAXED) == 1);
419 /* Fetches and drops all SW-owned and error CQEs to synchronize CQ. */
421 rxq_sync_cq(struct mlx5_rxq_data *rxq)
423 const uint16_t cqe_n = 1 << rxq->cqe_n;
424 const uint16_t cqe_mask = cqe_n - 1;
425 volatile struct mlx5_cqe *cqe;
430 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_mask];
431 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
432 if (ret == MLX5_CQE_STATUS_HW_OWN)
434 if (ret == MLX5_CQE_STATUS_ERR) {
438 MLX5_ASSERT(ret == MLX5_CQE_STATUS_SW_OWN);
439 if (MLX5_CQE_FORMAT(cqe->op_own) != MLX5_COMPRESSED) {
443 /* Compute the next non compressed CQE. */
444 rxq->cq_ci += rte_be_to_cpu_32(cqe->byte_cnt);
447 /* Move all CQEs to HW ownership, including possible MiniCQEs. */
448 for (i = 0; i < cqe_n; i++) {
449 cqe = &(*rxq->cqes)[i];
450 cqe->op_own = MLX5_CQE_INVALIDATE;
452 /* Resync CQE and WQE (WQ in RESET state). */
454 *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
456 *rxq->rq_db = rte_cpu_to_be_32(0);
461 * Rx queue stop. Device queue goes to the RESET state,
462 * all involved mbufs are freed from WQ.
465 * Pointer to Ethernet device structure.
470 * 0 on success, a negative errno value otherwise and rte_errno is set.
473 mlx5_rx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t idx)
475 struct mlx5_priv *priv = dev->data->dev_private;
476 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
477 struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
480 MLX5_ASSERT(rxq != NULL && rxq_ctrl != NULL);
481 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
482 ret = priv->obj_ops.rxq_obj_modify(rxq, MLX5_RXQ_MOD_RDY2RST);
484 DRV_LOG(ERR, "Cannot change Rx WQ state to RESET: %s",
489 /* Remove all processes CQEs. */
490 rxq_sync_cq(&rxq_ctrl->rxq);
491 /* Free all involved mbufs. */
492 rxq_free_elts(rxq_ctrl);
493 /* Set the actual queue state. */
494 dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
499 * Rx queue stop. Device queue goes to the RESET state,
500 * all involved mbufs are freed from WQ.
503 * Pointer to Ethernet device structure.
508 * 0 on success, a negative errno value otherwise and rte_errno is set.
511 mlx5_rx_queue_stop(struct rte_eth_dev *dev, uint16_t idx)
513 eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
516 if (rte_eth_dev_is_rx_hairpin_queue(dev, idx)) {
517 DRV_LOG(ERR, "Hairpin queue can't be stopped");
521 if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STOPPED)
524 * Vectorized Rx burst requires the CQ and RQ indices
525 * synchronized, that might be broken on RQ restart
526 * and cause Rx malfunction, so queue stopping is
527 * not supported if vectorized Rx burst is engaged.
528 * The routine pointer depends on the process
529 * type, should perform check there.
531 if (pkt_burst == mlx5_rx_burst_vec) {
532 DRV_LOG(ERR, "Rx queue stop is not supported "
533 "for vectorized Rx");
537 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
538 ret = mlx5_mp_os_req_queue_control(dev, idx,
539 MLX5_MP_REQ_QUEUE_RX_STOP);
541 ret = mlx5_rx_queue_stop_primary(dev, idx);
547 * Rx queue start. Device queue goes to the ready state,
548 * all required mbufs are allocated and WQ is replenished.
551 * Pointer to Ethernet device structure.
556 * 0 on success, a negative errno value otherwise and rte_errno is set.
559 mlx5_rx_queue_start_primary(struct rte_eth_dev *dev, uint16_t idx)
561 struct mlx5_priv *priv = dev->data->dev_private;
562 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
563 struct mlx5_rxq_data *rxq_data = &rxq->ctrl->rxq;
566 MLX5_ASSERT(rxq != NULL && rxq->ctrl != NULL);
567 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
568 /* Allocate needed buffers. */
569 ret = rxq_alloc_elts(rxq->ctrl);
571 DRV_LOG(ERR, "Cannot reallocate buffers for Rx WQ");
576 *rxq_data->cq_db = rte_cpu_to_be_32(rxq_data->cq_ci);
578 /* Reset RQ consumer before moving queue to READY state. */
579 *rxq_data->rq_db = rte_cpu_to_be_32(0);
581 ret = priv->obj_ops.rxq_obj_modify(rxq, MLX5_RXQ_MOD_RST2RDY);
583 DRV_LOG(ERR, "Cannot change Rx WQ state to READY: %s",
588 /* Reinitialize RQ - set WQEs. */
589 mlx5_rxq_initialize(rxq_data);
590 rxq_data->err_state = MLX5_RXQ_ERR_STATE_NO_ERROR;
591 /* Set actual queue state. */
592 dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
597 * Rx queue start. Device queue goes to the ready state,
598 * all required mbufs are allocated and WQ is replenished.
601 * Pointer to Ethernet device structure.
606 * 0 on success, a negative errno value otherwise and rte_errno is set.
609 mlx5_rx_queue_start(struct rte_eth_dev *dev, uint16_t idx)
613 if (rte_eth_dev_is_rx_hairpin_queue(dev, idx)) {
614 DRV_LOG(ERR, "Hairpin queue can't be started");
618 if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STARTED)
620 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
621 ret = mlx5_mp_os_req_queue_control(dev, idx,
622 MLX5_MP_REQ_QUEUE_RX_START);
624 ret = mlx5_rx_queue_start_primary(dev, idx);
630 * Rx queue presetup checks.
633 * Pointer to Ethernet device structure.
637 * Number of descriptors to configure in queue.
638 * @param[out] rxq_ctrl
639 * Address of pointer to shared Rx queue control.
642 * 0 on success, a negative errno value otherwise and rte_errno is set.
645 mlx5_rx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t *desc,
646 struct mlx5_rxq_ctrl **rxq_ctrl)
648 struct mlx5_priv *priv = dev->data->dev_private;
649 struct mlx5_rxq_priv *rxq;
652 if (!rte_is_power_of_2(*desc)) {
653 *desc = 1 << log2above(*desc);
655 "port %u increased number of descriptors in Rx queue %u"
656 " to the next power of two (%d)",
657 dev->data->port_id, idx, *desc);
659 DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
660 dev->data->port_id, idx, *desc);
661 if (idx >= priv->rxqs_n) {
662 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
663 dev->data->port_id, idx, priv->rxqs_n);
664 rte_errno = EOVERFLOW;
667 if (rxq_ctrl == NULL || *rxq_ctrl == NULL)
669 if (!(*rxq_ctrl)->rxq.shared) {
670 if (!mlx5_rxq_releasable(dev, idx)) {
671 DRV_LOG(ERR, "port %u unable to release queue index %u",
672 dev->data->port_id, idx);
676 mlx5_rxq_release(dev, idx);
678 if ((*rxq_ctrl)->obj != NULL)
679 /* Some port using shared Rx queue has been started. */
681 /* Release all owner RxQ to reconfigure Shared RxQ. */
683 rxq = LIST_FIRST(&(*rxq_ctrl)->owners);
684 LIST_REMOVE(rxq, owner_entry);
685 empty = LIST_EMPTY(&(*rxq_ctrl)->owners);
686 mlx5_rxq_release(ETH_DEV(rxq->priv), rxq->idx);
694 * Get the shared Rx queue object that matches group and queue index.
697 * Pointer to Ethernet device structure.
701 * Shared RX queue index.
704 * Shared RXQ object that matching, or NULL if not found.
706 static struct mlx5_rxq_ctrl *
707 mlx5_shared_rxq_get(struct rte_eth_dev *dev, uint32_t group, uint16_t share_qid)
709 struct mlx5_rxq_ctrl *rxq_ctrl;
710 struct mlx5_priv *priv = dev->data->dev_private;
712 LIST_FOREACH(rxq_ctrl, &priv->sh->shared_rxqs, share_entry) {
713 if (rxq_ctrl->share_group == group &&
714 rxq_ctrl->share_qid == share_qid)
721 * Check whether requested Rx queue configuration matches shared RXQ.
724 * Pointer to shared RXQ.
726 * Pointer to Ethernet device structure.
730 * Number of descriptors to configure in queue.
732 * NUMA socket on which memory must be allocated.
734 * Thresholds parameters.
736 * Memory pool for buffer allocations.
739 * 0 on success, a negative errno value otherwise and rte_errno is set.
742 mlx5_shared_rxq_match(struct mlx5_rxq_ctrl *rxq_ctrl, struct rte_eth_dev *dev,
743 uint16_t idx, uint16_t desc, unsigned int socket,
744 const struct rte_eth_rxconf *conf,
745 struct rte_mempool *mp)
747 struct mlx5_priv *spriv = LIST_FIRST(&rxq_ctrl->owners)->priv;
748 struct mlx5_priv *priv = dev->data->dev_private;
752 if (rxq_ctrl->socket != socket) {
753 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: socket mismatch",
754 dev->data->port_id, idx);
757 if (rxq_ctrl->rxq.elts_n != log2above(desc)) {
758 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: descriptor number mismatch",
759 dev->data->port_id, idx);
762 if (priv->mtu != spriv->mtu) {
763 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: mtu mismatch",
764 dev->data->port_id, idx);
767 if (priv->dev_data->dev_conf.intr_conf.rxq !=
768 spriv->dev_data->dev_conf.intr_conf.rxq) {
769 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: interrupt mismatch",
770 dev->data->port_id, idx);
773 if (mp != NULL && rxq_ctrl->rxq.mp != mp) {
774 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: mempool mismatch",
775 dev->data->port_id, idx);
777 } else if (mp == NULL) {
778 if (conf->rx_nseg != rxq_ctrl->rxseg_n) {
779 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: segment number mismatch",
780 dev->data->port_id, idx);
783 for (i = 0; i < conf->rx_nseg; i++) {
784 if (memcmp(&conf->rx_seg[i].split, &rxq_ctrl->rxseg[i],
785 sizeof(struct rte_eth_rxseg_split))) {
786 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: segment %u configuration mismatch",
787 dev->data->port_id, idx, i);
792 if (priv->config.hw_padding != spriv->config.hw_padding) {
793 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: padding mismatch",
794 dev->data->port_id, idx);
797 if (priv->config.cqe_comp != spriv->config.cqe_comp ||
798 (priv->config.cqe_comp &&
799 priv->config.cqe_comp_fmt != spriv->config.cqe_comp_fmt)) {
800 DRV_LOG(ERR, "port %u queue index %u failed to join shared group: CQE compression mismatch",
801 dev->data->port_id, idx);
810 * Pointer to Ethernet device structure.
814 * Number of descriptors to configure in queue.
816 * NUMA socket on which memory must be allocated.
818 * Thresholds parameters.
820 * Memory pool for buffer allocations.
823 * 0 on success, a negative errno value otherwise and rte_errno is set.
826 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
827 unsigned int socket, const struct rte_eth_rxconf *conf,
828 struct rte_mempool *mp)
830 struct mlx5_priv *priv = dev->data->dev_private;
831 struct mlx5_rxq_priv *rxq;
832 struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
833 struct rte_eth_rxseg_split *rx_seg =
834 (struct rte_eth_rxseg_split *)conf->rx_seg;
835 struct rte_eth_rxseg_split rx_single = {.mp = mp};
836 uint16_t n_seg = conf->rx_nseg;
838 uint64_t offloads = conf->offloads |
839 dev->data->dev_conf.rxmode.offloads;
843 * The parameters should be checked on rte_eth_dev layer.
844 * If mp is specified it means the compatible configuration
845 * without buffer split feature tuning.
851 /* The offloads should be checked on rte_eth_dev layer. */
852 MLX5_ASSERT(offloads & RTE_ETH_RX_OFFLOAD_SCATTER);
853 if (!(offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT)) {
854 DRV_LOG(ERR, "port %u queue index %u split "
855 "offload not configured",
856 dev->data->port_id, idx);
860 MLX5_ASSERT(n_seg < MLX5_MAX_RXQ_NSEG);
862 if (conf->share_group > 0) {
863 if (!priv->config.hca_attr.mem_rq_rmp) {
864 DRV_LOG(ERR, "port %u queue index %u shared Rx queue not supported by fw",
865 dev->data->port_id, idx);
869 if (priv->obj_ops.rxq_obj_new != devx_obj_ops.rxq_obj_new) {
870 DRV_LOG(ERR, "port %u queue index %u shared Rx queue needs DevX api",
871 dev->data->port_id, idx);
875 if (conf->share_qid >= priv->rxqs_n) {
876 DRV_LOG(ERR, "port %u shared Rx queue index %u > number of Rx queues %u",
877 dev->data->port_id, conf->share_qid,
882 if (priv->config.mprq.enabled) {
883 DRV_LOG(ERR, "port %u shared Rx queue index %u: not supported when MPRQ enabled",
884 dev->data->port_id, conf->share_qid);
888 /* Try to reuse shared RXQ. */
889 rxq_ctrl = mlx5_shared_rxq_get(dev, conf->share_group,
891 if (rxq_ctrl != NULL &&
892 !mlx5_shared_rxq_match(rxq_ctrl, dev, idx, desc, socket,
898 res = mlx5_rx_queue_pre_setup(dev, idx, &desc, &rxq_ctrl);
902 rxq = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*rxq), 0,
905 DRV_LOG(ERR, "port %u unable to allocate rx queue index %u private data",
906 dev->data->port_id, idx);
912 (*priv->rxq_privs)[idx] = rxq;
913 if (rxq_ctrl != NULL) {
914 /* Join owner list. */
915 LIST_INSERT_HEAD(&rxq_ctrl->owners, rxq, owner_entry);
916 rxq->ctrl = rxq_ctrl;
918 rxq_ctrl = mlx5_rxq_new(dev, rxq, desc, socket, conf, rx_seg,
920 if (rxq_ctrl == NULL) {
921 DRV_LOG(ERR, "port %u unable to allocate rx queue index %u",
922 dev->data->port_id, idx);
924 (*priv->rxq_privs)[idx] = NULL;
929 mlx5_rxq_ref(dev, idx);
930 DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
931 dev->data->port_id, idx);
932 dev->data->rx_queues[idx] = &rxq_ctrl->rxq;
939 * Pointer to Ethernet device structure.
943 * Number of descriptors to configure in queue.
944 * @param hairpin_conf
945 * Hairpin configuration parameters.
948 * 0 on success, a negative errno value otherwise and rte_errno is set.
951 mlx5_rx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
953 const struct rte_eth_hairpin_conf *hairpin_conf)
955 struct mlx5_priv *priv = dev->data->dev_private;
956 struct mlx5_rxq_priv *rxq;
957 struct mlx5_rxq_ctrl *rxq_ctrl;
960 res = mlx5_rx_queue_pre_setup(dev, idx, &desc, NULL);
963 if (hairpin_conf->peer_count != 1) {
965 DRV_LOG(ERR, "port %u unable to setup Rx hairpin queue index %u"
966 " peer count is %u", dev->data->port_id,
967 idx, hairpin_conf->peer_count);
970 if (hairpin_conf->peers[0].port == dev->data->port_id) {
971 if (hairpin_conf->peers[0].queue >= priv->txqs_n) {
973 DRV_LOG(ERR, "port %u unable to setup Rx hairpin queue"
974 " index %u, Tx %u is larger than %u",
975 dev->data->port_id, idx,
976 hairpin_conf->peers[0].queue, priv->txqs_n);
980 if (hairpin_conf->manual_bind == 0 ||
981 hairpin_conf->tx_explicit == 0) {
983 DRV_LOG(ERR, "port %u unable to setup Rx hairpin queue"
984 " index %u peer port %u with attributes %u %u",
985 dev->data->port_id, idx,
986 hairpin_conf->peers[0].port,
987 hairpin_conf->manual_bind,
988 hairpin_conf->tx_explicit);
992 rxq = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*rxq), 0,
995 DRV_LOG(ERR, "port %u unable to allocate hairpin rx queue index %u private data",
996 dev->data->port_id, idx);
1002 (*priv->rxq_privs)[idx] = rxq;
1003 rxq_ctrl = mlx5_rxq_hairpin_new(dev, rxq, desc, hairpin_conf);
1005 DRV_LOG(ERR, "port %u unable to allocate hairpin queue index %u",
1006 dev->data->port_id, idx);
1008 (*priv->rxq_privs)[idx] = NULL;
1012 DRV_LOG(DEBUG, "port %u adding hairpin Rx queue %u to list",
1013 dev->data->port_id, idx);
1014 dev->data->rx_queues[idx] = &rxq_ctrl->rxq;
1019 * DPDK callback to release a RX queue.
1022 * Pointer to Ethernet device structure.
1024 * Receive queue index.
1027 mlx5_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1029 struct mlx5_rxq_data *rxq = dev->data->rx_queues[qid];
1033 if (!mlx5_rxq_releasable(dev, qid))
1034 rte_panic("port %u Rx queue %u is still used by a flow and"
1035 " cannot be removed\n", dev->data->port_id, qid);
1036 mlx5_rxq_release(dev, qid);
1040 * Allocate queue vector and fill epoll fd list for Rx interrupts.
1043 * Pointer to Ethernet device.
1046 * 0 on success, a negative errno value otherwise and rte_errno is set.
1049 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
1051 struct mlx5_priv *priv = dev->data->dev_private;
1053 unsigned int rxqs_n = priv->rxqs_n;
1054 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1055 unsigned int count = 0;
1056 struct rte_intr_handle *intr_handle = dev->intr_handle;
1058 if (!dev->data->dev_conf.intr_conf.rxq)
1060 mlx5_rx_intr_vec_disable(dev);
1061 if (rte_intr_vec_list_alloc(intr_handle, NULL, n)) {
1063 "port %u failed to allocate memory for interrupt"
1064 " vector, Rx interrupts will not be supported",
1065 dev->data->port_id);
1070 if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_EXT))
1073 for (i = 0; i != n; ++i) {
1074 /* This rxq obj must not be released in this function. */
1075 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, i);
1076 struct mlx5_rxq_obj *rxq_obj = rxq ? rxq->ctrl->obj : NULL;
1079 /* Skip queues that cannot request interrupts. */
1080 if (!rxq_obj || (!rxq_obj->ibv_channel &&
1081 !rxq_obj->devx_channel)) {
1082 /* Use invalid intr_vec[] index to disable entry. */
1083 if (rte_intr_vec_list_index_set(intr_handle, i,
1084 RTE_INTR_VEC_RXTX_OFFSET + RTE_MAX_RXTX_INTR_VEC_ID))
1088 mlx5_rxq_ref(dev, i);
1089 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
1091 "port %u too many Rx queues for interrupt"
1092 " vector size (%d), Rx interrupts cannot be"
1094 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
1095 mlx5_rx_intr_vec_disable(dev);
1099 rc = mlx5_os_set_nonblock_channel_fd(rxq_obj->fd);
1103 "port %u failed to make Rx interrupt file"
1104 " descriptor %d non-blocking for queue index"
1106 dev->data->port_id, rxq_obj->fd, i);
1107 mlx5_rx_intr_vec_disable(dev);
1111 if (rte_intr_vec_list_index_set(intr_handle, i,
1112 RTE_INTR_VEC_RXTX_OFFSET + count))
1114 if (rte_intr_efds_index_set(intr_handle, count,
1120 mlx5_rx_intr_vec_disable(dev);
1121 else if (rte_intr_nb_efd_set(intr_handle, count))
1127 * Clean up Rx interrupts handler.
1130 * Pointer to Ethernet device.
1133 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
1135 struct mlx5_priv *priv = dev->data->dev_private;
1136 struct rte_intr_handle *intr_handle = dev->intr_handle;
1138 unsigned int rxqs_n = priv->rxqs_n;
1139 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
1141 if (!dev->data->dev_conf.intr_conf.rxq)
1143 if (rte_intr_vec_list_index_get(intr_handle, 0) < 0)
1145 for (i = 0; i != n; ++i) {
1146 if (rte_intr_vec_list_index_get(intr_handle, i) ==
1147 RTE_INTR_VEC_RXTX_OFFSET + RTE_MAX_RXTX_INTR_VEC_ID)
1150 * Need to access directly the queue to release the reference
1151 * kept in mlx5_rx_intr_vec_enable().
1153 mlx5_rxq_deref(dev, i);
1156 rte_intr_free_epoll_fd(intr_handle);
1158 rte_intr_vec_list_free(intr_handle);
1160 rte_intr_nb_efd_set(intr_handle, 0);
1164 * MLX5 CQ notification .
1167 * Pointer to receive queue structure.
1169 * Sequence number per receive queue .
1172 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
1175 uint32_t doorbell_hi;
1178 sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
1179 doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
1180 doorbell = (uint64_t)doorbell_hi << 32;
1181 doorbell |= rxq->cqn;
1182 mlx5_doorbell_ring(&rxq->uar_data, rte_cpu_to_be_64(doorbell),
1183 doorbell_hi, &rxq->cq_db[MLX5_CQ_ARM_DB], 0);
1187 * DPDK callback for Rx queue interrupt enable.
1190 * Pointer to Ethernet device structure.
1191 * @param rx_queue_id
1195 * 0 on success, a negative errno value otherwise and rte_errno is set.
1198 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1200 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, rx_queue_id);
1203 if (rxq->ctrl->irq) {
1204 if (!rxq->ctrl->obj)
1206 mlx5_arm_cq(&rxq->ctrl->rxq, rxq->ctrl->rxq.cq_arm_sn);
1215 * DPDK callback for Rx queue interrupt disable.
1218 * Pointer to Ethernet device structure.
1219 * @param rx_queue_id
1223 * 0 on success, a negative errno value otherwise and rte_errno is set.
1226 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1228 struct mlx5_priv *priv = dev->data->dev_private;
1229 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, rx_queue_id);
1236 if (!rxq->ctrl->obj)
1238 if (rxq->ctrl->irq) {
1239 ret = priv->obj_ops.rxq_event_get(rxq->ctrl->obj);
1242 rxq->ctrl->rxq.cq_arm_sn++;
1247 * The ret variable may be EAGAIN which means the get_event function was
1248 * called before receiving one.
1254 if (rte_errno != EAGAIN)
1255 DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
1256 dev->data->port_id, rx_queue_id);
1261 * Verify the Rx queue objects list is empty
1264 * Pointer to Ethernet device.
1267 * The number of objects not released.
1270 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1272 struct mlx5_priv *priv = dev->data->dev_private;
1274 struct mlx5_rxq_obj *rxq_obj;
1276 LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1277 if (rxq_obj->rxq_ctrl == NULL)
1279 if (rxq_obj->rxq_ctrl->rxq.shared &&
1280 !LIST_EMPTY(&rxq_obj->rxq_ctrl->owners))
1282 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1283 dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1290 * Callback function to initialize mbufs for Multi-Packet RQ.
1293 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1294 void *_m, unsigned int i __rte_unused)
1296 struct mlx5_mprq_buf *buf = _m;
1297 struct rte_mbuf_ext_shared_info *shinfo;
1298 unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
1301 memset(_m, 0, sizeof(*buf));
1303 __atomic_store_n(&buf->refcnt, 1, __ATOMIC_RELAXED);
1304 for (j = 0; j != strd_n; ++j) {
1305 shinfo = &buf->shinfos[j];
1306 shinfo->free_cb = mlx5_mprq_buf_free_cb;
1307 shinfo->fcb_opaque = buf;
1312 * Free mempool of Multi-Packet RQ.
1315 * Pointer to Ethernet device.
1318 * 0 on success, negative errno value on failure.
1321 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
1323 struct mlx5_priv *priv = dev->data->dev_private;
1324 struct rte_mempool *mp = priv->mprq_mp;
1329 DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
1330 dev->data->port_id, mp->name);
1332 * If a buffer in the pool has been externally attached to a mbuf and it
1333 * is still in use by application, destroying the Rx queue can spoil
1334 * the packet. It is unlikely to happen but if application dynamically
1335 * creates and destroys with holding Rx packets, this can happen.
1337 * TODO: It is unavoidable for now because the mempool for Multi-Packet
1338 * RQ isn't provided by application but managed by PMD.
1340 if (!rte_mempool_full(mp)) {
1342 "port %u mempool for Multi-Packet RQ is still in use",
1343 dev->data->port_id);
1347 rte_mempool_free(mp);
1348 /* Unset mempool for each Rx queue. */
1349 for (i = 0; i != priv->rxqs_n; ++i) {
1350 struct mlx5_rxq_data *rxq = mlx5_rxq_data_get(dev, i);
1354 rxq->mprq_mp = NULL;
1356 priv->mprq_mp = NULL;
1361 * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
1362 * mempool. If already allocated, reuse it if there're enough elements.
1363 * Otherwise, resize it.
1366 * Pointer to Ethernet device.
1369 * 0 on success, negative errno value on failure.
1372 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
1374 struct mlx5_priv *priv = dev->data->dev_private;
1375 struct rte_mempool *mp = priv->mprq_mp;
1376 char name[RTE_MEMPOOL_NAMESIZE];
1377 unsigned int desc = 0;
1378 unsigned int buf_len;
1379 unsigned int obj_num;
1380 unsigned int obj_size;
1381 unsigned int strd_num_n = 0;
1382 unsigned int strd_sz_n = 0;
1384 unsigned int n_ibv = 0;
1387 if (!mlx5_mprq_enabled(dev))
1389 /* Count the total number of descriptors configured. */
1390 for (i = 0; i != priv->rxqs_n; ++i) {
1391 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_ctrl_get(dev, i);
1392 struct mlx5_rxq_data *rxq;
1394 if (rxq_ctrl == NULL ||
1395 rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1397 rxq = &rxq_ctrl->rxq;
1399 desc += 1 << rxq->elts_n;
1400 /* Get the max number of strides. */
1401 if (strd_num_n < rxq->strd_num_n)
1402 strd_num_n = rxq->strd_num_n;
1403 /* Get the max size of a stride. */
1404 if (strd_sz_n < rxq->strd_sz_n)
1405 strd_sz_n = rxq->strd_sz_n;
1407 MLX5_ASSERT(strd_num_n && strd_sz_n);
1408 buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
1409 obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
1410 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
1412 * Received packets can be either memcpy'd or externally referenced. In
1413 * case that the packet is attached to an mbuf as an external buffer, as
1414 * it isn't possible to predict how the buffers will be queued by
1415 * application, there's no option to exactly pre-allocate needed buffers
1416 * in advance but to speculatively prepares enough buffers.
1418 * In the data path, if this Mempool is depleted, PMD will try to memcpy
1419 * received packets to buffers provided by application (rxq->mp) until
1420 * this Mempool gets available again.
1423 obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * n_ibv;
1425 * rte_mempool_create_empty() has sanity check to refuse large cache
1426 * size compared to the number of elements.
1427 * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
1428 * constant number 2 instead.
1430 obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
1431 /* Check a mempool is already allocated and if it can be resued. */
1432 if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
1433 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
1434 dev->data->port_id, mp->name);
1437 } else if (mp != NULL) {
1438 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
1439 dev->data->port_id, mp->name);
1441 * If failed to free, which means it may be still in use, no way
1442 * but to keep using the existing one. On buffer underrun,
1443 * packets will be memcpy'd instead of external buffer
1446 if (mlx5_mprq_free_mp(dev)) {
1447 if (mp->elt_size >= obj_size)
1453 snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
1454 mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
1455 0, NULL, NULL, mlx5_mprq_buf_init,
1456 (void *)((uintptr_t)1 << strd_num_n),
1457 dev->device->numa_node, 0);
1460 "port %u failed to allocate a mempool for"
1461 " Multi-Packet RQ, count=%u, size=%u",
1462 dev->data->port_id, obj_num, obj_size);
1466 ret = mlx5_mr_mempool_register(priv->sh->cdev, mp, false);
1467 if (ret < 0 && rte_errno != EEXIST) {
1469 DRV_LOG(ERR, "port %u failed to register a mempool for Multi-Packet RQ",
1470 dev->data->port_id);
1471 rte_mempool_free(mp);
1477 /* Set mempool for each Rx queue. */
1478 for (i = 0; i != priv->rxqs_n; ++i) {
1479 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_ctrl_get(dev, i);
1481 if (rxq_ctrl == NULL ||
1482 rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1484 rxq_ctrl->rxq.mprq_mp = mp;
1486 DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
1487 dev->data->port_id);
1491 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
1492 sizeof(struct rte_vlan_hdr) * 2 + \
1493 sizeof(struct rte_ipv6_hdr)))
1494 #define MAX_TCP_OPTION_SIZE 40u
1495 #define MLX5_MAX_LRO_HEADER_FIX ((unsigned int)(MLX5_MAX_TCP_HDR_OFFSET + \
1496 sizeof(struct rte_tcp_hdr) + \
1497 MAX_TCP_OPTION_SIZE))
1500 * Adjust the maximum LRO massage size.
1503 * Pointer to Ethernet device.
1506 * @param max_lro_size
1507 * The maximum size for LRO packet.
1510 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint16_t idx,
1511 uint32_t max_lro_size)
1513 struct mlx5_priv *priv = dev->data->dev_private;
1515 if (priv->config.hca_attr.lro_max_msg_sz_mode ==
1516 MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
1517 MLX5_MAX_TCP_HDR_OFFSET)
1518 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
1519 max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
1520 MLX5_ASSERT(max_lro_size >= MLX5_LRO_SEG_CHUNK_SIZE);
1521 max_lro_size /= MLX5_LRO_SEG_CHUNK_SIZE;
1522 if (priv->max_lro_msg_size)
1523 priv->max_lro_msg_size =
1524 RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
1526 priv->max_lro_msg_size = max_lro_size;
1528 "port %u Rx Queue %u max LRO message size adjusted to %u bytes",
1529 dev->data->port_id, idx,
1530 priv->max_lro_msg_size * MLX5_LRO_SEG_CHUNK_SIZE);
1534 * Create a DPDK Rx queue.
1537 * Pointer to Ethernet device.
1539 * RX queue private data.
1541 * Number of descriptors to configure in queue.
1543 * NUMA socket on which memory must be allocated.
1546 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1548 struct mlx5_rxq_ctrl *
1549 mlx5_rxq_new(struct rte_eth_dev *dev, struct mlx5_rxq_priv *rxq,
1551 unsigned int socket, const struct rte_eth_rxconf *conf,
1552 const struct rte_eth_rxseg_split *rx_seg, uint16_t n_seg)
1554 uint16_t idx = rxq->idx;
1555 struct mlx5_priv *priv = dev->data->dev_private;
1556 struct mlx5_rxq_ctrl *tmpl;
1557 unsigned int mb_len = rte_pktmbuf_data_room_size(rx_seg[0].mp);
1558 struct mlx5_dev_config *config = &priv->config;
1559 uint64_t offloads = conf->offloads |
1560 dev->data->dev_conf.rxmode.offloads;
1561 unsigned int lro_on_queue = !!(offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO);
1562 unsigned int max_rx_pktlen = lro_on_queue ?
1563 dev->data->dev_conf.rxmode.max_lro_pkt_size :
1564 dev->data->mtu + (unsigned int)RTE_ETHER_HDR_LEN +
1566 unsigned int non_scatter_min_mbuf_size = max_rx_pktlen +
1567 RTE_PKTMBUF_HEADROOM;
1568 unsigned int max_lro_size = 0;
1569 unsigned int first_mb_free_size = mb_len - RTE_PKTMBUF_HEADROOM;
1570 const int mprq_en = mlx5_check_mprq_support(dev) > 0 && n_seg == 1 &&
1571 !rx_seg[0].offset && !rx_seg[0].length;
1572 unsigned int mprq_stride_nums = config->mprq.stride_num_n ?
1573 config->mprq.stride_num_n : MLX5_MPRQ_STRIDE_NUM_N;
1574 unsigned int mprq_stride_size = non_scatter_min_mbuf_size <=
1575 (1U << config->mprq.max_stride_size_n) ?
1576 log2above(non_scatter_min_mbuf_size) : MLX5_MPRQ_STRIDE_SIZE_N;
1577 unsigned int mprq_stride_cap = (config->mprq.stride_num_n ?
1578 (1U << config->mprq.stride_num_n) : (1U << mprq_stride_nums)) *
1579 (config->mprq.stride_size_n ?
1580 (1U << config->mprq.stride_size_n) : (1U << mprq_stride_size));
1582 * Always allocate extra slots, even if eventually
1583 * the vector Rx will not be used.
1585 uint16_t desc_n = desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
1586 const struct rte_eth_rxseg_split *qs_seg = rx_seg;
1587 unsigned int tail_len;
1589 tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1590 sizeof(*tmpl) + desc_n * sizeof(struct rte_mbuf *) +
1592 (desc >> mprq_stride_nums) * sizeof(struct mlx5_mprq_buf *),
1598 LIST_INIT(&tmpl->owners);
1599 if (conf->share_group > 0) {
1600 tmpl->rxq.shared = 1;
1601 tmpl->share_group = conf->share_group;
1602 tmpl->share_qid = conf->share_qid;
1603 LIST_INSERT_HEAD(&priv->sh->shared_rxqs, tmpl, share_entry);
1606 LIST_INSERT_HEAD(&tmpl->owners, rxq, owner_entry);
1607 MLX5_ASSERT(n_seg && n_seg <= MLX5_MAX_RXQ_NSEG);
1609 * Save the original segment configuration in the shared queue
1610 * descriptor for the later check on the sibling queue creation.
1612 tmpl->rxseg_n = n_seg;
1613 rte_memcpy(tmpl->rxseg, qs_seg,
1614 sizeof(struct rte_eth_rxseg_split) * n_seg);
1616 * Build the array of actual buffer offsets and lengths.
1617 * Pad with the buffers from the last memory pool if
1618 * needed to handle max size packets, replace zero length
1619 * with the buffer length from the pool.
1621 tail_len = max_rx_pktlen;
1623 struct mlx5_eth_rxseg *hw_seg =
1624 &tmpl->rxq.rxseg[tmpl->rxq.rxseg_n];
1625 uint32_t buf_len, offset, seg_len;
1628 * For the buffers beyond descriptions offset is zero,
1629 * the first buffer contains head room.
1631 buf_len = rte_pktmbuf_data_room_size(qs_seg->mp);
1632 offset = (tmpl->rxq.rxseg_n >= n_seg ? 0 : qs_seg->offset) +
1633 (tmpl->rxq.rxseg_n ? 0 : RTE_PKTMBUF_HEADROOM);
1635 * For the buffers beyond descriptions the length is
1636 * pool buffer length, zero lengths are replaced with
1637 * pool buffer length either.
1639 seg_len = tmpl->rxq.rxseg_n >= n_seg ? buf_len :
1643 /* Check is done in long int, now overflows. */
1644 if (buf_len < seg_len + offset) {
1645 DRV_LOG(ERR, "port %u Rx queue %u: Split offset/length "
1646 "%u/%u can't be satisfied",
1647 dev->data->port_id, idx,
1648 qs_seg->length, qs_seg->offset);
1652 if (seg_len > tail_len)
1653 seg_len = buf_len - offset;
1654 if (++tmpl->rxq.rxseg_n > MLX5_MAX_RXQ_NSEG) {
1656 "port %u too many SGEs (%u) needed to handle"
1657 " requested maximum packet size %u, the maximum"
1658 " supported are %u", dev->data->port_id,
1659 tmpl->rxq.rxseg_n, max_rx_pktlen,
1661 rte_errno = ENOTSUP;
1664 /* Build the actual scattering element in the queue object. */
1665 hw_seg->mp = qs_seg->mp;
1666 MLX5_ASSERT(offset <= UINT16_MAX);
1667 MLX5_ASSERT(seg_len <= UINT16_MAX);
1668 hw_seg->offset = (uint16_t)offset;
1669 hw_seg->length = (uint16_t)seg_len;
1671 * Advance the segment descriptor, the padding is the based
1672 * on the attributes of the last descriptor.
1674 if (tmpl->rxq.rxseg_n < n_seg)
1676 tail_len -= RTE_MIN(tail_len, seg_len);
1677 } while (tail_len || !rte_is_power_of_2(tmpl->rxq.rxseg_n));
1678 MLX5_ASSERT(tmpl->rxq.rxseg_n &&
1679 tmpl->rxq.rxseg_n <= MLX5_MAX_RXQ_NSEG);
1680 if (tmpl->rxq.rxseg_n > 1 && !(offloads & RTE_ETH_RX_OFFLOAD_SCATTER)) {
1681 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
1682 " configured and no enough mbuf space(%u) to contain "
1683 "the maximum RX packet length(%u) with head-room(%u)",
1684 dev->data->port_id, idx, mb_len, max_rx_pktlen,
1685 RTE_PKTMBUF_HEADROOM);
1689 tmpl->type = MLX5_RXQ_TYPE_STANDARD;
1690 if (mlx5_mr_ctrl_init(&tmpl->rxq.mr_ctrl,
1691 &priv->sh->cdev->mr_scache.dev_gen, socket)) {
1692 /* rte_errno is already set. */
1695 tmpl->socket = socket;
1696 if (dev->data->dev_conf.intr_conf.rxq)
1699 * This Rx queue can be configured as a Multi-Packet RQ if all of the
1700 * following conditions are met:
1701 * - MPRQ is enabled.
1702 * - The number of descs is more than the number of strides.
1703 * - max_rx_pktlen plus overhead is less than the max size
1704 * of a stride or mprq_stride_size is specified by a user.
1705 * Need to make sure that there are enough strides to encap
1706 * the maximum packet size in case mprq_stride_size is set.
1707 * Otherwise, enable Rx scatter if necessary.
1709 if (mprq_en && desc > (1U << mprq_stride_nums) &&
1710 (non_scatter_min_mbuf_size <=
1711 (1U << config->mprq.max_stride_size_n) ||
1712 (config->mprq.stride_size_n &&
1713 non_scatter_min_mbuf_size <= mprq_stride_cap))) {
1714 /* TODO: Rx scatter isn't supported yet. */
1715 tmpl->rxq.sges_n = 0;
1716 /* Trim the number of descs needed. */
1717 desc >>= mprq_stride_nums;
1718 tmpl->rxq.strd_num_n = config->mprq.stride_num_n ?
1719 config->mprq.stride_num_n : mprq_stride_nums;
1720 tmpl->rxq.strd_sz_n = config->mprq.stride_size_n ?
1721 config->mprq.stride_size_n : mprq_stride_size;
1722 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
1723 tmpl->rxq.strd_scatter_en =
1724 !!(offloads & RTE_ETH_RX_OFFLOAD_SCATTER);
1725 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(first_mb_free_size,
1726 config->mprq.max_memcpy_len);
1727 max_lro_size = RTE_MIN(max_rx_pktlen,
1728 (1u << tmpl->rxq.strd_num_n) *
1729 (1u << tmpl->rxq.strd_sz_n));
1731 "port %u Rx queue %u: Multi-Packet RQ is enabled"
1732 " strd_num_n = %u, strd_sz_n = %u",
1733 dev->data->port_id, idx,
1734 tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
1735 } else if (tmpl->rxq.rxseg_n == 1) {
1736 MLX5_ASSERT(max_rx_pktlen <= first_mb_free_size);
1737 tmpl->rxq.sges_n = 0;
1738 max_lro_size = max_rx_pktlen;
1739 } else if (offloads & RTE_ETH_RX_OFFLOAD_SCATTER) {
1740 unsigned int sges_n;
1742 if (lro_on_queue && first_mb_free_size <
1743 MLX5_MAX_LRO_HEADER_FIX) {
1744 DRV_LOG(ERR, "Not enough space in the first segment(%u)"
1745 " to include the max header size(%u) for LRO",
1746 first_mb_free_size, MLX5_MAX_LRO_HEADER_FIX);
1747 rte_errno = ENOTSUP;
1751 * Determine the number of SGEs needed for a full packet
1752 * and round it to the next power of two.
1754 sges_n = log2above(tmpl->rxq.rxseg_n);
1755 if (sges_n > MLX5_MAX_LOG_RQ_SEGS) {
1757 "port %u too many SGEs (%u) needed to handle"
1758 " requested maximum packet size %u, the maximum"
1759 " supported are %u", dev->data->port_id,
1760 1 << sges_n, max_rx_pktlen,
1761 1u << MLX5_MAX_LOG_RQ_SEGS);
1762 rte_errno = ENOTSUP;
1765 tmpl->rxq.sges_n = sges_n;
1766 max_lro_size = max_rx_pktlen;
1768 if (config->mprq.enabled && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
1770 "port %u MPRQ is requested but cannot be enabled\n"
1771 " (requested: pkt_sz = %u, desc_num = %u,"
1772 " rxq_num = %u, stride_sz = %u, stride_num = %u\n"
1773 " supported: min_rxqs_num = %u,"
1774 " min_stride_sz = %u, max_stride_sz = %u).",
1775 dev->data->port_id, non_scatter_min_mbuf_size,
1777 config->mprq.stride_size_n ?
1778 (1U << config->mprq.stride_size_n) :
1779 (1U << mprq_stride_size),
1780 config->mprq.stride_num_n ?
1781 (1U << config->mprq.stride_num_n) :
1782 (1U << mprq_stride_nums),
1783 config->mprq.min_rxqs_num,
1784 (1U << config->mprq.min_stride_size_n),
1785 (1U << config->mprq.max_stride_size_n));
1786 DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
1787 dev->data->port_id, 1 << tmpl->rxq.sges_n);
1788 if (desc % (1 << tmpl->rxq.sges_n)) {
1790 "port %u number of Rx queue descriptors (%u) is not a"
1791 " multiple of SGEs per packet (%u)",
1794 1 << tmpl->rxq.sges_n);
1798 mlx5_max_lro_msg_size_adjust(dev, idx, max_lro_size);
1799 /* Toggle RX checksum offload if hardware supports it. */
1800 tmpl->rxq.csum = !!(offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM);
1801 /* Configure Rx timestamp. */
1802 tmpl->rxq.hw_timestamp = !!(offloads & RTE_ETH_RX_OFFLOAD_TIMESTAMP);
1803 tmpl->rxq.timestamp_rx_flag = 0;
1804 if (tmpl->rxq.hw_timestamp && rte_mbuf_dyn_rx_timestamp_register(
1805 &tmpl->rxq.timestamp_offset,
1806 &tmpl->rxq.timestamp_rx_flag) != 0) {
1807 DRV_LOG(ERR, "Cannot register Rx timestamp field/flag");
1810 /* Configure VLAN stripping. */
1811 tmpl->rxq.vlan_strip = !!(offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
1812 /* By default, FCS (CRC) is stripped by hardware. */
1813 tmpl->rxq.crc_present = 0;
1814 tmpl->rxq.lro = lro_on_queue;
1815 if (offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC) {
1816 if (config->hw_fcs_strip) {
1818 * RQs used for LRO-enabled TIRs should not be
1819 * configured to scatter the FCS.
1823 "port %u CRC stripping has been "
1824 "disabled but will still be performed "
1825 "by hardware, because LRO is enabled",
1826 dev->data->port_id);
1828 tmpl->rxq.crc_present = 1;
1831 "port %u CRC stripping has been disabled but will"
1832 " still be performed by hardware, make sure MLNX_OFED"
1833 " and firmware are up to date",
1834 dev->data->port_id);
1838 "port %u CRC stripping is %s, %u bytes will be subtracted from"
1839 " incoming frames to hide it",
1841 tmpl->rxq.crc_present ? "disabled" : "enabled",
1842 tmpl->rxq.crc_present << 2);
1844 tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
1845 (!!(dev->data->dev_conf.rxmode.mq_mode & RTE_ETH_MQ_RX_RSS));
1846 tmpl->rxq.port_id = dev->data->port_id;
1847 tmpl->sh = priv->sh;
1848 tmpl->rxq.mp = rx_seg[0].mp;
1849 tmpl->rxq.elts_n = log2above(desc);
1850 tmpl->rxq.rq_repl_thresh =
1851 MLX5_VPMD_RXQ_RPLNSH_THRESH(desc_n);
1853 (struct rte_mbuf *(*)[desc_n])(tmpl + 1);
1854 tmpl->rxq.mprq_bufs =
1855 (struct mlx5_mprq_buf *(*)[desc])(*tmpl->rxq.elts + desc_n);
1856 tmpl->rxq.idx = idx;
1857 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1860 mlx5_mr_btree_free(&tmpl->rxq.mr_ctrl.cache_bh);
1866 * Create a DPDK Rx hairpin queue.
1869 * Pointer to Ethernet device.
1873 * Number of descriptors to configure in queue.
1874 * @param hairpin_conf
1875 * The hairpin binding configuration.
1878 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1880 struct mlx5_rxq_ctrl *
1881 mlx5_rxq_hairpin_new(struct rte_eth_dev *dev, struct mlx5_rxq_priv *rxq,
1883 const struct rte_eth_hairpin_conf *hairpin_conf)
1885 uint16_t idx = rxq->idx;
1886 struct mlx5_priv *priv = dev->data->dev_private;
1887 struct mlx5_rxq_ctrl *tmpl;
1889 tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1895 LIST_INIT(&tmpl->owners);
1897 LIST_INSERT_HEAD(&tmpl->owners, rxq, owner_entry);
1898 tmpl->type = MLX5_RXQ_TYPE_HAIRPIN;
1899 tmpl->socket = SOCKET_ID_ANY;
1900 tmpl->rxq.rss_hash = 0;
1901 tmpl->rxq.port_id = dev->data->port_id;
1902 tmpl->sh = priv->sh;
1903 tmpl->rxq.mp = NULL;
1904 tmpl->rxq.elts_n = log2above(desc);
1905 tmpl->rxq.elts = NULL;
1906 tmpl->rxq.mr_ctrl.cache_bh = (struct mlx5_mr_btree) { 0 };
1907 tmpl->rxq.idx = idx;
1908 rxq->hairpin_conf = *hairpin_conf;
1909 mlx5_rxq_ref(dev, idx);
1910 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1915 * Increase Rx queue reference count.
1918 * Pointer to Ethernet device.
1923 * A pointer to the queue if it exists, NULL otherwise.
1925 struct mlx5_rxq_priv *
1926 mlx5_rxq_ref(struct rte_eth_dev *dev, uint16_t idx)
1928 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
1931 __atomic_fetch_add(&rxq->refcnt, 1, __ATOMIC_RELAXED);
1936 * Dereference a Rx queue.
1939 * Pointer to Ethernet device.
1944 * Updated reference count.
1947 mlx5_rxq_deref(struct rte_eth_dev *dev, uint16_t idx)
1949 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
1953 return __atomic_sub_fetch(&rxq->refcnt, 1, __ATOMIC_RELAXED);
1960 * Pointer to Ethernet device.
1965 * A pointer to the queue if it exists, NULL otherwise.
1967 struct mlx5_rxq_priv *
1968 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
1970 struct mlx5_priv *priv = dev->data->dev_private;
1972 MLX5_ASSERT(priv->rxq_privs != NULL);
1973 return (*priv->rxq_privs)[idx];
1977 * Get Rx queue shareable control.
1980 * Pointer to Ethernet device.
1985 * A pointer to the queue control if it exists, NULL otherwise.
1987 struct mlx5_rxq_ctrl *
1988 mlx5_rxq_ctrl_get(struct rte_eth_dev *dev, uint16_t idx)
1990 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
1992 return rxq == NULL ? NULL : rxq->ctrl;
1996 * Get Rx queue shareable data.
1999 * Pointer to Ethernet device.
2004 * A pointer to the queue data if it exists, NULL otherwise.
2006 struct mlx5_rxq_data *
2007 mlx5_rxq_data_get(struct rte_eth_dev *dev, uint16_t idx)
2009 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
2011 return rxq == NULL ? NULL : &rxq->ctrl->rxq;
2015 * Release a Rx queue.
2018 * Pointer to Ethernet device.
2023 * 1 while a reference on it exists, 0 when freed.
2026 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
2028 struct mlx5_priv *priv = dev->data->dev_private;
2029 struct mlx5_rxq_priv *rxq;
2030 struct mlx5_rxq_ctrl *rxq_ctrl;
2033 if (priv->rxq_privs == NULL)
2035 rxq = mlx5_rxq_get(dev, idx);
2036 if (rxq == NULL || rxq->refcnt == 0)
2038 rxq_ctrl = rxq->ctrl;
2039 refcnt = mlx5_rxq_deref(dev, idx);
2042 } else if (refcnt == 1) { /* RxQ stopped. */
2043 priv->obj_ops.rxq_obj_release(rxq);
2044 if (!rxq_ctrl->started && rxq_ctrl->obj != NULL) {
2045 LIST_REMOVE(rxq_ctrl->obj, next);
2046 mlx5_free(rxq_ctrl->obj);
2047 rxq_ctrl->obj = NULL;
2049 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD) {
2050 if (!rxq_ctrl->started)
2051 rxq_free_elts(rxq_ctrl);
2052 dev->data->rx_queue_state[idx] =
2053 RTE_ETH_QUEUE_STATE_STOPPED;
2055 } else { /* Refcnt zero, closing device. */
2056 LIST_REMOVE(rxq, owner_entry);
2057 if (LIST_EMPTY(&rxq_ctrl->owners)) {
2058 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD)
2060 (&rxq_ctrl->rxq.mr_ctrl.cache_bh);
2061 if (rxq_ctrl->rxq.shared)
2062 LIST_REMOVE(rxq_ctrl, share_entry);
2063 LIST_REMOVE(rxq_ctrl, next);
2064 mlx5_free(rxq_ctrl);
2066 dev->data->rx_queues[idx] = NULL;
2068 (*priv->rxq_privs)[idx] = NULL;
2074 * Verify the Rx Queue list is empty
2077 * Pointer to Ethernet device.
2080 * The number of object not released.
2083 mlx5_rxq_verify(struct rte_eth_dev *dev)
2085 struct mlx5_priv *priv = dev->data->dev_private;
2086 struct mlx5_rxq_ctrl *rxq_ctrl;
2089 LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
2090 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
2091 dev->data->port_id, rxq_ctrl->rxq.idx);
2098 * Get a Rx queue type.
2101 * Pointer to Ethernet device.
2106 * The Rx queue type.
2109 mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx)
2111 struct mlx5_priv *priv = dev->data->dev_private;
2112 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_ctrl_get(dev, idx);
2114 if (idx < priv->rxqs_n && rxq_ctrl != NULL)
2115 return rxq_ctrl->type;
2116 return MLX5_RXQ_TYPE_UNDEFINED;
2120 * Get a Rx hairpin queue configuration.
2123 * Pointer to Ethernet device.
2128 * Pointer to the configuration if a hairpin RX queue, otherwise NULL.
2130 const struct rte_eth_hairpin_conf *
2131 mlx5_rxq_get_hairpin_conf(struct rte_eth_dev *dev, uint16_t idx)
2133 struct mlx5_priv *priv = dev->data->dev_private;
2134 struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev, idx);
2136 if (idx < priv->rxqs_n && rxq != NULL) {
2137 if (rxq->ctrl->type == MLX5_RXQ_TYPE_HAIRPIN)
2138 return &rxq->hairpin_conf;
2144 * Match queues listed in arguments to queues contained in indirection table
2148 * Pointer to indirection table to match.
2150 * Queues to match to ques in indirection table.
2152 * Number of queues in the array.
2155 * 1 if all queues in indirection table match 0 othrwise.
2158 mlx5_ind_table_obj_match_queues(const struct mlx5_ind_table_obj *ind_tbl,
2159 const uint16_t *queues, uint32_t queues_n)
2161 return (ind_tbl->queues_n == queues_n) &&
2162 (!memcmp(ind_tbl->queues, queues,
2163 ind_tbl->queues_n * sizeof(ind_tbl->queues[0])));
2167 * Get an indirection table.
2170 * Pointer to Ethernet device.
2172 * Queues entering in the indirection table.
2174 * Number of queues in the array.
2177 * An indirection table if found.
2179 struct mlx5_ind_table_obj *
2180 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
2183 struct mlx5_priv *priv = dev->data->dev_private;
2184 struct mlx5_ind_table_obj *ind_tbl;
2186 rte_rwlock_read_lock(&priv->ind_tbls_lock);
2187 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2188 if ((ind_tbl->queues_n == queues_n) &&
2189 (memcmp(ind_tbl->queues, queues,
2190 ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
2192 __atomic_fetch_add(&ind_tbl->refcnt, 1,
2197 rte_rwlock_read_unlock(&priv->ind_tbls_lock);
2202 * Release an indirection table.
2205 * Pointer to Ethernet device.
2207 * Indirection table to release.
2209 * Indirection table for Standalone queue.
2211 * If true, then dereference RX queues related to indirection table.
2212 * Otherwise, no additional action will be taken.
2215 * 1 while a reference on it exists, 0 when freed.
2218 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
2219 struct mlx5_ind_table_obj *ind_tbl,
2223 struct mlx5_priv *priv = dev->data->dev_private;
2224 unsigned int i, ret;
2226 rte_rwlock_write_lock(&priv->ind_tbls_lock);
2227 ret = __atomic_sub_fetch(&ind_tbl->refcnt, 1, __ATOMIC_RELAXED);
2228 if (!ret && !standalone)
2229 LIST_REMOVE(ind_tbl, next);
2230 rte_rwlock_write_unlock(&priv->ind_tbls_lock);
2233 priv->obj_ops.ind_table_destroy(ind_tbl);
2235 for (i = 0; i != ind_tbl->queues_n; ++i)
2236 claim_nonzero(mlx5_rxq_deref(dev, ind_tbl->queues[i]));
2243 * Verify the Rx Queue list is empty
2246 * Pointer to Ethernet device.
2249 * The number of object not released.
2252 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
2254 struct mlx5_priv *priv = dev->data->dev_private;
2255 struct mlx5_ind_table_obj *ind_tbl;
2258 rte_rwlock_read_lock(&priv->ind_tbls_lock);
2259 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
2261 "port %u indirection table obj %p still referenced",
2262 dev->data->port_id, (void *)ind_tbl);
2265 rte_rwlock_read_unlock(&priv->ind_tbls_lock);
2270 * Setup an indirection table structure fields.
2273 * Pointer to Ethernet device.
2275 * Indirection table to modify.
2277 * Whether to increment RxQ reference counters.
2280 * 0 on success, a negative errno value otherwise and rte_errno is set.
2283 mlx5_ind_table_obj_setup(struct rte_eth_dev *dev,
2284 struct mlx5_ind_table_obj *ind_tbl,
2287 struct mlx5_priv *priv = dev->data->dev_private;
2288 uint32_t queues_n = ind_tbl->queues_n;
2289 uint16_t *queues = ind_tbl->queues;
2290 unsigned int i = 0, j;
2292 const unsigned int n = rte_is_power_of_2(queues_n) ?
2293 log2above(queues_n) :
2294 log2above(priv->config.ind_table_max_size);
2297 for (i = 0; i != queues_n; ++i) {
2298 if (mlx5_rxq_ref(dev, queues[i]) == NULL) {
2303 ret = priv->obj_ops.ind_table_new(dev, n, ind_tbl);
2306 __atomic_fetch_add(&ind_tbl->refcnt, 1, __ATOMIC_RELAXED);
2311 for (j = 0; j < i; j++)
2312 mlx5_rxq_deref(dev, queues[j]);
2315 DRV_LOG(DEBUG, "Port %u cannot setup indirection table.",
2316 dev->data->port_id);
2321 * Create an indirection table.
2324 * Pointer to Ethernet device.
2326 * Queues entering in the indirection table.
2328 * Number of queues in the array.
2330 * Indirection table for Standalone queue.
2332 * Whether to increment RxQ reference counters.
2335 * The Verbs/DevX object initialized, NULL otherwise and rte_errno is set.
2337 static struct mlx5_ind_table_obj *
2338 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
2339 uint32_t queues_n, bool standalone, bool ref_qs)
2341 struct mlx5_priv *priv = dev->data->dev_private;
2342 struct mlx5_ind_table_obj *ind_tbl;
2345 ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl) +
2346 queues_n * sizeof(uint16_t), 0, SOCKET_ID_ANY);
2351 ind_tbl->queues_n = queues_n;
2352 ind_tbl->queues = (uint16_t *)(ind_tbl + 1);
2353 memcpy(ind_tbl->queues, queues, queues_n * sizeof(*queues));
2354 ret = mlx5_ind_table_obj_setup(dev, ind_tbl, ref_qs);
2360 rte_rwlock_write_lock(&priv->ind_tbls_lock);
2361 LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
2362 rte_rwlock_write_unlock(&priv->ind_tbls_lock);
2368 mlx5_ind_table_obj_check_standalone(struct rte_eth_dev *dev __rte_unused,
2369 struct mlx5_ind_table_obj *ind_tbl)
2373 refcnt = __atomic_load_n(&ind_tbl->refcnt, __ATOMIC_RELAXED);
2377 * Modification of indirection tables having more than 1
2378 * reference is unsupported.
2381 "Port %u cannot modify indirection table %p (refcnt %u > 1).",
2382 dev->data->port_id, (void *)ind_tbl, refcnt);
2388 * Modify an indirection table.
2391 * Pointer to Ethernet device.
2393 * Indirection table to modify.
2395 * Queues replacement for the indirection table.
2397 * Number of queues in the array.
2399 * Indirection table for Standalone queue.
2401 * Whether to increment new RxQ set reference counters.
2402 * @param deref_old_qs
2403 * Whether to decrement old RxQ set reference counters.
2406 * 0 on success, a negative errno value otherwise and rte_errno is set.
2409 mlx5_ind_table_obj_modify(struct rte_eth_dev *dev,
2410 struct mlx5_ind_table_obj *ind_tbl,
2411 uint16_t *queues, const uint32_t queues_n,
2412 bool standalone, bool ref_new_qs, bool deref_old_qs)
2414 struct mlx5_priv *priv = dev->data->dev_private;
2415 unsigned int i = 0, j;
2417 const unsigned int n = rte_is_power_of_2(queues_n) ?
2418 log2above(queues_n) :
2419 log2above(priv->config.ind_table_max_size);
2421 MLX5_ASSERT(standalone);
2422 RTE_SET_USED(standalone);
2423 if (mlx5_ind_table_obj_check_standalone(dev, ind_tbl) < 0)
2426 for (i = 0; i != queues_n; ++i) {
2427 if (!mlx5_rxq_ref(dev, queues[i])) {
2432 MLX5_ASSERT(priv->obj_ops.ind_table_modify);
2433 ret = priv->obj_ops.ind_table_modify(dev, n, queues, queues_n, ind_tbl);
2437 for (i = 0; i < ind_tbl->queues_n; i++)
2438 claim_nonzero(mlx5_rxq_deref(dev, ind_tbl->queues[i]));
2439 ind_tbl->queues_n = queues_n;
2440 ind_tbl->queues = queues;
2445 for (j = 0; j < i; j++)
2446 mlx5_rxq_deref(dev, queues[j]);
2449 DRV_LOG(DEBUG, "Port %u cannot setup indirection table.",
2450 dev->data->port_id);
2455 * Attach an indirection table to its queues.
2458 * Pointer to Ethernet device.
2460 * Indirection table to attach.
2463 * 0 on success, a negative errno value otherwise and rte_errno is set.
2466 mlx5_ind_table_obj_attach(struct rte_eth_dev *dev,
2467 struct mlx5_ind_table_obj *ind_tbl)
2471 ret = mlx5_ind_table_obj_modify(dev, ind_tbl, ind_tbl->queues,
2473 true /* standalone */,
2474 true /* ref_new_qs */,
2475 false /* deref_old_qs */);
2477 DRV_LOG(ERR, "Port %u could not modify indirect table obj %p",
2478 dev->data->port_id, (void *)ind_tbl);
2483 * Detach an indirection table from its queues.
2486 * Pointer to Ethernet device.
2488 * Indirection table to detach.
2491 * 0 on success, a negative errno value otherwise and rte_errno is set.
2494 mlx5_ind_table_obj_detach(struct rte_eth_dev *dev,
2495 struct mlx5_ind_table_obj *ind_tbl)
2497 struct mlx5_priv *priv = dev->data->dev_private;
2498 const unsigned int n = rte_is_power_of_2(ind_tbl->queues_n) ?
2499 log2above(ind_tbl->queues_n) :
2500 log2above(priv->config.ind_table_max_size);
2504 ret = mlx5_ind_table_obj_check_standalone(dev, ind_tbl);
2507 MLX5_ASSERT(priv->obj_ops.ind_table_modify);
2508 ret = priv->obj_ops.ind_table_modify(dev, n, NULL, 0, ind_tbl);
2510 DRV_LOG(ERR, "Port %u could not modify indirect table obj %p",
2511 dev->data->port_id, (void *)ind_tbl);
2514 for (i = 0; i < ind_tbl->queues_n; i++)
2515 mlx5_rxq_release(dev, ind_tbl->queues[i]);
2520 mlx5_hrxq_match_cb(void *tool_ctx __rte_unused, struct mlx5_list_entry *entry,
2523 struct mlx5_flow_cb_ctx *ctx = cb_ctx;
2524 struct mlx5_flow_rss_desc *rss_desc = ctx->data;
2525 struct mlx5_hrxq *hrxq = container_of(entry, typeof(*hrxq), entry);
2527 return (hrxq->rss_key_len != rss_desc->key_len ||
2528 memcmp(hrxq->rss_key, rss_desc->key, rss_desc->key_len) ||
2529 hrxq->hash_fields != rss_desc->hash_fields ||
2530 hrxq->ind_table->queues_n != rss_desc->queue_num ||
2531 memcmp(hrxq->ind_table->queues, rss_desc->queue,
2532 rss_desc->queue_num * sizeof(rss_desc->queue[0])));
2536 * Modify an Rx Hash queue configuration.
2539 * Pointer to Ethernet device.
2541 * Index to Hash Rx queue to modify.
2543 * RSS key for the Rx hash queue.
2544 * @param rss_key_len
2546 * @param hash_fields
2547 * Verbs protocol hash field to make the RSS on.
2549 * Queues entering in hash queue. In case of empty hash_fields only the
2550 * first queue index will be taken for the indirection table.
2555 * 0 on success, a negative errno value otherwise and rte_errno is set.
2558 mlx5_hrxq_modify(struct rte_eth_dev *dev, uint32_t hrxq_idx,
2559 const uint8_t *rss_key, uint32_t rss_key_len,
2560 uint64_t hash_fields,
2561 const uint16_t *queues, uint32_t queues_n)
2564 struct mlx5_ind_table_obj *ind_tbl = NULL;
2565 struct mlx5_priv *priv = dev->data->dev_private;
2566 struct mlx5_hrxq *hrxq =
2567 mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2568 bool dev_started = !!dev->data->dev_started;
2576 if (hrxq->rss_key_len != rss_key_len) {
2577 /* rss_key_len is fixed size 40 byte & not supposed to change */
2581 queues_n = hash_fields ? queues_n : 1;
2582 if (mlx5_ind_table_obj_match_queues(hrxq->ind_table,
2583 queues, queues_n)) {
2584 ind_tbl = hrxq->ind_table;
2586 if (hrxq->standalone) {
2588 * Replacement of indirection table unsupported for
2589 * stanalone hrxq objects (used by shared RSS).
2591 rte_errno = ENOTSUP;
2594 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2596 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n,
2604 MLX5_ASSERT(priv->obj_ops.hrxq_modify);
2605 ret = priv->obj_ops.hrxq_modify(dev, hrxq, rss_key,
2606 hash_fields, ind_tbl);
2611 if (ind_tbl != hrxq->ind_table) {
2612 MLX5_ASSERT(!hrxq->standalone);
2613 mlx5_ind_table_obj_release(dev, hrxq->ind_table,
2614 hrxq->standalone, true);
2615 hrxq->ind_table = ind_tbl;
2617 hrxq->hash_fields = hash_fields;
2618 memcpy(hrxq->rss_key, rss_key, rss_key_len);
2622 if (ind_tbl != hrxq->ind_table) {
2623 MLX5_ASSERT(!hrxq->standalone);
2624 mlx5_ind_table_obj_release(dev, ind_tbl, hrxq->standalone,
2632 __mlx5_hrxq_remove(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq)
2634 struct mlx5_priv *priv = dev->data->dev_private;
2636 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
2637 mlx5_glue->destroy_flow_action(hrxq->action);
2639 priv->obj_ops.hrxq_destroy(hrxq);
2640 if (!hrxq->standalone) {
2641 mlx5_ind_table_obj_release(dev, hrxq->ind_table,
2642 hrxq->standalone, true);
2644 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq->idx);
2648 * Release the hash Rx queue.
2651 * Pointer to Ethernet device.
2653 * Index to Hash Rx queue to release.
2656 * mlx5 list pointer.
2658 * Hash queue entry pointer.
2661 mlx5_hrxq_remove_cb(void *tool_ctx, struct mlx5_list_entry *entry)
2663 struct rte_eth_dev *dev = tool_ctx;
2664 struct mlx5_hrxq *hrxq = container_of(entry, typeof(*hrxq), entry);
2666 __mlx5_hrxq_remove(dev, hrxq);
2669 static struct mlx5_hrxq *
2670 __mlx5_hrxq_create(struct rte_eth_dev *dev,
2671 struct mlx5_flow_rss_desc *rss_desc)
2673 struct mlx5_priv *priv = dev->data->dev_private;
2674 const uint8_t *rss_key = rss_desc->key;
2675 uint32_t rss_key_len = rss_desc->key_len;
2676 bool standalone = !!rss_desc->shared_rss;
2677 const uint16_t *queues =
2678 standalone ? rss_desc->const_q : rss_desc->queue;
2679 uint32_t queues_n = rss_desc->queue_num;
2680 struct mlx5_hrxq *hrxq = NULL;
2681 uint32_t hrxq_idx = 0;
2682 struct mlx5_ind_table_obj *ind_tbl = rss_desc->ind_tbl;
2685 queues_n = rss_desc->hash_fields ? queues_n : 1;
2687 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
2689 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n,
2691 !!dev->data->dev_started);
2694 hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
2697 hrxq->standalone = standalone;
2698 hrxq->idx = hrxq_idx;
2699 hrxq->ind_table = ind_tbl;
2700 hrxq->rss_key_len = rss_key_len;
2701 hrxq->hash_fields = rss_desc->hash_fields;
2702 memcpy(hrxq->rss_key, rss_key, rss_key_len);
2703 ret = priv->obj_ops.hrxq_new(dev, hrxq, rss_desc->tunnel);
2708 if (!rss_desc->ind_tbl)
2709 mlx5_ind_table_obj_release(dev, ind_tbl, standalone, true);
2711 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2715 struct mlx5_list_entry *
2716 mlx5_hrxq_create_cb(void *tool_ctx, void *cb_ctx)
2718 struct rte_eth_dev *dev = tool_ctx;
2719 struct mlx5_flow_cb_ctx *ctx = cb_ctx;
2720 struct mlx5_flow_rss_desc *rss_desc = ctx->data;
2721 struct mlx5_hrxq *hrxq;
2723 hrxq = __mlx5_hrxq_create(dev, rss_desc);
2724 return hrxq ? &hrxq->entry : NULL;
2727 struct mlx5_list_entry *
2728 mlx5_hrxq_clone_cb(void *tool_ctx, struct mlx5_list_entry *entry,
2729 void *cb_ctx __rte_unused)
2731 struct rte_eth_dev *dev = tool_ctx;
2732 struct mlx5_priv *priv = dev->data->dev_private;
2733 struct mlx5_hrxq *hrxq;
2734 uint32_t hrxq_idx = 0;
2736 hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
2739 memcpy(hrxq, entry, sizeof(*hrxq) + MLX5_RSS_HASH_KEY_LEN);
2740 hrxq->idx = hrxq_idx;
2741 return &hrxq->entry;
2745 mlx5_hrxq_clone_free_cb(void *tool_ctx, struct mlx5_list_entry *entry)
2747 struct rte_eth_dev *dev = tool_ctx;
2748 struct mlx5_priv *priv = dev->data->dev_private;
2749 struct mlx5_hrxq *hrxq = container_of(entry, typeof(*hrxq), entry);
2751 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq->idx);
2755 * Get an Rx Hash queue.
2758 * Pointer to Ethernet device.
2760 * RSS configuration for the Rx hash queue.
2763 * An hash Rx queue index on success.
2765 uint32_t mlx5_hrxq_get(struct rte_eth_dev *dev,
2766 struct mlx5_flow_rss_desc *rss_desc)
2768 struct mlx5_priv *priv = dev->data->dev_private;
2769 struct mlx5_hrxq *hrxq;
2770 struct mlx5_list_entry *entry;
2771 struct mlx5_flow_cb_ctx ctx = {
2775 if (rss_desc->shared_rss) {
2776 hrxq = __mlx5_hrxq_create(dev, rss_desc);
2778 entry = mlx5_list_register(priv->hrxqs, &ctx);
2781 hrxq = container_of(entry, typeof(*hrxq), entry);
2789 * Release the hash Rx queue.
2792 * Pointer to Ethernet device.
2794 * Index to Hash Rx queue to release.
2797 * 1 while a reference on it exists, 0 when freed.
2799 int mlx5_hrxq_release(struct rte_eth_dev *dev, uint32_t hrxq_idx)
2801 struct mlx5_priv *priv = dev->data->dev_private;
2802 struct mlx5_hrxq *hrxq;
2804 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2807 if (!hrxq->standalone)
2808 return mlx5_list_unregister(priv->hrxqs, &hrxq->entry);
2809 __mlx5_hrxq_remove(dev, hrxq);
2814 * Create a drop Rx Hash queue.
2817 * Pointer to Ethernet device.
2820 * The Verbs/DevX object initialized, NULL otherwise and rte_errno is set.
2823 mlx5_drop_action_create(struct rte_eth_dev *dev)
2825 struct mlx5_priv *priv = dev->data->dev_private;
2826 struct mlx5_hrxq *hrxq = NULL;
2829 if (priv->drop_queue.hrxq)
2830 return priv->drop_queue.hrxq;
2831 hrxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq), 0, SOCKET_ID_ANY);
2834 "Port %u cannot allocate memory for drop queue.",
2835 dev->data->port_id);
2839 priv->drop_queue.hrxq = hrxq;
2840 hrxq->ind_table = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq->ind_table),
2842 if (!hrxq->ind_table) {
2846 ret = priv->obj_ops.drop_action_create(dev);
2852 if (hrxq->ind_table)
2853 mlx5_free(hrxq->ind_table);
2854 priv->drop_queue.hrxq = NULL;
2861 * Release a drop hash Rx queue.
2864 * Pointer to Ethernet device.
2867 mlx5_drop_action_destroy(struct rte_eth_dev *dev)
2869 struct mlx5_priv *priv = dev->data->dev_private;
2870 struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
2872 if (!priv->drop_queue.hrxq)
2874 priv->obj_ops.drop_action_destroy(dev);
2875 mlx5_free(priv->drop_queue.rxq);
2876 mlx5_free(hrxq->ind_table);
2878 priv->drop_queue.rxq = NULL;
2879 priv->drop_queue.hrxq = NULL;
2883 * Verify the Rx Queue list is empty
2886 * Pointer to Ethernet device.
2889 * The number of object not released.
2892 mlx5_hrxq_verify(struct rte_eth_dev *dev)
2894 struct mlx5_priv *priv = dev->data->dev_private;
2896 return mlx5_list_get_entry_num(priv->hrxqs);
2900 * Set the Rx queue timestamp conversion parameters
2903 * Pointer to the Ethernet device structure.
2906 mlx5_rxq_timestamp_set(struct rte_eth_dev *dev)
2908 struct mlx5_priv *priv = dev->data->dev_private;
2909 struct mlx5_dev_ctx_shared *sh = priv->sh;
2912 for (i = 0; i != priv->rxqs_n; ++i) {
2913 struct mlx5_rxq_data *data = mlx5_rxq_data_get(dev, i);
2918 data->rt_timestamp = priv->config.rt_timestamp;