net/virtio: introduce backend data
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_vdpa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Red Hat Inc.
3  */
4
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10
11 #include <rte_memory.h>
12
13 #include "vhost.h"
14 #include "virtio_user_dev.h"
15
16 #define VHOST_VDPA_SUPPORTED_BACKEND_FEATURES           \
17         (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2   |       \
18         1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
19
20 /* vhost kernel & vdpa ioctls */
21 #define VHOST_VIRTIO 0xAF
22 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
23 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
24 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
25 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
26 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
27 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
28 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
29 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
30 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
31 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
32 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
33 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
34 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
35 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
36 #define VHOST_VDPA_GET_DEVICE_ID _IOR(VHOST_VIRTIO, 0x70, __u32)
37 #define VHOST_VDPA_GET_STATUS _IOR(VHOST_VIRTIO, 0x71, __u8)
38 #define VHOST_VDPA_SET_STATUS _IOW(VHOST_VIRTIO, 0x72, __u8)
39 #define VHOST_VDPA_SET_VRING_ENABLE _IOW(VHOST_VIRTIO, 0x75, struct vhost_vring_state)
40 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
41 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
42
43 /* no alignment requirement */
44 struct vhost_iotlb_msg {
45         uint64_t iova;
46         uint64_t size;
47         uint64_t uaddr;
48 #define VHOST_ACCESS_RO      0x1
49 #define VHOST_ACCESS_WO      0x2
50 #define VHOST_ACCESS_RW      0x3
51         uint8_t perm;
52 #define VHOST_IOTLB_MISS           1
53 #define VHOST_IOTLB_UPDATE         2
54 #define VHOST_IOTLB_INVALIDATE     3
55 #define VHOST_IOTLB_ACCESS_FAIL    4
56 #define VHOST_IOTLB_BATCH_BEGIN    5
57 #define VHOST_IOTLB_BATCH_END      6
58         uint8_t type;
59 };
60
61 #define VHOST_IOTLB_MSG_V2 0x2
62
63 struct vhost_msg {
64         uint32_t type;
65         uint32_t reserved;
66         union {
67                 struct vhost_iotlb_msg iotlb;
68                 uint8_t padding[64];
69         };
70 };
71
72
73 static int
74 vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
75 {
76         int ret;
77
78         ret = ioctl(fd, request, arg);
79         if (ret) {
80                 PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
81                                 request, strerror(errno));
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 static int
89 vhost_vdpa_set_owner(struct virtio_user_dev *dev)
90 {
91         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_OWNER, NULL);
92 }
93
94 static int
95 vhost_vdpa_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
96 {
97         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_BACKEND_FEATURES, features);
98 }
99
100 static int
101 vhost_vdpa_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
102 {
103         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_BACKEND_FEATURES, &features);
104 }
105
106 static int
107 vhost_vdpa_get_features(struct virtio_user_dev *dev, uint64_t *features)
108 {
109         int ret;
110
111         ret = vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_FEATURES, features);
112         if (ret) {
113                 PMD_DRV_LOG(ERR, "Failed to get features");
114                 return -1;
115         }
116
117         /* Multiqueue not supported for now */
118         *features &= ~(1ULL << VIRTIO_NET_F_MQ);
119
120         /* Negotiated vDPA backend features */
121         ret = vhost_vdpa_get_protocol_features(dev, &dev->protocol_features);
122         if (ret < 0) {
123                 PMD_DRV_LOG(ERR, "Failed to get backend features");
124                 return -1;
125         }
126
127         dev->protocol_features &= VHOST_VDPA_SUPPORTED_BACKEND_FEATURES;
128
129         ret = vhost_vdpa_set_protocol_features(dev, dev->protocol_features);
130         if (ret < 0) {
131                 PMD_DRV_LOG(ERR, "Failed to set backend features");
132                 return -1;
133         }
134
135         return 0;
136 }
137
138 static int
139 vhost_vdpa_set_features(struct virtio_user_dev *dev, uint64_t features)
140 {
141         /* WORKAROUND */
142         features |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
143
144         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_FEATURES, &features);
145 }
146
147 static int
148 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
149 {
150         struct vhost_msg msg = {};
151
152         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
153                 return 0;
154
155         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
156                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
157                 return -1;
158         }
159
160         msg.type = VHOST_IOTLB_MSG_V2;
161         msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
162
163         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
164                 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
165                                 strerror(errno));
166                 return -1;
167         }
168
169         return 0;
170 }
171
172 static int
173 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
174 {
175         struct vhost_msg msg = {};
176
177         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
178                 return 0;
179
180         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
181                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
182                 return -1;
183         }
184
185         msg.type = VHOST_IOTLB_MSG_V2;
186         msg.iotlb.type = VHOST_IOTLB_BATCH_END;
187
188         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
189                 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
190                                 strerror(errno));
191                 return -1;
192         }
193
194         return 0;
195 }
196
197 static int
198 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
199                                   uint64_t iova, size_t len)
200 {
201         struct vhost_msg msg = {};
202
203         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
204                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
205                 return -1;
206         }
207
208         msg.type = VHOST_IOTLB_MSG_V2;
209         msg.iotlb.type = VHOST_IOTLB_UPDATE;
210         msg.iotlb.iova = iova;
211         msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
212         msg.iotlb.size = len;
213         msg.iotlb.perm = VHOST_ACCESS_RW;
214
215         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
216                         __func__, iova, addr, len);
217
218         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
219                 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
220                                 strerror(errno));
221                 return -1;
222         }
223
224         return 0;
225 }
226
227 static int
228 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
229                                   uint64_t iova, size_t len)
230 {
231         struct vhost_msg msg = {};
232
233         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
234                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
235                 return -1;
236         }
237
238         msg.type = VHOST_IOTLB_MSG_V2;
239         msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
240         msg.iotlb.iova = iova;
241         msg.iotlb.size = len;
242
243         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
244                         __func__, iova, len);
245
246         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
247                 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
248                                 strerror(errno));
249                 return -1;
250         }
251
252         return 0;
253 }
254
255 static int
256 vhost_vdpa_dma_map_batch(struct virtio_user_dev *dev, void *addr,
257                                   uint64_t iova, size_t len)
258 {
259         int ret;
260
261         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
262                 return -1;
263
264         ret = vhost_vdpa_dma_map(dev, addr, iova, len);
265
266         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
267                 return -1;
268
269         return ret;
270 }
271
272 static int
273 vhost_vdpa_dma_unmap_batch(struct virtio_user_dev *dev, void *addr,
274                                   uint64_t iova, size_t len)
275 {
276         int ret;
277
278         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
279                 return -1;
280
281         ret = vhost_vdpa_dma_unmap(dev, addr, iova, len);
282
283         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
284                 return -1;
285
286         return ret;
287 }
288
289 static int
290 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
291                 const struct rte_memseg *ms, size_t len, void *arg)
292 {
293         struct virtio_user_dev *dev = arg;
294
295         if (msl->external)
296                 return 0;
297
298         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
299 }
300
301 static int
302 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
303                 void *arg)
304 {
305         struct virtio_user_dev *dev = arg;
306
307         /* skip external memory that isn't a heap */
308         if (msl->external && !msl->heap)
309                 return 0;
310
311         /* skip any segments with invalid IOVA addresses */
312         if (ms->iova == RTE_BAD_IOVA)
313                 return 0;
314
315         /* if IOVA mode is VA, we've already mapped the internal segments */
316         if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
317                 return 0;
318
319         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
320 }
321
322 static int
323 vhost_vdpa_set_memory_table(struct virtio_user_dev *dev)
324 {
325         int ret;
326
327         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
328                 return -1;
329
330         vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
331
332         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
333                 /* with IOVA as VA mode, we can get away with mapping contiguous
334                  * chunks rather than going page-by-page.
335                  */
336                 ret = rte_memseg_contig_walk_thread_unsafe(
337                                 vhost_vdpa_map_contig, dev);
338                 if (ret)
339                         goto batch_end;
340                 /* we have to continue the walk because we've skipped the
341                  * external segments during the config walk.
342                  */
343         }
344         ret = rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
345
346 batch_end:
347         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
348                 return -1;
349
350         return ret;
351 }
352
353 static int
354 vhost_vdpa_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
355 {
356         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_SET_VRING_ENABLE, state);
357 }
358
359 static int
360 vhost_vdpa_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
361 {
362         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_NUM, state);
363 }
364
365 static int
366 vhost_vdpa_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
367 {
368         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_BASE, state);
369 }
370
371 static int
372 vhost_vdpa_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
373 {
374         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_VRING_BASE, state);
375 }
376
377 static int
378 vhost_vdpa_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
379 {
380         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_CALL, file);
381 }
382
383 static int
384 vhost_vdpa_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
385 {
386         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_KICK, file);
387 }
388
389 static int
390 vhost_vdpa_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
391 {
392         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_ADDR, addr);
393 }
394
395 static int
396 vhost_vdpa_get_status(struct virtio_user_dev *dev, uint8_t *status)
397 {
398         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_GET_STATUS, status);
399 }
400
401 static int
402 vhost_vdpa_set_status(struct virtio_user_dev *dev, uint8_t status)
403 {
404         return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_SET_STATUS, &status);
405 }
406
407 /**
408  * Set up environment to talk with a vhost vdpa backend.
409  *
410  * @return
411  *   - (-1) if fail to set up;
412  *   - (>=0) if successful.
413  */
414 static int
415 vhost_vdpa_setup(struct virtio_user_dev *dev)
416 {
417         uint32_t did = (uint32_t)-1;
418
419         dev->vhostfd = open(dev->path, O_RDWR);
420         if (dev->vhostfd < 0) {
421                 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
422                                 dev->path, strerror(errno));
423                 return -1;
424         }
425
426         if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
427                         did != VIRTIO_ID_NETWORK) {
428                 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
429                 return -1;
430         }
431
432         return 0;
433 }
434
435 static int
436 vhost_vdpa_destroy(struct virtio_user_dev *dev __rte_unused)
437 {
438         return 0;
439 }
440
441 static int
442 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
443                                uint16_t pair_idx,
444                                int enable)
445 {
446         int i;
447
448         if (dev->qp_enabled[pair_idx] == enable)
449                 return 0;
450
451         for (i = 0; i < 2; ++i) {
452                 struct vhost_vring_state state = {
453                         .index = pair_idx * 2 + i,
454                         .num   = enable,
455                 };
456
457                 if (vhost_vdpa_set_vring_enable(dev, &state))
458                         return -1;
459         }
460
461         dev->qp_enabled[pair_idx] = enable;
462
463         return 0;
464 }
465
466 static int
467 vhost_vdpa_get_backend_features(uint64_t *features)
468 {
469         *features = 0;
470
471         return 0;
472 }
473
474 struct virtio_user_backend_ops virtio_ops_vdpa = {
475         .setup = vhost_vdpa_setup,
476         .destroy = vhost_vdpa_destroy,
477         .get_backend_features = vhost_vdpa_get_backend_features,
478         .set_owner = vhost_vdpa_set_owner,
479         .get_features = vhost_vdpa_get_features,
480         .set_features = vhost_vdpa_set_features,
481         .set_memory_table = vhost_vdpa_set_memory_table,
482         .set_vring_num = vhost_vdpa_set_vring_num,
483         .set_vring_base = vhost_vdpa_set_vring_base,
484         .get_vring_base = vhost_vdpa_get_vring_base,
485         .set_vring_call = vhost_vdpa_set_vring_call,
486         .set_vring_kick = vhost_vdpa_set_vring_kick,
487         .set_vring_addr = vhost_vdpa_set_vring_addr,
488         .get_status = vhost_vdpa_get_status,
489         .set_status = vhost_vdpa_set_status,
490         .enable_qp = vhost_vdpa_enable_queue_pair,
491         .dma_map = vhost_vdpa_dma_map_batch,
492         .dma_unmap = vhost_vdpa_dma_unmap_batch,
493 };