vdpa/mlx5: pre-create virtq at probing time
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa.c
index 76fa5d4..0ddc0bc 100644 (file)
@@ -14,6 +14,7 @@
 #include <rte_errno.h>
 #include <rte_string_fns.h>
 #include <rte_bus_pci.h>
+#include <rte_eal_paging.h>
 
 #include <mlx5_glue.h>
 #include <mlx5_common.h>
@@ -84,7 +85,7 @@ mlx5_vdpa_get_queue_num(struct rte_vdpa_device *vdev, uint32_t *queue_num)
                DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
                return -1;
        }
-       *queue_num = priv->caps.max_num_virtio_queues;
+       *queue_num = priv->caps.max_num_virtio_queues / 2;
        return 0;
 }
 
@@ -141,7 +142,7 @@ mlx5_vdpa_set_vring_state(int vid, int vring, int state)
                DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
                return -EINVAL;
        }
-       if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
+       if (vring >= (int)priv->caps.max_num_virtio_queues) {
                DRV_LOG(ERR, "Too big vring id: %d.", vring);
                return -E2BIG;
        }
@@ -244,7 +245,9 @@ mlx5_vdpa_mtu_set(struct mlx5_vdpa_priv *priv)
 static void
 mlx5_vdpa_dev_cache_clean(struct mlx5_vdpa_priv *priv)
 {
-       mlx5_vdpa_virtqs_cleanup(priv);
+       /* Clean pre-created resource in dev removal only. */
+       if (!priv->queues)
+               mlx5_vdpa_virtqs_cleanup(priv);
        mlx5_vdpa_mem_dereg(priv);
 }
 
@@ -267,6 +270,7 @@ mlx5_vdpa_dev_close(int vid)
        }
        mlx5_vdpa_steer_unset(priv);
        mlx5_vdpa_virtqs_release(priv);
+       mlx5_vdpa_drain_cq(priv);
        if (priv->lm_mr.addr)
                mlx5_os_wrapped_mkey_destroy(&priv->lm_mr);
        priv->state = MLX5_VDPA_STATE_PROBED;
@@ -388,7 +392,7 @@ mlx5_vdpa_get_stats(struct rte_vdpa_device *vdev, int qid,
                DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
                return -ENODEV;
        }
-       if (qid >= (int)priv->caps.max_num_virtio_queues * 2) {
+       if (qid >= (int)priv->caps.max_num_virtio_queues) {
                DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
                                vdev->device->name);
                return -E2BIG;
@@ -411,7 +415,7 @@ mlx5_vdpa_reset_stats(struct rte_vdpa_device *vdev, int qid)
                DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
                return -ENODEV;
        }
-       if (qid >= (int)priv->caps.max_num_virtio_queues * 2) {
+       if (qid >= (int)priv->caps.max_num_virtio_queues) {
                DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
                                vdev->device->name);
                return -E2BIG;
@@ -494,6 +498,12 @@ mlx5_vdpa_args_check_handler(const char *key, const char *val, void *opaque)
                priv->hw_max_latency_us = (uint32_t)tmp;
        } else if (strcmp(key, "hw_max_pending_comp") == 0) {
                priv->hw_max_pending_comp = (uint32_t)tmp;
+       } else if (strcmp(key, "queue_size") == 0) {
+               priv->queue_size = (uint16_t)tmp;
+       } else if (strcmp(key, "queues") == 0) {
+               priv->queues = (uint16_t)tmp;
+       } else {
+               DRV_LOG(WARNING, "Invalid key %s.", key);
        }
        return 0;
 }
@@ -524,9 +534,75 @@ mlx5_vdpa_config_get(struct mlx5_kvargs_ctrl *mkvlist,
        if (!priv->event_us &&
            priv->event_mode == MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER)
                priv->event_us = MLX5_VDPA_DEFAULT_TIMER_STEP_US;
+       if ((priv->queue_size && !priv->queues) ||
+               (!priv->queue_size && priv->queues)) {
+               priv->queue_size = 0;
+               priv->queues = 0;
+               DRV_LOG(WARNING, "Please provide both queue_size and queues.");
+       }
        DRV_LOG(DEBUG, "event mode is %d.", priv->event_mode);
        DRV_LOG(DEBUG, "event_us is %u us.", priv->event_us);
        DRV_LOG(DEBUG, "no traffic max is %u.", priv->no_traffic_max);
+       DRV_LOG(DEBUG, "queues is %u, queue_size is %u.", priv->queues,
+               priv->queue_size);
+}
+
+static int
+mlx5_vdpa_virtq_resource_prepare(struct mlx5_vdpa_priv *priv)
+{
+       uint32_t index;
+       uint32_t i;
+
+       if (!priv->queues)
+               return 0;
+       for (index = 0; index < (priv->queues * 2); ++index) {
+               struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
+               int ret = mlx5_vdpa_event_qp_prepare(priv, priv->queue_size,
+                                       -1, &virtq->eqp);
+
+               if (ret) {
+                       DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
+                               index);
+                       return -1;
+               }
+               if (priv->caps.queue_counters_valid) {
+                       if (!virtq->counters)
+                               virtq->counters =
+                                       mlx5_devx_cmd_create_virtio_q_counters
+                                               (priv->cdev->ctx);
+                       if (!virtq->counters) {
+                               DRV_LOG(ERR, "Failed to create virtq couners for virtq"
+                                       " %d.", index);
+                               return -1;
+                       }
+               }
+               for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
+                       uint32_t size;
+                       void *buf;
+                       struct mlx5dv_devx_umem *obj;
+
+                       size = priv->caps.umems[i].a * priv->queue_size +
+                                       priv->caps.umems[i].b;
+                       buf = rte_zmalloc(__func__, size, 4096);
+                       if (buf == NULL) {
+                               DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
+                                               " %u.", i, index);
+                               return -1;
+                       }
+                       obj = mlx5_glue->devx_umem_reg(priv->cdev->ctx, buf,
+                                       size, IBV_ACCESS_LOCAL_WRITE);
+                       if (obj == NULL) {
+                               rte_free(buf);
+                               DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
+                                               i, index);
+                               return -1;
+                       }
+                       virtq->umems[i].size = size;
+                       virtq->umems[i].buf = buf;
+                       virtq->umems[i].obj = obj;
+               }
+       }
+       return 0;
 }
 
 static int
