Skip to content

Commit 741948f

Browse files
committed
Merge branch 'netdevsim-device-and-bus'
Jakub Kicinski says: ==================== netdevsim: improve separation between device and bus VF config falls strangely in between device and bus responsibilities today. Because of this bus.c sticks fingers directly into struct nsim_dev and we look at nsim_bus_dev in many more places than necessary. Make bus.c contain pure interface code, and move the particulars of the logic (which touch on eswitch, devlink reloads etc) to dev.c. Rename the functions at the boundary of the interface to make the separation clearer. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 1adc58e + a66f64b commit 741948f

File tree

4 files changed

+235
-235
lines changed

4 files changed

+235
-235
lines changed

drivers/net/netdevsim/bus.c

Lines changed: 11 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <linux/kernel.h>
99
#include <linux/list.h>
1010
#include <linux/mutex.h>
11-
#include <linux/rtnetlink.h>
1211
#include <linux/slab.h>
1312
#include <linux/sysfs.h>
1413

@@ -24,39 +23,6 @@ static struct nsim_bus_dev *to_nsim_bus_dev(struct device *dev)
2423
return container_of(dev, struct nsim_bus_dev, dev);
2524
}
2625

27-
static int nsim_bus_dev_vfs_enable(struct nsim_bus_dev *nsim_bus_dev,
28-
unsigned int num_vfs)
29-
{
30-
struct nsim_dev *nsim_dev;
31-
int err = 0;
32-
33-
if (nsim_bus_dev->max_vfs < num_vfs)
34-
return -ENOMEM;
35-
36-
if (!nsim_bus_dev->vfconfigs)
37-
return -ENOMEM;
38-
nsim_bus_dev->num_vfs = num_vfs;
39-
40-
nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
41-
if (nsim_esw_mode_is_switchdev(nsim_dev)) {
42-
err = nsim_esw_switchdev_enable(nsim_dev, NULL);
43-
if (err)
44-
nsim_bus_dev->num_vfs = 0;
45-
}
46-
47-
return err;
48-
}
49-
50-
void nsim_bus_dev_vfs_disable(struct nsim_bus_dev *nsim_bus_dev)
51-
{
52-
struct nsim_dev *nsim_dev;
53-
54-
nsim_bus_dev->num_vfs = 0;
55-
nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
56-
if (nsim_esw_mode_is_switchdev(nsim_dev))
57-
nsim_esw_legacy_enable(nsim_dev, NULL);
58-
}
59-
6026
static ssize_t
6127
nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
6228
const char *buf, size_t count)
@@ -69,27 +35,13 @@ nsim_bus_dev_numvfs_store(struct device *dev, struct device_attribute *attr,
6935
if (ret)
7036
return ret;
7137

72-
mutex_lock(&nsim_bus_dev->vfs_lock);
73-
if (nsim_bus_dev->num_vfs == num_vfs)
74-
goto exit_good;
75-
if (nsim_bus_dev->num_vfs && num_vfs) {
76-
ret = -EBUSY;
77-
goto exit_unlock;
78-
}
79-
80-
if (num_vfs) {
81-
ret = nsim_bus_dev_vfs_enable(nsim_bus_dev, num_vfs);
82-
if (ret)
83-
goto exit_unlock;
84-
} else {
85-
nsim_bus_dev_vfs_disable(nsim_bus_dev);
86-
}
87-
exit_good:
88-
ret = count;
89-
exit_unlock:
90-
mutex_unlock(&nsim_bus_dev->vfs_lock);
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);
9143

92-
return ret;
44+
return ret ? ret : count;
9345
}
9446

9547
static ssize_t
@@ -105,79 +57,6 @@ static struct device_attribute nsim_bus_dev_numvfs_attr =
10557
__ATTR(sriov_numvfs, 0664, nsim_bus_dev_numvfs_show,
10658
nsim_bus_dev_numvfs_store);
10759

