ntb_tool.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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 <Allen.Hubbe@emc.com>");
  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 = memdup_user_nul(ubuf, size);
  336. if (IS_ERR(buf))
  337. return PTR_ERR(buf);
  338. n = sscanf(buf, "%c %lli", &cmd, &bits);
  339. kfree(buf);
  340. if (n != 2) {
  341. ret = -EINVAL;
  342. } else if (cmd == 's') {
  343. if (!fn_set)
  344. ret = -EINVAL;
  345. else
  346. ret = fn_set(tc->ntb, bits);
  347. } else if (cmd == 'c') {
  348. if (!fn_clear)
  349. ret = -EINVAL;
  350. else
  351. ret = fn_clear(tc->ntb, bits);
  352. } else {
  353. ret = -EINVAL;
  354. }
  355. return ret ? : size;
  356. }
  357. /*==============================================================================
  358. * Port read/write methods
  359. *==============================================================================
  360. */
  361. static ssize_t tool_port_read(struct file *filep, char __user *ubuf,
  362. size_t size, loff_t *offp)
  363. {
  364. struct tool_ctx *tc = filep->private_data;
  365. char buf[TOOL_BUF_LEN];
  366. int pos;
  367. pos = scnprintf(buf, sizeof(buf), "%d\n", ntb_port_number(tc->ntb));
  368. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  369. }
  370. static TOOL_FOPS_RDWR(tool_port_fops,
  371. tool_port_read,
  372. NULL);
  373. static ssize_t tool_peer_port_read(struct file *filep, char __user *ubuf,
  374. size_t size, loff_t *offp)
  375. {
  376. struct tool_peer *peer = filep->private_data;
  377. struct tool_ctx *tc = peer->tc;
  378. char buf[TOOL_BUF_LEN];
  379. int pos;
  380. pos = scnprintf(buf, sizeof(buf), "%d\n",
  381. ntb_peer_port_number(tc->ntb, peer->pidx));
  382. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  383. }
  384. static TOOL_FOPS_RDWR(tool_peer_port_fops,
  385. tool_peer_port_read,
  386. NULL);
  387. static int tool_init_peers(struct tool_ctx *tc)
  388. {
  389. int pidx;
  390. tc->peer_cnt = ntb_peer_port_count(tc->ntb);
  391. tc->peers = devm_kcalloc(&tc->ntb->dev, tc->peer_cnt,
  392. sizeof(*tc->peers), GFP_KERNEL);
  393. if (tc->peers == NULL)
  394. return -ENOMEM;
  395. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  396. tc->peers[pidx].pidx = pidx;
  397. tc->peers[pidx].tc = tc;
  398. }
  399. return 0;
  400. }
  401. /*==============================================================================
  402. * Link state read/write methods
  403. *==============================================================================
  404. */
  405. static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
  406. size_t size, loff_t *offp)
  407. {
  408. struct tool_ctx *tc = filep->private_data;
  409. bool val;
  410. int ret;
  411. ret = kstrtobool_from_user(ubuf, size, &val);
  412. if (ret)
  413. return ret;
  414. if (val)
  415. ret = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  416. else
  417. ret = ntb_link_disable(tc->ntb);
  418. if (ret)
  419. return ret;
  420. return size;
  421. }
  422. static TOOL_FOPS_RDWR(tool_link_fops,
  423. NULL,
  424. tool_link_write);
  425. static ssize_t tool_peer_link_read(struct file *filep, char __user *ubuf,
  426. size_t size, loff_t *offp)
  427. {
  428. struct tool_peer *peer = filep->private_data;
  429. struct tool_ctx *tc = peer->tc;
  430. char buf[3];
  431. if (ntb_link_is_up(tc->ntb, NULL, NULL) & BIT(peer->pidx))
  432. buf[0] = 'Y';
  433. else
  434. buf[0] = 'N';
  435. buf[1] = '\n';
  436. buf[2] = '\0';
  437. return simple_read_from_buffer(ubuf, size, offp, buf, 2);
  438. }
  439. static TOOL_FOPS_RDWR(tool_peer_link_fops,
  440. tool_peer_link_read,
  441. NULL);
  442. static ssize_t tool_peer_link_event_write(struct file *filep,
  443. const char __user *ubuf,
  444. size_t size, loff_t *offp)
  445. {
  446. struct tool_peer *peer = filep->private_data;
  447. struct tool_ctx *tc = peer->tc;
  448. u64 link_msk;
  449. bool val;
  450. int ret;
  451. ret = kstrtobool_from_user(ubuf, size, &val);
  452. if (ret)
  453. return ret;
  454. link_msk = BIT_ULL_MASK(peer->pidx);
  455. if (wait_event_interruptible(tc->link_wq,
  456. !!(ntb_link_is_up(tc->ntb, NULL, NULL) & link_msk) == val))
  457. return -ERESTART;
  458. return size;
  459. }
  460. static TOOL_FOPS_RDWR(tool_peer_link_event_fops,
  461. NULL,
  462. tool_peer_link_event_write);
  463. /*==============================================================================
  464. * Memory windows read/write/setting methods
  465. *==============================================================================
  466. */
  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 *inmw = filep->private_data;
  471. if (inmw->mm_base == NULL)
  472. return -ENXIO;
  473. return simple_read_from_buffer(ubuf, size, offp,
  474. inmw->mm_base, inmw->size);
  475. }
  476. static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
  477. size_t size, loff_t *offp)
  478. {
  479. struct tool_mw *inmw = filep->private_data;
  480. if (inmw->mm_base == NULL)
  481. return -ENXIO;
  482. return simple_write_to_buffer(inmw->mm_base, inmw->size, offp,
  483. ubuf, size);
  484. }
  485. static TOOL_FOPS_RDWR(tool_mw_fops,
  486. tool_mw_read,
  487. tool_mw_write);
  488. static int tool_setup_mw(struct tool_ctx *tc, int pidx, int widx,
  489. size_t req_size)
  490. {
  491. resource_size_t size, addr_align, size_align;
  492. struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
  493. char buf[TOOL_BUF_LEN];
  494. int ret;
  495. if (inmw->mm_base != NULL)
  496. return 0;
  497. ret = ntb_mw_get_align(tc->ntb, pidx, widx, &addr_align,
  498. &size_align, &size);
  499. if (ret)
  500. return ret;
  501. inmw->size = min_t(resource_size_t, req_size, size);
  502. inmw->size = round_up(inmw->size, addr_align);
  503. inmw->size = round_up(inmw->size, size_align);
  504. inmw->mm_base = dma_alloc_coherent(&tc->ntb->pdev->dev, inmw->size,
  505. &inmw->dma_base, GFP_KERNEL);
  506. if (!inmw->mm_base)
  507. return -ENOMEM;
  508. if (!IS_ALIGNED(inmw->dma_base, addr_align)) {
  509. ret = -ENOMEM;
  510. goto err_free_dma;
  511. }
  512. ret = ntb_mw_set_trans(tc->ntb, pidx, widx, inmw->dma_base, inmw->size);
  513. if (ret)
  514. goto err_free_dma;
  515. snprintf(buf, sizeof(buf), "mw%d", widx);
  516. inmw->dbgfs_file = debugfs_create_file(buf, 0600,
  517. tc->peers[pidx].dbgfs_dir, inmw,
  518. &tool_mw_fops);
  519. return 0;
  520. err_free_dma:
  521. dma_free_coherent(&tc->ntb->pdev->dev, inmw->size, inmw->mm_base,
  522. inmw->dma_base);
  523. inmw->mm_base = NULL;
  524. inmw->dma_base = 0;
  525. inmw->size = 0;
  526. return ret;
  527. }
  528. static void tool_free_mw(struct tool_ctx *tc, int pidx, int widx)
  529. {
  530. struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
  531. debugfs_remove(inmw->dbgfs_file);
  532. if (inmw->mm_base != NULL) {
  533. ntb_mw_clear_trans(tc->ntb, pidx, widx);
  534. dma_free_coherent(&tc->ntb->pdev->dev, inmw->size,
  535. inmw->mm_base, inmw->dma_base);
  536. }
  537. inmw->mm_base = NULL;
  538. inmw->dma_base = 0;
  539. inmw->size = 0;
  540. inmw->dbgfs_file = NULL;
  541. }
  542. static ssize_t tool_mw_trans_read(struct file *filep, char __user *ubuf,
  543. size_t size, loff_t *offp)
  544. {
  545. struct tool_mw *inmw = filep->private_data;
  546. resource_size_t addr_align;
  547. resource_size_t size_align;
  548. resource_size_t size_max;
  549. ssize_t ret, off = 0;
  550. size_t buf_size;
  551. char *buf;
  552. buf_size = min_t(size_t, size, 512);
  553. buf = kmalloc(buf_size, GFP_KERNEL);
  554. if (!buf)
  555. return -ENOMEM;
  556. ret = ntb_mw_get_align(inmw->tc->ntb, inmw->pidx, inmw->widx,
  557. &addr_align, &size_align, &size_max);
  558. if (ret)
  559. goto err;
  560. off += scnprintf(buf + off, buf_size - off,
  561. "Inbound MW \t%d\n",
  562. inmw->widx);
  563. off += scnprintf(buf + off, buf_size - off,
  564. "Port \t%d (%d)\n",
  565. ntb_peer_port_number(inmw->tc->ntb, inmw->pidx),
  566. inmw->pidx);
  567. off += scnprintf(buf + off, buf_size - off,
  568. "Window Address \t0x%pK\n", inmw->mm_base);
  569. off += scnprintf(buf + off, buf_size - off,
  570. "DMA Address \t%pad\n",
  571. &inmw->dma_base);
  572. off += scnprintf(buf + off, buf_size - off,
  573. "Window Size \t%pap\n",
  574. &inmw->size);
  575. off += scnprintf(buf + off, buf_size - off,
  576. "Alignment \t%pap\n",
  577. &addr_align);
  578. off += scnprintf(buf + off, buf_size - off,
  579. "Size Alignment \t%pap\n",
  580. &size_align);
  581. off += scnprintf(buf + off, buf_size - off,
  582. "Size Max \t%pap\n",
  583. &size_max);
  584. ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
  585. err:
  586. kfree(buf);
  587. return ret;
  588. }
  589. static ssize_t tool_mw_trans_write(struct file *filep, const char __user *ubuf,
  590. size_t size, loff_t *offp)
  591. {
  592. struct tool_mw *inmw = filep->private_data;
  593. unsigned int val;
  594. int ret;
  595. ret = kstrtouint_from_user(ubuf, size, 0, &val);
  596. if (ret)
  597. return ret;
  598. tool_free_mw(inmw->tc, inmw->pidx, inmw->widx);
  599. if (val) {
  600. ret = tool_setup_mw(inmw->tc, inmw->pidx, inmw->widx, val);
  601. if (ret)
  602. return ret;
  603. }
  604. return size;
  605. }
  606. static TOOL_FOPS_RDWR(tool_mw_trans_fops,
  607. tool_mw_trans_read,
  608. tool_mw_trans_write);
  609. static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
  610. size_t size, loff_t *offp)
  611. {
  612. struct tool_mw *outmw = filep->private_data;
  613. loff_t pos = *offp;
  614. ssize_t ret;
  615. void *buf;
  616. if (outmw->io_base == NULL)
  617. return -EIO;
  618. if (pos >= outmw->size || !size)
  619. return 0;
  620. if (size > outmw->size - pos)
  621. size = outmw->size - pos;
  622. buf = kmalloc(size, GFP_KERNEL);
  623. if (!buf)
  624. return -ENOMEM;
  625. memcpy_fromio(buf, outmw->io_base + pos, size);
  626. ret = copy_to_user(ubuf, buf, size);
  627. if (ret == size) {
  628. ret = -EFAULT;
  629. goto err_free;
  630. }
  631. size -= ret;
  632. *offp = pos + size;
  633. ret = size;
  634. err_free:
  635. kfree(buf);
  636. return ret;
  637. }
  638. static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
  639. size_t size, loff_t *offp)
  640. {
  641. struct tool_mw *outmw = filep->private_data;
  642. ssize_t ret;
  643. loff_t pos = *offp;
  644. void *buf;
  645. if (outmw->io_base == NULL)
  646. return -EIO;
  647. if (pos >= outmw->size || !size)
  648. return 0;
  649. if (size > outmw->size - pos)
  650. size = outmw->size - pos;
  651. buf = kmalloc(size, GFP_KERNEL);
  652. if (!buf)
  653. return -ENOMEM;
  654. ret = copy_from_user(buf, ubuf, size);
  655. if (ret == size) {
  656. ret = -EFAULT;
  657. goto err_free;
  658. }
  659. size -= ret;
  660. *offp = pos + size;
  661. ret = size;
  662. memcpy_toio(outmw->io_base + pos, buf, size);
  663. err_free:
  664. kfree(buf);
  665. return ret;
  666. }
  667. static TOOL_FOPS_RDWR(tool_peer_mw_fops,
  668. tool_peer_mw_read,
  669. tool_peer_mw_write);
  670. static int tool_setup_peer_mw(struct tool_ctx *tc, int pidx, int widx,
  671. u64 req_addr, size_t req_size)
  672. {
  673. struct tool_mw *outmw = &tc->outmws[widx];
  674. resource_size_t map_size;
  675. phys_addr_t map_base;
  676. char buf[TOOL_BUF_LEN];
  677. int ret;
  678. if (outmw->io_base != NULL)
  679. return 0;
  680. ret = ntb_peer_mw_get_addr(tc->ntb, widx, &map_base, &map_size);
  681. if (ret)
  682. return ret;
  683. ret = ntb_peer_mw_set_trans(tc->ntb, pidx, widx, req_addr, req_size);
  684. if (ret)
  685. return ret;
  686. outmw->io_base = ioremap_wc(map_base, map_size);
  687. if (outmw->io_base == NULL) {
  688. ret = -EFAULT;
  689. goto err_clear_trans;
  690. }
  691. outmw->tr_base = req_addr;
  692. outmw->size = req_size;
  693. outmw->pidx = pidx;
  694. snprintf(buf, sizeof(buf), "peer_mw%d", widx);
  695. outmw->dbgfs_file = debugfs_create_file(buf, 0600,
  696. tc->peers[pidx].dbgfs_dir, outmw,
  697. &tool_peer_mw_fops);
  698. return 0;
  699. err_clear_trans:
  700. ntb_peer_mw_clear_trans(tc->ntb, pidx, widx);
  701. return ret;
  702. }
  703. static void tool_free_peer_mw(struct tool_ctx *tc, int widx)
  704. {
  705. struct tool_mw *outmw = &tc->outmws[widx];
  706. debugfs_remove(outmw->dbgfs_file);
  707. if (outmw->io_base != NULL) {
  708. iounmap(tc->outmws[widx].io_base);
  709. ntb_peer_mw_clear_trans(tc->ntb, outmw->pidx, widx);
  710. }
  711. outmw->io_base = NULL;
  712. outmw->tr_base = 0;
  713. outmw->size = 0;
  714. outmw->pidx = -1;
  715. outmw->dbgfs_file = NULL;
  716. }
  717. static ssize_t tool_peer_mw_trans_read(struct file *filep, char __user *ubuf,
  718. size_t size, loff_t *offp)
  719. {
  720. struct tool_mw_wrap *outmw_wrap = filep->private_data;
  721. struct tool_mw *outmw = outmw_wrap->mw;
  722. resource_size_t map_size;
  723. phys_addr_t map_base;
  724. ssize_t off = 0;
  725. size_t buf_size;
  726. char *buf;
  727. int ret;
  728. ret = ntb_peer_mw_get_addr(outmw->tc->ntb, outmw->widx,
  729. &map_base, &map_size);
  730. if (ret)
  731. return ret;
  732. buf_size = min_t(size_t, size, 512);
  733. buf = kmalloc(buf_size, GFP_KERNEL);
  734. if (!buf)
  735. return -ENOMEM;
  736. off += scnprintf(buf + off, buf_size - off,
  737. "Outbound MW: \t%d\n", outmw->widx);
  738. if (outmw->io_base != NULL) {
  739. off += scnprintf(buf + off, buf_size - off,
  740. "Port attached \t%d (%d)\n",
  741. ntb_peer_port_number(outmw->tc->ntb, outmw->pidx),
  742. outmw->pidx);
  743. } else {
  744. off += scnprintf(buf + off, buf_size - off,
  745. "Port attached \t-1 (-1)\n");
  746. }
  747. off += scnprintf(buf + off, buf_size - off,
  748. "Virtual address \t0x%pK\n", outmw->io_base);
  749. off += scnprintf(buf + off, buf_size - off,
  750. "Phys Address \t%pap\n", &map_base);
  751. off += scnprintf(buf + off, buf_size - off,
  752. "Mapping Size \t%pap\n", &map_size);
  753. off += scnprintf(buf + off, buf_size - off,
  754. "Translation Address \t0x%016llx\n", outmw->tr_base);
  755. off += scnprintf(buf + off, buf_size - off,
  756. "Window Size \t%pap\n", &outmw->size);
  757. ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
  758. kfree(buf);
  759. return ret;
  760. }
  761. static ssize_t tool_peer_mw_trans_write(struct file *filep,
  762. const char __user *ubuf,
  763. size_t size, loff_t *offp)
  764. {
  765. struct tool_mw_wrap *outmw_wrap = filep->private_data;
  766. struct tool_mw *outmw = outmw_wrap->mw;
  767. size_t buf_size, wsize;
  768. char buf[TOOL_BUF_LEN];
  769. int ret, n;
  770. u64 addr;
  771. buf_size = min(size, (sizeof(buf) - 1));
  772. if (copy_from_user(buf, ubuf, buf_size))
  773. return -EFAULT;
  774. buf[buf_size] = '\0';
  775. n = sscanf(buf, "%lli:%zi", &addr, &wsize);
  776. if (n != 2)
  777. return -EINVAL;
  778. tool_free_peer_mw(outmw->tc, outmw->widx);
  779. if (wsize) {
  780. ret = tool_setup_peer_mw(outmw->tc, outmw_wrap->pidx,
  781. outmw->widx, addr, wsize);
  782. if (ret)
  783. return ret;
  784. }
  785. return size;
  786. }
  787. static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
  788. tool_peer_mw_trans_read,
  789. tool_peer_mw_trans_write);
  790. static int tool_init_mws(struct tool_ctx *tc)
  791. {
  792. int widx, pidx;
  793. /* Initialize outbound memory windows */
  794. tc->outmw_cnt = ntb_peer_mw_count(tc->ntb);
  795. tc->outmws = devm_kcalloc(&tc->ntb->dev, tc->outmw_cnt,
  796. sizeof(*tc->outmws), GFP_KERNEL);
  797. if (tc->outmws == NULL)
  798. return -ENOMEM;
  799. for (widx = 0; widx < tc->outmw_cnt; widx++) {
  800. tc->outmws[widx].widx = widx;
  801. tc->outmws[widx].pidx = -1;
  802. tc->outmws[widx].tc = tc;
  803. }
  804. /* Initialize inbound memory windows and outbound MWs wrapper */
  805. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  806. tc->peers[pidx].inmw_cnt = ntb_mw_count(tc->ntb, pidx);
  807. tc->peers[pidx].inmws =
  808. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].inmw_cnt,
  809. sizeof(*tc->peers[pidx].inmws), GFP_KERNEL);
  810. if (tc->peers[pidx].inmws == NULL)
  811. return -ENOMEM;
  812. for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
  813. tc->peers[pidx].inmws[widx].widx = widx;
  814. tc->peers[pidx].inmws[widx].pidx = pidx;
  815. tc->peers[pidx].inmws[widx].tc = tc;
  816. }
  817. tc->peers[pidx].outmw_cnt = ntb_peer_mw_count(tc->ntb);
  818. tc->peers[pidx].outmws =
  819. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmw_cnt,
  820. sizeof(*tc->peers[pidx].outmws), GFP_KERNEL);
  821. if (tc->peers[pidx].outmws == NULL)
  822. return -ENOMEM;
  823. for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
  824. tc->peers[pidx].outmws[widx].pidx = pidx;
  825. tc->peers[pidx].outmws[widx].mw = &tc->outmws[widx];
  826. }
  827. }
  828. return 0;
  829. }
  830. static void tool_clear_mws(struct tool_ctx *tc)
  831. {
  832. int widx, pidx;
  833. /* Free outbound memory windows */
  834. for (widx = 0; widx < tc->outmw_cnt; widx++)
  835. tool_free_peer_mw(tc, widx);
  836. /* Free outbound memory windows */
  837. for (pidx = 0; pidx < tc->peer_cnt; pidx++)
  838. for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
  839. tool_free_mw(tc, pidx, widx);
  840. }
  841. /*==============================================================================
  842. * Doorbell read/write methods
  843. *==============================================================================
  844. */
  845. static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
  846. size_t size, loff_t *offp)
  847. {
  848. struct tool_ctx *tc = filep->private_data;
  849. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read);
  850. }
  851. static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
  852. size_t size, loff_t *offp)
  853. {
  854. struct tool_ctx *tc = filep->private_data;
  855. return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set,
  856. tc->ntb->ops->db_clear);
  857. }
  858. static TOOL_FOPS_RDWR(tool_db_fops,
  859. tool_db_read,
  860. tool_db_write);
  861. static ssize_t tool_db_valid_mask_read(struct file *filep, char __user *ubuf,
  862. size_t size, loff_t *offp)
  863. {
  864. struct tool_ctx *tc = filep->private_data;
  865. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_valid_mask);
  866. }
  867. static TOOL_FOPS_RDWR(tool_db_valid_mask_fops,
  868. tool_db_valid_mask_read,
  869. NULL);
  870. static ssize_t tool_db_mask_read(struct file *filep, char __user *ubuf,
  871. size_t size, loff_t *offp)
  872. {
  873. struct tool_ctx *tc = filep->private_data;
  874. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read_mask);
  875. }
  876. static ssize_t tool_db_mask_write(struct file *filep, const char __user *ubuf,
  877. size_t size, loff_t *offp)
  878. {
  879. struct tool_ctx *tc = filep->private_data;
  880. return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set_mask,
  881. tc->ntb->ops->db_clear_mask);
  882. }
  883. static TOOL_FOPS_RDWR(tool_db_mask_fops,
  884. tool_db_mask_read,
  885. tool_db_mask_write);
  886. static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
  887. size_t size, loff_t *offp)
  888. {
  889. struct tool_ctx *tc = filep->private_data;
  890. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->peer_db_read);
  891. }
  892. static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
  893. size_t size, loff_t *offp)
  894. {
  895. struct tool_ctx *tc = filep->private_data;
  896. return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->peer_db_set,
  897. tc->ntb->ops->peer_db_clear);
  898. }
  899. static TOOL_FOPS_RDWR(tool_peer_db_fops,
  900. tool_peer_db_read,
  901. tool_peer_db_write);
  902. static ssize_t tool_peer_db_mask_read(struct file *filep, char __user *ubuf,
  903. size_t size, loff_t *offp)
  904. {
  905. struct tool_ctx *tc = filep->private_data;
  906. return tool_fn_read(tc, ubuf, size, offp,
  907. tc->ntb->ops->peer_db_read_mask);
  908. }
  909. static ssize_t tool_peer_db_mask_write(struct file *filep,
  910. const char __user *ubuf,
  911. size_t size, loff_t *offp)
  912. {
  913. struct tool_ctx *tc = filep->private_data;
  914. return tool_fn_write(tc, ubuf, size, offp,
  915. tc->ntb->ops->peer_db_set_mask,
  916. tc->ntb->ops->peer_db_clear_mask);
  917. }
  918. static TOOL_FOPS_RDWR(tool_peer_db_mask_fops,
  919. tool_peer_db_mask_read,
  920. tool_peer_db_mask_write);
  921. static ssize_t tool_db_event_write(struct file *filep,
  922. const char __user *ubuf,
  923. size_t size, loff_t *offp)
  924. {
  925. struct tool_ctx *tc = filep->private_data;
  926. u64 val;
  927. int ret;
  928. ret = kstrtou64_from_user(ubuf, size, 0, &val);
  929. if (ret)
  930. return ret;
  931. if (wait_event_interruptible(tc->db_wq, ntb_db_read(tc->ntb) == val))
  932. return -ERESTART;
  933. return size;
  934. }
  935. static TOOL_FOPS_RDWR(tool_db_event_fops,
  936. NULL,
  937. tool_db_event_write);
  938. /*==============================================================================
  939. * Scratchpads read/write methods
  940. *==============================================================================
  941. */
  942. static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
  943. size_t size, loff_t *offp)
  944. {
  945. struct tool_spad *spad = filep->private_data;
  946. char buf[TOOL_BUF_LEN];
  947. ssize_t pos;
  948. if (!spad->tc->ntb->ops->spad_read)
  949. return -EINVAL;
  950. pos = scnprintf(buf, sizeof(buf), "%#x\n",
  951. ntb_spad_read(spad->tc->ntb, spad->sidx));
  952. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  953. }
  954. static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
  955. size_t size, loff_t *offp)
  956. {
  957. struct tool_spad *spad = filep->private_data;
  958. u32 val;
  959. int ret;
  960. if (!spad->tc->ntb->ops->spad_write) {
  961. dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
  962. return -EINVAL;
  963. }
  964. ret = kstrtou32_from_user(ubuf, size, 0, &val);
  965. if (ret)
  966. return ret;
  967. ret = ntb_spad_write(spad->tc->ntb, spad->sidx, val);
  968. return ret ?: size;
  969. }
  970. static TOOL_FOPS_RDWR(tool_spad_fops,
  971. tool_spad_read,
  972. tool_spad_write);
  973. static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
  974. size_t size, loff_t *offp)
  975. {
  976. struct tool_spad *spad = filep->private_data;
  977. char buf[TOOL_BUF_LEN];
  978. ssize_t pos;
  979. if (!spad->tc->ntb->ops->peer_spad_read)
  980. return -EINVAL;
  981. pos = scnprintf(buf, sizeof(buf), "%#x\n",
  982. ntb_peer_spad_read(spad->tc->ntb, spad->pidx, spad->sidx));
  983. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  984. }
  985. static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
  986. size_t size, loff_t *offp)
  987. {
  988. struct tool_spad *spad = filep->private_data;
  989. u32 val;
  990. int ret;
  991. if (!spad->tc->ntb->ops->peer_spad_write) {
  992. dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
  993. return -EINVAL;
  994. }
  995. ret = kstrtou32_from_user(ubuf, size, 0, &val);
  996. if (ret)
  997. return ret;
  998. ret = ntb_peer_spad_write(spad->tc->ntb, spad->pidx, spad->sidx, val);
  999. return ret ?: size;
  1000. }
  1001. static TOOL_FOPS_RDWR(tool_peer_spad_fops,
  1002. tool_peer_spad_read,
  1003. tool_peer_spad_write);
  1004. static int tool_init_spads(struct tool_ctx *tc)
  1005. {
  1006. int sidx, pidx;
  1007. /* Initialize inbound scratchpad structures */
  1008. tc->inspad_cnt = ntb_spad_count(tc->ntb);
  1009. tc->inspads = devm_kcalloc(&tc->ntb->dev, tc->inspad_cnt,
  1010. sizeof(*tc->inspads), GFP_KERNEL);
  1011. if (tc->inspads == NULL)
  1012. return -ENOMEM;
  1013. for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
  1014. tc->inspads[sidx].sidx = sidx;
  1015. tc->inspads[sidx].pidx = -1;
  1016. tc->inspads[sidx].tc = tc;
  1017. }
  1018. /* Initialize outbound scratchpad structures */
  1019. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  1020. tc->peers[pidx].outspad_cnt = ntb_spad_count(tc->ntb);
  1021. tc->peers[pidx].outspads =
  1022. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outspad_cnt,
  1023. sizeof(*tc->peers[pidx].outspads), GFP_KERNEL);
  1024. if (tc->peers[pidx].outspads == NULL)
  1025. return -ENOMEM;
  1026. for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
  1027. tc->peers[pidx].outspads[sidx].sidx = sidx;
  1028. tc->peers[pidx].outspads[sidx].pidx = pidx;
  1029. tc->peers[pidx].outspads[sidx].tc = tc;
  1030. }
  1031. }
  1032. return 0;
  1033. }
  1034. /*==============================================================================
  1035. * Messages read/write methods
  1036. *==============================================================================
  1037. */
  1038. static ssize_t tool_inmsg_read(struct file *filep, char __user *ubuf,
  1039. size_t size, loff_t *offp)
  1040. {
  1041. struct tool_msg *msg = filep->private_data;
  1042. char buf[TOOL_BUF_LEN];
  1043. ssize_t pos;
  1044. u32 data;
  1045. int pidx;
  1046. data = ntb_msg_read(msg->tc->ntb, &pidx, msg->midx);
  1047. pos = scnprintf(buf, sizeof(buf), "0x%08x<-%d\n", data, pidx);
  1048. return simple_read_from_buffer(ubuf, size, offp, buf, pos);
  1049. }
  1050. static TOOL_FOPS_RDWR(tool_inmsg_fops,
  1051. tool_inmsg_read,
  1052. NULL);
  1053. static ssize_t tool_outmsg_write(struct file *filep,
  1054. const char __user *ubuf,
  1055. size_t size, loff_t *offp)
  1056. {
  1057. struct tool_msg *msg = filep->private_data;
  1058. u32 val;
  1059. int ret;
  1060. ret = kstrtou32_from_user(ubuf, size, 0, &val);
  1061. if (ret)
  1062. return ret;
  1063. ret = ntb_peer_msg_write(msg->tc->ntb, msg->pidx, msg->midx, val);
  1064. return ret ? : size;
  1065. }
  1066. static TOOL_FOPS_RDWR(tool_outmsg_fops,
  1067. NULL,
  1068. tool_outmsg_write);
  1069. static ssize_t tool_msg_sts_read(struct file *filep, char __user *ubuf,
  1070. size_t size, loff_t *offp)
  1071. {
  1072. struct tool_ctx *tc = filep->private_data;
  1073. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_read_sts);
  1074. }
  1075. static ssize_t tool_msg_sts_write(struct file *filep, const char __user *ubuf,
  1076. size_t size, loff_t *offp)
  1077. {
  1078. struct tool_ctx *tc = filep->private_data;
  1079. return tool_fn_write(tc, ubuf, size, offp, NULL,
  1080. tc->ntb->ops->msg_clear_sts);
  1081. }
  1082. static TOOL_FOPS_RDWR(tool_msg_sts_fops,
  1083. tool_msg_sts_read,
  1084. tool_msg_sts_write);
  1085. static ssize_t tool_msg_inbits_read(struct file *filep, char __user *ubuf,
  1086. size_t size, loff_t *offp)
  1087. {
  1088. struct tool_ctx *tc = filep->private_data;
  1089. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_inbits);
  1090. }
  1091. static TOOL_FOPS_RDWR(tool_msg_inbits_fops,
  1092. tool_msg_inbits_read,
  1093. NULL);
  1094. static ssize_t tool_msg_outbits_read(struct file *filep, char __user *ubuf,
  1095. size_t size, loff_t *offp)
  1096. {
  1097. struct tool_ctx *tc = filep->private_data;
  1098. return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_outbits);
  1099. }
  1100. static TOOL_FOPS_RDWR(tool_msg_outbits_fops,
  1101. tool_msg_outbits_read,
  1102. NULL);
  1103. static ssize_t tool_msg_mask_write(struct file *filep, const char __user *ubuf,
  1104. size_t size, loff_t *offp)
  1105. {
  1106. struct tool_ctx *tc = filep->private_data;
  1107. return tool_fn_write(tc, ubuf, size, offp,
  1108. tc->ntb->ops->msg_set_mask,
  1109. tc->ntb->ops->msg_clear_mask);
  1110. }
  1111. static TOOL_FOPS_RDWR(tool_msg_mask_fops,
  1112. NULL,
  1113. tool_msg_mask_write);
  1114. static ssize_t tool_msg_event_write(struct file *filep,
  1115. const char __user *ubuf,
  1116. size_t size, loff_t *offp)
  1117. {
  1118. struct tool_ctx *tc = filep->private_data;
  1119. u64 val;
  1120. int ret;
  1121. ret = kstrtou64_from_user(ubuf, size, 0, &val);
  1122. if (ret)
  1123. return ret;
  1124. if (wait_event_interruptible(tc->msg_wq,
  1125. ntb_msg_read_sts(tc->ntb) == val))
  1126. return -ERESTART;
  1127. return size;
  1128. }
  1129. static TOOL_FOPS_RDWR(tool_msg_event_fops,
  1130. NULL,
  1131. tool_msg_event_write);
  1132. static int tool_init_msgs(struct tool_ctx *tc)
  1133. {
  1134. int midx, pidx;
  1135. /* Initialize inbound message structures */
  1136. tc->inmsg_cnt = ntb_msg_count(tc->ntb);
  1137. tc->inmsgs = devm_kcalloc(&tc->ntb->dev, tc->inmsg_cnt,
  1138. sizeof(*tc->inmsgs), GFP_KERNEL);
  1139. if (tc->inmsgs == NULL)
  1140. return -ENOMEM;
  1141. for (midx = 0; midx < tc->inmsg_cnt; midx++) {
  1142. tc->inmsgs[midx].midx = midx;
  1143. tc->inmsgs[midx].pidx = -1;
  1144. tc->inmsgs[midx].tc = tc;
  1145. }
  1146. /* Initialize outbound message structures */
  1147. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  1148. tc->peers[pidx].outmsg_cnt = ntb_msg_count(tc->ntb);
  1149. tc->peers[pidx].outmsgs =
  1150. devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmsg_cnt,
  1151. sizeof(*tc->peers[pidx].outmsgs), GFP_KERNEL);
  1152. if (tc->peers[pidx].outmsgs == NULL)
  1153. return -ENOMEM;
  1154. for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
  1155. tc->peers[pidx].outmsgs[midx].midx = midx;
  1156. tc->peers[pidx].outmsgs[midx].pidx = pidx;
  1157. tc->peers[pidx].outmsgs[midx].tc = tc;
  1158. }
  1159. }
  1160. return 0;
  1161. }
  1162. /*==============================================================================
  1163. * Initialization methods
  1164. *==============================================================================
  1165. */
  1166. static struct tool_ctx *tool_create_data(struct ntb_dev *ntb)
  1167. {
  1168. struct tool_ctx *tc;
  1169. tc = devm_kzalloc(&ntb->dev, sizeof(*tc), GFP_KERNEL);
  1170. if (tc == NULL)
  1171. return ERR_PTR(-ENOMEM);
  1172. tc->ntb = ntb;
  1173. init_waitqueue_head(&tc->link_wq);
  1174. init_waitqueue_head(&tc->db_wq);
  1175. init_waitqueue_head(&tc->msg_wq);
  1176. if (ntb_db_is_unsafe(ntb))
  1177. dev_dbg(&ntb->dev, "doorbell is unsafe\n");
  1178. if (ntb_spad_is_unsafe(ntb))
  1179. dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
  1180. return tc;
  1181. }
  1182. static void tool_clear_data(struct tool_ctx *tc)
  1183. {
  1184. wake_up(&tc->link_wq);
  1185. wake_up(&tc->db_wq);
  1186. wake_up(&tc->msg_wq);
  1187. }
  1188. static int tool_init_ntb(struct tool_ctx *tc)
  1189. {
  1190. return ntb_set_ctx(tc->ntb, tc, &tool_ops);
  1191. }
  1192. static void tool_clear_ntb(struct tool_ctx *tc)
  1193. {
  1194. ntb_clear_ctx(tc->ntb);
  1195. ntb_link_disable(tc->ntb);
  1196. }
  1197. static void tool_setup_dbgfs(struct tool_ctx *tc)
  1198. {
  1199. int pidx, widx, sidx, midx;
  1200. char buf[TOOL_BUF_LEN];
  1201. /* This modules is useless without dbgfs... */
  1202. if (!tool_dbgfs_topdir) {
  1203. tc->dbgfs_dir = NULL;
  1204. return;
  1205. }
  1206. tc->dbgfs_dir = debugfs_create_dir(dev_name(&tc->ntb->dev),
  1207. tool_dbgfs_topdir);
  1208. debugfs_create_file("port", 0600, tc->dbgfs_dir,
  1209. tc, &tool_port_fops);
  1210. debugfs_create_file("link", 0600, tc->dbgfs_dir,
  1211. tc, &tool_link_fops);
  1212. debugfs_create_file("db", 0600, tc->dbgfs_dir,
  1213. tc, &tool_db_fops);
  1214. debugfs_create_file("db_valid_mask", 0600, tc->dbgfs_dir,
  1215. tc, &tool_db_valid_mask_fops);
  1216. debugfs_create_file("db_mask", 0600, tc->dbgfs_dir,
  1217. tc, &tool_db_mask_fops);
  1218. debugfs_create_file("db_event", 0600, tc->dbgfs_dir,
  1219. tc, &tool_db_event_fops);
  1220. debugfs_create_file("peer_db", 0600, tc->dbgfs_dir,
  1221. tc, &tool_peer_db_fops);
  1222. debugfs_create_file("peer_db_mask", 0600, tc->dbgfs_dir,
  1223. tc, &tool_peer_db_mask_fops);
  1224. if (tc->inspad_cnt != 0) {
  1225. for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
  1226. snprintf(buf, sizeof(buf), "spad%d", sidx);
  1227. debugfs_create_file(buf, 0600, tc->dbgfs_dir,
  1228. &tc->inspads[sidx], &tool_spad_fops);
  1229. }
  1230. }
  1231. if (tc->inmsg_cnt != 0) {
  1232. for (midx = 0; midx < tc->inmsg_cnt; midx++) {
  1233. snprintf(buf, sizeof(buf), "msg%d", midx);
  1234. debugfs_create_file(buf, 0600, tc->dbgfs_dir,
  1235. &tc->inmsgs[midx], &tool_inmsg_fops);
  1236. }
  1237. debugfs_create_file("msg_sts", 0600, tc->dbgfs_dir,
  1238. tc, &tool_msg_sts_fops);
  1239. debugfs_create_file("msg_inbits", 0600, tc->dbgfs_dir,
  1240. tc, &tool_msg_inbits_fops);
  1241. debugfs_create_file("msg_outbits", 0600, tc->dbgfs_dir,
  1242. tc, &tool_msg_outbits_fops);
  1243. debugfs_create_file("msg_mask", 0600, tc->dbgfs_dir,
  1244. tc, &tool_msg_mask_fops);
  1245. debugfs_create_file("msg_event", 0600, tc->dbgfs_dir,
  1246. tc, &tool_msg_event_fops);
  1247. }
  1248. for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
  1249. snprintf(buf, sizeof(buf), "peer%d", pidx);
  1250. tc->peers[pidx].dbgfs_dir =
  1251. debugfs_create_dir(buf, tc->dbgfs_dir);
  1252. debugfs_create_file("port", 0600,
  1253. tc->peers[pidx].dbgfs_dir,
  1254. &tc->peers[pidx], &tool_peer_port_fops);
  1255. debugfs_create_file("link", 0200,
  1256. tc->peers[pidx].dbgfs_dir,
  1257. &tc->peers[pidx], &tool_peer_link_fops);
  1258. debugfs_create_file("link_event", 0200,
  1259. tc->peers[pidx].dbgfs_dir,
  1260. &tc->peers[pidx], &tool_peer_link_event_fops);
  1261. for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
  1262. snprintf(buf, sizeof(buf), "mw_trans%d", widx);
  1263. debugfs_create_file(buf, 0600,
  1264. tc->peers[pidx].dbgfs_dir,
  1265. &tc->peers[pidx].inmws[widx],
  1266. &tool_mw_trans_fops);
  1267. }
  1268. for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
  1269. snprintf(buf, sizeof(buf), "peer_mw_trans%d", widx);
  1270. debugfs_create_file(buf, 0600,
  1271. tc->peers[pidx].dbgfs_dir,
  1272. &tc->peers[pidx].outmws[widx],
  1273. &tool_peer_mw_trans_fops);
  1274. }
  1275. for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
  1276. snprintf(buf, sizeof(buf), "spad%d", sidx);
  1277. debugfs_create_file(buf, 0600,
  1278. tc->peers[pidx].dbgfs_dir,
  1279. &tc->peers[pidx].outspads[sidx],
  1280. &tool_peer_spad_fops);
  1281. }
  1282. for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
  1283. snprintf(buf, sizeof(buf), "msg%d", midx);
  1284. debugfs_create_file(buf, 0600,
  1285. tc->peers[pidx].dbgfs_dir,
  1286. &tc->peers[pidx].outmsgs[midx],
  1287. &tool_outmsg_fops);
  1288. }
  1289. }
  1290. }
  1291. static void tool_clear_dbgfs(struct tool_ctx *tc)
  1292. {
  1293. debugfs_remove_recursive(tc->dbgfs_dir);
  1294. }
  1295. static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
  1296. {
  1297. struct tool_ctx *tc;
  1298. int ret;
  1299. tc = tool_create_data(ntb);
  1300. if (IS_ERR(tc))
  1301. return PTR_ERR(tc);
  1302. ret = tool_init_peers(tc);
  1303. if (ret != 0)
  1304. goto err_clear_data;
  1305. ret = tool_init_mws(tc);
  1306. if (ret != 0)
  1307. goto err_clear_data;
  1308. ret = tool_init_spads(tc);
  1309. if (ret != 0)
  1310. goto err_clear_mws;
  1311. ret = tool_init_msgs(tc);
  1312. if (ret != 0)
  1313. goto err_clear_mws;
  1314. ret = tool_init_ntb(tc);
  1315. if (ret != 0)
  1316. goto err_clear_mws;
  1317. tool_setup_dbgfs(tc);
  1318. return 0;
  1319. err_clear_mws:
  1320. tool_clear_mws(tc);
  1321. err_clear_data:
  1322. tool_clear_data(tc);
  1323. return ret;
  1324. }
  1325. static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
  1326. {
  1327. struct tool_ctx *tc = ntb->ctx;
  1328. tool_clear_dbgfs(tc);
  1329. tool_clear_ntb(tc);
  1330. tool_clear_mws(tc);
  1331. tool_clear_data(tc);
  1332. }
  1333. static struct ntb_client tool_client = {
  1334. .ops = {
  1335. .probe = tool_probe,
  1336. .remove = tool_remove,
  1337. }
  1338. };
  1339. static int __init tool_init(void)
  1340. {
  1341. int ret;
  1342. if (debugfs_initialized())
  1343. tool_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1344. ret = ntb_register_client(&tool_client);
  1345. if (ret)
  1346. debugfs_remove_recursive(tool_dbgfs_topdir);
  1347. return ret;
  1348. }
  1349. module_init(tool_init);
  1350. static void __exit tool_exit(void)
  1351. {
  1352. ntb_unregister_client(&tool_client);
  1353. debugfs_remove_recursive(tool_dbgfs_topdir);
  1354. }
  1355. module_exit(tool_exit);