xref: /linux/drivers/net/netdevsim/bus.c (revision 73b25505ce043b561028e5571d84dc82aa53c2b4)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2017 Netronome Systems, Inc.
3  * Copyright (C) 2019 Mellanox Technologies. All rights reserved
4  */
5 
6 #include <linux/device.h>
7 #include <linux/idr.h>
8 #include <linux/kernel.h>
9 #include <linux/list.h>
10 #include <linux/mutex.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 
14 #include "netdevsim.h"
15 
16 static DEFINE_IDA(nsim_bus_dev_ids);
17 static LIST_HEAD(nsim_bus_dev_list);
18 static DEFINE_MUTEX(nsim_bus_dev_list_lock);
19 static bool nsim_bus_enable;
20 
21 static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
22 {
23 	return container_of(dev, struct nsim_bus_dev, dev);
24 }
25 
26 static ssize_t
27 nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
28 			  const char *buf, size_t count)
29 {
30 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
31 	unsigned int num_vfs;
32 	int ret;
33 
34 	ret = kstrtouint(buf, 0, &num_vfs);
35 	if (ret)
36 		return ret;
37 
38 	device_lock(dev);
39 	ret = -ENOENT;
40 	if (dev_get_drvdata(dev))
41 		ret = nsim_drv_configure_vfs(nsim_bus_dev, num_vfs);
42 	device_unlock(dev);
43 
44 	return ret ? ret : count;
45 }
46 
47 static ssize_t
48 nsim_bus_dev_numvfs_show(struct device *dev,
49 			 struct device_attribute *attr, char *buf)
50 {
51 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
52 
53 	return sprintf(buf, "%u\n", nsim_bus_dev->num_vfs);
54 }
55 
56 static struct device_attribute nsim_bus_dev_numvfs_attr =
57 	__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
58 	       nsim_bus_dev_numvfs_store);
59 
60 static ssize_t
61 new_port_store(struct device *dev, struct device_attribute *attr,
62 	       const char *buf, size_t count)
63 {
64 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
65 	unsigned int port_index;
66 	int ret;
67 
68 	/* Prevent to use nsim_bus_dev before initialization. */
69 	if (!smp_load_acquire(&nsim_bus_dev->init))
70 		return -EBUSY;
71 	ret = kstrtouint(buf, 0, &port_index);
72 	if (ret)
73 		return ret;
74 
75 	ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
76 	return ret ? ret : count;
77 }
78 
79 static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
80 
81 static ssize_t
82 del_port_store(struct device *dev, struct device_attribute *attr,
83 	       const char *buf, size_t count)
84 {
85 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
86 	unsigned int port_index;
87 	int ret;
88 
89 	/* Prevent to use nsim_bus_dev before initialization. */
90 	if (!smp_load_acquire(&nsim_bus_dev->init))
91 		return -EBUSY;
92 	ret = kstrtouint(buf, 0, &port_index);
93 	if (ret)
94 		return ret;
95 
96 	ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
97 	return ret ? ret : count;
98 }
99 
100 static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
101 
102 static struct attribute *nsim_bus_dev_attrs[] = {
103 	&nsim_bus_dev_numvfs_attr.attr,
104 	&nsim_bus_dev_new_port_attr.attr,
105 	&nsim_bus_dev_del_port_attr.attr,
106 	NULL,
107 };
108 
109 static const struct attribute_group nsim_bus_dev_attr_group = {
110 	.attrs = nsim_bus_dev_attrs,
111 };
112 
113 static const struct attribute_group *nsim_bus_dev_attr_groups[] = {
114 	&nsim_bus_dev_attr_group,
115 	NULL,
116 };
117 
118 static void nsim_bus_dev_release(struct device *dev)
119 {
120 	struct nsim_bus_dev *nsim_bus_dev;
121 
122 	nsim_bus_dev = container_of(dev, struct nsim_bus_dev, dev);
123 	kfree(nsim_bus_dev);
124 }
125 
126 static struct device_type nsim_bus_dev_type = {
127 	.groups = nsim_bus_dev_attr_groups,
128 	.release = nsim_bus_dev_release,
129 };
130 
131 static struct nsim_bus_dev *
132 nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues);
133 
134 static ssize_t
135 new_device_store(const struct bus_type *bus, const char *buf, size_t count)
136 {
137 	unsigned int id, port_count, num_queues;
138 	struct nsim_bus_dev *nsim_bus_dev;
139 	int err;
140 
141 	err = sscanf(buf, "%u %u %u", &id, &port_count, &num_queues);
142 	switch (err) {
143 	case 1:
144 		port_count = 1;
145 		fallthrough;
146 	case 2:
147 		num_queues = 1;
148 		fallthrough;
149 	case 3:
150 		if (id > INT_MAX) {
151 			pr_err("Value of \"id\" is too big.\n");
152 			return -EINVAL;
153 		}
154 		break;
155 	default:
156 		pr_err("Format for adding new device is \"id port_count num_queues\" (uint uint unit).\n");
157 		return -EINVAL;
158 	}
159 
160 	mutex_lock(&nsim_bus_dev_list_lock);
161 	/* Prevent to use resource before initialization. */
162 	if (!smp_load_acquire(&nsim_bus_enable)) {
163 		err = -EBUSY;
164 		goto err;
165 	}
166 
167 	nsim_bus_dev = nsim_bus_dev_new(id, port_count, num_queues);
168 	if (IS_ERR(nsim_bus_dev)) {
169 		err = PTR_ERR(nsim_bus_dev);
170 		goto err;
171 	}
172 
173 	/* Allow using nsim_bus_dev */
174 	smp_store_release(&nsim_bus_dev->init, true);
175 
176 	list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list);
177 	mutex_unlock(&nsim_bus_dev_list_lock);
178 
179 	return count;
180 err:
181 	mutex_unlock(&nsim_bus_dev_list_lock);
182 	return err;
183 }
184 static BUS_ATTR_WO(new_device);
185 
186 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
187 
188 static ssize_t
189 del_device_store(const struct bus_type *bus, const char *buf, size_t count)
190 {
191 	struct nsim_bus_dev *nsim_bus_dev, *tmp;
192 	unsigned int id;
193 	int err;
194 
195 	err = sscanf(buf, "%u", &id);
196 	switch (err) {
197 	case 1:
198 		if (id > INT_MAX) {
199 			pr_err("Value of \"id\" is too big.\n");
200 			return -EINVAL;
201 		}
202 		break;
203 	default:
204 		pr_err("Format for deleting device is \"id\" (uint).\n");
205 		return -EINVAL;
206 	}
207 
208 	err = -ENOENT;
209 	mutex_lock(&nsim_bus_dev_list_lock);
210 	/* Prevent to use resource before initialization. */
211 	if (!smp_load_acquire(&nsim_bus_enable)) {
212 		mutex_unlock(&nsim_bus_dev_list_lock);
213 		return -EBUSY;
214 	}
215 	list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
216 		if (nsim_bus_dev->dev.id != id)
217 			continue;
218 		list_del(&nsim_bus_dev->list);
219 		nsim_bus_dev_del(nsim_bus_dev);
220 		err = 0;
221 		break;
222 	}
223 	mutex_unlock(&nsim_bus_dev_list_lock);
224 	return !err ? count : err;
225 }
226 static BUS_ATTR_WO(del_device);
227 
228 static struct attribute *nsim_bus_attrs[] = {
229 	&bus_attr_new_device.attr,
230 	&bus_attr_del_device.attr,
231 	NULL
232 };
233 ATTRIBUTE_GROUPS(nsim_bus);
234 
235 static int nsim_bus_probe(struct device *dev)
236 {
237 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
238 
239 	return nsim_drv_probe(nsim_bus_dev);
240 }
241 
242 static void nsim_bus_remove(struct device *dev)
243 {
244 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
245 
246 	nsim_drv_remove(nsim_bus_dev);
247 }
248 
249 static int nsim_num_vf(struct device *dev)
250 {
251 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
252 
253 	return nsim_bus_dev->num_vfs;
254 }
255 
256 static struct bus_type nsim_bus = {
257 	.name		= DRV_NAME,
258 	.dev_name	= DRV_NAME,
259 	.bus_groups	= nsim_bus_groups,
260 	.probe		= nsim_bus_probe,
261 	.remove		= nsim_bus_remove,
262 	.num_vf		= nsim_num_vf,
263 };
264 
265 #define NSIM_BUS_DEV_MAX_VFS 4
266 
267 static struct nsim_bus_dev *
268 nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues)
269 {
270 	struct nsim_bus_dev *nsim_bus_dev;
271 	int err;
272 
273 	nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL);
274 	if (!nsim_bus_dev)
275 		return ERR_PTR(-ENOMEM);
276 
277 	err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL);
278 	if (err < 0)
279 		goto err_nsim_bus_dev_free;
280 	nsim_bus_dev->dev.id = err;
281 	nsim_bus_dev->dev.bus = &nsim_bus;
282 	nsim_bus_dev->dev.type = &nsim_bus_dev_type;
283 	nsim_bus_dev->port_count = port_count;
284 	nsim_bus_dev->num_queues = num_queues;
285 	nsim_bus_dev->initial_net = current->nsproxy->net_ns;
286 	nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS;
287 	/* Disallow using nsim_bus_dev */
288 	smp_store_release(&nsim_bus_dev->init, false);
289 
290 	err = device_register(&nsim_bus_dev->dev);
291 	if (err)
292 		goto err_nsim_bus_dev_id_free;
293 
294 	return nsim_bus_dev;
295 
296 err_nsim_bus_dev_id_free:
297 	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
298 	put_device(&nsim_bus_dev->dev);
299 	nsim_bus_dev = NULL;
300 err_nsim_bus_dev_free:
301 	kfree(nsim_bus_dev);
302 	return ERR_PTR(err);
303 }
304 
305 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
306 {
307 	/* Disallow using nsim_bus_dev */
308 	smp_store_release(&nsim_bus_dev->init, false);
309 	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
310 	device_unregister(&nsim_bus_dev->dev);
311 }
312 
313 static struct device_driver nsim_driver = {
314 	.name		= DRV_NAME,
315 	.bus		= &nsim_bus,
316 	.owner		= THIS_MODULE,
317 };
318 
319 int nsim_bus_init(void)
320 {
321 	int err;
322 
323 	err = bus_register(&nsim_bus);
324 	if (err)
325 		return err;
326 	err = driver_register(&nsim_driver);
327 	if (err)
328 		goto err_bus_unregister;
329 	/* Allow using resources */
330 	smp_store_release(&nsim_bus_enable, true);
331 	return 0;
332 
333 err_bus_unregister:
334 	bus_unregister(&nsim_bus);
335 	return err;
336 }
337 
338 void nsim_bus_exit(void)
339 {
340 	struct nsim_bus_dev *nsim_bus_dev, *tmp;
341 
342 	/* Disallow using resources */
343 	smp_store_release(&nsim_bus_enable, false);
344 
345 	mutex_lock(&nsim_bus_dev_list_lock);
346 	list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
347 		list_del(&nsim_bus_dev->list);
348 		nsim_bus_dev_del(nsim_bus_dev);
349 	}
350 	mutex_unlock(&nsim_bus_dev_list_lock);
351 
352 	driver_unregister(&nsim_driver);
353 	bus_unregister(&nsim_bus);
354 }
355