ntb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * * Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * * Redistributions in binary form must reproduce the above copy
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * * Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. * PCIe NTB Linux driver
  49. *
  50. * Contact Information:
  51. * Allen Hubbe <Allen.Hubbe@emc.com>
  52. */
  53. #include <linux/device.h>
  54. #include <linux/kernel.h>
  55. #include <linux/module.h>
  56. #include <linux/ntb.h>
  57. #include <linux/pci.h>
  58. #define DRIVER_NAME "ntb"
  59. #define DRIVER_DESCRIPTION "PCIe NTB Driver Framework"
  60. #define DRIVER_LICENSE "Dual BSD/GPL"
  61. #define DRIVER_VERSION "1.0"
  62. #define DRIVER_RELDATE "24 March 2015"
  63. #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
  64. MODULE_LICENSE(DRIVER_LICENSE);
  65. MODULE_VERSION(DRIVER_VERSION);
  66. MODULE_AUTHOR(DRIVER_AUTHOR);
  67. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  68. static struct bus_type ntb_bus;
  69. static void ntb_dev_release(struct device *dev);
  70. int __ntb_register_client(struct ntb_client *client, struct module *mod,
  71. const char *mod_name)
  72. {
  73. if (!client)
  74. return -EINVAL;
  75. if (!ntb_client_ops_is_valid(&client->ops))
  76. return -EINVAL;
  77. memset(&client->drv, 0, sizeof(client->drv));
  78. client->drv.bus = &ntb_bus;
  79. client->drv.name = mod_name;
  80. client->drv.owner = mod;
  81. return driver_register(&client->drv);
  82. }
  83. EXPORT_SYMBOL(__ntb_register_client);
  84. void ntb_unregister_client(struct ntb_client *client)
  85. {
  86. driver_unregister(&client->drv);
  87. }
  88. EXPORT_SYMBOL(ntb_unregister_client);
  89. int ntb_register_device(struct ntb_dev *ntb)
  90. {
  91. if (!ntb)
  92. return -EINVAL;
  93. if (!ntb->pdev)
  94. return -EINVAL;
  95. if (!ntb->ops)
  96. return -EINVAL;
  97. if (!ntb_dev_ops_is_valid(ntb->ops))
  98. return -EINVAL;
  99. init_completion(&ntb->released);
  100. memset(&ntb->dev, 0, sizeof(ntb->dev));
  101. ntb->dev.bus = &ntb_bus;
  102. ntb->dev.parent = &ntb->pdev->dev;
  103. ntb->dev.release = ntb_dev_release;
  104. dev_set_name(&ntb->dev, "%s", pci_name(ntb->pdev));
  105. ntb->ctx = NULL;
  106. ntb->ctx_ops = NULL;
  107. spin_lock_init(&ntb->ctx_lock);
  108. return device_register(&ntb->dev);
  109. }
  110. EXPORT_SYMBOL(ntb_register_device);
  111. void ntb_unregister_device(struct ntb_dev *ntb)
  112. {
  113. device_unregister(&ntb->dev);
  114. wait_for_completion(&ntb->released);
  115. }
  116. EXPORT_SYMBOL(ntb_unregister_device);
  117. int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
  118. const struct ntb_ctx_ops *ctx_ops)
  119. {
  120. unsigned long irqflags;
  121. if (!ntb_ctx_ops_is_valid(ctx_ops))
  122. return -EINVAL;
  123. if (ntb->ctx_ops)
  124. return -EINVAL;
  125. spin_lock_irqsave(&ntb->ctx_lock, irqflags);
  126. {
  127. ntb->ctx = ctx;
  128. ntb->ctx_ops = ctx_ops;
  129. }
  130. spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(ntb_set_ctx);
  134. void ntb_clear_ctx(struct ntb_dev *ntb)
  135. {
  136. unsigned long irqflags;
  137. spin_lock_irqsave(&ntb->ctx_lock, irqflags);
  138. {
  139. ntb->ctx_ops = NULL;
  140. ntb->ctx = NULL;
  141. }
  142. spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
  143. }
  144. EXPORT_SYMBOL(ntb_clear_ctx);
  145. void ntb_link_event(struct ntb_dev *ntb)
  146. {
  147. unsigned long irqflags;
  148. spin_lock_irqsave(&ntb->ctx_lock, irqflags);
  149. {
  150. if (ntb->ctx_ops && ntb->ctx_ops->link_event)
  151. ntb->ctx_ops->link_event(ntb->ctx);
  152. }
  153. spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
  154. }
  155. EXPORT_SYMBOL(ntb_link_event);
  156. void ntb_db_event(struct ntb_dev *ntb, int vector)
  157. {
  158. unsigned long irqflags;
  159. spin_lock_irqsave(&ntb->ctx_lock, irqflags);
  160. {
  161. if (ntb->ctx_ops && ntb->ctx_ops->db_event)
  162. ntb->ctx_ops->db_event(ntb->ctx, vector);
  163. }
  164. spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
  165. }
  166. EXPORT_SYMBOL(ntb_db_event);
  167. static int ntb_probe(struct device *dev)
  168. {
  169. struct ntb_dev *ntb;
  170. struct ntb_client *client;
  171. int rc;
  172. get_device(dev);
  173. ntb = dev_ntb(dev);
  174. client = drv_ntb_client(dev->driver);
  175. rc = client->ops.probe(client, ntb);
  176. if (rc)
  177. put_device(dev);
  178. return rc;
  179. }
  180. static int ntb_remove(struct device *dev)
  181. {
  182. struct ntb_dev *ntb;
  183. struct ntb_client *client;
  184. if (dev->driver) {
  185. ntb = dev_ntb(dev);
  186. client = drv_ntb_client(dev->driver);
  187. client->ops.remove(client, ntb);
  188. put_device(dev);
  189. }
  190. return 0;
  191. }
  192. static void ntb_dev_release(struct device *dev)
  193. {
  194. struct ntb_dev *ntb = dev_ntb(dev);
  195. complete(&ntb->released);
  196. }
  197. static struct bus_type ntb_bus = {
  198. .name = "ntb",
  199. .probe = ntb_probe,
  200. .remove = ntb_remove,
  201. };
  202. static int __init ntb_driver_init(void)
  203. {
  204. return bus_register(&ntb_bus);
  205. }
  206. module_init(ntb_driver_init);
  207. static void __exit ntb_driver_exit(void)
  208. {
  209. bus_unregister(&ntb_bus);
  210. }
  211. module_exit(ntb_driver_exit);