ntb_pingpong.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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) 2017 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) 2017 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 Pingpong Linux driver
  51. */
  52. /*
  53. * How to use this tool, by example.
  54. *
  55. * Assuming $DBG_DIR is something like:
  56. * '/sys/kernel/debug/ntb_perf/0000:00:03.0'
  57. * Suppose aside from local device there is at least one remote device
  58. * connected to NTB with index 0.
  59. *-----------------------------------------------------------------------------
  60. * Eg: install driver with specified delay between doorbell event and response
  61. *
  62. * root@self# insmod ntb_pingpong.ko delay_ms=1000
  63. *-----------------------------------------------------------------------------
  64. * Eg: get number of ping-pong cycles performed
  65. *
  66. * root@self# cat $DBG_DIR/count
  67. */
  68. #include <linux/init.h>
  69. #include <linux/kernel.h>
  70. #include <linux/module.h>
  71. #include <linux/device.h>
  72. #include <linux/bitops.h>
  73. #include <linux/pci.h>
  74. #include <linux/slab.h>
  75. #include <linux/hrtimer.h>
  76. #include <linux/debugfs.h>
  77. #include <linux/ntb.h>
  78. #define DRIVER_NAME "ntb_pingpong"
  79. #define DRIVER_VERSION "2.0"
  80. MODULE_LICENSE("Dual BSD/GPL");
  81. MODULE_VERSION(DRIVER_VERSION);
  82. MODULE_AUTHOR("Allen Hubbe <Allen.Hubbe@emc.com>");
  83. MODULE_DESCRIPTION("PCIe NTB Simple Pingpong Client");
  84. static unsigned int unsafe;
  85. module_param(unsafe, uint, 0644);
  86. MODULE_PARM_DESC(unsafe, "Run even though ntb operations may be unsafe");
  87. static unsigned int delay_ms = 1000;
  88. module_param(delay_ms, uint, 0644);
  89. MODULE_PARM_DESC(delay_ms, "Milliseconds to delay the response to peer");
  90. struct pp_ctx {
  91. struct ntb_dev *ntb;
  92. struct hrtimer timer;
  93. u64 in_db;
  94. u64 out_db;
  95. int out_pidx;
  96. u64 nmask;
  97. u64 pmask;
  98. atomic_t count;
  99. spinlock_t lock;
  100. struct dentry *dbgfs_dir;
  101. };
  102. #define to_pp_timer(__timer) \
  103. container_of(__timer, struct pp_ctx, timer)
  104. static struct dentry *pp_dbgfs_topdir;
  105. static int pp_find_next_peer(struct pp_ctx *pp)
  106. {
  107. u64 link, out_db;
  108. int pidx;
  109. link = ntb_link_is_up(pp->ntb, NULL, NULL);
  110. /* Find next available peer */
  111. if (link & pp->nmask) {
  112. pidx = __ffs64(link & pp->nmask);
  113. out_db = BIT_ULL(pidx + 1);
  114. } else if (link & pp->pmask) {
  115. pidx = __ffs64(link & pp->pmask);
  116. out_db = BIT_ULL(pidx);
  117. } else {
  118. return -ENODEV;
  119. }
  120. spin_lock(&pp->lock);
  121. pp->out_pidx = pidx;
  122. pp->out_db = out_db;
  123. spin_unlock(&pp->lock);
  124. return 0;
  125. }
  126. static void pp_setup(struct pp_ctx *pp)
  127. {
  128. int ret;
  129. ntb_db_set_mask(pp->ntb, pp->in_db);
  130. hrtimer_cancel(&pp->timer);
  131. ret = pp_find_next_peer(pp);
  132. if (ret == -ENODEV) {
  133. dev_dbg(&pp->ntb->dev, "Got no peers, so cancel\n");
  134. return;
  135. }
  136. dev_dbg(&pp->ntb->dev, "Ping-pong started with port %d, db %#llx\n",
  137. ntb_peer_port_number(pp->ntb, pp->out_pidx), pp->out_db);
  138. hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
  139. }
  140. static void pp_clear(struct pp_ctx *pp)
  141. {
  142. hrtimer_cancel(&pp->timer);
  143. ntb_db_set_mask(pp->ntb, pp->in_db);
  144. dev_dbg(&pp->ntb->dev, "Ping-pong cancelled\n");
  145. }
  146. static void pp_ping(struct pp_ctx *pp)
  147. {
  148. u32 count;
  149. count = atomic_read(&pp->count);
  150. spin_lock(&pp->lock);
  151. ntb_peer_spad_write(pp->ntb, pp->out_pidx, 0, count);
  152. ntb_peer_msg_write(pp->ntb, pp->out_pidx, 0, count);
  153. dev_dbg(&pp->ntb->dev, "Ping port %d spad %#x, msg %#x\n",
  154. ntb_peer_port_number(pp->ntb, pp->out_pidx), count, count);
  155. ntb_peer_db_set(pp->ntb, pp->out_db);
  156. ntb_db_clear_mask(pp->ntb, pp->in_db);
  157. spin_unlock(&pp->lock);
  158. }
  159. static void pp_pong(struct pp_ctx *pp)
  160. {
  161. u32 msg_data = -1, spad_data = -1;
  162. int pidx = 0;
  163. /* Read pong data */
  164. spad_data = ntb_spad_read(pp->ntb, 0);
  165. msg_data = ntb_msg_read(pp->ntb, &pidx, 0);
  166. ntb_msg_clear_sts(pp->ntb, -1);
  167. /*
  168. * Scratchpad and message data may differ, since message register can't
  169. * be rewritten unless status is cleared. Additionally either of them
  170. * might be unsupported
  171. */
  172. dev_dbg(&pp->ntb->dev, "Pong spad %#x, msg %#x (port %d)\n",
  173. spad_data, msg_data, ntb_peer_port_number(pp->ntb, pidx));
  174. atomic_inc(&pp->count);
  175. ntb_db_set_mask(pp->ntb, pp->in_db);
  176. ntb_db_clear(pp->ntb, pp->in_db);
  177. hrtimer_start(&pp->timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
  178. }
  179. static enum hrtimer_restart pp_timer_func(struct hrtimer *t)
  180. {
  181. struct pp_ctx *pp = to_pp_timer(t);
  182. pp_ping(pp);
  183. return HRTIMER_NORESTART;
  184. }
  185. static void pp_link_event(void *ctx)
  186. {
  187. struct pp_ctx *pp = ctx;
  188. pp_setup(pp);
  189. }
  190. static void pp_db_event(void *ctx, int vec)
  191. {
  192. struct pp_ctx *pp = ctx;
  193. pp_pong(pp);
  194. }
  195. static const struct ntb_ctx_ops pp_ops = {
  196. .link_event = pp_link_event,
  197. .db_event = pp_db_event
  198. };
  199. static int pp_check_ntb(struct ntb_dev *ntb)
  200. {
  201. u64 pmask;
  202. if (ntb_db_is_unsafe(ntb)) {
  203. dev_dbg(&ntb->dev, "Doorbell is unsafe\n");
  204. if (!unsafe)
  205. return -EINVAL;
  206. }
  207. if (ntb_spad_is_unsafe(ntb)) {
  208. dev_dbg(&ntb->dev, "Scratchpad is unsafe\n");
  209. if (!unsafe)
  210. return -EINVAL;
  211. }
  212. pmask = GENMASK_ULL(ntb_peer_port_count(ntb), 0);
  213. if ((ntb_db_valid_mask(ntb) & pmask) != pmask) {
  214. dev_err(&ntb->dev, "Unsupported DB configuration\n");
  215. return -EINVAL;
  216. }
  217. if (ntb_spad_count(ntb) < 1 && ntb_msg_count(ntb) < 1) {
  218. dev_err(&ntb->dev, "Scratchpads and messages unsupported\n");
  219. return -EINVAL;
  220. } else if (ntb_spad_count(ntb) < 1) {
  221. dev_dbg(&ntb->dev, "Scratchpads unsupported\n");
  222. } else if (ntb_msg_count(ntb) < 1) {
  223. dev_dbg(&ntb->dev, "Messages unsupported\n");
  224. }
  225. return 0;
  226. }
  227. static struct pp_ctx *pp_create_data(struct ntb_dev *ntb)
  228. {
  229. struct pp_ctx *pp;
  230. pp = devm_kzalloc(&ntb->dev, sizeof(*pp), GFP_KERNEL);
  231. if (!pp)
  232. return ERR_PTR(-ENOMEM);
  233. pp->ntb = ntb;
  234. atomic_set(&pp->count, 0);
  235. spin_lock_init(&pp->lock);
  236. hrtimer_init(&pp->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  237. pp->timer.function = pp_timer_func;
  238. return pp;
  239. }
  240. static void pp_init_flds(struct pp_ctx *pp)
  241. {
  242. int pidx, lport, pcnt;
  243. /* Find global port index */
  244. lport = ntb_port_number(pp->ntb);
  245. pcnt = ntb_peer_port_count(pp->ntb);
  246. for (pidx = 0; pidx < pcnt; pidx++) {
  247. if (lport < ntb_peer_port_number(pp->ntb, pidx))
  248. break;
  249. }
  250. pp->in_db = BIT_ULL(pidx);
  251. pp->pmask = GENMASK_ULL(pidx, 0) >> 1;
  252. pp->nmask = GENMASK_ULL(pcnt - 1, pidx);
  253. dev_dbg(&pp->ntb->dev, "Inbound db %#llx, prev %#llx, next %#llx\n",
  254. pp->in_db, pp->pmask, pp->nmask);
  255. }
  256. static int pp_mask_events(struct pp_ctx *pp)
  257. {
  258. u64 db_mask, msg_mask;
  259. int ret;
  260. db_mask = ntb_db_valid_mask(pp->ntb);
  261. ret = ntb_db_set_mask(pp->ntb, db_mask);
  262. if (ret)
  263. return ret;
  264. /* Skip message events masking if unsupported */
  265. if (ntb_msg_count(pp->ntb) < 1)
  266. return 0;
  267. msg_mask = ntb_msg_outbits(pp->ntb) | ntb_msg_inbits(pp->ntb);
  268. return ntb_msg_set_mask(pp->ntb, msg_mask);
  269. }
  270. static int pp_setup_ctx(struct pp_ctx *pp)
  271. {
  272. int ret;
  273. ret = ntb_set_ctx(pp->ntb, pp, &pp_ops);
  274. if (ret)
  275. return ret;
  276. ntb_link_enable(pp->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  277. /* Might be not necessary */
  278. ntb_link_event(pp->ntb);
  279. return 0;
  280. }
  281. static void pp_clear_ctx(struct pp_ctx *pp)
  282. {
  283. ntb_link_disable(pp->ntb);
  284. ntb_clear_ctx(pp->ntb);
  285. }
  286. static void pp_setup_dbgfs(struct pp_ctx *pp)
  287. {
  288. struct pci_dev *pdev = pp->ntb->pdev;
  289. void *ret;
  290. pp->dbgfs_dir = debugfs_create_dir(pci_name(pdev), pp_dbgfs_topdir);
  291. ret = debugfs_create_atomic_t("count", 0600, pp->dbgfs_dir, &pp->count);
  292. if (!ret)
  293. dev_warn(&pp->ntb->dev, "DebugFS unsupported\n");
  294. }
  295. static void pp_clear_dbgfs(struct pp_ctx *pp)
  296. {
  297. debugfs_remove_recursive(pp->dbgfs_dir);
  298. }
  299. static int pp_probe(struct ntb_client *client, struct ntb_dev *ntb)
  300. {
  301. struct pp_ctx *pp;
  302. int ret;
  303. ret = pp_check_ntb(ntb);
  304. if (ret)
  305. return ret;
  306. pp = pp_create_data(ntb);
  307. if (IS_ERR(pp))
  308. return PTR_ERR(pp);
  309. pp_init_flds(pp);
  310. ret = pp_mask_events(pp);
  311. if (ret)
  312. return ret;
  313. ret = pp_setup_ctx(pp);
  314. if (ret)
  315. return ret;
  316. pp_setup_dbgfs(pp);
  317. return 0;
  318. }
  319. static void pp_remove(struct ntb_client *client, struct ntb_dev *ntb)
  320. {
  321. struct pp_ctx *pp = ntb->ctx;
  322. pp_clear_dbgfs(pp);
  323. pp_clear_ctx(pp);
  324. pp_clear(pp);
  325. }
  326. static struct ntb_client pp_client = {
  327. .ops = {
  328. .probe = pp_probe,
  329. .remove = pp_remove
  330. }
  331. };
  332. static int __init pp_init(void)
  333. {
  334. int ret;
  335. if (debugfs_initialized())
  336. pp_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  337. ret = ntb_register_client(&pp_client);
  338. if (ret)
  339. debugfs_remove_recursive(pp_dbgfs_topdir);
  340. return ret;
  341. }
  342. module_init(pp_init);
  343. static void __exit pp_exit(void)
  344. {
  345. ntb_unregister_client(&pp_client);
  346. debugfs_remove_recursive(pp_dbgfs_topdir);
  347. }
  348. module_exit(pp_exit);