xref: /openbmc/linux/drivers/dma/idxd/bus.c (revision 76a4f7cc)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2021 Intel Corporation. All rights rsvd. */
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/device.h>
7 #include "idxd.h"
8 
9 
10 int __idxd_driver_register(struct idxd_device_driver *idxd_drv, struct module *owner,
11 			   const char *mod_name)
12 {
13 	struct device_driver *drv = &idxd_drv->drv;
14 
15 	if (!idxd_drv->type) {
16 		pr_debug("driver type not set (%ps)\n", __builtin_return_address(0));
17 		return -EINVAL;
18 	}
19 
20 	drv->name = idxd_drv->name;
21 	drv->bus = &dsa_bus_type;
22 	drv->owner = owner;
23 	drv->mod_name = mod_name;
24 
25 	return driver_register(drv);
26 }
27 EXPORT_SYMBOL_GPL(__idxd_driver_register);
28 
29 void idxd_driver_unregister(struct idxd_device_driver *idxd_drv)
30 {
31 	driver_unregister(&idxd_drv->drv);
32 }
33 EXPORT_SYMBOL_GPL(idxd_driver_unregister);
34 
35 static int idxd_config_bus_match(struct device *dev,
36 				 struct device_driver *drv)
37 {
38 	struct idxd_device_driver *idxd_drv =
39 		container_of(drv, struct idxd_device_driver, drv);
40 	struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
41 	int i = 0;
42 
43 	while (idxd_drv->type[i] != IDXD_DEV_NONE) {
44 		if (idxd_dev->type == idxd_drv->type[i])
45 			return 1;
46 		i++;
47 	}
48 
49 	return 0;
50 }
51 
52 static int idxd_config_bus_probe(struct device *dev)
53 {
54 	struct idxd_device_driver *idxd_drv =
55 		container_of(dev->driver, struct idxd_device_driver, drv);
56 	struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
57 
58 	return idxd_drv->probe(idxd_dev);
59 }
60 
61 static void idxd_config_bus_remove(struct device *dev)
62 {
63 	struct idxd_device_driver *idxd_drv =
64 		container_of(dev->driver, struct idxd_device_driver, drv);
65 	struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
66 
67 	idxd_drv->remove(idxd_dev);
68 }
69 
70 struct bus_type dsa_bus_type = {
71 	.name = "dsa",
72 	.match = idxd_config_bus_match,
73 	.probe = idxd_config_bus_probe,
74 	.remove = idxd_config_bus_remove,
75 };
76 EXPORT_SYMBOL_GPL(dsa_bus_type);
77 
78 static int __init dsa_bus_init(void)
79 {
80 	return bus_register(&dsa_bus_type);
81 }
82 module_init(dsa_bus_init);
83 
84 static void __exit dsa_bus_exit(void)
85 {
86 	bus_unregister(&dsa_bus_type);
87 }
88 module_exit(dsa_bus_exit);
89 
90 MODULE_DESCRIPTION("IDXD driver dsa_bus_type driver");
91 MODULE_LICENSE("GPL v2");
92