ntb.c 7.7 KB

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