1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018-2020 Intel Corporation.
4 * Copyright (C) 2020 Red Hat, Inc.
6 * Author: Tiwei Bie <tiwei.bie@intel.com>
7 * Jason Wang <jasowang@redhat.com>
9 * Thanks Michael S. Tsirkin for the valuable comments and
10 * suggestions. And thanks to Cunming Liang and Zhihong Wang for all
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/cdev.h>
17 #include <linux/device.h>
19 #include <linux/iommu.h>
20 #include <linux/uuid.h>
21 #include <linux/vdpa.h>
22 #include <linux/nospec.h>
23 #include <linux/vhost.h>
24 #include <linux/virtio_net.h>
25 #include <linux/kernel.h>
30 VHOST_VDPA_BACKEND_FEATURES =
31 (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
32 (1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
35 #define VHOST_VDPA_DEV_MAX (1U << MINORBITS)
38 struct vhost_dev vdev;
39 struct iommu_domain *domain;
40 struct vhost_virtqueue *vqs;
41 struct completion completion;
42 struct vdpa_device *vdpa;
49 struct eventfd_ctx *config_ctx;
53 static DEFINE_IDA(vhost_vdpa_ida);
55 static dev_t vhost_vdpa_major;
57 static void handle_vq_kick(struct vhost_work *work)
59 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
61 struct vhost_vdpa *v = container_of(vq->dev, struct vhost_vdpa, vdev);
62 const struct vdpa_config_ops *ops = v->vdpa->config;
64 ops->kick_vq(v->vdpa, vq - v->vqs);
67 static irqreturn_t vhost_vdpa_virtqueue_cb(void *private)
69 struct vhost_virtqueue *vq = private;
70 struct eventfd_ctx *call_ctx = vq->call_ctx.ctx;
73 eventfd_signal(call_ctx, 1);
78 static irqreturn_t vhost_vdpa_config_cb(void *private)
80 struct vhost_vdpa *v = private;
81 struct eventfd_ctx *config_ctx = v->config_ctx;
84 eventfd_signal(config_ctx, 1);
89 static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa *v, u16 qid)
91 struct vhost_virtqueue *vq = &v->vqs[qid];
92 const struct vdpa_config_ops *ops = v->vdpa->config;
93 struct vdpa_device *vdpa = v->vdpa;
99 irq = ops->get_vq_irq(vdpa, qid);
100 spin_lock(&vq->call_ctx.ctx_lock);
101 irq_bypass_unregister_producer(&vq->call_ctx.producer);
102 if (!vq->call_ctx.ctx || irq < 0) {
103 spin_unlock(&vq->call_ctx.ctx_lock);
107 vq->call_ctx.producer.token = vq->call_ctx.ctx;
108 vq->call_ctx.producer.irq = irq;
109 ret = irq_bypass_register_producer(&vq->call_ctx.producer);
110 spin_unlock(&vq->call_ctx.ctx_lock);
113 static void vhost_vdpa_unsetup_vq_irq(struct vhost_vdpa *v, u16 qid)
115 struct vhost_virtqueue *vq = &v->vqs[qid];
117 spin_lock(&vq->call_ctx.ctx_lock);
118 irq_bypass_unregister_producer(&vq->call_ctx.producer);
119 spin_unlock(&vq->call_ctx.ctx_lock);
122 static void vhost_vdpa_reset(struct vhost_vdpa *v)
124 struct vdpa_device *vdpa = v->vdpa;
130 static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp)
132 struct vdpa_device *vdpa = v->vdpa;
133 const struct vdpa_config_ops *ops = vdpa->config;
136 device_id = ops->get_device_id(vdpa);
138 if (copy_to_user(argp, &device_id, sizeof(device_id)))
144 static long vhost_vdpa_get_status(struct vhost_vdpa *v, u8 __user *statusp)
146 struct vdpa_device *vdpa = v->vdpa;
147 const struct vdpa_config_ops *ops = vdpa->config;
150 status = ops->get_status(vdpa);
152 if (copy_to_user(statusp, &status, sizeof(status)))
158 static long vhost_vdpa_set_status(struct vhost_vdpa *v, u8 __user *statusp)
160 struct vdpa_device *vdpa = v->vdpa;
161 const struct vdpa_config_ops *ops = vdpa->config;
162 u8 status, status_old;
166 if (copy_from_user(&status, statusp, sizeof(status)))
169 status_old = ops->get_status(vdpa);
172 * Userspace shouldn't remove status bits unless reset the
175 if (status != 0 && (ops->get_status(vdpa) & ~status) != 0)
178 ops->set_status(vdpa, status);
180 if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && !(status_old & VIRTIO_CONFIG_S_DRIVER_OK))
181 for (i = 0; i < nvqs; i++)
182 vhost_vdpa_setup_vq_irq(v, i);
184 if ((status_old & VIRTIO_CONFIG_S_DRIVER_OK) && !(status & VIRTIO_CONFIG_S_DRIVER_OK))
185 for (i = 0; i < nvqs; i++)
186 vhost_vdpa_unsetup_vq_irq(v, i);
191 static int vhost_vdpa_config_validate(struct vhost_vdpa *v,
192 struct vhost_vdpa_config *c)
196 switch (v->virtio_id) {
198 size = sizeof(struct virtio_net_config);
205 if (c->len > size - c->off)
211 static long vhost_vdpa_get_config(struct vhost_vdpa *v,
212 struct vhost_vdpa_config __user *c)
214 struct vdpa_device *vdpa = v->vdpa;
215 struct vhost_vdpa_config config;
216 unsigned long size = offsetof(struct vhost_vdpa_config, buf);
219 if (copy_from_user(&config, c, size))
221 if (vhost_vdpa_config_validate(v, &config))
223 buf = kvzalloc(config.len, GFP_KERNEL);
227 vdpa_get_config(vdpa, config.off, buf, config.len);
229 if (copy_to_user(c->buf, buf, config.len)) {
238 static long vhost_vdpa_set_config(struct vhost_vdpa *v,
239 struct vhost_vdpa_config __user *c)
241 struct vdpa_device *vdpa = v->vdpa;
242 const struct vdpa_config_ops *ops = vdpa->config;
243 struct vhost_vdpa_config config;
244 unsigned long size = offsetof(struct vhost_vdpa_config, buf);
247 if (copy_from_user(&config, c, size))
249 if (vhost_vdpa_config_validate(v, &config))
251 buf = kvzalloc(config.len, GFP_KERNEL);
255 if (copy_from_user(buf, c->buf, config.len)) {
260 ops->set_config(vdpa, config.off, buf, config.len);
266 static long vhost_vdpa_get_features(struct vhost_vdpa *v, u64 __user *featurep)
268 struct vdpa_device *vdpa = v->vdpa;
269 const struct vdpa_config_ops *ops = vdpa->config;
272 features = ops->get_features(vdpa);
274 if (copy_to_user(featurep, &features, sizeof(features)))
280 static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
282 struct vdpa_device *vdpa = v->vdpa;
283 const struct vdpa_config_ops *ops = vdpa->config;
287 * It's not allowed to change the features after they have
290 if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_FEATURES_OK)
293 if (copy_from_user(&features, featurep, sizeof(features)))
296 if (vdpa_set_features(vdpa, features))
302 static long vhost_vdpa_get_vring_num(struct vhost_vdpa *v, u16 __user *argp)
304 struct vdpa_device *vdpa = v->vdpa;
305 const struct vdpa_config_ops *ops = vdpa->config;
308 num = ops->get_vq_num_max(vdpa);
310 if (copy_to_user(argp, &num, sizeof(num)))
316 static void vhost_vdpa_config_put(struct vhost_vdpa *v)
319 eventfd_ctx_put(v->config_ctx);
322 static long vhost_vdpa_set_config_call(struct vhost_vdpa *v, u32 __user *argp)
324 struct vdpa_callback cb;
326 struct eventfd_ctx *ctx;
328 cb.callback = vhost_vdpa_config_cb;
329 cb.private = v->vdpa;
330 if (copy_from_user(&fd, argp, sizeof(fd)))
333 ctx = fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(fd);
334 swap(ctx, v->config_ctx);
336 if (!IS_ERR_OR_NULL(ctx))
337 eventfd_ctx_put(ctx);
339 if (IS_ERR(v->config_ctx))
340 return PTR_ERR(v->config_ctx);
342 v->vdpa->config->set_config_cb(v->vdpa, &cb);
347 static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
350 struct vdpa_device *vdpa = v->vdpa;
351 const struct vdpa_config_ops *ops = vdpa->config;
352 struct vdpa_vq_state vq_state;
353 struct vdpa_callback cb;
354 struct vhost_virtqueue *vq;
355 struct vhost_vring_state s;
359 r = get_user(idx, (u32 __user *)argp);
366 idx = array_index_nospec(idx, v->nvqs);
370 case VHOST_VDPA_SET_VRING_ENABLE:
371 if (copy_from_user(&s, argp, sizeof(s)))
373 ops->set_vq_ready(vdpa, idx, s.num);
375 case VHOST_GET_VRING_BASE:
376 r = ops->get_vq_state(v->vdpa, idx, &vq_state);
380 vq->last_avail_idx = vq_state.avail_index;
384 r = vhost_vring_ioctl(&v->vdev, cmd, argp);
389 case VHOST_SET_VRING_ADDR:
390 if (ops->set_vq_address(vdpa, idx,
391 (u64)(uintptr_t)vq->desc,
392 (u64)(uintptr_t)vq->avail,
393 (u64)(uintptr_t)vq->used))
397 case VHOST_SET_VRING_BASE:
398 vq_state.avail_index = vq->last_avail_idx;
399 if (ops->set_vq_state(vdpa, idx, &vq_state))
403 case VHOST_SET_VRING_CALL:
404 if (vq->call_ctx.ctx) {
405 cb.callback = vhost_vdpa_virtqueue_cb;
411 ops->set_vq_cb(vdpa, idx, &cb);
412 vhost_vdpa_setup_vq_irq(v, idx);
415 case VHOST_SET_VRING_NUM:
416 ops->set_vq_num(vdpa, idx, vq->num);
423 static long vhost_vdpa_unlocked_ioctl(struct file *filep,
424 unsigned int cmd, unsigned long arg)
426 struct vhost_vdpa *v = filep->private_data;
427 struct vhost_dev *d = &v->vdev;
428 void __user *argp = (void __user *)arg;
429 u64 __user *featurep = argp;
433 if (cmd == VHOST_SET_BACKEND_FEATURES) {
434 r = copy_from_user(&features, featurep, sizeof(features));
437 if (features & ~VHOST_VDPA_BACKEND_FEATURES)
439 vhost_set_backend_features(&v->vdev, features);
443 mutex_lock(&d->mutex);
446 case VHOST_VDPA_GET_DEVICE_ID:
447 r = vhost_vdpa_get_device_id(v, argp);
449 case VHOST_VDPA_GET_STATUS:
450 r = vhost_vdpa_get_status(v, argp);
452 case VHOST_VDPA_SET_STATUS:
453 r = vhost_vdpa_set_status(v, argp);
455 case VHOST_VDPA_GET_CONFIG:
456 r = vhost_vdpa_get_config(v, argp);
458 case VHOST_VDPA_SET_CONFIG:
459 r = vhost_vdpa_set_config(v, argp);
461 case VHOST_GET_FEATURES:
462 r = vhost_vdpa_get_features(v, argp);
464 case VHOST_SET_FEATURES:
465 r = vhost_vdpa_set_features(v, argp);
467 case VHOST_VDPA_GET_VRING_NUM:
468 r = vhost_vdpa_get_vring_num(v, argp);
470 case VHOST_SET_LOG_BASE:
471 case VHOST_SET_LOG_FD:
474 case VHOST_VDPA_SET_CONFIG_CALL:
475 r = vhost_vdpa_set_config_call(v, argp);
477 case VHOST_GET_BACKEND_FEATURES:
478 features = VHOST_VDPA_BACKEND_FEATURES;
479 r = copy_to_user(featurep, &features, sizeof(features));
482 r = vhost_dev_ioctl(&v->vdev, cmd, argp);
483 if (r == -ENOIOCTLCMD)
484 r = vhost_vdpa_vring_ioctl(v, cmd, argp);
488 mutex_unlock(&d->mutex);
492 static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last)
494 struct vhost_dev *dev = &v->vdev;
495 struct vhost_iotlb *iotlb = dev->iotlb;
496 struct vhost_iotlb_map *map;
498 unsigned long pfn, pinned;
500 while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
501 pinned = map->size >> PAGE_SHIFT;
502 for (pfn = map->addr >> PAGE_SHIFT;
503 pinned > 0; pfn++, pinned--) {
504 page = pfn_to_page(pfn);
505 if (map->perm & VHOST_ACCESS_WO)
506 set_page_dirty_lock(page);
507 unpin_user_page(page);
509 atomic64_sub(map->size >> PAGE_SHIFT, &dev->mm->pinned_vm);
510 vhost_iotlb_map_free(iotlb, map);
514 static void vhost_vdpa_iotlb_free(struct vhost_vdpa *v)
516 struct vhost_dev *dev = &v->vdev;
518 vhost_vdpa_iotlb_unmap(v, 0ULL, 0ULL - 1);
523 static int perm_to_iommu_flags(u32 perm)
528 case VHOST_ACCESS_WO:
529 flags |= IOMMU_WRITE;
531 case VHOST_ACCESS_RO:
534 case VHOST_ACCESS_RW:
535 flags |= (IOMMU_WRITE | IOMMU_READ);
538 WARN(1, "invalidate vhost IOTLB permission\n");
542 return flags | IOMMU_CACHE;
545 static int vhost_vdpa_map(struct vhost_vdpa *v,
546 u64 iova, u64 size, u64 pa, u32 perm)
548 struct vhost_dev *dev = &v->vdev;
549 struct vdpa_device *vdpa = v->vdpa;
550 const struct vdpa_config_ops *ops = vdpa->config;
553 r = vhost_iotlb_add_range(dev->iotlb, iova, iova + size - 1,
559 r = ops->dma_map(vdpa, iova, size, pa, perm);
560 } else if (ops->set_map) {
562 r = ops->set_map(vdpa, dev->iotlb);
564 r = iommu_map(v->domain, iova, pa, size,
565 perm_to_iommu_flags(perm));
571 static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size)
573 struct vhost_dev *dev = &v->vdev;
574 struct vdpa_device *vdpa = v->vdpa;
575 const struct vdpa_config_ops *ops = vdpa->config;
577 vhost_vdpa_iotlb_unmap(v, iova, iova + size - 1);
580 ops->dma_unmap(vdpa, iova, size);
581 } else if (ops->set_map) {
583 ops->set_map(vdpa, dev->iotlb);
585 iommu_unmap(v->domain, iova, size);
589 static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
590 struct vhost_iotlb_msg *msg)
592 struct vhost_dev *dev = &v->vdev;
593 struct vhost_iotlb *iotlb = dev->iotlb;
594 struct page **page_list;
595 unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
596 unsigned int gup_flags = FOLL_LONGTERM;
597 unsigned long npages, cur_base, map_pfn, last_pfn = 0;
598 unsigned long locked, lock_limit, pinned, i;
599 u64 iova = msg->iova;
602 if (vhost_iotlb_itree_first(iotlb, msg->iova,
603 msg->iova + msg->size - 1))
606 page_list = (struct page **) __get_free_page(GFP_KERNEL);
610 if (msg->perm & VHOST_ACCESS_WO)
611 gup_flags |= FOLL_WRITE;
613 npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >> PAGE_SHIFT;
617 mmap_read_lock(dev->mm);
619 locked = atomic64_add_return(npages, &dev->mm->pinned_vm);
620 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
622 if (locked > lock_limit) {
627 cur_base = msg->uaddr & PAGE_MASK;
631 pinned = min_t(unsigned long, npages, list_size);
632 ret = pin_user_pages(cur_base, pinned,
633 gup_flags, page_list, NULL);
638 map_pfn = page_to_pfn(page_list[0]);
640 for (i = 0; i < ret; i++) {
641 unsigned long this_pfn = page_to_pfn(page_list[i]);
644 if (last_pfn && (this_pfn != last_pfn + 1)) {
645 /* Pin a contiguous chunk of memory */
646 csize = (last_pfn - map_pfn + 1) << PAGE_SHIFT;
647 if (vhost_vdpa_map(v, iova, csize,
648 map_pfn << PAGE_SHIFT,
658 cur_base += ret << PAGE_SHIFT;
662 /* Pin the rest chunk */
663 ret = vhost_vdpa_map(v, iova, (last_pfn - map_pfn + 1) << PAGE_SHIFT,
664 map_pfn << PAGE_SHIFT, msg->perm);
667 vhost_vdpa_unmap(v, msg->iova, msg->size);
668 atomic64_sub(npages, &dev->mm->pinned_vm);
670 mmap_read_unlock(dev->mm);
671 free_page((unsigned long)page_list);
675 static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
676 struct vhost_iotlb_msg *msg)
678 struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
679 struct vdpa_device *vdpa = v->vdpa;
680 const struct vdpa_config_ops *ops = vdpa->config;
683 r = vhost_dev_check_owner(dev);
688 case VHOST_IOTLB_UPDATE:
689 r = vhost_vdpa_process_iotlb_update(v, msg);
691 case VHOST_IOTLB_INVALIDATE:
692 vhost_vdpa_unmap(v, msg->iova, msg->size);
694 case VHOST_IOTLB_BATCH_BEGIN:
697 case VHOST_IOTLB_BATCH_END:
698 if (v->in_batch && ops->set_map)
699 ops->set_map(vdpa, dev->iotlb);
710 static ssize_t vhost_vdpa_chr_write_iter(struct kiocb *iocb,
711 struct iov_iter *from)
713 struct file *file = iocb->ki_filp;
714 struct vhost_vdpa *v = file->private_data;
715 struct vhost_dev *dev = &v->vdev;
717 return vhost_chr_write_iter(dev, from);
720 static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)
722 struct vdpa_device *vdpa = v->vdpa;
723 const struct vdpa_config_ops *ops = vdpa->config;
724 struct device *dma_dev = vdpa_get_dma_dev(vdpa);
725 struct bus_type *bus;
728 /* Device want to do DMA by itself */
729 if (ops->set_map || ops->dma_map)
736 if (!iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
739 v->domain = iommu_domain_alloc(bus);
743 ret = iommu_attach_device(v->domain, dma_dev);
750 iommu_domain_free(v->domain);
754 static void vhost_vdpa_free_domain(struct vhost_vdpa *v)
756 struct vdpa_device *vdpa = v->vdpa;
757 struct device *dma_dev = vdpa_get_dma_dev(vdpa);
760 iommu_detach_device(v->domain, dma_dev);
761 iommu_domain_free(v->domain);
767 static int vhost_vdpa_open(struct inode *inode, struct file *filep)
769 struct vhost_vdpa *v;
770 struct vhost_dev *dev;
771 struct vhost_virtqueue **vqs;
772 int nvqs, i, r, opened;
774 v = container_of(inode->i_cdev, struct vhost_vdpa, cdev);
776 opened = atomic_cmpxchg(&v->opened, 0, 1);
783 vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL);
790 for (i = 0; i < nvqs; i++) {
792 vqs[i]->handle_kick = handle_vq_kick;
794 vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
795 vhost_vdpa_process_iotlb_msg);
797 dev->iotlb = vhost_iotlb_alloc(0, 0);
803 r = vhost_vdpa_alloc_domain(v);
807 filep->private_data = v;
812 vhost_dev_cleanup(&v->vdev);
814 atomic_dec(&v->opened);
818 static void vhost_vdpa_clean_irq(struct vhost_vdpa *v)
820 struct vhost_virtqueue *vq;
823 for (i = 0; i < v->nvqs; i++) {
825 if (vq->call_ctx.producer.irq)
826 irq_bypass_unregister_producer(&vq->call_ctx.producer);
830 static int vhost_vdpa_release(struct inode *inode, struct file *filep)
832 struct vhost_vdpa *v = filep->private_data;
833 struct vhost_dev *d = &v->vdev;
835 mutex_lock(&d->mutex);
836 filep->private_data = NULL;
838 vhost_dev_stop(&v->vdev);
839 vhost_vdpa_iotlb_free(v);
840 vhost_vdpa_free_domain(v);
841 vhost_vdpa_config_put(v);
842 vhost_vdpa_clean_irq(v);
843 vhost_dev_cleanup(&v->vdev);
845 mutex_unlock(&d->mutex);
847 atomic_dec(&v->opened);
848 complete(&v->completion);
854 static vm_fault_t vhost_vdpa_fault(struct vm_fault *vmf)
856 struct vhost_vdpa *v = vmf->vma->vm_file->private_data;
857 struct vdpa_device *vdpa = v->vdpa;
858 const struct vdpa_config_ops *ops = vdpa->config;
859 struct vdpa_notification_area notify;
860 struct vm_area_struct *vma = vmf->vma;
861 u16 index = vma->vm_pgoff;
863 notify = ops->get_vq_notification(vdpa, index);
865 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
866 if (remap_pfn_range(vma, vmf->address & PAGE_MASK,
867 notify.addr >> PAGE_SHIFT, PAGE_SIZE,
869 return VM_FAULT_SIGBUS;
871 return VM_FAULT_NOPAGE;
874 static const struct vm_operations_struct vhost_vdpa_vm_ops = {
875 .fault = vhost_vdpa_fault,
878 static int vhost_vdpa_mmap(struct file *file, struct vm_area_struct *vma)
880 struct vhost_vdpa *v = vma->vm_file->private_data;
881 struct vdpa_device *vdpa = v->vdpa;
882 const struct vdpa_config_ops *ops = vdpa->config;
883 struct vdpa_notification_area notify;
884 unsigned long index = vma->vm_pgoff;
886 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
888 if ((vma->vm_flags & VM_SHARED) == 0)
890 if (vma->vm_flags & VM_READ)
894 if (!ops->get_vq_notification)
897 /* To be safe and easily modelled by userspace, We only
898 * support the doorbell which sits on the page boundary and
899 * does not share the page with other registers.
901 notify = ops->get_vq_notification(vdpa, index);
902 if (notify.addr & (PAGE_SIZE - 1))
904 if (vma->vm_end - vma->vm_start != notify.size)
907 vma->vm_ops = &vhost_vdpa_vm_ops;
910 #endif /* CONFIG_MMU */
912 static const struct file_operations vhost_vdpa_fops = {
913 .owner = THIS_MODULE,
914 .open = vhost_vdpa_open,
915 .release = vhost_vdpa_release,
916 .write_iter = vhost_vdpa_chr_write_iter,
917 .unlocked_ioctl = vhost_vdpa_unlocked_ioctl,
919 .mmap = vhost_vdpa_mmap,
920 #endif /* CONFIG_MMU */
921 .compat_ioctl = compat_ptr_ioctl,
924 static void vhost_vdpa_release_dev(struct device *device)
926 struct vhost_vdpa *v =
927 container_of(device, struct vhost_vdpa, dev);
929 ida_simple_remove(&vhost_vdpa_ida, v->minor);
934 static int vhost_vdpa_probe(struct vdpa_device *vdpa)
936 const struct vdpa_config_ops *ops = vdpa->config;
937 struct vhost_vdpa *v;
941 /* Currently, we only accept the network devices. */
942 if (ops->get_device_id(vdpa) != VIRTIO_ID_NET)
945 v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
949 minor = ida_simple_get(&vhost_vdpa_ida, 0,
950 VHOST_VDPA_DEV_MAX, GFP_KERNEL);
956 atomic_set(&v->opened, 0);
959 v->nvqs = vdpa->nvqs;
960 v->virtio_id = ops->get_device_id(vdpa);
962 device_initialize(&v->dev);
963 v->dev.release = vhost_vdpa_release_dev;
964 v->dev.parent = &vdpa->dev;
965 v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
966 v->vqs = kmalloc_array(v->nvqs, sizeof(struct vhost_virtqueue),
973 r = dev_set_name(&v->dev, "vhost-vdpa-%u", minor);
977 cdev_init(&v->cdev, &vhost_vdpa_fops);
978 v->cdev.owner = THIS_MODULE;
980 r = cdev_device_add(&v->cdev, &v->dev);
984 init_completion(&v->completion);
985 vdpa_set_drvdata(vdpa, v);
994 static void vhost_vdpa_remove(struct vdpa_device *vdpa)
996 struct vhost_vdpa *v = vdpa_get_drvdata(vdpa);
999 cdev_device_del(&v->cdev, &v->dev);
1002 opened = atomic_cmpxchg(&v->opened, 0, 1);
1005 wait_for_completion(&v->completion);
1008 put_device(&v->dev);
1011 static struct vdpa_driver vhost_vdpa_driver = {
1013 .name = "vhost_vdpa",
1015 .probe = vhost_vdpa_probe,
1016 .remove = vhost_vdpa_remove,
1019 static int __init vhost_vdpa_init(void)
1023 r = alloc_chrdev_region(&vhost_vdpa_major, 0, VHOST_VDPA_DEV_MAX,
1026 goto err_alloc_chrdev;
1028 r = vdpa_register_driver(&vhost_vdpa_driver);
1030 goto err_vdpa_register_driver;
1034 err_vdpa_register_driver:
1035 unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
1039 module_init(vhost_vdpa_init);
1041 static void __exit vhost_vdpa_exit(void)
1043 vdpa_unregister_driver(&vhost_vdpa_driver);
1044 unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
1046 module_exit(vhost_vdpa_exit);
1048 MODULE_VERSION("0.0.1");
1049 MODULE_LICENSE("GPL v2");
1050 MODULE_AUTHOR("Intel Corporation");
1051 MODULE_DESCRIPTION("vDPA-based vhost backend for virtio");