Merge tag 'armsoc-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[sfrench/cifs-2.6.git] / drivers / vfio / mdev / mdev_core.c
1 /*
2  * Mediated device Core Driver
3  *
4  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5  *     Author: Neo Jia <cjia@nvidia.com>
6  *             Kirti Wankhede <kwankhede@nvidia.com>
7  *
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.
11  */
12
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>
19
20 #include "mdev_private.h"
21
22 #define DRIVER_VERSION          "0.1"
23 #define DRIVER_AUTHOR           "NVIDIA Corporation"
24 #define DRIVER_DESC             "Mediated device Core Driver"
25
26 static LIST_HEAD(parent_list);
27 static DEFINE_MUTEX(parent_list_lock);
28 static struct class_compat *mdev_bus_compat_class;
29
30 static LIST_HEAD(mdev_list);
31 static DEFINE_MUTEX(mdev_list_lock);
32
33 struct device *mdev_parent_dev(struct mdev_device *mdev)
34 {
35         return mdev->parent->dev;
36 }
37 EXPORT_SYMBOL(mdev_parent_dev);
38
39 void *mdev_get_drvdata(struct mdev_device *mdev)
40 {
41         return mdev->driver_data;
42 }
43 EXPORT_SYMBOL(mdev_get_drvdata);
44
45 void mdev_set_drvdata(struct mdev_device *mdev, void *data)
46 {
47         mdev->driver_data = data;
48 }
49 EXPORT_SYMBOL(mdev_set_drvdata);
50
51 struct device *mdev_dev(struct mdev_device *mdev)
52 {
53         return &mdev->dev;
54 }
55 EXPORT_SYMBOL(mdev_dev);
56
57 struct mdev_device *mdev_from_dev(struct device *dev)
58 {
59         return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
60 }
61 EXPORT_SYMBOL(mdev_from_dev);
62
63 const guid_t *mdev_uuid(struct mdev_device *mdev)
64 {
65         return &mdev->uuid;
66 }
67 EXPORT_SYMBOL(mdev_uuid);
68
69 /* Should be called holding parent_list_lock */
70 static struct mdev_parent *__find_parent_device(struct device *dev)
71 {
72         struct mdev_parent *parent;
73
74         list_for_each_entry(parent, &parent_list, next) {
75                 if (parent->dev == dev)
76                         return parent;
77         }
78         return NULL;
79 }
80
81 static void mdev_release_parent(struct kref *kref)
82 {
83         struct mdev_parent *parent = container_of(kref, struct mdev_parent,
84                                                   ref);
85         struct device *dev = parent->dev;
86
87         kfree(parent);
88         put_device(dev);
89 }
90
91 static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
92 {
93         if (parent)
94                 kref_get(&parent->ref);
95
96         return parent;
97 }
98
99 static void mdev_put_parent(struct mdev_parent *parent)
100 {
101         if (parent)
102                 kref_put(&parent->ref, mdev_release_parent);
103 }
104
105 static int mdev_device_create_ops(struct kobject *kobj,
106                                   struct mdev_device *mdev)
107 {
108         struct mdev_parent *parent = mdev->parent;
109         int ret;
110
111         ret = parent->ops->create(kobj, mdev);
112         if (ret)
113                 return ret;
114
115         ret = sysfs_create_groups(&mdev->dev.kobj,
116                                   parent->ops->mdev_attr_groups);
117         if (ret)
118                 parent->ops->remove(mdev);
119
120         return ret;
121 }
122
123 /*
124  * mdev_device_remove_ops gets called from sysfs's 'remove' and when parent
125  * device is being unregistered from mdev device framework.
126  * - 'force_remove' is set to 'false' when called from sysfs's 'remove' which
127  *   indicates that if the mdev device is active, used by VMM or userspace
128  *   application, vendor driver could return error then don't remove the device.
129  * - 'force_remove' is set to 'true' when called from mdev_unregister_device()
130  *   which indicate that parent device is being removed from mdev device
131  *   framework so remove mdev device forcefully.
132  */
133 static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
134 {
135         struct mdev_parent *parent = mdev->parent;
136         int ret;
137
138         /*
139          * Vendor driver can return error if VMM or userspace application is
140          * using this mdev device.
141          */
142         ret = parent->ops->remove(mdev);
143         if (ret && !force_remove)
144                 return ret;
145
146         sysfs_remove_groups(&mdev->dev.kobj, parent->ops->mdev_attr_groups);
147         return 0;
148 }
149
150 static int mdev_device_remove_cb(struct device *dev, void *data)
151 {
152         if (dev_is_mdev(dev))
153                 mdev_device_remove(dev, true);
154
155         return 0;
156 }
157
158 /*
159  * mdev_register_device : Register a device
160  * @dev: device structure representing parent device.
161  * @ops: Parent device operation structure to be registered.
162  *
163  * Add device to list of registered parent devices.
164  * Returns a negative value on error, otherwise 0.
165  */
166 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
167 {
168         int ret;
169         struct mdev_parent *parent;
170
171         /* check for mandatory ops */
172         if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
173                 return -EINVAL;
174
175         dev = get_device(dev);
176         if (!dev)
177                 return -EINVAL;
178
179         mutex_lock(&parent_list_lock);
180
181         /* Check for duplicate */
182         parent = __find_parent_device(dev);
183         if (parent) {
184                 parent = NULL;
185                 ret = -EEXIST;
186                 goto add_dev_err;
187         }
188
189         parent = kzalloc(sizeof(*parent), GFP_KERNEL);
190         if (!parent) {
191                 ret = -ENOMEM;
192                 goto add_dev_err;
193         }
194
195         kref_init(&parent->ref);
196
197         parent->dev = dev;
198         parent->ops = ops;
199
200         if (!mdev_bus_compat_class) {
201                 mdev_bus_compat_class = class_compat_register("mdev_bus");
202                 if (!mdev_bus_compat_class) {
203                         ret = -ENOMEM;
204                         goto add_dev_err;
205                 }
206         }
207
208         ret = parent_create_sysfs_files(parent);
209         if (ret)
210                 goto add_dev_err;
211
212         ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
213         if (ret)
214                 dev_warn(dev, "Failed to create compatibility class link\n");
215
216         list_add(&parent->next, &parent_list);
217         mutex_unlock(&parent_list_lock);
218
219         dev_info(dev, "MDEV: Registered\n");
220         return 0;
221
222 add_dev_err:
223         mutex_unlock(&parent_list_lock);
224         if (parent)
225                 mdev_put_parent(parent);
226         else
227                 put_device(dev);
228         return ret;
229 }
230 EXPORT_SYMBOL(mdev_register_device);
231
232 /*
233  * mdev_unregister_device : Unregister a parent device
234  * @dev: device structure representing parent device.
235  *
236  * Remove device from list of registered parent devices. Give a chance to free
237  * existing mediated devices for given device.
238  */
239
240 void mdev_unregister_device(struct device *dev)
241 {
242         struct mdev_parent *parent;
243
244         mutex_lock(&parent_list_lock);
245         parent = __find_parent_device(dev);
246
247         if (!parent) {
248                 mutex_unlock(&parent_list_lock);
249                 return;
250         }
251         dev_info(dev, "MDEV: Unregistering\n");
252
253         list_del(&parent->next);
254         class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
255
256         device_for_each_child(dev, NULL, mdev_device_remove_cb);
257
258         parent_remove_sysfs_files(parent);
259
260         mutex_unlock(&parent_list_lock);
261         mdev_put_parent(parent);
262 }
263 EXPORT_SYMBOL(mdev_unregister_device);
264
265 static void mdev_device_release(struct device *dev)
266 {
267         struct mdev_device *mdev = to_mdev_device(dev);
268
269         mutex_lock(&mdev_list_lock);
270         list_del(&mdev->next);
271         mutex_unlock(&mdev_list_lock);
272
273         dev_dbg(&mdev->dev, "MDEV: destroying\n");
274         kfree(mdev);
275 }
276
277 int mdev_device_create(struct kobject *kobj,
278                        struct device *dev, const guid_t *uuid)
279 {
280         int ret;
281         struct mdev_device *mdev, *tmp;
282         struct mdev_parent *parent;
283         struct mdev_type *type = to_mdev_type(kobj);
284
285         parent = mdev_get_parent(type->parent);
286         if (!parent)
287                 return -EINVAL;
288
289         mutex_lock(&mdev_list_lock);
290
291         /* Check for duplicate */
292         list_for_each_entry(tmp, &mdev_list, next) {
293                 if (guid_equal(&tmp->uuid, uuid)) {
294                         mutex_unlock(&mdev_list_lock);
295                         ret = -EEXIST;
296                         goto mdev_fail;
297                 }
298         }
299
300         mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
301         if (!mdev) {
302                 mutex_unlock(&mdev_list_lock);
303                 ret = -ENOMEM;
304                 goto mdev_fail;
305         }
306
307         guid_copy(&mdev->uuid, uuid);
308         list_add(&mdev->next, &mdev_list);
309         mutex_unlock(&mdev_list_lock);
310
311         mdev->parent = parent;
312
313         mdev->dev.parent  = dev;
314         mdev->dev.bus     = &mdev_bus_type;
315         mdev->dev.release = mdev_device_release;
316         dev_set_name(&mdev->dev, "%pUl", uuid);
317
318         ret = device_register(&mdev->dev);
319         if (ret) {
320                 put_device(&mdev->dev);
321                 goto mdev_fail;
322         }
323
324         ret = mdev_device_create_ops(kobj, mdev);
325         if (ret)
326                 goto create_fail;
327
328         ret = mdev_create_sysfs_files(&mdev->dev, type);
329         if (ret) {
330                 mdev_device_remove_ops(mdev, true);
331                 goto create_fail;
332         }
333
334         mdev->type_kobj = kobj;
335         mdev->active = true;
336         dev_dbg(&mdev->dev, "MDEV: created\n");
337
338         return 0;
339
340 create_fail:
341         device_unregister(&mdev->dev);
342 mdev_fail:
343         mdev_put_parent(parent);
344         return ret;
345 }
346
347 int mdev_device_remove(struct device *dev, bool force_remove)
348 {
349         struct mdev_device *mdev, *tmp;
350         struct mdev_parent *parent;
351         struct mdev_type *type;
352         int ret;
353
354         mdev = to_mdev_device(dev);
355
356         mutex_lock(&mdev_list_lock);
357         list_for_each_entry(tmp, &mdev_list, next) {
358                 if (tmp == mdev)
359                         break;
360         }
361
362         if (tmp != mdev) {
363                 mutex_unlock(&mdev_list_lock);
364                 return -ENODEV;
365         }
366
367         if (!mdev->active) {
368                 mutex_unlock(&mdev_list_lock);
369                 return -EAGAIN;
370         }
371
372         mdev->active = false;
373         mutex_unlock(&mdev_list_lock);
374
375         type = to_mdev_type(mdev->type_kobj);
376         parent = mdev->parent;
377
378         ret = mdev_device_remove_ops(mdev, force_remove);
379         if (ret) {
380                 mdev->active = true;
381                 return ret;
382         }
383
384         mdev_remove_sysfs_files(dev, type);
385         device_unregister(dev);
386         mdev_put_parent(parent);
387
388         return 0;
389 }
390
391 int mdev_set_iommu_device(struct device *dev, struct device *iommu_device)
392 {
393         struct mdev_device *mdev = to_mdev_device(dev);
394
395         mdev->iommu_device = iommu_device;
396
397         return 0;
398 }
399 EXPORT_SYMBOL(mdev_set_iommu_device);
400
401 struct device *mdev_get_iommu_device(struct device *dev)
402 {
403         struct mdev_device *mdev = to_mdev_device(dev);
404
405         return mdev->iommu_device;
406 }
407 EXPORT_SYMBOL(mdev_get_iommu_device);
408
409 static int __init mdev_init(void)
410 {
411         return mdev_bus_register();
412 }
413
414 static void __exit mdev_exit(void)
415 {
416         if (mdev_bus_compat_class)
417                 class_compat_unregister(mdev_bus_compat_class);
418
419         mdev_bus_unregister();
420 }
421
422 module_init(mdev_init)
423 module_exit(mdev_exit)
424
425 MODULE_VERSION(DRIVER_VERSION);
426 MODULE_LICENSE("GPL v2");
427 MODULE_AUTHOR(DRIVER_AUTHOR);
428 MODULE_DESCRIPTION(DRIVER_DESC);
429 MODULE_SOFTDEP("post: vfio_mdev");