scif_bus.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 Symmetric Communications Interface Bus driver.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/idr.h>
  20. #include <linux/dma-mapping.h>
  21. #include "scif_bus.h"
  22. static ssize_t device_show(struct device *d,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. struct scif_hw_dev *dev = dev_to_scif(d);
  26. return sprintf(buf, "0x%04x\n", dev->id.device);
  27. }
  28. static DEVICE_ATTR_RO(device);
  29. static ssize_t vendor_show(struct device *d,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct scif_hw_dev *dev = dev_to_scif(d);
  33. return sprintf(buf, "0x%04x\n", dev->id.vendor);
  34. }
  35. static DEVICE_ATTR_RO(vendor);
  36. static ssize_t modalias_show(struct device *d,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct scif_hw_dev *dev = dev_to_scif(d);
  40. return sprintf(buf, "scif:d%08Xv%08X\n",
  41. dev->id.device, dev->id.vendor);
  42. }
  43. static DEVICE_ATTR_RO(modalias);
  44. static struct attribute *scif_dev_attrs[] = {
  45. &dev_attr_device.attr,
  46. &dev_attr_vendor.attr,
  47. &dev_attr_modalias.attr,
  48. NULL,
  49. };
  50. ATTRIBUTE_GROUPS(scif_dev);
  51. static inline int scif_id_match(const struct scif_hw_dev *dev,
  52. const struct scif_hw_dev_id *id)
  53. {
  54. if (id->device != dev->id.device && id->device != SCIF_DEV_ANY_ID)
  55. return 0;
  56. return id->vendor == SCIF_DEV_ANY_ID || id->vendor == dev->id.vendor;
  57. }
  58. /*
  59. * This looks through all the IDs a driver claims to support. If any of them
  60. * match, we return 1 and the kernel will call scif_dev_probe().
  61. */
  62. static int scif_dev_match(struct device *dv, struct device_driver *dr)
  63. {
  64. unsigned int i;
  65. struct scif_hw_dev *dev = dev_to_scif(dv);
  66. const struct scif_hw_dev_id *ids;
  67. ids = drv_to_scif(dr)->id_table;
  68. for (i = 0; ids[i].device; i++)
  69. if (scif_id_match(dev, &ids[i]))
  70. return 1;
  71. return 0;
  72. }
  73. static int scif_uevent(struct device *dv, struct kobj_uevent_env *env)
  74. {
  75. struct scif_hw_dev *dev = dev_to_scif(dv);
  76. return add_uevent_var(env, "MODALIAS=scif:d%08Xv%08X",
  77. dev->id.device, dev->id.vendor);
  78. }
  79. static int scif_dev_probe(struct device *d)
  80. {
  81. struct scif_hw_dev *dev = dev_to_scif(d);
  82. struct scif_driver *drv = drv_to_scif(dev->dev.driver);
  83. return drv->probe(dev);
  84. }
  85. static int scif_dev_remove(struct device *d)
  86. {
  87. struct scif_hw_dev *dev = dev_to_scif(d);
  88. struct scif_driver *drv = drv_to_scif(dev->dev.driver);
  89. drv->remove(dev);
  90. return 0;
  91. }
  92. static struct bus_type scif_bus = {
  93. .name = "scif_bus",
  94. .match = scif_dev_match,
  95. .dev_groups = scif_dev_groups,
  96. .uevent = scif_uevent,
  97. .probe = scif_dev_probe,
  98. .remove = scif_dev_remove,
  99. };
  100. int scif_register_driver(struct scif_driver *driver)
  101. {
  102. driver->driver.bus = &scif_bus;
  103. return driver_register(&driver->driver);
  104. }
  105. EXPORT_SYMBOL_GPL(scif_register_driver);
  106. void scif_unregister_driver(struct scif_driver *driver)
  107. {
  108. driver_unregister(&driver->driver);
  109. }
  110. EXPORT_SYMBOL_GPL(scif_unregister_driver);
  111. static void scif_release_dev(struct device *d)
  112. {
  113. struct scif_hw_dev *sdev = dev_to_scif(d);
  114. kfree(sdev);
  115. }
  116. struct scif_hw_dev *
  117. scif_register_device(struct device *pdev, int id, struct dma_map_ops *dma_ops,
  118. struct scif_hw_ops *hw_ops, u8 dnode, u8 snode,
  119. struct mic_mw *mmio, struct mic_mw *aper, void *dp,
  120. void __iomem *rdp, struct dma_chan **chan, int num_chan,
  121. bool card_rel_da)
  122. {
  123. int ret;
  124. struct scif_hw_dev *sdev;
  125. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  126. if (!sdev)
  127. return ERR_PTR(-ENOMEM);
  128. sdev->dev.parent = pdev;
  129. sdev->id.device = id;
  130. sdev->id.vendor = SCIF_DEV_ANY_ID;
  131. sdev->dev.archdata.dma_ops = dma_ops;
  132. sdev->dev.release = scif_release_dev;
  133. sdev->hw_ops = hw_ops;
  134. sdev->dnode = dnode;
  135. sdev->snode = snode;
  136. dev_set_drvdata(&sdev->dev, sdev);
  137. sdev->dev.bus = &scif_bus;
  138. sdev->mmio = mmio;
  139. sdev->aper = aper;
  140. sdev->dp = dp;
  141. sdev->rdp = rdp;
  142. sdev->dev.dma_mask = &sdev->dev.coherent_dma_mask;
  143. dma_set_mask(&sdev->dev, DMA_BIT_MASK(64));
  144. sdev->dma_ch = chan;
  145. sdev->num_dma_ch = num_chan;
  146. sdev->card_rel_da = card_rel_da;
  147. dev_set_name(&sdev->dev, "scif-dev%u", sdev->dnode);
  148. /*
  149. * device_register() causes the bus infrastructure to look for a
  150. * matching driver.
  151. */
  152. ret = device_register(&sdev->dev);
  153. if (ret)
  154. goto free_sdev;
  155. return sdev;
  156. free_sdev:
  157. put_device(&sdev->dev);
  158. return ERR_PTR(ret);
  159. }
  160. EXPORT_SYMBOL_GPL(scif_register_device);
  161. void scif_unregister_device(struct scif_hw_dev *sdev)
  162. {
  163. device_unregister(&sdev->dev);
  164. }
  165. EXPORT_SYMBOL_GPL(scif_unregister_device);
  166. static int __init scif_init(void)
  167. {
  168. return bus_register(&scif_bus);
  169. }
  170. static void __exit scif_exit(void)
  171. {
  172. bus_unregister(&scif_bus);
  173. }
  174. core_initcall(scif_init);
  175. module_exit(scif_exit);
  176. MODULE_AUTHOR("Intel Corporation");
  177. MODULE_DESCRIPTION("Intel(R) SCIF Bus driver");
  178. MODULE_LICENSE("GPL v2");