1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mediated device Core Driver
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/module.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/mdev.h>
15 #include "mdev_private.h"
17 #define DRIVER_VERSION "0.1"
18 #define DRIVER_AUTHOR "NVIDIA Corporation"
19 #define DRIVER_DESC "Mediated device Core Driver"
21 static struct class_compat *mdev_bus_compat_class;
23 static LIST_HEAD(mdev_list);
24 static DEFINE_MUTEX(mdev_list_lock);
26 /* Caller must hold parent unreg_sem read or write lock */
27 static void mdev_device_remove_common(struct mdev_device *mdev)
29 struct mdev_parent *parent = mdev->type->parent;
31 mdev_remove_sysfs_files(mdev);
32 device_del(&mdev->dev);
33 lockdep_assert_held(&parent->unreg_sem);
34 /* Balances with device_initialize() */
35 put_device(&mdev->dev);
38 static int mdev_device_remove_cb(struct device *dev, void *data)
40 if (dev->bus == &mdev_bus_type)
41 mdev_device_remove_common(to_mdev_device(dev));
46 * mdev_register_parent: Register a device as parent for mdevs
47 * @parent: parent structure registered
48 * @dev: device structure representing parent device.
49 * @mdev_driver: Device driver to bind to the newly created mdev
50 * @types: Array of supported mdev types
51 * @nr_types: Number of entries in @types
53 * Registers the @parent stucture as a parent for mdev types and thus mdev
54 * devices. The caller needs to hold a reference on @dev that must not be
55 * released until after the call to mdev_unregister_parent().
57 * Returns a negative value on error, otherwise 0.
59 int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
60 struct mdev_driver *mdev_driver, struct mdev_type **types,
61 unsigned int nr_types)
63 char *env_string = "MDEV_STATE=registered";
64 char *envp[] = { env_string, NULL };
67 memset(parent, 0, sizeof(*parent));
68 init_rwsem(&parent->unreg_sem);
70 parent->mdev_driver = mdev_driver;
71 parent->types = types;
72 parent->nr_types = nr_types;
73 atomic_set(&parent->available_instances, mdev_driver->max_instances);
75 ret = parent_create_sysfs_files(parent);
79 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
81 dev_warn(dev, "Failed to create compatibility class link\n");
83 dev_info(dev, "MDEV: Registered\n");
84 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
87 EXPORT_SYMBOL(mdev_register_parent);
90 * mdev_unregister_parent : Unregister a parent device
91 * @parent: parent structure to unregister
93 void mdev_unregister_parent(struct mdev_parent *parent)
95 char *env_string = "MDEV_STATE=unregistered";
96 char *envp[] = { env_string, NULL };
98 dev_info(parent->dev, "MDEV: Unregistering\n");
100 down_write(&parent->unreg_sem);
101 class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
102 device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
103 parent_remove_sysfs_files(parent);
104 up_write(&parent->unreg_sem);
106 kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
108 EXPORT_SYMBOL(mdev_unregister_parent);
110 static void mdev_device_release(struct device *dev)
112 struct mdev_device *mdev = to_mdev_device(dev);
113 struct mdev_parent *parent = mdev->type->parent;
115 mutex_lock(&mdev_list_lock);
116 list_del(&mdev->next);
117 if (!parent->mdev_driver->get_available)
118 atomic_inc(&parent->available_instances);
119 mutex_unlock(&mdev_list_lock);
121 /* Pairs with the get in mdev_device_create() */
122 kobject_put(&mdev->type->kobj);
124 dev_dbg(&mdev->dev, "MDEV: destroying\n");
128 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
131 struct mdev_device *mdev, *tmp;
132 struct mdev_parent *parent = type->parent;
133 struct mdev_driver *drv = parent->mdev_driver;
135 mutex_lock(&mdev_list_lock);
137 /* Check for duplicate */
138 list_for_each_entry(tmp, &mdev_list, next) {
139 if (guid_equal(&tmp->uuid, uuid)) {
140 mutex_unlock(&mdev_list_lock);
145 if (!drv->get_available) {
147 * Note: that non-atomic read and dec is fine here because
148 * all modifications are under mdev_list_lock.
150 if (!atomic_read(&parent->available_instances)) {
151 mutex_unlock(&mdev_list_lock);
154 atomic_dec(&parent->available_instances);
157 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
159 mutex_unlock(&mdev_list_lock);
163 device_initialize(&mdev->dev);
164 mdev->dev.parent = parent->dev;
165 mdev->dev.bus = &mdev_bus_type;
166 mdev->dev.release = mdev_device_release;
167 mdev->dev.groups = mdev_device_groups;
169 /* Pairs with the put in mdev_device_release() */
170 kobject_get(&type->kobj);
172 guid_copy(&mdev->uuid, uuid);
173 list_add(&mdev->next, &mdev_list);
174 mutex_unlock(&mdev_list_lock);
176 ret = dev_set_name(&mdev->dev, "%pUl", uuid);
180 /* Check if parent unregistration has started */
181 if (!down_read_trylock(&parent->unreg_sem)) {
186 ret = device_add(&mdev->dev);
190 ret = device_driver_attach(&drv->driver, &mdev->dev);
194 ret = mdev_create_sysfs_files(mdev);
199 dev_dbg(&mdev->dev, "MDEV: created\n");
200 up_read(&parent->unreg_sem);
205 device_del(&mdev->dev);
207 up_read(&parent->unreg_sem);
209 put_device(&mdev->dev);
213 int mdev_device_remove(struct mdev_device *mdev)
215 struct mdev_device *tmp;
216 struct mdev_parent *parent = mdev->type->parent;
218 mutex_lock(&mdev_list_lock);
219 list_for_each_entry(tmp, &mdev_list, next) {
225 mutex_unlock(&mdev_list_lock);
230 mutex_unlock(&mdev_list_lock);
234 mdev->active = false;
235 mutex_unlock(&mdev_list_lock);
237 /* Check if parent unregistration has started */
238 if (!down_read_trylock(&parent->unreg_sem))
241 mdev_device_remove_common(mdev);
242 up_read(&parent->unreg_sem);
246 static int __init mdev_init(void)
250 ret = bus_register(&mdev_bus_type);
254 mdev_bus_compat_class = class_compat_register("mdev_bus");
255 if (!mdev_bus_compat_class) {
256 bus_unregister(&mdev_bus_type);
263 static void __exit mdev_exit(void)
265 class_compat_unregister(mdev_bus_compat_class);
266 bus_unregister(&mdev_bus_type);
269 subsys_initcall(mdev_init)
270 module_exit(mdev_exit)
272 MODULE_VERSION(DRIVER_VERSION);
273 MODULE_LICENSE("GPL v2");
274 MODULE_AUTHOR(DRIVER_AUTHOR);
275 MODULE_DESCRIPTION(DRIVER_DESC);