2 * Mediated device Core Driver
4 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5 * Author: Neo Jia <cjia@nvidia.com>
6 * Kirti Wankhede <kwankhede@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/uuid.h>
17 #include <linux/sysfs.h>
18 #include <linux/mdev.h>
20 #include "mdev_private.h"
22 #define DRIVER_VERSION "0.1"
23 #define DRIVER_AUTHOR "NVIDIA Corporation"
24 #define DRIVER_DESC "Mediated device Core Driver"
26 static LIST_HEAD(parent_list);
27 static DEFINE_MUTEX(parent_list_lock);
28 static struct class_compat *mdev_bus_compat_class;
30 static LIST_HEAD(mdev_list);
31 static DEFINE_MUTEX(mdev_list_lock);
33 struct device *mdev_parent_dev(struct mdev_device *mdev)
35 return mdev->parent->dev;
37 EXPORT_SYMBOL(mdev_parent_dev);
39 void *mdev_get_drvdata(struct mdev_device *mdev)
41 return mdev->driver_data;
43 EXPORT_SYMBOL(mdev_get_drvdata);
45 void mdev_set_drvdata(struct mdev_device *mdev, void *data)
47 mdev->driver_data = data;
49 EXPORT_SYMBOL(mdev_set_drvdata);
51 struct device *mdev_dev(struct mdev_device *mdev)
55 EXPORT_SYMBOL(mdev_dev);
57 struct mdev_device *mdev_from_dev(struct device *dev)
59 return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
61 EXPORT_SYMBOL(mdev_from_dev);
63 const guid_t *mdev_uuid(struct mdev_device *mdev)
67 EXPORT_SYMBOL(mdev_uuid);
69 /* Should be called holding parent_list_lock */
70 static struct mdev_parent *__find_parent_device(struct device *dev)
72 struct mdev_parent *parent;
74 list_for_each_entry(parent, &parent_list, next) {
75 if (parent->dev == dev)
81 static void mdev_release_parent(struct kref *kref)
83 struct mdev_parent *parent = container_of(kref, struct mdev_parent,
85 struct device *dev = parent->dev;
91 static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
94 kref_get(&parent->ref);
99 static void mdev_put_parent(struct mdev_parent *parent)
102 kref_put(&parent->ref, mdev_release_parent);
105 /* Caller must hold parent unreg_sem read or write lock */
106 static void mdev_device_remove_common(struct mdev_device *mdev)
108 struct mdev_parent *parent;
109 struct mdev_type *type;
112 type = to_mdev_type(mdev->type_kobj);
113 mdev_remove_sysfs_files(&mdev->dev, type);
114 device_del(&mdev->dev);
115 parent = mdev->parent;
116 lockdep_assert_held(&parent->unreg_sem);
117 ret = parent->ops->remove(mdev);
119 dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
121 /* Balances with device_initialize() */
122 put_device(&mdev->dev);
123 mdev_put_parent(parent);
126 static int mdev_device_remove_cb(struct device *dev, void *data)
128 if (dev_is_mdev(dev)) {
129 struct mdev_device *mdev;
131 mdev = to_mdev_device(dev);
132 mdev_device_remove_common(mdev);
138 * mdev_register_device : Register a device
139 * @dev: device structure representing parent device.
140 * @ops: Parent device operation structure to be registered.
142 * Add device to list of registered parent devices.
143 * Returns a negative value on error, otherwise 0.
145 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
148 struct mdev_parent *parent;
150 /* check for mandatory ops */
151 if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
154 dev = get_device(dev);
158 mutex_lock(&parent_list_lock);
160 /* Check for duplicate */
161 parent = __find_parent_device(dev);
168 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
174 kref_init(&parent->ref);
175 init_rwsem(&parent->unreg_sem);
180 if (!mdev_bus_compat_class) {
181 mdev_bus_compat_class = class_compat_register("mdev_bus");
182 if (!mdev_bus_compat_class) {
188 ret = parent_create_sysfs_files(parent);
192 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
194 dev_warn(dev, "Failed to create compatibility class link\n");
196 list_add(&parent->next, &parent_list);
197 mutex_unlock(&parent_list_lock);
199 dev_info(dev, "MDEV: Registered\n");
203 mutex_unlock(&parent_list_lock);
205 mdev_put_parent(parent);
210 EXPORT_SYMBOL(mdev_register_device);
213 * mdev_unregister_device : Unregister a parent device
214 * @dev: device structure representing parent device.
216 * Remove device from list of registered parent devices. Give a chance to free
217 * existing mediated devices for given device.
220 void mdev_unregister_device(struct device *dev)
222 struct mdev_parent *parent;
224 mutex_lock(&parent_list_lock);
225 parent = __find_parent_device(dev);
228 mutex_unlock(&parent_list_lock);
231 dev_info(dev, "MDEV: Unregistering\n");
233 list_del(&parent->next);
234 mutex_unlock(&parent_list_lock);
236 down_write(&parent->unreg_sem);
238 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
240 device_for_each_child(dev, NULL, mdev_device_remove_cb);
242 parent_remove_sysfs_files(parent);
243 up_write(&parent->unreg_sem);
245 mdev_put_parent(parent);
247 EXPORT_SYMBOL(mdev_unregister_device);
249 static void mdev_device_free(struct mdev_device *mdev)
251 mutex_lock(&mdev_list_lock);
252 list_del(&mdev->next);
253 mutex_unlock(&mdev_list_lock);
255 dev_dbg(&mdev->dev, "MDEV: destroying\n");
259 static void mdev_device_release(struct device *dev)
261 struct mdev_device *mdev = to_mdev_device(dev);
263 mdev_device_free(mdev);
266 int mdev_device_create(struct kobject *kobj,
267 struct device *dev, const guid_t *uuid)
270 struct mdev_device *mdev, *tmp;
271 struct mdev_parent *parent;
272 struct mdev_type *type = to_mdev_type(kobj);
274 parent = mdev_get_parent(type->parent);
278 mutex_lock(&mdev_list_lock);
280 /* Check for duplicate */
281 list_for_each_entry(tmp, &mdev_list, next) {
282 if (guid_equal(&tmp->uuid, uuid)) {
283 mutex_unlock(&mdev_list_lock);
289 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
291 mutex_unlock(&mdev_list_lock);
296 guid_copy(&mdev->uuid, uuid);
297 list_add(&mdev->next, &mdev_list);
298 mutex_unlock(&mdev_list_lock);
300 mdev->parent = parent;
302 /* Check if parent unregistration has started */
303 if (!down_read_trylock(&parent->unreg_sem)) {
304 mdev_device_free(mdev);
309 device_initialize(&mdev->dev);
310 mdev->dev.parent = dev;
311 mdev->dev.bus = &mdev_bus_type;
312 mdev->dev.release = mdev_device_release;
313 dev_set_name(&mdev->dev, "%pUl", uuid);
314 mdev->dev.groups = parent->ops->mdev_attr_groups;
315 mdev->type_kobj = kobj;
317 ret = parent->ops->create(kobj, mdev);
319 goto ops_create_fail;
321 ret = device_add(&mdev->dev);
325 ret = mdev_create_sysfs_files(&mdev->dev, type);
330 dev_dbg(&mdev->dev, "MDEV: created\n");
331 up_read(&parent->unreg_sem);
336 device_del(&mdev->dev);
338 parent->ops->remove(mdev);
340 up_read(&parent->unreg_sem);
341 put_device(&mdev->dev);
343 mdev_put_parent(parent);
347 int mdev_device_remove(struct device *dev)
349 struct mdev_device *mdev, *tmp;
350 struct mdev_parent *parent;
352 mdev = to_mdev_device(dev);
354 mutex_lock(&mdev_list_lock);
355 list_for_each_entry(tmp, &mdev_list, next) {
361 mutex_unlock(&mdev_list_lock);
366 mutex_unlock(&mdev_list_lock);
370 mdev->active = false;
371 mutex_unlock(&mdev_list_lock);
373 parent = mdev->parent;
374 /* Check if parent unregistration has started */
375 if (!down_read_trylock(&parent->unreg_sem))
378 mdev_device_remove_common(mdev);
379 up_read(&parent->unreg_sem);
383 int mdev_set_iommu_device(struct device *dev, struct device *iommu_device)
385 struct mdev_device *mdev = to_mdev_device(dev);
387 mdev->iommu_device = iommu_device;
391 EXPORT_SYMBOL(mdev_set_iommu_device);
393 struct device *mdev_get_iommu_device(struct device *dev)
395 struct mdev_device *mdev = to_mdev_device(dev);
397 return mdev->iommu_device;
399 EXPORT_SYMBOL(mdev_get_iommu_device);
401 static int __init mdev_init(void)
403 return mdev_bus_register();
406 static void __exit mdev_exit(void)
408 if (mdev_bus_compat_class)
409 class_compat_unregister(mdev_bus_compat_class);
411 mdev_bus_unregister();
414 module_init(mdev_init)
415 module_exit(mdev_exit)
417 MODULE_VERSION(DRIVER_VERSION);
418 MODULE_LICENSE("GPL v2");
419 MODULE_AUTHOR(DRIVER_AUTHOR);
420 MODULE_DESCRIPTION(DRIVER_DESC);
421 MODULE_SOFTDEP("post: vfio_mdev");