ntb_tool.c 42 KB

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