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