Loading...
Note: File does not exist in v6.8.
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
20static int vfio_mdev_open(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))
26 return -EINVAL;
27
28 return parent->ops->open(mdev);
29}
30
31static void vfio_mdev_release(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->release))
37 parent->ops->release(mdev);
38}
39
40static 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 -EINVAL;
48
49 return parent->ops->ioctl(mdev, cmd, arg);
50}
51
52static 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
64static 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
77static 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
89static 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
101static const struct vfio_device_ops vfio_mdev_dev_ops = {
102 .name = "vfio-mdev",
103 .open = vfio_mdev_open,
104 .release = vfio_mdev_release,
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
112static 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_group_dev(vdev);
123 if (ret) {
124 kfree(vdev);
125 return ret;
126 }
127 dev_set_drvdata(&mdev->dev, vdev);
128 return 0;
129}
130
131static void vfio_mdev_remove(struct mdev_device *mdev)
132{
133 struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
134
135 vfio_unregister_group_dev(vdev);
136 kfree(vdev);
137}
138
139struct mdev_driver vfio_mdev_driver = {
140 .driver = {
141 .name = "vfio_mdev",
142 .owner = THIS_MODULE,
143 .mod_name = KBUILD_MODNAME,
144 },
145 .probe = vfio_mdev_probe,
146 .remove = vfio_mdev_remove,
147};