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/slab.h> 12 #include <linux/sysfs.h> 13 14 #include "netdevsim.h" 15 16 static DEFINE_IDA(nsim_bus_dev_ids); 17 static LIST_HEAD(nsim_bus_dev_list); 18 static DEFINE_MUTEX(nsim_bus_dev_list_lock); 19 static bool nsim_bus_enable; 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 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 38 device_lock(dev); 39 ret = -ENOENT; 40 if (dev_get_drvdata(dev)) 41 ret = nsim_drv_configure_vfs(nsim_bus_dev, num_vfs); 42 device_unlock(dev); 43 44 return ret ? ret : count; 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 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 68 /* Prevent to use nsim_bus_dev before initialization. */ 69 if (!smp_load_acquire(&nsim_bus_dev->init)) 70 return -EBUSY; 71 ret = kstrtouint(buf, 0, &port_index); 72 if (ret) 73 return ret; 74 75 ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index); 76 return ret ? ret : count; 77 } 78 79 static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port); 80 81 static ssize_t 82 del_port_store(struct device *dev, struct device_attribute *attr, 83 const char *buf, size_t count) 84 { 85 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); 86 unsigned int port_index; 87 int ret; 88 89 /* Prevent to use nsim_bus_dev before initialization. */ 90 if (!smp_load_acquire(&nsim_bus_dev->init)) 91 return -EBUSY; 92 ret = kstrtouint(buf, 0, &port_index); 93 if (ret) 94 return ret; 95 96 ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index); 97 return ret ? ret : count; 98 } 99 100 static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port); 101 102 static struct attribute *nsim_bus_dev_attrs[] = { 103 &nsim_bus_dev_numvfs_attr.attr, 104 &nsim_bus_dev_new_port_attr.attr, 105 &nsim_bus_dev_del_port_attr.attr, 106 NULL, 107 }; 108 109 static const struct attribute_group nsim_bus_dev_attr_group = { 110 .attrs = nsim_bus_dev_attrs, 111 }; 112 113 static const struct attribute_group *nsim_bus_dev_attr_groups[] = { 114 &nsim_bus_dev_attr_group, 115 NULL, 116 }; 117 118 static void nsim_bus_dev_release(struct device *dev) 119 { 120 } 121 122 static struct device_type nsim_bus_dev_type = { 123 .groups = nsim_bus_dev_attr_groups, 124 .release = nsim_bus_dev_release, 125 }; 126 127 static struct nsim_bus_dev * 128 nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues); 129 130 static ssize_t 131 new_device_store(struct bus_type *bus, const char *buf, size_t count) 132 { 133 unsigned int id, port_count, num_queues; 134 struct nsim_bus_dev *nsim_bus_dev; 135 int err; 136 137 err = sscanf(buf, "%u %u %u", &id, &port_count, &num_queues); 138 switch (err) { 139 case 1: 140 port_count = 1; 141 fallthrough; 142 case 2: 143 num_queues = 1; 144 fallthrough; 145 case 3: 146 if (id > INT_MAX) { 147 pr_err("Value of \"id\" is too big.\n"); 148 return -EINVAL; 149 } 150 break; 151 default: 152 pr_err("Format for adding new device is \"id port_count num_queues\" (uint uint unit).\n"); 153 return -EINVAL; 154 } 155 156 mutex_lock(&nsim_bus_dev_list_lock); 157 /* Prevent to use resource before initialization. */ 158 if (!smp_load_acquire(&nsim_bus_enable)) { 159 err = -EBUSY; 160 goto err; 161 } 162 163 nsim_bus_dev = nsim_bus_dev_new(id, port_count, num_queues); 164 if (IS_ERR(nsim_bus_dev)) { 165 err = PTR_ERR(nsim_bus_dev); 166 goto err; 167 } 168 169 /* Allow using nsim_bus_dev */ 170 smp_store_release(&nsim_bus_dev->init, true); 171 172 list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list); 173 mutex_unlock(&nsim_bus_dev_list_lock); 174 175 return count; 176 err: 177 mutex_unlock(&nsim_bus_dev_list_lock); 178 return err; 179 } 180 static BUS_ATTR_WO(new_device); 181 182 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev); 183 184 static ssize_t 185 del_device_store(struct bus_type *bus, const char *buf, size_t count) 186 { 187 struct nsim_bus_dev *nsim_bus_dev, *tmp; 188 unsigned int id; 189 int err; 190 191 err = sscanf(buf, "%u", &id); 192 switch (err) { 193 case 1: 194 if (id > INT_MAX) { 195 pr_err("Value of \"id\" is too big.\n"); 196 return -EINVAL; 197 } 198 break; 199 default: 200 pr_err("Format for deleting device is \"id\" (uint).\n"); 201 return -EINVAL; 202 } 203 204 err = -ENOENT; 205 mutex_lock(&nsim_bus_dev_list_lock); 206 /* Prevent to use resource before initialization. */ 207 if (!smp_load_acquire(&nsim_bus_enable)) { 208 mutex_unlock(&nsim_bus_dev_list_lock); 209 return -EBUSY; 210 } 211 list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) { 212 if (nsim_bus_dev->dev.id != id) 213 continue; 214 list_del(&nsim_bus_dev->list); 215 nsim_bus_dev_del(nsim_bus_dev); 216 err = 0; 217 break; 218 } 219 mutex_unlock(&nsim_bus_dev_list_lock); 220 return !err ? count : err; 221 } 222 static BUS_ATTR_WO(del_device); 223 224 static struct attribute *nsim_bus_attrs[] = { 225 &bus_attr_new_device.attr, 226 &bus_attr_del_device.attr, 227 NULL 228 }; 229 ATTRIBUTE_GROUPS(nsim_bus); 230 231 static int nsim_bus_probe(struct device *dev) 232 { 233 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); 234 235 return nsim_drv_probe(nsim_bus_dev); 236 } 237 238 static void nsim_bus_remove(struct device *dev) 239 { 240 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); 241 242 nsim_drv_remove(nsim_bus_dev); 243 } 244 245 static int nsim_num_vf(struct device *dev) 246 { 247 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev); 248 249 return nsim_bus_dev->num_vfs; 250 } 251 252 static struct bus_type nsim_bus = { 253 .name = DRV_NAME, 254 .dev_name = DRV_NAME, 255 .bus_groups = nsim_bus_groups, 256 .probe = nsim_bus_probe, 257 .remove = nsim_bus_remove, 258 .num_vf = nsim_num_vf, 259 }; 260 261 #define NSIM_BUS_DEV_MAX_VFS 4 262 263 static struct nsim_bus_dev * 264 nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues) 265 { 266 struct nsim_bus_dev *nsim_bus_dev; 267 int err; 268 269 nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL); 270 if (!nsim_bus_dev) 271 return ERR_PTR(-ENOMEM); 272 273 err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL); 274 if (err < 0) 275 goto err_nsim_bus_dev_free; 276 nsim_bus_dev->dev.id = err; 277 nsim_bus_dev->dev.bus = &nsim_bus; 278 nsim_bus_dev->dev.type = &nsim_bus_dev_type; 279 nsim_bus_dev->port_count = port_count; 280 nsim_bus_dev->num_queues = num_queues; 281 nsim_bus_dev->initial_net = current->nsproxy->net_ns; 282 nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS; 283 /* Disallow using nsim_bus_dev */ 284 smp_store_release(&nsim_bus_dev->init, false); 285 286 err = device_register(&nsim_bus_dev->dev); 287 if (err) 288 goto err_nsim_bus_dev_id_free; 289 290 return nsim_bus_dev; 291 292 err_nsim_bus_dev_id_free: 293 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id); 294 err_nsim_bus_dev_free: 295 kfree(nsim_bus_dev); 296 return ERR_PTR(err); 297 } 298 299 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev) 300 { 301 /* Disallow using nsim_bus_dev */ 302 smp_store_release(&nsim_bus_dev->init, false); 303 device_unregister(&nsim_bus_dev->dev); 304 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id); 305 kfree(nsim_bus_dev); 306 } 307 308 static struct device_driver nsim_driver = { 309 .name = DRV_NAME, 310 .bus = &nsim_bus, 311 .owner = THIS_MODULE, 312 }; 313 314 int nsim_bus_init(void) 315 { 316 int err; 317 318 err = bus_register(&nsim_bus); 319 if (err) 320 return err; 321 err = driver_register(&nsim_driver); 322 if (err) 323 goto err_bus_unregister; 324 /* Allow using resources */ 325 smp_store_release(&nsim_bus_enable, true); 326 return 0; 327 328 err_bus_unregister: 329 bus_unregister(&nsim_bus); 330 return err; 331 } 332 333 void nsim_bus_exit(void) 334 { 335 struct nsim_bus_dev *nsim_bus_dev, *tmp; 336 337 /* Disallow using resources */ 338 smp_store_release(&nsim_bus_enable, false); 339 340 mutex_lock(&nsim_bus_dev_list_lock); 341 list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) { 342 list_del(&nsim_bus_dev->list); 343 nsim_bus_dev_del(nsim_bus_dev); 344 } 345 mutex_unlock(&nsim_bus_dev_list_lock); 346 347 driver_unregister(&nsim_driver); 348 bus_unregister(&nsim_bus); 349 } 350