ntb_pingpong.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/ntb.h>
  62. #define DRIVER_NAME "ntb_pingpong"
  63. #define DRIVER_DESCRIPTION "PCIe NTB Simple Pingpong Client"
  64. #define DRIVER_LICENSE "Dual BSD/GPL"
  65. #define DRIVER_VERSION "1.0"
  66. #define DRIVER_RELDATE "24 March 2015"
  67. #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
  68. MODULE_LICENSE(DRIVER_LICENSE);
  69. MODULE_VERSION(DRIVER_VERSION);
  70. MODULE_AUTHOR(DRIVER_AUTHOR);
  71. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  72. static unsigned int unsafe;
  73. module_param(unsafe, uint, 0644);
  74. MODULE_PARM_DESC(unsafe, "Run even though ntb operations may be unsafe");
  75. static unsigned int delay_ms = 1000;
  76. module_param(delay_ms, uint, 0644);
  77. MODULE_PARM_DESC(delay_ms, "Milliseconds to delay the response to peer");
  78. static unsigned long db_init = 0x7;
  79. module_param(db_init, ulong, 0644);
  80. MODULE_PARM_DESC(delay_ms, "Initial doorbell bits to ring on the peer");
  81. struct pp_ctx {
  82. struct ntb_dev *ntb;
  83. u64 db_bits;
  84. /* synchronize access to db_bits by ping and pong */
  85. spinlock_t db_lock;
  86. struct timer_list db_timer;
  87. unsigned long db_delay;
  88. };
  89. static void pp_ping(unsigned long ctx)
  90. {
  91. struct pp_ctx *pp = (void *)ctx;
  92. unsigned long irqflags;
  93. u64 db_bits, db_mask;
  94. u32 spad_rd, spad_wr;
  95. spin_lock_irqsave(&pp->db_lock, irqflags);
  96. {
  97. db_mask = ntb_db_valid_mask(pp->ntb);
  98. db_bits = ntb_db_read(pp->ntb);
  99. if (db_bits) {
  100. dev_dbg(&pp->ntb->dev,
  101. "Masked pongs %#llx\n",
  102. db_bits);
  103. ntb_db_clear(pp->ntb, db_bits);
  104. }
  105. db_bits = ((pp->db_bits | db_bits) << 1) & db_mask;
  106. if (!db_bits)
  107. db_bits = db_init;
  108. spad_rd = ntb_spad_read(pp->ntb, 0);
  109. spad_wr = spad_rd + 1;
  110. dev_dbg(&pp->ntb->dev,
  111. "Ping bits %#llx read %#x write %#x\n",
  112. db_bits, spad_rd, spad_wr);
  113. ntb_peer_spad_write(pp->ntb, 0, spad_wr);
  114. ntb_peer_db_set(pp->ntb, db_bits);
  115. ntb_db_clear_mask(pp->ntb, db_mask);
  116. pp->db_bits = 0;
  117. }
  118. spin_unlock_irqrestore(&pp->db_lock, irqflags);
  119. }
  120. static void pp_link_event(void *ctx)
  121. {
  122. struct pp_ctx *pp = ctx;
  123. if (ntb_link_is_up(pp->ntb, NULL, NULL) == 1) {
  124. dev_dbg(&pp->ntb->dev, "link is up\n");
  125. pp_ping((unsigned long)pp);
  126. } else {
  127. dev_dbg(&pp->ntb->dev, "link is down\n");
  128. del_timer(&pp->db_timer);
  129. }
  130. }
  131. static void pp_db_event(void *ctx, int vec)
  132. {
  133. struct pp_ctx *pp = ctx;
  134. u64 db_bits, db_mask;
  135. unsigned long irqflags;
  136. spin_lock_irqsave(&pp->db_lock, irqflags);
  137. {
  138. db_mask = ntb_db_vector_mask(pp->ntb, vec);
  139. db_bits = db_mask & ntb_db_read(pp->ntb);
  140. ntb_db_set_mask(pp->ntb, db_mask);
  141. ntb_db_clear(pp->ntb, db_bits);
  142. pp->db_bits |= db_bits;
  143. mod_timer(&pp->db_timer, jiffies + pp->db_delay);
  144. dev_dbg(&pp->ntb->dev,
  145. "Pong vec %d bits %#llx\n",
  146. vec, db_bits);
  147. }
  148. spin_unlock_irqrestore(&pp->db_lock, irqflags);
  149. }
  150. static const struct ntb_ctx_ops pp_ops = {
  151. .link_event = pp_link_event,
  152. .db_event = pp_db_event,
  153. };
  154. static int pp_probe(struct ntb_client *client,
  155. struct ntb_dev *ntb)
  156. {
  157. struct pp_ctx *pp;
  158. int rc;
  159. if (ntb_db_is_unsafe(ntb)) {
  160. dev_dbg(&ntb->dev, "doorbell is unsafe\n");
  161. if (!unsafe) {
  162. rc = -EINVAL;
  163. goto err_pp;
  164. }
  165. }
  166. if (ntb_spad_is_unsafe(ntb)) {
  167. dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
  168. if (!unsafe) {
  169. rc = -EINVAL;
  170. goto err_pp;
  171. }
  172. }
  173. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  174. if (!pp) {
  175. rc = -ENOMEM;
  176. goto err_pp;
  177. }
  178. pp->ntb = ntb;
  179. pp->db_bits = 0;
  180. spin_lock_init(&pp->db_lock);
  181. setup_timer(&pp->db_timer, pp_ping, (unsigned long)pp);
  182. pp->db_delay = msecs_to_jiffies(delay_ms);
  183. rc = ntb_set_ctx(ntb, pp, &pp_ops);
  184. if (rc)
  185. goto err_ctx;
  186. ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  187. ntb_link_event(ntb);
  188. return 0;
  189. err_ctx:
  190. kfree(pp);
  191. err_pp:
  192. return rc;
  193. }
  194. static void pp_remove(struct ntb_client *client,
  195. struct ntb_dev *ntb)
  196. {
  197. struct pp_ctx *pp = ntb->ctx;
  198. ntb_clear_ctx(ntb);
  199. del_timer_sync(&pp->db_timer);
  200. ntb_link_disable(ntb);
  201. kfree(pp);
  202. }
  203. static struct ntb_client pp_client = {
  204. .ops = {
  205. .probe = pp_probe,
  206. .remove = pp_remove,
  207. },
  208. };
  209. module_ntb_client(pp_client);