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 uuid_le
mdev_uuid(struct mdev_device
*mdev
)
67 EXPORT_SYMBOL(mdev_uuid
);
69 static int _find_mdev_device(struct device
*dev
, void *data
)
71 struct mdev_device
*mdev
;
73 if (!dev_is_mdev(dev
))
76 mdev
= to_mdev_device(dev
);
78 if (uuid_le_cmp(mdev
->uuid
, *(uuid_le
*)data
) == 0)
84 static bool mdev_device_exist(struct mdev_parent
*parent
, uuid_le uuid
)
88 dev
= device_find_child(parent
->dev
, &uuid
, _find_mdev_device
);
97 /* Should be called holding parent_list_lock */
98 static struct mdev_parent
*__find_parent_device(struct device
*dev
)
100 struct mdev_parent
*parent
;
102 list_for_each_entry(parent
, &parent_list
, next
) {
103 if (parent
->dev
== dev
)
109 static void mdev_release_parent(struct kref
*kref
)
111 struct mdev_parent
*parent
= container_of(kref
, struct mdev_parent
,
113 struct device
*dev
= parent
->dev
;
120 inline struct mdev_parent
*mdev_get_parent(struct mdev_parent
*parent
)
123 kref_get(&parent
->ref
);
128 static inline void mdev_put_parent(struct mdev_parent
*parent
)
131 kref_put(&parent
->ref
, mdev_release_parent
);
134 static int mdev_device_create_ops(struct kobject
*kobj
,
135 struct mdev_device
*mdev
)
137 struct mdev_parent
*parent
= mdev
->parent
;
140 ret
= parent
->ops
->create(kobj
, mdev
);
144 ret
= sysfs_create_groups(&mdev
->dev
.kobj
,
145 parent
->ops
->mdev_attr_groups
);
147 parent
->ops
->remove(mdev
);
153 * mdev_device_remove_ops gets called from sysfs's 'remove' and when parent
154 * device is being unregistered from mdev device framework.
155 * - 'force_remove' is set to 'false' when called from sysfs's 'remove' which
156 * indicates that if the mdev device is active, used by VMM or userspace
157 * application, vendor driver could return error then don't remove the device.
158 * - 'force_remove' is set to 'true' when called from mdev_unregister_device()
159 * which indicate that parent device is being removed from mdev device
160 * framework so remove mdev device forcefully.
162 static int mdev_device_remove_ops(struct mdev_device
*mdev
, bool force_remove
)
164 struct mdev_parent
*parent
= mdev
->parent
;
168 * Vendor driver can return error if VMM or userspace application is
169 * using this mdev device.
171 ret
= parent
->ops
->remove(mdev
);
172 if (ret
&& !force_remove
)
175 sysfs_remove_groups(&mdev
->dev
.kobj
, parent
->ops
->mdev_attr_groups
);
179 static int mdev_device_remove_cb(struct device
*dev
, void *data
)
181 if (!dev_is_mdev(dev
))
184 return mdev_device_remove(dev
, data
? *(bool *)data
: true);
188 * mdev_register_device : Register a device
189 * @dev: device structure representing parent device.
190 * @ops: Parent device operation structure to be registered.
192 * Add device to list of registered parent devices.
193 * Returns a negative value on error, otherwise 0.
195 int mdev_register_device(struct device
*dev
, const struct mdev_parent_ops
*ops
)
198 struct mdev_parent
*parent
;
200 /* check for mandatory ops */
201 if (!ops
|| !ops
->create
|| !ops
->remove
|| !ops
->supported_type_groups
)
204 dev
= get_device(dev
);
208 mutex_lock(&parent_list_lock
);
210 /* Check for duplicate */
211 parent
= __find_parent_device(dev
);
217 parent
= kzalloc(sizeof(*parent
), GFP_KERNEL
);
223 kref_init(&parent
->ref
);
224 mutex_init(&parent
->lock
);
229 if (!mdev_bus_compat_class
) {
230 mdev_bus_compat_class
= class_compat_register("mdev_bus");
231 if (!mdev_bus_compat_class
) {
237 ret
= parent_create_sysfs_files(parent
);
241 ret
= class_compat_create_link(mdev_bus_compat_class
, dev
, NULL
);
243 dev_warn(dev
, "Failed to create compatibility class link\n");
245 list_add(&parent
->next
, &parent_list
);
246 mutex_unlock(&parent_list_lock
);
248 dev_info(dev
, "MDEV: Registered\n");
252 mutex_unlock(&parent_list_lock
);
254 mdev_put_parent(parent
);
259 EXPORT_SYMBOL(mdev_register_device
);
262 * mdev_unregister_device : Unregister a parent device
263 * @dev: device structure representing parent device.
265 * Remove device from list of registered parent devices. Give a chance to free
266 * existing mediated devices for given device.
269 void mdev_unregister_device(struct device
*dev
)
271 struct mdev_parent
*parent
;
272 bool force_remove
= true;
274 mutex_lock(&parent_list_lock
);
275 parent
= __find_parent_device(dev
);
278 mutex_unlock(&parent_list_lock
);
281 dev_info(dev
, "MDEV: Unregistering\n");
283 list_del(&parent
->next
);
284 class_compat_remove_link(mdev_bus_compat_class
, dev
, NULL
);
286 device_for_each_child(dev
, (void *)&force_remove
,
287 mdev_device_remove_cb
);
289 parent_remove_sysfs_files(parent
);
291 mutex_unlock(&parent_list_lock
);
292 mdev_put_parent(parent
);
294 EXPORT_SYMBOL(mdev_unregister_device
);
296 static void mdev_device_release(struct device
*dev
)
298 struct mdev_device
*mdev
= to_mdev_device(dev
);
300 dev_dbg(&mdev
->dev
, "MDEV: destroying\n");
304 int mdev_device_create(struct kobject
*kobj
, struct device
*dev
, uuid_le uuid
)
307 struct mdev_device
*mdev
;
308 struct mdev_parent
*parent
;
309 struct mdev_type
*type
= to_mdev_type(kobj
);
311 parent
= mdev_get_parent(type
->parent
);
315 mutex_lock(&parent
->lock
);
317 /* Check for duplicate */
318 if (mdev_device_exist(parent
, uuid
)) {
323 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
329 memcpy(&mdev
->uuid
, &uuid
, sizeof(uuid_le
));
330 mdev
->parent
= parent
;
331 kref_init(&mdev
->ref
);
333 mdev
->dev
.parent
= dev
;
334 mdev
->dev
.bus
= &mdev_bus_type
;
335 mdev
->dev
.release
= mdev_device_release
;
336 dev_set_name(&mdev
->dev
, "%pUl", uuid
.b
);
338 ret
= device_register(&mdev
->dev
);
340 put_device(&mdev
->dev
);
344 ret
= mdev_device_create_ops(kobj
, mdev
);
348 ret
= mdev_create_sysfs_files(&mdev
->dev
, type
);
350 mdev_device_remove_ops(mdev
, true);
354 mdev
->type_kobj
= kobj
;
355 dev_dbg(&mdev
->dev
, "MDEV: created\n");
357 mutex_unlock(&parent
->lock
);
359 mutex_lock(&mdev_list_lock
);
360 list_add(&mdev
->next
, &mdev_list
);
361 mutex_unlock(&mdev_list_lock
);
366 device_unregister(&mdev
->dev
);
369 mutex_unlock(&parent
->lock
);
370 mdev_put_parent(parent
);
374 int mdev_device_remove(struct device
*dev
, bool force_remove
)
376 struct mdev_device
*mdev
, *tmp
;
377 struct mdev_parent
*parent
;
378 struct mdev_type
*type
;
382 mdev
= to_mdev_device(dev
);
384 mutex_lock(&mdev_list_lock
);
385 list_for_each_entry(tmp
, &mdev_list
, next
) {
393 list_del(&mdev
->next
);
395 mutex_unlock(&mdev_list_lock
);
400 type
= to_mdev_type(mdev
->type_kobj
);
401 parent
= mdev
->parent
;
402 mutex_lock(&parent
->lock
);
404 ret
= mdev_device_remove_ops(mdev
, force_remove
);
406 mutex_unlock(&parent
->lock
);
408 mutex_lock(&mdev_list_lock
);
409 list_add(&mdev
->next
, &mdev_list
);
410 mutex_unlock(&mdev_list_lock
);
415 mdev_remove_sysfs_files(dev
, type
);
416 device_unregister(dev
);
417 mutex_unlock(&parent
->lock
);
418 mdev_put_parent(parent
);
423 static int __init
mdev_init(void)
427 ret
= mdev_bus_register();
430 * Attempt to load known vfio_mdev. This gives us a working environment
431 * without the user needing to explicitly load vfio_mdev driver.
434 request_module_nowait("vfio_mdev");
439 static void __exit
mdev_exit(void)
441 if (mdev_bus_compat_class
)
442 class_compat_unregister(mdev_bus_compat_class
);
444 mdev_bus_unregister();
447 module_init(mdev_init
)
448 module_exit(mdev_exit
)
450 MODULE_VERSION(DRIVER_VERSION
);
451 MODULE_LICENSE("GPL v2");
452 MODULE_AUTHOR(DRIVER_AUTHOR
);
453 MODULE_DESCRIPTION(DRIVER_DESC
);