ntb_tool.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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 Debugging Tool 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_tool/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: check local/peer device information.
  61. *
  62. * # Get local device port number
  63. * root@self# cat $DBG_DIR/port
  64. *
  65. * # Check local device functionality
  66. * root@self# ls $DBG_DIR
  67. * db msg1 msg_sts peer4/ port
  68. * db_event msg2 peer0/ peer5/ spad0
  69. * db_mask msg3 peer1/ peer_db spad1
  70. * link msg_event peer2/ peer_db_mask spad2
  71. * msg0 msg_mask peer3/ peer_spad spad3
  72. * # As one can see it supports:
  73. * # 1) four inbound message registers
  74. * # 2) four inbound scratchpads
  75. * # 3) up to six peer devices
  76. *
  77. * # Check peer device port number
  78. * root@self# cat $DBG_DIR/peer0/port
  79. *
  80. * # Check peer device(s) functionality to be used
  81. * root@self# ls $DBG_DIR/peer0
  82. * link mw_trans0 mw_trans6 port
  83. * link_event mw_trans1 mw_trans7 spad0
  84. * msg0 mw_trans2 peer_mw_trans0 spad1
  85. * msg1 mw_trans3 peer_mw_trans1 spad2
  86. * msg2 mw_trans4 peer_mw_trans2 spad3
  87. * msg3 mw_trans5 peer_mw_trans3
  88. * # As one can see we got:
  89. * # 1) four outbound message registers
  90. * # 2) four outbound scratchpads
  91. * # 3) eight inbound memory windows
  92. * # 4) four outbound memory windows
  93. *-----------------------------------------------------------------------------
  94. * Eg: NTB link tests
  95. *
  96. * # Set local link up/down
  97. * root@self# echo Y > $DBG_DIR/link
  98. * root@self# echo N > $DBG_DIR/link
  99. *
  100. * # Check if link with peer device is up/down:
  101. * root@self# cat $DBG_DIR/peer0/link
  102. *
  103. * # Block until the link is up/down
  104. * root@self# echo Y > $DBG_DIR/peer0/link_event
  105. * root@self# echo N > $DBG_DIR/peer0/link_event
  106. *-----------------------------------------------------------------------------
  107. * Eg: Doorbell registers tests (some functionality might be absent)
  108. *
  109. * # Set/clear/get local doorbell
  110. * root@self# echo 's 1' > $DBG_DIR/db
  111. * root@self# echo 'c 1' > $DBG_DIR/db
  112. * root@self# cat $DBG_DIR/db
  113. *
  114. * # Set/clear/get local doorbell mask
  115. * root@self# echo 's 1' > $DBG_DIR/db_mask
  116. * root@self# echo 'c 1' > $DBG_DIR/db_mask
  117. * root@self# cat $DBG_DIR/db_mask
  118. *
  119. * # Ring/clear/get peer doorbell
  120. * root@peer# echo 's 1' > $DBG_DIR/peer_db
  121. * root@peer# echo 'c 1' > $DBG_DIR/peer_db
  122. * root@peer# cat $DBG_DIR/peer_db
  123. *
  124. * # Set/clear/get peer doorbell mask
  125. * root@self# echo 's 1' > $DBG_DIR/peer_db_mask
  126. * root@self# echo 'c 1' > $DBG_DIR/peer_db_mask
  127. * root@self# cat $DBG_DIR/peer_db_mask
  128. *
  129. * # Block until local doorbell is set with specified value
  130. * root@self# echo 1 > $DBG_DIR/db_event
  131. *-----------------------------------------------------------------------------
  132. * Eg: Message registers tests (functionality might be absent)
  133. *
  134. * # Set/clear/get in/out message registers status
  135. * root@self# echo 's 1' > $DBG_DIR/msg_sts
  136. * root@self# echo 'c 1' > $DBG_DIR/msg_sts
  137. * root@self# cat $DBG_DIR/msg_sts
  138. *
  139. * # Set/clear in/out message registers mask
  140. * root@self# echo 's 1' > $DBG_DIR/msg_mask
  141. * root@self# echo 'c 1' > $DBG_DIR/msg_mask
  142. *
  143. * # Get inbound message register #0 value and source of port index
  144. * root@self# cat $DBG_DIR/msg0
  145. *
  146. * # Send some data to peer over outbound message register #0
  147. * root@self# echo 0x01020304 > $DBG_DIR/peer0/msg0
  148. *-----------------------------------------------------------------------------
  149. * Eg: Scratchpad registers tests (functionality might be absent)
  150. *
  151. * # Write/read to/from local scratchpad register #0
  152. * root@peer# echo 0x01020304 > $DBG_DIR/spad0
  153. * root@peer# cat $DBG_DIR/spad0
  154. *
  155. * # Write/read to/from peer scratchpad register #0
  156. * root@peer# echo 0x01020304 > $DBG_DIR/peer0/spad0
  157. * root@peer# cat $DBG_DIR/peer0/spad0
  158. *-----------------------------------------------------------------------------
  159. * Eg: Memory windows tests
  160. *
  161. * # Create inbound memory window buffer of specified size/get its base address
  162. * root@peer# echo 16384 > $DBG_DIR/peer0/mw_trans0
  163. * root@peer# cat $DBG_DIR/peer0/mw_trans0
  164. *
  165. * # Write/read data to/from inbound memory window
  166. * root@peer# echo Hello > $DBG_DIR/peer0/mw0
  167. * root@peer# head -c 7 $DBG_DIR/peer0/mw0
  168. *
  169. * # Map outbound memory window/check it settings (on peer device)
  170. * root@peer# echo 0xADD0BA5E:16384 > $DBG_DIR/peer0/peer_mw_trans0
  171. * root@peer# cat $DBG_DIR/peer0/peer_mw_trans0
  172. *
  173. * # Write/read data to/from outbound memory window (on peer device)
  174. * root@peer# echo olleH > $DBG_DIR/peer0/peer_mw0
  175. * root@peer# head -c 7 $DBG_DIR/peer0/peer_mw0
  176. */
  177. #include <linux/init.h>
  178. #include <linux/kernel.h>
  179. #include <linux/module.h>
  180. #include <linux/debugfs.h>
  181. #include <linux/dma-mapping.h>
  182. #include <linux/pci.h>
  183. #include <linux/slab.h>
  184. #include <linux/uaccess.h>
  185. #include <linux/ntb.h>
  186. #define DRIVER_NAME "ntb_tool"
  187. #define DRIVER_VERSION "2.0"
  188. MODULE_LICENSE("Dual BSD/GPL");
  189. MODULE_VERSION(DRIVER_VERSION);
  190. MODULE_AUTHOR("Allen Hubbe <[email protected]>");
  191. MODULE_DESCRIPTION("PCIe NTB Debugging Tool");
  192. /*
  193. * Inbound and outbound memory windows descriptor. Union members selection
  194. * depends on the MW type the structure describes. mm_base/dma_base are the
  195. * virtual and DMA address of an inbound MW. io_base/tr_base are the MMIO
  196. * mapped virtual and xlat addresses of an outbound MW respectively.
  197. */
  198. struct tool_mw {
  199. int widx;
  200. int pidx;
  201. struct tool_ctx *tc;
  202. union {
  203. u8 *mm_base;
  204. u8 __iomem *io_base;
  205. };
  206. union {
  207. dma_addr_t dma_base;
  208. u64 tr_base;
  209. };
  210. resource_size_t size;
  211. struct dentry *dbgfs_file;
  212. };
  213. /*
  214. * Wrapper structure is used to distinguish the outbound MW peers reference
  215. * within the corresponding DebugFS directory IO operation.
  216. */
  217. struct tool_mw_wrap {
  218. int pidx;
  219. struct tool_mw *mw;
  220. };
  221. struct tool_msg {
  222. int midx;
  223. int pidx;
  224. struct tool_ctx *tc;
  225. };
  226. struct tool_spad {
  227. int sidx;
  228. int pidx;
  229. struct tool_ctx *tc;
  230. };
  231. struct tool_peer {
  232. int pidx;
  233. struct tool_ctx *tc;
  234. int inmw_cnt;
  235. struct tool_mw *inmws;
  236. int outmw_cnt;
  237. struct tool_mw_wrap *outmws;
  238. int outmsg_cnt;
  239. struct tool_msg *outmsgs;
  240. int outspad_cnt;
  241. struct tool_spad *outspads;
  242. struct dentry *dbgfs_dir;
  243. };
  244. struct tool_ctx {
  245. struct ntb_dev *ntb;
  246. wait_queue_head_t link_wq;
  247. wait_queue_head_t db_wq;
  248. wait_queue_head_t msg_wq;
  249. int outmw_cnt;
  250. struct tool_mw *outmws;
  251. int peer_cnt;
  252. struct tool_peer *peers;
  253. int inmsg_cnt;
  254. struct tool_msg *inmsgs;
  255. int inspad_cnt;
  256. struct tool_spad *inspads;
  257. struct dentry *dbgfs_dir;
  258. };
  259. #define TOOL_FOPS_RDWR(__name, __read, __write) \
  260. const struct file_operations __name = { \
  261. .owner = THIS_MODULE, \
  262. .open = simple_open, \
  263. .read = __read, \
  264. .write = __write, \
  265. }
  266. #define TOOL_BUF_LEN 32
  267. static struct dentry *tool_dbgfs_topdir;
  268. /*==============================================================================
  269. * NTB events handlers
  270. *==============================================================================
  271. */
  272. static void tool_link_event(void *ctx)
  273. {
  274. struct tool_ctx *tc = ctx;
  275. enum ntb_speed speed;
  276. enum ntb_width width;
  277. int up;
  278. up = ntb_link_is_up(tc->ntb, &speed, &width);
  279. dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
  280. up ? "up" : "down", speed, width);
  281. wake_up(&tc->link_wq);
  282. }
  283. static void tool_db_event(void *ctx, int vec)
  284. {
  285. struct tool_ctx *tc = ctx;
  286. u64 db_bits, db_mask;
  287. db_mask = ntb_db_vector_mask(tc->ntb, vec);
  288. db_bits = ntb_db_read(tc->ntb);
  289. dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
  290. vec, db_mask, db_bits);
  291. wake_up(&tc->db_wq);
  292. }
  293. static void tool_msg_event(void *ctx)
  294. {
  295. struct tool_ctx *tc = ctx;
  296. u64 msg_sts;
  297. msg_sts = ntb_msg_read_sts(tc->ntb);
  298. dev_dbg(&tc->ntb->dev, "message bits %#llx\n", msg_sts);
  299. wake_up(&tc->msg_wq);
  300. }
  301. static const struct ntb_ctx_ops tool_ops = {
  302. .link_event = tool_link_event,
  303. .db_event = tool_db_event,
  304. .msg_event = tool_msg_event
  305. };
  306. /*==============================================================================
  307. * Common read/write methods
  308. *==============================================================================
  309. */
  310. static ssize_t tool_fn_read(struct tool_ctx *tc, char __user *ubuf,
  311. size_t size, loff_t *offp,
  312. u64 (*fn_read)(struct ntb_dev *))
  313. {
  314. size_t buf_size;
  315. char buf[TOOL_BUF_LEN];
  316. ssize_t pos;
  317. if (!fn_read)
  318. return -EINVAL;
  319. buf_size = min(size, sizeof(buf));
  320. pos = scnprintf(buf, buf_size, "%#llx\n", fn_read(tc->ntb));
  321. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  322. }
  323. static ssize_t tool_fn_write(struct tool_ctx *tc,
  324. const char __user *ubuf,
  325. size_t size, loff_t *offp,
  326. int (*fn_set)(struct ntb_dev *, u64),
  327. int (*fn_clear)(struct ntb_dev *, u64))
  328. {
  329. char *buf, cmd;
  330. ssize_t ret;
  331. u64 bits;
  332. int n;
  333. if (*offp)
  334. return 0;
  335. buf = kmalloc(size + 1, GFP_KERNEL);
  336. if (!buf)
  337. return -ENOMEM;
  338. if (copy_from_user(buf, ubuf, size)) {
  339. kfree(buf);
  340. return -EFAULT;
  341. }
  342. buf[size] = 0;
  343. n = sscanf(buf, "%c %lli", &cmd, &bits);
  344. kfree(buf);
  345. if (n != 2) {
  346. ret = -EINVAL;
  347. } else if (cmd == 's') {
  348. if (!fn_set)
  349. ret = -EINVAL;
  350. else
  351. ret = fn_set(tc->ntb, bits);
  352. } else if (cmd == 'c') {
  353. if (!fn_clear)
  354. ret = -EINVAL;
  355. else
  356. ret = fn_clear(tc->ntb, bits);
  357. } else {
  358. ret = -EINVAL;
  359. }
  360. return ret ? : size;
  361. }
  362. /*==============================================================================
  363. * Port read/write methods
  364. *==============================================================================
  365. */
  366. static ssize_t tool_port_read(struct file *filep, char __user *ubuf,
  367. size_t size, loff_t *offp)
  368. {
  369. struct tool_ctx *tc = filep->private_data;
  370. char buf[TOOL_BUF_LEN];
  371. int pos;
  372. pos = scnprintf(buf, sizeof(buf), "%d\n", ntb_port_number(tc->ntb));
  373. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  374. }
  375. static TOOL_FOPS_RDWR(tool_port_fops,
  376. tool_port_read,
  377. NULL);
  378. static ssize_t tool_peer_port_read(struct file *filep, char __user *ubuf,
  379. size_t size, loff_t *offp)
  380. {
  381. struct tool_peer *peer = filep->private_data;
  382. struct tool_ctx *tc = peer->tc;
  383. char buf[TOOL_BUF_LEN];
  384. int pos;
  385. pos = scnprintf(buf, sizeof(buf), "%d\n",
  386. ntb_peer_port_number(tc->ntb, peer->pidx));
  387. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  388. }
  389. static TOOL_FOPS_RDWR(tool_peer_port_fops,
  390. tool_peer_port_read,
  391. NULL);
  392. static int tool_init_peers(struct tool_ctx *tc)
  393. {
  394. int pidx;
  395. tc->peer_cnt = ntb_peer_port_count(tc->ntb);
  396. tc->peers = devm_kcalloc(&tc->ntb->dev, tc->peer_cnt,
  397. sizeof(*tc->peers), GFP_KERNEL);
  398. if (tc->peers == NULL)
  399. return -ENOMEM;
  400. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  401. tc->peers[pidx].pidx = pidx;
  402. tc->peers[pidx].tc = tc;
  403. }
  404. return 0;
  405. }
  406. /*==============================================================================
  407. * Link state read/write methods
  408. *==============================================================================
  409. */
  410. static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
  411. size_t size, loff_t *offp)
  412. {
  413. struct tool_ctx *tc = filep->private_data;
  414. bool val;
  415. int ret;
  416. ret = kstrtobool_from_user(ubuf, size, &val);
  417. if (ret)
  418. return ret;
  419. if (val)
  420. ret = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  421. else
  422. ret = ntb_link_disable(tc->ntb);
  423. if (ret)
  424. return ret;
  425. return size;
  426. }
  427. static TOOL_FOPS_RDWR(tool_link_fops,
  428. NULL,
  429. tool_link_write);
  430. static ssize_t tool_peer_link_read(struct file *filep, char __user *ubuf,
  431. size_t size, loff_t *offp)
  432. {
  433. struct tool_peer *peer = filep->private_data;
  434. struct tool_ctx *tc = peer->tc;
  435. char buf[3];
  436. if (ntb_link_is_up(tc->ntb, NULL, NULL) & BIT(peer->pidx))
  437. buf[0] = 'Y';
  438. else
  439. buf[0] = 'N';
  440. buf[1] = '\n';
  441. buf[2] = '\0';
  442. return simple_read_from_buffer(ubuf, size, offp, buf, 2);
  443. }
  444. static TOOL_FOPS_RDWR(tool_peer_link_fops,
  445. tool_peer_link_read,
  446. NULL);
  447. static ssize_t tool_peer_link_event_write(struct file *filep,
  448. const char __user *ubuf,
  449. size_t size, loff_t *offp)
  450. {
  451. struct tool_peer *peer = filep->private_data;
  452. struct tool_ctx *tc = peer->tc;
  453. u64 link_msk;
  454. bool val;
  455. int ret;
  456. ret = kstrtobool_from_user(ubuf, size, &val);
  457. if (ret)
  458. return ret;
  459. link_msk = BIT_ULL_MASK(peer->pidx);
  460. if (wait_event_interruptible(tc->link_wq,
  461. !!(ntb_link_is_up(tc->ntb, NULL, NULL) & link_msk) == val))
  462. return -ERESTART;
  463. return size;
  464. }
  465. static TOOL_FOPS_RDWR(tool_peer_link_event_fops,
  466. NULL,
  467. tool_peer_link_event_write);
  468. /*==============================================================================
  469. * Memory windows read/write/setting methods
  470. *==============================================================================
  471. */
  472. static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
  473. size_t size, loff_t *offp)
  474. {
  475. struct tool_mw *inmw = filep->private_data;
  476. if (inmw->mm_base == NULL)
  477. return -ENXIO;
  478. return simple_read_from_buffer(ubuf, size, offp,
  479. inmw->mm_base, inmw->size);
  480. }
  481. static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
  482. size_t size, loff_t *offp)
  483. {
  484. struct tool_mw *inmw = filep->private_data;
  485. if (inmw->mm_base == NULL)
  486. return -ENXIO;
  487. return simple_write_to_buffer(inmw->mm_base, inmw->size, offp,
  488. ubuf, size);
  489. }
  490. static TOOL_FOPS_RDWR(tool_mw_fops,
  491. tool_mw_read,
  492. tool_mw_write);
  493. static int tool_setup_mw(struct tool_ctx *tc, int pidx, int widx,
  494. size_t req_size)
  495. {
  496. resource_size_t size, addr_align, size_align;
  497. struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
  498. char buf[TOOL_BUF_LEN];
  499. int ret;
  500. if (inmw->mm_base != NULL)
  501. return 0;
  502. ret = ntb_mw_get_align(tc->ntb, pidx, widx, &addr_align,
  503. &size_align, &size);
  504. if (ret)
  505. return ret;
  506. inmw->size = min_t(resource_size_t, req_size, size);
  507. inmw->size = round_up(inmw->size, addr_align);
  508. inmw->size = round_up(inmw->size, size_align);
  509. inmw->mm_base = dma_alloc_coherent(&tc->ntb->pdev->dev, inmw->size,
  510. &inmw->dma_base, GFP_KERNEL);
  511. if (!inmw->mm_base)
  512. return -ENOMEM;
  513. if (!IS_ALIGNED(inmw->dma_base, addr_align)) {
  514. ret = -ENOMEM;
  515. goto err_free_dma;
  516. }
  517. ret = ntb_mw_set_trans(tc->ntb, pidx, widx, inmw->dma_base, inmw->size);
  518. if (ret)
  519. goto err_free_dma;
  520. snprintf(buf, sizeof(buf), "mw%d", widx);
  521. inmw->dbgfs_file = debugfs_create_file(buf, 0600,
  522. tc->peers[pidx].dbgfs_dir, inmw,
  523. &tool_mw_fops);
  524. return 0;
  525. err_free_dma:
  526. dma_free_coherent(&tc->ntb->pdev->dev, inmw->size, inmw->mm_base,
  527. inmw->dma_base);
  528. inmw->mm_base = NULL;
  529. inmw->dma_base = 0;
  530. inmw->size = 0;
  531. return ret;
  532. }
  533. static void tool_free_mw(struct tool_ctx *tc, int pidx, int widx)
  534. {
  535. struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
  536. debugfs_remove(inmw->dbgfs_file);
  537. if (inmw->mm_base != NULL) {
  538. ntb_mw_clear_trans(tc->ntb, pidx, widx);
  539. dma_free_coherent(&tc->ntb->pdev->dev, inmw->size,
  540. inmw->mm_base, inmw->dma_base);
  541. }
  542. inmw->mm_base = NULL;
  543. inmw->dma_base = 0;
  544. inmw->size = 0;
  545. inmw->dbgfs_file = NULL;
  546. }
  547. static ssize_t tool_mw_trans_read(struct file *filep, char __user *ubuf,
  548. size_t size, loff_t *offp)
  549. {
  550. struct tool_mw *inmw = filep->private_data;
  551. resource_size_t addr_align;
  552. resource_size_t size_align;
  553. resource_size_t size_max;
  554. ssize_t ret, off = 0;
  555. size_t buf_size;
  556. char *buf;
  557. buf_size = min_t(size_t, size, 512);
  558. buf = kmalloc(buf_size, GFP_KERNEL);
  559. if (!buf)
  560. return -ENOMEM;
  561. ret = ntb_mw_get_align(inmw->tc->ntb, inmw->pidx, inmw->widx,
  562. &addr_align, &size_align, &size_max);
  563. if (ret)
  564. goto err;
  565. off += scnprintf(buf + off, buf_size - off,
  566. "Inbound MW \t%d\n",
  567. inmw->widx);
  568. off += scnprintf(buf + off, buf_size - off,
  569. "Port \t%d (%d)\n",
  570. ntb_peer_port_number(inmw->tc->ntb, inmw->pidx),
  571. inmw->pidx);
  572. off += scnprintf(buf + off, buf_size - off,
  573. "Window Address \t0x%pK\n", inmw->mm_base);
  574. off += scnprintf(buf + off, buf_size - off,
  575. "DMA Address \t%pad\n",
  576. &inmw->dma_base);
  577. off += scnprintf(buf + off, buf_size - off,
  578. "Window Size \t%pap\n",
  579. &inmw->size);
  580. off += scnprintf(buf + off, buf_size - off,
  581. "Alignment \t%pap\n",
  582. &addr_align);
  583. off += scnprintf(buf + off, buf_size - off,
  584. "Size Alignment \t%pap\n",
  585. &size_align);
  586. off += scnprintf(buf + off, buf_size - off,
  587. "Size Max \t%pap\n",
  588. &size_max);
  589. ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
  590. err:
  591. kfree(buf);
  592. return ret;
  593. }
  594. static ssize_t tool_mw_trans_write(struct file *filep, const char __user *ubuf,
  595. size_t size, loff_t *offp)
  596. {
  597. struct tool_mw *inmw = filep->private_data;
  598. unsigned int val;
  599. int ret;
  600. ret = kstrtouint_from_user(ubuf, size, 0, &val);
  601. if (ret)
  602. return ret;
  603. tool_free_mw(inmw->tc, inmw->pidx, inmw->widx);
  604. if (val) {
  605. ret = tool_setup_mw(inmw->tc, inmw->pidx, inmw->widx, val);
  606. if (ret)
  607. return ret;
  608. }
  609. return size;
  610. }
  611. static TOOL_FOPS_RDWR(tool_mw_trans_fops,
  612. tool_mw_trans_read,
  613. tool_mw_trans_write);
  614. static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
  615. size_t size, loff_t *offp)
  616. {
  617. struct tool_mw *outmw = filep->private_data;
  618. loff_t pos = *offp;
  619. ssize_t ret;
  620. void *buf;
  621. if (outmw->io_base == NULL)
  622. return -EIO;
  623. if (pos >= outmw->size || !size)
  624. return 0;
  625. if (size > outmw->size - pos)
  626. size = outmw->size - pos;
  627. buf = kmalloc(size, GFP_KERNEL);
  628. if (!buf)
  629. return -ENOMEM;
  630. memcpy_fromio(buf, outmw->io_base + pos, size);
  631. ret = copy_to_user(ubuf, buf, size);
  632. if (ret == size) {
  633. ret = -EFAULT;
  634. goto err_free;
  635. }
  636. size -= ret;
  637. *offp = pos + size;
  638. ret = size;
  639. err_free:
  640. kfree(buf);
  641. return ret;
  642. }
  643. static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
  644. size_t size, loff_t *offp)
  645. {
  646. struct tool_mw *outmw = filep->private_data;
  647. ssize_t ret;
  648. loff_t pos = *offp;
  649. void *buf;
  650. if (outmw->io_base == NULL)
  651. return -EIO;
  652. if (pos >= outmw->size || !size)
  653. return 0;
  654. if (size > outmw->size - pos)
  655. size = outmw->size - pos;
  656. buf = kmalloc(size, GFP_KERNEL);
  657. if (!buf)
  658. return -ENOMEM;
  659. ret = copy_from_user(buf, ubuf, size);
  660. if (ret == size) {
  661. ret = -EFAULT;
  662. goto err_free;
  663. }
  664. size -= ret;
  665. *offp = pos + size;
  666. ret = size;
  667. memcpy_toio(outmw->io_base + pos, buf, size);
  668. err_free:
  669. kfree(buf);
  670. return ret;
  671. }
  672. static TOOL_FOPS_RDWR(tool_peer_mw_fops,
  673. tool_peer_mw_read,
  674. tool_peer_mw_write);
  675. static int tool_setup_peer_mw(struct tool_ctx *tc, int pidx, int widx,
  676. u64 req_addr, size_t req_size)
  677. {
  678. struct tool_mw *outmw = &tc->outmws[widx];
  679. resource_size_t map_size;
  680. phys_addr_t map_base;
  681. char buf[TOOL_BUF_LEN];
  682. int ret;
  683. if (outmw->io_base != NULL)
  684. return 0;
  685. ret = ntb_peer_mw_get_addr(tc->ntb, widx, &map_base, &map_size);
  686. if (ret)
  687. return ret;
  688. ret = ntb_peer_mw_set_trans(tc->ntb, pidx, widx, req_addr, req_size);
  689. if (ret)
  690. return ret;
  691. outmw->io_base = ioremap_wc(map_base, map_size);
  692. if (outmw->io_base == NULL) {
  693. ret = -EFAULT;
  694. goto err_clear_trans;
  695. }
  696. outmw->tr_base = req_addr;
  697. outmw->size = req_size;
  698. outmw->pidx = pidx;
  699. snprintf(buf, sizeof(buf), "peer_mw%d", widx);
  700. outmw->dbgfs_file = debugfs_create_file(buf, 0600,
  701. tc->peers[pidx].dbgfs_dir, outmw,
  702. &tool_peer_mw_fops);
  703. return 0;
  704. err_clear_trans:
  705. ntb_peer_mw_clear_trans(tc->ntb, pidx, widx);
  706. return ret;
  707. }
  708. static void tool_free_peer_mw(struct tool_ctx *tc, int widx)
  709. {
  710. struct tool_mw *outmw = &tc->outmws[widx];
  711. debugfs_remove(outmw->dbgfs_file);
  712. if (outmw->io_base != NULL) {
  713. iounmap(tc->outmws[widx].io_base);
  714. ntb_peer_mw_clear_trans(tc->ntb, outmw->pidx, widx);
  715. }
  716. outmw->io_base = NULL;
  717. outmw->tr_base = 0;
  718. outmw->size = 0;
  719. outmw->pidx = -1;
  720. outmw->dbgfs_file = NULL;
  721. }
  722. static ssize_t tool_peer_mw_trans_read(struct file *filep, char __user *ubuf,
  723. size_t size, loff_t *offp)
  724. {
  725. struct tool_mw_wrap *outmw_wrap = filep->private_data;
  726. struct tool_mw *outmw = outmw_wrap->mw;
  727. resource_size_t map_size;
  728. phys_addr_t map_base;
  729. ssize_t off = 0;
  730. size_t buf_size;
  731. char *buf;
  732. int ret;
  733. ret = ntb_peer_mw_get_addr(outmw->tc->ntb, outmw->widx,
  734. &map_base, &map_size);
  735. if (ret)
  736. return ret;
  737. buf_size = min_t(size_t, size, 512);
  738. buf = kmalloc(buf_size, GFP_KERNEL);
  739. if (!buf)
  740. return -ENOMEM;
  741. off += scnprintf(buf + off, buf_size - off,
  742. "Outbound MW: \t%d\n", outmw->widx);
  743. if (outmw->io_base != NULL) {
  744. off += scnprintf(buf + off, buf_size - off,
  745. "Port attached \t%d (%d)\n",
  746. ntb_peer_port_number(outmw->tc->ntb, outmw->pidx),
  747. outmw->pidx);
  748. } else {
  749. off += scnprintf(buf + off, buf_size - off,
  750. "Port attached \t-1 (-1)\n");
  751. }
  752. off += scnprintf(buf + off, buf_size - off,
  753. "Virtual address \t0x%pK\n", outmw->io_base);
  754. off += scnprintf(buf + off, buf_size - off,
  755. "Phys Address \t%pap\n", &map_base);
  756. off += scnprintf(buf + off, buf_size - off,
  757. "Mapping Size \t%pap\n", &map_size);
  758. off += scnprintf(buf + off, buf_size - off,
  759. "Translation Address \t0x%016llx\n", outmw->tr_base);
  760. off += scnprintf(buf + off, buf_size - off,
  761. "Window Size \t%pap\n", &outmw->size);
  762. ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
  763. kfree(buf);
  764. return ret;
  765. }
  766. static ssize_t tool_peer_mw_trans_write(struct file *filep,
  767. const char __user *ubuf,
  768. size_t size, loff_t *offp)
  769. {
  770. struct tool_mw_wrap *outmw_wrap = filep->private_data;
  771. struct tool_mw *outmw = outmw_wrap->mw;
  772. size_t buf_size, wsize;
  773. char buf[TOOL_BUF_LEN];
  774. int ret, n;
  775. u64 addr;
  776. buf_size = min(size, (sizeof(buf) - 1));
  777. if (copy_from_user(buf, ubuf, buf_size))
  778. return -EFAULT;
  779. buf[buf_size] = '\0';
  780. n = sscanf(buf, "%lli:%zi", &addr, &wsize);
  781. if (n != 2)
  782. return -EINVAL;
  783. tool_free_peer_mw(outmw->tc, outmw->widx);
  784. if (wsize) {
  785. ret = tool_setup_peer_mw(outmw->tc, outmw_wrap->pidx,
  786. outmw->widx, addr, wsize);
  787. if (ret)
  788. return ret;
  789. }
  790. return size;
  791. }
  792. static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
  793. tool_peer_mw_trans_read,
  794. tool_peer_mw_trans_write);
  795. static int tool_init_mws(struct tool_ctx *tc)
  796. {
  797. int widx, pidx;
  798. /* Initialize outbound memory windows */
  799. tc->outmw_cnt = ntb_peer_mw_count(tc->ntb);
  800. tc->outmws = devm_kcalloc(&tc->ntb->dev, tc->outmw_cnt,
  801. sizeof(*tc->outmws), GFP_KERNEL);
  802. if (tc->outmws == NULL)
  803. return -ENOMEM;
  804. for (widx = 0; widx < tc->outmw_cnt; widx++) {
  805. tc->outmws[widx].widx = widx;
  806. tc->outmws[widx].pidx = -1;
  807. tc->outmws[widx].tc = tc;
  808. }
  809. /* Initialize inbound memory windows and outbound MWs wrapper */
  810. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  811. tc->peers[pidx].inmw_cnt = ntb_mw_count(tc->ntb, pidx);
  812. tc->peers[pidx].inmws =
  813. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].inmw_cnt,
  814. sizeof(*tc->peers[pidx].inmws), GFP_KERNEL);
  815. if (tc->peers[pidx].inmws == NULL)
  816. return -ENOMEM;
  817. for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
  818. tc->peers[pidx].inmws[widx].widx = widx;
  819. tc->peers[pidx].inmws[widx].pidx = pidx;
  820. tc->peers[pidx].inmws[widx].tc = tc;
  821. }
  822. tc->peers[pidx].outmw_cnt = ntb_peer_mw_count(tc->ntb);
  823. tc->peers[pidx].outmws =
  824. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmw_cnt,
  825. sizeof(*tc->peers[pidx].outmws), GFP_KERNEL);
  826. if (tc->peers[pidx].outmws == NULL)
  827. return -ENOMEM;
  828. for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
  829. tc->peers[pidx].outmws[widx].pidx = pidx;
  830. tc->peers[pidx].outmws[widx].mw = &tc->outmws[widx];
  831. }
  832. }
  833. return 0;
  834. }
  835. static void tool_clear_mws(struct tool_ctx *tc)
  836. {
  837. int widx, pidx;
  838. /* Free outbound memory windows */
  839. for (widx = 0; widx < tc->outmw_cnt; widx++)
  840. tool_free_peer_mw(tc, widx);
  841. /* Free outbound memory windows */
  842. for (pidx = 0; pidx < tc->peer_cnt; pidx++)
  843. for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
  844. tool_free_mw(tc, pidx, widx);
  845. }
  846. /*==============================================================================
  847. * Doorbell read/write methods
  848. *==============================================================================
  849. */
  850. static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
  851. size_t size, loff_t *offp)
  852. {
  853. struct tool_ctx *tc = filep->private_data;
  854. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read);
  855. }
  856. static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
  857. size_t size, loff_t *offp)
  858. {
  859. struct tool_ctx *tc = filep->private_data;
  860. return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set,
  861. tc->ntb->ops->db_clear);
  862. }
  863. static TOOL_FOPS_RDWR(tool_db_fops,
  864. tool_db_read,
  865. tool_db_write);
  866. static ssize_t tool_db_valid_mask_read(struct file *filep, char __user *ubuf,
  867. size_t size, loff_t *offp)
  868. {
  869. struct tool_ctx *tc = filep->private_data;
  870. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_valid_mask);
  871. }
  872. static TOOL_FOPS_RDWR(tool_db_valid_mask_fops,
  873. tool_db_valid_mask_read,
  874. NULL);
  875. static ssize_t tool_db_mask_read(struct file *filep, char __user *ubuf,
  876. size_t size, loff_t *offp)
  877. {
  878. struct tool_ctx *tc = filep->private_data;
  879. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read_mask);
  880. }
  881. static ssize_t tool_db_mask_write(struct file *filep, const char __user *ubuf,
  882. size_t size, loff_t *offp)
  883. {
  884. struct tool_ctx *tc = filep->private_data;
  885. return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set_mask,
  886. tc->ntb->ops->db_clear_mask);
  887. }
  888. static TOOL_FOPS_RDWR(tool_db_mask_fops,
  889. tool_db_mask_read,
  890. tool_db_mask_write);
  891. static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
  892. size_t size, loff_t *offp)
  893. {
  894. struct tool_ctx *tc = filep->private_data;
  895. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->peer_db_read);
  896. }
  897. static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
  898. size_t size, loff_t *offp)
  899. {
  900. struct tool_ctx *tc = filep->private_data;
  901. return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->peer_db_set,
  902. tc->ntb->ops->peer_db_clear);
  903. }
  904. static TOOL_FOPS_RDWR(tool_peer_db_fops,
  905. tool_peer_db_read,
  906. tool_peer_db_write);
  907. static ssize_t tool_peer_db_mask_read(struct file *filep, char __user *ubuf,
  908. size_t size, loff_t *offp)
  909. {
  910. struct tool_ctx *tc = filep->private_data;
  911. return tool_fn_read(tc, ubuf, size, offp,
  912. tc->ntb->ops->peer_db_read_mask);
  913. }
  914. static ssize_t tool_peer_db_mask_write(struct file *filep,
  915. const char __user *ubuf,
  916. size_t size, loff_t *offp)
  917. {
  918. struct tool_ctx *tc = filep->private_data;
  919. return tool_fn_write(tc, ubuf, size, offp,
  920. tc->ntb->ops->peer_db_set_mask,
  921. tc->ntb->ops->peer_db_clear_mask);
  922. }
  923. static TOOL_FOPS_RDWR(tool_peer_db_mask_fops,
  924. tool_peer_db_mask_read,
  925. tool_peer_db_mask_write);
  926. static ssize_t tool_db_event_write(struct file *filep,
  927. const char __user *ubuf,
  928. size_t size, loff_t *offp)
  929. {
  930. struct tool_ctx *tc = filep->private_data;
  931. u64 val;
  932. int ret;
  933. ret = kstrtou64_from_user(ubuf, size, 0, &val);
  934. if (ret)
  935. return ret;
  936. if (wait_event_interruptible(tc->db_wq, ntb_db_read(tc->ntb) == val))
  937. return -ERESTART;
  938. return size;
  939. }
  940. static TOOL_FOPS_RDWR(tool_db_event_fops,
  941. NULL,
  942. tool_db_event_write);
  943. /*==============================================================================
  944. * Scratchpads read/write methods
  945. *==============================================================================
  946. */
  947. static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
  948. size_t size, loff_t *offp)
  949. {
  950. struct tool_spad *spad = filep->private_data;
  951. char buf[TOOL_BUF_LEN];
  952. ssize_t pos;
  953. if (!spad->tc->ntb->ops->spad_read)
  954. return -EINVAL;
  955. pos = scnprintf(buf, sizeof(buf), "%#x\n",
  956. ntb_spad_read(spad->tc->ntb, spad->sidx));
  957. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  958. }
  959. static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
  960. size_t size, loff_t *offp)
  961. {
  962. struct tool_spad *spad = filep->private_data;
  963. u32 val;
  964. int ret;
  965. if (!spad->tc->ntb->ops->spad_write) {
  966. dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
  967. return -EINVAL;
  968. }
  969. ret = kstrtou32_from_user(ubuf, size, 0, &val);
  970. if (ret)
  971. return ret;
  972. ret = ntb_spad_write(spad->tc->ntb, spad->sidx, val);
  973. return ret ?: size;
  974. }
  975. static TOOL_FOPS_RDWR(tool_spad_fops,
  976. tool_spad_read,
  977. tool_spad_write);
  978. static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
  979. size_t size, loff_t *offp)
  980. {
  981. struct tool_spad *spad = filep->private_data;
  982. char buf[TOOL_BUF_LEN];
  983. ssize_t pos;
  984. if (!spad->tc->ntb->ops->peer_spad_read)
  985. return -EINVAL;
  986. pos = scnprintf(buf, sizeof(buf), "%#x\n",
  987. ntb_peer_spad_read(spad->tc->ntb, spad->pidx, spad->sidx));
  988. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  989. }
  990. static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
  991. size_t size, loff_t *offp)
  992. {
  993. struct tool_spad *spad = filep->private_data;
  994. u32 val;
  995. int ret;
  996. if (!spad->tc->ntb->ops->peer_spad_write) {
  997. dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
  998. return -EINVAL;
  999. }
  1000. ret = kstrtou32_from_user(ubuf, size, 0, &val);
  1001. if (ret)
  1002. return ret;
  1003. ret = ntb_peer_spad_write(spad->tc->ntb, spad->pidx, spad->sidx, val);
  1004. return ret ?: size;
  1005. }
  1006. static TOOL_FOPS_RDWR(tool_peer_spad_fops,
  1007. tool_peer_spad_read,
  1008. tool_peer_spad_write);
  1009. static int tool_init_spads(struct tool_ctx *tc)
  1010. {
  1011. int sidx, pidx;
  1012. /* Initialize inbound scratchpad structures */
  1013. tc->inspad_cnt = ntb_spad_count(tc->ntb);
  1014. tc->inspads = devm_kcalloc(&tc->ntb->dev, tc->inspad_cnt,
  1015. sizeof(*tc->inspads), GFP_KERNEL);
  1016. if (tc->inspads == NULL)
  1017. return -ENOMEM;
  1018. for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
  1019. tc->inspads[sidx].sidx = sidx;
  1020. tc->inspads[sidx].pidx = -1;
  1021. tc->inspads[sidx].tc = tc;
  1022. }
  1023. /* Initialize outbound scratchpad structures */
  1024. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  1025. tc->peers[pidx].outspad_cnt = ntb_spad_count(tc->ntb);
  1026. tc->peers[pidx].outspads =
  1027. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outspad_cnt,
  1028. sizeof(*tc->peers[pidx].outspads), GFP_KERNEL);
  1029. if (tc->peers[pidx].outspads == NULL)
  1030. return -ENOMEM;
  1031. for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
  1032. tc->peers[pidx].outspads[sidx].sidx = sidx;
  1033. tc->peers[pidx].outspads[sidx].pidx = pidx;
  1034. tc->peers[pidx].outspads[sidx].tc = tc;
  1035. }
  1036. }
  1037. return 0;
  1038. }
  1039. /*==============================================================================
  1040. * Messages read/write methods
  1041. *==============================================================================
  1042. */
  1043. static ssize_t tool_inmsg_read(struct file *filep, char __user *ubuf,
  1044. size_t size, loff_t *offp)
  1045. {
  1046. struct tool_msg *msg = filep->private_data;
  1047. char buf[TOOL_BUF_LEN];
  1048. ssize_t pos;
  1049. u32 data;
  1050. int pidx;
  1051. data = ntb_msg_read(msg->tc->ntb, &pidx, msg->midx);
  1052. pos = scnprintf(buf, sizeof(buf), "0x%08x<-%d\n", data, pidx);
  1053. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  1054. }
  1055. static TOOL_FOPS_RDWR(tool_inmsg_fops,
  1056. tool_inmsg_read,
  1057. NULL);
  1058. static ssize_t tool_outmsg_write(struct file *filep,
  1059. const char __user *ubuf,
  1060. size_t size, loff_t *offp)
  1061. {
  1062. struct tool_msg *msg = filep->private_data;
  1063. u32 val;
  1064. int ret;
  1065. ret = kstrtou32_from_user(ubuf, size, 0, &val);
  1066. if (ret)
  1067. return ret;
  1068. ret = ntb_peer_msg_write(msg->tc->ntb, msg->pidx, msg->midx, val);
  1069. return ret ? : size;
  1070. }
  1071. static TOOL_FOPS_RDWR(tool_outmsg_fops,
  1072. NULL,
  1073. tool_outmsg_write);
  1074. static ssize_t tool_msg_sts_read(struct file *filep, char __user *ubuf,
  1075. size_t size, loff_t *offp)
  1076. {
  1077. struct tool_ctx *tc = filep->private_data;
  1078. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_read_sts);
  1079. }
  1080. static ssize_t tool_msg_sts_write(struct file *filep, const char __user *ubuf,
  1081. size_t size, loff_t *offp)
  1082. {
  1083. struct tool_ctx *tc = filep->private_data;
  1084. return tool_fn_write(tc, ubuf, size, offp, NULL,
  1085. tc->ntb->ops->msg_clear_sts);
  1086. }
  1087. static TOOL_FOPS_RDWR(tool_msg_sts_fops,
  1088. tool_msg_sts_read,
  1089. tool_msg_sts_write);
  1090. static ssize_t tool_msg_inbits_read(struct file *filep, char __user *ubuf,
  1091. size_t size, loff_t *offp)
  1092. {
  1093. struct tool_ctx *tc = filep->private_data;
  1094. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_inbits);
  1095. }
  1096. static TOOL_FOPS_RDWR(tool_msg_inbits_fops,
  1097. tool_msg_inbits_read,
  1098. NULL);
  1099. static ssize_t tool_msg_outbits_read(struct file *filep, char __user *ubuf,
  1100. size_t size, loff_t *offp)
  1101. {
  1102. struct tool_ctx *tc = filep->private_data;
  1103. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_outbits);
  1104. }
  1105. static TOOL_FOPS_RDWR(tool_msg_outbits_fops,
  1106. tool_msg_outbits_read,
  1107. NULL);
  1108. static ssize_t tool_msg_mask_write(struct file *filep, const char __user *ubuf,
  1109. size_t size, loff_t *offp)
  1110. {
  1111. struct tool_ctx *tc = filep->private_data;
  1112. return tool_fn_write(tc, ubuf, size, offp,
  1113. tc->ntb->ops->msg_set_mask,
  1114. tc->ntb->ops->msg_clear_mask);
  1115. }
  1116. static TOOL_FOPS_RDWR(tool_msg_mask_fops,
  1117. NULL,
  1118. tool_msg_mask_write);
  1119. static ssize_t tool_msg_event_write(struct file *filep,
  1120. const char __user *ubuf,
  1121. size_t size, loff_t *offp)
  1122. {
  1123. struct tool_ctx *tc = filep->private_data;
  1124. u64 val;
  1125. int ret;
  1126. ret = kstrtou64_from_user(ubuf, size, 0, &val);
  1127. if (ret)
  1128. return ret;
  1129. if (wait_event_interruptible(tc->msg_wq,
  1130. ntb_msg_read_sts(tc->ntb) == val))
  1131. return -ERESTART;
  1132. return size;
  1133. }
  1134. static TOOL_FOPS_RDWR(tool_msg_event_fops,
  1135. NULL,
  1136. tool_msg_event_write);
  1137. static int tool_init_msgs(struct tool_ctx *tc)
  1138. {
  1139. int midx, pidx;
  1140. /* Initialize inbound message structures */
  1141. tc->inmsg_cnt = ntb_msg_count(tc->ntb);
  1142. tc->inmsgs = devm_kcalloc(&tc->ntb->dev, tc->inmsg_cnt,
  1143. sizeof(*tc->inmsgs), GFP_KERNEL);
  1144. if (tc->inmsgs == NULL)
  1145. return -ENOMEM;
  1146. for (midx = 0; midx < tc->inmsg_cnt; midx++) {
  1147. tc->inmsgs[midx].midx = midx;
  1148. tc->inmsgs[midx].pidx = -1;
  1149. tc->inmsgs[midx].tc = tc;
  1150. }
  1151. /* Initialize outbound message structures */
  1152. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  1153. tc->peers[pidx].outmsg_cnt = ntb_msg_count(tc->ntb);
  1154. tc->peers[pidx].outmsgs =
  1155. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmsg_cnt,
  1156. sizeof(*tc->peers[pidx].outmsgs), GFP_KERNEL);
  1157. if (tc->peers[pidx].outmsgs == NULL)
  1158. return -ENOMEM;
  1159. for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
  1160. tc->peers[pidx].outmsgs[midx].midx = midx;
  1161. tc->peers[pidx].outmsgs[midx].pidx = pidx;
  1162. tc->peers[pidx].outmsgs[midx].tc = tc;
  1163. }
  1164. }
  1165. return 0;
  1166. }
  1167. /*==============================================================================
  1168. * Initialization methods
  1169. *==============================================================================
  1170. */
  1171. static struct tool_ctx *tool_create_data(struct ntb_dev *ntb)
  1172. {
  1173. struct tool_ctx *tc;
  1174. tc = devm_kzalloc(&ntb->dev, sizeof(*tc), GFP_KERNEL);
  1175. if (tc == NULL)
  1176. return ERR_PTR(-ENOMEM);
  1177. tc->ntb = ntb;
  1178. init_waitqueue_head(&tc->link_wq);
  1179. init_waitqueue_head(&tc->db_wq);
  1180. init_waitqueue_head(&tc->msg_wq);
  1181. if (ntb_db_is_unsafe(ntb))
  1182. dev_dbg(&ntb->dev, "doorbell is unsafe\n");
  1183. if (ntb_spad_is_unsafe(ntb))
  1184. dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
  1185. return tc;
  1186. }
  1187. static void tool_clear_data(struct tool_ctx *tc)
  1188. {
  1189. wake_up(&tc->link_wq);
  1190. wake_up(&tc->db_wq);
  1191. wake_up(&tc->msg_wq);
  1192. }
  1193. static int tool_init_ntb(struct tool_ctx *tc)
  1194. {
  1195. return ntb_set_ctx(tc->ntb, tc, &tool_ops);
  1196. }
  1197. static void tool_clear_ntb(struct tool_ctx *tc)
  1198. {
  1199. ntb_clear_ctx(tc->ntb);
  1200. ntb_link_disable(tc->ntb);
  1201. }
  1202. static void tool_setup_dbgfs(struct tool_ctx *tc)
  1203. {
  1204. int pidx, widx, sidx, midx;
  1205. char buf[TOOL_BUF_LEN];
  1206. /* This modules is useless without dbgfs... */
  1207. if (!tool_dbgfs_topdir) {
  1208. tc->dbgfs_dir = NULL;
  1209. return;
  1210. }
  1211. tc->dbgfs_dir = debugfs_create_dir(dev_name(&tc->ntb->dev),
  1212. tool_dbgfs_topdir);
  1213. if (!tc->dbgfs_dir)
  1214. return;
  1215. debugfs_create_file("port", 0600, tc->dbgfs_dir,
  1216. tc, &tool_port_fops);
  1217. debugfs_create_file("link", 0600, tc->dbgfs_dir,
  1218. tc, &tool_link_fops);
  1219. debugfs_create_file("db", 0600, tc->dbgfs_dir,
  1220. tc, &tool_db_fops);
  1221. debugfs_create_file("db_valid_mask", 0600, tc->dbgfs_dir,
  1222. tc, &tool_db_valid_mask_fops);
  1223. debugfs_create_file("db_mask", 0600, tc->dbgfs_dir,
  1224. tc, &tool_db_mask_fops);
  1225. debugfs_create_file("db_event", 0600, tc->dbgfs_dir,
  1226. tc, &tool_db_event_fops);
  1227. debugfs_create_file("peer_db", 0600, tc->dbgfs_dir,
  1228. tc, &tool_peer_db_fops);
  1229. debugfs_create_file("peer_db_mask", 0600, tc->dbgfs_dir,
  1230. tc, &tool_peer_db_mask_fops);
  1231. if (tc->inspad_cnt != 0) {
  1232. for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
  1233. snprintf(buf, sizeof(buf), "spad%d", sidx);
  1234. debugfs_create_file(buf, 0600, tc->dbgfs_dir,
  1235. &tc->inspads[sidx], &tool_spad_fops);
  1236. }
  1237. }
  1238. if (tc->inmsg_cnt != 0) {
  1239. for (midx = 0; midx < tc->inmsg_cnt; midx++) {
  1240. snprintf(buf, sizeof(buf), "msg%d", midx);
  1241. debugfs_create_file(buf, 0600, tc->dbgfs_dir,
  1242. &tc->inmsgs[midx], &tool_inmsg_fops);
  1243. }
  1244. debugfs_create_file("msg_sts", 0600, tc->dbgfs_dir,
  1245. tc, &tool_msg_sts_fops);
  1246. debugfs_create_file("msg_inbits", 0600, tc->dbgfs_dir,
  1247. tc, &tool_msg_inbits_fops);
  1248. debugfs_create_file("msg_outbits", 0600, tc->dbgfs_dir,
  1249. tc, &tool_msg_outbits_fops);
  1250. debugfs_create_file("msg_mask", 0600, tc->dbgfs_dir,
  1251. tc, &tool_msg_mask_fops);
  1252. debugfs_create_file("msg_event", 0600, tc->dbgfs_dir,
  1253. tc, &tool_msg_event_fops);
  1254. }
  1255. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  1256. snprintf(buf, sizeof(buf), "peer%d", pidx);
  1257. tc->peers[pidx].dbgfs_dir =
  1258. debugfs_create_dir(buf, tc->dbgfs_dir);
  1259. debugfs_create_file("port", 0600,
  1260. tc->peers[pidx].dbgfs_dir,
  1261. &tc->peers[pidx], &tool_peer_port_fops);
  1262. debugfs_create_file("link", 0200,
  1263. tc->peers[pidx].dbgfs_dir,
  1264. &tc->peers[pidx], &tool_peer_link_fops);
  1265. debugfs_create_file("link_event", 0200,
  1266. tc->peers[pidx].dbgfs_dir,
  1267. &tc->peers[pidx], &tool_peer_link_event_fops);
  1268. for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
  1269. snprintf(buf, sizeof(buf), "mw_trans%d", widx);
  1270. debugfs_create_file(buf, 0600,
  1271. tc->peers[pidx].dbgfs_dir,
  1272. &tc->peers[pidx].inmws[widx],
  1273. &tool_mw_trans_fops);
  1274. }
  1275. for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
  1276. snprintf(buf, sizeof(buf), "peer_mw_trans%d", widx);
  1277. debugfs_create_file(buf, 0600,
  1278. tc->peers[pidx].dbgfs_dir,
  1279. &tc->peers[pidx].outmws[widx],
  1280. &tool_peer_mw_trans_fops);
  1281. }
  1282. for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
  1283. snprintf(buf, sizeof(buf), "spad%d", sidx);
  1284. debugfs_create_file(buf, 0600,
  1285. tc->peers[pidx].dbgfs_dir,
  1286. &tc->peers[pidx].outspads[sidx],
  1287. &tool_peer_spad_fops);
  1288. }
  1289. for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
  1290. snprintf(buf, sizeof(buf), "msg%d", midx);
  1291. debugfs_create_file(buf, 0600,
  1292. tc->peers[pidx].dbgfs_dir,
  1293. &tc->peers[pidx].outmsgs[midx],
  1294. &tool_outmsg_fops);
  1295. }
  1296. }
  1297. }
  1298. static void tool_clear_dbgfs(struct tool_ctx *tc)
  1299. {
  1300. debugfs_remove_recursive(tc->dbgfs_dir);
  1301. }
  1302. static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
  1303. {
  1304. struct tool_ctx *tc;
  1305. int ret;
  1306. tc = tool_create_data(ntb);
  1307. if (IS_ERR(tc))
  1308. return PTR_ERR(tc);
  1309. ret = tool_init_peers(tc);
  1310. if (ret != 0)
  1311. goto err_clear_data;
  1312. ret = tool_init_mws(tc);
  1313. if (ret != 0)
  1314. goto err_clear_data;
  1315. ret = tool_init_spads(tc);
  1316. if (ret != 0)
  1317. goto err_clear_mws;
  1318. ret = tool_init_msgs(tc);
  1319. if (ret != 0)
  1320. goto err_clear_mws;
  1321. ret = tool_init_ntb(tc);
  1322. if (ret != 0)
  1323. goto err_clear_mws;
  1324. tool_setup_dbgfs(tc);
  1325. return 0;
  1326. err_clear_mws:
  1327. tool_clear_mws(tc);
  1328. err_clear_data:
  1329. tool_clear_data(tc);
  1330. return ret;
  1331. }
  1332. static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
  1333. {
  1334. struct tool_ctx *tc = ntb->ctx;
  1335. tool_clear_dbgfs(tc);
  1336. tool_clear_ntb(tc);
  1337. tool_clear_mws(tc);
  1338. tool_clear_data(tc);
  1339. }
  1340. static struct ntb_client tool_client = {
  1341. .ops = {
  1342. .probe = tool_probe,
  1343. .remove = tool_remove,
  1344. }
  1345. };
  1346. static int __init tool_init(void)
  1347. {
  1348. int ret;
  1349. if (debugfs_initialized())
  1350. tool_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1351. ret = ntb_register_client(&tool_client);
  1352. if (ret)
  1353. debugfs_remove_recursive(tool_dbgfs_topdir);
  1354. return ret;
  1355. }
  1356. module_init(tool_init);
  1357. static void __exit tool_exit(void)
  1358. {
  1359. ntb_unregister_client(&tool_client);
  1360. debugfs_remove_recursive(tool_dbgfs_topdir);
  1361. }
  1362. module_exit(tool_exit);