1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
7 #include <sys/eventfd.h>
9 #include <rte_malloc.h>
10 #include <rte_errno.h>
13 #include <mlx5_common.h>
15 #include "mlx5_vdpa_utils.h"
16 #include "mlx5_vdpa.h"
20 mlx5_vdpa_virtq_handler(void *cb_arg)
22 struct mlx5_vdpa_virtq *virtq = cb_arg;
23 struct mlx5_vdpa_priv *priv = virtq->priv;
28 nbytes = read(rte_intr_fd_get(virtq->intr_handle), &buf,
32 errno == EWOULDBLOCK ||
35 DRV_LOG(ERR, "Failed to read kickfd of virtq %d: %s",
36 virtq->index, strerror(errno));
40 rte_write32(virtq->index, priv->virtq_db_addr);
41 if (virtq->notifier_state == MLX5_VDPA_NOTIFIER_STATE_DISABLED) {
42 if (rte_vhost_host_notifier_ctrl(priv->vid, virtq->index, true))
43 virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_ERR;
45 virtq->notifier_state =
46 MLX5_VDPA_NOTIFIER_STATE_ENABLED;
47 DRV_LOG(INFO, "Virtq %u notifier state is %s.", virtq->index,
48 virtq->notifier_state ==
49 MLX5_VDPA_NOTIFIER_STATE_ENABLED ? "enabled" :
52 DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index);
56 mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
59 int retries = MLX5_VDPA_INTR_RETRIES;
62 if (rte_intr_fd_get(virtq->intr_handle) != -1) {
63 while (retries-- && ret == -EAGAIN) {
64 ret = rte_intr_callback_unregister(virtq->intr_handle,
65 mlx5_vdpa_virtq_handler,
68 DRV_LOG(DEBUG, "Try again to unregister fd %d "
69 "of virtq %d interrupt, retries = %d.",
70 rte_intr_fd_get(virtq->intr_handle),
71 (int)virtq->index, retries);
73 usleep(MLX5_VDPA_INTR_RETRIES_USEC);
76 rte_intr_fd_set(virtq->intr_handle, -1);
78 rte_intr_instance_free(virtq->intr_handle);
80 ret = mlx5_vdpa_virtq_stop(virtq->priv, virtq->index);
82 DRV_LOG(WARNING, "Failed to stop virtq %d.",
84 claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
87 for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
88 if (virtq->umems[i].obj)
89 claim_zero(mlx5_glue->devx_umem_dereg
90 (virtq->umems[i].obj));
91 if (virtq->umems[i].buf)
92 rte_free(virtq->umems[i].buf);
94 memset(&virtq->umems, 0, sizeof(virtq->umems));
96 mlx5_vdpa_event_qp_destroy(&virtq->eqp);
97 virtq->notifier_state = MLX5_VDPA_NOTIFIER_STATE_DISABLED;
102 mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
105 struct mlx5_vdpa_virtq *virtq;
107 for (i = 0; i < priv->nr_virtqs; i++) {
108 virtq = &priv->virtqs[i];
109 mlx5_vdpa_virtq_unset(virtq);
111 claim_zero(mlx5_devx_cmd_destroy(virtq->counters));
113 for (i = 0; i < priv->num_lag_ports; i++) {
115 claim_zero(mlx5_devx_cmd_destroy(priv->tiss[i]));
116 priv->tiss[i] = NULL;
120 claim_zero(mlx5_devx_cmd_destroy(priv->td));
123 if (priv->virtq_db_addr) {
124 claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
125 priv->virtq_db_addr = NULL;
128 memset(priv->virtqs, 0, sizeof(*virtq) * priv->nr_virtqs);
133 mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
135 struct mlx5_devx_virtq_attr attr = {
136 .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
137 .state = state ? MLX5_VIRTQ_STATE_RDY :
138 MLX5_VIRTQ_STATE_SUSPEND,
139 .queue_index = virtq->index,
142 return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
146 mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index)
148 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
153 ret = mlx5_vdpa_virtq_modify(virtq, 0);
156 virtq->stopped = true;
157 DRV_LOG(DEBUG, "vid %u virtq %u was stopped.", priv->vid, index);
158 return mlx5_vdpa_virtq_query(priv, index);
162 mlx5_vdpa_virtq_query(struct mlx5_vdpa_priv *priv, int index)
164 struct mlx5_devx_virtq_attr attr = {0};
165 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
168 if (mlx5_devx_cmd_query_virtq(virtq->virtq, &attr)) {
169 DRV_LOG(ERR, "Failed to query virtq %d.", index);
172 DRV_LOG(INFO, "Query vid %d vring %d: hw_available_idx=%d, "
173 "hw_used_index=%d", priv->vid, index,
174 attr.hw_available_index, attr.hw_used_index);
175 ret = rte_vhost_set_vring_base(priv->vid, index,
176 attr.hw_available_index,
179 DRV_LOG(ERR, "Failed to set virtq %d base.", index);
182 if (attr.state == MLX5_VIRTQ_STATE_ERROR)
183 DRV_LOG(WARNING, "vid %d vring %d hw error=%hhu",
184 priv->vid, index, attr.error_type);
189 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
191 struct rte_vhost_mem_region *reg;
195 for (i = 0; i < mem->nregions; i++) {
196 reg = &mem->regions[i];
197 if (hva >= reg->host_user_addr &&
198 hva < reg->host_user_addr + reg->size) {
199 gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
207 mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
209 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
210 struct rte_vhost_vring vq;
211 struct mlx5_devx_virtq_attr attr = {0};
215 uint16_t last_avail_idx;
216 uint16_t last_used_idx;
217 uint16_t event_num = MLX5_EVENT_TYPE_OBJECT_CHANGE;
220 ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
223 virtq->index = index;
224 virtq->vq_size = vq.size;
225 attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4));
226 attr.tso_ipv6 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6));
227 attr.tx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_CSUM));
228 attr.rx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM));
229 attr.virtio_version_1_0 = !!(priv->features & (1ULL <<
230 VIRTIO_F_VERSION_1));
231 attr.type = (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) ?
232 MLX5_VIRTQ_TYPE_PACKED : MLX5_VIRTQ_TYPE_SPLIT;
234 * No need event QPs creation when the guest in poll mode or when the
235 * capability allows it.
237 attr.event_mode = vq.callfd != -1 || !(priv->caps.event_mode & (1 <<
238 MLX5_VIRTQ_EVENT_MODE_NO_MSIX)) ?
239 MLX5_VIRTQ_EVENT_MODE_QP :
240 MLX5_VIRTQ_EVENT_MODE_NO_MSIX;
241 if (attr.event_mode == MLX5_VIRTQ_EVENT_MODE_QP) {
242 ret = mlx5_vdpa_event_qp_create(priv, vq.size, vq.callfd,
245 DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
249 attr.qp_id = virtq->eqp.fw_qp->id;
251 DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
252 " need event QPs and event mechanism.", index);
254 if (priv->caps.queue_counters_valid) {
255 if (!virtq->counters)
256 virtq->counters = mlx5_devx_cmd_create_virtio_q_counters
258 if (!virtq->counters) {
259 DRV_LOG(ERR, "Failed to create virtq couners for virtq"
263 attr.counters_obj_id = virtq->counters->id;
265 /* Setup 3 UMEMs for each virtq. */
266 for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
267 virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
268 priv->caps.umems[i].b;
269 virtq->umems[i].buf = rte_zmalloc(__func__,
270 virtq->umems[i].size, 4096);
271 if (!virtq->umems[i].buf) {
272 DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
276 virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->cdev->ctx,
278 virtq->umems[i].size,
279 IBV_ACCESS_LOCAL_WRITE);
280 if (!virtq->umems[i].obj) {
281 DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
285 attr.umems[i].id = virtq->umems[i].obj->umem_id;
286 attr.umems[i].offset = 0;
287 attr.umems[i].size = virtq->umems[i].size;
289 if (attr.type == MLX5_VIRTQ_TYPE_SPLIT) {
290 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
291 (uint64_t)(uintptr_t)vq.desc);
293 DRV_LOG(ERR, "Failed to get descriptor ring GPA.");
296 attr.desc_addr = gpa;
297 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
298 (uint64_t)(uintptr_t)vq.used);
300 DRV_LOG(ERR, "Failed to get GPA for used ring.");
303 attr.used_addr = gpa;
304 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
305 (uint64_t)(uintptr_t)vq.avail);
307 DRV_LOG(ERR, "Failed to get GPA for available ring.");
310 attr.available_addr = gpa;
312 ret = rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
317 DRV_LOG(WARNING, "Couldn't get vring base, idx are set to 0");
319 DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
320 "virtq %d.", priv->vid, last_avail_idx,
321 last_used_idx, index);
323 attr.hw_available_index = last_avail_idx;
324 attr.hw_used_index = last_used_idx;
325 attr.q_size = vq.size;
326 attr.mkey = priv->gpa_mkey_index;
327 attr.tis_id = priv->tiss[(index / 2) % priv->num_lag_ports]->id;
328 attr.queue_index = index;
329 attr.pd = priv->cdev->pdn;
330 attr.hw_latency_mode = priv->hw_latency_mode;
331 attr.hw_max_latency_us = priv->hw_max_latency_us;
332 attr.hw_max_pending_comp = priv->hw_max_pending_comp;
333 virtq->virtq = mlx5_devx_cmd_create_virtq(priv->cdev->ctx, &attr);
337 claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
338 if (mlx5_vdpa_virtq_modify(virtq, 1))
341 rte_write32(virtq->index, priv->virtq_db_addr);
342 /* Setup doorbell mapping. */
344 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
345 if (virtq->intr_handle == NULL) {
346 DRV_LOG(ERR, "Fail to allocate intr_handle");
350 if (rte_intr_fd_set(virtq->intr_handle, vq.kickfd))
353 if (rte_intr_fd_get(virtq->intr_handle) == -1) {
354 DRV_LOG(WARNING, "Virtq %d kickfd is invalid.", index);
356 if (rte_intr_type_set(virtq->intr_handle, RTE_INTR_HANDLE_EXT))
359 if (rte_intr_callback_register(virtq->intr_handle,
360 mlx5_vdpa_virtq_handler,
362 rte_intr_fd_set(virtq->intr_handle, -1);
363 DRV_LOG(ERR, "Failed to register virtq %d interrupt.",
367 DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
368 rte_intr_fd_get(virtq->intr_handle),
372 /* Subscribe virtq error event. */
374 cookie = ((uint64_t)virtq->version << 32) + index;
375 ret = mlx5_glue->devx_subscribe_devx_event(priv->err_chnl,
380 DRV_LOG(ERR, "Failed to subscribe device %d virtq %d error event.",
385 virtq->stopped = false;
386 /* Initial notification to ask Qemu handling completed buffers. */
387 if (virtq->eqp.cq.callfd != -1)
388 eventfd_write(virtq->eqp.cq.callfd, (eventfd_t)1);
389 DRV_LOG(DEBUG, "vid %u virtq %u was created successfully.", priv->vid,
393 mlx5_vdpa_virtq_unset(virtq);
398 mlx5_vdpa_features_validate(struct mlx5_vdpa_priv *priv)
400 if (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) {
401 if (!(priv->caps.virtio_queue_type & (1 <<
402 MLX5_VIRTQ_TYPE_PACKED))) {
403 DRV_LOG(ERR, "Failed to configur PACKED mode for vdev "
404 "%d - it was not reported by HW/driver"
405 " capability.", priv->vid);
409 if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) {
410 if (!priv->caps.tso_ipv4) {
411 DRV_LOG(ERR, "Failed to enable TSO4 for vdev %d - TSO4"
412 " was not reported by HW/driver capability.",
417 if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) {
418 if (!priv->caps.tso_ipv6) {
419 DRV_LOG(ERR, "Failed to enable TSO6 for vdev %d - TSO6"
420 " was not reported by HW/driver capability.",
425 if (priv->features & (1ULL << VIRTIO_NET_F_CSUM)) {
426 if (!priv->caps.tx_csum) {
427 DRV_LOG(ERR, "Failed to enable CSUM for vdev %d - CSUM"
428 " was not reported by HW/driver capability.",
433 if (priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
434 if (!priv->caps.rx_csum) {
435 DRV_LOG(ERR, "Failed to enable GUEST CSUM for vdev %d"
436 " GUEST CSUM was not reported by HW/driver "
437 "capability.", priv->vid);
441 if (priv->features & (1ULL << VIRTIO_F_VERSION_1)) {
442 if (!priv->caps.virtio_version_1_0) {
443 DRV_LOG(ERR, "Failed to enable version 1 for vdev %d "
444 "version 1 was not reported by HW/driver"
445 " capability.", priv->vid);
453 mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
455 struct mlx5_devx_tis_attr tis_attr = {0};
456 struct ibv_context *ctx = priv->cdev->ctx;
458 uint16_t nr_vring = rte_vhost_get_vring_num(priv->vid);
459 int ret = rte_vhost_get_negotiated_features(priv->vid, &priv->features);
461 if (ret || mlx5_vdpa_features_validate(priv)) {
462 DRV_LOG(ERR, "Failed to configure negotiated features.");
465 if ((priv->features & (1ULL << VIRTIO_NET_F_CSUM)) == 0 &&
466 ((priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) > 0 ||
467 (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) > 0)) {
468 /* Packet may be corrupted if TSO is enabled without CSUM. */
469 DRV_LOG(INFO, "TSO is enabled without CSUM, force CSUM.");
470 priv->features |= (1ULL << VIRTIO_NET_F_CSUM);
472 if (nr_vring > priv->caps.max_num_virtio_queues * 2) {
473 DRV_LOG(ERR, "Do not support more than %d virtqs(%d).",
474 (int)priv->caps.max_num_virtio_queues * 2,
478 /* Always map the entire page. */
479 priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
480 PROT_WRITE, MAP_SHARED, ctx->cmd_fd,
481 priv->var->mmap_off);
482 if (priv->virtq_db_addr == MAP_FAILED) {
483 DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
484 priv->virtq_db_addr = NULL;
487 DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
488 priv->virtq_db_addr);
490 priv->td = mlx5_devx_cmd_create_td(ctx);
492 DRV_LOG(ERR, "Failed to create transport domain.");
495 tis_attr.transport_domain = priv->td->id;
496 for (i = 0; i < priv->num_lag_ports; i++) {
497 /* 0 is auto affinity, non-zero value to propose port. */
498 tis_attr.lag_tx_port_affinity = i + 1;
499 priv->tiss[i] = mlx5_devx_cmd_create_tis(ctx, &tis_attr);
500 if (!priv->tiss[i]) {
501 DRV_LOG(ERR, "Failed to create TIS %u.", i);
505 priv->nr_virtqs = nr_vring;
506 for (i = 0; i < nr_vring; i++)
507 if (priv->virtqs[i].enable && mlx5_vdpa_virtq_setup(priv, i))
511 mlx5_vdpa_virtqs_release(priv);
516 mlx5_vdpa_virtq_is_modified(struct mlx5_vdpa_priv *priv,
517 struct mlx5_vdpa_virtq *virtq)
519 struct rte_vhost_vring vq;
520 int ret = rte_vhost_get_vhost_vring(priv->vid, virtq->index, &vq);
524 if (vq.size != virtq->vq_size || vq.kickfd !=
525 rte_intr_fd_get(virtq->intr_handle))
527 if (virtq->eqp.cq.cq_obj.cq) {
528 if (vq.callfd != virtq->eqp.cq.callfd)
530 } else if (vq.callfd != -1) {
537 mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable)
539 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
542 DRV_LOG(INFO, "Update virtq %d status %sable -> %sable.", index,
543 virtq->enable ? "en" : "dis", enable ? "en" : "dis");
544 if (!priv->configured) {
545 virtq->enable = !!enable;
548 if (virtq->enable == !!enable) {
551 ret = mlx5_vdpa_virtq_is_modified(priv, virtq);
553 DRV_LOG(ERR, "Virtq %d modify check failed.", index);
558 DRV_LOG(INFO, "Virtq %d was modified, recreate it.", index);
562 if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
563 ret = mlx5_vdpa_steer_update(priv);
565 DRV_LOG(WARNING, "Failed to disable steering "
566 "for virtq %d.", index);
568 mlx5_vdpa_virtq_unset(virtq);
571 ret = mlx5_vdpa_virtq_setup(priv, index);
573 DRV_LOG(ERR, "Failed to setup virtq %d.", index);
577 if (is_virtq_recvq(virtq->index, priv->nr_virtqs)) {
578 ret = mlx5_vdpa_steer_update(priv);
580 DRV_LOG(WARNING, "Failed to enable steering "
581 "for virtq %d.", index);
588 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
589 struct rte_vdpa_stat *stats, unsigned int n)
591 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
592 struct mlx5_devx_virtio_q_couners_attr attr = {0};
595 if (!virtq->counters) {
596 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
600 ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters, &attr);
602 DRV_LOG(ERR, "Failed to read virtq %d stats from HW.", qid);
605 ret = (int)RTE_MIN(n, (unsigned int)MLX5_VDPA_STATS_MAX);
606 if (ret == MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS)
608 stats[MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS] = (struct rte_vdpa_stat) {
609 .id = MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS,
610 .value = attr.received_desc - virtq->reset.received_desc,
612 if (ret == MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS)
614 stats[MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS] = (struct rte_vdpa_stat) {
615 .id = MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS,
616 .value = attr.completed_desc - virtq->reset.completed_desc,
618 if (ret == MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS)
620 stats[MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS] = (struct rte_vdpa_stat) {
621 .id = MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS,
622 .value = attr.bad_desc_errors - virtq->reset.bad_desc_errors,
624 if (ret == MLX5_VDPA_STATS_EXCEED_MAX_CHAIN)
626 stats[MLX5_VDPA_STATS_EXCEED_MAX_CHAIN] = (struct rte_vdpa_stat) {
627 .id = MLX5_VDPA_STATS_EXCEED_MAX_CHAIN,
628 .value = attr.exceed_max_chain - virtq->reset.exceed_max_chain,
630 if (ret == MLX5_VDPA_STATS_INVALID_BUFFER)
632 stats[MLX5_VDPA_STATS_INVALID_BUFFER] = (struct rte_vdpa_stat) {
633 .id = MLX5_VDPA_STATS_INVALID_BUFFER,
634 .value = attr.invalid_buffer - virtq->reset.invalid_buffer,
636 if (ret == MLX5_VDPA_STATS_COMPLETION_ERRORS)
638 stats[MLX5_VDPA_STATS_COMPLETION_ERRORS] = (struct rte_vdpa_stat) {
639 .id = MLX5_VDPA_STATS_COMPLETION_ERRORS,
640 .value = attr.error_cqes - virtq->reset.error_cqes,
646 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid)
648 struct mlx5_vdpa_virtq *virtq = &priv->virtqs[qid];
651 if (!virtq->counters) {
652 DRV_LOG(ERR, "Failed to read virtq %d statistics - virtq "
656 ret = mlx5_devx_cmd_query_virtio_q_counters(virtq->counters,
659 DRV_LOG(ERR, "Failed to read virtq %d reset stats from HW.",