scif_peer_bus.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * Intel SCIF driver.
  16. */
  17. #include "scif_main.h"
  18. #include "../bus/scif_bus.h"
  19. #include "scif_peer_bus.h"
  20. static inline struct scif_peer_dev *
  21. dev_to_scif_peer(struct device *dev)
  22. {
  23. return container_of(dev, struct scif_peer_dev, dev);
  24. }
  25. static inline struct scif_peer_driver *
  26. drv_to_scif_peer(struct device_driver *drv)
  27. {
  28. return container_of(drv, struct scif_peer_driver, driver);
  29. }
  30. static int scif_peer_dev_match(struct device *dv, struct device_driver *dr)
  31. {
  32. return !strncmp(dev_name(dv), dr->name, 4);
  33. }
  34. static int scif_peer_dev_probe(struct device *d)
  35. {
  36. struct scif_peer_dev *dev = dev_to_scif_peer(d);
  37. struct scif_peer_driver *drv = drv_to_scif_peer(dev->dev.driver);
  38. return drv->probe(dev);
  39. }
  40. static int scif_peer_dev_remove(struct device *d)
  41. {
  42. struct scif_peer_dev *dev = dev_to_scif_peer(d);
  43. struct scif_peer_driver *drv = drv_to_scif_peer(dev->dev.driver);
  44. drv->remove(dev);
  45. return 0;
  46. }
  47. static struct bus_type scif_peer_bus = {
  48. .name = "scif_peer_bus",
  49. .match = scif_peer_dev_match,
  50. .probe = scif_peer_dev_probe,
  51. .remove = scif_peer_dev_remove,
  52. };
  53. int scif_peer_register_driver(struct scif_peer_driver *driver)
  54. {
  55. driver->driver.bus = &scif_peer_bus;
  56. return driver_register(&driver->driver);
  57. }
  58. void scif_peer_unregister_driver(struct scif_peer_driver *driver)
  59. {
  60. driver_unregister(&driver->driver);
  61. }
  62. static void scif_peer_release_dev(struct device *d)
  63. {
  64. struct scif_peer_dev *sdev = dev_to_scif_peer(d);
  65. struct scif_dev *scifdev = &scif_dev[sdev->dnode];
  66. scif_cleanup_scifdev(scifdev);
  67. kfree(sdev);
  68. }
  69. struct scif_peer_dev *
  70. scif_peer_register_device(struct scif_dev *scifdev)
  71. {
  72. int ret;
  73. struct scif_peer_dev *spdev;
  74. spdev = kzalloc(sizeof(*spdev), GFP_KERNEL);
  75. if (!spdev)
  76. return ERR_PTR(-ENOMEM);
  77. spdev->dev.parent = scifdev->sdev->dev.parent;
  78. spdev->dev.release = scif_peer_release_dev;
  79. spdev->dnode = scifdev->node;
  80. spdev->dev.bus = &scif_peer_bus;
  81. dev_set_name(&spdev->dev, "scif_peer-dev%u", spdev->dnode);
  82. /*
  83. * device_register() causes the bus infrastructure to look for a
  84. * matching driver.
  85. */
  86. ret = device_register(&spdev->dev);
  87. if (ret)
  88. goto free_spdev;
  89. return spdev;
  90. free_spdev:
  91. kfree(spdev);
  92. return ERR_PTR(ret);
  93. }
  94. void scif_peer_unregister_device(struct scif_peer_dev *sdev)
  95. {
  96. device_unregister(&sdev->dev);
  97. }
  98. int scif_peer_bus_init(void)
  99. {
  100. return bus_register(&scif_peer_bus);
  101. }
  102. void scif_peer_bus_exit(void)
  103. {
  104. bus_unregister(&scif_peer_bus);
  105. }