1 // SPDX-License-Identifier: GPL-2.0-only
3 * VDPA networking device simulator.
5 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
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"
33 #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
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 u8 macaddr_buf[ETH_ALEN];
47 struct vdpasim_virtqueue {
49 struct vringh_kiov iov;
57 irqreturn_t (*cb)(void *data);
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"
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);
74 struct vdpasim_dev_attr {
77 void (*get_config)(struct vdpasim *vdpasim, void *config);
80 /* State of each vdpasim device */
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 */
88 /* virtio config according to device type */
90 struct vhost_iotlb *iommu;
95 /* spinlock to synchronize iommu table */
96 spinlock_t iommu_lock;
99 /* TODO: cross-endian support */
100 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
102 return virtio_legacy_is_little_endian() ||
103 (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
106 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
108 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
111 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
113 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
116 static struct vdpasim *vdpasim_dev;
118 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
120 return container_of(vdpa, struct vdpasim, vdpa);
123 static struct vdpasim *dev_to_sim(struct device *dev)
125 struct vdpa_device *vdpa = dev_to_vdpa(dev);
127 return vdpa_to_sim(vdpa);
130 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
132 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
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);
143 static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
151 vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
152 false, NULL, NULL, NULL);
155 static void vdpasim_reset(struct vdpasim *vdpasim)
159 for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
160 vdpasim_vq_reset(&vdpasim->vqs[i]);
162 spin_lock(&vdpasim->iommu_lock);
163 vhost_iotlb_reset(vdpasim->iommu);
164 spin_unlock(&vdpasim->iommu_lock);
166 vdpasim->features = 0;
168 ++vdpasim->generation;
171 static void vdpasim_work(struct work_struct *work)
173 struct vdpasim *vdpasim = container_of(work, struct
175 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
176 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
182 spin_lock(&vdpasim->lock);
184 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
187 if (!txq->ready || !rxq->ready)
192 err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
193 &txq->head, GFP_ATOMIC);
197 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
198 &rxq->head, GFP_ATOMIC);
200 vringh_complete_iotlb(&txq->vring, txq->head, 0);
205 read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
211 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
212 vdpasim->buffer, read);
216 total_write += write;
219 /* Make sure data is wrote before advancing index */
222 vringh_complete_iotlb(&txq->vring, txq->head, 0);
223 vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
225 /* Make sure used is visible before rasing the interrupt. */
230 txq->cb(txq->private);
232 rxq->cb(rxq->private);
236 schedule_work(&vdpasim->work);
242 spin_unlock(&vdpasim->lock);
245 static int dir_to_perm(enum dma_data_direction dir)
250 case DMA_FROM_DEVICE:
256 case DMA_BIDIRECTIONAL:
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,
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);
277 return DMA_MAPPING_ERROR;
279 /* For simplicity, use identical mapping to avoid e.g iova
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);
287 return DMA_MAPPING_ERROR;
289 return (dma_addr_t)(pa);
292 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
293 size_t size, enum dma_data_direction dir,
296 struct vdpasim *vdpasim = dev_to_sim(dev);
297 struct vhost_iotlb *iommu = vdpasim->iommu;
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);
305 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
306 dma_addr_t *dma_addr, gfp_t flag,
309 struct vdpasim *vdpasim = dev_to_sim(dev);
310 struct vhost_iotlb *iommu = vdpasim->iommu;
311 void *addr = kmalloc(size, flag);
314 spin_lock(&vdpasim->iommu_lock);
316 *dma_addr = DMA_MAPPING_ERROR;
318 u64 pa = virt_to_phys(addr);
320 ret = vhost_iotlb_add_range(iommu, (u64)pa,
324 *dma_addr = DMA_MAPPING_ERROR;
328 *dma_addr = (dma_addr_t)pa;
330 spin_unlock(&vdpasim->iommu_lock);
335 static void vdpasim_free_coherent(struct device *dev, size_t size,
336 void *vaddr, dma_addr_t dma_addr,
339 struct vdpasim *vdpasim = dev_to_sim(dev);
340 struct vhost_iotlb *iommu = vdpasim->iommu;
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);
347 kfree(phys_to_virt((uintptr_t)dma_addr));
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,
357 static const struct vdpa_config_ops vdpasim_net_config_ops;
358 static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
360 static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
362 const struct vdpa_config_ops *ops;
363 struct vdpasim *vdpasim;
365 int i, ret = -ENOMEM;
368 ops = &vdpasim_net_batch_config_ops;
370 ops = &vdpasim_net_config_ops;
372 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
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);
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)))
386 set_dma_ops(dev, &vdpasim_dma_ops);
388 vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
389 if (!vdpasim->config)
392 vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
397 vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
401 vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
402 if (!vdpasim->buffer)
406 mac_pton(macaddr, macaddr_buf);
407 if (!is_valid_ether_addr(macaddr_buf)) {
408 ret = -EADDRNOTAVAIL;
412 eth_random_addr(macaddr_buf);
415 for (i = 0; i < dev_attr->nvqs; i++)
416 vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
418 vdpasim->vdpa.dma_dev = dev;
419 ret = vdpa_register_device(&vdpasim->vdpa);
431 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
432 u64 desc_area, u64 driver_area,
435 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
436 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
438 vq->desc_addr = desc_area;
439 vq->driver_addr = driver_area;
440 vq->device_addr = device_area;
445 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
447 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
448 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
453 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
455 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
456 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
459 schedule_work(&vdpasim->work);
462 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
463 struct vdpa_callback *cb)
465 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
466 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
468 vq->cb = cb->callback;
469 vq->private = cb->private;
472 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
474 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
475 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
478 spin_lock(&vdpasim->lock);
479 old_ready = vq->ready;
481 if (vq->ready && !old_ready) {
482 vdpasim_queue_ready(vdpasim, idx);
484 spin_unlock(&vdpasim->lock);
487 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
489 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
490 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
495 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
496 const struct vdpa_vq_state *state)
498 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
499 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
500 struct vringh *vrh = &vq->vring;
502 spin_lock(&vdpasim->lock);
503 vrh->last_avail_idx = state->avail_index;
504 spin_unlock(&vdpasim->lock);
509 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
510 struct vdpa_vq_state *state)
512 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
513 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
514 struct vringh *vrh = &vq->vring;
516 state->avail_index = vrh->last_avail_idx;
520 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
522 return VDPASIM_QUEUE_ALIGN;
525 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
527 return vdpasim_features;
530 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
532 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
534 /* DMA mapping must be done by driver */
535 if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
538 vdpasim->features = features & vdpasim_features;
543 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
544 struct vdpa_callback *cb)
546 /* We don't support config interrupt */
549 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
551 return VDPASIM_QUEUE_MAX;
554 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
556 return VDPASIM_DEVICE_ID;
559 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
561 return VDPASIM_VENDOR_ID;
564 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
566 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
569 spin_lock(&vdpasim->lock);
570 status = vdpasim->status;
571 spin_unlock(&vdpasim->lock);
576 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
578 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
580 spin_lock(&vdpasim->lock);
581 vdpasim->status = status;
583 vdpasim_reset(vdpasim);
584 spin_unlock(&vdpasim->lock);
587 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
588 void *buf, unsigned int len)
590 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
592 if (offset + len > vdpasim->dev_attr.config_size)
595 if (vdpasim->dev_attr.get_config)
596 vdpasim->dev_attr.get_config(vdpasim, vdpasim->config);
598 memcpy(buf, vdpasim->config + offset, len);
601 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
602 const void *buf, unsigned int len)
604 /* No writable config supportted by vdpasim */
607 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
609 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
611 return vdpasim->generation;
614 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
616 struct vdpa_iova_range range = {
624 static int vdpasim_set_map(struct vdpa_device *vdpa,
625 struct vhost_iotlb *iotlb)
627 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
628 struct vhost_iotlb_map *map;
629 u64 start = 0ULL, last = 0ULL - 1;
632 spin_lock(&vdpasim->iommu_lock);
633 vhost_iotlb_reset(vdpasim->iommu);
635 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
636 map = vhost_iotlb_itree_next(map, start, last)) {
637 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
638 map->last, map->addr, map->perm);
642 spin_unlock(&vdpasim->iommu_lock);
646 vhost_iotlb_reset(vdpasim->iommu);
647 spin_unlock(&vdpasim->iommu_lock);
651 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
654 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
657 spin_lock(&vdpasim->iommu_lock);
658 ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
660 spin_unlock(&vdpasim->iommu_lock);
665 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
667 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
669 spin_lock(&vdpasim->iommu_lock);
670 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
671 spin_unlock(&vdpasim->iommu_lock);
676 static void vdpasim_free(struct vdpa_device *vdpa)
678 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
680 cancel_work_sync(&vdpasim->work);
681 kfree(vdpasim->buffer);
683 vhost_iotlb_free(vdpasim->iommu);
685 kfree(vdpasim->config);
688 static const struct vdpa_config_ops vdpasim_net_config_ops = {
689 .set_vq_address = vdpasim_set_vq_address,
690 .set_vq_num = vdpasim_set_vq_num,
691 .kick_vq = vdpasim_kick_vq,
692 .set_vq_cb = vdpasim_set_vq_cb,
693 .set_vq_ready = vdpasim_set_vq_ready,
694 .get_vq_ready = vdpasim_get_vq_ready,
695 .set_vq_state = vdpasim_set_vq_state,
696 .get_vq_state = vdpasim_get_vq_state,
697 .get_vq_align = vdpasim_get_vq_align,
698 .get_features = vdpasim_get_features,
699 .set_features = vdpasim_set_features,
700 .set_config_cb = vdpasim_set_config_cb,
701 .get_vq_num_max = vdpasim_get_vq_num_max,
702 .get_device_id = vdpasim_get_device_id,
703 .get_vendor_id = vdpasim_get_vendor_id,
704 .get_status = vdpasim_get_status,
705 .set_status = vdpasim_set_status,
706 .get_config = vdpasim_get_config,
707 .set_config = vdpasim_set_config,
708 .get_generation = vdpasim_get_generation,
709 .get_iova_range = vdpasim_get_iova_range,
710 .dma_map = vdpasim_dma_map,
711 .dma_unmap = vdpasim_dma_unmap,
712 .free = vdpasim_free,
715 static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
716 .set_vq_address = vdpasim_set_vq_address,
717 .set_vq_num = vdpasim_set_vq_num,
718 .kick_vq = vdpasim_kick_vq,
719 .set_vq_cb = vdpasim_set_vq_cb,
720 .set_vq_ready = vdpasim_set_vq_ready,
721 .get_vq_ready = vdpasim_get_vq_ready,
722 .set_vq_state = vdpasim_set_vq_state,
723 .get_vq_state = vdpasim_get_vq_state,
724 .get_vq_align = vdpasim_get_vq_align,
725 .get_features = vdpasim_get_features,
726 .set_features = vdpasim_set_features,
727 .set_config_cb = vdpasim_set_config_cb,
728 .get_vq_num_max = vdpasim_get_vq_num_max,
729 .get_device_id = vdpasim_get_device_id,
730 .get_vendor_id = vdpasim_get_vendor_id,
731 .get_status = vdpasim_get_status,
732 .set_status = vdpasim_set_status,
733 .get_config = vdpasim_get_config,
734 .set_config = vdpasim_set_config,
735 .get_generation = vdpasim_get_generation,
736 .get_iova_range = vdpasim_get_iova_range,
737 .set_map = vdpasim_set_map,
738 .free = vdpasim_free,
741 static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
743 struct virtio_net_config *net_config =
744 (struct virtio_net_config *)config;
746 net_config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
747 net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
748 memcpy(net_config->mac, macaddr_buf, ETH_ALEN);
751 static int __init vdpasim_dev_init(void)
753 struct vdpasim_dev_attr dev_attr = {};
755 dev_attr.nvqs = VDPASIM_VQ_NUM;
756 dev_attr.config_size = sizeof(struct virtio_net_config);
757 dev_attr.get_config = vdpasim_net_get_config;
759 vdpasim_dev = vdpasim_create(&dev_attr);
761 if (!IS_ERR(vdpasim_dev))
764 return PTR_ERR(vdpasim_dev);
767 static void __exit vdpasim_dev_exit(void)
769 struct vdpa_device *vdpa = &vdpasim_dev->vdpa;
771 vdpa_unregister_device(vdpa);
774 module_init(vdpasim_dev_init)
775 module_exit(vdpasim_dev_exit)
777 MODULE_VERSION(DRV_VERSION);
778 MODULE_LICENSE(DRV_LICENSE);
779 MODULE_AUTHOR(DRV_AUTHOR);
780 MODULE_DESCRIPTION(DRV_DESC);