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 
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 /* Caller must hold parent unreg_sem read or write lock */
mdev_device_remove_common(struct mdev_device * mdev)27 static 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 
mdev_device_remove_cb(struct device * dev,void * data)38 static 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  */
mdev_register_parent(struct mdev_parent * parent,struct device * dev,struct mdev_driver * mdev_driver,struct mdev_type ** types,unsigned int nr_types)59 int 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 	if (!mdev_bus_compat_class) {
76 		mdev_bus_compat_class = class_compat_register("mdev_bus");
77 		if (!mdev_bus_compat_class)
78 			return -ENOMEM;
79 	}
80 
81 	ret = parent_create_sysfs_files(parent);
82 	if (ret)
83 		return ret;
84 
85 	ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
86 	if (ret)
87 		dev_warn(dev, "Failed to create compatibility class link\n");
88 
89 	dev_info(dev, "MDEV: Registered\n");
90 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
91 	return 0;
92 }
93 EXPORT_SYMBOL(mdev_register_parent);
94 
95 /*
96  * mdev_unregister_parent : Unregister a parent device
97  * @parent: parent structure to unregister
98  */
mdev_unregister_parent(struct mdev_parent * parent)99 void mdev_unregister_parent(struct mdev_parent *parent)
100 {
101 	char *env_string = "MDEV_STATE=unregistered";
102 	char *envp[] = { env_string, NULL };
103 
104 	dev_info(parent->dev, "MDEV: Unregistering\n");
105 
106 	down_write(&parent->unreg_sem);
107 	class_compat_remove_link(mdev_bus_compat_class, parent->dev, NULL);
108 	device_for_each_child(parent->dev, NULL, mdev_device_remove_cb);
109 	parent_remove_sysfs_files(parent);
110 	up_write(&parent->unreg_sem);
111 
112 	kobject_uevent_env(&parent->dev->kobj, KOBJ_CHANGE, envp);
113 }
114 EXPORT_SYMBOL(mdev_unregister_parent);
115 
mdev_device_release(struct device * dev)116 static void mdev_device_release(struct device *dev)
117 {
118 	struct mdev_device *mdev = to_mdev_device(dev);
119 	struct mdev_parent *parent = mdev->type->parent;
120 
121 	mutex_lock(&mdev_list_lock);
122 	list_del(&mdev->next);
123 	if (!parent->mdev_driver->get_available)
124 		atomic_inc(&parent->available_instances);
125 	mutex_unlock(&mdev_list_lock);
126 
127 	/* Pairs with the get in mdev_device_create() */
128 	kobject_put(&mdev->type->kobj);
129 
130 	dev_dbg(&mdev->dev, "MDEV: destroying\n");
131 	kfree(mdev);
132 }
133 
mdev_device_create(struct mdev_type * type,const guid_t * uuid)134 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
135 {
136 	int ret;
137 	struct mdev_device *mdev, *tmp;
138 	struct mdev_parent *parent = type->parent;
139 	struct mdev_driver *drv = parent->mdev_driver;
140 
141 	mutex_lock(&mdev_list_lock);
142 
143 	/* Check for duplicate */
144 	list_for_each_entry(tmp, &mdev_list, next) {
145 		if (guid_equal(&tmp->uuid, uuid)) {
146 			mutex_unlock(&mdev_list_lock);
147 			return -EEXIST;
148 		}
149 	}
150 
151 	if (!drv->get_available) {
152 		/*
153 		 * Note: that non-atomic read and dec is fine here because
154 		 * all modifications are under mdev_list_lock.
155 		 */
156 		if (!atomic_read(&parent->available_instances)) {
157 			mutex_unlock(&mdev_list_lock);
158 			return -EUSERS;
159 		}
160 		atomic_dec(&parent->available_instances);
161 	}
162 
163 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
164 	if (!mdev) {
165 		mutex_unlock(&mdev_list_lock);
166 		return -ENOMEM;
167 	}
168 
169 	device_initialize(&mdev->dev);
170 	mdev->dev.parent  = parent->dev;
171 	mdev->dev.bus = &mdev_bus_type;
172 	mdev->dev.release = mdev_device_release;
173 	mdev->dev.groups = mdev_device_groups;
174 	mdev->type = type;
175 	/* Pairs with the put in mdev_device_release() */
176 	kobject_get(&type->kobj);
177 
178 	guid_copy(&mdev->uuid, uuid);
179 	list_add(&mdev->next, &mdev_list);
180 	mutex_unlock(&mdev_list_lock);
181 
182 	ret = dev_set_name(&mdev->dev, "%pUl", uuid);
183 	if (ret)
184 		goto out_put_device;
185 
186 	/* Check if parent unregistration has started */
187 	if (!down_read_trylock(&parent->unreg_sem)) {
188 		ret = -ENODEV;
189 		goto out_put_device;
190 	}
191 
192 	ret = device_add(&mdev->dev);
193 	if (ret)
194 		goto out_unlock;
195 
196 	ret = device_driver_attach(&drv->driver, &mdev->dev);
197 	if (ret)
198 		goto out_del;
199 
200 	ret = mdev_create_sysfs_files(mdev);
201 	if (ret)
202 		goto out_del;
203 
204 	mdev->active = true;
205 	dev_dbg(&mdev->dev, "MDEV: created\n");
206 	up_read(&parent->unreg_sem);
207 
208 	return 0;
209 
210 out_del:
211 	device_del(&mdev->dev);
212 out_unlock:
213 	up_read(&parent->unreg_sem);
214 out_put_device:
215 	put_device(&mdev->dev);
216 	return ret;
217 }
218 
mdev_device_remove(struct mdev_device * mdev)219 int mdev_device_remove(struct mdev_device *mdev)
220 {
221 	struct mdev_device *tmp;
222 	struct mdev_parent *parent = mdev->type->parent;
223 
224 	mutex_lock(&mdev_list_lock);
225 	list_for_each_entry(tmp, &mdev_list, next) {
226 		if (tmp == mdev)
227 			break;
228 	}
229 
230 	if (tmp != mdev) {
231 		mutex_unlock(&mdev_list_lock);
232 		return -ENODEV;
233 	}
234 
235 	if (!mdev->active) {
236 		mutex_unlock(&mdev_list_lock);
237 		return -EAGAIN;
238 	}
239 
240 	mdev->active = false;
241 	mutex_unlock(&mdev_list_lock);
242 
243 	/* Check if parent unregistration has started */
244 	if (!down_read_trylock(&parent->unreg_sem))
245 		return -ENODEV;
246 
247 	mdev_device_remove_common(mdev);
248 	up_read(&parent->unreg_sem);
249 	return 0;
250 }
251 
mdev_init(void)252 static int __init mdev_init(void)
253 {
254 	return bus_register(&mdev_bus_type);
255 }
256 
mdev_exit(void)257 static void __exit mdev_exit(void)
258 {
259 	if (mdev_bus_compat_class)
260 		class_compat_unregister(mdev_bus_compat_class);
261 	bus_unregister(&mdev_bus_type);
262 }
263 
264 subsys_initcall(mdev_init)
265 module_exit(mdev_exit)
266 
267 MODULE_VERSION(DRIVER_VERSION);
268 MODULE_LICENSE("GPL v2");
269 MODULE_AUTHOR(DRIVER_AUTHOR);
270 MODULE_DESCRIPTION(DRIVER_DESC);
271