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->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 static 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 static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
61 kref_get(&parent->ref);
66 static void mdev_put_parent(struct mdev_parent *parent)
69 kref_put(&parent->ref, mdev_release_parent);
72 /* Caller must hold parent unreg_sem read or write lock */
73 static void mdev_device_remove_common(struct mdev_device *mdev)
75 struct mdev_parent *parent;
76 struct mdev_type *type;
79 type = to_mdev_type(mdev->type_kobj);
80 mdev_remove_sysfs_files(mdev, type);
81 device_del(&mdev->dev);
82 parent = mdev->parent;
83 lockdep_assert_held(&parent->unreg_sem);
84 ret = parent->ops->remove(mdev);
86 dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
88 /* Balances with device_initialize() */
89 put_device(&mdev->dev);
90 mdev_put_parent(parent);
93 static int mdev_device_remove_cb(struct device *dev, void *data)
95 struct mdev_device *mdev = mdev_from_dev(dev);
98 mdev_device_remove_common(mdev);
103 * mdev_register_device : Register a device
104 * @dev: device structure representing parent device.
105 * @ops: Parent device operation structure to be registered.
107 * Add device to list of registered parent devices.
108 * Returns a negative value on error, otherwise 0.
110 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
113 struct mdev_parent *parent;
114 char *env_string = "MDEV_STATE=registered";
115 char *envp[] = { env_string, NULL };
117 /* check for mandatory ops */
118 if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
121 dev = get_device(dev);
125 /* Not mandatory, but its absence could be a problem */
127 dev_info(dev, "Driver cannot be asked to release device\n");
129 mutex_lock(&parent_list_lock);
131 /* Check for duplicate */
132 parent = __find_parent_device(dev);
139 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
145 kref_init(&parent->ref);
146 init_rwsem(&parent->unreg_sem);
151 if (!mdev_bus_compat_class) {
152 mdev_bus_compat_class = class_compat_register("mdev_bus");
153 if (!mdev_bus_compat_class) {
159 ret = parent_create_sysfs_files(parent);
163 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
165 dev_warn(dev, "Failed to create compatibility class link\n");
167 list_add(&parent->next, &parent_list);
168 mutex_unlock(&parent_list_lock);
170 dev_info(dev, "MDEV: Registered\n");
171 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
176 mutex_unlock(&parent_list_lock);
178 mdev_put_parent(parent);
183 EXPORT_SYMBOL(mdev_register_device);
186 * mdev_unregister_device : Unregister a parent device
187 * @dev: device structure representing parent device.
189 * Remove device from list of registered parent devices. Give a chance to free
190 * existing mediated devices for given device.
193 void mdev_unregister_device(struct device *dev)
195 struct mdev_parent *parent;
196 char *env_string = "MDEV_STATE=unregistered";
197 char *envp[] = { env_string, NULL };
199 mutex_lock(&parent_list_lock);
200 parent = __find_parent_device(dev);
203 mutex_unlock(&parent_list_lock);
206 dev_info(dev, "MDEV: Unregistering\n");
208 list_del(&parent->next);
209 mutex_unlock(&parent_list_lock);
211 down_write(&parent->unreg_sem);
213 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
215 device_for_each_child(dev, NULL, mdev_device_remove_cb);
217 parent_remove_sysfs_files(parent);
218 up_write(&parent->unreg_sem);
220 mdev_put_parent(parent);
222 /* We still have the caller's reference to use for the uevent */
223 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
225 EXPORT_SYMBOL(mdev_unregister_device);
227 static void mdev_device_free(struct mdev_device *mdev)
229 mutex_lock(&mdev_list_lock);
230 list_del(&mdev->next);
231 mutex_unlock(&mdev_list_lock);
233 dev_dbg(&mdev->dev, "MDEV: destroying\n");
237 static void mdev_device_release(struct device *dev)
239 struct mdev_device *mdev = to_mdev_device(dev);
241 mdev_device_free(mdev);
244 int mdev_device_create(struct kobject *kobj,
245 struct device *dev, const guid_t *uuid)
248 struct mdev_device *mdev, *tmp;
249 struct mdev_parent *parent;
250 struct mdev_type *type = to_mdev_type(kobj);
252 parent = mdev_get_parent(type->parent);
256 mutex_lock(&mdev_list_lock);
258 /* Check for duplicate */
259 list_for_each_entry(tmp, &mdev_list, next) {
260 if (guid_equal(&tmp->uuid, uuid)) {
261 mutex_unlock(&mdev_list_lock);
267 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
269 mutex_unlock(&mdev_list_lock);
274 guid_copy(&mdev->uuid, uuid);
275 list_add(&mdev->next, &mdev_list);
276 mutex_unlock(&mdev_list_lock);
278 mdev->parent = parent;
280 /* Check if parent unregistration has started */
281 if (!down_read_trylock(&parent->unreg_sem)) {
282 mdev_device_free(mdev);
287 device_initialize(&mdev->dev);
288 mdev->dev.parent = dev;
289 mdev->dev.bus = &mdev_bus_type;
290 mdev->dev.release = mdev_device_release;
291 dev_set_name(&mdev->dev, "%pUl", uuid);
292 mdev->dev.groups = parent->ops->mdev_attr_groups;
293 mdev->type_kobj = kobj;
295 ret = parent->ops->create(kobj, mdev);
297 goto ops_create_fail;
299 ret = device_add(&mdev->dev);
303 ret = mdev_create_sysfs_files(mdev, type);
308 dev_dbg(&mdev->dev, "MDEV: created\n");
309 up_read(&parent->unreg_sem);
314 device_del(&mdev->dev);
316 parent->ops->remove(mdev);
318 up_read(&parent->unreg_sem);
319 put_device(&mdev->dev);
321 mdev_put_parent(parent);
325 int mdev_device_remove(struct mdev_device *mdev)
327 struct mdev_device *tmp;
328 struct mdev_parent *parent;
330 mutex_lock(&mdev_list_lock);
331 list_for_each_entry(tmp, &mdev_list, next) {
337 mutex_unlock(&mdev_list_lock);
342 mutex_unlock(&mdev_list_lock);
346 mdev->active = false;
347 mutex_unlock(&mdev_list_lock);
349 parent = mdev->parent;
350 /* Check if parent unregistration has started */
351 if (!down_read_trylock(&parent->unreg_sem))
354 mdev_device_remove_common(mdev);
355 up_read(&parent->unreg_sem);
359 static int __init mdev_init(void)
361 return mdev_bus_register();
364 static void __exit mdev_exit(void)
366 if (mdev_bus_compat_class)
367 class_compat_unregister(mdev_bus_compat_class);
369 mdev_bus_unregister();
372 module_init(mdev_init)
373 module_exit(mdev_exit)
375 MODULE_VERSION(DRIVER_VERSION);
376 MODULE_LICENSE("GPL v2");
377 MODULE_AUTHOR(DRIVER_AUTHOR);
378 MODULE_DESCRIPTION(DRIVER_DESC);
379 MODULE_SOFTDEP("post: vfio_mdev");