e07c82612a9a21640ab6c5c6a5ef85d92e1f5465
[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         DRV_LOG(DEBUG, "Ring virtq %u doorbell.", virtq->index);
40 }
41
42 static int
43 mlx5_vdpa_virtq_unset(struct mlx5_vdpa_virtq *virtq)
44 {
45         unsigned int i;
46         int retries = MLX5_VDPA_INTR_RETRIES;
47         int ret = -EAGAIN;
48
49         if (virtq->intr_handle.fd != -1) {
50                 while (retries-- && ret == -EAGAIN) {
51                         ret = rte_intr_callback_unregister(&virtq->intr_handle,
52                                                         mlx5_vdpa_virtq_handler,
53                                                         virtq);
54                         if (ret == -EAGAIN) {
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);
60                         }
61                 }
62         }
63         if (virtq->virtq)
64                 claim_zero(mlx5_devx_cmd_destroy(virtq->virtq));
65         for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
66                 if (virtq->umems[i].obj)
67                         claim_zero(mlx5_glue->devx_umem_dereg
68                                                          (virtq->umems[i].obj));
69                 if (virtq->umems[i].buf)
70                         rte_free(virtq->umems[i].buf);
71         }
72         if (virtq->eqp.fw_qp)
73                 mlx5_vdpa_event_qp_destroy(&virtq->eqp);
74         memset(virtq, 0, sizeof(*virtq));
75         virtq->intr_handle.fd = -1;
76         return 0;
77 }
78
79 void
80 mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv)
81 {
82         int i;
83
84         for (i = 0; i < priv->nr_virtqs; i++)
85                 mlx5_vdpa_virtq_unset(&priv->virtqs[i]);
86         if (priv->tis) {
87                 claim_zero(mlx5_devx_cmd_destroy(priv->tis));
88                 priv->tis = NULL;
89         }
90         if (priv->td) {
91                 claim_zero(mlx5_devx_cmd_destroy(priv->td));
92                 priv->td = NULL;
93         }
94         if (priv->virtq_db_addr) {
95                 claim_zero(munmap(priv->virtq_db_addr, priv->var->length));
96                 priv->virtq_db_addr = NULL;
97         }
98         priv->features = 0;
99         priv->nr_virtqs = 0;
100 }
101
102 int
103 mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state)
104 {
105         struct mlx5_devx_virtq_attr attr = {
106                         .type = MLX5_VIRTQ_MODIFY_TYPE_STATE,
107                         .state = state ? MLX5_VIRTQ_STATE_RDY :
108                                          MLX5_VIRTQ_STATE_SUSPEND,
109                         .queue_index = virtq->index,
110         };
111
112         return mlx5_devx_cmd_modify_virtq(virtq->virtq, &attr);
113 }
114
115 static uint64_t
116 mlx5_vdpa_hva_to_gpa(struct rte_vhost_memory *mem, uint64_t hva)
117 {
118         struct rte_vhost_mem_region *reg;
119         uint32_t i;
120         uint64_t gpa = 0;
121
122         for (i = 0; i < mem->nregions; i++) {
123                 reg = &mem->regions[i];
124                 if (hva >= reg->host_user_addr &&
125                     hva < reg->host_user_addr + reg->size) {
126                         gpa = hva - reg->host_user_addr + reg->guest_phys_addr;
127                         break;
128                 }
129         }
130         return gpa;
131 }
132
133 static int
134 mlx5_vdpa_virtq_setup(struct mlx5_vdpa_priv *priv, int index)
135 {
136         struct mlx5_vdpa_virtq *virtq = &priv->virtqs[index];
137         struct rte_vhost_vring vq;
138         struct mlx5_devx_virtq_attr attr = {0};
139         uint64_t gpa;
140         int ret;
141         unsigned int i;
142         uint16_t last_avail_idx;
143         uint16_t last_used_idx;
144
145         ret = rte_vhost_get_vhost_vring(priv->vid, index, &vq);
146         if (ret)
147                 return -1;
148         virtq->index = index;
149         virtq->vq_size = vq.size;
150         attr.tso_ipv4 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4));
151         attr.tso_ipv6 = !!(priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6));
152         attr.tx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_CSUM));
153         attr.rx_csum = !!(priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM));
154         attr.virtio_version_1_0 = !!(priv->features & (1ULL <<
155                                                         VIRTIO_F_VERSION_1));
156         attr.type = (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) ?
157                         MLX5_VIRTQ_TYPE_PACKED : MLX5_VIRTQ_TYPE_SPLIT;
158         /*
159          * No need event QPs creation when the guest in poll mode or when the
160          * capability allows it.
161          */
162         attr.event_mode = vq.callfd != -1 || !(priv->caps.event_mode & (1 <<
163                                                MLX5_VIRTQ_EVENT_MODE_NO_MSIX)) ?
164                                                       MLX5_VIRTQ_EVENT_MODE_QP :
165                                                   MLX5_VIRTQ_EVENT_MODE_NO_MSIX;
166         if (attr.event_mode == MLX5_VIRTQ_EVENT_MODE_QP) {
167                 ret = mlx5_vdpa_event_qp_create(priv, vq.size, vq.callfd,
168                                                 &virtq->eqp);
169                 if (ret) {
170                         DRV_LOG(ERR, "Failed to create event QPs for virtq %d.",
171                                 index);
172                         return -1;
173                 }
174                 attr.qp_id = virtq->eqp.fw_qp->id;
175         } else {
176                 DRV_LOG(INFO, "Virtq %d is, for sure, working by poll mode, no"
177                         " need event QPs and event mechanism.", index);
178         }
179         /* Setup 3 UMEMs for each virtq. */
180         for (i = 0; i < RTE_DIM(virtq->umems); ++i) {
181                 virtq->umems[i].size = priv->caps.umems[i].a * vq.size +
182                                                           priv->caps.umems[i].b;
183                 virtq->umems[i].buf = rte_zmalloc(__func__,
184                                                   virtq->umems[i].size, 4096);
185                 if (!virtq->umems[i].buf) {
186                         DRV_LOG(ERR, "Cannot allocate umem %d memory for virtq"
187                                 " %u.", i, index);
188                         goto error;
189                 }
190                 virtq->umems[i].obj = mlx5_glue->devx_umem_reg(priv->ctx,
191                                                         virtq->umems[i].buf,
192                                                         virtq->umems[i].size,
193                                                         IBV_ACCESS_LOCAL_WRITE);
194                 if (!virtq->umems[i].obj) {
195                         DRV_LOG(ERR, "Failed to register umem %d for virtq %u.",
196                                 i, index);
197                         goto error;
198                 }
199                 attr.umems[i].id = virtq->umems[i].obj->umem_id;
200                 attr.umems[i].offset = 0;
201                 attr.umems[i].size = virtq->umems[i].size;
202         }
203         if (attr.type == MLX5_VIRTQ_TYPE_SPLIT) {
204                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
205                                            (uint64_t)(uintptr_t)vq.desc);
206                 if (!gpa) {
207                         DRV_LOG(ERR, "Failed to get descriptor ring GPA.");
208                         goto error;
209                 }
210                 attr.desc_addr = gpa;
211                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
212                                            (uint64_t)(uintptr_t)vq.used);
213                 if (!gpa) {
214                         DRV_LOG(ERR, "Failed to get GPA for used ring.");
215                         goto error;
216                 }
217                 attr.used_addr = gpa;
218                 gpa = mlx5_vdpa_hva_to_gpa(priv->vmem,
219                                            (uint64_t)(uintptr_t)vq.avail);
220                 if (!gpa) {
221                         DRV_LOG(ERR, "Failed to get GPA for available ring.");
222                         goto error;
223                 }
224                 attr.available_addr = gpa;
225         }
226         ret = rte_vhost_get_vring_base(priv->vid, index, &last_avail_idx,
227                                  &last_used_idx);
228         if (ret) {
229                 last_avail_idx = 0;
230                 last_used_idx = 0;
231                 DRV_LOG(WARNING, "Couldn't get vring base, idx are set to 0");
232         } else {
233                 DRV_LOG(INFO, "vid %d: Init last_avail_idx=%d, last_used_idx=%d for "
234                                 "virtq %d.", priv->vid, last_avail_idx,
235                                 last_used_idx, index);
236         }
237         attr.hw_available_index = last_avail_idx;
238         attr.hw_used_index = last_used_idx;
239         attr.q_size = vq.size;
240         attr.mkey = priv->gpa_mkey_index;
241         attr.tis_id = priv->tis->id;
242         attr.queue_index = index;
243         virtq->virtq = mlx5_devx_cmd_create_virtq(priv->ctx, &attr);
244         virtq->priv = priv;
245         if (!virtq->virtq)
246                 goto error;
247         if (mlx5_vdpa_virtq_modify(virtq, 1))
248                 goto error;
249         virtq->enable = 1;
250         virtq->priv = priv;
251         /* Be sure notifications are not missed during configuration. */
252         claim_zero(rte_vhost_enable_guest_notification(priv->vid, index, 1));
253         rte_write32(virtq->index, priv->virtq_db_addr);
254         /* Setup doorbell mapping. */
255         virtq->intr_handle.fd = vq.kickfd;
256         if (virtq->intr_handle.fd == -1) {
257                 DRV_LOG(WARNING, "Virtq %d kickfd is invalid.", index);
258                 if (!priv->direct_notifier) {
259                         DRV_LOG(ERR, "Virtq %d cannot be notified.", index);
260                         goto error;
261                 }
262         } else {
263                 virtq->intr_handle.type = RTE_INTR_HANDLE_EXT;
264                 if (rte_intr_callback_register(&virtq->intr_handle,
265                                                mlx5_vdpa_virtq_handler,
266                                                virtq)) {
267                         virtq->intr_handle.fd = -1;
268                         DRV_LOG(ERR, "Failed to register virtq %d interrupt.",
269                                 index);
270                         goto error;
271                 } else {
272                         DRV_LOG(DEBUG, "Register fd %d interrupt for virtq %d.",
273                                 virtq->intr_handle.fd, index);
274                 }
275         }
276         return 0;
277 error:
278         mlx5_vdpa_virtq_unset(virtq);
279         return -1;
280 }
281
282 static int
283 mlx5_vdpa_features_validate(struct mlx5_vdpa_priv *priv)
284 {
285         if (priv->features & (1ULL << VIRTIO_F_RING_PACKED)) {
286                 if (!(priv->caps.virtio_queue_type & (1 <<
287                                                      MLX5_VIRTQ_TYPE_PACKED))) {
288                         DRV_LOG(ERR, "Failed to configur PACKED mode for vdev "
289                                 "%d - it was not reported by HW/driver"
290                                 " capability.", priv->vid);
291                         return -ENOTSUP;
292                 }
293         }
294         if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO4)) {
295                 if (!priv->caps.tso_ipv4) {
296                         DRV_LOG(ERR, "Failed to enable TSO4 for vdev %d - TSO4"
297                                 " was not reported by HW/driver capability.",
298                                 priv->vid);
299                         return -ENOTSUP;
300                 }
301         }
302         if (priv->features & (1ULL << VIRTIO_NET_F_HOST_TSO6)) {
303                 if (!priv->caps.tso_ipv6) {
304                         DRV_LOG(ERR, "Failed to enable TSO6 for vdev %d - TSO6"
305                                 " was not reported by HW/driver capability.",
306                                 priv->vid);
307                         return -ENOTSUP;
308                 }
309         }
310         if (priv->features & (1ULL << VIRTIO_NET_F_CSUM)) {
311                 if (!priv->caps.tx_csum) {
312                         DRV_LOG(ERR, "Failed to enable CSUM for vdev %d - CSUM"
313                                 " was not reported by HW/driver capability.",
314                                 priv->vid);
315                         return -ENOTSUP;
316                 }
317         }
318         if (priv->features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
319                 if (!priv->caps.rx_csum) {
320                         DRV_LOG(ERR, "Failed to enable GUEST CSUM for vdev %d"
321                                 " GUEST CSUM was not reported by HW/driver "
322                                 "capability.", priv->vid);
323                         return -ENOTSUP;
324                 }
325         }
326         if (priv->features & (1ULL << VIRTIO_F_VERSION_1)) {
327                 if (!priv->caps.virtio_version_1_0) {
328                         DRV_LOG(ERR, "Failed to enable version 1 for vdev %d "
329                                 "version 1 was not reported by HW/driver"
330                                 " capability.", priv->vid);
331                         return -ENOTSUP;
332                 }
333         }
334         return 0;
335 }
336
337 int
338 mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv)
339 {
340         struct mlx5_devx_tis_attr tis_attr = {0};
341         uint32_t i;
342         uint16_t nr_vring = rte_vhost_get_vring_num(priv->vid);
343         int ret = rte_vhost_get_negotiated_features(priv->vid, &priv->features);
344
345         if (ret || mlx5_vdpa_features_validate(priv)) {
346                 DRV_LOG(ERR, "Failed to configure negotiated features.");
347                 return -1;
348         }
349         if (nr_vring > priv->caps.max_num_virtio_queues * 2) {
350                 DRV_LOG(ERR, "Do not support more than %d virtqs(%d).",
351                         (int)priv->caps.max_num_virtio_queues * 2,
352                         (int)nr_vring);
353                 return -1;
354         }
355         /* Always map the entire page. */
356         priv->virtq_db_addr = mmap(NULL, priv->var->length, PROT_READ |
357                                    PROT_WRITE, MAP_SHARED, priv->ctx->cmd_fd,
358                                    priv->var->mmap_off);
359         if (priv->virtq_db_addr == MAP_FAILED) {
360                 DRV_LOG(ERR, "Failed to map doorbell page %u.", errno);
361                 priv->virtq_db_addr = NULL;
362                 goto error;
363         } else {
364                 DRV_LOG(DEBUG, "VAR address of doorbell mapping is %p.",
365                         priv->virtq_db_addr);
366         }
367         priv->td = mlx5_devx_cmd_create_td(priv->ctx);
368         if (!priv->td) {
369                 DRV_LOG(ERR, "Failed to create transport domain.");
370                 return -rte_errno;
371         }
372         tis_attr.transport_domain = priv->td->id;
373         priv->tis = mlx5_devx_cmd_create_tis(priv->ctx, &tis_attr);
374         if (!priv->tis) {
375                 DRV_LOG(ERR, "Failed to create TIS.");
376                 goto error;
377         }
378         priv->nr_virtqs = nr_vring;
379         for (i = 0; i < nr_vring; i++)
380                 if (mlx5_vdpa_virtq_setup(priv, i))
381                         goto error;
382         return 0;
383 error:
384         mlx5_vdpa_virtqs_release(priv);
385         return -1;
386 }