1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA networking device simulator.
5 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.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>
32 #define DRV_VERSION "0.1"
34 #define DRV_DESC "vDPA Device Simulator"
35 #define DRV_LICENSE "GPL v2"
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");
42 module_param(macaddr, charp, 0);
43 MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
45 struct vdpasim_virtqueue {
47 struct vringh_kiov iov;
55 irqreturn_t (*cb)(void *data);
58 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
59 #define VDPASIM_QUEUE_MAX 256
60 #define VDPASIM_DEVICE_ID 0x1
61 #define VDPASIM_VENDOR_ID 0
62 #define VDPASIM_VQ_NUM 0x2
63 #define VDPASIM_NAME "vdpasim-netdev"
65 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
66 (1ULL << VIRTIO_F_VERSION_1) |
67 (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
68 (1ULL << VIRTIO_NET_F_MAC);
70 /* State of each vdpasim device */
72 struct vdpa_device vdpa;
73 struct vdpasim_virtqueue vqs[VDPASIM_VQ_NUM];
74 struct work_struct work;
75 /* spinlock to synchronize virtqueue state */
77 struct virtio_net_config config;
78 struct vhost_iotlb *iommu;
83 /* spinlock to synchronize iommu table */
84 spinlock_t iommu_lock;
87 /* TODO: cross-endian support */
88 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
90 return virtio_legacy_is_little_endian() ||
91 (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
94 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
96 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
99 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
101 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
104 static struct vdpasim *vdpasim_dev;
106 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
108 return container_of(vdpa, struct vdpasim, vdpa);
111 static struct vdpasim *dev_to_sim(struct device *dev)
113 struct vdpa_device *vdpa = dev_to_vdpa(dev);
115 return vdpa_to_sim(vdpa);
118 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
120 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
122 vringh_init_iotlb(&vq->vring, vdpasim_features,
123 VDPASIM_QUEUE_MAX, false,
124 (struct vring_desc *)(uintptr_t)vq->desc_addr,
125 (struct vring_avail *)
126 (uintptr_t)vq->driver_addr,
127 (struct vring_used *)
128 (uintptr_t)vq->device_addr);
131 static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
139 vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
140 false, NULL, NULL, NULL);
143 static void vdpasim_reset(struct vdpasim *vdpasim)
147 for (i = 0; i < VDPASIM_VQ_NUM; i++)
148 vdpasim_vq_reset(&vdpasim->vqs[i]);
150 spin_lock(&vdpasim->iommu_lock);
151 vhost_iotlb_reset(vdpasim->iommu);
152 spin_unlock(&vdpasim->iommu_lock);
154 vdpasim->features = 0;
156 ++vdpasim->generation;
159 static void vdpasim_work(struct work_struct *work)
161 struct vdpasim *vdpasim = container_of(work, struct
163 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
164 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
170 spin_lock(&vdpasim->lock);
172 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
175 if (!txq->ready || !rxq->ready)
180 err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
181 &txq->head, GFP_ATOMIC);
185 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
186 &rxq->head, GFP_ATOMIC);
188 vringh_complete_iotlb(&txq->vring, txq->head, 0);
193 read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
199 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
200 vdpasim->buffer, read);
204 total_write += write;
207 /* Make sure data is wrote before advancing index */
210 vringh_complete_iotlb(&txq->vring, txq->head, 0);
211 vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
213 /* Make sure used is visible before rasing the interrupt. */
218 txq->cb(txq->private);
220 rxq->cb(rxq->private);
224 schedule_work(&vdpasim->work);
230 spin_unlock(&vdpasim->lock);
233 static int dir_to_perm(enum dma_data_direction dir)
238 case DMA_FROM_DEVICE:
244 case DMA_BIDIRECTIONAL:
254 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
255 unsigned long offset, size_t size,
256 enum dma_data_direction dir,
259 struct vdpasim *vdpasim = dev_to_sim(dev);
260 struct vhost_iotlb *iommu = vdpasim->iommu;
261 u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
262 int ret, perm = dir_to_perm(dir);
265 return DMA_MAPPING_ERROR;
267 /* For simplicity, use identical mapping to avoid e.g iova
270 spin_lock(&vdpasim->iommu_lock);
271 ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
272 pa, dir_to_perm(dir));
273 spin_unlock(&vdpasim->iommu_lock);
275 return DMA_MAPPING_ERROR;
277 return (dma_addr_t)(pa);
280 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
281 size_t size, enum dma_data_direction dir,
284 struct vdpasim *vdpasim = dev_to_sim(dev);
285 struct vhost_iotlb *iommu = vdpasim->iommu;
287 spin_lock(&vdpasim->iommu_lock);
288 vhost_iotlb_del_range(iommu, (u64)dma_addr,
289 (u64)dma_addr + size - 1);
290 spin_unlock(&vdpasim->iommu_lock);
293 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
294 dma_addr_t *dma_addr, gfp_t flag,
297 struct vdpasim *vdpasim = dev_to_sim(dev);
298 struct vhost_iotlb *iommu = vdpasim->iommu;
299 void *addr = kmalloc(size, flag);
302 spin_lock(&vdpasim->iommu_lock);
304 *dma_addr = DMA_MAPPING_ERROR;
306 u64 pa = virt_to_phys(addr);
308 ret = vhost_iotlb_add_range(iommu, (u64)pa,
312 *dma_addr = DMA_MAPPING_ERROR;
316 *dma_addr = (dma_addr_t)pa;
318 spin_unlock(&vdpasim->iommu_lock);
323 static void vdpasim_free_coherent(struct device *dev, size_t size,
324 void *vaddr, dma_addr_t dma_addr,
327 struct vdpasim *vdpasim = dev_to_sim(dev);
328 struct vhost_iotlb *iommu = vdpasim->iommu;
330 spin_lock(&vdpasim->iommu_lock);
331 vhost_iotlb_del_range(iommu, (u64)dma_addr,
332 (u64)dma_addr + size - 1);
333 spin_unlock(&vdpasim->iommu_lock);
335 kfree(phys_to_virt((uintptr_t)dma_addr));
338 static const struct dma_map_ops vdpasim_dma_ops = {
339 .map_page = vdpasim_map_page,
340 .unmap_page = vdpasim_unmap_page,
341 .alloc = vdpasim_alloc_coherent,
342 .free = vdpasim_free_coherent,
345 static const struct vdpa_config_ops vdpasim_net_config_ops;
346 static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
348 static struct vdpasim *vdpasim_create(void)
350 const struct vdpa_config_ops *ops;
351 struct vdpasim *vdpasim;
356 ops = &vdpasim_net_batch_config_ops;
358 ops = &vdpasim_net_config_ops;
360 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, VDPASIM_VQ_NUM);
364 INIT_WORK(&vdpasim->work, vdpasim_work);
365 spin_lock_init(&vdpasim->lock);
366 spin_lock_init(&vdpasim->iommu_lock);
368 dev = &vdpasim->vdpa.dev;
369 dev->dma_mask = &dev->coherent_dma_mask;
370 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
372 set_dma_ops(dev, &vdpasim_dma_ops);
374 vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
378 vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
379 if (!vdpasim->buffer)
383 mac_pton(macaddr, vdpasim->config.mac);
384 if (!is_valid_ether_addr(vdpasim->config.mac)) {
385 ret = -EADDRNOTAVAIL;
389 eth_random_addr(vdpasim->config.mac);
392 vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
393 vringh_set_iotlb(&vdpasim->vqs[1].vring, vdpasim->iommu);
395 vdpasim->vdpa.dma_dev = dev;
396 ret = vdpa_register_device(&vdpasim->vdpa);
408 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
409 u64 desc_area, u64 driver_area,
412 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
413 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
415 vq->desc_addr = desc_area;
416 vq->driver_addr = driver_area;
417 vq->device_addr = device_area;
422 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
424 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
425 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
430 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
432 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
433 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
436 schedule_work(&vdpasim->work);
439 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
440 struct vdpa_callback *cb)
442 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
443 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
445 vq->cb = cb->callback;
446 vq->private = cb->private;
449 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
451 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
452 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
454 spin_lock(&vdpasim->lock);
457 vdpasim_queue_ready(vdpasim, idx);
458 spin_unlock(&vdpasim->lock);
461 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
463 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
464 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
469 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
470 const struct vdpa_vq_state *state)
472 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
473 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
474 struct vringh *vrh = &vq->vring;
476 spin_lock(&vdpasim->lock);
477 vrh->last_avail_idx = state->avail_index;
478 spin_unlock(&vdpasim->lock);
483 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
484 struct vdpa_vq_state *state)
486 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
487 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
488 struct vringh *vrh = &vq->vring;
490 state->avail_index = vrh->last_avail_idx;
494 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
496 return VDPASIM_QUEUE_ALIGN;
499 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
501 return vdpasim_features;
504 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
506 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
507 struct virtio_net_config *config = &vdpasim->config;
509 /* DMA mapping must be done by driver */
510 if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
513 vdpasim->features = features & vdpasim_features;
515 /* We generally only know whether guest is using the legacy interface
516 * here, so generally that's the earliest we can set config fields.
517 * Note: We actually require VIRTIO_F_ACCESS_PLATFORM above which
518 * implies VIRTIO_F_VERSION_1, but let's not try to be clever here.
521 config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
522 config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
526 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
527 struct vdpa_callback *cb)
529 /* We don't support config interrupt */
532 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
534 return VDPASIM_QUEUE_MAX;
537 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
539 return VDPASIM_DEVICE_ID;
542 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
544 return VDPASIM_VENDOR_ID;
547 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
549 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
552 spin_lock(&vdpasim->lock);
553 status = vdpasim->status;
554 spin_unlock(&vdpasim->lock);
559 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
561 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
563 spin_lock(&vdpasim->lock);
564 vdpasim->status = status;
566 vdpasim_reset(vdpasim);
567 spin_unlock(&vdpasim->lock);
570 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
571 void *buf, unsigned int len)
573 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
575 if (offset + len < sizeof(struct virtio_net_config))
576 memcpy(buf, (u8 *)&vdpasim->config + offset, len);
579 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
580 const void *buf, unsigned int len)
582 /* No writable config supportted by vdpasim */
585 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
587 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
589 return vdpasim->generation;
592 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
594 struct vdpa_iova_range range = {
602 static int vdpasim_set_map(struct vdpa_device *vdpa,
603 struct vhost_iotlb *iotlb)
605 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
606 struct vhost_iotlb_map *map;
607 u64 start = 0ULL, last = 0ULL - 1;
610 spin_lock(&vdpasim->iommu_lock);
611 vhost_iotlb_reset(vdpasim->iommu);
613 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
614 map = vhost_iotlb_itree_next(map, start, last)) {
615 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
616 map->last, map->addr, map->perm);
620 spin_unlock(&vdpasim->iommu_lock);
624 vhost_iotlb_reset(vdpasim->iommu);
625 spin_unlock(&vdpasim->iommu_lock);
629 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
632 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
635 spin_lock(&vdpasim->iommu_lock);
636 ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
638 spin_unlock(&vdpasim->iommu_lock);
643 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
645 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
647 spin_lock(&vdpasim->iommu_lock);
648 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
649 spin_unlock(&vdpasim->iommu_lock);
654 static void vdpasim_free(struct vdpa_device *vdpa)
656 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
658 cancel_work_sync(&vdpasim->work);
659 kfree(vdpasim->buffer);
661 vhost_iotlb_free(vdpasim->iommu);
664 static const struct vdpa_config_ops vdpasim_net_config_ops = {
665 .set_vq_address = vdpasim_set_vq_address,
666 .set_vq_num = vdpasim_set_vq_num,
667 .kick_vq = vdpasim_kick_vq,
668 .set_vq_cb = vdpasim_set_vq_cb,
669 .set_vq_ready = vdpasim_set_vq_ready,
670 .get_vq_ready = vdpasim_get_vq_ready,
671 .set_vq_state = vdpasim_set_vq_state,
672 .get_vq_state = vdpasim_get_vq_state,
673 .get_vq_align = vdpasim_get_vq_align,
674 .get_features = vdpasim_get_features,
675 .set_features = vdpasim_set_features,
676 .set_config_cb = vdpasim_set_config_cb,
677 .get_vq_num_max = vdpasim_get_vq_num_max,
678 .get_device_id = vdpasim_get_device_id,
679 .get_vendor_id = vdpasim_get_vendor_id,
680 .get_status = vdpasim_get_status,
681 .set_status = vdpasim_set_status,
682 .get_config = vdpasim_get_config,
683 .set_config = vdpasim_set_config,
684 .get_generation = vdpasim_get_generation,
685 .get_iova_range = vdpasim_get_iova_range,
686 .dma_map = vdpasim_dma_map,
687 .dma_unmap = vdpasim_dma_unmap,
688 .free = vdpasim_free,
691 static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
692 .set_vq_address = vdpasim_set_vq_address,
693 .set_vq_num = vdpasim_set_vq_num,
694 .kick_vq = vdpasim_kick_vq,
695 .set_vq_cb = vdpasim_set_vq_cb,
696 .set_vq_ready = vdpasim_set_vq_ready,
697 .get_vq_ready = vdpasim_get_vq_ready,
698 .set_vq_state = vdpasim_set_vq_state,
699 .get_vq_state = vdpasim_get_vq_state,
700 .get_vq_align = vdpasim_get_vq_align,
701 .get_features = vdpasim_get_features,
702 .set_features = vdpasim_set_features,
703 .set_config_cb = vdpasim_set_config_cb,
704 .get_vq_num_max = vdpasim_get_vq_num_max,
705 .get_device_id = vdpasim_get_device_id,
706 .get_vendor_id = vdpasim_get_vendor_id,
707 .get_status = vdpasim_get_status,
708 .set_status = vdpasim_set_status,
709 .get_config = vdpasim_get_config,
710 .set_config = vdpasim_set_config,
711 .get_generation = vdpasim_get_generation,
712 .get_iova_range = vdpasim_get_iova_range,
713 .set_map = vdpasim_set_map,
714 .free = vdpasim_free,
717 static int __init vdpasim_dev_init(void)
719 vdpasim_dev = vdpasim_create();
721 if (!IS_ERR(vdpasim_dev))
724 return PTR_ERR(vdpasim_dev);
727 static void __exit vdpasim_dev_exit(void)
729 struct vdpa_device *vdpa = &vdpasim_dev->vdpa;
731 vdpa_unregister_device(vdpa);
734 module_init(vdpasim_dev_init)
735 module_exit(vdpasim_dev_exit)
737 MODULE_VERSION(DRV_VERSION);
738 MODULE_LICENSE(DRV_LICENSE);
739 MODULE_AUTHOR(DRV_AUTHOR);
740 MODULE_DESCRIPTION(DRV_DESC);