18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2017 Netronome Systems, Inc.
38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Mellanox Technologies. All rights reserved
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/idr.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/list.h>
108c2ecf20Sopenharmony_ci#include <linux/mutex.h>
118c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "netdevsim.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic DEFINE_IDA(nsim_bus_dev_ids);
188c2ecf20Sopenharmony_cistatic LIST_HEAD(nsim_bus_dev_list);
198c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(nsim_bus_dev_list_lock);
208c2ecf20Sopenharmony_cistatic bool nsim_bus_enable;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	return container_of(dev, struct nsim_bus_dev, dev);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev,
288c2ecf20Sopenharmony_ci				   unsigned int num_vfs)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	nsim_bus_dev->vfconfigs = kcalloc(num_vfs,
318c2ecf20Sopenharmony_ci					  sizeof(struct nsim_vf_config),
328c2ecf20Sopenharmony_ci					  GFP_KERNEL | __GFP_NOWARN);
338c2ecf20Sopenharmony_ci	if (!nsim_bus_dev->vfconfigs)
348c2ecf20Sopenharmony_ci		return -ENOMEM;
358c2ecf20Sopenharmony_ci	nsim_bus_dev->num_vfs = num_vfs;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return 0;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	kfree(nsim_bus_dev->vfconfigs);
438c2ecf20Sopenharmony_ci	nsim_bus_dev->vfconfigs = NULL;
448c2ecf20Sopenharmony_ci	nsim_bus_dev->num_vfs = 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic ssize_t
488c2ecf20Sopenharmony_cinsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
498c2ecf20Sopenharmony_ci			  const char *buf, size_t count)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
528c2ecf20Sopenharmony_ci	unsigned int num_vfs;
538c2ecf20Sopenharmony_ci	int ret;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	ret = kstrtouint(buf, 0, &num_vfs);
568c2ecf20Sopenharmony_ci	if (ret)
578c2ecf20Sopenharmony_ci		return ret;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	rtnl_lock();
608c2ecf20Sopenharmony_ci	if (nsim_bus_dev->num_vfs == num_vfs)
618c2ecf20Sopenharmony_ci		goto exit_good;
628c2ecf20Sopenharmony_ci	if (nsim_bus_dev->num_vfs && num_vfs) {
638c2ecf20Sopenharmony_ci		ret = -EBUSY;
648c2ecf20Sopenharmony_ci		goto exit_unlock;
658c2ecf20Sopenharmony_ci	}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (num_vfs) {
688c2ecf20Sopenharmony_ci		ret = nsim_bus_dev_vfs_enable(nsim_bus_dev, num_vfs);
698c2ecf20Sopenharmony_ci		if (ret)
708c2ecf20Sopenharmony_ci			goto exit_unlock;
718c2ecf20Sopenharmony_ci	} else {
728c2ecf20Sopenharmony_ci		nsim_bus_dev_vfs_disable(nsim_bus_dev);
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ciexit_good:
758c2ecf20Sopenharmony_ci	ret = count;
768c2ecf20Sopenharmony_ciexit_unlock:
778c2ecf20Sopenharmony_ci	rtnl_unlock();
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return ret;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic ssize_t
838c2ecf20Sopenharmony_cinsim_bus_dev_numvfs_show(struct device *dev,
848c2ecf20Sopenharmony_ci			 struct device_attribute *attr, char *buf)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", nsim_bus_dev->num_vfs);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic struct device_attribute nsim_bus_dev_numvfs_attr =
928c2ecf20Sopenharmony_ci	__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
938c2ecf20Sopenharmony_ci	       nsim_bus_dev_numvfs_store);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic ssize_t
968c2ecf20Sopenharmony_cinew_port_store(struct device *dev, struct device_attribute *attr,
978c2ecf20Sopenharmony_ci	       const char *buf, size_t count)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
1008c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
1018c2ecf20Sopenharmony_ci	struct devlink *devlink;
1028c2ecf20Sopenharmony_ci	unsigned int port_index;
1038c2ecf20Sopenharmony_ci	int ret;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* Prevent to use nsim_bus_dev before initialization. */
1068c2ecf20Sopenharmony_ci	if (!smp_load_acquire(&nsim_bus_dev->init))
1078c2ecf20Sopenharmony_ci		return -EBUSY;
1088c2ecf20Sopenharmony_ci	ret = kstrtouint(buf, 0, &port_index);
1098c2ecf20Sopenharmony_ci	if (ret)
1108c2ecf20Sopenharmony_ci		return ret;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	devlink = priv_to_devlink(nsim_dev);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock);
1158c2ecf20Sopenharmony_ci	devlink_reload_disable(devlink);
1168c2ecf20Sopenharmony_ci	ret = nsim_dev_port_add(nsim_bus_dev, port_index);
1178c2ecf20Sopenharmony_ci	devlink_reload_enable(devlink);
1188c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
1198c2ecf20Sopenharmony_ci	return ret ? ret : count;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic ssize_t
1258c2ecf20Sopenharmony_cidel_port_store(struct device *dev, struct device_attribute *attr,
1268c2ecf20Sopenharmony_ci	       const char *buf, size_t count)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
1298c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
1308c2ecf20Sopenharmony_ci	struct devlink *devlink;
1318c2ecf20Sopenharmony_ci	unsigned int port_index;
1328c2ecf20Sopenharmony_ci	int ret;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/* Prevent to use nsim_bus_dev before initialization. */
1358c2ecf20Sopenharmony_ci	if (!smp_load_acquire(&nsim_bus_dev->init))
1368c2ecf20Sopenharmony_ci		return -EBUSY;
1378c2ecf20Sopenharmony_ci	ret = kstrtouint(buf, 0, &port_index);
1388c2ecf20Sopenharmony_ci	if (ret)
1398c2ecf20Sopenharmony_ci		return ret;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	devlink = priv_to_devlink(nsim_dev);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock);
1448c2ecf20Sopenharmony_ci	devlink_reload_disable(devlink);
1458c2ecf20Sopenharmony_ci	ret = nsim_dev_port_del(nsim_bus_dev, port_index);
1468c2ecf20Sopenharmony_ci	devlink_reload_enable(devlink);
1478c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
1488c2ecf20Sopenharmony_ci	return ret ? ret : count;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic struct attribute *nsim_bus_dev_attrs[] = {
1548c2ecf20Sopenharmony_ci	&nsim_bus_dev_numvfs_attr.attr,
1558c2ecf20Sopenharmony_ci	&nsim_bus_dev_new_port_attr.attr,
1568c2ecf20Sopenharmony_ci	&nsim_bus_dev_del_port_attr.attr,
1578c2ecf20Sopenharmony_ci	NULL,
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic const struct attribute_group nsim_bus_dev_attr_group = {
1618c2ecf20Sopenharmony_ci	.attrs = nsim_bus_dev_attrs,
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic const struct attribute_group *nsim_bus_dev_attr_groups[] = {
1658c2ecf20Sopenharmony_ci	&nsim_bus_dev_attr_group,
1668c2ecf20Sopenharmony_ci	NULL,
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void nsim_bus_dev_release(struct device *dev)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	nsim_bus_dev_vfs_disable(nsim_bus_dev);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic struct device_type nsim_bus_dev_type = {
1778c2ecf20Sopenharmony_ci	.groups = nsim_bus_dev_attr_groups,
1788c2ecf20Sopenharmony_ci	.release = nsim_bus_dev_release,
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic struct nsim_bus_dev *
1828c2ecf20Sopenharmony_cinsim_bus_dev_new(unsigned int id, unsigned int port_count);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic ssize_t
1858c2ecf20Sopenharmony_cinew_device_store(struct bus_type *bus, const char *buf, size_t count)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev;
1888c2ecf20Sopenharmony_ci	unsigned int port_count;
1898c2ecf20Sopenharmony_ci	unsigned int id;
1908c2ecf20Sopenharmony_ci	int err;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	err = sscanf(buf, "%u %u", &id, &port_count);
1938c2ecf20Sopenharmony_ci	switch (err) {
1948c2ecf20Sopenharmony_ci	case 1:
1958c2ecf20Sopenharmony_ci		port_count = 1;
1968c2ecf20Sopenharmony_ci		fallthrough;
1978c2ecf20Sopenharmony_ci	case 2:
1988c2ecf20Sopenharmony_ci		if (id > INT_MAX) {
1998c2ecf20Sopenharmony_ci			pr_err("Value of \"id\" is too big.\n");
2008c2ecf20Sopenharmony_ci			return -EINVAL;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci		break;
2038c2ecf20Sopenharmony_ci	default:
2048c2ecf20Sopenharmony_ci		pr_err("Format for adding new device is \"id port_count\" (uint uint).\n");
2058c2ecf20Sopenharmony_ci		return -EINVAL;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	mutex_lock(&nsim_bus_dev_list_lock);
2098c2ecf20Sopenharmony_ci	/* Prevent to use resource before initialization. */
2108c2ecf20Sopenharmony_ci	if (!smp_load_acquire(&nsim_bus_enable)) {
2118c2ecf20Sopenharmony_ci		err = -EBUSY;
2128c2ecf20Sopenharmony_ci		goto err;
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	nsim_bus_dev = nsim_bus_dev_new(id, port_count);
2168c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_bus_dev)) {
2178c2ecf20Sopenharmony_ci		err = PTR_ERR(nsim_bus_dev);
2188c2ecf20Sopenharmony_ci		goto err;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* Allow using nsim_bus_dev */
2228c2ecf20Sopenharmony_ci	smp_store_release(&nsim_bus_dev->init, true);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list);
2258c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_bus_dev_list_lock);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	return count;
2288c2ecf20Sopenharmony_cierr:
2298c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_bus_dev_list_lock);
2308c2ecf20Sopenharmony_ci	return err;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_cistatic BUS_ATTR_WO(new_device);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic ssize_t
2378c2ecf20Sopenharmony_cidel_device_store(struct bus_type *bus, const char *buf, size_t count)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev, *tmp;
2408c2ecf20Sopenharmony_ci	unsigned int id;
2418c2ecf20Sopenharmony_ci	int err;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	err = sscanf(buf, "%u", &id);
2448c2ecf20Sopenharmony_ci	switch (err) {
2458c2ecf20Sopenharmony_ci	case 1:
2468c2ecf20Sopenharmony_ci		if (id > INT_MAX) {
2478c2ecf20Sopenharmony_ci			pr_err("Value of \"id\" is too big.\n");
2488c2ecf20Sopenharmony_ci			return -EINVAL;
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci		break;
2518c2ecf20Sopenharmony_ci	default:
2528c2ecf20Sopenharmony_ci		pr_err("Format for deleting device is \"id\" (uint).\n");
2538c2ecf20Sopenharmony_ci		return -EINVAL;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	err = -ENOENT;
2578c2ecf20Sopenharmony_ci	mutex_lock(&nsim_bus_dev_list_lock);
2588c2ecf20Sopenharmony_ci	/* Prevent to use resource before initialization. */
2598c2ecf20Sopenharmony_ci	if (!smp_load_acquire(&nsim_bus_enable)) {
2608c2ecf20Sopenharmony_ci		mutex_unlock(&nsim_bus_dev_list_lock);
2618c2ecf20Sopenharmony_ci		return -EBUSY;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci	list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
2648c2ecf20Sopenharmony_ci		if (nsim_bus_dev->dev.id != id)
2658c2ecf20Sopenharmony_ci			continue;
2668c2ecf20Sopenharmony_ci		list_del(&nsim_bus_dev->list);
2678c2ecf20Sopenharmony_ci		nsim_bus_dev_del(nsim_bus_dev);
2688c2ecf20Sopenharmony_ci		err = 0;
2698c2ecf20Sopenharmony_ci		break;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_bus_dev_list_lock);
2728c2ecf20Sopenharmony_ci	return !err ? count : err;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_cistatic BUS_ATTR_WO(del_device);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic struct attribute *nsim_bus_attrs[] = {
2778c2ecf20Sopenharmony_ci	&bus_attr_new_device.attr,
2788c2ecf20Sopenharmony_ci	&bus_attr_del_device.attr,
2798c2ecf20Sopenharmony_ci	NULL
2808c2ecf20Sopenharmony_ci};
2818c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(nsim_bus);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic int nsim_bus_probe(struct device *dev)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	return nsim_dev_probe(nsim_bus_dev);
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic int nsim_bus_remove(struct device *dev)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	nsim_dev_remove(nsim_bus_dev);
2958c2ecf20Sopenharmony_ci	return 0;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic int nsim_num_vf(struct device *dev)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	return nsim_bus_dev->num_vfs;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic struct bus_type nsim_bus = {
3068c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
3078c2ecf20Sopenharmony_ci	.dev_name	= DRV_NAME,
3088c2ecf20Sopenharmony_ci	.bus_groups	= nsim_bus_groups,
3098c2ecf20Sopenharmony_ci	.probe		= nsim_bus_probe,
3108c2ecf20Sopenharmony_ci	.remove		= nsim_bus_remove,
3118c2ecf20Sopenharmony_ci	.num_vf		= nsim_num_vf,
3128c2ecf20Sopenharmony_ci};
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic struct nsim_bus_dev *
3158c2ecf20Sopenharmony_cinsim_bus_dev_new(unsigned int id, unsigned int port_count)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev;
3188c2ecf20Sopenharmony_ci	int err;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL);
3218c2ecf20Sopenharmony_ci	if (!nsim_bus_dev)
3228c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL);
3258c2ecf20Sopenharmony_ci	if (err < 0)
3268c2ecf20Sopenharmony_ci		goto err_nsim_bus_dev_free;
3278c2ecf20Sopenharmony_ci	nsim_bus_dev->dev.id = err;
3288c2ecf20Sopenharmony_ci	nsim_bus_dev->dev.bus = &nsim_bus;
3298c2ecf20Sopenharmony_ci	nsim_bus_dev->dev.type = &nsim_bus_dev_type;
3308c2ecf20Sopenharmony_ci	nsim_bus_dev->port_count = port_count;
3318c2ecf20Sopenharmony_ci	nsim_bus_dev->initial_net = current->nsproxy->net_ns;
3328c2ecf20Sopenharmony_ci	mutex_init(&nsim_bus_dev->nsim_bus_reload_lock);
3338c2ecf20Sopenharmony_ci	/* Disallow using nsim_bus_dev */
3348c2ecf20Sopenharmony_ci	smp_store_release(&nsim_bus_dev->init, false);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	err = device_register(&nsim_bus_dev->dev);
3378c2ecf20Sopenharmony_ci	if (err)
3388c2ecf20Sopenharmony_ci		goto err_nsim_bus_dev_id_free;
3398c2ecf20Sopenharmony_ci	return nsim_bus_dev;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cierr_nsim_bus_dev_id_free:
3428c2ecf20Sopenharmony_ci	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
3438c2ecf20Sopenharmony_cierr_nsim_bus_dev_free:
3448c2ecf20Sopenharmony_ci	kfree(nsim_bus_dev);
3458c2ecf20Sopenharmony_ci	return ERR_PTR(err);
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	/* Disallow using nsim_bus_dev */
3518c2ecf20Sopenharmony_ci	smp_store_release(&nsim_bus_dev->init, false);
3528c2ecf20Sopenharmony_ci	device_unregister(&nsim_bus_dev->dev);
3538c2ecf20Sopenharmony_ci	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
3548c2ecf20Sopenharmony_ci	kfree(nsim_bus_dev);
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic struct device_driver nsim_driver = {
3588c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
3598c2ecf20Sopenharmony_ci	.bus		= &nsim_bus,
3608c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
3618c2ecf20Sopenharmony_ci};
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ciint nsim_bus_init(void)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	int err;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	err = bus_register(&nsim_bus);
3688c2ecf20Sopenharmony_ci	if (err)
3698c2ecf20Sopenharmony_ci		return err;
3708c2ecf20Sopenharmony_ci	err = driver_register(&nsim_driver);
3718c2ecf20Sopenharmony_ci	if (err)
3728c2ecf20Sopenharmony_ci		goto err_bus_unregister;
3738c2ecf20Sopenharmony_ci	/* Allow using resources */
3748c2ecf20Sopenharmony_ci	smp_store_release(&nsim_bus_enable, true);
3758c2ecf20Sopenharmony_ci	return 0;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cierr_bus_unregister:
3788c2ecf20Sopenharmony_ci	bus_unregister(&nsim_bus);
3798c2ecf20Sopenharmony_ci	return err;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_civoid nsim_bus_exit(void)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev, *tmp;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* Disallow using resources */
3878c2ecf20Sopenharmony_ci	smp_store_release(&nsim_bus_enable, false);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	mutex_lock(&nsim_bus_dev_list_lock);
3908c2ecf20Sopenharmony_ci	list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
3918c2ecf20Sopenharmony_ci		list_del(&nsim_bus_dev->list);
3928c2ecf20Sopenharmony_ci		nsim_bus_dev_del(nsim_bus_dev);
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_bus_dev_list_lock);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	driver_unregister(&nsim_driver);
3978c2ecf20Sopenharmony_ci	bus_unregister(&nsim_bus);
3988c2ecf20Sopenharmony_ci}
399