c5b59e2f95627add7f89a6652ceb46ee5909bf24
[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
39 static uint64_t vhost_req_user_to_vdpa[] = {
40         [VHOST_USER_SET_OWNER] = VHOST_SET_OWNER,
41         [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
42         [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
43         [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
44         [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
45         [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
46         [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
47         [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
48         [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
49         [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
50         [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
51         [VHOST_USER_SET_STATUS] = VHOST_VDPA_SET_STATUS,
52         [VHOST_USER_GET_STATUS] = VHOST_VDPA_GET_STATUS,
53         [VHOST_USER_SET_VRING_ENABLE] = VHOST_VDPA_SET_VRING_ENABLE,
54 };
55
56 /* no alignment requirement */
57 struct vhost_iotlb_msg {
58         uint64_t iova;
59         uint64_t size;
60         uint64_t uaddr;
61 #define VHOST_ACCESS_RO      0x1
62 #define VHOST_ACCESS_WO      0x2
63 #define VHOST_ACCESS_RW      0x3
64         uint8_t perm;
65 #define VHOST_IOTLB_MISS           1
66 #define VHOST_IOTLB_UPDATE         2
67 #define VHOST_IOTLB_INVALIDATE     3
68 #define VHOST_IOTLB_ACCESS_FAIL    4
69         uint8_t type;
70 };
71
72 #define VHOST_IOTLB_MSG_V2 0x2
73
74 struct vhost_msg {
75         uint32_t type;
76         uint32_t reserved;
77         union {
78                 struct vhost_iotlb_msg iotlb;
79                 uint8_t padding[64];
80         };
81 };
82
83 static int
84 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
85                                   uint64_t iova, size_t len)
86 {
87         struct vhost_msg msg = {};
88
89         msg.type = VHOST_IOTLB_MSG_V2;
90         msg.iotlb.type = VHOST_IOTLB_UPDATE;
91         msg.iotlb.iova = iova;
92         msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
93         msg.iotlb.size = len;
94         msg.iotlb.perm = VHOST_ACCESS_RW;
95
96         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
97                         __func__, iova, addr, len);
98
99         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
100                 PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
101                                 strerror(errno));
102                 return -1;
103         }
104
105         return 0;
106 }
107
108 static int
109 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
110                                   uint64_t iova, size_t len)
111 {
112         struct vhost_msg msg = {};
113
114         msg.type = VHOST_IOTLB_MSG_V2;
115         msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
116         msg.iotlb.iova = iova;
117         msg.iotlb.size = len;
118
119         PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
120                         __func__, iova, len);
121
122         if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
123                 PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
124                                 strerror(errno));
125                 return -1;
126         }
127
128         return 0;
129 }
130
131
132 static int
133 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
134                 const struct rte_memseg *ms, size_t len, void *arg)
135 {
136         struct virtio_user_dev *dev = arg;
137
138         if (msl->external)
139                 return 0;
140
141         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
142 }
143
144 static int
145 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
146                 void *arg)
147 {
148         struct virtio_user_dev *dev = arg;
149
150         /* skip external memory that isn't a heap */
151         if (msl->external && !msl->heap)
152                 return 0;
153
154         /* skip any segments with invalid IOVA addresses */
155         if (ms->iova == RTE_BAD_IOVA)
156                 return 0;
157
158         /* if IOVA mode is VA, we've already mapped the internal segments */
159         if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
160                 return 0;
161
162         return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
163 }
164
165 static int
166 vhost_vdpa_dma_map_all(struct virtio_user_dev *dev)
167 {
168         vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
169
170         if (rte_eal_iova_mode() == RTE_IOVA_VA) {
171                 /* with IOVA as VA mode, we can get away with mapping contiguous
172                  * chunks rather than going page-by-page.
173                  */
174                 int ret = rte_memseg_contig_walk_thread_unsafe(
175                                 vhost_vdpa_map_contig, dev);
176                 if (ret)
177                         return ret;
178                 /* we have to continue the walk because we've skipped the
179                  * external segments during the config walk.
180                  */
181         }
182         return rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
183 }
184
185 /* with below features, vhost vdpa does not need to do the checksum and TSO,
186  * these info will be passed to virtio_user through virtio net header.
187  */
188 #define VHOST_VDPA_GUEST_OFFLOADS_MASK  \
189         ((1ULL << VIRTIO_NET_F_GUEST_CSUM) |    \
190          (1ULL << VIRTIO_NET_F_GUEST_TSO4) |    \
191          (1ULL << VIRTIO_NET_F_GUEST_TSO6) |    \
192          (1ULL << VIRTIO_NET_F_GUEST_ECN)  |    \
193          (1ULL << VIRTIO_NET_F_GUEST_UFO))
194
195 #define VHOST_VDPA_HOST_OFFLOADS_MASK           \
196         ((1ULL << VIRTIO_NET_F_HOST_TSO4) |     \
197          (1ULL << VIRTIO_NET_F_HOST_TSO6) |     \
198          (1ULL << VIRTIO_NET_F_CSUM))
199
200 static int
201 vhost_vdpa_ioctl(struct virtio_user_dev *dev,
202                    enum vhost_user_request req,
203                    void *arg)
204 {
205         int ret = -1;
206         uint64_t req_vdpa;
207
208         PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
209
210         req_vdpa = vhost_req_user_to_vdpa[req];
211
212         if (req_vdpa == VHOST_SET_MEM_TABLE)
213                 return vhost_vdpa_dma_map_all(dev);
214
215         if (req_vdpa == VHOST_SET_FEATURES) {
216                 /* WORKAROUND */
217                 *(uint64_t *)arg |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
218
219                 /* Multiqueue not supported for now */
220                 *(uint64_t *)arg &= ~(1ULL << VIRTIO_NET_F_MQ);
221         }
222
223         switch (req_vdpa) {
224         case VHOST_SET_VRING_NUM:
225         case VHOST_SET_VRING_ADDR:
226         case VHOST_SET_VRING_BASE:
227         case VHOST_GET_VRING_BASE:
228         case VHOST_SET_VRING_KICK:
229         case VHOST_SET_VRING_CALL:
230                 PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
231                             dev->vhostfd, *(unsigned int *)arg);
232                 break;
233         default:
234                 break;
235         }
236
237         ret = ioctl(dev->vhostfd, req_vdpa, arg);
238         if (ret < 0)
239                 PMD_DRV_LOG(ERR, "%s failed: %s",
240                             vhost_msg_strings[req], strerror(errno));
241
242         return ret;
243 }
244
245 /**
246  * Set up environment to talk with a vhost vdpa backend.
247  *
248  * @return
249  *   - (-1) if fail to set up;
250  *   - (>=0) if successful.
251  */
252 static int
253 vhost_vdpa_setup(struct virtio_user_dev *dev)
254 {
255         uint32_t did = (uint32_t)-1;
256
257         dev->vhostfd = open(dev->path, O_RDWR);
258         if (dev->vhostfd < 0) {
259                 PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
260                                 dev->path, strerror(errno));
261                 return -1;
262         }
263
264         if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
265                         did != VIRTIO_ID_NETWORK) {
266                 PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
267                 return -1;
268         }
269
270         return 0;
271 }
272
273 static int
274 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
275                                uint16_t pair_idx,
276                                int enable)
277 {
278         int i;
279
280         if (dev->qp_enabled[pair_idx] == enable)
281                 return 0;
282
283         for (i = 0; i < 2; ++i) {
284                 struct vhost_vring_state state = {
285                         .index = pair_idx * 2 + i,
286                         .num   = enable,
287                 };
288
289                 if (vhost_vdpa_ioctl(dev, VHOST_USER_SET_VRING_ENABLE, &state))
290                         return -1;
291         }
292
293         dev->qp_enabled[pair_idx] = enable;
294
295         return 0;
296 }
297
298 struct virtio_user_backend_ops virtio_ops_vdpa = {
299         .setup = vhost_vdpa_setup,
300         .send_request = vhost_vdpa_ioctl,
301         .enable_qp = vhost_vdpa_enable_queue_pair,
302         .dma_map = vhost_vdpa_dma_map,
303         .dma_unmap = vhost_vdpa_dma_unmap,
304 };