core.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_VERSION "1.0"
  63. #define DRIVER_RELDATE "24 March 2015"
  64. #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
  65. MODULE_LICENSE("Dual BSD/GPL");
  66. MODULE_VERSION(DRIVER_VERSION);
  67. MODULE_AUTHOR(DRIVER_AUTHOR);
  68. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  69. static struct bus_type ntb_bus;
  70. static void ntb_dev_release(struct device *dev);
  71. int __ntb_register_client(struct ntb_client *client, struct module *mod,
  72. const char *mod_name)
  73. {
  74. if (!client)
  75. return -EINVAL;
  76. if (!ntb_client_ops_is_valid(&client->ops))
  77. return -EINVAL;
  78. memset(&client->drv, 0, sizeof(client->drv));
  79. client->drv.bus = &ntb_bus;
  80. client->drv.name = mod_name;
  81. client->drv.owner = mod;
  82. return driver_register(&client->drv);
  83. }
  84. EXPORT_SYMBOL(__ntb_register_client);
  85. void ntb_unregister_client(struct ntb_client *client)
  86. {
  87. driver_unregister(&client->drv);
  88. }
  89. EXPORT_SYMBOL(ntb_unregister_client);
  90. int ntb_register_device(struct ntb_dev *ntb)
  91. {
  92. if (!ntb)
  93. return -EINVAL;
  94. if (!ntb->pdev)
  95. return -EINVAL;
  96. if (!ntb->ops)
  97. return -EINVAL;
  98. if (!ntb_dev_ops_is_valid(ntb->ops))
  99. return -EINVAL;
  100. init_completion(&ntb->released);
  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. void ntb_msg_event(struct ntb_dev *ntb)
  168. {
  169. unsigned long irqflags;
  170. spin_lock_irqsave(&ntb->ctx_lock, irqflags);
  171. {
  172. if (ntb->ctx_ops && ntb->ctx_ops->msg_event)
  173. ntb->ctx_ops->msg_event(ntb->ctx);
  174. }
  175. spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
  176. }
  177. EXPORT_SYMBOL(ntb_msg_event);
  178. int ntb_default_port_number(struct ntb_dev *ntb)
  179. {
  180. switch (ntb->topo) {
  181. case NTB_TOPO_PRI:
  182. case NTB_TOPO_B2B_USD:
  183. return NTB_PORT_PRI_USD;
  184. case NTB_TOPO_SEC:
  185. case NTB_TOPO_B2B_DSD:
  186. return NTB_PORT_SEC_DSD;
  187. default:
  188. return 0;
  189. }
  190. }
  191. EXPORT_SYMBOL(ntb_default_port_number);
  192. int ntb_default_peer_port_count(struct ntb_dev *ntb)
  193. {
  194. return NTB_DEF_PEER_CNT;
  195. }
  196. EXPORT_SYMBOL(ntb_default_peer_port_count);
  197. int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx)
  198. {
  199. if (pidx != NTB_DEF_PEER_IDX)
  200. return -EINVAL;
  201. switch (ntb->topo) {
  202. case NTB_TOPO_PRI:
  203. case NTB_TOPO_B2B_USD:
  204. return NTB_PORT_SEC_DSD;
  205. case NTB_TOPO_SEC:
  206. case NTB_TOPO_B2B_DSD:
  207. return NTB_PORT_PRI_USD;
  208. default:
  209. return 0;
  210. }
  211. }
  212. EXPORT_SYMBOL(ntb_default_peer_port_number);
  213. int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port)
  214. {
  215. int peer_port = ntb_default_peer_port_number(ntb, NTB_DEF_PEER_IDX);
  216. if (peer_port == -EINVAL || port != peer_port)
  217. return -EINVAL;
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(ntb_default_peer_port_idx);
  221. static int ntb_probe(struct device *dev)
  222. {
  223. struct ntb_dev *ntb;
  224. struct ntb_client *client;
  225. int rc;
  226. get_device(dev);
  227. ntb = dev_ntb(dev);
  228. client = drv_ntb_client(dev->driver);
  229. rc = client->ops.probe(client, ntb);
  230. if (rc)
  231. put_device(dev);
  232. return rc;
  233. }
  234. static int ntb_remove(struct device *dev)
  235. {
  236. struct ntb_dev *ntb;
  237. struct ntb_client *client;
  238. if (dev->driver) {
  239. ntb = dev_ntb(dev);
  240. client = drv_ntb_client(dev->driver);
  241. client->ops.remove(client, ntb);
  242. put_device(dev);
  243. }
  244. return 0;
  245. }
  246. static void ntb_dev_release(struct device *dev)
  247. {
  248. struct ntb_dev *ntb = dev_ntb(dev);
  249. complete(&ntb->released);
  250. }
  251. static struct bus_type ntb_bus = {
  252. .name = "ntb",
  253. .probe = ntb_probe,
  254. .remove = ntb_remove,
  255. };
  256. static int __init ntb_driver_init(void)
  257. {
  258. return bus_register(&ntb_bus);
  259. }
  260. module_init(ntb_driver_init);
  261. static void __exit ntb_driver_exit(void)
  262. {
  263. bus_unregister(&ntb_bus);
  264. }
  265. module_exit(ntb_driver_exit);