Linux 4.19.133
[linux/fpc-iii.git] / drivers / vfio / mdev / mdev_core.c
blobe052f62fdea7e8151cf4fd2b3d650805161da341
1 /*
2 * Mediated device Core Driver
4 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5 * Author: Neo Jia <cjia@nvidia.com>
6 * Kirti Wankhede <kwankhede@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/uuid.h>
17 #include <linux/sysfs.h>
18 #include <linux/mdev.h>
20 #include "mdev_private.h"
22 #define DRIVER_VERSION "0.1"
23 #define DRIVER_AUTHOR "NVIDIA Corporation"
24 #define DRIVER_DESC "Mediated device Core Driver"
26 static LIST_HEAD(parent_list);
27 static DEFINE_MUTEX(parent_list_lock);
28 static struct class_compat *mdev_bus_compat_class;
30 static LIST_HEAD(mdev_list);
31 static DEFINE_MUTEX(mdev_list_lock);
33 struct device *mdev_parent_dev(struct mdev_device *mdev)
35 return mdev->parent->dev;
37 EXPORT_SYMBOL(mdev_parent_dev);
39 void *mdev_get_drvdata(struct mdev_device *mdev)
41 return mdev->driver_data;
43 EXPORT_SYMBOL(mdev_get_drvdata);
45 void mdev_set_drvdata(struct mdev_device *mdev, void *data)
47 mdev->driver_data = data;
49 EXPORT_SYMBOL(mdev_set_drvdata);
51 struct device *mdev_dev(struct mdev_device *mdev)
53 return &mdev->dev;
55 EXPORT_SYMBOL(mdev_dev);
57 struct mdev_device *mdev_from_dev(struct device *dev)
59 return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
61 EXPORT_SYMBOL(mdev_from_dev);
63 uuid_le mdev_uuid(struct mdev_device *mdev)
65 return mdev->uuid;
67 EXPORT_SYMBOL(mdev_uuid);
69 /* Should be called holding parent_list_lock */
70 static struct mdev_parent *__find_parent_device(struct device *dev)
72 struct mdev_parent *parent;
74 list_for_each_entry(parent, &parent_list, next) {
75 if (parent->dev == dev)
76 return parent;
78 return NULL;
81 static void mdev_release_parent(struct kref *kref)
83 struct mdev_parent *parent = container_of(kref, struct mdev_parent,
84 ref);
85 struct device *dev = parent->dev;
87 kfree(parent);
88 put_device(dev);
91 static
92 inline struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
94 if (parent)
95 kref_get(&parent->ref);
97 return parent;
100 static inline void mdev_put_parent(struct mdev_parent *parent)
102 if (parent)
103 kref_put(&parent->ref, mdev_release_parent);
106 static int mdev_device_create_ops(struct kobject *kobj,
107 struct mdev_device *mdev)
109 struct mdev_parent *parent = mdev->parent;
110 int ret;
112 ret = parent->ops->create(kobj, mdev);
113 if (ret)
114 return ret;
116 ret = sysfs_create_groups(&mdev->dev.kobj,
117 parent->ops->mdev_attr_groups);
118 if (ret)
119 parent->ops->remove(mdev);
121 return ret;
125 * mdev_device_remove_ops gets called from sysfs's 'remove' and when parent
126 * device is being unregistered from mdev device framework.
127 * - 'force_remove' is set to 'false' when called from sysfs's 'remove' which
128 * indicates that if the mdev device is active, used by VMM or userspace
129 * application, vendor driver could return error then don't remove the device.
130 * - 'force_remove' is set to 'true' when called from mdev_unregister_device()
131 * which indicate that parent device is being removed from mdev device
132 * framework so remove mdev device forcefully.
134 static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
136 struct mdev_parent *parent = mdev->parent;
137 int ret;
140 * Vendor driver can return error if VMM or userspace application is
141 * using this mdev device.
143 ret = parent->ops->remove(mdev);
144 if (ret && !force_remove)
145 return -EBUSY;
147 sysfs_remove_groups(&mdev->dev.kobj, parent->ops->mdev_attr_groups);
148 return 0;
151 static int mdev_device_remove_cb(struct device *dev, void *data)
153 if (dev_is_mdev(dev))
154 mdev_device_remove(dev, true);
156 return 0;
160 * mdev_register_device : Register a device
161 * @dev: device structure representing parent device.
162 * @ops: Parent device operation structure to be registered.
164 * Add device to list of registered parent devices.
165 * Returns a negative value on error, otherwise 0.
167 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
169 int ret;
170 struct mdev_parent *parent;
172 /* check for mandatory ops */
173 if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
174 return -EINVAL;
176 dev = get_device(dev);
177 if (!dev)
178 return -EINVAL;
180 mutex_lock(&parent_list_lock);
182 /* Check for duplicate */
183 parent = __find_parent_device(dev);
184 if (parent) {
185 parent = NULL;
186 ret = -EEXIST;
187 goto add_dev_err;
190 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
191 if (!parent) {
192 ret = -ENOMEM;
193 goto add_dev_err;
196 kref_init(&parent->ref);
198 parent->dev = dev;
199 parent->ops = ops;
201 if (!mdev_bus_compat_class) {
202 mdev_bus_compat_class = class_compat_register("mdev_bus");
203 if (!mdev_bus_compat_class) {
204 ret = -ENOMEM;
205 goto add_dev_err;
209 ret = parent_create_sysfs_files(parent);
210 if (ret)
211 goto add_dev_err;
213 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
214 if (ret)
215 dev_warn(dev, "Failed to create compatibility class link\n");
217 list_add(&parent->next, &parent_list);
218 mutex_unlock(&parent_list_lock);
220 dev_info(dev, "MDEV: Registered\n");
221 return 0;
223 add_dev_err:
224 mutex_unlock(&parent_list_lock);
225 if (parent)
226 mdev_put_parent(parent);
227 else
228 put_device(dev);
229 return ret;
231 EXPORT_SYMBOL(mdev_register_device);
234 * mdev_unregister_device : Unregister a parent device
235 * @dev: device structure representing parent device.
237 * Remove device from list of registered parent devices. Give a chance to free
238 * existing mediated devices for given device.
241 void mdev_unregister_device(struct device *dev)
243 struct mdev_parent *parent;
245 mutex_lock(&parent_list_lock);
246 parent = __find_parent_device(dev);
248 if (!parent) {
249 mutex_unlock(&parent_list_lock);
250 return;
252 dev_info(dev, "MDEV: Unregistering\n");
254 list_del(&parent->next);
255 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
257 device_for_each_child(dev, NULL, mdev_device_remove_cb);
259 parent_remove_sysfs_files(parent);
261 mutex_unlock(&parent_list_lock);
262 mdev_put_parent(parent);
264 EXPORT_SYMBOL(mdev_unregister_device);
266 static void mdev_device_release(struct device *dev)
268 struct mdev_device *mdev = to_mdev_device(dev);
270 mutex_lock(&mdev_list_lock);
271 list_del(&mdev->next);
272 mutex_unlock(&mdev_list_lock);
274 dev_dbg(&mdev->dev, "MDEV: destroying\n");
275 kfree(mdev);
278 int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
280 int ret;
281 struct mdev_device *mdev, *tmp;
282 struct mdev_parent *parent;
283 struct mdev_type *type = to_mdev_type(kobj);
285 parent = mdev_get_parent(type->parent);
286 if (!parent)
287 return -EINVAL;
289 mutex_lock(&mdev_list_lock);
291 /* Check for duplicate */
292 list_for_each_entry(tmp, &mdev_list, next) {
293 if (!uuid_le_cmp(tmp->uuid, uuid)) {
294 mutex_unlock(&mdev_list_lock);
295 ret = -EEXIST;
296 goto mdev_fail;
300 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
301 if (!mdev) {
302 mutex_unlock(&mdev_list_lock);
303 ret = -ENOMEM;
304 goto mdev_fail;
307 memcpy(&mdev->uuid, &uuid, sizeof(uuid_le));
308 list_add(&mdev->next, &mdev_list);
309 mutex_unlock(&mdev_list_lock);
311 mdev->parent = parent;
312 kref_init(&mdev->ref);
314 mdev->dev.parent = dev;
315 mdev->dev.bus = &mdev_bus_type;
316 mdev->dev.release = mdev_device_release;
317 dev_set_name(&mdev->dev, "%pUl", uuid.b);
319 ret = device_register(&mdev->dev);
320 if (ret) {
321 put_device(&mdev->dev);
322 goto mdev_fail;
325 ret = mdev_device_create_ops(kobj, mdev);
326 if (ret)
327 goto create_fail;
329 ret = mdev_create_sysfs_files(&mdev->dev, type);
330 if (ret) {
331 mdev_device_remove_ops(mdev, true);
332 goto create_fail;
335 mdev->type_kobj = kobj;
336 mdev->active = true;
337 dev_dbg(&mdev->dev, "MDEV: created\n");
339 return 0;
341 create_fail:
342 device_unregister(&mdev->dev);
343 mdev_fail:
344 mdev_put_parent(parent);
345 return ret;
348 int mdev_device_remove(struct device *dev, bool force_remove)
350 struct mdev_device *mdev, *tmp;
351 struct mdev_parent *parent;
352 struct mdev_type *type;
353 int ret;
355 mdev = to_mdev_device(dev);
357 mutex_lock(&mdev_list_lock);
358 list_for_each_entry(tmp, &mdev_list, next) {
359 if (tmp == mdev)
360 break;
363 if (tmp != mdev) {
364 mutex_unlock(&mdev_list_lock);
365 return -ENODEV;
368 if (!mdev->active) {
369 mutex_unlock(&mdev_list_lock);
370 return -EAGAIN;
373 mdev->active = false;
374 mutex_unlock(&mdev_list_lock);
376 type = to_mdev_type(mdev->type_kobj);
377 parent = mdev->parent;
379 ret = mdev_device_remove_ops(mdev, force_remove);
380 if (ret) {
381 mdev->active = true;
382 return ret;
385 mdev_remove_sysfs_files(dev, type);
386 device_unregister(dev);
387 mdev_put_parent(parent);
389 return 0;
392 static int __init mdev_init(void)
394 return mdev_bus_register();
397 static void __exit mdev_exit(void)
399 if (mdev_bus_compat_class)
400 class_compat_unregister(mdev_bus_compat_class);
402 mdev_bus_unregister();
405 module_init(mdev_init)
406 module_exit(mdev_exit)
408 MODULE_VERSION(DRIVER_VERSION);
409 MODULE_LICENSE("GPL v2");
410 MODULE_AUTHOR(DRIVER_AUTHOR);
411 MODULE_DESCRIPTION(DRIVER_DESC);
412 MODULE_SOFTDEP("post: vfio_mdev");