Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Mediated device Core Driver
4 *
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
8 */
9
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <linux/sysfs.h>
13#include <linux/mdev.h>
14
15#include "mdev_private.h"
16
17#define DRIVER_VERSION "0.1"
18#define DRIVER_AUTHOR "NVIDIA Corporation"
19#define DRIVER_DESC "Mediated device Core Driver"
20
21static struct class_compat *mdev_bus_compat_class;
22
23static LIST_HEAD(mdev_list);
24static DEFINE_MUTEX(mdev_list_lock);
25
26/* Caller must hold parent unreg_sem read or write lock */
27static void mdev_device_remove_common(struct mdev_device *mdev)
28{
29 struct mdev_parent *parent = mdev->type->parent;
30
31 mdev_remove_sysfs_files(mdev);
32 device_del(&mdev->dev);
33 lockdep_assert_held(&parent->unreg_sem);
34 /* Balances with device_initialize() */
35 put_device(&mdev->dev);
36}
37
38static int mdev_device_remove_cb(struct device *dev, void *data)
39{
40 if (dev->bus == &mdev_bus_type)
41 mdev_device_remove_common(to_mdev_device(dev));
42 return 0;
43}
44
45/*
46 * mdev_register_parent: Register a device as parent for mdevs
47 * @parent: parent structure registered
48 * @dev: device structure representing parent device.
49 * @mdev_driver: Device driver to bind to the newly created mdev
50 * @types: Array of supported mdev types
51 * @nr_types: Number of entries in @types
52 *
53 * Registers the @parent stucture as a parent for mdev types and thus mdev
54 * devices. The caller needs to hold a reference on @dev that must not be
55 * released until after the call to mdev_unregister_parent().
56 *
57 * Returns a negative value on error, otherwise 0.
58 */
59int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
60 struct mdev_driver *mdev_driver, struct mdev_type **types,
61 unsigned int nr_types)
62{
63 char *env_string = "MDEV_STATE=registered";
64 char *envp[] = { env_string, NULL };
65 int ret;
66
67 memset(parent, 0, sizeof(*parent));
68 init_rwsem(&parent->unreg_sem);
69 parent->dev = dev;
70 parent->mdev_driver = mdev_driver;
71 parent->types = types;
72 parent->nr_types = nr_types;
73 atomic_set(&parent->available_instances, mdev_driver->max_instances);
74
75 ret = parent_create_sysfs_files(parent);
76 if (ret)
77 return ret;
78
79 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
80 if (ret)
81 dev_warn(dev, "Failed to create compatibility class link\n");
82
83 dev_info(dev, "MDEV: Registered\n");
84 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
85 return 0;
86}
87EXPORT_SYMBOL(mdev_register_parent);
88
89/*
90 * mdev_unregister_parent : Unregister a parent device
91 * @parent: parent structure to unregister
92 */
93void mdev_unregister_parent(struct mdev_parent *parent)
94{
95 char *env_string = "MDEV_STATE=unregistered";
96 char *envp[] = { env_string, NULL };
97
98 dev_info(parent->dev, "MDEV: Unregistering\n");
99
100 down_write(&parent->unreg_sem);
101 class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
102 device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
103 parent_remove_sysfs_files(parent);
104 up_write(&parent->unreg_sem);
105
106 kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
107}
108EXPORT_SYMBOL(mdev_unregister_parent);
109
110static void mdev_device_release(struct device *dev)
111{
112 struct mdev_device *mdev = to_mdev_device(dev);
113 struct mdev_parent *parent = mdev->type->parent;
114
115 mutex_lock(&mdev_list_lock);
116 list_del(&mdev->next);
117 if (!parent->mdev_driver->get_available)
118 atomic_inc(&parent->available_instances);
119 mutex_unlock(&mdev_list_lock);
120
121 /* Pairs with the get in mdev_device_create() */
122 kobject_put(&mdev->type->kobj);
123
124 dev_dbg(&mdev->dev, "MDEV: destroying\n");
125 kfree(mdev);
126}
127
128int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
129{
130 int ret;
131 struct mdev_device *mdev, *tmp;
132 struct mdev_parent *parent = type->parent;
133 struct mdev_driver *drv = parent->mdev_driver;
134
135 mutex_lock(&mdev_list_lock);
136
137 /* Check for duplicate */
138 list_for_each_entry(tmp, &mdev_list, next) {
139 if (guid_equal(&tmp->uuid, uuid)) {
140 mutex_unlock(&mdev_list_lock);
141 return -EEXIST;
142 }
143 }
144
145 if (!drv->get_available) {
146 /*
147 * Note: that non-atomic read and dec is fine here because
148 * all modifications are under mdev_list_lock.
149 */
150 if (!atomic_read(&parent->available_instances)) {
151 mutex_unlock(&mdev_list_lock);
152 return -EUSERS;
153 }
154 atomic_dec(&parent->available_instances);
155 }
156
157 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
158 if (!mdev) {
159 mutex_unlock(&mdev_list_lock);
160 return -ENOMEM;
161 }
162
163 device_initialize(&mdev->dev);
164 mdev->dev.parent = parent->dev;
165 mdev->dev.bus = &mdev_bus_type;
166 mdev->dev.release = mdev_device_release;
167 mdev->dev.groups = mdev_device_groups;
168 mdev->type = type;
169 /* Pairs with the put in mdev_device_release() */
170 kobject_get(&type->kobj);
171
172 guid_copy(&mdev->uuid, uuid);
173 list_add(&mdev->next, &mdev_list);
174 mutex_unlock(&mdev_list_lock);
175
176 ret = dev_set_name(&mdev->dev, "%pUl", uuid);
177 if (ret)
178 goto out_put_device;
179
180 /* Check if parent unregistration has started */
181 if (!down_read_trylock(&parent->unreg_sem)) {
182 ret = -ENODEV;
183 goto out_put_device;
184 }
185
186 ret = device_add(&mdev->dev);
187 if (ret)
188 goto out_unlock;
189
190 ret = device_driver_attach(&drv->driver, &mdev->dev);
191 if (ret)
192 goto out_del;
193
194 ret = mdev_create_sysfs_files(mdev);
195 if (ret)
196 goto out_del;
197
198 mdev->active = true;
199 dev_dbg(&mdev->dev, "MDEV: created\n");
200 up_read(&parent->unreg_sem);
201
202 return 0;
203
204out_del:
205 device_del(&mdev->dev);
206out_unlock:
207 up_read(&parent->unreg_sem);
208out_put_device:
209 put_device(&mdev->dev);
210 return ret;
211}
212
213int mdev_device_remove(struct mdev_device *mdev)
214{
215 struct mdev_device *tmp;
216 struct mdev_parent *parent = mdev->type->parent;
217
218 mutex_lock(&mdev_list_lock);
219 list_for_each_entry(tmp, &mdev_list, next) {
220 if (tmp == mdev)
221 break;
222 }
223
224 if (tmp != mdev) {
225 mutex_unlock(&mdev_list_lock);
226 return -ENODEV;
227 }
228
229 if (!mdev->active) {
230 mutex_unlock(&mdev_list_lock);
231 return -EAGAIN;
232 }
233
234 mdev->active = false;
235 mutex_unlock(&mdev_list_lock);
236
237 /* Check if parent unregistration has started */
238 if (!down_read_trylock(&parent->unreg_sem))
239 return -ENODEV;
240
241 mdev_device_remove_common(mdev);
242 up_read(&parent->unreg_sem);
243 return 0;
244}
245
246static int __init mdev_init(void)
247{
248 int ret;
249
250 ret = bus_register(&mdev_bus_type);
251 if (ret)
252 return ret;
253
254 mdev_bus_compat_class = class_compat_register("mdev_bus");
255 if (!mdev_bus_compat_class) {
256 bus_unregister(&mdev_bus_type);
257 return -ENOMEM;
258 }
259
260 return 0;
261}
262
263static void __exit mdev_exit(void)
264{
265 class_compat_unregister(mdev_bus_compat_class);
266 bus_unregister(&mdev_bus_type);
267}
268
269subsys_initcall(mdev_init)
270module_exit(mdev_exit)
271
272MODULE_VERSION(DRIVER_VERSION);
273MODULE_LICENSE("GPL v2");
274MODULE_AUTHOR(DRIVER_AUTHOR);
275MODULE_DESCRIPTION(DRIVER_DESC);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Mediated device Core Driver
4 *
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
8 */
9
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>
16
17#include "mdev_private.h"
18
19#define DRIVER_VERSION "0.1"
20#define DRIVER_AUTHOR "NVIDIA Corporation"
21#define DRIVER_DESC "Mediated device Core Driver"
22
23static LIST_HEAD(parent_list);
24static DEFINE_MUTEX(parent_list_lock);
25static struct class_compat *mdev_bus_compat_class;
26
27static LIST_HEAD(mdev_list);
28static DEFINE_MUTEX(mdev_list_lock);
29
30struct device *mdev_parent_dev(struct mdev_device *mdev)
31{
32 return mdev->type->parent->dev;
33}
34EXPORT_SYMBOL(mdev_parent_dev);
35
36/*
37 * Return the index in supported_type_groups that this mdev_device was created
38 * from.
39 */
40unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
41{
42 return mdev->type->type_group_id;
43}
44EXPORT_SYMBOL(mdev_get_type_group_id);
45
46/*
47 * Used in mdev_type_attribute sysfs functions to return the index in the
48 * supported_type_groups that the sysfs is called from.
49 */
50unsigned int mtype_get_type_group_id(struct mdev_type *mtype)
51{
52 return mtype->type_group_id;
53}
54EXPORT_SYMBOL(mtype_get_type_group_id);
55
56/*
57 * Used in mdev_type_attribute sysfs functions to return the parent struct
58 * device
59 */
60struct device *mtype_get_parent_dev(struct mdev_type *mtype)
61{
62 return mtype->parent->dev;
63}
64EXPORT_SYMBOL(mtype_get_parent_dev);
65
66/* Should be called holding parent_list_lock */
67static struct mdev_parent *__find_parent_device(struct device *dev)
68{
69 struct mdev_parent *parent;
70
71 list_for_each_entry(parent, &parent_list, next) {
72 if (parent->dev == dev)
73 return parent;
74 }
75 return NULL;
76}
77
78void mdev_release_parent(struct kref *kref)
79{
80 struct mdev_parent *parent = container_of(kref, struct mdev_parent,
81 ref);
82 struct device *dev = parent->dev;
83
84 kfree(parent);
85 put_device(dev);
86}
87
88/* Caller must hold parent unreg_sem read or write lock */
89static void mdev_device_remove_common(struct mdev_device *mdev)
90{
91 struct mdev_parent *parent = mdev->type->parent;
92 int ret;
93
94 mdev_remove_sysfs_files(mdev);
95 device_del(&mdev->dev);
96 lockdep_assert_held(&parent->unreg_sem);
97 if (parent->ops->remove) {
98 ret = parent->ops->remove(mdev);
99 if (ret)
100 dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
101 }
102
103 /* Balances with device_initialize() */
104 put_device(&mdev->dev);
105}
106
107static int mdev_device_remove_cb(struct device *dev, void *data)
108{
109 struct mdev_device *mdev = mdev_from_dev(dev);
110
111 if (mdev)
112 mdev_device_remove_common(mdev);
113 return 0;
114}
115
116/*
117 * mdev_register_device : Register a device
118 * @dev: device structure representing parent device.
119 * @ops: Parent device operation structure to be registered.
120 *
121 * Add device to list of registered parent devices.
122 * Returns a negative value on error, otherwise 0.
123 */
124int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
125{
126 int ret;
127 struct mdev_parent *parent;
128 char *env_string = "MDEV_STATE=registered";
129 char *envp[] = { env_string, NULL };
130
131 /* check for mandatory ops */
132 if (!ops || !ops->supported_type_groups)
133 return -EINVAL;
134 if (!ops->device_driver && (!ops->create || !ops->remove))
135 return -EINVAL;
136
137 dev = get_device(dev);
138 if (!dev)
139 return -EINVAL;
140
141 /* Not mandatory, but its absence could be a problem */
142 if (!ops->request)
143 dev_info(dev, "Driver cannot be asked to release device\n");
144
145 mutex_lock(&parent_list_lock);
146
147 /* Check for duplicate */
148 parent = __find_parent_device(dev);
149 if (parent) {
150 parent = NULL;
151 ret = -EEXIST;
152 goto add_dev_err;
153 }
154
155 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
156 if (!parent) {
157 ret = -ENOMEM;
158 goto add_dev_err;
159 }
160
161 kref_init(&parent->ref);
162 init_rwsem(&parent->unreg_sem);
163
164 parent->dev = dev;
165 parent->ops = ops;
166
167 if (!mdev_bus_compat_class) {
168 mdev_bus_compat_class = class_compat_register("mdev_bus");
169 if (!mdev_bus_compat_class) {
170 ret = -ENOMEM;
171 goto add_dev_err;
172 }
173 }
174
175 ret = parent_create_sysfs_files(parent);
176 if (ret)
177 goto add_dev_err;
178
179 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
180 if (ret)
181 dev_warn(dev, "Failed to create compatibility class link\n");
182
183 list_add(&parent->next, &parent_list);
184 mutex_unlock(&parent_list_lock);
185
186 dev_info(dev, "MDEV: Registered\n");
187 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
188
189 return 0;
190
191add_dev_err:
192 mutex_unlock(&parent_list_lock);
193 if (parent)
194 mdev_put_parent(parent);
195 else
196 put_device(dev);
197 return ret;
198}
199EXPORT_SYMBOL(mdev_register_device);
200
201/*
202 * mdev_unregister_device : Unregister a parent device
203 * @dev: device structure representing parent device.
204 *
205 * Remove device from list of registered parent devices. Give a chance to free
206 * existing mediated devices for given device.
207 */
208
209void mdev_unregister_device(struct device *dev)
210{
211 struct mdev_parent *parent;
212 char *env_string = "MDEV_STATE=unregistered";
213 char *envp[] = { env_string, NULL };
214
215 mutex_lock(&parent_list_lock);
216 parent = __find_parent_device(dev);
217
218 if (!parent) {
219 mutex_unlock(&parent_list_lock);
220 return;
221 }
222 dev_info(dev, "MDEV: Unregistering\n");
223
224 list_del(&parent->next);
225 mutex_unlock(&parent_list_lock);
226
227 down_write(&parent->unreg_sem);
228
229 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
230
231 device_for_each_child(dev, NULL, mdev_device_remove_cb);
232
233 parent_remove_sysfs_files(parent);
234 up_write(&parent->unreg_sem);
235
236 mdev_put_parent(parent);
237
238 /* We still have the caller's reference to use for the uevent */
239 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
240}
241EXPORT_SYMBOL(mdev_unregister_device);
242
243static void mdev_device_release(struct device *dev)
244{
245 struct mdev_device *mdev = to_mdev_device(dev);
246
247 /* Pairs with the get in mdev_device_create() */
248 kobject_put(&mdev->type->kobj);
249
250 mutex_lock(&mdev_list_lock);
251 list_del(&mdev->next);
252 mutex_unlock(&mdev_list_lock);
253
254 dev_dbg(&mdev->dev, "MDEV: destroying\n");
255 kfree(mdev);
256}
257
258int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
259{
260 int ret;
261 struct mdev_device *mdev, *tmp;
262 struct mdev_parent *parent = type->parent;
263 struct mdev_driver *drv = parent->ops->device_driver;
264
265 mutex_lock(&mdev_list_lock);
266
267 /* Check for duplicate */
268 list_for_each_entry(tmp, &mdev_list, next) {
269 if (guid_equal(&tmp->uuid, uuid)) {
270 mutex_unlock(&mdev_list_lock);
271 return -EEXIST;
272 }
273 }
274
275 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
276 if (!mdev) {
277 mutex_unlock(&mdev_list_lock);
278 return -ENOMEM;
279 }
280
281 device_initialize(&mdev->dev);
282 mdev->dev.parent = parent->dev;
283 mdev->dev.bus = &mdev_bus_type;
284 mdev->dev.release = mdev_device_release;
285 mdev->dev.groups = parent->ops->mdev_attr_groups;
286 mdev->type = type;
287 /* Pairs with the put in mdev_device_release() */
288 kobject_get(&type->kobj);
289
290 guid_copy(&mdev->uuid, uuid);
291 list_add(&mdev->next, &mdev_list);
292 mutex_unlock(&mdev_list_lock);
293
294 ret = dev_set_name(&mdev->dev, "%pUl", uuid);
295 if (ret)
296 goto out_put_device;
297
298 /* Check if parent unregistration has started */
299 if (!down_read_trylock(&parent->unreg_sem)) {
300 ret = -ENODEV;
301 goto out_put_device;
302 }
303
304 if (parent->ops->create) {
305 ret = parent->ops->create(mdev);
306 if (ret)
307 goto out_unlock;
308 }
309
310 ret = device_add(&mdev->dev);
311 if (ret)
312 goto out_remove;
313
314 if (!drv)
315 drv = &vfio_mdev_driver;
316 ret = device_driver_attach(&drv->driver, &mdev->dev);
317 if (ret)
318 goto out_del;
319
320 ret = mdev_create_sysfs_files(mdev);
321 if (ret)
322 goto out_del;
323
324 mdev->active = true;
325 dev_dbg(&mdev->dev, "MDEV: created\n");
326 up_read(&parent->unreg_sem);
327
328 return 0;
329
330out_del:
331 device_del(&mdev->dev);
332out_remove:
333 if (parent->ops->remove)
334 parent->ops->remove(mdev);
335out_unlock:
336 up_read(&parent->unreg_sem);
337out_put_device:
338 put_device(&mdev->dev);
339 return ret;
340}
341
342int mdev_device_remove(struct mdev_device *mdev)
343{
344 struct mdev_device *tmp;
345 struct mdev_parent *parent = mdev->type->parent;
346
347 mutex_lock(&mdev_list_lock);
348 list_for_each_entry(tmp, &mdev_list, next) {
349 if (tmp == mdev)
350 break;
351 }
352
353 if (tmp != mdev) {
354 mutex_unlock(&mdev_list_lock);
355 return -ENODEV;
356 }
357
358 if (!mdev->active) {
359 mutex_unlock(&mdev_list_lock);
360 return -EAGAIN;
361 }
362
363 mdev->active = false;
364 mutex_unlock(&mdev_list_lock);
365
366 /* Check if parent unregistration has started */
367 if (!down_read_trylock(&parent->unreg_sem))
368 return -ENODEV;
369
370 mdev_device_remove_common(mdev);
371 up_read(&parent->unreg_sem);
372 return 0;
373}
374
375static int __init mdev_init(void)
376{
377 int rc;
378
379 rc = mdev_bus_register();
380 if (rc)
381 return rc;
382 rc = mdev_register_driver(&vfio_mdev_driver);
383 if (rc)
384 goto err_bus;
385 return 0;
386err_bus:
387 mdev_bus_unregister();
388 return rc;
389}
390
391static void __exit mdev_exit(void)
392{
393 mdev_unregister_driver(&vfio_mdev_driver);
394
395 if (mdev_bus_compat_class)
396 class_compat_unregister(mdev_bus_compat_class);
397
398 mdev_bus_unregister();
399}
400
401module_init(mdev_init)
402module_exit(mdev_exit)
403
404MODULE_VERSION(DRIVER_VERSION);
405MODULE_LICENSE("GPL v2");
406MODULE_AUTHOR(DRIVER_AUTHOR);
407MODULE_DESCRIPTION(DRIVER_DESC);