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 DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index);
43 mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
46 int retries = MLX5_VDPA_INTR_RETRIES;
49 if (virtq->intr_handle.fd) {
50 while (retries-- && ret == -EAGAIN) {
51 ret = rte_intr_callback_unregister(&virtq->intr_handle,
52 mlx5_vdpa_virtq_handler,
55 DRV_LOG(DEBUG, "Try again to unregister fd %d "
56 "of virtq %d interrupt, retries = %d.",
57 virtq->intr_handle.fd,
58 (int)virtq->index, retries);
59 usleep(MLX5_VDPA_INTR_RETRIES_USEC);
62 memset(&virtq->intr_handle, 0, sizeof(virtq->intr_handle));
65 claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
68 for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
69 if (virtq->umems[i].obj)
70 claim_zero(mlx5_glue->devx_umem_dereg
71 (virtq->umems[i].obj));
72 if (virtq->umems[i].buf)
73 rte_free(virtq->umems[i].buf);
75 memset(&virtq->umems, 0, sizeof(virtq->umems));
77 mlx5_vdpa_event_qp_destroy(&virtq->eqp);
82 mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
84 struct mlx5_vdpa_virtq *entry;
85 struct mlx5_vdpa_virtq *next;
87 entry = SLIST_FIRST(&priv->virtq_list);
89 next = SLIST_NEXT(entry, next);
90 mlx5_vdpa_virtq_unset(entry);
91 SLIST_REMOVE(&priv->virtq_list, entry, mlx5_vdpa_virtq, next);
95 SLIST_INIT(&priv->virtq_list);
97 claim_zero(mlx5_devx_cmd_destroy(priv->tis));
101 claim_zero(mlx5_devx_cmd_destroy(priv->td));
104 if (priv->virtq_db_addr) {
105 claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
106 priv->virtq_db_addr = NULL;
109 mlx5_glue->dv_free_var(priv->var);
116 mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
118 struct mlx5_devx_virtq_attr attr = {
119 .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
120 .state = state ? MLX5_VIRTQ_STATE_RDY :
121 MLX5_VIRTQ_STATE_SUSPEND,
122 .queue_index = virtq->index,
125 return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
129 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
131 struct rte_vhost_mem_region *reg;
135 for (i = 0; i < mem->nregions; i++) {
136 reg = &mem->regions[i];
137 if (hva >= reg->host_user_addr &&
138 hva < reg->host_user_addr + reg->size) {
139 gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
147 mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv,
148 struct mlx5_vdpa_virtq *virtq, int index)
150 struct rte_vhost_vring vq;
151 struct mlx5_devx_virtq_attr attr = {0};
155 uint16_t last_avail_idx;
156 uint16_t last_used_idx;
158 ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
161 virtq->index = index;
162 virtq->vq_size = vq.size;
163 attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4));
164 attr.tso_ipv6 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6));
165 attr.tx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_CSUM));
166 attr.rx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM));
167 attr.virtio_version_1_0 = !!(priv->features & (1ULL <<
168 VIRTIO_F_VERSION_1));
169 attr.type = (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) ?
170 MLX5_VIRTQ_TYPE_PACKED : MLX5_VIRTQ_TYPE_SPLIT;
172 * No need event QPs creation when the guest in poll mode or when the
173 * capability allows it.
175 attr.event_mode = vq.callfd != -1 || !(priv->caps.event_mode & (1 <<
176 MLX5_VIRTQ_EVENT_MODE_NO_MSIX)) ?
177 MLX5_VIRTQ_EVENT_MODE_QP :
178 MLX5_VIRTQ_EVENT_MODE_NO_MSIX;
179 if (attr.event_mode == MLX5_VIRTQ_EVENT_MODE_QP) {
180 ret = mlx5_vdpa_event_qp_create(priv, vq.size, vq.callfd,
183 DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
187 attr.qp_id = virtq->eqp.fw_qp->id;
189 DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
190 " need event QPs and event mechanism.", index);
192 /* Setup 3 UMEMs for each virtq. */
193 for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
194 virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
195 priv->caps.umems[i].b;
196 virtq->umems[i].buf = rte_zmalloc(__func__,
197 virtq->umems[i].size, 4096);
198 if (!virtq->umems[i].buf) {
199 DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
203 virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->ctx,
205 virtq->umems[i].size,
206 IBV_ACCESS_LOCAL_WRITE);
207 if (!virtq->umems[i].obj) {
208 DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
212 attr.umems[i].id = virtq->umems[i].obj->umem_id;
213 attr.umems[i].offset = 0;
214 attr.umems[i].size = virtq->umems[i].size;
216 if (attr.type == MLX5_VIRTQ_TYPE_SPLIT) {
217 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
218 (uint64_t)(uintptr_t)vq.desc);
220 DRV_LOG(ERR, "Failed to get descriptor ring GPA.");
223 attr.desc_addr = gpa;
224 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
225 (uint64_t)(uintptr_t)vq.used);
227 DRV_LOG(ERR, "Failed to get GPA for used ring.");
230 attr.used_addr = gpa;
231 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
232 (uint64_t)(uintptr_t)vq.avail);
234 DRV_LOG(ERR, "Failed to get GPA for available ring.");
237 attr.available_addr = gpa;
239 rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
241 DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
242 "virtq %d.", priv->vid, last_avail_idx, last_used_idx, index);
243 attr.hw_available_index = last_avail_idx;
244 attr.hw_used_index = last_used_idx;
245 attr.q_size = vq.size;
246 attr.mkey = priv->gpa_mkey_index;
247 attr.tis_id = priv->tis->id;
248 attr.queue_index = index;
249 virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
253 if (mlx5_vdpa_virtq_modify(virtq, 1))
257 /* Be sure notifications are not missed during configuration. */
258 claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
259 rte_write32(virtq->index, priv->virtq_db_addr);
260 /* Setup doorbell mapping. */
261 virtq->intr_handle.fd = vq.kickfd;
262 virtq->intr_handle.type = RTE_INTR_HANDLE_EXT;
263 if (rte_intr_callback_register(&virtq->intr_handle,
264 mlx5_vdpa_virtq_handler, virtq)) {
265 virtq->intr_handle.fd = 0;
266 DRV_LOG(ERR, "Failed to register virtq %d interrupt.", index);
269 DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
270 virtq->intr_handle.fd, index);
274 mlx5_vdpa_virtq_unset(virtq);
279 mlx5_vdpa_features_validate(struct mlx5_vdpa_priv *priv)
281 if (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) {
282 if (!(priv->caps.virtio_queue_type & (1 <<
283 MLX5_VIRTQ_TYPE_PACKED))) {
284 DRV_LOG(ERR, "Failed to configur PACKED mode for vdev "
285 "%d - it was not reported by HW/driver"
286 " capability.", priv->vid);
290 if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) {
291 if (!priv->caps.tso_ipv4) {
292 DRV_LOG(ERR, "Failed to enable TSO4 for vdev %d - TSO4"
293 " was not reported by HW/driver capability.",
298 if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) {
299 if (!priv->caps.tso_ipv6) {
300 DRV_LOG(ERR, "Failed to enable TSO6 for vdev %d - TSO6"
301 " was not reported by HW/driver capability.",
306 if (priv->features & (1ULL << VIRTIO_NET_F_CSUM)) {
307 if (!priv->caps.tx_csum) {
308 DRV_LOG(ERR, "Failed to enable CSUM for vdev %d - CSUM"
309 " was not reported by HW/driver capability.",
314 if (priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
315 if (!priv->caps.rx_csum) {
316 DRV_LOG(ERR, "Failed to enable GUEST CSUM for vdev %d"
317 " GUEST CSUM was not reported by HW/driver "
318 "capability.", priv->vid);
322 if (priv->features & (1ULL << VIRTIO_F_VERSION_1)) {
323 if (!priv->caps.virtio_version_1_0) {
324 DRV_LOG(ERR, "Failed to enable version 1 for vdev %d "
325 "version 1 was not reported by HW/driver"
326 " capability.", priv->vid);
334 mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
336 struct mlx5_devx_tis_attr tis_attr = {0};
337 struct mlx5_vdpa_virtq *virtq;
339 uint16_t nr_vring = rte_vhost_get_vring_num(priv->vid);
340 int ret = rte_vhost_get_negotiated_features(priv->vid, &priv->features);
342 if (ret || mlx5_vdpa_features_validate(priv)) {
343 DRV_LOG(ERR, "Failed to configure negotiated features.");
346 priv->var = mlx5_glue->dv_alloc_var(priv->ctx, 0);
348 DRV_LOG(ERR, "Failed to allocate VAR %u.\n", errno);
351 /* Always map the entire page. */
352 priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
353 PROT_WRITE, MAP_SHARED, priv->ctx->cmd_fd,
354 priv->var->mmap_off);
355 if (priv->virtq_db_addr == MAP_FAILED) {
356 DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
357 priv->virtq_db_addr = NULL;
360 DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
361 priv->virtq_db_addr);
363 priv->td = mlx5_devx_cmd_create_td(priv->ctx);
365 DRV_LOG(ERR, "Failed to create transport domain.");
368 tis_attr.transport_domain = priv->td->id;
369 priv->tis = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
371 DRV_LOG(ERR, "Failed to create TIS.");
374 for (i = 0; i < nr_vring; i++) {
375 virtq = rte_zmalloc(__func__, sizeof(*virtq), 0);
376 if (!virtq || mlx5_vdpa_virtq_setup(priv, virtq, i)) {
381 SLIST_INSERT_HEAD(&priv->virtq_list, virtq, next);
383 priv->nr_virtqs = nr_vring;
386 mlx5_vdpa_virtqs_release(priv);