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 struct vhost_vdpa_data {
17 	int vhostfd;
18 	uint64_t protocol_features;
19 };
20 
21 #define VHOST_VDPA_SUPPORTED_BACKEND_FEATURES		\
22 	(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2	|	\
23 	1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
24 
25 /* vhost kernel & vdpa ioctls */
26 #define VHOST_VIRTIO 0xAF
27 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
28 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
29 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
30 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
31 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
32 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
33 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
34 #define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
35 #define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
36 #define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
37 #define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
38 #define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
39 #define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
40 #define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)
41 #define VHOST_VDPA_GET_DEVICE_ID _IOR(VHOST_VIRTIO, 0x70, __u32)
42 #define VHOST_VDPA_GET_STATUS _IOR(VHOST_VIRTIO, 0x71, __u8)
43 #define VHOST_VDPA_SET_STATUS _IOW(VHOST_VIRTIO, 0x72, __u8)
44 #define VHOST_VDPA_GET_CONFIG _IOR(VHOST_VIRTIO, 0x73, struct vhost_vdpa_config)
45 #define VHOST_VDPA_SET_CONFIG _IOW(VHOST_VIRTIO, 0x74, struct vhost_vdpa_config)
46 #define VHOST_VDPA_SET_VRING_ENABLE _IOW(VHOST_VIRTIO, 0x75, struct vhost_vring_state)
47 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
48 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
49 
50 /* no alignment requirement */
51 struct vhost_iotlb_msg {
52 	uint64_t iova;
53 	uint64_t size;
54 	uint64_t uaddr;
55 #define VHOST_ACCESS_RO      0x1
56 #define VHOST_ACCESS_WO      0x2
57 #define VHOST_ACCESS_RW      0x3
58 	uint8_t perm;
59 #define VHOST_IOTLB_MISS           1
60 #define VHOST_IOTLB_UPDATE         2
61 #define VHOST_IOTLB_INVALIDATE     3
62 #define VHOST_IOTLB_ACCESS_FAIL    4
63 #define VHOST_IOTLB_BATCH_BEGIN    5
64 #define VHOST_IOTLB_BATCH_END      6
65 	uint8_t type;
66 };
67 
68 #define VHOST_IOTLB_MSG_V2 0x2
69 
70 struct vhost_vdpa_config {
71 	uint32_t off;
72 	uint32_t len;
73 	uint8_t buf[0];
74 };
75 
76 struct vhost_msg {
77 	uint32_t type;
78 	uint32_t reserved;
79 	union {
80 		struct vhost_iotlb_msg iotlb;
81 		uint8_t padding[64];
82 	};
83 };
84 
85 
86 static int
vhost_vdpa_ioctl(int fd,uint64_t request,void * arg)87 vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
88 {
89 	int ret;
90 
91 	ret = ioctl(fd, request, arg);
92 	if (ret) {
93 		PMD_DRV_LOG(ERR, "Vhost-vDPA ioctl %"PRIu64" failed (%s)",
94 				request, strerror(errno));
95 		return -1;
96 	}
97 
98 	return 0;
99 }
100 
101 static int
vhost_vdpa_set_owner(struct virtio_user_dev * dev)102 vhost_vdpa_set_owner(struct virtio_user_dev *dev)
103 {
104 	struct vhost_vdpa_data *data = dev->backend_data;
105 
106 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_OWNER, NULL);
107 }
108 
109 static int
vhost_vdpa_get_protocol_features(struct virtio_user_dev * dev,uint64_t * features)110 vhost_vdpa_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
111 {
112 	struct vhost_vdpa_data *data = dev->backend_data;
113 
114 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_BACKEND_FEATURES, features);
115 }
116 
117 static int
vhost_vdpa_set_protocol_features(struct virtio_user_dev * dev,uint64_t features)118 vhost_vdpa_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
119 {
120 	struct vhost_vdpa_data *data = dev->backend_data;
121 
122 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_BACKEND_FEATURES, &features);
123 }
124 
125 static int
vhost_vdpa_get_features(struct virtio_user_dev * dev,uint64_t * features)126 vhost_vdpa_get_features(struct virtio_user_dev *dev, uint64_t *features)
127 {
128 	struct vhost_vdpa_data *data = dev->backend_data;
129 	int ret;
130 
131 	ret = vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_FEATURES, features);
132 	if (ret) {
133 		PMD_DRV_LOG(ERR, "Failed to get features");
134 		return -1;
135 	}
136 
137 	/* Multiqueue not supported for now */
138 	*features &= ~(1ULL << VIRTIO_NET_F_MQ);
139 
140 	/* Negotiated vDPA backend features */
141 	ret = vhost_vdpa_get_protocol_features(dev, &data->protocol_features);
142 	if (ret < 0) {
143 		PMD_DRV_LOG(ERR, "Failed to get backend features");
144 		return -1;
145 	}
146 
147 	data->protocol_features &= VHOST_VDPA_SUPPORTED_BACKEND_FEATURES;
148 
149 	ret = vhost_vdpa_set_protocol_features(dev, data->protocol_features);
150 	if (ret < 0) {
151 		PMD_DRV_LOG(ERR, "Failed to set backend features");
152 		return -1;
153 	}
154 
155 	return 0;
156 }
157 
158 static int
vhost_vdpa_set_features(struct virtio_user_dev * dev,uint64_t features)159 vhost_vdpa_set_features(struct virtio_user_dev *dev, uint64_t features)
160 {
161 	struct vhost_vdpa_data *data = dev->backend_data;
162 
163 	/* WORKAROUND */
164 	features |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
165 
166 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_FEATURES, &features);
167 }
168 
169 static int
vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev * dev)170 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
171 {
172 	struct vhost_vdpa_data *data = dev->backend_data;
173 	struct vhost_msg msg = {};
174 
175 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
176 		return 0;
177 
178 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
179 		PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
180 		return -1;
181 	}
182 
183 	msg.type = VHOST_IOTLB_MSG_V2;
184 	msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
185 
186 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
187 		PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
188 				strerror(errno));
189 		return -1;
190 	}
191 
192 	return 0;
193 }
194 
195 static int
vhost_vdpa_iotlb_batch_end(struct virtio_user_dev * dev)196 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
197 {
198 	struct vhost_vdpa_data *data = dev->backend_data;
199 	struct vhost_msg msg = {};
200 
201 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
202 		return 0;
203 
204 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
205 		PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
206 		return -1;
207 	}
208 
209 	msg.type = VHOST_IOTLB_MSG_V2;
210 	msg.iotlb.type = VHOST_IOTLB_BATCH_END;
211 
212 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
213 		PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
214 				strerror(errno));
215 		return -1;
216 	}
217 
218 	return 0;
219 }
220 
221 static int
vhost_vdpa_dma_map(struct virtio_user_dev * dev,void * addr,uint64_t iova,size_t len)222 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
223 				  uint64_t iova, size_t len)
224 {
225 	struct vhost_vdpa_data *data = dev->backend_data;
226 	struct vhost_msg msg = {};
227 
228 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
229 		PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
230 		return -1;
231 	}
232 
233 	msg.type = VHOST_IOTLB_MSG_V2;
234 	msg.iotlb.type = VHOST_IOTLB_UPDATE;
235 	msg.iotlb.iova = iova;
236 	msg.iotlb.uaddr = (uint64_t)(uintptr_t)addr;
237 	msg.iotlb.size = len;
238 	msg.iotlb.perm = VHOST_ACCESS_RW;
239 
240 	PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
241 			__func__, iova, addr, len);
242 
243 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
244 		PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
245 				strerror(errno));
246 		return -1;
247 	}
248 
249 	return 0;
250 }
251 
252 static int
vhost_vdpa_dma_unmap(struct virtio_user_dev * dev,__rte_unused void * addr,uint64_t iova,size_t len)253 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
254 				  uint64_t iova, size_t len)
255 {
256 	struct vhost_vdpa_data *data = dev->backend_data;
257 	struct vhost_msg msg = {};
258 
259 	if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
260 		PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
261 		return -1;
262 	}
263 
264 	msg.type = VHOST_IOTLB_MSG_V2;
265 	msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
266 	msg.iotlb.iova = iova;
267 	msg.iotlb.size = len;
268 
269 	PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
270 			__func__, iova, len);
271 
272 	if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
273 		PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
274 				strerror(errno));
275 		return -1;
276 	}
277 
278 	return 0;
279 }
280 
281 static int
vhost_vdpa_dma_map_batch(struct virtio_user_dev * dev,void * addr,uint64_t iova,size_t len)282 vhost_vdpa_dma_map_batch(struct virtio_user_dev *dev, void *addr,
283 				  uint64_t iova, size_t len)
284 {
285 	int ret;
286 
287 	if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
288 		return -1;
289 
290 	ret = vhost_vdpa_dma_map(dev, addr, iova, len);
291 
292 	if (vhost_vdpa_iotlb_batch_end(dev) < 0)
293 		return -1;
294 
295 	return ret;
296 }
297 
298 static int
vhost_vdpa_dma_unmap_batch(struct virtio_user_dev * dev,void * addr,uint64_t iova,size_t len)299 vhost_vdpa_dma_unmap_batch(struct virtio_user_dev *dev, void *addr,
300 				  uint64_t iova, size_t len)
301 {
302 	int ret;
303 
304 	if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
305 		return -1;
306 
307 	ret = vhost_vdpa_dma_unmap(dev, addr, iova, len);
308 
309 	if (vhost_vdpa_iotlb_batch_end(dev) < 0)
310 		return -1;
311 
312 	return ret;
313 }
314 
315 static int
vhost_vdpa_map_contig(const struct rte_memseg_list * msl,const struct rte_memseg * ms,size_t len,void * arg)316 vhost_vdpa_map_contig(const struct rte_memseg_list *msl,
317 		const struct rte_memseg *ms, size_t len, void *arg)
318 {
319 	struct virtio_user_dev *dev = arg;
320 
321 	if (msl->external)
322 		return 0;
323 
324 	return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, len);
325 }
326 
327 static int
vhost_vdpa_map(const struct rte_memseg_list * msl,const struct rte_memseg * ms,void * arg)328 vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
329 		void *arg)
330 {
331 	struct virtio_user_dev *dev = arg;
332 
333 	/* skip external memory that isn't a heap */
334 	if (msl->external && !msl->heap)
335 		return 0;
336 
337 	/* skip any segments with invalid IOVA addresses */
338 	if (ms->iova == RTE_BAD_IOVA)
339 		return 0;
340 
341 	/* if IOVA mode is VA, we've already mapped the internal segments */
342 	if (!msl->external && rte_eal_iova_mode() == RTE_IOVA_VA)
343 		return 0;
344 
345 	return vhost_vdpa_dma_map(dev, ms->addr, ms->iova, ms->len);
346 }
347 
348 static int
vhost_vdpa_set_memory_table(struct virtio_user_dev * dev)349 vhost_vdpa_set_memory_table(struct virtio_user_dev *dev)
350 {
351 	int ret;
352 
353 	if (vhost_vdpa_iotlb_batch_begin(dev) < 0)
354 		return -1;
355 
356 	vhost_vdpa_dma_unmap(dev, NULL, 0, SIZE_MAX);
357 
358 	if (rte_eal_iova_mode() == RTE_IOVA_VA) {
359 		/* with IOVA as VA mode, we can get away with mapping contiguous
360 		 * chunks rather than going page-by-page.
361 		 */
362 		ret = rte_memseg_contig_walk_thread_unsafe(
363 				vhost_vdpa_map_contig, dev);
364 		if (ret)
365 			goto batch_end;
366 		/* we have to continue the walk because we've skipped the
367 		 * external segments during the config walk.
368 		 */
369 	}
370 	ret = rte_memseg_walk_thread_unsafe(vhost_vdpa_map, dev);
371 
372 batch_end:
373 	if (vhost_vdpa_iotlb_batch_end(dev) < 0)
374 		return -1;
375 
376 	return ret;
377 }
378 
379 static int
vhost_vdpa_set_vring_enable(struct virtio_user_dev * dev,struct vhost_vring_state * state)380 vhost_vdpa_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
381 {
382 	struct vhost_vdpa_data *data = dev->backend_data;
383 
384 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_SET_VRING_ENABLE, state);
385 }
386 
387 static int
vhost_vdpa_set_vring_num(struct virtio_user_dev * dev,struct vhost_vring_state * state)388 vhost_vdpa_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
389 {
390 	struct vhost_vdpa_data *data = dev->backend_data;
391 
392 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_NUM, state);
393 }
394 
395 static int
vhost_vdpa_set_vring_base(struct virtio_user_dev * dev,struct vhost_vring_state * state)396 vhost_vdpa_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
397 {
398 	struct vhost_vdpa_data *data = dev->backend_data;
399 
400 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_BASE, state);
401 }
402 
403 static int
vhost_vdpa_get_vring_base(struct virtio_user_dev * dev,struct vhost_vring_state * state)404 vhost_vdpa_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
405 {
406 	struct vhost_vdpa_data *data = dev->backend_data;
407 
408 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_VRING_BASE, state);
409 }
410 
411 static int
vhost_vdpa_set_vring_call(struct virtio_user_dev * dev,struct vhost_vring_file * file)412 vhost_vdpa_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
413 {
414 	struct vhost_vdpa_data *data = dev->backend_data;
415 
416 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_CALL, file);
417 }
418 
419 static int
vhost_vdpa_set_vring_kick(struct virtio_user_dev * dev,struct vhost_vring_file * file)420 vhost_vdpa_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
421 {
422 	struct vhost_vdpa_data *data = dev->backend_data;
423 
424 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_KICK, file);
425 }
426 
427 static int
vhost_vdpa_set_vring_addr(struct virtio_user_dev * dev,struct vhost_vring_addr * addr)428 vhost_vdpa_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
429 {
430 	struct vhost_vdpa_data *data = dev->backend_data;
431 
432 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_ADDR, addr);
433 }
434 
435 static int
vhost_vdpa_get_status(struct virtio_user_dev * dev,uint8_t * status)436 vhost_vdpa_get_status(struct virtio_user_dev *dev, uint8_t *status)
437 {
438 	struct vhost_vdpa_data *data = dev->backend_data;
439 
440 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_GET_STATUS, status);
441 }
442 
443 static int
vhost_vdpa_set_status(struct virtio_user_dev * dev,uint8_t status)444 vhost_vdpa_set_status(struct virtio_user_dev *dev, uint8_t status)
445 {
446 	struct vhost_vdpa_data *data = dev->backend_data;
447 
448 	return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_SET_STATUS, &status);
449 }
450 
451 static int
vhost_vdpa_get_config(struct virtio_user_dev * dev,uint8_t * data,uint32_t off,uint32_t len)452 vhost_vdpa_get_config(struct virtio_user_dev *dev, uint8_t *data, uint32_t off, uint32_t len)
453 {
454 	struct vhost_vdpa_data *vdpa_data = dev->backend_data;
455 	struct vhost_vdpa_config *config;
456 	int ret = 0;
457 
458 	config = malloc(sizeof(*config) + len);
459 	if (!config) {
460 		PMD_DRV_LOG(ERR, "Failed to allocate vDPA config data");
461 		return -1;
462 	}
463 
464 	config->off = off;
465 	config->len = len;
466 
467 	ret = vhost_vdpa_ioctl(vdpa_data->vhostfd, VHOST_VDPA_GET_CONFIG, config);
468 	if (ret) {
469 		PMD_DRV_LOG(ERR, "Failed to get vDPA config (offset 0x%x, len 0x%x)", off, len);
470 		ret = -1;
471 		goto out;
472 	}
473 
474 	memcpy(data, config->buf, len);
475 out:
476 	free(config);
477 
478 	return ret;
479 }
480 
481 static int
vhost_vdpa_set_config(struct virtio_user_dev * dev,const uint8_t * data,uint32_t off,uint32_t len)482 vhost_vdpa_set_config(struct virtio_user_dev *dev, const uint8_t *data, uint32_t off, uint32_t len)
483 {
484 	struct vhost_vdpa_data *vdpa_data = dev->backend_data;
485 	struct vhost_vdpa_config *config;
486 	int ret = 0;
487 
488 	config = malloc(sizeof(*config) + len);
489 	if (!config) {
490 		PMD_DRV_LOG(ERR, "Failed to allocate vDPA config data");
491 		return -1;
492 	}
493 
494 	config->off = off;
495 	config->len = len;
496 
497 	memcpy(config->buf, data, len);
498 
499 	ret = vhost_vdpa_ioctl(vdpa_data->vhostfd, VHOST_VDPA_SET_CONFIG, config);
500 	if (ret) {
501 		PMD_DRV_LOG(ERR, "Failed to set vDPA config (offset 0x%x, len 0x%x)", off, len);
502 		ret = -1;
503 	}
504 
505 	free(config);
506 
507 	return ret;
508 }
509 
510 /**
511  * Set up environment to talk with a vhost vdpa backend.
512  *
513  * @return
514  *   - (-1) if fail to set up;
515  *   - (>=0) if successful.
516  */
517 static int
vhost_vdpa_setup(struct virtio_user_dev * dev)518 vhost_vdpa_setup(struct virtio_user_dev *dev)
519 {
520 	struct vhost_vdpa_data *data;
521 	uint32_t did = (uint32_t)-1;
522 
523 	data = malloc(sizeof(*data));
524 	if (!data) {
525 		PMD_DRV_LOG(ERR, "(%s) Faidle to allocate backend data", dev->path);
526 		return -1;
527 	}
528 
529 	data->vhostfd = open(dev->path, O_RDWR);
530 	if (data->vhostfd < 0) {
531 		PMD_DRV_LOG(ERR, "Failed to open %s: %s",
532 				dev->path, strerror(errno));
533 		free(data);
534 		return -1;
535 	}
536 
537 	if (ioctl(data->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
538 			did != VIRTIO_ID_NETWORK) {
539 		PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u", did);
540 		close(data->vhostfd);
541 		free(data);
542 		return -1;
543 	}
544 
545 	dev->backend_data = data;
546 
547 	return 0;
548 }
549 
550 static int
vhost_vdpa_destroy(struct virtio_user_dev * dev)551 vhost_vdpa_destroy(struct virtio_user_dev *dev)
552 {
553 	struct vhost_vdpa_data *data = dev->backend_data;
554 
555 	if (!data)
556 		return 0;
557 
558 	close(data->vhostfd);
559 
560 	free(data);
561 	dev->backend_data = NULL;
562 
563 	return 0;
564 }
565 
566 static int
vhost_vdpa_enable_queue_pair(struct virtio_user_dev * dev,uint16_t pair_idx,int enable)567 vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
568 			       uint16_t pair_idx,
569 			       int enable)
570 {
571 	int i;
572 
573 	if (dev->qp_enabled[pair_idx] == enable)
574 		return 0;
575 
576 	for (i = 0; i < 2; ++i) {
577 		struct vhost_vring_state state = {
578 			.index = pair_idx * 2 + i,
579 			.num   = enable,
580 		};
581 
582 		if (vhost_vdpa_set_vring_enable(dev, &state))
583 			return -1;
584 	}
585 
586 	dev->qp_enabled[pair_idx] = enable;
587 
588 	return 0;
589 }
590 
591 static int
vhost_vdpa_get_backend_features(uint64_t * features)592 vhost_vdpa_get_backend_features(uint64_t *features)
593 {
594 	*features = 0;
595 
596 	return 0;
597 }
598 
599 static int
vhost_vdpa_update_link_state(struct virtio_user_dev * dev __rte_unused)600 vhost_vdpa_update_link_state(struct virtio_user_dev *dev __rte_unused)
601 {
602 	/* Nothing to update (for now?) */
603 	return 0;
604 }
605 
606 static int
vhost_vdpa_get_intr_fd(struct virtio_user_dev * dev __rte_unused)607 vhost_vdpa_get_intr_fd(struct virtio_user_dev *dev __rte_unused)
608 {
609 	/* No link state interrupt with Vhost-vDPA */
610 	return -1;
611 }
612 
613 struct virtio_user_backend_ops virtio_ops_vdpa = {
614 	.setup = vhost_vdpa_setup,
615 	.destroy = vhost_vdpa_destroy,
616 	.get_backend_features = vhost_vdpa_get_backend_features,
617 	.set_owner = vhost_vdpa_set_owner,
618 	.get_features = vhost_vdpa_get_features,
619 	.set_features = vhost_vdpa_set_features,
620 	.set_memory_table = vhost_vdpa_set_memory_table,
621 	.set_vring_num = vhost_vdpa_set_vring_num,
622 	.set_vring_base = vhost_vdpa_set_vring_base,
623 	.get_vring_base = vhost_vdpa_get_vring_base,
624 	.set_vring_call = vhost_vdpa_set_vring_call,
625 	.set_vring_kick = vhost_vdpa_set_vring_kick,
626 	.set_vring_addr = vhost_vdpa_set_vring_addr,
627 	.get_status = vhost_vdpa_get_status,
628 	.set_status = vhost_vdpa_set_status,
629 	.get_config = vhost_vdpa_get_config,
630 	.set_config = vhost_vdpa_set_config,
631 	.enable_qp = vhost_vdpa_enable_queue_pair,
632 	.dma_map = vhost_vdpa_dma_map_batch,
633 	.dma_unmap = vhost_vdpa_dma_unmap_batch,
634 	.update_link_state = vhost_vdpa_update_link_state,
635 	.get_intr_fd = vhost_vdpa_get_intr_fd,
636 };
637