xref: /linux-6.15/drivers/vfio/mdev/mdev_core.c (revision 062e720c)
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 <[email protected]>
7  *             Kirti Wankhede <[email protected]>
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 
21 static struct class_compat *mdev_bus_compat_class;
22 
23 static LIST_HEAD(mdev_list);
24 static DEFINE_MUTEX(mdev_list_lock);
25 
26 /*
27  * Used in mdev_type_attribute sysfs functions to return the parent struct
28  * device
29  */
30 struct device *mtype_get_parent_dev(struct mdev_type *mtype)
31 {
32 	return mtype->parent->dev;
33 }
34 EXPORT_SYMBOL(mtype_get_parent_dev);
35 
36 /* Caller must hold parent unreg_sem read or write lock */
37 static void mdev_device_remove_common(struct mdev_device *mdev)
38 {
39 	struct mdev_parent *parent = mdev->type->parent;
40 
41 	mdev_remove_sysfs_files(mdev);
42 	device_del(&mdev->dev);
43 	lockdep_assert_held(&parent->unreg_sem);
44 	/* Balances with device_initialize() */
45 	put_device(&mdev->dev);
46 }
47 
48 static int mdev_device_remove_cb(struct device *dev, void *data)
49 {
50 	if (dev->bus == &mdev_bus_type)
51 		mdev_device_remove_common(to_mdev_device(dev));
52 	return 0;
53 }
54 
55 /*
56  * mdev_register_parent: Register a device as parent for mdevs
57  * @parent: parent structure registered
58  * @dev: device structure representing parent device.
59  * @mdev_driver: Device driver to bind to the newly created mdev
60  * @types: Array of supported mdev types
61  * @nr_types: Number of entries in @types
62  *
63  * Registers the @parent stucture as a parent for mdev types and thus mdev
64  * devices.  The caller needs to hold a reference on @dev that must not be
65  * released until after the call to mdev_unregister_parent().
66  *
67  * Returns a negative value on error, otherwise 0.
68  */
69 int mdev_register_parent(struct mdev_parent *parent, struct device *dev,
70 		struct mdev_driver *mdev_driver, struct mdev_type **types,
71 		unsigned int nr_types)
72 {
73 	char *env_string = "MDEV_STATE=registered";
74 	char *envp[] = { env_string, NULL };
75 	int ret;
76 
77 	memset(parent, 0, sizeof(*parent));
78 	init_rwsem(&parent->unreg_sem);
79 	parent->dev = dev;
80 	parent->mdev_driver = mdev_driver;
81 	parent->types = types;
82 	parent->nr_types = nr_types;
83 
84 	if (!mdev_bus_compat_class) {
85 		mdev_bus_compat_class = class_compat_register("mdev_bus");
86 		if (!mdev_bus_compat_class)
87 			return -ENOMEM;
88 	}
89 
90 	ret = parent_create_sysfs_files(parent);
91 	if (ret)
92 		return ret;
93 
94 	ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
95 	if (ret)
96 		dev_warn(dev, "Failed to create compatibility class link\n");
97 
98 	dev_info(dev, "MDEV: Registered\n");
99 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
100 	return 0;
101 }
102 EXPORT_SYMBOL(mdev_register_parent);
103 
104 /*
105  * mdev_unregister_parent : Unregister a parent device
106  * @parent: parent structure to unregister
107  */
108 void mdev_unregister_parent(struct mdev_parent *parent)
109 {
110 	char *env_string = "MDEV_STATE=unregistered";
111 	char *envp[] = { env_string, NULL };
112 
113 	dev_info(parent->dev, "MDEV: Unregistering\n");
114 
115 	down_write(&parent->unreg_sem);
116 	class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
117 	device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
118 	parent_remove_sysfs_files(parent);
119 	up_write(&parent->unreg_sem);
120 
121 	kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
122 }
123 EXPORT_SYMBOL(mdev_unregister_parent);
124 
125 static void mdev_device_release(struct device *dev)
126 {
127 	struct mdev_device *mdev = to_mdev_device(dev);
128 
129 	/* Pairs with the get in mdev_device_create() */
130 	kobject_put(&mdev->type->kobj);
131 
132 	mutex_lock(&mdev_list_lock);
133 	list_del(&mdev->next);
134 	mutex_unlock(&mdev_list_lock);
135 
136 	dev_dbg(&mdev->dev, "MDEV: destroying\n");
137 	kfree(mdev);
138 }
139 
140 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
141 {
142 	int ret;
143 	struct mdev_device *mdev, *tmp;
144 	struct mdev_parent *parent = type->parent;
145 	struct mdev_driver *drv = parent->mdev_driver;
146 
147 	mutex_lock(&mdev_list_lock);
148 
149 	/* Check for duplicate */
150 	list_for_each_entry(tmp, &mdev_list, next) {
151 		if (guid_equal(&tmp->uuid, uuid)) {
152 			mutex_unlock(&mdev_list_lock);
153 			return -EEXIST;
154 		}
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 
204 out_del:
205 	device_del(&mdev->dev);
206 out_unlock:
207 	up_read(&parent->unreg_sem);
208 out_put_device:
209 	put_device(&mdev->dev);
210 	return ret;
211 }
212 
213 int 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 
246 static int __init mdev_init(void)
247 {
248 	return bus_register(&mdev_bus_type);
249 }
250 
251 static void __exit mdev_exit(void)
252 {
253 	if (mdev_bus_compat_class)
254 		class_compat_unregister(mdev_bus_compat_class);
255 	bus_unregister(&mdev_bus_type);
256 }
257 
258 subsys_initcall(mdev_init)
259 module_exit(mdev_exit)
260 
261 MODULE_VERSION(DRIVER_VERSION);
262 MODULE_LICENSE("GPL v2");
263 MODULE_AUTHOR(DRIVER_AUTHOR);
264 MODULE_DESCRIPTION(DRIVER_DESC);
265