1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
8 #include <rte_malloc.h>
12 #include <mlx5_common.h>
14 #include "mlx5_vdpa_utils.h"
15 #include "mlx5_vdpa.h"
19 mlx5_vdpa_virtq_handler(void *cb_arg)
21 struct mlx5_vdpa_virtq *virtq = cb_arg;
22 struct mlx5_vdpa_priv *priv = virtq->priv;
27 nbytes = read(virtq->intr_handle.fd, &buf, 8);
30 errno == EWOULDBLOCK ||
33 DRV_LOG(ERR, "Failed to read kickfd of virtq %d: %s",
34 virtq->index, strerror(errno));
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;
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" :
50 DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index);
54 mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
57 int retries = MLX5_VDPA_INTR_RETRIES;
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,
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);
73 virtq->intr_handle.fd = -1;
76 ret = mlx5_vdpa_virtq_stop(virtq->priv, virtq->index);
78 DRV_LOG(WARNING, "Failed to stop virtq %d.",
80 claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
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);
90 memset(&virtq->umems, 0, sizeof(virtq->umems));
91 if (virtq->counters) {
92 claim_zero(mlx5_devx_cmd_destroy(virtq->counters));
93 virtq->counters = NULL;
95 memset(&virtq->reset, 0, sizeof(virtq->reset));
97 mlx5_vdpa_event_qp_destroy(&virtq->eqp);
98 virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_DISABLED;
103 mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
107 for (i = 0; i < priv->nr_virtqs; i++)
108 mlx5_vdpa_virtq_unset(&priv->virtqs[i]);
110 claim_zero(mlx5_devx_cmd_destroy(priv->tis));
114 claim_zero(mlx5_devx_cmd_destroy(priv->td));
117 if (priv->virtq_db_addr) {
118 claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
119 priv->virtq_db_addr = NULL;
126 mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
128 struct mlx5_devx_virtq_attr attr = {
129 .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
130 .state = state ? MLX5_VIRTQ_STATE_RDY :
131 MLX5_VIRTQ_STATE_SUSPEND,
132 .queue_index = virtq->index,
135 return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
139 mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index)
141 struct mlx5_devx_virtq_attr attr = {0};
142 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
147 ret = mlx5_vdpa_virtq_modify(virtq, 0);
150 virtq->stopped = true;
151 if (mlx5_devx_cmd_query_virtq(virtq->virtq, &attr)) {
152 DRV_LOG(ERR, "Failed to query virtq %d.", index);
155 DRV_LOG(INFO, "Query vid %d vring %d: hw_available_idx=%d, "
156 "hw_used_index=%d", priv->vid, index,
157 attr.hw_available_index, attr.hw_used_index);
158 ret = rte_vhost_set_vring_base(priv->vid, index,
159 attr.hw_available_index,
162 DRV_LOG(ERR, "Failed to set virtq %d base.", index);
165 DRV_LOG(DEBUG, "vid %u virtq %u was stopped.", priv->vid, index);
170 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
172 struct rte_vhost_mem_region *reg;
176 for (i = 0; i < mem->nregions; i++) {
177 reg = &mem->regions[i];
178 if (hva >= reg->host_user_addr &&
179 hva < reg->host_user_addr + reg->size) {
180 gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
188 mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
190 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
191 struct rte_vhost_vring vq;
192 struct mlx5_devx_virtq_attr attr = {0};
196 uint16_t last_avail_idx;
197 uint16_t last_used_idx;
199 ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
202 virtq->index = index;
203 virtq->vq_size = vq.size;
204 attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4));
205 attr.tso_ipv6 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6));
206 attr.tx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_CSUM));
207 attr.rx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM));
208 attr.virtio_version_1_0 = !!(priv->features & (1ULL <<
209 VIRTIO_F_VERSION_1));
210 attr.type = (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) ?
211 MLX5_VIRTQ_TYPE_PACKED : MLX5_VIRTQ_TYPE_SPLIT;
213 * No need event QPs creation when the guest in poll mode or when the
214 * capability allows it.
216 attr.event_mode = vq.callfd != -1 || !(priv->caps.event_mode & (1 <<
217 MLX5_VIRTQ_EVENT_MODE_NO_MSIX)) ?
218 MLX5_VIRTQ_EVENT_MODE_QP :
219 MLX5_VIRTQ_EVENT_MODE_NO_MSIX;
220 if (attr.event_mode == MLX5_VIRTQ_EVENT_MODE_QP) {
221 ret = mlx5_vdpa_event_qp_create(priv, vq.size, vq.callfd,
224 DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
228 attr.qp_id = virtq->eqp.fw_qp->id;
230 DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
231 " need event QPs and event mechanism.", index);
233 if (priv->caps.queue_counters_valid) {
234 virtq->counters = mlx5_devx_cmd_create_virtio_q_counters
236 if (!virtq->counters) {
237 DRV_LOG(ERR, "Failed to create virtq couners for virtq"
241 attr.counters_obj_id = virtq->counters->id;
243 /* Setup 3 UMEMs for each virtq. */
244 for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
245 virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
246 priv->caps.umems[i].b;
247 virtq->umems[i].buf = rte_zmalloc(__func__,
248 virtq->umems[i].size, 4096);
249 if (!virtq->umems[i].buf) {
250 DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
254 virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->ctx,
256 virtq->umems[i].size,
257 IBV_ACCESS_LOCAL_WRITE);
258 if (!virtq->umems[i].obj) {
259 DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
263 attr.umems[i].id = virtq->umems[i].obj->umem_id;
264 attr.umems[i].offset = 0;
265 attr.umems[i].size = virtq->umems[i].size;
267 if (attr.type == MLX5_VIRTQ_TYPE_SPLIT) {
268 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
269 (uint64_t)(uintptr_t)vq.desc);
271 DRV_LOG(ERR, "Failed to get descriptor ring GPA.");
274 attr.desc_addr = gpa;
275 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
276 (uint64_t)(uintptr_t)vq.used);
278 DRV_LOG(ERR, "Failed to get GPA for used ring.");
281 attr.used_addr = gpa;
282 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
283 (uint64_t)(uintptr_t)vq.avail);
285 DRV_LOG(ERR, "Failed to get GPA for available ring.");
288 attr.available_addr = gpa;
290 ret = rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
295 DRV_LOG(WARNING, "Couldn't get vring base, idx are set to 0");
297 DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
298 "virtq %d.", priv->vid, last_avail_idx,
299 last_used_idx, index);
301 attr.hw_available_index = last_avail_idx;
302 attr.hw_used_index = last_used_idx;
303 attr.q_size = vq.size;
304 attr.mkey = priv->gpa_mkey_index;
305 attr.tis_id = priv->tis->id;
306 attr.queue_index = index;
308 virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
312 claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
313 if (mlx5_vdpa_virtq_modify(virtq, 1))
316 rte_write32(virtq->index, priv->virtq_db_addr);
317 /* Setup doorbell mapping. */
318 virtq->intr_handle.fd = vq.kickfd;
319 if (virtq->intr_handle.fd == -1) {
320 DRV_LOG(WARNING, "Virtq %d kickfd is invalid.", index);
322 virtq->intr_handle.type = RTE_INTR_HANDLE_EXT;
323 if (rte_intr_callback_register(&virtq->intr_handle,
324 mlx5_vdpa_virtq_handler,
326 virtq->intr_handle.fd = -1;
327 DRV_LOG(ERR, "Failed to register virtq %d interrupt.",
331 DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
332 virtq->intr_handle.fd, index);
335 virtq->stopped = false;
336 DRV_LOG(DEBUG, "vid %u virtq %u was created successfully.", priv->vid,
340 mlx5_vdpa_virtq_unset(virtq);
345 mlx5_vdpa_features_validate(struct mlx5_vdpa_priv *priv)
347 if (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) {
348 if (!(priv->caps.virtio_queue_type & (1 <<
349 MLX5_VIRTQ_TYPE_PACKED))) {
350 DRV_LOG(ERR, "Failed to configur PACKED mode for vdev "
351 "%d - it was not reported by HW/driver"
352 " capability.", priv->vid);
356 if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) {
357 if (!priv->caps.tso_ipv4) {
358 DRV_LOG(ERR, "Failed to enable TSO4 for vdev %d - TSO4"
359 " was not reported by HW/driver capability.",
364 if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) {
365 if (!priv->caps.tso_ipv6) {
366 DRV_LOG(ERR, "Failed to enable TSO6 for vdev %d - TSO6"
367 " was not reported by HW/driver capability.",
372 if (priv->features & (1ULL << VIRTIO_NET_F_CSUM)) {
373 if (!priv->caps.tx_csum) {
374 DRV_LOG(ERR, "Failed to enable CSUM for vdev %d - CSUM"
375 " was not reported by HW/driver capability.",
380 if (priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
381 if (!priv->caps.rx_csum) {
382 DRV_LOG(ERR, "Failed to enable GUEST CSUM for vdev %d"
383 " GUEST CSUM was not reported by HW/driver "
384 "capability.", priv->vid);
388 if (priv->features & (1ULL << VIRTIO_F_VERSION_1)) {
389 if (!priv->caps.virtio_version_1_0) {
390 DRV_LOG(ERR, "Failed to enable version 1 for vdev %d "
391 "version 1 was not reported by HW/driver"
392 " capability.", priv->vid);
400 mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
402 struct mlx5_devx_tis_attr tis_attr = {0};
404 uint16_t nr_vring = rte_vhost_get_vring_num(priv->vid);
405 int ret = rte_vhost_get_negotiated_features(priv->vid, &priv->features);
407 if (ret || mlx5_vdpa_features_validate(priv)) {
408 DRV_LOG(ERR, "Failed to configure negotiated features.");
411 if (nr_vring > priv->caps.max_num_virtio_queues * 2) {
412 DRV_LOG(ERR, "Do not support more than %d virtqs(%d).",
413 (int)priv->caps.max_num_virtio_queues * 2,
417 /* Always map the entire page. */
418 priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
419 PROT_WRITE, MAP_SHARED, priv->ctx->cmd_fd,
420 priv->var->mmap_off);
421 if (priv->virtq_db_addr == MAP_FAILED) {
422 DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
423 priv->virtq_db_addr = NULL;
426 DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
427 priv->virtq_db_addr);
429 priv->td = mlx5_devx_cmd_create_td(priv->ctx);
431 DRV_LOG(ERR, "Failed to create transport domain.");
434 tis_attr.transport_domain = priv->td->id;
435 priv->tis = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
437 DRV_LOG(ERR, "Failed to create TIS.");
440 priv->nr_virtqs = nr_vring;
441 for (i = 0; i < nr_vring; i++)
442 if (priv->virtqs[i].enable && mlx5_vdpa_virtq_setup(priv, i))
446 mlx5_vdpa_virtqs_release(priv);
451 mlx5_vdpa_virtq_is_modified(struct mlx5_vdpa_priv *priv,
452 struct mlx5_vdpa_virtq *virtq)
454 struct rte_vhost_vring vq;
455 int ret = rte_vhost_get_vhost_vring(priv->vid, virtq->index, &vq);
459 if (vq.size != virtq->vq_size || vq.kickfd != virtq->intr_handle.fd)
461 if (virtq->eqp.cq.cq) {
462 if (vq.callfd != virtq->eqp.cq.callfd)
464 } else if (vq.callfd != -1) {
471 mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable)
473 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
476 DRV_LOG(INFO, "Update virtq %d status %sable -> %sable.", index,
477 virtq->enable ? "en" : "dis", enable ? "en" : "dis");
478 if (!priv->configured) {
479 virtq->enable = !!enable;
482 if (virtq->enable == !!enable) {
485 ret = mlx5_vdpa_virtq_is_modified(priv, virtq);
487 DRV_LOG(ERR, "Virtq %d modify check failed.", index);
492 DRV_LOG(INFO, "Virtq %d was modified, recreate it.", index);
496 if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
497 ret = mlx5_vdpa_steer_update(priv);
499 DRV_LOG(WARNING, "Failed to disable steering "
500 "for virtq %d.", index);
502 mlx5_vdpa_virtq_unset(virtq);
505 ret = mlx5_vdpa_virtq_setup(priv, index);
507 DRV_LOG(ERR, "Failed to setup virtq %d.", index);
511 if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
512 ret = mlx5_vdpa_steer_update(priv);
514 DRV_LOG(WARNING, "Failed to enable steering "
515 "for virtq %d.", index);
522 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
523 struct rte_vdpa_stat *stats, unsigned int n)
525 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
526 struct mlx5_devx_virtio_q_couners_attr attr = {0};
529 if (!virtq->virtq || !virtq->enable) {
530 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
534 MLX5_ASSERT(virtq->counters);
535 ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters, &attr);
537 DRV_LOG(ERR, "Failed to read virtq %d stats from HW.", qid);
540 ret = (int)RTE_MIN(n, (unsigned int)MLX5_VDPA_STATS_MAX);
541 if (ret == MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS)
543 stats[MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS] = (struct rte_vdpa_stat) {
544 .id = MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS,
545 .value = attr.received_desc - virtq->reset.received_desc,
547 if (ret == MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS)
549 stats[MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS] = (struct rte_vdpa_stat) {
550 .id = MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS,
551 .value = attr.completed_desc - virtq->reset.completed_desc,
553 if (ret == MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS)
555 stats[MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS] = (struct rte_vdpa_stat) {
556 .id = MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS,
557 .value = attr.bad_desc_errors - virtq->reset.bad_desc_errors,
559 if (ret == MLX5_VDPA_STATS_EXCEED_MAX_CHAIN)
561 stats[MLX5_VDPA_STATS_EXCEED_MAX_CHAIN] = (struct rte_vdpa_stat) {
562 .id = MLX5_VDPA_STATS_EXCEED_MAX_CHAIN,
563 .value = attr.exceed_max_chain - virtq->reset.exceed_max_chain,
565 if (ret == MLX5_VDPA_STATS_INVALID_BUFFER)
567 stats[MLX5_VDPA_STATS_INVALID_BUFFER] = (struct rte_vdpa_stat) {
568 .id = MLX5_VDPA_STATS_INVALID_BUFFER,
569 .value = attr.invalid_buffer - virtq->reset.invalid_buffer,
571 if (ret == MLX5_VDPA_STATS_COMPLETION_ERRORS)
573 stats[MLX5_VDPA_STATS_COMPLETION_ERRORS] = (struct rte_vdpa_stat) {
574 .id = MLX5_VDPA_STATS_COMPLETION_ERRORS,
575 .value = attr.error_cqes - virtq->reset.error_cqes,
581 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid)
583 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
586 if (!virtq->virtq || !virtq->enable) {
587 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
591 MLX5_ASSERT(virtq->counters);
592 ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters,
595 DRV_LOG(ERR, "Failed to read virtq %d reset stats from HW.",