vdpa/mlx5: add hardware queue moderation
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa_virtq.c
index 19554f6..332753f 100644 (file)
@@ -72,8 +72,13 @@ mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
                }
                virtq->intr_handle.fd = -1;
        }
-       if (virtq->virtq)
+       if (virtq->virtq) {
+               ret = mlx5_vdpa_virtq_stop(virtq->priv, virtq->index);
+               if (ret)
+                       DRV_LOG(WARNING, "Failed to stop virtq %d.",
+                               virtq->index);
                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)
@@ -83,11 +88,6 @@ mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
                        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);
        virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_DISABLED;
@@ -98,12 +98,24 @@ void
 mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
 {
        int i;
+       struct mlx5_vdpa_virtq *virtq;
 
-       for (i = 0; i < priv->nr_virtqs; i++)
-               mlx5_vdpa_virtq_unset(&priv->virtqs[i]);
-       if (priv->tis) {
-               claim_zero(mlx5_devx_cmd_destroy(priv->tis));
-               priv->tis = NULL;
+       for (i = 0; i < priv->nr_virtqs; i++) {
+               virtq = &priv->virtqs[i];
+               mlx5_vdpa_virtq_unset(virtq);
+               if (virtq->counters) {
+                       claim_zero(mlx5_devx_cmd_destroy(virtq->counters));
+                       virtq->counters = NULL;
+                       memset(&virtq->reset, 0, sizeof(virtq->reset));
+               }
+               memset(virtq->err_time, 0, sizeof(virtq->err_time));
+               virtq->n_retry = 0;
+       }
+       for (i = 0; i < priv->num_lag_ports; i++) {
+               if (priv->tiss[i]) {
+                       claim_zero(mlx5_devx_cmd_destroy(priv->tiss[i]));
+                       priv->tiss[i] = NULL;
+               }
        }
        if (priv->td) {
                claim_zero(mlx5_devx_cmd_destroy(priv->td));
@@ -133,12 +145,26 @@ mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
 int
 mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index)
 {
-       struct mlx5_devx_virtq_attr attr = {0};
        struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
-       int ret = mlx5_vdpa_virtq_modify(virtq, 0);
+       int ret;
 
+       if (virtq->stopped)
+               return 0;
+       ret = mlx5_vdpa_virtq_modify(virtq, 0);
        if (ret)
                return -1;
+       virtq->stopped = true;
+       DRV_LOG(DEBUG, "vid %u virtq %u was stopped.", priv->vid, index);
+       return mlx5_vdpa_virtq_query(priv, index);
+}
+
+int
+mlx5_vdpa_virtq_query(struct mlx5_vdpa_priv *priv, int index)
+{
+       struct mlx5_devx_virtq_attr attr = {0};
+       struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
+       int ret;
+
        if (mlx5_devx_cmd_query_virtq(virtq->virtq, &attr)) {
                DRV_LOG(ERR, "Failed to query virtq %d.", index);
                return -1;
@@ -153,7 +179,9 @@ mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index)
                DRV_LOG(ERR, "Failed to set virtq %d base.", index);
                return -1;
        }
-       DRV_LOG(DEBUG, "vid %u virtq %u was stopped.", priv->vid, index);
+       if (attr.state == MLX5_VIRTQ_STATE_ERROR)
+               DRV_LOG(WARNING, "vid %d vring %d hw error=%hhu",
+                       priv->vid, index, attr.error_type);
        return 0;
 }
 
@@ -186,6 +214,8 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
        unsigned int i;
        uint16_t last_avail_idx;
        uint16_t last_used_idx;
+       uint16_t event_num = MLX5_EVENT_TYPE_OBJECT_CHANGE;
+       uint64_t cookie;
 
        ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
        if (ret)
@@ -222,8 +252,9 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
                        " 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)
+                       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);
@@ -293,9 +324,12 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
        attr.hw_used_index = last_used_idx;
        attr.q_size = vq.size;
        attr.mkey = priv->gpa_mkey_index;
-       attr.tis_id = priv->tis->id;
+       attr.tis_id = priv->tiss[(index / 2) % priv->num_lag_ports]->id;
        attr.queue_index = index;
        attr.pd = priv->pdn;
+       attr.hw_latency_mode = priv->hw_latency_mode;
+       attr.hw_max_latency_us = priv->hw_max_latency_us;
+       attr.hw_max_pending_comp = priv->hw_max_pending_comp;
        virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
        virtq->priv = priv;
        if (!virtq->virtq)
@@ -323,6 +357,20 @@ mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
                                virtq->intr_handle.fd, index);
                }
        }
+       /* Subscribe virtq error event. */
+       virtq->version++;
+       cookie = ((uint64_t)virtq->version << 32) + index;
+       ret = mlx5_glue->devx_subscribe_devx_event(priv->err_chnl,
+                                                  virtq->virtq->obj,
+                                                  sizeof(event_num),
+                                                  &event_num, cookie);
+       if (ret) {
+               DRV_LOG(ERR, "Failed to subscribe device %d virtq %d error event.",
+                       priv->vid, index);
+               rte_errno = errno;
+               goto error;
+       }
+       virtq->stopped = false;
        DRV_LOG(DEBUG, "vid %u virtq %u was created successfully.", priv->vid,
                index);
        return 0;
@@ -422,10 +470,14 @@ mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
                return -rte_errno;
        }
        tis_attr.transport_domain = priv->td->id;
-       priv->tis = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
-       if (!priv->tis) {
-               DRV_LOG(ERR, "Failed to create TIS.");
-               goto error;
+       for (i = 0; i < priv->num_lag_ports; i++) {
+               /* 0 is auto affinity, non-zero value to propose port. */
+               tis_attr.lag_tx_port_affinity = i + 1;
+               priv->tiss[i] = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
+               if (!priv->tiss[i]) {
+                       DRV_LOG(ERR, "Failed to create TIS %u.", i);
+                       goto error;
+               }
        }
        priv->nr_virtqs = nr_vring;
        for (i = 0; i < nr_vring; i++)
@@ -489,9 +541,6 @@ mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable)
                                DRV_LOG(WARNING, "Failed to disable steering "
                                        "for virtq %d.", index);
                }
-               ret = mlx5_vdpa_virtq_stop(priv, index);
-               if (ret)
-                       DRV_LOG(WARNING, "Failed to stop virtq %d.", index);
                mlx5_vdpa_virtq_unset(virtq);
        }
        if (enable) {
@@ -519,12 +568,11 @@ mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
        struct mlx5_devx_virtio_q_couners_attr attr = {0};
        int ret;
 
-       if (!virtq->virtq || !virtq->enable) {
+       if (!virtq->counters) {
                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);
@@ -576,12 +624,11 @@ 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) {
+       if (!virtq->counters) {
                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)