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 int _find_mdev_device(struct device
*dev
, void *data
)
32 struct mdev_device
*mdev
;
34 if (!dev_is_mdev(dev
))
37 mdev
= to_mdev_device(dev
);
39 if (uuid_le_cmp(mdev
->uuid
, *(uuid_le
*)data
) == 0)
45 static bool mdev_device_exist(struct parent_device
*parent
, uuid_le uuid
)
49 dev
= device_find_child(parent
->dev
, &uuid
, _find_mdev_device
);
58 /* Should be called holding parent_list_lock */
59 static struct parent_device
*__find_parent_device(struct device
*dev
)
61 struct parent_device
*parent
;
63 list_for_each_entry(parent
, &parent_list
, next
) {
64 if (parent
->dev
== dev
)
70 static void mdev_release_parent(struct kref
*kref
)
72 struct parent_device
*parent
= container_of(kref
, struct parent_device
,
74 struct device
*dev
= parent
->dev
;
81 inline struct parent_device
*mdev_get_parent(struct parent_device
*parent
)
84 kref_get(&parent
->ref
);
89 static inline void mdev_put_parent(struct parent_device
*parent
)
92 kref_put(&parent
->ref
, mdev_release_parent
);
95 static int mdev_device_create_ops(struct kobject
*kobj
,
96 struct mdev_device
*mdev
)
98 struct parent_device
*parent
= mdev
->parent
;
101 ret
= parent
->ops
->create(kobj
, mdev
);
105 ret
= sysfs_create_groups(&mdev
->dev
.kobj
,
106 parent
->ops
->mdev_attr_groups
);
108 parent
->ops
->remove(mdev
);
114 * mdev_device_remove_ops gets called from sysfs's 'remove' and when parent
115 * device is being unregistered from mdev device framework.
116 * - 'force_remove' is set to 'false' when called from sysfs's 'remove' which
117 * indicates that if the mdev device is active, used by VMM or userspace
118 * application, vendor driver could return error then don't remove the device.
119 * - 'force_remove' is set to 'true' when called from mdev_unregister_device()
120 * which indicate that parent device is being removed from mdev device
121 * framework so remove mdev device forcefully.
123 static int mdev_device_remove_ops(struct mdev_device
*mdev
, bool force_remove
)
125 struct parent_device
*parent
= mdev
->parent
;
129 * Vendor driver can return error if VMM or userspace application is
130 * using this mdev device.
132 ret
= parent
->ops
->remove(mdev
);
133 if (ret
&& !force_remove
)
136 sysfs_remove_groups(&mdev
->dev
.kobj
, parent
->ops
->mdev_attr_groups
);
140 static int mdev_device_remove_cb(struct device
*dev
, void *data
)
142 if (!dev_is_mdev(dev
))
145 return mdev_device_remove(dev
, data
? *(bool *)data
: true);
149 * mdev_register_device : Register a device
150 * @dev: device structure representing parent device.
151 * @ops: Parent device operation structure to be registered.
153 * Add device to list of registered parent devices.
154 * Returns a negative value on error, otherwise 0.
156 int mdev_register_device(struct device
*dev
, const struct parent_ops
*ops
)
159 struct parent_device
*parent
;
161 /* check for mandatory ops */
162 if (!ops
|| !ops
->create
|| !ops
->remove
|| !ops
->supported_type_groups
)
165 dev
= get_device(dev
);
169 mutex_lock(&parent_list_lock
);
171 /* Check for duplicate */
172 parent
= __find_parent_device(dev
);
178 parent
= kzalloc(sizeof(*parent
), GFP_KERNEL
);
184 kref_init(&parent
->ref
);
185 mutex_init(&parent
->lock
);
190 if (!mdev_bus_compat_class
) {
191 mdev_bus_compat_class
= class_compat_register("mdev_bus");
192 if (!mdev_bus_compat_class
) {
198 ret
= parent_create_sysfs_files(parent
);
202 ret
= class_compat_create_link(mdev_bus_compat_class
, dev
, NULL
);
204 dev_warn(dev
, "Failed to create compatibility class link\n");
206 list_add(&parent
->next
, &parent_list
);
207 mutex_unlock(&parent_list_lock
);
209 dev_info(dev
, "MDEV: Registered\n");
213 mutex_unlock(&parent_list_lock
);
215 mdev_put_parent(parent
);
220 EXPORT_SYMBOL(mdev_register_device
);
223 * mdev_unregister_device : Unregister a parent device
224 * @dev: device structure representing parent device.
226 * Remove device from list of registered parent devices. Give a chance to free
227 * existing mediated devices for given device.
230 void mdev_unregister_device(struct device
*dev
)
232 struct parent_device
*parent
;
233 bool force_remove
= true;
235 mutex_lock(&parent_list_lock
);
236 parent
= __find_parent_device(dev
);
239 mutex_unlock(&parent_list_lock
);
242 dev_info(dev
, "MDEV: Unregistering\n");
244 list_del(&parent
->next
);
245 class_compat_remove_link(mdev_bus_compat_class
, dev
, NULL
);
247 device_for_each_child(dev
, (void *)&force_remove
,
248 mdev_device_remove_cb
);
250 parent_remove_sysfs_files(parent
);
252 mutex_unlock(&parent_list_lock
);
253 mdev_put_parent(parent
);
255 EXPORT_SYMBOL(mdev_unregister_device
);
257 static void mdev_device_release(struct device
*dev
)
259 struct mdev_device
*mdev
= to_mdev_device(dev
);
261 dev_dbg(&mdev
->dev
, "MDEV: destroying\n");
265 int mdev_device_create(struct kobject
*kobj
, struct device
*dev
, uuid_le uuid
)
268 struct mdev_device
*mdev
;
269 struct parent_device
*parent
;
270 struct mdev_type
*type
= to_mdev_type(kobj
);
272 parent
= mdev_get_parent(type
->parent
);
276 mutex_lock(&parent
->lock
);
278 /* Check for duplicate */
279 if (mdev_device_exist(parent
, uuid
)) {
284 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
290 memcpy(&mdev
->uuid
, &uuid
, sizeof(uuid_le
));
291 mdev
->parent
= parent
;
292 kref_init(&mdev
->ref
);
294 mdev
->dev
.parent
= dev
;
295 mdev
->dev
.bus
= &mdev_bus_type
;
296 mdev
->dev
.release
= mdev_device_release
;
297 dev_set_name(&mdev
->dev
, "%pUl", uuid
.b
);
299 ret
= device_register(&mdev
->dev
);
301 put_device(&mdev
->dev
);
305 ret
= mdev_device_create_ops(kobj
, mdev
);
309 ret
= mdev_create_sysfs_files(&mdev
->dev
, type
);
311 mdev_device_remove_ops(mdev
, true);
315 mdev
->type_kobj
= kobj
;
316 dev_dbg(&mdev
->dev
, "MDEV: created\n");
318 mutex_unlock(&parent
->lock
);
322 device_unregister(&mdev
->dev
);
325 mutex_unlock(&parent
->lock
);
326 mdev_put_parent(parent
);
330 int mdev_device_remove(struct device
*dev
, bool force_remove
)
332 struct mdev_device
*mdev
;
333 struct parent_device
*parent
;
334 struct mdev_type
*type
;
337 mdev
= to_mdev_device(dev
);
338 type
= to_mdev_type(mdev
->type_kobj
);
339 parent
= mdev
->parent
;
340 mutex_lock(&parent
->lock
);
342 ret
= mdev_device_remove_ops(mdev
, force_remove
);
344 mutex_unlock(&parent
->lock
);
348 mdev_remove_sysfs_files(dev
, type
);
349 device_unregister(dev
);
350 mutex_unlock(&parent
->lock
);
351 mdev_put_parent(parent
);
355 static int __init
mdev_init(void)
359 ret
= mdev_bus_register();
362 * Attempt to load known vfio_mdev. This gives us a working environment
363 * without the user needing to explicitly load vfio_mdev driver.
366 request_module_nowait("vfio_mdev");
371 static void __exit
mdev_exit(void)
373 if (mdev_bus_compat_class
)
374 class_compat_unregister(mdev_bus_compat_class
);
376 mdev_bus_unregister();
379 module_init(mdev_init
)
380 module_exit(mdev_exit
)
382 MODULE_VERSION(DRIVER_VERSION
);
383 MODULE_LICENSE("GPL v2");
384 MODULE_AUTHOR(DRIVER_AUTHOR
);
385 MODULE_DESCRIPTION(DRIVER_DESC
);