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>
18 static LIST_HEAD(mdev_head);
19 /* A global mutex that protects vdpa management device and device level operations. */
20 static DEFINE_MUTEX(vdpa_dev_mutex);
21 static DEFINE_IDA(vdpa_index_ida);
23 static struct genl_family vdpa_nl_family;
25 static int vdpa_dev_probe(struct device *d)
27 struct vdpa_device *vdev = dev_to_vdpa(d);
28 struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
31 if (drv && drv->probe)
32 ret = drv->probe(vdev);
37 static int vdpa_dev_remove(struct device *d)
39 struct vdpa_device *vdev = dev_to_vdpa(d);
40 struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
42 if (drv && drv->remove)
48 static struct bus_type vdpa_bus = {
50 .probe = vdpa_dev_probe,
51 .remove = vdpa_dev_remove,
54 static void vdpa_release_dev(struct device *d)
56 struct vdpa_device *vdev = dev_to_vdpa(d);
57 const struct vdpa_config_ops *ops = vdev->config;
62 ida_simple_remove(&vdpa_index_ida, vdev->index);
67 * __vdpa_alloc_device - allocate and initilaize a vDPA device
68 * This allows driver to some prepartion after device is
69 * initialized but before registered.
70 * @parent: the parent device
71 * @config: the bus operations that is supported by this device
72 * @size: size of the parent structure that contains private data
73 * @name: name of the vdpa device; optional.
75 * Driver should use vdpa_alloc_device() wrapper macro instead of
76 * using this directly.
78 * Return: Returns an error when parent/config/dma_dev is not set or fail to get
81 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
82 const struct vdpa_config_ops *config,
83 size_t size, const char *name)
85 struct vdpa_device *vdev;
91 if (!!config->dma_map != !!config->dma_unmap)
95 vdev = kzalloc(size, GFP_KERNEL);
99 err = ida_alloc(&vdpa_index_ida, GFP_KERNEL);
103 vdev->dev.bus = &vdpa_bus;
104 vdev->dev.parent = parent;
105 vdev->dev.release = vdpa_release_dev;
107 vdev->config = config;
108 vdev->features_valid = false;
111 err = dev_set_name(&vdev->dev, "%s", name);
113 err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
117 device_initialize(&vdev->dev);
122 ida_simple_remove(&vdpa_index_ida, vdev->index);
128 EXPORT_SYMBOL_GPL(__vdpa_alloc_device);
130 static int vdpa_name_match(struct device *dev, const void *data)
132 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
134 return (strcmp(dev_name(&vdev->dev), data) == 0);
137 static int __vdpa_register_device(struct vdpa_device *vdev, int nvqs)
143 lockdep_assert_held(&vdpa_dev_mutex);
144 dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match);
149 return device_add(&vdev->dev);
153 * _vdpa_register_device - register a vDPA device with vdpa lock held
154 * Caller must have a succeed call of vdpa_alloc_device() before.
155 * Caller must invoke this routine in the management device dev_add()
156 * callback after setting up valid mgmtdev for this vdpa device.
157 * @vdev: the vdpa device to be registered to vDPA bus
158 * @nvqs: number of virtqueues supported by this device
160 * Return: Returns an error when fail to add device to vDPA bus
162 int _vdpa_register_device(struct vdpa_device *vdev, int nvqs)
167 return __vdpa_register_device(vdev, nvqs);
169 EXPORT_SYMBOL_GPL(_vdpa_register_device);
172 * vdpa_register_device - register a vDPA device
173 * Callers must have a succeed call of vdpa_alloc_device() before.
174 * @vdev: the vdpa device to be registered to vDPA bus
175 * @nvqs: number of virtqueues supported by this device
177 * Return: Returns an error when fail to add to vDPA bus
179 int vdpa_register_device(struct vdpa_device *vdev, int nvqs)
183 mutex_lock(&vdpa_dev_mutex);
184 err = __vdpa_register_device(vdev, nvqs);
185 mutex_unlock(&vdpa_dev_mutex);
188 EXPORT_SYMBOL_GPL(vdpa_register_device);
191 * _vdpa_unregister_device - unregister a vDPA device
192 * Caller must invoke this routine as part of management device dev_del()
194 * @vdev: the vdpa device to be unregisted from vDPA bus
196 void _vdpa_unregister_device(struct vdpa_device *vdev)
198 lockdep_assert_held(&vdpa_dev_mutex);
199 WARN_ON(!vdev->mdev);
200 device_unregister(&vdev->dev);
202 EXPORT_SYMBOL_GPL(_vdpa_unregister_device);
205 * vdpa_unregister_device - unregister a vDPA device
206 * @vdev: the vdpa device to be unregisted from vDPA bus
208 void vdpa_unregister_device(struct vdpa_device *vdev)
210 mutex_lock(&vdpa_dev_mutex);
211 device_unregister(&vdev->dev);
212 mutex_unlock(&vdpa_dev_mutex);
214 EXPORT_SYMBOL_GPL(vdpa_unregister_device);
217 * __vdpa_register_driver - register a vDPA device driver
218 * @drv: the vdpa device driver to be registered
219 * @owner: module owner of the driver
221 * Return: Returns an err when fail to do the registration
223 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner)
225 drv->driver.bus = &vdpa_bus;
226 drv->driver.owner = owner;
228 return driver_register(&drv->driver);
230 EXPORT_SYMBOL_GPL(__vdpa_register_driver);
233 * vdpa_unregister_driver - unregister a vDPA device driver
234 * @drv: the vdpa device driver to be unregistered
236 void vdpa_unregister_driver(struct vdpa_driver *drv)
238 driver_unregister(&drv->driver);
240 EXPORT_SYMBOL_GPL(vdpa_unregister_driver);
243 * vdpa_mgmtdev_register - register a vdpa management device
245 * @mdev: Pointer to vdpa management device
246 * vdpa_mgmtdev_register() register a vdpa management device which supports
247 * vdpa device management.
248 * Return: Returns 0 on success or failure when required callback ops are not
251 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev)
253 if (!mdev->device || !mdev->ops || !mdev->ops->dev_add || !mdev->ops->dev_del)
256 INIT_LIST_HEAD(&mdev->list);
257 mutex_lock(&vdpa_dev_mutex);
258 list_add_tail(&mdev->list, &mdev_head);
259 mutex_unlock(&vdpa_dev_mutex);
262 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register);
264 static int vdpa_match_remove(struct device *dev, void *data)
266 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
267 struct vdpa_mgmt_dev *mdev = vdev->mdev;
270 mdev->ops->dev_del(mdev, vdev);
274 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev)
276 mutex_lock(&vdpa_dev_mutex);
278 list_del(&mdev->list);
280 /* Filter out all the entries belong to this management device and delete it. */
281 bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove);
283 mutex_unlock(&vdpa_dev_mutex);
285 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister);
287 static bool mgmtdev_handle_match(const struct vdpa_mgmt_dev *mdev,
288 const char *busname, const char *devname)
290 /* Bus name is optional for simulated management device, so ignore the
291 * device with bus if bus attribute is provided.
293 if ((busname && !mdev->device->bus) || (!busname && mdev->device->bus))
296 if (!busname && strcmp(dev_name(mdev->device), devname) == 0)
299 if (busname && (strcmp(mdev->device->bus->name, busname) == 0) &&
300 (strcmp(dev_name(mdev->device), devname) == 0))
306 static struct vdpa_mgmt_dev *vdpa_mgmtdev_get_from_attr(struct nlattr **attrs)
308 struct vdpa_mgmt_dev *mdev;
309 const char *busname = NULL;
312 if (!attrs[VDPA_ATTR_MGMTDEV_DEV_NAME])
313 return ERR_PTR(-EINVAL);
314 devname = nla_data(attrs[VDPA_ATTR_MGMTDEV_DEV_NAME]);
315 if (attrs[VDPA_ATTR_MGMTDEV_BUS_NAME])
316 busname = nla_data(attrs[VDPA_ATTR_MGMTDEV_BUS_NAME]);
318 list_for_each_entry(mdev, &mdev_head, list) {
319 if (mgmtdev_handle_match(mdev, busname, devname))
322 return ERR_PTR(-ENODEV);
325 static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff *msg, const struct vdpa_mgmt_dev *mdev)
327 if (mdev->device->bus &&
328 nla_put_string(msg, VDPA_ATTR_MGMTDEV_BUS_NAME, mdev->device->bus->name))
330 if (nla_put_string(msg, VDPA_ATTR_MGMTDEV_DEV_NAME, dev_name(mdev->device)))
335 static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *msg,
336 u32 portid, u32 seq, int flags)
338 u64 supported_classes = 0;
343 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_MGMTDEV_NEW);
346 err = vdpa_nl_mgmtdev_handle_fill(msg, mdev);
350 while (mdev->id_table[i].device) {
351 supported_classes |= BIT(mdev->id_table[i].device);
355 if (nla_put_u64_64bit(msg, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES,
356 supported_classes, VDPA_ATTR_UNSPEC)) {
361 genlmsg_end(msg, hdr);
365 genlmsg_cancel(msg, hdr);
369 static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *info)
371 struct vdpa_mgmt_dev *mdev;
375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
379 mutex_lock(&vdpa_dev_mutex);
380 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
382 mutex_unlock(&vdpa_dev_mutex);
383 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device");
388 err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0);
389 mutex_unlock(&vdpa_dev_mutex);
392 err = genlmsg_reply(msg, info);
401 vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
403 struct vdpa_mgmt_dev *mdev;
404 int start = cb->args[0];
408 mutex_lock(&vdpa_dev_mutex);
409 list_for_each_entry(mdev, &mdev_head, list) {
414 err = vdpa_mgmtdev_fill(mdev, msg, NETLINK_CB(cb->skb).portid,
415 cb->nlh->nlmsg_seq, NLM_F_MULTI);
421 mutex_unlock(&vdpa_dev_mutex);
426 static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *info)
428 struct vdpa_mgmt_dev *mdev;
432 if (!info->attrs[VDPA_ATTR_DEV_NAME])
435 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
437 mutex_lock(&vdpa_dev_mutex);
438 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
440 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device");
445 err = mdev->ops->dev_add(mdev, name);
447 mutex_unlock(&vdpa_dev_mutex);
451 static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *info)
453 struct vdpa_mgmt_dev *mdev;
454 struct vdpa_device *vdev;
459 if (!info->attrs[VDPA_ATTR_DEV_NAME])
461 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
463 mutex_lock(&vdpa_dev_mutex);
464 dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
466 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
470 vdev = container_of(dev, struct vdpa_device, dev);
472 NL_SET_ERR_MSG_MOD(info->extack, "Only user created device can be deleted by user");
477 mdev->ops->dev_del(mdev, vdev);
481 mutex_unlock(&vdpa_dev_mutex);
486 vdpa_dev_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
487 int flags, struct netlink_ext_ack *extack)
495 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_DEV_NEW);
499 err = vdpa_nl_mgmtdev_handle_fill(msg, vdev->mdev);
503 device_id = vdev->config->get_device_id(vdev);
504 vendor_id = vdev->config->get_vendor_id(vdev);
505 max_vq_size = vdev->config->get_vq_num_max(vdev);
508 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev)))
510 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id))
512 if (nla_put_u32(msg, VDPA_ATTR_DEV_VENDOR_ID, vendor_id))
514 if (nla_put_u32(msg, VDPA_ATTR_DEV_MAX_VQS, vdev->nvqs))
516 if (nla_put_u16(msg, VDPA_ATTR_DEV_MAX_VQ_SIZE, max_vq_size))
519 genlmsg_end(msg, hdr);
523 genlmsg_cancel(msg, hdr);
527 static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
529 struct vdpa_device *vdev;
535 if (!info->attrs[VDPA_ATTR_DEV_NAME])
537 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
538 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
542 mutex_lock(&vdpa_dev_mutex);
543 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
545 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
549 vdev = container_of(dev, struct vdpa_device, dev);
554 err = vdpa_dev_fill(vdev, msg, info->snd_portid, info->snd_seq, 0, info->extack);
556 err = genlmsg_reply(msg, info);
560 mutex_unlock(&vdpa_dev_mutex);
566 struct vdpa_dev_dump_info {
568 struct netlink_callback *cb;
573 static int vdpa_dev_dump(struct device *dev, void *data)
575 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
576 struct vdpa_dev_dump_info *info = data;
581 if (info->idx < info->start_idx) {
585 err = vdpa_dev_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
586 info->cb->nlh->nlmsg_seq, NLM_F_MULTI, info->cb->extack);
594 static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
596 struct vdpa_dev_dump_info info;
600 info.start_idx = cb->args[0];
603 mutex_lock(&vdpa_dev_mutex);
604 bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump);
605 mutex_unlock(&vdpa_dev_mutex);
606 cb->args[0] = info.idx;
610 static const struct nla_policy vdpa_nl_policy[VDPA_ATTR_MAX + 1] = {
611 [VDPA_ATTR_MGMTDEV_BUS_NAME] = { .type = NLA_NUL_STRING },
612 [VDPA_ATTR_MGMTDEV_DEV_NAME] = { .type = NLA_STRING },
613 [VDPA_ATTR_DEV_NAME] = { .type = NLA_STRING },
616 static const struct genl_ops vdpa_nl_ops[] = {
618 .cmd = VDPA_CMD_MGMTDEV_GET,
619 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
620 .doit = vdpa_nl_cmd_mgmtdev_get_doit,
621 .dumpit = vdpa_nl_cmd_mgmtdev_get_dumpit,
624 .cmd = VDPA_CMD_DEV_NEW,
625 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
626 .doit = vdpa_nl_cmd_dev_add_set_doit,
627 .flags = GENL_ADMIN_PERM,
630 .cmd = VDPA_CMD_DEV_DEL,
631 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
632 .doit = vdpa_nl_cmd_dev_del_set_doit,
633 .flags = GENL_ADMIN_PERM,
636 .cmd = VDPA_CMD_DEV_GET,
637 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
638 .doit = vdpa_nl_cmd_dev_get_doit,
639 .dumpit = vdpa_nl_cmd_dev_get_dumpit,
643 static struct genl_family vdpa_nl_family __ro_after_init = {
644 .name = VDPA_GENL_NAME,
645 .version = VDPA_GENL_VERSION,
646 .maxattr = VDPA_ATTR_MAX,
647 .policy = vdpa_nl_policy,
649 .module = THIS_MODULE,
651 .n_ops = ARRAY_SIZE(vdpa_nl_ops),
654 static int vdpa_init(void)
658 err = bus_register(&vdpa_bus);
661 err = genl_register_family(&vdpa_nl_family);
667 bus_unregister(&vdpa_bus);
671 static void __exit vdpa_exit(void)
673 genl_unregister_family(&vdpa_nl_family);
674 bus_unregister(&vdpa_bus);
675 ida_destroy(&vdpa_index_ida);
677 core_initcall(vdpa_init);
678 module_exit(vdpa_exit);
680 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
681 MODULE_LICENSE("GPL v2");