^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vDPA bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2020, Red Hat. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Jason Wang <jasowang@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/vdpa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static DEFINE_IDA(vdpa_index_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static int vdpa_dev_probe(struct device *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct vdpa_device *vdev = dev_to_vdpa(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (drv && drv->probe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) ret = drv->probe(vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int vdpa_dev_remove(struct device *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct vdpa_device *vdev = dev_to_vdpa(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct vdpa_driver *drv = drv_to_vdpa(vdev->dev.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (drv && drv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) drv->remove(vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static struct bus_type vdpa_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .name = "vdpa",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .probe = vdpa_dev_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) .remove = vdpa_dev_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static void vdpa_release_dev(struct device *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct vdpa_device *vdev = dev_to_vdpa(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const struct vdpa_config_ops *ops = vdev->config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (ops->free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ops->free(vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ida_simple_remove(&vdpa_index_ida, vdev->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kfree(vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * __vdpa_alloc_device - allocate and initilaize a vDPA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * This allows driver to some prepartion after device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * initialized but before registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @parent: the parent device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @config: the bus operations that is supported by this device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @nvqs: number of virtqueues supported by this device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @size: size of the parent structure that contains private data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * Driver should use vdpa_alloc_device() wrapper macro instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * using this directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Returns an error when parent/config/dma_dev is not set or fail to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * ida.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct vdpa_device *__vdpa_alloc_device(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const struct vdpa_config_ops *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int nvqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct vdpa_device *vdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!!config->dma_map != !!config->dma_unmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) vdev = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) err = ida_simple_get(&vdpa_index_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto err_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) vdev->dev.bus = &vdpa_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) vdev->dev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) vdev->dev.release = vdpa_release_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) vdev->index = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) vdev->config = config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) vdev->features_valid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) vdev->nvqs = nvqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) err = dev_set_name(&vdev->dev, "vdpa%u", vdev->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto err_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) device_initialize(&vdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return vdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ida_simple_remove(&vdpa_index_ida, vdev->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree(vdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) EXPORT_SYMBOL_GPL(__vdpa_alloc_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * vdpa_register_device - register a vDPA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Callers must have a succeed call of vdpa_alloc_device() before.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @vdev: the vdpa device to be registered to vDPA bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Returns an error when fail to add to vDPA bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int vdpa_register_device(struct vdpa_device *vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return device_add(&vdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) EXPORT_SYMBOL_GPL(vdpa_register_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * vdpa_unregister_device - unregister a vDPA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * @vdev: the vdpa device to be unregisted from vDPA bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void vdpa_unregister_device(struct vdpa_device *vdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) device_unregister(&vdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXPORT_SYMBOL_GPL(vdpa_unregister_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * __vdpa_register_driver - register a vDPA device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @drv: the vdpa device driver to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * @owner: module owner of the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Returns an err when fail to do the registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) drv->driver.bus = &vdpa_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) drv->driver.owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return driver_register(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) EXPORT_SYMBOL_GPL(__vdpa_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * vdpa_unregister_driver - unregister a vDPA device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * @drv: the vdpa device driver to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void vdpa_unregister_driver(struct vdpa_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) driver_unregister(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) EXPORT_SYMBOL_GPL(vdpa_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int vdpa_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return bus_register(&vdpa_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static void __exit vdpa_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) bus_unregister(&vdpa_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ida_destroy(&vdpa_index_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) core_initcall(vdpa_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) module_exit(vdpa_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) MODULE_LICENSE("GPL v2");