vdpa/mlx5: validate notifier configuration
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa_virtq.c
index e27af28..e59c172 100644 (file)
@@ -2,9 +2,12 @@
  * Copyright 2019 Mellanox Technologies, Ltd
  */
 #include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
 
 #include <rte_malloc.h>
 #include <rte_errno.h>
+#include <rte_io.h>
 
 #include <mlx5_common.h>
 
 #include "mlx5_vdpa.h"
 
 
+static void
+mlx5_vdpa_virtq_handler(void *cb_arg)
+{
+       struct mlx5_vdpa_virtq *virtq = cb_arg;
+       struct mlx5_vdpa_priv *priv = virtq->priv;
+       uint64_t buf;
+       int nbytes;
+
+       do {
+               nbytes = read(virtq->intr_handle.fd, &buf, 8);
+               if (nbytes < 0) {
+                       if (errno == EINTR ||
+                           errno == EWOULDBLOCK ||
+                           errno == EAGAIN)
+                               continue;
+                       DRV_LOG(ERR,  "Failed to read kickfd of virtq %d: %s",
+                               virtq->index, strerror(errno));
+               }
+               break;
+       } while (1);
+       rte_write32(virtq->index, priv->virtq_db_addr);
+       DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index);
+}
+
 static int
 mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
 {
-       int i;
+       unsigned int i;
+       int retries = MLX5_VDPA_INTR_RETRIES;
+       int ret = -EAGAIN;
 
+       if (virtq->intr_handle.fd != -1) {
+               while (retries-- && ret == -EAGAIN) {
+                       ret = rte_intr_callback_unregister(&virtq->intr_handle,
+                                                       mlx5_vdpa_virtq_handler,
+                                                       virtq);
+                       if (ret == -EAGAIN) {
+                               DRV_LOG(DEBUG, "Try again to unregister fd %d "
+                                       "of virtq %d interrupt, retries = %d.",
+                                       virtq->intr_handle.fd,
+                                       (int)virtq->index, retries);
+                               usleep(MLX5_VDPA_INTR_RETRIES_USEC);
+                       }
+               }
+               virtq->intr_handle.fd = -1;
+       }
        if (virtq->virtq) {
                claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
                virtq->virtq = NULL;
        }
-       for (i = 0; i < 3; ++i) {
+       for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
                if (virtq->umems[i].obj)
                        claim_zero(mlx5_glue->devx_umem_dereg
                                                         (virtq->umems[i].obj));
@@ -57,9 +101,26 @@ mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
                claim_zero(mlx5_devx_cmd_destroy(priv->td));
                priv->td = NULL;
        }
+       if (priv->virtq_db_addr) {
+               claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
+               priv->virtq_db_addr = NULL;
+       }
        priv->features = 0;
 }
 
+int
+mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
+{
+       struct mlx5_devx_virtq_attr attr = {
+                       .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
+                       .state = state ? MLX5_VIRTQ_STATE_RDY :
+                                        MLX5_VIRTQ_STATE_SUSPEND,
+                       .queue_index = virtq->index,
+       };
+
+       return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
+}
+
 static uint64_t
 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
 {
@@ -86,7 +147,7 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv,
        struct mlx5_devx_virtq_attr attr = {0};
        uint64_t gpa;
        int ret;
-       int i;
+       unsigned int i;
        uint16_t last_avail_idx;
        uint16_t last_used_idx;
 
@@ -125,7 +186,7 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv,
                        " need event QPs and event mechanism.", index);
        }
        /* Setup 3 UMEMs for each virtq. */
-       for (i = 0; i < 3; ++i) {
+       for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
                virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
                                                          priv->caps.umems[i].b;
                virtq->umems[i].buf = rte_zmalloc(__func__,
@@ -171,10 +232,17 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv,
                }
                attr.available_addr = gpa;
        }
-       rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
+       ret = rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
                                 &last_used_idx);
-       DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
-               "virtq %d.", priv->vid, last_avail_idx, last_used_idx, index);
+       if (ret) {
+               last_avail_idx = 0;
+               last_used_idx = 0;
+               DRV_LOG(WARNING, "Couldn't get vring base, idx are set to 0");
+       } else {
+               DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
+                               "virtq %d.", priv->vid, last_avail_idx,
+                               last_used_idx, index);
+       }
        attr.hw_available_index = last_avail_idx;
        attr.hw_used_index = last_used_idx;
        attr.q_size = vq.size;
@@ -182,8 +250,38 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv,
        attr.tis_id = priv->tis->id;
        attr.queue_index = index;
        virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
+       virtq->priv = priv;
        if (!virtq->virtq)
                goto error;
+       if (mlx5_vdpa_virtq_modify(virtq, 1))
+               goto error;
+       virtq->enable = 1;
+       virtq->priv = priv;
+       /* Be sure notifications are not missed during configuration. */
+       claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
+       rte_write32(virtq->index, priv->virtq_db_addr);
+       /* Setup doorbell mapping. */
+       virtq->intr_handle.fd = vq.kickfd;
+       if (virtq->intr_handle.fd == -1) {
+               DRV_LOG(WARNING, "Virtq %d kickfd is invalid.", index);
+               if (!priv->direct_notifier) {
+                       DRV_LOG(ERR, "Virtq %d cannot be notified.", index);
+                       goto error;
+               }
+       } else {
+               virtq->intr_handle.type = RTE_INTR_HANDLE_EXT;
+               if (rte_intr_callback_register(&virtq->intr_handle,
+                                              mlx5_vdpa_virtq_handler,
+                                              virtq)) {
+                       virtq->intr_handle.fd = -1;
+                       DRV_LOG(ERR, "Failed to register virtq %d interrupt.",
+                               index);
+                       goto error;
+               } else {
+                       DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
+                               virtq->intr_handle.fd, index);
+               }
+       }
        return 0;
 error:
        mlx5_vdpa_virtq_unset(virtq);
@@ -258,6 +356,18 @@ mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
                DRV_LOG(ERR, "Failed to configure negotiated features.");
                return -1;
        }
+       /* Always map the entire page. */
+       priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
+                                  PROT_WRITE, MAP_SHARED, priv->ctx->cmd_fd,
+                                  priv->var->mmap_off);
+       if (priv->virtq_db_addr == MAP_FAILED) {
+               DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
+               priv->virtq_db_addr = NULL;
+               goto error;
+       } else {
+               DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
+                       priv->virtq_db_addr);
+       }
        priv->td = mlx5_devx_cmd_create_td(priv->ctx);
        if (!priv->td) {
                DRV_LOG(ERR, "Failed to create transport domain.");