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 if (!mdev_bus_compat_class) {
76 mdev_bus_compat_class = class_compat_register("mdev_bus");
77 if (!mdev_bus_compat_class)
81 ret = parent_create_sysfs_files(parent);
85 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
87 dev_warn(dev, "Failed to create compatibility class link\n");
89 dev_info(dev, "MDEV: Registered\n");
90 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
93 EXPORT_SYMBOL(mdev_register_parent);
96 * mdev_unregister_parent : Unregister a parent device
97 * @parent: parent structure to unregister
99 void mdev_unregister_parent(struct mdev_parent *parent)
101 char *env_string = "MDEV_STATE=unregistered";
102 char *envp[] = { env_string, NULL };
104 dev_info(parent->dev, "MDEV: Unregistering\n");
106 down_write(&parent->unreg_sem);
107 class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
108 device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
109 parent_remove_sysfs_files(parent);
110 up_write(&parent->unreg_sem);
112 kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
114 EXPORT_SYMBOL(mdev_unregister_parent);
116 static void mdev_device_release(struct device *dev)
118 struct mdev_device *mdev = to_mdev_device(dev);
119 struct mdev_parent *parent = mdev->type->parent;
121 mutex_lock(&mdev_list_lock);
122 list_del(&mdev->next);
123 if (!parent->mdev_driver->get_available)
124 atomic_inc(&parent->available_instances);
125 mutex_unlock(&mdev_list_lock);
127 /* Pairs with the get in mdev_device_create() */
128 kobject_put(&mdev->type->kobj);
130 dev_dbg(&mdev->dev, "MDEV: destroying\n");
134 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
137 struct mdev_device *mdev, *tmp;
138 struct mdev_parent *parent = type->parent;
139 struct mdev_driver *drv = parent->mdev_driver;
141 mutex_lock(&mdev_list_lock);
143 /* Check for duplicate */
144 list_for_each_entry(tmp, &mdev_list, next) {
145 if (guid_equal(&tmp->uuid, uuid)) {
146 mutex_unlock(&mdev_list_lock);
151 if (!drv->get_available) {
153 * Note: that non-atomic read and dec is fine here because
154 * all modifications are under mdev_list_lock.
156 if (!atomic_read(&parent->available_instances)) {
157 mutex_unlock(&mdev_list_lock);
160 atomic_dec(&parent->available_instances);
163 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
165 mutex_unlock(&mdev_list_lock);
169 device_initialize(&mdev->dev);
170 mdev->dev.parent = parent->dev;
171 mdev->dev.bus = &mdev_bus_type;
172 mdev->dev.release = mdev_device_release;
173 mdev->dev.groups = mdev_device_groups;
175 /* Pairs with the put in mdev_device_release() */
176 kobject_get(&type->kobj);
178 guid_copy(&mdev->uuid, uuid);
179 list_add(&mdev->next, &mdev_list);
180 mutex_unlock(&mdev_list_lock);
182 ret = dev_set_name(&mdev->dev, "%pUl", uuid);
186 /* Check if parent unregistration has started */
187 if (!down_read_trylock(&parent->unreg_sem)) {
192 ret = device_add(&mdev->dev);
196 ret = device_driver_attach(&drv->driver, &mdev->dev);
200 ret = mdev_create_sysfs_files(mdev);
205 dev_dbg(&mdev->dev, "MDEV: created\n");
206 up_read(&parent->unreg_sem);
211 device_del(&mdev->dev);
213 up_read(&parent->unreg_sem);
215 put_device(&mdev->dev);
219 int mdev_device_remove(struct mdev_device *mdev)
221 struct mdev_device *tmp;
222 struct mdev_parent *parent = mdev->type->parent;
224 mutex_lock(&mdev_list_lock);
225 list_for_each_entry(tmp, &mdev_list, next) {
231 mutex_unlock(&mdev_list_lock);
236 mutex_unlock(&mdev_list_lock);
240 mdev->active = false;
241 mutex_unlock(&mdev_list_lock);
243 /* Check if parent unregistration has started */
244 if (!down_read_trylock(&parent->unreg_sem))
247 mdev_device_remove_common(mdev);
248 up_read(&parent->unreg_sem);
252 static int __init mdev_init(void)
254 return bus_register(&mdev_bus_type);
257 static void __exit mdev_exit(void)
259 if (mdev_bus_compat_class)
260 class_compat_unregister(mdev_bus_compat_class);
261 bus_unregister(&mdev_bus_type);
264 subsys_initcall(mdev_init)
265 module_exit(mdev_exit)
267 MODULE_VERSION(DRIVER_VERSION);
268 MODULE_LICENSE("GPL v2");
269 MODULE_AUTHOR(DRIVER_AUTHOR);
270 MODULE_DESCRIPTION(DRIVER_DESC);