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 
__idxd_driver_register(struct idxd_device_driver * idxd_drv,struct module * owner,const char * mod_name)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 
idxd_driver_unregister(struct idxd_device_driver * idxd_drv)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 
idxd_config_bus_match(struct device * dev,const struct device_driver * drv)35 static int idxd_config_bus_match(struct device *dev,
36 				 const struct device_driver *drv)
37 {
38 	const struct idxd_device_driver *idxd_drv =
39 		container_of_const(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 
idxd_config_bus_probe(struct device * dev)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 
idxd_config_bus_remove(struct device * dev)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 
idxd_bus_uevent(const struct device * dev,struct kobj_uevent_env * env)70 static int idxd_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
71 {
72 	return add_uevent_var(env, "MODALIAS=" IDXD_DEVICES_MODALIAS_FMT, 0);
73 }
74 
75 const struct bus_type dsa_bus_type = {
76 	.name = "dsa",
77 	.match = idxd_config_bus_match,
78 	.probe = idxd_config_bus_probe,
79 	.remove = idxd_config_bus_remove,
80 	.uevent = idxd_bus_uevent,
81 };
82 EXPORT_SYMBOL_GPL(dsa_bus_type);
83 
dsa_bus_init(void)84 static int __init dsa_bus_init(void)
85 {
86 	return bus_register(&dsa_bus_type);
87 }
88 module_init(dsa_bus_init);
89 
dsa_bus_exit(void)90 static void __exit dsa_bus_exit(void)
91 {
92 	bus_unregister(&dsa_bus_type);
93 }
94 module_exit(dsa_bus_exit);
95 
96 MODULE_DESCRIPTION("IDXD driver dsa_bus_type driver");
97 MODULE_LICENSE("GPL v2");
98