vdpa/mlx5: fix event channel setup
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa_event.c
index 0414c91..bda547f 100644 (file)
@@ -51,12 +51,11 @@ mlx5_vdpa_event_qp_global_release(struct mlx5_vdpa_priv *priv)
 static int
 mlx5_vdpa_event_qp_global_prepare(struct mlx5_vdpa_priv *priv)
 {
-       uint32_t lcore;
+       int flags, ret;
 
        if (priv->eventc)
                return 0;
-       lcore = (uint32_t)rte_lcore_to_cpu_id(-1);
-       if (mlx5_glue->devx_query_eqn(priv->ctx, lcore, &priv->eqn)) {
+       if (mlx5_glue->devx_query_eqn(priv->ctx, 0, &priv->eqn)) {
                rte_errno = errno;
                DRV_LOG(ERR, "Failed to query EQ number %d.", rte_errno);
                return -1;
@@ -69,6 +68,12 @@ mlx5_vdpa_event_qp_global_prepare(struct mlx5_vdpa_priv *priv)
                        rte_errno);
                goto error;
        }
+       flags = fcntl(priv->eventc->fd, F_GETFL);
+       ret = fcntl(priv->eventc->fd, F_SETFL, flags | O_NONBLOCK);
+       if (ret) {
+               DRV_LOG(ERR, "Failed to change event channel FD.");
+               goto error;
+       }
        priv->uar = mlx5_glue->devx_alloc_uar(priv->ctx, 0);
        if (!priv->uar) {
                rte_errno = errno;
@@ -121,7 +126,7 @@ static int
 mlx5_vdpa_cq_create(struct mlx5_vdpa_priv *priv, uint16_t log_desc_n,
                    int callfd, struct mlx5_vdpa_cq *cq)
 {
-       struct mlx5_devx_cq_attr attr;
+       struct mlx5_devx_cq_attr attr = {0};
        size_t pgsize = sysconf(_SC_PAGESIZE);
        uint32_t umem_size;
        uint16_t event_nums[1] = {0};
@@ -277,6 +282,7 @@ mlx5_vdpa_poll_handle(void *arg)
                                                                 priv->event_us;
        while (1) {
                max = 0;
+               pthread_mutex_lock(&priv->vq_config_lock);
                for (i = 0; i < priv->nr_virtqs; i++) {
                        cq = &priv->virtqs[i].eqp.cq;
                        if (cq->cq && !cq->armed) {
@@ -300,6 +306,7 @@ mlx5_vdpa_poll_handle(void *arg)
                                DRV_LOG(DEBUG, "Device %s traffic was stopped.",
                                        priv->vdev->device->name);
                                mlx5_vdpa_arm_all_cqs(priv);
+                               pthread_mutex_unlock(&priv->vq_config_lock);
                                pthread_mutex_lock(&priv->timer_lock);
                                priv->timer_on = 0;
                                while (!priv->timer_on)
@@ -315,6 +322,7 @@ mlx5_vdpa_poll_handle(void *arg)
                } else {
                        priv->last_traffic_tic = current_tic;
                }
+               pthread_mutex_unlock(&priv->vq_config_lock);
                mlx5_vdpa_timer_sleep(priv, max);
        }
        return NULL;
@@ -330,6 +338,7 @@ mlx5_vdpa_interrupt_handler(void *cb_arg)
                uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
        } out;
 
+       pthread_mutex_lock(&priv->vq_config_lock);
        while (mlx5_glue->devx_get_event(priv->eventc, &out.event_resp,
                                         sizeof(out.buf)) >=
                                       (ssize_t)sizeof(out.event_resp.cookie)) {
@@ -340,12 +349,15 @@ mlx5_vdpa_interrupt_handler(void *cb_arg)
                struct mlx5_vdpa_virtq *virtq = container_of(eqp,
                                                   struct mlx5_vdpa_virtq, eqp);
 
+               if (!virtq->enable)
+                       continue;
                mlx5_vdpa_cq_poll(cq);
                /* Notify guest for descs consuming. */
                if (cq->callfd != -1)
                        eventfd_write(cq->callfd, (eventfd_t)1);
                if (priv->event_mode == MLX5_VDPA_EVENT_MODE_ONLY_INTERRUPT) {
                        mlx5_vdpa_cq_arm(priv, cq);
+                       pthread_mutex_unlock(&priv->vq_config_lock);
                        return;
                }
                /* Don't arm again - timer will take control. */
@@ -366,12 +378,12 @@ mlx5_vdpa_interrupt_handler(void *cb_arg)
                pthread_cond_signal(&priv->timer_cond);
        }
        pthread_mutex_unlock(&priv->timer_lock);
+       pthread_mutex_unlock(&priv->vq_config_lock);
 }
 
 int
 mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv)
 {
-       int flags;
        int ret;
 
        if (!priv->eventc)
@@ -388,12 +400,6 @@ mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv)
                        return -1;
                }
        }
-       flags = fcntl(priv->eventc->fd, F_GETFL);
-       ret = fcntl(priv->eventc->fd, F_SETFL, flags | O_NONBLOCK);
-       if (ret) {
-               DRV_LOG(ERR, "Failed to change event channel FD.");
-               goto error;
-       }
        priv->intr_handle.fd = priv->eventc->fd;
        priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
        if (rte_intr_callback_register(&priv->intr_handle,