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 void 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)
46 static struct bus_type vdpa_bus = {
48 .probe = vdpa_dev_probe,
49 .remove = vdpa_dev_remove,
52 static void vdpa_release_dev(struct device *d)
54 struct vdpa_device *vdev = dev_to_vdpa(d);
55 const struct vdpa_config_ops *ops = vdev->config;
60 ida_simple_remove(&vdpa_index_ida, vdev->index);
65 * __vdpa_alloc_device - allocate and initilaize a vDPA device
66 * This allows driver to some prepartion after device is
67 * initialized but before registered.
68 * @parent: the parent device
69 * @config: the bus operations that is supported by this device
70 * @size: size of the parent structure that contains private data
71 * @name: name of the vdpa device; optional.
73 * Driver should use vdpa_alloc_device() wrapper macro instead of
74 * using this directly.
76 * Return: Returns an error when parent/config/dma_dev is not set or fail to get
79 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
80 const struct vdpa_config_ops *config,
81 size_t size, const char *name)
83 struct vdpa_device *vdev;
89 if (!!config->dma_map != !!config->dma_unmap)
93 vdev = kzalloc(size, GFP_KERNEL);
97 err = ida_alloc(&vdpa_index_ida, GFP_KERNEL);
101 vdev->dev.bus = &vdpa_bus;
102 vdev->dev.parent = parent;
103 vdev->dev.release = vdpa_release_dev;
105 vdev->config = config;
106 vdev->features_valid = false;
109 err = dev_set_name(&vdev->dev, "%s", name);
111 err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
115 device_initialize(&vdev->dev);
120 ida_simple_remove(&vdpa_index_ida, vdev->index);
126 EXPORT_SYMBOL_GPL(__vdpa_alloc_device);
128 static int vdpa_name_match(struct device *dev, const void *data)
130 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
132 return (strcmp(dev_name(&vdev->dev), data) == 0);
135 static int __vdpa_register_device(struct vdpa_device *vdev, int nvqs)
141 lockdep_assert_held(&vdpa_dev_mutex);
142 dev = bus_find_device(&vdpa_bus, NULL, dev_name(&vdev->dev), vdpa_name_match);
147 return device_add(&vdev->dev);
151 * _vdpa_register_device - register a vDPA device with vdpa lock held
152 * Caller must have a succeed call of vdpa_alloc_device() before.
153 * Caller must invoke this routine in the management device dev_add()
154 * callback after setting up valid mgmtdev for this vdpa device.
155 * @vdev: the vdpa device to be registered to vDPA bus
156 * @nvqs: number of virtqueues supported by this device
158 * Return: Returns an error when fail to add device to vDPA bus
160 int _vdpa_register_device(struct vdpa_device *vdev, int nvqs)
165 return __vdpa_register_device(vdev, nvqs);
167 EXPORT_SYMBOL_GPL(_vdpa_register_device);
170 * vdpa_register_device - register a vDPA device
171 * Callers must have a succeed call of vdpa_alloc_device() before.
172 * @vdev: the vdpa device to be registered to vDPA bus
173 * @nvqs: number of virtqueues supported by this device
175 * Return: Returns an error when fail to add to vDPA bus
177 int vdpa_register_device(struct vdpa_device *vdev, int nvqs)
181 mutex_lock(&vdpa_dev_mutex);
182 err = __vdpa_register_device(vdev, nvqs);
183 mutex_unlock(&vdpa_dev_mutex);
186 EXPORT_SYMBOL_GPL(vdpa_register_device);
189 * _vdpa_unregister_device - unregister a vDPA device
190 * Caller must invoke this routine as part of management device dev_del()
192 * @vdev: the vdpa device to be unregisted from vDPA bus
194 void _vdpa_unregister_device(struct vdpa_device *vdev)
196 lockdep_assert_held(&vdpa_dev_mutex);
197 WARN_ON(!vdev->mdev);
198 device_unregister(&vdev->dev);
200 EXPORT_SYMBOL_GPL(_vdpa_unregister_device);
203 * vdpa_unregister_device - unregister a vDPA device
204 * @vdev: the vdpa device to be unregisted from vDPA bus
206 void vdpa_unregister_device(struct vdpa_device *vdev)
208 mutex_lock(&vdpa_dev_mutex);
209 device_unregister(&vdev->dev);
210 mutex_unlock(&vdpa_dev_mutex);
212 EXPORT_SYMBOL_GPL(vdpa_unregister_device);
215 * __vdpa_register_driver - register a vDPA device driver
216 * @drv: the vdpa device driver to be registered
217 * @owner: module owner of the driver
219 * Return: Returns an err when fail to do the registration
221 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner)
223 drv->driver.bus = &vdpa_bus;
224 drv->driver.owner = owner;
226 return driver_register(&drv->driver);
228 EXPORT_SYMBOL_GPL(__vdpa_register_driver);
231 * vdpa_unregister_driver - unregister a vDPA device driver
232 * @drv: the vdpa device driver to be unregistered
234 void vdpa_unregister_driver(struct vdpa_driver *drv)
236 driver_unregister(&drv->driver);
238 EXPORT_SYMBOL_GPL(vdpa_unregister_driver);
241 * vdpa_mgmtdev_register - register a vdpa management device
243 * @mdev: Pointer to vdpa management device
244 * vdpa_mgmtdev_register() register a vdpa management device which supports
245 * vdpa device management.
246 * Return: Returns 0 on success or failure when required callback ops are not
249 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev)
251 if (!mdev->device || !mdev->ops || !mdev->ops->dev_add || !mdev->ops->dev_del)
254 INIT_LIST_HEAD(&mdev->list);
255 mutex_lock(&vdpa_dev_mutex);
256 list_add_tail(&mdev->list, &mdev_head);
257 mutex_unlock(&vdpa_dev_mutex);
260 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_register);
262 static int vdpa_match_remove(struct device *dev, void *data)
264 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
265 struct vdpa_mgmt_dev *mdev = vdev->mdev;
268 mdev->ops->dev_del(mdev, vdev);
272 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev)
274 mutex_lock(&vdpa_dev_mutex);
276 list_del(&mdev->list);
278 /* Filter out all the entries belong to this management device and delete it. */
279 bus_for_each_dev(&vdpa_bus, NULL, mdev, vdpa_match_remove);
281 mutex_unlock(&vdpa_dev_mutex);
283 EXPORT_SYMBOL_GPL(vdpa_mgmtdev_unregister);
285 static bool mgmtdev_handle_match(const struct vdpa_mgmt_dev *mdev,
286 const char *busname, const char *devname)
288 /* Bus name is optional for simulated management device, so ignore the
289 * device with bus if bus attribute is provided.
291 if ((busname && !mdev->device->bus) || (!busname && mdev->device->bus))
294 if (!busname && strcmp(dev_name(mdev->device), devname) == 0)
297 if (busname && (strcmp(mdev->device->bus->name, busname) == 0) &&
298 (strcmp(dev_name(mdev->device), devname) == 0))
304 static struct vdpa_mgmt_dev *vdpa_mgmtdev_get_from_attr(struct nlattr **attrs)
306 struct vdpa_mgmt_dev *mdev;
307 const char *busname = NULL;
310 if (!attrs[VDPA_ATTR_MGMTDEV_DEV_NAME])
311 return ERR_PTR(-EINVAL);
312 devname = nla_data(attrs[VDPA_ATTR_MGMTDEV_DEV_NAME]);
313 if (attrs[VDPA_ATTR_MGMTDEV_BUS_NAME])
314 busname = nla_data(attrs[VDPA_ATTR_MGMTDEV_BUS_NAME]);
316 list_for_each_entry(mdev, &mdev_head, list) {
317 if (mgmtdev_handle_match(mdev, busname, devname))
320 return ERR_PTR(-ENODEV);
323 static int vdpa_nl_mgmtdev_handle_fill(struct sk_buff *msg, const struct vdpa_mgmt_dev *mdev)
325 if (mdev->device->bus &&
326 nla_put_string(msg, VDPA_ATTR_MGMTDEV_BUS_NAME, mdev->device->bus->name))
328 if (nla_put_string(msg, VDPA_ATTR_MGMTDEV_DEV_NAME, dev_name(mdev->device)))
333 static int vdpa_mgmtdev_fill(const struct vdpa_mgmt_dev *mdev, struct sk_buff *msg,
334 u32 portid, u32 seq, int flags)
336 u64 supported_classes = 0;
341 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_MGMTDEV_NEW);
344 err = vdpa_nl_mgmtdev_handle_fill(msg, mdev);
348 while (mdev->id_table[i].device) {
349 supported_classes |= BIT(mdev->id_table[i].device);
353 if (nla_put_u64_64bit(msg, VDPA_ATTR_MGMTDEV_SUPPORTED_CLASSES,
354 supported_classes, VDPA_ATTR_UNSPEC)) {
359 genlmsg_end(msg, hdr);
363 genlmsg_cancel(msg, hdr);
367 static int vdpa_nl_cmd_mgmtdev_get_doit(struct sk_buff *skb, struct genl_info *info)
369 struct vdpa_mgmt_dev *mdev;
373 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
377 mutex_lock(&vdpa_dev_mutex);
378 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
380 mutex_unlock(&vdpa_dev_mutex);
381 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified mgmt device");
386 err = vdpa_mgmtdev_fill(mdev, msg, info->snd_portid, info->snd_seq, 0);
387 mutex_unlock(&vdpa_dev_mutex);
390 err = genlmsg_reply(msg, info);
399 vdpa_nl_cmd_mgmtdev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
401 struct vdpa_mgmt_dev *mdev;
402 int start = cb->args[0];
406 mutex_lock(&vdpa_dev_mutex);
407 list_for_each_entry(mdev, &mdev_head, list) {
412 err = vdpa_mgmtdev_fill(mdev, msg, NETLINK_CB(cb->skb).portid,
413 cb->nlh->nlmsg_seq, NLM_F_MULTI);
419 mutex_unlock(&vdpa_dev_mutex);
424 static int vdpa_nl_cmd_dev_add_set_doit(struct sk_buff *skb, struct genl_info *info)
426 struct vdpa_mgmt_dev *mdev;
430 if (!info->attrs[VDPA_ATTR_DEV_NAME])
433 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
435 mutex_lock(&vdpa_dev_mutex);
436 mdev = vdpa_mgmtdev_get_from_attr(info->attrs);
438 NL_SET_ERR_MSG_MOD(info->extack, "Fail to find the specified management device");
443 err = mdev->ops->dev_add(mdev, name);
445 mutex_unlock(&vdpa_dev_mutex);
449 static int vdpa_nl_cmd_dev_del_set_doit(struct sk_buff *skb, struct genl_info *info)
451 struct vdpa_mgmt_dev *mdev;
452 struct vdpa_device *vdev;
457 if (!info->attrs[VDPA_ATTR_DEV_NAME])
459 name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
461 mutex_lock(&vdpa_dev_mutex);
462 dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
464 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
468 vdev = container_of(dev, struct vdpa_device, dev);
470 NL_SET_ERR_MSG_MOD(info->extack, "Only user created device can be deleted by user");
475 mdev->ops->dev_del(mdev, vdev);
479 mutex_unlock(&vdpa_dev_mutex);
484 vdpa_dev_fill(struct vdpa_device *vdev, struct sk_buff *msg, u32 portid, u32 seq,
485 int flags, struct netlink_ext_ack *extack)
493 hdr = genlmsg_put(msg, portid, seq, &vdpa_nl_family, flags, VDPA_CMD_DEV_NEW);
497 err = vdpa_nl_mgmtdev_handle_fill(msg, vdev->mdev);
501 device_id = vdev->config->get_device_id(vdev);
502 vendor_id = vdev->config->get_vendor_id(vdev);
503 max_vq_size = vdev->config->get_vq_num_max(vdev);
506 if (nla_put_string(msg, VDPA_ATTR_DEV_NAME, dev_name(&vdev->dev)))
508 if (nla_put_u32(msg, VDPA_ATTR_DEV_ID, device_id))
510 if (nla_put_u32(msg, VDPA_ATTR_DEV_VENDOR_ID, vendor_id))
512 if (nla_put_u32(msg, VDPA_ATTR_DEV_MAX_VQS, vdev->nvqs))
514 if (nla_put_u16(msg, VDPA_ATTR_DEV_MAX_VQ_SIZE, max_vq_size))
517 genlmsg_end(msg, hdr);
521 genlmsg_cancel(msg, hdr);
525 static int vdpa_nl_cmd_dev_get_doit(struct sk_buff *skb, struct genl_info *info)
527 struct vdpa_device *vdev;
533 if (!info->attrs[VDPA_ATTR_DEV_NAME])
535 devname = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
536 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
540 mutex_lock(&vdpa_dev_mutex);
541 dev = bus_find_device(&vdpa_bus, NULL, devname, vdpa_name_match);
543 NL_SET_ERR_MSG_MOD(info->extack, "device not found");
547 vdev = container_of(dev, struct vdpa_device, dev);
552 err = vdpa_dev_fill(vdev, msg, info->snd_portid, info->snd_seq, 0, info->extack);
554 err = genlmsg_reply(msg, info);
558 mutex_unlock(&vdpa_dev_mutex);
564 struct vdpa_dev_dump_info {
566 struct netlink_callback *cb;
571 static int vdpa_dev_dump(struct device *dev, void *data)
573 struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
574 struct vdpa_dev_dump_info *info = data;
579 if (info->idx < info->start_idx) {
583 err = vdpa_dev_fill(vdev, info->msg, NETLINK_CB(info->cb->skb).portid,
584 info->cb->nlh->nlmsg_seq, NLM_F_MULTI, info->cb->extack);
592 static int vdpa_nl_cmd_dev_get_dumpit(struct sk_buff *msg, struct netlink_callback *cb)
594 struct vdpa_dev_dump_info info;
598 info.start_idx = cb->args[0];
601 mutex_lock(&vdpa_dev_mutex);
602 bus_for_each_dev(&vdpa_bus, NULL, &info, vdpa_dev_dump);
603 mutex_unlock(&vdpa_dev_mutex);
604 cb->args[0] = info.idx;
608 static const struct nla_policy vdpa_nl_policy[VDPA_ATTR_MAX + 1] = {
609 [VDPA_ATTR_MGMTDEV_BUS_NAME] = { .type = NLA_NUL_STRING },
610 [VDPA_ATTR_MGMTDEV_DEV_NAME] = { .type = NLA_STRING },
611 [VDPA_ATTR_DEV_NAME] = { .type = NLA_STRING },
614 static const struct genl_ops vdpa_nl_ops[] = {
616 .cmd = VDPA_CMD_MGMTDEV_GET,
617 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
618 .doit = vdpa_nl_cmd_mgmtdev_get_doit,
619 .dumpit = vdpa_nl_cmd_mgmtdev_get_dumpit,
622 .cmd = VDPA_CMD_DEV_NEW,
623 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
624 .doit = vdpa_nl_cmd_dev_add_set_doit,
625 .flags = GENL_ADMIN_PERM,
628 .cmd = VDPA_CMD_DEV_DEL,
629 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
630 .doit = vdpa_nl_cmd_dev_del_set_doit,
631 .flags = GENL_ADMIN_PERM,
634 .cmd = VDPA_CMD_DEV_GET,
635 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
636 .doit = vdpa_nl_cmd_dev_get_doit,
637 .dumpit = vdpa_nl_cmd_dev_get_dumpit,
641 static struct genl_family vdpa_nl_family __ro_after_init = {
642 .name = VDPA_GENL_NAME,
643 .version = VDPA_GENL_VERSION,
644 .maxattr = VDPA_ATTR_MAX,
645 .policy = vdpa_nl_policy,
647 .module = THIS_MODULE,
649 .n_ops = ARRAY_SIZE(vdpa_nl_ops),
652 static int vdpa_init(void)
656 err = bus_register(&vdpa_bus);
659 err = genl_register_family(&vdpa_nl_family);
665 bus_unregister(&vdpa_bus);
669 static void __exit vdpa_exit(void)
671 genl_unregister_family(&vdpa_nl_family);
672 bus_unregister(&vdpa_bus);
673 ida_destroy(&vdpa_index_ida);
675 core_initcall(vdpa_init);
676 module_exit(vdpa_exit);
678 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
679 MODULE_LICENSE("GPL v2");