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
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);
24
to_nsim_bus_dev(struct device * dev)25 static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
26 {
27 return container_of(dev, struct nsim_bus_dev, dev);
28 }
29
30 static ssize_t
nsim_bus_dev_numvfs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)31 nsim_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
51 static ssize_t
nsim_bus_dev_numvfs_show(struct device * dev,struct device_attribute * attr,char * buf)52 nsim_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
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);
63
64 static ssize_t
new_port_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)65 new_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
83 static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
84
85 static ssize_t
del_port_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)86 del_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
104 static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
105
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,
110 NULL,
111 };
112
113 static const struct attribute_group nsim_bus_dev_attr_group = {
114 .attrs = nsim_bus_dev_attrs,
115 };
116
117 static const struct attribute_group *nsim_bus_dev_attr_groups[] = {
118 &nsim_bus_dev_attr_group,
119 NULL,
120 };
121
nsim_bus_dev_release(struct device * dev)122 static 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
132 static struct device_type nsim_bus_dev_type = {
133 .groups = nsim_bus_dev_attr_groups,
134 .release = nsim_bus_dev_release,
135 };
136
137 static struct nsim_bus_dev *
138 nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues);
139
140 static ssize_t
new_device_store(const struct bus_type * bus,const char * buf,size_t count)141 new_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;
187 err:
188 mutex_unlock(&nsim_bus_dev_list_lock);
189 return err;
190 }
191 static BUS_ATTR_WO(new_device);
192
193 static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
194
195 static ssize_t
del_device_store(const struct bus_type * bus,const char * buf,size_t count)196 del_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 }
233 static BUS_ATTR_WO(del_device);
234
235 static struct attribute *nsim_bus_attrs[] = {
236 &bus_attr_new_device.attr,
237 &bus_attr_del_device.attr,
238 NULL
239 };
240 ATTRIBUTE_GROUPS(nsim_bus);
241
nsim_bus_probe(struct device * dev)242 static 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
nsim_bus_remove(struct device * dev)249 static 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
nsim_num_vf(struct device * dev)256 static 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
263 static 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
274 static struct nsim_bus_dev *
nsim_bus_dev_new(unsigned int id,unsigned int port_count,unsigned int num_queues)275 nsim_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
303 err_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;
307 err_nsim_bus_dev_free:
308 kfree(nsim_bus_dev);
309 return ERR_PTR(err);
310 }
311
nsim_bus_dev_del(struct nsim_bus_dev * nsim_bus_dev)312 static 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
320 static struct device_driver nsim_driver = {
321 .name = DRV_NAME,
322 .bus = &nsim_bus,
323 .owner = THIS_MODULE,
324 };
325
nsim_bus_init(void)326 int 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
341 err_bus_unregister:
342 bus_unregister(&nsim_bus);
343 return err;
344 }
345
nsim_bus_exit(void)346 void 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 }
367