3e6126492b38eb109a7135fef3417c94b8811e22
[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                 claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
77         virtq->virtq = NULL;
78         for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
79                 if (virtq->umems[i].obj)
80                         claim_zero(mlx5_glue->devx_umem_dereg
81                                                          (virtq->umems[i].obj));
82                 if (virtq->umems[i].buf)
83                         rte_free(virtq->umems[i].buf);
84         }
85         memset(&virtq->umems, 0, sizeof(virtq->umems));
86         if (virtq->counters) {
87                 claim_zero(mlx5_devx_cmd_destroy(virtq->counters));
88                 virtq->counters = NULL;
89         }
90         memset(&virtq->reset, 0, sizeof(virtq->reset));
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
102         for (i = 0; i < priv->nr_virtqs; i++)
103                 mlx5_vdpa_virtq_unset(&priv->virtqs[i]);
104         if (priv->tis) {
105                 claim_zero(mlx5_devx_cmd_destroy(priv->tis));
106                 priv->tis = NULL;
107         }
108         if (priv->td) {
109                 claim_zero(mlx5_devx_cmd_destroy(priv->td));
110                 priv->td = NULL;
111         }
112         if (priv->virtq_db_addr) {
113                 claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
114                 priv->virtq_db_addr = NULL;
115         }
116         priv->features = 0;
117         priv->nr_virtqs = 0;
118 }
119
120 int
121 mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
122 {
123         struct mlx5_devx_virtq_attr attr = {
124                         .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
125                         .state = state ? MLX5_VIRTQ_STATE_RDY :
126                                          MLX5_VIRTQ_STATE_SUSPEND,
127                         .queue_index = virtq->index,
128         };
129
130         return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
131 }
132
133 int
134 mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index)
135 {
136         struct mlx5_devx_virtq_attr attr = {0};
137         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
138         int ret = mlx5_vdpa_virtq_modify(virtq, 0);
139
140         if (ret)
141                 return -1;
142         if (mlx5_devx_cmd_query_virtq(virtq->virtq, &attr)) {
143                 DRV_LOG(ERR, "Failed to query virtq %d.", index);
144                 return -1;
145         }
146         DRV_LOG(INFO, "Query vid %d vring %d: hw_available_idx=%d, "
147                 "hw_used_index=%d", priv->vid, index,
148                 attr.hw_available_index, attr.hw_used_index);
149         ret = rte_vhost_set_vring_base(priv->vid, index,
150                                        attr.hw_available_index,
151                                        attr.hw_used_index);
152         if (ret) {
153                 DRV_LOG(ERR, "Failed to set virtq %d base.", index);
154                 return -1;
155         }
156         DRV_LOG(DEBUG, "vid %u virtq %u was stopped.", priv->vid, index);
157         return 0;
158 }
159
160 static uint64_t
161 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
162 {
163         struct rte_vhost_mem_region *reg;
164         uint32_t i;
165         uint64_t gpa = 0;
166
167         for (i = 0; i < mem->nregions; i++) {
168                 reg = &mem->regions[i];
169                 if (hva >= reg->host_user_addr &&
170                     hva < reg->host_user_addr + reg->size) {
171                         gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
172                         break;
173                 }
174         }
175         return gpa;
176 }
177
178 static int
179 mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
180 {
181         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
182         struct rte_vhost_vring vq;
183         struct mlx5_devx_virtq_attr attr = {0};
184         uint64_t gpa;
185         int ret;
186         unsigned int i;
187         uint16_t last_avail_idx;
188         uint16_t last_used_idx;
189
190         ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
191         if (ret)
192                 return -1;
193         virtq->index = index;
194         virtq->vq_size = vq.size;
195         attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4));
196         attr.tso_ipv6 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6));
197         attr.tx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_CSUM));
198         attr.rx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM));
199         attr.virtio_version_1_0 = !!(priv->features & (1ULL <<
200                                                         VIRTIO_F_VERSION_1));
201         attr.type = (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) ?
202                         MLX5_VIRTQ_TYPE_PACKED : MLX5_VIRTQ_TYPE_SPLIT;
203         /*
204          * No need event QPs creation when the guest in poll mode or when the
205          * capability allows it.
206          */
207         attr.event_mode = vq.callfd != -1 || !(priv->caps.event_mode & (1 <<
208                                                MLX5_VIRTQ_EVENT_MODE_NO_MSIX)) ?
209                                                       MLX5_VIRTQ_EVENT_MODE_QP :
210                                                   MLX5_VIRTQ_EVENT_MODE_NO_MSIX;
211         if (attr.event_mode == MLX5_VIRTQ_EVENT_MODE_QP) {
212                 ret = mlx5_vdpa_event_qp_create(priv, vq.size, vq.callfd,
213                                                 &virtq->eqp);
214                 if (ret) {
215                         DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
216                                 index);
217                         return -1;
218                 }
219                 attr.qp_id = virtq->eqp.fw_qp->id;
220         } else {
221                 DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
222                         " need event QPs and event mechanism.", index);
223         }
224         if (priv->caps.queue_counters_valid) {
225                 virtq->counters = mlx5_devx_cmd_create_virtio_q_counters
226                                                                     (priv->ctx);
227                 if (!virtq->counters) {
228                         DRV_LOG(ERR, "Failed to create virtq couners for virtq"
229                                 " %d.", index);
230                         goto error;
231                 }
232                 attr.counters_obj_id = virtq->counters->id;
233         }
234         /* Setup 3 UMEMs for each virtq. */
235         for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
236                 virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
237                                                           priv->caps.umems[i].b;
238                 virtq->umems[i].buf = rte_zmalloc(__func__,
239                                                   virtq->umems[i].size, 4096);
240                 if (!virtq->umems[i].buf) {
241                         DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
242                                 " %u.", i, index);
243                         goto error;
244                 }
245                 virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->ctx,
246                                                         virtq->umems[i].buf,
247                                                         virtq->umems[i].size,
248                                                         IBV_ACCESS_LOCAL_WRITE);
249                 if (!virtq->umems[i].obj) {
250                         DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
251                                 i, index);
252                         goto error;
253                 }
254                 attr.umems[i].id = virtq->umems[i].obj->umem_id;
255                 attr.umems[i].offset = 0;
256                 attr.umems[i].size = virtq->umems[i].size;
257         }
258         if (attr.type == MLX5_VIRTQ_TYPE_SPLIT) {
259                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
260                                            (uint64_t)(uintptr_t)vq.desc);
261                 if (!gpa) {
262                         DRV_LOG(ERR, "Failed to get descriptor ring GPA.");
263                         goto error;
264                 }
265                 attr.desc_addr = gpa;
266                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
267                                            (uint64_t)(uintptr_t)vq.used);
268                 if (!gpa) {
269                         DRV_LOG(ERR, "Failed to get GPA for used ring.");
270                         goto error;
271                 }
272                 attr.used_addr = gpa;
273                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
274                                            (uint64_t)(uintptr_t)vq.avail);
275                 if (!gpa) {
276                         DRV_LOG(ERR, "Failed to get GPA for available ring.");
277                         goto error;
278                 }
279                 attr.available_addr = gpa;
280         }
281         ret = rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
282                                  &last_used_idx);
283         if (ret) {
284                 last_avail_idx = 0;
285                 last_used_idx = 0;
286                 DRV_LOG(WARNING, "Couldn't get vring base, idx are set to 0");
287         } else {
288                 DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
289                                 "virtq %d.", priv->vid, last_avail_idx,
290                                 last_used_idx, index);
291         }
292         attr.hw_available_index = last_avail_idx;
293         attr.hw_used_index = last_used_idx;
294         attr.q_size = vq.size;
295         attr.mkey = priv->gpa_mkey_index;
296         attr.tis_id = priv->tis->id;
297         attr.queue_index = index;
298         attr.pd = priv->pdn;
299         virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
300         virtq->priv = priv;
301         if (!virtq->virtq)
302                 goto error;
303         claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
304         if (mlx5_vdpa_virtq_modify(virtq, 1))
305                 goto error;
306         virtq->priv = priv;
307         rte_write32(virtq->index, priv->virtq_db_addr);
308         /* Setup doorbell mapping. */
309         virtq->intr_handle.fd = vq.kickfd;
310         if (virtq->intr_handle.fd == -1) {
311                 DRV_LOG(WARNING, "Virtq %d kickfd is invalid.", index);
312         } else {
313                 virtq->intr_handle.type = RTE_INTR_HANDLE_EXT;
314                 if (rte_intr_callback_register(&virtq->intr_handle,
315                                                mlx5_vdpa_virtq_handler,
316                                                virtq)) {
317                         virtq->intr_handle.fd = -1;
318                         DRV_LOG(ERR, "Failed to register virtq %d interrupt.",
319                                 index);
320                         goto error;
321                 } else {
322                         DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
323                                 virtq->intr_handle.fd, index);
324                 }
325         }
326         DRV_LOG(DEBUG, "vid %u virtq %u was created successfully.", priv->vid,
327                 index);
328         return 0;
329 error:
330         mlx5_vdpa_virtq_unset(virtq);
331         return -1;
332 }
333
334 static int
335 mlx5_vdpa_features_validate(struct mlx5_vdpa_priv *priv)
336 {
337         if (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) {
338                 if (!(priv->caps.virtio_queue_type & (1 <<
339                                                      MLX5_VIRTQ_TYPE_PACKED))) {
340                         DRV_LOG(ERR, "Failed to configur PACKED mode for vdev "
341                                 "%d - it was not reported by HW/driver"
342                                 " capability.", priv->vid);
343                         return -ENOTSUP;
344                 }
345         }
346         if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) {
347                 if (!priv->caps.tso_ipv4) {
348                         DRV_LOG(ERR, "Failed to enable TSO4 for vdev %d - TSO4"
349                                 " was not reported by HW/driver capability.",
350                                 priv->vid);
351                         return -ENOTSUP;
352                 }
353         }
354         if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) {
355                 if (!priv->caps.tso_ipv6) {
356                         DRV_LOG(ERR, "Failed to enable TSO6 for vdev %d - TSO6"
357                                 " was not reported by HW/driver capability.",
358                                 priv->vid);
359                         return -ENOTSUP;
360                 }
361         }
362         if (priv->features & (1ULL << VIRTIO_NET_F_CSUM)) {
363                 if (!priv->caps.tx_csum) {
364                         DRV_LOG(ERR, "Failed to enable CSUM for vdev %d - CSUM"
365                                 " was not reported by HW/driver capability.",
366                                 priv->vid);
367                         return -ENOTSUP;
368                 }
369         }
370         if (priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
371                 if (!priv->caps.rx_csum) {
372                         DRV_LOG(ERR, "Failed to enable GUEST CSUM for vdev %d"
373                                 " GUEST CSUM was not reported by HW/driver "
374                                 "capability.", priv->vid);
375                         return -ENOTSUP;
376                 }
377         }
378         if (priv->features & (1ULL << VIRTIO_F_VERSION_1)) {
379                 if (!priv->caps.virtio_version_1_0) {
380                         DRV_LOG(ERR, "Failed to enable version 1 for vdev %d "
381                                 "version 1 was not reported by HW/driver"
382                                 " capability.", priv->vid);
383                         return -ENOTSUP;
384                 }
385         }
386         return 0;
387 }
388
389 int
390 mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
391 {
392         struct mlx5_devx_tis_attr tis_attr = {0};
393         uint32_t i;
394         uint16_t nr_vring = rte_vhost_get_vring_num(priv->vid);
395         int ret = rte_vhost_get_negotiated_features(priv->vid, &priv->features);
396
397         if (ret || mlx5_vdpa_features_validate(priv)) {
398                 DRV_LOG(ERR, "Failed to configure negotiated features.");
399                 return -1;
400         }
401         if (nr_vring > priv->caps.max_num_virtio_queues * 2) {
402                 DRV_LOG(ERR, "Do not support more than %d virtqs(%d).",
403                         (int)priv->caps.max_num_virtio_queues * 2,
404                         (int)nr_vring);
405                 return -1;
406         }
407         /* Always map the entire page. */
408         priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
409                                    PROT_WRITE, MAP_SHARED, priv->ctx->cmd_fd,
410                                    priv->var->mmap_off);
411         if (priv->virtq_db_addr == MAP_FAILED) {
412                 DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
413                 priv->virtq_db_addr = NULL;
414                 goto error;
415         } else {
416                 DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
417                         priv->virtq_db_addr);
418         }
419         priv->td = mlx5_devx_cmd_create_td(priv->ctx);
420         if (!priv->td) {
421                 DRV_LOG(ERR, "Failed to create transport domain.");
422                 return -rte_errno;
423         }
424         tis_attr.transport_domain = priv->td->id;
425         priv->tis = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
426         if (!priv->tis) {
427                 DRV_LOG(ERR, "Failed to create TIS.");
428                 goto error;
429         }
430         priv->nr_virtqs = nr_vring;
431         for (i = 0; i < nr_vring; i++)
432                 if (priv->virtqs[i].enable && mlx5_vdpa_virtq_setup(priv, i))
433                         goto error;
434         return 0;
435 error:
436         mlx5_vdpa_virtqs_release(priv);
437         return -1;
438 }
439
440 static int
441 mlx5_vdpa_virtq_is_modified(struct mlx5_vdpa_priv *priv,
442                             struct mlx5_vdpa_virtq *virtq)
443 {
444         struct rte_vhost_vring vq;
445         int ret = rte_vhost_get_vhost_vring(priv->vid, virtq->index, &vq);
446
447         if (ret)
448                 return -1;
449         if (vq.size != virtq->vq_size || vq.kickfd != virtq->intr_handle.fd)
450                 return 1;
451         if (virtq->eqp.cq.cq) {
452                 if (vq.callfd != virtq->eqp.cq.callfd)
453                         return 1;
454         } else if (vq.callfd != -1) {
455                 return 1;
456         }
457         return 0;
458 }
459
460 int
461 mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable)
462 {
463         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
464         int ret;
465
466         DRV_LOG(INFO, "Update virtq %d status %sable -> %sable.", index,
467                 virtq->enable ? "en" : "dis", enable ? "en" : "dis");
468         if (!priv->configured) {
469                 virtq->enable = !!enable;
470                 return 0;
471         }
472         if (virtq->enable == !!enable) {
473                 if (!enable)
474                         return 0;
475                 ret = mlx5_vdpa_virtq_is_modified(priv, virtq);
476                 if (ret < 0) {
477                         DRV_LOG(ERR, "Virtq %d modify check failed.", index);
478                         return -1;
479                 }
480                 if (ret == 0)
481                         return 0;
482                 DRV_LOG(INFO, "Virtq %d was modified, recreate it.", index);
483         }
484         if (virtq->virtq) {
485                 ret = mlx5_vdpa_virtq_stop(priv, index);
486                 if (ret)
487                         DRV_LOG(WARNING, "Failed to stop virtq %d.", index);
488                 mlx5_vdpa_virtq_unset(virtq);
489         }
490         if (enable) {
491                 ret = mlx5_vdpa_virtq_setup(priv, index);
492                 if (ret) {
493                         DRV_LOG(ERR, "Failed to setup virtq %d.", index);
494                         return ret;
495                 }
496         }
497         virtq->enable = !!enable;
498         if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
499                 /* Need to add received virtq to the RQT table of the TIRs. */
500                 ret = mlx5_vdpa_steer_update(priv);
501                 if (ret) {
502                         virtq->enable = !enable;
503                         return ret;
504                 }
505         }
506         return 0;
507 }
508
509 int
510 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
511                           struct rte_vdpa_stat *stats, unsigned int n)
512 {
513         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
514         struct mlx5_devx_virtio_q_couners_attr attr = {0};
515         int ret;
516
517         if (!virtq->virtq || !virtq->enable) {
518                 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
519                         "is invalid.", qid);
520                 return -EINVAL;
521         }
522         MLX5_ASSERT(virtq->counters);
523         ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters, &attr);
524         if (ret) {
525                 DRV_LOG(ERR, "Failed to read virtq %d stats from HW.", qid);
526                 return ret;
527         }
528         ret = (int)RTE_MIN(n, (unsigned int)MLX5_VDPA_STATS_MAX);
529         if (ret == MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS)
530                 return ret;
531         stats[MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS] = (struct rte_vdpa_stat) {
532                 .id = MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS,
533                 .value = attr.received_desc - virtq->reset.received_desc,
534         };
535         if (ret == MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS)
536                 return ret;
537         stats[MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS] = (struct rte_vdpa_stat) {
538                 .id = MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS,
539                 .value = attr.completed_desc - virtq->reset.completed_desc,
540         };
541         if (ret == MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS)
542                 return ret;
543         stats[MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS] = (struct rte_vdpa_stat) {
544                 .id = MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS,
545                 .value = attr.bad_desc_errors - virtq->reset.bad_desc_errors,
546         };
547         if (ret == MLX5_VDPA_STATS_EXCEED_MAX_CHAIN)
548                 return ret;
549         stats[MLX5_VDPA_STATS_EXCEED_MAX_CHAIN] = (struct rte_vdpa_stat) {
550                 .id = MLX5_VDPA_STATS_EXCEED_MAX_CHAIN,
551                 .value = attr.exceed_max_chain - virtq->reset.exceed_max_chain,
552         };
553         if (ret == MLX5_VDPA_STATS_INVALID_BUFFER)
554                 return ret;
555         stats[MLX5_VDPA_STATS_INVALID_BUFFER] = (struct rte_vdpa_stat) {
556                 .id = MLX5_VDPA_STATS_INVALID_BUFFER,
557                 .value = attr.invalid_buffer - virtq->reset.invalid_buffer,
558         };
559         if (ret == MLX5_VDPA_STATS_COMPLETION_ERRORS)
560                 return ret;
561         stats[MLX5_VDPA_STATS_COMPLETION_ERRORS] = (struct rte_vdpa_stat) {
562                 .id = MLX5_VDPA_STATS_COMPLETION_ERRORS,
563                 .value = attr.error_cqes - virtq->reset.error_cqes,
564         };
565         return ret;
566 }
567
568 int
569 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid)
570 {
571         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
572         int ret;
573
574         if (!virtq->virtq || !virtq->enable) {
575                 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
576                         "is invalid.", qid);
577                 return -EINVAL;
578         }
579         MLX5_ASSERT(virtq->counters);
580         ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters,
581                                                     &virtq->reset);
582         if (ret)
583                 DRV_LOG(ERR, "Failed to read virtq %d reset stats from HW.",
584                         qid);
585         return ret;
586 }