ntb_tool.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. * # Check the link status
  62. * root@self# cat $DBG_DIR/link
  63. *
  64. * # Block until the link is up
  65. * root@self# echo Y > $DBG_DIR/link_event
  66. *
  67. * # Set the doorbell mask
  68. * root@self# echo 's 1' > $DBG_DIR/mask
  69. *
  70. * # Ring the doorbell from the peer
  71. * root@peer# echo 's 1' > $DBG_DIR/peer_db
  72. *
  73. * # Clear the doorbell mask
  74. * root@self# echo 'c 1' > $DBG_DIR/mask
  75. *
  76. * Observe debugging output in dmesg or your console. You should see a
  77. * doorbell event triggered by clearing the mask. If not, this may indicate an
  78. * issue with the hardware that needs to be worked around in the driver.
  79. *
  80. * Eg: read and write scratchpad registers
  81. *
  82. * root@peer# echo '0 0x01010101 1 0x7f7f7f7f' > $DBG_DIR/peer_spad
  83. *
  84. * root@self# cat $DBG_DIR/spad
  85. *
  86. * Observe that spad 0 and 1 have the values set by the peer.
  87. *
  88. * # Check the memory window translation info
  89. * cat $DBG_DIR/peer_trans0
  90. *
  91. * # Setup a 16k memory window buffer
  92. * echo 16384 > $DBG_DIR/peer_trans0
  93. *
  94. */
  95. #include <linux/init.h>
  96. #include <linux/kernel.h>
  97. #include <linux/module.h>
  98. #include <linux/debugfs.h>
  99. #include <linux/dma-mapping.h>
  100. #include <linux/pci.h>
  101. #include <linux/slab.h>
  102. #include <linux/uaccess.h>
  103. #include <linux/ntb.h>
  104. #define DRIVER_NAME "ntb_tool"
  105. #define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
  106. #define DRIVER_LICENSE "Dual BSD/GPL"
  107. #define DRIVER_VERSION "1.0"
  108. #define DRIVER_RELDATE "22 April 2015"
  109. #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
  110. MODULE_LICENSE(DRIVER_LICENSE);
  111. MODULE_VERSION(DRIVER_VERSION);
  112. MODULE_AUTHOR(DRIVER_AUTHOR);
  113. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  114. /* It is rare to have hadrware with greater than six MWs */
  115. #define MAX_MWS 6
  116. /* Only two-ports devices are supported */
  117. #define PIDX NTB_DEF_PEER_IDX
  118. static struct dentry *tool_dbgfs;
  119. struct tool_mw {
  120. int idx;
  121. struct tool_ctx *tc;
  122. resource_size_t win_size;
  123. resource_size_t size;
  124. u8 __iomem *local;
  125. u8 *peer;
  126. dma_addr_t peer_dma;
  127. struct dentry *peer_dbg_file;
  128. };
  129. struct tool_ctx {
  130. struct ntb_dev *ntb;
  131. struct dentry *dbgfs;
  132. wait_queue_head_t link_wq;
  133. int mw_count;
  134. struct tool_mw mws[MAX_MWS];
  135. };
  136. #define SPAD_FNAME_SIZE 0x10
  137. #define INT_PTR(x) ((void *)(unsigned long)x)
  138. #define PTR_INT(x) ((int)(unsigned long)x)
  139. #define TOOL_FOPS_RDWR(__name, __read, __write) \
  140. const struct file_operations __name = { \
  141. .owner = THIS_MODULE, \
  142. .open = simple_open, \
  143. .read = __read, \
  144. .write = __write, \
  145. }
  146. static void tool_link_event(void *ctx)
  147. {
  148. struct tool_ctx *tc = ctx;
  149. enum ntb_speed speed;
  150. enum ntb_width width;
  151. int up;
  152. up = ntb_link_is_up(tc->ntb, &speed, &width);
  153. dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
  154. up ? "up" : "down", speed, width);
  155. wake_up(&tc->link_wq);
  156. }
  157. static void tool_db_event(void *ctx, int vec)
  158. {
  159. struct tool_ctx *tc = ctx;
  160. u64 db_bits, db_mask;
  161. db_mask = ntb_db_vector_mask(tc->ntb, vec);
  162. db_bits = ntb_db_read(tc->ntb);
  163. dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
  164. vec, db_mask, db_bits);
  165. }
  166. static const struct ntb_ctx_ops tool_ops = {
  167. .link_event = tool_link_event,
  168. .db_event = tool_db_event,
  169. };
  170. static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf,
  171. size_t size, loff_t *offp,
  172. u64 (*db_read_fn)(struct ntb_dev *))
  173. {
  174. size_t buf_size;
  175. char *buf;
  176. ssize_t pos, rc;
  177. if (!db_read_fn)
  178. return -EINVAL;
  179. buf_size = min_t(size_t, size, 0x20);
  180. buf = kmalloc(buf_size, GFP_KERNEL);
  181. if (!buf)
  182. return -ENOMEM;
  183. pos = scnprintf(buf, buf_size, "%#llx\n",
  184. db_read_fn(tc->ntb));
  185. rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
  186. kfree(buf);
  187. return rc;
  188. }
  189. static ssize_t tool_dbfn_write(struct tool_ctx *tc,
  190. const char __user *ubuf,
  191. size_t size, loff_t *offp,
  192. int (*db_set_fn)(struct ntb_dev *, u64),
  193. int (*db_clear_fn)(struct ntb_dev *, u64))
  194. {
  195. u64 db_bits;
  196. char *buf, cmd;
  197. ssize_t rc;
  198. int n;
  199. buf = kmalloc(size + 1, GFP_KERNEL);
  200. if (!buf)
  201. return -ENOMEM;
  202. rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
  203. if (rc < 0) {
  204. kfree(buf);
  205. return rc;
  206. }
  207. buf[size] = 0;
  208. n = sscanf(buf, "%c %lli", &cmd, &db_bits);
  209. kfree(buf);
  210. if (n != 2) {
  211. rc = -EINVAL;
  212. } else if (cmd == 's') {
  213. if (!db_set_fn)
  214. rc = -EINVAL;
  215. else
  216. rc = db_set_fn(tc->ntb, db_bits);
  217. } else if (cmd == 'c') {
  218. if (!db_clear_fn)
  219. rc = -EINVAL;
  220. else
  221. rc = db_clear_fn(tc->ntb, db_bits);
  222. } else {
  223. rc = -EINVAL;
  224. }
  225. return rc ? : size;
  226. }
  227. static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf,
  228. size_t size, loff_t *offp,
  229. u32 (*spad_read_fn)(struct ntb_dev *, int))
  230. {
  231. size_t buf_size;
  232. char *buf;
  233. ssize_t pos, rc;
  234. int i, spad_count;
  235. if (!spad_read_fn)
  236. return -EINVAL;
  237. spad_count = ntb_spad_count(tc->ntb);
  238. /*
  239. * We multiply the number of spads by 15 to get the buffer size
  240. * this is from 3 for the %d, 10 for the largest hex value
  241. * (0x00000000) and 2 for the tab and line feed.
  242. */
  243. buf_size = min_t(size_t, size, spad_count * 15);
  244. buf = kmalloc(buf_size, GFP_KERNEL);
  245. if (!buf)
  246. return -ENOMEM;
  247. pos = 0;
  248. for (i = 0; i < spad_count; ++i) {
  249. pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
  250. i, spad_read_fn(tc->ntb, i));
  251. }
  252. rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
  253. kfree(buf);
  254. return rc;
  255. }
  256. static ssize_t tool_spadfn_write(struct tool_ctx *tc,
  257. const char __user *ubuf,
  258. size_t size, loff_t *offp,
  259. int (*spad_write_fn)(struct ntb_dev *,
  260. int, u32))
  261. {
  262. int spad_idx;
  263. u32 spad_val;
  264. char *buf, *buf_ptr;
  265. int pos, n;
  266. ssize_t rc;
  267. if (!spad_write_fn) {
  268. dev_dbg(&tc->ntb->dev, "no spad write fn\n");
  269. return -EINVAL;
  270. }
  271. buf = kmalloc(size + 1, GFP_KERNEL);
  272. if (!buf)
  273. return -ENOMEM;
  274. rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
  275. if (rc < 0) {
  276. kfree(buf);
  277. return rc;
  278. }
  279. buf[size] = 0;
  280. buf_ptr = buf;
  281. n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
  282. while (n == 2) {
  283. buf_ptr += pos;
  284. rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
  285. if (rc)
  286. break;
  287. n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
  288. }
  289. if (n < 0)
  290. rc = n;
  291. kfree(buf);
  292. return rc ? : size;
  293. }
  294. static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
  295. size_t size, loff_t *offp)
  296. {
  297. struct tool_ctx *tc = filep->private_data;
  298. return tool_dbfn_read(tc, ubuf, size, offp,
  299. tc->ntb->ops->db_read);
  300. }
  301. static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
  302. size_t size, loff_t *offp)
  303. {
  304. struct tool_ctx *tc = filep->private_data;
  305. return tool_dbfn_write(tc, ubuf, size, offp,
  306. tc->ntb->ops->db_set,
  307. tc->ntb->ops->db_clear);
  308. }
  309. static TOOL_FOPS_RDWR(tool_db_fops,
  310. tool_db_read,
  311. tool_db_write);
  312. static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
  313. size_t size, loff_t *offp)
  314. {
  315. struct tool_ctx *tc = filep->private_data;
  316. return tool_dbfn_read(tc, ubuf, size, offp,
  317. tc->ntb->ops->db_read_mask);
  318. }
  319. static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
  320. size_t size, loff_t *offp)
  321. {
  322. struct tool_ctx *tc = filep->private_data;
  323. return tool_dbfn_write(tc, ubuf, size, offp,
  324. tc->ntb->ops->db_set_mask,
  325. tc->ntb->ops->db_clear_mask);
  326. }
  327. static TOOL_FOPS_RDWR(tool_mask_fops,
  328. tool_mask_read,
  329. tool_mask_write);
  330. static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
  331. size_t size, loff_t *offp)
  332. {
  333. struct tool_ctx *tc = filep->private_data;
  334. return tool_dbfn_read(tc, ubuf, size, offp,
  335. tc->ntb->ops->peer_db_read);
  336. }
  337. static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
  338. size_t size, loff_t *offp)
  339. {
  340. struct tool_ctx *tc = filep->private_data;
  341. return tool_dbfn_write(tc, ubuf, size, offp,
  342. tc->ntb->ops->peer_db_set,
  343. tc->ntb->ops->peer_db_clear);
  344. }
  345. static TOOL_FOPS_RDWR(tool_peer_db_fops,
  346. tool_peer_db_read,
  347. tool_peer_db_write);
  348. static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
  349. size_t size, loff_t *offp)
  350. {
  351. struct tool_ctx *tc = filep->private_data;
  352. return tool_dbfn_read(tc, ubuf, size, offp,
  353. tc->ntb->ops->peer_db_read_mask);
  354. }
  355. static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
  356. size_t size, loff_t *offp)
  357. {
  358. struct tool_ctx *tc = filep->private_data;
  359. return tool_dbfn_write(tc, ubuf, size, offp,
  360. tc->ntb->ops->peer_db_set_mask,
  361. tc->ntb->ops->peer_db_clear_mask);
  362. }
  363. static TOOL_FOPS_RDWR(tool_peer_mask_fops,
  364. tool_peer_mask_read,
  365. tool_peer_mask_write);
  366. static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
  367. size_t size, loff_t *offp)
  368. {
  369. struct tool_ctx *tc = filep->private_data;
  370. return tool_spadfn_read(tc, ubuf, size, offp,
  371. tc->ntb->ops->spad_read);
  372. }
  373. static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
  374. size_t size, loff_t *offp)
  375. {
  376. struct tool_ctx *tc = filep->private_data;
  377. return tool_spadfn_write(tc, ubuf, size, offp,
  378. tc->ntb->ops->spad_write);
  379. }
  380. static TOOL_FOPS_RDWR(tool_spad_fops,
  381. tool_spad_read,
  382. tool_spad_write);
  383. static u32 ntb_tool_peer_spad_read(struct ntb_dev *ntb, int sidx)
  384. {
  385. return ntb_peer_spad_read(ntb, PIDX, sidx);
  386. }
  387. static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
  388. size_t size, loff_t *offp)
  389. {
  390. struct tool_ctx *tc = filep->private_data;
  391. return tool_spadfn_read(tc, ubuf, size, offp, ntb_tool_peer_spad_read);
  392. }
  393. static int ntb_tool_peer_spad_write(struct ntb_dev *ntb, int sidx, u32 val)
  394. {
  395. return ntb_peer_spad_write(ntb, PIDX, sidx, val);
  396. }
  397. static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
  398. size_t size, loff_t *offp)
  399. {
  400. struct tool_ctx *tc = filep->private_data;
  401. return tool_spadfn_write(tc, ubuf, size, offp,
  402. ntb_tool_peer_spad_write);
  403. }
  404. static TOOL_FOPS_RDWR(tool_peer_spad_fops,
  405. tool_peer_spad_read,
  406. tool_peer_spad_write);
  407. static ssize_t tool_link_read(struct file *filep, char __user *ubuf,
  408. size_t size, loff_t *offp)
  409. {
  410. struct tool_ctx *tc = filep->private_data;
  411. char buf[3];
  412. buf[0] = ntb_link_is_up(tc->ntb, NULL, NULL) ? 'Y' : 'N';
  413. buf[1] = '\n';
  414. buf[2] = '\0';
  415. return simple_read_from_buffer(ubuf, size, offp, buf, 2);
  416. }
  417. static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
  418. size_t size, loff_t *offp)
  419. {
  420. struct tool_ctx *tc = filep->private_data;
  421. char buf[32];
  422. size_t buf_size;
  423. bool val;
  424. int rc;
  425. buf_size = min(size, (sizeof(buf) - 1));
  426. if (copy_from_user(buf, ubuf, buf_size))
  427. return -EFAULT;
  428. buf[buf_size] = '\0';
  429. rc = strtobool(buf, &val);
  430. if (rc)
  431. return rc;
  432. if (val)
  433. rc = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  434. else
  435. rc = ntb_link_disable(tc->ntb);
  436. if (rc)
  437. return rc;
  438. return size;
  439. }
  440. static TOOL_FOPS_RDWR(tool_link_fops,
  441. tool_link_read,
  442. tool_link_write);
  443. static ssize_t tool_link_event_write(struct file *filep,
  444. const char __user *ubuf,
  445. size_t size, loff_t *offp)
  446. {
  447. struct tool_ctx *tc = filep->private_data;
  448. char buf[32];
  449. size_t buf_size;
  450. bool val;
  451. int rc;
  452. buf_size = min(size, (sizeof(buf) - 1));
  453. if (copy_from_user(buf, ubuf, buf_size))
  454. return -EFAULT;
  455. buf[buf_size] = '\0';
  456. rc = strtobool(buf, &val);
  457. if (rc)
  458. return rc;
  459. if (wait_event_interruptible(tc->link_wq,
  460. ntb_link_is_up(tc->ntb, NULL, NULL) == val))
  461. return -ERESTART;
  462. return size;
  463. }
  464. static TOOL_FOPS_RDWR(tool_link_event_fops,
  465. NULL,
  466. tool_link_event_write);
  467. static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
  468. size_t size, loff_t *offp)
  469. {
  470. struct tool_mw *mw = filep->private_data;
  471. ssize_t rc;
  472. loff_t pos = *offp;
  473. void *buf;
  474. if (mw->local == NULL)
  475. return -EIO;
  476. if (pos < 0)
  477. return -EINVAL;
  478. if (pos >= mw->win_size || !size)
  479. return 0;
  480. if (size > mw->win_size - pos)
  481. size = mw->win_size - pos;
  482. buf = kmalloc(size, GFP_KERNEL);
  483. if (!buf)
  484. return -ENOMEM;
  485. memcpy_fromio(buf, mw->local + pos, size);
  486. rc = copy_to_user(ubuf, buf, size);
  487. if (rc == size) {
  488. rc = -EFAULT;
  489. goto err_free;
  490. }
  491. size -= rc;
  492. *offp = pos + size;
  493. rc = size;
  494. err_free:
  495. kfree(buf);
  496. return rc;
  497. }
  498. static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
  499. size_t size, loff_t *offp)
  500. {
  501. struct tool_mw *mw = filep->private_data;
  502. ssize_t rc;
  503. loff_t pos = *offp;
  504. void *buf;
  505. if (pos < 0)
  506. return -EINVAL;
  507. if (pos >= mw->win_size || !size)
  508. return 0;
  509. if (size > mw->win_size - pos)
  510. size = mw->win_size - pos;
  511. buf = kmalloc(size, GFP_KERNEL);
  512. if (!buf)
  513. return -ENOMEM;
  514. rc = copy_from_user(buf, ubuf, size);
  515. if (rc == size) {
  516. rc = -EFAULT;
  517. goto err_free;
  518. }
  519. size -= rc;
  520. *offp = pos + size;
  521. rc = size;
  522. memcpy_toio(mw->local + pos, buf, size);
  523. err_free:
  524. kfree(buf);
  525. return rc;
  526. }
  527. static TOOL_FOPS_RDWR(tool_mw_fops,
  528. tool_mw_read,
  529. tool_mw_write);
  530. static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
  531. size_t size, loff_t *offp)
  532. {
  533. struct tool_mw *mw = filep->private_data;
  534. if (!mw->peer)
  535. return -ENXIO;
  536. return simple_read_from_buffer(ubuf, size, offp, mw->peer, mw->size);
  537. }
  538. static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
  539. size_t size, loff_t *offp)
  540. {
  541. struct tool_mw *mw = filep->private_data;
  542. if (!mw->peer)
  543. return -ENXIO;
  544. return simple_write_to_buffer(mw->peer, mw->size, offp, ubuf, size);
  545. }
  546. static TOOL_FOPS_RDWR(tool_peer_mw_fops,
  547. tool_peer_mw_read,
  548. tool_peer_mw_write);
  549. static int tool_setup_mw(struct tool_ctx *tc, int idx, size_t req_size)
  550. {
  551. int rc;
  552. struct tool_mw *mw = &tc->mws[idx];
  553. resource_size_t size, align_addr, align_size;
  554. char buf[16];
  555. if (mw->peer)
  556. return 0;
  557. rc = ntb_mw_get_align(tc->ntb, PIDX, idx, &align_addr,
  558. &align_size, &size);
  559. if (rc)
  560. return rc;
  561. mw->size = min_t(resource_size_t, req_size, size);
  562. mw->size = round_up(mw->size, align_addr);
  563. mw->size = round_up(mw->size, align_size);
  564. mw->peer = dma_alloc_coherent(&tc->ntb->pdev->dev, mw->size,
  565. &mw->peer_dma, GFP_KERNEL);
  566. if (!mw->peer || !IS_ALIGNED(mw->peer_dma, align_addr))
  567. return -ENOMEM;
  568. rc = ntb_mw_set_trans(tc->ntb, PIDX, idx, mw->peer_dma, mw->size);
  569. if (rc)
  570. goto err_free_dma;
  571. snprintf(buf, sizeof(buf), "peer_mw%d", idx);
  572. mw->peer_dbg_file = debugfs_create_file(buf, S_IRUSR | S_IWUSR,
  573. mw->tc->dbgfs, mw,
  574. &tool_peer_mw_fops);
  575. return 0;
  576. err_free_dma:
  577. dma_free_coherent(&tc->ntb->pdev->dev, mw->size,
  578. mw->peer,
  579. mw->peer_dma);
  580. mw->peer = NULL;
  581. mw->peer_dma = 0;
  582. mw->size = 0;
  583. return rc;
  584. }
  585. static void tool_free_mw(struct tool_ctx *tc, int idx)
  586. {
  587. struct tool_mw *mw = &tc->mws[idx];
  588. if (mw->peer) {
  589. ntb_mw_clear_trans(tc->ntb, PIDX, idx);
  590. dma_free_coherent(&tc->ntb->pdev->dev, mw->size,
  591. mw->peer,
  592. mw->peer_dma);
  593. }
  594. mw->peer = NULL;
  595. mw->peer_dma = 0;
  596. debugfs_remove(mw->peer_dbg_file);
  597. mw->peer_dbg_file = NULL;
  598. }
  599. static ssize_t tool_peer_mw_trans_read(struct file *filep,
  600. char __user *ubuf,
  601. size_t size, loff_t *offp)
  602. {
  603. struct tool_mw *mw = filep->private_data;
  604. char *buf;
  605. size_t buf_size;
  606. ssize_t ret, off = 0;
  607. phys_addr_t base;
  608. resource_size_t mw_size;
  609. resource_size_t align_addr = 0;
  610. resource_size_t align_size = 0;
  611. resource_size_t max_size = 0;
  612. buf_size = min_t(size_t, size, 512);
  613. buf = kmalloc(buf_size, GFP_KERNEL);
  614. if (!buf)
  615. return -ENOMEM;
  616. ntb_mw_get_align(mw->tc->ntb, PIDX, mw->idx,
  617. &align_addr, &align_size, &max_size);
  618. ntb_peer_mw_get_addr(mw->tc->ntb, mw->idx, &base, &mw_size);
  619. off += scnprintf(buf + off, buf_size - off,
  620. "Peer MW %d Information:\n", mw->idx);
  621. off += scnprintf(buf + off, buf_size - off,
  622. "Physical Address \t%pa[p]\n",
  623. &base);
  624. off += scnprintf(buf + off, buf_size - off,
  625. "Window Size \t%lld\n",
  626. (unsigned long long)mw_size);
  627. off += scnprintf(buf + off, buf_size - off,
  628. "Alignment \t%lld\n",
  629. (unsigned long long)align_addr);
  630. off += scnprintf(buf + off, buf_size - off,
  631. "Size Alignment \t%lld\n",
  632. (unsigned long long)align_size);
  633. off += scnprintf(buf + off, buf_size - off,
  634. "Size Max \t%lld\n",
  635. (unsigned long long)max_size);
  636. off += scnprintf(buf + off, buf_size - off,
  637. "Ready \t%c\n",
  638. (mw->peer) ? 'Y' : 'N');
  639. off += scnprintf(buf + off, buf_size - off,
  640. "Allocated Size \t%zd\n",
  641. (mw->peer) ? (size_t)mw->size : 0);
  642. ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
  643. kfree(buf);
  644. return ret;
  645. }
  646. static ssize_t tool_peer_mw_trans_write(struct file *filep,
  647. const char __user *ubuf,
  648. size_t size, loff_t *offp)
  649. {
  650. struct tool_mw *mw = filep->private_data;
  651. char buf[32];
  652. size_t buf_size;
  653. unsigned long long val;
  654. int rc;
  655. buf_size = min(size, (sizeof(buf) - 1));
  656. if (copy_from_user(buf, ubuf, buf_size))
  657. return -EFAULT;
  658. buf[buf_size] = '\0';
  659. rc = kstrtoull(buf, 0, &val);
  660. if (rc)
  661. return rc;
  662. tool_free_mw(mw->tc, mw->idx);
  663. if (val)
  664. rc = tool_setup_mw(mw->tc, mw->idx, val);
  665. if (rc)
  666. return rc;
  667. return size;
  668. }
  669. static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
  670. tool_peer_mw_trans_read,
  671. tool_peer_mw_trans_write);
  672. static int tool_init_mw(struct tool_ctx *tc, int idx)
  673. {
  674. struct tool_mw *mw = &tc->mws[idx];
  675. phys_addr_t base;
  676. int rc;
  677. rc = ntb_peer_mw_get_addr(tc->ntb, idx, &base, &mw->win_size);
  678. if (rc)
  679. return rc;
  680. mw->tc = tc;
  681. mw->idx = idx;
  682. mw->local = ioremap_wc(base, mw->win_size);
  683. if (!mw->local)
  684. return -EFAULT;
  685. return 0;
  686. }
  687. static void tool_free_mws(struct tool_ctx *tc)
  688. {
  689. int i;
  690. for (i = 0; i < tc->mw_count; i++) {
  691. tool_free_mw(tc, i);
  692. if (tc->mws[i].local)
  693. iounmap(tc->mws[i].local);
  694. tc->mws[i].local = NULL;
  695. }
  696. }
  697. static void tool_setup_dbgfs(struct tool_ctx *tc)
  698. {
  699. int i;
  700. /* This modules is useless without dbgfs... */
  701. if (!tool_dbgfs) {
  702. tc->dbgfs = NULL;
  703. return;
  704. }
  705. tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
  706. tool_dbgfs);
  707. if (!tc->dbgfs)
  708. return;
  709. debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
  710. tc, &tool_db_fops);
  711. debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
  712. tc, &tool_mask_fops);
  713. debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
  714. tc, &tool_peer_db_fops);
  715. debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
  716. tc, &tool_peer_mask_fops);
  717. debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
  718. tc, &tool_spad_fops);
  719. debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
  720. tc, &tool_peer_spad_fops);
  721. debugfs_create_file("link", S_IRUSR | S_IWUSR, tc->dbgfs,
  722. tc, &tool_link_fops);
  723. debugfs_create_file("link_event", S_IWUSR, tc->dbgfs,
  724. tc, &tool_link_event_fops);
  725. for (i = 0; i < tc->mw_count; i++) {
  726. char buf[30];
  727. snprintf(buf, sizeof(buf), "mw%d", i);
  728. debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
  729. &tc->mws[i], &tool_mw_fops);
  730. snprintf(buf, sizeof(buf), "peer_trans%d", i);
  731. debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
  732. &tc->mws[i], &tool_peer_mw_trans_fops);
  733. }
  734. }
  735. static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
  736. {
  737. struct tool_ctx *tc;
  738. int rc;
  739. int i;
  740. if (!ntb->ops->mw_set_trans) {
  741. dev_dbg(&ntb->dev, "need inbound MW based NTB API\n");
  742. rc = -EINVAL;
  743. goto err_tc;
  744. }
  745. if (ntb_spad_count(ntb) < 1) {
  746. dev_dbg(&ntb->dev, "no enough scratchpads\n");
  747. rc = -EINVAL;
  748. goto err_tc;
  749. }
  750. if (ntb_db_is_unsafe(ntb))
  751. dev_dbg(&ntb->dev, "doorbell is unsafe\n");
  752. if (ntb_spad_is_unsafe(ntb))
  753. dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
  754. if (ntb_peer_port_count(ntb) != NTB_DEF_PEER_CNT)
  755. dev_warn(&ntb->dev, "multi-port NTB is unsupported\n");
  756. tc = kzalloc(sizeof(*tc), GFP_KERNEL);
  757. if (!tc) {
  758. rc = -ENOMEM;
  759. goto err_tc;
  760. }
  761. tc->ntb = ntb;
  762. init_waitqueue_head(&tc->link_wq);
  763. tc->mw_count = min(ntb_peer_mw_count(tc->ntb), MAX_MWS);
  764. for (i = 0; i < tc->mw_count; i++) {
  765. rc = tool_init_mw(tc, i);
  766. if (rc)
  767. goto err_ctx;
  768. }
  769. tool_setup_dbgfs(tc);
  770. rc = ntb_set_ctx(ntb, tc, &tool_ops);
  771. if (rc)
  772. goto err_ctx;
  773. ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  774. ntb_link_event(ntb);
  775. return 0;
  776. err_ctx:
  777. tool_free_mws(tc);
  778. debugfs_remove_recursive(tc->dbgfs);
  779. kfree(tc);
  780. err_tc:
  781. return rc;
  782. }
  783. static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
  784. {
  785. struct tool_ctx *tc = ntb->ctx;
  786. tool_free_mws(tc);
  787. ntb_clear_ctx(ntb);
  788. ntb_link_disable(ntb);
  789. debugfs_remove_recursive(tc->dbgfs);
  790. kfree(tc);
  791. }
  792. static struct ntb_client tool_client = {
  793. .ops = {
  794. .probe = tool_probe,
  795. .remove = tool_remove,
  796. },
  797. };
  798. static int __init tool_init(void)
  799. {
  800. int rc;
  801. if (debugfs_initialized())
  802. tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
  803. rc = ntb_register_client(&tool_client);
  804. if (rc)
  805. goto err_client;
  806. return 0;
  807. err_client:
  808. debugfs_remove_recursive(tool_dbgfs);
  809. return rc;
  810. }
  811. module_init(tool_init);
  812. static void __exit tool_exit(void)
  813. {
  814. ntb_unregister_client(&tool_client);
  815. debugfs_remove_recursive(tool_dbgfs);
  816. }
  817. module_exit(tool_exit);