Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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->parent->dev;
33}
34EXPORT_SYMBOL(mdev_parent_dev);
35
36void *mdev_get_drvdata(struct mdev_device *mdev)
37{
38 return mdev->driver_data;
39}
40EXPORT_SYMBOL(mdev_get_drvdata);
41
42void mdev_set_drvdata(struct mdev_device *mdev, void *data)
43{
44 mdev->driver_data = data;
45}
46EXPORT_SYMBOL(mdev_set_drvdata);
47
48struct device *mdev_dev(struct mdev_device *mdev)
49{
50 return &mdev->dev;
51}
52EXPORT_SYMBOL(mdev_dev);
53
54struct mdev_device *mdev_from_dev(struct device *dev)
55{
56 return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
57}
58EXPORT_SYMBOL(mdev_from_dev);
59
60const guid_t *mdev_uuid(struct mdev_device *mdev)
61{
62 return &mdev->uuid;
63}
64EXPORT_SYMBOL(mdev_uuid);
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
78static void 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
88static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
89{
90 if (parent)
91 kref_get(&parent->ref);
92
93 return parent;
94}
95
96static void mdev_put_parent(struct mdev_parent *parent)
97{
98 if (parent)
99 kref_put(&parent->ref, mdev_release_parent);
100}
101
102/* Caller must hold parent unreg_sem read or write lock */
103static void mdev_device_remove_common(struct mdev_device *mdev)
104{
105 struct mdev_parent *parent;
106 struct mdev_type *type;
107 int ret;
108
109 type = to_mdev_type(mdev->type_kobj);
110 mdev_remove_sysfs_files(&mdev->dev, type);
111 device_del(&mdev->dev);
112 parent = mdev->parent;
113 lockdep_assert_held(&parent->unreg_sem);
114 ret = parent->ops->remove(mdev);
115 if (ret)
116 dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
117
118 /* Balances with device_initialize() */
119 put_device(&mdev->dev);
120 mdev_put_parent(parent);
121}
122
123static int mdev_device_remove_cb(struct device *dev, void *data)
124{
125 if (dev_is_mdev(dev)) {
126 struct mdev_device *mdev;
127
128 mdev = to_mdev_device(dev);
129 mdev_device_remove_common(mdev);
130 }
131 return 0;
132}
133
134/*
135 * mdev_register_device : Register a device
136 * @dev: device structure representing parent device.
137 * @ops: Parent device operation structure to be registered.
138 *
139 * Add device to list of registered parent devices.
140 * Returns a negative value on error, otherwise 0.
141 */
142int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
143{
144 int ret;
145 struct mdev_parent *parent;
146 char *env_string = "MDEV_STATE=registered";
147 char *envp[] = { env_string, NULL };
148
149 /* check for mandatory ops */
150 if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
151 return -EINVAL;
152
153 dev = get_device(dev);
154 if (!dev)
155 return -EINVAL;
156
157 /* Not mandatory, but its absence could be a problem */
158 if (!ops->request)
159 dev_info(dev, "Driver cannot be asked to release device\n");
160
161 mutex_lock(&parent_list_lock);
162
163 /* Check for duplicate */
164 parent = __find_parent_device(dev);
165 if (parent) {
166 parent = NULL;
167 ret = -EEXIST;
168 goto add_dev_err;
169 }
170
171 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
172 if (!parent) {
173 ret = -ENOMEM;
174 goto add_dev_err;
175 }
176
177 kref_init(&parent->ref);
178 init_rwsem(&parent->unreg_sem);
179
180 parent->dev = dev;
181 parent->ops = ops;
182
183 if (!mdev_bus_compat_class) {
184 mdev_bus_compat_class = class_compat_register("mdev_bus");
185 if (!mdev_bus_compat_class) {
186 ret = -ENOMEM;
187 goto add_dev_err;
188 }
189 }
190
191 ret = parent_create_sysfs_files(parent);
192 if (ret)
193 goto add_dev_err;
194
195 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
196 if (ret)
197 dev_warn(dev, "Failed to create compatibility class link\n");
198
199 list_add(&parent->next, &parent_list);
200 mutex_unlock(&parent_list_lock);
201
202 dev_info(dev, "MDEV: Registered\n");
203 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
204
205 return 0;
206
207add_dev_err:
208 mutex_unlock(&parent_list_lock);
209 if (parent)
210 mdev_put_parent(parent);
211 else
212 put_device(dev);
213 return ret;
214}
215EXPORT_SYMBOL(mdev_register_device);
216
217/*
218 * mdev_unregister_device : Unregister a parent device
219 * @dev: device structure representing parent device.
220 *
221 * Remove device from list of registered parent devices. Give a chance to free
222 * existing mediated devices for given device.
223 */
224
225void mdev_unregister_device(struct device *dev)
226{
227 struct mdev_parent *parent;
228 char *env_string = "MDEV_STATE=unregistered";
229 char *envp[] = { env_string, NULL };
230
231 mutex_lock(&parent_list_lock);
232 parent = __find_parent_device(dev);
233
234 if (!parent) {
235 mutex_unlock(&parent_list_lock);
236 return;
237 }
238 dev_info(dev, "MDEV: Unregistering\n");
239
240 list_del(&parent->next);
241 mutex_unlock(&parent_list_lock);
242
243 down_write(&parent->unreg_sem);
244
245 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
246
247 device_for_each_child(dev, NULL, mdev_device_remove_cb);
248
249 parent_remove_sysfs_files(parent);
250 up_write(&parent->unreg_sem);
251
252 mdev_put_parent(parent);
253
254 /* We still have the caller's reference to use for the uevent */
255 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
256}
257EXPORT_SYMBOL(mdev_unregister_device);
258
259static void mdev_device_free(struct mdev_device *mdev)
260{
261 mutex_lock(&mdev_list_lock);
262 list_del(&mdev->next);
263 mutex_unlock(&mdev_list_lock);
264
265 dev_dbg(&mdev->dev, "MDEV: destroying\n");
266 kfree(mdev);
267}
268
269static void mdev_device_release(struct device *dev)
270{
271 struct mdev_device *mdev = to_mdev_device(dev);
272
273 mdev_device_free(mdev);
274}
275
276int mdev_device_create(struct kobject *kobj,
277 struct device *dev, const guid_t *uuid)
278{
279 int ret;
280 struct mdev_device *mdev, *tmp;
281 struct mdev_parent *parent;
282 struct mdev_type *type = to_mdev_type(kobj);
283
284 parent = mdev_get_parent(type->parent);
285 if (!parent)
286 return -EINVAL;
287
288 mutex_lock(&mdev_list_lock);
289
290 /* Check for duplicate */
291 list_for_each_entry(tmp, &mdev_list, next) {
292 if (guid_equal(&tmp->uuid, uuid)) {
293 mutex_unlock(&mdev_list_lock);
294 ret = -EEXIST;
295 goto mdev_fail;
296 }
297 }
298
299 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
300 if (!mdev) {
301 mutex_unlock(&mdev_list_lock);
302 ret = -ENOMEM;
303 goto mdev_fail;
304 }
305
306 guid_copy(&mdev->uuid, uuid);
307 list_add(&mdev->next, &mdev_list);
308 mutex_unlock(&mdev_list_lock);
309
310 mdev->parent = parent;
311
312 /* Check if parent unregistration has started */
313 if (!down_read_trylock(&parent->unreg_sem)) {
314 mdev_device_free(mdev);
315 ret = -ENODEV;
316 goto mdev_fail;
317 }
318
319 device_initialize(&mdev->dev);
320 mdev->dev.parent = dev;
321 mdev->dev.bus = &mdev_bus_type;
322 mdev->dev.release = mdev_device_release;
323 dev_set_name(&mdev->dev, "%pUl", uuid);
324 mdev->dev.groups = parent->ops->mdev_attr_groups;
325 mdev->type_kobj = kobj;
326
327 ret = parent->ops->create(kobj, mdev);
328 if (ret)
329 goto ops_create_fail;
330
331 ret = device_add(&mdev->dev);
332 if (ret)
333 goto add_fail;
334
335 ret = mdev_create_sysfs_files(&mdev->dev, type);
336 if (ret)
337 goto sysfs_fail;
338
339 mdev->active = true;
340 dev_dbg(&mdev->dev, "MDEV: created\n");
341 up_read(&parent->unreg_sem);
342
343 return 0;
344
345sysfs_fail:
346 device_del(&mdev->dev);
347add_fail:
348 parent->ops->remove(mdev);
349ops_create_fail:
350 up_read(&parent->unreg_sem);
351 put_device(&mdev->dev);
352mdev_fail:
353 mdev_put_parent(parent);
354 return ret;
355}
356
357int mdev_device_remove(struct device *dev)
358{
359 struct mdev_device *mdev, *tmp;
360 struct mdev_parent *parent;
361
362 mdev = to_mdev_device(dev);
363
364 mutex_lock(&mdev_list_lock);
365 list_for_each_entry(tmp, &mdev_list, next) {
366 if (tmp == mdev)
367 break;
368 }
369
370 if (tmp != mdev) {
371 mutex_unlock(&mdev_list_lock);
372 return -ENODEV;
373 }
374
375 if (!mdev->active) {
376 mutex_unlock(&mdev_list_lock);
377 return -EAGAIN;
378 }
379
380 mdev->active = false;
381 mutex_unlock(&mdev_list_lock);
382
383 parent = mdev->parent;
384 /* Check if parent unregistration has started */
385 if (!down_read_trylock(&parent->unreg_sem))
386 return -ENODEV;
387
388 mdev_device_remove_common(mdev);
389 up_read(&parent->unreg_sem);
390 return 0;
391}
392
393int mdev_set_iommu_device(struct device *dev, struct device *iommu_device)
394{
395 struct mdev_device *mdev = to_mdev_device(dev);
396
397 mdev->iommu_device = iommu_device;
398
399 return 0;
400}
401EXPORT_SYMBOL(mdev_set_iommu_device);
402
403struct device *mdev_get_iommu_device(struct device *dev)
404{
405 struct mdev_device *mdev = to_mdev_device(dev);
406
407 return mdev->iommu_device;
408}
409EXPORT_SYMBOL(mdev_get_iommu_device);
410
411static int __init mdev_init(void)
412{
413 return mdev_bus_register();
414}
415
416static void __exit mdev_exit(void)
417{
418 if (mdev_bus_compat_class)
419 class_compat_unregister(mdev_bus_compat_class);
420
421 mdev_bus_unregister();
422}
423
424module_init(mdev_init)
425module_exit(mdev_exit)
426
427MODULE_VERSION(DRIVER_VERSION);
428MODULE_LICENSE("GPL v2");
429MODULE_AUTHOR(DRIVER_AUTHOR);
430MODULE_DESCRIPTION(DRIVER_DESC);
431MODULE_SOFTDEP("post: vfio_mdev");