4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Rx queues configuration for mlx4 driver.
45 /* Verbs headers do not support -pedantic. */
47 #pragma GCC diagnostic ignored "-Wpedantic"
49 #include <infiniband/mlx4dv.h>
50 #include <infiniband/verbs.h>
52 #pragma GCC diagnostic error "-Wpedantic"
55 #include <rte_byteorder.h>
56 #include <rte_common.h>
57 #include <rte_errno.h>
58 #include <rte_ethdev.h>
60 #include <rte_malloc.h>
62 #include <rte_mempool.h>
65 #include "mlx4_flow.h"
66 #include "mlx4_rxtx.h"
67 #include "mlx4_utils.h"
70 * Historical RSS hash key.
72 * This used to be the default for mlx4 in Linux before v3.19 switched to
73 * generating random hash keys through netdev_rss_key_fill().
75 * It is used in this PMD for consistency with past DPDK releases but can
76 * now be overridden through user configuration.
78 * Note: this is not const to work around API quirks.
81 mlx4_rss_hash_key_default[MLX4_RSS_HASH_KEY_SIZE] = {
82 0x2c, 0xc6, 0x81, 0xd1,
83 0x5b, 0xdb, 0xf4, 0xf7,
84 0xfc, 0xa2, 0x83, 0x19,
85 0xdb, 0x1a, 0x3e, 0x94,
86 0x6b, 0x9e, 0x38, 0xd9,
87 0x2c, 0x9c, 0x03, 0xd1,
88 0xad, 0x99, 0x44, 0xa7,
89 0xd9, 0x56, 0x3d, 0x59,
90 0x06, 0x3c, 0x25, 0xf3,
91 0xfc, 0x1f, 0xdc, 0x2a,
95 * Obtain a RSS context with specified properties.
97 * Used when creating a flow rule targeting one or several Rx queues.
99 * If a matching RSS context already exists, it is returned with its
100 * reference count incremented.
103 * Pointer to private structure.
105 * Fields for RSS processing (Verbs format).
107 * Hash key to use (whose size is exactly MLX4_RSS_HASH_KEY_SIZE).
109 * Number of target queues.
110 * @param[in] queue_id
114 * Pointer to RSS context on success, NULL otherwise and rte_errno is set.
117 mlx4_rss_get(struct priv *priv, uint64_t fields,
118 uint8_t key[MLX4_RSS_HASH_KEY_SIZE],
119 uint16_t queues, const uint16_t queue_id[])
121 struct mlx4_rss *rss;
122 size_t queue_id_size = sizeof(queue_id[0]) * queues;
124 LIST_FOREACH(rss, &priv->rss, next)
125 if (fields == rss->fields &&
126 queues == rss->queues &&
127 !memcmp(key, rss->key, MLX4_RSS_HASH_KEY_SIZE) &&
128 !memcmp(queue_id, rss->queue_id, queue_id_size)) {
132 rss = rte_malloc(__func__, offsetof(struct mlx4_rss, queue_id) +
136 *rss = (struct mlx4_rss){
145 memcpy(rss->key, key, MLX4_RSS_HASH_KEY_SIZE);
146 memcpy(rss->queue_id, queue_id, queue_id_size);
147 LIST_INSERT_HEAD(&priv->rss, rss, next);
155 * Release a RSS context instance.
157 * Used when destroying a flow rule targeting one or several Rx queues.
159 * This function decrements the reference count of the context and destroys
160 * it after reaching 0. The context must have no users at this point; all
161 * prior calls to mlx4_rss_attach() must have been followed by matching
162 * calls to mlx4_rss_detach().
165 * RSS context to release.
168 mlx4_rss_put(struct mlx4_rss *rss)
173 assert(!rss->usecnt);
176 LIST_REMOVE(rss, next);
181 * Attach a user to a RSS context instance.
183 * Used when the RSS QP and indirection table objects must be instantiated,
184 * that is, when a flow rule must be enabled.
186 * This function increments the usage count of the context.
189 * RSS context to attach to.
192 * 0 on success, a negative errno value otherwise and rte_errno is set.
195 mlx4_rss_attach(struct mlx4_rss *rss)
204 struct ibv_wq *ind_tbl[rss->queues];
205 struct priv *priv = rss->priv;
210 if (!rte_is_power_of_2(RTE_DIM(ind_tbl))) {
212 msg = "number of RSS queues must be a power of two";
215 for (i = 0; i != RTE_DIM(ind_tbl); ++i) {
216 uint16_t id = rss->queue_id[i];
217 struct rxq *rxq = NULL;
219 if (id < priv->dev->data->nb_rx_queues)
220 rxq = priv->dev->data->rx_queues[id];
223 msg = "RSS target queue is not configured";
226 ret = mlx4_rxq_attach(rxq);
229 msg = "unable to attach RSS target queue";
232 ind_tbl[i] = rxq->wq;
234 rss->ind = ibv_create_rwq_ind_table
236 &(struct ibv_rwq_ind_table_init_attr){
237 .log_ind_tbl_size = rte_log2_u32(RTE_DIM(ind_tbl)),
242 ret = errno ? errno : EINVAL;
243 msg = "RSS indirection table creation failure";
246 rss->qp = ibv_create_qp_ex
248 &(struct ibv_qp_init_attr_ex){
249 .comp_mask = (IBV_QP_INIT_ATTR_PD |
250 IBV_QP_INIT_ATTR_RX_HASH |
251 IBV_QP_INIT_ATTR_IND_TABLE),
252 .qp_type = IBV_QPT_RAW_PACKET,
254 .rwq_ind_tbl = rss->ind,
256 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
257 .rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
258 .rx_hash_key = rss->key,
259 .rx_hash_fields_mask = rss->fields,
263 ret = errno ? errno : EINVAL;
264 msg = "RSS hash QP creation failure";
269 &(struct ibv_qp_attr){
270 .qp_state = IBV_QPS_INIT,
271 .port_num = priv->port,
273 IBV_QP_STATE | IBV_QP_PORT);
275 msg = "failed to switch RSS hash QP to INIT state";
280 &(struct ibv_qp_attr){
281 .qp_state = IBV_QPS_RTR,
285 msg = "failed to switch RSS hash QP to RTR state";
291 claim_zero(ibv_destroy_qp(rss->qp));
295 claim_zero(ibv_destroy_rwq_ind_table(rss->ind));
299 mlx4_rxq_detach(priv->dev->data->rx_queues[rss->queue_id[i]]);
300 ERROR("mlx4: %s", msg);
307 * Detach a user from a RSS context instance.
309 * Used when disabling (not destroying) a flow rule.
311 * This function decrements the usage count of the context and destroys
312 * usage resources after reaching 0.
315 * RSS context to detach from.
318 mlx4_rss_detach(struct mlx4_rss *rss)
320 struct priv *priv = rss->priv;
328 claim_zero(ibv_destroy_qp(rss->qp));
330 claim_zero(ibv_destroy_rwq_ind_table(rss->ind));
332 for (i = 0; i != rss->queues; ++i)
333 mlx4_rxq_detach(priv->dev->data->rx_queues[rss->queue_id[i]]);
337 * Initialize common RSS context resources.
339 * Because ConnectX-3 hardware limitations require a fixed order in the
340 * indirection table, WQs must be allocated sequentially to be part of a
341 * common RSS context.
343 * Since a newly created WQ cannot be moved to a different context, this
344 * function allocates them all at once, one for each configured Rx queue,
345 * as well as all related resources (CQs and mbufs).
347 * This must therefore be done before creating any Rx flow rules relying on
348 * indirection tables.
351 * Pointer to private structure.
354 * 0 on success, a negative errno value otherwise and rte_errno is set.
357 mlx4_rss_init(struct priv *priv)
359 struct rte_eth_dev *dev = priv->dev;
360 uint8_t log2_range = rte_log2_u32(dev->data->nb_rx_queues);
361 uint32_t wq_num_prev = 0;
366 /* Prepare range for RSS contexts before creating the first WQ. */
367 ret = mlx4dv_set_context_attr(priv->ctx,
368 MLX4DV_SET_CTX_ATTR_LOG_WQS_RANGE_SZ,
371 ERROR("cannot set up range size for RSS context to %u"
372 " (for %u Rx queues), error: %s",
373 1 << log2_range, dev->data->nb_rx_queues, strerror(ret));
377 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
378 struct rxq *rxq = priv->dev->data->rx_queues[i];
383 /* Attach the configured Rx queues. */
385 assert(!rxq->usecnt);
386 ret = mlx4_rxq_attach(rxq);
388 wq_num = rxq->wq->wq_num;
392 msg = "unable to create Rx queue resources";
396 * WQs are temporarily allocated for unconfigured Rx queues
397 * to maintain proper index alignment in indirection table
398 * by skipping unused WQ numbers.
400 * The reason this works at all even though these WQs are
401 * immediately destroyed is that WQNs are allocated
402 * sequentially and are guaranteed to never be reused in the
403 * same context by the underlying implementation.
405 cq = ibv_create_cq(priv->ctx, 1, NULL, NULL, 0);
408 msg = "placeholder CQ creation failure";
413 &(struct ibv_wq_init_attr){
414 .wq_type = IBV_WQT_RQ,
422 claim_zero(ibv_destroy_wq(wq));
424 wq_num = 0; /* Shut up GCC 4.8 warnings. */
426 claim_zero(ibv_destroy_cq(cq));
429 msg = "placeholder WQ creation failure";
434 * While guaranteed by the implementation, make sure WQ
435 * numbers are really sequential (as the saying goes,
436 * trust, but verify).
438 if (i && wq_num - wq_num_prev != 1) {
440 mlx4_rxq_detach(rxq);
442 msg = "WQ numbers are not sequential";
445 wq_num_prev = wq_num;
449 ERROR("cannot initialize common RSS resources (queue %u): %s: %s",
450 i, msg, strerror(ret));
452 struct rxq *rxq = priv->dev->data->rx_queues[i];
455 mlx4_rxq_detach(rxq);
462 * Release common RSS context resources.
464 * As the reverse of mlx4_rss_init(), this must be done after removing all
465 * flow rules relying on indirection tables.
468 * Pointer to private structure.
471 mlx4_rss_deinit(struct priv *priv)
475 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i) {
476 struct rxq *rxq = priv->dev->data->rx_queues[i];
479 assert(rxq->usecnt == 1);
480 mlx4_rxq_detach(rxq);
486 * Attach a user to a Rx queue.
488 * Used when the resources of an Rx queue must be instantiated for it to
489 * become in a usable state.
491 * This function increments the usage count of the Rx queue.
494 * Pointer to Rx queue structure.
497 * 0 on success, negative errno value otherwise and rte_errno is set.
500 mlx4_rxq_attach(struct rxq *rxq)
510 struct priv *priv = rxq->priv;
511 const uint32_t elts_n = 1 << rxq->elts_n;
512 const uint32_t sges_n = 1 << rxq->sges_n;
513 struct rte_mbuf *(*elts)[elts_n] = rxq->elts;
514 struct mlx4dv_obj mlxdv;
515 struct mlx4dv_rwq dv_rwq;
516 struct mlx4dv_cq dv_cq = { .comp_mask = MLX4DV_CQ_MASK_UAR, };
518 struct ibv_cq *cq = NULL;
519 struct ibv_wq *wq = NULL;
520 volatile struct mlx4_wqe_data_seg (*wqes)[];
524 assert(rte_is_power_of_2(elts_n));
525 cq = ibv_create_cq(priv->ctx, elts_n / sges_n, NULL, rxq->channel, 0);
528 msg = "CQ creation failure";
533 &(struct ibv_wq_init_attr){
534 .wq_type = IBV_WQT_RQ,
535 .max_wr = elts_n / sges_n,
541 ret = errno ? errno : EINVAL;
542 msg = "WQ creation failure";
547 &(struct ibv_wq_attr){
548 .attr_mask = IBV_WQ_ATTR_STATE,
549 .wq_state = IBV_WQS_RDY,
552 msg = "WQ state change to IBV_WQS_RDY failed";
555 /* Retrieve device queue information. */
557 mlxdv.cq.out = &dv_cq;
559 mlxdv.rwq.out = &dv_rwq;
560 ret = mlx4dv_init_obj(&mlxdv, MLX4DV_OBJ_RWQ | MLX4DV_OBJ_CQ);
562 msg = "failed to obtain device information from WQ/CQ objects";
565 wqes = (volatile struct mlx4_wqe_data_seg (*)[])
566 ((uintptr_t)dv_rwq.buf.buf + dv_rwq.rq.offset);
567 for (i = 0; i != RTE_DIM(*elts); ++i) {
568 volatile struct mlx4_wqe_data_seg *scat = &(*wqes)[i];
569 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
573 rte_pktmbuf_free_seg((*elts)[i]);
577 msg = "cannot allocate mbuf";
580 /* Headroom is reserved by rte_pktmbuf_alloc(). */
581 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
582 /* Buffer is supposed to be empty. */
583 assert(rte_pktmbuf_data_len(buf) == 0);
584 assert(rte_pktmbuf_pkt_len(buf) == 0);
585 /* Only the first segment keeps headroom. */
588 buf->port = rxq->port_id;
589 buf->data_len = rte_pktmbuf_tailroom(buf);
590 buf->pkt_len = rte_pktmbuf_tailroom(buf);
592 *scat = (struct mlx4_wqe_data_seg){
593 .addr = rte_cpu_to_be_64(rte_pktmbuf_mtod(buf,
595 .byte_count = rte_cpu_to_be_32(buf->data_len),
596 .lkey = rte_cpu_to_be_32(rxq->mr->lkey),
600 DEBUG("%p: allocated and configured %u segments (max %u packets)",
601 (void *)rxq, elts_n, elts_n / sges_n);
605 rxq->rq_db = dv_rwq.rdb;
606 rxq->mcq.buf = dv_cq.buf.buf;
607 rxq->mcq.cqe_cnt = dv_cq.cqe_cnt;
608 rxq->mcq.set_ci_db = dv_cq.set_ci_db;
609 rxq->mcq.cqe_64 = (dv_cq.cqe_size & 64) ? 1 : 0;
610 rxq->mcq.arm_db = dv_cq.arm_db;
611 rxq->mcq.arm_sn = dv_cq.arm_sn;
612 rxq->mcq.cqn = dv_cq.cqn;
613 rxq->mcq.cq_uar = dv_cq.cq_uar;
614 rxq->mcq.cq_db_reg = (uint8_t *)dv_cq.cq_uar + MLX4_CQ_DOORBELL;
615 /* Update doorbell counter. */
616 rxq->rq_ci = elts_n / sges_n;
618 *rxq->rq_db = rte_cpu_to_be_32(rxq->rq_ci);
622 claim_zero(ibv_destroy_wq(wq));
624 claim_zero(ibv_destroy_cq(cq));
626 ERROR("error while attaching Rx queue %p: %s: %s",
627 (void *)rxq, msg, strerror(ret));
632 * Detach a user from a Rx queue.
634 * This function decrements the usage count of the Rx queue and destroys
635 * usage resources after reaching 0.
638 * Pointer to Rx queue structure.
641 mlx4_rxq_detach(struct rxq *rxq)
644 struct rte_mbuf *(*elts)[1 << rxq->elts_n] = rxq->elts;
649 memset(&rxq->mcq, 0, sizeof(rxq->mcq));
652 claim_zero(ibv_destroy_wq(rxq->wq));
654 claim_zero(ibv_destroy_cq(rxq->cq));
656 DEBUG("%p: freeing Rx queue elements", (void *)rxq);
657 for (i = 0; (i != RTE_DIM(*elts)); ++i) {
660 rte_pktmbuf_free_seg((*elts)[i]);
666 * DPDK callback to configure a Rx queue.
669 * Pointer to Ethernet device structure.
673 * Number of descriptors to configure in queue.
675 * NUMA socket on which memory must be allocated.
677 * Thresholds parameters.
679 * Memory pool for buffer allocations.
682 * 0 on success, negative errno value otherwise and rte_errno is set.
685 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
686 unsigned int socket, const struct rte_eth_rxconf *conf,
687 struct rte_mempool *mp)
689 struct priv *priv = dev->data->dev_private;
690 uint32_t mb_len = rte_pktmbuf_data_room_size(mp);
691 struct rte_mbuf *(*elts)[rte_align32pow2(desc)];
693 struct mlx4_malloc_vec vec[] = {
695 .align = RTE_CACHE_LINE_SIZE,
696 .size = sizeof(*rxq),
697 .addr = (void **)&rxq,
700 .align = RTE_CACHE_LINE_SIZE,
701 .size = sizeof(*elts),
702 .addr = (void **)&elts,
707 (void)conf; /* Thresholds configuration (ignored). */
708 DEBUG("%p: configuring queue %u for %u descriptors",
709 (void *)dev, idx, desc);
710 if (idx >= dev->data->nb_rx_queues) {
711 rte_errno = EOVERFLOW;
712 ERROR("%p: queue index out of range (%u >= %u)",
713 (void *)dev, idx, dev->data->nb_rx_queues);
716 rxq = dev->data->rx_queues[idx];
719 ERROR("%p: Rx queue %u already configured, release it first",
725 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
728 if (desc != RTE_DIM(*elts)) {
729 desc = RTE_DIM(*elts);
730 WARN("%p: increased number of descriptors in Rx queue %u"
731 " to the next power of two (%u)",
732 (void *)dev, idx, desc);
734 /* Allocate and initialize Rx queue. */
735 mlx4_zmallocv_socket("RXQ", vec, RTE_DIM(vec), socket);
737 ERROR("%p: unable to allocate queue index %u",
744 .port_id = dev->data->port_id,
746 .elts_n = rte_log2_u32(desc),
748 /* Toggle Rx checksum offload if hardware supports it. */
749 .csum = (priv->hw_csum &&
750 dev->data->dev_conf.rxmode.hw_ip_checksum),
751 .csum_l2tun = (priv->hw_csum_l2tun &&
752 dev->data->dev_conf.rxmode.hw_ip_checksum),
753 .l2tun_offload = priv->hw_csum_l2tun,
759 /* Enable scattered packets support for this queue if necessary. */
760 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
761 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
762 (mb_len - RTE_PKTMBUF_HEADROOM)) {
764 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
766 RTE_PKTMBUF_HEADROOM +
767 dev->data->dev_conf.rxmode.max_rx_pkt_len;
771 * Determine the number of SGEs needed for a full packet
772 * and round it to the next power of two.
774 sges_n = rte_log2_u32((size / mb_len) + !!(size % mb_len));
775 rxq->sges_n = sges_n;
776 /* Make sure sges_n did not overflow. */
777 size = mb_len * (1 << rxq->sges_n);
778 size -= RTE_PKTMBUF_HEADROOM;
779 if (size < dev->data->dev_conf.rxmode.max_rx_pkt_len) {
780 rte_errno = EOVERFLOW;
781 ERROR("%p: too many SGEs (%u) needed to handle"
782 " requested maximum packet size %u",
785 dev->data->dev_conf.rxmode.max_rx_pkt_len);
789 WARN("%p: the requested maximum Rx packet size (%u) is"
790 " larger than a single mbuf (%u) and scattered"
791 " mode has not been requested",
793 dev->data->dev_conf.rxmode.max_rx_pkt_len,
794 mb_len - RTE_PKTMBUF_HEADROOM);
796 DEBUG("%p: maximum number of segments per packet: %u",
797 (void *)dev, 1 << rxq->sges_n);
798 if (desc % (1 << rxq->sges_n)) {
800 ERROR("%p: number of Rx queue descriptors (%u) is not a"
801 " multiple of maximum segments per packet (%u)",
807 /* Use the entire Rx mempool as the memory region. */
808 rxq->mr = mlx4_mr_get(priv, mp);
810 ERROR("%p: MR creation failure: %s",
811 (void *)dev, strerror(rte_errno));
814 if (dev->data->dev_conf.intr_conf.rxq) {
815 rxq->channel = ibv_create_comp_channel(priv->ctx);
816 if (rxq->channel == NULL) {
818 ERROR("%p: Rx interrupt completion channel creation"
820 (void *)dev, strerror(rte_errno));
823 if (mlx4_fd_set_non_blocking(rxq->channel->fd) < 0) {
824 ERROR("%p: unable to make Rx interrupt completion"
825 " channel non-blocking: %s",
826 (void *)dev, strerror(rte_errno));
830 DEBUG("%p: adding Rx queue %p to list", (void *)dev, (void *)rxq);
831 dev->data->rx_queues[idx] = rxq;
834 dev->data->rx_queues[idx] = NULL;
836 mlx4_rx_queue_release(rxq);
838 assert(rte_errno > 0);
843 * DPDK callback to release a Rx queue.
846 * Generic Rx queue pointer.
849 mlx4_rx_queue_release(void *dpdk_rxq)
851 struct rxq *rxq = (struct rxq *)dpdk_rxq;
858 for (i = 0; i != priv->dev->data->nb_rx_queues; ++i)
859 if (priv->dev->data->rx_queues[i] == rxq) {
860 DEBUG("%p: removing Rx queue %p from list",
861 (void *)priv->dev, (void *)rxq);
862 priv->dev->data->rx_queues[i] = NULL;
870 claim_zero(ibv_destroy_comp_channel(rxq->channel));
872 mlx4_mr_put(rxq->mr);