1 // SPDX-License-Identifier: GPL-2.0-only
3 * VFIO based driver for Mediated device
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.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/vfio.h>
16 #include <linux/mdev.h>
18 #include "mdev_private.h"
20 #define DRIVER_VERSION "0.1"
21 #define DRIVER_AUTHOR "NVIDIA Corporation"
22 #define DRIVER_DESC "VFIO based driver for Mediated device"
24 static int vfio_mdev_open(struct vfio_device *core_vdev)
26 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
27 struct mdev_parent *parent = mdev->parent;
31 if (unlikely(!parent->ops->open))
34 if (!try_module_get(THIS_MODULE))
37 ret = parent->ops->open(mdev);
39 module_put(THIS_MODULE);
44 static void vfio_mdev_release(struct vfio_device *core_vdev)
46 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
47 struct mdev_parent *parent = mdev->parent;
49 if (likely(parent->ops->release))
50 parent->ops->release(mdev);
52 module_put(THIS_MODULE);
55 static long vfio_mdev_unlocked_ioctl(struct vfio_device *core_vdev,
56 unsigned int cmd, unsigned long arg)
58 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
59 struct mdev_parent *parent = mdev->parent;
61 if (unlikely(!parent->ops->ioctl))
64 return parent->ops->ioctl(mdev, cmd, arg);
67 static ssize_t vfio_mdev_read(struct vfio_device *core_vdev, char __user *buf,
68 size_t count, loff_t *ppos)
70 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
71 struct mdev_parent *parent = mdev->parent;
73 if (unlikely(!parent->ops->read))
76 return parent->ops->read(mdev, buf, count, ppos);
79 static ssize_t vfio_mdev_write(struct vfio_device *core_vdev,
80 const char __user *buf, size_t count,
83 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
84 struct mdev_parent *parent = mdev->parent;
86 if (unlikely(!parent->ops->write))
89 return parent->ops->write(mdev, buf, count, ppos);
92 static int vfio_mdev_mmap(struct vfio_device *core_vdev,
93 struct vm_area_struct *vma)
95 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
96 struct mdev_parent *parent = mdev->parent;
98 if (unlikely(!parent->ops->mmap))
101 return parent->ops->mmap(mdev, vma);
104 static void vfio_mdev_request(struct vfio_device *core_vdev, unsigned int count)
106 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
107 struct mdev_parent *parent = mdev->parent;
109 if (parent->ops->request)
110 parent->ops->request(mdev, count);
112 dev_notice(mdev_dev(mdev),
113 "No mdev vendor driver request callback support, blocked until released by user\n");
116 static const struct vfio_device_ops vfio_mdev_dev_ops = {
118 .open = vfio_mdev_open,
119 .release = vfio_mdev_release,
120 .ioctl = vfio_mdev_unlocked_ioctl,
121 .read = vfio_mdev_read,
122 .write = vfio_mdev_write,
123 .mmap = vfio_mdev_mmap,
124 .request = vfio_mdev_request,
127 static int vfio_mdev_probe(struct device *dev)
129 struct mdev_device *mdev = to_mdev_device(dev);
130 struct vfio_device *vdev;
133 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
137 vfio_init_group_dev(vdev, &mdev->dev, &vfio_mdev_dev_ops, mdev);
138 ret = vfio_register_group_dev(vdev);
143 dev_set_drvdata(&mdev->dev, vdev);
147 static void vfio_mdev_remove(struct device *dev)
149 struct vfio_device *vdev = dev_get_drvdata(dev);
151 vfio_unregister_group_dev(vdev);
155 static struct mdev_driver vfio_mdev_driver = {
157 .probe = vfio_mdev_probe,
158 .remove = vfio_mdev_remove,
161 static int __init vfio_mdev_init(void)
163 return mdev_register_driver(&vfio_mdev_driver, THIS_MODULE);
166 static void __exit vfio_mdev_exit(void)
168 mdev_unregister_driver(&vfio_mdev_driver);
171 module_init(vfio_mdev_init)
172 module_exit(vfio_mdev_exit)
174 MODULE_VERSION(DRIVER_VERSION);
175 MODULE_LICENSE("GPL v2");
176 MODULE_AUTHOR(DRIVER_AUTHOR);
177 MODULE_DESCRIPTION(DRIVER_DESC);