blob: 25cb2e600d530ada315e493f9bfdebdadbecb25b [file] [log] [blame]
Jiri Pirko925f5af2019-04-25 15:59:44 +02001// 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 Pirko57ce9772019-04-25 15:59:47 +02007#include <linux/idr.h>
Jiri Pirko40e4fe42019-04-25 15:59:45 +02008#include <linux/kernel.h>
Jiri Pirkof9d9db42019-04-25 15:59:48 +02009#include <linux/list.h>
10#include <linux/mutex.h>
Jiri Pirko40e4fe42019-04-25 15:59:45 +020011#include <linux/slab.h>
12#include <linux/sysfs.h>
Jiri Pirko925f5af2019-04-25 15:59:44 +020013
14#include "netdevsim.h"
15
Jiri Pirko57ce9772019-04-25 15:59:47 +020016static DEFINE_IDA(nsim_bus_dev_ids);
Jiri Pirkof9d9db42019-04-25 15:59:48 +020017static LIST_HEAD(nsim_bus_dev_list);
18static DEFINE_MUTEX(nsim_bus_dev_list_lock);
Taehee Yoof5cd2162020-02-01 16:42:54 +000019static bool nsim_bus_enable;
Jiri Pirko40e4fe42019-04-25 15:59:45 +020020
21static 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 Pirko40e4fe42019-04-25 15:59:45 +020026static ssize_t
27nsim_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 Kicinski5e388f32021-10-30 16:15:02 -070038 device_lock(dev);
Jakub Kicinski1c401072021-10-30 16:15:03 -070039 ret = -ENOENT;
40 if (dev_get_drvdata(dev))
41 ret = nsim_drv_configure_vfs(nsim_bus_dev, num_vfs);
Jakub Kicinski5e388f32021-10-30 16:15:02 -070042 device_unlock(dev);
Jiri Pirko40e4fe42019-04-25 15:59:45 +020043
Jakub Kicinski1c401072021-10-30 16:15:03 -070044 return ret ? ret : count;
Jiri Pirko40e4fe42019-04-25 15:59:45 +020045}
46
47static ssize_t
48nsim_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
56static 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 Pirko794b2c02019-04-25 15:59:54 +020060static ssize_t
61new_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 Yoof5cd2162020-02-01 16:42:54 +000068 /* Prevent to use nsim_bus_dev before initialization. */
69 if (!smp_load_acquire(&nsim_bus_dev->init))
70 return -EBUSY;
Jiri Pirko794b2c02019-04-25 15:59:54 +020071 ret = kstrtouint(buf, 0, &port_index);
72 if (ret)
73 return ret;
Taehee Yoo6ab63362020-02-01 16:43:04 +000074
Leon Romanovsky23809a72021-08-05 14:02:45 +030075 if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock))
76 return -EBUSY;
Taehee Yoo6ab63362020-02-01 16:43:04 +000077
Leon Romanovsky5c0418e2021-08-05 17:34:28 +030078 if (nsim_bus_dev->in_reload) {
79 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
80 return -EBUSY;
81 }
82
Jakub Kicinskia66f64b2021-10-30 16:15:05 -070083 ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
Taehee Yoo6ab63362020-02-01 16:43:04 +000084 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
Jiri Pirko794b2c02019-04-25 15:59:54 +020085 return ret ? ret : count;
86}
87
88static struct device_attribute nsim_bus_dev_new_port_attr = __ATTR_WO(new_port);
89
90static ssize_t
91del_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 Yoof5cd2162020-02-01 16:42:54 +000098 /* Prevent to use nsim_bus_dev before initialization. */
99 if (!smp_load_acquire(&nsim_bus_dev->init))
100 return -EBUSY;
Jiri Pirko794b2c02019-04-25 15:59:54 +0200101 ret = kstrtouint(buf, 0, &port_index);
102 if (ret)
103 return ret;
Taehee Yoo6ab63362020-02-01 16:43:04 +0000104
Leon Romanovsky23809a72021-08-05 14:02:45 +0300105 if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock))
106 return -EBUSY;
Taehee Yoo6ab63362020-02-01 16:43:04 +0000107
Leon Romanovsky5c0418e2021-08-05 17:34:28 +0300108 if (nsim_bus_dev->in_reload) {
109 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
110 return -EBUSY;
111 }
112
Jakub Kicinskia66f64b2021-10-30 16:15:05 -0700113 ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
Taehee Yoo6ab63362020-02-01 16:43:04 +0000114 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
Jiri Pirko794b2c02019-04-25 15:59:54 +0200115 return ret ? ret : count;
116}
117
118static struct device_attribute nsim_bus_dev_del_port_attr = __ATTR_WO(del_port);
119
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200120static struct attribute *nsim_bus_dev_attrs[] = {
121 &nsim_bus_dev_numvfs_attr.attr,
Jiri Pirko794b2c02019-04-25 15:59:54 +0200122 &nsim_bus_dev_new_port_attr.attr,
123 &nsim_bus_dev_del_port_attr.attr,
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200124 NULL,
125};
126
127static const struct attribute_group nsim_bus_dev_attr_group = {
128 .attrs = nsim_bus_dev_attrs,
129};
130
131static const struct attribute_group *nsim_bus_dev_attr_groups[] = {
132 &nsim_bus_dev_attr_group,
133 NULL,
134};
135
136static void nsim_bus_dev_release(struct device *dev)
137{
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200138}
139
140static struct device_type nsim_bus_dev_type = {
141 .groups = nsim_bus_dev_attr_groups,
142 .release = nsim_bus_dev_release,
143};
144
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200145static struct nsim_bus_dev *
Peilin Yed4861fc2021-07-15 18:52:45 -0700146nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues);
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200147
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200148static ssize_t
149new_device_store(struct bus_type *bus, const char *buf, size_t count)
150{
Peilin Yed4861fc2021-07-15 18:52:45 -0700151 unsigned int id, port_count, num_queues;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200152 struct nsim_bus_dev *nsim_bus_dev;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200153 int err;
154
Peilin Yed4861fc2021-07-15 18:52:45 -0700155 err = sscanf(buf, "%u %u %u", &id, &port_count, &num_queues);
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200156 switch (err) {
157 case 1:
158 port_count = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500159 fallthrough;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200160 case 2:
Peilin Yed4861fc2021-07-15 18:52:45 -0700161 num_queues = 1;
162 fallthrough;
163 case 3:
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200164 if (id > INT_MAX) {
165 pr_err("Value of \"id\" is too big.\n");
166 return -EINVAL;
167 }
168 break;
169 default:
Peilin Yed4861fc2021-07-15 18:52:45 -0700170 pr_err("Format for adding new device is \"id port_count num_queues\" (uint uint unit).\n");
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200171 return -EINVAL;
172 }
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200173
174 mutex_lock(&nsim_bus_dev_list_lock);
Taehee Yoof5cd2162020-02-01 16:42:54 +0000175 /* Prevent to use resource before initialization. */
176 if (!smp_load_acquire(&nsim_bus_enable)) {
177 err = -EBUSY;
178 goto err;
179 }
180
Peilin Yed4861fc2021-07-15 18:52:45 -0700181 nsim_bus_dev = nsim_bus_dev_new(id, port_count, num_queues);
Taehee Yoof5cd2162020-02-01 16:42:54 +0000182 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 Pirkof9d9db42019-04-25 15:59:48 +0200190 list_add_tail(&nsim_bus_dev->list, &nsim_bus_dev_list);
191 mutex_unlock(&nsim_bus_dev_list_lock);
192
193 return count;
Taehee Yoof5cd2162020-02-01 16:42:54 +0000194err:
195 mutex_unlock(&nsim_bus_dev_list_lock);
196 return err;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200197}
198static BUS_ATTR_WO(new_device);
199
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200200static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev);
201
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200202static ssize_t
203del_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 Yoof5cd2162020-02-01 16:42:54 +0000224 /* 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 Pirkof9d9db42019-04-25 15:59:48 +0200229 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}
240static BUS_ATTR_WO(del_device);
241
242static struct attribute *nsim_bus_attrs[] = {
243 &bus_attr_new_device.attr,
244 &bus_attr_del_device.attr,
245 NULL
246};
247ATTRIBUTE_GROUPS(nsim_bus);
248
Jiri Pirko8320d142019-04-25 15:59:53 +0200249static int nsim_bus_probe(struct device *dev)
250{
251 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
252
Jakub Kicinskia66f64b2021-10-30 16:15:05 -0700253 return nsim_drv_probe(nsim_bus_dev);
Jiri Pirko8320d142019-04-25 15:59:53 +0200254}
255
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +0200256static void nsim_bus_remove(struct device *dev)
Jiri Pirko8320d142019-04-25 15:59:53 +0200257{
258 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
259
Jakub Kicinskia66f64b2021-10-30 16:15:05 -0700260 nsim_drv_remove(nsim_bus_dev);
Jiri Pirko8320d142019-04-25 15:59:53 +0200261}
262
YueHaibing69bbbdc2019-05-04 16:12:07 +0800263static int nsim_num_vf(struct device *dev)
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200264{
265 struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
266
267 return nsim_bus_dev->num_vfs;
268}
269
270static struct bus_type nsim_bus = {
Jiri Pirko925f5af2019-04-25 15:59:44 +0200271 .name = DRV_NAME,
272 .dev_name = DRV_NAME,
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200273 .bus_groups = nsim_bus_groups,
Jiri Pirko8320d142019-04-25 15:59:53 +0200274 .probe = nsim_bus_probe,
275 .remove = nsim_bus_remove,
Jiri Pirko925f5af2019-04-25 15:59:44 +0200276 .num_vf = nsim_num_vf,
277};
278
Dmytro Linkind3953812021-06-02 15:17:14 +0300279#define NSIM_BUS_DEV_MAX_VFS 4
280
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200281static struct nsim_bus_dev *
Peilin Yed4861fc2021-07-15 18:52:45 -0700282nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues)
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200283{
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 Pirkoe05b2d12019-04-25 15:59:55 +0200291 err = ida_alloc_range(&nsim_bus_dev_ids, id, id, GFP_KERNEL);
Jiri Pirko57ce9772019-04-25 15:59:47 +0200292 if (err < 0)
293 goto err_nsim_bus_dev_free;
294 nsim_bus_dev->dev.id = err;
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200295 nsim_bus_dev->dev.bus = &nsim_bus;
296 nsim_bus_dev->dev.type = &nsim_bus_dev_type;
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200297 nsim_bus_dev->port_count = port_count;
Peilin Yed4861fc2021-07-15 18:52:45 -0700298 nsim_bus_dev->num_queues = num_queues;
Jiri Pirko7b600272019-10-05 08:10:32 +0200299 nsim_bus_dev->initial_net = current->nsproxy->net_ns;
Dmytro Linkind3953812021-06-02 15:17:14 +0300300 nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS;
Taehee Yoo6ab63362020-02-01 16:43:04 +0000301 mutex_init(&nsim_bus_dev->nsim_bus_reload_lock);
Taehee Yoof5cd2162020-02-01 16:42:54 +0000302 /* Disallow using nsim_bus_dev */
303 smp_store_release(&nsim_bus_dev->init, false);
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200304
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200305 err = device_register(&nsim_bus_dev->dev);
306 if (err)
Jakub Kicinski5e388f32021-10-30 16:15:02 -0700307 goto err_nsim_bus_dev_id_free;
Dmytro Linkind3953812021-06-02 15:17:14 +0300308
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200309 return nsim_bus_dev;
310
Jiri Pirko57ce9772019-04-25 15:59:47 +0200311err_nsim_bus_dev_id_free:
312 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200313err_nsim_bus_dev_free:
314 kfree(nsim_bus_dev);
315 return ERR_PTR(err);
316}
317
Jiri Pirkoe05b2d12019-04-25 15:59:55 +0200318static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200319{
Taehee Yoof5cd2162020-02-01 16:42:54 +0000320 /* Disallow using nsim_bus_dev */
321 smp_store_release(&nsim_bus_dev->init, false);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200322 device_unregister(&nsim_bus_dev->dev);
Jiri Pirko57ce9772019-04-25 15:59:47 +0200323 ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
Jiri Pirko40e4fe42019-04-25 15:59:45 +0200324 kfree(nsim_bus_dev);
325}
326
Jiri Pirko23d415d2019-04-25 15:59:46 +0200327static struct device_driver nsim_driver = {
328 .name = DRV_NAME,
329 .bus = &nsim_bus,
330 .owner = THIS_MODULE,
331};
332
Jiri Pirko925f5af2019-04-25 15:59:44 +0200333int nsim_bus_init(void)
334{
Jiri Pirko23d415d2019-04-25 15:59:46 +0200335 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 Yoof5cd2162020-02-01 16:42:54 +0000343 /* Allow using resources */
344 smp_store_release(&nsim_bus_enable, true);
Jiri Pirko23d415d2019-04-25 15:59:46 +0200345 return 0;
346
347err_bus_unregister:
348 bus_unregister(&nsim_bus);
349 return err;
Jiri Pirko925f5af2019-04-25 15:59:44 +0200350}
351
352void nsim_bus_exit(void)
353{
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200354 struct nsim_bus_dev *nsim_bus_dev, *tmp;
355
Taehee Yoof5cd2162020-02-01 16:42:54 +0000356 /* Disallow using resources */
357 smp_store_release(&nsim_bus_enable, false);
358
Jiri Pirkof9d9db42019-04-25 15:59:48 +0200359 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 Yoof5cd2162020-02-01 16:42:54 +0000365
Jiri Pirko23d415d2019-04-25 15:59:46 +0200366 driver_unregister(&nsim_driver);
Jiri Pirko925f5af2019-04-25 15:59:44 +0200367 bus_unregister(&nsim_bus);
368}