xref: /openbmc/linux/drivers/vfio/mdev/mdev_core.c (revision 4e95bc26)
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 
23 static LIST_HEAD(parent_list);
24 static DEFINE_MUTEX(parent_list_lock);
25 static struct class_compat *mdev_bus_compat_class;
26 
27 static LIST_HEAD(mdev_list);
28 static DEFINE_MUTEX(mdev_list_lock);
29 
30 struct device *mdev_parent_dev(struct mdev_device *mdev)
31 {
32 	return mdev->parent->dev;
33 }
34 EXPORT_SYMBOL(mdev_parent_dev);
35 
36 void *mdev_get_drvdata(struct mdev_device *mdev)
37 {
38 	return mdev->driver_data;
39 }
40 EXPORT_SYMBOL(mdev_get_drvdata);
41 
42 void mdev_set_drvdata(struct mdev_device *mdev, void *data)
43 {
44 	mdev->driver_data = data;
45 }
46 EXPORT_SYMBOL(mdev_set_drvdata);
47 
48 struct device *mdev_dev(struct mdev_device *mdev)
49 {
50 	return &mdev->dev;
51 }
52 EXPORT_SYMBOL(mdev_dev);
53 
54 struct mdev_device *mdev_from_dev(struct device *dev)
55 {
56 	return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
57 }
58 EXPORT_SYMBOL(mdev_from_dev);
59 
60 const guid_t *mdev_uuid(struct mdev_device *mdev)
61 {
62 	return &mdev->uuid;
63 }
64 EXPORT_SYMBOL(mdev_uuid);
65 
66 /* Should be called holding parent_list_lock */
67 static 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 
78 static 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 
88 static 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 
96 static 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 */
103 static 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 
123 static 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  */
142 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
143 {
144 	int ret;
145 	struct mdev_parent *parent;
146 
147 	/* check for mandatory ops */
148 	if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
149 		return -EINVAL;
150 
151 	dev = get_device(dev);
152 	if (!dev)
153 		return -EINVAL;
154 
155 	mutex_lock(&parent_list_lock);
156 
157 	/* Check for duplicate */
158 	parent = __find_parent_device(dev);
159 	if (parent) {
160 		parent = NULL;
161 		ret = -EEXIST;
162 		goto add_dev_err;
163 	}
164 
165 	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
166 	if (!parent) {
167 		ret = -ENOMEM;
168 		goto add_dev_err;
169 	}
170 
171 	kref_init(&parent->ref);
172 	init_rwsem(&parent->unreg_sem);
173 
174 	parent->dev = dev;
175 	parent->ops = ops;
176 
177 	if (!mdev_bus_compat_class) {
178 		mdev_bus_compat_class = class_compat_register("mdev_bus");
179 		if (!mdev_bus_compat_class) {
180 			ret = -ENOMEM;
181 			goto add_dev_err;
182 		}
183 	}
184 
185 	ret = parent_create_sysfs_files(parent);
186 	if (ret)
187 		goto add_dev_err;
188 
189 	ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
190 	if (ret)
191 		dev_warn(dev, "Failed to create compatibility class link\n");
192 
193 	list_add(&parent->next, &parent_list);
194 	mutex_unlock(&parent_list_lock);
195 
196 	dev_info(dev, "MDEV: Registered\n");
197 	return 0;
198 
199 add_dev_err:
200 	mutex_unlock(&parent_list_lock);
201 	if (parent)
202 		mdev_put_parent(parent);
203 	else
204 		put_device(dev);
205 	return ret;
206 }
207 EXPORT_SYMBOL(mdev_register_device);
208 
209 /*
210  * mdev_unregister_device : Unregister a parent device
211  * @dev: device structure representing parent device.
212  *
213  * Remove device from list of registered parent devices. Give a chance to free
214  * existing mediated devices for given device.
215  */
216 
217 void mdev_unregister_device(struct device *dev)
218 {
219 	struct mdev_parent *parent;
220 
221 	mutex_lock(&parent_list_lock);
222 	parent = __find_parent_device(dev);
223 
224 	if (!parent) {
225 		mutex_unlock(&parent_list_lock);
226 		return;
227 	}
228 	dev_info(dev, "MDEV: Unregistering\n");
229 
230 	list_del(&parent->next);
231 	mutex_unlock(&parent_list_lock);
232 
233 	down_write(&parent->unreg_sem);
234 
235 	class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
236 
237 	device_for_each_child(dev, NULL, mdev_device_remove_cb);
238 
239 	parent_remove_sysfs_files(parent);
240 	up_write(&parent->unreg_sem);
241 
242 	mdev_put_parent(parent);
243 }
244 EXPORT_SYMBOL(mdev_unregister_device);
245 
246 static void mdev_device_free(struct mdev_device *mdev)
247 {
248 	mutex_lock(&mdev_list_lock);
249 	list_del(&mdev->next);
250 	mutex_unlock(&mdev_list_lock);
251 
252 	dev_dbg(&mdev->dev, "MDEV: destroying\n");
253 	kfree(mdev);
254 }
255 
256 static void mdev_device_release(struct device *dev)
257 {
258 	struct mdev_device *mdev = to_mdev_device(dev);
259 
260 	mdev_device_free(mdev);
261 }
262 
263 int mdev_device_create(struct kobject *kobj,
264 		       struct device *dev, const guid_t *uuid)
265 {
266 	int ret;
267 	struct mdev_device *mdev, *tmp;
268 	struct mdev_parent *parent;
269 	struct mdev_type *type = to_mdev_type(kobj);
270 
271 	parent = mdev_get_parent(type->parent);
272 	if (!parent)
273 		return -EINVAL;
274 
275 	mutex_lock(&mdev_list_lock);
276 
277 	/* Check for duplicate */
278 	list_for_each_entry(tmp, &mdev_list, next) {
279 		if (guid_equal(&tmp->uuid, uuid)) {
280 			mutex_unlock(&mdev_list_lock);
281 			ret = -EEXIST;
282 			goto mdev_fail;
283 		}
284 	}
285 
286 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
287 	if (!mdev) {
288 		mutex_unlock(&mdev_list_lock);
289 		ret = -ENOMEM;
290 		goto mdev_fail;
291 	}
292 
293 	guid_copy(&mdev->uuid, uuid);
294 	list_add(&mdev->next, &mdev_list);
295 	mutex_unlock(&mdev_list_lock);
296 
297 	mdev->parent = parent;
298 
299 	/* Check if parent unregistration has started */
300 	if (!down_read_trylock(&parent->unreg_sem)) {
301 		mdev_device_free(mdev);
302 		ret = -ENODEV;
303 		goto mdev_fail;
304 	}
305 
306 	device_initialize(&mdev->dev);
307 	mdev->dev.parent  = dev;
308 	mdev->dev.bus     = &mdev_bus_type;
309 	mdev->dev.release = mdev_device_release;
310 	dev_set_name(&mdev->dev, "%pUl", uuid);
311 	mdev->dev.groups = parent->ops->mdev_attr_groups;
312 	mdev->type_kobj = kobj;
313 
314 	ret = parent->ops->create(kobj, mdev);
315 	if (ret)
316 		goto ops_create_fail;
317 
318 	ret = device_add(&mdev->dev);
319 	if (ret)
320 		goto add_fail;
321 
322 	ret = mdev_create_sysfs_files(&mdev->dev, type);
323 	if (ret)
324 		goto sysfs_fail;
325 
326 	mdev->active = true;
327 	dev_dbg(&mdev->dev, "MDEV: created\n");
328 	up_read(&parent->unreg_sem);
329 
330 	return 0;
331 
332 sysfs_fail:
333 	device_del(&mdev->dev);
334 add_fail:
335 	parent->ops->remove(mdev);
336 ops_create_fail:
337 	up_read(&parent->unreg_sem);
338 	put_device(&mdev->dev);
339 mdev_fail:
340 	mdev_put_parent(parent);
341 	return ret;
342 }
343 
344 int mdev_device_remove(struct device *dev)
345 {
346 	struct mdev_device *mdev, *tmp;
347 	struct mdev_parent *parent;
348 
349 	mdev = to_mdev_device(dev);
350 
351 	mutex_lock(&mdev_list_lock);
352 	list_for_each_entry(tmp, &mdev_list, next) {
353 		if (tmp == mdev)
354 			break;
355 	}
356 
357 	if (tmp != mdev) {
358 		mutex_unlock(&mdev_list_lock);
359 		return -ENODEV;
360 	}
361 
362 	if (!mdev->active) {
363 		mutex_unlock(&mdev_list_lock);
364 		return -EAGAIN;
365 	}
366 
367 	mdev->active = false;
368 	mutex_unlock(&mdev_list_lock);
369 
370 	parent = mdev->parent;
371 	/* Check if parent unregistration has started */
372 	if (!down_read_trylock(&parent->unreg_sem))
373 		return -ENODEV;
374 
375 	mdev_device_remove_common(mdev);
376 	up_read(&parent->unreg_sem);
377 	return 0;
378 }
379 
380 int mdev_set_iommu_device(struct device *dev, struct device *iommu_device)
381 {
382 	struct mdev_device *mdev = to_mdev_device(dev);
383 
384 	mdev->iommu_device = iommu_device;
385 
386 	return 0;
387 }
388 EXPORT_SYMBOL(mdev_set_iommu_device);
389 
390 struct device *mdev_get_iommu_device(struct device *dev)
391 {
392 	struct mdev_device *mdev = to_mdev_device(dev);
393 
394 	return mdev->iommu_device;
395 }
396 EXPORT_SYMBOL(mdev_get_iommu_device);
397 
398 static int __init mdev_init(void)
399 {
400 	return mdev_bus_register();
401 }
402 
403 static void __exit mdev_exit(void)
404 {
405 	if (mdev_bus_compat_class)
406 		class_compat_unregister(mdev_bus_compat_class);
407 
408 	mdev_bus_unregister();
409 }
410 
411 module_init(mdev_init)
412 module_exit(mdev_exit)
413 
414 MODULE_VERSION(DRIVER_VERSION);
415 MODULE_LICENSE("GPL v2");
416 MODULE_AUTHOR(DRIVER_AUTHOR);
417 MODULE_DESCRIPTION(DRIVER_DESC);
418 MODULE_SOFTDEP("post: vfio_mdev");
419