Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 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> |
Jiri Pirko | 57ce977 | 2019-04-25 15:59:47 +0200 | [diff] [blame] | 7 | #include <linux/idr.h> |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 9 | #include <linux/list.h> |
| 10 | #include <linux/mutex.h> |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/sysfs.h> |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 13 | |
| 14 | #include "netdevsim.h" |
| 15 | |
Jiri Pirko | 57ce977 | 2019-04-25 15:59:47 +0200 | [diff] [blame] | 16 | static DEFINE_IDA(nsim_bus_dev_ids); |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 17 | static LIST_HEAD(nsim_bus_dev_list); |
| 18 | static DEFINE_MUTEX(nsim_bus_dev_list_lock); |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 19 | static bool nsim_bus_enable; |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 20 | |
| 21 | static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev) |
| 22 | { |
| 23 | return container_of(dev, struct nsim_bus_dev, dev); |
| 24 | } |
| 25 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 26 | static ssize_t |
| 27 | nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr, |
| 28 | const char *buf, size_t count) |
| 29 | { |
| 30 | struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); |
| 31 | unsigned int num_vfs; |
| 32 | int ret; |
| 33 | |
| 34 | ret = kstrtouint(buf, 0, &num_vfs); |
| 35 | if (ret) |
| 36 | return ret; |
| 37 | |
Jakub Kicinski | 5e388f3 | 2021-10-30 16:15:02 -0700 | [diff] [blame] | 38 | device_lock(dev); |
Jakub Kicinski | 1c40107 | 2021-10-30 16:15:03 -0700 | [diff] [blame] | 39 | ret = -ENOENT; |
| 40 | if (dev_get_drvdata(dev)) |
| 41 | ret = nsim_drv_configure_vfs(nsim_bus_dev, num_vfs); |
Jakub Kicinski | 5e388f3 | 2021-10-30 16:15:02 -0700 | [diff] [blame] | 42 | device_unlock(dev); |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 43 | |
Jakub Kicinski | 1c40107 | 2021-10-30 16:15:03 -0700 | [diff] [blame] | 44 | return ret ? ret : count; |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static ssize_t |
| 48 | nsim_bus_dev_numvfs_show(struct device *dev, |
| 49 | struct device_attribute *attr, char *buf) |
| 50 | { |
| 51 | struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); |
| 52 | |
| 53 | return sprintf(buf, "%u\n", nsim_bus_dev->num_vfs); |
| 54 | } |
| 55 | |
| 56 | static struct device_attribute nsim_bus_dev_numvfs_attr = |
| 57 | __ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show, |
| 58 | nsim_bus_dev_numvfs_store); |
| 59 | |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 60 | static ssize_t |
| 61 | new_port_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 port_index; |
| 66 | int ret; |
| 67 | |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 68 | /* Prevent to use nsim_bus_dev before initialization. */ |
| 69 | if (!smp_load_acquire(&nsim_bus_dev->init)) |
| 70 | return -EBUSY; |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 71 | ret = kstrtouint(buf, 0, &port_index); |
| 72 | if (ret) |
| 73 | return ret; |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 74 | |
Leon Romanovsky | 23809a7 | 2021-08-05 14:02:45 +0300 | [diff] [blame] | 75 | if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock)) |
| 76 | return -EBUSY; |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 77 | |
Leon Romanovsky | 5c0418e | 2021-08-05 17:34:28 +0300 | [diff] [blame] | 78 | if (nsim_bus_dev->in_reload) { |
| 79 | mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); |
| 80 | return -EBUSY; |
| 81 | } |
| 82 | |
Jakub Kicinski | a66f64b | 2021-10-30 16:15:05 -0700 | [diff] [blame] | 83 | ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index); |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 84 | mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 85 | return ret ? ret : count; |
| 86 | } |
| 87 | |
| 88 | static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port); |
| 89 | |
| 90 | static ssize_t |
| 91 | del_port_store(struct device *dev, struct device_attribute *attr, |
| 92 | const char *buf, size_t count) |
| 93 | { |
| 94 | struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); |
| 95 | unsigned int port_index; |
| 96 | int ret; |
| 97 | |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 98 | /* Prevent to use nsim_bus_dev before initialization. */ |
| 99 | if (!smp_load_acquire(&nsim_bus_dev->init)) |
| 100 | return -EBUSY; |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 101 | ret = kstrtouint(buf, 0, &port_index); |
| 102 | if (ret) |
| 103 | return ret; |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 104 | |
Leon Romanovsky | 23809a7 | 2021-08-05 14:02:45 +0300 | [diff] [blame] | 105 | if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock)) |
| 106 | return -EBUSY; |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 107 | |
Leon Romanovsky | 5c0418e | 2021-08-05 17:34:28 +0300 | [diff] [blame] | 108 | if (nsim_bus_dev->in_reload) { |
| 109 | mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); |
| 110 | return -EBUSY; |
| 111 | } |
| 112 | |
Jakub Kicinski | a66f64b | 2021-10-30 16:15:05 -0700 | [diff] [blame] | 113 | ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index); |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 114 | mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 115 | return ret ? ret : count; |
| 116 | } |
| 117 | |
| 118 | static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port); |
| 119 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 120 | static struct attribute *nsim_bus_dev_attrs[] = { |
| 121 | &nsim_bus_dev_numvfs_attr.attr, |
Jiri Pirko | 794b2c0 | 2019-04-25 15:59:54 +0200 | [diff] [blame] | 122 | &nsim_bus_dev_new_port_attr.attr, |
| 123 | &nsim_bus_dev_del_port_attr.attr, |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 124 | NULL, |
| 125 | }; |
| 126 | |
| 127 | static const struct attribute_group nsim_bus_dev_attr_group = { |
| 128 | .attrs = nsim_bus_dev_attrs, |
| 129 | }; |
| 130 | |
| 131 | static const struct attribute_group *nsim_bus_dev_attr_groups[] = { |
| 132 | &nsim_bus_dev_attr_group, |
| 133 | NULL, |
| 134 | }; |
| 135 | |
| 136 | static void nsim_bus_dev_release(struct device *dev) |
| 137 | { |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static struct device_type nsim_bus_dev_type = { |
| 141 | .groups = nsim_bus_dev_attr_groups, |
| 142 | .release = nsim_bus_dev_release, |
| 143 | }; |
| 144 | |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 145 | static struct nsim_bus_dev * |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 146 | nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues); |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 147 | |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 148 | static ssize_t |
| 149 | new_device_store(struct bus_type *bus, const char *buf, size_t count) |
| 150 | { |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 151 | unsigned int id, port_count, num_queues; |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 152 | struct nsim_bus_dev *nsim_bus_dev; |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 153 | int err; |
| 154 | |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 155 | err = sscanf(buf, "%u %u %u", &id, &port_count, &num_queues); |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 156 | switch (err) { |
| 157 | case 1: |
| 158 | port_count = 1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 159 | fallthrough; |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 160 | case 2: |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 161 | num_queues = 1; |
| 162 | fallthrough; |
| 163 | case 3: |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 164 | if (id > INT_MAX) { |
| 165 | pr_err("Value of \"id\" is too big.\n"); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | break; |
| 169 | default: |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 170 | pr_err("Format for adding new device is \"id port_count num_queues\" (uint uint unit).\n"); |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 171 | return -EINVAL; |
| 172 | } |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 173 | |
| 174 | mutex_lock(&nsim_bus_dev_list_lock); |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 175 | /* Prevent to use resource before initialization. */ |
| 176 | if (!smp_load_acquire(&nsim_bus_enable)) { |
| 177 | err = -EBUSY; |
| 178 | goto err; |
| 179 | } |
| 180 | |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 181 | nsim_bus_dev = nsim_bus_dev_new(id, port_count, num_queues); |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 182 | if (IS_ERR(nsim_bus_dev)) { |
| 183 | err = PTR_ERR(nsim_bus_dev); |
| 184 | goto err; |
| 185 | } |
| 186 | |
| 187 | /* Allow using nsim_bus_dev */ |
| 188 | smp_store_release(&nsim_bus_dev->init, true); |
| 189 | |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 190 | list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list); |
| 191 | mutex_unlock(&nsim_bus_dev_list_lock); |
| 192 | |
| 193 | return count; |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 194 | err: |
| 195 | mutex_unlock(&nsim_bus_dev_list_lock); |
| 196 | return err; |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 197 | } |
| 198 | static BUS_ATTR_WO(new_device); |
| 199 | |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 200 | static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev); |
| 201 | |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 202 | static ssize_t |
| 203 | del_device_store(struct bus_type *bus, const char *buf, size_t count) |
| 204 | { |
| 205 | struct nsim_bus_dev *nsim_bus_dev, *tmp; |
| 206 | unsigned int id; |
| 207 | int err; |
| 208 | |
| 209 | err = sscanf(buf, "%u", &id); |
| 210 | switch (err) { |
| 211 | case 1: |
| 212 | if (id > INT_MAX) { |
| 213 | pr_err("Value of \"id\" is too big.\n"); |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | break; |
| 217 | default: |
| 218 | pr_err("Format for deleting device is \"id\" (uint).\n"); |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | err = -ENOENT; |
| 223 | mutex_lock(&nsim_bus_dev_list_lock); |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 224 | /* Prevent to use resource before initialization. */ |
| 225 | if (!smp_load_acquire(&nsim_bus_enable)) { |
| 226 | mutex_unlock(&nsim_bus_dev_list_lock); |
| 227 | return -EBUSY; |
| 228 | } |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 229 | list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) { |
| 230 | if (nsim_bus_dev->dev.id != id) |
| 231 | continue; |
| 232 | list_del(&nsim_bus_dev->list); |
| 233 | nsim_bus_dev_del(nsim_bus_dev); |
| 234 | err = 0; |
| 235 | break; |
| 236 | } |
| 237 | mutex_unlock(&nsim_bus_dev_list_lock); |
| 238 | return !err ? count : err; |
| 239 | } |
| 240 | static BUS_ATTR_WO(del_device); |
| 241 | |
| 242 | static struct attribute *nsim_bus_attrs[] = { |
| 243 | &bus_attr_new_device.attr, |
| 244 | &bus_attr_del_device.attr, |
| 245 | NULL |
| 246 | }; |
| 247 | ATTRIBUTE_GROUPS(nsim_bus); |
| 248 | |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 249 | static int nsim_bus_probe(struct device *dev) |
| 250 | { |
| 251 | struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); |
| 252 | |
Jakub Kicinski | a66f64b | 2021-10-30 16:15:05 -0700 | [diff] [blame] | 253 | return nsim_drv_probe(nsim_bus_dev); |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 254 | } |
| 255 | |
Uwe Kleine-König | fc7a620 | 2021-07-13 21:35:22 +0200 | [diff] [blame] | 256 | static void nsim_bus_remove(struct device *dev) |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 257 | { |
| 258 | struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); |
| 259 | |
Jakub Kicinski | a66f64b | 2021-10-30 16:15:05 -0700 | [diff] [blame] | 260 | nsim_drv_remove(nsim_bus_dev); |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 261 | } |
| 262 | |
YueHaibing | 69bbbdc | 2019-05-04 16:12:07 +0800 | [diff] [blame] | 263 | static int nsim_num_vf(struct device *dev) |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 264 | { |
| 265 | struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); |
| 266 | |
| 267 | return nsim_bus_dev->num_vfs; |
| 268 | } |
| 269 | |
| 270 | static struct bus_type nsim_bus = { |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 271 | .name = DRV_NAME, |
| 272 | .dev_name = DRV_NAME, |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 273 | .bus_groups = nsim_bus_groups, |
Jiri Pirko | 8320d14 | 2019-04-25 15:59:53 +0200 | [diff] [blame] | 274 | .probe = nsim_bus_probe, |
| 275 | .remove = nsim_bus_remove, |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 276 | .num_vf = nsim_num_vf, |
| 277 | }; |
| 278 | |
Dmytro Linkin | d395381 | 2021-06-02 15:17:14 +0300 | [diff] [blame] | 279 | #define NSIM_BUS_DEV_MAX_VFS 4 |
| 280 | |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 281 | static struct nsim_bus_dev * |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 282 | nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues) |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 283 | { |
| 284 | struct nsim_bus_dev *nsim_bus_dev; |
| 285 | int err; |
| 286 | |
| 287 | nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL); |
| 288 | if (!nsim_bus_dev) |
| 289 | return ERR_PTR(-ENOMEM); |
| 290 | |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 291 | err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL); |
Jiri Pirko | 57ce977 | 2019-04-25 15:59:47 +0200 | [diff] [blame] | 292 | if (err < 0) |
| 293 | goto err_nsim_bus_dev_free; |
| 294 | nsim_bus_dev->dev.id = err; |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 295 | nsim_bus_dev->dev.bus = &nsim_bus; |
| 296 | nsim_bus_dev->dev.type = &nsim_bus_dev_type; |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 297 | nsim_bus_dev->port_count = port_count; |
Peilin Ye | d4861fc | 2021-07-15 18:52:45 -0700 | [diff] [blame] | 298 | nsim_bus_dev->num_queues = num_queues; |
Jiri Pirko | 7b60027 | 2019-10-05 08:10:32 +0200 | [diff] [blame] | 299 | nsim_bus_dev->initial_net = current->nsproxy->net_ns; |
Dmytro Linkin | d395381 | 2021-06-02 15:17:14 +0300 | [diff] [blame] | 300 | nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS; |
Taehee Yoo | 6ab6336 | 2020-02-01 16:43:04 +0000 | [diff] [blame] | 301 | mutex_init(&nsim_bus_dev->nsim_bus_reload_lock); |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 302 | /* Disallow using nsim_bus_dev */ |
| 303 | smp_store_release(&nsim_bus_dev->init, false); |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 304 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 305 | err = device_register(&nsim_bus_dev->dev); |
| 306 | if (err) |
Jakub Kicinski | 5e388f3 | 2021-10-30 16:15:02 -0700 | [diff] [blame] | 307 | goto err_nsim_bus_dev_id_free; |
Dmytro Linkin | d395381 | 2021-06-02 15:17:14 +0300 | [diff] [blame] | 308 | |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 309 | return nsim_bus_dev; |
| 310 | |
Jiri Pirko | 57ce977 | 2019-04-25 15:59:47 +0200 | [diff] [blame] | 311 | err_nsim_bus_dev_id_free: |
| 312 | ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id); |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 313 | err_nsim_bus_dev_free: |
| 314 | kfree(nsim_bus_dev); |
| 315 | return ERR_PTR(err); |
| 316 | } |
| 317 | |
Jiri Pirko | e05b2d1 | 2019-04-25 15:59:55 +0200 | [diff] [blame] | 318 | static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev) |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 319 | { |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 320 | /* Disallow using nsim_bus_dev */ |
| 321 | smp_store_release(&nsim_bus_dev->init, false); |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 322 | device_unregister(&nsim_bus_dev->dev); |
Jiri Pirko | 57ce977 | 2019-04-25 15:59:47 +0200 | [diff] [blame] | 323 | ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id); |
Jiri Pirko | 40e4fe4 | 2019-04-25 15:59:45 +0200 | [diff] [blame] | 324 | kfree(nsim_bus_dev); |
| 325 | } |
| 326 | |
Jiri Pirko | 23d415d | 2019-04-25 15:59:46 +0200 | [diff] [blame] | 327 | static struct device_driver nsim_driver = { |
| 328 | .name = DRV_NAME, |
| 329 | .bus = &nsim_bus, |
| 330 | .owner = THIS_MODULE, |
| 331 | }; |
| 332 | |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 333 | int nsim_bus_init(void) |
| 334 | { |
Jiri Pirko | 23d415d | 2019-04-25 15:59:46 +0200 | [diff] [blame] | 335 | int err; |
| 336 | |
| 337 | err = bus_register(&nsim_bus); |
| 338 | if (err) |
| 339 | return err; |
| 340 | err = driver_register(&nsim_driver); |
| 341 | if (err) |
| 342 | goto err_bus_unregister; |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 343 | /* Allow using resources */ |
| 344 | smp_store_release(&nsim_bus_enable, true); |
Jiri Pirko | 23d415d | 2019-04-25 15:59:46 +0200 | [diff] [blame] | 345 | return 0; |
| 346 | |
| 347 | err_bus_unregister: |
| 348 | bus_unregister(&nsim_bus); |
| 349 | return err; |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void nsim_bus_exit(void) |
| 353 | { |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 354 | struct nsim_bus_dev *nsim_bus_dev, *tmp; |
| 355 | |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 356 | /* Disallow using resources */ |
| 357 | smp_store_release(&nsim_bus_enable, false); |
| 358 | |
Jiri Pirko | f9d9db4 | 2019-04-25 15:59:48 +0200 | [diff] [blame] | 359 | mutex_lock(&nsim_bus_dev_list_lock); |
| 360 | list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) { |
| 361 | list_del(&nsim_bus_dev->list); |
| 362 | nsim_bus_dev_del(nsim_bus_dev); |
| 363 | } |
| 364 | mutex_unlock(&nsim_bus_dev_list_lock); |
Taehee Yoo | f5cd216 | 2020-02-01 16:42:54 +0000 | [diff] [blame] | 365 | |
Jiri Pirko | 23d415d | 2019-04-25 15:59:46 +0200 | [diff] [blame] | 366 | driver_unregister(&nsim_driver); |
Jiri Pirko | 925f5af | 2019-04-25 15:59:44 +0200 | [diff] [blame] | 367 | bus_unregister(&nsim_bus); |
| 368 | } |