1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2020, Red Hat. All rights reserved.
6 * Author: Jason Wang <jasowang@redhat.com>
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>
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);
24 void vdpa_set_status(struct vdpa_device *vdev, u8 status)
26 down_write(&vdev->cf_lock);
27 vdev->config->set_status(vdev, status);
28 up_write(&vdev->cf_lock);
30 EXPORT_SYMBOL(vdpa_set_status);
32 static struct genl_family vdpa_nl_family;
34 static int vdpa_dev_probe(struct device *d)
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;
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)
48 if (drv && drv->probe)
49 ret = drv->probe(vdev);
54 static void vdpa_dev_remove(struct device *d)
56 struct vdpa_device *vdev = dev_to_vdpa(d);
57 struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
59 if (drv && drv->remove)
63 static int vdpa_dev_match(struct device *dev, struct device_driver *drv)
65 struct vdpa_device *vdev = dev_to_vdpa(dev);
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;
71 /* Currently devices must be supported by all vDPA bus drivers */
75 static ssize_t driver_override_store(struct device *dev,
76 struct device_attribute *attr,
77 const char *buf, size_t count)
79 struct vdpa_device *vdev = dev_to_vdpa(dev);
82 ret = driver_set_override(dev, &vdev->driver_override, buf, count);
89 static ssize_t driver_override_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 struct vdpa_device *vdev = dev_to_vdpa(dev);
96 len = snprintf(buf, PAGE_SIZE, "%s\n", vdev->driver_override);
101 static DEVICE_ATTR_RW(driver_override);
103 static struct attribute *vdpa_dev_attrs[] = {
104 &dev_attr_driver_override.attr,
108 static const struct attribute_group vdpa_dev_group = {
109 .attrs = vdpa_dev_attrs,
111 __ATTRIBUTE_GROUPS(vdpa_dev);
113 static struct bus_type vdpa_bus = {
115 .dev_groups = vdpa_dev_groups,
116 .match = vdpa_dev_match,
117 .probe = vdpa_dev_probe,
118 .remove = vdpa_dev_remove,
121 static void vdpa_release_dev(struct device *d)
123 struct vdpa_device *vdev = dev_to_vdpa(d);
124 const struct vdpa_config_ops *ops = vdev->config;
129 ida_simple_remove(&vdpa_index_ida, vdev->index);
130 kfree(vdev->driver_override);
135 * __vdpa_alloc_device - allocate and initilaize a vDPA device
136 * This allows driver to some prepartion after device is
137 * initialized but before registered.
138 * @parent: the parent device
139 * @config: the bus operations that is supported by this device
140 * @ngroups: number of groups supported by this device
141 * @nas: number of address spaces supported by this device
142 * @size: size of the parent structure that contains private data
143 * @name: name of the vdpa device; optional.
144 * @use_va: indicate whether virtual address must be used by this device
146 * Driver should use vdpa_alloc_device() wrapper macro instead of
147 * using this directly.
149 * Return: Returns an error when parent/config/dma_dev is not set or fail to get
152 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
153 const struct vdpa_config_ops *config,
154 unsigned int ngroups, unsigned int nas,
155 size_t size, const char *name,
158 struct vdpa_device *vdev;
164 if (!!config->dma_map != !!config->dma_unmap)
167 /* It should only work for the device that use on-chip IOMMU */
168 if (use_va && !(config->dma_map || config->set_map))
172 vdev = kzalloc(size, GFP_KERNEL);
176 err = ida_alloc(&vdpa_index_ida, GFP_KERNEL);
180 vdev->dev.bus = &vdpa_bus;
181 vdev->dev.parent = parent;
182 vdev->dev.release = vdpa_release_dev;
184 vdev->config = config;
185 vdev->features_valid = false;
186 vdev->use_va = use_va;
187 vdev->ngroups = ngroups;
191 err = dev_set_name(&vdev->dev, "%s", name);
193 err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
197 init_rwsem(&vdev->cf_lock);
198 device_initialize(&vdev->dev);
203 ida_simple_remove(&vdpa_index_ida, vdev->index);
209 EXPORT_SYMBOL_GPL(__vdpa_alloc_device);
211 static int vdpa_name_match(struct device *dev, const void *data)
213 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
215 return (strcmp(dev_name(&vdev->dev), data) == 0);
218 static int __vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
224 lockdep_assert_held(&vdpa_dev_lock);
225 dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match);
230 return device_add(&vdev->dev);
234 * _vdpa_register_device - register a vDPA device with vdpa lock held
235 * Caller must have a succeed call of vdpa_alloc_device() before.
236 * Caller must invoke this routine in the management device dev_add()
237 * callback after setting up valid mgmtdev for this vdpa device.
238 * @vdev: the vdpa device to be registered to vDPA bus
239 * @nvqs: number of virtqueues supported by this device
241 * Return: Returns an error when fail to add device to vDPA bus
243 int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
248 return __vdpa_register_device(vdev, nvqs);
250 EXPORT_SYMBOL_GPL(_vdpa_register_device);
253 * vdpa_register_device - register a vDPA device
254 * Callers must have a succeed call of vdpa_alloc_device() before.
255 * @vdev: the vdpa device to be registered to vDPA bus
256 * @nvqs: number of virtqueues supported by this device
258 * Return: Returns an error when fail to add to vDPA bus
260 int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs)
264 down_write(&vdpa_dev_lock);
265 err = __vdpa_register_device(vdev, nvqs);
266 up_write(&vdpa_dev_lock);
269 EXPORT_SYMBOL_GPL(vdpa_register_device);
272 * _vdpa_unregister_device - unregister a vDPA device
273 * Caller must invoke this routine as part of management device dev_del()
275 * @vdev: the vdpa device to be unregisted from vDPA bus
277 void _vdpa_unregister_device(struct vdpa_device *vdev)
279 lockdep_assert_held(&vdpa_dev_lock);
280 WARN_ON(!vdev->mdev);
281 device_unregister(&vdev->dev);
283 EXPORT_SYMBOL_GPL(_vdpa_unregister_device);
286 * vdpa_unregister_device - unregister a vDPA device
287 * @vdev: the vdpa device to be unregisted from vDPA bus
289 void vdpa_unregister_device(struct vdpa_device *vdev)
291 down_write(&vdpa_dev_lock);
292 device_unregister(&vdev->dev);
293 up_write(&vdpa_dev_lock);
295 EXPORT_SYMBOL_GPL(vdpa_unregister_device);
298 * __vdpa_register_driver - register a vDPA device driver
299 * @drv: the vdpa device driver to be registered
300 * @owner: module owner of the driver
302 * Return: Returns an err when fail to do the registration
304 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner)
306 drv->driver.bus = &vdpa_bus;
307 drv->driver.owner = owner;
309 return driver_register(&drv->driver);
311 EXPORT_SYMBOL_GPL(__vdpa_register_driver);
314 * vdpa_unregister_driver - unregister a vDPA device driver
315 * @drv: the vdpa device driver to be unregistered
317 void vdpa_unregister_driver(struct vdpa_driver *drv)
319 driver_unregister(&drv->driver);
321 EXPORT_SYMBOL_GPL(vdpa_unregister_driver);
324 * vdpa_mgmtdev_register - register a vdpa management device
326 * @mdev: Pointer to vdpa management device
327 * vdpa_mgmtdev_register() register a vdpa management device which supports
328 * vdpa device management.
329 * Return: Returns 0 on success or failure when required callback ops are not
332 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev)
334 if (!mdev->device || !mdev->ops || !mdev->ops->dev_add || !mdev->ops->dev_del)
337 INIT_LIST_HEAD(&mdev->list);
338 down_write(&vdpa_dev_lock);
339 list_add_tail(&mdev->list, &mdev_head);
340 up_write(&vdpa_dev_lock);
343 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register);
345 static int vdpa_match_remove(struct device *dev, void *data)
347 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
348 struct vdpa_mgmt_dev *mdev = vdev->mdev;
351 mdev->ops->dev_del(mdev, vdev);
355 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev)
357 down_write(&vdpa_dev_lock);
359 list_del(&mdev->list);
361 /* Filter out all the entries belong to this management device and delete it. */
362 bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove);
364 up_write(&vdpa_dev_lock);
366 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister);
368 static void vdpa_get_config_unlocked(struct vdpa_device *vdev,
370 void *buf, unsigned int len)
372 const struct vdpa_config_ops *ops = vdev->config;
375 * Config accesses aren't supposed to trigger before features are set.
376 * If it does happen we assume a legacy guest.
378 if (!vdev->features_valid)
379 vdpa_set_features_unlocked(vdev, 0);
380 ops->get_config(vdev, offset, buf, len);
384 * vdpa_get_config - Get one or more device configuration fields.
385 * @vdev: vdpa device to operate on
386 * @offset: starting byte offset of the field
387 * @buf: buffer pointer to read to
388 * @len: length of the configuration fields in bytes
390 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
391 void *buf, unsigned int len)
393 down_read(&vdev->cf_lock);
394 vdpa_get_config_unlocked(vdev, offset, buf, len);
395 up_read(&vdev->cf_lock);
397 EXPORT_SYMBOL_GPL(vdpa_get_config);
400 * vdpa_set_config - Set 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 from
404 * @length: length of the configuration fields in bytes
406 void vdpa_set_config(struct vdpa_device *vdev, unsigned int offset,
407 const void *buf, unsigned int length)
409 down_write(&vdev->cf_lock);
410 vdev->config->set_config(vdev, offset, buf, length);
411 up_write(&vdev->cf_lock);
413 EXPORT_SYMBOL_GPL(vdpa_set_config);
415 static bool mgmtdev_handle_match(const struct vdpa_mgmt_dev *mdev,
416 const char *busname, const char *devname)
418 /* Bus name is optional for simulated management device, so ignore the
419 * device with bus if bus attribute is provided.
421 if ((busname && !mdev->device->bus) || (!busname && mdev->device->bus))
424 if (!busname && strcmp(dev_name(mdev->device), devname) == 0)
427 if (busname && (strcmp(mdev->device->bus->name, busname) == 0) &&
428 (strcmp(dev_name(mdev->device), devname) == 0))
434 static struct vdpa_mgmt_dev *vdpa_mgmtdev_get_from_attr(struct nlattr **attrs)
436 struct vdpa_mgmt_dev *mdev;
437 const char *busname = NULL;
440 if (!attrs[VDPA_ATTR_MGMTDEV_DEV_NAME])
441 return ERR_PTR(-EINVAL);
442 devname = nla_data(attrs[VDPA_ATTR_MGMTDEV_DEV_NAME]);
443 if (attrs[VDPA_ATTR_MGMTDEV_BUS_NAME])
444 busname = nla_data(attrs[VDPA_ATTR_MGMTDEV_BUS_NAME]);
446 list_for_each_entry(mdev, &mdev_head, list) {
447 if (mgmtdev_handle_match(mdev, busname, devname))
450 return ERR_PTR(-ENODEV);
453 static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff *msg, const struct vdpa_mgmt_dev *mdev)
455 if (mdev->device->bus &&
456 nla_put_string(msg, VDPA_ATTR_MGMTDEV_BUS_NAME, mdev->device->bus->name))
458 if (nla_put_string(msg, VDPA_ATTR_MGMTDEV_DEV_NAME, dev_name(mdev->device)))
463 static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *msg,
464 u32 portid, u32 seq, int flags)
466 u64 supported_classes = 0;
471 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_MGMTDEV_NEW);
474 err = vdpa_nl_mgmtdev_handle_fill(msg, mdev);
478 while (mdev->id_table[i].device) {
479 if (mdev->id_table[i].device <= 63)
480 supported_classes |= BIT_ULL(mdev->id_table[i].device);
484 if (nla_put_u64_64bit(msg, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES,
485 supported_classes, VDPA_ATTR_UNSPEC)) {
489 if (nla_put_u32(msg, VDPA_ATTR_DEV_MGMTDEV_MAX_VQS,
490 mdev->max_supported_vqs)) {
494 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_SUPPORTED_FEATURES,
495 mdev->supported_features, VDPA_ATTR_PAD)) {
500 genlmsg_end(msg, hdr);
504 genlmsg_cancel(msg, hdr);
508 static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *info)
510 struct vdpa_mgmt_dev *mdev;
514 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
518 down_read(&vdpa_dev_lock);
519 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
521 up_read(&vdpa_dev_lock);
522 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device");
527 err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0);
528 up_read(&vdpa_dev_lock);
531 err = genlmsg_reply(msg, info);
540 vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
542 struct vdpa_mgmt_dev *mdev;
543 int start = cb->args[0];
547 down_read(&vdpa_dev_lock);
548 list_for_each_entry(mdev, &mdev_head, list) {
553 err = vdpa_mgmtdev_fill(mdev, msg, NETLINK_CB(cb->skb).portid,
554 cb->nlh->nlmsg_seq, NLM_F_MULTI);
560 up_read(&vdpa_dev_lock);
565 #define VDPA_DEV_NET_ATTRS_MASK (BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) | \
566 BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU) | \
567 BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
569 static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *info)
571 struct vdpa_dev_set_config config = {};
572 struct nlattr **nl_attrs = info->attrs;
573 struct vdpa_mgmt_dev *mdev;
578 if (!info->attrs[VDPA_ATTR_DEV_NAME])
581 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
583 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
584 macaddr = nla_data(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
585 memcpy(config.net.mac, macaddr, sizeof(config.net.mac));
586 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
588 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU]) {
590 nla_get_u16(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MTU]);
591 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MTU);
593 if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]) {
594 config.net.max_vq_pairs =
595 nla_get_u16(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MAX_VQP]);
596 if (!config.net.max_vq_pairs) {
597 NL_SET_ERR_MSG_MOD(info->extack,
598 "At least one pair of VQs is required");
601 config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
604 /* Skip checking capability if user didn't prefer to configure any
605 * device networking attributes. It is likely that user might have used
606 * a device specific method to configure such attributes or using device
607 * default attributes.
609 if ((config.mask & VDPA_DEV_NET_ATTRS_MASK) &&
610 !netlink_capable(skb, CAP_NET_ADMIN))
613 down_write(&vdpa_dev_lock);
614 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
616 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device");
620 if ((config.mask & mdev->config_attr_mask) != config.mask) {
621 NL_SET_ERR_MSG_MOD(info->extack,
622 "All provided attributes are not supported");
627 err = mdev->ops->dev_add(mdev, name, &config);
629 up_write(&vdpa_dev_lock);
633 static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *info)
635 struct vdpa_mgmt_dev *mdev;
636 struct vdpa_device *vdev;
641 if (!info->attrs[VDPA_ATTR_DEV_NAME])
643 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
645 down_write(&vdpa_dev_lock);
646 dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
648 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
652 vdev = container_of(dev, struct vdpa_device, dev);
654 NL_SET_ERR_MSG_MOD(info->extack, "Only user created device can be deleted by user");
659 mdev->ops->dev_del(mdev, vdev);
663 up_write(&vdpa_dev_lock);
668 vdpa_dev_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
669 int flags, struct netlink_ext_ack *extack)
678 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_DEV_NEW);
682 err = vdpa_nl_mgmtdev_handle_fill(msg, vdev->mdev);
686 device_id = vdev->config->get_device_id(vdev);
687 vendor_id = vdev->config->get_vendor_id(vdev);
688 max_vq_size = vdev->config->get_vq_num_max(vdev);
689 if (vdev->config->get_vq_num_min)
690 min_vq_size = vdev->config->get_vq_num_min(vdev);
693 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev)))
695 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id))
697 if (nla_put_u32(msg, VDPA_ATTR_DEV_VENDOR_ID, vendor_id))
699 if (nla_put_u32(msg, VDPA_ATTR_DEV_MAX_VQS, vdev->nvqs))
701 if (nla_put_u16(msg, VDPA_ATTR_DEV_MAX_VQ_SIZE, max_vq_size))
703 if (nla_put_u16(msg, VDPA_ATTR_DEV_MIN_VQ_SIZE, min_vq_size))
706 genlmsg_end(msg, hdr);
710 genlmsg_cancel(msg, hdr);
714 static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
716 struct vdpa_device *vdev;
722 if (!info->attrs[VDPA_ATTR_DEV_NAME])
724 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
729 down_read(&vdpa_dev_lock);
730 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
732 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
736 vdev = container_of(dev, struct vdpa_device, dev);
741 err = vdpa_dev_fill(vdev, msg, info->snd_portid, info->snd_seq, 0, info->extack);
745 err = genlmsg_reply(msg, info);
747 up_read(&vdpa_dev_lock);
753 up_read(&vdpa_dev_lock);
758 struct vdpa_dev_dump_info {
760 struct netlink_callback *cb;
765 static int vdpa_dev_dump(struct device *dev, void *data)
767 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
768 struct vdpa_dev_dump_info *info = data;
773 if (info->idx < info->start_idx) {
777 err = vdpa_dev_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
778 info->cb->nlh->nlmsg_seq, NLM_F_MULTI, info->cb->extack);
786 static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
788 struct vdpa_dev_dump_info info;
792 info.start_idx = cb->args[0];
795 down_read(&vdpa_dev_lock);
796 bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump);
797 up_read(&vdpa_dev_lock);
798 cb->args[0] = info.idx;
802 static int vdpa_dev_net_mq_config_fill(struct vdpa_device *vdev,
803 struct sk_buff *msg, u64 features,
804 const struct virtio_net_config *config)
808 if ((features & BIT_ULL(VIRTIO_NET_F_MQ)) == 0)
811 val_u16 = le16_to_cpu(config->max_virtqueue_pairs);
812 return nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, val_u16);
815 static int vdpa_dev_net_config_fill(struct vdpa_device *vdev, struct sk_buff *msg)
817 struct virtio_net_config config = {};
821 vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
823 if (nla_put(msg, VDPA_ATTR_DEV_NET_CFG_MACADDR, sizeof(config.mac),
827 val_u16 = __virtio16_to_cpu(true, config.status);
828 if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_STATUS, val_u16))
831 val_u16 = __virtio16_to_cpu(true, config.mtu);
832 if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MTU, val_u16))
835 features = vdev->config->get_driver_features(vdev);
836 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_NEGOTIATED_FEATURES, features,
840 return vdpa_dev_net_mq_config_fill(vdev, msg, features, &config);
844 vdpa_dev_config_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
845 int flags, struct netlink_ext_ack *extack)
851 down_read(&vdev->cf_lock);
852 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags,
853 VDPA_CMD_DEV_CONFIG_GET);
859 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev))) {
864 device_id = vdev->config->get_device_id(vdev);
865 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id)) {
872 err = vdpa_dev_net_config_fill(vdev, msg);
881 up_read(&vdev->cf_lock);
882 genlmsg_end(msg, hdr);
886 genlmsg_cancel(msg, hdr);
888 up_read(&vdev->cf_lock);
892 static int vdpa_fill_stats_rec(struct vdpa_device *vdev, struct sk_buff *msg,
893 struct genl_info *info, u32 index)
895 struct virtio_net_config config = {};
901 status = vdev->config->get_status(vdev);
902 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
903 NL_SET_ERR_MSG_MOD(info->extack, "feature negotiation not complete");
906 vdpa_get_config_unlocked(vdev, 0, &config, sizeof(config));
908 max_vqp = __virtio16_to_cpu(true, config.max_virtqueue_pairs);
909 if (nla_put_u16(msg, VDPA_ATTR_DEV_NET_CFG_MAX_VQP, max_vqp))
912 features = vdev->config->get_driver_features(vdev);
913 if (nla_put_u64_64bit(msg, VDPA_ATTR_DEV_NEGOTIATED_FEATURES,
914 features, VDPA_ATTR_PAD))
917 if (nla_put_u32(msg, VDPA_ATTR_DEV_QUEUE_INDEX, index))
920 err = vdev->config->get_vendor_vq_stats(vdev, index, msg, info->extack);
927 static int vendor_stats_fill(struct vdpa_device *vdev, struct sk_buff *msg,
928 struct genl_info *info, u32 index)
932 down_read(&vdev->cf_lock);
933 if (!vdev->config->get_vendor_vq_stats) {
938 err = vdpa_fill_stats_rec(vdev, msg, info, index);
940 up_read(&vdev->cf_lock);
944 static int vdpa_dev_vendor_stats_fill(struct vdpa_device *vdev,
946 struct genl_info *info, u32 index)
951 u32 portid = info->snd_portid;
952 u32 seq = info->snd_seq;
955 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags,
956 VDPA_CMD_DEV_VSTATS_GET);
960 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev))) {
965 device_id = vdev->config->get_device_id(vdev);
966 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id)) {
973 if (index > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX) {
974 NL_SET_ERR_MSG_MOD(info->extack, "queue index excceeds max value");
979 err = vendor_stats_fill(vdev, msg, info, index);
985 genlmsg_end(msg, hdr);
990 genlmsg_cancel(msg, hdr);
994 static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info *info)
996 struct vdpa_device *vdev;
1002 if (!info->attrs[VDPA_ATTR_DEV_NAME])
1004 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1005 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1009 down_read(&vdpa_dev_lock);
1010 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
1012 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1016 vdev = container_of(dev, struct vdpa_device, dev);
1018 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1022 err = vdpa_dev_config_fill(vdev, msg, info->snd_portid, info->snd_seq,
1025 err = genlmsg_reply(msg, info);
1030 up_read(&vdpa_dev_lock);
1036 static int vdpa_dev_config_dump(struct device *dev, void *data)
1038 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
1039 struct vdpa_dev_dump_info *info = data;
1044 if (info->idx < info->start_idx) {
1048 err = vdpa_dev_config_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
1049 info->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1059 vdpa_nl_cmd_dev_config_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
1061 struct vdpa_dev_dump_info info;
1065 info.start_idx = cb->args[0];
1068 down_read(&vdpa_dev_lock);
1069 bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_config_dump);
1070 up_read(&vdpa_dev_lock);
1071 cb->args[0] = info.idx;
1075 static int vdpa_nl_cmd_dev_stats_get_doit(struct sk_buff *skb,
1076 struct genl_info *info)
1078 struct vdpa_device *vdev;
1079 struct sk_buff *msg;
1080 const char *devname;
1085 if (!info->attrs[VDPA_ATTR_DEV_NAME])
1088 if (!info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX])
1091 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
1092 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1096 index = nla_get_u32(info->attrs[VDPA_ATTR_DEV_QUEUE_INDEX]);
1097 down_read(&vdpa_dev_lock);
1098 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
1100 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
1104 vdev = container_of(dev, struct vdpa_device, dev);
1106 NL_SET_ERR_MSG_MOD(info->extack, "unmanaged vdpa device");
1110 err = vdpa_dev_vendor_stats_fill(vdev, msg, info, index);
1114 err = genlmsg_reply(msg, info);
1117 up_read(&vdpa_dev_lock);
1125 up_read(&vdpa_dev_lock);
1129 static const struct nla_policy vdpa_nl_policy[VDPA_ATTR_MAX + 1] = {
1130 [VDPA_ATTR_MGMTDEV_BUS_NAME] = { .type = NLA_NUL_STRING },
1131 [VDPA_ATTR_MGMTDEV_DEV_NAME] = { .type = NLA_STRING },
1132 [VDPA_ATTR_DEV_NAME] = { .type = NLA_STRING },
1133 [VDPA_ATTR_DEV_NET_CFG_MACADDR] = NLA_POLICY_ETH_ADDR,
1134 /* virtio spec 1.1 section 5.1.4.1 for valid MTU range */
1135 [VDPA_ATTR_DEV_NET_CFG_MTU] = NLA_POLICY_MIN(NLA_U16, 68),
1138 static const struct genl_ops vdpa_nl_ops[] = {
1140 .cmd = VDPA_CMD_MGMTDEV_GET,
1141 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1142 .doit = vdpa_nl_cmd_mgmtdev_get_doit,
1143 .dumpit = vdpa_nl_cmd_mgmtdev_get_dumpit,
1146 .cmd = VDPA_CMD_DEV_NEW,
1147 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1148 .doit = vdpa_nl_cmd_dev_add_set_doit,
1149 .flags = GENL_ADMIN_PERM,
1152 .cmd = VDPA_CMD_DEV_DEL,
1153 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1154 .doit = vdpa_nl_cmd_dev_del_set_doit,
1155 .flags = GENL_ADMIN_PERM,
1158 .cmd = VDPA_CMD_DEV_GET,
1159 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1160 .doit = vdpa_nl_cmd_dev_get_doit,
1161 .dumpit = vdpa_nl_cmd_dev_get_dumpit,
1164 .cmd = VDPA_CMD_DEV_CONFIG_GET,
1165 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1166 .doit = vdpa_nl_cmd_dev_config_get_doit,
1167 .dumpit = vdpa_nl_cmd_dev_config_get_dumpit,
1170 .cmd = VDPA_CMD_DEV_VSTATS_GET,
1171 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1172 .doit = vdpa_nl_cmd_dev_stats_get_doit,
1173 .flags = GENL_ADMIN_PERM,
1177 static struct genl_family vdpa_nl_family __ro_after_init = {
1178 .name = VDPA_GENL_NAME,
1179 .version = VDPA_GENL_VERSION,
1180 .maxattr = VDPA_ATTR_MAX,
1181 .policy = vdpa_nl_policy,
1183 .module = THIS_MODULE,
1185 .n_ops = ARRAY_SIZE(vdpa_nl_ops),
1188 static int vdpa_init(void)
1192 err = bus_register(&vdpa_bus);
1195 err = genl_register_family(&vdpa_nl_family);
1201 bus_unregister(&vdpa_bus);
1205 static void __exit vdpa_exit(void)
1207 genl_unregister_family(&vdpa_nl_family);
1208 bus_unregister(&vdpa_bus);
1209 ida_destroy(&vdpa_index_ida);
1211 core_initcall(vdpa_init);
1212 module_exit(vdpa_exit);
1214 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
1215 MODULE_LICENSE("GPL v2");