bus.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. int __idxd_driver_register(struct idxd_device_driver *idxd_drv, struct module *owner,
  9. const char *mod_name)
  10. {
  11. struct device_driver *drv = &idxd_drv->drv;
  12. if (!idxd_drv->type) {
  13. pr_debug("driver type not set (%ps)\n", __builtin_return_address(0));
  14. return -EINVAL;
  15. }
  16. drv->name = idxd_drv->name;
  17. drv->bus = &dsa_bus_type;
  18. drv->owner = owner;
  19. drv->mod_name = mod_name;
  20. return driver_register(drv);
  21. }
  22. EXPORT_SYMBOL_GPL(__idxd_driver_register);
  23. void idxd_driver_unregister(struct idxd_device_driver *idxd_drv)
  24. {
  25. driver_unregister(&idxd_drv->drv);
  26. }
  27. EXPORT_SYMBOL_GPL(idxd_driver_unregister);
  28. static int idxd_config_bus_match(struct device *dev,
  29. const struct device_driver *drv)
  30. {
  31. const struct idxd_device_driver *idxd_drv =
  32. container_of_const(drv, struct idxd_device_driver, drv);
  33. struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
  34. int i = 0;
  35. while (idxd_drv->type[i] != IDXD_DEV_NONE) {
  36. if (idxd_dev->type == idxd_drv->type[i])
  37. return 1;
  38. i++;
  39. }
  40. return 0;
  41. }
  42. static int idxd_config_bus_probe(struct device *dev)
  43. {
  44. struct idxd_device_driver *idxd_drv =
  45. container_of(dev->driver, struct idxd_device_driver, drv);
  46. struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
  47. return idxd_drv->probe(idxd_dev);
  48. }
  49. static void idxd_config_bus_remove(struct device *dev)
  50. {
  51. struct idxd_device_driver *idxd_drv =
  52. container_of(dev->driver, struct idxd_device_driver, drv);
  53. struct idxd_dev *idxd_dev = confdev_to_idxd_dev(dev);
  54. idxd_drv->remove(idxd_dev);
  55. }
  56. static int idxd_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
  57. {
  58. return add_uevent_var(env, "MODALIAS=" IDXD_DEVICES_MODALIAS_FMT, 0);
  59. }
  60. const struct bus_type dsa_bus_type = {
  61. .name = "dsa",
  62. .match = idxd_config_bus_match,
  63. .probe = idxd_config_bus_probe,
  64. .remove = idxd_config_bus_remove,
  65. .uevent = idxd_bus_uevent,
  66. };
  67. EXPORT_SYMBOL_GPL(dsa_bus_type);
  68. static int __init dsa_bus_init(void)
  69. {
  70. return bus_register(&dsa_bus_type);
  71. }
  72. module_init(dsa_bus_init);
  73. static void __exit dsa_bus_exit(void)
  74. {
  75. bus_unregister(&dsa_bus_type);
  76. }
  77. module_exit(dsa_bus_exit);
  78. MODULE_DESCRIPTION("IDXD driver dsa_bus_type driver");
  79. MODULE_LICENSE("GPL v2");