xref: /openbmc/linux/drivers/net/netdevsim/bus.c (revision caf58a2c)
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/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 
15 #include "netdevsim.h"
16 
17 static DEFINE_IDA(nsim_bus_dev_ids);
18 static LIST_HEAD(nsim_bus_dev_list);
19 static DEFINE_MUTEX(nsim_bus_dev_list_lock);
20 static bool nsim_bus_enable;
21 
22 static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
23 {
24 	return container_of(dev, struct nsim_bus_dev, dev);
25 }
26 
27 static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev,
28 				   unsigned int num_vfs)
29 {
30 	struct nsim_dev *nsim_dev;
31 	int err = 0;
32 
33 	if (nsim_bus_dev->max_vfs < num_vfs)
34 		return -ENOMEM;
35 
36 	if (!nsim_bus_dev->vfconfigs)
37 		return -ENOMEM;
38 	nsim_bus_dev->num_vfs = num_vfs;
39 
40 	nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
41 	if (nsim_esw_mode_is_switchdev(nsim_dev)) {
42 		err = nsim_esw_switchdev_enable(nsim_dev, NULL);
43 		if (err)
44 			nsim_bus_dev->num_vfs = 0;
45 	}
46 
47 	return err;
48 }
49 
50 void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev)
51 {
52 	struct nsim_dev *nsim_dev;
53 
54 	nsim_bus_dev->num_vfs = 0;
55 	nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
56 	if (nsim_esw_mode_is_switchdev(nsim_dev))
57 		nsim_esw_legacy_enable(nsim_dev, NULL);
58 }
59 
60 static ssize_t
61 nsim_bus_dev_numvfs_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 num_vfs;
66 	int ret;
67 
68 	ret = kstrtouint(buf, 0, &num_vfs);
69 	if (ret)
70 		return ret;
71 
72 	mutex_lock(&nsim_bus_dev->vfs_lock);
73 	if (nsim_bus_dev->num_vfs == num_vfs)
74 		goto exit_good;
75 	if (nsim_bus_dev->num_vfs && num_vfs) {
76 		ret = -EBUSY;
77 		goto exit_unlock;
78 	}
79 
80 	if (num_vfs) {
81 		ret = nsim_bus_dev_vfs_enable(nsim_bus_dev, num_vfs);
82 		if (ret)
83 			goto exit_unlock;
84 	} else {
85 		nsim_bus_dev_vfs_disable(nsim_bus_dev);
86 	}
87 exit_good:
88 	ret = count;
89 exit_unlock:
90 	mutex_unlock(&nsim_bus_dev->vfs_lock);
91 
92 	return ret;
93 }
94 
95 static ssize_t
96 nsim_bus_dev_numvfs_show(struct device *dev,
97 			 struct device_attribute *attr, char *buf)
98 {
99 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
100 
101 	return sprintf(buf, "%u\n", nsim_bus_dev->num_vfs);
102 }
103 
104 static struct device_attribute nsim_bus_dev_numvfs_attr =
105 	__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
106 	       nsim_bus_dev_numvfs_store);
107 
108 ssize_t nsim_bus_dev_max_vfs_read(struct file *file,
109 				  char __user *data,
110 				  size_t count, loff_t *ppos)
111 {
112 	struct nsim_bus_dev *nsim_bus_dev = file->private_data;
113 	char buf[11];
114 	ssize_t len;
115 
116 	len = snprintf(buf, sizeof(buf), "%u\n", nsim_bus_dev->max_vfs);
117 	if (len < 0)
118 		return len;
119 
120 	return simple_read_from_buffer(data, count, ppos, buf, len);
121 }
122 
123 ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
124 				   const char __user *data,
125 				   size_t count, loff_t *ppos)
126 {
127 	struct nsim_bus_dev *nsim_bus_dev = file->private_data;
128 	struct nsim_vf_config *vfconfigs;
129 	ssize_t ret;
130 	char buf[10];
131 	u32 val;
132 
133 	if (*ppos != 0)
134 		return 0;
135 
136 	if (count >= sizeof(buf))
137 		return -ENOSPC;
138 
139 	mutex_lock(&nsim_bus_dev->vfs_lock);
140 	/* Reject if VFs are configured */
141 	if (nsim_bus_dev->num_vfs) {
142 		ret = -EBUSY;
143 		goto unlock;
144 	}
145 
146 	ret = copy_from_user(buf, data, count);
147 	if (ret) {
148 		ret = -EFAULT;
149 		goto unlock;
150 	}
151 
152 	buf[count] = '\0';
153 	ret = kstrtouint(buf, 10, &val);
154 	if (ret) {
155 		ret = -EIO;
156 		goto unlock;
157 	}
158 
159 	/* max_vfs limited by the maximum number of provided port indexes */
160 	if (val > NSIM_DEV_VF_PORT_INDEX_MAX - NSIM_DEV_VF_PORT_INDEX_BASE) {
161 		ret = -ERANGE;
162 		goto unlock;
163 	}
164 
165 	vfconfigs = kcalloc(val, sizeof(struct nsim_vf_config), GFP_KERNEL | __GFP_NOWARN);
166 	if (!vfconfigs) {
167 		ret = -ENOMEM;
168 		goto unlock;
169 	}
170 
171 	kfree(nsim_bus_dev->vfconfigs);
172 	nsim_bus_dev->vfconfigs = vfconfigs;
173 	nsim_bus_dev->max_vfs = val;
174 	*ppos += count;
175 	ret = count;
176 unlock:
177 	mutex_unlock(&nsim_bus_dev->vfs_lock);
178 	return ret;
179 }
180 
181 static ssize_t
182 new_port_store(struct device *dev, struct device_attribute *attr,
183 	       const char *buf, size_t count)
184 {
185 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
186 	struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
187 	struct devlink *devlink;
188 	unsigned int port_index;
189 	int ret;
190 
191 	/* Prevent to use nsim_bus_dev before initialization. */
192 	if (!smp_load_acquire(&nsim_bus_dev->init))
193 		return -EBUSY;
194 	ret = kstrtouint(buf, 0, &port_index);
195 	if (ret)
196 		return ret;
197 
198 	devlink = priv_to_devlink(nsim_dev);
199 
200 	mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock);
201 	devlink_reload_disable(devlink);
202 	ret = nsim_dev_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
203 	devlink_reload_enable(devlink);
204 	mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
205 	return ret ? ret : count;
206 }
207 
208 static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
209 
210 static ssize_t
211 del_port_store(struct device *dev, struct device_attribute *attr,
212 	       const char *buf, size_t count)
213 {
214 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
215 	struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
216 	struct devlink *devlink;
217 	unsigned int port_index;
218 	int ret;
219 
220 	/* Prevent to use nsim_bus_dev before initialization. */
221 	if (!smp_load_acquire(&nsim_bus_dev->init))
222 		return -EBUSY;
223 	ret = kstrtouint(buf, 0, &port_index);
224 	if (ret)
225 		return ret;
226 
227 	devlink = priv_to_devlink(nsim_dev);
228 
229 	mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock);
230 	devlink_reload_disable(devlink);
231 	ret = nsim_dev_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
232 	devlink_reload_enable(devlink);
233 	mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
234 	return ret ? ret : count;
235 }
236 
237 static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
238 
239 static struct attribute *nsim_bus_dev_attrs[] = {
240 	&nsim_bus_dev_numvfs_attr.attr,
241 	&nsim_bus_dev_new_port_attr.attr,
242 	&nsim_bus_dev_del_port_attr.attr,
243 	NULL,
244 };
245 
246 static const struct attribute_group nsim_bus_dev_attr_group = {
247 	.attrs = nsim_bus_dev_attrs,
248 };
249 
250 static const struct attribute_group *nsim_bus_dev_attr_groups[] = {
251 	&nsim_bus_dev_attr_group,
252 	NULL,
253 };
254 
255 static void nsim_bus_dev_release(struct device *dev)
256 {
257 }
258 
259 static struct device_type nsim_bus_dev_type = {
260 	.groups = nsim_bus_dev_attr_groups,
261 	.release = nsim_bus_dev_release,
262 };
263 
264 static struct nsim_bus_dev *
265 nsim_bus_dev_new(unsigned int id, unsigned int port_count);
266 
267 static ssize_t
268 new_device_store(struct bus_type *bus, const char *buf, size_t count)
269 {
270 	struct nsim_bus_dev *nsim_bus_dev;
271 	unsigned int port_count;
272 	unsigned int id;
273 	int err;
274 
275 	err = sscanf(buf, "%u %u", &id, &port_count);
276 	switch (err) {
277 	case 1:
278 		port_count = 1;
279 		fallthrough;
280 	case 2:
281 		if (id > INT_MAX) {
282 			pr_err("Value of \"id\" is too big.\n");
283 			return -EINVAL;
284 		}
285 		break;
286 	default:
287 		pr_err("Format for adding new device is \"id port_count\" (uint uint).\n");
288 		return -EINVAL;
289 	}
290 
291 	mutex_lock(&nsim_bus_dev_list_lock);
292 	/* Prevent to use resource before initialization. */
293 	if (!smp_load_acquire(&nsim_bus_enable)) {
294 		err = -EBUSY;
295 		goto err;
296 	}
297 
298 	nsim_bus_dev = nsim_bus_dev_new(id, port_count);
299 	if (IS_ERR(nsim_bus_dev)) {
300 		err = PTR_ERR(nsim_bus_dev);
301 		goto err;
302 	}
303 
304 	/* Allow using nsim_bus_dev */
305 	smp_store_release(&nsim_bus_dev->init, true);
306 
307 	list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list);
308 	mutex_unlock(&nsim_bus_dev_list_lock);
309 
310 	return count;
311 err:
312 	mutex_unlock(&nsim_bus_dev_list_lock);
313 	return err;
314 }
315 static BUS_ATTR_WO(new_device);
316 
317 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
318 
319 static ssize_t
320 del_device_store(struct bus_type *bus, const char *buf, size_t count)
321 {
322 	struct nsim_bus_dev *nsim_bus_dev, *tmp;
323 	unsigned int id;
324 	int err;
325 
326 	err = sscanf(buf, "%u", &id);
327 	switch (err) {
328 	case 1:
329 		if (id > INT_MAX) {
330 			pr_err("Value of \"id\" is too big.\n");
331 			return -EINVAL;
332 		}
333 		break;
334 	default:
335 		pr_err("Format for deleting device is \"id\" (uint).\n");
336 		return -EINVAL;
337 	}
338 
339 	err = -ENOENT;
340 	mutex_lock(&nsim_bus_dev_list_lock);
341 	/* Prevent to use resource before initialization. */
342 	if (!smp_load_acquire(&nsim_bus_enable)) {
343 		mutex_unlock(&nsim_bus_dev_list_lock);
344 		return -EBUSY;
345 	}
346 	list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
347 		if (nsim_bus_dev->dev.id != id)
348 			continue;
349 		list_del(&nsim_bus_dev->list);
350 		nsim_bus_dev_del(nsim_bus_dev);
351 		err = 0;
352 		break;
353 	}
354 	mutex_unlock(&nsim_bus_dev_list_lock);
355 	return !err ? count : err;
356 }
357 static BUS_ATTR_WO(del_device);
358 
359 static struct attribute *nsim_bus_attrs[] = {
360 	&bus_attr_new_device.attr,
361 	&bus_attr_del_device.attr,
362 	NULL
363 };
364 ATTRIBUTE_GROUPS(nsim_bus);
365 
366 static int nsim_bus_probe(struct device *dev)
367 {
368 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
369 
370 	return nsim_dev_probe(nsim_bus_dev);
371 }
372 
373 static void nsim_bus_remove(struct device *dev)
374 {
375 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
376 
377 	nsim_dev_remove(nsim_bus_dev);
378 }
379 
380 static int nsim_num_vf(struct device *dev)
381 {
382 	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
383 
384 	return nsim_bus_dev->num_vfs;
385 }
386 
387 static struct bus_type nsim_bus = {
388 	.name		= DRV_NAME,
389 	.dev_name	= DRV_NAME,
390 	.bus_groups	= nsim_bus_groups,
391 	.probe		= nsim_bus_probe,
392 	.remove		= nsim_bus_remove,
393 	.num_vf		= nsim_num_vf,
394 };
395 
396 #define NSIM_BUS_DEV_MAX_VFS 4
397 
398 static struct nsim_bus_dev *
399 nsim_bus_dev_new(unsigned int id, unsigned int port_count)
400 {
401 	struct nsim_bus_dev *nsim_bus_dev;
402 	int err;
403 
404 	nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL);
405 	if (!nsim_bus_dev)
406 		return ERR_PTR(-ENOMEM);
407 
408 	err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL);
409 	if (err < 0)
410 		goto err_nsim_bus_dev_free;
411 	nsim_bus_dev->dev.id = err;
412 	nsim_bus_dev->dev.bus = &nsim_bus;
413 	nsim_bus_dev->dev.type = &nsim_bus_dev_type;
414 	nsim_bus_dev->port_count = port_count;
415 	nsim_bus_dev->initial_net = current->nsproxy->net_ns;
416 	nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS;
417 	mutex_init(&nsim_bus_dev->nsim_bus_reload_lock);
418 	mutex_init(&nsim_bus_dev->vfs_lock);
419 	/* Disallow using nsim_bus_dev */
420 	smp_store_release(&nsim_bus_dev->init, false);
421 
422 	nsim_bus_dev->vfconfigs = kcalloc(nsim_bus_dev->max_vfs,
423 					  sizeof(struct nsim_vf_config),
424 					  GFP_KERNEL | __GFP_NOWARN);
425 	if (!nsim_bus_dev->vfconfigs) {
426 		err = -ENOMEM;
427 		goto err_nsim_bus_dev_id_free;
428 	}
429 
430 	err = device_register(&nsim_bus_dev->dev);
431 	if (err)
432 		goto err_nsim_vfs_free;
433 
434 	return nsim_bus_dev;
435 
436 err_nsim_vfs_free:
437 	kfree(nsim_bus_dev->vfconfigs);
438 err_nsim_bus_dev_id_free:
439 	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
440 err_nsim_bus_dev_free:
441 	kfree(nsim_bus_dev);
442 	return ERR_PTR(err);
443 }
444 
445 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
446 {
447 	/* Disallow using nsim_bus_dev */
448 	smp_store_release(&nsim_bus_dev->init, false);
449 	device_unregister(&nsim_bus_dev->dev);
450 	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
451 	kfree(nsim_bus_dev->vfconfigs);
452 	kfree(nsim_bus_dev);
453 }
454 
455 static struct device_driver nsim_driver = {
456 	.name		= DRV_NAME,
457 	.bus		= &nsim_bus,
458 	.owner		= THIS_MODULE,
459 };
460 
461 int nsim_bus_init(void)
462 {
463 	int err;
464 
465 	err = bus_register(&nsim_bus);
466 	if (err)
467 		return err;
468 	err = driver_register(&nsim_driver);
469 	if (err)
470 		goto err_bus_unregister;
471 	/* Allow using resources */
472 	smp_store_release(&nsim_bus_enable, true);
473 	return 0;
474 
475 err_bus_unregister:
476 	bus_unregister(&nsim_bus);
477 	return err;
478 }
479 
480 void nsim_bus_exit(void)
481 {
482 	struct nsim_bus_dev *nsim_bus_dev, *tmp;
483 
484 	/* Disallow using resources */
485 	smp_store_release(&nsim_bus_enable, false);
486 
487 	mutex_lock(&nsim_bus_dev_list_lock);
488 	list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
489 		list_del(&nsim_bus_dev->list);
490 		nsim_bus_dev_del(nsim_bus_dev);
491 	}
492 	mutex_unlock(&nsim_bus_dev_list_lock);
493 
494 	driver_unregister(&nsim_driver);
495 	bus_unregister(&nsim_bus);
496 }
497