108-
ssize_t nsim_bus_dev_max_vfs_read(struct file *file,
109-
char __user *data,
110-
size_t count, loff_t *ppos)
111-
{
112-
struct nsim_bus_dev *nsim_bus_dev = file->private_data;
113-
char buf[11];
114-
ssize_t len;
115-
116-
len = snprintf(buf, sizeof(buf), "%u\n", nsim_bus_dev->max_vfs);
117-
if (len < 0)
118-
return len;
119-
120-
return simple_read_from_buffer(data, count, ppos, buf, len);
121-
}
122-
123-
ssize_t nsim_bus_dev_max_vfs_write(struct file *file,
124-
const char __user *data,
125-
size_t count, loff_t *ppos)
126-
{
127-
struct nsim_bus_dev *nsim_bus_dev = file->private_data;
128-
struct nsim_vf_config *vfconfigs;
129-
ssize_t ret;
130-
char buf[10];
131-
u32 val;
132-
133-
if (*ppos != 0)
134-
return 0;
135-
136-
if (count >= sizeof(buf))
137-
return -ENOSPC;
138-
139-
mutex_lock(&nsim_bus_dev->vfs_lock);
140-
/* Reject if VFs are configured */
141-
if (nsim_bus_dev->num_vfs) {
142-
ret = -EBUSY;
143-
goto unlock;
144-
}
145-
146-
ret = copy_from_user(buf, data, count);
147-
if (ret) {
148-
ret = -EFAULT;
149-
goto unlock;
150-
}
151-
152-
buf[count] = '\0';
153-
ret = kstrtouint(buf, 10, &val);
154-
if (ret) {
155-
ret = -EIO;
156-
goto unlock;
157-
}
158-
159-
/* max_vfs limited by the maximum number of provided port indexes */
160-
if (val > NSIM_DEV_VF_PORT_INDEX_MAX - NSIM_DEV_VF_PORT_INDEX_BASE) {
161-
ret = -ERANGE;
162-
goto unlock;
163-
}
164-
165-
vfconfigs = kcalloc(val, sizeof(struct nsim_vf_config), GFP_KERNEL | __GFP_NOWARN);
166-
if (!vfconfigs) {
167-
ret = -ENOMEM;
168-
goto unlock;
169-
}
170-
171-
kfree(nsim_bus_dev->vfconfigs);
172-
nsim_bus_dev->vfconfigs = vfconfigs;
173-
nsim_bus_dev->max_vfs = val;
174-
*ppos += count;
175-
ret = count;
176-
unlock:
177-
mutex_unlock(&nsim_bus_dev->vfs_lock);
178-
return ret;
179-
}
180-
18160
static ssize_t
18261
new_port_store(struct device *dev, struct device_attribute *attr,
18362
const char *buf, size_t count)
@@ -201,7 +80,7 @@ new_port_store(struct device *dev, struct device_attribute *attr,
20180
return -EBUSY;
20281
}
20382

204-
ret = nsim_dev_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
83+
ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
20584
mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
20685
return ret ? ret : count;
20786
}
@@ -231,7 +110,7 @@ del_port_store(struct device *dev, struct device_attribute *attr,
231110
return -EBUSY;
232111
}
233112

234-
ret = nsim_dev_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
113+
ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
235114
mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
236115
return ret ? ret : count;
237116
}
@@ -371,14 +250,14 @@ static int nsim_bus_probe(struct device *dev)
371250
{
372251
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
373252

374-
return nsim_dev_probe(nsim_bus_dev);
253+
return nsim_drv_probe(nsim_bus_dev);
375254
}
376255

377256
static void nsim_bus_remove(struct device *dev)
378257
{
379258
struct nsim_bus_dev *nsim_bus_dev = to_nsim_bus_dev(dev);
380259

381-
nsim_dev_remove(nsim_bus_dev);
260+
nsim_drv_remove(nsim_bus_dev);
382261
}
383262

384263
static int nsim_num_vf(struct device *dev)
@@ -420,26 +299,15 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queu
420299
nsim_bus_dev->initial_net = current->nsproxy->net_ns;
421300
nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS;
422301
mutex_init(&nsim_bus_dev->nsim_bus_reload_lock);
423-
mutex_init(&nsim_bus_dev->vfs_lock);
424302
/* Disallow using nsim_bus_dev */
425303
smp_store_release(&nsim_bus_dev->init, false);
426304

427-
nsim_bus_dev->vfconfigs = kcalloc(nsim_bus_dev->max_vfs,
428-
sizeof(struct nsim_vf_config),
429-
GFP_KERNEL | __GFP_NOWARN);
430-
if (!nsim_bus_dev->vfconfigs) {
431-
err = -ENOMEM;
432-
goto err_nsim_bus_dev_id_free;
433-
}
434-
435305
err = device_register(&nsim_bus_dev->dev);
436306
if (err)
437-
goto err_nsim_vfs_free;
307+
goto err_nsim_bus_dev_id_free;
438308

439309
return nsim_bus_dev;
440310

441-
err_nsim_vfs_free:
442-
kfree(nsim_bus_dev->vfconfigs);
443311
err_nsim_bus_dev_id_free:
444312
ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
445313
err_nsim_bus_dev_free:
@@ -453,7 +321,6 @@ static void nsim_bus_dev_del(struct nsim_bus_dev *nsim_bus_dev)
453321
smp_store_release(&nsim_bus_dev->init, false);
454322
device_unregister(&nsim_bus_dev->dev);
455323
ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
456-
kfree(nsim_bus_dev->vfconfigs);
457324
kfree(nsim_bus_dev);
458325
}
459326

0 commit comments

Comments
 (0)