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/device.h>
12 #include <linux/slab.h>
13 #include <linux/uuid.h>
14 #include <linux/sysfs.h>
15 #include <linux/mdev.h>
17 #include "mdev_private.h"
19 #define DRIVER_VERSION "0.1"
20 #define DRIVER_AUTHOR "NVIDIA Corporation"
21 #define DRIVER_DESC "Mediated device Core Driver"
23 static LIST_HEAD(parent_list);
24 static DEFINE_MUTEX(parent_list_lock);
25 static struct class_compat *mdev_bus_compat_class;
27 static LIST_HEAD(mdev_list);
28 static DEFINE_MUTEX(mdev_list_lock);
30 struct device *mdev_parent_dev(struct mdev_device *mdev)
32 return mdev->type->parent->dev;
34 EXPORT_SYMBOL(mdev_parent_dev);
37 * Return the index in supported_type_groups that this mdev_device was created
40 unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
42 return mdev->type->type_group_id;
44 EXPORT_SYMBOL(mdev_get_type_group_id);
47 * Used in mdev_type_attribute sysfs functions to return the index in the
48 * supported_type_groups that the sysfs is called from.
50 unsigned int mtype_get_type_group_id(struct mdev_type *mtype)
52 return mtype->type_group_id;
54 EXPORT_SYMBOL(mtype_get_type_group_id);
57 * Used in mdev_type_attribute sysfs functions to return the parent struct
60 struct device *mtype_get_parent_dev(struct mdev_type *mtype)
62 return mtype->parent->dev;
64 EXPORT_SYMBOL(mtype_get_parent_dev);
66 /* Should be called holding parent_list_lock */
67 static struct mdev_parent *__find_parent_device(struct device *dev)
69 struct mdev_parent *parent;
71 list_for_each_entry(parent, &parent_list, next) {
72 if (parent->dev == dev)
78 void mdev_release_parent(struct kref *kref)
80 struct mdev_parent *parent = container_of(kref, struct mdev_parent,
82 struct device *dev = parent->dev;
88 /* Caller must hold parent unreg_sem read or write lock */
89 static void mdev_device_remove_common(struct mdev_device *mdev)
91 struct mdev_parent *parent = mdev->type->parent;
93 mdev_remove_sysfs_files(mdev);
94 device_del(&mdev->dev);
95 lockdep_assert_held(&parent->unreg_sem);
96 /* Balances with device_initialize() */
97 put_device(&mdev->dev);
100 static int mdev_device_remove_cb(struct device *dev, void *data)
102 struct mdev_device *mdev = mdev_from_dev(dev);
105 mdev_device_remove_common(mdev);
110 * mdev_register_device : Register a device
111 * @dev: device structure representing parent device.
112 * @mdev_driver: Device driver to bind to the newly created mdev
114 * Add device to list of registered parent devices.
115 * Returns a negative value on error, otherwise 0.
117 int mdev_register_device(struct device *dev, struct mdev_driver *mdev_driver)
120 struct mdev_parent *parent;
121 char *env_string = "MDEV_STATE=registered";
122 char *envp[] = { env_string, NULL };
124 /* check for mandatory ops */
125 if (!mdev_driver->supported_type_groups)
128 dev = get_device(dev);
132 mutex_lock(&parent_list_lock);
134 /* Check for duplicate */
135 parent = __find_parent_device(dev);
142 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
148 kref_init(&parent->ref);
149 init_rwsem(&parent->unreg_sem);
152 parent->mdev_driver = mdev_driver;
154 if (!mdev_bus_compat_class) {
155 mdev_bus_compat_class = class_compat_register("mdev_bus");
156 if (!mdev_bus_compat_class) {
162 ret = parent_create_sysfs_files(parent);
166 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
168 dev_warn(dev, "Failed to create compatibility class link\n");
170 list_add(&parent->next, &parent_list);
171 mutex_unlock(&parent_list_lock);
173 dev_info(dev, "MDEV: Registered\n");
174 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
179 mutex_unlock(&parent_list_lock);
181 mdev_put_parent(parent);
186 EXPORT_SYMBOL(mdev_register_device);
189 * mdev_unregister_device : Unregister a parent device
190 * @dev: device structure representing parent device.
192 * Remove device from list of registered parent devices. Give a chance to free
193 * existing mediated devices for given device.
196 void mdev_unregister_device(struct device *dev)
198 struct mdev_parent *parent;
199 char *env_string = "MDEV_STATE=unregistered";
200 char *envp[] = { env_string, NULL };
202 mutex_lock(&parent_list_lock);
203 parent = __find_parent_device(dev);
206 mutex_unlock(&parent_list_lock);
209 dev_info(dev, "MDEV: Unregistering\n");
211 list_del(&parent->next);
212 mutex_unlock(&parent_list_lock);
214 down_write(&parent->unreg_sem);
216 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
218 device_for_each_child(dev, NULL, mdev_device_remove_cb);
220 parent_remove_sysfs_files(parent);
221 up_write(&parent->unreg_sem);
223 mdev_put_parent(parent);
225 /* We still have the caller's reference to use for the uevent */
226 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
228 EXPORT_SYMBOL(mdev_unregister_device);
230 static void mdev_device_release(struct device *dev)
232 struct mdev_device *mdev = to_mdev_device(dev);
234 /* Pairs with the get in mdev_device_create() */
235 kobject_put(&mdev->type->kobj);
237 mutex_lock(&mdev_list_lock);
238 list_del(&mdev->next);
239 mutex_unlock(&mdev_list_lock);
241 dev_dbg(&mdev->dev, "MDEV: destroying\n");
245 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
248 struct mdev_device *mdev, *tmp;
249 struct mdev_parent *parent = type->parent;
250 struct mdev_driver *drv = parent->mdev_driver;
252 mutex_lock(&mdev_list_lock);
254 /* Check for duplicate */
255 list_for_each_entry(tmp, &mdev_list, next) {
256 if (guid_equal(&tmp->uuid, uuid)) {
257 mutex_unlock(&mdev_list_lock);
262 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
264 mutex_unlock(&mdev_list_lock);
268 device_initialize(&mdev->dev);
269 mdev->dev.parent = parent->dev;
270 mdev->dev.bus = &mdev_bus_type;
271 mdev->dev.release = mdev_device_release;
272 mdev->dev.groups = mdev_device_groups;
274 /* Pairs with the put in mdev_device_release() */
275 kobject_get(&type->kobj);
277 guid_copy(&mdev->uuid, uuid);
278 list_add(&mdev->next, &mdev_list);
279 mutex_unlock(&mdev_list_lock);
281 ret = dev_set_name(&mdev->dev, "%pUl", uuid);
285 /* Check if parent unregistration has started */
286 if (!down_read_trylock(&parent->unreg_sem)) {
291 ret = device_add(&mdev->dev);
295 ret = device_driver_attach(&drv->driver, &mdev->dev);
299 ret = mdev_create_sysfs_files(mdev);
304 dev_dbg(&mdev->dev, "MDEV: created\n");
305 up_read(&parent->unreg_sem);
310 device_del(&mdev->dev);
312 up_read(&parent->unreg_sem);
314 put_device(&mdev->dev);
318 int mdev_device_remove(struct mdev_device *mdev)
320 struct mdev_device *tmp;
321 struct mdev_parent *parent = mdev->type->parent;
323 mutex_lock(&mdev_list_lock);
324 list_for_each_entry(tmp, &mdev_list, next) {
330 mutex_unlock(&mdev_list_lock);
335 mutex_unlock(&mdev_list_lock);
339 mdev->active = false;
340 mutex_unlock(&mdev_list_lock);
342 /* Check if parent unregistration has started */
343 if (!down_read_trylock(&parent->unreg_sem))
346 mdev_device_remove_common(mdev);
347 up_read(&parent->unreg_sem);
351 static int __init mdev_init(void)
353 return bus_register(&mdev_bus_type);
356 static void __exit mdev_exit(void)
358 if (mdev_bus_compat_class)
359 class_compat_unregister(mdev_bus_compat_class);
360 bus_unregister(&mdev_bus_type);
363 subsys_initcall(mdev_init)
364 module_exit(mdev_exit)
366 MODULE_VERSION(DRIVER_VERSION);
367 MODULE_LICENSE("GPL v2");
368 MODULE_AUTHOR(DRIVER_AUTHOR);
369 MODULE_DESCRIPTION(DRIVER_DESC);