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 <rte_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>
25 #include "mlx5_defs.h"
27 #include "mlx5_rxtx.h"
28 #include "mlx5_utils.h"
29 #include "mlx5_autoconf.h"
32 /* Default RSS hash key also used for ConnectX-3. */
33 uint8_t rss_hash_default_key[] = {
34 0x2c, 0xc6, 0x81, 0xd1,
35 0x5b, 0xdb, 0xf4, 0xf7,
36 0xfc, 0xa2, 0x83, 0x19,
37 0xdb, 0x1a, 0x3e, 0x94,
38 0x6b, 0x9e, 0x38, 0xd9,
39 0x2c, 0x9c, 0x03, 0xd1,
40 0xad, 0x99, 0x44, 0xa7,
41 0xd9, 0x56, 0x3d, 0x59,
42 0x06, 0x3c, 0x25, 0xf3,
43 0xfc, 0x1f, 0xdc, 0x2a,
46 /* Length of the default RSS hash key. */
47 static_assert(MLX5_RSS_HASH_KEY_LEN ==
48 (unsigned int)sizeof(rss_hash_default_key),
49 "wrong RSS default key size.");
52 * Check whether Multi-Packet RQ can be enabled for the device.
55 * Pointer to Ethernet device.
58 * 1 if supported, negative errno value if not.
61 mlx5_check_mprq_support(struct rte_eth_dev *dev)
63 struct mlx5_priv *priv = dev->data->dev_private;
65 if (priv->config.mprq.enabled &&
66 priv->rxqs_n >= priv->config.mprq.min_rxqs_num)
72 * Check whether Multi-Packet RQ is enabled for the Rx queue.
75 * Pointer to receive queue structure.
78 * 0 if disabled, otherwise enabled.
81 mlx5_rxq_mprq_enabled(struct mlx5_rxq_data *rxq)
83 return rxq->strd_num_n > 0;
87 * Check whether Multi-Packet RQ is enabled for the device.
90 * Pointer to Ethernet device.
93 * 0 if disabled, otherwise enabled.
96 mlx5_mprq_enabled(struct rte_eth_dev *dev)
98 struct mlx5_priv *priv = dev->data->dev_private;
103 if (mlx5_check_mprq_support(dev) < 0)
105 /* All the configured queues should be enabled. */
106 for (i = 0; i < priv->rxqs_n; ++i) {
107 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
108 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
109 (rxq, struct mlx5_rxq_ctrl, rxq);
111 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
114 if (mlx5_rxq_mprq_enabled(rxq))
117 /* Multi-Packet RQ can't be partially configured. */
118 MLX5_ASSERT(n == 0 || n == n_ibv);
123 * Calculate the number of CQEs in CQ for the Rx queue.
126 * Pointer to receive queue structure.
129 * Number of CQEs in CQ.
132 mlx5_rxq_cqe_num(struct mlx5_rxq_data *rxq_data)
135 unsigned int wqe_n = 1 << rxq_data->elts_n;
137 if (mlx5_rxq_mprq_enabled(rxq_data))
138 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
145 * Allocate RX queue elements for Multi-Packet RQ.
148 * Pointer to RX queue structure.
151 * 0 on success, a negative errno value otherwise and rte_errno is set.
154 rxq_alloc_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
156 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
157 unsigned int wqe_n = 1 << rxq->elts_n;
161 /* Iterate on segments. */
162 for (i = 0; i <= wqe_n; ++i) {
163 struct mlx5_mprq_buf *buf;
165 if (rte_mempool_get(rxq->mprq_mp, (void **)&buf) < 0) {
166 DRV_LOG(ERR, "port %u empty mbuf pool", rxq->port_id);
171 (*rxq->mprq_bufs)[i] = buf;
173 rxq->mprq_repl = buf;
176 "port %u Rx queue %u allocated and configured %u segments",
177 rxq->port_id, rxq->idx, wqe_n);
180 err = rte_errno; /* Save rte_errno before cleanup. */
182 for (i = 0; (i != wqe_n); ++i) {
183 if ((*rxq->mprq_bufs)[i] != NULL)
184 rte_mempool_put(rxq->mprq_mp,
185 (*rxq->mprq_bufs)[i]);
186 (*rxq->mprq_bufs)[i] = NULL;
188 DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
189 rxq->port_id, rxq->idx);
190 rte_errno = err; /* Restore rte_errno. */
195 * Allocate RX queue elements for Single-Packet RQ.
198 * Pointer to RX queue structure.
201 * 0 on success, errno value on failure.
204 rxq_alloc_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
206 const unsigned int sges_n = 1 << rxq_ctrl->rxq.sges_n;
207 unsigned int elts_n = 1 << rxq_ctrl->rxq.elts_n;
211 /* Iterate on segments. */
212 for (i = 0; (i != elts_n); ++i) {
213 struct rte_mbuf *buf;
215 buf = rte_pktmbuf_alloc(rxq_ctrl->rxq.mp);
217 DRV_LOG(ERR, "port %u empty mbuf pool",
218 PORT_ID(rxq_ctrl->priv));
222 /* Headroom is reserved by rte_pktmbuf_alloc(). */
223 MLX5_ASSERT(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
224 /* Buffer is supposed to be empty. */
225 MLX5_ASSERT(rte_pktmbuf_data_len(buf) == 0);
226 MLX5_ASSERT(rte_pktmbuf_pkt_len(buf) == 0);
227 MLX5_ASSERT(!buf->next);
228 /* Only the first segment keeps headroom. */
230 SET_DATA_OFF(buf, 0);
231 PORT(buf) = rxq_ctrl->rxq.port_id;
232 DATA_LEN(buf) = rte_pktmbuf_tailroom(buf);
233 PKT_LEN(buf) = DATA_LEN(buf);
235 (*rxq_ctrl->rxq.elts)[i] = buf;
237 /* If Rx vector is activated. */
238 if (mlx5_rxq_check_vec_support(&rxq_ctrl->rxq) > 0) {
239 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
240 struct rte_mbuf *mbuf_init = &rxq->fake_mbuf;
241 struct rte_pktmbuf_pool_private *priv =
242 (struct rte_pktmbuf_pool_private *)
243 rte_mempool_get_priv(rxq_ctrl->rxq.mp);
246 /* Initialize default rearm_data for vPMD. */
247 mbuf_init->data_off = RTE_PKTMBUF_HEADROOM;
248 rte_mbuf_refcnt_set(mbuf_init, 1);
249 mbuf_init->nb_segs = 1;
250 mbuf_init->port = rxq->port_id;
251 if (priv->flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF)
252 mbuf_init->ol_flags = EXT_ATTACHED_MBUF;
254 * prevent compiler reordering:
255 * rearm_data covers previous fields.
257 rte_compiler_barrier();
258 rxq->mbuf_initializer =
259 *(rte_xmm_t *)&mbuf_init->rearm_data;
260 /* Padding with a fake mbuf for vectorized Rx. */
261 for (j = 0; j < MLX5_VPMD_DESCS_PER_LOOP; ++j)
262 (*rxq->elts)[elts_n + j] = &rxq->fake_mbuf;
265 "port %u Rx queue %u allocated and configured %u segments"
267 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx, elts_n,
268 elts_n / (1 << rxq_ctrl->rxq.sges_n));
271 err = rte_errno; /* Save rte_errno before cleanup. */
273 for (i = 0; (i != elts_n); ++i) {
274 if ((*rxq_ctrl->rxq.elts)[i] != NULL)
275 rte_pktmbuf_free_seg((*rxq_ctrl->rxq.elts)[i]);
276 (*rxq_ctrl->rxq.elts)[i] = NULL;
278 DRV_LOG(DEBUG, "port %u Rx queue %u failed, freed everything",
279 PORT_ID(rxq_ctrl->priv), rxq_ctrl->rxq.idx);
280 rte_errno = err; /* Restore rte_errno. */
285 * Allocate RX queue elements.
288 * Pointer to RX queue structure.
291 * 0 on success, errno value on failure.
294 rxq_alloc_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
296 return mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
297 rxq_alloc_elts_mprq(rxq_ctrl) : rxq_alloc_elts_sprq(rxq_ctrl);
301 * Free RX queue elements for Multi-Packet RQ.
304 * Pointer to RX queue structure.
307 rxq_free_elts_mprq(struct mlx5_rxq_ctrl *rxq_ctrl)
309 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
312 DRV_LOG(DEBUG, "port %u Multi-Packet Rx queue %u freeing WRs",
313 rxq->port_id, rxq->idx);
314 if (rxq->mprq_bufs == NULL)
316 MLX5_ASSERT(mlx5_rxq_check_vec_support(rxq) < 0);
317 for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
318 if ((*rxq->mprq_bufs)[i] != NULL)
319 mlx5_mprq_buf_free((*rxq->mprq_bufs)[i]);
320 (*rxq->mprq_bufs)[i] = NULL;
322 if (rxq->mprq_repl != NULL) {
323 mlx5_mprq_buf_free(rxq->mprq_repl);
324 rxq->mprq_repl = NULL;
329 * Free RX queue elements for Single-Packet RQ.
332 * Pointer to RX queue structure.
335 rxq_free_elts_sprq(struct mlx5_rxq_ctrl *rxq_ctrl)
337 struct mlx5_rxq_data *rxq = &rxq_ctrl->rxq;
338 const uint16_t q_n = (1 << rxq->elts_n);
339 const uint16_t q_mask = q_n - 1;
340 uint16_t used = q_n - (rxq->rq_ci - rxq->rq_pi);
343 DRV_LOG(DEBUG, "port %u Rx queue %u freeing WRs",
344 PORT_ID(rxq_ctrl->priv), rxq->idx);
345 if (rxq->elts == NULL)
348 * Some mbuf in the Ring belongs to the application. They cannot be
351 if (mlx5_rxq_check_vec_support(rxq) > 0) {
352 for (i = 0; i < used; ++i)
353 (*rxq->elts)[(rxq->rq_ci + i) & q_mask] = NULL;
354 rxq->rq_pi = rxq->rq_ci;
356 for (i = 0; (i != (1u << rxq->elts_n)); ++i) {
357 if ((*rxq->elts)[i] != NULL)
358 rte_pktmbuf_free_seg((*rxq->elts)[i]);
359 (*rxq->elts)[i] = NULL;
364 * Free RX queue elements.
367 * Pointer to RX queue structure.
370 rxq_free_elts(struct mlx5_rxq_ctrl *rxq_ctrl)
372 if (mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq))
373 rxq_free_elts_mprq(rxq_ctrl);
375 rxq_free_elts_sprq(rxq_ctrl);
379 * Returns the per-queue supported offloads.
382 * Pointer to Ethernet device.
385 * Supported Rx offloads.
388 mlx5_get_rx_queue_offloads(struct rte_eth_dev *dev)
390 struct mlx5_priv *priv = dev->data->dev_private;
391 struct mlx5_dev_config *config = &priv->config;
392 uint64_t offloads = (DEV_RX_OFFLOAD_SCATTER |
393 DEV_RX_OFFLOAD_TIMESTAMP |
394 DEV_RX_OFFLOAD_JUMBO_FRAME |
395 DEV_RX_OFFLOAD_RSS_HASH);
397 if (config->hw_fcs_strip)
398 offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
401 offloads |= (DEV_RX_OFFLOAD_IPV4_CKSUM |
402 DEV_RX_OFFLOAD_UDP_CKSUM |
403 DEV_RX_OFFLOAD_TCP_CKSUM);
404 if (config->hw_vlan_strip)
405 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
406 if (MLX5_LRO_SUPPORTED(dev))
407 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
413 * Returns the per-port supported offloads.
416 * Supported Rx offloads.
419 mlx5_get_rx_port_offloads(void)
421 uint64_t offloads = DEV_RX_OFFLOAD_VLAN_FILTER;
427 * Verify if the queue can be released.
430 * Pointer to Ethernet device.
435 * 1 if the queue can be released
436 * 0 if the queue can not be released, there are references to it.
437 * Negative errno and rte_errno is set if queue doesn't exist.
440 mlx5_rxq_releasable(struct rte_eth_dev *dev, uint16_t idx)
442 struct mlx5_priv *priv = dev->data->dev_private;
443 struct mlx5_rxq_ctrl *rxq_ctrl;
445 if (!(*priv->rxqs)[idx]) {
449 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
450 return (rte_atomic32_read(&rxq_ctrl->refcnt) == 1);
453 /* Fetches and drops all SW-owned and error CQEs to synchronize CQ. */
455 rxq_sync_cq(struct mlx5_rxq_data *rxq)
457 const uint16_t cqe_n = 1 << rxq->cqe_n;
458 const uint16_t cqe_mask = cqe_n - 1;
459 volatile struct mlx5_cqe *cqe;
464 cqe = &(*rxq->cqes)[rxq->cq_ci & cqe_mask];
465 ret = check_cqe(cqe, cqe_n, rxq->cq_ci);
466 if (ret == MLX5_CQE_STATUS_HW_OWN)
468 if (ret == MLX5_CQE_STATUS_ERR) {
472 MLX5_ASSERT(ret == MLX5_CQE_STATUS_SW_OWN);
473 if (MLX5_CQE_FORMAT(cqe->op_own) != MLX5_COMPRESSED) {
477 /* Compute the next non compressed CQE. */
478 rxq->cq_ci += rte_be_to_cpu_32(cqe->byte_cnt);
481 /* Move all CQEs to HW ownership, including possible MiniCQEs. */
482 for (i = 0; i < cqe_n; i++) {
483 cqe = &(*rxq->cqes)[i];
484 cqe->op_own = MLX5_CQE_INVALIDATE;
486 /* Resync CQE and WQE (WQ in RESET state). */
488 *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
490 *rxq->rq_db = rte_cpu_to_be_32(0);
495 * Rx queue stop. Device queue goes to the RESET state,
496 * all involved mbufs are freed from WQ.
499 * Pointer to Ethernet device structure.
504 * 0 on success, a negative errno value otherwise and rte_errno is set.
507 mlx5_rx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t idx)
509 struct mlx5_priv *priv = dev->data->dev_private;
510 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
511 struct mlx5_rxq_ctrl *rxq_ctrl =
512 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
515 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
516 ret = priv->obj_ops.rxq_obj_modify(rxq_ctrl->obj, false);
518 DRV_LOG(ERR, "Cannot change Rx WQ state to RESET: %s",
523 /* Remove all processes CQEs. */
525 /* Free all involved mbufs. */
526 rxq_free_elts(rxq_ctrl);
527 /* Set the actual queue state. */
528 dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
533 * Rx queue stop. Device queue goes to the RESET state,
534 * all involved mbufs are freed from WQ.
537 * Pointer to Ethernet device structure.
542 * 0 on success, a negative errno value otherwise and rte_errno is set.
545 mlx5_rx_queue_stop(struct rte_eth_dev *dev, uint16_t idx)
547 eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
550 if (rte_eth_dev_is_rx_hairpin_queue(dev, idx)) {
551 DRV_LOG(ERR, "Hairpin queue can't be stopped");
555 if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STOPPED)
558 * Vectorized Rx burst requires the CQ and RQ indices
559 * synchronized, that might be broken on RQ restart
560 * and cause Rx malfunction, so queue stopping is
561 * not supported if vectorized Rx burst is engaged.
562 * The routine pointer depends on the process
563 * type, should perform check there.
565 if (pkt_burst == mlx5_rx_burst_vec) {
566 DRV_LOG(ERR, "Rx queue stop is not supported "
567 "for vectorized Rx");
571 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
572 ret = mlx5_mp_os_req_queue_control(dev, idx,
573 MLX5_MP_REQ_QUEUE_RX_STOP);
575 ret = mlx5_rx_queue_stop_primary(dev, idx);
581 * Rx queue start. Device queue goes to the ready state,
582 * all required mbufs are allocated and WQ is replenished.
585 * Pointer to Ethernet device structure.
590 * 0 on success, a negative errno value otherwise and rte_errno is set.
593 mlx5_rx_queue_start_primary(struct rte_eth_dev *dev, uint16_t idx)
595 struct mlx5_priv *priv = dev->data->dev_private;
596 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
597 struct mlx5_rxq_ctrl *rxq_ctrl =
598 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
601 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
602 /* Allocate needed buffers. */
603 ret = rxq_alloc_elts(rxq_ctrl);
605 DRV_LOG(ERR, "Cannot reallocate buffers for Rx WQ");
610 *rxq->cq_db = rte_cpu_to_be_32(rxq->cq_ci);
612 /* Reset RQ consumer before moving queue ro READY state. */
613 *rxq->rq_db = rte_cpu_to_be_32(0);
615 ret = priv->obj_ops.rxq_obj_modify(rxq_ctrl->obj, true);
617 DRV_LOG(ERR, "Cannot change Rx WQ state to READY: %s",
622 /* Reinitialize RQ - set WQEs. */
623 mlx5_rxq_initialize(rxq);
624 rxq->err_state = MLX5_RXQ_ERR_STATE_NO_ERROR;
625 /* Set actual queue state. */
626 dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
631 * Rx queue start. Device queue goes to the ready state,
632 * all required mbufs are allocated and WQ is replenished.
635 * Pointer to Ethernet device structure.
640 * 0 on success, a negative errno value otherwise and rte_errno is set.
643 mlx5_rx_queue_start(struct rte_eth_dev *dev, uint16_t idx)
647 if (rte_eth_dev_is_rx_hairpin_queue(dev, idx)) {
648 DRV_LOG(ERR, "Hairpin queue can't be started");
652 if (dev->data->rx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STARTED)
654 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
655 ret = mlx5_mp_os_req_queue_control(dev, idx,
656 MLX5_MP_REQ_QUEUE_RX_START);
658 ret = mlx5_rx_queue_start_primary(dev, idx);
664 * Rx queue presetup checks.
667 * Pointer to Ethernet device structure.
671 * Number of descriptors to configure in queue.
674 * 0 on success, a negative errno value otherwise and rte_errno is set.
677 mlx5_rx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t *desc)
679 struct mlx5_priv *priv = dev->data->dev_private;
681 if (!rte_is_power_of_2(*desc)) {
682 *desc = 1 << log2above(*desc);
684 "port %u increased number of descriptors in Rx queue %u"
685 " to the next power of two (%d)",
686 dev->data->port_id, idx, *desc);
688 DRV_LOG(DEBUG, "port %u configuring Rx queue %u for %u descriptors",
689 dev->data->port_id, idx, *desc);
690 if (idx >= priv->rxqs_n) {
691 DRV_LOG(ERR, "port %u Rx queue index out of range (%u >= %u)",
692 dev->data->port_id, idx, priv->rxqs_n);
693 rte_errno = EOVERFLOW;
696 if (!mlx5_rxq_releasable(dev, idx)) {
697 DRV_LOG(ERR, "port %u unable to release queue index %u",
698 dev->data->port_id, idx);
702 mlx5_rxq_release(dev, idx);
709 * Pointer to Ethernet device structure.
713 * Number of descriptors to configure in queue.
715 * NUMA socket on which memory must be allocated.
717 * Thresholds parameters.
719 * Memory pool for buffer allocations.
722 * 0 on success, a negative errno value otherwise and rte_errno is set.
725 mlx5_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
726 unsigned int socket, const struct rte_eth_rxconf *conf,
727 struct rte_mempool *mp)
729 struct mlx5_priv *priv = dev->data->dev_private;
730 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
731 struct mlx5_rxq_ctrl *rxq_ctrl =
732 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
735 res = mlx5_rx_queue_pre_setup(dev, idx, &desc);
738 rxq_ctrl = mlx5_rxq_new(dev, idx, desc, socket, conf, mp);
740 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
741 dev->data->port_id, idx);
745 DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
746 dev->data->port_id, idx);
747 (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
754 * Pointer to Ethernet device structure.
758 * Number of descriptors to configure in queue.
759 * @param hairpin_conf
760 * Hairpin configuration parameters.
763 * 0 on success, a negative errno value otherwise and rte_errno is set.
766 mlx5_rx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
768 const struct rte_eth_hairpin_conf *hairpin_conf)
770 struct mlx5_priv *priv = dev->data->dev_private;
771 struct mlx5_rxq_data *rxq = (*priv->rxqs)[idx];
772 struct mlx5_rxq_ctrl *rxq_ctrl =
773 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
776 res = mlx5_rx_queue_pre_setup(dev, idx, &desc);
779 if (hairpin_conf->peer_count != 1 ||
780 hairpin_conf->peers[0].port != dev->data->port_id ||
781 hairpin_conf->peers[0].queue >= priv->txqs_n) {
782 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
783 " invalid hairpind configuration", dev->data->port_id,
788 rxq_ctrl = mlx5_rxq_hairpin_new(dev, idx, desc, hairpin_conf);
790 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
791 dev->data->port_id, idx);
795 DRV_LOG(DEBUG, "port %u adding Rx queue %u to list",
796 dev->data->port_id, idx);
797 (*priv->rxqs)[idx] = &rxq_ctrl->rxq;
802 * DPDK callback to release a RX queue.
805 * Generic RX queue pointer.
808 mlx5_rx_queue_release(void *dpdk_rxq)
810 struct mlx5_rxq_data *rxq = (struct mlx5_rxq_data *)dpdk_rxq;
811 struct mlx5_rxq_ctrl *rxq_ctrl;
812 struct mlx5_priv *priv;
816 rxq_ctrl = container_of(rxq, struct mlx5_rxq_ctrl, rxq);
817 priv = rxq_ctrl->priv;
818 if (!mlx5_rxq_releasable(ETH_DEV(priv), rxq_ctrl->rxq.idx))
819 rte_panic("port %u Rx queue %u is still used by a flow and"
820 " cannot be removed\n",
821 PORT_ID(priv), rxq->idx);
822 mlx5_rxq_release(ETH_DEV(priv), rxq_ctrl->rxq.idx);
826 * Allocate queue vector and fill epoll fd list for Rx interrupts.
829 * Pointer to Ethernet device.
832 * 0 on success, a negative errno value otherwise and rte_errno is set.
835 mlx5_rx_intr_vec_enable(struct rte_eth_dev *dev)
837 struct mlx5_priv *priv = dev->data->dev_private;
839 unsigned int rxqs_n = priv->rxqs_n;
840 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
841 unsigned int count = 0;
842 struct rte_intr_handle *intr_handle = dev->intr_handle;
844 if (!dev->data->dev_conf.intr_conf.rxq)
846 mlx5_rx_intr_vec_disable(dev);
847 intr_handle->intr_vec = mlx5_malloc(0,
848 n * sizeof(intr_handle->intr_vec[0]),
850 if (intr_handle->intr_vec == NULL) {
852 "port %u failed to allocate memory for interrupt"
853 " vector, Rx interrupts will not be supported",
858 intr_handle->type = RTE_INTR_HANDLE_EXT;
859 for (i = 0; i != n; ++i) {
860 /* This rxq obj must not be released in this function. */
861 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
862 struct mlx5_rxq_obj *rxq_obj = rxq_ctrl ? rxq_ctrl->obj : NULL;
865 /* Skip queues that cannot request interrupts. */
866 if (!rxq_obj || (!rxq_obj->ibv_channel &&
867 !rxq_obj->devx_channel)) {
868 /* Use invalid intr_vec[] index to disable entry. */
869 intr_handle->intr_vec[i] =
870 RTE_INTR_VEC_RXTX_OFFSET +
871 RTE_MAX_RXTX_INTR_VEC_ID;
872 /* Decrease the rxq_ctrl's refcnt */
874 mlx5_rxq_release(dev, i);
877 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
879 "port %u too many Rx queues for interrupt"
880 " vector size (%d), Rx interrupts cannot be"
882 dev->data->port_id, RTE_MAX_RXTX_INTR_VEC_ID);
883 mlx5_rx_intr_vec_disable(dev);
887 rc = mlx5_os_set_nonblock_channel_fd(rxq_obj->fd);
891 "port %u failed to make Rx interrupt file"
892 " descriptor %d non-blocking for queue index"
894 dev->data->port_id, rxq_obj->fd, i);
895 mlx5_rx_intr_vec_disable(dev);
898 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
899 intr_handle->efds[count] = rxq_obj->fd;
903 mlx5_rx_intr_vec_disable(dev);
905 intr_handle->nb_efd = count;
910 * Clean up Rx interrupts handler.
913 * Pointer to Ethernet device.
916 mlx5_rx_intr_vec_disable(struct rte_eth_dev *dev)
918 struct mlx5_priv *priv = dev->data->dev_private;
919 struct rte_intr_handle *intr_handle = dev->intr_handle;
921 unsigned int rxqs_n = priv->rxqs_n;
922 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
924 if (!dev->data->dev_conf.intr_conf.rxq)
926 if (!intr_handle->intr_vec)
928 for (i = 0; i != n; ++i) {
929 if (intr_handle->intr_vec[i] == RTE_INTR_VEC_RXTX_OFFSET +
930 RTE_MAX_RXTX_INTR_VEC_ID)
933 * Need to access directly the queue to release the reference
934 * kept in mlx5_rx_intr_vec_enable().
936 mlx5_rxq_release(dev, i);
939 rte_intr_free_epoll_fd(intr_handle);
940 if (intr_handle->intr_vec)
941 mlx5_free(intr_handle->intr_vec);
942 intr_handle->nb_efd = 0;
943 intr_handle->intr_vec = NULL;
947 * MLX5 CQ notification .
950 * Pointer to receive queue structure.
952 * Sequence number per receive queue .
955 mlx5_arm_cq(struct mlx5_rxq_data *rxq, int sq_n_rxq)
958 uint32_t doorbell_hi;
960 void *cq_db_reg = (char *)rxq->cq_uar + MLX5_CQ_DOORBELL;
962 sq_n = sq_n_rxq & MLX5_CQ_SQN_MASK;
963 doorbell_hi = sq_n << MLX5_CQ_SQN_OFFSET | (rxq->cq_ci & MLX5_CI_MASK);
964 doorbell = (uint64_t)doorbell_hi << 32;
965 doorbell |= rxq->cqn;
966 rxq->cq_db[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
967 mlx5_uar_write64(rte_cpu_to_be_64(doorbell),
968 cq_db_reg, rxq->uar_lock_cq);
972 * DPDK callback for Rx queue interrupt enable.
975 * Pointer to Ethernet device structure.
980 * 0 on success, a negative errno value otherwise and rte_errno is set.
983 mlx5_rx_intr_enable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
985 struct mlx5_rxq_ctrl *rxq_ctrl;
987 rxq_ctrl = mlx5_rxq_get(dev, rx_queue_id);
991 if (!rxq_ctrl->obj) {
992 mlx5_rxq_release(dev, rx_queue_id);
995 mlx5_arm_cq(&rxq_ctrl->rxq, rxq_ctrl->rxq.cq_arm_sn);
997 mlx5_rxq_release(dev, rx_queue_id);
1005 * DPDK callback for Rx queue interrupt disable.
1008 * Pointer to Ethernet device structure.
1009 * @param rx_queue_id
1013 * 0 on success, a negative errno value otherwise and rte_errno is set.
1016 mlx5_rx_intr_disable(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1018 struct mlx5_priv *priv = dev->data->dev_private;
1019 struct mlx5_rxq_ctrl *rxq_ctrl;
1022 rxq_ctrl = mlx5_rxq_get(dev, rx_queue_id);
1029 if (rxq_ctrl->irq) {
1030 ret = priv->obj_ops.rxq_event_get(rxq_ctrl->obj);
1033 rxq_ctrl->rxq.cq_arm_sn++;
1035 mlx5_rxq_release(dev, rx_queue_id);
1039 * The ret variable may be EAGAIN which means the get_event function was
1040 * called before receiving one.
1046 ret = rte_errno; /* Save rte_errno before cleanup. */
1047 mlx5_rxq_release(dev, rx_queue_id);
1049 DRV_LOG(WARNING, "port %u unable to disable interrupt on Rx queue %d",
1050 dev->data->port_id, rx_queue_id);
1051 rte_errno = ret; /* Restore rte_errno. */
1056 * Verify the Rx queue objects list is empty
1059 * Pointer to Ethernet device.
1062 * The number of objects not released.
1065 mlx5_rxq_obj_verify(struct rte_eth_dev *dev)
1067 struct mlx5_priv *priv = dev->data->dev_private;
1069 struct mlx5_rxq_obj *rxq_obj;
1071 LIST_FOREACH(rxq_obj, &priv->rxqsobj, next) {
1072 DRV_LOG(DEBUG, "port %u Rx queue %u still referenced",
1073 dev->data->port_id, rxq_obj->rxq_ctrl->rxq.idx);
1080 * Callback function to initialize mbufs for Multi-Packet RQ.
1083 mlx5_mprq_buf_init(struct rte_mempool *mp, void *opaque_arg,
1084 void *_m, unsigned int i __rte_unused)
1086 struct mlx5_mprq_buf *buf = _m;
1087 struct rte_mbuf_ext_shared_info *shinfo;
1088 unsigned int strd_n = (unsigned int)(uintptr_t)opaque_arg;
1091 memset(_m, 0, sizeof(*buf));
1093 rte_atomic16_set(&buf->refcnt, 1);
1094 for (j = 0; j != strd_n; ++j) {
1095 shinfo = &buf->shinfos[j];
1096 shinfo->free_cb = mlx5_mprq_buf_free_cb;
1097 shinfo->fcb_opaque = buf;
1102 * Free mempool of Multi-Packet RQ.
1105 * Pointer to Ethernet device.
1108 * 0 on success, negative errno value on failure.
1111 mlx5_mprq_free_mp(struct rte_eth_dev *dev)
1113 struct mlx5_priv *priv = dev->data->dev_private;
1114 struct rte_mempool *mp = priv->mprq_mp;
1119 DRV_LOG(DEBUG, "port %u freeing mempool (%s) for Multi-Packet RQ",
1120 dev->data->port_id, mp->name);
1122 * If a buffer in the pool has been externally attached to a mbuf and it
1123 * is still in use by application, destroying the Rx queue can spoil
1124 * the packet. It is unlikely to happen but if application dynamically
1125 * creates and destroys with holding Rx packets, this can happen.
1127 * TODO: It is unavoidable for now because the mempool for Multi-Packet
1128 * RQ isn't provided by application but managed by PMD.
1130 if (!rte_mempool_full(mp)) {
1132 "port %u mempool for Multi-Packet RQ is still in use",
1133 dev->data->port_id);
1137 rte_mempool_free(mp);
1138 /* Unset mempool for each Rx queue. */
1139 for (i = 0; i != priv->rxqs_n; ++i) {
1140 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1144 rxq->mprq_mp = NULL;
1146 priv->mprq_mp = NULL;
1151 * Allocate a mempool for Multi-Packet RQ. All configured Rx queues share the
1152 * mempool. If already allocated, reuse it if there're enough elements.
1153 * Otherwise, resize it.
1156 * Pointer to Ethernet device.
1159 * 0 on success, negative errno value on failure.
1162 mlx5_mprq_alloc_mp(struct rte_eth_dev *dev)
1164 struct mlx5_priv *priv = dev->data->dev_private;
1165 struct rte_mempool *mp = priv->mprq_mp;
1166 char name[RTE_MEMPOOL_NAMESIZE];
1167 unsigned int desc = 0;
1168 unsigned int buf_len;
1169 unsigned int obj_num;
1170 unsigned int obj_size;
1171 unsigned int strd_num_n = 0;
1172 unsigned int strd_sz_n = 0;
1174 unsigned int n_ibv = 0;
1176 if (!mlx5_mprq_enabled(dev))
1178 /* Count the total number of descriptors configured. */
1179 for (i = 0; i != priv->rxqs_n; ++i) {
1180 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1181 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
1182 (rxq, struct mlx5_rxq_ctrl, rxq);
1184 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1187 desc += 1 << rxq->elts_n;
1188 /* Get the max number of strides. */
1189 if (strd_num_n < rxq->strd_num_n)
1190 strd_num_n = rxq->strd_num_n;
1191 /* Get the max size of a stride. */
1192 if (strd_sz_n < rxq->strd_sz_n)
1193 strd_sz_n = rxq->strd_sz_n;
1195 MLX5_ASSERT(strd_num_n && strd_sz_n);
1196 buf_len = (1 << strd_num_n) * (1 << strd_sz_n);
1197 obj_size = sizeof(struct mlx5_mprq_buf) + buf_len + (1 << strd_num_n) *
1198 sizeof(struct rte_mbuf_ext_shared_info) + RTE_PKTMBUF_HEADROOM;
1200 * Received packets can be either memcpy'd or externally referenced. In
1201 * case that the packet is attached to an mbuf as an external buffer, as
1202 * it isn't possible to predict how the buffers will be queued by
1203 * application, there's no option to exactly pre-allocate needed buffers
1204 * in advance but to speculatively prepares enough buffers.
1206 * In the data path, if this Mempool is depleted, PMD will try to memcpy
1207 * received packets to buffers provided by application (rxq->mp) until
1208 * this Mempool gets available again.
1211 obj_num = desc + MLX5_MPRQ_MP_CACHE_SZ * n_ibv;
1213 * rte_mempool_create_empty() has sanity check to refuse large cache
1214 * size compared to the number of elements.
1215 * CACHE_FLUSHTHRESH_MULTIPLIER is defined in a C file, so using a
1216 * constant number 2 instead.
1218 obj_num = RTE_MAX(obj_num, MLX5_MPRQ_MP_CACHE_SZ * 2);
1219 /* Check a mempool is already allocated and if it can be resued. */
1220 if (mp != NULL && mp->elt_size >= obj_size && mp->size >= obj_num) {
1221 DRV_LOG(DEBUG, "port %u mempool %s is being reused",
1222 dev->data->port_id, mp->name);
1225 } else if (mp != NULL) {
1226 DRV_LOG(DEBUG, "port %u mempool %s should be resized, freeing it",
1227 dev->data->port_id, mp->name);
1229 * If failed to free, which means it may be still in use, no way
1230 * but to keep using the existing one. On buffer underrun,
1231 * packets will be memcpy'd instead of external buffer
1234 if (mlx5_mprq_free_mp(dev)) {
1235 if (mp->elt_size >= obj_size)
1241 snprintf(name, sizeof(name), "port-%u-mprq", dev->data->port_id);
1242 mp = rte_mempool_create(name, obj_num, obj_size, MLX5_MPRQ_MP_CACHE_SZ,
1243 0, NULL, NULL, mlx5_mprq_buf_init,
1244 (void *)(uintptr_t)(1 << strd_num_n),
1245 dev->device->numa_node, 0);
1248 "port %u failed to allocate a mempool for"
1249 " Multi-Packet RQ, count=%u, size=%u",
1250 dev->data->port_id, obj_num, obj_size);
1256 /* Set mempool for each Rx queue. */
1257 for (i = 0; i != priv->rxqs_n; ++i) {
1258 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
1259 struct mlx5_rxq_ctrl *rxq_ctrl = container_of
1260 (rxq, struct mlx5_rxq_ctrl, rxq);
1262 if (rxq == NULL || rxq_ctrl->type != MLX5_RXQ_TYPE_STANDARD)
1266 DRV_LOG(INFO, "port %u Multi-Packet RQ is configured",
1267 dev->data->port_id);
1271 #define MLX5_MAX_TCP_HDR_OFFSET ((unsigned int)(sizeof(struct rte_ether_hdr) + \
1272 sizeof(struct rte_vlan_hdr) * 2 + \
1273 sizeof(struct rte_ipv6_hdr)))
1274 #define MAX_TCP_OPTION_SIZE 40u
1275 #define MLX5_MAX_LRO_HEADER_FIX ((unsigned int)(MLX5_MAX_TCP_HDR_OFFSET + \
1276 sizeof(struct rte_tcp_hdr) + \
1277 MAX_TCP_OPTION_SIZE))
1280 * Adjust the maximum LRO massage size.
1283 * Pointer to Ethernet device.
1286 * @param max_lro_size
1287 * The maximum size for LRO packet.
1290 mlx5_max_lro_msg_size_adjust(struct rte_eth_dev *dev, uint16_t idx,
1291 uint32_t max_lro_size)
1293 struct mlx5_priv *priv = dev->data->dev_private;
1295 if (priv->config.hca_attr.lro_max_msg_sz_mode ==
1296 MLX5_LRO_MAX_MSG_SIZE_START_FROM_L4 && max_lro_size >
1297 MLX5_MAX_TCP_HDR_OFFSET)
1298 max_lro_size -= MLX5_MAX_TCP_HDR_OFFSET;
1299 max_lro_size = RTE_MIN(max_lro_size, MLX5_MAX_LRO_SIZE);
1300 MLX5_ASSERT(max_lro_size >= MLX5_LRO_SEG_CHUNK_SIZE);
1301 max_lro_size /= MLX5_LRO_SEG_CHUNK_SIZE;
1302 if (priv->max_lro_msg_size)
1303 priv->max_lro_msg_size =
1304 RTE_MIN((uint32_t)priv->max_lro_msg_size, max_lro_size);
1306 priv->max_lro_msg_size = max_lro_size;
1308 "port %u Rx Queue %u max LRO message size adjusted to %u bytes",
1309 dev->data->port_id, idx,
1310 priv->max_lro_msg_size * MLX5_LRO_SEG_CHUNK_SIZE);
1314 * Create a DPDK Rx queue.
1317 * Pointer to Ethernet device.
1321 * Number of descriptors to configure in queue.
1323 * NUMA socket on which memory must be allocated.
1326 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1328 struct mlx5_rxq_ctrl *
1329 mlx5_rxq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1330 unsigned int socket, const struct rte_eth_rxconf *conf,
1331 struct rte_mempool *mp)
1333 struct mlx5_priv *priv = dev->data->dev_private;
1334 struct mlx5_rxq_ctrl *tmpl;
1335 unsigned int mb_len = rte_pktmbuf_data_room_size(mp);
1336 unsigned int mprq_stride_nums;
1337 unsigned int mprq_stride_size;
1338 unsigned int mprq_stride_cap;
1339 struct mlx5_dev_config *config = &priv->config;
1341 * Always allocate extra slots, even if eventually
1342 * the vector Rx will not be used.
1345 desc + config->rx_vec_en * MLX5_VPMD_DESCS_PER_LOOP;
1346 uint64_t offloads = conf->offloads |
1347 dev->data->dev_conf.rxmode.offloads;
1348 unsigned int lro_on_queue = !!(offloads & DEV_RX_OFFLOAD_TCP_LRO);
1349 const int mprq_en = mlx5_check_mprq_support(dev) > 0;
1350 unsigned int max_rx_pkt_len = lro_on_queue ?
1351 dev->data->dev_conf.rxmode.max_lro_pkt_size :
1352 dev->data->dev_conf.rxmode.max_rx_pkt_len;
1353 unsigned int non_scatter_min_mbuf_size = max_rx_pkt_len +
1354 RTE_PKTMBUF_HEADROOM;
1355 unsigned int max_lro_size = 0;
1356 unsigned int first_mb_free_size = mb_len - RTE_PKTMBUF_HEADROOM;
1358 if (non_scatter_min_mbuf_size > mb_len && !(offloads &
1359 DEV_RX_OFFLOAD_SCATTER)) {
1360 DRV_LOG(ERR, "port %u Rx queue %u: Scatter offload is not"
1361 " configured and no enough mbuf space(%u) to contain "
1362 "the maximum RX packet length(%u) with head-room(%u)",
1363 dev->data->port_id, idx, mb_len, max_rx_pkt_len,
1364 RTE_PKTMBUF_HEADROOM);
1368 tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl) +
1369 desc_n * sizeof(struct rte_mbuf *), 0, socket);
1374 tmpl->type = MLX5_RXQ_TYPE_STANDARD;
1375 if (mlx5_mr_btree_init(&tmpl->rxq.mr_ctrl.cache_bh,
1376 MLX5_MR_BTREE_CACHE_N, socket)) {
1377 /* rte_errno is already set. */
1380 tmpl->socket = socket;
1381 if (dev->data->dev_conf.intr_conf.rxq)
1383 mprq_stride_nums = config->mprq.stride_num_n ?
1384 config->mprq.stride_num_n : MLX5_MPRQ_STRIDE_NUM_N;
1385 mprq_stride_size = non_scatter_min_mbuf_size <=
1386 (1U << config->mprq.max_stride_size_n) ?
1387 log2above(non_scatter_min_mbuf_size) : MLX5_MPRQ_STRIDE_SIZE_N;
1388 mprq_stride_cap = (config->mprq.stride_num_n ?
1389 (1U << config->mprq.stride_num_n) : (1U << mprq_stride_nums)) *
1390 (config->mprq.stride_size_n ?
1391 (1U << config->mprq.stride_size_n) : (1U << mprq_stride_size));
1393 * This Rx queue can be configured as a Multi-Packet RQ if all of the
1394 * following conditions are met:
1395 * - MPRQ is enabled.
1396 * - The number of descs is more than the number of strides.
1397 * - max_rx_pkt_len plus overhead is less than the max size
1398 * of a stride or mprq_stride_size is specified by a user.
1399 * Need to nake sure that there are enough stides to encap
1400 * the maximum packet size in case mprq_stride_size is set.
1401 * Otherwise, enable Rx scatter if necessary.
1403 if (mprq_en && desc > (1U << mprq_stride_nums) &&
1404 (non_scatter_min_mbuf_size <=
1405 (1U << config->mprq.max_stride_size_n) ||
1406 (config->mprq.stride_size_n &&
1407 non_scatter_min_mbuf_size <= mprq_stride_cap))) {
1408 /* TODO: Rx scatter isn't supported yet. */
1409 tmpl->rxq.sges_n = 0;
1410 /* Trim the number of descs needed. */
1411 desc >>= mprq_stride_nums;
1412 tmpl->rxq.strd_num_n = config->mprq.stride_num_n ?
1413 config->mprq.stride_num_n : mprq_stride_nums;
1414 tmpl->rxq.strd_sz_n = config->mprq.stride_size_n ?
1415 config->mprq.stride_size_n : mprq_stride_size;
1416 tmpl->rxq.strd_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT;
1417 tmpl->rxq.strd_scatter_en =
1418 !!(offloads & DEV_RX_OFFLOAD_SCATTER);
1419 tmpl->rxq.mprq_max_memcpy_len = RTE_MIN(first_mb_free_size,
1420 config->mprq.max_memcpy_len);
1421 max_lro_size = RTE_MIN(max_rx_pkt_len,
1422 (1u << tmpl->rxq.strd_num_n) *
1423 (1u << tmpl->rxq.strd_sz_n));
1425 "port %u Rx queue %u: Multi-Packet RQ is enabled"
1426 " strd_num_n = %u, strd_sz_n = %u",
1427 dev->data->port_id, idx,
1428 tmpl->rxq.strd_num_n, tmpl->rxq.strd_sz_n);
1429 } else if (max_rx_pkt_len <= first_mb_free_size) {
1430 tmpl->rxq.sges_n = 0;
1431 max_lro_size = max_rx_pkt_len;
1432 } else if (offloads & DEV_RX_OFFLOAD_SCATTER) {
1433 unsigned int size = non_scatter_min_mbuf_size;
1434 unsigned int sges_n;
1436 if (lro_on_queue && first_mb_free_size <
1437 MLX5_MAX_LRO_HEADER_FIX) {
1438 DRV_LOG(ERR, "Not enough space in the first segment(%u)"
1439 " to include the max header size(%u) for LRO",
1440 first_mb_free_size, MLX5_MAX_LRO_HEADER_FIX);
1441 rte_errno = ENOTSUP;
1445 * Determine the number of SGEs needed for a full packet
1446 * and round it to the next power of two.
1448 sges_n = log2above((size / mb_len) + !!(size % mb_len));
1449 if (sges_n > MLX5_MAX_LOG_RQ_SEGS) {
1451 "port %u too many SGEs (%u) needed to handle"
1452 " requested maximum packet size %u, the maximum"
1453 " supported are %u", dev->data->port_id,
1454 1 << sges_n, max_rx_pkt_len,
1455 1u << MLX5_MAX_LOG_RQ_SEGS);
1456 rte_errno = ENOTSUP;
1459 tmpl->rxq.sges_n = sges_n;
1460 max_lro_size = max_rx_pkt_len;
1462 if (config->mprq.enabled && !mlx5_rxq_mprq_enabled(&tmpl->rxq))
1464 "port %u MPRQ is requested but cannot be enabled\n"
1465 " (requested: pkt_sz = %u, desc_num = %u,"
1466 " rxq_num = %u, stride_sz = %u, stride_num = %u\n"
1467 " supported: min_rxqs_num = %u,"
1468 " min_stride_sz = %u, max_stride_sz = %u).",
1469 dev->data->port_id, non_scatter_min_mbuf_size,
1471 config->mprq.stride_size_n ?
1472 (1U << config->mprq.stride_size_n) :
1473 (1U << mprq_stride_size),
1474 config->mprq.stride_num_n ?
1475 (1U << config->mprq.stride_num_n) :
1476 (1U << mprq_stride_nums),
1477 config->mprq.min_rxqs_num,
1478 (1U << config->mprq.min_stride_size_n),
1479 (1U << config->mprq.max_stride_size_n));
1480 DRV_LOG(DEBUG, "port %u maximum number of segments per packet: %u",
1481 dev->data->port_id, 1 << tmpl->rxq.sges_n);
1482 if (desc % (1 << tmpl->rxq.sges_n)) {
1484 "port %u number of Rx queue descriptors (%u) is not a"
1485 " multiple of SGEs per packet (%u)",
1488 1 << tmpl->rxq.sges_n);
1492 mlx5_max_lro_msg_size_adjust(dev, idx, max_lro_size);
1493 /* Toggle RX checksum offload if hardware supports it. */
1494 tmpl->rxq.csum = !!(offloads & DEV_RX_OFFLOAD_CHECKSUM);
1495 tmpl->rxq.hw_timestamp = !!(offloads & DEV_RX_OFFLOAD_TIMESTAMP);
1496 /* Configure VLAN stripping. */
1497 tmpl->rxq.vlan_strip = !!(offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
1498 /* By default, FCS (CRC) is stripped by hardware. */
1499 tmpl->rxq.crc_present = 0;
1500 tmpl->rxq.lro = lro_on_queue;
1501 if (offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
1502 if (config->hw_fcs_strip) {
1504 * RQs used for LRO-enabled TIRs should not be
1505 * configured to scatter the FCS.
1509 "port %u CRC stripping has been "
1510 "disabled but will still be performed "
1511 "by hardware, because LRO is enabled",
1512 dev->data->port_id);
1514 tmpl->rxq.crc_present = 1;
1517 "port %u CRC stripping has been disabled but will"
1518 " still be performed by hardware, make sure MLNX_OFED"
1519 " and firmware are up to date",
1520 dev->data->port_id);
1524 "port %u CRC stripping is %s, %u bytes will be subtracted from"
1525 " incoming frames to hide it",
1527 tmpl->rxq.crc_present ? "disabled" : "enabled",
1528 tmpl->rxq.crc_present << 2);
1530 tmpl->rxq.rss_hash = !!priv->rss_conf.rss_hf &&
1531 (!!(dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS));
1532 tmpl->rxq.port_id = dev->data->port_id;
1535 tmpl->rxq.elts_n = log2above(desc);
1536 tmpl->rxq.rq_repl_thresh =
1537 MLX5_VPMD_RXQ_RPLNSH_THRESH(1 << tmpl->rxq.elts_n);
1539 (struct rte_mbuf *(*)[1 << tmpl->rxq.elts_n])(tmpl + 1);
1541 tmpl->rxq.uar_lock_cq = &priv->sh->uar_lock_cq;
1543 tmpl->rxq.idx = idx;
1544 rte_atomic32_inc(&tmpl->refcnt);
1545 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1553 * Create a DPDK Rx hairpin queue.
1556 * Pointer to Ethernet device.
1560 * Number of descriptors to configure in queue.
1561 * @param hairpin_conf
1562 * The hairpin binding configuration.
1565 * A DPDK queue object on success, NULL otherwise and rte_errno is set.
1567 struct mlx5_rxq_ctrl *
1568 mlx5_rxq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1569 const struct rte_eth_hairpin_conf *hairpin_conf)
1571 struct mlx5_priv *priv = dev->data->dev_private;
1572 struct mlx5_rxq_ctrl *tmpl;
1574 tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
1580 tmpl->type = MLX5_RXQ_TYPE_HAIRPIN;
1581 tmpl->socket = SOCKET_ID_ANY;
1582 tmpl->rxq.rss_hash = 0;
1583 tmpl->rxq.port_id = dev->data->port_id;
1585 tmpl->rxq.mp = NULL;
1586 tmpl->rxq.elts_n = log2above(desc);
1587 tmpl->rxq.elts = NULL;
1588 tmpl->rxq.mr_ctrl.cache_bh = (struct mlx5_mr_btree) { 0 };
1589 tmpl->hairpin_conf = *hairpin_conf;
1590 tmpl->rxq.idx = idx;
1591 rte_atomic32_inc(&tmpl->refcnt);
1592 LIST_INSERT_HEAD(&priv->rxqsctrl, tmpl, next);
1600 * Pointer to Ethernet device.
1605 * A pointer to the queue if it exists, NULL otherwise.
1607 struct mlx5_rxq_ctrl *
1608 mlx5_rxq_get(struct rte_eth_dev *dev, uint16_t idx)
1610 struct mlx5_priv *priv = dev->data->dev_private;
1611 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
1612 struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1615 rxq_ctrl = container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
1616 rte_atomic32_inc(&rxq_ctrl->refcnt);
1622 * Release a Rx queue.
1625 * Pointer to Ethernet device.
1630 * 1 while a reference on it exists, 0 when freed.
1633 mlx5_rxq_release(struct rte_eth_dev *dev, uint16_t idx)
1635 struct mlx5_priv *priv = dev->data->dev_private;
1636 struct mlx5_rxq_ctrl *rxq_ctrl;
1638 if (!(*priv->rxqs)[idx])
1640 rxq_ctrl = container_of((*priv->rxqs)[idx], struct mlx5_rxq_ctrl, rxq);
1641 if (!rte_atomic32_dec_and_test(&rxq_ctrl->refcnt))
1643 if (rxq_ctrl->obj) {
1644 priv->obj_ops.rxq_obj_release(rxq_ctrl->obj);
1645 LIST_REMOVE(rxq_ctrl->obj, next);
1646 mlx5_free(rxq_ctrl->obj);
1647 rxq_ctrl->obj = NULL;
1649 if (rxq_ctrl->type == MLX5_RXQ_TYPE_STANDARD) {
1650 mlx5_mr_btree_free(&rxq_ctrl->rxq.mr_ctrl.cache_bh);
1651 rxq_free_elts(rxq_ctrl);
1653 LIST_REMOVE(rxq_ctrl, next);
1654 mlx5_free(rxq_ctrl);
1655 (*priv->rxqs)[idx] = NULL;
1660 * Verify the Rx Queue list is empty
1663 * Pointer to Ethernet device.
1666 * The number of object not released.
1669 mlx5_rxq_verify(struct rte_eth_dev *dev)
1671 struct mlx5_priv *priv = dev->data->dev_private;
1672 struct mlx5_rxq_ctrl *rxq_ctrl;
1675 LIST_FOREACH(rxq_ctrl, &priv->rxqsctrl, next) {
1676 DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
1677 dev->data->port_id, rxq_ctrl->rxq.idx);
1684 * Get a Rx queue type.
1687 * Pointer to Ethernet device.
1692 * The Rx queue type.
1695 mlx5_rxq_get_type(struct rte_eth_dev *dev, uint16_t idx)
1697 struct mlx5_priv *priv = dev->data->dev_private;
1698 struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1700 if (idx < priv->rxqs_n && (*priv->rxqs)[idx]) {
1701 rxq_ctrl = container_of((*priv->rxqs)[idx],
1702 struct mlx5_rxq_ctrl,
1704 return rxq_ctrl->type;
1706 return MLX5_RXQ_TYPE_UNDEFINED;
1710 * Get an indirection table.
1713 * Pointer to Ethernet device.
1715 * Queues entering in the indirection table.
1717 * Number of queues in the array.
1720 * An indirection table if found.
1722 struct mlx5_ind_table_obj *
1723 mlx5_ind_table_obj_get(struct rte_eth_dev *dev, const uint16_t *queues,
1726 struct mlx5_priv *priv = dev->data->dev_private;
1727 struct mlx5_ind_table_obj *ind_tbl;
1729 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1730 if ((ind_tbl->queues_n == queues_n) &&
1731 (memcmp(ind_tbl->queues, queues,
1732 ind_tbl->queues_n * sizeof(ind_tbl->queues[0]))
1739 rte_atomic32_inc(&ind_tbl->refcnt);
1740 for (i = 0; i != ind_tbl->queues_n; ++i)
1741 mlx5_rxq_get(dev, ind_tbl->queues[i]);
1747 * Release an indirection table.
1750 * Pointer to Ethernet device.
1752 * Indirection table to release.
1755 * 1 while a reference on it exists, 0 when freed.
1758 mlx5_ind_table_obj_release(struct rte_eth_dev *dev,
1759 struct mlx5_ind_table_obj *ind_tbl)
1761 struct mlx5_priv *priv = dev->data->dev_private;
1764 if (rte_atomic32_dec_and_test(&ind_tbl->refcnt))
1765 priv->obj_ops.ind_table_destroy(ind_tbl);
1766 for (i = 0; i != ind_tbl->queues_n; ++i)
1767 claim_nonzero(mlx5_rxq_release(dev, ind_tbl->queues[i]));
1768 if (!rte_atomic32_read(&ind_tbl->refcnt)) {
1769 LIST_REMOVE(ind_tbl, next);
1777 * Verify the Rx Queue list is empty
1780 * Pointer to Ethernet device.
1783 * The number of object not released.
1786 mlx5_ind_table_obj_verify(struct rte_eth_dev *dev)
1788 struct mlx5_priv *priv = dev->data->dev_private;
1789 struct mlx5_ind_table_obj *ind_tbl;
1792 LIST_FOREACH(ind_tbl, &priv->ind_tbls, next) {
1794 "port %u indirection table obj %p still referenced",
1795 dev->data->port_id, (void *)ind_tbl);
1802 * Create an indirection table.
1805 * Pointer to Ethernet device.
1807 * Queues entering in the indirection table.
1809 * Number of queues in the array.
1812 * The Verbs/DevX object initialized, NULL otherwise and rte_errno is set.
1814 static struct mlx5_ind_table_obj *
1815 mlx5_ind_table_obj_new(struct rte_eth_dev *dev, const uint16_t *queues,
1818 struct mlx5_priv *priv = dev->data->dev_private;
1819 struct mlx5_ind_table_obj *ind_tbl;
1820 const unsigned int n = rte_is_power_of_2(queues_n) ?
1821 log2above(queues_n) :
1822 log2above(priv->config.ind_table_max_size);
1826 ind_tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*ind_tbl) +
1827 queues_n * sizeof(uint16_t), 0, SOCKET_ID_ANY);
1832 ind_tbl->queues_n = queues_n;
1833 for (i = 0; i != queues_n; ++i) {
1834 struct mlx5_rxq_ctrl *rxq = mlx5_rxq_get(dev, queues[i]);
1837 ind_tbl->queues[i] = queues[i];
1839 ret = priv->obj_ops.ind_table_new(dev, n, ind_tbl);
1842 rte_atomic32_inc(&ind_tbl->refcnt);
1843 LIST_INSERT_HEAD(&priv->ind_tbls, ind_tbl, next);
1847 for (j = 0; j < i; j++)
1848 mlx5_rxq_release(dev, ind_tbl->queues[j]);
1851 DEBUG("Port %u cannot create indirection table.", dev->data->port_id);
1856 * Get an Rx Hash queue.
1859 * Pointer to Ethernet device.
1861 * RSS configuration for the Rx hash queue.
1863 * Queues entering in hash queue. In case of empty hash_fields only the
1864 * first queue index will be taken for the indirection table.
1869 * An hash Rx queue index on success.
1872 mlx5_hrxq_get(struct rte_eth_dev *dev,
1873 const uint8_t *rss_key, uint32_t rss_key_len,
1874 uint64_t hash_fields,
1875 const uint16_t *queues, uint32_t queues_n)
1877 struct mlx5_priv *priv = dev->data->dev_private;
1878 struct mlx5_hrxq *hrxq;
1881 queues_n = hash_fields ? queues_n : 1;
1882 ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_HRXQ], priv->hrxqs, idx,
1884 struct mlx5_ind_table_obj *ind_tbl;
1886 if (hrxq->rss_key_len != rss_key_len)
1888 if (memcmp(hrxq->rss_key, rss_key, rss_key_len))
1890 if (hrxq->hash_fields != hash_fields)
1892 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
1895 if (ind_tbl != hrxq->ind_table) {
1896 mlx5_ind_table_obj_release(dev, ind_tbl);
1899 rte_atomic32_inc(&hrxq->refcnt);
1906 * Release the hash Rx queue.
1909 * Pointer to Ethernet device.
1911 * Index to Hash Rx queue to release.
1914 * 1 while a reference on it exists, 0 when freed.
1917 mlx5_hrxq_release(struct rte_eth_dev *dev, uint32_t hrxq_idx)
1919 struct mlx5_priv *priv = dev->data->dev_private;
1920 struct mlx5_hrxq *hrxq;
1922 hrxq = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
1925 if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
1926 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1927 mlx5_glue->destroy_flow_action(hrxq->action);
1929 priv->obj_ops.hrxq_destroy(hrxq);
1930 mlx5_ind_table_obj_release(dev, hrxq->ind_table);
1931 ILIST_REMOVE(priv->sh->ipool[MLX5_IPOOL_HRXQ], &priv->hrxqs,
1932 hrxq_idx, hrxq, next);
1933 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
1936 claim_nonzero(mlx5_ind_table_obj_release(dev, hrxq->ind_table));
1941 * Create an Rx Hash queue.
1944 * Pointer to Ethernet device.
1946 * RSS key for the Rx hash queue.
1947 * @param rss_key_len
1949 * @param hash_fields
1950 * Verbs protocol hash field to make the RSS on.
1952 * Queues entering in hash queue. In case of empty hash_fields only the
1953 * first queue index will be taken for the indirection table.
1960 * The DevX object initialized index, 0 otherwise and rte_errno is set.
1963 mlx5_hrxq_new(struct rte_eth_dev *dev,
1964 const uint8_t *rss_key, uint32_t rss_key_len,
1965 uint64_t hash_fields,
1966 const uint16_t *queues, uint32_t queues_n,
1967 int tunnel __rte_unused)
1969 struct mlx5_priv *priv = dev->data->dev_private;
1970 struct mlx5_hrxq *hrxq = NULL;
1971 uint32_t hrxq_idx = 0;
1972 struct mlx5_ind_table_obj *ind_tbl;
1975 queues_n = hash_fields ? queues_n : 1;
1976 ind_tbl = mlx5_ind_table_obj_get(dev, queues, queues_n);
1978 ind_tbl = mlx5_ind_table_obj_new(dev, queues, queues_n);
1983 hrxq = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_HRXQ], &hrxq_idx);
1986 hrxq->ind_table = ind_tbl;
1987 hrxq->rss_key_len = rss_key_len;
1988 hrxq->hash_fields = hash_fields;
1989 memcpy(hrxq->rss_key, rss_key, rss_key_len);
1990 ret = priv->obj_ops.hrxq_new(dev, hrxq, tunnel);
1995 rte_atomic32_inc(&hrxq->refcnt);
1996 ILIST_INSERT(priv->sh->ipool[MLX5_IPOOL_HRXQ], &priv->hrxqs, hrxq_idx,
2000 ret = rte_errno; /* Save rte_errno before cleanup. */
2001 mlx5_ind_table_obj_release(dev, ind_tbl);
2003 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_HRXQ], hrxq_idx);
2004 rte_errno = ret; /* Restore rte_errno. */
2009 * Create a drop Rx Hash queue.
2012 * Pointer to Ethernet device.
2015 * The Verbs/DevX object initialized, NULL otherwise and rte_errno is set.
2018 mlx5_drop_action_create(struct rte_eth_dev *dev)
2020 struct mlx5_priv *priv = dev->data->dev_private;
2021 struct mlx5_hrxq *hrxq = NULL;
2024 if (priv->drop_queue.hrxq) {
2025 rte_atomic32_inc(&priv->drop_queue.hrxq->refcnt);
2026 return priv->drop_queue.hrxq;
2028 hrxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq), 0, SOCKET_ID_ANY);
2031 "Port %u cannot allocate memory for drop queue.",
2032 dev->data->port_id);
2036 priv->drop_queue.hrxq = hrxq;
2037 hrxq->ind_table = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*hrxq->ind_table),
2039 if (!hrxq->ind_table) {
2043 ret = priv->obj_ops.drop_action_create(dev);
2046 rte_atomic32_set(&hrxq->refcnt, 1);
2050 if (hrxq->ind_table)
2051 mlx5_free(hrxq->ind_table);
2052 priv->drop_queue.hrxq = NULL;
2059 * Release a drop hash Rx queue.
2062 * Pointer to Ethernet device.
2065 mlx5_drop_action_destroy(struct rte_eth_dev *dev)
2067 struct mlx5_priv *priv = dev->data->dev_private;
2068 struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
2070 if (rte_atomic32_dec_and_test(&hrxq->refcnt)) {
2071 priv->obj_ops.drop_action_destroy(dev);
2072 mlx5_free(priv->drop_queue.rxq);
2073 mlx5_free(hrxq->ind_table);
2075 priv->drop_queue.rxq = NULL;
2076 priv->drop_queue.hrxq = NULL;
2081 * Verify the Rx Queue list is empty
2084 * Pointer to Ethernet device.
2087 * The number of object not released.
2090 mlx5_hrxq_verify(struct rte_eth_dev *dev)
2092 struct mlx5_priv *priv = dev->data->dev_private;
2093 struct mlx5_hrxq *hrxq;
2097 ILIST_FOREACH(priv->sh->ipool[MLX5_IPOOL_HRXQ], priv->hrxqs, idx,
2100 "port %u hash Rx queue %p still referenced",
2101 dev->data->port_id, (void *)hrxq);
2108 * Set the Rx queue timestamp conversion parameters
2111 * Pointer to the Ethernet device structure.
2114 mlx5_rxq_timestamp_set(struct rte_eth_dev *dev)
2116 struct mlx5_priv *priv = dev->data->dev_private;
2117 struct mlx5_dev_ctx_shared *sh = priv->sh;
2118 struct mlx5_rxq_data *data;
2121 for (i = 0; i != priv->rxqs_n; ++i) {
2122 if (!(*priv->rxqs)[i])
2124 data = (*priv->rxqs)[i];
2126 data->rt_timestamp = priv->config.rt_timestamp;