vdpa/mlx5: support virtio queue statistics get
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa_virtq.c
index 9c0284c..e99d82c 100644 (file)
@@ -59,9 +59,11 @@ mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
                                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 < RTE_DIM(virtq->umems); ++i) {
                if (virtq->umems[i].obj)
                        claim_zero(mlx5_glue->devx_umem_dereg
@@ -69,10 +71,14 @@ mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
                if (virtq->umems[i].buf)
                        rte_free(virtq->umems[i].buf);
        }
+       memset(&virtq->umems, 0, sizeof(virtq->umems));
+       if (virtq->counters) {
+               claim_zero(mlx5_devx_cmd_destroy(virtq->counters));
+               virtq->counters = NULL;
+       }
+       memset(&virtq->reset, 0, sizeof(virtq->reset));
        if (virtq->eqp.fw_qp)
                mlx5_vdpa_event_qp_destroy(&virtq->eqp);
-       memset(virtq, 0, sizeof(*virtq));
-       virtq->intr_handle.fd = -1;
        return 0;
 }
 
@@ -81,8 +87,10 @@ mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
 {
        int i;
 
-       for (i = 0; i < priv->nr_virtqs; i++)
+       for (i = 0; i < priv->nr_virtqs; i++) {
                mlx5_vdpa_virtq_unset(&priv->virtqs[i]);
+               priv->virtqs[i].enable = 0;
+       }
        if (priv->tis) {
                claim_zero(mlx5_devx_cmd_destroy(priv->tis));
                priv->tis = NULL;
@@ -202,6 +210,16 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
                DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
                        " need event QPs and event mechanism.", index);
        }
+       if (priv->caps.queue_counters_valid) {
+               virtq->counters = mlx5_devx_cmd_create_virtio_q_counters
+                                                                   (priv->ctx);
+               if (!virtq->counters) {
+                       DRV_LOG(ERR, "Failed to create virtq couners for virtq"
+                               " %d.", index);
+                       goto error;
+               }
+               attr.counters_obj_id = virtq->counters->id;
+       }
        /* Setup 3 UMEMs for each virtq. */
        for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
                virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
@@ -272,10 +290,7 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
                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;
@@ -402,11 +417,135 @@ mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
                goto error;
        }
        priv->nr_virtqs = nr_vring;
-       for (i = 0; i < nr_vring; i++)
+       for (i = 0; i < nr_vring; i++) {
+               claim_zero(rte_vhost_enable_guest_notification(priv->vid, i,
+                                                              1));
                if (mlx5_vdpa_virtq_setup(priv, i))
                        goto error;
+       }
        return 0;
 error:
        mlx5_vdpa_virtqs_release(priv);
        return -1;
 }
+
+int
+mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable)
+{
+       struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
+       int ret;
+
+       DRV_LOG(INFO, "Update virtq %d status %sable -> %sable.", index,
+               virtq->enable ? "en" : "dis", enable ? "en" : "dis");
+       if (virtq->enable == !!enable)
+               return 0;
+       if (!priv->configured) {
+               virtq->enable = !!enable;
+               return 0;
+       }
+       if (enable) {
+               /* Configuration might have been updated - reconfigure virtq. */
+               if (virtq->virtq) {
+                       ret = mlx5_vdpa_virtq_stop(priv, index);
+                       if (ret)
+                               DRV_LOG(WARNING, "Failed to stop virtq %d.",
+                                       index);
+                       mlx5_vdpa_virtq_unset(virtq);
+               }
+               ret = mlx5_vdpa_virtq_setup(priv, index);
+               if (ret) {
+                       DRV_LOG(ERR, "Failed to setup virtq %d.", index);
+                       return ret;
+                       /* The only case virtq can stay invalid. */
+               }
+       }
+       virtq->enable = !!enable;
+       if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
+               /* Need to add received virtq to the RQT table of the TIRs. */
+               ret = mlx5_vdpa_steer_update(priv);
+               if (ret) {
+                       virtq->enable = !enable;
+                       return ret;
+               }
+       }
+       return 0;
+}
+
+int
+mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
+                         struct rte_vdpa_stat *stats, unsigned int n)
+{
+       struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
+       struct mlx5_devx_virtio_q_couners_attr attr = {0};
+       int ret;
+
+       if (!virtq->virtq || !virtq->enable) {
+               DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
+                       "is invalid.", qid);
+               return -EINVAL;
+       }
+       MLX5_ASSERT(virtq->counters);
+       ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters, &attr);
+       if (ret) {
+               DRV_LOG(ERR, "Failed to read virtq %d stats from HW.", qid);
+               return ret;
+       }
+       ret = (int)RTE_MIN(n, (unsigned int)MLX5_VDPA_STATS_MAX);
+       if (ret == MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS)
+               return ret;
+       stats[MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS] = (struct rte_vdpa_stat) {
+               .id = MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS,
+               .value = attr.received_desc - virtq->reset.received_desc,
+       };
+       if (ret == MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS)
+               return ret;
+       stats[MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS] = (struct rte_vdpa_stat) {
+               .id = MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS,
+               .value = attr.completed_desc - virtq->reset.completed_desc,
+       };
+       if (ret == MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS)
+               return ret;
+       stats[MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS] = (struct rte_vdpa_stat) {
+               .id = MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS,
+               .value = attr.bad_desc_errors - virtq->reset.bad_desc_errors,
+       };
+       if (ret == MLX5_VDPA_STATS_EXCEED_MAX_CHAIN)
+               return ret;
+       stats[MLX5_VDPA_STATS_EXCEED_MAX_CHAIN] = (struct rte_vdpa_stat) {
+               .id = MLX5_VDPA_STATS_EXCEED_MAX_CHAIN,
+               .value = attr.exceed_max_chain - virtq->reset.exceed_max_chain,
+       };
+       if (ret == MLX5_VDPA_STATS_INVALID_BUFFER)
+               return ret;
+       stats[MLX5_VDPA_STATS_INVALID_BUFFER] = (struct rte_vdpa_stat) {
+               .id = MLX5_VDPA_STATS_INVALID_BUFFER,
+               .value = attr.invalid_buffer - virtq->reset.invalid_buffer,
+       };
+       if (ret == MLX5_VDPA_STATS_COMPLETION_ERRORS)
+               return ret;
+       stats[MLX5_VDPA_STATS_COMPLETION_ERRORS] = (struct rte_vdpa_stat) {
+               .id = MLX5_VDPA_STATS_COMPLETION_ERRORS,
+               .value = attr.error_cqes - virtq->reset.error_cqes,
+       };
+       return ret;
+}
+
+int
+mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid)
+{
+       struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
+       int ret;
+
+       if (!virtq->virtq || !virtq->enable) {
+               DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
+                       "is invalid.", qid);
+               return -EINVAL;
+       }
+       MLX5_ASSERT(virtq->counters);
+       ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters,
+                                                   &virtq->reset);
+       if (ret)
+               DRV_LOG(ERR, "Failed to read virtq %d reset stats from HW.",
+                       qid);
+       return ret;
+}