ntb_pingpong.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 Pingpong Linux driver
  49. *
  50. * Contact Information:
  51. * Allen Hubbe <Allen.Hubbe@emc.com>
  52. */
  53. /* Note: load this module with option 'dyndbg=+p' */
  54. #include <linux/init.h>
  55. #include <linux/kernel.h>
  56. #include <linux/module.h>
  57. #include <linux/dma-mapping.h>
  58. #include <linux/pci.h>
  59. #include <linux/slab.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/debugfs.h>
  62. #include <linux/ntb.h>
  63. #define DRIVER_NAME "ntb_pingpong"
  64. #define DRIVER_DESCRIPTION "PCIe NTB Simple Pingpong Client"
  65. #define DRIVER_LICENSE "Dual BSD/GPL"
  66. #define DRIVER_VERSION "1.0"
  67. #define DRIVER_RELDATE "24 March 2015"
  68. #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
  69. MODULE_LICENSE(DRIVER_LICENSE);
  70. MODULE_VERSION(DRIVER_VERSION);
  71. MODULE_AUTHOR(DRIVER_AUTHOR);
  72. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  73. static unsigned int unsafe;
  74. module_param(unsafe, uint, 0644);
  75. MODULE_PARM_DESC(unsafe, "Run even though ntb operations may be unsafe");
  76. static unsigned int delay_ms = 1000;
  77. module_param(delay_ms, uint, 0644);
  78. MODULE_PARM_DESC(delay_ms, "Milliseconds to delay the response to peer");
  79. static unsigned long db_init = 0x7;
  80. module_param(db_init, ulong, 0644);
  81. MODULE_PARM_DESC(db_init, "Initial doorbell bits to ring on the peer");
  82. struct pp_ctx {
  83. struct ntb_dev *ntb;
  84. u64 db_bits;
  85. /* synchronize access to db_bits by ping and pong */
  86. spinlock_t db_lock;
  87. struct timer_list db_timer;
  88. unsigned long db_delay;
  89. struct dentry *debugfs_node_dir;
  90. struct dentry *debugfs_count;
  91. atomic_t count;
  92. };
  93. static struct dentry *pp_debugfs_dir;
  94. static void pp_ping(unsigned long ctx)
  95. {
  96. struct pp_ctx *pp = (void *)ctx;
  97. unsigned long irqflags;
  98. u64 db_bits, db_mask;
  99. u32 spad_rd, spad_wr;
  100. spin_lock_irqsave(&pp->db_lock, irqflags);
  101. {
  102. db_mask = ntb_db_valid_mask(pp->ntb);
  103. db_bits = ntb_db_read(pp->ntb);
  104. if (db_bits) {
  105. dev_dbg(&pp->ntb->dev,
  106. "Masked pongs %#llx\n",
  107. db_bits);
  108. ntb_db_clear(pp->ntb, db_bits);
  109. }
  110. db_bits = ((pp->db_bits | db_bits) << 1) & db_mask;
  111. if (!db_bits)
  112. db_bits = db_init;
  113. spad_rd = ntb_spad_read(pp->ntb, 0);
  114. spad_wr = spad_rd + 1;
  115. dev_dbg(&pp->ntb->dev,
  116. "Ping bits %#llx read %#x write %#x\n",
  117. db_bits, spad_rd, spad_wr);
  118. ntb_peer_spad_write(pp->ntb, 0, spad_wr);
  119. ntb_peer_db_set(pp->ntb, db_bits);
  120. ntb_db_clear_mask(pp->ntb, db_mask);
  121. pp->db_bits = 0;
  122. }
  123. spin_unlock_irqrestore(&pp->db_lock, irqflags);
  124. }
  125. static void pp_link_event(void *ctx)
  126. {
  127. struct pp_ctx *pp = ctx;
  128. if (ntb_link_is_up(pp->ntb, NULL, NULL) == 1) {
  129. dev_dbg(&pp->ntb->dev, "link is up\n");
  130. pp_ping((unsigned long)pp);
  131. } else {
  132. dev_dbg(&pp->ntb->dev, "link is down\n");
  133. del_timer(&pp->db_timer);
  134. }
  135. }
  136. static void pp_db_event(void *ctx, int vec)
  137. {
  138. struct pp_ctx *pp = ctx;
  139. u64 db_bits, db_mask;
  140. unsigned long irqflags;
  141. spin_lock_irqsave(&pp->db_lock, irqflags);
  142. {
  143. db_mask = ntb_db_vector_mask(pp->ntb, vec);
  144. db_bits = db_mask & ntb_db_read(pp->ntb);
  145. ntb_db_set_mask(pp->ntb, db_mask);
  146. ntb_db_clear(pp->ntb, db_bits);
  147. pp->db_bits |= db_bits;
  148. mod_timer(&pp->db_timer, jiffies + pp->db_delay);
  149. dev_dbg(&pp->ntb->dev,
  150. "Pong vec %d bits %#llx\n",
  151. vec, db_bits);
  152. atomic_inc(&pp->count);
  153. }
  154. spin_unlock_irqrestore(&pp->db_lock, irqflags);
  155. }
  156. static int pp_debugfs_setup(struct pp_ctx *pp)
  157. {
  158. struct pci_dev *pdev = pp->ntb->pdev;
  159. if (!pp_debugfs_dir)
  160. return -ENODEV;
  161. pp->debugfs_node_dir = debugfs_create_dir(pci_name(pdev),
  162. pp_debugfs_dir);
  163. if (!pp->debugfs_node_dir)
  164. return -ENODEV;
  165. pp->debugfs_count = debugfs_create_atomic_t("count", S_IRUSR | S_IWUSR,
  166. pp->debugfs_node_dir,
  167. &pp->count);
  168. if (!pp->debugfs_count)
  169. return -ENODEV;
  170. return 0;
  171. }
  172. static const struct ntb_ctx_ops pp_ops = {
  173. .link_event = pp_link_event,
  174. .db_event = pp_db_event,
  175. };
  176. static int pp_probe(struct ntb_client *client,
  177. struct ntb_dev *ntb)
  178. {
  179. struct pp_ctx *pp;
  180. int rc;
  181. if (ntb_db_is_unsafe(ntb)) {
  182. dev_dbg(&ntb->dev, "doorbell is unsafe\n");
  183. if (!unsafe) {
  184. rc = -EINVAL;
  185. goto err_pp;
  186. }
  187. }
  188. if (ntb_spad_is_unsafe(ntb)) {
  189. dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
  190. if (!unsafe) {
  191. rc = -EINVAL;
  192. goto err_pp;
  193. }
  194. }
  195. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  196. if (!pp) {
  197. rc = -ENOMEM;
  198. goto err_pp;
  199. }
  200. pp->ntb = ntb;
  201. pp->db_bits = 0;
  202. atomic_set(&pp->count, 0);
  203. spin_lock_init(&pp->db_lock);
  204. setup_timer(&pp->db_timer, pp_ping, (unsigned long)pp);
  205. pp->db_delay = msecs_to_jiffies(delay_ms);
  206. rc = ntb_set_ctx(ntb, pp, &pp_ops);
  207. if (rc)
  208. goto err_ctx;
  209. rc = pp_debugfs_setup(pp);
  210. if (rc)
  211. goto err_ctx;
  212. ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  213. ntb_link_event(ntb);
  214. return 0;
  215. err_ctx:
  216. kfree(pp);
  217. err_pp:
  218. return rc;
  219. }
  220. static void pp_remove(struct ntb_client *client,
  221. struct ntb_dev *ntb)
  222. {
  223. struct pp_ctx *pp = ntb->ctx;
  224. debugfs_remove_recursive(pp->debugfs_node_dir);
  225. ntb_clear_ctx(ntb);
  226. del_timer_sync(&pp->db_timer);
  227. ntb_link_disable(ntb);
  228. kfree(pp);
  229. }
  230. static struct ntb_client pp_client = {
  231. .ops = {
  232. .probe = pp_probe,
  233. .remove = pp_remove,
  234. },
  235. };
  236. static int __init pp_init(void)
  237. {
  238. int rc;
  239. if (debugfs_initialized())
  240. pp_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  241. rc = ntb_register_client(&pp_client);
  242. if (rc)
  243. goto err_client;
  244. return 0;
  245. err_client:
  246. debugfs_remove_recursive(pp_debugfs_dir);
  247. return rc;
  248. }
  249. module_init(pp_init);
  250. static void __exit pp_exit(void)
  251. {
  252. ntb_unregister_client(&pp_client);
  253. debugfs_remove_recursive(pp_debugfs_dir);
  254. }
  255. module_exit(pp_exit);