1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VFIO based driver for Mediated device
4 *
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
8 */
9
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>
17
18 #include "mdev_private.h"
19
vfio_mdev_open_device(struct vfio_device * core_vdev)20 static int vfio_mdev_open_device(struct vfio_device *core_vdev)
21 {
22 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
23 struct mdev_parent *parent = mdev->type->parent;
24
25 if (unlikely(!parent->ops->open_device))
26 return 0;
27
28 return parent->ops->open_device(mdev);
29 }
30
vfio_mdev_close_device(struct vfio_device * core_vdev)31 static void vfio_mdev_close_device(struct vfio_device *core_vdev)
32 {
33 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
34 struct mdev_parent *parent = mdev->type->parent;
35
36 if (likely(parent->ops->close_device))
37 parent->ops->close_device(mdev);
38 }
39
vfio_mdev_unlocked_ioctl(struct vfio_device * core_vdev,unsigned int cmd,unsigned long arg)40 static long vfio_mdev_unlocked_ioctl(struct vfio_device *core_vdev,
41 unsigned int cmd, unsigned long arg)
42 {
43 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
44 struct mdev_parent *parent = mdev->type->parent;
45
46 if (unlikely(!parent->ops->ioctl))
47 return 0;
48
49 return parent->ops->ioctl(mdev, cmd, arg);
50 }
51
vfio_mdev_read(struct vfio_device * core_vdev,char __user * buf,size_t count,loff_t * ppos)52 static ssize_t vfio_mdev_read(struct vfio_device *core_vdev, char __user *buf,
53 size_t count, loff_t *ppos)
54 {
55 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
56 struct mdev_parent *parent = mdev->type->parent;
57
58 if (unlikely(!parent->ops->read))
59 return -EINVAL;
60
61 return parent->ops->read(mdev, buf, count, ppos);
62 }
63
vfio_mdev_write(struct vfio_device * core_vdev,const char __user * buf,size_t count,loff_t * ppos)64 static ssize_t vfio_mdev_write(struct vfio_device *core_vdev,
65 const char __user *buf, size_t count,
66 loff_t *ppos)
67 {
68 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
69 struct mdev_parent *parent = mdev->type->parent;
70
71 if (unlikely(!parent->ops->write))
72 return -EINVAL;
73
74 return parent->ops->write(mdev, buf, count, ppos);
75 }
76
vfio_mdev_mmap(struct vfio_device * core_vdev,struct vm_area_struct * vma)77 static int vfio_mdev_mmap(struct vfio_device *core_vdev,
78 struct vm_area_struct *vma)
79 {
80 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
81 struct mdev_parent *parent = mdev->type->parent;
82
83 if (unlikely(!parent->ops->mmap))
84 return -EINVAL;
85
86 return parent->ops->mmap(mdev, vma);
87 }
88
vfio_mdev_request(struct vfio_device * core_vdev,unsigned int count)89 static void vfio_mdev_request(struct vfio_device *core_vdev, unsigned int count)
90 {
91 struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
92 struct mdev_parent *parent = mdev->type->parent;
93
94 if (parent->ops->request)
95 parent->ops->request(mdev, count);
96 else if (count == 0)
97 dev_notice(mdev_dev(mdev),
98 "No mdev vendor driver request callback support, blocked until released by user\n");
99 }
100
101 static const struct vfio_device_ops vfio_mdev_dev_ops = {
102 .name = "vfio-mdev",
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,
110 };
111
vfio_mdev_probe(struct mdev_device * mdev)112 static int vfio_mdev_probe(struct mdev_device *mdev)
113 {
114 struct vfio_device *vdev;
115 int ret;
116
117 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
118 if (!vdev)
119 return -ENOMEM;
120
121 vfio_init_group_dev(vdev, &mdev->dev, &vfio_mdev_dev_ops);
122 ret = vfio_register_emulated_iommu_dev(vdev);
123 if (ret)
124 goto out_uninit;
125
126 dev_set_drvdata(&mdev->dev, vdev);
127 return 0;
128
129 out_uninit:
130 vfio_uninit_group_dev(vdev);
131 kfree(vdev);
132 return ret;
133 }
134
vfio_mdev_remove(struct mdev_device * mdev)135 static void vfio_mdev_remove(struct mdev_device *mdev)
136 {
137 struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
138
139 vfio_unregister_group_dev(vdev);
140 vfio_uninit_group_dev(vdev);
141 kfree(vdev);
142 }
143
144 struct mdev_driver vfio_mdev_driver = {
145 .driver = {
146 .name = "vfio_mdev",
147 .owner = THIS_MODULE,
148 .mod_name = KBUILD_MODNAME,
149 },
150 .probe = vfio_mdev_probe,
151 .remove = vfio_mdev_remove,
152 };
153