1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2017 Netronome Systems, Inc.
3 * Copyright (C) 2019 Mellanox Technologies. All rights reserved
6 #include <linux/completion.h>
7 #include <linux/device.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>
16 #include "netdevsim.h"
18 static DEFINE_IDA(nsim_bus_dev_ids
);
19 static LIST_HEAD(nsim_bus_dev_list
);
20 static DEFINE_MUTEX(nsim_bus_dev_list_lock
);
21 static bool nsim_bus_enable
;
22 static refcount_t nsim_bus_devs
; /* Including the bus itself. */
23 static DECLARE_COMPLETION(nsim_bus_devs_released
);
25 static struct nsim_bus_dev
*to_nsim_bus_dev(struct device
*dev
)
27 return container_of(dev
, struct nsim_bus_dev
, dev
);
31 nsim_bus_dev_numvfs_store(struct device
*dev
, struct device_attribute
*attr
,
32 const char *buf
, size_t count
)
34 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
38 ret
= kstrtouint(buf
, 0, &num_vfs
);
44 if (dev_get_drvdata(dev
))
45 ret
= nsim_drv_configure_vfs(nsim_bus_dev
, num_vfs
);
48 return ret
? ret
: count
;
52 nsim_bus_dev_numvfs_show(struct device
*dev
,
53 struct device_attribute
*attr
, char *buf
)
55 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
57 return sprintf(buf
, "%u\n", nsim_bus_dev
->num_vfs
);
60 static struct device_attribute nsim_bus_dev_numvfs_attr
=
61 __ATTR(sriov_numvfs
, 0664, nsim_bus_dev_numvfs_show
,
62 nsim_bus_dev_numvfs_store
);
65 new_port_store(struct device
*dev
, struct device_attribute
*attr
,
66 const char *buf
, size_t count
)
68 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
69 unsigned int port_index
;
72 /* Prevent to use nsim_bus_dev before initialization. */
73 if (!smp_load_acquire(&nsim_bus_dev
->init
))
75 ret
= kstrtouint(buf
, 0, &port_index
);
79 ret
= nsim_drv_port_add(nsim_bus_dev
, NSIM_DEV_PORT_TYPE_PF
, port_index
);
80 return ret
? ret
: count
;
83 static struct device_attribute nsim_bus_dev_new_port_attr
= __ATTR_WO(new_port
);
86 del_port_store(struct device
*dev
, struct device_attribute
*attr
,
87 const char *buf
, size_t count
)
89 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
90 unsigned int port_index
;
93 /* Prevent to use nsim_bus_dev before initialization. */
94 if (!smp_load_acquire(&nsim_bus_dev
->init
))
96 ret
= kstrtouint(buf
, 0, &port_index
);
100 ret
= nsim_drv_port_del(nsim_bus_dev
, NSIM_DEV_PORT_TYPE_PF
, port_index
);
101 return ret
? ret
: count
;
104 static struct device_attribute nsim_bus_dev_del_port_attr
= __ATTR_WO(del_port
);
106 static 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
,
113 static const struct attribute_group nsim_bus_dev_attr_group
= {
114 .attrs
= nsim_bus_dev_attrs
,
117 static const struct attribute_group
*nsim_bus_dev_attr_groups
[] = {
118 &nsim_bus_dev_attr_group
,
122 static void nsim_bus_dev_release(struct device
*dev
)
124 struct nsim_bus_dev
*nsim_bus_dev
;
126 nsim_bus_dev
= container_of(dev
, struct nsim_bus_dev
, dev
);
128 if (refcount_dec_and_test(&nsim_bus_devs
))
129 complete(&nsim_bus_devs_released
);
132 static const struct device_type nsim_bus_dev_type
= {
133 .groups
= nsim_bus_dev_attr_groups
,
134 .release
= nsim_bus_dev_release
,
137 static struct nsim_bus_dev
*
138 nsim_bus_dev_new(unsigned int id
, unsigned int port_count
, unsigned int num_queues
);
141 new_device_store(const struct bus_type
*bus
, const char *buf
, size_t count
)
143 unsigned int id
, port_count
, num_queues
;
144 struct nsim_bus_dev
*nsim_bus_dev
;
147 err
= sscanf(buf
, "%u %u %u", &id
, &port_count
, &num_queues
);
157 pr_err("Value of \"id\" is too big.\n");
162 pr_err("Format for adding new device is \"id port_count num_queues\" (uint uint unit).\n");
166 mutex_lock(&nsim_bus_dev_list_lock
);
167 /* Prevent to use resource before initialization. */
168 if (!smp_load_acquire(&nsim_bus_enable
)) {
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
);
179 refcount_inc(&nsim_bus_devs
);
180 /* Allow using nsim_bus_dev */
181 smp_store_release(&nsim_bus_dev
->init
, true);
183 list_add_tail(&nsim_bus_dev
->list
, &nsim_bus_dev_list
);
184 mutex_unlock(&nsim_bus_dev_list_lock
);
188 mutex_unlock(&nsim_bus_dev_list_lock
);
191 static BUS_ATTR_WO(new_device
);
193 static void nsim_bus_dev_del(struct nsim_bus_dev
*nsim_bus_dev
);
196 del_device_store(const struct bus_type
*bus
, const char *buf
, size_t count
)
198 struct nsim_bus_dev
*nsim_bus_dev
, *tmp
;
202 err
= sscanf(buf
, "%u", &id
);
206 pr_err("Value of \"id\" is too big.\n");
211 pr_err("Format for deleting device is \"id\" (uint).\n");
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
);
222 list_for_each_entry_safe(nsim_bus_dev
, tmp
, &nsim_bus_dev_list
, list
) {
223 if (nsim_bus_dev
->dev
.id
!= id
)
225 list_del(&nsim_bus_dev
->list
);
226 nsim_bus_dev_del(nsim_bus_dev
);
230 mutex_unlock(&nsim_bus_dev_list_lock
);
231 return !err
? count
: err
;
233 static BUS_ATTR_WO(del_device
);
235 static ssize_t
link_device_store(const struct bus_type
*bus
, const char *buf
, size_t count
)
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
;
243 err
= sscanf(buf
, "%d:%u %d:%u", &netnsfd_a
, &ifidx_a
, &netnsfd_b
,
246 pr_err("Format for linking two devices is \"netnsfd_a:ifidx_a netnsfd_b:ifidx_b\" (int uint int uint).\n");
250 ns_a
= get_net_ns_by_fd(netnsfd_a
);
252 pr_err("Could not find netns with fd: %d\n", netnsfd_a
);
256 ns_b
= get_net_ns_by_fd(netnsfd_b
);
258 pr_err("Could not find netns with fd: %d\n", netnsfd_b
);
265 dev_a
= __dev_get_by_index(ns_a
, ifidx_a
);
267 pr_err("Could not find device with ifindex %u in netnsfd %d\n",
272 if (!netdev_is_nsim(dev_a
)) {
273 pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n",
278 dev_b
= __dev_get_by_index(ns_b
, ifidx_b
);
280 pr_err("Could not find device with ifindex %u in netnsfd %d\n",
285 if (!netdev_is_nsim(dev_b
)) {
286 pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n",
291 if (dev_a
== dev_b
) {
292 pr_err("Cannot link a netdevsim to itself\n");
297 nsim_a
= netdev_priv(dev_a
);
298 peer
= rtnl_dereference(nsim_a
->peer
);
300 pr_err("Netdevsim %d:%u is already linked\n", netnsfd_a
,
305 nsim_b
= netdev_priv(dev_b
);
306 peer
= rtnl_dereference(nsim_b
->peer
);
308 pr_err("Netdevsim %d:%u is already linked\n", netnsfd_b
,
314 rcu_assign_pointer(nsim_a
->peer
, nsim_b
);
315 rcu_assign_pointer(nsim_b
->peer
, nsim_a
);
322 return !err
? count
: err
;
324 static BUS_ATTR_WO(link_device
);
326 static ssize_t
unlink_device_store(const struct bus_type
*bus
, const char *buf
, size_t count
)
328 struct netdevsim
*nsim
, *peer
;
329 struct net_device
*dev
;
334 err
= sscanf(buf
, "%u:%u", &netnsfd
, &ifidx
);
336 pr_err("Format for unlinking a device is \"netnsfd:ifidx\" (int uint).\n");
340 ns
= get_net_ns_by_fd(netnsfd
);
342 pr_err("Could not find netns with fd: %d\n", netnsfd
);
348 dev
= __dev_get_by_index(ns
, ifidx
);
350 pr_err("Could not find device with ifindex %u in netnsfd %d\n",
355 if (!netdev_is_nsim(dev
)) {
356 pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n",
361 nsim
= netdev_priv(dev
);
362 peer
= rtnl_dereference(nsim
->peer
);
367 RCU_INIT_POINTER(nsim
->peer
, NULL
);
368 RCU_INIT_POINTER(peer
->peer
, NULL
);
374 return !err
? count
: err
;
376 static BUS_ATTR_WO(unlink_device
);
378 static 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
,
385 ATTRIBUTE_GROUPS(nsim_bus
);
387 static int nsim_bus_probe(struct device
*dev
)
389 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
391 return nsim_drv_probe(nsim_bus_dev
);
394 static void nsim_bus_remove(struct device
*dev
)
396 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
398 nsim_drv_remove(nsim_bus_dev
);
401 static int nsim_num_vf(struct device
*dev
)
403 struct nsim_bus_dev
*nsim_bus_dev
= to_nsim_bus_dev(dev
);
405 return nsim_bus_dev
->num_vfs
;
408 static const struct bus_type nsim_bus
= {
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
,
417 #define NSIM_BUS_DEV_MAX_VFS 4
419 static struct nsim_bus_dev
*
420 nsim_bus_dev_new(unsigned int id
, unsigned int port_count
, unsigned int num_queues
)
422 struct nsim_bus_dev
*nsim_bus_dev
;
425 nsim_bus_dev
= kzalloc(sizeof(*nsim_bus_dev
), GFP_KERNEL
);
427 return ERR_PTR(-ENOMEM
);
429 err
= ida_alloc_range(&nsim_bus_dev_ids
, id
, id
, GFP_KERNEL
);
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);
442 err
= device_register(&nsim_bus_dev
->dev
);
444 goto err_nsim_bus_dev_id_free
;
448 err_nsim_bus_dev_id_free
:
449 ida_free(&nsim_bus_dev_ids
, nsim_bus_dev
->dev
.id
);
450 put_device(&nsim_bus_dev
->dev
);
452 err_nsim_bus_dev_free
:
457 static void nsim_bus_dev_del(struct nsim_bus_dev
*nsim_bus_dev
)
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
);
465 static struct device_driver nsim_driver
= {
468 .owner
= THIS_MODULE
,
471 int nsim_bus_init(void)
475 err
= bus_register(&nsim_bus
);
478 err
= driver_register(&nsim_driver
);
480 goto err_bus_unregister
;
481 refcount_set(&nsim_bus_devs
, 1);
482 /* Allow using resources */
483 smp_store_release(&nsim_bus_enable
, true);
487 bus_unregister(&nsim_bus
);
491 void nsim_bus_exit(void)
493 struct nsim_bus_dev
*nsim_bus_dev
, *tmp
;
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
);
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
);
505 mutex_unlock(&nsim_bus_dev_list_lock
);
507 wait_for_completion(&nsim_bus_devs_released
);
509 driver_unregister(&nsim_driver
);
510 bus_unregister(&nsim_bus
);