d5ac04054426817bda837517f4ab5aec4fe32079
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa_virtq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
7
8 #include <rte_malloc.h>
9 #include <rte_errno.h>
10 #include <rte_io.h>
11
12 #include <mlx5_common.h>
13
14 #include "mlx5_vdpa_utils.h"
15 #include "mlx5_vdpa.h"
16
17
18 static void
19 mlx5_vdpa_virtq_handler(void *cb_arg)
20 {
21         struct mlx5_vdpa_virtq *virtq = cb_arg;
22         struct mlx5_vdpa_priv *priv = virtq->priv;
23         uint64_t buf;
24         int nbytes;
25
26         do {
27                 nbytes = read(virtq->intr_handle.fd, &buf, 8);
28                 if (nbytes < 0) {
29                         if (errno == EINTR ||
30                             errno == EWOULDBLOCK ||
31                             errno == EAGAIN)
32                                 continue;
33                         DRV_LOG(ERR,  "Failed to read kickfd of virtq %d: %s",
34                                 virtq->index, strerror(errno));
35                 }
36                 break;
37         } while (1);
38         rte_write32(virtq->index, priv->virtq_db_addr);
39         if (virtq->notifier_state == MLX5_VDPA_NOTIFIER_STATE_DISABLED) {
40                 if (rte_vhost_host_notifier_ctrl(priv->vid, virtq->index, true))
41                         virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_ERR;
42                 else
43                         virtq->notifier_state =
44                                                MLX5_VDPA_NOTIFIER_STATE_ENABLED;
45                 DRV_LOG(INFO, "Virtq %u notifier state is %s.", virtq->index,
46                         virtq->notifier_state ==
47                                 MLX5_VDPA_NOTIFIER_STATE_ENABLED ? "enabled" :
48                                                                     "disabled");
49         }
50         DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index);
51 }
52
53 static int
54 mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
55 {
56         unsigned int i;
57         int retries = MLX5_VDPA_INTR_RETRIES;
58         int ret = -EAGAIN;
59
60         if (virtq->intr_handle.fd != -1) {
61                 while (retries-- && ret == -EAGAIN) {
62                         ret = rte_intr_callback_unregister(&virtq->intr_handle,
63                                                         mlx5_vdpa_virtq_handler,
64                                                         virtq);
65                         if (ret == -EAGAIN) {
66                                 DRV_LOG(DEBUG, "Try again to unregister fd %d "
67                                         "of virtq %d interrupt, retries = %d.",
68                                         virtq->intr_handle.fd,
69                                         (int)virtq->index, retries);
70                                 usleep(MLX5_VDPA_INTR_RETRIES_USEC);
71                         }
72                 }
73                 virtq->intr_handle.fd = -1;
74         }
75         if (virtq->virtq) {
76                 ret = mlx5_vdpa_virtq_stop(virtq->priv, virtq->index);
77                 if (ret)
78                         DRV_LOG(WARNING, "Failed to stop virtq %d.",
79                                 virtq->index);
80                 claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
81         }
82         virtq->virtq = NULL;
83         for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
84                 if (virtq->umems[i].obj)
85                         claim_zero(mlx5_glue->devx_umem_dereg
86                                                          (virtq->umems[i].obj));
87                 if (virtq->umems[i].buf)
88                         rte_free(virtq->umems[i].buf);
89         }
90         memset(&virtq->umems, 0, sizeof(virtq->umems));
91         if (virtq->eqp.fw_qp)
92                 mlx5_vdpa_event_qp_destroy(&virtq->eqp);
93         virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_DISABLED;
94         return 0;
95 }
96
97 void
98 mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
99 {
100         int i;
101         struct mlx5_vdpa_virtq *virtq;
102
103         for (i = 0; i < priv->nr_virtqs; i++) {
104                 virtq = &priv->virtqs[i];
105                 mlx5_vdpa_virtq_unset(virtq);
106                 if (virtq->counters) {
107                         claim_zero(mlx5_devx_cmd_destroy(virtq->counters));
108                         virtq->counters = NULL;
109                         memset(&virtq->reset, 0, sizeof(virtq->reset));
110                 }
111                 memset(virtq->err_time, 0, sizeof(virtq->err_time));
112                 virtq->n_retry = 0;
113         }
114         if (priv->tis) {
115                 claim_zero(mlx5_devx_cmd_destroy(priv->tis));
116                 priv->tis = NULL;
117         }
118         if (priv->td) {
119                 claim_zero(mlx5_devx_cmd_destroy(priv->td));
120                 priv->td = NULL;
121         }
122         if (priv->virtq_db_addr) {
123                 claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
124                 priv->virtq_db_addr = NULL;
125         }
126         priv->features = 0;
127         priv->nr_virtqs = 0;
128 }
129
130 int
131 mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
132 {
133         struct mlx5_devx_virtq_attr attr = {
134                         .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
135                         .state = state ? MLX5_VIRTQ_STATE_RDY :
136                                          MLX5_VIRTQ_STATE_SUSPEND,
137                         .queue_index = virtq->index,
138         };
139
140         return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
141 }
142
143 int
144 mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index)
145 {
146         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
147         int ret;
148
149         if (virtq->stopped)
150                 return 0;
151         ret = mlx5_vdpa_virtq_modify(virtq, 0);
152         if (ret)
153                 return -1;
154         virtq->stopped = true;
155         DRV_LOG(DEBUG, "vid %u virtq %u was stopped.", priv->vid, index);
156         return mlx5_vdpa_virtq_query(priv, index);
157 }
158
159 int
160 mlx5_vdpa_virtq_query(struct mlx5_vdpa_priv *priv, int index)
161 {
162         struct mlx5_devx_virtq_attr attr = {0};
163         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
164         int ret;
165
166         if (mlx5_devx_cmd_query_virtq(virtq->virtq, &attr)) {
167                 DRV_LOG(ERR, "Failed to query virtq %d.", index);
168                 return -1;
169         }
170         DRV_LOG(INFO, "Query vid %d vring %d: hw_available_idx=%d, "
171                 "hw_used_index=%d", priv->vid, index,
172                 attr.hw_available_index, attr.hw_used_index);
173         ret = rte_vhost_set_vring_base(priv->vid, index,
174                                        attr.hw_available_index,
175                                        attr.hw_used_index);
176         if (ret) {
177                 DRV_LOG(ERR, "Failed to set virtq %d base.", index);
178                 return -1;
179         }
180         if (attr.state == MLX5_VIRTQ_STATE_ERROR)
181                 DRV_LOG(WARNING, "vid %d vring %d hw error=%hhu",
182                         priv->vid, index, attr.error_type);
183         return 0;
184 }
185
186 static uint64_t
187 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
188 {
189         struct rte_vhost_mem_region *reg;
190         uint32_t i;
191         uint64_t gpa = 0;
192
193         for (i = 0; i < mem->nregions; i++) {
194                 reg = &mem->regions[i];
195                 if (hva >= reg->host_user_addr &&
196                     hva < reg->host_user_addr + reg->size) {
197                         gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
198                         break;
199                 }
200         }
201         return gpa;
202 }
203
204 static int
205 mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
206 {
207         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
208         struct rte_vhost_vring vq;
209         struct mlx5_devx_virtq_attr attr = {0};
210         uint64_t gpa;
211         int ret;
212         unsigned int i;
213         uint16_t last_avail_idx;
214         uint16_t last_used_idx;
215         uint16_t event_num = MLX5_EVENT_TYPE_OBJECT_CHANGE;
216         uint64_t cookie;
217
218         ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
219         if (ret)
220                 return -1;
221         virtq->index = index;
222         virtq->vq_size = vq.size;
223         attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4));
224         attr.tso_ipv6 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6));
225         attr.tx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_CSUM));
226         attr.rx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM));
227         attr.virtio_version_1_0 = !!(priv->features & (1ULL <<
228                                                         VIRTIO_F_VERSION_1));
229         attr.type = (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) ?
230                         MLX5_VIRTQ_TYPE_PACKED : MLX5_VIRTQ_TYPE_SPLIT;
231         /*
232          * No need event QPs creation when the guest in poll mode or when the
233          * capability allows it.
234          */
235         attr.event_mode = vq.callfd != -1 || !(priv->caps.event_mode & (1 <<
236                                                MLX5_VIRTQ_EVENT_MODE_NO_MSIX)) ?
237                                                       MLX5_VIRTQ_EVENT_MODE_QP :
238                                                   MLX5_VIRTQ_EVENT_MODE_NO_MSIX;
239         if (attr.event_mode == MLX5_VIRTQ_EVENT_MODE_QP) {
240                 ret = mlx5_vdpa_event_qp_create(priv, vq.size, vq.callfd,
241                                                 &virtq->eqp);
242                 if (ret) {
243                         DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
244                                 index);
245                         return -1;
246                 }
247                 attr.qp_id = virtq->eqp.fw_qp->id;
248         } else {
249                 DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
250                         " need event QPs and event mechanism.", index);
251         }
252         if (priv->caps.queue_counters_valid) {
253                 if (!virtq->counters)
254                         virtq->counters = mlx5_devx_cmd_create_virtio_q_counters
255                                                                 (priv->ctx);
256                 if (!virtq->counters) {
257                         DRV_LOG(ERR, "Failed to create virtq couners for virtq"
258                                 " %d.", index);
259                         goto error;
260                 }
261                 attr.counters_obj_id = virtq->counters->id;
262         }
263         /* Setup 3 UMEMs for each virtq. */
264         for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
265                 virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
266                                                           priv->caps.umems[i].b;
267                 virtq->umems[i].buf = rte_zmalloc(__func__,
268                                                   virtq->umems[i].size, 4096);
269                 if (!virtq->umems[i].buf) {
270                         DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
271                                 " %u.", i, index);
272                         goto error;
273                 }
274                 virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->ctx,
275                                                         virtq->umems[i].buf,
276                                                         virtq->umems[i].size,
277                                                         IBV_ACCESS_LOCAL_WRITE);
278                 if (!virtq->umems[i].obj) {
279                         DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
280                                 i, index);
281                         goto error;
282                 }
283                 attr.umems[i].id = virtq->umems[i].obj->umem_id;
284                 attr.umems[i].offset = 0;
285                 attr.umems[i].size = virtq->umems[i].size;
286         }
287         if (attr.type == MLX5_VIRTQ_TYPE_SPLIT) {
288                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
289                                            (uint64_t)(uintptr_t)vq.desc);
290                 if (!gpa) {
291                         DRV_LOG(ERR, "Failed to get descriptor ring GPA.");
292                         goto error;
293                 }
294                 attr.desc_addr = gpa;
295                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
296                                            (uint64_t)(uintptr_t)vq.used);
297                 if (!gpa) {
298                         DRV_LOG(ERR, "Failed to get GPA for used ring.");
299                         goto error;
300                 }
301                 attr.used_addr = gpa;
302                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
303                                            (uint64_t)(uintptr_t)vq.avail);
304                 if (!gpa) {
305                         DRV_LOG(ERR, "Failed to get GPA for available ring.");
306                         goto error;
307                 }
308                 attr.available_addr = gpa;
309         }
310         ret = rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
311                                  &last_used_idx);
312         if (ret) {
313                 last_avail_idx = 0;
314                 last_used_idx = 0;
315                 DRV_LOG(WARNING, "Couldn't get vring base, idx are set to 0");
316         } else {
317                 DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
318                                 "virtq %d.", priv->vid, last_avail_idx,
319                                 last_used_idx, index);
320         }
321         attr.hw_available_index = last_avail_idx;
322         attr.hw_used_index = last_used_idx;
323         attr.q_size = vq.size;
324         attr.mkey = priv->gpa_mkey_index;
325         attr.tis_id = priv->tis->id;
326         attr.queue_index = index;
327         attr.pd = priv->pdn;
328         virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
329         virtq->priv = priv;
330         if (!virtq->virtq)
331                 goto error;
332         claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
333         if (mlx5_vdpa_virtq_modify(virtq, 1))
334                 goto error;
335         virtq->priv = priv;
336         rte_write32(virtq->index, priv->virtq_db_addr);
337         /* Setup doorbell mapping. */
338         virtq->intr_handle.fd = vq.kickfd;
339         if (virtq->intr_handle.fd == -1) {
340                 DRV_LOG(WARNING, "Virtq %d kickfd is invalid.", index);
341         } else {
342                 virtq->intr_handle.type = RTE_INTR_HANDLE_EXT;
343                 if (rte_intr_callback_register(&virtq->intr_handle,
344                                                mlx5_vdpa_virtq_handler,
345                                                virtq)) {
346                         virtq->intr_handle.fd = -1;
347                         DRV_LOG(ERR, "Failed to register virtq %d interrupt.",
348                                 index);
349                         goto error;
350                 } else {
351                         DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
352                                 virtq->intr_handle.fd, index);
353                 }
354         }
355         /* Subscribe virtq error event. */
356         virtq->version++;
357         cookie = ((uint64_t)virtq->version << 32) + index;
358         ret = mlx5_glue->devx_subscribe_devx_event(priv->err_chnl,
359                                                    virtq->virtq->obj,
360                                                    sizeof(event_num),
361                                                    &event_num, cookie);
362         if (ret) {
363                 DRV_LOG(ERR, "Failed to subscribe device %d virtq %d error event.",
364                         priv->vid, index);
365                 rte_errno = errno;
366                 goto error;
367         }
368         virtq->stopped = false;
369         DRV_LOG(DEBUG, "vid %u virtq %u was created successfully.", priv->vid,
370                 index);
371         return 0;
372 error:
373         mlx5_vdpa_virtq_unset(virtq);
374         return -1;
375 }
376
377 static int
378 mlx5_vdpa_features_validate(struct mlx5_vdpa_priv *priv)
379 {
380         if (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) {
381                 if (!(priv->caps.virtio_queue_type & (1 <<
382                                                      MLX5_VIRTQ_TYPE_PACKED))) {
383                         DRV_LOG(ERR, "Failed to configur PACKED mode for vdev "
384                                 "%d - it was not reported by HW/driver"
385                                 " capability.", priv->vid);
386                         return -ENOTSUP;
387                 }
388         }
389         if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) {
390                 if (!priv->caps.tso_ipv4) {
391                         DRV_LOG(ERR, "Failed to enable TSO4 for vdev %d - TSO4"
392                                 " was not reported by HW/driver capability.",
393                                 priv->vid);
394                         return -ENOTSUP;
395                 }
396         }
397         if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) {
398                 if (!priv->caps.tso_ipv6) {
399                         DRV_LOG(ERR, "Failed to enable TSO6 for vdev %d - TSO6"
400                                 " was not reported by HW/driver capability.",
401                                 priv->vid);
402                         return -ENOTSUP;
403                 }
404         }
405         if (priv->features & (1ULL << VIRTIO_NET_F_CSUM)) {
406                 if (!priv->caps.tx_csum) {
407                         DRV_LOG(ERR, "Failed to enable CSUM for vdev %d - CSUM"
408                                 " was not reported by HW/driver capability.",
409                                 priv->vid);
410                         return -ENOTSUP;
411                 }
412         }
413         if (priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
414                 if (!priv->caps.rx_csum) {
415                         DRV_LOG(ERR, "Failed to enable GUEST CSUM for vdev %d"
416                                 " GUEST CSUM was not reported by HW/driver "
417                                 "capability.", priv->vid);
418                         return -ENOTSUP;
419                 }
420         }
421         if (priv->features & (1ULL << VIRTIO_F_VERSION_1)) {
422                 if (!priv->caps.virtio_version_1_0) {
423                         DRV_LOG(ERR, "Failed to enable version 1 for vdev %d "
424                                 "version 1 was not reported by HW/driver"
425                                 " capability.", priv->vid);
426                         return -ENOTSUP;
427                 }
428         }
429         return 0;
430 }
431
432 int
433 mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
434 {
435         struct mlx5_devx_tis_attr tis_attr = {0};
436         uint32_t i;
437         uint16_t nr_vring = rte_vhost_get_vring_num(priv->vid);
438         int ret = rte_vhost_get_negotiated_features(priv->vid, &priv->features);
439
440         if (ret || mlx5_vdpa_features_validate(priv)) {
441                 DRV_LOG(ERR, "Failed to configure negotiated features.");
442                 return -1;
443         }
444         if (nr_vring > priv->caps.max_num_virtio_queues * 2) {
445                 DRV_LOG(ERR, "Do not support more than %d virtqs(%d).",
446                         (int)priv->caps.max_num_virtio_queues * 2,
447                         (int)nr_vring);
448                 return -1;
449         }
450         /* Always map the entire page. */
451         priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
452                                    PROT_WRITE, MAP_SHARED, priv->ctx->cmd_fd,
453                                    priv->var->mmap_off);
454         if (priv->virtq_db_addr == MAP_FAILED) {
455                 DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
456                 priv->virtq_db_addr = NULL;
457                 goto error;
458         } else {
459                 DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
460                         priv->virtq_db_addr);
461         }
462         priv->td = mlx5_devx_cmd_create_td(priv->ctx);
463         if (!priv->td) {
464                 DRV_LOG(ERR, "Failed to create transport domain.");
465                 return -rte_errno;
466         }
467         tis_attr.transport_domain = priv->td->id;
468         priv->tis = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
469         if (!priv->tis) {
470                 DRV_LOG(ERR, "Failed to create TIS.");
471                 goto error;
472         }
473         priv->nr_virtqs = nr_vring;
474         for (i = 0; i < nr_vring; i++)
475                 if (priv->virtqs[i].enable && mlx5_vdpa_virtq_setup(priv, i))
476                         goto error;
477         return 0;
478 error:
479         mlx5_vdpa_virtqs_release(priv);
480         return -1;
481 }
482
483 static int
484 mlx5_vdpa_virtq_is_modified(struct mlx5_vdpa_priv *priv,
485                             struct mlx5_vdpa_virtq *virtq)
486 {
487         struct rte_vhost_vring vq;
488         int ret = rte_vhost_get_vhost_vring(priv->vid, virtq->index, &vq);
489
490         if (ret)
491                 return -1;
492         if (vq.size != virtq->vq_size || vq.kickfd != virtq->intr_handle.fd)
493                 return 1;
494         if (virtq->eqp.cq.cq) {
495                 if (vq.callfd != virtq->eqp.cq.callfd)
496                         return 1;
497         } else if (vq.callfd != -1) {
498                 return 1;
499         }
500         return 0;
501 }
502
503 int
504 mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable)
505 {
506         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
507         int ret;
508
509         DRV_LOG(INFO, "Update virtq %d status %sable -> %sable.", index,
510                 virtq->enable ? "en" : "dis", enable ? "en" : "dis");
511         if (!priv->configured) {
512                 virtq->enable = !!enable;
513                 return 0;
514         }
515         if (virtq->enable == !!enable) {
516                 if (!enable)
517                         return 0;
518                 ret = mlx5_vdpa_virtq_is_modified(priv, virtq);
519                 if (ret < 0) {
520                         DRV_LOG(ERR, "Virtq %d modify check failed.", index);
521                         return -1;
522                 }
523                 if (ret == 0)
524                         return 0;
525                 DRV_LOG(INFO, "Virtq %d was modified, recreate it.", index);
526         }
527         if (virtq->virtq) {
528                 virtq->enable = 0;
529                 if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
530                         ret = mlx5_vdpa_steer_update(priv);
531                         if (ret)
532                                 DRV_LOG(WARNING, "Failed to disable steering "
533                                         "for virtq %d.", index);
534                 }
535                 mlx5_vdpa_virtq_unset(virtq);
536         }
537         if (enable) {
538                 ret = mlx5_vdpa_virtq_setup(priv, index);
539                 if (ret) {
540                         DRV_LOG(ERR, "Failed to setup virtq %d.", index);
541                         return ret;
542                 }
543                 virtq->enable = 1;
544                 if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
545                         ret = mlx5_vdpa_steer_update(priv);
546                         if (ret)
547                                 DRV_LOG(WARNING, "Failed to enable steering "
548                                         "for virtq %d.", index);
549                 }
550         }
551         return 0;
552 }
553
554 int
555 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
556                           struct rte_vdpa_stat *stats, unsigned int n)
557 {
558         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
559         struct mlx5_devx_virtio_q_couners_attr attr = {0};
560         int ret;
561
562         if (!virtq->counters) {
563                 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
564                         "is invalid.", qid);
565                 return -EINVAL;
566         }
567         ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters, &attr);
568         if (ret) {
569                 DRV_LOG(ERR, "Failed to read virtq %d stats from HW.", qid);
570                 return ret;
571         }
572         ret = (int)RTE_MIN(n, (unsigned int)MLX5_VDPA_STATS_MAX);
573         if (ret == MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS)
574                 return ret;
575         stats[MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS] = (struct rte_vdpa_stat) {
576                 .id = MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS,
577                 .value = attr.received_desc - virtq->reset.received_desc,
578         };
579         if (ret == MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS)
580                 return ret;
581         stats[MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS] = (struct rte_vdpa_stat) {
582                 .id = MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS,
583                 .value = attr.completed_desc - virtq->reset.completed_desc,
584         };
585         if (ret == MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS)
586                 return ret;
587         stats[MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS] = (struct rte_vdpa_stat) {
588                 .id = MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS,
589                 .value = attr.bad_desc_errors - virtq->reset.bad_desc_errors,
590         };
591         if (ret == MLX5_VDPA_STATS_EXCEED_MAX_CHAIN)
592                 return ret;
593         stats[MLX5_VDPA_STATS_EXCEED_MAX_CHAIN] = (struct rte_vdpa_stat) {
594                 .id = MLX5_VDPA_STATS_EXCEED_MAX_CHAIN,
595                 .value = attr.exceed_max_chain - virtq->reset.exceed_max_chain,
596         };
597         if (ret == MLX5_VDPA_STATS_INVALID_BUFFER)
598                 return ret;
599         stats[MLX5_VDPA_STATS_INVALID_BUFFER] = (struct rte_vdpa_stat) {
600                 .id = MLX5_VDPA_STATS_INVALID_BUFFER,
601                 .value = attr.invalid_buffer - virtq->reset.invalid_buffer,
602         };
603         if (ret == MLX5_VDPA_STATS_COMPLETION_ERRORS)
604                 return ret;
605         stats[MLX5_VDPA_STATS_COMPLETION_ERRORS] = (struct rte_vdpa_stat) {
606                 .id = MLX5_VDPA_STATS_COMPLETION_ERRORS,
607                 .value = attr.error_cqes - virtq->reset.error_cqes,
608         };
609         return ret;
610 }
611
612 int
613 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid)
614 {
615         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
616         int ret;
617
618         if (!virtq->counters) {
619                 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
620                         "is invalid.", qid);
621                 return -EINVAL;
622         }
623         ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters,
624                                                     &virtq->reset);
625         if (ret)
626                 DRV_LOG(ERR, "Failed to read virtq %d reset stats from HW.",
627                         qid);
628         return ret;
629 }