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>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/dma-map-ops.h>
17 #include <linux/etherdevice.h>
18 #include <linux/vringh.h>
19 #include <linux/vdpa.h>
20 #include <linux/virtio_byteorder.h>
21 #include <linux/vhost_iotlb.h>
22 #include <uapi/linux/virtio_config.h>
23 #include <uapi/linux/virtio_net.h>
25 #define DRV_VERSION "0.1"
26 #define DRV_AUTHOR "Jason Wang <jasowang@redhat.com>"
27 #define DRV_DESC "vDPA Device Simulator"
28 #define DRV_LICENSE "GPL v2"
30 static int batch_mapping = 1;
31 module_param(batch_mapping, int, 0444);
32 MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");
34 static int max_iotlb_entries = 2048;
35 module_param(max_iotlb_entries, int, 0444);
36 MODULE_PARM_DESC(max_iotlb_entries,
37 "Maximum number of iotlb entries. 0 means unlimited. (default: 2048)");
40 module_param(macaddr, charp, 0);
41 MODULE_PARM_DESC(macaddr, "Ethernet MAC address");
43 struct vdpasim_virtqueue {
45 struct vringh_kiov iov;
53 irqreturn_t (*cb)(void *data);
56 #define VDPASIM_QUEUE_ALIGN PAGE_SIZE
57 #define VDPASIM_QUEUE_MAX 256
58 #define VDPASIM_DEVICE_ID 0x1
59 #define VDPASIM_VENDOR_ID 0
60 #define VDPASIM_VQ_NUM 0x2
61 #define VDPASIM_NAME "vdpasim-netdev"
63 static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
64 (1ULL << VIRTIO_F_VERSION_1) |
65 (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
66 (1ULL << VIRTIO_NET_F_MAC);
68 /* State of each vdpasim device */
70 struct vdpa_device vdpa;
71 struct vdpasim_virtqueue *vqs;
72 struct work_struct work;
73 /* spinlock to synchronize virtqueue state */
75 struct virtio_net_config config;
76 struct vhost_iotlb *iommu;
82 /* spinlock to synchronize iommu table */
83 spinlock_t iommu_lock;
86 /* TODO: cross-endian support */
87 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
89 return virtio_legacy_is_little_endian() ||
90 (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
93 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
95 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
98 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
100 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
103 static struct vdpasim *vdpasim_dev;
105 static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
107 return container_of(vdpa, struct vdpasim, vdpa);
110 static struct vdpasim *dev_to_sim(struct device *dev)
112 struct vdpa_device *vdpa = dev_to_vdpa(dev);
114 return vdpa_to_sim(vdpa);
117 static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
119 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
121 vringh_init_iotlb(&vq->vring, vdpasim_features,
122 VDPASIM_QUEUE_MAX, false,
123 (struct vring_desc *)(uintptr_t)vq->desc_addr,
124 (struct vring_avail *)
125 (uintptr_t)vq->driver_addr,
126 (struct vring_used *)
127 (uintptr_t)vq->device_addr);
130 static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
138 vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
139 false, NULL, NULL, NULL);
142 static void vdpasim_reset(struct vdpasim *vdpasim)
146 for (i = 0; i < vdpasim->nvqs; i++)
147 vdpasim_vq_reset(&vdpasim->vqs[i]);
149 spin_lock(&vdpasim->iommu_lock);
150 vhost_iotlb_reset(vdpasim->iommu);
151 spin_unlock(&vdpasim->iommu_lock);
153 vdpasim->features = 0;
155 ++vdpasim->generation;
158 static void vdpasim_work(struct work_struct *work)
160 struct vdpasim *vdpasim = container_of(work, struct
162 struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
163 struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
169 spin_lock(&vdpasim->lock);
171 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
174 if (!txq->ready || !rxq->ready)
179 err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
180 &txq->head, GFP_ATOMIC);
184 err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
185 &rxq->head, GFP_ATOMIC);
187 vringh_complete_iotlb(&txq->vring, txq->head, 0);
192 read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
198 write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
199 vdpasim->buffer, read);
203 total_write += write;
206 /* Make sure data is wrote before advancing index */
209 vringh_complete_iotlb(&txq->vring, txq->head, 0);
210 vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);
212 /* Make sure used is visible before rasing the interrupt. */
217 txq->cb(txq->private);
219 rxq->cb(rxq->private);
223 schedule_work(&vdpasim->work);
229 spin_unlock(&vdpasim->lock);
232 static int dir_to_perm(enum dma_data_direction dir)
237 case DMA_FROM_DEVICE:
243 case DMA_BIDIRECTIONAL:
253 static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
254 unsigned long offset, size_t size,
255 enum dma_data_direction dir,
258 struct vdpasim *vdpasim = dev_to_sim(dev);
259 struct vhost_iotlb *iommu = vdpasim->iommu;
260 u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
261 int ret, perm = dir_to_perm(dir);
264 return DMA_MAPPING_ERROR;
266 /* For simplicity, use identical mapping to avoid e.g iova
269 spin_lock(&vdpasim->iommu_lock);
270 ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
271 pa, dir_to_perm(dir));
272 spin_unlock(&vdpasim->iommu_lock);
274 return DMA_MAPPING_ERROR;
276 return (dma_addr_t)(pa);
279 static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
280 size_t size, enum dma_data_direction dir,
283 struct vdpasim *vdpasim = dev_to_sim(dev);
284 struct vhost_iotlb *iommu = vdpasim->iommu;
286 spin_lock(&vdpasim->iommu_lock);
287 vhost_iotlb_del_range(iommu, (u64)dma_addr,
288 (u64)dma_addr + size - 1);
289 spin_unlock(&vdpasim->iommu_lock);
292 static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
293 dma_addr_t *dma_addr, gfp_t flag,
296 struct vdpasim *vdpasim = dev_to_sim(dev);
297 struct vhost_iotlb *iommu = vdpasim->iommu;
298 void *addr = kmalloc(size, flag);
301 spin_lock(&vdpasim->iommu_lock);
303 *dma_addr = DMA_MAPPING_ERROR;
305 u64 pa = virt_to_phys(addr);
307 ret = vhost_iotlb_add_range(iommu, (u64)pa,
311 *dma_addr = DMA_MAPPING_ERROR;
315 *dma_addr = (dma_addr_t)pa;
317 spin_unlock(&vdpasim->iommu_lock);
322 static void vdpasim_free_coherent(struct device *dev, size_t size,
323 void *vaddr, dma_addr_t dma_addr,
326 struct vdpasim *vdpasim = dev_to_sim(dev);
327 struct vhost_iotlb *iommu = vdpasim->iommu;
329 spin_lock(&vdpasim->iommu_lock);
330 vhost_iotlb_del_range(iommu, (u64)dma_addr,
331 (u64)dma_addr + size - 1);
332 spin_unlock(&vdpasim->iommu_lock);
334 kfree(phys_to_virt((uintptr_t)dma_addr));
337 static const struct dma_map_ops vdpasim_dma_ops = {
338 .map_page = vdpasim_map_page,
339 .unmap_page = vdpasim_unmap_page,
340 .alloc = vdpasim_alloc_coherent,
341 .free = vdpasim_free_coherent,
344 static const struct vdpa_config_ops vdpasim_config_ops;
345 static const struct vdpa_config_ops vdpasim_batch_config_ops;
347 static struct vdpasim *vdpasim_create(void)
349 const struct vdpa_config_ops *ops;
350 struct vdpasim *vdpasim;
352 int i, ret = -ENOMEM;
355 ops = &vdpasim_batch_config_ops;
357 ops = &vdpasim_config_ops;
359 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, VDPASIM_VQ_NUM);
363 vdpasim->nvqs = 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->vqs = kcalloc(vdpasim->nvqs, sizeof(struct vdpasim_virtqueue),
379 vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);
383 vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
384 if (!vdpasim->buffer)
388 mac_pton(macaddr, vdpasim->config.mac);
389 if (!is_valid_ether_addr(vdpasim->config.mac)) {
390 ret = -EADDRNOTAVAIL;
394 eth_random_addr(vdpasim->config.mac);
397 for (i = 0; i < vdpasim->nvqs; i++)
398 vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
400 vdpasim->vdpa.dma_dev = dev;
401 ret = vdpa_register_device(&vdpasim->vdpa);
413 static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
414 u64 desc_area, u64 driver_area,
417 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
418 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
420 vq->desc_addr = desc_area;
421 vq->driver_addr = driver_area;
422 vq->device_addr = device_area;
427 static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
429 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
430 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
435 static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
437 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
438 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
441 schedule_work(&vdpasim->work);
444 static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
445 struct vdpa_callback *cb)
447 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
448 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
450 vq->cb = cb->callback;
451 vq->private = cb->private;
454 static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
456 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
457 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
459 spin_lock(&vdpasim->lock);
462 vdpasim_queue_ready(vdpasim, idx);
463 spin_unlock(&vdpasim->lock);
466 static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
468 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
469 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
474 static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
475 const struct vdpa_vq_state *state)
477 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
478 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
479 struct vringh *vrh = &vq->vring;
481 spin_lock(&vdpasim->lock);
482 vrh->last_avail_idx = state->avail_index;
483 spin_unlock(&vdpasim->lock);
488 static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
489 struct vdpa_vq_state *state)
491 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
492 struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
493 struct vringh *vrh = &vq->vring;
495 state->avail_index = vrh->last_avail_idx;
499 static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
501 return VDPASIM_QUEUE_ALIGN;
504 static u64 vdpasim_get_features(struct vdpa_device *vdpa)
506 return vdpasim_features;
509 static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
511 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
512 struct virtio_net_config *config = &vdpasim->config;
514 /* DMA mapping must be done by driver */
515 if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
518 vdpasim->features = features & vdpasim_features;
520 /* We generally only know whether guest is using the legacy interface
521 * here, so generally that's the earliest we can set config fields.
522 * Note: We actually require VIRTIO_F_ACCESS_PLATFORM above which
523 * implies VIRTIO_F_VERSION_1, but let's not try to be clever here.
526 config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
527 config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
531 static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
532 struct vdpa_callback *cb)
534 /* We don't support config interrupt */
537 static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
539 return VDPASIM_QUEUE_MAX;
542 static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
544 return VDPASIM_DEVICE_ID;
547 static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
549 return VDPASIM_VENDOR_ID;
552 static u8 vdpasim_get_status(struct vdpa_device *vdpa)
554 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
557 spin_lock(&vdpasim->lock);
558 status = vdpasim->status;
559 spin_unlock(&vdpasim->lock);
564 static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
566 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
568 spin_lock(&vdpasim->lock);
569 vdpasim->status = status;
571 vdpasim_reset(vdpasim);
572 spin_unlock(&vdpasim->lock);
575 static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
576 void *buf, unsigned int len)
578 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
580 if (offset + len < sizeof(struct virtio_net_config))
581 memcpy(buf, (u8 *)&vdpasim->config + offset, len);
584 static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
585 const void *buf, unsigned int len)
587 /* No writable config supportted by vdpasim */
590 static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
592 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
594 return vdpasim->generation;
597 static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
599 struct vdpa_iova_range range = {
607 static int vdpasim_set_map(struct vdpa_device *vdpa,
608 struct vhost_iotlb *iotlb)
610 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
611 struct vhost_iotlb_map *map;
612 u64 start = 0ULL, last = 0ULL - 1;
615 spin_lock(&vdpasim->iommu_lock);
616 vhost_iotlb_reset(vdpasim->iommu);
618 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
619 map = vhost_iotlb_itree_next(map, start, last)) {
620 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
621 map->last, map->addr, map->perm);
625 spin_unlock(&vdpasim->iommu_lock);
629 vhost_iotlb_reset(vdpasim->iommu);
630 spin_unlock(&vdpasim->iommu_lock);
634 static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
637 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
640 spin_lock(&vdpasim->iommu_lock);
641 ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
643 spin_unlock(&vdpasim->iommu_lock);
648 static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
650 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
652 spin_lock(&vdpasim->iommu_lock);
653 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
654 spin_unlock(&vdpasim->iommu_lock);
659 static void vdpasim_free(struct vdpa_device *vdpa)
661 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
663 cancel_work_sync(&vdpasim->work);
664 kfree(vdpasim->buffer);
666 vhost_iotlb_free(vdpasim->iommu);
670 static const struct vdpa_config_ops vdpasim_config_ops = {
671 .set_vq_address = vdpasim_set_vq_address,
672 .set_vq_num = vdpasim_set_vq_num,
673 .kick_vq = vdpasim_kick_vq,
674 .set_vq_cb = vdpasim_set_vq_cb,
675 .set_vq_ready = vdpasim_set_vq_ready,
676 .get_vq_ready = vdpasim_get_vq_ready,
677 .set_vq_state = vdpasim_set_vq_state,
678 .get_vq_state = vdpasim_get_vq_state,
679 .get_vq_align = vdpasim_get_vq_align,
680 .get_features = vdpasim_get_features,
681 .set_features = vdpasim_set_features,
682 .set_config_cb = vdpasim_set_config_cb,
683 .get_vq_num_max = vdpasim_get_vq_num_max,
684 .get_device_id = vdpasim_get_device_id,
685 .get_vendor_id = vdpasim_get_vendor_id,
686 .get_status = vdpasim_get_status,
687 .set_status = vdpasim_set_status,
688 .get_config = vdpasim_get_config,
689 .set_config = vdpasim_set_config,
690 .get_generation = vdpasim_get_generation,
691 .get_iova_range = vdpasim_get_iova_range,
692 .dma_map = vdpasim_dma_map,
693 .dma_unmap = vdpasim_dma_unmap,
694 .free = vdpasim_free,
697 static const struct vdpa_config_ops vdpasim_batch_config_ops = {
698 .set_vq_address = vdpasim_set_vq_address,
699 .set_vq_num = vdpasim_set_vq_num,
700 .kick_vq = vdpasim_kick_vq,
701 .set_vq_cb = vdpasim_set_vq_cb,
702 .set_vq_ready = vdpasim_set_vq_ready,
703 .get_vq_ready = vdpasim_get_vq_ready,
704 .set_vq_state = vdpasim_set_vq_state,
705 .get_vq_state = vdpasim_get_vq_state,
706 .get_vq_align = vdpasim_get_vq_align,
707 .get_features = vdpasim_get_features,
708 .set_features = vdpasim_set_features,
709 .set_config_cb = vdpasim_set_config_cb,
710 .get_vq_num_max = vdpasim_get_vq_num_max,
711 .get_device_id = vdpasim_get_device_id,
712 .get_vendor_id = vdpasim_get_vendor_id,
713 .get_status = vdpasim_get_status,
714 .set_status = vdpasim_set_status,
715 .get_config = vdpasim_get_config,
716 .set_config = vdpasim_set_config,
717 .get_generation = vdpasim_get_generation,
718 .get_iova_range = vdpasim_get_iova_range,
719 .set_map = vdpasim_set_map,
720 .free = vdpasim_free,
723 static int __init vdpasim_dev_init(void)
725 vdpasim_dev = vdpasim_create();
727 if (!IS_ERR(vdpasim_dev))
730 return PTR_ERR(vdpasim_dev);
733 static void __exit vdpasim_dev_exit(void)
735 struct vdpa_device *vdpa = &vdpasim_dev->vdpa;
737 vdpa_unregister_device(vdpa);
740 module_init(vdpasim_dev_init)
741 module_exit(vdpasim_dev_exit)
743 MODULE_VERSION(DRV_VERSION);
744 MODULE_LICENSE(DRV_LICENSE);
745 MODULE_AUTHOR(DRV_AUTHOR);
746 MODULE_DESCRIPTION(DRV_DESC);