net/virtio: move features definition to generic header
[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 /* vhost kernel & vdpa ioctls */
17 #define VHOST_VIRTIO 0xAF
18 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
19 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
20 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
21 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
22 #define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, void *)
23 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
24 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
25 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
26 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
27 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
28 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
29 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
30 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
31 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
32 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
33 #define VHOST_VDPA_GET_DEVICE_ID _IOR(VHOST_VIRTIO, 0x70, __u32)
34 #define VHOST_VDPA_GET_STATUS _IOR(VHOST_VIRTIO, 0x71, __u8)
35 #define VHOST_VDPA_SET_STATUS _IOW(VHOST_VIRTIO, 0x72, __u8)
36 #define VHOST_VDPA_SET_VRING_ENABLE     _IOW(VHOST_VIRTIO, 0x75, \
37                                              struct vhost_vring_state)
38 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
39 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
40
41 static uint64_t vhost_req_user_to_vdpa[] = {
42         [VHOST_USER_SET_OWNER] = VHOST_SET_OWNER,
43         [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
44         [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
45         [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
46         [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
47         [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
48         [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
49         [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
50         [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
51         [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
52         [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
53         [VHOST_USER_SET_STATUS] = VHOST_VDPA_SET_STATUS,
54         [VHOST_USER_GET_STATUS] = VHOST_VDPA_GET_STATUS,
55         [VHOST_USER_SET_VRING_ENABLE] = VHOST_VDPA_SET_VRING_ENABLE,
56         [VHOST_USER_GET_PROTOCOL_FEATURES] = VHOST_GET_BACKEND_FEATURES,
57         [VHOST_USER_SET_PROTOCOL_FEATURES] = VHOST_SET_BACKEND_FEATURES,
58 };
59
60 /* no alignment requirement */
61 struct vhost_iotlb_msg {
62         uint64_t iova;
63         uint64_t size;
64         uint64_t uaddr;
65 #define VHOST_ACCESS_RO      0x1
66 #define VHOST_ACCESS_WO      0x2
67 #define VHOST_ACCESS_RW      0x3
68         uint8_t perm;
69 #define VHOST_IOTLB_MISS           1
70 #define VHOST_IOTLB_UPDATE         2
71 #define VHOST_IOTLB_INVALIDATE     3
72 #define VHOST_IOTLB_ACCESS_FAIL    4
73 #define VHOST_IOTLB_BATCH_BEGIN    5
74 #define VHOST_IOTLB_BATCH_END      6
75         uint8_t type;
76 };
77
78 #define VHOST_IOTLB_MSG_V2 0x2
79
80 struct vhost_msg {
81         uint32_t type;
82         uint32_t reserved;
83         union {
84                 struct vhost_iotlb_msg iotlb;
85                 uint8_t padding[64];
86         };
87 };
88
89 static int
90 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
91 {
92         struct vhost_msg msg = {};
93
94         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
95                 return 0;
96
97         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
98                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
99                 return -1;
100         }
101
102         msg.type = VHOST_IOTLB_MSG_V2;
103         msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
104
105         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
106                 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
107                                 strerror(errno));
108                 return -1;
109         }
110
111         return 0;
112 }
113
114 static int
115 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
116 {
117         struct vhost_msg msg = {};
118
119         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
120                 return 0;
121
122         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
123                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
124                 return -1;
125         }
126
127         msg.type = VHOST_IOTLB_MSG_V2;
128         msg.iotlb.type = VHOST_IOTLB_BATCH_END;
129
130         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
131                 PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
132                                 strerror(errno));
133                 return -1;
134         }
135
136         return 0;
137 }
138
139 static int
140 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
141                                   uint64_t iova, size_t len)
142 {
143         struct vhost_msg msg = {};
144
145         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
146                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
147                 return -1;
148         }
149
150         msg.type = VHOST_IOTLB_MSG_V2;
151         msg.iotlb.type = VHOST_IOTLB_UPDATE;
152         msg.iotlb.iova = iova;
153         msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
154         msg.iotlb.size = len;
155         msg.iotlb.perm = VHOST_ACCESS_RW;
156
157         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
158                         __func__, iova, addr, len);
159
160         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
161                 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
162                                 strerror(errno));
163                 return -1;
164         }
165
166         return 0;
167 }
168
169 static int
170 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
171                                   uint64_t iova, size_t len)
172 {
173         struct vhost_msg msg = {};
174
175         if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
176                 PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
177                 return -1;
178         }
179
180         msg.type = VHOST_IOTLB_MSG_V2;
181         msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
182         msg.iotlb.iova = iova;
183         msg.iotlb.size = len;
184
185         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
186                         __func__, iova, len);
187
188         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
189                 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
190                                 strerror(errno));
191                 return -1;
192         }
193
194         return 0;
195 }
196
197 static int
198 vhost_vdpa_dma_map_batch(struct virtio_user_dev *dev, void *addr,
199                                   uint64_t iova, size_t len)
200 {
201         int ret;
202
203         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
204                 return -1;
205
206         ret = vhost_vdpa_dma_map(dev, addr, iova, len);
207
208         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
209                 return -1;
210
211         return ret;
212 }
213
214 static int
215 vhost_vdpa_dma_unmap_batch(struct virtio_user_dev *dev, void *addr,
216                                   uint64_t iova, size_t len)
217 {
218         int ret;
219
220         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
221                 return -1;
222
223         ret = vhost_vdpa_dma_unmap(dev, addr, iova, len);
224
225         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
226                 return -1;
227
228         return ret;
229 }
230
231 static int
232 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
233                 const struct rte_memseg *ms, size_t len, void *arg)
234 {
235         struct virtio_user_dev *dev = arg;
236
237         if (msl->external)
238                 return 0;
239
240         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
241 }
242
243 static int
244 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
245                 void *arg)
246 {
247         struct virtio_user_dev *dev = arg;
248
249         /* skip external memory that isn't a heap */
250         if (msl->external && !msl->heap)
251                 return 0;
252
253         /* skip any segments with invalid IOVA addresses */
254         if (ms->iova == RTE_BAD_IOVA)
255                 return 0;
256
257         /* if IOVA mode is VA, we've already mapped the internal segments */
258         if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
259                 return 0;
260
261         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
262 }
263
264 static int
265 vhost_vdpa_dma_map_all(struct virtio_user_dev *dev)
266 {
267         int ret;
268
269         if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
270                 return -1;
271
272         vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
273
274         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
275                 /* with IOVA as VA mode, we can get away with mapping contiguous
276                  * chunks rather than going page-by-page.
277                  */
278                 ret = rte_memseg_contig_walk_thread_unsafe(
279                                 vhost_vdpa_map_contig, dev);
280                 if (ret)
281                         goto batch_end;
282                 /* we have to continue the walk because we've skipped the
283                  * external segments during the config walk.
284                  */
285         }
286         ret = rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
287
288 batch_end:
289         if (vhost_vdpa_iotlb_batch_end(dev) < 0)
290                 return -1;
291
292         return ret;
293 }
294
295 /* with below features, vhost vdpa does not need to do the checksum and TSO,
296  * these info will be passed to virtio_user through virtio net header.
297  */
298 #define VHOST_VDPA_GUEST_OFFLOADS_MASK  \
299         ((1ULL << VIRTIO_NET_F_GUEST_CSUM) |    \
300          (1ULL << VIRTIO_NET_F_GUEST_TSO4) |    \
301          (1ULL << VIRTIO_NET_F_GUEST_TSO6) |    \
302          (1ULL << VIRTIO_NET_F_GUEST_ECN)  |    \
303          (1ULL << VIRTIO_NET_F_GUEST_UFO))
304
305 #define VHOST_VDPA_HOST_OFFLOADS_MASK           \
306         ((1ULL << VIRTIO_NET_F_HOST_TSO4) |     \
307          (1ULL << VIRTIO_NET_F_HOST_TSO6) |     \
308          (1ULL << VIRTIO_NET_F_CSUM))
309
310 static int
311 vhost_vdpa_ioctl(struct virtio_user_dev *dev,
312                    enum vhost_user_request req,
313                    void *arg)
314 {
315         int ret = -1;
316         uint64_t req_vdpa;
317
318         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
319
320         req_vdpa = vhost_req_user_to_vdpa[req];
321
322         if (req_vdpa == VHOST_SET_MEM_TABLE)
323                 return vhost_vdpa_dma_map_all(dev);
324
325         if (req_vdpa == VHOST_SET_FEATURES) {
326                 /* WORKAROUND */
327                 *(uint64_t *)arg |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
328
329                 /* Multiqueue not supported for now */
330                 *(uint64_t *)arg &= ~(1ULL << VIRTIO_NET_F_MQ);
331         }
332
333         switch (req_vdpa) {
334         case VHOST_SET_VRING_NUM:
335         case VHOST_SET_VRING_ADDR:
336         case VHOST_SET_VRING_BASE:
337         case VHOST_GET_VRING_BASE:
338         case VHOST_SET_VRING_KICK:
339         case VHOST_SET_VRING_CALL:
340                 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
341                             dev->vhostfd, *(unsigned int *)arg);
342                 break;
343         default:
344                 break;
345         }
346
347         ret = ioctl(dev->vhostfd, req_vdpa, arg);
348         if (ret < 0)
349                 PMD_DRV_LOG(ERR, "%s failed: %s",
350                             vhost_msg_strings[req], strerror(errno));
351
352         return ret;
353 }
354
355 /**
356  * Set up environment to talk with a vhost vdpa backend.
357  *
358  * @return
359  *   - (-1) if fail to set up;
360  *   - (>=0) if successful.
361  */
362 static int
363 vhost_vdpa_setup(struct virtio_user_dev *dev)
364 {
365         uint32_t did = (uint32_t)-1;
366
367         dev->vhostfd = open(dev->path, O_RDWR);
368         if (dev->vhostfd < 0) {
369                 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
370                                 dev->path, strerror(errno));
371                 return -1;
372         }
373
374         if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
375                         did != VIRTIO_ID_NETWORK) {
376                 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
377                 return -1;
378         }
379
380         return 0;
381 }
382
383 static int
384 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
385                                uint16_t pair_idx,
386                                int enable)
387 {
388         int i;
389
390         if (dev->qp_enabled[pair_idx] == enable)
391                 return 0;
392
393         for (i = 0; i < 2; ++i) {
394                 struct vhost_vring_state state = {
395                         .index = pair_idx * 2 + i,
396                         .num   = enable,
397                 };
398
399                 if (vhost_vdpa_ioctl(dev, VHOST_USER_SET_VRING_ENABLE, &state))
400                         return -1;
401         }
402
403         dev->qp_enabled[pair_idx] = enable;
404
405         return 0;
406 }
407
408 struct virtio_user_backend_ops virtio_ops_vdpa = {
409         .setup = vhost_vdpa_setup,
410         .send_request = vhost_vdpa_ioctl,
411         .enable_qp = vhost_vdpa_enable_queue_pair,
412         .dma_map = vhost_vdpa_dma_map_batch,
413         .dma_unmap = vhost_vdpa_dma_unmap_batch,
414 };