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);
36 /* Should be called holding parent_list_lock */
37 static struct mdev_parent *__find_parent_device(struct device *dev)
39 struct mdev_parent *parent;
41 list_for_each_entry(parent, &parent_list, next) {
42 if (parent->dev == dev)
48 void mdev_release_parent(struct kref *kref)
50 struct mdev_parent *parent = container_of(kref, struct mdev_parent,
52 struct device *dev = parent->dev;
58 /* Caller must hold parent unreg_sem read or write lock */
59 static void mdev_device_remove_common(struct mdev_device *mdev)
61 struct mdev_parent *parent = mdev->type->parent;
64 mdev_remove_sysfs_files(mdev);
65 device_del(&mdev->dev);
66 lockdep_assert_held(&parent->unreg_sem);
67 ret = parent->ops->remove(mdev);
69 dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
71 /* Balances with device_initialize() */
72 put_device(&mdev->dev);
75 static int mdev_device_remove_cb(struct device *dev, void *data)
77 struct mdev_device *mdev = mdev_from_dev(dev);
80 mdev_device_remove_common(mdev);
85 * mdev_register_device : Register a device
86 * @dev: device structure representing parent device.
87 * @ops: Parent device operation structure to be registered.
89 * Add device to list of registered parent devices.
90 * Returns a negative value on error, otherwise 0.
92 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
95 struct mdev_parent *parent;
96 char *env_string = "MDEV_STATE=registered";
97 char *envp[] = { env_string, NULL };
99 /* check for mandatory ops */
100 if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
103 dev = get_device(dev);
107 /* Not mandatory, but its absence could be a problem */
109 dev_info(dev, "Driver cannot be asked to release device\n");
111 mutex_lock(&parent_list_lock);
113 /* Check for duplicate */
114 parent = __find_parent_device(dev);
121 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
127 kref_init(&parent->ref);
128 init_rwsem(&parent->unreg_sem);
133 if (!mdev_bus_compat_class) {
134 mdev_bus_compat_class = class_compat_register("mdev_bus");
135 if (!mdev_bus_compat_class) {
141 ret = parent_create_sysfs_files(parent);
145 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
147 dev_warn(dev, "Failed to create compatibility class link\n");
149 list_add(&parent->next, &parent_list);
150 mutex_unlock(&parent_list_lock);
152 dev_info(dev, "MDEV: Registered\n");
153 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
158 mutex_unlock(&parent_list_lock);
160 mdev_put_parent(parent);
165 EXPORT_SYMBOL(mdev_register_device);
168 * mdev_unregister_device : Unregister a parent device
169 * @dev: device structure representing parent device.
171 * Remove device from list of registered parent devices. Give a chance to free
172 * existing mediated devices for given device.
175 void mdev_unregister_device(struct device *dev)
177 struct mdev_parent *parent;
178 char *env_string = "MDEV_STATE=unregistered";
179 char *envp[] = { env_string, NULL };
181 mutex_lock(&parent_list_lock);
182 parent = __find_parent_device(dev);
185 mutex_unlock(&parent_list_lock);
188 dev_info(dev, "MDEV: Unregistering\n");
190 list_del(&parent->next);
191 mutex_unlock(&parent_list_lock);
193 down_write(&parent->unreg_sem);
195 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
197 device_for_each_child(dev, NULL, mdev_device_remove_cb);
199 parent_remove_sysfs_files(parent);
200 up_write(&parent->unreg_sem);
202 mdev_put_parent(parent);
204 /* We still have the caller's reference to use for the uevent */
205 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
207 EXPORT_SYMBOL(mdev_unregister_device);
209 static void mdev_device_release(struct device *dev)
211 struct mdev_device *mdev = to_mdev_device(dev);
213 /* Pairs with the get in mdev_device_create() */
214 kobject_put(&mdev->type->kobj);
216 mutex_lock(&mdev_list_lock);
217 list_del(&mdev->next);
218 mutex_unlock(&mdev_list_lock);
220 dev_dbg(&mdev->dev, "MDEV: destroying\n");
224 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
227 struct mdev_device *mdev, *tmp;
228 struct mdev_parent *parent = type->parent;
230 mutex_lock(&mdev_list_lock);
232 /* Check for duplicate */
233 list_for_each_entry(tmp, &mdev_list, next) {
234 if (guid_equal(&tmp->uuid, uuid)) {
235 mutex_unlock(&mdev_list_lock);
240 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
242 mutex_unlock(&mdev_list_lock);
246 device_initialize(&mdev->dev);
247 mdev->dev.parent = parent->dev;
248 mdev->dev.bus = &mdev_bus_type;
249 mdev->dev.release = mdev_device_release;
250 mdev->dev.groups = parent->ops->mdev_attr_groups;
252 /* Pairs with the put in mdev_device_release() */
253 kobject_get(&type->kobj);
255 guid_copy(&mdev->uuid, uuid);
256 list_add(&mdev->next, &mdev_list);
257 mutex_unlock(&mdev_list_lock);
259 ret = dev_set_name(&mdev->dev, "%pUl", uuid);
263 /* Check if parent unregistration has started */
264 if (!down_read_trylock(&parent->unreg_sem)) {
269 ret = parent->ops->create(&type->kobj, mdev);
273 ret = device_add(&mdev->dev);
277 ret = mdev_create_sysfs_files(mdev);
282 dev_dbg(&mdev->dev, "MDEV: created\n");
283 up_read(&parent->unreg_sem);
288 device_del(&mdev->dev);
290 parent->ops->remove(mdev);
292 up_read(&parent->unreg_sem);
294 put_device(&mdev->dev);
298 int mdev_device_remove(struct mdev_device *mdev)
300 struct mdev_device *tmp;
301 struct mdev_parent *parent = mdev->type->parent;
303 mutex_lock(&mdev_list_lock);
304 list_for_each_entry(tmp, &mdev_list, next) {
310 mutex_unlock(&mdev_list_lock);
315 mutex_unlock(&mdev_list_lock);
319 mdev->active = false;
320 mutex_unlock(&mdev_list_lock);
322 /* Check if parent unregistration has started */
323 if (!down_read_trylock(&parent->unreg_sem))
326 mdev_device_remove_common(mdev);
327 up_read(&parent->unreg_sem);
331 static int __init mdev_init(void)
333 return mdev_bus_register();
336 static void __exit mdev_exit(void)
338 if (mdev_bus_compat_class)
339 class_compat_unregister(mdev_bus_compat_class);
341 mdev_bus_unregister();
344 module_init(mdev_init)
345 module_exit(mdev_exit)
347 MODULE_VERSION(DRIVER_VERSION);
348 MODULE_LICENSE("GPL v2");
349 MODULE_AUTHOR(DRIVER_AUTHOR);
350 MODULE_DESCRIPTION(DRIVER_DESC);
351 MODULE_SOFTDEP("post: vfio_mdev");