@@ -560,6 +636,9 @@ mlx5_vdpa_create_dev_resources(struct mlx5_vdpa_priv *priv)
                rte_errno = errno;
                return -rte_errno;
        }
+       /* Add within page offset for 64K page system. */
+       priv->virtq_db_addr = (char *)priv->virtq_db_addr +
+               ((rte_mem_page_size() - 1) & priv->caps.doorbell_bar_offset);
        DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
                priv->virtq_db_addr);
        priv->td = mlx5_devx_cmd_create_td(ctx);
@@ -604,6 +683,8 @@ mlx5_vdpa_create_dev_resources(struct mlx5_vdpa_priv *priv)
                return -rte_errno;
        if (mlx5_vdpa_event_qp_global_prepare(priv))
                return -rte_errno;
+       if (mlx5_vdpa_virtq_resource_prepare(priv))
+               return -rte_errno;
        return 0;
 }
 
@@ -624,7 +705,7 @@ mlx5_vdpa_dev_probe(struct mlx5_common_device *cdev,
                DRV_LOG(DEBUG, "No capability to support virtq statistics.");
        priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv) +
                           sizeof(struct mlx5_vdpa_virtq) *
-                          attr->vdpa.max_num_virtio_queues * 2,
+                          attr->vdpa.max_num_virtio_queues,
                           RTE_CACHE_LINE_SIZE);
        if (!priv) {
                DRV_LOG(ERR, "Failed to allocate private memory.");
@@ -638,6 +719,7 @@ mlx5_vdpa_dev_probe(struct mlx5_common_device *cdev,
                priv->num_lag_ports = 1;
        pthread_mutex_init(&priv->vq_config_lock, NULL);
        priv->cdev = cdev;
+       mlx5_vdpa_config_get(mkvlist, priv);
        if (mlx5_vdpa_create_dev_resources(priv))
                goto error;
        priv->vdev = rte_vdpa_register_device(cdev->dev, &mlx5_vdpa_ops);
@@ -646,7 +728,6 @@ mlx5_vdpa_dev_probe(struct mlx5_common_device *cdev,
                rte_errno = rte_errno ? rte_errno : EINVAL;
                goto error;
        }
-       mlx5_vdpa_config_get(mkvlist, priv);
        SLIST_INIT(&priv->mr_list);
        pthread_mutex_lock(&priv_list_lock);
        TAILQ_INSERT_TAIL(&priv_list, priv, next);
@@ -684,8 +765,10 @@ mlx5_vdpa_release_dev_resources(struct mlx5_vdpa_priv *priv)
 {
        uint32_t i;
 
+       if (priv->queues)
+               mlx5_vdpa_virtqs_cleanup(priv);
        mlx5_vdpa_dev_cache_clean(priv);
-       for (i = 0; i < priv->caps.max_num_virtio_queues * 2; i++) {
+       for (i = 0; i < priv->caps.max_num_virtio_queues; i++) {
                if (!priv->virtqs[i].counters)
                        continue;
                claim_zero(mlx5_devx_cmd_destroy(priv->virtqs[i].counters));
@@ -705,7 +788,9 @@ mlx5_vdpa_release_dev_resources(struct mlx5_vdpa_priv *priv)
        if (priv->td)
                claim_zero(mlx5_devx_cmd_destroy(priv->td));
        if (priv->virtq_db_addr)
-               claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
+               /* Mask out the within page offset for munmap. */
+               claim_zero(munmap((void *)((uintptr_t)priv->virtq_db_addr &
+                       ~(rte_mem_page_size() - 1)), priv->var->length));
        if (priv->var)
                mlx5_glue->dv_free_var(priv->var);
 }