ntb_tool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 Debugging Tool Linux driver
  49. *
  50. * Contact Information:
  51. * Allen Hubbe <Allen.Hubbe@emc.com>
  52. */
  53. /*
  54. * How to use this tool, by example.
  55. *
  56. * Assuming $DBG_DIR is something like:
  57. * '/sys/kernel/debug/ntb_tool/0000:00:03.0'
  58. *
  59. * Eg: check if clearing the doorbell mask generates an interrupt.
  60. *
  61. * # Set the doorbell mask
  62. * root@self# echo 's 1' > $DBG_DIR/mask
  63. *
  64. * # Ring the doorbell from the peer
  65. * root@peer# echo 's 1' > $DBG_DIR/peer_db
  66. *
  67. * # Clear the doorbell mask
  68. * root@self# echo 'c 1' > $DBG_DIR/mask
  69. *
  70. * Observe debugging output in dmesg or your console. You should see a
  71. * doorbell event triggered by clearing the mask. If not, this may indicate an
  72. * issue with the hardware that needs to be worked around in the driver.
  73. *
  74. * Eg: read and write scratchpad registers
  75. *
  76. * root@peer# echo '0 0x01010101 1 0x7f7f7f7f' > $DBG_DIR/peer_spad
  77. *
  78. * root@self# cat $DBG_DIR/spad
  79. *
  80. * Observe that spad 0 and 1 have the values set by the peer.
  81. */
  82. #include <linux/init.h>
  83. #include <linux/kernel.h>
  84. #include <linux/module.h>
  85. #include <linux/debugfs.h>
  86. #include <linux/dma-mapping.h>
  87. #include <linux/pci.h>
  88. #include <linux/slab.h>
  89. #include <linux/ntb.h>
  90. #define DRIVER_NAME "ntb_tool"
  91. #define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
  92. #define DRIVER_LICENSE "Dual BSD/GPL"
  93. #define DRIVER_VERSION "1.0"
  94. #define DRIVER_RELDATE "22 April 2015"
  95. #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
  96. MODULE_LICENSE(DRIVER_LICENSE);
  97. MODULE_VERSION(DRIVER_VERSION);
  98. MODULE_AUTHOR(DRIVER_AUTHOR);
  99. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  100. static struct dentry *tool_dbgfs;
  101. struct tool_ctx {
  102. struct ntb_dev *ntb;
  103. struct dentry *dbgfs;
  104. };
  105. #define SPAD_FNAME_SIZE 0x10
  106. #define INT_PTR(x) ((void *)(unsigned long)x)
  107. #define PTR_INT(x) ((int)(unsigned long)x)
  108. #define TOOL_FOPS_RDWR(__name, __read, __write) \
  109. const struct file_operations __name = { \
  110. .owner = THIS_MODULE, \
  111. .open = simple_open, \
  112. .read = __read, \
  113. .write = __write, \
  114. }
  115. static void tool_link_event(void *ctx)
  116. {
  117. struct tool_ctx *tc = ctx;
  118. enum ntb_speed speed;
  119. enum ntb_width width;
  120. int up;
  121. up = ntb_link_is_up(tc->ntb, &speed, &width);
  122. dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
  123. up ? "up" : "down", speed, width);
  124. }
  125. static void tool_db_event(void *ctx, int vec)
  126. {
  127. struct tool_ctx *tc = ctx;
  128. u64 db_bits, db_mask;
  129. db_mask = ntb_db_vector_mask(tc->ntb, vec);
  130. db_bits = ntb_db_read(tc->ntb);
  131. dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
  132. vec, db_mask, db_bits);
  133. }
  134. static const struct ntb_ctx_ops tool_ops = {
  135. .link_event = tool_link_event,
  136. .db_event = tool_db_event,
  137. };
  138. static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf,
  139. size_t size, loff_t *offp,
  140. u64 (*db_read_fn)(struct ntb_dev *))
  141. {
  142. size_t buf_size;
  143. char *buf;
  144. ssize_t pos, rc;
  145. if (!db_read_fn)
  146. return -EINVAL;
  147. buf_size = min_t(size_t, size, 0x20);
  148. buf = kmalloc(buf_size, GFP_KERNEL);
  149. if (!buf)
  150. return -ENOMEM;
  151. pos = scnprintf(buf, buf_size, "%#llx\n",
  152. db_read_fn(tc->ntb));
  153. rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
  154. kfree(buf);
  155. return rc;
  156. }
  157. static ssize_t tool_dbfn_write(struct tool_ctx *tc,
  158. const char __user *ubuf,
  159. size_t size, loff_t *offp,
  160. int (*db_set_fn)(struct ntb_dev *, u64),
  161. int (*db_clear_fn)(struct ntb_dev *, u64))
  162. {
  163. u64 db_bits;
  164. char *buf, cmd;
  165. ssize_t rc;
  166. int n;
  167. buf = kmalloc(size + 1, GFP_KERNEL);
  168. if (!buf)
  169. return -ENOMEM;
  170. rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
  171. if (rc < 0) {
  172. kfree(buf);
  173. return rc;
  174. }
  175. buf[size] = 0;
  176. n = sscanf(buf, "%c %lli", &cmd, &db_bits);
  177. kfree(buf);
  178. if (n != 2) {
  179. rc = -EINVAL;
  180. } else if (cmd == 's') {
  181. if (!db_set_fn)
  182. rc = -EINVAL;
  183. else
  184. rc = db_set_fn(tc->ntb, db_bits);
  185. } else if (cmd == 'c') {
  186. if (!db_clear_fn)
  187. rc = -EINVAL;
  188. else
  189. rc = db_clear_fn(tc->ntb, db_bits);
  190. } else {
  191. rc = -EINVAL;
  192. }
  193. return rc ? : size;
  194. }
  195. static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf,
  196. size_t size, loff_t *offp,
  197. u32 (*spad_read_fn)(struct ntb_dev *, int))
  198. {
  199. size_t buf_size;
  200. char *buf;
  201. ssize_t pos, rc;
  202. int i, spad_count;
  203. if (!spad_read_fn)
  204. return -EINVAL;
  205. buf_size = min_t(size_t, size, 0x100);
  206. buf = kmalloc(buf_size, GFP_KERNEL);
  207. if (!buf)
  208. return -ENOMEM;
  209. pos = 0;
  210. spad_count = ntb_spad_count(tc->ntb);
  211. for (i = 0; i < spad_count; ++i) {
  212. pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
  213. i, spad_read_fn(tc->ntb, i));
  214. }
  215. rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
  216. kfree(buf);
  217. return rc;
  218. }
  219. static ssize_t tool_spadfn_write(struct tool_ctx *tc,
  220. const char __user *ubuf,
  221. size_t size, loff_t *offp,
  222. int (*spad_write_fn)(struct ntb_dev *,
  223. int, u32))
  224. {
  225. int spad_idx;
  226. u32 spad_val;
  227. char *buf;
  228. int pos, n;
  229. ssize_t rc;
  230. if (!spad_write_fn) {
  231. dev_dbg(&tc->ntb->dev, "no spad write fn\n");
  232. return -EINVAL;
  233. }
  234. buf = kmalloc(size + 1, GFP_KERNEL);
  235. if (!buf)
  236. return -ENOMEM;
  237. rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
  238. if (rc < 0) {
  239. kfree(buf);
  240. return rc;
  241. }
  242. buf[size] = 0;
  243. n = sscanf(buf, "%d %i%n", &spad_idx, &spad_val, &pos);
  244. while (n == 2) {
  245. rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
  246. if (rc)
  247. break;
  248. n = sscanf(buf + pos, "%d %i%n", &spad_idx, &spad_val, &pos);
  249. }
  250. if (n < 0)
  251. rc = n;
  252. kfree(buf);
  253. return rc ? : size;
  254. }
  255. static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
  256. size_t size, loff_t *offp)
  257. {
  258. struct tool_ctx *tc = filep->private_data;
  259. return tool_dbfn_read(tc, ubuf, size, offp,
  260. tc->ntb->ops->db_read);
  261. }
  262. static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
  263. size_t size, loff_t *offp)
  264. {
  265. struct tool_ctx *tc = filep->private_data;
  266. return tool_dbfn_write(tc, ubuf, size, offp,
  267. tc->ntb->ops->db_set,
  268. tc->ntb->ops->db_clear);
  269. }
  270. static TOOL_FOPS_RDWR(tool_db_fops,
  271. tool_db_read,
  272. tool_db_write);
  273. static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
  274. size_t size, loff_t *offp)
  275. {
  276. struct tool_ctx *tc = filep->private_data;
  277. return tool_dbfn_read(tc, ubuf, size, offp,
  278. tc->ntb->ops->db_read_mask);
  279. }
  280. static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
  281. size_t size, loff_t *offp)
  282. {
  283. struct tool_ctx *tc = filep->private_data;
  284. return tool_dbfn_write(tc, ubuf, size, offp,
  285. tc->ntb->ops->db_set_mask,
  286. tc->ntb->ops->db_clear_mask);
  287. }
  288. static TOOL_FOPS_RDWR(tool_mask_fops,
  289. tool_mask_read,
  290. tool_mask_write);
  291. static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
  292. size_t size, loff_t *offp)
  293. {
  294. struct tool_ctx *tc = filep->private_data;
  295. return tool_dbfn_read(tc, ubuf, size, offp,
  296. tc->ntb->ops->peer_db_read);
  297. }
  298. static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
  299. size_t size, loff_t *offp)
  300. {
  301. struct tool_ctx *tc = filep->private_data;
  302. return tool_dbfn_write(tc, ubuf, size, offp,
  303. tc->ntb->ops->peer_db_set,
  304. tc->ntb->ops->peer_db_clear);
  305. }
  306. static TOOL_FOPS_RDWR(tool_peer_db_fops,
  307. tool_peer_db_read,
  308. tool_peer_db_write);
  309. static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
  310. size_t size, loff_t *offp)
  311. {
  312. struct tool_ctx *tc = filep->private_data;
  313. return tool_dbfn_read(tc, ubuf, size, offp,
  314. tc->ntb->ops->peer_db_read_mask);
  315. }
  316. static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
  317. size_t size, loff_t *offp)
  318. {
  319. struct tool_ctx *tc = filep->private_data;
  320. return tool_dbfn_write(tc, ubuf, size, offp,
  321. tc->ntb->ops->peer_db_set_mask,
  322. tc->ntb->ops->peer_db_clear_mask);
  323. }
  324. static TOOL_FOPS_RDWR(tool_peer_mask_fops,
  325. tool_peer_mask_read,
  326. tool_peer_mask_write);
  327. static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
  328. size_t size, loff_t *offp)
  329. {
  330. struct tool_ctx *tc = filep->private_data;
  331. return tool_spadfn_read(tc, ubuf, size, offp,
  332. tc->ntb->ops->spad_read);
  333. }
  334. static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
  335. size_t size, loff_t *offp)
  336. {
  337. struct tool_ctx *tc = filep->private_data;
  338. return tool_spadfn_write(tc, ubuf, size, offp,
  339. tc->ntb->ops->spad_write);
  340. }
  341. static TOOL_FOPS_RDWR(tool_spad_fops,
  342. tool_spad_read,
  343. tool_spad_write);
  344. static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
  345. size_t size, loff_t *offp)
  346. {
  347. struct tool_ctx *tc = filep->private_data;
  348. return tool_spadfn_read(tc, ubuf, size, offp,
  349. tc->ntb->ops->peer_spad_read);
  350. }
  351. static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
  352. size_t size, loff_t *offp)
  353. {
  354. struct tool_ctx *tc = filep->private_data;
  355. return tool_spadfn_write(tc, ubuf, size, offp,
  356. tc->ntb->ops->peer_spad_write);
  357. }
  358. static TOOL_FOPS_RDWR(tool_peer_spad_fops,
  359. tool_peer_spad_read,
  360. tool_peer_spad_write);
  361. static void tool_setup_dbgfs(struct tool_ctx *tc)
  362. {
  363. /* This modules is useless without dbgfs... */
  364. if (!tool_dbgfs) {
  365. tc->dbgfs = NULL;
  366. return;
  367. }
  368. tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
  369. tool_dbgfs);
  370. if (!tc->dbgfs)
  371. return;
  372. debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
  373. tc, &tool_db_fops);
  374. debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
  375. tc, &tool_mask_fops);
  376. debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
  377. tc, &tool_peer_db_fops);
  378. debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
  379. tc, &tool_peer_mask_fops);
  380. debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
  381. tc, &tool_spad_fops);
  382. debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
  383. tc, &tool_peer_spad_fops);
  384. }
  385. static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
  386. {
  387. struct tool_ctx *tc;
  388. int rc;
  389. if (ntb_db_is_unsafe(ntb))
  390. dev_dbg(&ntb->dev, "doorbell is unsafe\n");
  391. if (ntb_spad_is_unsafe(ntb))
  392. dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
  393. tc = kmalloc(sizeof(*tc), GFP_KERNEL);
  394. if (!tc) {
  395. rc = -ENOMEM;
  396. goto err_tc;
  397. }
  398. tc->ntb = ntb;
  399. tool_setup_dbgfs(tc);
  400. rc = ntb_set_ctx(ntb, tc, &tool_ops);
  401. if (rc)
  402. goto err_ctx;
  403. ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  404. ntb_link_event(ntb);
  405. return 0;
  406. err_ctx:
  407. debugfs_remove_recursive(tc->dbgfs);
  408. kfree(tc);
  409. err_tc:
  410. return rc;
  411. }
  412. static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
  413. {
  414. struct tool_ctx *tc = ntb->ctx;
  415. ntb_clear_ctx(ntb);
  416. ntb_link_disable(ntb);
  417. debugfs_remove_recursive(tc->dbgfs);
  418. kfree(tc);
  419. }
  420. static struct ntb_client tool_client = {
  421. .ops = {
  422. .probe = tool_probe,
  423. .remove = tool_remove,
  424. },
  425. };
  426. static int __init tool_init(void)
  427. {
  428. int rc;
  429. if (debugfs_initialized())
  430. tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
  431. rc = ntb_register_client(&tool_client);
  432. if (rc)
  433. goto err_client;
  434. return 0;
  435. err_client:
  436. debugfs_remove_recursive(tool_dbgfs);
  437. return rc;
  438. }
  439. module_init(tool_init);
  440. static void __exit tool_exit(void)
  441. {
  442. ntb_unregister_client(&tool_client);
  443. debugfs_remove_recursive(tool_dbgfs);
  444. }
  445. module_exit(tool_exit);