Linux Audio

Check our new training course

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