• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VDPA networking device simulator.
4  *
5  * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6  *     Author: Jason Wang <jasowang@redhat.com>
7  *
8  */
9 
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/poll.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/uuid.h>
20 #include <linux/iommu.h>
21 #include <linux/dma-map-ops.h>
22 #include <linux/sysfs.h>
23 #include <linux/file.h>
24 #include <linux/etherdevice.h>
25 #include <linux/vringh.h>
26 #include <linux/vdpa.h>
27 #include <linux/virtio_byteorder.h>
28 #include <linux/vhost_iotlb.h>
29 #include <uapi/linux/virtio_config.h>
30 #include <uapi/linux/virtio_net.h>
31 
32 #define DRV_VERSION  "0.1"
33 #define DRV_AUTHOR   "Jason Wang <jasowang@redhat.com>"
34 #define DRV_DESC     "vDPA Device Simulator"
35 #define DRV_LICENSE  "GPL v2"
36 
37 static int batch_mapping = 1;
38 module_param(batch_mapping, int, 0444);
39 MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
40 
41 static char *macaddr;
42 module_param(macaddr, charp, 0);
43 MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
44 
45 u8 macaddr_buf[ETH_ALEN];
46 
47 struct vdpasim_virtqueue {
48 	struct vringh vring;
49 	struct vringh_kiov iov;
50 	unsigned short head;
51 	bool ready;
52 	u64 desc_addr;
53 	u64 device_addr;
54 	u64 driver_addr;
55 	u32 num;
56 	void *private;
57 	irqreturn_t (*cb)(void *data);
58 };
59 
60 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
61 #define VDPASIM_QUEUE_MAX 256
62 #define VDPASIM_DEVICE_ID 0x1
63 #define VDPASIM_VENDOR_ID 0
64 #define VDPASIM_VQ_NUM 0x2
65 #define VDPASIM_NAME "vdpasim-netdev"
66 
67 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
68 			      (1ULL << VIRTIO_F_VERSION_1)  |
69 			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
70 			      (1ULL << VIRTIO_NET_F_MAC);
71 
72 struct vdpasim;
73 
74 struct vdpasim_dev_attr {
75 	size_t config_size;
76 	int nvqs;
77 	void (*get_config)(struct vdpasim *vdpasim, void *config);
78 };
79 
80 /* State of each vdpasim device */
81 struct vdpasim {
82 	struct vdpa_device vdpa;
83 	struct vdpasim_virtqueue *vqs;
84 	struct work_struct work;
85 	struct vdpasim_dev_attr dev_attr;
86 	/* spinlock to synchronize virtqueue state */
87 	spinlock_t lock;
88 	/* virtio config according to device type */
89 	void *config;
90 	struct vhost_iotlb *iommu;
91 	void *buffer;
92 	u32 status;
93 	u32 generation;
94 	u64 features;
95 	/* spinlock to synchronize iommu table */
96 	spinlock_t iommu_lock;
97 };
98 
99 /* TODO: cross-endian support */
vdpasim_is_little_endian(struct vdpasim * vdpasim)100 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
101 {
102 	return virtio_legacy_is_little_endian() ||
103 		(vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
104 }
105 
vdpasim16_to_cpu(struct vdpasim * vdpasim,__virtio16 val)106 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
107 {
108 	return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
109 }
110 
cpu_to_vdpasim16(struct vdpasim * vdpasim,u16 val)111 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
112 {
113 	return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
114 }
115 
116 static struct vdpasim *vdpasim_dev;
117 
vdpa_to_sim(struct vdpa_device * vdpa)118 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
119 {
120 	return container_of(vdpa, struct vdpasim, vdpa);
121 }
122 
dev_to_sim(struct device * dev)123 static struct vdpasim *dev_to_sim(struct device *dev)
124 {
125 	struct vdpa_device *vdpa = dev_to_vdpa(dev);
126 
127 	return vdpa_to_sim(vdpa);
128 }
129 
vdpasim_queue_ready(struct vdpasim * vdpasim,unsigned int idx)130 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
131 {
132 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
133 
134 	vringh_init_iotlb(&vq->vring, vdpasim_features,
135 			  VDPASIM_QUEUE_MAX, false,
136 			  (struct vring_desc *)(uintptr_t)vq->desc_addr,
137 			  (struct vring_avail *)
138 			  (uintptr_t)vq->driver_addr,
139 			  (struct vring_used *)
140 			  (uintptr_t)vq->device_addr);
141 }
142 
vdpasim_vq_reset(struct vdpasim_virtqueue * vq)143 static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
144 {
145 	vq->ready = false;
146 	vq->desc_addr = 0;
147 	vq->driver_addr = 0;
148 	vq->device_addr = 0;
149 	vq->cb = NULL;
150 	vq->private = NULL;
151 	vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
152 			  false, NULL, NULL, NULL);
153 }
154 
vdpasim_reset(struct vdpasim * vdpasim)155 static void vdpasim_reset(struct vdpasim *vdpasim)
156 {
157 	int i;
158 
159 	for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
160 		vdpasim_vq_reset(&vdpasim->vqs[i]);
161 
162 	spin_lock(&vdpasim->iommu_lock);
163 	vhost_iotlb_reset(vdpasim->iommu);
164 	spin_unlock(&vdpasim->iommu_lock);
165 
166 	vdpasim->features = 0;
167 	vdpasim->status = 0;
168 	++vdpasim->generation;
169 }
170 
vdpasim_work(struct work_struct * work)171 static void vdpasim_work(struct work_struct *work)
172 {
173 	struct vdpasim *vdpasim = container_of(work, struct
174 						 vdpasim, work);
175 	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
176 	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
177 	ssize_t read, write;
178 	size_t total_write;
179 	int pkts = 0;
180 	int err;
181 
182 	spin_lock(&vdpasim->lock);
183 
184 	if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
185 		goto out;
186 
187 	if (!txq->ready || !rxq->ready)
188 		goto out;
189 
190 	while (true) {
191 		total_write = 0;
192 		err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
193 					   &txq->head, GFP_ATOMIC);
194 		if (err <= 0)
195 			break;
196 
197 		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
198 					   &rxq->head, GFP_ATOMIC);
199 		if (err <= 0) {
200 			vringh_complete_iotlb(&txq->vring, txq->head, 0);
201 			break;
202 		}
203 
204 		while (true) {
205 			read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
206 						     vdpasim->buffer,
207 						     PAGE_SIZE);
208 			if (read <= 0)
209 				break;
210 
211 			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
212 						      vdpasim->buffer, read);
213 			if (write <= 0)
214 				break;
215 
216 			total_write += write;
217 		}
218 
219 		/* Make sure data is wrote before advancing index */
220 		smp_wmb();
221 
222 		vringh_complete_iotlb(&txq->vring, txq->head, 0);
223 		vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
224 
225 		/* Make sure used is visible before rasing the interrupt. */
226 		smp_wmb();
227 
228 		local_bh_disable();
229 		if (txq->cb)
230 			txq->cb(txq->private);
231 		if (rxq->cb)
232 			rxq->cb(rxq->private);
233 		local_bh_enable();
234 
235 		if (++pkts > 4) {
236 			schedule_work(&vdpasim->work);
237 			goto out;
238 		}
239 	}
240 
241 out:
242 	spin_unlock(&vdpasim->lock);
243 }
244 
dir_to_perm(enum dma_data_direction dir)245 static int dir_to_perm(enum dma_data_direction dir)
246 {
247 	int perm = -EFAULT;
248 
249 	switch (dir) {
250 	case DMA_FROM_DEVICE:
251 		perm = VHOST_MAP_WO;
252 		break;
253 	case DMA_TO_DEVICE:
254 		perm = VHOST_MAP_RO;
255 		break;
256 	case DMA_BIDIRECTIONAL:
257 		perm = VHOST_MAP_RW;
258 		break;
259 	default:
260 		break;
261 	}
262 
263 	return perm;
264 }
265 
vdpasim_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,unsigned long attrs)266 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
267 				   unsigned long offset, size_t size,
268 				   enum dma_data_direction dir,
269 				   unsigned long attrs)
270 {
271 	struct vdpasim *vdpasim = dev_to_sim(dev);
272 	struct vhost_iotlb *iommu = vdpasim->iommu;
273 	u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
274 	int ret, perm = dir_to_perm(dir);
275 
276 	if (perm < 0)
277 		return DMA_MAPPING_ERROR;
278 
279 	/* For simplicity, use identical mapping to avoid e.g iova
280 	 * allocator.
281 	 */
282 	spin_lock(&vdpasim->iommu_lock);
283 	ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
284 				    pa, dir_to_perm(dir));
285 	spin_unlock(&vdpasim->iommu_lock);
286 	if (ret)
287 		return DMA_MAPPING_ERROR;
288 
289 	return (dma_addr_t)(pa);
290 }
291 
vdpasim_unmap_page(struct device * dev,dma_addr_t dma_addr,size_t size,enum dma_data_direction dir,unsigned long attrs)292 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
293 			       size_t size, enum dma_data_direction dir,
294 			       unsigned long attrs)
295 {
296 	struct vdpasim *vdpasim = dev_to_sim(dev);
297 	struct vhost_iotlb *iommu = vdpasim->iommu;
298 
299 	spin_lock(&vdpasim->iommu_lock);
300 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
301 			      (u64)dma_addr + size - 1);
302 	spin_unlock(&vdpasim->iommu_lock);
303 }
304 
vdpasim_alloc_coherent(struct device * dev,size_t size,dma_addr_t * dma_addr,gfp_t flag,unsigned long attrs)305 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
306 				    dma_addr_t *dma_addr, gfp_t flag,
307 				    unsigned long attrs)
308 {
309 	struct vdpasim *vdpasim = dev_to_sim(dev);
310 	struct vhost_iotlb *iommu = vdpasim->iommu;
311 	void *addr = kmalloc(size, flag);
312 	int ret;
313 
314 	spin_lock(&vdpasim->iommu_lock);
315 	if (!addr) {
316 		*dma_addr = DMA_MAPPING_ERROR;
317 	} else {
318 		u64 pa = virt_to_phys(addr);
319 
320 		ret = vhost_iotlb_add_range(iommu, (u64)pa,
321 					    (u64)pa + size - 1,
322 					    pa, VHOST_MAP_RW);
323 		if (ret) {
324 			*dma_addr = DMA_MAPPING_ERROR;
325 			kfree(addr);
326 			addr = NULL;
327 		} else
328 			*dma_addr = (dma_addr_t)pa;
329 	}
330 	spin_unlock(&vdpasim->iommu_lock);
331 
332 	return addr;
333 }
334 
vdpasim_free_coherent(struct device * dev,size_t size,void * vaddr,dma_addr_t dma_addr,unsigned long attrs)335 static void vdpasim_free_coherent(struct device *dev, size_t size,
336 				  void *vaddr, dma_addr_t dma_addr,
337 				  unsigned long attrs)
338 {
339 	struct vdpasim *vdpasim = dev_to_sim(dev);
340 	struct vhost_iotlb *iommu = vdpasim->iommu;
341 
342 	spin_lock(&vdpasim->iommu_lock);
343 	vhost_iotlb_del_range(iommu, (u64)dma_addr,
344 			      (u64)dma_addr + size - 1);
345 	spin_unlock(&vdpasim->iommu_lock);
346 
347 	kfree(phys_to_virt((uintptr_t)dma_addr));
348 }
349 
350 static const struct dma_map_ops vdpasim_dma_ops = {
351 	.map_page = vdpasim_map_page,
352 	.unmap_page = vdpasim_unmap_page,
353 	.alloc = vdpasim_alloc_coherent,
354 	.free = vdpasim_free_coherent,
355 };
356 
357 static const struct vdpa_config_ops vdpasim_net_config_ops;
358 static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
359 
vdpasim_create(struct vdpasim_dev_attr * dev_attr)360 static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
361 {
362 	const struct vdpa_config_ops *ops;
363 	struct vdpasim *vdpasim;
364 	struct device *dev;
365 	int i, ret = -ENOMEM;
366 
367 	if (batch_mapping)
368 		ops = &vdpasim_net_batch_config_ops;
369 	else
370 		ops = &vdpasim_net_config_ops;
371 
372 	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
373 				    dev_attr->nvqs);
374 	if (!vdpasim)
375 		goto err_alloc;
376 
377 	vdpasim->dev_attr = *dev_attr;
378 	INIT_WORK(&vdpasim->work, vdpasim_work);
379 	spin_lock_init(&vdpasim->lock);
380 	spin_lock_init(&vdpasim->iommu_lock);
381 
382 	dev = &vdpasim->vdpa.dev;
383 	dev->dma_mask = &dev->coherent_dma_mask;
384 	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
385 		goto err_iommu;
386 	set_dma_ops(dev, &vdpasim_dma_ops);
387 
388 	vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
389 	if (!vdpasim->config)
390 		goto err_iommu;
391 
392 	vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
393 			       GFP_KERNEL);
394 	if (!vdpasim->vqs)
395 		goto err_iommu;
396 
397 	vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
398 	if (!vdpasim->iommu)
399 		goto err_iommu;
400 
401 	vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
402 	if (!vdpasim->buffer)
403 		goto err_iommu;
404 
405 	if (macaddr) {
406 		mac_pton(macaddr, macaddr_buf);
407 		if (!is_valid_ether_addr(macaddr_buf)) {
408 			ret = -EADDRNOTAVAIL;
409 			goto err_iommu;
410 		}
411 	} else {
412 		eth_random_addr(macaddr_buf);
413 	}
414 
415 	for (i = 0; i < dev_attr->nvqs; i++)
416 		vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
417 
418 	vdpasim->vdpa.dma_dev = dev;
419 	ret = vdpa_register_device(&vdpasim->vdpa);
420 	if (ret)
421 		goto err_iommu;
422 
423 	return vdpasim;
424 
425 err_iommu:
426 	put_device(dev);
427 err_alloc:
428 	return ERR_PTR(ret);
429 }
430 
vdpasim_set_vq_address(struct vdpa_device * vdpa,u16 idx,u64 desc_area,u64 driver_area,u64 device_area)431 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
432 				  u64 desc_area, u64 driver_area,
433 				  u64 device_area)
434 {
435 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
436 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
437 
438 	vq->desc_addr = desc_area;
439 	vq->driver_addr = driver_area;
440 	vq->device_addr = device_area;
441 
442 	return 0;
443 }
444 
vdpasim_set_vq_num(struct vdpa_device * vdpa,u16 idx,u32 num)445 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
446 {
447 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
448 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
449 
450 	vq->num = num;
451 }
452 
vdpasim_kick_vq(struct vdpa_device * vdpa,u16 idx)453 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
454 {
455 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
456 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
457 
458 	if (vq->ready)
459 		schedule_work(&vdpasim->work);
460 }
461 
vdpasim_set_vq_cb(struct vdpa_device * vdpa,u16 idx,struct vdpa_callback * cb)462 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
463 			      struct vdpa_callback *cb)
464 {
465 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
466 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
467 
468 	vq->cb = cb->callback;
469 	vq->private = cb->private;
470 }
471 
vdpasim_set_vq_ready(struct vdpa_device * vdpa,u16 idx,bool ready)472 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
473 {
474 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
475 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
476 
477 	spin_lock(&vdpasim->lock);
478 	vq->ready = ready;
479 	if (vq->ready)
480 		vdpasim_queue_ready(vdpasim, idx);
481 	spin_unlock(&vdpasim->lock);
482 }
483 
vdpasim_get_vq_ready(struct vdpa_device * vdpa,u16 idx)484 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
485 {
486 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
487 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
488 
489 	return vq->ready;
490 }
491 
vdpasim_set_vq_state(struct vdpa_device * vdpa,u16 idx,const struct vdpa_vq_state * state)492 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
493 				const struct vdpa_vq_state *state)
494 {
495 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
496 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
497 	struct vringh *vrh = &vq->vring;
498 
499 	spin_lock(&vdpasim->lock);
500 	vrh->last_avail_idx = state->avail_index;
501 	spin_unlock(&vdpasim->lock);
502 
503 	return 0;
504 }
505 
vdpasim_get_vq_state(struct vdpa_device * vdpa,u16 idx,struct vdpa_vq_state * state)506 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
507 				struct vdpa_vq_state *state)
508 {
509 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
510 	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
511 	struct vringh *vrh = &vq->vring;
512 
513 	state->avail_index = vrh->last_avail_idx;
514 	return 0;
515 }
516 
vdpasim_get_vq_align(struct vdpa_device * vdpa)517 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
518 {
519 	return VDPASIM_QUEUE_ALIGN;
520 }
521 
vdpasim_get_features(struct vdpa_device * vdpa)522 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
523 {
524 	return vdpasim_features;
525 }
526 
vdpasim_set_features(struct vdpa_device * vdpa,u64 features)527 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
528 {
529 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
530 
531 	/* DMA mapping must be done by driver */
532 	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
533 		return -EINVAL;
534 
535 	vdpasim->features = features & vdpasim_features;
536 
537 	return 0;
538 }
539 
vdpasim_set_config_cb(struct vdpa_device * vdpa,struct vdpa_callback * cb)540 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
541 				  struct vdpa_callback *cb)
542 {
543 	/* We don't support config interrupt */
544 }
545 
vdpasim_get_vq_num_max(struct vdpa_device * vdpa)546 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
547 {
548 	return VDPASIM_QUEUE_MAX;
549 }
550 
vdpasim_get_device_id(struct vdpa_device * vdpa)551 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
552 {
553 	return VDPASIM_DEVICE_ID;
554 }
555 
vdpasim_get_vendor_id(struct vdpa_device * vdpa)556 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
557 {
558 	return VDPASIM_VENDOR_ID;
559 }
560 
vdpasim_get_status(struct vdpa_device * vdpa)561 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
562 {
563 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
564 	u8 status;
565 
566 	spin_lock(&vdpasim->lock);
567 	status = vdpasim->status;
568 	spin_unlock(&vdpasim->lock);
569 
570 	return status;
571 }
572 
vdpasim_set_status(struct vdpa_device * vdpa,u8 status)573 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
574 {
575 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
576 
577 	spin_lock(&vdpasim->lock);
578 	vdpasim->status = status;
579 	if (status == 0)
580 		vdpasim_reset(vdpasim);
581 	spin_unlock(&vdpasim->lock);
582 }
583 
vdpasim_get_config(struct vdpa_device * vdpa,unsigned int offset,void * buf,unsigned int len)584 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
585 			     void *buf, unsigned int len)
586 {
587 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
588 
589 	if (offset + len > vdpasim->dev_attr.config_size)
590 		return;
591 
592 	if (vdpasim->dev_attr.get_config)
593 		vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
594 
595 	memcpy(buf, vdpasim->config + offset, len);
596 }
597 
vdpasim_set_config(struct vdpa_device * vdpa,unsigned int offset,const void * buf,unsigned int len)598 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
599 			     const void *buf, unsigned int len)
600 {
601 	/* No writable config supportted by vdpasim */
602 }
603 
vdpasim_get_generation(struct vdpa_device * vdpa)604 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
605 {
606 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
607 
608 	return vdpasim->generation;
609 }
610 
vdpasim_get_iova_range(struct vdpa_device * vdpa)611 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
612 {
613 	struct vdpa_iova_range range = {
614 		.first = 0ULL,
615 		.last = ULLONG_MAX,
616 	};
617 
618 	return range;
619 }
620 
vdpasim_set_map(struct vdpa_device * vdpa,struct vhost_iotlb * iotlb)621 static int vdpasim_set_map(struct vdpa_device *vdpa,
622 			   struct vhost_iotlb *iotlb)
623 {
624 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
625 	struct vhost_iotlb_map *map;
626 	u64 start = 0ULL, last = 0ULL - 1;
627 	int ret;
628 
629 	spin_lock(&vdpasim->iommu_lock);
630 	vhost_iotlb_reset(vdpasim->iommu);
631 
632 	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
633 	     map = vhost_iotlb_itree_next(map, start, last)) {
634 		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
635 					    map->last, map->addr, map->perm);
636 		if (ret)
637 			goto err;
638 	}
639 	spin_unlock(&vdpasim->iommu_lock);
640 	return 0;
641 
642 err:
643 	vhost_iotlb_reset(vdpasim->iommu);
644 	spin_unlock(&vdpasim->iommu_lock);
645 	return ret;
646 }
647 
vdpasim_dma_map(struct vdpa_device * vdpa,u64 iova,u64 size,u64 pa,u32 perm)648 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
649 			   u64 pa, u32 perm)
650 {
651 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
652 	int ret;
653 
654 	spin_lock(&vdpasim->iommu_lock);
655 	ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
656 				    perm);
657 	spin_unlock(&vdpasim->iommu_lock);
658 
659 	return ret;
660 }
661 
vdpasim_dma_unmap(struct vdpa_device * vdpa,u64 iova,u64 size)662 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
663 {
664 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
665 
666 	spin_lock(&vdpasim->iommu_lock);
667 	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
668 	spin_unlock(&vdpasim->iommu_lock);
669 
670 	return 0;
671 }
672 
vdpasim_free(struct vdpa_device * vdpa)673 static void vdpasim_free(struct vdpa_device *vdpa)
674 {
675 	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
676 
677 	cancel_work_sync(&vdpasim->work);
678 	kfree(vdpasim->buffer);
679 	if (vdpasim->iommu)
680 		vhost_iotlb_free(vdpasim->iommu);
681 	kfree(vdpasim->vqs);
682 	kfree(vdpasim->config);
683 }
684 
685 static const struct vdpa_config_ops vdpasim_net_config_ops = {
686 	.set_vq_address         = vdpasim_set_vq_address,
687 	.set_vq_num             = vdpasim_set_vq_num,
688 	.kick_vq                = vdpasim_kick_vq,
689 	.set_vq_cb              = vdpasim_set_vq_cb,
690 	.set_vq_ready           = vdpasim_set_vq_ready,
691 	.get_vq_ready           = vdpasim_get_vq_ready,
692 	.set_vq_state           = vdpasim_set_vq_state,
693 	.get_vq_state           = vdpasim_get_vq_state,
694 	.get_vq_align           = vdpasim_get_vq_align,
695 	.get_features           = vdpasim_get_features,
696 	.set_features           = vdpasim_set_features,
697 	.set_config_cb          = vdpasim_set_config_cb,
698 	.get_vq_num_max         = vdpasim_get_vq_num_max,
699 	.get_device_id          = vdpasim_get_device_id,
700 	.get_vendor_id          = vdpasim_get_vendor_id,
701 	.get_status             = vdpasim_get_status,
702 	.set_status             = vdpasim_set_status,
703 	.get_config             = vdpasim_get_config,
704 	.set_config             = vdpasim_set_config,
705 	.get_generation         = vdpasim_get_generation,
706 	.get_iova_range         = vdpasim_get_iova_range,
707 	.dma_map                = vdpasim_dma_map,
708 	.dma_unmap              = vdpasim_dma_unmap,
709 	.free                   = vdpasim_free,
710 };
711 
712 static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
713 	.set_vq_address         = vdpasim_set_vq_address,
714 	.set_vq_num             = vdpasim_set_vq_num,
715 	.kick_vq                = vdpasim_kick_vq,
716 	.set_vq_cb              = vdpasim_set_vq_cb,
717 	.set_vq_ready           = vdpasim_set_vq_ready,
718 	.get_vq_ready           = vdpasim_get_vq_ready,
719 	.set_vq_state           = vdpasim_set_vq_state,
720 	.get_vq_state           = vdpasim_get_vq_state,
721 	.get_vq_align           = vdpasim_get_vq_align,
722 	.get_features           = vdpasim_get_features,
723 	.set_features           = vdpasim_set_features,
724 	.set_config_cb          = vdpasim_set_config_cb,
725 	.get_vq_num_max         = vdpasim_get_vq_num_max,
726 	.get_device_id          = vdpasim_get_device_id,
727 	.get_vendor_id          = vdpasim_get_vendor_id,
728 	.get_status             = vdpasim_get_status,
729 	.set_status             = vdpasim_set_status,
730 	.get_config             = vdpasim_get_config,
731 	.set_config             = vdpasim_set_config,
732 	.get_generation         = vdpasim_get_generation,
733 	.get_iova_range         = vdpasim_get_iova_range,
734 	.set_map                = vdpasim_set_map,
735 	.free                   = vdpasim_free,
736 };
737 
vdpasim_net_get_config(struct vdpasim * vdpasim,void * config)738 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
739 {
740 	struct virtio_net_config *net_config =
741 		(struct virtio_net_config *)config;
742 
743 	net_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
744 	net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
745 	memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
746 }
747 
vdpasim_dev_init(void)748 static int __init vdpasim_dev_init(void)
749 {
750 	struct vdpasim_dev_attr dev_attr = {};
751 
752 	dev_attr.nvqs = VDPASIM_VQ_NUM;
753 	dev_attr.config_size = sizeof(struct virtio_net_config);
754 	dev_attr.get_config = vdpasim_net_get_config;
755 
756 	vdpasim_dev = vdpasim_create(&dev_attr);
757 
758 	if (!IS_ERR(vdpasim_dev))
759 		return 0;
760 
761 	return PTR_ERR(vdpasim_dev);
762 }
763 
vdpasim_dev_exit(void)764 static void __exit vdpasim_dev_exit(void)
765 {
766 	struct vdpa_device *vdpa = &vdpasim_dev->vdpa;
767 
768 	vdpa_unregister_device(vdpa);
769 }
770 
771 module_init(vdpasim_dev_init)
772 module_exit(vdpasim_dev_exit)
773 
774 MODULE_VERSION(DRV_VERSION);
775 MODULE_LICENSE(DRV_LICENSE);
776 MODULE_AUTHOR(DRV_AUTHOR);
777 MODULE_DESCRIPTION(DRV_DESC);
778