1 // SPDX-License-Identifier: GPL-2.0-only
3 * VFIO based driver for Mediated device
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
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 static int vfio_mdev_open_device(struct vfio_device *core_vdev)
22 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
23 struct mdev_parent *parent = mdev->type->parent;
25 if (unlikely(!parent->ops->open_device))
28 return parent->ops->open_device(mdev);
31 static void vfio_mdev_close_device(struct vfio_device *core_vdev)
33 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
34 struct mdev_parent *parent = mdev->type->parent;
36 if (likely(parent->ops->close_device))
37 parent->ops->close_device(mdev);
40 static long vfio_mdev_unlocked_ioctl(struct vfio_device *core_vdev,
41 unsigned int cmd, unsigned long arg)
43 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
44 struct mdev_parent *parent = mdev->type->parent;
46 if (unlikely(!parent->ops->ioctl))
49 return parent->ops->ioctl(mdev, cmd, arg);
52 static ssize_t vfio_mdev_read(struct vfio_device *core_vdev, char __user *buf,
53 size_t count, loff_t *ppos)
55 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
56 struct mdev_parent *parent = mdev->type->parent;
58 if (unlikely(!parent->ops->read))
61 return parent->ops->read(mdev, buf, count, ppos);
64 static ssize_t vfio_mdev_write(struct vfio_device *core_vdev,
65 const char __user *buf, size_t count,
68 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
69 struct mdev_parent *parent = mdev->type->parent;
71 if (unlikely(!parent->ops->write))
74 return parent->ops->write(mdev, buf, count, ppos);
77 static int vfio_mdev_mmap(struct vfio_device *core_vdev,
78 struct vm_area_struct *vma)
80 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
81 struct mdev_parent *parent = mdev->type->parent;
83 if (unlikely(!parent->ops->mmap))
86 return parent->ops->mmap(mdev, vma);
89 static void vfio_mdev_request(struct vfio_device *core_vdev, unsigned int count)
91 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
92 struct mdev_parent *parent = mdev->type->parent;
94 if (parent->ops->request)
95 parent->ops->request(mdev, count);
97 dev_notice(mdev_dev(mdev),
98 "No mdev vendor driver request callback support, blocked until released by user\n");
101 static const struct vfio_device_ops vfio_mdev_dev_ops = {
103 .open_device = vfio_mdev_open_device,
104 .close_device = vfio_mdev_close_device,
105 .ioctl = vfio_mdev_unlocked_ioctl,
106 .read = vfio_mdev_read,
107 .write = vfio_mdev_write,
108 .mmap = vfio_mdev_mmap,
109 .request = vfio_mdev_request,
112 static int vfio_mdev_probe(struct mdev_device *mdev)
114 struct vfio_device *vdev;
117 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
121 vfio_init_group_dev(vdev, &mdev->dev, &vfio_mdev_dev_ops);
122 ret = vfio_register_emulated_iommu_dev(vdev);
126 dev_set_drvdata(&mdev->dev, vdev);
130 vfio_uninit_group_dev(vdev);
135 static void vfio_mdev_remove(struct mdev_device *mdev)
137 struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
139 vfio_unregister_group_dev(vdev);
140 vfio_uninit_group_dev(vdev);
144 struct mdev_driver vfio_mdev_driver = {
147 .owner = THIS_MODULE,
148 .mod_name = KBUILD_MODNAME,
150 .probe = vfio_mdev_probe,
151 .remove = vfio_mdev_remove,