net/vdpa: Use readers/writers semaphore instead of cf_mutex
[linux-2.6-microblaze.git] / drivers / vdpa / vdpa.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vDPA bus.
4  *
5  * Copyright (c) 2020, Red Hat. All rights reserved.
6  *     Author: Jason Wang <jasowang@redhat.com>
7  *
8  */
9
10 #include <linux/module.h>
11 #include <linux/idr.h>
12 #include <linux/slab.h>
13 #include <linux/vdpa.h>
14 #include <uapi/linux/vdpa.h>
15 #include <net/genetlink.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/virtio_ids.h>
18
19 static LIST_HEAD(mdev_head);
20 /* A global mutex that protects vdpa management device and device level operations. */
21 static DECLARE_RWSEM(vdpa_dev_lock);
22 static DEFINE_IDA(vdpa_index_ida);
23
24 void vdpa_set_status(struct vdpa_device *vdev, u8 status)
25 {
26         down_write(&vdev->cf_lock);
27         vdev->config->set_status(vdev, status);
28         up_write(&vdev->cf_lock);
29 }
30 EXPORT_SYMBOL(vdpa_set_status);
31
32 static struct genl_family vdpa_nl_family;
33
34 static int vdpa_dev_probe(struct device *d)
35 {
36         struct vdpa_device *vdev = dev_to_vdpa(d);
37         struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
38         const struct vdpa_config_ops *ops = vdev->config;
39         u32 max_num, min_num = 1;
40         int ret = 0;
41
42         max_num = ops->get_vq_num_max(vdev);
43         if (ops->get_vq_num_min)
44                 min_num = ops->get_vq_num_min(vdev);
45         if (max_num < min_num)
46                 return -EINVAL;
47
48         if (drv && drv->probe)
49                 ret = drv->probe(vdev);
50
51         return ret;
52 }
53
54 static void vdpa_dev_remove(struct device *d)
55 {
56         struct vdpa_device *vdev = dev_to_vdpa(d);
57         struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
58
59         if (drv && drv->remove)
60                 drv->remove(vdev);
61 }
62
63 static int vdpa_dev_match(struct device *dev, struct device_driver *drv)
64 {
65         struct vdpa_device *vdev = dev_to_vdpa(dev);
66
67         /* Check override first, and if set, only use the named driver */
68         if (vdev->driver_override)
69                 return strcmp(vdev->driver_override, drv->name) == 0;
70
71         /* Currently devices must be supported by all vDPA bus drivers */
72         return 1;
73 }
74
75 static ssize_t driver_override_store(struct device *dev,
76                                      struct device_attribute *attr,
77                                      const char *buf, size_t count)
78 {
79         struct vdpa_device *vdev = dev_to_vdpa(dev);
80         const char *driver_override, *old;
81         char *cp;
82
83         /* We need to keep extra room for a newline */
84         if (count >= (PAGE_SIZE - 1))
85                 return -EINVAL;
86
87         driver_override = kstrndup(buf, count, GFP_KERNEL);
88         if (!driver_override)
89                 return -ENOMEM;
90
91         cp = strchr(driver_override, '\n');
92         if (cp)
93                 *cp = '\0';
94
95         device_lock(dev);
96         old = vdev->driver_override;
97         if (strlen(driver_override)) {
98                 vdev->driver_override = driver_override;
99         } else {
100                 kfree(driver_override);
101                 vdev->driver_override = NULL;
102         }
103         device_unlock(dev);
104
105         kfree(old);
106
107         return count;
108 }
109
110 static ssize_t driver_override_show(struct device *dev,
111                                     struct device_attribute *attr, char *buf)
112 {
113         struct vdpa_device *vdev = dev_to_vdpa(dev);
114         ssize_t len;
115
116         device_lock(dev);
117         len = snprintf(buf, PAGE_SIZE, "%s\n", vdev->driver_override);
118         device_unlock(dev);
119
120         return len;
121 }
122 static DEVICE_ATTR_RW(driver_override);
123
124 static struct attribute *vdpa_dev_attrs[] = {
125         &dev_attr_driver_override.attr,
126         NULL,
127 };
128
129 static const struct attribute_group vdpa_dev_group = {
130         .attrs  = vdpa_dev_attrs,
131 };
132 __ATTRIBUTE_GROUPS(vdpa_dev);
133
134 static struct bus_type vdpa_bus = {
135         .name  = "vdpa",
136         .dev_groups = vdpa_dev_groups,
137         .match = vdpa_dev_match,
138         .probe = vdpa_dev_probe,
139         .remove = vdpa_dev_remove,
140 };
141
142 static void vdpa_release_dev(struct device *d)
143 {
144         struct vdpa_device *vdev = dev_to_vdpa(d);
145         const struct vdpa_config_ops *ops = vdev->config;
146
147         if (ops->free)
148                 ops->free(vdev);
149
150         ida_simple_remove(&vdpa_index_ida, vdev->index);
151         kfree(vdev->driver_override);
152         kfree(vdev);
153 }
154
155 /**
156  * __vdpa_alloc_device - allocate and initilaize a vDPA device
157  * This allows driver to some prepartion after device is
158  * initialized but before registered.
159  * @parent: the parent device
160  * @config: the bus operations that is supported by this device
161  * @size: size of the parent structure that contains private data
162  * @name: name of the vdpa device; optional.
163  * @use_va: indicate whether virtual address must be used by this device
164  *
165  * Driver should use vdpa_alloc_device() wrapper macro instead of
166  * using this directly.
167  *
168  * Return: Returns an error when parent/config/dma_dev is not set or fail to get
169  *         ida.
170  */
171 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
172                                         const struct vdpa_config_ops *config,
173                                         size_t size, const char *name,
174                                         bool use_va)
175 {
176         struct vdpa_device *vdev;
177         int err = -EINVAL;
178
179         if (!config)
180                 goto err;
181
182         if (!!config->dma_map != !!config->dma_unmap)
183                 goto err;
184
185         /* It should only work for the device that use on-chip IOMMU */
186         if (use_va && !(config->dma_map || config->set_map))
187                 goto err;
188
189         err = -ENOMEM;
190         vdev = kzalloc(size, GFP_KERNEL);
191         if (!vdev)
192                 goto err;
193
194         err = ida_alloc(&vdpa_index_ida, GFP_KERNEL);
195         if (err < 0)
196                 goto err_ida;
197
198         vdev->dev.bus = &vdpa_bus;
199         vdev->dev.parent = parent;
200         vdev->dev.release = vdpa_release_dev;
201         vdev->index = err;
202         vdev->config = config;
203         vdev->features_valid = false;
204         vdev->use_va = use_va;
205
206         if (name)
207                 err = dev_set_name(&vdev->dev, "%s", name);
208         else
209                 err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
210         if (err)
211                 goto err_name;
212
213         init_rwsem(&vdev->cf_lock);
214         device_initialize(&vdev->dev);
215
216         return vdev;
217
218 err_name:
219         ida_simple_remove(&vdpa_index_ida, vdev->index);
220 err_ida:
221         kfree(vdev);
222 err:
223         return ERR_PTR(err);
224 }
225 EXPORT_SYMBOL_GPL(__vdpa_alloc_device);
226
227 static int vdpa_name_match(struct device *dev, const void *data)
228 {
229         struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
230
231         return (strcmp(dev_name(&vdev->dev), data) == 0);
232 }
233
234 static int __vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
235 {
236         struct device *dev;
237
238         vdev->nvqs = nvqs;
239
240         lockdep_assert_held(&vdpa_dev_lock);
241         dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match);
242         if (dev) {
243                 put_device(dev);
244                 return -EEXIST;
245         }
246         return device_add(&vdev->dev);
247 }
248
249 /**
250  * _vdpa_register_device - register a vDPA device with vdpa lock held
251  * Caller must have a succeed call of vdpa_alloc_device() before.
252  * Caller must invoke this routine in the management device dev_add()
253  * callback after setting up valid mgmtdev for this vdpa device.
254  * @vdev: the vdpa device to be registered to vDPA bus
255  * @nvqs: number of virtqueues supported by this device
256  *
257  * Return: Returns an error when fail to add device to vDPA bus
258  */
259 int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
260 {
261         if (!vdev->mdev)
262                 return -EINVAL;
263
264         return __vdpa_register_device(vdev, nvqs);
265 }
266 EXPORT_SYMBOL_GPL(_vdpa_register_device);
267
268 /**
269  * vdpa_register_device - register a vDPA device
270  * Callers must have a succeed call of vdpa_alloc_device() before.
271  * @vdev: the vdpa device to be registered to vDPA bus
272  * @nvqs: number of virtqueues supported by this device
273  *
274  * Return: Returns an error when fail to add to vDPA bus
275  */
276 int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
277 {
278         int err;
279
280         down_write(&vdpa_dev_lock);
281         err = __vdpa_register_device(vdev, nvqs);
282         up_write(&vdpa_dev_lock);
283         return err;
284 }
285 EXPORT_SYMBOL_GPL(vdpa_register_device);
286
287 /**
288  * _vdpa_unregister_device - unregister a vDPA device
289  * Caller must invoke this routine as part of management device dev_del()
290  * callback.
291  * @vdev: the vdpa device to be unregisted from vDPA bus
292  */
293 void _vdpa_unregister_device(struct vdpa_device *vdev)
294 {
295         lockdep_assert_held(&vdpa_dev_lock);
296         WARN_ON(!vdev->mdev);
297         device_unregister(&vdev->dev);
298 }
299 EXPORT_SYMBOL_GPL(_vdpa_unregister_device);
300
301 /**
302  * vdpa_unregister_device - unregister a vDPA device
303  * @vdev: the vdpa device to be unregisted from vDPA bus
304  */
305 void vdpa_unregister_device(struct vdpa_device *vdev)
306 {
307         down_write(&vdpa_dev_lock);
308         device_unregister(&vdev->dev);
309         up_write(&vdpa_dev_lock);
310 }
311 EXPORT_SYMBOL_GPL(vdpa_unregister_device);
312
313 /**
314  * __vdpa_register_driver - register a vDPA device driver
315  * @drv: the vdpa device driver to be registered
316  * @owner: module owner of the driver
317  *
318  * Return: Returns an err when fail to do the registration
319  */
320 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner)
321 {
322         drv->driver.bus = &vdpa_bus;
323         drv->driver.owner = owner;
324
325         return driver_register(&drv->driver);
326 }
327 EXPORT_SYMBOL_GPL(__vdpa_register_driver);
328
329 /**
330  * vdpa_unregister_driver - unregister a vDPA device driver
331  * @drv: the vdpa device driver to be unregistered
332  */
333 void vdpa_unregister_driver(struct vdpa_driver *drv)
334 {
335         driver_unregister(&drv->driver);
336 }
337 EXPORT_SYMBOL_GPL(vdpa_unregister_driver);
338
339 /**
340  * vdpa_mgmtdev_register - register a vdpa management device
341  *
342  * @mdev: Pointer to vdpa management device
343  * vdpa_mgmtdev_register() register a vdpa management device which supports
344  * vdpa device management.
345  * Return: Returns 0 on success or failure when required callback ops are not
346  *         initialized.
347  */
348 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev)
349 {
350         if (!mdev->device || !mdev->ops || !mdev->ops->dev_add || !mdev->ops->dev_del)
351                 return -EINVAL;
352
353         INIT_LIST_HEAD(&mdev->list);
354         down_write(&vdpa_dev_lock);
355         list_add_tail(&mdev->list, &mdev_head);
356         up_write(&vdpa_dev_lock);
357         return 0;
358 }
359 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register);
360
361 static int vdpa_match_remove(struct device *dev, void *data)
362 {
363         struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
364         struct vdpa_mgmt_dev *mdev = vdev->mdev;
365
366         if (mdev == data)
367                 mdev->ops->dev_del(mdev, vdev);
368         return 0;
369 }
370
371 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev)
372 {
373         down_write(&vdpa_dev_lock);
374
375         list_del(&mdev->list);
376
377         /* Filter out all the entries belong to this management device and delete it. */
378         bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove);
379
380         up_write(&vdpa_dev_lock);
381 }
382 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister);
383
384 static void vdpa_get_config_unlocked(struct vdpa_device *vdev,
385                                      unsigned int offset,
386                                      void *buf, unsigned int len)
387 {
388         const struct vdpa_config_ops *ops = vdev->config;
389
390         /*
391          * Config accesses aren't supposed to trigger before features are set.
392          * If it does happen we assume a legacy guest.
393          */
394         if (!vdev->features_valid)
395                 vdpa_set_features_unlocked(vdev, 0);
396         ops->get_config(vdev, offset, buf, len);
397 }
398
399 /**
400  * vdpa_get_config - Get one or more device configuration fields.
401  * @vdev: vdpa device to operate on
402  * @offset: starting byte offset of the field
403  * @buf: buffer pointer to read to
404  * @len: length of the configuration fields in bytes
405  */
406 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
407                      void *buf, unsigned int len)
408 {
409         down_read(&vdev->cf_lock);
410         vdpa_get_config_unlocked(vdev, offset, buf, len);
411         up_read(&vdev->cf_lock);
412 }
413 EXPORT_SYMBOL_GPL(vdpa_get_config);
414
415 /**
416  * vdpa_set_config - Set one or more device configuration fields.
417  * @vdev: vdpa device to operate on
418  * @offset: starting byte offset of the field
419  * @buf: buffer pointer to read from
420  * @length: length of the configuration fields in bytes
421  */
422 void vdpa_set_config(struct vdpa_device *vdev, unsigned int offset,
423                      const void *buf, unsigned int length)
424 {
425         down_write(&vdev->cf_lock);
426         vdev->config->set_config(vdev, offset, buf, length);
427         up_write(&vdev->cf_lock);
428 }
429 EXPORT_SYMBOL_GPL(vdpa_set_config);
430
431 static bool mgmtdev_handle_match(const struct vdpa_mgmt_dev *mdev,
432                                  const char *busname, const char *devname)
433 {
434         /* Bus name is optional for simulated management device, so ignore the
435          * device with bus if bus attribute is provided.
436          */
437         if ((busname && !mdev->device->bus) || (!busname && mdev->device->bus))
438                 return false;
439
440         if (!busname && strcmp(dev_name(mdev->device), devname) == 0)
441                 return true;
442
443         if (busname && (strcmp(mdev->device->bus->name, busname) == 0) &&
444             (strcmp(dev_name(mdev->device), devname) == 0))
445                 return true;
446
447         return false;
448 }
449
450 static struct vdpa_mgmt_dev *vdpa_mgmtdev_get_from_attr(struct nlattr **attrs)
451 {
452         struct vdpa_mgmt_dev *mdev;
453         const char *busname = NULL;
454         const char *devname;
455
456         if (!attrs[VDPA_ATTR_MGMTDEV_DEV_NAME])
457                 return ERR_PTR(-EINVAL);
458         devname = nla_data(attrs[VDPA_ATTR_MGMTDEV_DEV_NAME]);
459         if (attrs[VDPA_ATTR_MGMTDEV_BUS_NAME])
460                 busname = nla_data(attrs[VDPA_ATTR_MGMTDEV_BUS_NAME]);
461
462         list_for_each_entry(mdev, &mdev_head, list) {
463                 if (mgmtdev_handle_match(mdev, busname, devname))
464                         return mdev;
465         }
466         return ERR_PTR(-ENODEV);
467 }
468
469 static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff *msg, const struct vdpa_mgmt_dev *mdev)
470 {
471         if (mdev->device->bus &&
472             nla_put_string(msg, VDPA_ATTR_MGMTDEV_BUS_NAME, mdev->device->bus->name))
473                 return -EMSGSIZE;
474         if (nla_put_string(msg, VDPA_ATTR_MGMTDEV_DEV_NAME, dev_name(mdev->device)))
475                 return -EMSGSIZE;
476         return 0;
477 }
478
479 static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *msg,
480                              u32 portid, u32 seq, int flags)
481 {
482         u64 supported_classes = 0;
483         void *hdr;
484         int i = 0;
485         int err;
486
487         hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_MGMTDEV_NEW);
488         if (!hdr)
489                 return -EMSGSIZE;
490         err = vdpa_nl_mgmtdev_handle_fill(msg, mdev);
491         if (err)
492                 goto msg_err;
493
494         while (mdev->id_table[i].device) {
495                 if (mdev->id_table[i].device <= 63)
496                         supported_classes |= BIT_ULL(mdev->id_table[i].device);
497                 i++;
498         }
499
500         if (nla_put_u64_64bit(msg, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES,
501                               supported_classes, VDPA_ATTR_UNSPEC)) {
502                 err = -EMSGSIZE;
503                 goto msg_err;
504         }
505         if (nla_put_u32(msg, VDPA_ATTR_DEV_MGMTDEV_MAX_VQS,
506                         mdev->max_supported_vqs)) {
507                 err = -EMSGSIZE;
508                 goto msg_err;
509         }
510         if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_SUPPORTED_FEATURES,
511                               mdev->supported_features, VDPA_ATTR_PAD)) {
512                 err = -EMSGSIZE;
513                 goto msg_err;
514         }
515
516         genlmsg_end(msg, hdr);
517         return 0;
518
519 msg_err:
520         genlmsg_cancel(msg, hdr);
521         return err;
522 }
523
524 static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *info)
525 {
526         struct vdpa_mgmt_dev *mdev;
527         struct sk_buff *msg;
528         int err;
529
530         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
531         if (!msg)
532                 return -ENOMEM;
533
534         down_read(&vdpa_dev_lock);
535         mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
536         if (IS_ERR(mdev)) {
537                 up_read(&vdpa_dev_lock);
538                 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device");
539                 err = PTR_ERR(mdev);
540                 goto out;
541         }
542
543         err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0);
544         up_read(&vdpa_dev_lock);
545         if (err)
546                 goto out;
547         err = genlmsg_reply(msg, info);
548         return err;
549
550 out:
551         nlmsg_free(msg);
552         return err;
553 }
554
555 static int
556 vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
557 {
558         struct vdpa_mgmt_dev *mdev;
559         int start = cb->args[0];
560         int idx = 0;
561         int err;
562
563         down_read(&vdpa_dev_lock);
564         list_for_each_entry(mdev, &mdev_head, list) {
565                 if (idx < start) {
566                         idx++;
567                         continue;
568                 }
569                 err = vdpa_mgmtdev_fill(mdev, msg, NETLINK_CB(cb->skb).portid,
570                                         cb->nlh->nlmsg_seq, NLM_F_MULTI);
571                 if (err)
572                         goto out;
573                 idx++;
574         }
575 out:
576         up_read(&vdpa_dev_lock);
577         cb->args[0] = idx;
578         return msg->len;
579 }
580
581 #define VDPA_DEV_NET_ATTRS_MASK (BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) | \
582                                  BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU)     | \
583                                  BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
584
585 static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *info)
586 {
587         struct vdpa_dev_set_config config = {};
588         struct nlattr **nl_attrs = info->attrs;
589         struct vdpa_mgmt_dev *mdev;
590         const u8 *macaddr;
591         const char *name;
592         int err = 0;
593
594         if (!info->attrs[VDPA_ATTR_DEV_NAME])
595                 return -EINVAL;
596
597         name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
598
599         if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
600                 macaddr = nla_data(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
601                 memcpy(config.net.mac, macaddr, sizeof(config.net.mac));
602                 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
603         }
604         if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU]) {
605                 config.net.mtu =
606                         nla_get_u16(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU]);
607                 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU);
608         }
609         if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]) {
610                 config.net.max_vq_pairs =
611                         nla_get_u16(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]);
612                 if (!config.net.max_vq_pairs) {
613                         NL_SET_ERR_MSG_MOD(info->extack,
614                                            "At least one pair of VQs is required");
615                         return -EINVAL;
616                 }
617                 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
618         }
619
620         /* Skip checking capability if user didn't prefer to configure any
621          * device networking attributes. It is likely that user might have used
622          * a device specific method to configure such attributes or using device
623          * default attributes.
624          */
625         if ((config.mask & VDPA_DEV_NET_ATTRS_MASK) &&
626             !netlink_capable(skb, CAP_NET_ADMIN))
627                 return -EPERM;
628
629         down_write(&vdpa_dev_lock);
630         mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
631         if (IS_ERR(mdev)) {
632                 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device");
633                 err = PTR_ERR(mdev);
634                 goto err;
635         }
636         if ((config.mask & mdev->config_attr_mask) != config.mask) {
637                 NL_SET_ERR_MSG_MOD(info->extack,
638                                    "All provided attributes are not supported");
639                 err = -EOPNOTSUPP;
640                 goto err;
641         }
642
643         err = mdev->ops->dev_add(mdev, name, &config);
644 err:
645         up_write(&vdpa_dev_lock);
646         return err;
647 }
648
649 static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *info)
650 {
651         struct vdpa_mgmt_dev *mdev;
652         struct vdpa_device *vdev;
653         struct device *dev;
654         const char *name;
655         int err = 0;
656
657         if (!info->attrs[VDPA_ATTR_DEV_NAME])
658                 return -EINVAL;
659         name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
660
661         down_write(&vdpa_dev_lock);
662         dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
663         if (!dev) {
664                 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
665                 err = -ENODEV;
666                 goto dev_err;
667         }
668         vdev = container_of(dev, struct vdpa_device, dev);
669         if (!vdev->mdev) {
670                 NL_SET_ERR_MSG_MOD(info->extack, "Only user created device can be deleted by user");
671                 err = -EINVAL;
672                 goto mdev_err;
673         }
674         mdev = vdev->mdev;
675         mdev->ops->dev_del(mdev, vdev);
676 mdev_err:
677         put_device(dev);
678 dev_err:
679         up_write(&vdpa_dev_lock);
680         return err;
681 }
682
683 static int
684 vdpa_dev_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
685               int flags, struct netlink_ext_ack *extack)
686 {
687         u16 max_vq_size;
688         u16 min_vq_size = 1;
689         u32 device_id;
690         u32 vendor_id;
691         void *hdr;
692         int err;
693
694         hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_DEV_NEW);
695         if (!hdr)
696                 return -EMSGSIZE;
697
698         err = vdpa_nl_mgmtdev_handle_fill(msg, vdev->mdev);
699         if (err)
700                 goto msg_err;
701
702         device_id = vdev->config->get_device_id(vdev);
703         vendor_id = vdev->config->get_vendor_id(vdev);
704         max_vq_size = vdev->config->get_vq_num_max(vdev);
705         if (vdev->config->get_vq_num_min)
706                 min_vq_size = vdev->config->get_vq_num_min(vdev);
707
708         err = -EMSGSIZE;
709         if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev)))
710                 goto msg_err;
711         if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id))
712                 goto msg_err;
713         if (nla_put_u32(msg, VDPA_ATTR_DEV_VENDOR_ID, vendor_id))
714                 goto msg_err;
715         if (nla_put_u32(msg, VDPA_ATTR_DEV_MAX_VQS, vdev->nvqs))
716                 goto msg_err;
717         if (nla_put_u16(msg, VDPA_ATTR_DEV_MAX_VQ_SIZE, max_vq_size))
718                 goto msg_err;
719         if (nla_put_u16(msg, VDPA_ATTR_DEV_MIN_VQ_SIZE, min_vq_size))
720                 goto msg_err;
721
722         genlmsg_end(msg, hdr);
723         return 0;
724
725 msg_err:
726         genlmsg_cancel(msg, hdr);
727         return err;
728 }
729
730 static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
731 {
732         struct vdpa_device *vdev;
733         struct sk_buff *msg;
734         const char *devname;
735         struct device *dev;
736         int err;
737
738         if (!info->attrs[VDPA_ATTR_DEV_NAME])
739                 return -EINVAL;
740         devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
741         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
742         if (!msg)
743                 return -ENOMEM;
744
745         down_read(&vdpa_dev_lock);
746         dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
747         if (!dev) {
748                 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
749                 err = -ENODEV;
750                 goto err;
751         }
752         vdev = container_of(dev, struct vdpa_device, dev);
753         if (!vdev->mdev) {
754                 err = -EINVAL;
755                 goto mdev_err;
756         }
757         err = vdpa_dev_fill(vdev, msg, info->snd_portid, info->snd_seq, 0, info->extack);
758         if (err)
759                 goto mdev_err;
760
761         err = genlmsg_reply(msg, info);
762         put_device(dev);
763         up_read(&vdpa_dev_lock);
764         return err;
765
766 mdev_err:
767         put_device(dev);
768 err:
769         up_read(&vdpa_dev_lock);
770         nlmsg_free(msg);
771         return err;
772 }
773
774 struct vdpa_dev_dump_info {
775         struct sk_buff *msg;
776         struct netlink_callback *cb;
777         int start_idx;
778         int idx;
779 };
780
781 static int vdpa_dev_dump(struct device *dev, void *data)
782 {
783         struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
784         struct vdpa_dev_dump_info *info = data;
785         int err;
786
787         if (!vdev->mdev)
788                 return 0;
789         if (info->idx < info->start_idx) {
790                 info->idx++;
791                 return 0;
792         }
793         err = vdpa_dev_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
794                             info->cb->nlh->nlmsg_seq, NLM_F_MULTI, info->cb->extack);
795         if (err)
796                 return err;
797
798         info->idx++;
799         return 0;
800 }
801
802 static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
803 {
804         struct vdpa_dev_dump_info info;
805
806         info.msg = msg;
807         info.cb = cb;
808         info.start_idx = cb->args[0];
809         info.idx = 0;
810
811         down_read(&vdpa_dev_lock);
812         bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump);
813         up_read(&vdpa_dev_lock);
814         cb->args[0] = info.idx;
815         return msg->len;
816 }
817
818 static int vdpa_dev_net_mq_config_fill(struct vdpa_device *vdev,
819                                        struct sk_buff *msg, u64 features,
820                                        const struct virtio_net_config *config)
821 {
822         u16 val_u16;
823
824         if ((features & BIT_ULL(VIRTIO_NET_F_MQ)) == 0)
825                 return 0;
826
827         val_u16 = le16_to_cpu(config->max_virtqueue_pairs);
828         return nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, val_u16);
829 }
830
831 static int vdpa_dev_net_config_fill(struct vdpa_device *vdev, struct sk_buff *msg)
832 {
833         struct virtio_net_config config = {};
834         u64 features;
835         u16 val_u16;
836
837         vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
838
839         if (nla_put(msg, VDPA_ATTR_DEV_NET_CFG_MACADDR, sizeof(config.mac),
840                     config.mac))
841                 return -EMSGSIZE;
842
843         val_u16 = le16_to_cpu(config.status);
844         if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_STATUS, val_u16))
845                 return -EMSGSIZE;
846
847         val_u16 = le16_to_cpu(config.mtu);
848         if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MTU, val_u16))
849                 return -EMSGSIZE;
850
851         features = vdev->config->get_driver_features(vdev);
852         if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_NEGOTIATED_FEATURES, features,
853                               VDPA_ATTR_PAD))
854                 return -EMSGSIZE;
855
856         return vdpa_dev_net_mq_config_fill(vdev, msg, features, &config);
857 }
858
859 static int
860 vdpa_dev_config_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
861                      int flags, struct netlink_ext_ack *extack)
862 {
863         u32 device_id;
864         void *hdr;
865         u8 status;
866         int err;
867
868         down_read(&vdev->cf_lock);
869         status = vdev->config->get_status(vdev);
870         if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
871                 NL_SET_ERR_MSG_MOD(extack, "Features negotiation not completed");
872                 err = -EAGAIN;
873                 goto out;
874         }
875
876         hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags,
877                           VDPA_CMD_DEV_CONFIG_GET);
878         if (!hdr) {
879                 err = -EMSGSIZE;
880                 goto out;
881         }
882
883         if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev))) {
884                 err = -EMSGSIZE;
885                 goto msg_err;
886         }
887
888         device_id = vdev->config->get_device_id(vdev);
889         if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id)) {
890                 err = -EMSGSIZE;
891                 goto msg_err;
892         }
893
894         switch (device_id) {
895         case VIRTIO_ID_NET:
896                 err = vdpa_dev_net_config_fill(vdev, msg);
897                 break;
898         default:
899                 err = -EOPNOTSUPP;
900                 break;
901         }
902         if (err)
903                 goto msg_err;
904
905         up_read(&vdev->cf_lock);
906         genlmsg_end(msg, hdr);
907         return 0;
908
909 msg_err:
910         genlmsg_cancel(msg, hdr);
911 out:
912         up_read(&vdev->cf_lock);
913         return err;
914 }
915
916 static int vdpa_fill_stats_rec(struct vdpa_device *vdev, struct sk_buff *msg,
917                                struct genl_info *info, u32 index)
918 {
919         struct virtio_net_config config = {};
920         u64 features;
921         u16 max_vqp;
922         u8 status;
923         int err;
924
925         status = vdev->config->get_status(vdev);
926         if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
927                 NL_SET_ERR_MSG_MOD(info->extack, "feature negotiation not complete");
928                 return -EAGAIN;
929         }
930         vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
931
932         max_vqp = le16_to_cpu(config.max_virtqueue_pairs);
933         if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, max_vqp))
934                 return -EMSGSIZE;
935
936         features = vdev->config->get_driver_features(vdev);
937         if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_NEGOTIATED_FEATURES,
938                               features, VDPA_ATTR_PAD))
939                 return -EMSGSIZE;
940
941         if (nla_put_u32(msg, VDPA_ATTR_DEV_QUEUE_INDEX, index))
942                 return -EMSGSIZE;
943
944         err = vdev->config->get_vendor_vq_stats(vdev, index, msg, info->extack);
945         if (err)
946                 return err;
947
948         return 0;
949 }
950
951 static int vendor_stats_fill(struct vdpa_device *vdev, struct sk_buff *msg,
952                              struct genl_info *info, u32 index)
953 {
954         int err;
955
956         down_read(&vdev->cf_lock);
957         if (!vdev->config->get_vendor_vq_stats) {
958                 err = -EOPNOTSUPP;
959                 goto out;
960         }
961
962         err = vdpa_fill_stats_rec(vdev, msg, info, index);
963 out:
964         up_read(&vdev->cf_lock);
965         return err;
966 }
967
968 static int vdpa_dev_vendor_stats_fill(struct vdpa_device *vdev,
969                                       struct sk_buff *msg,
970                                       struct genl_info *info, u32 index)
971 {
972         u32 device_id;
973         void *hdr;
974         int err;
975         u32 portid = info->snd_portid;
976         u32 seq = info->snd_seq;
977         u32 flags = 0;
978
979         hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags,
980                           VDPA_CMD_DEV_VSTATS_GET);
981         if (!hdr)
982                 return -EMSGSIZE;
983
984         if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev))) {
985                 err = -EMSGSIZE;
986                 goto undo_msg;
987         }
988
989         device_id = vdev->config->get_device_id(vdev);
990         if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id)) {
991                 err = -EMSGSIZE;
992                 goto undo_msg;
993         }
994
995         switch (device_id) {
996         case VIRTIO_ID_NET:
997                 if (index > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX) {
998                         NL_SET_ERR_MSG_MOD(info->extack, "queue index excceeds max value");
999                         err = -ERANGE;
1000                         break;
1001                 }
1002
1003                 err = vendor_stats_fill(vdev, msg, info, index);
1004                 break;
1005         default:
1006                 err = -EOPNOTSUPP;
1007                 break;
1008         }
1009         genlmsg_end(msg, hdr);
1010
1011         return err;
1012
1013 undo_msg:
1014         genlmsg_cancel(msg, hdr);
1015         return err;
1016 }
1017
1018 static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info *info)
1019 {
1020         struct vdpa_device *vdev;
1021         struct sk_buff *msg;
1022         const char *devname;
1023         struct device *dev;
1024         int err;
1025
1026         if (!info->attrs[VDPA_ATTR_DEV_NAME])
1027                 return -EINVAL;
1028         devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1029         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1030         if (!msg)
1031                 return -ENOMEM;
1032
1033         down_read(&vdpa_dev_lock);
1034         dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
1035         if (!dev) {
1036                 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1037                 err = -ENODEV;
1038                 goto dev_err;
1039         }
1040         vdev = container_of(dev, struct vdpa_device, dev);
1041         if (!vdev->mdev) {
1042                 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1043                 err = -EINVAL;
1044                 goto mdev_err;
1045         }
1046         err = vdpa_dev_config_fill(vdev, msg, info->snd_portid, info->snd_seq,
1047                                    0, info->extack);
1048         if (!err)
1049                 err = genlmsg_reply(msg, info);
1050
1051 mdev_err:
1052         put_device(dev);
1053 dev_err:
1054         up_read(&vdpa_dev_lock);
1055         if (err)
1056                 nlmsg_free(msg);
1057         return err;
1058 }
1059
1060 static int vdpa_dev_config_dump(struct device *dev, void *data)
1061 {
1062         struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
1063         struct vdpa_dev_dump_info *info = data;
1064         int err;
1065
1066         if (!vdev->mdev)
1067                 return 0;
1068         if (info->idx < info->start_idx) {
1069                 info->idx++;
1070                 return 0;
1071         }
1072         err = vdpa_dev_config_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
1073                                    info->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1074                                    info->cb->extack);
1075         if (err)
1076                 return err;
1077
1078         info->idx++;
1079         return 0;
1080 }
1081
1082 static int
1083 vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
1084 {
1085         struct vdpa_dev_dump_info info;
1086
1087         info.msg = msg;
1088         info.cb = cb;
1089         info.start_idx = cb->args[0];
1090         info.idx = 0;
1091
1092         down_read(&vdpa_dev_lock);
1093         bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_config_dump);
1094         up_read(&vdpa_dev_lock);
1095         cb->args[0] = info.idx;
1096         return msg->len;
1097 }
1098
1099 static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb,
1100                                           struct genl_info *info)
1101 {
1102         struct vdpa_device *vdev;
1103         struct sk_buff *msg;
1104         const char *devname;
1105         struct device *dev;
1106         u32 index;
1107         int err;
1108
1109         if (!info->attrs[VDPA_ATTR_DEV_NAME])
1110                 return -EINVAL;
1111
1112         if (!info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX])
1113                 return -EINVAL;
1114
1115         devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1116         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1117         if (!msg)
1118                 return -ENOMEM;
1119
1120         index = nla_get_u32(info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX]);
1121         down_read(&vdpa_dev_lock);
1122         dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
1123         if (!dev) {
1124                 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1125                 err = -ENODEV;
1126                 goto dev_err;
1127         }
1128         vdev = container_of(dev, struct vdpa_device, dev);
1129         if (!vdev->mdev) {
1130                 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1131                 err = -EINVAL;
1132                 goto mdev_err;
1133         }
1134         err = vdpa_dev_vendor_stats_fill(vdev, msg, info, index);
1135         if (err)
1136                 goto mdev_err;
1137
1138         err = genlmsg_reply(msg, info);
1139
1140         put_device(dev);
1141         up_read(&vdpa_dev_lock);
1142
1143         return err;
1144
1145 mdev_err:
1146         put_device(dev);
1147 dev_err:
1148         nlmsg_free(msg);
1149         up_read(&vdpa_dev_lock);
1150         return err;
1151 }
1152
1153 static const struct nla_policy vdpa_nl_policy[VDPA_ATTR_MAX + 1] = {
1154         [VDPA_ATTR_MGMTDEV_BUS_NAME] = { .type = NLA_NUL_STRING },
1155         [VDPA_ATTR_MGMTDEV_DEV_NAME] = { .type = NLA_STRING },
1156         [VDPA_ATTR_DEV_NAME] = { .type = NLA_STRING },
1157         [VDPA_ATTR_DEV_NET_CFG_MACADDR] = NLA_POLICY_ETH_ADDR,
1158         /* virtio spec 1.1 section 5.1.4.1 for valid MTU range */
1159         [VDPA_ATTR_DEV_NET_CFG_MTU] = NLA_POLICY_MIN(NLA_U16, 68),
1160 };
1161
1162 static const struct genl_ops vdpa_nl_ops[] = {
1163         {
1164                 .cmd = VDPA_CMD_MGMTDEV_GET,
1165                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1166                 .doit = vdpa_nl_cmd_mgmtdev_get_doit,
1167                 .dumpit = vdpa_nl_cmd_mgmtdev_get_dumpit,
1168         },
1169         {
1170                 .cmd = VDPA_CMD_DEV_NEW,
1171                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1172                 .doit = vdpa_nl_cmd_dev_add_set_doit,
1173                 .flags = GENL_ADMIN_PERM,
1174         },
1175         {
1176                 .cmd = VDPA_CMD_DEV_DEL,
1177                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1178                 .doit = vdpa_nl_cmd_dev_del_set_doit,
1179                 .flags = GENL_ADMIN_PERM,
1180         },
1181         {
1182                 .cmd = VDPA_CMD_DEV_GET,
1183                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1184                 .doit = vdpa_nl_cmd_dev_get_doit,
1185                 .dumpit = vdpa_nl_cmd_dev_get_dumpit,
1186         },
1187         {
1188                 .cmd = VDPA_CMD_DEV_CONFIG_GET,
1189                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1190                 .doit = vdpa_nl_cmd_dev_config_get_doit,
1191                 .dumpit = vdpa_nl_cmd_dev_config_get_dumpit,
1192         },
1193         {
1194                 .cmd = VDPA_CMD_DEV_VSTATS_GET,
1195                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1196                 .doit = vdpa_nl_cmd_dev_stats_get_doit,
1197                 .flags = GENL_ADMIN_PERM,
1198         },
1199 };
1200
1201 static struct genl_family vdpa_nl_family __ro_after_init = {
1202         .name = VDPA_GENL_NAME,
1203         .version = VDPA_GENL_VERSION,
1204         .maxattr = VDPA_ATTR_MAX,
1205         .policy = vdpa_nl_policy,
1206         .netnsok = false,
1207         .module = THIS_MODULE,
1208         .ops = vdpa_nl_ops,
1209         .n_ops = ARRAY_SIZE(vdpa_nl_ops),
1210 };
1211
1212 static int vdpa_init(void)
1213 {
1214         int err;
1215
1216         err = bus_register(&vdpa_bus);
1217         if (err)
1218                 return err;
1219         err = genl_register_family(&vdpa_nl_family);
1220         if (err)
1221                 goto err;
1222         return 0;
1223
1224 err:
1225         bus_unregister(&vdpa_bus);
1226         return err;
1227 }
1228
1229 static void __exit vdpa_exit(void)
1230 {
1231         genl_unregister_family(&vdpa_nl_family);
1232         bus_unregister(&vdpa_bus);
1233         ida_destroy(&vdpa_index_ida);
1234 }
1235 core_initcall(vdpa_init);
1236 module_exit(vdpa_exit);
1237
1238 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
1239 MODULE_LICENSE("GPL v2");