Loading...
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 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 struct attribute *nsim_bus_attrs[] = {
236 &bus_attr_new_device.attr,
237 &bus_attr_del_device.attr,
238 NULL
239};
240ATTRIBUTE_GROUPS(nsim_bus);
241
242static int nsim_bus_probe(struct device *dev)
243{
244 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
245
246 return nsim_drv_probe(nsim_bus_dev);
247}
248
249static void nsim_bus_remove(struct device *dev)
250{
251 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
252
253 nsim_drv_remove(nsim_bus_dev);
254}
255
256static int nsim_num_vf(struct device *dev)
257{
258 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
259
260 return nsim_bus_dev->num_vfs;
261}
262
263static struct bus_type nsim_bus = {
264 .name = DRV_NAME,
265 .dev_name = DRV_NAME,
266 .bus_groups = nsim_bus_groups,
267 .probe = nsim_bus_probe,
268 .remove = nsim_bus_remove,
269 .num_vf = nsim_num_vf,
270};
271
272#define NSIM_BUS_DEV_MAX_VFS 4
273
274static struct nsim_bus_dev *
275nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues)
276{
277 struct nsim_bus_dev *nsim_bus_dev;
278 int err;
279
280 nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL);
281 if (!nsim_bus_dev)
282 return ERR_PTR(-ENOMEM);
283
284 err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL);
285 if (err < 0)
286 goto err_nsim_bus_dev_free;
287 nsim_bus_dev->dev.id = err;
288 nsim_bus_dev->dev.bus = &nsim_bus;
289 nsim_bus_dev->dev.type = &nsim_bus_dev_type;
290 nsim_bus_dev->port_count = port_count;
291 nsim_bus_dev->num_queues = num_queues;
292 nsim_bus_dev->initial_net = current->nsproxy->net_ns;
293 nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS;
294 /* Disallow using nsim_bus_dev */
295 smp_store_release(&nsim_bus_dev->init, false);
296
297 err = device_register(&nsim_bus_dev->dev);
298 if (err)
299 goto err_nsim_bus_dev_id_free;
300
301 return nsim_bus_dev;
302
303err_nsim_bus_dev_id_free:
304 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
305 put_device(&nsim_bus_dev->dev);
306 nsim_bus_dev = NULL;
307err_nsim_bus_dev_free:
308 kfree(nsim_bus_dev);
309 return ERR_PTR(err);
310}
311
312static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
313{
314 /* Disallow using nsim_bus_dev */
315 smp_store_release(&nsim_bus_dev->init, false);
316 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
317 device_unregister(&nsim_bus_dev->dev);
318}
319
320static struct device_driver nsim_driver = {
321 .name = DRV_NAME,
322 .bus = &nsim_bus,
323 .owner = THIS_MODULE,
324};
325
326int nsim_bus_init(void)
327{
328 int err;
329
330 err = bus_register(&nsim_bus);
331 if (err)
332 return err;
333 err = driver_register(&nsim_driver);
334 if (err)
335 goto err_bus_unregister;
336 refcount_set(&nsim_bus_devs, 1);
337 /* Allow using resources */
338 smp_store_release(&nsim_bus_enable, true);
339 return 0;
340
341err_bus_unregister:
342 bus_unregister(&nsim_bus);
343 return err;
344}
345
346void nsim_bus_exit(void)
347{
348 struct nsim_bus_dev *nsim_bus_dev, *tmp;
349
350 /* Disallow using resources */
351 smp_store_release(&nsim_bus_enable, false);
352 if (refcount_dec_and_test(&nsim_bus_devs))
353 complete(&nsim_bus_devs_released);
354
355 mutex_lock(&nsim_bus_dev_list_lock);
356 list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
357 list_del(&nsim_bus_dev->list);
358 nsim_bus_dev_del(nsim_bus_dev);
359 }
360 mutex_unlock(&nsim_bus_dev_list_lock);
361
362 wait_for_completion(&nsim_bus_devs_released);
363
364 driver_unregister(&nsim_driver);
365 bus_unregister(&nsim_bus);
366}
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 nsim_bus_dev->vfconfigs = kcalloc(num_vfs,
31 sizeof(struct nsim_vf_config),
32 GFP_KERNEL | __GFP_NOWARN);
33 if (!nsim_bus_dev->vfconfigs)
34 return -ENOMEM;
35 nsim_bus_dev->num_vfs = num_vfs;
36
37 return 0;
38}
39
40static void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev)
41{
42 kfree(nsim_bus_dev->vfconfigs);
43 nsim_bus_dev->vfconfigs = NULL;
44 nsim_bus_dev->num_vfs = 0;
45}
46
47static ssize_t
48nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
49 const char *buf, size_t count)
50{
51 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
52 unsigned int num_vfs;
53 int ret;
54
55 ret = kstrtouint(buf, 0, &num_vfs);
56 if (ret)
57 return ret;
58
59 rtnl_lock();
60 if (nsim_bus_dev->num_vfs == num_vfs)
61 goto exit_good;
62 if (nsim_bus_dev->num_vfs && num_vfs) {
63 ret = -EBUSY;
64 goto exit_unlock;
65 }
66
67 if (num_vfs) {
68 ret = nsim_bus_dev_vfs_enable(nsim_bus_dev, num_vfs);
69 if (ret)
70 goto exit_unlock;
71 } else {
72 nsim_bus_dev_vfs_disable(nsim_bus_dev);
73 }
74exit_good:
75 ret = count;
76exit_unlock:
77 rtnl_unlock();
78
79 return ret;
80}
81
82static ssize_t
83nsim_bus_dev_numvfs_show(struct device *dev,
84 struct device_attribute *attr, char *buf)
85{
86 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
87
88 return sprintf(buf, "%u\n", nsim_bus_dev->num_vfs);
89}
90
91static struct device_attribute nsim_bus_dev_numvfs_attr =
92 __ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
93 nsim_bus_dev_numvfs_store);
94
95static ssize_t
96new_port_store(struct device *dev, struct device_attribute *attr,
97 const char *buf, size_t count)
98{
99 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
100 struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
101 struct devlink *devlink;
102 unsigned int port_index;
103 int ret;
104
105 /* Prevent to use nsim_bus_dev before initialization. */
106 if (!smp_load_acquire(&nsim_bus_dev->init))
107 return -EBUSY;
108 ret = kstrtouint(buf, 0, &port_index);
109 if (ret)
110 return ret;
111
112 devlink = priv_to_devlink(nsim_dev);
113
114 mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock);
115 devlink_reload_disable(devlink);
116 ret = nsim_dev_port_add(nsim_bus_dev, port_index);
117 devlink_reload_enable(devlink);
118 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
119 return ret ? ret : count;
120}
121
122static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
123
124static ssize_t
125del_port_store(struct device *dev, struct device_attribute *attr,
126 const char *buf, size_t count)
127{
128 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
129 struct nsim_dev *nsim_dev = dev_get_drvdata(dev);
130 struct devlink *devlink;
131 unsigned int port_index;
132 int ret;
133
134 /* Prevent to use nsim_bus_dev before initialization. */
135 if (!smp_load_acquire(&nsim_bus_dev->init))
136 return -EBUSY;
137 ret = kstrtouint(buf, 0, &port_index);
138 if (ret)
139 return ret;
140
141 devlink = priv_to_devlink(nsim_dev);
142
143 mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock);
144 devlink_reload_disable(devlink);
145 ret = nsim_dev_port_del(nsim_bus_dev, port_index);
146 devlink_reload_enable(devlink);
147 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
148 return ret ? ret : count;
149}
150
151static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
152
153static struct attribute *nsim_bus_dev_attrs[] = {
154 &nsim_bus_dev_numvfs_attr.attr,
155 &nsim_bus_dev_new_port_attr.attr,
156 &nsim_bus_dev_del_port_attr.attr,
157 NULL,
158};
159
160static const struct attribute_group nsim_bus_dev_attr_group = {
161 .attrs = nsim_bus_dev_attrs,
162};
163
164static const struct attribute_group *nsim_bus_dev_attr_groups[] = {
165 &nsim_bus_dev_attr_group,
166 NULL,
167};
168
169static void nsim_bus_dev_release(struct device *dev)
170{
171 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
172
173 nsim_bus_dev_vfs_disable(nsim_bus_dev);
174}
175
176static struct device_type nsim_bus_dev_type = {
177 .groups = nsim_bus_dev_attr_groups,
178 .release = nsim_bus_dev_release,
179};
180
181static struct nsim_bus_dev *
182nsim_bus_dev_new(unsigned int id, unsigned int port_count);
183
184static ssize_t
185new_device_store(struct bus_type *bus, const char *buf, size_t count)
186{
187 struct nsim_bus_dev *nsim_bus_dev;
188 unsigned int port_count;
189 unsigned int id;
190 int err;
191
192 err = sscanf(buf, "%u %u", &id, &port_count);
193 switch (err) {
194 case 1:
195 port_count = 1;
196 fallthrough;
197 case 2:
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 adding new device is \"id port_count\" (uint uint).\n");
205 return -EINVAL;
206 }
207
208 mutex_lock(&nsim_bus_dev_list_lock);
209 /* Prevent to use resource before initialization. */
210 if (!smp_load_acquire(&nsim_bus_enable)) {
211 err = -EBUSY;
212 goto err;
213 }
214
215 nsim_bus_dev = nsim_bus_dev_new(id, port_count);
216 if (IS_ERR(nsim_bus_dev)) {
217 err = PTR_ERR(nsim_bus_dev);
218 goto err;
219 }
220
221 /* Allow using nsim_bus_dev */
222 smp_store_release(&nsim_bus_dev->init, true);
223
224 list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list);
225 mutex_unlock(&nsim_bus_dev_list_lock);
226
227 return count;
228err:
229 mutex_unlock(&nsim_bus_dev_list_lock);
230 return err;
231}
232static BUS_ATTR_WO(new_device);
233
234static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
235
236static ssize_t
237del_device_store(struct bus_type *bus, const char *buf, size_t count)
238{
239 struct nsim_bus_dev *nsim_bus_dev, *tmp;
240 unsigned int id;
241 int err;
242
243 err = sscanf(buf, "%u", &id);
244 switch (err) {
245 case 1:
246 if (id > INT_MAX) {
247 pr_err("Value of \"id\" is too big.\n");
248 return -EINVAL;
249 }
250 break;
251 default:
252 pr_err("Format for deleting device is \"id\" (uint).\n");
253 return -EINVAL;
254 }
255
256 err = -ENOENT;
257 mutex_lock(&nsim_bus_dev_list_lock);
258 /* Prevent to use resource before initialization. */
259 if (!smp_load_acquire(&nsim_bus_enable)) {
260 mutex_unlock(&nsim_bus_dev_list_lock);
261 return -EBUSY;
262 }
263 list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
264 if (nsim_bus_dev->dev.id != id)
265 continue;
266 list_del(&nsim_bus_dev->list);
267 nsim_bus_dev_del(nsim_bus_dev);
268 err = 0;
269 break;
270 }
271 mutex_unlock(&nsim_bus_dev_list_lock);
272 return !err ? count : err;
273}
274static BUS_ATTR_WO(del_device);
275
276static struct attribute *nsim_bus_attrs[] = {
277 &bus_attr_new_device.attr,
278 &bus_attr_del_device.attr,
279 NULL
280};
281ATTRIBUTE_GROUPS(nsim_bus);
282
283static int nsim_bus_probe(struct device *dev)
284{
285 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
286
287 return nsim_dev_probe(nsim_bus_dev);
288}
289
290static int nsim_bus_remove(struct device *dev)
291{
292 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
293
294 nsim_dev_remove(nsim_bus_dev);
295 return 0;
296}
297
298static int nsim_num_vf(struct device *dev)
299{
300 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
301
302 return nsim_bus_dev->num_vfs;
303}
304
305static struct bus_type nsim_bus = {
306 .name = DRV_NAME,
307 .dev_name = DRV_NAME,
308 .bus_groups = nsim_bus_groups,
309 .probe = nsim_bus_probe,
310 .remove = nsim_bus_remove,
311 .num_vf = nsim_num_vf,
312};
313
314static struct nsim_bus_dev *
315nsim_bus_dev_new(unsigned int id, unsigned int port_count)
316{
317 struct nsim_bus_dev *nsim_bus_dev;
318 int err;
319
320 nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL);
321 if (!nsim_bus_dev)
322 return ERR_PTR(-ENOMEM);
323
324 err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL);
325 if (err < 0)
326 goto err_nsim_bus_dev_free;
327 nsim_bus_dev->dev.id = err;
328 nsim_bus_dev->dev.bus = &nsim_bus;
329 nsim_bus_dev->dev.type = &nsim_bus_dev_type;
330 nsim_bus_dev->port_count = port_count;
331 nsim_bus_dev->initial_net = current->nsproxy->net_ns;
332 mutex_init(&nsim_bus_dev->nsim_bus_reload_lock);
333 /* Disallow using nsim_bus_dev */
334 smp_store_release(&nsim_bus_dev->init, false);
335
336 err = device_register(&nsim_bus_dev->dev);
337 if (err)
338 goto err_nsim_bus_dev_id_free;
339 return nsim_bus_dev;
340
341err_nsim_bus_dev_id_free:
342 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
343err_nsim_bus_dev_free:
344 kfree(nsim_bus_dev);
345 return ERR_PTR(err);
346}
347
348static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
349{
350 /* Disallow using nsim_bus_dev */
351 smp_store_release(&nsim_bus_dev->init, false);
352 device_unregister(&nsim_bus_dev->dev);
353 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
354 kfree(nsim_bus_dev);
355}
356
357static struct device_driver nsim_driver = {
358 .name = DRV_NAME,
359 .bus = &nsim_bus,
360 .owner = THIS_MODULE,
361};
362
363int nsim_bus_init(void)
364{
365 int err;
366
367 err = bus_register(&nsim_bus);
368 if (err)
369 return err;
370 err = driver_register(&nsim_driver);
371 if (err)
372 goto err_bus_unregister;
373 /* Allow using resources */
374 smp_store_release(&nsim_bus_enable, true);
375 return 0;
376
377err_bus_unregister:
378 bus_unregister(&nsim_bus);
379 return err;
380}
381
382void nsim_bus_exit(void)
383{
384 struct nsim_bus_dev *nsim_bus_dev, *tmp;
385
386 /* Disallow using resources */
387 smp_store_release(&nsim_bus_enable, false);
388
389 mutex_lock(&nsim_bus_dev_list_lock);
390 list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
391 list_del(&nsim_bus_dev->list);
392 nsim_bus_dev_del(nsim_bus_dev);
393 }
394 mutex_unlock(&nsim_bus_dev_list_lock);
395
396 driver_unregister(&nsim_driver);
397 bus_unregister(&nsim_bus);
398}