xref: /linux/drivers/vfio/mdev/mdev_driver.c (revision d69d804845985c29ab5be5a4b3b1f4787893daf8)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27b96953bSKirti Wankhede /*
37b96953bSKirti Wankhede  * MDEV driver
47b96953bSKirti Wankhede  *
57b96953bSKirti Wankhede  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
67b96953bSKirti Wankhede  *     Author: Neo Jia <cjia@nvidia.com>
77b96953bSKirti Wankhede  *             Kirti Wankhede <kwankhede@nvidia.com>
87b96953bSKirti Wankhede  */
97b96953bSKirti Wankhede 
107b96953bSKirti Wankhede #include <linux/iommu.h>
117b96953bSKirti Wankhede #include <linux/mdev.h>
127b96953bSKirti Wankhede 
137b96953bSKirti Wankhede #include "mdev_private.h"
147b96953bSKirti Wankhede 
157b96953bSKirti Wankhede static int mdev_probe(struct device *dev)
167b96953bSKirti Wankhede {
1791b9969dSJason Gunthorpe 	struct mdev_driver *drv =
1891b9969dSJason Gunthorpe 		container_of(dev->driver, struct mdev_driver, driver);
197b96953bSKirti Wankhede 
20c68ea0d0SChristoph Hellwig 	if (!drv->probe)
21c68ea0d0SChristoph Hellwig 		return 0;
22c68ea0d0SChristoph Hellwig 	return drv->probe(to_mdev_device(dev));
237b96953bSKirti Wankhede }
247b96953bSKirti Wankhede 
25fc7a6209SUwe Kleine-König static void mdev_remove(struct device *dev)
267b96953bSKirti Wankhede {
2791b9969dSJason Gunthorpe 	struct mdev_driver *drv =
2891b9969dSJason Gunthorpe 		container_of(dev->driver, struct mdev_driver, driver);
297b96953bSKirti Wankhede 
3091b9969dSJason Gunthorpe 	if (drv->remove)
31c68ea0d0SChristoph Hellwig 		drv->remove(to_mdev_device(dev));
327b96953bSKirti Wankhede }
337b96953bSKirti Wankhede 
34*d69d8048SGreg Kroah-Hartman static int mdev_match(struct device *dev, const struct device_driver *drv)
3588a21f26SJason Gunthorpe {
3688a21f26SJason Gunthorpe 	/*
3788a21f26SJason Gunthorpe 	 * No drivers automatically match. Drivers are only bound by explicit
3888a21f26SJason Gunthorpe 	 * device_driver_attach()
3988a21f26SJason Gunthorpe 	 */
4088a21f26SJason Gunthorpe 	return 0;
4188a21f26SJason Gunthorpe }
4288a21f26SJason Gunthorpe 
4377943f4dSRicardo B. Marliere const struct bus_type mdev_bus_type = {
447b96953bSKirti Wankhede 	.name		= "mdev",
457b96953bSKirti Wankhede 	.probe		= mdev_probe,
467b96953bSKirti Wankhede 	.remove		= mdev_remove,
4788a21f26SJason Gunthorpe 	.match		= mdev_match,
487b96953bSKirti Wankhede };
497b96953bSKirti Wankhede 
507b96953bSKirti Wankhede /**
517b96953bSKirti Wankhede  * mdev_register_driver - register a new MDEV driver
527b96953bSKirti Wankhede  * @drv: the driver to register
537b96953bSKirti Wankhede  *
547b96953bSKirti Wankhede  * Returns a negative value on error, otherwise 0.
557b96953bSKirti Wankhede  **/
5691b9969dSJason Gunthorpe int mdev_register_driver(struct mdev_driver *drv)
577b96953bSKirti Wankhede {
58685a1537SChristoph Hellwig 	if (!drv->device_api)
59da44c340SChristoph Hellwig 		return -EINVAL;
60290aac5dSJason Gunthorpe 
61290aac5dSJason Gunthorpe 	/* initialize common driver fields */
627b96953bSKirti Wankhede 	drv->driver.bus = &mdev_bus_type;
637b96953bSKirti Wankhede 	return driver_register(&drv->driver);
647b96953bSKirti Wankhede }
657b96953bSKirti Wankhede EXPORT_SYMBOL(mdev_register_driver);
667b96953bSKirti Wankhede 
677b96953bSKirti Wankhede /*
687b96953bSKirti Wankhede  * mdev_unregister_driver - unregister MDEV driver
697b96953bSKirti Wankhede  * @drv: the driver to unregister
707b96953bSKirti Wankhede  */
717b96953bSKirti Wankhede void mdev_unregister_driver(struct mdev_driver *drv)
727b96953bSKirti Wankhede {
737b96953bSKirti Wankhede 	driver_unregister(&drv->driver);
747b96953bSKirti Wankhede }
757b96953bSKirti Wankhede EXPORT_SYMBOL(mdev_unregister_driver);
76